Skip to content

Commit

Permalink
amend
Browse files Browse the repository at this point in the history
  • Loading branch information
vmoens committed Jan 5, 2024
1 parent e21d553 commit 0efacb7
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions test/test_nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -3282,10 +3282,7 @@ def test_static(self, module_name, input_name, as_module, inplace):
params = TensorDict.from_module(module, as_module=as_module)
if inplace:
params = params.clone()
params0 = params.clone().apply(
lambda t, p: nn.Parameter(t * 0) if isinstance(p, nn.Parameter) else t * 0,
params,
)
params0 = params.clone().zero_()
y = module(*x)
params0.to_module(module, inplace=inplace)
y0 = module(*x)
Expand Down Expand Up @@ -3324,24 +3321,23 @@ def test_cm(self, module_name, input_name, as_module, inplace):
module = getattr(self, module_name)
x = getattr(self, input_name)
params = TensorDict.from_module(module, as_module=as_module)
params0 = params.clone().apply(
lambda t, p: nn.Parameter(t * 0) if isinstance(p, nn.Parameter) else t * 0,
params,
)
params0 = params.clone().zero_()
y = module(*x)
with params0.to_module(module, inplace=inplace):
y0 = module(*x)
assert (params0 == TensorDict.from_module(module)).all()

# check identities
for p1, p2 in zip(
TensorDict.from_module(module).values(True, True),
params0.values(True, True),
):
if inplace:
assert p1 is not p2
else:
assert p1 is p2
if as_module:
# if as_module=False, params0 is not made of parameters anymore
assert (params0 == TensorDict.from_module(module)).all()

# check identities
for p1, p2 in zip(
TensorDict.from_module(module).values(True, True),
params0.values(True, True),
):
if inplace:
assert p1 is not p2
else:
assert p1 is p2

y1 = module(*x)
torch.testing.assert_close(y, y1)
Expand Down

0 comments on commit 0efacb7

Please sign in to comment.