Skip to content

Replace dill with cloudpickle - #865

Merged
mrocklin merged 7 commits into
dask:masterfrom
PedroMDuarte:replace_dill_with_cloudpickle
Dec 10, 2015
Merged

mrocklin merged 7 commits into
dask:masterfrom
PedroMDuarte:replace_dill_with_cloudpickle

Conversation

@PedroMDuarte

Copy link
Copy Markdown
Contributor

WIP for #847

  • updated docs, travis, and conda.recipe to use cloudpickle.
  • left dill in the travis installs because test_dataframe_series_are_dillable uses it
  • centralized the choice between dill and cloudpickle at dask/multiprocessing.py. Other tests where dill was imported now use multiprocessing._dumps and multiprocessing._loads.
  • added a test for pickling functions with __globals__: dask/tests/test_multiprocessing.py::test_pickle_globals.
    • The test fails with dill and passes with cloudpickle
  • the switch to cloudpickle introduced regressions in some tests for Python <= 3.3
    • dask/bag/core.py::dask.bag.core.Bag.distinct FAILED (failed in Python:2.7 only)
    • dask/bag/tests/test_bag.py::test_fold FAILED
    • dask/bag/tests/test_bag.py::test_distinct FAILED
    • dask/bag/tests/test_bag.py::test_from_filenames_large FAILED
    • dask/bag/tests/test_bag.py::test_from_filenames_encoding FAILED
    • dask/bag/tests/test_bag.py::test_from_filenames_large_gzip FAILED
  • the error seen in the FAILED tests above was always: PicklingError: Could not pickle object as excessively deep recursion required.
  • I introduced a somewhat ugly hack in dask/multiprocessing.py that fixes the recursion errors seen so far: if dumps fails the recursion limit is increased by a factor of 2 and cloudpickle tries again.

I appreciate any feedback on this WIP. Thanks!

Comment thread dask/multiprocessing.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what objects were we hitting a recursion limit? Ideally nothing we do is this heavily nested.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrocklin I am investigating a bit more about what is going on in the test cases that hit the recursion limit.

Running the tests on my laptop (on py27 and py35) I cannot reproduce the recursionlimit errors, so I am temporarily adding more info to the exceptions such that I can get a better idea of what is happening with these tests in travis. I'll post a comment when I have more info.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have narrowed this down a little bit. For example in dask/bag/tests/test_bag.py::test_fold FAILED the object that is failing to pickle is

['bag-33', (<built-in function reduce>, <method 'union' of 'set' objects>, [('bag-32', 0), ('bag-32', 1), ('bag-32', 2)]), {('bag-32', 0): set([0, 1]), ('bag-32', 1): set([2, 3]), ('bag-32', 2): set([4])}, <AutoProxy[Queue] object, typeid 'Queue' at 0x4497110>, <function _process_get_id at 0x34eb410>, False]

I went ahead and tried pickling the items in this list, and found that the one that causes the recursion error is:

(<built-in function reduce>, <method 'union' of 'set' objects>, [('bag-32', 0), ('bag-32', 1), ('bag-32', 2)])

I did the same for the other failing tests, and found that the objects that hit the recursion limit are:

  • dask/bag/tests/test_bag.py::test_fold FAILED:
    (<built-in function reduce>, <method 'union' of 'set' objects>, [('bag-32', 0), ('bag-32', 1), ('bag-32', 2)])
  • dask/bag/tests/test_bag.py::test_distinct FAILED:
    (<built-in function apply>, <method 'union' of 'set' objects>, [('bag-34', 2), ('bag-34', 1), ('bag-34', 0)])
  • dask/bag/tests/test_bag.py::test_from_filenames_large FAILED:
    (<method 'decode' of 'str' objects>, (<function textblock at 0x2157e60>, '/tmp/tmpju03wI.', 200, 300, None), 'utf-8')
  • dask/bag/tests/test_bag.py::test_from_filenames_encoding FAILED:
    (<method 'decode' of 'str' objects>, (<function textblock at 0x2157e60>, '/tmp/tmpTsLX9O.', 0, 100, None), 'gb18030')
  • dask/bag/tests/test_bag.py::test_from_filenames_large_gzip FAILED:
    (<method 'decode' of 'str' objects>, (<function textblock at 0x2157e60>, '/tmp/tmpKXpz29.gz', 500, 600, 'gzip'), 'utf-8')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's quite odd. None of those strike me as particularly troublesome.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the probem occurs for method descriptor objects:

Python 2.7.10 |Continuum Analytics, Inc.| (default, Oct 19 2015, 18:04:42) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import cloudpickle, pickle

>>> cloudpickle.dumps(set.union)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/pmd/anaconda3/envs/python2/lib/python2.7/site-packages/cloudpickle/cloudpickle.py", line 602, in dumps
    cp.dump(obj)
  File "/home/pmd/anaconda3/envs/python2/lib/python2.7/site-packages/cloudpickle/cloudpickle.py", line 111, in dump
    raise pickle.PicklingError(msg)
pickle.PicklingError: Could not pickle object as excessively deep recursion required.

>>> cloudpickle.dumps(str.decode)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/pmd/anaconda3/envs/python2/lib/python2.7/site-packages/cloudpickle/cloudpickle.py", line 602, in dumps
    cp.dump(obj)
  File "/home/pmd/anaconda3/envs/python2/lib/python2.7/site-packages/cloudpickle/cloudpickle.py", line 111, in dump
    raise pickle.PicklingError(msg)
pickle.PicklingError: Could not pickle object as excessively deep recursion required.

>>> pickle.dumps(set.union)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/pmd/anaconda3/envs/python2/lib/python2.7/pickle.py", line 1374, in dumps
    Pickler(file, protocol).dump(obj)
  File "/home/pmd/anaconda3/envs/python2/lib/python2.7/pickle.py", line 224, in dump
    self.save(obj)
  File "/home/pmd/anaconda3/envs/python2/lib/python2.7/pickle.py", line 306, in save
    rv = reduce(self.proto)
  File "/home/pmd/anaconda3/envs/python2/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
    raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle method_descriptor objects

>>> def f(x):
...     return set.union(x)
... 
>>> cloudpickle.dumps(f)
'\x80\x02ccloudpickle.cloudpickle\n_fill_function\nq\x00(ccloudpickle.cloudpickle\n_make_skel_func\nq\x01ccloudpickle.cloudpickle\n_builtin_type\nq\x02U\x08CodeTypeq\x03\x85q\x04Rq\x05(K\x01K\x01K\x02KCU\rt\x00\x00j\x01\x00|\x00\x00\x83\x01\x00Sq\x06N\x85q\x07U\x03setq\x08U\x05unionq\t\x86q\nU\x01xq\x0b\x85q\x0cU\x07<stdin>q\rU\x01fq\x0eK\x01U\x02\x00\x01q\x0f))tq\x10Rq\x11]q\x12}q\x13\x87q\x14Rq\x15}q\x16N}q\x17tR.'

>>> def g(x):
...     return str.decode(x)
... 
>>> cloudpickle.dumps(g)
'\x80\x02ccloudpickle.cloudpickle\n_fill_function\nq\x00(ccloudpickle.cloudpickle\n_make_skel_func\nq\x01ccloudpickle.cloudpickle\n_builtin_type\nq\x02U\x08CodeTypeq\x03\x85q\x04Rq\x05(K\x01K\x01K\x02KCU\rt\x00\x00j\x01\x00|\x00\x00\x83\x01\x00Sq\x06N\x85q\x07U\x03strq\x08U\x06decodeq\t\x86q\nU\x01xq\x0b\x85q\x0cU\x07<stdin>q\rU\x01gq\x0eK\x01U\x02\x00\x01q\x0f))tq\x10Rq\x11]q\x12}q\x13\x87q\x14Rq\x15}q\x16N}q\x17tR.'
>>> 

@mrocklin

mrocklin commented Dec 6, 2015

Copy link
Copy Markdown
Member

At first pass this looks pretty good to me.

I wonder if @eribean or @jni can verify that this resolves issues that they've seen in the past with dill.

@PedroMDuarte

Copy link
Copy Markdown
Contributor Author

I found that the switch to cloudpickle can solve an issue when running on windows. On my linux laptop I found that the global np issue is handled ok by both dill and cloudpickle

Win32 python 3.5 with dill:
Python 3.5.0 |Continuum Analytics, Inc.| (default, Dec  1 2015, 11:46:22) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import dask
>>> import numpy as np
>>> def f(x):
...     return np.sin(x) + np.cos(x)
...
>>> import multiprocessing
>>> p = multiprocessing.Pool()
>>> from dask.multiprocessing import dill_apply_async
>>> result = dill_apply_async(p.apply_async, f, args=[0.])
>>> result.get()
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
  File "C:\Users\pedromdu\AppData\Local\Continuum\Anaconda3\envs\python3\lib\multiprocessing\pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
  File "C:\Users\pedromdu\AppData\Local\Continuum\Anaconda3\envs\python3\lib\site-packages\toolz\functoolz.py", line 219, in __call_
_
    return self._partial(*args, **kwargs)
  File "C:\Users\pedromdu\AppData\Local\Continuum\Anaconda3\envs\python3\lib\site-packages\dask\multiprocessing.py", line 68, in app
ly_func
    return func(*args, **kwds)
  File "<stdin>", line 2, in f
NameError: name 'np' is not defined
"""
Win32 python 3.5 with cloudpickle:
Python 3.5.0 |Continuum Analytics, Inc.| (default, Dec  1 2015, 11:46:22) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import dask
>>> import numpy as np
>>> def f(x):
...     return np.sin(x) + np.cos(x)
...
>>> import multiprocessing
>>> p = multiprocessing.Pool()
>>> from dask.multiprocessing import pickle_apply_async
>>> result = pickle_apply_async(p.apply_async, f, args=[0.])
>>> result.get()
1.0
Linux python 3.5 with dill:
Python 3.5.0 |Continuum Analytics, Inc.| (default, Oct 19 2015, 21:57:25) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dask
>>> import numpy as np
>>> def f(x):
...     return np.sin(x) + np.cos(x)
... 
>>> import multiprocessing
>>> p = multiprocessing.Pool()
>>> from dask.multiprocessing import dill_apply_async
>>> result = dill_apply_async(p.apply_async, f, args=[0.])
>>> result.get()
1.0
>>> 
Linux python 3.5 with cloudpickle:
Python 3.5.0 |Continuum Analytics, Inc.| (default, Oct 19 2015, 21:57:25) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dask
>>> import numpy as np
>>> def f(x):
...     return np.sin(x) + np.cos(x)
... 
>>> import multiprocessing
>>> p = multiprocessing.Pool()
>>> from dask.multiprocessing import pickle_apply_async
>>> result = pickle_apply_async(p.apply_async, f, args=[0.])
>>> result.get()
1.0

@PedroMDuarte

Copy link
Copy Markdown
Contributor Author

It looks like the the 2*recursionlimit hack is too flaky and definitely not going to work. On first trial it passed on travis, but now it has caused a Segentation fault. Will look into this further.

@mrocklin

mrocklin commented Dec 7, 2015

Copy link
Copy Markdown
Member

Whoa, that's a surprising limitation. Hrm, we could consider choosing the serialization library based on the python version and system platform.

@PedroMDuarte

Copy link
Copy Markdown
Contributor Author

I may be onto something. Using the copyreg module we can register pickling and unpickling functions for <type 'method_descriptor'>. We can then drop in dill to do the work only for those offending objects, and let cloudpickle handle what it is good for. This can be done only for the python versions that need the fix. I will play around with this idea in the next couple of days.

@mrocklin

mrocklin commented Dec 7, 2015

Copy link
Copy Markdown
Member

Very cool.

@PedroMDuarte

Copy link
Copy Markdown
Contributor Author

Looks like I may have found a better solution to the method_descriptor problem. I borrowed some code from multiprocessing.forking, where the ForkingPickler is defined precisely for this purpose. This has been a good learning experience.

@mrocklin

mrocklin commented Dec 8, 2015

Copy link
Copy Markdown
Member

Oh cool. That is indeed much nicer

@PedroMDuarte

Copy link
Copy Markdown
Contributor Author

@mrocklin Should I squash all commits in to a single one in this case as well? Let me know if you think there is anything pending for this request to be complete.

@mrocklin

mrocklin commented Dec 9, 2015

Copy link
Copy Markdown
Member

Looks good to me. Merging in 12 hours if no comments

@hcarvalhoalves

Copy link
Copy Markdown

Excited about this, cloudpickle has been robust in my experience.

mrocklin added a commit that referenced this pull request Dec 10, 2015
@mrocklin
mrocklin merged commit 924eeb3 into dask:master Dec 10, 2015
@mrocklin

Copy link
Copy Markdown
Member

This is in. Thanks @PedroMDuarte !

@sinhrks sinhrks added this to the 0.7.6 milestone Jan 7, 2016
@PedroMDuarte
PedroMDuarte deleted the replace_dill_with_cloudpickle branch April 2, 2016 18:09
Comment thread dask/multiprocessing.py
def _dumps(x):
return cloudpickle.dumps(x, protocol=pickle.HIGHEST_PROTOCOL)

_loads = pickle.loads

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this was intentionally meant to be pickle or if this should actually be cloudpickle. Assumed the latter and added PR ( #3180 ) to change it. However if I assumed incorrectly, please feel free to let me know. Would be good to understand why if this was intentional.

@mrocklin

mrocklin commented Feb 20, 2018 via email

Copy link
Copy Markdown
Member

@jakirkham

Copy link
Copy Markdown
Member

Not sure I follow. We are already using cloudpickle with dumps where this presumably matters. We just are not using it for loads (despite saying we are in the docs).

@mrocklin

Copy link
Copy Markdown
Member
In [1]: import pickle, cloudpickle

In [2]: pickle.loads is cloudpickle.loads
Out[2]: True

@jakirkham

Copy link
Copy Markdown
Member

I see. Still wouldn't it be better to be explicit? Just in case cloudpickle ever changed the behavior.

@mrocklin

mrocklin commented Feb 20, 2018 via email

Copy link
Copy Markdown
Member

phofl added a commit to phofl/dask that referenced this pull request Dec 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants