Skip to content

perf(k3): block-parallel router top-k replaces the serial TileLang scan - #939

Open
xiaguan wants to merge 1 commit into
mainfrom
perf/k3-router-topk-parallel
Open

perf(k3): block-parallel router top-k replaces the serial TileLang scan#939
xiaguan wants to merge 1 commit into
mainfrom
perf/k3-router-topk-parallel

Conversation

@xiaguan

@xiaguan xiaguan commented Aug 19, 2026

Copy link
Copy Markdown
Collaborator

What

Replaces the TileLang router_topk_batched kernel — whose biased top-k ran as a serial scan on thread 0 (TOPK×E = 16×896 iterations per row, 65.7µs per launch, ~6ms of every EP16 verify round across the 92 expert layers) — with a hand-written CUDA kernel (csrc/k3/k3_router_topk.cu) doing a block-parallel argmax per selection round.

Every arithmetic step keeps the retired kernel's exact spelling, so outputs are bit-identical:

  • f32 sigmoid via plain expf (no fast-math), bias added in f32
  • strict < comparisons with the lowest-index tie-break: each thread scans its stride-256 subsequence ascending, the shuffle/shared reduction prefers the strictly-greater value and breaks equal values to the lower index — exactly the serial first-match selection
  • denominator accumulated in selection order; (w / (den + 1e-20)) * (float)rs unchanged

The TileLang factory, its generator plan, and the AOT launcher entry are retired (the vendored tilelang_defs.py stays verbatim minus the removed factory). Batch and expert count become plain runtime launch values — no per-bucket instantiation.

Correctness

Gates on the pruned 224-expert checkpoint (any selection divergence would blow the golden logits far past the calibrated 2.0-ULP noise floor):

  • golden_decode 13/13 (mega + masked-chain transports)
  • spec_verify 6/6
  • kernels + k3 lib tests green

Perf

per launch per EP16 verify step (92 launches)
serial TileLang scan 65.7µs ~6.0ms
parallel argmax 10.3µs (max 11.2µs, 240-launch capture) ~0.95ms

🤖 Generated with Claude Code

The TileLang router_topk_batched kernel ran the whole biased top-k as a
serial scan on thread 0 — TOPK x E iterations per row, 65.7us per launch
at 896 experts, ~6ms of every EP16 verify round across the 92 expert
layers. Replace it with a hand-written CUDA kernel whose selection is a
block-parallel argmax per round while every arithmetic step keeps the
retired kernel's spelling, so the outputs are bit-identical: f32 sigmoid
via plain expf, bias added in f32, strict less-than comparisons with the
lowest-index tie-break (each thread scans its stride ascending; the
reduction prefers the strictly greater value and breaks equal values to
the lower index, which is exactly the serial first-match), the
denominator accumulated in selection order, and the division-then-scale
weight normalization.

The TileLang factory, its generator plan and the AOT launcher entry are
retired; batch and expert count become plain runtime launch values (no
per-bucket instantiation).

Gates on the pruned 224-expert checkpoint: golden decode 13/13 (mega +
masked chain — any selection divergence would blow past the 2.0-ULP
noise floor), spec_verify 6/6, lib tests green.

Perf: 65.7us -> 10.3us per launch (bucket-one golden replay capture,
240 launches, max 11.2us); an EP16 verify step's 92 router launches drop
from ~6.0ms to ~0.95ms.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: xiaguan <751080330@qq.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 14bb15ffd7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@@ -0,0 +1,151 @@
// Kimi-K3 MoE router: sigmoid scores plus biased top-k selection.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Add the required DCO sign-off

This commit has no Signed-off-by: trailer, so the repository's DCO check will reject it regardless of the code's correctness; recreate or rebase the commit with --signoff before submitting it.

AGENTS.md reference: AGENTS.md:L184-L184

Useful? React with 👍 / 👎.

Comment on lines +9 to +11
k3_land_rms_norm_rbs_batched.cu k3_attnres_scores_batched.cu
k3_add2_batched.cu k3_attnres_mix_batched.cu
k3_mul_sigmoid_batched.cu k3_situ_batched.cu

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Update the retired router's kernel inventories

After removing this TileLang family, the authoritative inventories still describe router_topk_batched as generated TileLang code: pegainfer-k3/kernels/README.md still reports eleven families, 420 instantiations, and the removed launcher, while pegainfer-kernels/KERNELS.md:141 still names the retired .cu artifact and serial implementation. This now misdirects anyone diagnosing or regenerating the K3 kernel set, so update those records alongside the implementation change.

AGENTS.md reference: AGENTS.md:L168-L170

Useful? React with 👍 / 👎.

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