Skip to content

fix(models): compute the SSM A from A_log in float32 in six more models - #3122

Merged
inureyes merged 4 commits into
mainfrom
fix/issue-3057-ssm-a-f32
Nov 16, 2026
Merged

inureyes merged 4 commits into
mainfrom
fix/issue-3057-ssm-a-f32

Conversation

@inureyes

@inureyes inureyes commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Summary

Six Mamba-family models still ran A = -exp(A_log) in the loaded dtype. An f16 load overflows exp past 11.09, and a zero softplus step then gives 0 * -inf = NaN logits (the #3046 failure that #3053 fixed for Mamba). exp now runs on a float32 copy everywhere, through one helper.

  • mamba::ssm_a_f32 is the unevaluated float32 graph; ssm_a_from_log evaluates it at load.
  • Jamba stores A from ssm_a_from_log at load and reads it in the activation dtype like Mamba's scan_a. Only f16 activations keep it in float32, and there the graph scans round the decay back to f16.
  • The shared chunked SSD scan (PLaMo2, Falcon-H1, GraniteMoeHybrid and Nemotron-H prefill, so also nemotron_h_ssm_attn) and Mamba2's ssm_attn call ssm_a_f32. This adds a per-head cast and no sync.

Deviation from the issue: PLaMo2, Falcon-H1 and GraniteMoeHybrid keep their a_log field instead of storing A, because their decode path passes A_log to ssm_update_kernel, which already computes exp in float32. Since the issue was filed, their prefill moved into the shared SSD scan, so fixing that one site covers them. No other code reads these fields (sanitize, quantization skip lists and state snapshots do not touch them).

Float32 loads give the same A bit for bit. A bf16 A_log differs from the old bf16 exp only by bf16 rounding.

Test plan

  • One test per model: an f16 A_log ramping to 19.5 gives finite prefill logits, bit-identical to a float32 copy. Jamba also checks that its stored A is float32 and exact. Nemotron-H calls nemotron_h_ssm_attn directly.
  • ssd_scan test: float32 A is bit-identical, bf16 stays within 2^-8 relative, and a bf16 scan matches its float32-A_log copy.
  • jamba_f16_model_with_an_overflowing_a_log_keeps_finite_logits: an all-f16 Jamba with an overflowing A_log and a zero step keeps finite f16 logits over prefill and three decode steps, on both the fused scan and the forced graph scan. Casting A to f16 in scan_a, or skipping the decay round-back, fails it.
  • Mutation check: removing the float32 cast in ssm_a_f32 fails all 8 tests (the 7 above plus the existing Mamba test). Restoring it makes them pass.
  • Existing suites for jamba, plamo2, falcon_h1, granitemoehybrid, mamba2, nemotron_h, mamba, ssd_scan, last_row_logits_stateful and granite4_vision: 143 passed.
  • cargo clippy --lib --tests -- -D warnings, cargo fmt --check, plus the env-var-docs, native-env-writes and kernel-port-dispatch checks.

Notes

Closes #3057

#3053 moved Mamba to a float32 A = -exp(A_log), because a bf16 checkpoint cast to f16 at load overflows exp past 11.09 and a zero softplus step then computes 0 * -inf = NaN. Jamba, PLaMo2, Falcon-H1, GraniteMoeHybrid, Mamba2 and Nemotron-H still ran exp in the loaded dtype.

The exp now lives in one place, mamba::ssm_a_f32. Jamba computes A once at load with ssm_a_from_log and reads it in the activation dtype like Mamba's scan_a, keeping it float32 only for f16 activations, where its graph scans round the decay back. The shared chunked SSD scan (PLaMo2, Falcon-H1, GraniteMoeHybrid, Nemotron-H prefill) and Mamba2's ssm_attn call ssm_a_f32 per forward. Those four models keep their A_log field because their decode kernels take A_log and already compute exp in float32.

Float32 loads compute the same A bit for bit. A bf16 A_log differs from the old bf16 exp only by its bf16 rounding.

Tests: each of the six models loads an f16 A_log ramping to 19.5 and asserts finite prefill logits that match a float32 copy of A_log bit for bit (Jamba also checks its stored A is float32 and exact; Nemotron-H drives nemotron_h_ssm_attn directly). Where the model allows a zero step floor the test uses a zero-step dt bias, the NaN case; PLaMo2's fixed 0.001 floor uses a step near 2 so the f16 rounding of A reaches the logits. An SSD scan test pins the float32 A and bounds the bf16 difference. All fail with the float32 cast removed from ssm_a_f32.
@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority area:models Model architectures, weights, loading, metadata arch:hybrid Hybrid attention stack (linear/sliding/full or attention+SSM interleave) arch:ssm State-space / recurrent model (Mamba, RWKV) status:done Completed and removed status:review Under review labels Nov 16, 2026
The f16 branch of JambaMambaMixer::scan_a (A kept in float32) and decay_in_activation_dtype (decay rounded back to f16) had no test, because the Jamba fixtures run in float32.

The new test converts the whole Mamba Jamba fixture to f16, uses an A_log ramping to 19.5 and a dt_proj bias whose softplus is zero, and runs a prefill plus three decode steps on the fused scan and on the forced graph scan. It asserts f16, finite logits and that the forced run walks every step. Casting A to f16 in scan_a fails it with NaN logits; skipping the decay round-back fails it on the logits dtype.

Refs #3057
@inureyes
inureyes marked this pull request as ready for review November 16, 2026 07:14
@inureyes
inureyes merged commit 1f0ad0a into main Nov 16, 2026
32 checks passed
@inureyes
inureyes deleted the fix/issue-3057-ssm-a-f32 branch November 16, 2026 07:18
inureyes added a commit that referenced this pull request Nov 16, 2026
…3128)

## Summary

- CUDA's graph-exact Mamba1 scan now accepts f16 `x` with the float32 `A` that f16 Mamba and Jamba keep (f16 cannot hold `-exp(A_log)`), so f16 Jamba on CUDA takes the kernel instead of the per-timestep graph scan. `mamba1_scan_kernel_accepts` admits exactly that one extra mix (every other input in f16). Metal and ROCm (float-state port) are untouched: the new `TA` template arg is added only for the CUDA port.
- The kernel's new `mamba1_decay` mirrors what the graph scan does for that pair (`decay_in_activation_dtype` in jamba.rs, `MambaBlock::ssm_step` in mamba.rs): MLX `multiply` promotes delta to float32 and rounds once (`__fmul_rn`), `exp` runs on float32 (`Exp{}`), and `astype` rounds the decay to f16 (`static_cast`, as MLX's CastOp does). Uniform-dtype inputs run the same ops as before.
- `ssm_a_f32` and `ssm_a_from_log` move from mamba.rs into a new `models::ssm_a` module; Mamba, Jamba, Mamba2, the SSD scan and their tests import from there. No behavior change.
- `jamba_f16_model_with_an_overflowing_a_log_keeps_finite_logits` (from #3122) now returns early without a GPU backend: its MoE fixture hits MLX's CPU `GatherMM`, which only takes float32, and aborted the whole CPU-only lib test process.

Mamba / Falcon-Mamba still take the per-step graph scan on CUDA by design (their gate reads the float-state port; switching would change CUDA output, see `MambaBlock::forward`). Their f16 scan inputs are now accepted by the predicate, which a test pins, but the Mamba forward path itself is unchanged.

## Test plan

- [x] `mamba1_scan` (mlxcel-core) and `models::mamba`, `models::jamba`, `models::ssd_scan` pass on Metal, also after merging current main; clippy `--lib --tests -D warnings` (both crates), fmt, and the env-var, native-env, kernel-port-dispatch and kernel-dtype-key checks pass.
- [x] New everywhere-run test `graph_exact_dtype_rule_admits_only_uniform_inputs_and_f16_with_float32_a` fails under both mutations tried (rule widened to any float32 `A`; rule reverted to uniform only).
- [x] GB10 (sm_121), `--features cuda`: `mamba1_scan` 7 passed, including `cuda_kernel_is_bit_identical_to_the_graph_scan_for_f16_x_with_float32_a` and the extended `cuda_kernel_declines_mixed_dtype_inputs`; `models::jamba models::mamba models::ssd_scan` 56 passed, 3 ignored, including `jamba_f16_cuda_kernel_matches_the_graph_scan_bit_for_bit` and `jamba_mamba_prefill_takes_the_fused_scan_where_a_port_exists`. The JIT source compiles under NVRTC.
- [x] GB10 prefill A/B on `ai21-jamba-reasoning-3b-4bit` with `MLXCEL_CUDA_F16_NORMALIZE=1`, 2151-token prompt, 3 runs each (details in the PR comment).

| Scan path | Prefill (tok/s) | Decode (tok/s) |
|---|---|---|
| Kernel | 2071.8 / 2091.5 / 2115.9 | 99.4 / 101.1 / 101.2 |
| Graph scan (`MLXCEL_MAMBA1_SCAN_KERNEL=0`) | 1003.3 / 1012.1 / 1058.1 | 92.3 / 95.1 / 95.8 |

Prefill is about 2.0x faster and decode about 6% faster. Greedy output (32 tokens) is byte-identical across all six runs and finite. Not measured: an 8192-token prompt.

Closes #3125
@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) arch:ssm State-space / recurrent model (Mamba, RWKV) area:models Model architectures, weights, loading, metadata priority:medium Medium priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(models): compute the SSM A from A_log in float32 in the remaining Mamba-family models

1 participant