Skip to content

fix(kv): compare Turbo codebook boundaries in the integer domain - #3733

Closed
hephaex wants to merge 1 commit into
lablup:mainfrom
hephaex:fix/3652-turbo-boundary-subnormal-clean
Closed

hephaex wants to merge 1 commit into
lablup:mainfrom
hephaex:fix/3652-turbo-boundary-subnormal-clean

Conversation

@hephaex

@hephaex hephaex commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Refs #3652.

Problem

The nightly-verify run for 4d8a05c4 failed mlxcel-core --lib on two device-vs-host index tests:

cache::turbo::codebook::tests::device_boundary_indices_match_host_at_the_edges
cache::turbo::quant::device_tests::device_pack_handles_boundary_ties_and_extremes

2688 passed; 2 failed

The night's diff named the input class: device 3 against host 4 at two positions.

Cause

Metal kernels compile with flush-to-zero, so a float32 greater reads a positive subnormal as 0.0. Every symmetric Turbo codebook from 3 bits up has a 0.0 boundary, so f32::from_bits(1) (one bucket above that boundary on the host) was landing on the boundary on device.

Probes against boundary_indices_on_device separate the layers: the upload round-trip and the sum both keep bits 1, greater(denormal, -1.0) is correct, and only greater(denormal, 0.0) and even denormal * 2.0 flush.

Change

boundary_indices_on_device compares an unsigned monotonic key instead, whose integer order equals the IEEE compare order, so no float compare takes part. Two host rules the raw bits do not carry are explicit:

  • -0.0 folds onto +0.0 (the host treats them as equal)
  • NaN maps to 0, so it sorts below every value including -inf.

The rest is the standard transform: flip the sign bit for non-negative values, every bit for negative ones.

Verification

On 4d8a05c4, Metal, macOS 27.2, then re-run on the merge base this PR targets:

  • cache::turbo::: 125 passed, 0 failed (was 2 failed).
  • cargo test -p mlxcel-core --lib -- --test-threads=1: 2722 passed, 7 ignored, 0 failed.
  • New regression test device_boundary_indices_keep_subnormals_above_a_zero_boundary pins the rule at bits 3/4 and head dims 64/128/256, negative side included. Mutation-checked: restoring the float compare fails it with left: [3, 3, 4] / right: [4, 4, 4].
  • The key was prototyped against the host over all six (bits, head_dim) combinations and 41/73 values each (0 mismatches) before the function changed.
  • cargo fmt --check and cargo clippy -p mlxcel-core --lib --tests -- -D warnings clean.

One file, codebook.rs only.

Metal kernels compile with flush-to-zero, so a float32 `greater` reads a
positive subnormal as `0.0`. Every symmetric codebook from 3 bits up has a
`0.0` boundary, so `f32::from_bits(1)` — one bucket above that boundary on
the host — was landing on the boundary on device. The nightly-verify run
46937282079 failed on exactly two values of exactly that shape:

  device: [... 3, 3, ...]   host: [... 4, 3, ...]

`boundary_indices_on_device` now compares an unsigned monotonic key whose
integer order is the IEEE-754 compare order, so no float compare is
involved: fold -0.0 onto +0.0, map NaN to 0 (it is above nothing), then flip
the sign bit for non-negative values and every bit for negative ones.
Integer compares do not flush.

Verified on the night's commit 4d8a05c (Metal, macOS 27.2):

- `cache::turbo::` 125 passed, 0 failed (was 2 failed).
- `cargo test -p mlxcel-core --lib -- --test-threads=1`: 2687 passed,
  7 ignored, 0 failed.
- New regression test `device_boundary_indices_keep_subnormals_above_a_zero_boundary`
  pins the rule at bits 3/4 and head dims 64/128/256; mutation-checked by
  restoring the float compare, which fails it with `left: [3, 3, 4]` /
  `right: [4, 4, 4]`.
- `cargo fmt --check` and `cargo clippy -p mlxcel-core --lib --tests -- -D warnings` clean.

Refs lablup#3652
@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:high High priority labels Dec 4, 2026
@inureyes

inureyes commented Dec 4, 2026

Copy link
Copy Markdown
Member

Holding this PR: main already carries an equivalent fix for the same function, so merging it now would replace one correct integer-key compare with another.

What landed on main. be51bc391 (#3686, merged today) rewrote boundary_indices_on_device to compare signed integer order keys instead of floats: bits & 0x7fffffff for non-negative values, its negation for negative ones (so -0.0 and +0.0 share key 0), and NaN mapped to the -inf key so it counts above no finite boundary. I checked it against the host rule (partition_point(|b| b < v)) for every class this PR targets: positive and negative subnormals around the 0.0 boundary, both zeros, both infinities, both NaN signs, the finite extremes, and each boundary with its next_up/next_down. It matches in all of them. It also casts the input to float32 before the view, which this PR's version does not, so a non-f32 input would be reinterpreted by view(UINT32) here and not on main.

Your version is also correct. The unsigned key (flip the sign bit for non-negative values, every bit for negative ones, fold -0.0, NaN to 0) reproduces IEEE order exactly, and the encode path has no separate Metal or CUDA kernel. The custom Turbo kernels only dequantize, so both quantizers go through this one function and stay in sync. The two fixes do the same thing.

Test value. The new device_boundary_indices_keep_subnormals_above_a_zero_boundary is covered by the existing device_boundary_indices_match_host_at_the_edges, which already checks f32::from_bits(1), its negative, and boundary.next_up() for the 0.0 boundary (the same value), and asserts the upper index there.

#3652 scope. The tracker has more failures than these two tests: the night of 9402cf89 failed mlx_evaluating_lib_tests_take_mlx_test_guard, the gemma3 a_truncated_quantized_restore_reproduces_a_cold_prefill_of_the_prefix, step3p7 text_decode_delegates_sequence_local_native_batching, and layers::tests::prefill_dense_gemm_matches_qmm_bytes_where_eligible. I changed Closes #3652 to Refs #3652 in the body so the tracker would stay open if this ever merges.

What would make it mergeable. Merge origin/main (it conflicts in codebook.rs), keep main's implementation, and leave only something main lacks. One option is a device-vs-host sweep over random raw bit patterns, which would test the key across every float class rather than hand-picked edges. Without that, I suggest closing this as superseded by #3686.

@hephaex

hephaex commented Dec 5, 2026

Copy link
Copy Markdown
Contributor Author

Closing as a duplicate: the FTZ defect this branch fixes is already on main.

be51bc391 ("feat(speculative): add DeepSeek-V4.1 bundled DSpark drafter (#3262)") carries the same fix in boundary_indices_on_device, with a comment naming the same cause — "Metal floating-point comparisons may flush subnormals to zero. Compare ..." — and an order_key that maps IEEE compare order onto unsigned integers via sign/magnitude, going further than this branch by also giving both signed zeros the same key and mapping input NaNs below the boundaries.

Upstream's version therefore supersedes this one on the same defect, so there is nothing to rebase.

Note that #3652 remains open as the nightly-red tracking issue and needs a fresh look at the current run; that is separate from this specific FTZ fix, which is now in main.

@hephaex hephaex closed this Dec 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:high High priority status:review Under review type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants