question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot pickle linen Modules

See original GitHub issue

I am using 0.3.4 and I am getting an error when trying to pickle flax modules, specifically Dense seems to be the problem but other might have similar issues.

Problem you have encountered:

from flax import linen
import pickle

with open("model.pkl", "wb") as f:
    model = pickle.dump(linen.Dense(10), f)

Traceback (most recent call last): File “test.py”, line 8, in <module> model = pickle.dump(linen.Dense(10), f) AttributeError: Can’t pickle local object ‘variance_scaling.<locals>.init’

While the previous is solved with cloudpickle, this other code doesn’t work:

import cloudpickle
from flax import linen
import pickle

class IndentityFlax(linen.Module):
    def __call__(self, x):
        return x

with open("mlp.pkl", "wb") as f:
    cloudpickle.dump(IndentityFlax(), f)

Traceback (most recent call last): File “test.py”, line 25, in <module> cloudpickle.dump(IndentityFlax(), f) File “/data/cristian/elegy/.venv/lib/python3.8/site-packages/cloudpickle/cloudpickle_fast.py”, line 55, in dump CloudPickler( File “/data/cristian/elegy/.venv/lib/python3.8/site-packages/cloudpickle/cloudpickle_fast.py”, line 563, in dump return Pickler.dump(self, obj) TypeError: cannot pickle ‘_thread._local’ object

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jheekcommented, Aug 4, 2021

@cgarciae I found the issue. cloudpickle will not serialize functions that are part of a library but it does serialize other globals. I guess this is a python limitation (missing qualname I suppose?). We use the threadlocal inside a decorator function which will get serialized. All we have to do is factor out the body of the decorator into a method so cloudpickle doens’t serialize its closure variables. I’m working on a PR

0reactions
jheekcommented, Aug 6, 2021

+1 to the comments by @levskaya

Especially remote code execution is really overlooked. This is of course intended for RPC protocols but we really don’t want a protocol with remote code execution as the standard way of distributing Flax models. I’m also curious how the PyTorch worlds thinks about this. The remote code execution problems is not mentioned in the torch.save/load docs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

flax.linen.module - Read the Docs
name: the name of the variable. value: the new value of the variable. Returns: """ if self.scope is None: raise ValueError("Can't access variables...
Read more >
flax/module.py at main · google/flax - linen - GitHub
# We don't want to return anything for python copy / pickle methods. if name in _UNDEFINED_COPY_PICKLE_METHODS: raise AttributeError().
Read more >
Python: can't pickle module objects error - Stack Overflow
Python's inability to pickle module objects is the real problem. Is there a good reason? I don't think so. Having module objects unpicklable ......
Read more >
Models - Hugging Face
Module subclass. Use it as a regular Flax linen Module and refer to the Flax documentation for all matter related to general usage...
Read more >
[D] What JAX NN library to use? : r/MachineLearning - Reddit
Here's another example of a footgun: in (IIRC) Flax, you can't do ... There are "known" tradeoffs to using pytree modules like Equinox...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found