Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make fx.Transformer.get_attr call tracer to preserve node.meta #95245

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.create_proxy("get_attr", target, args, kwargs)

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