Skip to content

[ROCm][Inductor] Fix CKTILE float16 autotune dtype mismatch - #193902

Closed
jagadish-amd wants to merge 5 commits into
pytorch:mainfrom
jagadish-amd:ck-tile_universal_gemm_followup
Closed

jagadish-amd wants to merge 5 commits into
pytorch:mainfrom
jagadish-amd:ck-tile_universal_gemm_followup

Conversation

@jagadish-amd

@jagadish-amd jagadish-amd commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Fix CK-Tile float16 autotune silently dropping all fp16 GEMM kernels. ops() named half-precision instances "FP16" while check_dtypes compared against _TORCH_DTYPE_TO_CK[float16] == "F16", so every fp16 kernel was filtered out before autotuning. Rename catalog labels to "F16" to match the existing C++ alias in globals().

Also decouple the universal GEMM dtype catalog from ck_dtype_to_size. GEMM_DTYPES explicitly declares which dtypes get GEMM instances. ck_dtype_to_size remains for alignment and padding math only. _CK_DTYPE_ALIASES is the single source of truth for C++ using aliases, and globals() renders from it.

Tests parametrize fp16 and bf16 across template emit and compile coverage, and assert that ops() implements GEMM_DTYPES, that rendered globals() matches _CK_DTYPE_ALIASES, and that every GEMM dtype has a size-table entry.

This is a follow-up to the CK-Tile inductor GEMM fix #189646
Authored with Cursor Grok 4.6.

cc @jeffdaily @sunway513 @jithunnair-amd @pruthvistony @ROCmSupport @jataylo @hongxiayang @naromero77amd @pragupta @jerrymannil @xinyazhang @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben

ops() named half-precision instances "FP16" while check_dtypes compared
against _TORCH_DTYPE_TO_CK[float16] == "F16", so every fp16 kernel was
silently dropped. Rename ops() and ck_dtype_to_size to "F16" to match
the existing C++ alias.

This is a follow-up to the CK-Tile inductor GEMM fix pytorch#189646

Signed-off-by: Jagadish Krishnamoorthy <[email protected]>
@pytorch-bot pytorch-bot Bot added ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests module: inductor module: rocm AMD GPU support for Pytorch labels Aug 18, 2026
@pytorch-bot

pytorch-bot Bot commented Aug 18, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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 3d379af with merge base 4863ab7 (image):

BROKEN TRUNK - The following job 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.

@pytorch-bot

pytorch-bot Bot commented Aug 18, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@pytorch-bot

pytorch-bot Bot commented Aug 18, 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.

@jagadish-amd

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "topic: not user facing"

@pytorch-bot pytorch-bot Bot added the topic: not user facing topic category label Aug 18, 2026
@jithunnair-amd jithunnair-amd added the ciflow/trunk Trigger trunk jobs on your pull request label Aug 18, 2026
@zou3519 zou3519 added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Aug 18, 2026
@zou3519
zou3519 requested a review from jeffdaily August 18, 2026 13:39
@jansel

jansel commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

@claude review these changes

@claude

claude Bot commented Aug 18, 2026 •

Copy link
Copy Markdown

Claude finished @jansel's task in 9m 24s —— View job


The dtype-label rename is correct and the new fp16 subtest is a genuine regression test, but flipping TestCKTileUniversalGemmTemplate._DTYPE from bfloat16 to float16 silently drops bf16 from the only test that actually compiles emitted CK-Tile instances.

Full review (AI-generated by Claude, reviewed output below)

PR Review: #193902

Summary

Renames the half-precision CK-Tile dtype label from "FP16" to "F16" so ops() labels match _TORCH_DTYPE_TO_CK[torch.float16], un-breaking fp16 kernel selection. I confirmed the fix is correct on both sides of the seam: check_dtypes (ck_tile_universal_gemm_template.py:440-445) now matches, and emit_ck_instance renders using ADataType = F16; against the using F16 = ck_tile::half_t; alias in ck_tile_template.py:47 — so pre-fix these instances would not have compiled even if they had passed the filter. Two problems: the PR fixes the symptom rather than the string duplication that caused it, and it trades away bf16 compile coverage.

Code Quality

The label "F16" is now hardcoded in five independent places that must agree or fp16 silently disappears again:

  1. ck_tile_template.py:15 — _TORCH_DTYPE_TO_CK[torch.float16]
  2. ck_tile_template.py:26 — ck_dtype_to_size key
  3. ck_tile_template.py:47 — the emitted C++ using F16 = ... alias
  4. ck_tile_universal_gemm_template.py:218,256,296 — three literal copies inside ops()
  5. test_ck_backend.py:621 — _CK_DTYPE

That duplication is the bug. ops() should derive its dtype list rather than restate it — iterating CKTileTemplate.ck_dtype_to_size keys would collapse (2) and (4) into one source and additionally guarantee the self.ck_dtype_to_size[...] lookups in check_alignments (line 522), check_alignments/CShuffle (line 578), and check_block_tile_size (lines 601-602) can never KeyError for a generated instance. Same for _CK_DTYPE in the test: _CK_DTYPE = CKTileTemplate._TORCH_DTYPE_TO_CK[_DTYPE] removes the third restatement. This matters concretely for the next dtype: _TORCH_DTYPE_TO_CK already maps fp8 to "F8"/"BF8", and adding those instances to ops() reproduces exactly this failure mode.

Fix this →

Testing

bf16 compile coverage is lost. TestCKTileUniversalGemmTemplate._DTYPE / _CK_DTYPE were swapped from bfloat16 to float16 (test_ck_backend.py:620-621) rather than parametrized. _DTYPE feeds _make_template, which feeds _find_ck_tile_op and thence test_offered_instance_compiles — the one test that actually shells out to hipcc on the emitted instance (_compile_ck_tile_source, line 641). After this change no bf16 instance is ever compile-checked, across all three pipelines and both epilogues. The whole point of the PR is that both dtypes are now reachable, so both should be covered: parametrize _DTYPE over (torch.float16, torch.bfloat16) instead of swapping the class attribute. Per CLAUDE.md, dtype axes belong in @parametrize/@dtypes, not a class constant.

Fix this →

The regression test is far more expensive than it needs to be. I traced the new ("CKTILE", torch.float16) subtest and it does fail pre-fix: gen_ops returns an empty list, add_choices appends nothing, and with max_autotune_gemm_backends="CKTILE" neither use_aten_gemm_kernels() nor use_triton_template() contributes a fallback, so select_algorithm.py:4063 raises NoValidChoicesError. So it is a real regression test — but it needs a ROCm GPU, an installed CK library, and a full autotune plus AOTI compile, and it is multiplied by the autotune_in_subproc x use_aoti cross product. A pure-Python assertion in TestCKTileUniversalGemmTemplate that every dtype label produced by ops() is both a value of _TORCH_DTYPE_TO_CK and a key of ck_dtype_to_size catches this entire bug class in milliseconds and would have caught it before the original PR landed. That is the regression test this fix warrants; the e2e subtest is the belt, not the suspenders.

Fix this →

The e2e @parametrize restructure itself is fine — subtest with tuple values against a "a,b" arg string is supported (common_utils.py:822-826, :898), and the explicit name= per subtest reads better than the dict-lookup name_fn it replaces.

Recommendation

Request Changes — narrowly, on the bf16 coverage regression in TestCKTileUniversalGemmTemplate. The production fix is correct and I would take it as-is; parametrizing _DTYPE instead of swapping it is a small change that keeps bf16 instances compile-checked. Deriving the dtype labels rather than restating them in five places is the durable fix for the class of bug and is worth doing in this PR while the context is fresh.

| Branch: `ck-tile_universal_gemm_followup`

Signed-off-by: Jagadish Krishnamoorthy <[email protected]>

@jeffdaily jeffdaily left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed this with an AI assistant; findings below are all in the new test, the production fix looks right to me. Expanding the dtype axis is fine, don't trim it — the extra compile time is acceptable.

Requesting changes. The production fix is correct: _TORCH_DTYPE_TO_CK[torch.float16]
is "F16", which matches both check_dtypes and the `using F16 = ck_tile::half_t;`
alias in globals(). Pre-fix, "FP16" instances would not have compiled even if they
had passed the filter. All the problems below are in the test.

1. test_ck_backend.py:818 - `assertEqual(labels, sized)` can never fail. ops() now
   builds its dtype list from ck_dtype_to_size (ck_tile_universal_gemm_template.py:190),
   so labels is by construction exactly set(ck_dtype_to_size). Only the
   `labels - mapped` assertion carries any signal.

2. The test does not check what ck_tile_template.py:24 says it checks. That comment
   says the keys must match the C++ aliases in globals(), but the test compares
   against _TORCH_DTYPE_TO_CK.values() = {F32, F64, F16, BF16, I32, I8, F8, BF8},
   while globals() only declares F8, BF8, F16, F32, BF16. emit_ck_instance splices
   the label straight into `using ADataType = {{datatype_a}};`
   (ck_tile_universal_gemm_template.py:676-678), so a label like I8 or F64 would pass
   this test and produce HIP that does not compile. Please hoist the aliases in
   globals() into a dict, render globals() from it, and assert against that.

3. ck_dtype_to_size now does two unrelated jobs. It lives on the base CKTileTemplate
   and is documented as bytes-per-element for alignment math (check_alignments,
   check_block_tile_size). After this PR its keys also decide which universal GEMM
   instances exist. A future ck_tile template (scaled mm, conv) that needs fp8
   alignment math would add F8/BF8 to that table and silently generate a full fp8
   GEMM catalog that has never been compile-tested. Declare the GEMM dtype list
   explicitly in ck_tile_universal_gemm_template.py and keep the dependency one-way:
   assert in the test that every GEMM dtype has a size entry, rather than deriving
   the catalog from the size table.

4. test_ck_backend.py:627 and :636 - the `dtype=torch.float16` defaults on
   _make_template and _find_ck_tile_op are dead; all five call sites pass dtype
   explicitly. Leaving the default means the next test added to this class silently
   gets fp16-only coverage, which is how bf16 coverage was lost in the first revision
   of this PR. Make dtype a required parameter.

5. test_ck_backend.py:625 - `self._ck_tile.ops.cache_clear()` has no effect. Nothing
   in the suite mutates ck_dtype_to_size, so this just rebuilds the instance catalog
   before every test in the class. Drop it, or say which test needs it.

6. The five-line @parametrize("dtype", ...) block is repeated four times (:820, :838,
   :870, :894) plus once at :345. Hoist it to a module-level
   `_parametrize_dtype = parametrize("dtype", (torch.float16, torch.bfloat16),
   name_fn=_dtype_test_name)` and apply it as a decorator. Reuse across functions is
   safe; _TestParametrizer.__call__ only writes to the function it decorates.

Not asking for any change to the dtype coverage itself. Parametrizing
test_offered_instance_compiles over both dtypes is right, keep it.

jagadish-amd and others added 3 commits August 24, 2026 14:13
The consistency test added in this PR asserts relations that hold by construction and cannot fail. ops() builds its dtype list from GEMM_DTYPES, so labels == set(GEMM_DTYPES) is an identity; globals() renders from _CK_DTYPE_ALIASES, so comparing the rendered aliases back to that dict is the same identity. Reproducing the original mistake under the new structure, GEMM_DTYPES = ("FP16", "BF16") with a matching size-table key, leaves the test green.

The seam that actually broke is unguarded: emit_ck_instance splices the dtype label straight into `using ADataType = {{datatype_a}};`, so a label with no rendered alias emits HIP that does not compile. Asserting labels <= cpp_aliases covers that, is not an identity because the two sides come from the instance catalog and from the rendered source respectively, and subsumes the `cpp_aliases - {"Row", "Col"}` fixup, which hardcoded the current contents of CKTileGemmTemplate.globals() and would have failed spuriously the next time that method gained a `using`.

Kept the labels == set(GEMM_DTYPES) assertion even though it is an identity today. It is the only line tying ops() output to the declared catalog, and it costs nothing if a future instance family hardcodes a dtype literal instead of iterating GEMM_DTYPES. The alias comparison was different: its only reachable failure mode was a false positive.

Also renders the alias block through writeline rather than hand-indenting each line by 16 spaces so that IndentedBuffer.splice's textwrap.dedent strips them back off; the emitted source is byte-identical. The ck_dtype_to_size comment claimed its keys must match _CK_DTYPE_ALIASES, which is false in the direction it reads, {F16, BF16} against five aliases, and enforced by nothing; it now says what the table is for and points at GEMM_DTYPES as the catalog.

Test Plan:

Ran the CK-Tile template tests on gfx90a, ROCm 7.14:

```
python test/inductor/test_ck_backend.py TestCKTileUniversalGemmTemplate
```

37 tests, 13 skipped. The 12 test_offered_instance_compiles cells skip without a configured rocm.ck_dir; they exercise hipcc in CI.

Confirmed the tightened assertion catches the bug this PR fixes by reintroducing it, GEMM_DTYPES = ("FP16", "BF16") plus the matching ck_dtype_to_size key, and rerunning:

```
python test/inductor/test_ck_backend.py TestCKTileUniversalGemmTemplate.test_ops_dtype_labels_agree_with_maps
```

Before this commit: OK. After: AssertionError, {'FP16', 'BF16'} not <= {'BF8', 'F8', 'Col', 'F32', 'BF16', 'Row', 'F16'}.

Authored with the assistance of Claude.

@jeffdaily jeffdaily left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. I pushed 3d379af to tighten the new test so it fails on the bug this PR fixes; please pull before your next push.

@jeffdaily

Copy link
Copy Markdown
Collaborator

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your 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

Advanced Debugging
Check the merge workflow status
here

@jagadish-amd
jagadish-amd deleted the ck-tile_universal_gemm_followup branch August 27, 2026 00:25
orrangetabby17 pushed a commit to orrangetabby17/pytorch that referenced this pull request Sep 2, 2026
…193902)

Fix CK-Tile float16 autotune silently dropping all fp16 GEMM kernels. ops() named half-precision instances "FP16" while check_dtypes compared against _TORCH_DTYPE_TO_CK[float16] == "F16", so every fp16 kernel was filtered out before autotuning. Rename catalog labels to "F16" to match the existing C++ alias in globals().

Also decouple the universal GEMM dtype catalog from ck_dtype_to_size. GEMM_DTYPES explicitly declares which dtypes get GEMM instances. ck_dtype_to_size remains for alignment and padding math only. _CK_DTYPE_ALIASES is the single source of truth for C++ using aliases, and globals() renders from it.

Tests parametrize fp16 and bf16 across template emit and compile coverage, and assert that ops() implements GEMM_DTYPES, that rendered globals() matches _CK_DTYPE_ALIASES, and that every GEMM dtype has a size-table entry.

This is a follow-up to the CK-Tile inductor GEMM fix pytorch#189646
Authored with Cursor Grok 4.6.

Pull Request resolved: pytorch#193902
Approved by: https://github.com/jeffdaily

Co-authored-by: Jeff Daily <[email protected]>
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 ciflow/trunk Trigger trunk jobs on your pull request Merged module: inductor module: rocm AMD GPU support for Pytorch open source topic: not user facing topic category 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.

7 participants