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

add_hook_to_module and remove_hook_from_module compatibility with fx.GraphModule #2369

Merged
merged 2 commits into from
Jan 25, 2024

Conversation

fxmarty
Copy link
Contributor

@fxmarty fxmarty commented Jan 22, 2024

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.

@HuggingFaceDocBuilderDev

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.

Copy link
Member

@SunMarc SunMarc left a 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.

Comment on lines 369 to 390
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()
Copy link
Member

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 ?

Copy link
Contributor Author

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!

@fxmarty fxmarty merged commit 7644a02 into main Jan 25, 2024
25 checks passed
@fxmarty fxmarty deleted the fix-add-remove-hook-fx-graphmodule branch January 25, 2024 09:53
Copy link
Collaborator

@muellerzr muellerzr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants