Skip to content

[Feat] [NVIDIA] megakernels: Qwen3.5 binding and EAGLE entry wiring for NVIDIA megakernel plugins - #45266

Merged
Oasis-Git merged 13 commits into
sgl-project:mainfrom
YAMY1234:nv-mega/qwen35-megakernel-binding-main
Oct 27, 2026
Merged

Oasis-Git merged 13 commits into
sgl-project:mainfrom
YAMY1234:nv-mega/qwen35-megakernel-binding-main

Conversation

@YAMY1234

@YAMY1234 YAMY1234 commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

The NVIDIA megakernel for Qwen3.5-397B-A17B (NVFP4) decode on GB300 lives out of tree, in sgl-project/sgl-nvidia-mega#20. #44945 added --enable-nvidia-megakernel and the plugin layer for DeepSeek-V4.1. This PR adds what the Qwen3.5 megakernel needs on the SGLang side, in the same shape:

  • a binding for Qwen3.5: the runtime state SGLang hands the plugin, as data;
  • EAGLE draft entries: the draft and draft-extend graph runners capture a plugin step at the batch sizes it serves;
  • KV-length attention graph variants: a runner captures one graph per attention variant the megakernel names, and each replay picks one by KV length.

SGLang keeps every runner, worker, schedule batch and CUDA graph, and everything after a target step (sampling, acceptance, commits). The plugin gets data: the forward's attention and GDN metadata, the KV and mamba pools, and per-entry structs with SGLang-owned outputs. Before capture, every rank must offer the same entries, batch sizes and variants, or SGLang refuses to start.

This PR is based on main and does not depend on #45474 (the runtime changes for the same decode path). Either can merge first; merged in either order, the two give the same tree. Without --enable-nvidia-megakernel nothing changes.

Modifications

One commit per change:

  1. Pick each megakernel's binding by architecture, and agree on the entries it offers (megakernels/runtime.py, spec.py). Qwen3.5's HybridLinearAttnBackend keeps its metadata on its two child backends, so it needs its own binding. spec.ENTRIES names the optional entries (draft_sampler, draft_loop, draft_extend, begin_draft_round), and every rank must offer the same ones.
  2. The binding of Qwen3.5's megakernels (megakernels/eagle_binding.py, new). EagleBinding returns the view a covered step reads: attention metadata, GDN slots, the KV and mamba pools. Each pool is sealed at its first read, and assert_live fails by name if one is reallocated. The entries' structs carry tensors and numbers only. What a plugin may call or read besides data is listed in literal tuples that the plugin's contract test pins.
  3. EAGLE draft graphs run the draft megakernel's draft_loop and draft_extend (speculative/eagle_megakernel.py, new, plus the worker and the two draft graph runners).
    • Before capture, each runner calls the entry's factory once with its own outputs. At the batch sizes the factory serves, the graph captures that step in place of SGLang's draft loop, or in place of the draft model step and its top-1.
    • begin_draft_round runs on the host before a served draft graph replays.
    • The draft worker builds the entries at the end of its init_attention_backends, before any capture. It refuses setups it cannot serve: a non-chain draft, rejection sampling, DP attention, a sliding-window target KV pool, or backends other than GDN with trtllm_mha.
  4. Capture a megakernel's KV-length attention graph variants in the verify and EAGLE draft graphs (spec.py, runtime.py, forward_batch_info.py, decode_cuda_graph_runner.py, the draft runners).
    • A megakernel may offer graph_variants for target_verify, draft_loop or draft_extend: capture labels plus a select that picks one from host state. At the batch sizes that run the megakernel, the runner captures one graph per label and replays the one select picks.
    • ForwardBatch.attention_kv_len_lower_bound gives select a one-request batch's committed KV length without touching the device.
    • With the plugin, its split-KV kernels run from 8K (verify), 16K (draft steps) or 32K (draft extend) tokens, and SGLang's attention below.

Earlier revisions of this PR were stacked on #45474 and also offered verify_sample and gdn_extend. Both are removed: the plugin's verify_sample had no kernel and only turned on #45474's post-verify graphs (SGLANG_ENABLE_EAGLE_POST_VERIFY_CUDA_GRAPH=1 does the same), and no plugin offered gdn_extend. They can come back in a follow-up once a plugin has a kernel for them.

Known limits: draft entries serve graph replays only; a variant's select gets a KV length only for one-request batches; SGLang's images pin a plugin commit without Qwen3.5 (SGL_NVIDIA_MEGA_COMMIT), so sgl-project/sgl-nvidia-mega#20 must be installed until that pin moves.

Accuracy Tests

GB300, TP8, real acceptance, GSM8K on a fixed 250-question subset (sampled: temperature 0.6, top-p 0.95, top-k 20).

configuration greedy sampled acceptance length vs reference (greedy / sampled)
reference: the development build 0.980 0.984 1 / 1
main + this PR + plugin 0.980 0.988 1.000 / 0.991
main + #45474 + this PR + plugin 0.980 0.976 1.000 / 0.998

Greedy outputs match the reference on 249 / 250 questions in both rows. Every slot, entry and variant capture is active on 8 / 8 ranks.

  • CPU: the megakernel, speculative and graph-runner unit tests pass wherever main's pass. The plugin's Qwen3.5 tests, including its contract test, pass against this head.

Speed Tests and Profiling

GB300, TP8 across two 4-GPU trays. Static decode of one request at 128K context with forced acceptance 4.80, about 1,780 rounds per server start. TPS/User = 4.80 tokens / round time. Rows 1–3 are the mean of two pairs of trays run in opposite orders. The plugin was measured at 3509187d74, an earlier revision of sgl-project/sgl-nvidia-mega#20; the PR head differs only by an opt-in for more than one request, off by default.

configuration TPS/User P50 TPS/User P90
main 706.4 700.3
main + this PR + plugin 765.7 (+8.4%) 729.6 (+4.2%)
main + #45474 + this PR + plugin 1291.7 (+82.8%) 1276.0 (+82.2%)
the development build (reference) ¹ 1275.2 1249.5

¹ On a third pair of trays, row 3 reaches 101.0% of the reference at P50 at 128K, and 100.8% / 100.6% and 101.9% / 101.6% (P50 / P90) at 8K and 32K.

These results were measured on main 28448a86d4; the branch has since been rebased onto main b57b5e2286 without changes, and the GPU checks are being repeated on the new base.

Row 2 gains little because the round becomes host-bound: the verify takes 1.87 ms less, but the gaps between graph segments grow from 0.06 to 1.36 ms per round. Row 3 adds #45474, which removes that host work, and the gaps return to 0.06 ms.

Checklist

Review and Merge Process

  1. Ping Merge Oncalls to start the process. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • Common commands include /tag-and-rerun-ci, /tag-run-ci-label, /rerun-failed-ci
  4. After green CI and required approvals, ask Merge Oncalls or people with Write permission to merge the PR.

CI States

Latest PR Test (Base): 🚫 Run #40268603176
Latest PR Test (Extra): ❌ Run #40268603163
Latest PR Test (AMD ROCm 10): ✅ Run #40268603217

@YAMY1234 YAMY1234 changed the title [Feat] [NVIDIA] megakernels: Qwen3.5 binding and EAGLE entries for NVIDIA megakernel plugins [NOT READY][Feat] [NVIDIA] megakernels: Qwen3.5 binding and EAGLE entries for NVIDIA megakernel plugins Oct 20, 2026
@YAMY1234
YAMY1234 force-pushed the nv-mega/qwen35-megakernel-binding-main branch from c1c79ac to dd3b395 Compare October 20, 2026 20:53
@YAMY1234
YAMY1234 force-pushed the nv-mega/qwen35-megakernel-binding-main branch from 4401622 to aa9f2a8 Compare October 21, 2026 05:09
@YAMY1234 YAMY1234 changed the title [NOT READY][Feat] [NVIDIA] megakernels: Qwen3.5 binding and EAGLE entries for NVIDIA megakernel plugins [Feat] [NVIDIA] megakernels: Qwen3.5 binding and EAGLE entry wiring for NVIDIA megakernel plugins Oct 21, 2026
@YAMY1234
YAMY1234 force-pushed the nv-mega/qwen35-megakernel-binding-main branch from aa9f2a8 to 29eb7e9 Compare October 21, 2026 13:14
…ecture, and agree on the entries it offers

- install_megakernel builds the binding SGLang registers for the model's
  architecture (runtime._BINDINGS), and MegakernelBinding for every other one. It
  hands the binding over as BuildContext.binding, as before.
- spec.ENTRIES names the optional entries of a built object: draft_sampler and
  the EAGLE entries (draft_loop, draft_extend and begin_draft_round). Every rank
  must offer the same ones, or every rank refuses. megakernel_entry returns an
  entry, or None for SGLang's own path.
- The startup line of a spec that covers no step names the entries it offers.

The API version is unchanged: a built object written without the EAGLE entries
offers none of them, and the DSpark draft sampler path is untouched.
megakernels/eagle_binding.py is what binding.py is for DeepSeek-V4.1: the one place
the megakernel of a Qwen3.5 target, whose GatedDeltaNet and full-attention layers
run under HybridLinearAttnBackend, and of its MTP draft read SGLang's runtime state.
runtime._BINDINGS hands EagleBinding to the Qwen3.5 architectures.

- A covered step gets EagleForwardView as Step.view: the full-attention metadata
  (page table, KV lengths, cumulative lengths), the GDN metadata (mamba slots,
  query offsets, intermediate rows, track slots), the KV pool per full-attention
  layer with its FP8 scales, and the mamba pool with its ReplaySSM ring. A model
  without GDN layers, such as the draft, gets no GDN metadata and no mamba pool.
- The pools are read once and sealed; assert_live, after the decode graphs are
  captured, raises once one was reallocated.
- The module defines the structs of the EAGLE entries (draft loop, draft extend,
  draft round) and documents what each entry may write. Its TOLERATED lists name
  what a megakernel may call or read beyond that data: the TP process groups,
  SGLang's kernel ops, configuration, and the covered forward's own batch fields.
- The installed forward types the binding it holds as BuildContext.binding does:
  EagleBinding has MegakernelBinding's forward_view and assert_live without
  subclassing it.

test_eagle_binding.py checks that the view holds the backends' own tensors, the
sealing, that every struct carries data only, and that every listed name exists.
…nel's draft_loop and draft_extend

When the draft model's megakernel offers draft_loop or draft_extend, the EAGLE
draft and draft-extend graph runners call the factory once, before capture, with
their own outputs: the draft tokens, and the extend's top-1, its probability and
its hidden state. For a batch size whose step the factory returns, the graph
captures that step in place of SGLang's draft loop, or in place of the draft
model step and its top-1. Every rank must serve the same batch sizes, and every
other size runs SGLang's code. SGLang keeps the graphs, their replays and the
outputs.

- begin_draft_round, when offered, runs on the host before a served draft graph
  replays. It says whether a served draft extend of the same requests produced
  the round's input.
- Draft entries serve graph replays only; an eager draft step runs SGLang's loop.
- The EAGLE draft worker looks for entries only with --enable-nvidia-megakernel,
  at the end of its init_attention_backends, once the pools and both attention
  backends exist and before any graph is captured. It refuses offered entries it
  cannot serve: a draft other than a multi-step topk-1 chain, rejection sampling,
  DP attention, a sliding-window target KV pool, or backends other than GDN with
  trtllm_mha.
- The graph runners read the worker's megakernel, None unless entries are
  offered. The attention test kit's draft worker harnesses and the inkling
  checkpoint test's draft worker carry it as None, as EagleDraftWorker does.
  The inkling test builds its draft-extend runners with cls.__new__, so it sets
  their megakernel members as __init__ does for a worker without a megakernel.

speculative/eagle_megakernel.py holds SGLang's side of the entries: the served
batch sizes, the outputs, and the provenance of each draft round's input. It is
never handed to a plugin.
…on graph variants in the verify and EAGLE draft graphs

A built megakernel may offer graph_variants: for the target's verify graphs
("target_verify"), the EAGLE draft graphs ("draft_loop") or the EAGLE draft-extend
graphs ("draft_extend"), an AttentionGraphVariants whose capture labels name graphs
that differ in the attention a step runs, and whose select picks one from host state.
At each batch size whose graph runs the megakernel, that runner captures one graph
per label, in order, with the label readable through get_capture_attention_variant()
during the capture, and every replay asks for the graph select picks. Every other
batch size, and every runner without an offer, captures one graph as before.

- ForwardBatch.attention_kv_len_lower_bound: init_new sets it to a one-request
  batch's committed KV length (Req.kv.kv_committed_len), which can lag one verify and
  so never exceeds the KV the step reads; None for several requests. select reads it
  without touching the device.
- The verify graphs take the target's variants at the batch sizes whose rows its
  megakernel built, unless SGLang captures variants of its own (DSA, the candidate
  indexer). The draft and draft-extend graphs take the draft's at the batch sizes
  their entry serves.
- DecodeCudaGraphRunner.attention_variant_sizes limits the capture and the replay of
  the variants to those batch sizes; None keeps every size, as SGLang's own variants
  have it. The EAGLE draft and draft-extend runners key their graphs by the label.
- Every rank must offer the same runners and labels, or every rank refuses; a key
  that names no runner is refused.
- The EAGLE binding tolerates the reads a select makes and the capture-mode query.
- SGLANG_LOG_DECODE_GRAPH_KEY also prints the replayed graph's attention variant.

The API version is unchanged: a built object without graph_variants captures as
before.
@YAMY1234
YAMY1234 force-pushed the nv-mega/qwen35-megakernel-binding-main branch from 29eb7e9 to 0de5c03 Compare October 21, 2026 18:46
…b a model

The draft-extend graph runner now asks the draft model for the megakernel's
attention graph variants, so the stub model_runner needs a model attribute.
@YAMY1234
YAMY1234 marked this pull request as ready for review October 21, 2026 22:42
# Conflicts:
#	test/registered/unit/layers/test_mamba_prefill_track_metadata.py
@YAMY1234 YAMY1234 added bypass-fail-fast CI: a failing job no longer aborts its siblings (lint still gates) run-ci CI: run the baseline test suite on this PR labels Oct 22, 2026

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

[by Claude Code] This review is done by agents. Please feel free to disagree if they do not make sense.

[by Claude Code] Severity: style | Confidence: High

Issue

The GSM8K and TPS/User results have no commands, so nobody can rerun them. The description gives GB300, TP8 and the plugin commit, but not the model checkpoint path or the server flags.

Fix

Add these to the PR description:

  • the server launch command for each row (model path, the two-tray TP8 flags, --enable-nvidia-megakernel, the speculative algorithm and step counts, attention backend, KV cache dtype);
  • the GSM8K eval command, and how the fixed 250-question subset is chosen;
  • the benchmark command for the static 128K decode, and how acceptance is forced to 4.80.

…th bound

ForwardBatch.init_new sets attention_kv_len_lower_bound on every one-request
batch, and TBO's filter_batch rejects any set field it does not carry, so a
two-batch-overlap split of such a batch raised before the forward. The bound
belongs to the unsplit batch; the children get None.
…rectly

_BINDINGS held "module:attribute" strings that _binding_class resolved with
importlib and getattr. EagleBinding is in-tree and eagle_binding.py does not
import runtime.py, so runtime.py imports it and _BINDINGS maps each
architecture to the class.
…a speculative token map

With --speculative-token-map the draft lm_head keeps only the hot tokens, so
the draft extend's top-1 is a hot-vocab index that SGLang maps through
hot_token_id. The entries get no map and would hand verify hot-vocab ids, so
init_eagle_megakernel refuses the setup at startup.
…rnel-binding-main

decode_cuda_graph_runner.py: keep main's DSpark class defaults beside the
attention-variant defaults, and main's string LoRA variants in the capture
loop, which still captures the attention variants only where
_attention_varies_at holds.
# Conflicts:
#	python/sglang/kernels/plugin_kernels/nvidia/spec.py
#	python/sglang/srt/megakernels/runtime.py
#	test/registered/unit/kernel_plugins/nvidia/test_eagle_binding.py
@YAMY1234

Copy link
Copy Markdown
Collaborator Author

@Oasis-Git Po-Han's comments are addressed and the conflicts are resolved; could you take a look when you have a moment? Thanks!

@Oasis-Git

Copy link
Copy Markdown
Collaborator

@Oasis-Git
Oasis-Git merged commit 8f1b769 into sgl-project:main Oct 27, 2026
324 of 343 checks passed
weireweire added a commit to SemiAnalysisAI/InferenceX that referenced this pull request Oct 28, 2026
… 2 plugin

nightly-dev-cu13-20261028-586355a8 bundles sgl-nvidia-mega 6fd7ac30 (megakernel API 1), while its SGLang requires API 2 since sgl-project/sglang#45266, so --enable-nvidia-megakernel fails at startup. TP4 c1 keeps its previous engine settings and pins flashmla like the other TP-sharded points.
nvrohanv added a commit to nvrohanv/sglang that referenced this pull request Oct 29, 2026
One conflict, in the spec import block of
python/sglang/srt/kernel_plugins/nvidia/binding.py: this branch imports
FLAG_SETS_ENV and main (sgl-project#45266) imports
GRAPH_VARIANT_RUNNERS; both are kept.
nvrohanv added a commit to nvrohanv/sglang that referenced this pull request Oct 29, 2026
sgl-project#45266 made the megakernel contract version 2: the
binding reads draft_loop, draft_extend, begin_draft_round and
graph_variants from every built object. The draft capture test's built
object offers none of them, as main's FakeBuilt does.
nvpohanh added a commit to SemiAnalysisAI/InferenceX that referenced this pull request Nov 3, 2026
…ly with the tuned low-latency settings and TP4 c1 megakernel / [NVIDIA][AgentX] GB300 DSV4.1-Flash SGLang 升级到 20261030 nightly,采用调优后的低延迟配置并在 TP4 c1 启用 megakernel (#4603)

* Update GB300 DSV4.1-Flash SGLang AgentX image to nightly-dev-cu13-20261025-65633129

Move dsv41flash-fp4-gb300-sglang-agentic-dspark from nightly-dev-cu13-20261021-c1c287a4 to the digest-pinned nightly-dev-cu13-20261025-65633129, the image the Dynamo + SGLang agg key already uses. Recipes and search space are unchanged.

* Set perf-changelog pr-link to #4603

* Move GB300 DSV4.1-Flash SGLang AgentX to 20261028 nightly with the tuned low-latency settings

Use nightly-dev-cu13-20261028-586355a8, which includes sgl-project/sglang#46404 and #46891. TP4 c1 runs as the batch-1 NVIDIA megakernel point of the Dynamo + SGLang aggregated row, without SIM_DRAW_AHEAD so the simulated step does the same work as a real one. TP4 c4/c16 and TP2/EP2 c16/c32 pin the flashmla attention backend instead of auto (MegaAttention pads per-rank heads to 64).

* Keep SIM_DRAW_AHEAD on the TP4 c1 megakernel point

The gate only moves the simulated-acceptance draw, a host-side sample plus a fill that real acceptance never runs, ahead of the draft launch. It does not skip work that a real-acceptance step performs.

* Keep TP4 c1 off the megakernel until the image ships a megakernel API 2 plugin

nightly-dev-cu13-20261028-586355a8 bundles sgl-nvidia-mega 6fd7ac30 (megakernel API 1), while its SGLang requires API 2 since sgl-project/sglang#45266, so --enable-nvidia-megakernel fails at startup. TP4 c1 keeps its previous engine settings and pins flashmla like the other TP-sharded points.

* Move the GB300 DSV4.1-Flash SGLang curve to the 20261029 nightly and run TP4 c1 on the megakernel

nightly-dev-cu13-20261029-c10e0e17 bundles sgl-nvidia-mega 54b428921749
(megakernel API 2), so TP4 c1 returns to its megakernel config. The 16
runtime-optimization variables it set one by one are replaced by
SGLANG_ENABLE_NV_MEGA_RUNTIME_OPTIMIZATION (sgl-project/sglang#47105).

* Move the GB300 DSV4.1-Flash SGLang key to the 20261030 nightly for the simulated verify accept cost fix

Root cause: nightly-dev-cu13-20261029-c10e0e17 predates sgl-project/sglang#46840,
so simulated acceptance skipped the real verify accept cost and the TP4 c1 point
was not measured on the methodology-conformant path.

Fix: pin lmsysorg/sglang:nightly-dev-cu13-20261030-302b78cc by digest. It is
63 commits past the #46840 merge and bundles the same sgl-nvidia-mega
54b428921749 (megakernel API 2). Update this PR's changelog entry to name the image
and #46840.

Validation: Dynamo + SGLang TP4 c1 megakernel, c4 and c16 started and completed
on 302b78cc with target and draft megakernels active on all four ranks.

---------

Co-authored-by: weireweire <[email protected]>
Co-authored-by: Po-Han Huang <[email protected]>
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

blackwell SM100/SM120 bypass-fail-fast CI: a failing job no longer aborts its siblings (lint still gates) jit-kernel run-ci CI: run the baseline test suite on this PR speculative-decoding unified-radix-cache

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants