Repository navigation
158232 Fix autocast cache incorrectly retaining no_grad state - #165068
stmcgovern wants to merge 1 commit into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/165068
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (4 Unrelated Failures)As of commit 5ecda48 with merge base 75d1815 ( FLAKY - The following jobs failed but were likely due to flakiness present on trunk:
BROKEN TRUNK - The following jobs failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
| arg.is_leaf() && !arg.is_view() && cache_enabled && | ||
| !at::caching::is_cached_tensor(arg)); | ||
| !at::caching::is_cached_tensor(arg) && | ||
| at::GradMode::is_enabled()); // Only cache when gradients are enabled |
There was a problem hiding this comment.
@janeyx99 : this unblocks fsdp2 training but regress inference perf. any opinions?
@stmcgovern great finding! any chance to not affecting inference? this can be a big perf change
There was a problem hiding this comment.
@weifengpy Thanks! Yes, I was just trying to highlight the root cause. One idea is to maintain separate caches for gradient-enabled and gradient-disabled modes.
There was a problem hiding this comment.
got you - just being able to identify the root cause is a big step
|
I've pushed a dual cache solution for consideration @weifengpy @janeyx99 |
|
@ngimel Flagging this autocast bug + also for the perf implication |
|
Please fix the PR description to reflect the actual PR change. If people complain about extra memory use we'd need to expose control over which mode gets cached, but for now I think it's ok to land as is |
|
@pytorchbot label "topic: not user facing" |
|
I've added the skip for dynamo decorators for 3 of 4 added tests (failed in previous CI run) and resolved the merge conflict. @ngimel @janeyx99 @weifengpy |
|
Hi, any update on this issue? |
|
@janeyx99 are you ok with landing this? |
janeyx99
left a comment
There was a problem hiding this comment.
The change looks good to me, but I don't feel great that we're just skipping dynamo tests. Do you know why those 3 tests run into KeyErrors?
At the very least, let's track the fact that dynamo doesn't work with the new autocast cache system in an issue and link to that issue in the test skips.
|
Dynamo/inductor already turn off autocast caching |
janeyx99
left a comment
There was a problem hiding this comment.
Oh. Then sounds good to me lol
|
@pytorchbot merge |
Merge startedYour 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 |
|
@pytorchbot revert -c nosignal -m "This broke ROCm CI. test/test_transformers.py::TestTransformersCUDA::test_transformerencoder_fastpath_use_torchscript_False_enable_nested_tensor_True_use_autocast_True_d_model_256_cuda GH job link HUD commit link" |
|
@pytorchbot successfully started a revert job. Check the current status here. |
…pytorch#165068)" This reverts commit 5daef30. Reverted pytorch#165068 on behalf of https://github.com/jeffdaily due to This broke ROCm CI. test/test_transformers.py::TestTransformersCUDA::test_transformerencoder_fastpath_use_torchscript_False_enable_nested_tensor_True_use_autocast_True_d_model_256_cuda [GH job link](https://github.com/pytorch/pytorch/actions/runs/18572589089/job/52952074008) [HUD commit link](https://hud.pytorch.org/pytorch/pytorch/commit/5daef30b26b794d237fbbc399c1d47ec0380200a) ([comment](pytorch#165068 (comment)))
…h#165068) Fixes pytorch#158232 The autocast caching heuristic in `aten/src/ATen/autocast_mode.cpp:139` did not account for gradient mode state when deciding whether to cache. FSDP2 is not directly related. ~~This PR adds `GradMode::is_enabled()` check to caching condition. Caching is now disabled in `no_grad()` contexts to prevent storing tensors with incorrect gradient state. Ensures correctness at the cost of using cache.~~ This PR proposes separate caches for gradient-enabled and gradient-disabled modes. Adds tests. Pull Request resolved: pytorch#165068 Approved by: https://github.com/ngimel, https://github.com/janeyx99
…pytorch#165068)" This reverts commit 5daef30. Reverted pytorch#165068 on behalf of https://github.com/jeffdaily due to This broke ROCm CI. test/test_transformers.py::TestTransformersCUDA::test_transformerencoder_fastpath_use_torchscript_False_enable_nested_tensor_True_use_autocast_True_d_model_256_cuda [GH job link](https://github.com/pytorch/pytorch/actions/runs/18572589089/job/52952074008) [HUD commit link](https://hud.pytorch.org/pytorch/pytorch/commit/5daef30b26b794d237fbbc399c1d47ec0380200a) ([comment](pytorch#165068 (comment)))
3a8fb13 to
1bb6cfd
Compare
When I got the opportunity to reproduce this failure on MI300X, investigation showed that the values used in the two caches (one for no_grad and one for grad_enabled) accumulated slightly different values. Different requires_grad states led to different kernel dispatch paths and tiny numerical differences accumulate through transformer layers. This also occurred on CUDA but stayed under the tolerance. The new commit uses a single cache but always creates the tensor in grad_enabled mode to ensure that it works for grad and no_grad. |
The autocast cache was not handling tensors created in no_grad() context. When a weight was first cast in no_grad mode, the cached result had no grad_fn. Subsequent uses in grad-enabled mode would reuse this tensor, breaking gradient computation. Fix: 1. Skip caching in inference_mode (enable_grad can't override it) 2. Always create cached tensors with grad enabled via AutoGradMode RAII The cached tensor now has grad_fn and works in both contexts: - no_grad: grad_fn ignored (correct) - grad enabled: grad_fn used (correct) Fixes pytorch#158232
1bb6cfd to
5ecda48
Compare
|
@pytorchbot merge |
Merge startedYour 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 |
|
The merge job was canceled or timed out. This most often happen if two merge requests were issued for the same PR, or if merge job was waiting for more than 6 hours for tests to finish. In later case, please do not hesitate to reissue the merge command |
|
@pytorchbot merge |
Merge startedYour 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 |
…h#165068) Fixes pytorch#158232 The autocast caching heuristic in `aten/src/ATen/autocast_mode.cpp:139` did not account for gradient mode state when deciding whether to cache. FSDP2 is not directly related. ~~This PR adds `GradMode::is_enabled()` check to caching condition. Caching is now disabled in `no_grad()` contexts to prevent storing tensors with incorrect gradient state. Ensures correctness at the cost of using cache.~~ ~~This PR proposes separate caches for gradient-enabled and gradient-disabled modes.~~ This PR uses a single cache and always creates cached tensors with grad enabled. Skips caching in inference_mode (not no_grad). Cached tensors work in both no_grad and grad contexts. Adds tests. Pull Request resolved: pytorch#165068 Approved by: https://github.com/ngimel
- Bump `onnx-ir < 0.3.0` to support PyTorch 2.11 schemas - Update AMP test to expect `grad_fn` since PyTorch 2.11 fixed the autocast cache bug (pytorch/pytorch#165068)
* ci: add pytorch 2.11 to cpu tests * test: fix precision and onnx tests for PyTorch 2.11 - Bump `onnx-ir < 0.3.0` to support PyTorch 2.11 schemas - Update AMP test to expect `grad_fn` since PyTorch 2.11 fixed the autocast cache bug (pytorch/pytorch#165068) * fix: update gloo device condition for PyTorch 2.11 compatibility * fix: update xfail condition for gloo device in PyTorch 2.11 * update
Fixes #158232
The autocast caching heuristic in
aten/src/ATen/autocast_mode.cpp:139did not account for gradient mode state when deciding whether to cache. FSDP2 is not directly related.This PR addsGradMode::is_enabled()check to caching condition. Caching is now disabled inno_grad()contexts to prevent storing tensors with incorrect gradient state. Ensures correctness at the cost of using cache.This PR proposes separate caches for gradient-enabled and gradient-disabled modes.This PR uses a single cache and always creates cached tensors with grad enabled. Skips caching in inference_mode (not no_grad). Cached tensors work in both no_grad and grad contexts.
Adds tests.
cc @mcarilli @ptrblck @leslie-fang-intel @jgong5