Skip to content

fix stackoverflow when loading pickled module #3286

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

Merged
merged 1 commit into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions flax/linen/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,8 @@ def __set_name__(self, *args, **kwargs):
self.wrapped.__set_name__(*args, **kwargs)

def __getattr__(self, name):
if 'wrapped' not in vars(self):
raise AttributeError()
return getattr(self.wrapped, name)

return _DescriptorWrapper(descriptor)
Expand Down
26 changes: 26 additions & 0 deletions tests/linen/linen_module_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import inspect
import operator
import sys
from tempfile import TemporaryDirectory
from typing import (
Any,
Callable,
Expand Down Expand Up @@ -2399,6 +2400,31 @@ def record_interceptor(f, args, kwargs, context):
self.assertIs(called[0], bar)
self.assertIs(called[1], foo)

def test_cloudpickle_module(self):
from cloudpickle import cloudpickle_fast

class NNModuleWithProperty(nn.Module):
a: int
b: str

@property
def my_property(self):
return self.b * self.a

m = NNModuleWithProperty(a=2, b='ok')

with TemporaryDirectory() as tmpdir:
filename = f'{tmpdir}/module.pkl'
with open(filename, 'wb') as f:
cloudpickle_fast.dump(m, f)

with open(filename, 'rb') as f:
obj_loaded = cloudpickle_fast.load(f)

self.assertEqual(obj_loaded.a, 2)
self.assertEqual(obj_loaded.b, 'ok')
self.assertEqual(obj_loaded.my_property, 'okok')


class LeakTests(absltest.TestCase):

Expand Down