Skip to content

fix(models): infer per-tensor bits for quantized embeddings (#291) - #292

Merged
inureyes merged 1 commit into
mainfrom
291-quantized-embedding-per-tensor-bits
Jun 15, 2026
Merged

inureyes merged 1 commit into
mainfrom
291-quantized-embedding-per-tensor-bits

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

Fixes #291. QuantizedEmbedding::from_weights_with_mode stored the caller-supplied bits verbatim, unlike UnifiedLinear, the fused-QKV path, and SwitchLinear, which all reconcile bits against the tensor shapes via infer_quantization_bits. Mixed-precision exports that quantize the embedding at a different width than the top-level config.quantization therefore dequantized the embedding lookup, the tied-embedding lm_head, and dequantized_weight() with the wrong bits and aborted.

Repro (before)

mlx-community/diffusiongemma-26B-A4B-it-4bit (current upstream export) is mixed-precision: embed_tokens / attention / MLP at 8-bit under a 4-bit top-level default.

[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. The (14,704) is the gathered embedding rows for the 14 prompt tokens.

Fix

Infer per-tensor bits in QuantizedEmbedding::from_weights_with_mode for affine mode, mirroring UnifiedLinear. One change covers the embedding lookup, the tied lm_head (as_linear), and dequantized_weight() (the diffusion soft-embedding table). Uniform-quant checkpoints are unaffected (inference returns the caller bits unchanged).

Validation

  • Unit test added (infer_bits_detects_8bit_embedding_under_4bit_config, real diffusiongemma shapes → 8 bits); cargo test -p mlxcel-core infer_bits green.
  • End to end on diffusiongemma-26B-A4B-it-4bit: now loads; diffusion generation correct ("The capital of France is Paris."); AR-equivalent decode 68.50 tok/s on M1 Ultra.
  • cargo fmt --check and cargo clippy --release --all-targets -- -D warnings clean.

Notes

Separate from #289 (that was a bf16 dtype issue; this is a bits/shape issue). Same class as the dots.llm1 mixed-bit MoE handling, now extended to embeddings.

QuantizedEmbedding::from_weights_with_mode stored the caller-supplied
bits verbatim, unlike UnifiedLinear, the fused-QKV path, and SwitchLinear,
which all reconcile bits against the tensor shapes via
infer_quantization_bits. Mixed-precision exports that quantize the
embedding at a different width than the top-level config (e.g.
diffusiongemma / gemma4: embed_tokens, attention, and MLP at 8-bit under
a 4-bit top-level default) therefore dequantized the embedding lookup,
the tied-embedding lm_head, and dequantized_weight() with the wrong bits
and aborted:

  [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 [262144,704] / scales [262144,44] implies 8 bits).

Infer per-tensor bits in QuantizedEmbedding::from_weights_with_mode for
affine mode, mirroring UnifiedLinear. Uniform-quant checkpoints are
unaffected (inference returns the caller bits unchanged). Validated end
to end on mlx-community/diffusiongemma-26B-A4B-it-4bit: it now loads, its
diffusion generation is correct ("The capital of France is Paris."), and
AR-equivalent decode is 68.50 tok/s on M1 Ultra. Adds a unit test for the
8-bit-embedding-under-4-bit-config shape.
@inureyes inureyes added type:bug Bug fixes, error corrections, or issue resolutions area:models Model architectures, weights, loading, metadata priority:medium Medium priority platform:macos macOS (Apple Silicon) specific labels Jun 15, 2026
@inureyes
inureyes merged commit a24c9c0 into main Jun 15, 2026
4 checks passed
@inureyes
inureyes deleted the 291-quantized-embedding-per-tensor-bits branch June 15, 2026 05:51
@inureyes inureyes added this to the 0.3 milestone Jun 21, 2026
@inureyes inureyes self-assigned this Aug 31, 2026
@inureyes inureyes added the status:done Completed label Sep 6, 2026
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
…) (lablup#292)

QuantizedEmbedding::from_weights_with_mode stored the caller-supplied
bits verbatim, unlike UnifiedLinear, the fused-QKV path, and SwitchLinear,
which all reconcile bits against the tensor shapes via
infer_quantization_bits. Mixed-precision exports that quantize the
embedding at a different width than the top-level config (e.g.
diffusiongemma / gemma4: embed_tokens, attention, and MLP at 8-bit under
a 4-bit top-level default) therefore dequantized the embedding lookup,
the tied-embedding lm_head, and dequantized_weight() with the wrong bits
and aborted:

  [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 [262144,704] / scales [262144,44] implies 8 bits).

Infer per-tensor bits in QuantizedEmbedding::from_weights_with_mode for
affine mode, mirroring UnifiedLinear. Uniform-quant checkpoints are
unaffected (inference returns the caller bits unchanged). Validated end
to end on mlx-community/diffusiongemma-26B-A4B-it-4bit: it now loads, its
diffusion generation is correct ("The capital of France is Paris."), and
AR-equivalent decode is 68.50 tok/s on M1 Ultra. Adds a unit test for the
8-bit-embedding-under-4-bit-config shape.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:models Model architectures, weights, loading, metadata platform:macos macOS (Apple Silicon) specific priority:medium Medium priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant