Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tugsbayasgalan
Copy link
Contributor

@tugsbayasgalan tugsbayasgalan commented Aug 1, 2025

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

cc @ezyang @SherlockNoMad @EikanWang @jgong5 @wenzhe-nrv

Copy link

pytorch-bot bot commented Aug 1, 2025

🔗 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.

✅ You can merge normally! (1 Unrelated Failure)

As of commit 1412ce7 with merge base bfc873d (image):

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.

@facebook-github-bot
Copy link
Contributor

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,
Copy link
Contributor Author

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?

Copy link
Contributor

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!

Copy link
Contributor Author

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.

Copy link
Contributor

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:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

cc: @zou3519, @bdhirsh This is behavioral change from before.

Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

@tugsbayasgalan tugsbayasgalan requested a review from Chillee as a code owner August 3, 2025 21:17
tugsbayasgalan added a commit to tugsbayasgalan/pytorch that referenced this pull request Aug 3, 2025
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
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D79460136

Copy link
Contributor

@zou3519 zou3519 left a 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

self.assertTrue("parameter" in val.values())

with self.assertRaisesRegex(RuntimeError, "leaf"):
ep.module()(torch.rand(4, 4))
Copy link
Contributor

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?

Copy link
Contributor Author

@tugsbayasgalan tugsbayasgalan Aug 4, 2025

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.

Copy link
Contributor

@ydwu4 ydwu4 Aug 6, 2025

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.

tugsbayasgalan added a commit to tugsbayasgalan/pytorch that referenced this pull request Aug 5, 2025
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
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D79460136

tugsbayasgalan added a commit to tugsbayasgalan/pytorch that referenced this pull request Aug 6, 2025
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
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D79460136

@tugsbayasgalan tugsbayasgalan requested review from zou3519 and ydwu4 August 6, 2025 00:37
@@ -1241,6 +1242,15 @@ def serialize_output_spec(self, spec: ep.OutputSpec) -> OutputSpec:
buffer_name=spec.target,
)
)
elif spec.kind == ep.OutputKind.PARAMETER_MUTATION:
Copy link
Contributor

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?

@@ -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]
Copy link
Contributor

@ydwu4 ydwu4 Aug 6, 2025

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

self.assertTrue("parameter" in val.values())

with self.assertRaisesRegex(RuntimeError, "leaf"):
ep.module()(torch.rand(4, 4))
Copy link
Contributor

@ydwu4 ydwu4 Aug 6, 2025

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.

pytorch-bot bot pushed a commit that referenced this pull request Aug 11, 2025
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
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D79460136

@tugsbayasgalan tugsbayasgalan requested a review from ydwu4 August 11, 2025 18:06
with (
ctx,
override_getattribute_for_subclasses(flat_args),
_maybe_restore_grad_state(),
Copy link
Contributor

@ydwu4 ydwu4 Aug 11, 2025

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.

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Aug 11, 2025
tugsbayasgalan added a commit to tugsbayasgalan/pytorch that referenced this pull request Aug 12, 2025
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
tugsbayasgalan added a commit to tugsbayasgalan/pytorch that referenced this pull request Aug 12, 2025
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:
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
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D79460136

1 similar comment
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D79460136

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

Successfully merging this pull request may close these issues.

5 participants