Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloudpickle compatibility #759

Closed
samimia-swks opened this issue Jun 5, 2023 · 3 comments · Fixed by #783
Closed

Cloudpickle compatibility #759

samimia-swks opened this issue Jun 5, 2023 · 3 comments · Fixed by #783
Labels
type-feature Feature request
Milestone

Comments

@samimia-swks
Copy link

samimia-swks commented Jun 5, 2023

params 1.13.0
cloudpickle 2.2.1
python 3.11
Ubuntu 22.04

The docs recommend Cloudpickle as a way to pickle Parameterized classes with lambda functions, but even the most basic Parameterized object (with or without lambdas) cannot be pickled with cloudpickle :

import param
import cloudpickle

class MyParams(param.Parameterized):
    a = param.Integer()
    b = param.Integer()
    # sum_pull = Integer(lambda : p.a + p.b)

p = MyParams()
pkl = cloudpickle.dumps(p)
p2 = cloudpickle.loads(pkl)

Output

File [/param/parameterized.py:1685), in Parameters.__setstate__(self, state)
   1682 def __setstate__(self, state):
   1683     # Set old parameters state on Parameterized._parameters_state
   1684     self_or_cls = state.get('self', state.get('cls'))
-> 1685     for k in self_or_cls._parameters_state:
   1686         key = '_'+k
   1687         if key in state:

AttributeError: 'NoneType' object has no attribute '_parameters_state'
@maximlt maximlt added the type-feature Feature request label Jun 19, 2023
@maximlt maximlt added this to the Wishlist milestone Jun 19, 2023
@maximlt
Copy link
Member

maximlt commented Jun 19, 2023

Hi @samimia-swks,

cloudpickle is indeed mentioned in the docs, albeit in a pretty light way 🙃

lambdas cannot easily be pickled for saving and restoring state (though see cloudpickle for an alternative to pickle that does support lambdas)

Unfortunately it seems that Param doesn't support it out of the box, even for very simple cases like the one you tried above. It'd be nice to add support for cloudpickle (and fix the original support for pickle if it's been broken in some ways), contributions in the area of serializing/deserializing Param objects are welcomed :) !

@samimia-swks
Copy link
Author

samimia-swks commented Jun 22, 2023

Hi @maximlt,

I was able to figure out that cloudpickle (or dill) support was broken by #386 which is between version 1.9.3 and 1.10.0.

If I remove the offending lines of code, which are the definition setstate() method for the Parameters class, I can get cloudpickle to pickle a Parametrized class with both lambdas and @param.depends() decorators even on the latest 1.13.0 version :

    def __setstate__(self, state):
        # Set old parameters state on Parameterized._parameters_state
        self_or_cls = state.get('self', state.get('cls'))
        for k in self_or_cls._parameters_state:
            key = '_'+k
            if key in state:
                self_or_cls._parameters_state[k] = state.pop(key)
        for k, v in state.items():
            setattr(self, k, v)

From what I understand, this is a method that's called by pickle after unpickeling. However, I can't tell what it is doing because things still seem to work without it. My knowledge of python and this project is not nearly good enough to figure this out in the short terrm :)

Perhaps @philippjfr, who authored this code can clarify.

@maximlt
Copy link
Member

maximlt commented Jun 22, 2023

Thanks @samimia-swks I'm sure that's going to be very helpful!

maximlt added a commit that referenced this issue Jul 3, 2023
@maximlt maximlt modified the milestones: Wishlist, 2.0 Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature Feature request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants