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 support for models with mutated buffer on torch.onnx.dynamo_export #112272

Closed

Conversation

thiagocrepaldi
Copy link
Collaborator

@thiagocrepaldi thiagocrepaldi commented Oct 27, 2023

Stack from ghstack (oldest at bottom):

This PR adds a unit test that leverages torch.export.ExportedProgram models that mutates registered buffers. Although the exporter already works out of the box in such scenario, the GraphModule and the exported ONNX model have extra outputs containing the mutated buffers. On future runs of the ONNX model, the mutated buffers are used as input to the model.

The aforementioned extra inputs and outputs are by design and the ONNXProgram.model_signature can be used to fetch detailed input/output schema for the exported model.

However, when we want to compare pytorch output to ONNX's, there is a mismatch between the schema because pytorch output does not include the mutated buffers present on the ONNX output.

This PR extends onnx_program.adapt_torch_outputs_to_onnx(torch_outputs) so that the mutated buffers are prepended to the Pytorch output, matching the ONNX schema.

`run_test_with_fx_to_onnx_exporter_and_onnx_runtime` had to be extended
to allow extra outputs on the ONNX graph due to extra outputs due to
mutated inputs and buffers

[ghstack-poisoned]
@pytorch-bot
Copy link

pytorch-bot bot commented Oct 27, 2023

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/112272

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 ce48b3d with merge base 88a8a0d (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.

thiagocrepaldi pushed a commit that referenced this pull request Oct 27, 2023
`run_test_with_fx_to_onnx_exporter_and_onnx_runtime` had to be extended
to allow extra outputs on the ONNX graph due to extra outputs due to
mutated inputs and buffers

ghstack-source-id: 4ef89ce32fecf1ecb3bd996b76ce65e1880c864b
Pull Request resolved: #112272
@thiagocrepaldi thiagocrepaldi marked this pull request as draft October 27, 2023 18:16
…tated buffer"

`run_test_with_fx_to_onnx_exporter_and_onnx_runtime` had to be extended
to allow extra outputs on the ONNX graph due to extra outputs due to
mutated inputs and buffers

[ghstack-poisoned]
thiagocrepaldi pushed a commit that referenced this pull request Oct 27, 2023
`run_test_with_fx_to_onnx_exporter_and_onnx_runtime` had to be extended
to allow extra outputs on the ONNX graph due to extra outputs due to
mutated inputs and buffers

ghstack-source-id: 979da739bfeeb9fcadc498e53d2afba1374de9c4
Pull Request resolved: #112272
@thiagocrepaldi thiagocrepaldi added module: onnx Related to torch.onnx triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module onnx-triaged triaged by ONNX team release notes: onnx torch.onnx related changes that should show up in the release notes labels Oct 27, 2023
Thiago Crepaldi added 2 commits October 30, 2023 21:24
…tated buffer"

`run_test_with_fx_to_onnx_exporter_and_onnx_runtime` had to be extended
to allow extra outputs on the ONNX graph due to extra outputs due to
mutated inputs and buffers

[ghstack-poisoned]
…tated buffer"

`run_test_with_fx_to_onnx_exporter_and_onnx_runtime` had to be extended
to allow extra outputs on the ONNX graph due to extra outputs due to
mutated inputs and buffers

[ghstack-poisoned]
Thiago Crepaldi added 2 commits October 31, 2023 15:21
…tated buffer"

`run_test_with_fx_to_onnx_exporter_and_onnx_runtime` had to be extended
to allow extra outputs on the ONNX graph due to extra outputs due to
mutated inputs and buffers

[ghstack-poisoned]
…tated buffer"

`run_test_with_fx_to_onnx_exporter_and_onnx_runtime` had to be extended
to allow extra outputs on the ONNX graph due to extra outputs due to
mutated inputs and buffers

[ghstack-poisoned]
@thiagocrepaldi thiagocrepaldi marked this pull request as ready for review October 31, 2023 18:51
Thiago Crepaldi added 3 commits November 1, 2023 23:54
…tated buffer"

`run_test_with_fx_to_onnx_exporter_and_onnx_runtime` had to be extended
to allow extra outputs on the ONNX graph due to extra outputs due to
mutated inputs and buffers

[ghstack-poisoned]
…tated buffer"

`run_test_with_fx_to_onnx_exporter_and_onnx_runtime` had to be extended
to allow extra outputs on the ONNX graph due to extra outputs due to
mutated inputs and buffers

[ghstack-poisoned]
…tated buffer"

`run_test_with_fx_to_onnx_exporter_and_onnx_runtime` had to be extended
to allow extra outputs on the ONNX graph due to extra outputs due to
mutated inputs and buffers

[ghstack-poisoned]
…tated buffer"

`run_test_with_fx_to_onnx_exporter_and_onnx_runtime` had to be extended
to allow extra outputs on the ONNX graph due to extra outputs due to
mutated inputs and buffers

[ghstack-poisoned]
…ynamo_export"


This PR adds a unit test that leverages `torch.export.ExportedProgram` models that mutates registered buffers. Although the exporter already works out of the box in such scenario, the GraphModule and the exported ONNX model have extra outputs containing the mutated buffers. On future runs of the ONNX model, the mutated buffers are used as input to the model.

The aforementioned extra inputs and outputs are by design and the `ONNXProgram.model_signature` can be used to fetch detailed input/output schema for the exported model.

However, when we want to compare pytorch output to ONNX's, there is a mismatch between the schema because pytorch output does not include the mutated buffers present on the ONNX output.

This PR extends `onnx_program.adapt_torch_outputs_to_onnx(torch_outputs)` so that the mutated buffers are prepended to the Pytorch output, matching the ONNX schema.

[ghstack-poisoned]
…ynamo_export"


This PR adds a unit test that leverages `torch.export.ExportedProgram` models that mutates registered buffers. Although the exporter already works out of the box in such scenario, the GraphModule and the exported ONNX model have extra outputs containing the mutated buffers. On future runs of the ONNX model, the mutated buffers are used as input to the model.

The aforementioned extra inputs and outputs are by design and the `ONNXProgram.model_signature` can be used to fetch detailed input/output schema for the exported model.

However, when we want to compare pytorch output to ONNX's, there is a mismatch between the schema because pytorch output does not include the mutated buffers present on the ONNX output.

This PR extends `onnx_program.adapt_torch_outputs_to_onnx(torch_outputs)` so that the mutated buffers are prepended to the Pytorch output, matching the ONNX schema.

[ghstack-poisoned]
@thiagocrepaldi
Copy link
Collaborator Author

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Nov 22, 2023
@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here


# ONNXProgram holds a reference (not copy) to the original ref_model, including its state_dict.
# Thus, ONNXProgram() must run before ref_model() to prevent ref_model.forward() from changing the state_dict.
# Otherwise, the ref_model can change buffers on state_dict which would be used by ONNXProgram.__call__()
Copy link
Collaborator

Choose a reason for hiding this comment

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

I recall there was a PR that will make ONNXProgram.call() update original pytorch model lol. This problem will arise again if that's added.

Btw there is a flag has_mutation up there that triggers model cloning. Might be helpful.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

that pr was merged with this one.

@huydhn
Copy link
Contributor

huydhn commented Nov 23, 2023

@pytorchbot revert -m 'Sorry for reverting you change but it is failing dynamo test in trunk https://hud.pytorch.org/pytorch/pytorch/commit/c4a22d6918b7ca218f2712d7e7e147aca7127fa3' -c weird

@pytorchmergebot
Copy link
Collaborator

@pytorchbot successfully started a revert job. Check the current status here.
Questions? Feedback? Please reach out to the PyTorch DevX Team

@pytorchmergebot
Copy link
Collaborator

@thiagocrepaldi your PR has been successfully reverted.

pytorchmergebot added a commit that referenced this pull request Nov 23, 2023
…mo_export (#112272)"

This reverts commit c4a22d6.

Reverted #112272 on behalf of https://github.com/huydhn due to Sorry for reverting you change but it is failing dynamo test in trunk https://hud.pytorch.org/pytorch/pytorch/commit/c4a22d6918b7ca218f2712d7e7e147aca7127fa3 ([comment](#112272 (comment)))
@huydhn
Copy link
Contributor

huydhn commented Nov 23, 2023

It looks like the revert doesn't help, I will reland the change. Sorry for the churn.

@huydhn
Copy link
Contributor

huydhn commented Nov 23, 2023

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

xunsongh pushed a commit to xunsongh/pytorch that referenced this pull request Nov 24, 2023
pytorch#112272)

This PR adds a unit test that leverages `torch.export.ExportedProgram` models that mutates registered buffers. Although the exporter already works out of the box in such scenario, the GraphModule and the exported ONNX model have extra outputs containing the mutated buffers. On future runs of the ONNX model, the mutated buffers are used as input to the model.

The aforementioned extra inputs and outputs are by design and the `ONNXProgram.model_signature` can be used to fetch detailed input/output schema for the exported model.

However, when we want to compare pytorch output to ONNX's, there is a mismatch between the schema because pytorch output does not include the mutated buffers present on the ONNX output.

This PR extends `onnx_program.adapt_torch_outputs_to_onnx(torch_outputs)` so that the mutated buffers are prepended to the Pytorch output, matching the ONNX schema.
Pull Request resolved: pytorch#112272
Approved by: https://github.com/titaiwangms, https://github.com/BowenBao
xunsongh pushed a commit to xunsongh/pytorch that referenced this pull request Nov 24, 2023
xunsongh pushed a commit to xunsongh/pytorch that referenced this pull request Nov 24, 2023
pytorch#112272)

This PR adds a unit test that leverages `torch.export.ExportedProgram` models that mutates registered buffers. Although the exporter already works out of the box in such scenario, the GraphModule and the exported ONNX model have extra outputs containing the mutated buffers. On future runs of the ONNX model, the mutated buffers are used as input to the model.

The aforementioned extra inputs and outputs are by design and the `ONNXProgram.model_signature` can be used to fetch detailed input/output schema for the exported model.

However, when we want to compare pytorch output to ONNX's, there is a mismatch between the schema because pytorch output does not include the mutated buffers present on the ONNX output.

This PR extends `onnx_program.adapt_torch_outputs_to_onnx(torch_outputs)` so that the mutated buffers are prepended to the Pytorch output, matching the ONNX schema.
Pull Request resolved: pytorch#112272
Approved by: https://github.com/titaiwangms, https://github.com/BowenBao
@facebook-github-bot facebook-github-bot deleted the gh/thiagocrepaldi/7/head branch November 26, 2023 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ciflow/trunk Trigger trunk jobs on your pull request Merged module: onnx Related to torch.onnx onnx-triaged triaged by ONNX team open source release notes: onnx torch.onnx related changes that should show up in the release notes Reverted topic: not user facing topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

6 participants