Skip to content

fix(models): infer per-tensor bits for quantized embeddings (mixed-precision exports like diffusiongemma fail to load) #291

Closed

Description

@inureyes

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_mode infers per-tensor bits for mode == "affine" via infer_quantization_bits, mirroring UnifiedLinear.
  • Embedding lookup, tied-embedding as_linear lm_head, and dequantized_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_bits tests (8-bit embed under a 4-bit top-level).
  • End-to-end validation that mlx-community/diffusiongemma-26B-A4B-it-4bit loads 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

  1. added
    type:bug
    area:models
    priority:medium
    platform:macos
    on Jun 15, 2026
  2. added this to the 0.3 milestone on Jun 21, 2026
  3. self-assigned this
    on Jun 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:modelsplatform:macospriority:mediumstatus:donetype:bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions