Skip to content

Commit

Permalink
Avoid infinite recursion in BaseVar when unpickling. (#221)
Browse files Browse the repository at this point in the history
This commit also adds a unit test to check that pickling and unpickling
of a simple object (objax.nn.Linear) functions as expected.
  • Loading branch information
bytbox authored and Scott Lawrence committed May 12, 2021
1 parent c4785ff commit dbf20f0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions objax/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ def __bool__(self):
'To check if variable is `None` use `is None` or `is not None` instead.')

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

@property
Expand Down
36 changes: 36 additions & 0 deletions tests/pickle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Unittests for Convolution Layer."""

import unittest

import jax.numpy as jn

import objax

import pickle

class TestPickle(unittest.TestCase):
def test_on_linear(self):
"""
Create a Linear module, try to pickle, try to unpickle.
"""
lin = objax.nn.Linear(2,3)
pickled = pickle.dumps(lin)
lin_ = pickle.loads(pickled)
self.assertTrue(jn.all(lin.w == lin_.w))

if __name__ == '__main__':
unittest.main()

0 comments on commit dbf20f0

Please sign in to comment.