Repository navigation
Quantize GGUF output head (lm_head) → MatMulNBits - #401
Conversation
Performance Comparison
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
🏗️ Architecture Diff
No architecture changes detected. ✅ Legend: ⚪ No change · 🔵 Minor (attrs/inits) · 🟡 Moderate (nodes added/removed) · 🔴 Major (interface changed) |
There was a problem hiding this comment.
Pull request overview
This PR extends the GGUF import pipeline so quantized models avoid shipping an untied lm_head as a large float MatMul, instead emitting the output head as MatMulNBits (untied) or sharing the packed embedding table (tied). It also improves Q4_K handling by dequantizing/requantizing in a way that respects K-quant super-block layout across logical rows.
Changes:
- Quantize GGUF
lm_headintoMatMulNBits(or share the embedding table for tied models) to eliminate large float head weights. - Rework Q4_K repacking to reference-dequantize full super-block streams and requantize into MatMulNBits block-32 layout; add a generic float→MatMulNBits requantizer.
- Plumb GGUF raw dequantization as a first-class helper and add tests/CLI messaging for the expanded “keep quantized” behavior.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/mobius/models/base.py | Avoid float weight-tying helper when embedding/head are tied via quantized packed tables. |
| src/mobius/integrations/gguf/_repacker.py | Handle Q4_K flattened super-block layout and add repack_dequantized_tensor() for normalization. |
| src/mobius/integrations/gguf/_repacker_test.py | Add reference-dequant/round-trip tests for new Q4_K path and float requantization. |
| src/mobius/integrations/gguf/_reader.py | Add dequantize_raw_tensor() to ensure correct K-quant raw layout dequantization. |
| src/mobius/integrations/gguf/_builder.py | Detect embedding/head quantization and normalize mixed GGUF quant types into a common MatMulNBits target. |
| src/mobius/integrations/gguf/_builder_test.py | Add coverage for quantized embeddings, tied packed tables, and untied quantized heads. |
| src/mobius/components/_quantized_linear.py | Set GatherBlockQuantized output dtype/shape to improve IR typing and downstream shape propagation. |
| src/mobius/components/_quantized_linear_test.py | Assert dtype/shape propagation for quantized embedding forward. |
| src/mobius/_configs/_quantization.py | Document that quantize_embeddings/quantize_lm_head also apply to GGUF imports. |
| src/mobius/main.py | Update CLI messaging/help text for expanded quantized GGUF mode. |
Comments suppressed due to low confidence (1)
src/mobius/integrations/gguf/_builder.py:673
- For symmetric MatMulNBits/GatherBlockQuantized builds (
target_symmetric=True), the graph does not have a*.zero_pointsinitializer (QuantizedLinear/QuantizedEmbedding are constructed withhas_zero_point=False). But_load_quantized_state_dict()still adds*.zero_pointswhenever the repacker returns one (e.g. Q4_0/Q8_0), which will be ignored byapply_weights()(warns "Weight not found") and can be misleading. Gate emitting*.zero_pointsonnot target_symmetric(or on whether the module actually has a zero_points parameter).
state_dict[f"{stem}.scales"] = s
if repacked.zero_points is not None:
zp = torch.from_numpy(repacked.zero_points)
if _needs_qk_permute(hf_name, num_heads, num_kv_heads, model_type):
zp = _reverse_permute(zp, n_head)
state_dict[f"{stem}.zero_points"] = zp
c49e205 to
3e23990
Compare
Keep untied quantized output weights in MatMulNBits, requantizing mixed GGUF types to the graph's Q4 layout while preserving tied embedding/head sharing. Co-authored-by: Copilot <[email protected]>
3e23990 to
c0fd6d3
Compare
| - **Quantized** (``keep_quantized=True``): Linear-layer weights, including | ||
| a quantized output head, are repacked into MatMulNBits format and token | ||
| embeddings into GatherBlockQuantized format. Mixed presets such as | ||
| Q4_K_M are normalized to one quantization layout. Other tensors are | ||
| dequantized. |
| Projection weights (Q/K/V/O, MLP, and a quantized output head) are | ||
| converted to the graph's common MatMulNBits format, and token embeddings | ||
| to GatherBlockQuantized format. Mixed or unsupported source types are | ||
| dequantized and requantized when they do not match that target. Norms | ||
| and other non-linear tensors remain dequantized. |
| projection_quantization: str = "q4_0", | ||
| output_quantization: str | None = None, | ||
| tie_embeddings: bool = False, | ||
| ) -> None: | ||
| """Write a GGUF file with Q4_0 quantized projection weights. |
Follow-up to #400 (quantized embedding). Profiling showed the CPU model shipped an untied
lm_headas a plain fp32 MatMul (~544 MB) run every token. This emits the output head as Q4MatMulNBits(untied) / shares the packed embedding table (tied).lintrunner clean; gguf pytest 157 passed.