Skip to content

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

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

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

Closes #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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[nightly-verify] main is red

1 participant