Skip to content

Make tf32_off thread-safe for multithreaded test runners - #190285

Closed
jeffdaily wants to merge 1 commit into
mainfrom
jeffdaily/tf32-off-thread-safe
Closed

jeffdaily wants to merge 1 commit into
mainfrom
jeffdaily/tf32-off-thread-safe

Conversation

@jeffdaily

Copy link
Copy Markdown
Collaborator

The tf32_off context manager mutates process-global TF32 state (torch.backends.cuda.matmul plus the cudnn flags) with a per-entry save/restore. Multithreaded test runners such as MultiThreadedTestCase execute the decorated test body once per rank thread, so linalg OpInfo tests carrying with_tf32_off enter tf32_off concurrently: a thread can snapshot already-modified state and restore it last, leaking modified flags past the final exit. The fp32 precision leak detector added in #185236 now catches this in the distributed dtensor CPU shards (cuda.matmul fp32_precision "none" -> "ieee", cudnn conv/rnn "tf32" -> "none", the exact signature of the interleaved cudnn.flags entry write surviving).

The fix makes tf32_off first-in/last-out: a refcount guarded by a lock so only the first concurrent entry saves state and applies the writes, and only the last exit restores. The lock is held only around the bookkeeping, never across the yield, so rank threads still run concurrently and dtensor collectives cannot deadlock. The writes themselves are unchanged, and the cudnn.flags context manager is kept (entered and exited manually so the exit can run at last-out) because it handles the frozen-flags machinery test suites enable. tf32_on is untouched since it is only reachable through the already device-gated tf32_on_and_off, which multithreaded runners do not use.

An alternative was a thread-local implementation via at::NoTF32Guard, which already exists and is consulted in the cuBLAS handle pool, but it has no Python binding, no cudnn coverage, and is invisible to compiled-code paths that read the global flag at compile time; that remains a possible follow-up.

Authored with an AI assistant (Claude Code).

The tf32_off context manager mutates process-global TF32 state (torch.backends.cuda.matmul plus the cudnn flags) with a per-entry save/restore. Multithreaded test runners such as MultiThreadedTestCase execute the decorated test body once per rank thread, so linalg OpInfo tests carrying with_tf32_off enter tf32_off concurrently: a thread can snapshot already-modified state and restore it last, leaking modified flags past the final exit. The fp32 precision leak detector added in #185236 now catches this in the distributed dtensor CPU shards (cuda.matmul fp32_precision "none" -> "ieee", cudnn conv/rnn "tf32" -> "none", the exact signature of the interleaved cudnn.flags entry write surviving).

The fix makes tf32_off first-in/last-out: a refcount guarded by a lock so only the first concurrent entry saves state and applies the writes, and only the last exit restores. The lock is held only around the bookkeeping, never across the yield, so rank threads still run concurrently and dtensor collectives cannot deadlock. The writes themselves are unchanged, and the cudnn.flags context manager is kept (entered and exited manually so the exit can run at last-out) because it handles the frozen-flags machinery test suites enable. tf32_on is untouched since it is only reachable through the already device-gated tf32_on_and_off, which multithreaded runners do not use.

An alternative was a thread-local implementation via at::NoTF32Guard, which already exists and is consulted in the cuBLAS handle pool, but it has no Python binding, no cudnn coverage, and is invisible to compiled-code paths that read the global flag at compile time; that remains a possible follow-up.

Authored with Claude Code (Claude).
@pytorch-bot

pytorch-bot Bot commented Jul 16, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

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

@pytorch-bot

pytorch-bot Bot commented Jul 16, 2026

Copy link
Copy Markdown

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.

@jeffdaily

Copy link
Copy Markdown
Collaborator Author

Superseded: rolled into the reland of #185236 (commit c867793 there), since the fix exists to address exactly the regression that PR's revert flagged.

@jeffdaily jeffdaily closed this Jul 16, 2026
@github-actions
github-actions Bot deleted the jeffdaily/tf32-off-thread-safe branch August 16, 2026 02:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants