Skip to content

Commit

Permalink
Make fx.Transformer.get_attr call tracer to preserve node.meta
Browse files Browse the repository at this point in the history
  • Loading branch information
ydwu4 committed Feb 21, 2023
1 parent c6d8d10 commit 5e6489e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
25 changes: 25 additions & 0 deletions test/test_fx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,31 @@ def forward(self, x):
stack_list = list(mod_stack.items())
self.assertEqual(stack_list, expected_stack)

def test_transformer_preserves_nn_module_stack_for_get_attr(self):
class M(torch.nn.Module):
def __init__(self):
super().__init__()
self.weight = torch.nn.Parameter(torch.ones(1, 1))

def forward(self, x):
return self.weight + x

tracer = torch.fx.Tracer()
graph = tracer.trace(M())
gm = GraphModule(tracer.root, graph)
for node in gm.graph.nodes:
if node.op == 'get_attr':
node.meta["nn_module_stack"] = "self"
node.meta["stack_trace"] = "stack_trace"
node.meta["source_fn"] = "source_fn"
new_gm = Transformer(gm).transform()
for node in new_gm.graph.nodes:
if node.op == 'get_attr':
self.assertEqual(node.meta["nn_module_stack"], "self")
self.assertEqual(node.meta["stack_trace"], "stack_trace")
self.assertEqual(node.meta["source_fn"], "source_fn")


def test_interpreter(self):
class MyModule(torch.nn.Module):
def __init__(self):
Expand Down
2 changes: 1 addition & 1 deletion torch/fx/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ def get_attr(self, target : 'Target', args : Tuple[Argument, ...], kwargs : Dict
kwargs (Dict): Dict of keyword arguments for this invocation
"""
assert isinstance(target, str)
return Proxy(self.new_graph.get_attr(target), self.tracer)
return self.tracer.getattr(target, self.fetch_attr(target), {})

@compatibility(is_backward_compatible=True)
def call_module(self, target : 'Target', args : Tuple[Argument, ...], kwargs : Dict[str, Any]) -> Any:
Expand Down

0 comments on commit 5e6489e

Please sign in to comment.