Repository navigation
feat(xla): accept bf16 scales and biases on the packed quant path - #3310
Merged
Merged
Conversation
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
4 of 5 tasks
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
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): Checked, no change needed:
mlxcel-xla lib tests (with and without |
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
…-bf16-scales # Conflicts: # src/lib/mlxcel-xla/src/emitter/mod.rs
…-bf16-scales # Conflicts: # CHANGELOG.md
…-bf16-scales # Conflicts: # CHANGELOG.md
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.
Summary
MLXCEL_XLA_QUANT=packedfailed at load on MLX checkpoints whose affine scales and biases are bf16 (quant part model.layers.0.mlp.down_proj.biases dtype BF16, expected F16on qwen3-0.6b-4bit). Those checkpoints now stay packed and dequantize in the graph.packed_args, it reads the dtype of every packedscales/biasestensor from the safetensors headers and stores it in the newConfig::quant_scale_dtype.take_weightandtake_lm_headdeclare the args with that type, and theQuantRawupload uses the same field for both the expected dtype and the C dtype code (new code 3,IREE_HAL_ELEMENT_TYPE_BFLOAT_16inxla_iree.c). A unit test checks the Rust codes against the Cswitch.dequant_affinewidens bf16 to f32 in the graph like f16, and it now asserts that the scales and biases are a matching f16 or bf16 pair.MLXCEL_XLA_QUANT=nativeuses the same buffers, butqmv4.hipandqmv4.cureadhalf. 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)expected F16up_projrewritten to F16The #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 theireefeature, linked by hand with the ROCm IREE arguments), andregistered_fixtures_are_byte_exactpasses unchanged. On main, the new shim-code test and the emitter bf16-declaration test fail.-D warningson mlxcel-xla lib and tests, with and without--features iree; fmt.Changes during review
checkpoint_candidate_shardsiniree.rs(used by the new scale-dtype probe, the native head probe and the weight loader) andresolve_weight_shardsinphi4_audio.rsjoined eachweight_mapvalue frommodel.safetensors.index.jsononto the model directory unchecked, so an absolute or..entry made the loader open a file outside the checkpoint. The newindex_shard_pathaccepts only a plain file name (the rule mlxcel-core'svalidate_index_shardsalready 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_dirfails on the previous code. Pre-existing, reached by the new probe.Pending: a CUDA run on GB10 (
MLXCEL_XLA_DEVICE=cuda, qwen3-0.6b-4bit packed vs dequant-at-load).Closes #603