Repository navigation
fix(attention): use the attention dtype for mixed bf16/fp16 paths - #2435
Merged
Merged
Conversation
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
force-pushed
the
fix/issue-2422-mixed-dtype-attention-follow-ups
branch
from
October 17, 2026 02:09
3f6bfd1 to
9857463
Compare
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.
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
sdpa_attention_dtype(q, k, v), next tosdpa_mask_for_inputs. It reads the backend's promotion table. cuDNN eligibility and score row bytes now use it, and the decision is factored intosdpa_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 inlib.rsshares the gate.compiled_softcap_sdpaand_gqa, including the grouped decode branch, cast masks throughsdpa_mask_for_inputsand return the attention dtype instead of q's and v's.dtype.cppno longer says bf16 + fp16 gives bf16. The new test exposed one more fact, now noted there: the patched fold is order-dependent on CUDA, soresult_type(fp16, bf16, bf16)is bf16. TurboQuant never produces that order.snapshot_standardandsnapshot_rotatingrefuse a populated non-FP16 cache by mode before the keys/values pairing check. This also closes a silent gap. On main, a populated symmetricturbo4KVCache(keys and values bothNone, offset 8) returnedOkand stored nothing, which a probe test confirmed. Empty quantized layers (KV-shared) are still skipped. Everykv_snapshotuser 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}, andgemma4_unified_mask_tests::turbo4_snapshot_donation_names_the_mode(throughCache::snapshot_into).With the fixes reverted, the chunk-gate, softcap, populated-turbo and Gemma 4 tests fail (
NonevsSome(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-corelayers::tests(102),softcap(18) andffi_tests(112), plus rootmodels::kv_snapshot(25),models::gemma4(121),gemma3,gemma2,afmoe,llama4andmuse_glimmer. Clippy-D warnings(core and root--lib --tests), fmt and thescripts/cichecks are clean.Real checkpoints
All runs use
MLXCEL_SDPA_DETERMINISTIC=1and temperature 0. "Before" is maincadc6366, and each before-arm showed self-parity.mlxcel-server -m ~/models/mlx/gemma-4-26b-a4b-it-4bit --kv-cache-mode fp16+turbo4with a 1987-token prompt and 300 tokens: coherent, and the response is byte-identical to before. The skip reason changes fromrotating cache has only one of keys/valuestorotating cache mode Turbo4Asym is not supported by model-state snapshots.mlxcel generate, 200 tokens: byte-identical before and after onqwen3-0.6b-4bit, and ongemma-2-2b-it-4bitwith default KV and withfp16+turbo4. That checkpoint is fp16, so both of its arms are same-dtype.gemma-2-2b-it-4bit(not committed) is byte-identical with default KV. Withfp16+turbo4it diverges at character 645 and stays coherent, which is expected from float32 masked scores.