Skip to content

Commit

Permalink
[fx] improve args mutation error (#51175)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #51175

gives a suggestion about how to deal with immutable args/kwargs list

Test Plan: Imported from OSS

Reviewed By: jamesr66a

Differential Revision: D26093478

Pulled By: zdevito

fbshipit-source-id: 832631c125561c3b343539e887c047f185060252
  • Loading branch information
zdevito authored and facebook-github-bot committed Jan 28, 2021
1 parent 4288f08 commit 33d5180
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 6 additions & 0 deletions test/test_fx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,12 @@ def forward(self, x):
i += 1
self.assertEqual(i, 3)

def test_no_mutation(self):
from torch.fx.immutable_collections import immutable_list
x = immutable_list([3, 4])
with self.assertRaisesRegex(NotImplementedError, "new_args"):
x[0] = 4


def run_getitem_target():
from torch.fx.symbolic_trace import _wrapped_methods_to_patch
Expand Down
10 changes: 9 additions & 1 deletion torch/fx/immutable_collections.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@

_help_mutation = """\
If you are attempting to modify the kwargs or args of a torch.fx.Node object,
instead create a new copy of it and assign the copy to the node:
new_args = ... # copy and mutate args
node.args = new_args
"""

def _no_mutation(self, *args, **kwargs):
raise NotImplementedError(f"'{type(self).__name__}' object does not support mutation")
raise NotImplementedError(f"'{type(self).__name__}' object does not support mutation. {_help_mutation}")

def _create_immutable_container(base, mutable_functions):
container = type('immutable_' + base.__name__, (base,), {})
Expand Down

0 comments on commit 33d5180

Please sign in to comment.