Repository navigation
[rl] SGLang generator: use SGLang's public attention-layer and FA kernel hooks - #6021
Draft
acisseJZhong wants to merge 3 commits into
Draft
acisseJZhong wants to merge 3 commits into
acisseJZhong wants to merge 3 commits into
Conversation
2 tasks done
acisseJZhong
force-pushed
the
sglang-public-extension-points
branch
2 times, most recently
from
November 20, 2026 05:16
a1b976e to
20adf7e
Compare
acisseJZhong
changed the base branch from
main
to
jessicazhong/sglang-unified-memory
November 20, 2026 05:16
acisseJZhong
force-pushed
the
jessicazhong/sglang-unified-memory
branch
from
November 20, 2026 09:41
22be3b4 to
34ed86f
Compare
acisseJZhong
added a commit
that referenced
this pull request
Nov 21, 2026
…tention states in one pool) (#6261) ## Summary (human-written) <!-- Write this yourself, not with an AI tool. 2-5 sentences: the problem, why this approach, and what you checked yourself. --> Utilize the unified memory feature in latest sglang, will delete this patch once upstream fix sgl-project/sglang#49681 lands. ## Why For GDN/KDA models, SGLang splits memory into a fixed KV pool and a fixed state pool (`mamba_full_memory_ratio`), so long-context and many-short-request workloads need opposite splits ([sgl-project/sglang#49676](sgl-project/sglang#49676)). SGLang's unified memory (`enable_unified_memory`) puts KV and states in one pool, but on `main` it fails at engine build: ``` AssertionError: --enable-page-major-kv-layout: the resolved attention backends ['torchtitan_varlen'] are not in the allowed set ['fa3', 'fa4', 'flashinfer', 'triton', 'trtllm_mha'] ``` ## What changed - `registry.py`: SGLang checks the unified memory backend allow-list by name; check `torchtitan_varlen` as `fa3`, the FlashAttention backend it subclasses (its varlen kernel reads the strided per-layer K/V views). TODO links the upstream gap. - `engine.py`: always set `enable_unified_memory` for GDN and KDA models, and reserve it from `extra_server_args`. SGLang builds the unified pool for hybrid models only, so other models are unchanged. - Tests: `TestSGLangBitwiseParityGDN` now runs on unified memory and checks that SGLang built the unified pool. `TestSGLangBitwiseParityGDNHiCache` adds HiCache and a next-turn parity check after evicting every GPU copy, so KV and GDN states load back from host memory. Added to the B200 CI job. - README: replace the state pool sizing section with a short paragraph. Follow-up: #6021 (stacked on this PR) moves SGLang to sgl-project/sglang#49681 and replaces the allow-list patch with `register_attention_backend(..., page_major_kv_layout=True)`. ## Performance Qwen3.5-9B, TP 1, one GB300, `gpu_memory_limit=0.86`, `max_num_seqs=512`, `cuda_graph="breakable"`, random prompts decoded to `max_tokens`. Seconds until all requests finish (mean of n runs), max running requests in parentheses. | Workload | Default ratio (0.9) | Ratio 0.25 | Unified memory (untuned) | Unified vs default | |---|---|---|---|---| | 512 req, 16K prompt, 8K out (KV-bound), n=3/2/3 | 819 (215) | 739 (301) | 771 (339) | 6% faster | | 1024 req, 512 prompt, 4K out (512 running), n=3/2/3 | 234 (512) | 267 (301) | 248 (512) | 6% slower | Under unified memory, the ratio only sets the running-request cap (`state slots // 3`); memory is not split. Unified memory does not speed up every workload: at equal running requests it costs ~1 ms of a ~25 ms decode step (scheduler + kernels). Its gain is that one setting stays within 4-6% of the best static split on both workloads, while each static split loses 11-14% on the other one. ## Testing `test_sglang_bitwise_parity.py` with the pinned SGLang (`851642ade`), unified memory on for every GDN and KDA class: | Class | GB300 (FA4) | H100 (FA3) | |---|---|---| | GDN | 7 passed | 7 passed | | GDNHiCache | 8 passed in 5 of 6 runs | 8 passed in 7 of 7 runs | | KDA | 6 passed | 3 failed, same as without unified memory | | KDADPEP | 6 passed, 1 skipped | 4 failed without unified memory, 1 with | | SlidingWindow, Varlen, MoEEP (unchanged) | SlidingWindow 6 passed | Varlen 7 passed, MoEEP 5 passed | - HiCache load-back: every cached token of the next turn comes from host memory (`cached_tokens_host`), and decode matches the trainer bitwise; also passes with `hicache_write_policy="write_back"` (run once, not in CI).
acisseJZhong
force-pushed
the
sglang-public-extension-points
branch
from
November 21, 2026 06:51
20adf7e to
627f9e8
Compare
acisseJZhong
changed the base branch from
jessicazhong/sglang-unified-memory
to
main
November 21, 2026 06:51
…nel hooks - TorchTitanVarlenAttnBackend passes its kernels as FlashAttentionBackend's kernels= instead of overriding the private _init_flash_attention_functions. - _DecoderBody lists its attention modules with get_attention_layers() instead of aliasing them at layers[i].attention.attn. - Pin SGLang to sgl-project/sglang#49681 and import next_power_of_2 from its new module.
…or SGLang's conv slice axis
acisseJZhong
force-pushed
the
sglang-public-extension-points
branch
from
November 21, 2026 18:59
4c9fe55 to
bd18ead
Compare
This branch has not been deployed
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.
Summary (human-written)
Assumes SGLang has sgl-project/sglang#49681: it pins SGLang to a commit with it.
Why
The SGLang generator uses two private SGLang hooks (sgl-project/sglang#48475, items 2 and 3), and one already breaks on SGLang main:
TorchTitanVarlenAttnBackendoverridesFlashAttentionBackend._init_flash_attention_functions; SGLang main now passesver=toflash_attn_with_kvcache, which the override does not accept._DecoderBodyaliases each layer'sRadixAttentionatlayers[i].attention.attnso SGLang's prefill CUDA graphs can find it.sgl-project/sglang#49681 adds public hooks for both, and lets a registered backend declare it runs on the page-major KV layout of unified memory.
What changed
attention/attention.py: pass the kernels throughFlashAttentionBackend(kernels=FlashAttentionKernels(...)).model_wrapper.py:_DecoderBody.get_attention_layers()replaces theattnalias.registry.py: registertorchtitan_varlenwithpage_major_kv_layout=Trueand delete the allow-list patch from [rl] SGLang engine: support SGLang's unified memory (KV and linear attention states in one pool) #6261.attention/kda.py: store each flattened replay state entry as(1, n)instead of(n,). Newer SGLang applies the conv TP axis (1) to every conv entry, so 1-D entries fail at engine build (invalid TP axis for tensor part 'conv.1').install_rl_env.sh: pin SGLang10122ebc0a([Feat] models: Public attention-layer lookup, FA kernel hooks and page-major backend registration for external models sgl-project/sglang#49681 on76589d6fde). TODO: move to its merge commit; newer SGLang changed scheduler APIs the engine uses.engine.py:next_power_of_2import moved (mechanical).Not covered:
ModelRegistry.models[...] = cls(item 1) waits on sgl-project/sglang#49333.Testing
Main
cfea168ae9(SGLang851642ade) vs. this PR (SGLang10122ebc0a):test_sglang_bitwise_parity.py(H100): Varlen, GDN, GDNHiCache, SlidingWindow and MoEEP pass; MLA, KDA and KDADPEP fail the same tests as on main (H100 only; CI runs B200).max_num_seqs=512): seconds until all requests finish, mean of 2 runs.