You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
groupby does not seem to work for non-numerics #6640
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
%%time# This one is comparatively fast and produces the same result.fromoperatorimportadddefincr(tot, _):
returntot+1result=js.foldby(key='name',
binop=incr,
initial=0,
combine=add,
combine_initial=0).compute()
print(sorted(result))
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:
There was a recent commit that changed how groupby works on bags.
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!!)
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.
@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?
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.
@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.
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.
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.
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.
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?
Activity
lazarillo commentedon Sep 14, 2020
In fact, I just tried the next portion of the tutorial, and it gave incorrect results:
groupbyResult:
foldbyResult:
foldbyworks as expected,groupbydoes not.jsignell commentedon Sep 16, 2020
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.
lazarillo commentedon Sep 16, 2020
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:
groupbyworks onbags.groupbydoes not have an automated unit tests around it to capture this aspect.groupby's speed/efficiency that for some reason does not treat hashed objects the same as "raw" objects.hash(obj) == objyieldsTruelike withhash(4) == 4, whereas this isFalsefor most hashable things. I suspect thegroupbyworks properly for cases wherehash(obj) == obj, but not for other cases.dsaxton commentedon Sep 16, 2020
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
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
jsignell commentedon Sep 16, 2020
@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?
mrocklin commentedon Sep 16, 2020
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.
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.
lazarillo commentedon Sep 16, 2020
@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.
lazarillo commentedon Sep 16, 2020
@jsignell Oh, now I understand: you want me to write the unit test, not the fix. OK, I'm on it. 👍
itamarst commentedon Sep 16, 2020
Sounds like setting
PYTHONHASHSEEDfor the processes in the process pool should do the trick? E.g. doingos.environ["PYTHONHASHSEED"] = 123before launching the mulitprocessing pool.lazarillo commentedon Sep 16, 2020
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.
mrocklin commentedon Sep 16, 2020
Right. This sounds familiar. I think that with forkserver this didn't matter because the seed was generated once and then copied with fork.
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.environto make sure that things are consistent between them.Looking at multiprocessing.py it looks like there is already an
initialize_worker_processfunction 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.itamarst commentedon Sep 22, 2020
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?
itamarst commentedon Sep 22, 2020
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?
mrocklin commentedon Sep 22, 2020
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?
itamarst commentedon Sep 22, 2020
Sure.
Fix dask#6640.
Adds test_bag.py tests to assist with issue dask#6640.
Adds test_bag.py tests to assist with issue dask#6640, formatted with…
Fix dask#6640.
Fix dask#6640 by making sure subprocesses have a consistent hash.
Make sure subprocesses have a consistent hash for bag groupby (#6660)
Make sure subprocesses have a consistent hash for bag groupby (dask#6660