← Back to Pull requests

feat(Kimi-K3): run each MoE layer's post-attention work as one gfx950 launch in single-request decode #3613

Closed
unknown closed this pull request without merging on Nov 19, 2026 01:22 UTC
View on GitHub
unknown unknown commented on Nov 18, 2026 23:02 UTC
Author

Summary

This PR adds the "MoE layer tail": a persistent gfx950 Gluon kernel for Kimi-K3 and Kimi-K3.1. It serves decode steps that hold one request, including speculative verify steps. In such a step, it runs each MoE layer's work after the attention core as one launch per rank:

  • the attention o-projection, and its all-reduce across the 8 TP ranks through each other's memory

  • the MLP-side AttnRes mix and the post-attention RMSNorm

  • the router (sigmoid plus correction bias, top-16 of 896), the latent down projection and the latent RMSNorm

  • the routed experts (MXFP4) and the shared experts

  • the latent up projection and the layer output

  • the next layer's attention input, when that layer mixes it from a prepared block partial
  • The kernel is a persistent launch of 256 workgroups. Its stages hand data to each other through tagged words in an uncached scratch buffer. The ranks exchange partial sums through an uncached peer buffer that every rank maps with HIP IPC. Each rank adds the 8 parts in rank order, so every rank computes the same sums.

    How the model uses it:

  • The kernel is registered in a new kimi_k3 family (ops moe_tail, moe_tail_prepare and moe_tail_pack). Its API is tokenspeed_kernel.ops.model.kimi_k3, and that module's README describes it.

  • While the model builds its layers, it asks the API which expert bank layout the tail reads. When a tail serves the deployment, the MoE layers store their routed experts once, in that layout (N16).

  • Before graph capture, the model prepares the per-rank buffers in a collective step. It then checks and packs every MoE layer and binds the tail to all of them. If any layer fails the checks, no layer uses the tail.

  • A decode forward whose row count equals the verify width, that is a step with exactly one request, runs each layer's tail. The decision uses only the forward mode and the row count, so every rank runs the same layers. Every other forward uses the regular path.

  • The regular gfx950 MXFP4 SiTU kernels (decode and both prefill stages) read N16 banks, so one copy of the experts serves both paths. Their outputs are bit-identical to the default gdot128 layout: in decode, and in prefill above 2,048 rows. At or below 2,048 rows, the prefill adds routes with BF16 atomics, so its output varies from run to run in either layout. N16 banks are about 19 GB per GPU smaller than gdot128 at Kimi-K3.1 sizes, and the runtime gives that memory to the KV cache.

  • The projection-plus-partials GEMV in ops/gemm/gluon.py now accepts Kimi-K3.1's 3808-row decode projection, so the attention input projection can also produce the next layer's AttnRes block partials.
  • Numerics: the tail quantizes the routed experts' activations to MXFP4 (A4W4), where the regular decode MoE uses FP8 (A8W4). A step that the tail serves therefore does not give the same values as the regular path. A request's decode numerics depend on whether other requests share its steps. GSM8K accuracy is unchanged within noise (below).

    Limits:

  • gfx950 with 256 to 1024 compute units.

  • Kimi-K3 sizes at TP8, with one TP group for attention and MoE on one node (HIP IPC), and 1 to 8 rows, fixed per server.

  • The model keeps the regular path in these cases: attention DP, EP or PP, deterministic collectives, unfused norms, a prefill-only role, quantized or split attention projections, non-BF16 router, latent or shared-expert weights, and other expert formats or sizes.

  • To turn the tail off, set TOKENSPEED_KERNEL_OVERRIDE_KIMI_K3_MOE_TAIL=none.

  • The larger KV pool also grows a host KV tier sized by --kvstore-ratio, by about 2.1× at ratio 8. Set --kvstore-size to keep the previous host-tier size, for example 107 GB per rank at ratio 8.

  • Every wait in the kernel has a 60 s deadline. A rank that stalls past it inside a decode step aborts the server with a GPU memory fault. One example is a first-time compile with CUDA graphs off.
  • Benchmarks and accuracy

    Kimi-K3.1 on 8x MI355X (gfx950), TP8, one request in flight, DSpark speculative decoding (7 draft steps, 8 rows per verify step), greedy sampling. Serving numbers compare the base with this PR; decode and GSM8K were measured on one build of this PR, prefill and AgentX on another. Those builds' tail code compiles to the same instructions as this branch.

    Decode verify step, median client step gap:

    | Workload | Regular path | Tail | Change |
    |---|---|---|---|
    | 50k-token prompts, real acceptance (base vs this PR, 4 interleaved starts) | 14.66 ms | 13.89 ms | −5.2% (95% CI −5.3% to −5.1%) |
    | 200k-token prompts, AgentX flags (one build, tail off vs on) | 15.24 ms | 14.40 ms | −5.5% (95% CI −5.7% to −5.3%) |

    With the 50k-token prompts, accepted tokens per step were 3.6% lower (95% CI −7.0% to −0.5%), and the per-request output rate showed no gain.

    InferenceX AgentX, concurrency 1, 30 minutes per build, synthetic acceptance 4.16 (not quality evidence), one run per build, means:

    | Metric | Base | This PR | Change |
    |---|---|---|---|
    | Output throughput per GPU | 16.36 tokens per second | 16.74 tokens per second | +2.4% |
    | Time per output token | 3.67 ms | 3.47 ms | −5.5% |
    | Time to first token | 1.65 s | 1.57 s | −5.0% |
    | End-to-end latency | 6.69 s | 6.41 s | −4.2% |

    Prefill, time to first token with 1 output token, mean of 20 requests per arm (2 interleaved starts each):

    | Input tokens | Base | This PR | Change |
    |---|---|---|---|
    | 2,048 | 230.5 ms | 233.9 ms | +1.5% |
    | 16,384 | 819.7 ms | 823.4 ms | +0.4% |

    GSM8K, first 256 test questions, greedy, up to 2,048 output tokens, real acceptance:

    | Metric | Base | This PR | Change |
    |---|---|---|---|
    | Accuracy | 243 of 256 (94.9%) | 242 of 256 (94.5%) | −0.4 points (95% CI −2.4 to +1.6) |
    | Decode output rate per request, mean | 404.5 tokens per second | 419.9 tokens per second | +3.8% (95% CI +1.8% to +5.8%) |

    Tests on this branch:

  • pre-commit (all files) and ruff with undefined-name checks pass.

  • CPU model runtime tests: 1,131 passed, 26 failed. The same 26 fail on the base.

  • test_kernel_api_selection.py, on one GPU: 499 passed, 122 skipped, 0 failed.

  • The gfx950 MoE tests that build the changed prefill stage, including 10 N16-versus-gdot128 bitwise cases: 390 passed.

  • The 8-GPU tail test (widths 1, 4 and 8, a block-write case, two steps across the step-count wrap): 4 of 4 passed.

  • The off switch, checked on CPU: no tail layout with it set, n16 without it.

  • The broader gfx950 kernel tests for the changed MoE, GEMV and shared extern code ran on an earlier commit with the same tail, GEMV and extern code. They gave 1,295 passed, 0 failed and 75 skipped, and the 13 skipped torchrun cases passed on all 8 ranks.
  • Diff: 650 lines (+487 / -163) across 8 files Reviewer: Yu-Zhewen Review duration: 139m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.52) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #3613"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 22.5% · Safe to merge
    22.5%