Skip to content

[MPS] Break .grad reference cycles in TestMPS._linear_helper - #201955

Closed
lsyurea wants to merge 1 commit into
pytorch:mainfrom
lsyurea:mps/test-linear-backward-grad-cycle
Closed

lsyurea wants to merge 1 commit into
pytorch:mainfrom
lsyurea:mps/test-linear-backward-grad-cycle

Conversation

@lsyurea

@lsyurea lsyurea commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Linked issue or supporting maintainer

Fixes #201954

BC-breaking?

No

Summary (human written only)

[YOUR SUMMARY: 1-2 sentences in your own words, pointing to #201954 for the details.]

Agent details (if applicable)

Prepared with an AI assistant (Claude Code).

Test-only change. The backward branch of TestMPS._linear_helper calls backward(create_graph=True), which leaves reference cycles through .grad that gc.collect() can't free, so test_mps_allocator_module fails when it runs after any test_linear*_backward test in the same process (details in #201954). The fix resets mps_linear's parameter grads and linear_mps_input.grad at the end of the branch. Both are needed: zero_grad() alone still leaves 1280 bytes per test.

Test plan: before the fix, the allocator test fails with Expected 0 but got 1536 (7680 for the last two); after it, all pass.

python test/test_mps.py -v TestMPS.test_linear1D_backward TestMPS.test_mps_allocator_module
python test/test_mps.py -v TestMPS.test_linear1D_backward TestMPS.test_linear2D_backward TestMPS.test_linear2D_no_bias_backward TestMPS.test_linear3D_backward TestMPS.test_linear3D_no_bias_backward TestMPS.test_mps_allocator_module
python test/test_mps.py -v -k test_linear -k test_mps_allocator_module

🤖 Generated with Claude Code

test_mps_allocator_module asserts that torch.mps.current_allocated_memory() is 0
after gc.collect() and torch.mps.empty_cache(). It fails whenever a
test_linear*_backward test runs before it in the same process, which is what
`python test/test_mps.py` does.

Root cause: the backward branch of _linear_helper calls
backward(create_graph=True), which makes each leaf's .grad reference the leaf
again through the autograd graph. Those cycles go through C++ autograd nodes,
so gc.collect() cannot free them, and the MPS buffers behind
mps_linear.weight/bias.grad and linear_mps_input.grad stay allocated (1536
bytes per test). CI does not see it because test_mps_allocator_module is a
serial test and run_test.py runs serial tests in their own pass.

The fix resets those .grad fields at the end of the backward branch, as the
create_graph=True warning recommends. Both resets are needed: zero_grad()
alone leaves 1280 bytes per test because of the input's cycle. Rewriting the
helper to use torch.autograd.grad would also avoid the cycles, but it changes
what the test exercises, and clearing every tensor in the helper is
unnecessary.

Fixes pytorch#201954

Test Plan:

Without the fix, the allocator test fails with "Expected 0 but got 1536" (7680
for the last two commands); with it, all three pass (the last runs 51 tests):

```
python test/test_mps.py -v TestMPS.test_linear1D_backward TestMPS.test_mps_allocator_module
python test/test_mps.py -v TestMPS.test_linear1D_backward TestMPS.test_linear2D_backward TestMPS.test_linear2D_no_bias_backward TestMPS.test_linear3D_backward TestMPS.test_linear3D_no_bias_backward TestMPS.test_mps_allocator_module
python test/test_mps.py -v -k test_linear -k test_mps_allocator_module
```

This change was authored with an AI assistant (Claude Code).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@pytorch-bot

pytorch-bot Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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 8e1ac54 with merge base 04df708 (image):

NEW FAILURES SUPPRESSED BY AI (non-blocking) - The following job failed, but the AI CI Advisor judged the failure not to be evidence against this change:

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

@pytorch-bot pytorch-bot Bot added ciflow/mps Run MPS tests (subset of trunk) topic: not user facing topic category labels Oct 24, 2026
@pytorch-bot

pytorch-bot Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@malfet
malfet marked this pull request as ready for review October 24, 2026 21:30
@github-actions github-actions Bot added bot-triaged This is a label only to be used by the auto triage bot triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Oct 24, 2026
@malfet

malfet commented Oct 24, 2026

Copy link
Copy Markdown
Collaborator

@pytorchbot merge -f "Lint + MPS are green"

@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

@lsyurea
lsyurea deleted the mps/test-linear-backward-grad-cycle branch October 30, 2026 10:19

This branch was previously deployed

1 inactive deployment
bedrock — 8e1ac549 Deployed Oct 24, 2026 by malfet via analyze #13795
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot-triaged This is a label only to be used by the auto triage bot ciflow/mps Run MPS tests (subset of trunk) Merged open source 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

None yet

Development

Successfully merging this pull request may close these issues.

[MPS] test_mps_allocator_module fails when run after test_linear*_backward (.grad reference cycles in _linear_helper)

4 participants