-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Add support for param mutation under inference mode #159661
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
base: main
Are you sure you want to change the base?
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/159661
Note: Links to docs will display an error until the docs builds have been completed. ⏳ 3 Pending, 1 Unrelated FailureAs of commit 1412ce7 with merge base bfc873d ( FLAKY - The following job failed but was likely due to flakiness present on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This pull request was exported from Phabricator. Differential Revision: D79460136 |
@@ -460,6 +460,7 @@ def create_graph_signature( | |||
named_buffers=buffer_names, | |||
num_user_inputs=num_user_args, | |||
num_user_outputs=num_user_fw_outs, | |||
trace_joint=trace_joint, |
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.
cc: @ezyang does this look ok in terms of your refactoring work?
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.
The change looks reversible enough, but I can't really say I understand why you need yet another flag here. Additionally in your tests you are not even tracing out the joint!
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.
This is because in the graph signature, we need to distinguish between joint IR vs inference IR. In the joint IR case, we can't have mutations on parameters.
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.
One thing I'll say is that for a generic "this works when torch.compile works" export frontend, these cases also need to be accounted for.
@@ -1430,9 +1430,11 @@ def __torch_function__( | |||
torch.amp.autocast_mode._exit_autocast, | |||
]: | |||
node.meta["val"] = None | |||
# For autocast, the python APIs run so we don't have to run them again | |||
# here. | |||
if func is torch._C._set_grad_enabled: |
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.
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.
er what is going on 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.
We are actually running the state change instead of just proxy-ing so that param mutation under no-grad still works.
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.
# Don't actually run the function! We just want to trace the calls
# into a graph. We don't actually want to change global autograd state.
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.
So, at the very least, pre-dispatch tracing should not change the state after it runs. If there is only one single torch.set_grad_enabled(False) call in the graph, after one does the pre-dispatch trace, it should not be off.
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.
Yep i wrapped it in ctx manager under export.
da3eb83
to
76593ac
Compare
Summary: In HF model rwkv, we have parameter mutation under inference mode which should be safe. This PR does multiple things to make sure it works: 1. We execute global autograd mutation while tracing so that we can actually trace through parameter inplace mutation 2. Add support for parameter mutation under inference mode in AOTAutograd 3. Add support for parameter mutation under inference mode in export. Test Plan: test Rollback Plan: Differential Revision: D79460136
This pull request was exported from Phabricator. Differential Revision: D79460136 |
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.
your tests don't look happy
test/export/test_export.py
Outdated
self.assertTrue("parameter" in val.values()) | ||
|
||
with self.assertRaisesRegex(RuntimeError, "leaf"): | ||
ep.module()(torch.rand(4, 4)) |
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.
Is the error that leaf receives a mutation? Does it work if we set the parameters.require_grad to be False?
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.
Yeah we can do that but we don't know when it is supposed to be True or False which depends on whether we are under torch.no_grad or something else.
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.
the requires_grad attribute of tensors doesn't change with with torch.no_grad i think. One way to test is to manually flip the attribute to false and see if eager works.
It seems weird that eager doesn't work but we can export successfully lol, which seems a divergent behavior and is hard to explain.
Summary: In HF model rwkv, we have parameter mutation under inference mode which should be safe. This PR does multiple things to make sure it works: 1. We execute global autograd mutation while tracing so that we can actually trace through parameter inplace mutation 2. Add support for parameter mutation under inference mode in AOTAutograd 3. Add support for parameter mutation under inference mode in export. Test Plan: test Rollback Plan: Differential Revision: D79460136
76593ac
to
098450f
Compare
This pull request was exported from Phabricator. Differential Revision: D79460136 |
098450f
to
b9bb196
Compare
Summary: In HF model rwkv, we have parameter mutation under inference mode which should be safe. This PR does multiple things to make sure it works: 1. We execute global autograd mutation while tracing so that we can actually trace through parameter inplace mutation 2. Add support for parameter mutation under inference mode in AOTAutograd 3. Add support for parameter mutation under inference mode in export. Test Plan: test Rollback Plan: Differential Revision: D79460136
This pull request was exported from Phabricator. Differential Revision: D79460136 |
@@ -1241,6 +1242,15 @@ def serialize_output_spec(self, spec: ep.OutputSpec) -> OutputSpec: | |||
buffer_name=spec.target, | |||
) | |||
) | |||
elif spec.kind == ep.OutputKind.PARAMETER_MUTATION: |
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.
I didn't see the logic of handling de-serializing a parameter mutation OutputSpec. Why this doesn't trigger an error for the serde test?
torch/_export/serde/schema.py
Outdated
@@ -359,6 +365,7 @@ class OutputSpec(_Union): | |||
gradient_to_user_input: Annotated[GradientToUserInputSpec, 50] | |||
user_input_mutation: Annotated[UserInputMutationSpec, 60] | |||
token: Annotated[OutputTokenSpec, 70] | |||
parameter_mutation: Annotated[Optional[ParameterMutationSpec], 80] |
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.
After a second thought, I think a union type is probably already optional lol meaning that when we can always create the dataclass (using the new dataclass definition) from json of an old artifact. Should we remove the Optional?
This is BC-compitable but it breaks FC. I remember seeing internal pipelines sometimes rely on FC not sure if it's fixed or not. So probably we should check with internal folks to warn them or do more testing also cc @yiming0416 @SherlockNoMad
test/export/test_export.py
Outdated
self.assertTrue("parameter" in val.values()) | ||
|
||
with self.assertRaisesRegex(RuntimeError, "leaf"): | ||
ep.module()(torch.rand(4, 4)) |
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.
the requires_grad attribute of tensors doesn't change with with torch.no_grad i think. One way to test is to manually flip the attribute to false and see if eager works.
It seems weird that eager doesn't work but we can export successfully lol, which seems a divergent behavior and is hard to explain.
b9bb196
to
e55fb54
Compare
Summary: In HF model rwkv, we have parameter mutation under inference mode which should be safe. This PR does multiple things to make sure it works: 1. We execute global autograd mutation while tracing so that we can actually trace through parameter inplace mutation 2. Add support for parameter mutation under inference mode in AOTAutograd 3. Add support for parameter mutation under inference mode in export. Test Plan: test Rollback Plan: Differential Revision: D79460136
This pull request was exported from Phabricator. Differential Revision: D79460136 |
with ( | ||
ctx, | ||
override_getattribute_for_subclasses(flat_args), | ||
_maybe_restore_grad_state(), |
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.
Is it accurate that parameter mutation is supported in export (for all the flags/mode)? We should document all the semantic of mutations, e.g. input, buffer, parameters mutations somewhere. Right now I don't have a clear mental model for them.
e55fb54
to
5644567
Compare
Summary: In HF model rwkv, we have parameter mutation under inference mode which should be safe. This PR does multiple things to make sure it works: 1. We execute global autograd mutation while tracing so that we can actually trace through parameter inplace mutation 2. Add support for parameter mutation under inference mode in AOTAutograd 3. Add support for parameter mutation under inference mode in export. Test Plan: test Rollback Plan: Reviewed By: ydwu4 Differential Revision: D79460136
Summary: In HF model rwkv, we have parameter mutation under inference mode which should be safe. This PR does multiple things to make sure it works: 1. We execute global autograd mutation while tracing so that we can actually trace through parameter inplace mutation 2. Add support for parameter mutation under inference mode in AOTAutograd 3. Add support for parameter mutation under inference mode in export. Test Plan: test Rollback Plan: Reviewed By: ydwu4 Differential Revision: D79460136
5644567
to
fbe846d
Compare
Summary: Pull Request resolved: pytorch#159661 In HF model rwkv, we have parameter mutation under inference mode which should be safe. This PR does multiple things to make sure it works: 1. We execute global autograd mutation while tracing so that we can actually trace through parameter inplace mutation 2. Add support for parameter mutation under inference mode in AOTAutograd 3. Add support for parameter mutation under inference mode in export. Test Plan: test Rollback Plan: Reviewed By: ydwu4 Differential Revision: D79460136
This pull request was exported from Phabricator. Differential Revision: D79460136 |
1 similar comment
This pull request was exported from Phabricator. Differential Revision: D79460136 |
fbe846d
to
1412ce7
Compare
Summary:
In HF model rwkv, we have parameter mutation under inference mode which should be safe. This PR does multiple things to make sure it works:
Test Plan:
test
Rollback Plan:
Differential Revision: D79460136
cc @ezyang @SherlockNoMad @EikanWang @jgong5 @wenzhe-nrv