Skip to content

[Feat] kimi-k3.1: MegaMoE, breakable prefill graphs and DFlash under prefill CP - #48710

Merged
Fridge003 merged 3 commits into
mainfrom
k31-pcp/2-megamoe-bcg
Nov 7, 2026
Merged

Fridge003 merged 3 commits into
mainfrom
k31-pcp/2-megamoe-bcg

Conversation

@Fridge003

@Fridge003 Fridge003 commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Original authors: @Fridge003

Port of #48414

MegaMoE, breakable prefill CUDA graphs (BCG) and DFlash under Kimi-K3.1 prefill CP (radixark/sglang-k31#34), the second PR of the split of #48504. Depends on the core PR (base branch k31-pcp/1-core).

Against the raw port, rebased onto #46890, which landed the interleave BCG path for the GLM DSA family:

  • The dsa_indexer.py change is dropped: main already runs the indexer split op under every PCG/BCG prefill.
  • The indexer split op keeps main's cp_live_store_rows for the K store and adds only the interleave pieces: query this rank's live rows, skip the K-only path, and keep the top-k rows past its logical tokens at -1.
  • The capture-time fill uses eager_on_graph(True, on_capture=...). The raw port passed capture_stub=, renamed to on_capture in [Refactor] bcg: Delete capture leftovers and rename capture_stub to on_capture #46792, which raises TypeError when the module is imported.
  • The trtllm prefill RoPE trim is dropped (main has it via [Feat] dsa: Extend interleave-CP breakable prefill graphs to GlmMoeDs… #46890).
  • InterleaveCPBCGInput adds Kimi-K3.1 to main's supported architectures; EP is admitted for Kimi only as MegaMoE over the CP group, and GLM keeps main's rules.

Left for the next PR: the padding mask moves into the MegaMoE pre-dispatch kernel.

Split of #48504. Stack: #48709 (core) → #48710 (MegaMoE / BCG / DFlash) → #48711 (fused kernels) → #48712 (PP × PCP BCG). Independent: #48713 (shared-expert TP across PCP), #48714 (trtllm DSA), #48715 (Linker PCP L3).

Modifications

  • MegaMoE (kimi_k3.py, mega_moe_hook.py):
    • Zero the input and route the padding rows of a static graph bucket to -1 before dispatch, and zero their output.
    • Allocate the EP symmetric workspace at load, so it counts against the KV budget.
    • Kimi-K3.1 joins MEGA_MOE_NO_FALLBACK_ARCHS. The token budget now bounds eager decode / verify rows (this requires --max-running-requests outside a PD prefill node) and prefill rows (chunk, --max-prefill-tokens and prefill graph buckets).
  • BCG (interleave_bcg.py, dsa_prefill_cuda_graph.py, outer-model pp_group / capture_aux_hidden_states / logits_processor properties): admit Kimi-K3.1 to interleave BCG.
  • trtllm DSA (dsa_backend.py): pad 65–127 query heads to 128 for trtllm-gen; attention TP 1 under CP-TP group sharing leaves all of Kimi's heads on one rank.
  • DFlash:
    • Allow integrated as well as PD prefill.
    • Mooncake lets a PD pair with different physical TP sizes exchange a replicated single-entry MLA draft KV.
  • Linear attention (linear/utils.py): size the verify scratch index for CUDA graph batch padding as well as eager padding.

Accuracy Tests

8× B300, PCP8 + EP8 MegaMoE + BCG + DFlash2, TRTLLM DSA, measured on sglang-k31 dev with the original PR (not re-run on this branch):

  • GSM8K first 500: 493/500 = 98.6%.
  • AIME26 30 × 16: 457/480 = 95.21%.

Speed Tests and Profiling

BS1 / ISL16384 / OSL1, TP8 MoE → EP8 MegaMoE (sglang-k31 dev):

  • Mean prefill layer time: 7.036 → 5.119 ms (−27.2%).
  • Unprofiled request median: 675.4 → 497.8 ms.
  • BCG: all eight ranks replay, and every MegaMoE kernel is captured.

Review notes

The new --max-running-requests requirement also applies to non-CP Kimi-K3.1 MegaMoE launches (unified and decode).

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ❌ Run #42322986151
Latest PR Test (Extra): ❌ Run #42322985976
Latest PR Test (AMD ROCm 10): ❌ Run #42322986023

Tip

Base CI only covers the core cases, and its workload may not exercise your change. Use /rerun-test to run the tests your change touches, or copy this prompt for an agent:

Find the registered tests this PR affects and run them with /rerun-test, following https://github.com/sgl-project/sglang/blob/main/.agents/skills/rerun-test/SKILL.md

@claude claude 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.

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.

Tip: disable this comment in your organization's Code Review settings.

@Fridge003

Copy link
Copy Markdown
Collaborator Author

Suggestion: reuse more of #46890's interleave-CP path in pcg_dsa_indexer_prefill_split

This PR already reuses most of #46890 (InterleaveCPBCGInput._SUPPORTED_ARCHITECTURES, cp_live_store_rows, the trtllm cp_local_rope_positions narrowing). The remaining divergence is in the indexer split op: cp = is_interleave_extend(forward_batch) adds a second CP branch on top of the one #46890 already validated for GLM (main L161-L182). On main, the gathered K carries the bucket padding at its tail, cp_live_store_rows trims the store to the live rows, and the padded q_fp8 goes to top-k unchanged.

Since cp is true for every interleave-CP model, the extra branch also changes GLM-5.2/5.3 and GLM-5.3 Flash under BCG. Could we fold it into the existing in_cp path and keep only the pieces that are generic fixes?

  1. Keep, as a generic fix: if k_only and not cp plus the if not return_indices: return after the store. _forward_cuda_k_only does no CP all-gather, so a top-k-reuse layer (return_indices=False) under CP would write the local K to global slots. Please key it on in_cp like the rest of the function.
  2. Keep, as a generic fix: the top-k tail topk_result[logical_tokens:].fill_(-1) and the on_capture fill. Filling the padded rows with -1 is also safe for GLM.
  3. Drop if possible: the truncation of x / q_lora / positions to per_rank_actual_token (L81-L99). [Feat] dsa: Extend interleave-CP breakable prefill graphs to GlmMoeDs… #46890 already handles the bucket rows through the tail padding plus cp_live_store_rows. This is the main behavior change for GLM. If Kimi-K3.1 really needs it, please explain why, and gate it so GLM keeps main's path.
  4. Drop, or gate to Kimi: not cp and indexer._aiter_fused_fp8_active(...). Kimi does not run on ROCm today, and this moves GLM CP on AMD from the aiter fused path to the bf16 path.

With (1) and (2) only, Kimi and GLM would share a single indexer CP path. Validation needed afterwards:

MEGA_MOE_NO_FALLBACK_ARCHS = frozenset(
{
"DeepseekV4ForCausalLM",
"KimiK31ForConditionalGeneration",

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

If K31 has no fallback, we need to throw error when chunked preill size is larger than MegaMoE workspace num tokens * EP size

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done in fb51283: for Kimi-K3.1, validate_mega_moe_token_budget now raises when the largest prefill forward (--chunked-prefill-size / --max-prefill-tokens / prefill graph buckets) exceeds SGLANG_OPT_DEEPGEMM_MEGA_MOE_NUM_MAX_TOKENS_PER_RANK x ep_size.

@Fridge003

Copy link
Copy Markdown
Collaborator Author

Folded the indexer split op into #46890's in_cp path in fb51283:

  • Dropped the live-row truncation of x / q_lora / positions, and the not cp gate on the aiter fused path. GLM is back on main's path.
  • Kept only the generic fixes, keyed on in_cp: a top-k-reuse layer under CP skips the local K-only store and goes through the gathered store, which returns before top-k; and under interleave CP, top-k rows past this rank's logical tokens are filled with -1, including the capture-time fill.

Still to validate on GPU: Kimi-K3.1 graph vs eager match with interleave BCG, and GLM's #46890 boundary matrix and gsm8k.

Fridge003 and others added 3 commits November 6, 2026 17:12
…prefill CP

Port of radixark/sglang-k31#34, split out of #48504 and
rebased onto #46890.

- MegaMoE: mask the CP padding rows of a static graph bucket before
  dispatch and after combine; allocate the EP symmetric workspace at load so
  it counts against the KV budget; bound the Kimi MegaMoE eager decode /
  verify rows and the prefill rows (chunk, max prefill tokens and prefill
  graph buckets) in the token-budget check.
- Breakable prefill graphs: admit Kimi-K3.1 to the interleave BCG adapter
  (EP only as MegaMoE over the CP group); run the DSA indexer split op on
  this rank's live rows and keep the unused top-k rows at -1, also during
  capture (on_capture).
- trtllm DSA: pad 65-127 query heads to 128 for trtllm-gen (attention TP 1
  under CP-TP group sharing leaves every head on one rank).
- DFlash: allow integrated as well as PD prefill; let a PD pair register a
  replicated MLA draft KV across different physical TP sizes.
- Size the linear-attention verify scratch for CUDA graph batch padding.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…d bound MegaMoE prefill rows

The indexer split op keeps #46890's in_cp path for every interleave-CP
model: no live-row truncation, and the aiter fused path stays ungated.
Only the generic fixes remain: a top-k-reuse layer under CP stores the
gathered K instead of the local K-only path, and the top-k rows past this
rank's logical tokens stay -1.

Kimi-K3.1 MegaMoE no longer requires --max-running-requests, and its
largest prefill forward must fit the workspace of the EP group since it
has no non-mega fallback.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Allocating every MoE layer's MegaMoE workspace in post_load_weights charges
it to the static memory budget. That is what the prefill-CP recipes are
sized for, but the existing Kimi-K3 MegaMoE recipes (e.g. the
TestKimiK3B300MegaMoE TP8/EP8/DCP8 launch at --mem-fraction-static 0.85)
take it from the non-static slack as on main; charging ~33 GB there leaves
the mamba state cache a negative budget and the server fails to start.
Keep the eager allocation for attn_cp_size > 1 only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Fridge003
Fridge003 force-pushed the k31-pcp/2-megamoe-bcg branch from 08b99bf to 8558d86 Compare November 7, 2026 01:14
@Fridge003
Fridge003 merged commit 79a3b21 into main Nov 7, 2026
108 of 127 checks passed
@Fridge003
Fridge003 deleted the k31-pcp/2-megamoe-bcg branch November 7, 2026 01:17
mattteochen added a commit to mattteochen/sglang that referenced this pull request Nov 9, 2026
Conflicts:
- environ.py / environment_variables.mdx: keep
  SGLANG_ENABLE_NEXTN_DRAFT_PREFILL_CUDA_GRAPH; drop SGLANG_ENABLE_SPLITKV_VERIFY
  and SGLANG_NGRAM_FORCE_GREEDY_VERIFY, which main removed.
- dsa_prefill_cuda_graph.py: keep main's on_capture wrapper of
  bcg_dsa_indexer_prefill_split (sgl-project#48710) and this PR's top-k-only break.
- dsa_backend.py imports: union of main's cp/dcp imports and the cuda graph
  config imports.

Adapted to main:
- get_cuda_graph_max_batch_size moved to sglang.srt.layers.dp_attention (sgl-project#48781).
- The DSA backend's DCP size is now attn_dcp_size.
- Keep the in-graph prefill attention off for 65-127 query heads per rank, which
  main pads to 128 for trtllm-gen (_trtllm_padded_heads) and
  forward_prefill_in_graph does not.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant