Skip to content

fix(attention): use the attention dtype for mixed bf16/fp16 paths - #2435

Merged
inureyes merged 4 commits into
mainfrom
fix/issue-2422-mixed-dtype-attention-follow-ups
Oct 17, 2026
Merged

inureyes merged 4 commits into
mainfrom
fix/issue-2422-mixed-dtype-attention-follow-ups

Conversation

@inureyes

@inureyes inureyes commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

The four mixed bf16/fp16 defects that review of #2417 found. A bf16 model on an fp16 TurboQuant KV cache attends in result_type(q, k, v), which is float32. Same-dtype attention is unchanged.

Closes #2422

Changes

  • Chunk gate (CUDA). New bridge helper sdpa_attention_dtype(q, k, v), next to sdpa_mask_for_inputs. It reads the backend's promotion table. cuDNN eligibility and score row bytes now use it, and the decision is factored into sdpa_query_chunk_within_budget(.., budget). bf16 over fp16 now chunks at head_dim 128 and is sized at 4 bytes per score at head_dim 256. The maskless causal path in lib.rs shares the gate.
  • Softcap composite (Gemma 2). compiled_softcap_sdpa and _gqa, including the grouped decode branch, cast masks through sdpa_mask_for_inputs and return the attention dtype instead of q's and v's.
  • Overlay header. dtype.cpp no longer says bf16 + fp16 gives bf16. The new test exposed one more fact, now noted there: the patched fold is order-dependent on CUDA, so result_type(fp16, bf16, bf16) is bf16. TurboQuant never produces that order.
  • Snapshot skip reason. snapshot_standard and snapshot_rotating refuse a populated non-FP16 cache by mode before the keys/values pairing check. This also closes a silent gap. On main, a populated symmetric turbo4 KVCache (keys and values both None, offset 8) returned Ok and stored nothing, which a probe test confirmed. Empty quantized layers (KV-shared) are still skipped. Every kv_snapshot user gets the fix: Gemma 3, AFMoE, Gemma 4, Llama 4 and Muse Glimmer. Qwen3-Next/3.5, LFM2 and Bailing already check the mode first. Jamba, Granite MoE Hybrid, Nemotron-H, Plamo2 and Falcon-H1 get no injected KV mode.

Tests and revert check

New tests: layers::tests::query_chunk_gate_sizes_mixed_half_kv_from_the_attention_dtype (it also asserts the same-dtype decisions), layers::tests::sdpa_attention_dtype_follows_the_backend_promotion_table, ffi_tests::test_compiled_softcap_sdpa_mixed_half_kv_attends_in_float32, models::kv_snapshot::tests::{populated_turbo_caches_are_refused_by_mode, empty_turbo_caches_are_skipped_not_refused}, and gemma4_unified_mask_tests::turbo4_snapshot_donation_names_the_mode (through Cache::snapshot_into).

With the fixes reverted, the chunk-gate, softcap, populated-turbo and Gemma 4 tests fail (None vs Some(16), output dtype fp16, has only one of keys/values). The softcap test's mask carries finite biases, so reverting only the mask cast also fails it, on the float32 comparison. The promotion-table test and the empty-cache test pass on main by design.

Run on GB10 (--features cuda, --test-threads=1, all passing): -p mlxcel-core layers::tests (102), softcap (18) and ffi_tests (112), plus root models::kv_snapshot (25), models::gemma4 (121), gemma3, gemma2, afmoe, llama4 and muse_glimmer. Clippy -D warnings (core and root --lib --tests), fmt and the scripts/ci checks are clean.

Real checkpoints

All runs use MLXCEL_SDPA_DETERMINISTIC=1 and temperature 0. "Before" is main cadc6366, and each before-arm showed self-parity.

  • mlxcel-server -m ~/models/mlx/gemma-4-26b-a4b-it-4bit --kv-cache-mode fp16+turbo4 with a 1987-token prompt and 300 tokens: coherent, and the response is byte-identical to before. The skip reason changes from rotating cache has only one of keys/values to rotating cache mode Turbo4Asym is not supported by model-state snapshots.
  • mlxcel generate, 200 tokens: byte-identical before and after on qwen3-0.6b-4bit, and on gemma-2-2b-it-4bit with default KV and with fp16+turbo4. That checkpoint is fp16, so both of its arms are same-dtype.
  • The mixed softcap path needs a bf16 Gemma 2, and every mlx-community Gemma 2 is fp16. A local bf16 conversion of gemma-2-2b-it-4bit (not committed) is byte-identical with default KV. With fp16+turbo4 it diverges at character 645 and stays coherent, which is expected from float32 masked scores.

@inureyes inureyes added type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers platform:linux Linux (CUDA / packaging) specific status:review Under review status:done Completed and removed status:review Under review labels Oct 17, 2026
A bf16 model on an fp16 TurboQuant KV cache attends in result_type(q, k, v), which is float32, but the CUDA query-chunk gate and the softcap composite still read q's or v's dtype, and snapshot donation under turbo4 logged a misleading reason.

- New bridge helper sdpa_attention_dtype(q, k, v), next to sdpa_mask_for_inputs. The chunk gate uses it for cuDNN eligibility and score row bytes, and the decision is factored into sdpa_query_chunk_within_budget so tests can pass a budget.
- compiled_softcap_sdpa and _gqa cast masks through sdpa_mask_for_inputs and return the attention dtype.
- The CUDA dtype.cpp overlay header no longer claims bf16 + fp16 gives bf16, and it notes that the patched fold is order-dependent.
- snapshot_standard and snapshot_rotating refuse a populated non-FP16 cache by mode before the keys/values pairing check. This also stops a populated symmetric turbo4 KVCache from being dropped from the snapshot with Ok.

Same-dtype attention is unchanged: greedy output is byte-identical before and after on qwen3-0.6b-4bit and gemma-2-2b-it-4bit. Each new test fails with its fix reverted.

Closes #2422
Review follow-up. The mixed-dtype softcap test only caught the output dtype: a causal mask's 0/-inf values survive a bf16 cast exactly, so rounding the mask through the query dtype went unnoticed. The mask now carries finite biases bf16 cannot represent. With only the mask cast reverted, the test fails on the float32 comparison.

The symmetric Turbo4 snapshot assertion matched "Turbo4" as a substring of "Turbo4Asym". It now matches the whole "cache mode Turbo4 is not supported" phrase.

Refs #2422
@inureyes
inureyes force-pushed the fix/issue-2422-mixed-dtype-attention-follow-ups branch from 3f6bfd1 to 9857463 Compare October 17, 2026 02:09
@inureyes
inureyes merged commit 044df19 into main Oct 17, 2026
27 checks passed
@inureyes
inureyes deleted the fix/issue-2422-mixed-dtype-attention-follow-ups branch October 17, 2026 03:06
@inureyes inureyes added this to the 0.8 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core mlxcel-core: MLX FFI, primitives, KV cache, layers platform:linux Linux (CUDA / packaging) specific 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(attention): mixed bf16/fp16 attention follow-ups after #2417

1 participant