Skip to content

Make copy.copy of operator objects return the operator itself - #200954

Closed
anijain2305 wants to merge 1 commit into
gh/anijain2305/1358/basefrom
gh/anijain2305/1358/head
Closed

anijain2305 wants to merge 1 commit into
gh/anijain2305/1358/basefrom
gh/anijain2305/1358/head

Conversation

@anijain2305

@anijain2305 anijain2305 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

OpOverload, OpOverloadPacket and HigherOrderOperator define
__deepcopy__ to return self because each must be unique, but they did not
define __copy__. A shallow copy therefore built a new instance:

  • FX copies node metadata such as original_aten with copy.copy when
    tracing with preserved node meta, so every node traced by AOTAutograd
    carried its own copy of its op. The copies hash like the op but compare
    unequal, which defeats any cache or identity check keyed on the op.
  • copy.copy(torch.ops.aten.add) never finished: the half-built packet's
    __getattr__ and __str__ recursed through each other.

Return self from __copy__, matching __deepcopy__.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 1 New Failure

As of commit e7fba7e with merge base 207451c (image):

🟢 GREEN LIGHT - PR approved to be merged without human review:

  • Triviality
    • Single-cause bugfix: torch/_ops.py adds __copy__ returning self to HigherOrderOperator, OpOverload and OpOverloadPacket, matching their __deepcopy__
    • Root cause read: torch/fx/proxy.py:260 runs copy.copy on _COPY_META_FIELDS, which include original_aten, building a new op instance
  • Tests
    • test/test_per_overload_api.py asserts copy identity for all three classes and adds an aot_function test checking original_aten
    • These ops define no __eq__ or __reduce__, so the old default copy was a distinct object and the assertIn would have failed before the fix
  • Socialization
    • The copy.copy hits in test_ops.py and test_meta.py copy OpInfos, not operators; no other caller relies on getting a distinct op back

reason: clean
Reviewed commit: e7fba7e

Inference job

🐛 Report a wrong verdict

NEW FAILURE - The following job has failed:

  • trunk / macos-py3-arm64 / test (default, 3, 3, macos-m1-stable) (gh)
    inductor/test_torchinductor

    AI verdict: AI verdict: probably not related

    The shard failed because the MPS GPU on the M1 runner ran out of memory (kIOGPUCommandBufferCallbackErrorOutOfMemory). It happened right after GPUTests::test_large_block_sizes_mps passed, a test that allocates about 8 GB. The suspect commit only adds __copy__ methods that return self to three op classes, and nothing connects that to MPS memory use.

    Full reasoning on HUD →

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

anijain2305 added a commit that referenced this pull request Oct 14, 2026
`OpOverload`, `OpOverloadPacket` and `HigherOrderOperator` define
`__deepcopy__` to return `self` because each must be unique, but they did not
define `__copy__`. A shallow copy therefore built a new instance:

- FX copies node metadata such as `original_aten` with `copy.copy` when
  tracing with preserved node meta, so every node traced by AOTAutograd
  carried its own copy of its op. The copies hash like the op but compare
  unequal, which defeats any cache or identity check keyed on the op.
- `copy.copy(torch.ops.aten.add)` never finished: the half-built packet's
  `__getattr__` and `__str__` recursed through each other.

Return `self` from `__copy__`, matching `__deepcopy__`.


ghstack-source-id: 307f8cd
Pull-Request: #200954
@anijain2305
anijain2305 marked this pull request as ready for review October 14, 2026 18:34
@anijain2305

Copy link
Copy Markdown
Contributor Author

@claude review this PR

@claude

claude Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Claude finished @anijain2305's task in 35s —— View job


I found no issues in this PR and recommend approving it.

PR Review: #200954

Summary

The PR adds __copy__ returning self to OpOverload, OpOverloadPacket and HigherOrderOperator, which makes copy.copy behave like the existing __deepcopy__. No REVIEW.md files apply.

What I checked:

  • Root cause. torch/fx/proxy.py:260 copies each field in _COPY_META_FIELDS with copy.copy, which includes original_aten. Before this PR, that copy produced a new OpOverload instance. OpOverload keeps the default identity __eq__, so the copy compared unequal to the real op even though __hash__ (torch/_ops.py:941) made it hash the same.
  • Regression test. test_traced_original_aten_is_the_op should fail without the fix: assertIn compares with ==, which here means identity. I did not run it locally because Python execution needed approval in this environment.
  • Coverage of the change. In torch/ these are the only three classes whose __deepcopy__ returns self, so no other op-like class is left without __copy__. TorchBindOpOverload inherits the new method from OpOverload.
  • Backward compatibility. Nothing should depend on copy.copy of an op returning a distinct object, because the previous behaviour was broken: copying a packet recursed forever, and copied overloads failed equality checks.

Recommendation

Approve

The change is small and matches the existing __deepcopy__ contract. It includes a unit test for each class and an end-to-end AOTAutograd regression test.

@anijain2305

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Oct 14, 2026
@anijain2305 anijain2305 added the topic: not user facing topic category label Oct 14, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: This PR needs a release notes: label
If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Details for Dev Infra team Raised by workflow job

@anijain2305

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
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

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: trunk / macos-py3-arm64 / test (default, 3, 3, macos-m1-stable)

Details for Dev Infra team Raised by workflow job

@anijain2305

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge -f "unrelated CI failure"

@pytorchmergebot

Copy link
Copy Markdown
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

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

Labels

ciflow/inductor ciflow/trunk Trigger trunk jobs on your pull request Merged topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants