8000 Use object identity for deepcopy memo by davidberard98 · Pull Request #126126 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

Use object identity for deepcopy memo #126126

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

Closed

Conversation

davidberard98
Copy link
Contributor
@davidberard98 davidberard98 commented May 14, 2024

Stack from ghstack (oldest at bottom):

Copy of #126089, with some additional fixes & tests

Partial fix for #125635: previously, the deepcopy implementation would group together any tensors with any aliasing relationship and assign them to the same tensor. This was sort of good if you have two tensors b = a.detach(), because then if you deepcopy list = [a, b] to list2 = list.deepcopy(), then writes to list2[0] will also modify list2[1]. But for the most part, it's bad; (1) if you have b = a.as_strided((4, 4), (16, 1), 16), then it'll make b == a in the deepcopied implementation, which is completely wrong; and (2) even if you have b = a.detach(), these are still initially two different tensors which become the same tensor after the old deepcopy implementation.

The new implementation only groups together tensors that have the same identity. This is a partial fix, but it's more reasonable. What changes:

  • (becomes more correct): different views of the same base tensor will no longer all become equal after deepcopying
  • (still kind of wrong): views won't actually alias each other after deepcopying.
  • (arguably a minor regression): equivalent views of the same tensor will no longer be copied to the same tensor - so they won't alias.

BC breaking: C++ deepcopy interface changes from accepting IValue::HashAliasedIValueMap memo to accepting IValue::HashIdentityIValueMap memo. If there are objections, we can keep the old API. However, it seems likely that users generally won't try to deepcopy from C++.

cc @ezyang @gchanan

Differential Revision: D57406306

Copy link
pytorch-bot bot commented May 14, 2024

🔗 Helpful Links

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

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 21183d5 with merge base ee8c155 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

Copy of #126089, with some fixes & tests (TODO)

[TODO description]


[ghstack-poisoned]
@pytorch-bot pytorch-bot bot added the release notes: jit release notes category label May 14, 2024
davidberard98 added a commit that referenced this pull request May 14, 2024
ghstack-source-id: 05bda28
Pull Request resolved: #126126
Copy of #126089, with some fixes & tests (TODO)

[TODO description]


[ghstack-poisoned]
@davidberard98
Copy link
Contributor Author

@davidberard98 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Copy of #126089, with some fixes & tests (TODO)

[TODO description]

Differential Revision: [D57340612](https://our.internmc.facebook.com/intern/diff/D57340612)

[ghstack-poisoned]
davidberard98 added a commit that referenced this pull request May 14, 2024
ghstack-source-id: 87ce5ed
Pull Request resolved: #126126
@davidberard98 davidberard98 added module: bc-breaking Related to a BC-breaking change topic: bc breaking topic category labels May 14, 2024
@davidberard98
Copy link
Contributor Author

@davidberard98 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Copy link
Contributor
@ezyang ezyang left a comment

Choose a reason for hiding this comment

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

Thanks a lot!

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label May 14, 2024
@davidberard98 davidberard98 marked this pull request as ready for review May 14, 2024 20:50
@facebook-github-bot
Copy link
Contributor

@pytorchbot merge -f 'Landed internally'

(Initiating merge automatically since Phabricator Diff has merged, using force because this PR might not pass merge_rules.json but landed internally)

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

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

@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: Command git -C /home/runner/work/pytorch/pytorch cherry-pick -x f2eff2da2dccb4326f1fe668cc7438833501f243 returned non-zero exit code 1

Auto-merging aten/src/ATen/core/ivalue.cpp
CONFLICT (content): Merge conflict in aten/src/ATen/core/ivalue.cpp
Auto-merging aten/src/ATen/core/ivalue.h
CONFLICT (content): Merge conflict in aten/src/ATen/core/ivalue.h
Auto-merging aten/src/ATen/core/ivalue_inl.h
CONFLICT (content): Merge conflict in aten/src/ATen/core/ivalue_inl.h
Auto-merging torch/csrc/jit/api/module.cpp
Auto-merging torch/csrc/jit/api/module.h
Auto-merging torch/csrc/jit/passes/quantization/insert_observers.cpp
Auto-merging torch/csrc/jit/python/script_init.cpp
error: could not apply f2eff2da2dc... Use object identity for deepcopy memo
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
Details for Dev Infra team Raised by workflow job

Copy of #126089, with some additional fixes & tests

Partial fix for #125635: previously, the deepcopy implementation would group together any tensors with any aliasing relationship and assign them to the same tensor. This was sort of good if you have two tensors `b = a.detach()`, because then if you deepcopy `list = [a, b]` to `list2 = list.deepcopy()`, then writes to `list2[0]` will also modify `list2[1]`. But for the most part, it's bad; (1) if you have `b = a.as_strided((4, 4), (16, 1), 16)`, then it'll make `b == a` in the deepcopied implementation, which is completely wrong; and (2) even if you have `b = a.detach()`, these are still initially two different tensors which become the same tensor after the old deepcopy implementation.

The new implementation only groups together tensors that have the same identity. This is a partial fix, but it's more reasonable. What changes:
* (becomes more correct): different views of the same base tensor will no longer all become equal after deepcopying
* (still kind of wrong): views won't actually alias each other after deepcopying.
* (arguably a minor regression): equivalent views of the same tensor will no longer be copied to the same tensor - so they won't alias.

BC breaking: C++ deepcopy interface changes from accepting `IValue::HashAliasedIValueMap memo` to accepting `IValue::HashIdentityIValueMap memo`. If there are objections, we can keep the old API. However, it seems likely that users generally won't try to deepcopy from C++.

Differential Revision: [D57340612](https://our.internmc.facebook.com/intern/diff/D57340612)

cc ezyang gchanan

[ghstack-poisoned]
davidberard98 added a commit that referenced this pull request May 15, 2024
ghstack-source-id: f857f0e
Pull Request resolved: #126126
@davidberard98
Copy link
Contributor Author

@davidberard98 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@davidberard98
Copy link
Contributor Author

@pytorchbot merge

this landed internally already, but the bot didn't merge it in OSS

@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

ZelboK pushed a commit to ZelboK/pytorch that referenced this pull request May 19, 2024
Copy of pytorch#126089, with some additional fixes & tests

Partial fix for pytorch#125635: previously, the deepcopy implementation would group together any tensors with any aliasing relationship and assign them to the same tensor. This was sort of good if you have two tensors `b = a.detach()`, because then if you deepcopy `list = [a, b]` to `list2 = list.deepcopy()`, then writes to `list2[0]` will also modify `list2[1]`. But for the most part, it's bad; (1) if you have `b = a.as_strided((4, 4), (16, 1), 16)`, then it'll make `b == a` in the deepcopied implementation, which is completely wrong; and (2) even if you have `b = a.detach()`, these are still initially two different tensors which become the same tensor after the old deepcopy implementation.

The new implementation only groups together tensors that have the same identity. This is a partial fix, but it's more reasonable. What changes:
* (becomes more correct): different views of the same base tensor will no longer all become equal after deepcopying
* (still kind of wrong): views won't actually alias each other after deepcopying.
* (arguably a minor regression): equivalent views of the same tensor will no longer be copied to the same tensor - so they won't alias.

BC breaking: C++ deepcopy interface changes from accepting `IValue::HashAliasedIValueMap memo` to accepting `IValue::HashIdentityIValueMap memo`. If there are objections, we can keep the old API. However, it seems likely that users generally won't try to deepcopy from C++.

Differential Revision: [D57406306](https://our.internmc.facebook.com/intern/diff/D57406306)
Pull Request resolved: pytorch#126126
Approved by: https://github.com/ezyang
@github-actions github-actions bot deleted the gh/davidberard98/298/head branch June 16, 2024 01:59
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: bc-breaking Related to a BC-breaking change release notes: jit release notes category topic: bc breaking topic category
4B46
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0