Repository navigation
fix(models): compute the SSM A from A_log in float32 in six more models - #3122
Merged
Merged
Conversation
#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.
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
marked this pull request as ready for review
November 16, 2026 07:14
This was referenced Nov 16, 2026
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
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
Six Mamba-family models still ran
A = -exp(A_log)in the loaded dtype. An f16 load overflowsexppast 11.09, and a zerosoftplusstep then gives0 * -inf = NaNlogits (the #3046 failure that #3053 fixed for Mamba).expnow runs on a float32 copy everywhere, through one helper.mamba::ssm_a_f32is the unevaluated float32 graph;ssm_a_from_logevaluates it at load.Afromssm_a_from_logat load and reads it in the activation dtype like Mamba'sscan_a. Only f16 activations keep it in float32, and there the graph scans round the decay back to f16.nemotron_h_ssm_attn) and Mamba2'sssm_attncallssm_a_f32. This adds a per-head cast and no sync.Deviation from the issue: PLaMo2, Falcon-H1 and GraniteMoeHybrid keep their
a_logfield instead of storingA, because their decode path passesA_logtossm_update_kernel, which already computesexpin 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
Abit for bit. A bf16A_logdiffers from the old bf16exponly by bf16 rounding.Test plan
A_logramping to 19.5 gives finite prefill logits, bit-identical to a float32 copy. Jamba also checks that its storedAis float32 and exact. Nemotron-H callsnemotron_h_ssm_attndirectly.ssd_scantest: float32Ais bit-identical, bf16 stays within 2^-8 relative, and a bf16 scan matches its float32-A_logcopy.jamba_f16_model_with_an_overflowing_a_log_keeps_finite_logits: an all-f16 Jamba with an overflowingA_logand a zero step keeps finite f16 logits over prefill and three decode steps, on both the fused scan and the forced graph scan. CastingAto f16 inscan_a, or skipping the decay round-back, fails it.ssm_a_f32fails all 8 tests (the 7 above plus the existing Mamba test). Restoring it makes them pass.cargo clippy --lib --tests -- -D warnings,cargo fmt --check, plus the env-var-docs, native-env-writes and kernel-port-dispatch checks.Notes
A_lognext to bf16 weights now roundsAand the decay to the activation dtype, matching Mamba since fix(models): keep Mamba's SSM A in float32 so f16 loads stay finite #3053.A, so f16 Jamba (and Mamba, already since fix(models): keep Mamba's SSM A in float32 so f16 loads stay finite #3053) falls back to the per-step graph scan there. Metal and ROCm are unaffected. A follow-up issue will cover it.Closes #3057