Skip to content

[Fix] specs: Speak megakernel API version 3 and apply verify penalties through step_penalties - #85

Closed
Oasis-Git wants to merge 1 commit into
mainfrom
megakernel-api-v3
Closed

Oasis-Git wants to merge 1 commit into
mainfrom
megakernel-api-v3

Conversation

@Oasis-Git

Copy link
Copy Markdown
Collaborator

Motivation

sgl-project/sglang#52054 (e96e967570, "Apply exact per-row sampling penalties in verify") bumped MEGAKERNEL_API_VERSION in python/sglang/kernels/plugin_kernels/nvidia/spec.py from 2 to 3. In v3, VerifySamplingView drops additive_penalties, scaling_penalties and the acc_additive_penalties / acc_scaling_penalties properties. It carries step_penalties instead: a VerifyPenaltiesView whose apply_verify(logits, draft_tokens, next_child=None, next_sibling=None) applies each verify row's own penalties in place. SamplingBatchInfo lost the acc_* tensors as well.

Every spec in this package still declares version 2. On SGLang main, --enable-nvidia-megakernel therefore refuses every model at argument resolution, for example on GB300 with kimi_k31:

sgl_nvidia_mega.kimi_k31.spec:SPEC uses megakernel API version 2; this SGLang requires version 3

CI did not catch this because it pins SGLang cf6133a, which is still at v2.

Modifications

The megakernel plugin contract changes between cf6133a and SGLang main 2544c9ac34, and how each lands here:

SGLang change Contract change Breaking? Packages here
#52054 e96e967570 API 3: VerifySamplingView.step_penalties (VerifyPenaltiesView) replaces the additive/scaling penalty tensors yes (version bump) all specs; penalties consumers in spec_step, glm5_3/sm107, muse_spark (this PR)
#47930, #50157, #49411, #48414, #46210, #51676 Additive: Step prefill/draft modes, SupportsPrefillBody, RecurrentCommitIO, SupportsStateSlotReset, SupportsRecurrentCommit, SupportsDecodeTensorEntry, SupportsDFlashRuntime, ENTRIES += draft_step_fold, MegakernelSpec.{tensor_verify_binding_kind, tensor_verify_build, binding, prefill_graph}, EAGLE_DRAFT_CAPTURE_CHECK, FLAG_SETS_ENV. The K3.1 draft protocols that #49411 added were removed again in #51676. no (all have defaults) already used by kimi_k31, qwen3_5, glm5_3
#48414 6321f714dd binding.py drops the K3 Day-0 fields (aux_hidden_states, tokens_per_request, verify_scratch_indices, intermediate_conv_window, intermediate_ssm) and changes the MlaPoolView / KdaStepView / MlaStepView / KdaStateView signatures binding-level kimi_k3 (not in this PR)
#52438 2544c9ac34 A draft spec the server does not meet declines instead of refusing the server behaviour kimi_k31 draft at TP8 (no change needed)
#48007, #44588, #49376, #49719, #51074, #51719, #51983, #50748 Internal to SGLang's bindings (sub-pool names, DSpark estimator, q6 bound, ReplaySSM ring, removed flags, watermark interface, K3.1 request buckets, AMD) no none

Changes in this PR:

  • API version 3 in every spec. deepseek_v41, qwen3_5, glm5_3, muse_spark and kimi_k3 declare 3. The drafts (deepseek_v41/draft.py, qwen3_5/draft, glm5_3/draft.py, kimi_k31/draft/serving.py) take their target's version. kimi_k31/k3/spec.py used to derive its version from the host (2 if hasattr(_host_spec, "ENTRIES") else 1). It now declares 3, and the host import goes away.
  • The penalties consumers read step_penalties, the way SGLang's own verify does:
    • spec_step/lifted.py eagle_sample calls step_penalties.apply_verify(logits, candidates, retrieve_next_token, retrieve_next_sibling) where it used to add the repeated per-request tensors. This is the same hunk #52054 applied to SGLang's eagle_sample: candidates moves up, and apply_scaling_penalties is no longer imported.
    • Three gates now test step_penalties is not None to hand a step with penalties back to SGLang:
      • spec_step/runner.py (the step-graph gate);
      • glm5_3/sm107/verify_tail.py (the fused verify tail);
      • muse_spark/components/verify_topk_sample.py. Before this PR it read the stale acc_linear_penalties and the penalizer_orchestrator, which SGLang has removed.
    • Each of these four modules lists step_penalties (and apply_verify(next_child|next_sibling) for the lifted sampler) in SGLANG_READS.
  • CI pin: SGLANG_COMMIT moves from cf6133a (v2) to 2544c9ac34 (SGLang main at the time of writing, v3, includes #52438). There is no version any-of: a v3 spec cannot be tested against a v2 SGLang. [CI] test: Run the CPU unit tests against SGLang main #81 moves CI to unpinned SGLang main. If [CI] test: Run the CPU unit tests against SGLang main #81 lands first, this PR drops its unit-test.yml hunk; if this PR lands first, [CI] test: Run the CPU unit tests against SGLang main #81's diff deletes the pin as before.

Tests (added to existing files):

  • test/unit/test_plugin.py: each registered architecture resolves through find_spec at the installed SGLang's MEGAKERNEL_API_VERSION. This is one case per architecture, split out of test_register_registers_every_spec_and_no_hook, so a package left at the old version is named.
  • test/unit/test_kimi_k3_contract.py: asserts version 3; the mismatch check uses versions 2 and 4.
  • test/unit/test_glm5_3_sm107.py:
    • the verify tail and the step-graph gate hand back a real SamplingBatchInfo that has step_penalties;
    • the lifted eagle_sample, installed into SGLang's eagle_utils through hooks._eagle_sample() at ForwardMode.TARGET_VERIFY, calls apply_verify first, with the logits, the [bs, draft_token_num] candidates and the retrieve tree.

CPU unit suite, one file per process, on this branch vs origin/main on the same SGLang:

SGLang origin/main failures this PR failures fixed new
2544c9ac34 (main, new pin) 179 155 26 2*
e96e967570 (#52054) 170 146 26 2*

* test_every_registered_spec_resolves_at_this_sglangs_api_version[KimiK31ForConditionalGeneration|DFlash2DraftModel]: the local CPU env has no cutlass. The old loop in test_register_registers_every_spec_and_no_hook failed on the same import, at cf6133a too.

The 26 fixed cases are the version refusals in test_deepseek_v41_spec (21), test_kimi_k3_contract (2), test_qwen3_5_spec (1) and test_plugin (1), plus the spec_step/lifted.py contract read (1).

At the new pin, the remaining failures that are not local-env predate this PR. They come from SGLang main drift that this PR does not touch:

  • kimi_k31/dflash/runtime.py imports the helpers #52184 removed (fixed by the open DFlash runtime import fix);
  • kimi_k3 K3 Day-0 binding views;
  • qwen3_5 reads ServerArgs.enable_dp_attention;
  • mirror digests in glm5_3 / qwen3_5;
  • spec_step/runner.py reads srt/utils/common.py: is_cuda.

So CI at the new pin, like #81 at main, stays red until those land.

Eight deliberate mutants were each killed by a failure specific to them:

  • qwen3_5 left at 2;
  • kimi_k3 left at 2;
  • the lifted sampler without apply_verify, with the flat drafts, or without the tree;
  • the GLM tail and the step-graph gate reading a wrong field;
  • a wrong SGLANG_READS name.

pre-commit run --files <changed> passes; every changed file was format-clean on base.

GPU validation

Pending. One GB300 boot of kimi_k31 with --enable-nvidia-megakernel on SGLang main, to check that argument resolution and the build pass. The penalties path (frequency_penalty / repetition_penalty requests) runs through SGLang's apply_verify and is not changed on the kimi_k31 MK path.

Checklist

  • pre-commit run --files <changed> passes.
  • Tests under test/ cover the change (cases added to test_plugin.py, test_kimi_k3_contract.py, test_glm5_3_sm107.py).
  • Required SGLang bindings passed normal SGLang CI and are merged, or no binding change is needed. [Feat] speculative: Apply exact per-row sampling penalties in verify sglang#52054 is merged.
  • The tested SGLang and Mega SHAs and integration run link are named above. SGLang 2544c9ac34, e96e967570; GPU run pending.
  • Accuracy and performance E2E results against the matched baseline are attached for each affected supported model/hardware configuration. Pending GPU.
  • Required E2E cases ran on the appropriate hardware; missing coverage is listed explicitly. Pending GPU.
  • A new model adds one line to .github/CODEOWNERS that names its owner. N/A.

🤖 Generated with Claude Code

…s through step_penalties

SGLang #52054 bumped MEGAKERNEL_API_VERSION from 2 to 3: VerifySamplingView
replaces additive_penalties / scaling_penalties (and the acc_* properties)
with step_penalties, a VerifyPenaltiesView whose apply_verify applies each
verify row's own penalties, and SamplingBatchInfo no longer has the acc_*
penalty tensors. Every spec here still declared version 2, so
--enable-nvidia-megakernel refused every model at argument resolution on
SGLang main.

- Every spec declares version 3: deepseek_v41, qwen3_5, glm5_3, muse_spark,
  kimi_k3 and kimi_k31 (the drafts take their target's). kimi_k31/k3 no
  longer derives its version from the host module.
- The penalties consumers read step_penalties, as SGLang's own verify does:
  the lifted eagle_sample (spec_step) calls step_penalties.apply_verify with
  the candidates and the tree before any other adjustment; the step-graph
  gate (spec_step), the fused GLM verify tail (glm5_3/sm107) and the top-k
  verify sampler (muse_spark) hand back a step with penalties. Each lists
  step_penalties in SGLANG_READS.
- CI pins SGLang 2544c9ac34, which has version 3.

Tests: the kimi_k3 contract asserts version 3; test_plugin resolves each
registered spec at the installed SGLang's API version (one case per
architecture); test_glm5_3_sm107 covers the verify tail and step-graph
gates and the lifted eagle_sample's apply_verify call.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@YAMY1234 YAMY1234 left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. Unit CI is red from other SGLang changes the new pin pulls in, not from this PR; please fix those before merging.

weireweire added a commit to SemiAnalysisAI/InferenceX that referenced this pull request Nov 25, 2026
…0-03fde9cc

Root cause: nightly-dev-cu13-20261122-e96e9675 includes sgl-project/sglang#52054, which raised MEGAKERNEL_API_VERSION to 3 while its bundled sgl-nvidia-mega plugin still exposes API version 2 (sgl-project/sgl-nvidia-mega#85 is still open). All four agg points enable --enable-nvidia-megakernel and fail at startup on that image, whereas disagg does not enable the megakernel and stays on 20261122.

Fix: roll dsv41flash-fp4-gb300-dynamo-sglang-agentic-agg back to nightly-dev-cu13-20261120-03fde9cc (Dynamo 1.8.0.dev20261119, SGLang 0.0.0.dev1+g03fde9cc3) in nvidia-master.yaml, agg-variants.yaml, and the PR perf-changelog.yaml entry, leaving the disagg key on 20261122-e96e9675.

Validation: all four agg points previously succeeded on 20261120-03fde9cc in the PR sweep; local changelog, curve, and recipe generation checks pass.
@Oasis-Git

Copy link
Copy Markdown
Collaborator Author

Superseded by #81, which carries this change on main.

@Oasis-Git Oasis-Git closed this Nov 26, 2026
Oasis-Git pushed a commit that referenced this pull request Nov 30, 2026
…st or KV layout setting (#94)

* deepseek_v41: Let slot moe_balanced deal over --ep-num-redundant-experts 384

Root cause: with SGLang's --ep-num-redundant-experts 384 and the trivial placement
each EP rank already holds every routed expert in router-id order, but the spec
refused the flag and the routed pair took the layer's slot count (768) as the
router's expert count.

Fix: admit 384 redundant experts; the router's expert count is the slots less the
redundant ones; under moe_balanced with that layout the pair takes the rank's
storage whole (offset 0) and deals on the EP rank.

Validation: served at attention TP8 + MoE TP4 x EP2 on GB300 at one request: step
time and weights match SGLang's balanced expert storage once the four FlashInfer
tactics of the 384-local-expert MoE shape are pinned. Numerics change (the deal),
as slot moe_balanced already is.

* deepseek_v41: Turn the batch-1 slots and KV layout on by default

Root cause: serving DeepSeek-V4.1 at one request, attention TP8 + MoE TP4 x EP2,
at its best needed a hand-written SGLANG_NVIDIA_MEGAKERNEL_SLOTS list (the
shipped slots plus eight opt-in ones) and SGLANG_DSV4_KV_LAYOUT=v41_new for
slot attn_core_lean. A default start ran none of those.

Fix:
- Default-on slots: attn_wo_a_fp8, attn_index_weights_bf16, attn_index_q_first,
  attn_cluster_merge, draft_stage0_first, draft_embed and draft_final_collapse
  (bitwise; TP8_DEFAULT_SLOTS: a default start takes them at attention TP8
  only; at TP4 the group measured slower and changed seeded T=1 outputs, and
  listed they still run there), and attn_core_lean (numerics change; a default
  start takes it only on SGLang's v41_new KV layout). runtime.default_takes
  holds the rule; the attention and draft builds read it, and the startup
  summary lists a default slot left off as before. draft_embed is left off,
  not refused, when the draft has its own vocabulary.
- serving.py: a resolution step SGLang runs ahead of handle_nvidia_megakernel
  (register_resolution_hook, registered by plugin.register). With the
  megakernel on, DeepSeek-V4.1 at --tp 8 --ep-size 2, --max-running-requests 1
  and no PD disaggregation, it sets SGLANG_DSV4_KV_LAYOUT=v41_new when the user
  left it unset and attn_core_lean is requested, and logs it. Any other shape
  keeps SGLang's V4 layout.
- The redundant experts stay the user's choice: on that configuration without
  --ep-num-redundant-experts the step logs that moe_balanced is idle and that
  the flag turns it on at about 47 GB more weights per GPU.
- SGLANG_NVIDIA_MEGAKERNEL_SLOTS still replaces the list; hop_final_collapse,
  lm_head_pinned_tactic, draft_lm_head_mxfp8, hop_b_cap16 and
  hop_b_quad_trigger stay opt-in.
- docs/models/deepseek_v41.md: the default and opt-in tables, and the
  one-request launch without a slot list.

Numerics change: attn_core_lean (v41_new KV encodings, its own split and merge
order) now runs by default in one-request TP8 serving; it was already a
declared numerics change.

Validation: unit tests (test_deepseek_v41_serving.py, default-slot rules) on
CPU. Served at one request on two GB300 trays: with no slot list and
--ep-num-redundant-experts 384 the startup summaries, KV pool and outputs (T=0,
seeded T=1, finish reasons) equal the hand-written configuration's and the step
time is unchanged within 2 us; without the flag the layout is chosen and
moe_balanced logs idle; two requests keep the V4 layout.

* deepseek_v41: Declare SGLang megakernel API version 3

Root cause: SGLang main #52054 raised MEGAKERNEL_API_VERSION from 2 to 3
(VerifySamplingView replaces additive_penalties / scaling_penalties with
step_penalties). SGLang refuses a spec whose api_version differs, so the
DeepSeek-V4.1 spec at version 2 cannot start on SGLang main.

Fix: the DeepSeek-V4.1 package reads none of the removed fields, so only the
declared version moves; the draft spec follows spec.API_VERSION.

Validation: served on GB300 on SGLang main past #52054 with outputs identical
to version 2 on the SGLang before it. SGLang before #52054 refuses this spec.

* deepseek_v41: Keep megakernel API version 2 in this change

The move of every spec to megakernel API version 3, together with the unit
tests' SGLang pin, is #85. Declaring version 3 for one model here fails the
unit tests on the pinned SGLang, which requires version 2.

---------

Co-authored-by: weireweire <[email protected]>
Co-authored-by: Yangmin Li <[email protected]>
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.

2 participants