Repository navigation
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/189955
Note: Links to docs will display an error until the docs builds have been completed. ❌ You can merge normally! (4 Unrelated Failures), 1 Unclassified FailureAs of commit a9e20c9 with merge base 6341c24 ( UNCLASSIFIED FAILURE - DrCI could not classify the following job because the workflow did not run on the merge base. The failure may be pre-existing on trunk or introduced by this PR:
UNSTABLE - The following jobs are marked as unstable, possibly due to flakiness on trunk:
CRCR (non-blocking) - The following CRCR downstream CI jobs failed but are non-blocking:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@claude review these changes |
|
Claude finished @jansel's task in 9m 37s —— View job PR Review: #189955Summary: Migrates framework-internal legacy fp32-matmul-precision readers (inductor cache keys, dynamo region tracking, TunableOp naming, ROCm CK gating, benchmark util) to the non-throwing per-backend getters. The migration is correct where applied, but it misses at least one internal reader with the same failure mode and ships no regression test. Full review detailsTesting
Correctness — remaining poison path
Backward Compatibility
Minor
RecommendationRequest Changes — primarily for the missing regression test, and to also migrate the flex-attention reader ( |
|
Both review points addressed in eda5fda:
The TunableGemm.h path is not unit-tested here since it needs TunableOp on MI300 hardware; the existing |
|
@albanD @jansel the PR failures here are caused by a race introduced by the fp32/tf32 state leak detector PR #185236. Rather than revert, I have a forward fix PR #190285 here if you could take a look. EDIT: I was operating on stale information. #185236 had been auto-reverted. So I've rolled #190285 into it to fix the reason for the auto-revert. Will rebase this PR once it relands. |
|
pytorch/torch/_inductor/kernel/flex/flex_attention.py Lines 130 to 142 in da9c79b does this work correctly with ^^ ? |
|
Yes. This reply was drafted with an AI assistant (Claude Code). |
| if torch.backends.cuda.matmul.allow_tf32 is False and torch.cuda.get_device_capability() >= (8, 0): | ||
| tf32_off = torch._C._get_fp32_precision_getter("cuda", "matmul") != "tf32" | ||
| if tf32_off and torch.cuda.get_device_capability() >= (8, 0): | ||
| # tf32 being off implies the legacy precision was "highest" |
There was a problem hiding this comment.
AI-assisted review note: Could we snapshot and restore the per-backend precision values directly here? The resolved CUDA state being non-TF32 does not imply the legacy state was
highest. For example, starting with CUDAieeeand MKLDNNbf16, this sets both backends totf32, then_disable_tensor_cores()restores both toieee, silently losing the caller's MKLDNN setting.
| # per-backend APIs have been mixed. | ||
| getter = torch._C._get_fp32_precision_getter | ||
| return f"cuda:{getter('cuda', 'matmul')},mkldnn:{getter('mkldnn', 'matmul')}" | ||
|
|
There was a problem hiding this comment.
AI-assisted review note: Should this migration also update the nearby XPU path in
get_device_tflops()? It still readstorch.backends.mkldnn.allow_tf32, so a new-styletorch.backends.mkldnn.matmul.fp32_precision = "tf32"is not reflected in the roofline/device estimate. Using_get_fp32_precision_getter("mkldnn", "matmul") == "tf32"there would keep this path consistent with the new API.
|
Friendly ping on this one. @drisspg I addressed your question about This message was drafted with an AI assistant (Claude Code). |
| ( | ||
| torch.backends.cuda.matmul.fp32_precision == "ieee" | ||
| if torch.backends.cuda.matmul.fp32_precision != "none" | ||
| else torch.get_float32_matmul_precision() == "highest" | ||
| ) |
There was a problem hiding this comment.
Wow! This was one hard to read conditional ☠️
If it's not too hard to do, it would be nice to have a unit test for this function.
| @serialTest() | ||
| def test_fp32_precision_new_api_internal_readers(self): | ||
| # Framework-internal readers must use the per-backend getters and stay | ||
| # usable after new-style writes put the legacy enum out of sync (the |
There was a problem hiding this comment.
Since it's possible for legacy enums to go out of sync, should we just get rid of them altogether? Doesn't need to be this PR but I would like to understand if there's any good reason to keep them around. If we decide they can be removed then marking them [[deprecated]] would be a good first move.
|
@pytorchbot rebase |
|
@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here |
|
Successfully rebased |
d7aeb8c to
090bed7
Compare
Reading torch.get_float32_matmul_precision() or torch.backends.cuda.matmul.allow_tf32 raises a mixed-API TORCH_CHECK error whenever fp32 precision was last written through the new per-backend torch.backends.cuda.matmul.fp32_precision API, because the new-style setter intentionally does not update the legacy global enum. Several framework-internal hot paths still read through the legacy getters, so a single new-style write poisons inductor compilation (cache keys in codecache and select_algorithm), ROCm CK instance gating, and the benchmark compile util. This blocks migrating tests and user code to the new API, as suggested by review on #185236. This PR converts those framework-internal readers to the resolved per-backend getter (torch._C._get_fp32_precision_getter), which never throws and is the same source of truth the aten matmul paths already consult. The two inductor cache keys now key on the (cuda, mkldnn) matmul precision pair via a shared fp32_matmul_precision_key() helper; the pair carries exactly the information of the legacy string ("highest" -> ieee/ieee, "high" -> tf32/tf32, "medium" -> tf32/bf16). torch/utils/benchmark/utils/compile.py previously called the legacy getter at import time; the snapshot is now taken lazily and only when the util actually changes the precision. The legacy API surface itself (the bindings behind get_float32_matmul_precision and allow_tf32) keeps its consistency check by design, and legacy writers are untouched since the legacy setters keep both representations in sync. The TunableOp TypeName and dynamo graph_region_tracker readers this PR originally also migrated were converted upstream in the meantime by #195301, so they dropped out of the diff when rebasing onto viable/strict. An alternative was to relax the TORCH_CHECK in Context::allowTF32CuBLAS() and Context::float32MatmulPrecision(), but that check is the only guard alerting users to genuinely inconsistent mixed usage, so migrating the internal readers is strictly safer. Test Plan: Verified the failure mode and the fix on a ROCm host (gfx90a). Before this change, importing the benchmark util or reading the legacy getters after a new-style write raises; after it, all converted readers work under both APIs: ``` python -c " import torch torch.backends.cuda.matmul.fp32_precision = 'tf32' print(torch._C._get_fp32_precision_getter('cuda', 'matmul'))" ``` Exercised the modified benchmark util end to end (enable sets high, disable restores highest, idempotent, and no throw under mixed state). Verified getter equivalences under the legacy setter: highest -> ieee/ieee, high -> tf32/tf32, medium -> tf32/bf16 for (cuda, mkldnn). ``` python -m py_compile torch/_inductor/utils.py torch/_inductor/codecache.py torch/_inductor/select_algorithm.py torch/_inductor/codegen/rocm/ck_template.py torch/utils/benchmark/utils/compile.py lintrunner --skip PYREFLY <changed py files> ``` Authored with Claude Code (Claude).
Addresses review: the fp32 precision helper in flex_attention.py (now set_float32_precision() after the BF16x9 rework in #195301) kept a fallback to the legacy torch.get_float32_matmul_precision() for the unset ("none") state, which still throws when a new-style write to another backend (e.g. torch.backends.mkldnn.matmul.fp32_precision = "bf16") makes the legacy enum inconsistent. A resolved "none" means no write at any level of the per-backend hierarchy, which is exactly the legacy default "highest": the legacy setter keeps the per-backend map in sync, and the TORCH_ALLOW_TF32_CUBLAS_OVERRIDE env override initializes the map directly (Context.h fp32_precision initializer). So the fallback collapses to "ieee" and the legacy reader is not needed. The new test in test_cuda.py locks in the fix: after new-style cuda and mkldnn matmul writes (a state under which the legacy getters raise, as test_invalid_status_for_legacy_api asserts), the converted internal readers must work and return the new-style values. Test Plan: Validated on a ROCm host (gfx90a) by overlaying the changed Python files onto the installed torch (pure-Python change, no rebuild): ``` python test/test_cuda.py TestCuda.test_fp32_precision_new_api_internal_readers -v python test/test_cuda.py -k fp32_precision -v ``` All pass (2 skips: CUDA 12.9 bfx9 test, unrelated cudnn issue). Also verified set_float32_precision() semantics directly: never-set state yields 'ieee', legacy set_float32_matmul_precision('high') yields 'tf32' on CUDA / 'ieee' on hip, and the benchmark util enable/disable round trip works under mixed API state. ``` lintrunner --skip PYREFLY torch/_inductor/kernel/flex/flex_attention.py test/test_cuda.py ``` Authored with Claude Code (Claude).
090bed7 to
a08ffbb
Compare
|
Adding concrete downstream impact: this fixes the failure reported in #196728, and the fix has been validated against the original workload. The reproduction was a DeepSeek-V3 16B pretraining workload with:
During distributed initialization, TorchTitan sets torch.backends.cuda.matmul.fp32_precision = "bfx9"on compute capability 10.0 and newer. The failure occurs even without explicitly enabling model compilation because TorchTitan's determinism setup compiles FlexAttention. The first compiled FlexAttention call reaches Applying the on-CUDA-path hunks from this PR to the PyTorch installation used by the original workload resolved the failure. All three previously failing runs then trained for 50 steps to completion:
Across the three runs there were 0 OOMs and 0 allocation retries. The changes here to Maybe it is useful to add |
Address review on pytorch#196737: the CUDA-only helper still fell back to the legacy getter when cuda was unset but mkldnn used the new API, and it collapsed legacy high (tf32/tf32) vs medium (tf32/bf16). Use the resolved per-backend pair via _get_fp32_precision_getter, matching the approach in pytorch#189955 for the overlapping cache-key sites. Test Plan: - test/inductor/test_codecache.py::TestAutotuneCacheFp32Precision Authored with an AI assistant.
|
@jeffdaily any blockers here? |
Resolves conflicts with #196860, which added a narrower workaround at the two autotune cache-key sites this PR migrates: read the new-style per-backend value and only consult the legacy getter when it is not "bfx9". This PR's fp32_matmul_precision_key() supersedes that, since it never consults the legacy getter and so covers bfx9 along with every other mixed-API state. Updates test_precompile_cache_key_handles_bfx9 for the pair-format key and tightens it to assert the legacy getter is never called for any precision.
|
The blocker was a conflict with #196860, which landed a narrower fix at the same two autotune cache-key sites this PR touches: it reads the new-style per-backend value and only falls back to the legacy getter when that value is not I resolved it by taking @EddieYan flagging you since this removes the workaround from #196860. Merge is pushed and CI is rerunning. Your approval still stands from 2026-08-12; let me know if you want another look at the conflict resolution given it deletes code from #196860. This reply was drafted with an AI assistant (Claude Code). |
|
@pytorchbot merge -i The three red checks are all unrelated. Two are This comment was drafted with an AI assistant (Claude Code). |
Merge startedYour change will be merged while ignoring the following 3 checks: inductor / unit-test / inductor-cpu-test / test (inductor_aoti_fallback, 1, 2, mt-l-x86iamx-8-64, unstable), inductor / unit-test / inductor-cpu-test / test (inductor_aoti_fallback, 2, 2, mt-l-x86iamx-8-64, unstable), rocm-mi300 / linux-noble-rocm-py3.11-mi300 / test (default, 4, 8, linux.rocm.gpu.mi300.1) Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
…i-structured test (#199248) `test_max_autotune_cutlass_backend_sparse_semi_structured_mm` fails with `KeyError: 'high'` because it reads the autotune local cache with the legacy precision string. #189955 changed `PersistentCache` to key timings by `fp32_matmul_precision_key()`, so with the module's `torch.set_float32_matmul_precision("high")` the bucket is now `"cuda:tf32,mkldnn:tf32"`. That PR updated `test_codecache.py` but missed this lookup. The test now indexes the cache with the same helper the cache writes with. OSS CI did not catch this because nothing runs the test on hardware where it executes. It is gated to sm_8x exactly; the only OSS job that runs `test_cutlass_backend.py` is `h100-cutlass-backend`, where it skips, and the A10G inductor jobs exclude the file. It fails in fbcode, where the file runs on A100. Test Plan: On an A100 (sm_80), with CUDA 13.0 `nvcc` on PATH: ``` python test/inductor/test_cutlass_backend.py -k test_max_autotune_cutlass_backend_sparse_semi_structured_mm ``` Fails with `KeyError: 'high'` before this change and passes after. This PR was authored with the assistance of an AI coding assistant (Claude). Pull Request resolved: #199248 Approved by: https://github.com/pytorchgreenlight
…i-structured test (pytorch#199248) `test_max_autotune_cutlass_backend_sparse_semi_structured_mm` fails with `KeyError: 'high'` because it reads the autotune local cache with the legacy precision string. pytorch#189955 changed `PersistentCache` to key timings by `fp32_matmul_precision_key()`, so with the module's `torch.set_float32_matmul_precision("high")` the bucket is now `"cuda:tf32,mkldnn:tf32"`. That PR updated `test_codecache.py` but missed this lookup. The test now indexes the cache with the same helper the cache writes with. OSS CI did not catch this because nothing runs the test on hardware where it executes. It is gated to sm_8x exactly; the only OSS job that runs `test_cutlass_backend.py` is `h100-cutlass-backend`, where it skips, and the A10G inductor jobs exclude the file. It fails in fbcode, where the file runs on A100. Test Plan: On an A100 (sm_80), with CUDA 13.0 `nvcc` on PATH: ``` python test/inductor/test_cutlass_backend.py -k test_max_autotune_cutlass_backend_sparse_semi_structured_mm ``` Fails with `KeyError: 'high'` before this change and passes after. This PR was authored with the assistance of an AI coding assistant (Claude). Pull Request resolved: pytorch#199248 Approved by: https://github.com/pytorchgreenlight
Reading
torch.get_float32_matmul_precision()ortorch.backends.cuda.matmul.allow_tf32raises a mixed-API TORCH_CHECK error whenever fp32 precision was last written through the new per-backendtorch.backends.cuda.matmul.fp32_precisionAPI, because the new-style setter intentionally does not update the legacy global enum. Several framework-internal hot paths still read through the legacy getters, so a single new-style write poisons inductor compilation (cache keys in codecache and select_algorithm), ROCm CK instance gating, flex attention precision selection, and the benchmark compile util. This blocks migrating tests and user code to the new API, as suggested in review on #185236.This PR converts those framework-internal readers to the resolved per-backend getter (
torch._C._get_fp32_precision_getter), which never throws and is the same source of truth the aten matmul paths already consult. The converted readers are the two inductor cache keys (now keyed on the (cuda, mkldnn) matmul precision pair via a sharedfp32_matmul_precision_key()helper; the pair carries exactly the information of the legacy string: "highest" -> ieee/ieee, "high" -> tf32/tf32, "medium" -> tf32/bf16), ROCm CK instance gating, flex attention'sset_float32_precision()(its legacy fallback for the unset "none" state still threw on new-style writes to other backends; a resolved "none" means unset at every level of the per-backend hierarchy, which is exactly the legacy default "highest", so the fallback collapses to "ieee"), andtorch/utils/benchmark/utils/compile.py, which previously called the legacy getter at import time and now snapshots lazily. The legacy API surface itself keeps its consistency check by design, and legacy writers are untouched since the legacy setters keep both representations in sync. A regression test (test_fp32_precision_new_api_internal_readersin test_cuda.py) exercises the converted readers under the mixed state that makes the legacy getters raise.The TunableOp TypeName and dynamo
get_global_state_key()readers this PR originally also migrated were converted upstream in the meantime by #195301, so they dropped out of the diff when rebasing.#196860 landed a narrower workaround at the two autotune cache-key sites this PR migrates: it reads the new-style per-backend value and only falls back to the legacy getter when that value is not
bfx9. That dodges the throw forbfx9specifically, but the legacy getter still throws for every other precision written through the new API.fp32_matmul_precision_key()supersedes it, because it never consults the legacy getter at all and so coversbfx9along with the rest; merging main therefore drops that workaround.test_precompile_cache_key_handles_bfx9is updated for the pair-format key and tightened to assert the legacy getter is never called for any precision.Compatibility note: the autotune/precompile cache-key component changes format (e.g. "high" becomes "cuda:tf32,mkldnn:tf32"), which invalidates existing on-disk autotune caches once on upgrade; this causes a one-time re-benchmark and has no correctness impact.
An alternative was to relax the TORCH_CHECK in
Context::allowTF32CuBLAS()/Context::float32MatmulPrecision(), but that check is the only guard alerting users to genuinely inconsistent mixed usage, so migrating the internal readers is strictly safer.A follow-up will migrate the module-level precision setup in the test suite to the per-backend API (the follow-up promised in the #185236 review thread).
Authored with an AI assistant (Claude Code).
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo @azahed98