Qwen3-4B architecture support — GGUF loads but inference produces garbage
Description
Qwen3-4B (Q4_K_M GGUF from official Qwen/Qwen3-4B-GGUF) loads successfully but inference produces garbage output. This blocks Qwen3-4B from being a viable upgrade over Phi-3.5-mini for users who need better quality.
Context
Qwen3-4B (MMLU 73.0, GSM8K ~85) is the highest-quality sub-4B model available. With vocab 152K it would run at ~1.0 tok/s on M3 — slower than Phi-3.5 (3.0 tok/s, vocab 32K) but potentially worth it for quality-sensitive use cases.
Architecture differences (Qwen3 vs Qwen2)
From the Qwen3 technical report and HuggingFace docs:
| Feature | Qwen2/2.5 | Qwen3 |
|---|---|---|
| QK-norm | No | Yes (required) |
| Attention bias | Yes | No |
| Vocab size | 151,936 | 151,669 |
| GQA (4B) | 16 heads / 2 KV | 32 heads / 8 KV |
| Architecture string | qwen2 |
qwen3 |
| Chat template | ChatML | ChatML + thinking mode |
Key change: QK-norm is now applied to every layer (not just hybrid/DeltaNet layers). quant.cpp already has QK-norm support (use_qk_norm flag + q_norm/k_norm weight loading) from the Qwen3.5 DeltaNet work, but the detection and loading path needs verification for the "qwen3" arch string.
Suspected issues
- Architecture detection:
gguf->arch = "qwen3"— does the existing loader handle this? Current code only checks for"gemma"explicitly; everything else falls tomodel_type = 0. Should be OK but needs verification. - QK-norm loading: Qwen3 GGUF tensors may use
blk.N.attn_q_norm.weight/blk.N.attn_k_norm.weight. Need to verify these are detected and loaded. - Attention bias removal: If quant.cpp's forward path assumes bias for Qwen-family models, it would produce wrong results.
- GQA head ratio: 32 Q / 8 KV = 4:1 GQA. quant.cpp supports GQA but the server path had issues (#61, now closed).
- Thinking mode tokens: Qwen3 may emit
<think>/</think>tokens that need to be handled in the chat template / stop detection.
Model file
Source: https://huggingface.co/Qwen/Qwen3-4B-GGUF
File: Qwen3-4B-Q4_K_M.gguf (2.5 GB)
Environment
- quant.cpp: v0.13.0+ (main branch)
- Hardware: Apple M3, 16GB
- Build: cmake -DTQ_BUILD_SERVER=ON
Steps to reproduce
# Download
curl -L -o ~/.cache/quantcpp/Qwen3-4B-Q4_K_M.gguf \
https://huggingface.co/Qwen/Qwen3-4B-GGUF/resolve/main/Qwen3-4B-Q4_K_M.gguf
# Test with single-header (quant.h)
cc -O2 -o /tmp/qwen3_test tools/phi3_infer_test.c -lm -lpthread
/tmp/qwen3_test ~/.cache/quantcpp/Qwen3-4B-Q4_K_M.gguf "What is gravity?"
Action plan
- Download + inspect GGUF tensor layout with
tools/gguf_inspect.c - Verify QK-norm tensor detection
- Check architecture-specific config (GQA heads, vocab, rope)
- Test inference, debug garbage output
- Add to model registry if working
Resolution
Qwen3-4B architecture support has been verified end-to-end. Three bugs were identified and fixed:
Bug 1: Gemma hybrid head_dim detection (commit 8a7825b)
The unconditional blk.0.attn_k.weight shape heuristic for Gemma hybrid attention was overriding Qwen3's correct head_dim=128 to 64. Fixed by gating with is_gemma_arch.
Bug 2: NeoX RoPE for non-standard head dimensions (commit 8a7825b)
When n_heads * head_dim != hidden_dim (Qwen3: 32×128=4096 ≠ 2560), the GGUF converter's GQA K-weight permutation creates cross-head interleaving instead of per-head interleaving. Standard interleaved RoPE produces wrong rotations. Fixed by detecting the dimension mismatch and using NeoX-style rotation (q[i], q[i+half]).
Bug 3: Special token encoding (earlier commit)
ChatML tokens like <|im_start|> were BPE-split into 6+ fragments instead of mapping to single IDs (151644). Fixed by adding a special-token pre-pass in tq_encode.
Verification
$ TQ_NO_Q4=1 /tmp/qwen3_nobos Qwen3-4B-Q4_K_M-bartowski.gguf
tokens (15): 151644 872 198 3838 374 279 6722 315 9625 30 ...
<think>
Okay, the user is asking for the capital of France. I need to make sure
I provide the correct answer. Let me recall... France is a country in
Europe, and I know that Paris is its capital. But wait, I should verify
that. Sometimes people might confuse other cities, like Lyon or Marseille,
but those are major cities, not the capital.
I remember that the capital
Coherent thinking-mode output with correct reasoning about Paris.
Remaining work
- Add Qwen3 to model registry + CLI aliases
- Handle
<think>/</think>tokens in stop detection - Verify with Q4 conversion path (not just GGUF on-the-fly)
- Port NeoX RoPE to libturboquant split sources