Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_state_pytorch_alternating_nested_with_modules(self, alternating_nn_module_with_state):
obj = alternating_nn_module_with_state(from_config=True)
t1 = obj.model.linear.weight
t2 = obj.model.linear.bias
expected_state = {'model.leaf.x': 2019, 'model.linear.weight': t1, 'model.linear.bias': t2}
root_source_code = dill.source.getsource(RootTorch)
intermediate_source_code = dill.source.getsource(IntermediateStatefulTorch)
leaf_source_code = dill.source.getsource(BasicStateful)
expected_metadata = OrderedDict({FLAMBE_DIRECTORIES_KEY: set(['', 'model', 'model.leaf']), 'keep_vars': False, '': {VERSION_KEY: '0.0.0', FLAMBE_CLASS_KEY: 'RootTorch', FLAMBE_SOURCE_KEY: root_source_code, FLAMBE_CONFIG_KEY: "!RootTorch {}\n"},
'model': {VERSION_KEY: '0.0.0', FLAMBE_CLASS_KEY: 'IntermediateStatefulTorch', FLAMBE_SOURCE_KEY: intermediate_source_code, 'version': 1}, # TODO add config back: FLAMBE_CONFIG_KEY: "!IntermediateStatefulTorch {}\n"
'model.leaf': {VERSION_KEY: '0.0.0', FLAMBE_CLASS_KEY: 'BasicStateful', FLAMBE_SOURCE_KEY: leaf_source_code}, # TODO add config back: FLAMBE_CONFIG_KEY: "!BasicStateful {}\n"
'model.linear': {'version': 1}})
state = obj.get_state()
check_mapping_equivalence(state._metadata, expected_metadata)
check_mapping_equivalence(expected_metadata, state._metadata)
check_mapping_equivalence(state, expected_state)
check_mapping_equivalence(expected_state, state)
def test_getsource():
assert getsource(f) == 'f = lambda x: x**2\n'
assert getsource(g) == 'def g(x): return f(x) - x\n'
assert getsource(h) == 'def h(x):\n def g(x): return x\n return g(x) - x\n'
assert getname(f) == 'f'
assert getname(g) == 'g'
assert getname(h) == 'h'
assert _wrap(f)(4) == 16
assert _wrap(g)(4) == 12
assert _wrap(h)(4) == 0
assert getname(Foo) == 'Foo'
assert getname(Bar) == 'Bar'
assert getsource(Bar) == 'class Bar:\n pass\n'
assert getsource(Foo) == 'class Foo(object):\n def bar(self, x):\n return x*x+x\n'
#XXX: add getsource for _foo, _bar
def test_getsource():
assert getsource(f) == 'f = lambda x: x**2\n'
assert getsource(g) == 'def g(x): return f(x) - x\n'
assert getsource(h) == 'def h(x):\n def g(x): return x\n return g(x) - x\n'
assert getname(f) == 'f'
assert getname(g) == 'g'
assert getname(h) == 'h'
assert _wrap(f)(4) == 16
assert _wrap(g)(4) == 12
assert _wrap(h)(4) == 0
assert getname(Foo) == 'Foo'
assert getname(Bar) == 'Bar'
assert getsource(Bar) == 'class Bar:\n pass\n'
assert getsource(Foo) == 'class Foo(object):\n def bar(self, x):\n return x*x+x\n'
#XXX: add getsource for _foo, _bar
def test_getsource():
assert getsource(f) == 'f = lambda x: x**2\n'
assert getsource(g) == 'def g(x): return f(x) - x\n'
assert getsource(h) == 'def h(x):\n def g(x): return x\n return g(x) - x\n'
assert getname(f) == 'f'
assert getname(g) == 'g'
assert getname(h) == 'h'
assert _wrap(f)(4) == 16
assert _wrap(g)(4) == 12
assert _wrap(h)(4) == 0
assert getname(Foo) == 'Foo'
assert getname(Bar) == 'Bar'
assert getsource(Bar) == 'class Bar:\n pass\n'
assert getsource(Foo) == 'class Foo(object):\n def bar(self, x):\n return x*x+x\n'
#XXX: add getsource for _foo, _bar
def test_getsource():
assert getsource(f) == 'f = lambda x: x**2\n'
assert getsource(g) == 'def g(x): return f(x) - x\n'
assert getsource(h) == 'def h(x):\n def g(x): return x\n return g(x) - x\n'
assert getname(f) == 'f'
assert getname(g) == 'g'
assert getname(h) == 'h'
assert _wrap(f)(4) == 16
assert _wrap(g)(4) == 12
assert _wrap(h)(4) == 0
assert getname(Foo) == 'Foo'
assert getname(Bar) == 'Bar'
assert getsource(Bar) == 'class Bar:\n pass\n'
assert getsource(Foo) == 'class Foo(object):\n def bar(self, x):\n return x*x+x\n'
#XXX: add getsource for _foo, _bar
def serialize(self, data):
name = data.__name__
body = dill.source.getsource(data)
x = codecs.encode(pickle.dumps((name, body)), 'base64').decode()
return self.identifier + x