Skip to content

perf(models): chunk the Mamba2 SSD and block the Mamba1 graph scan - #3087

Merged
inureyes merged 2 commits into
mainfrom
perf/issue-3073-chunked-ssm-scans
Nov 15, 2026
Merged

inureyes merged 2 commits into
mainfrom
perf/issue-3073-chunked-ssm-scans

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

  • The Falcon-H1, GraniteMoeHybrid and PLaMo 2 SSD scans built the [B, H, L, L] float32 surrogate attention over the whole prefill piece. They now share one float32-hardened chunked scan with NemotronH (src/models/ssd_scan.rs). The chunk is 256 tokens, as in mlx-lm's ssm_attn, independent of the piece. NemotronH keeps its configured chunk_size and per-chunk eval. A single pass longer than 2048 tokens schedules each window's output and state with schedule_prefill_state. The callers keep the fix(models): release long prefill inputs held by lazy recurrent state #3063 behavior: the final state is scheduled and conv tails go through materialized_state_tail.
  • Jamba's Mamba1 graph-scan fallback built (delta * x) B and exp(delta A) as [B, L, D, N] before its sequential loop. graph_selective_scan now builds them per 256-step block, and the result is bit-identical for any block length. The Mamba fallback (mamba.rs) already projects one timestep at a time, so its state is [B, D, N] per step, and it is unchanged.

Measurements (M5 Max, 28K prompt, two order-reversed cooled rounds)

Checkpoint Single pass, main / branch 2048 chunks peak, main / branch 2048 chunks prefill tok/s, main / branch 2048 greedy output
granite-4.0-h-tiny-4bit OOM / 17.10 GB 9.85 / 7.46 GB 1,834-2,345 / 3,639-5,872 identical
falcon-h1-tiny-90m-instruct-4bit OOM / 9.99 GB 3.99 / 2.28 GB 6,756-7,222 / 17,916-20,439 differs (see trace)
plamo-2-1b OOM abort / 25.62 GB 12.21 / 9.90 GB 5,191-6,115 / 8,130-8,910 identical
ai21-jamba-reasoning-3b-4bit 6.45 / 6.45 GB 3.25 / 3.25 GB unchanged within noise identical

The chunk changes the arithmetic order. Teacher-forced logit_trace at forward width 2112 (16,896 positions each), main as the reference, decided gap 2:

Checkpoint Decided disagreement Perplexity delta
granite-4.0-h-tiny-4bit 4 / 16,148 (0.025%), largest gap 2.75 -0.098%
falcon-h1-tiny-90m-instruct-4bit 0 / 12,168 -0.024%
plamo-2-1b 0 / 9,400 +0.007%

Falcon-H1 tiny's greedy divergence is in output that is already degenerate at 28K on both arms. Full tables and CSVs are in docs/benchmark_results/ssm-prefill-scan-chunking-m5max-2026-11-15.md.

Test plan

  • New unit tests: chunked against unchunked SSD for f32 and f16, with and without a prior state, and every eval policy bit-identical (ssd_scan::tests). Jamba graph-scan blocks bit-identical, and a forced fallback prefill over three blocks matches the fused kernel (jamba::tests).
  • Family unit tests for ssd_scan, jamba, falcon_h1, granitemoehybrid, plamo2 and nemotron_h, including nemotron_h's ignored chunked-SSD oracles.
  • tests/mamba2_hybrid_finite.rs and tests/mamba2_hybrid_decode_finite.rs with --include-ignored against the real checkpoints (MLXCEL_REQUIRE_MODELS=1).
  • clippy, fmt, check_env_var_docs.py, check_benchmark_raw_logs.py, make verify-kernel-dtype-keys.

Closes #3073

The Falcon-H1, GraniteMoeHybrid and PLaMo 2 SSD scans built the [B, H, L, L] float32 surrogate attention over the whole prefill piece, so a 28K single-pass prefill of granite-4.0-h-tiny or falcon-h1-tiny ran out of GPU memory on M5 Max. The three families and NemotronH now share one float32-hardened chunked scan (src/models/ssd_scan.rs) with a chunk length independent of the piece: 256 tokens, as in mlx-lm's ssm_attn, for the three, and NemotronH's configured chunk_size with its per-chunk eval unchanged. A single pass longer than 2048 tokens schedules each window's output and state with schedule_prefill_state; the callers keep scheduling the final state and storing conv tails through materialized_state_tail as after #3063.

Jamba's Mamba1 graph-scan fallback built (delta * x) B and exp(delta A) as [B, L, D, N] for the whole piece before its sequential loop. It now builds them per 256-step block in graph_selective_scan, which is bit-identical for any block length. Mamba's fallback already projects one timestep at a time and is unchanged.

Validated on M5 Max: single pass completes for all three families (granite 17.1 GB), 2048-chunk greedy output is byte-identical to main for granite and PLaMo 2, and the teacher-forced logit trace shows 0 to 0.025% disagreement at decided positions. New unit tests cover chunked against unchunked scans and a forced Jamba fallback.

Refs #3073
@inureyes inureyes added status:done Completed type:performance Performance improvements priority:high High priority area:models Model architectures, weights, loading, metadata area:inference Generation, sampling, decoding (incl. speculative, DRY) arch:hybrid Hybrid attention stack (linear/sliding/full or attention+SSM interleave) labels Nov 15, 2026
@inureyes
inureyes marked this pull request as ready for review November 15, 2026 13:08
@inureyes
inureyes merged commit 05312c6 into main Nov 15, 2026
@inureyes
inureyes deleted the perf/issue-3073-chunked-ssm-scans branch November 15, 2026 13:09
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch:hybrid Hybrid attention stack (linear/sliding/full or attention+SSM interleave) area:inference Generation, sampling, decoding (incl. speculative, DRY) area:models Model architectures, weights, loading, metadata priority:high High priority status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(models): SSM prefill scans materialize full-sequence intermediates

1 participant