Skip to content

[Kernel][Perf] Add GLM-5 and Kimi-K3 MonoKernels - #1204

Merged
coderfeli merged 14 commits into
mainfrom
codex/kimi-k3-mxfp4-fused
Sep 28, 2026
Merged

coderfeli merged 14 commits into
mainfrom
codex/kimi-k3-mxfp4-fused

Conversation

@coderfeli

@coderfeli coderfeli commented Sep 27, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

This PR adds focused GLM-5 and Kimi-K3 decode MonoKernels for TP8 MI355X and now organizes them under one top-level package:

kernels/monokernel/
├── glm/                  # GLM-5 indexed full-layer MonoKernel
├── k3/                   # Kimi-K3 MLA/KDA model-specific code and tools
├── gemm_a16w16.py        # shared gfx950 A16W16 GEMM
├── mxfp8_linear.py       # shared small-batch MXFP8 projection
├── symmetric_allreduce.py# shared tagged BF16 all-reduce
└── config/layout/ops/packing/reference/runtime/weights.py
  • Integrates the complete GLM-5 Indexer + sparse MLA + router + MXFP4 MoE + TP reductions + residual update from [Kernel][Perf] GLM-5 MonoKernel: indexer + mla + moe fused kernel for a full decode layer #1205 at one GPU launch per layer.
  • Keeps the fastest Kimi-K3 MLA and KDA staged paths.
  • Adds the complete Kimi-K3 KDA + AttnRes + latent-MoE MonoKernel at one GPU launch per layer.
  • Supports true ordered Kimi-K3 MTP. For S speculative tokens, state_indices has shape [S + 1]: token s reads the convolution/recurrent snapshot at state_indices[s] and writes its complete post-token snapshot to state_indices[s + 1].
  • Keeps Kimi-K3 layer 0's dense FFN out of scope.

The public interfaces are intentionally narrow:

  • kernels.monokernel.glm.Glm5MonoKernel
  • kernels.monokernel.k3.KimiK3MonoKernel

Generic gfx950 GEMM, MXFP8 projection, tagged symmetric all-reduce, low-level AMDGPU operations, packing, layouts, runtime/IPC, reference helpers, and weight contracts are shared at kernels/monokernel/. Model-specific schedules and state semantics remain under glm/ and k3/. Unused experimental storage-resolution helpers, shared-expert Torch code, and packing aliases were removed; no source-inspection tests were added.

MTP schedule

The retained implementation preserves the real snapshot chain rather than treating speculative tokens as independent requests.

  • Four 32-row recurrent-state splits per head.
  • At S=8, one recurrence CTA advances two consecutive tokens and keeps its FP32 state fragment resident across the pair: 4 token pairs x 12 heads x 4 splits = 192 CTAs.
  • Gate projection is issued before the causal-convolution dependency wait so all gate work can overlap.
  • Recurrence scalar loads are staged through LDS.
  • Recurrence writes FP32 state snapshots; grouped norm CTAs produce a separate packed-BF16 output-projection buffer.
  • S<=4 keeps dedicated stage-local LDS views because that layout benchmarks faster; S=8 uses a safe phase-overlaid LDS arena while keeping activation and MFMA reduction storage disjoint.

TileRT design lineage

The resident-grid schedule, tagged-pair mailbox protocol, and phase-overlaid LDS arena follow the TileRT/GLM MonoKernel design lineage. The relevant GLM and Kimi source comments and kernels/monokernel/README.md reference:

https://github.com/SemiAnalysisAI/InferenceX/tree/8ac98344b038a3f2da20a565fe9b974772a67ef9

The ordered KDA snapshot pipeline and state-resident MTP recurrence are Kimi-K3-specific FlyDSL extensions.

Launch count

Path S=4 S=8
GLM-5 full Indexer MonoKernel 1 1
Kimi MLA staged full layer 7 7
Kimi KDA staged full layer 7 9
Kimi KDA true-MTP staged full layer 7 9
Kimi KDA true-MTP MonoKernel 1 1

Performance

Hardware: 8 x AMD Instinct MI355X, TP8, position 3000. Kimi results use HIP-graph replay with 32 layer invocations and 40 measured repeats; values are critical-rank medians.

Path Samples FlyDSL Mono kernel latency ATOM latest Relative
GLM-5 full Indexer MonoKernel 4 78.4-78.6 us 205.8-213.7 us 2.62-2.73x faster
GLM-5 full Indexer MonoKernel 8 118.2-118.3 us 223.4 us 1.89x faster
Kimi MLA staged, layer 3 4 115.54 us 222.56 us 1.93x faster
Kimi MLA staged, layer 3 8 127.76 us 253.93 us 1.99x faster
Kimi KDA staged, independent decode, layer 1 4 113.66 us 197.30 us 1.74x faster
Kimi KDA staged, independent decode, layer 1 8 128.42 us 227.38 us 1.77x faster
Kimi KDA staged, true MTP 4 120.83 us 207.24 us 1.72x faster
Kimi KDA staged, true MTP 8 155.75 us 241.80 us 1.55x faster

True-MTP attention alone is 39.29 us at S=4 and 63.57 us at S=8. Relative to the previous retained MTP baseline (39.76/71.73 us), S=8 attention improves by 11.4%. The complete one-launch MTP layer improves from 164.94/260.99 us to 153.11/243.15 us at S=4/S=8.

Excluding the out-of-scope layer-0 dense FFN, weighting 24 MLA layers and 68 KDA layers gives a 92-layer independent-decode core estimate of 1.786x at S=4 and 1.827x at S=8 versus ATOM. This is not a full-model end-to-end result.

Validation

  • 16 passed: complete GLM-5 and Kimi-K3 GPU integration suites.
  • True MTP TP8 correctness at S=4/S=8: rank agreement, finite output, ordered convolution/recurrent snapshots, routing selection, and full-layer output checks all pass.
  • Independent KDA decode and Kimi MLA S=4/S=8 show no regression.
  • GLM-5 TP8 Indexer + native MXFP4 correctness passes at S=4/S=8.
  • Black, Ruff, Python compilation, and git diff --check pass.
  • The branch includes [Kernel][Perf] GLM-5 MonoKernel: indexer + mla + moe fused kernel for a full decode layer #1205 and is merged with current main (e4245c23).
  • The two local handoff files remain untracked and are not part of the PR.

Cold-link validation used a local external-LLVM compatibility bridge because the bundled parser cannot consume newer ROC-DL kernel metadata for some GEMM binaries. That local bridge is not included in this PR.

No new third-party dependencies.

@coderfeli
coderfeli force-pushed the codex/kimi-k3-mxfp4-fused branch from 51c7081 to 12f8f93 Compare September 27, 2026 01:14
@coderfeli coderfeli changed the title [Kernel][Perf] Add fused GLM-5 and Kimi-K3 MLA MoE [Kernel][Perf] Add fused GLM-5 and Kimi-K3 MLA/KDA MoE Sep 27, 2026
@coderfeli coderfeli changed the title [Kernel][Perf] Add fused GLM-5 and Kimi-K3 MLA/KDA MoE [Kernel][Perf] Add fused GLM-5 and Kimi-K3 MLA/KDA MoE / 添加融合的 GLM-5 与 Kimi-K3 MLA/KDA MoE Sep 27, 2026
@coderfeli coderfeli changed the title [Kernel][Perf] Add fused GLM-5 and Kimi-K3 MLA/KDA MoE / 添加融合的 GLM-5 与 Kimi-K3 MLA/KDA MoE [Kernel][Perf] Add fused GLM-5 and Kimi-K3 MLA/KDA MoE Sep 28, 2026
@coderfeli coderfeli changed the title [Kernel][Perf] Add fused GLM-5 and Kimi-K3 MLA/KDA MoE [Kernel][Perf] Add GLM-5 and Kimi-K3 MonoKernels Sep 28, 2026
Felix Li and others added 11 commits September 28, 2026 07:43
Signed-off-by: Felix Li <felix@local>
Add an opt-in ATOM full-layer benchmark and kernel profile so the sparse indexer cost is measured separately from the preselected-index decoder body. Make the Kimi CPU golden test independent of the process-wide default device.
Signed-off-by: Felix Li <felix@local>
Signed-off-by: Felix Li <felix@local>
融合 Kimi KDA 输出投影与 TP8 归约,并更新双语性能结果。
新增 Kimi-K3 KDA 完整层单次 GPU launch 实现,并保留按批量选择的最快 staged 路径。
保留按 batch 选择的 staged KDA 性能路径,修复完整单 launch 层的延迟初始化,并补充 S=8 覆盖。

同步格式化新 KDA 算子,并记录合入 GLM-5 #1205 后的最终性能结果。
移除与完整 GLM-5/Kimi-K3 层无关的通用 GEMM、MoE、旧 benchmark 与单元测试改动。

将 Kimi 专用 MLA、KDA、MoE 和通信实现归入 kernels/kimi_k3,并仅保留两个端到端 GPU 测试。
统一 GLM-5 与 Kimi-K3 MonoKernel 的共享目录、命名和公开接口。\n\n删除未使用的 KDA recurrence 适配器和通用 MLA/MoE wrapper,保留经过性能验证的 staged 基线与单 launch 路径。
为 Kimi-K3 MonoKernel 增加真正的顺序 MTP 状态链、单次 launch 的因果 KDA 管线,以及 TP8 正确性覆盖;保持普通 decode 与 MLA 路径性能不回退。
@coderfeli
coderfeli force-pushed the codex/kimi-k3-mxfp4-fused branch from c240241 to 4ea131b Compare September 28, 2026 07:45
@coderfeli coderfeli changed the title [Kernel][Perf] Add GLM-5 and Kimi-K3 MonoKernels [Kernel][Perf] Add GLM-5 and Kimi-K3 MonoKernels / 增加 GLM-5 与 Kimi-K3 单体内核 Sep 28, 2026
@coderfeli coderfeli changed the title [Kernel][Perf] Add GLM-5 and Kimi-K3 MonoKernels / 增加 GLM-5 与 Kimi-K3 单体内核 [Kernel][Perf] Add GLM-5 and Kimi-K3 MonoKernels Sep 28, 2026
Felix Li added 3 commits September 28, 2026 07:57
补充 Kimi-K3 MonoKernel 的顺序 MTP 状态链接口、命令行用法和单 launch 调度说明。
Move GLM and Kimi-K3 under kernels/monokernel, promote reusable GEMM, MXFP8, collective, and AMDGPU primitives, and retain shape-specialized LDS layouts for the best MTP performance.\n\n将 GLM 和 Kimi-K3 统一整理到 kernels/monokernel,提取可复用的 GEMM、MXFP8、集合通信和 AMDGPU 基础模块,并保留按 shape 专门化的 LDS 布局以维持最佳 MTP 性能。
Merge the latest upstream CI and causal-flash scheduling changes before updating PR #1204.\n\n在更新 PR #1204 前合入最新上游 CI 与 causal-flash 调度改动。
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