-
Notifications
You must be signed in to change notification settings - Fork 899
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
add_hook_to_module
and remove_hook_from_module
compatibility with fx.GraphModule
#2369
Conversation
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM ! Just a nit.
tests/test_hooks.py
Outdated
linear2_node = None | ||
for node in graph_model.graph.nodes: | ||
if node.name == "linear2": | ||
linear2_node = node | ||
|
||
self.assertTrue(linear2_node is not None) | ||
|
||
graph_model.graph.inserting_after(linear2_node) | ||
new_node = graph_model.graph.create_node( | ||
op="call_function", target=torch.sigmoid, args=(linear2_node,), name="relu" | ||
) | ||
|
||
output_node = None | ||
for node in graph_model.graph.nodes: | ||
if node.name == "output": | ||
output_node = node | ||
self.assertTrue(output_node is not None) | ||
|
||
output_node.replace_input_with(linear2_node, new_node) | ||
|
||
graph_model.graph.lint() | ||
graph_model.recompile() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you describe a little bit what we are doing here ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a comment, thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
It appears to be illegal to modify the forward method of a
GraphModuleImpl
instance, PyTorch rather favors modifying the class method (https://github.com/pytorch/pytorch/blob/c3780010a58a84920335296ee5f091a0db18259f/torch/fx/graph_module.py#L708-L723) though it is unclear why the behavior would be different.Just modifying the class instance forward method results in a frozen forward call - despite modifying the graph (as in the test).
@Giuseppe5 and @nickfraser found this bug. cc @jamesr66a as this was quite a tricky one.