Skip to content

[Inductor] Key autotune cache on per-backend fp32_precision - #196737

Closed
indrajeetapache wants to merge 2 commits into
pytorch:mainfrom
indrajeetapache:fix/inductor-autotune-cache-fp32-precision
Closed

indrajeetapache wants to merge 2 commits into
pytorch:mainfrom
indrajeetapache:fix/inductor-autotune-cache-fp32-precision

Conversation

@indrajeetapache

@indrajeetapache indrajeetapache commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #196728.

Setting torch.backends.cuda.matmul.fp32_precision = "bfx9" (TorchTitan on Blackwell) makes Inductor's autotune cache raise:

RuntimeError: ... mix of the legacy and new APIs to set the matmul precision

Root cause: after #195301, several Inductor sites moved to the per-backend API, but these two still call the legacy getter:

  • PersistentCache.lookup in torch/_inductor/codecache.py (raising frame from the issue)
  • create_precompile_key in torch/_inductor/select_algorithm.py

The cache lookup is unconditional, so even bf16 FlexAttention fails.

Fix: add get_autotune_cache_fp32_precision() that:

  1. Prefers torch.backends.cuda.matmul.fp32_precision
  2. Falls back to torch.get_float32_matmul_precision() only when the new API is "none"

This matches the pattern already used in FlexAttention / other #195301 call sites, keeps legacy-only IEEE/TF32 cache keys working when the new API is unset, and makes bfx9 a distinct autotune key (correct, since GEMM arithmetic differs).

Test plan

  • Unit: test/inductor/test_codecache.py::TestAutotuneCacheFp32Precision
    • mixed new-API state (highest then fp32_precision="tf32") must not raise in helper / create_precompile_key / PersistentCache.lookup
    • synced legacy setter maps to "ieee" via the helper
  • CI: inductor / flex_attention paths

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo @zasdfgbnm @ptrblck @janbernloehr @Chillee @drisspg

PersistentCache.lookup and create_precompile_key called
torch.get_float32_matmul_precision(), which raises after the new
per-backend API is used (e.g. fp32_precision="bfx9"). Read
torch.backends.cuda.matmul.fp32_precision instead, falling back to the
legacy getter only when it reports "none".

Fixes pytorch#196728

Test Plan:
- test/inductor/test_codecache.py -k TestAutotuneCacheFp32Precision
- CI inductor coverage for flex_attention / autotune paths
@pytorch-bot

pytorch-bot Bot commented Sep 11, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⚠️ 4 Awaiting Approval

As of commit c30f32b with merge base 353bbb7 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

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

@pytorch-bot

pytorch-bot Bot commented Sep 11, 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.

@pytorch-bot

pytorch-bot Bot commented Sep 11, 2026

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/inductor
  • ciflow/torchtitan

Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows.

1 similar comment
@pytorch-bot

pytorch-bot Bot commented Sep 11, 2026

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/inductor
  • ciflow/torchtitan

Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows.

Comment thread torch/_inductor/utils.py Outdated
Comment on lines +3646 to +3658
def get_autotune_cache_fp32_precision() -> str:
"""Precision string for Inductor autotune / PersistentCache keys.

Prefer the per-backend CUDA matmul API. Calling the legacy
``torch.get_float32_matmul_precision()`` after the new API has been used
(e.g. ``fp32_precision="bfx9"`` or ``"tf32"``) raises a mixed-API error —
see #196728. Fall back to the legacy getter only when the new API reports
``"none"``.
"""
precision = torch.backends.cuda.matmul.fp32_precision
if precision != "none":
return precision
return torch.get_float32_matmul_precision()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def get_autotune_cache_fp32_precision() -> str:
"""Precision string for Inductor autotune / PersistentCache keys.
Prefer the per-backend CUDA matmul API. Calling the legacy
``torch.get_float32_matmul_precision()`` after the new API has been used
(e.g. ``fp32_precision="bfx9"`` or ``"tf32"``) raises a mixed-API error —
see #196728. Fall back to the legacy getter only when the new API reports
``"none"``.
"""
precision = torch.backends.cuda.matmul.fp32_precision
if precision != "none":
return precision
return torch.get_float32_matmul_precision()
def get_autotune_cache_fp32_precision() -> str:
"""Return a cache key for CUDA and MKLDNN matmul precision."""
cuda_precision = torch.backends.cuda.matmul.fp32_precision
cpu_precision = torch.backends.mkldnn.matmul.fp32_precision
if cuda_precision == "none" and cpu_precision == "none":
return torch.get_float32_matmul_precision()
return f"cuda={cuda_precision};mkldnn={cpu_precision}"

@janbernloehr

Copy link
Copy Markdown

Thanks for picking up #196728. I found that the older #189955 already fixes the same two call sites—PersistentCache.lookup and create_precompile_key—as part of a broader migration of framework-internal legacy precision readers. I think this PR should be closed as a duplicate in favor of #189955.

There is also an edge case in the helper introduced here: when CUDA precision is "none" but MKLDNN was written through the new per-backend API, falling back to torch.get_float32_matmul_precision() can still raise the mixed-API error. Keying only on CUDA also collapses legacy "high" (cuda=tf32,mkldnn=tf32) and "medium" (cuda=tf32,mkldnn=bf16) to the same value. #189955 avoids both problems by using the resolved CUDA+MKLDNN precision pair.

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.
@indrajeetapache

Copy link
Copy Markdown
Contributor Author

Addressed the review feedback in the latest commit:

  • Replaced the CUDA-only helper with fp32_matmul_precision_key() that keys on the resolved (cuda, mkldnn) pair via torch._C._get_fp32_precision_getter (never calls the legacy getter).
  • That fixes the MKLDNN-only mixed-API edge case and keeps legacy "high" (tf32/tf32) vs "medium" (tf32/bf16) distinct in the cache key.

@drisspg @janbernloehr — this aligns with the cache-key approach in #189955 for the two overlapping inductor sites. #189955 is older/broader (also covers flex attention, ROCm CK, benchmark util) and already approved; happy to close this as a duplicate once that lands, or keep this as a narrower #196728-focused path if maintainers prefer. Please advise.

cc @zasdfgbnm @ptrblck @drisspg @janbernloehr @coconutruben @jataylo

@janbernloehr

Copy link
Copy Markdown

Thanks for working on this fix. The underlying issue has since been resolved in main by 7949d704 from #196860.
That change updates the same two Inductor call sites PersistentCache.lookup and create_precompile_key to avoid the legacy precision getter when the per-backend precision is bfx9, and includes regression coverage. We also independently verified the landed fix on GB200 with compiled FlexAttention forward and backward.

Since the fix is now in main, this PR appears to be superseded and can be closed. Thanks again for investigating and preparing the patch.

@ezyang
ezyang requested a review from drisspg September 21, 2026 19:04
@ezyang ezyang added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Sep 21, 2026
@drisspg

drisspg commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

We should wait for jeffs pr to land if it hasnt

See Why was my issue or PR closed?.

@drisspg drisspg closed this Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests module: inductor open source 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.

Inductor autotune cache calls legacy get_float32_matmul_precision() and hard-fails under fp32_precision="bfx9"

5 participants