Skip to content

[rl] SGLang generator: use SGLang's public attention-layer and FA kernel hooks - #6021

Draft
acisseJZhong wants to merge 3 commits into
pytorch:mainfrom
acisseJZhong:sglang-public-extension-points
Draft

acisseJZhong wants to merge 3 commits into
pytorch:mainfrom
acisseJZhong:sglang-public-extension-points

Conversation

@acisseJZhong

@acisseJZhong acisseJZhong commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • TorchTitanVarlenAttnBackend overrides FlashAttentionBackend._init_flash_attention_functions; SGLang main now passes ver= to flash_attn_with_kvcache, which the override does not accept.
  • _DecoderBody aliases each layer's RadixAttention at layers[i].attention.attn so 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

Not covered: ModelRegistry.models[...] = cls (item 1) waits on sgl-project/sglang#49333.

Testing

Main cfea168ae9 (SGLang 851642ade) vs. this PR (SGLang 10122ebc0a):

  • Numerics (H100): the same seeded requests, a turn and its cached next turn, give bitwise equal tokens, logprobs and cached-token counts for Varlen, GDN, SlidingWindow, MoEEP, MLA and KDA.
  • 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).
  • Performance (one GB300, TP 1, max_num_seqs=512): seconds until all requests finish, mean of 2 runs.
Model, workload Main This PR
Qwen3.5-9B, 256 req, 24K prompt, 1K out 125.0 121.8 (-2.5%)
Qwen3.5-9B, 1024 req, 512 prompt, 4K out 220.2 219.1 (-0.5%)
Qwen3-0.6B, 1024 req, 512 prompt, 4K out 212.8 211.5 (-0.6%)

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 11, 2026
@acisseJZhong
acisseJZhong force-pushed the sglang-public-extension-points branch 2 times, most recently from a1b976e to 20adf7e Compare November 20, 2026 05:16
@acisseJZhong
acisseJZhong changed the base branch from main to jessicazhong/sglang-unified-memory November 20, 2026 05:16
@acisseJZhong
acisseJZhong force-pushed the jessicazhong/sglang-unified-memory branch from 22be3b4 to 34ed86f Compare November 20, 2026 09:41
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
acisseJZhong force-pushed the sglang-public-extension-points branch from 20adf7e to 627f9e8 Compare November 21, 2026 06:51
@acisseJZhong
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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant