Repository navigation
fix(models): infer per-tensor bits for quantized embeddings (#291) - #292
Merged
Merged
Conversation
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
added a commit
that referenced
this pull request
Jun 15, 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.
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
Fixes #291.
QuantizedEmbedding::from_weights_with_modestored the caller-suppliedbitsverbatim, unlikeUnifiedLinear, the fused-QKV path, andSwitchLinear, which all reconcile bits against the tensor shapes viainfer_quantization_bits. Mixed-precision exports that quantize the embedding at a different width than the top-levelconfig.quantizationtherefore dequantized the embedding lookup, the tied-embedding lm_head, anddequantized_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.embed_tokens.weightis[262144,704]with scales[262144,44]→704*32/(44*64) = 8bits. The(14,704)is the gathered embedding rows for the 14 prompt tokens.Fix
Infer per-tensor bits in
QuantizedEmbedding::from_weights_with_modeforaffinemode, mirroringUnifiedLinear. One change covers the embedding lookup, the tied lm_head (as_linear), anddequantized_weight()(the diffusion soft-embedding table). Uniform-quant checkpoints are unaffected (inference returns the caller bits unchanged).Validation
infer_bits_detects_8bit_embedding_under_4bit_config, real diffusiongemma shapes → 8 bits);cargo test -p mlxcel-core infer_bitsgreen.cargo fmt --checkandcargo clippy --release --all-targets -- -D warningsclean.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.