Repository navigation
[Feat] [NVIDIA] megakernels: DSA / MLA binding view, EAGLE draft capture checks, early draft embedding sharing - #46210
Conversation
…ure checks, early draft embedding sharing A megakernel that replaces the decode or verify step of a DSA (sparse MLA) model reads the DSA backend's layout and decode buffers and writes the MLA and index-K rows of its KV pool. Offer them through the binding: KvPoolView gains the MLA key and index-K buffers, the index page size and the MLA key write; ForwardView.dsa is a DsaView of the running DSA backend (MLA dimensions, decode implementation, top-k and DCP settings, the decode workspace and the multi-CTA KV counter as sealed data, and the indexer metadata). TOLERATED_DSA_CALLABLES / TOLERATED_DSA_WRITES list what a megakernel still calls or writes; the existing TOLERATED_* lists are unchanged. The EAGLE draft-decode and draft-extend graph runners now call the capture tail hooks at the end of their captured function, as the decode graph runner does, so a megakernel spec installed on a draft model is checked after capture. The draft-decode runner runs them under its first per-step attention backend and checks that the per-step backends share one KV pool. spec.EAGLE_DRAFT_CAPTURE_CHECK marks an SGLang that does this; Step documents the draft modes. Under SGLANG_ENABLE_EMBED_REPLICATION the NextN draft loads its own full embedding table on every rank. EagleDraftWorker replaced it with the target's in alloc_memory_pool, which the scheduler calls after it has sized the target KV pool, so the draft's copy (and its lm_head shard) was still allocated when the pool was sized. Under replication the draft worker now binds the target's embedding and lm_head at the end of its construction, so the draft's own copies are freed before the pool is sized and the pool can use that memory. Without replication the calls stay where they were. With --enable-nvidia-megakernel, argument resolution now sets a variable of spec.FLAG_SETS_ENV (SGLANG_ENABLE_EMBED_REPLICATION) that the served spec requires and the user left unset, to the spec's value, and logs it. SGLang reads that variable only when a model is built, in the model processes started after resolution, so every rank sees the value. A value the user set is kept and checked as before, and a refused setup leaves the environment as it was.
c39924b to
465a9dd
Compare
|
Validated the combined GLM-5.3 NVFP4 static-table stack on 8×B300 TP8 with hooks Full table and protocol: sgl-project/sgl-nvidia-mega#24 (comment). This measures the combined stack, not the hooks' isolated contribution. The DSA test consolidation is now in |
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.
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.
One conflict, in handle_nvidia_megakernel of python/sglang/srt/kernel_plugins/nvidia/binding.py: main (sgl-project#46625) returns early when the MSA plugin serves the architectures, and this branch starts its FLAG_SETS_ENV bookkeeping (env_set = []) at the same place. Both are kept: main's return first, then env_set, so the flag sets variables only for an sgl-nvidia-mega spec.
- spec, draft_spec: API_VERSION 2; the built objects offer no EAGLE entries or graph variants (draft_loop, draft_extend, begin_draft_round, graph_variants are None). Restore the target's _Built (widths, summary, plan, forward), which the package split had dropped, so build returns a BuiltMegakernel again. - spec: accept Feature.MULTI_NODE_TP, so TP8 can span the two 4-GPU trays of an NVL72 rack, as the draft spec already does. - spec: drop the DsaView binding-member and EAGLE_DRAFT_CAPTURE_CHECK gates, which only an unmerged SGLang (sgl-project/sglang#46210) passes; the covered step reads the DSA backend through get_attn_backend() and never through those views. - attention: build the AttentionGlue directly (it has no build classmethod). - draft_spec: import the megakernel interface from its new location first. - glm_dsa, spec_step: declare every SGLang import in SGLANG_READS (the stricter contract test); import modules as modules (import a.b.c as c); name reads by the file that defines them. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve conflicts with main's draft runtime and binding seal refactor: - binding.py: seal the DSA buffers through main's _seal(name, value, current) in forward_view (non-hybrid only); take main's assert_live; drop the PR's duplicate KvPoolView.get_key_buffer that shadowed main's sealing one. - eagle_draft_cuda_graph_runner.py: keep main's draft_runtime.run_draft_loop; call the capture tail hooks at the end of run_stock, as the extend runner does. - spec.py: keep main's Step mode list and add the draft modes and notes. - test_megakernel_draft_capture.py: follow the hook call into run_stock and give the patched forward_view a view main's seal_capture_view accepts. Co-Authored-By: Claude Opus 5.5 <[email protected]>
YAMY1234
left a comment
There was a problem hiding this comment.
Merged main and resolved the conflicts; LGTM.
|
/tag-and-rerun-ci |
…raft-capture main's sgl-project#48414 replaced MegakernelBinding with the Kimi-K3.1 design (identity-only seals read back through a fixed table, HybridForwardView). The DSA view now seals on that design: _seal takes an optional re-read, which assert_live uses for the per- backend DSA buffers, and forward_view seals them on the ForwardView path. alloc_memory_pool keeps main's draft branch tail-copy check, then binds the target's embedding / lm_head unless the early share already did. # Conflicts: # python/sglang/srt/kernel_plugins/nvidia/binding.py # python/sglang/srt/speculative/eagle_worker_v2.py Co-Authored-By: Claude Opus 5.5 <[email protected]>
main's binding imports get_attn_backend at module level, so the DSA view tests patch it there, as the rest of test_megakernel_binding.py does. alloc_memory_pool now checks the draft pool's branch tail copy; the embed share test gives its bare worker a draft runner, topk 1 (where the check is a no-op) and a page size. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This comment has been minimized.
This comment has been minimized.
…raft-capture Picks up sgl-project#51517, which fixes main's export-cookbook-commands lint hook. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
/tag-and-rerun-ci |
|
[by Claude Code] I've started babysitting the NVIDIA CI of this PR. I'll check it every 30 minutes until all NV pipelines pass. If a relevant job fails, I may rerun it, merge the latest Anyone can stop me at any time: just post a comment asking me to stop (for example, |
|
/rerun-failed-ci |
|
[by Claude Code] All NV pipelines have passed. |
…v2 step CUDA graph (spec_step) (#19) * [Feat] [GB300/Rubin] glm_dsa, spec_step: GLM-5 DSA fused verify and NextN draft; EAGLE v2 step CUDA graph - glm_dsa: target verify (4-16 rows) and NextN draft/draft-extend megakernel specs for GlmMoeDsaForCausalLM on SM103 and SM107 (TP8, NVFP4 MoE). - spec_step: one CUDA graph per EAGLE v2 topk-1 decode step plus bitwise- identical spec fusions, installed via HookRegistry behind env switches. * [Fix] spec_step: keep the step graph on torch 2.14 and make lifted.py importable - hooks.py: torch 2.14's `_CUDAGraph` binding reads `keep_graph` in `__init__`, so overriding only `__new__` let a bare `torch.cuda.CUDAGraph()` drop its raw graph at capture end, and decode graph capture failed in cuGraphInstantiate with CUDA_ERROR_INVALID_VALUE. - lifted.py: defer annotations. `Tuple`, `Optional` and `torch` are bound only in the SGLang module each function is rebound into, so the first verify raised `NameError: Tuple`. - lifted.py: `eagle_sample` drops its inline sampler imports, which referenced `_is_npu` (removed from eagle_utils by sgl-project/sglang#45504), and uses eagle_utils' module names. - lifted.py: `_draft_extend_for_decode` passes `sampling_seed=` and `positions=` again, as current SGLang does. Tested on 8x B300, GLM-5.3-NVFP4, TP8, EAGLE 3/1/4, SGLang f4a2df3dac, all spec_step switches on. * [Fix] glm_dsa: accept --speculative-algorithm NEXTN The construction check compared the raw server argument with EAGLE, so a NEXTN launch (the InferenceX GLM-5.3 B300 AgentX recipe) was refused with "speculative algorithm 'NEXTN' is not supported (EAGLE / NEXTN only)". NEXTN resolves to the same EAGLE worker; the spec's requirement and the spec_step runner already accept it. * [Perf] glm_dsa: split the verify path into packages; Rubin round-7 hop scheduling switches Split the GLM-5 DSA verify path into the attention/, boundary/, moe/ and runtime/ packages plus plan.py (runtime.py kept as runtime_legacy.py), and fix two leftovers of the split: a stray @DataClass in moe/__init__.py and a duplicated module header in boundary/__init__.py. The spec's model_disable_reasons now calls eligibility.model_construction_reasons with the HF config and the server arguments. Port two hop scheduling ideas from the TRT-LLM Rubin round-7 branch, both off by default and output-neutral: - SGLANG_NVIDIA_MEGAKERNEL_GLM_HOP_MAX_SHARED_CARVEOUT=1: every hop kernel prefers the maximum shared-memory carveout, so an SM a hop CTA lingers on can host the next layer's large-shared-memory kernels under PDL. - SGLANG_NVIDIA_MEGAKERNEL_GLM_HOP_CTAS_PER_SM=1|2: pad the o_proj consumer hop's launch shared memory (sized from the device's per-SM limits) so at most N CTAs share an SM while polling. Round 7's single-loop 16-slot polling does not apply: at 8 ranks the consumer already polls all sources in parallel. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [Feat] glm_dsa: serve MTP with 5 draft steps (6 verify rows a request) Port #24's 5 / 1 / 6 support (3889557, 492aa0a, 77015e7) to the GLM-5 DSA specs, which served speculative_num_draft_tokens 4 / speculative_num_steps 3 only. - eligibility: SERVED_TOKENS_PER_REQUEST = (4, 6) and MAX_ROWS = 16; supported_rows(T) gives every whole number of requests up to 16 rows (T 4: 4 / 8 / 12 / 16, T 6: 6 / 12). The executor builds only the widths of the server's speculative_num_draft_tokens; the spec and Tier 1 refuse any other value or a pair with draft tokens != steps + 1, naming 4 / 3 and 6 / 5. - MoE: moe_split groups rows by 4 with the remainder last (6 -> (4, 2) on the two-group engine, 12 -> (4, 4, 4) on the four-group engine). - Draft: the eh_proj shard rows are the draft decode's 1-4 plus the covered widths (T 6: 1, 2, 3, 4, 6, 12). - One request at 6 rows keeps the fast forms: split MoE fold hop (SPLIT_MAX_ROWS 6), plain hops on dense layers (DENSE_MAX_ROWS 6) and the 16-warp sparse MLA up to 6 rows. Kernels unchanged. At T 4 every width, row map, eh row set and kernel form is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [Fix] glm_dsa: run on released SGLang (megakernel API 2, multi-node TP8) - spec, draft_spec: API_VERSION 2; the built objects offer no EAGLE entries or graph variants (draft_loop, draft_extend, begin_draft_round, graph_variants are None). Restore the target's _Built (widths, summary, plan, forward), which the package split had dropped, so build returns a BuiltMegakernel again. - spec: accept Feature.MULTI_NODE_TP, so TP8 can span the two 4-GPU trays of an NVL72 rack, as the draft spec already does. - spec: drop the DsaView binding-member and EAGLE_DRAFT_CAPTURE_CHECK gates, which only an unmerged SGLang (sgl-project/sglang#46210) passes; the covered step reads the DSA backend through get_attn_backend() and never through those views. - attention: build the AttentionGlue directly (it has no build classmethod). - draft_spec: import the megakernel interface from its new location first. - glm_dsa, spec_step: declare every SGLang import in SGLANG_READS (the stricter contract test); import modules as modules (import a.b.c as c); name reads by the file that defines them. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [Fix] glm_dsa: name the NextN draft's architecture and pin its config fields draft_spec gets ARCHITECTURE and pins the fused draft layer's attention shapes and model_type (Requirements.hf_config), as test/unit/test_glm_dsa_config.py expects: a NextN config that differs in one of them is refused at load, naming it. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [Fix] glm_dsa: repair names lost in the package split (TP8 e2e on Rubin) - draft.py: read TARGET / tp_gather / per_rank from runtime.executor; runtime is now a package and no longer exports them. - attention/step.py, moe/step.py: restore @DataClass on StepContext and MoeRecords (capture failed with "StepContext() takes no arguments"). - moe/step.py, moe/__init__.py: import the shape constants, HIDDEN and MoeGlue where they are used; moe/__init__ re-exports from moe.step. - draft_attention.py: import AttentionGlue / FusedAttention / kernel_rows from attention.fused. Verified on two Rubin trays (TP8, --nnodes 2, dummy weights): the fused verify routes the decode graphs at 4/3/1 (bs 1-4) and 6/5/1 (bs 1-2), generate requests succeed; unit tests 582 passed. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [Feat] glm52/sm107: CuTeDSL megakernels for GLM-5.2 TP16 decode and MTP draft Adds sgl_nvidia_mega/glm52/sm107 (Rubin, sm_107a), enabled with GLM5MK=1 through the plugin. For bs=1 decode on agentx it reaches 1139 tok/s/user against stock SGLang's 352 (about 3.2x): - the 75-layer decoder chain runs as one kernel launched per layer with PDL overlap (MLA attention with the sparse indexer, NVFP4 MoE on tcgen05, cross-rank exchange over Lamport buffers); - dense layers 0-2, the MTP draft step and the fused verify/sampling tail replace the stock paths; - CuTeDSL ops keep the per-step bookkeeping (page table, post-verify, draft pages, tree layout) inside CUDA graph replay. Unit tests cover the hook-up, the kernel-tree import order and the ops against torch implementations. Co-Authored-By: Claude <[email protected]> * [Refactor] glm52/sm107: drop the draft diagnostics, ablation and test knobs and the unused non-TMA EH load path Co-Authored-By: Claude <[email protected]> * [Refactor] glm5_3/sm107: move the GLM-5.2 Rubin megakernels under glm5_3 Follow the model/<sm> layout of main's glm5_3/sm103: the TP16 bs=1 decode and MTP draft kernels move from glm52/sm107 to glm5_3/sm107. - add glm5_3/__init__.py, byte-identical to main's, so the package ships with setuptools' find_packages and merges with main without a conflict - rename the package, its error messages and the plugin hook-up (GLM52_TP16_ENV -> GLM5_3_SM107_ENV; the switch is still GLM5MK=1) - rename test/unit/test_glm52_sm107.py -> test_glm5_3_sm107.py - point the import-isolation test's PYTHONPATH at the repo root Kernel-internal outlined function names (glm52_*_piece) are unchanged so the compiled code stays identical to the validated build. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [Cleanup] glm_dsa: remove the package superseded by glm5_3 After merging main, GlmMoeDsaForCausalLM and its NextN draft are registered to glm5_3, which covers the same model, GPUs and TP8 verify / draft steps, so nothing loads glm_dsa any more. Remove the package, its config test and its CODEOWNERS line. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [Feat] glm5_3/sm107: serve TP16 through --enable-nvidia-megakernel The GLM-5.2 TP16 Rubin path is now selected by glm5_3's specs, with --enable-nvidia-megakernel as its only switch: - glm5_3/spec.py admits TP16 (sm_107 only) and dispatches by TP size: config_disable_reasons, model_disable_reasons and build go to sm107 at TP16. The TP8 / TP4 server arguments and environment move from Requirements (one per spec) to config_disable_reasons (per TP size), checked and worded as SGLang checks them. - glm5_3/draft.py: the NextN draft dispatches to sm107 at TP16. - sm107: build_target / build_draft pack the weights at build and bind the KV pools before the runner's CUDA graphs; the built megakernel plans one request's verify step (target) and draft steps (draft). sm107 pins its own validated server arguments and SGLANG_DSA_FUSE_TOPK. - Remove the GLM5MK switch, the sys.meta_path import hooks, the os._exit guards and root/sitecustomize.py. The cubin directory is SGLANG_NVIDIA_MEGAKERNEL_GLM_SM107_CUBINS, refused at argument resolution when unset or incomplete (no /glm5mk default). - Tolerate SGLang without dsa_backend.fused_dsa_target_verify_metadata. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [Docs] glm5_3/sm107: state the steps the TP16 path does not cover Co-Authored-By: Claude Opus 5.5 <[email protected]> * [Lint] Apply pre-commit (ruff, ruff-format, isort, codespell) Formatting only: ruff-format and isort over glm5_3/sm107 (mostly the kernels from #2), spec_step and the sm107 test. No statement changes: the ASTs match apart from import ordering, every module imports the same names, and the sm107 unit tests (including the CuTe DSL / GPU tests and the kernel-tree import order) pass on VR200. codespell: `arith.CmpFPredicate.OLT` (MLIR's ordered less-than) is marked `# codespell:ignore`. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [Test] glm5_3/sm107: ignore __pycache__ in the no-binaries check Any earlier test that imports the package writes git-ignored __pycache__/*.pyc under sm107, which this check flagged in CI. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Brayden Zhong <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Donglin Yang <[email protected]>
… of SGLang's capture check Since sgl-project/sglang#46210 the draft decode and draft extend CUDA graph runners run the capture tail hooks, which on a draft model runner include the monokernel capture check; it compares the captured steps with the target's verify keys and raises at the draft decode capture. The draft's wrapper routes and logs its own steps, so the draft's build makes the check skip a draft runner (the target runner's check is unchanged). Co-authored-by: Cursor <[email protected]>
…odel's graph captures The draft decode and draft extend CUDA graph runners run the model runner's capture tail hooks since #46210. On a draft model runner the AMD plugin's _CaptureCheck compares the draft's captured steps (a 1-token decode, a draft extend) with the target_verify keys the target's spec serves and raises, so a server with --enable-amd-monokernel and an MTP draft dies at the draft decode capture. Co-authored-by: Cursor <[email protected]>
Motivation
SGLang-side support for the GLM-5.3 / GLM-5.2 NVFP4 MTP megakernel specs in sgl-project/sgl-nvidia-mega#24 (merged). Those specs refuse at startup without this PR. It closes four gaps in the megakernel layer (#44945):
SGLANG_ENABLE_EMBED_REPLICATION, the EAGLE v2 draft worker binds the target's embedding / lm_head only after the KV pool is sized, so the draft's own copies still count against the KV cache.SGLANG_ENABLE_EMBED_REPLICATION=1refuses--enable-nvidia-megakernelunless the user also sets the variable by hand.Companion PR: #46211 (EAGLE draft step in one CUDA graph). The two are meant to land together.
Modifications
srt/kernel_plugins/nvidia/binding.py):ForwardView.dsareturns aDsaViewof the running DSA backend (Noneotherwise): MLA dims, decode impl, KV dtype, top-k / DCP settings, decode workspace, multi-CTA KV counter andindexer_metadata. The two buffers are sealed at first read and checked byassert_live.KvPoolViewgainsget_index_k_with_scale_buffer,index_page_sizeandset_mla_kv_buffer. NewTOLERATED_DSA_CALLABLES/TOLERATED_DSA_WRITESlist what a megakernel still calls or writes; existingTOLERATED_*lists are unchanged.eagle_draft_cuda_graph_runner.py,eagle_draft_extend_cuda_graph_runner.py): both runners call_run_capture_tail_hooksat the end of their stock captured forward. The draft-decode runner runs the hooks under its first per-step backend and checks that all per-step backends share one KV pool. Drafts served by a megakernel's owndraft_loop/draft_extenddo not take this path.spec.pyadds theEAGLE_DRAFT_CAPTURE_CHECKmarker and documents the draft step modes.eagle_worker_v2.py): under embedding replication,EagleDraftWorkerrunsinit_token_map/init_lm_headat the end of__init__instead of inalloc_memory_pool, so the draft's copies are freed before the KV pool is sized. Without replication nothing moves.spec.py,binding.py):FLAG_SETS_ENV = {SGLANG_ENABLE_EMBED_REPLICATION}.handle_nvidia_megakernelsets it to the spec's value when the spec requires it and the user left it unset, logs it, and restores it if the setup is refused. A user-set value is kept and checked as before (0is still refused).TestMegakernelBindingDsaintest_megakernel_binding.py,TestFlagSetsEnvintest_megakernel_guards.py, and newtest_megakernel_draft_capture.pyandtest/registered/unit/spec/test_eagle_draft_embed_share.py.Accuracy Tests
No output change is expected: the view only reads backend state, the hooks run only at capture, and the embedding change binds the same target tensors earlier. Measured on 8 x B300, TP 8, MTP, GLM-5.3 NVFP4, with this PR + #46211 + #45795 + sgl-nvidia-mega#24:
--enable-nvidia-megakernelalone (no replication env): the launcher logsset SGLANG_ENABLE_EMBED_REPLICATION=Trueonce and target + draft specs are active on 8 / 8 ranks.Speed Tests and Profiling
No per-step change in SGLang. The hooks run once per captured batch size at startup.
Checklist
CI States
Latest PR Test (Base): ✅ Run #44654964790
Latest PR Test (Extra): ⊘ Skipped -- add the
run-ci-nv-extralabel to opt in.Latest PR Test (AMD ROCm 10): ⏳ Run #44654964647
Tip
Base CI only covers the core cases, and its workload may not exercise your change. Use
/rerun-testto run the tests your change touches, or copy this prompt for an agent: