{"slug": "qwen4-35b-a3b-gguf-on-your-own-device-a-20-kib-per-token-cache-a-20b-n-gram-and", "title": "Qwen4-35B-A3B GGUF on Your Own Device: A 20 KiB-per-Token Cache, a 20B N-gram Table, and Which Build Fits 16, 24 and 48 GB", "summary": "A developer parsed the GGUF header and config of Qwen4-35B-A3B, Qwen's Apache 2.0 35B-parameter mixture-of-experts model with 3B active parameters and a native 1,048,576-token context, to estimate on-device memory costs. Because only 10 of 40 layers use full attention, the KV cache comes to about 20 KiB per token in BF16 (20 GiB at 1M tokens), versus 80 KiB if all layers were standard GQA, while the 30 Gated DeltaNet layers hold a fixed 60 MiB recurrent state that does not grow with context. The author cautions that the fit tables for 16, 24 and 48 GB devices are plans rather than measurements.", "body_md": "Qwen released **Qwen4-35B-A3B** on December 3, 2026 under Apache 2.0: 35B parameters, about 3B active per token, 256 experts, 40 layers, and a native 1,048,576-token window. Community GGUF builds appeared within hours. We read the model config and parsed a real GGUF header (the Q4_K_M file of Chungulus/Qwen4-35B-A3B-GGUF) to answer the on-device question: what does it cost in memory?\n\nAll sizes below come from the Hugging Face file listings and the parsed header. Fit tables are plans, not measurements: run your own prompt before you rely on them.\n\nSources: [Qwen/Qwen4-35B-A3B](https://huggingface.co/Qwen/Qwen4-35B-A3B), [Chungulus/Qwen4-35B-A3B-GGUF](https://huggingface.co/Chungulus/Qwen4-35B-A3B-GGUF), [tomngdev/Qwen4-35B-A3B-GGUF](https://huggingface.co/tomngdev/Qwen4-35B-A3B-GGUF).\n\nThe model card describes a hybrid layout: ten repeats of three Gated DeltaNet layers (linear attention) followed by one Qwen Sparse Attention layer, with a mixture-of-experts feed-forward after each. From `config.json`:\n\n| Field | Value | \n|---|---|\n| Layers | 40 (30 linear attention, 10 full attention) | \n| Hidden size | 2048 | \n| Experts | 256 routed, 8 active per token, plus 1 shared, intermediate size 512 | \n| Attention heads | 16 query, 2 KV, head dimension 256 | \n| Linear attention heads | 32 value, 16 query/key, head dimension 128 | \n| Sparse attention budget | 2048 tokens per query (indexer with 4 heads, 1 shared key head, dimension 128) | \n| Vocabulary | 248,320 | \n| Context | 1,048,576 native | \n| Extras | multi-token prediction layer, vision encoder (27 layers), 4-branch gated residual | \n| License | Apache 2.0 | \n\nThe card also lists a \"20B n-gram embedding\" on top of the 35B, and says the architecture \"improves model quality without extra compute or accelerator memory overhead\". That sentence is the key to the file sizes below: the n-gram table is a lookup, not a layer that every token multiplies through.\n\nOnly layers with `full_attention` keep a per-token key and value cache. We counted them in the config: 10 of 40, with 2 KV heads of dimension 256.\n\n```\nfull_layers, kv_heads, head_dim = 10, 2, 256\nbf16 = 2  # bytes\nper_token = full_layers * 2 * kv_heads * head_dim * bf16   # K and V\nprint(per_token, per_token / 1024)      # 20480 bytes -> 20.0 KiB\n\nfor ctx in (32_768, 131_072, 262_144, 1_048_576):\n    print(ctx, round(per_token * ctx / 2**30, 3), \"GiB\")\n# 32K 0.625, 128K 2.5, 256K 5.0, 1M 20.0\n```\n\nIf all 40 layers were ordinary GQA layers with the same shape, the figure would be 80 KiB per token, four times larger. This is a counterfactual we computed to show the effect of the hybrid design, not a model that exists.\n\nTwo caveats. First, the sparse attention indexer keeps its own key (1 head, dimension 128). If a runtime caches it in BF16 that could add up to 2.5 KiB per token across the 10 layers; we did not measure what each runtime allocates. Second, the 2048-token budget limits how much the model reads per step, not how much you store. The cache still grows with the prompt.\n\nWith an 8-bit KV cache type the per-token figure drops to about 10.6 KiB (Q8_0 stores 34 bytes per 32 values, computed). Whether your build supports a quantized cache for this architecture is something to test, not assume.\n\nGated DeltaNet layers carry a fixed-size recurrent state instead of a growing cache. From the config, the state per layer is value heads times key dimension times value dimension: 32 x 128 x 128 = 524,288 values. In FP32 (`mamba_ssm_dtype` is `float32`) that is 2 MiB per layer, **60 MiB for all 30 layers**. A short convolution state (kernel size 4) adds a small amount on top. The point for edge deployment: this part does not grow when you go from 8K to 800K tokens.\n\nWe downloaded the first 24 MiB of the Q4_K_M file (the GGUF header with all tensor descriptors) and summed tensor sizes by role. The header reports architecture `qwen4exp`, 40 blocks, 256 experts, 8 used, and 882 tensors.\n\n| Group | Size in the Q4_K_M file | \n|---|---|\n| Routed expert tensors (gate, up, down) | 18.16 GiB | \n| Everything else (attention, linear attention, shared expert, routers, output, embeddings, gated residual) | 1.79 GiB | \n| `per_layer_token_embd.weight` , shape 128 x 160,003,584, Q5_0 | 13.11 GiB | \n| **File total** | **33.07 GiB** | \n\nThe n-gram table holds 128 x 160,003,584 = 20.48 billion values, which matches the \"20B n-gram embedding\" in the card. One row is 128 values, 88 bytes in Q5_0. Each token needs a few such rows, not the whole table. That is why other builders ship it as a separate file (`engram-*.gguf` or `ple-*.gguf`) and tell you to add it with `--shard`.\n\nThe consequence for planning: the number to compare against your VRAM is the **core**, not the file total. Whether your runtime keeps the table in system memory or maps it from disk depends on the build and flags, so read the load log.\n\nFrom the file listing of Chungulus/Qwen4-35B-A3B-GGUF (shards summed, GiB):\n\n| Build | Core | Embedding file | Total | \n|---|---|---|---|\n| Q2_K | 12.25 | 10.73 (Q4_0) | 22.98 | \n| Q3_K_S | 14.31 | 10.73 (Q4_0) | 25.04 | \n| Q3_K_M | 15.83 | 10.73 (Q4_0) | 26.56 | \n| Q3_K_L | 17.12 | 10.73 (Q4_0) | 27.85 | \n| IQ4_XS | 17.88 | 10.73 (IQ4_NL) | 28.61 | \n| IQ4_NL | 18.83 | 10.73 (IQ4_NL) | 29.56 | \n| Q4_K_S | 18.74 | 13.11 (Q5_0) | 31.85 | \n| Q4_K_M | 19.96 (computed) | 13.11 bundled | 33.08 | \n| Q5_K_S | 22.59 | 14.31 (Q5_1) | 36.90 | \n| Q5_K_M | 23.31 | 14.31 (Q5_1) | 37.62 | \n| Q6_K | 26.86 | 20.27 (Q8_0) | 47.13 | \n| Q8_0 | 34.75 | 20.27 (Q8_0) | 55.02 | \n\nA separate release by tomngdev uses `llama-quantize --target-bpw` and offers Q4_K_XL (23.15 GiB core, target 5.5366 bits per weight) and Q6_K_XL (32.13 GiB, 7.6860), with the embedding as `engram-Q8_0.gguf` (20.27 GiB) or `engram-IQ4_NL.gguf` (10.73 GiB). Its card includes a multi-token-prediction head quantized to Q8_0 and a llama.cpp command using `--spec-type draft-mtp`. The Chungulus bundle also lists an F16 vision projector of 0.84 GiB.\n\nThe embedding precision is a separate choice from the core precision. Q4_K_S pairs an 18.74 GiB core with a Q5_0 table, while IQ4_NL pairs an 18.83 GiB core with an IQ4_NL table that is 2.4 GiB smaller. We have no quality numbers for these combinations, so we do not rank them.\n\nPlan = core + cache at BF16 + 60 MiB recurrent state + about 1.5 GiB for compute buffers (our assumption; it varies with batch size and runtime).\n\n| GPU | Build (core) | Cache bound | Plan total | \n|---|---|---|---|\n| 16 GB | Q2_K (12.25 GiB) | 0.63 GiB at 32K | about 14.4 GiB | \n| 24 GB | IQ4_XS (17.88 GiB) | 2.5 GiB at 128K | about 21.9 GiB | \n| 24 GB | Q4_K_M (19.96 GiB) | 0.63 GiB at 32K | about 22.1 GiB | \n| 32 GB | Q6_K (26.86 GiB) | 2.5 GiB at 128K | about 30.9 GiB | \n| 48 GB | Q8_0 (34.75 GiB) | 5.0 GiB at 256K | about 41.3 GiB | \n\nThe n-gram table is not in these totals. Budget 10.7 to 20.3 GiB of system RAM or fast SSD for it (13.11 GiB for Q4_K_M), and 0.84 GiB more for the vision projector if you want images. The MoE structure also makes partial expert offload to system RAM an option for smaller cards, but we have not measured its speed and we give no number.\n\nQuality is the open column. The official card links a benchmark image, and we have not rerun any of it on the quantized files, so we make no claim about how much each build keeps. Test on your own task.\n\nThe Chungulus card points to a pinned llama.cpp commit for Qwen4 support (59b7165). Build that, start from the first shard, and add the embedding file with `--shard`:\n\n```\nhf download Chungulus/Qwen4-35B-A3B-GGUF \\\n  --include \"Qwen4-35B-A3B-IQ4_XS-*.gguf\" \"ple-Qwen4-35B-A3B-IQ4_NL.gguf\" \\\n  --local-dir .\n\nllama-server -m Qwen4-35B-A3B-IQ4_XS-00001-of-00003.gguf \\\n  --shard ple-Qwen4-35B-A3B-IQ4_NL.gguf \\\n  -ngl 99 -c 131072\n```\n\nAdd `--mmproj mmproj-Qwen4-35B-A3B-F16.gguf` for images. Official sampling advice from the card: thinking mode `temperature=1.0, top_p=0.95, top_k=20`; non-thinking `temperature=0.7, top_p=0.80, top_k=20, presence_penalty=1.5`. Thinking is controlled with `reasoning_effort` (`xhigh`, `high`, `low`, `off`).\n\nA quick audit you can repeat: fetch the first 24 MiB of your file, list the tensors, and sum the bytes of `per_layer_token_embd.weight` versus the rest. It takes a minute and tells you what you are about to place in memory.\n\n**Is Qwen4-35B-A3B open?** The weights are on Hugging Face under Apache 2.0, in Transformers format, and the card names vLLM, SGLang, llama.cpp, ollama, MLX and LiteRT-LM as supported.\n\n**Why does a 35B model need only about 20 GiB of core weights at 4 bits?** The card's 35B figure covers the transformer and experts, while the separate 20B n-gram embedding is a lookup table. In the Q4_K_M file, experts take 18.16 GiB and everything else 1.79 GiB.\n\n**How much memory does a 1M-token prompt need?** Cache: 20 GiB at BF16 by our computation (plus possible indexer keys), plus 60 MiB of recurrent state. That is on top of the weights, so it is a workstation figure, not a phone figure.\n\n**Does the cache really stop growing in the linear layers?** Yes: their state is fixed at 60 MiB in FP32 for all 30 layers, independent of prompt length.\n\n**Which quant should I start with?** For 24 GB, IQ4_XS with a 128K context is the plan with the most headroom in our table. For 16 GB, Q2_K at 32K. Run your own evaluation before you ship either.", "url": "https://wpnews.pro/news/qwen4-35b-a3b-gguf-on-your-own-device-a-20-kib-per-token-cache-a-20b-n-gram-and", "canonical_source": "https://dev.to/ginigen_ai_010d9180cbbdb1/qwen4-35b-a3b-gguf-on-your-own-device-a-20-kib-per-token-cache-a-20b-n-gram-table-and-which-3g37", "published_at": "2026-12-04 09:07:38+00:00", "updated_at": "2026-12-04 09:15:05.851814+00:00", "lang": "en", "topics": ["large-language-models", "ai-infrastructure", "machine-learning", "ai-tools"], "entities": ["Qwen", "Qwen4-35B-A3B", "Chungulus/Qwen4-35B-A3B-GGUF", "tomngdev/Qwen4-35B-A3B-GGUF", "Hugging Face"], "also_reported_by": [], "takeaway": "Qwen4-35B-A3B's hybrid layout keeps KV cache at about 20 KiB per token in BF16 (20 GiB at 1M tokens) because only 10 of 40 layers are full attention, while its 30 Gated DeltaNet layers add a fixed 60 MiB state that does not grow with context.", "recommendation": {"advice": "Size KV cache from full-attention layers only (10 of 40 here) and treat the Gated DeltaNet state as a fixed 60 MiB cost, then test your runtime's quantized-cache and indexer allocation before trusting fit tables.", "playbook": "inference-serving", "playbook_url": "https://wpnews.pro/playbooks/inference-serving"}, "alternates": {"html": "https://wpnews.pro/news/qwen4-35b-a3b-gguf-on-your-own-device-a-20-kib-per-token-cache-a-20b-n-gram-and", "markdown": "https://wpnews.pro/news/qwen4-35b-a3b-gguf-on-your-own-device-a-20-kib-per-token-cache-a-20b-n-gram-and.md", "text": "https://wpnews.pro/news/qwen4-35b-a3b-gguf-on-your-own-device-a-20-kib-per-token-cache-a-20b-n-gram-and.txt", "jsonld": "https://wpnews.pro/news/qwen4-35b-a3b-gguf-on-your-own-device-a-20-kib-per-token-cache-a-20b-n-gram-and.jsonld"}}