Skip to content

feat(xla): accept bf16 scales and biases on the packed quant path - #3310

Merged
inureyes merged 8 commits into
mainfrom
feature/issue-603-xla-bf16-scales
Nov 23, 2026
Merged

inureyes merged 8 commits into
mainfrom
feature/issue-603-xla-bf16-scales

Conversation

@inureyes

@inureyes inureyes commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Summary

MLXCEL_XLA_QUANT=packed failed at load on MLX checkpoints whose affine scales and biases are bf16 (quant part model.layers.0.mlp.down_proj.biases dtype BF16, expected F16 on qwen3-0.6b-4bit). Those checkpoints now stay packed and dequantize in the graph.

  • One source of truth: when the loader turns on packed_args, it reads the dtype of every packed scales/biases tensor from the safetensors headers and stores it in the new Config::quant_scale_dtype. take_weight and take_lm_head declare the args with that type, and the QuantRaw upload uses the same field for both the expected dtype and the C dtype code (new code 3, IREE_HAL_ELEMENT_TYPE_BFLOAT_16 in xla_iree.c). A unit test checks the Rust codes against the C switch.
  • dequant_affine widens bf16 to f32 in the graph like f16, and it now asserts that the scales and biases are a matching f16 or bf16 pair.
  • A checkpoint that mixes f16 and bf16 metadata logs a warning and dequantizes at load.
  • MLXCEL_XLA_QUANT=native uses the same buffers, but qmv4.hip and qmv4.cu read half. On a bf16 checkpoint it now logs a warning and falls back to the f16 weight path (single-sequence and ragged) instead of failing at load. bf16 bytes are never passed to the kernels.

Validation (gfx1151, IREE hip, --temp 0 -n 32)

Checkpoint Run Result
qwen3-0.6b-4bit (BF16) main, packed load error, expected F16
qwen3-0.6b-4bit packed vs dequant-at-load 32/32 ids identical
qwen3-1.7b-4bit (BF16) packed vs dequant-at-load 32/32 ids identical
qwen3-0.6b-4bit native was a load error, now warns and falls back, ids identical
qwen3-0.6b-4bit with layer 3 up_proj rewritten to F16 packed mixed-dtype warning, dequant at load, ids identical
llama-3.2-1b-instruct-4bit (F16) packed, main vs branch ids identical, and all three graphs reused main's cache entries (the cache key hashes the MLIR text)

The #515 gate is a token-exact greedy trajectory, and these runs are token-exact.

  • cargo test --profile test-fast -p mlxcel-xla --lib: 455 passed (480 with the iree feature, linked by hand with the ROCm IREE arguments), and registered_fixtures_are_byte_exact passes unchanged. On main, the new shim-code test and the emitter bf16-declaration test fail.
  • clippy -D warnings on mlxcel-xla lib and tests, with and without --features iree; fmt.

Changes during review

  • Security review (bf08e0a): checkpoint_candidate_shards in iree.rs (used by the new scale-dtype probe, the native head probe and the weight loader) and resolve_weight_shards in phi4_audio.rs joined each weight_map value from model.safetensors.index.json onto the model directory unchecked, so an absolute or .. entry made the loader open a file outside the checkpoint. The new index_shard_path accepts only a plain file name (the rule mlxcel-core's validate_index_shards already applies), and a hostile name fails the load instead of falling back to the directory scan. All 49 local checkpoint indexes use plain names. candidate_shards_reject_index_names_outside_the_model_dir fails on the previous code. Pre-existing, reached by the new probe.
  • Correctness review: no findings that needed a change. Re-ran qwen3-0.6b-4bit on a rebuilt binary: packed and native (fallback) both match dequant-at-load over 32 tokens.
  • CHANGELOG Fixed entry for the shard-name check (c5d0510).

Pending: a CUDA run on GB10 (MLXCEL_XLA_DEVICE=cuda, qwen3-0.6b-4bit packed vs dequant-at-load).

Closes #603

MLXCEL_XLA_QUANT=packed rejected MLX affine checkpoints whose scales and biases are bf16 (Qwen3, Gemma3-27B, Qwen3-MoE), because the emitter hard-coded f16 metadata args and the loader errored on anything else.

The loader now reads the metadata dtype of every packed scales/biases tensor from the safetensors header and stores it in Config::quant_scale_dtype. The emitter declares the args with that type and the loader uploads the raw bytes with the matching dtype code (new code 3, IREE_HAL_ELEMENT_TYPE_BFLOAT_16 in the C shim), so both sides read one field. dequant_affine widens bf16 to f32 in the graph exactly like f16. A checkpoint mixing f16 and bf16 metadata falls back to dequant-at-load with a warning.

MLXCEL_XLA_QUANT=native shares the packed buffers, but its HIP and CUDA kernels read f16, so a bf16 checkpoint now falls back to the f16 weight path with a warning instead of failing at load.

f16 checkpoints emit byte-identical graphs; the registered goldens pass unchanged.

Closes #603
@inureyes inureyes added status:review Under review type:enhancement New features, capabilities, or significant additions priority:low Low priority area:inference Generation, sampling, decoding (incl. speculative, DRY) labels Nov 23, 2026
The XLA loader's shard list (`checkpoint_candidate_shards`), which the new packed scale-dtype probe, the native head probe and the weight loader all read, joined every `weight_map` value from an untrusted checkpoint's model.safetensors.index.json onto the model directory unchecked. An absolute path or a `..` entry made the loader mmap and parse a file outside the checkpoint. The Phi-4 audio shard resolver had the same gap.

Shard names must now be a single plain file name, matching `validate_index_shards` in mlxcel-core. A hostile name fails the load instead of falling back to the directory scan used for stale indexes. All 49 local checkpoint indexes use plain names.

The new test fails on the previous code (the `../` entry resolved outside the directory) and passes now; the mlxcel-xla lib suite with and without the iree feature and clippy -D warnings pass.

Refs #603
@inureyes

Copy link
Copy Markdown
Member Author

Security and performance review of the bf16 scale path. One finding fixed, nothing else open.

Fixed (bf08e0a, LOW, pre-existing but on the probe's path): checkpoint_candidate_shards joined each weight_map value from an untrusted index.json onto the model directory unchecked, so an absolute or .. entry made the probe and loader mmap a file outside the checkpoint. Shard names must now be a single plain file name (same rule as validate_index_shards in mlxcel-core), and a hostile name fails the load instead of falling back to the directory scan. The Phi-4 audio resolver got the same check. The new test fails on the old code.

Checked, no change needed:

  • Header parsing goes through SafeTensors::deserialize on an mmap: the header is capped at 100 MB, shape and offset arithmetic is checked, and each tensor's byte span must equal its shape times the element size, BF16 included. Only header pages are touched.
  • QuantRaw upload: the dtype must equal the config's scale dtype and the rank must be 2, so the C copy length (dims * 2 for code 3) always equals the safetensors span. The test-support byte counter uses 2 for code 3. xla_aux.c rejects code 3 as unknown, and the aux path never carries packed parts.
  • Native GEMV: bf16 or mixed metadata turns off the native path, and the loader still expects F16 when native is on, so bf16 bytes cannot reach qmv4.hip / qmv4.cu.
  • VMFB cache keys and the bundle fingerprint hash the MLIR text, so an f16 artifact cannot serve a bf16 graph.
  • Probe cost: headers total 100 to 270 KB on local Qwen3-4B, Gemma3-12B and Qwen3.6-35B-A3B (1 to 4 shards), a few ms per load, negligible next to the upload.

mlxcel-xla lib tests (with and without iree) and clippy -D warnings pass.

The shard-name check added in bf08e0a (index.json entries must be plain file names inside the model directory, applied to the shard probe, the IREE loader and the Phi-4 audio loader) had no changelog entry. Add one under Fixed.

Validation:
- cargo fmt --check, clippy -D warnings with and without --features iree on mlxcel-xla lib and tests, and cargo test -p mlxcel-xla --lib (455 passed) are clean
- scripts/ci/check_env_var_docs.py and check_used_by.py pass

Refs #603
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 23, 2026
@inureyes
inureyes merged commit 56bfb92 into main Nov 23, 2026
15 checks passed
@inureyes
inureyes deleted the feature/issue-603-xla-bf16-scales branch November 23, 2026 08:28
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:inference Generation, sampling, decoding (incl. speculative, DRY) priority:low Low priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: extend the OpenXLA packed-quant path to bf16 scales/biases

1 participant