Skip to content

[Reland] Reinitialize ModelWrapper.cache_state_dict because set_model_state_dict has side effects - #977

Merged
fegin merged 4 commits into
mainfrom
gh/fegin/4/base
Mar 17, 2025
Merged

fegin merged 4 commits into
mainfrom
gh/fegin/4/base

Conversation

@fegin

@fegin fegin commented Mar 17, 2025

Copy link
Copy Markdown
Contributor

Reland #971 due to ghstack issues.

fegin added 4 commits March 17, 2025 11:30
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@pytorch-bot pytorch-bot Bot added the ci-no-td label Mar 17, 2025
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Meta Open Source bot. label Mar 17, 2025
@fegin
fegin merged commit 12cb82b into main Mar 17, 2025
MaxiBoether pushed a commit to eth-easl/torchtitan-mixtera that referenced this pull request Apr 17, 2025
…_state_dict has side effects (pytorch#977)

Reland pytorch#971 due to ghstack
issues.
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 13, 2026
…_state_dict has side effects (pytorch#977)

Reland pytorch#971 due to ghstack
issues.
xrsrke pushed a commit to NousResearch/torchtitan that referenced this pull request Feb 25, 2026
…_state_dict has side effects (pytorch#977)

Reland pytorch#971 due to ghstack
issues.
@fegin
fegin deleted the gh/fegin/4/base branch June 30, 2026 17:41
acisseJZhong added a commit that referenced this pull request Nov 20, 2026
…ing for GDN and KDA

The GDN and KDA backends register SGLang's extra_buffer mamba radix cache strategy
(sgl-project/sglang#48463), which keeps overlap scheduling: they snapshot each tracked
request's conv and recurrent state into its track slot. In prefill, Attention Gym's
StateCheckpoints writes the recurrent state straight into the track slot, and
copy_replay_tails gives batch-invariant KDA's slot its replay tail
(meta-pytorch/attention-gym#975, #977); decode snapshots come from SGLang's decode
tracking. The engine then always runs SGLang's overlap event loop, and requests with
a shared prompt reuse its cached state.
acisseJZhong added a commit that referenced this pull request Nov 20, 2026
…ing for GDN and KDA

The GDN and KDA backends register SGLang's extra_buffer mamba radix cache strategy
(sgl-project/sglang#48463), which keeps overlap scheduling: they snapshot each tracked
request's conv and recurrent state into its track slot. In prefill, Attention Gym's
StateCheckpoints writes the recurrent state straight into the track slot, and
copy_replay_tails gives batch-invariant KDA's slot its replay tail
(meta-pytorch/attention-gym#975, #977); decode snapshots come from SGLang's decode
tracking. The engine then always runs SGLang's overlap event loop, and requests with
a shared prompt reuse its cached state.
acisseJZhong added a commit that referenced this pull request Nov 20, 2026
…ing for GDN and KDA

The GDN and KDA backends register SGLang's extra_buffer mamba radix cache strategy
(sgl-project/sglang#48463), which keeps overlap scheduling: they snapshot each tracked
request's conv and recurrent state into its track slot. In prefill, Attention Gym's
StateCheckpoints writes the recurrent state straight into the track slot, and
copy_replay_tails gives batch-invariant KDA's slot its replay tail
(meta-pytorch/attention-gym#975, #977); decode snapshots come from SGLang's decode
tracking. The engine then always runs SGLang's overlap event loop, and requests with
a shared prompt reuse its cached state.
acisseJZhong added a commit that referenced this pull request Nov 20, 2026
…ing for GDN and KDA

The GDN and KDA backends register SGLang's extra_buffer mamba radix cache strategy
(sgl-project/sglang#48463), which keeps overlap scheduling: they snapshot each tracked
request's conv and recurrent state into its track slot. In prefill, Attention Gym's
StateCheckpoints writes the recurrent state straight into the track slot, and
copy_replay_tails gives batch-invariant KDA's slot its replay tail
(meta-pytorch/attention-gym#975, #977); decode snapshots come from SGLang's decode
tracking. The engine then always runs SGLang's overlap event loop, and requests with
a shared prompt reuse its cached state.
acisseJZhong added a commit that referenced this pull request Nov 20, 2026
…ing for GDN and KDA

The GDN and KDA backends register SGLang's extra_buffer mamba radix cache strategy
(sgl-project/sglang#48463), which keeps overlap scheduling: they snapshot each tracked
request's conv and recurrent state into its track slot. In prefill, Attention Gym's
StateCheckpoints writes the recurrent state straight into the track slot, and
copy_replay_tails gives batch-invariant KDA's slot its replay tail
(meta-pytorch/attention-gym#975, #977); decode snapshots come from SGLang's decode
tracking. The engine then always runs SGLang's overlap event loop, and requests with
a shared prompt reuse its cached state.
acisseJZhong added a commit that referenced this pull request Nov 20, 2026
…ing for GDN and KDA

The GDN and KDA backends register SGLang's extra_buffer mamba radix cache strategy
(sgl-project/sglang#48463), which keeps overlap scheduling: they snapshot each tracked
request's conv and recurrent state into its track slot. In prefill, Attention Gym's
StateCheckpoints writes the recurrent state straight into the track slot, and
copy_replay_tails gives batch-invariant KDA's slot its replay tail
(meta-pytorch/attention-gym#975, #977); decode snapshots come from SGLang's decode
tracking. The engine then always runs SGLang's overlap event loop, and requests with
a shared prompt reuse its cached state.
acisseJZhong added a commit that referenced this pull request Nov 20, 2026
…ing for GDN and KDA

The GDN and KDA backends register SGLang's extra_buffer mamba radix cache strategy
(sgl-project/sglang#48463), which keeps overlap scheduling: they snapshot each tracked
request's conv and recurrent state into its track slot. In prefill, Attention Gym's
StateCheckpoints writes the recurrent state straight into the track slot, and
copy_replay_tails gives batch-invariant KDA's slot its replay tail
(meta-pytorch/attention-gym#975, #977); decode snapshots come from SGLang's decode
tracking. The engine then always runs SGLang's overlap event loop, and requests with
a shared prompt reuse its cached state.
acisseJZhong added a commit that referenced this pull request Nov 20, 2026
…ing for GDN and KDA

The GDN and KDA backends register SGLang's extra_buffer mamba radix cache strategy
(sgl-project/sglang#48463), which keeps overlap scheduling: they snapshot each tracked
request's conv and recurrent state into its track slot. In prefill, Attention Gym's
StateCheckpoints writes the recurrent state straight into the track slot, and
copy_replay_tails gives batch-invariant KDA's slot its replay tail
(meta-pytorch/attention-gym#975, #977); decode snapshots come from SGLang's decode
tracking. The engine then always runs SGLang's overlap event loop, and requests with
a shared prompt reuse its cached state.
acisseJZhong added a commit that referenced this pull request Nov 20, 2026
…ing for GDN and KDA

The GDN and KDA backends register SGLang's extra_buffer mamba radix cache strategy
(sgl-project/sglang#48463), which keeps overlap scheduling: they snapshot each tracked
request's conv and recurrent state into its track slot. In prefill, Attention Gym's
StateCheckpoints writes the recurrent state straight into the track slot, and
copy_replay_tails gives batch-invariant KDA's slot its replay tail
(meta-pytorch/attention-gym#975, #977); decode snapshots come from SGLang's decode
tracking. The engine then always runs SGLang's overlap event loop, and requests with
a shared prompt reuse its cached state.
acisseJZhong added a commit that referenced this pull request Nov 20, 2026
…ing for GDN and KDA

The GDN and KDA backends register SGLang's extra_buffer mamba radix cache strategy
(sgl-project/sglang#48463), which keeps overlap scheduling: they snapshot each tracked
request's conv and recurrent state into its track slot. In prefill, Attention Gym's
StateCheckpoints writes the recurrent state straight into the track slot, and
copy_replay_tails gives batch-invariant KDA's slot its replay tail
(meta-pytorch/attention-gym#975, #977); decode snapshots come from SGLang's decode
tracking. The engine then always runs SGLang's overlap event loop, and requests with
a shared prompt reuse its cached state.
acisseJZhong added a commit that referenced this pull request Nov 20, 2026
…ing for GDN and KDA

The GDN and KDA backends register SGLang's extra_buffer mamba radix cache strategy
(sgl-project/sglang#48463), which keeps overlap scheduling: they snapshot each tracked
request's conv and recurrent state into its track slot. In prefill, Attention Gym's
StateCheckpoints writes the recurrent state straight into the track slot, and
copy_replay_tails gives batch-invariant KDA's slot its replay tail
(meta-pytorch/attention-gym#975, #977); decode snapshots come from SGLang's decode
tracking. The engine then always runs SGLang's overlap event loop, and requests with
a shared prompt reuse its cached state.
acisseJZhong added a commit that referenced this pull request Nov 20, 2026
…ing for GDN and KDA

The GDN and KDA backends register SGLang's extra_buffer mamba radix cache strategy
(sgl-project/sglang#48463), which keeps overlap scheduling: they snapshot each tracked
request's conv and recurrent state into its track slot. In prefill, Attention Gym's
StateCheckpoints writes the recurrent state straight into the track slot, and
copy_replay_tails gives batch-invariant KDA's slot its replay tail
(meta-pytorch/attention-gym#975, #977); decode snapshots come from SGLang's decode
tracking. The engine then always runs SGLang's overlap event loop, and requests with
a shared prompt reuse its cached state.
acisseJZhong added a commit that referenced this pull request Nov 20, 2026
…ing for GDN and KDA

The GDN and KDA backends register SGLang's extra_buffer mamba radix cache strategy
(sgl-project/sglang#48463), which keeps overlap scheduling: they snapshot each tracked
request's conv and recurrent state into its track slot. In prefill, Attention Gym's
StateCheckpoints writes the recurrent state straight into the track slot, and
copy_replay_tails gives batch-invariant KDA's slot its replay tail
(meta-pytorch/attention-gym#975, #977); decode snapshots come from SGLang's decode
tracking. The engine then always runs SGLang's overlap event loop, and requests with
a shared prompt reuse its cached state.

Pins Attention Gym to a main commit with these APIs (no release has them yet), which
renames its cuDNN-derived GDN/KDA backend to warpdrive.
acisseJZhong added a commit that referenced this pull request Nov 20, 2026
…pstream TODO

- Pin Attention Gym to a main commit with StateCheckpoints and copy_replay_tails
  (meta-pytorch/attention-gym#975, #977); no release has them yet. That commit
  renames the cuDNN-derived GDN/KDA backend to warpdrive, so import and select it
  by its new name, and point the missing-package hint at requirements.txt.
- README: linear attention models now hold requests that share a prompt.
- TODO(sgl-project/sglang#48463) on the SGLang internals the extra_buffer support
  relies on.
acisseJZhong added a commit that referenced this pull request Nov 20, 2026
…pstream TODO

- Pin Attention Gym to a main commit with StateCheckpoints and copy_replay_tails
  (meta-pytorch/attention-gym#975, #977); no release has them yet. That commit
  renames the cuDNN-derived GDN/KDA backend to warpdrive, so import and select it
  by its new name, and point the missing-package hint at requirements.txt.
- README: linear attention models now hold requests that share a prompt.
- TODO(sgl-project/sglang#48463) on the SGLang internals the extra_buffer support
  relies on.
acisseJZhong added a commit that referenced this pull request Nov 21, 2026
…ing for GDN and KDA

The GDN and KDA backends register SGLang's extra_buffer mamba radix cache strategy
(sgl-project/sglang#48463), which keeps overlap scheduling: they snapshot each tracked
request's conv and recurrent state into its track slot. In prefill, Attention Gym's
StateCheckpoints writes the recurrent state straight into the track slot, and
copy_replay_tails gives batch-invariant KDA's slot its replay tail
(meta-pytorch/attention-gym#975, #977); decode snapshots come from SGLang's decode
tracking. The engine then always runs SGLang's overlap event loop, and requests with
a shared prompt reuse its cached state.
acisseJZhong added a commit that referenced this pull request Nov 21, 2026
…pstream TODO

- Pin Attention Gym to a main commit with StateCheckpoints and copy_replay_tails
  (meta-pytorch/attention-gym#975, #977); no release has them yet. That commit
  renames the cuDNN-derived GDN/KDA backend to warpdrive, so import and select it
  by its new name, and point the missing-package hint at requirements.txt.
- README: linear attention models now hold requests that share a prompt.
- TODO(sgl-project/sglang#48463) on the SGLang internals the extra_buffer support
  relies on.
acisseJZhong added a commit that referenced this pull request Nov 21, 2026
…pstream TODO

- Pin Attention Gym to a main commit with StateCheckpoints and copy_replay_tails
  (meta-pytorch/attention-gym#975, #977); no release has them yet. That commit
  renames the cuDNN-derived GDN/KDA backend to warpdrive, so import and select it
  by its new name, and point the missing-package hint at requirements.txt.
- README: linear attention models now hold requests that share a prompt.
- TODO(sgl-project/sglang#48463) on the SGLang internals the extra_buffer support
  relies on.
acisseJZhong added a commit that referenced this pull request Nov 21, 2026
…ing for GDN and KDA (#6172)

## Summary (human-written)
<!-- Write this yourself, not with an AI tool. 2-5 sentences: the
problem, why
     this approach, and what you checked yourself. -->

## Why
SGLang runs our GDN/KDA backends (Qwen3.5, Kimi K3) with the `no_buffer`
mamba radix cache. That disables overlap scheduling, and because it
stores a state only at the end of a cached prefix, a repeated prompt
reuses nothing (sgl-project/sglang#48463). With `extra_buffer`, SGLang
keeps overlap on. During the forward, the backend writes the state at a
64-token-aligned position into a separate track slot. Attention Gym's
kernels now support that (meta-pytorch/attention-gym#975, #977).

| 150-token prompt, 50 decoded tokens | main (`no_buffer`) | this PR
(`extra_buffer`) |
|---|---|---|
| Overlap scheduling | off | on |
| Same prompt again | recomputes 150 tokens | reuses the state at token
128 |
| Next turn | resumes at token 200 | resumes at token 192 |

## What changed
- `registry.py`: register `support_mamba_cache_extra_buffer=True`.
- Prefill (`sglang/attention/linear_attention.py`,
`attention/{gdn,kda}.py`): SGLang's tracked position and track slot go
to Attention Gym as `StateCheckpoints(state_pool, positions, slots)`,
and the kernel writes the state into that slot. The conv history is
copied from the inputs. Batch-invariant KDA also calls
`copy_replay_tails` for the unfinished chunk. Prefills that take a
snapshot use Attention Gym's fused kernels, the only ones that write
checkpoints.
- Decode (`sglang/attention/{gdn,kda}.py`): SGLang's
`_track_mamba_state_decode` copies the states, and KDA zeroes the track
slot's replay count.
- `engine.py`: overlap is always on (the normal event loop path is
deleted), requests sharing a prompt are held for every model, and
`mamba_track_interval` is reserved at 64.
- Dependency (mechanical): pin Attention Gym to main `755691f`, the
first commit with both APIs, until a release has them. That commit
renames the cuDNN-derived GDN/KDA backend to warpdrive, so the imports
and `chunk_kernel_options` use the new name.

## Behavior changes for SGLang generator users
These apply to GDN/KDA models (Qwen3.5, Kimi K3); other models are
unchanged.
- Overlap scheduling is on: `SGLangEngine.step()` runs SGLang's overlap
event loop, as for other models.
- Samples of a rollout group wait for the first sample's prefill, then
start from its state at the prompt's last 64-token boundary.
- Each running request reserves 5 state slots instead of 3, so fewer
requests run at once with the same state pool (Qwen3.5-9B on GB300 at
the default ratio: 512 -> 428). If you set `max_mamba_cache_size` in
`extra_server_args`, size it as 5x the running requests instead of 3x.
- `mamba_track_interval` is fixed at 64: setting it in
`extra_server_args` now raises `ValueError`.

## Performance
Qwen3.5-9B and 0.8B (GDN), TP 1, one GB300, default mode,
`cuda_graph="breakable"`. 1000-token prompts, 512 tokens out per
request. Time until all requests finish, median of 6 runs.

| Workload | 9B: main → PR | 0.8B: main → PR |
|---|---|---|
| 128 unique prompts | 9.27 → 8.22 s (1.13x) | 5.62 → 4.75 s (1.19x) |
| GRPO: 32 prompts × 8 samples | 15.22 → 11.50 s (1.32x) | 9.91 → 7.89 s
(1.25x) |
| Next turn of 128 sessions (+64 tokens) | 8.29 → 7.24 s (1.14x) | 5.44
→ 4.49 s (1.21x) |

Overlap scheduling gives the 1.13-1.21x; GRPO also reuses each group's
prompt.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-no-td 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.

2 participants