Skip to content

[BugFix][CUDA] Fall back for non-MMA GEMM K shapes - #2818

Open
JayceSu98 wants to merge 1 commit into
tile-ai:mainfrom
JayceSu98:jayce/fix-gemm-layout-stride
Open

[BugFix][CUDA] Fall back for non-MMA GEMM K shapes#2818
JayceSu98 wants to merge 1 commit into
tile-ai:mainfrom
JayceSu98:jayce/fix-gemm-layout-stride

Conversation

@JayceSu98

@JayceSu98 JayceSu98 commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Fixes #2715

Summary

CUDA GEMM lowering selected the shared-memory layout and the MMA emitter independently. For K=20 and K=28, the swizzled layout fails its stride check; bypassing that check only moves the failure to the MMA atom divisibility check.

The layout and emitter now use the same MMA-shape check. Supported shapes keep the existing MMA path; other valid K extents use the padded shared-memory layout and the existing CUDA FMA emitter. Both choices are made in TileLang's GEMM lowering, so callers do not need to pad the mathematical K extent.

Changes

  • Add a shared GEMM-shape eligibility check before selecting tensor-core MMA.
  • Route non-MMA K extents through the existing CUDA FMA emitter.
  • Use the padded shared-memory layout when the bank-swizzle stride contract is not satisfied.
  • Add numerical CUDA regressions for K=16, K=20, and K=28.

Review Notes

  • K=16 remains on the existing optimized path.
  • K=20 and K=28 use the fallback path.
  • The change does not pad or reinterpret the mathematical K extent.

Validation

  • testing/python/issue/test_tilelang_issue_gemm_non_mma_k_fallback.py: 3 passed on NVIDIA A100.
  • testing/python/issue/test_tilelang_issue_gemm_non_mma_k_fallback.py: 3 passed on NVIDIA H100.
  • Numerical output compared against PyTorch GEMM for all three K shapes.
  • clang-format and git diff --check passed.

@github-actions

Copy link
Copy Markdown

👋 Hi! Thank you for contributing to the TileLang project.

Please remember to run pre-commit run --all-files in the root directory of the project to ensure your changes are properly linted and formatted. This will help ensure your contribution passes the format check.

We appreciate you taking this step! Our team will review your contribution, and we look forward to your awesome work! 🚀

@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

GEMM layout selection now falls back to padded layouts for unsupported strides, while instruction selection falls back from MMA to CUDA FMA for unsupported K shapes. A CUDA regression test covers FP16 GEMM cases with K values 16, 20, and 28.

Changes

GEMM shape fallback

Layer / File(s) Summary
GEMM dispatch and layout fallback
src/cuda/op/gemm.cc, src/layout/gemm_layouts.cc
MMA eligibility validates operand widths and K divisibility, unsupported shapes use CUDA FMA, and invalid swizzle strides use the padded GEMM layout.
Regression validation
testing/python/issue/test_tilelang_issue_2715.py
A CUDA-parameterized FP16 GEMM test validates numerical results for K dimensions 16, 20, and 28.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Suggested reviewers: leiwang1999, yongqi-zhuo

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes address issue #2715 by selecting padded layouts for invalid strides and avoiding MMA selection for unsupported K shapes.
Out of Scope Changes check ✅ Passed All changes, including MMA eligibility checks and regression tests, directly support the linked issue and stated PR objectives.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the CUDA fallback added for GEMM K shapes that are incompatible with MMA.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@testing/python/issue/test_tilelang_issue_2715.py`:
- Around line 29-40: Extend test_gemm_falls_back_for_non_swizzle_stride to
inspect the lowered CUDA source or instruction output for each parameterized k
value. Assert that k=16 uses the optimized CUDA MMA/GEMM instruction path, while
k=20 and k=28 use the scalar FMA fallback, preserving the existing numerical
correctness assertion.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 4a6753e2-f5ad-458d-a650-2e23913931eb

📥 Commits

Reviewing files that changed from the base of the PR and between e01c498 and a2949c0.

📒 Files selected for processing (3)
  • src/cuda/op/gemm.cc
  • src/layout/gemm_layouts.cc
  • testing/python/issue/test_tilelang_issue_2715.py

Comment thread testing/python/issue/test_tilelang_issue_gemm_non_mma_k_fallback.py
T.gemm selected bank-swizzled layouts and tensor-core MMA for K extents such as 20 and 28. The layout constructor rejected the stride first; bypassing that check alone only exposed the MMA atom divisibility assertion.

Route unsupported MMA K atoms through the existing CUDA FMA implementation and use the padded shared-memory layout whenever a swizzle stride is invalid. Add A100/H100 numerical coverage for aligned and fallback K shapes.

Co-authored-by: dingsg <shengge.ding@enflame-tech.com>
@JayceSu98
JayceSu98 force-pushed the jayce/fix-gemm-layout-stride branch from a2949c0 to 962c804 Compare July 30, 2026 23:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][Fuzzer][ice-on-valid-code] T.gemm with a K not divisible by 8 aborts with an internal stride ICHECK instead of using the padded-layout fallback

1 participant