Skip to content

feat: add gemm_n16384_k3072 definition and reference test#374

Open
yyihuang wants to merge 2 commits into
flashinfer-ai:mainfrom
yyihuang:feat/def-gemm_n16384_k3072
Open

feat: add gemm_n16384_k3072 definition and reference test#374
yyihuang wants to merge 2 commits into
flashinfer-ai:mainfrom
yyihuang:feat/def-gemm_n16384_k3072

Conversation

@yyihuang

@yyihuang yyihuang commented Apr 12, 2026

Copy link
Copy Markdown
Contributor

Summary

Add gemm_n16384_k3072 definition and reference test.

This GEMM shape (N=16384, K=3072) appears in:

  • Llama 3.2 3B mlp.gate_up_proj (fused gate+up: 2 × 8192 = 16384)
  • MiniMax M2 MoE FC1/W13 (same hidden_size=3072, 2 × intermediate_size=16384)
Field Value
Definition gemm_n16384_k3072
Op type gemm
Models Llama 3.2 3B, MiniMax M2
N 16384
K 3072
dtype float16

Reference Test Output

Testing GEMM N=16384, K=3072 Reference Implementation
Summary: 6/6 tests passed
✓ All tests passed!

Coverage Update

MiniMax M2: 14/15 → 15/16 definitions present

PR2 (HuggingFace trace)

Link to be added after PR2 opens.

Summary by CodeRabbit

  • New Features

    • Added a new GEMM kernel definition to enhance computation acceleration capabilities.
  • Documentation

    • Updated model coverage metrics reflecting the newly available kernel definition, expanding tracked definitions.
  • Tests

    • Implemented test suite for validating the newly added GEMM kernel configuration.

Add GEMM N=16384, K=3072 definition and reference test.
Applies to Llama 3.2 3B mlp.gate_up_proj (fused gate+up: 2×8192=16384)
and MiniMax M2 MoE FC1/W13 (same hidden_size=3072, 2×intermediate=16384).

- Add definition JSON with float16 dtype
- Add reference test (all 6 M-value configs pass, zero numerical difference)
- Update MiniMax M2 coverage: 14/15 → 15/16

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Apr 12, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

This PR introduces a new GEMM kernel definition (gemm_n16384_k3072) for the MiniMax M2 model's MoE FC1 layer, including a JSON schema file, a Python reference implementation with validation tests, and updates to the model coverage documentation.

Changes

Cohort / File(s) Summary
GEMM Kernel Definition & Tests
flashinfer_trace/definitions/gemm/gemm_n16384_k3072.json, flashinfer_trace/tests/references/test_gemm_n16384_k3072.py
Adds a new GEMM definition for fixed N=16384, K=3072 dimensions with float16 tensors, along with a reference PyTorch implementation and comprehensive test suite supporting variable M dimensions with correctness validation and tolerance checking.
Model Coverage Documentation
docs/model_coverage.mdx
Updates MiniMax M2 model coverage table to reflect the new GEMM kernel definition, incrementing the tracked definition count from 15 to 16 and coverage metric from 14 / 15 to 15 / 16 definitions present.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • #345 — Directly adds the same gemm_n16384_k3072 GEMM definition and updates model coverage documentation.
  • #203 — Modifies the same docs/model_coverage.mdx file to track GEMM definitions introduced in this PR.

Suggested reviewers

  • yzh119

Poem

🐰✨ A new kernel hops into the trace,
N sixteen-three-eight-four finds its place,
Tests verify with torch so bright,
Coverage metrics shine just right! 🌟

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely describes the main changes: adding a new GEMM kernel definition and its reference test.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 and usage tips.

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request introduces a new GEMM operation definition (gemm_n16384_k3072) for Llama 3.2 3B and MiniMax M2 models, updates the model coverage documentation, and adds a reference test script. Feedback was provided to ensure the test script handles environments without CUDA gracefully by returning True instead of False, preventing unnecessary CI failures on non-GPU nodes.

Comment on lines +48 to +50
if device == "cpu":
print("WARNING: CUDA not available, skipping test")
return False

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.

medium

The current implementation returns False when CUDA is not available. This causes the main() function to report a failure and exit with code 1, which will break CI pipelines running on non-GPU nodes. Since this is a reference test script, it should ideally skip gracefully or return True if the environment doesn't support the required hardware.

Suggested change
if device == "cpu":
print("WARNING: CUDA not available, skipping test")
return False
if device == "cpu":
print("WARNING: CUDA not available, skipping test")
return True

MiniMax M2 uses MoE with intermediate_size=1536, so gate_up output is
2*1536=3072, not 16384. This GEMM belongs to Llama 3.2 3B (intermediate=8192).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

@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: 3

🧹 Nitpick comments (1)
flashinfer_trace/tests/references/test_gemm_n16384_k3072.py (1)

78-78: Remove unnecessary f prefix from the static string.

Line 78 uses an f-string with no variable interpolation, triggering Ruff's F541 lint check.

Proposed fix
-    print(f"\nOutput tensor comparison:")
+    print("\nOutput tensor comparison:")
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@flashinfer_trace/tests/references/test_gemm_n16384_k3072.py` at line 78, The
print call uses an unnecessary f-string with no interpolation—change the print
invocation that currently reads print(f"\nOutput tensor comparison:") to a
regular string literal (print("\nOutput tensor comparison:")) to satisfy Ruff's
F541; update the print statement in the test_gemm_n16384_k3072.py test where the
"Output tensor comparison:" message is emitted.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@docs/model_coverage.mdx`:
- Around line 684-690: The table incorrectly lists `gemm_n16384_k3072` under
MiniMax M2; remove that row (or move the `gemm_n16384_k3072` entry to the Llama
3.2 3B section) and update the MiniMax M2 coverage count from "15 / 16" to the
correct value after removing the misplaced definition so the coverage math
reflects the actual definitions present; ensure any reference to `MoE gate /
topk / experts`, `top_k_sampling_from_probs_v200064`,
`top_k_top_p_sampling_from_probs_v200064`, and
`top_p_sampling_from_probs_v200064` remain unchanged.

In `@flashinfer_trace/tests/references/test_gemm_n16384_k3072.py`:
- Around line 47-50: The test currently treats "CUDA not available" as a failure
by returning False; change the behavior to mark the test as skipped instead:
when device is set with device = "cuda" if torch.cuda.is_available() else "cpu"
and you detect device == "cpu", either call pytest.skip("CUDA not available,
skipping test") or return True (so it isn't treated as a failure) and update the
identical blocks around the 104-122 region similarly; adjust imports if you use
pytest.skip (import pytest) and keep the same warning message text.
- Around line 10-13: Update the top docstring attribution in
test_gemm_n16384_k3072.py: replace the incorrect model name "MiniMax M2" with
the correct mapping "Llama 3.2 3B" in the descriptive comment that currently
reads "This corresponds to MiniMax M2 MoE FC1/gate-up projection (W13)" so the
docstring accurately reflects that the test/definition is mapped to Llama 3.2
3B.

---

Nitpick comments:
In `@flashinfer_trace/tests/references/test_gemm_n16384_k3072.py`:
- Line 78: The print call uses an unnecessary f-string with no
interpolation—change the print invocation that currently reads print(f"\nOutput
tensor comparison:") to a regular string literal (print("\nOutput tensor
comparison:")) to satisfy Ruff's F541; update the print statement in the
test_gemm_n16384_k3072.py test where the "Output tensor comparison:" message is
emitted.
🪄 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: 69bb4b16-310d-4feb-a17a-8ec4ace07085

📥 Commits

Reviewing files that changed from the base of the PR and between 534a6f6 and 31e1407.

📒 Files selected for processing (3)
  • docs/model_coverage.mdx
  • flashinfer_trace/definitions/gemm/gemm_n16384_k3072.json
  • flashinfer_trace/tests/references/test_gemm_n16384_k3072.py

Comment thread docs/model_coverage.mdx
Comment on lines +684 to +690
| `gemm_n16384_k3072` | gemm (MoE FC1/W13) | 🟡 |
| MoE gate / topk / experts | moe | — |
| `top_k_sampling_from_probs_v200064` | sampling | 🟡 |
| `top_k_top_p_sampling_from_probs_v200064` | sampling | 🟡 |
| `top_p_sampling_from_probs_v200064` | sampling | 🟡 |

**Coverage**: 14 / 15 definitions present. Workloads not yet collected.
**Coverage**: 15 / 16 definitions present. Workloads not yet collected.

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.

⚠️ Potential issue | 🟠 Major

Fix incorrect MiniMax M2 attribution and coverage count.

Line 684 adds gemm_n16384_k3072 under MiniMax M2, but this shape is attributed to Llama 3.2 3B in this PR context; keeping it here also makes the Line 690 coverage math incorrect for MiniMax M2.

📝 Proposed fix
 | `gemm_n256_k3072` | gemm (MoE gate) | 🟡 |
-| `gemm_n16384_k3072` | gemm (MoE FC1/W13) | 🟡 |
 | MoE gate / topk / experts | moe | — |
 | `top_k_sampling_from_probs_v200064` | sampling | 🟡 |
 | `top_k_top_p_sampling_from_probs_v200064` | sampling | 🟡 |
 | `top_p_sampling_from_probs_v200064` | sampling | 🟡 |

-**Coverage**: 15 / 16 definitions present. Workloads not yet collected.
+**Coverage**: 14 / 15 definitions present. Workloads not yet collected.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
| `gemm_n16384_k3072` | gemm (MoE FC1/W13) | 🟡 |
| MoE gate / topk / experts | moe ||
| `top_k_sampling_from_probs_v200064` | sampling | 🟡 |
| `top_k_top_p_sampling_from_probs_v200064` | sampling | 🟡 |
| `top_p_sampling_from_probs_v200064` | sampling | 🟡 |
**Coverage**: 14 / 15 definitions present. Workloads not yet collected.
**Coverage**: 15 / 16 definitions present. Workloads not yet collected.
| MoE gate / topk / experts | moe ||
| `top_k_sampling_from_probs_v200064` | sampling | 🟡 |
| `top_k_top_p_sampling_from_probs_v200064` | sampling | 🟡 |
| `top_p_sampling_from_probs_v200064` | sampling | 🟡 |
**Coverage**: 14 / 15 definitions present. Workloads not yet collected.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@docs/model_coverage.mdx` around lines 684 - 690, The table incorrectly lists
`gemm_n16384_k3072` under MiniMax M2; remove that row (or move the
`gemm_n16384_k3072` entry to the Llama 3.2 3B section) and update the MiniMax M2
coverage count from "15 / 16" to the correct value after removing the misplaced
definition so the coverage math reflects the actual definitions present; ensure
any reference to `MoE gate / topk / experts`,
`top_k_sampling_from_probs_v200064`, `top_k_top_p_sampling_from_probs_v200064`,
and `top_p_sampling_from_probs_v200064` remain unchanged.

Comment on lines +10 to +13
This corresponds to MiniMax M2 MoE FC1/gate-up projection (W13):
Input: hidden_size = 3072
Output: 2 * intermediate_size = 2 * 8192 = 16384

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.

⚠️ Potential issue | 🟡 Minor

Update the model attribution in the docstring.

Line 10 still says MiniMax M2, but this test/definition is mapped to Llama 3.2 3B in this PR. Please update the text to avoid future remapping mistakes.

📝 Proposed fix
-    This corresponds to MiniMax M2 MoE FC1/gate-up projection (W13):
+    This corresponds to Llama 3.2 3B MLP gate-up projection:
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@flashinfer_trace/tests/references/test_gemm_n16384_k3072.py` around lines 10
- 13, Update the top docstring attribution in test_gemm_n16384_k3072.py: replace
the incorrect model name "MiniMax M2" with the correct mapping "Llama 3.2 3B" in
the descriptive comment that currently reads "This corresponds to MiniMax M2 MoE
FC1/gate-up projection (W13)" so the docstring accurately reflects that the
test/definition is mapped to Llama 3.2 3B.

Comment on lines +47 to +50
device = "cuda" if torch.cuda.is_available() else "cpu"
if device == "cpu":
print("WARNING: CUDA not available, skipping test")
return False

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.

⚠️ Potential issue | 🟠 Major

Don’t treat “skipped (no CUDA)” as failed tests.

Line 49 says “skipping test”, but Line 50 returns False, which is then counted as a failure and triggers exit(1) in main().

🛠️ Proposed fix
 def test_correctness(M=128, atol=1e-2, rtol=1e-2):
@@
     device = "cuda" if torch.cuda.is_available() else "cpu"
     if device == "cpu":
         print("WARNING: CUDA not available, skipping test")
-        return False
+        return None
@@
 def main():
@@
-    passed = 0
+    passed = 0
+    skipped = 0
@@
     for M in test_M_values:
         try:
-            if test_correctness(M):
+            result = test_correctness(M)
+            if result is True:
                 passed += 1
+            elif result is None:
+                skipped += 1
         except Exception as e:
             print(f"✗ Test failed with exception: {str(e)}")
@@
-    print(f"Summary: {passed}/{total} tests passed")
+    effective_total = total - skipped
+    print(f"Summary: {passed}/{effective_total} tests passed, {skipped} skipped")
@@
-    if passed == total:
+    if skipped == total:
+        print("⚠ All tests skipped (CUDA not available).")
+        return
+    if passed == effective_total:
         print("✓ All tests passed!")

Also applies to: 104-122

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@flashinfer_trace/tests/references/test_gemm_n16384_k3072.py` around lines 47
- 50, The test currently treats "CUDA not available" as a failure by returning
False; change the behavior to mark the test as skipped instead: when device is
set with device = "cuda" if torch.cuda.is_available() else "cpu" and you detect
device == "cpu", either call pytest.skip("CUDA not available, skipping test") or
return True (so it isn't treated as a failure) and update the identical blocks
around the 104-122 region similarly; adjust imports if you use pytest.skip
(import pytest) and keep the same warning message text.

@yyihuang yyihuang marked this pull request as draft April 13, 2026 17:17
@yyihuang yyihuang marked this pull request as ready for review April 14, 2026 07:00
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.

1 participant