Skip to content

groupby does not seem to work for non-numerics #6640

Closed

Description

@lazarillo

What happened:

Following along tutorial bag, I played around with it for better understanding:

b = db.from_sequence(list(range(10)))
b.groupby(lambda x: 'even' if (x % 2) == 0 else 'odd').compute()

Result:

[('odd', [3, 7]), ('even', [0, 2, 4, 6, 8]), ('odd', [1, 5, 9])]

What you expected to happen:

I expected:

[('odd', [1, 3, 5, 7, 9]), ('even', [0, 2, 4, 6, 8])]

Minimal Complete Verifiable Example:

Shown above.

Anything else we need to know?:

I tried to think of what could be causing this difference, so that I can understand what to watch out for, but could not manage:

  • Maybe it was because the return object needs to be the same (pointing to the same object, not just the same value).
    • But the string literal should be the same object. Just to be certain, I added variables even and odd, defined globally, and still got the strange result.
  • Maybe it was due to partitioning.
    • First, this seems strange, since groupby API specifically states that it makes the expensive traversal across all partitions.
    • Second, this is non-ideal: a major advantage is to allow me, as the user, to needn't worry about partition details.
    • Third, why does it work fine when the return value (the groupby key) is a number?

Environment:

  • Dask version: '2.20.0'
  • Python version: '3.8.5'
  • Operating System: 'Mac OS 10.15.6'
  • Install method (conda, pip, source): conda, specifically using the environment.yml as specified in the tutorial: conda env create -f binder/environment.yml

Activity

  1. lazarillo commented on Sep 14, 2020

    @lazarillo
    Author

    In fact, I just tried the next portion of the tutorial, and it gave incorrect results:

    groupby

    %%time
    # Warning, this one takes a while...
    result = js.groupby(lambda item: item['name']).starmap(lambda k, v: (k, len(v))).compute()
    print(sorted(result))

    Result:

    [('Alice', 251), ('Alice', 251), ('Alice', 273), ('Alice', 273), ('Bob', 120), ('Bob', 120), ('Bob', 130), ('Bob', 130), ('Charlie', 278), ('Charlie', 281), ('Charlie', 302), ('Charlie', 303), ('Dan', 239), ('Dan', 240), ('Dan', 258), ('Dan', 259), ('Edith', 159), ('Edith', 160), ('Edith', 170), ('Edith', 174), ('Frank', 216), ('Frank', 234), ('Frank', 449), ('George', 264), ('George', 264), ('George', 286), ('George', 286), ('Hannah', 275), ('Hannah', 275), ('Hannah', 298), ('Hannah', 299), ('Ingrid', 196), ('Ingrid', 197), ('Ingrid', 213), ('Ingrid', 213), ('Jerry', 205), ('Jerry', 208), ('Jerry', 222), ('Jerry', 223), ('Kevin', 131), ('Kevin', 132), ('Kevin', 142), ('Kevin', 143), ('Laura', 216), ('Laura', 216), ('Laura', 234), ('Laura', 234), ('Michael', 372), ('Michael', 372), ('Michael', 403), ('Michael', 403), ('Norbert', 240), ('Norbert', 240), ('Norbert', 260), ('Norbert', 260), ('Oliver', 288), ('Oliver', 288), ('Oliver', 312), ('Oliver', 312), ('Patricia', 297), ('Patricia', 298), ('Patricia', 324), ('Patricia', 324), ('Quinn', 192), ('Quinn', 192), ('Quinn', 208), ('Quinn', 208), ('Ray', 180), ('Ray', 180), ('Ray', 195), ('Ray', 195), ('Sarah', 192), ('Sarah', 192), ('Sarah', 208), ('Sarah', 208), ('Tim', 315), ('Tim', 319), ('Tim', 342), ('Tim', 344), ('Ursula', 252), ('Ursula', 252), ('Ursula', 273), ('Ursula', 273), ('Victor', 226), ('Victor', 227), ('Victor', 246), ('Victor', 247), ('Wendy', 174), ('Wendy', 174), ('Wendy', 187), ('Wendy', 191), ('Xavier', 180), ('Xavier', 180), ('Xavier', 195), ('Xavier', 195), ('Yvonne', 228), ('Yvonne', 228), ('Yvonne', 247), ('Yvonne', 247), ('Zelda', 240), ('Zelda', 240), ('Zelda', 260), ('Zelda', 260)]
    CPU times: user 1.29 s, sys: 102 ms, total: 1.39 s
    Wall time: 6.39 s

    foldby

    %%time
    # This one is comparatively fast and produces the same result.
    from operator import add
    def incr(tot, _):
        return tot+1
    
    result = js.foldby(key='name', 
                       binop=incr, 
                       initial=0, 
                       combine=add, 
                       combine_initial=0).compute()
    print(sorted(result))

    Result:

    [('Alice', 1048), ('Bob', 500), ('Charlie', 1164), ('Dan', 996), ('Edith', 663), ('Frank', 899), ('George', 1100), ('Hannah', 1147), ('Ingrid', 819), ('Jerry', 858), ('Kevin', 548), ('Laura', 900), ('Michael', 1550), ('Norbert', 1000), ('Oliver', 1200), ('Patricia', 1243), ('Quinn', 800), ('Ray', 750), ('Sarah', 800), ('Tim', 1320), ('Ursula', 1050), ('Victor', 946), ('Wendy', 726), ('Xavier', 750), ('Yvonne', 950), ('Zelda', 1000)]
    CPU times: user 184 ms, sys: 20 ms, total: 204 ms
    Wall time: 1.56 s

    foldby works as expected, groupby does not. ☹️

  2. jsignell commented on Sep 16, 2020

    @jsignell
    Member

    Thanks for opening such a well-described issue. I can produce but I don't know quite what it going on either. Maybe @jrbourbeau has some insight.

  3. lazarillo commented on Sep 16, 2020

    @lazarillo
    Author

    Thanks, @jsignell !

    I did not dig into the actual code because, well, I'm still just getting my head around Dask itself.

    But it seems to me that something along the following must have happened:

    1. There was a recent commit that changed how groupby works on bags.
    2. groupby does not have an automated unit tests around it to capture this aspect.
      • (I do not mean this as an insult... I know how hard 100% coverage is!!)
    3. And this change probably involved an aspect of increasing groupby's speed/efficiency that for some reason does not treat hashed objects the same as "raw" objects.
      • By "hashed vs raw", I mean that in Python, somtimes hash(obj) == obj yields True like with hash(4) == 4, whereas this is False for most hashable things. I suspect the groupby works properly for cases where hash(obj) == obj, but not for other cases.
  4. dsaxton commented on Sep 16, 2020

    @dsaxton
    Contributor

    Did a bisect that turned up this commit:

    ae42c72 is the first bad commit
    commit ae42c72
    Author: Itamar Turner-Trauring [email protected]
    Date: Mon Apr 27 11:12:25 2020 -0500

    Switch default multiprocessing context to "spawn" (#4003)
    
    * Switch default context to "spawn".
    

    dask/multiprocessing.py | 18 +++++++-----------
    dask/tests/test_multiprocessing.py | 26 ++++++++++++++++----------
    docs/source/scheduler-overview.rst | 3 ++-
    3 files changed, 25 insertions(+), 22 deletions(-)

    PR #4003

  5. jsignell commented on Sep 16, 2020

    @jsignell
    Member

    @lazarillo your diagnosis seems right. And thanks @dsaxton for the bisect. I guess the first step would be to right a test for this behavior. @lazarillo are you interested in opening a pull request?

  6. mrocklin commented on Sep 16, 2020

    @mrocklin
    Member

    cc @itamarst due to the git bisect.

    A process-dependent hash could be the problem here. I thought that we had this solved at one point.

    $ python -c "print(hash('odd'))"
    5157549014634029155
    $ python -c "print(hash('odd'))"
    -8690531012311993190
    $ python -c "print(hash('odd'))"
    -2372920119262523031
    $ python -c "print(hash('odd'))"
    4809590348173657841
    

    We need to find a way to use seed hash consistently, or we need to find a better hashing function that is still fast and handles arbitrary Python objects.

    Again I think that we may have solved this problem at one point. Grepping through logs might be informative.

  7. lazarillo commented on Sep 16, 2020

    @lazarillo
    Author

    @jsignell Sure, I'm happy to help out. But what's the process of review and/or unit testing? I don't know Dask well. I'm happy to help out, but a little worried that I might fix 1 thing and break 2 more. ;)

    I'll look through it now and see how well I can isolate the issue and figure it out.

  8. lazarillo commented on Sep 16, 2020

    @lazarillo
    Author

    @jsignell Oh, now I understand: you want me to write the unit test, not the fix. OK, I'm on it. 👍

  9. itamarst commented on Sep 16, 2020

    @itamarst
    Contributor

    Sounds like setting PYTHONHASHSEED for the processes in the process pool should do the trick? E.g. doing os.environ["PYTHONHASHSEED"] = 123 before launching the mulitprocessing pool.

  10. lazarillo commented on Sep 16, 2020

    @lazarillo
    Author

    I just added two unit tests ( #6648 )to capture this, one checking for cases where hash(key) == key (this currently works fine), and another where hash(key) != key... this is the part that does not currently work.

    I also highlighted with TODO several areas in the dask/bag/core.py code where there was no coverage. (I used the coverage tool.) I was originally hoping maybe I would have time to find where the issue was in the core.py file, but I did not have time. I thought I would at least flag the potential / likely areas for investigation.

  11. mrocklin commented on Sep 16, 2020

    @mrocklin
    Member

    Sounds like setting PYTHONHASHSEED for the processes in the process pool should do the trick? E.g. doing os.environ["PYTHONHASHSEED"] = 123 before launching the mulitprocessing pool.

    Right. This sounds familiar. I think that with forkserver this didn't matter because the seed was generated once and then copied with fork.

    doing os.environ["PYTHONHASHSEED"] = 123 before launching the mulitprocessing pool.

    I'm curious if this works. I'm also curious if we can capture the current seed in the main process and copy that over to os.environ to make sure that things are consistent between them.

    Looking at multiprocessing.py it looks like there is already an initialize_worker_process function that sets a seed for numpy. I'm not sure if we can modify the seed for hash at this stage, but that's also an option.

  12. itamarst commented on Sep 22, 2020

    @itamarst
    Contributor

    https://stackoverflow.com/a/41088757/6214034 suggests it's not possible to get the current one, though that may be wrong.

    What does Distributed do about this issue, which I assume would occur there too?

  13. itamarst commented on Sep 22, 2020

    @itamarst
    Contributor

    Here's the code: https://github.com/python/cpython/blob/e81f6e687d0f04a45f2389d0b43fafd6d8491624/Python/bootstrap_hash.c#L551

    Looks like what you'd actually need to extract and set is the hash secret, which isn't really possible.

    Does the hash seed need to match the parent process?

  14. mrocklin commented on Sep 22, 2020

    @mrocklin
    Member

    Does the hash seed need to match the parent process?

    I suppose not, so maybe it's enough to add a line to dask/multiprocessing.py::initialize_worker_process? I would be in favor of using a consistent seed across all Dask runs, unless the system/user has already provided a PYTHONHASHSEED variable.

    @itamarst do you have any interest in trying this out?

  15. itamarst commented on Sep 22, 2020

    @itamarst
    Contributor

    Sure.

  16. added 5 commits that reference this issue on Sep 22, 2020
    a329173
    b9241c5
    96228ac
    c0cff78
    d26a055
  17. added a commit that references this issue on Sep 29, 2020
    f20eded
  18. added a commit that references this issue on Oct 29, 2020
    084bc53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions