Repository navigation
Conversation
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]>
🔗 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 ( 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. |
|
|
|
@pytorchbot merge -f "Lint + MPS are green" |
Merge startedYour change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
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)