Repository navigation
[Refactor] spec/dflash: Take the megakernel out of the DFLASH worker behind a guarded runtime binding - #51676
Merged
Merged
Conversation
Oasis-Git
requested review from
BBuf,
DarkSharpness,
HaiShaw,
HydraQYH,
Qiaolin-Yu,
Ying1123,
celve,
hnyls2002,
merrymercy and
yuan-luo
as code owners
November 20, 2026 01:56
…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
force-pushed
the
dflash-runtime
branch
from
November 20, 2026 02:45
a2d4377 to
dbc3d04
Compare
…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]>
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]>
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]>
5 of 7 tasks
This was referenced Nov 20, 2026
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
The Kimi-K3.1 day-0 merge put about 1k lines of NVIDIA-megakernel-only code into
dflash_worker_v2.py:megakernel_*accessors inkernel_plugins/nvidia/binding.pyfor theSupports*protocols behind them.This code only runs with
--enable-nvidia-megakerneland 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 indflash_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)ininit_cuda_graphs, before the draft graph captures. It may supply the selector draft sampler folded into that graph;Nonekeeps SGLang's;decode_round(batch, on_publish=, grammar_barrier=)for the decode branch offorward_batch_generation. It runs the whole round, or returnsNonebefore 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:
bindings: the worker state it readsBINDINGS: 17 names, each a worker member with a checked typeDFlashBindingpull(name)reads the value on demand, past the worker's__getattr__, which would otherwise reach the target workerguards: the setup value it expects per keyGUARDS: 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, LoRADFlashGuardunmet(worker)reads the running values on every TP rank, requires the ranks to agree, and names each key that differsbind_dflash_runtimereads as: resolve the class, bind, guard, build. The outcomes:So a runtime only ever runs on the setup it declared.
3. Resolution and fallback. These keep the existing rules:
""keeps SGLang's own round;SupportsDFlashRuntimelogs one warning on TP rank 0, so an older plugin no longer falls back silently;Removed from SGLang:
dflash_worker_v2.py:_MegakernelSelectorDraftSampler,_MegakernelTailand the worker's megakernel head, embed, aux, accept, tail, broadcast and KDA-commit code;binding.py: the sevenmegakernel_*draft/KDA accessors;spec.py: the matchingSupports*protocols;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:
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
K31DFlashRuntimeand binds it through this module. It must land together with this PR.Accuracy Tests
test_megakernel_runtime.pycovers 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.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
max_total_num_tokensis the same in both arms.Performance (tok/s/user = 1000 / TPOT)
Checklist
🤖 Generated with Claude Code