Skip to content

Quantize GGUF output head (lm_head) → MatMulNBits - #401

Merged
justinchuby merged 1 commit into
mainfrom
feat/gguf-quantized-head
Jul 13, 2026
Merged

justinchuby merged 1 commit into
mainfrom
feat/gguf-quantized-head

Conversation

@justinchuby

Copy link
Copy Markdown
Member

Follow-up to #400 (quantized embedding). Profiling showed the CPU model shipped an untied lm_head as a plain fp32 MatMul (~544 MB) run every token. This emits the output head as Q4 MatMulNBits (untied) / shares the packed embedding table (tied).

  • Result: 169 MatMulNBits, 1 GatherBlockQuantized, 0 plain fp32 MatMul; no 544 MB fp32 tables. Model ~1.2 GB → ~399 MB.
  • Coherent output verified ('Paris').
  • Note: a quick 6-thread decode check showed ~38 vs ~40 tok/s (no clear speedup) — a rigorous benchmark is pending; the win is model size + correct all-quantized graph shape (matches llama.cpp). Stacks on Quantized token embedding via GatherBlockQuantized (272MB→76MB) #400's embedding commit.

lintrunner clean; gguf pytest 157 passed.

@github-actions

github-actions Bot commented Jul 13, 2026 •

Copy link
Copy Markdown

Performance Comparison

Comparing 400808d → c0fd6d3

Model Metric Baseline Current Delta
bert (feature-extraction) model_size_bytes 359 KB 359 KB +0.0% ⚪
bert (feature-extraction) num_nodes 60 60 +0.0% ⚪
falcon model_size_bytes 364 KB 364 KB +0.0% ⚪
falcon num_nodes 68 68 +0.0% ⚪
gemma2 model_size_bytes 428 KB 428 KB +0.0% ⚪
gemma2 num_nodes 107 107 +0.0% ⚪
gpt2 model_size_bytes 388 KB 388 KB +0.0% ⚪
gpt2 num_nodes 54 54 +0.0% ⚪
llama model_size_bytes 425 KB 425 KB +0.0% ⚪
llama num_nodes 62 62 +0.0% ⚪
llama (static-cache) model_size_bytes 425 KB 425 KB +0.0% ⚪
llama (static-cache) num_nodes 58 58 +0.0% ⚪
mamba (ssm-text-generation) model_size_bytes 296 KB 296 KB +0.0% ⚪
mamba (ssm-text-generation) num_nodes 98 98 +0.0% ⚪
phi3 model_size_bytes 421 KB 421 KB +0.0% ⚪
phi3 num_nodes 60 60 +0.0% ⚪
phi3 (static-cache) model_size_bytes 421 KB 421 KB +0.0% ⚪
phi3 (static-cache) num_nodes 56 56 +0.0% ⚪
qwen2 model_size_bytes 425 KB 425 KB +0.0% ⚪
qwen2 num_nodes 62 62 +0.0% ⚪
qwen2 (static-cache) model_size_bytes 425 KB 425 KB +0.0% ⚪
qwen2 (static-cache) num_nodes 58 58 +0.0% ⚪
qwen3_5_moe (hybrid-text-generation) model_size_bytes 506 KB 506 KB +0.0% ⚪
qwen3_5_moe (hybrid-text-generation) num_nodes 275 275 +0.0% ⚪
qwen3_5_text (hybrid-text-generation) model_size_bytes 458 KB 458 KB +0.0% ⚪
qwen3_5_text (hybrid-text-generation) num_nodes 129 129 +0.0% ⚪
qwen3_5_vl (hybrid-qwen-vl) model_size_bytes 977 KB 977 KB +0.0% ⚪
qwen3_5_vl (hybrid-qwen-vl) num_nodes 413 413 +0.0% ⚪
t5 (seq2seq) model_size_bytes 836 KB 836 KB +0.0% ⚪
t5 (seq2seq) num_nodes 166 166 +0.0% ⚪
whisper (speech-to-text) model_size_bytes 1008 KB 1008 KB +0.0% ⚪
whisper (speech-to-text) num_nodes 128 128 +0.0% ⚪

No performance regressions.

@codecov

codecov Bot commented Jul 13, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.66667% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/mobius/integrations/gguf/_builder.py 78.57% 2 Missing and 1 partial ⚠️
src/mobius/components/_quantized_linear.py 83.33% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Jul 13, 2026 •

Copy link
Copy Markdown

🏗️ Architecture Diff

Comparing 400808d → c0fd6d3

Model Sub-model Changes Status
bert (feature-extraction) model 0 ⚪
falcon model 0 ⚪
gemma2 model 0 ⚪
gemma4 (gemma4) decoder 0 ⚪
gemma4 (gemma4) embedding 0 ⚪
gemma4 (gemma4) vision_encoder 0 ⚪
gemma4_text model 0 ⚪
gpt2 model 0 ⚪
llama model 0 ⚪
llama (static-cache) model 0 ⚪
mamba (ssm-text-generation) model 0 ⚪
phi3 model 0 ⚪
phi3 (static-cache) model 0 ⚪
qwen model 0 ⚪
qwen (static-cache) model 0 ⚪
qwen2 model 0 ⚪
qwen2 (static-cache) model 0 ⚪
qwen2_moe model 0 ⚪
qwen2_moe (static-cache) model 0 ⚪
qwen3 model 0 ⚪
qwen3 (static-cache) model 0 ⚪
qwen3_5_moe (hybrid-text-generation) model 0 ⚪
qwen3_5_text (hybrid-text-generation) model 0 ⚪
qwen3_5_vl (hybrid-qwen-vl) decoder 0 ⚪
qwen3_5_vl (hybrid-qwen-vl) embedding 0 ⚪
qwen3_5_vl (hybrid-qwen-vl) vision_encoder 0 ⚪
qwen3_moe model 0 ⚪
qwen3_moe (static-cache) model 0 ⚪
qwen3_next (hybrid-text-generation) model 0 ⚪
t5 (seq2seq) decoder 0 ⚪
t5 (seq2seq) encoder 0 ⚪
whisper (speech-to-text) decoder 0 ⚪
whisper (speech-to-text) encoder 0 ⚪

No architecture changes detected. ✅


Legend: ⚪ No change · 🔵 Minor (attrs/inits) · 🟡 Moderate (nodes added/removed) · 🔴 Major (interface changed)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_head into MatMulNBits (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_points initializer (QuantizedLinear/QuantizedEmbedding are constructed with has_zero_point=False). But _load_quantized_state_dict() still adds *.zero_points whenever the repacker returns one (e.g. Q4_0/Q8_0), which will be ignored by apply_weights() (warns "Weight not found") and can be misleading. Gate emitting *.zero_points on not 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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.

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]>
@justinchuby
justinchuby force-pushed the feat/gguf-quantized-head branch from 3e23990 to c0fd6d3 Compare July 13, 2026 15:22
@justinchuby
justinchuby requested a review from Copilot July 13, 2026 15:30
@justinchuby
justinchuby merged commit 57177b7 into main Jul 13, 2026
19 of 22 checks passed
@justinchuby
justinchuby deleted the feat/gguf-quantized-head branch July 13, 2026 15:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Comment on lines +11 to +15
- **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.
Comment on lines +550 to 554
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.
Comment on lines +24 to 28
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants