Skip to content

[Refactor] spec/dflash: Take the megakernel out of the DFLASH worker behind a guarded runtime binding - #51676

Merged
Oasis-Git merged 8 commits into
sgl-project:mainfrom
Oasis-Git:dflash-runtime
Nov 20, 2026
Merged

Oasis-Git merged 8 commits into
sgl-project:mainfrom
Oasis-Git:dflash-runtime

Conversation

@Oasis-Git

@Oasis-Git Oasis-Git commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

The Kimi-K3.1 day-0 merge put about 1k lines of NVIDIA-megakernel-only code into dflash_worker_v2.py:

  • the megakernel draft head, embed, aux append, greedy and sampled accepts, post-verify tail, rank-0 broadcast and KDA commit, inline in the per-round decode path;
  • seven megakernel_* accessors in kernel_plugins/nvidia/binding.py for the Supports* protocols behind them.

This code only runs with --enable-nvidia-megakernel and a Kimi-K3.1 build. Still, every DFLASH user has to read past it, and every DFLASH change has to keep it working.

Modifications

The megakernel's DFLASH code moves to the megakernel package. SGLang keeps one small module, kernel_plugins/nvidia/kimi_k31_dflash_binding.py (253 lines, next to the other Kimi K3.1 host bindings), and three whole-phase entry points. The stock decode round stays where it is in dflash_worker_v2.py; this PR only removes its megakernel branches.

1. Three whole-phase entries. A megakernel target build that runs the DFLASH round itself names its runtime class by "module:attribute" (spec.SupportsDFlashRuntime). The worker hands that class:

  • cls(binding, built) at the end of the worker's __init__, before the KV pools are sized, so the runtime's allocations come out of the KV budget;
  • draft_sampler(lm_head) in init_cuda_graphs, before the draft graph captures. It may supply the selector draft sampler folded into that graph; None keeps SGLang's;
  • decode_round(batch, on_publish=, grammar_barrier=) for the decode branch of forward_batch_generation. It runs the whole round, or returns None before launching anything and SGLang runs its own round. Every TP rank makes the same decision.

2. Binding and guard. The runtime class declares two things. Each has a table and a checker in SGLang:

runtime declares SGLang table checker what it gives
bindings: the worker state it reads BINDINGS: 17 names, each a worker member with a checked type DFlashBinding pull(name) reads the value on demand, past the worker's __getattr__, which would otherwise reach the target worker
guards: the setup value it expects per key GUARDS: 27 keys, each a reader. Examples: TP/PP size, DP attention, page size, dtype, draft architecture and KV dtype, block size, compact draft window, KV ring, selector sampling, decode graph batch sizes, verify mode, HiCache, hisparse, LoRA DFlashGuard unmet(worker) reads the running values on every TP rank, requires the ranks to agree, and names each key that differs

bind_dflash_runtime reads as: resolve the class, bind, guard, build. The outcomes:

  • raises at startup: a binding name or guard key SGLang does not have, ranks that disagree, or a binding of the wrong type. These mean the runtime and SGLang no longer match.
  • declines the runtime: a guard value that differs, e.g. TP8, or decode graph batch sizes beyond the megakernel's. TP rank 0 logs one warning naming each key with its actual and expected value, and SGLang's stock round runs.

So a runtime only ever runs on the setup it declared.

3. Resolution and fallback. These keep the existing rules:

  • the flag off, no build, or a build naming "" keeps SGLang's own round;
  • a megakernel build without SupportsDFlashRuntime logs one warning on TP rank 0, so an older plugin no longer falls back silently;
  • while a runtime is bound, the stock round keeps its aux append off the side stream.

Removed from SGLang:

  • from dflash_worker_v2.py: _MegakernelSelectorDraftSampler, _MegakernelTail and the worker's megakernel head, embed, aux, accept, tail, broadcast and KDA-commit code;
  • from binding.py: the seven megakernel_* draft/KDA accessors;
  • from spec.py: the matching Supports* protocols;
  • from dflash_sim_acc.py: the megakernel-only ring;
  • SGLANG_OPT_DFLASH_MEGAKERNEL_TAIL_GRAPH.

The worker's new code is 22 lines. With the megakernel off, behavior does not change.

Size:

  • source +335 / −1056, including the 253-line binding module;
  • tests +155 / −6, no new test files.

Companion PR

sgl-project/sgl-nvidia-mega#74, stacked on sgl-project/sgl-nvidia-mega#70. It moves the whole Kimi-K3.1 round into the package as K31DFlashRuntime and binds it through this module. It must land together with this PR.

Accuracy Tests

  • The DFLASH and nvidia plugin unit tests: the failure set equals the base's, plus the new cases.
  • test_megakernel_runtime.py covers resolution, the rank-0 warning, and the guard. Mutants that drop a binding or change a guard value fail the tests; all 16 mutants across this PR and the companion PR are killed, including a value mismatch that raises instead of declining and a missing binding that declines instead of raising.
  • The companion PR's CPU tests run the runtime's round order against these entries.

Benchmarking and Profiling

Kimi-K3.1-Pro on GB300 (one NVL72 domain, 4 nodes), TP16, one running request (graph bs [1]), AgentX 900 s, simulated acceptance 3.69. Baseline G0 is main + sgl-project/sgl-nvidia-mega#70. G3 is this PR + the companion mega PR. Both arms ran on the same nodes and environment, with boots interleaved.

Correctness

  • A short-prompt probe (12 prompts × greedy and default sampling, 512 tokens) is token-for-token identical between G3 and G0.
  • max_total_num_tokens is the same in both arms.
  • 752/752 decode rounds run on CUDA graphs.
  • The bound runtime serves every round; none is handed back.

Performance (tok/s/user = 1000 / TPOT)

arm boots P90 per boot P50 mean P90 mean TPOT median round time median
G0: main + mega#70 3 451.67 / 449.19 / 447.49 473.96 449.45 2.1099 ms 7.8283 ms
G3: this PR + companion 2 447.41 / 447.36 472.32 447.39 2.1172 ms 7.8303 ms
  • G3 − G0 is within the boot-to-boot spread: G0's own P90 spread is 4.18.
  • Round time per decode round agrees within ±5 µs.

Checklist

🤖 Generated with Claude Code

@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 pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

…name

The DFLASH worker carried the NVIDIA megakernel's draft head, embed, aux
append, greedy and sampled accepts, post-verify tail, rank-0 broadcast and
KDA commit inline, behind accessors for seven Supports* protocols. That code
now lives with the megakernel; SGLang keeps a small binding
(speculative/dflash_binding.py) and three whole-phase entries:

- the end of the worker's __init__, before the KV pools are sized: the
  target build's runtime class (SupportsDFlashRuntime, "module:attribute")
  is resolved, agreed across TP ranks and built as cls(binding, built);
- init_cuda_graphs, before the draft graph captures: draft_sampler(lm_head)
  may supply the selector sampler folded into that graph;
- the decode branch of forward_batch_generation: decode_round(batch, ...)
  runs the whole round, or returns None for the stock one.

The runtime pulls worker state on demand through DFlashBinding.pull, only
the EXPORTS names it declares (read past the worker's __getattr__, which
would reach the target worker). It declares the values it expects for GUARD
keys (TP size, block size, draft architecture, sampler and verify modes,
cache features); bind_dflash_runtime agrees them across TP ranks and raises
naming each export, key, API version or value it disagrees on. With the
flag off, no build, or a build naming "" the worker keeps its own round;
a build without SupportsDFlashRuntime logs one warning on TP rank 0.

The stock round is unchanged apart from losing its megakernel branches, and
keeps the aux append off the side stream while a runtime is bound.

Removed: _MegakernelSelectorDraftSampler, _MegakernelTail and the worker's
26 megakernel methods, dflash_sim_acc.py, the seven megakernel_* draft and
KDA accessors with _offered/_offered_draft, SupportsDraftAux,
SupportsDraftSelectorHead, SupportsDraftEmbed, SupportsDraftAccept,
SupportsRank0Broadcast, SupportsSampledAccept, SupportsKdaCommit and
SGLANG_OPT_DFLASH_MEGAKERNEL_TAIL_GRAPH.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Oasis-Git Oasis-Git changed the title [Refactor] spec/dflash: Move the DFLASH decode round into DFlashRuntime and the megakernel out of the worker [Refactor] spec/dflash: Take the megakernel out of the DFLASH worker behind a guarded runtime binding Nov 20, 2026
Oasis-Git and others added 6 commits November 19, 2026 19:41
…erve

speculative/dflash_binding.py moves to
kernel_plugins/nvidia/kimi_k31_dflash_binding.py, beside the Kimi K3.1
decode and verify bindings (as eagle_binding and dspark_binding sit): the
DFLASH worker binds the Kimi K3.1 megakernel's DFlash runtime through it.
The worker, spec.py and the tests follow the new path.

A guard value the runtime does not serve (a TP size, decode graph batch
sizes or another setup it is not built for) no longer fails startup:
bind_dflash_runtime agrees the values across TP ranks, logs one warning on
TP rank 0 naming each mismatched key with SGLang's value and the expected
one, and returns None, so the worker runs its stock DFLASH round beside the
target megakernel, as before the runtime existed. What the two sides
disagree on structurally still raises: the API version, an export or guard
key SGLang does not have, ranks reading different guard values, and an
export failing its type check on pull.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The binding already raises on an export or guard key SGLang does not have,
and SGLang declines a runtime whose guard values do not match. A version
number on top of that only helps when someone remembers to bump it; when
the binding and a runtime stop matching, fix them instead.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A runtime states one expected value per guard key. The only key that took a
set of values, the plain-gather deferral env, becomes the boolean
defer_plain_gather_refused (the env set to 0).

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

The runtime declares two things, and each has a matching table and checker:

- exports (EXPORTS, DFlashBinding): the worker state it reads, by name, on
  demand through pull;
- guards (GUARDS, DFlashGuard): the setup value it expects per key; unmet()
  reads the running values on every TP rank.

A name or key missing from a table raises when the runtime binds; a guard
value that differs declines the runtime. bind_dflash_runtime now reads as
resolve, bind, guard, build. The runtime attribute invariants is renamed
guards, and each guard entry is just its reader.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
One word for the reads: the runtime declares bindings, SGLang lists them in
BINDINGS (name -> WorkerMember), and DFlashBinding.pull reads them. The
other half stays guards / GUARDS / DFlashGuard.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Oasis-Git Oasis-Git added the run-ci CI: run the baseline test suite on this PR label Nov 20, 2026
@Oasis-Git
Oasis-Git merged commit a034bb5 into sgl-project:main Nov 20, 2026
177 of 179 checks passed
jasperjiaguo added a commit to jasperjiaguo/sglang that referenced this pull request Nov 20, 2026
Resolve the conflict with sgl-project#51676, which moved the DFLASH megakernel out of the
worker: drop this branch's edits to the removed megakernel selector sampler, head
attach and accept attach. bind_dflash_runtime now declines the megakernel's DFlash
runtime while SGLANG_BENCH_DRAFTER_TEMPERATURE is on, since the runtime replaces the
round in which the draft temperature applies (one rank-0 warning naming the =0
opt-out; covered in test_megakernel_runtime.py).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
shyeh25 added a commit to shyeh25/sglang that referenced this pull request Nov 21, 2026
Resolve conflicts with the per-request penalty pool (sgl-project#52035), DFLASH request
penalties (sgl-project#47020) and the megakernel runtime binding (sgl-project#51676): the shared
logit-adjustment check reads step_penalties, so penalized batches keep the
dense verify and get upstream's penalties; the sparse accept syncs its
decision across TP ranks like the dense selector accept.

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

jit-kernel run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant