Repository navigation
Description
Summary
QuantizedEmbedding::from_weights_with_mode (src/lib/mlxcel-core/src/layers.rs) stores the caller-supplied bits verbatim and never reconciles it against the actual tensor shapes. For mixed-precision checkpoints where the embedding is quantized at a different bit width than the model's top-level config.quantization.bits, the embedding lookup, the tied lm_head projection, and any dequantized_weight() call dequantize with the wrong bits and abort.
Repro
mlx-community/diffusiongemma-26B-A4B-it-4bit (current upstream export) is mixed-precision: config.quantization has top-level {group_size: 64, bits: 4} but per-layer entries put embed_tokens / attention / mlp at bits: 8. Loading it:
mlxcel-bench-decode -m models/diffusiongemma-26b-a4b-it-4bit -p "Hello" -n 8
-> libc++abi: terminating ... [dequantize] Shape of scales and biases does not
match the matrix ... matrix (14,704), scales/biases (14,44), group_size=64, bits=4
embed_tokens.weight is [262144, 704] with scales [262144, 44]: 704*32/(44*64) = 8 bits, not 4. The (14,704) in the error is the gathered embedding rows for the 14 prompt tokens.
Root cause
The other quantized paths already infer per-tensor bits via infer_quantization_bits(): UnifiedLinear::from_weights_with_mode (layers.rs ~723), the fused-QKV path (~986), and SwitchLinear (mixed-bit MoE, e.g. dots.llm1). Only QuantizedEmbedding::from_weights_with_mode was never updated, so quantized embeddings still trust the top-level bits.
Fix
In QuantizedEmbedding::from_weights_with_mode, for mode == "affine", call infer_quantization_bits(&weight_shape, &scales_shape, group_size, bits) and store the inferred bits (mirroring UnifiedLinear). This fixes the embedding lookup, the tied-embedding lm_head (as_linear), and dequantized_weight() (used by the diffusion soft-embedding table) in one place. Uniform-quant checkpoints are unaffected (inference returns the caller bits unchanged).
Acceptance Criteria
-
QuantizedEmbedding::from_weights_with_modeinfers per-tensor bits formode == "affine"viainfer_quantization_bits, mirroringUnifiedLinear. - Embedding lookup, tied-embedding
as_linearlm_head, anddequantized_weight()all use the inferred bits. - Uniform-quant checkpoints remain unaffected (inference returns the caller bits unchanged).
- Unit test added mirroring the existing
infer_quantization_bitstests (8-bit embed under a 4-bit top-level). - End-to-end validation that
mlx-community/diffusiongemma-26B-A4B-it-4bitloads and decodes correctly.
Context
Found during the 2026-06-15 M1 Ultra benchmark refresh; diffusiongemma's prior uniform-4bit export decoded at 70.66 tok/s, the re-export changed to mixed precision. Separate from the #289 bf16-scale fix (that was a dtype issue; this is a bits/shape issue).
Activity
fix(models): infer per-tensor bits for quantized embeddings (#291) (#292
docs(benchmarks): refresh M1 Ultra + M5 Max for mlxcel 0.2.1 (#295)
fix(models): infer per-tensor bits for quantized embeddings (lablup#291…
docs(benchmarks): refresh M1 Ultra + M5 Max for mlxcel 0.2.1 (lablup#295