Skip to content

qwen4exp : load a row-paired per_layer_token_embd for 256-block table types - #484

Open
danielhanchen wants to merge 3 commits into
base/upstream-a105b1924from
engram-tbl-rowpair
Open

danielhanchen wants to merge 3 commits into
base/upstream-a105b1924from
engram-tbl-rowpair

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

The qwen4exp n-gram table (per_layer_token_embd.weight, 128 wide, 160,003,584 rows) can today only be stored in a type whose block size divides 128: Q1_0, Q2_0, Q4_0, Q4_1, Q5_0, Q5_1, Q8_0, IQ4_NL, MXFP4, F16, BF16. Every K-quant and every 256-block IQ type is rejected when the GGUF is parsed, so between IQ4_NL (11.52 GB) and Q2_0 (5.76 GB) there is nothing.

This change lets the table be stored row-paired: a tensor of shape [256, n_rows/2], where stored row r holds table rows 2r and 2r+1. Any 256-block type then fits (Q2_K, Q3_K, Q4_K, Q5_K, Q6_K, IQ1_S, IQ1_M, IQ2_XXS, IQ2_XS, IQ2_S, IQ3_XXS, IQ3_S, IQ4_XS). The layout is detected from the tensor shape, so there is no new GGUF key.

What changes

One file, src/models/qwen4exp.cpp:

  • Loader: if the table tensor has ne[0] == 2*ple_head_dim, the table is created as [2*ple_head_dim, ceil(n_rows/2)] and the existing row-count check runs on the paired row count.
  • Graph: the table get_rows gathers stored row id/2; a second get_rows on the F32 result (viewed as [ple_head_dim, 2*n]) keeps half id%2. The extra index tensor is only created for a row-paired table.
  • Input: set_input fills the second index tensor and halves the row ids (also the ids passed to the row prefetch).

A table stored as [128, n_rows] builds exactly the same graph as before. No ggml kernel changes: the table is gathered on the host (lazy table), and the CPU, CUDA and Metal backends all support GET_ROWS for these types already; the half select is an F32 get_rows.

Files are produced by quantizing pairs of rows as one 256-wide row and writing the tensor as [256, n_rows/2]; a loader without this change refuses such a file at load time (per_layer_token_embd.weight has 80001792 rows, too few for the PLE head ranges), it never reads it wrongly.

Exactness

  • Header-only repack of the shipped IQ4_NL table to [256, n_rows/2] (same data bytes): per-token KLD dumps byte-identical to the unpaired table on all 12 evaluation sets, and wikitext-2 (580 x 512) dump byte-identical.
  • For every new type (Q4_K, Q3_K, IQ3_S, IQ3_XXS, Q2_K, IQ2_S, IQ2_XXS) a decode through the eval callback compares every gathered value bitwise with an offline dequantize of the stored row: 688 lookups x 256 values per type, 0 mismatches, and the logical row ids match the unpaired path exactly.
  • test-backend-ops -o GET_ROWS -b CUDA0: 239/239 pass, including every K and IQ type.
  • External table through --ple gives the same result as the table inside the split (KLD 0, top-1 100%).

Unchanged for existing files (b13004 base vs b13004 + this change, B200)

wikitext-2 -c 512 --chunks 40, IQ2_XXS quant:

table base PPL head PPL mean KLD head vs base same top-1
IQ4_NL (shipped) 6.0807 6.080670 0.000000 99.990%
Q2_0 6.1248 6.124826 0.000000 99.980%
IQ4_NL, row-paired header (refused) 6.080670 0.000000 99.990%

The top-1 figure is the floor of the stored base log-probs: base scored against its own stored logits also gives 99.990%.

Accuracy and size (35B-A3B, table swapped, everything else byte-identical)

Mean KLD over 11 evaluation sets vs the full-precision model, change relative to the same quant with the IQ4_NL table.

table size IQ1_S IQ2_XXS IQ2_M IQ3_XXS code / zh (IQ2_XXS)
IQ4_NL (current) 11.52 GB 0 0 0 0 0 / 0
IQ3_S (new) 8.80 GB +0.53% +0.92% +1.99% +3.18% +5.0% / +2.8%
Q3_K (new) 8.80 GB +0.66% +1.09% +2.45% +4.36% +4.7% / +3.0%
IQ3_XXS (new) 7.84 GB +1.23% +2.01% +11.0% / +5.0%
Q2_K (new) 6.72 GB +2.98% +4.96% +26.4% / +11.8%
IQ2_S (new) 6.56 GB +2.50% +4.15% +9.56% +15.47% +23.0% / +9.7%
Q2_0 (already loadable) 5.76 GB +5.70% +9.38% +20.6% +33.5% +51% / +22%
IQ2_XXS (new) 5.28 GB +5.07% +8.42% +48.3% / +18.5%
  • At the same size class every 256-block table beats Q2_0: IQ2_XXS is 0.48 GB smaller than Q2_0 and still slightly better (-0.9% / -0.6%); IQ2_S is -4.8% vs Q2_0 for +0.8 GB; IQ3_S is -7.7% vs Q2_0 and saves 2.72 GB vs IQ4_NL.
  • Q4_K at the same 11.52 GB as IQ4_NL: wikitext KLD -0.2% (IQ2_XXS) and -1.4% (Q4_K_XL quant), a small gain at equal size.
  • Higher quality quants are more sensitive to the table: on the Q4_K_XL quant wikitext KLD rises +12.0% with IQ3_S and +15.1% with Q3_K, so the IQ4_NL table (or larger) remains the right default there.

Speed

The table is memory-mapped and gathered on the host, so the cost is the CPU dequantize of 16 lookups per token. Single-thread gather cost per lookup (random rows, warm page cache): IQ4_NL 0.44 us, Q4_K 0.37 us, Q3_K 0.66 us, Q2_K 0.58 us, IQ3_S 1.44 us, IQ2_S 1.39 us, IQ2_XXS 1.35 us; that is about 22 us per decoded token for IQ3_S against a decode step of about 7 ms, and the prompt path splits it across threads. On a B200 with the table in page cache, tg128 was 146 to 149 t/s for every table in the same uncontended pass; cold page cache tg128 was 52 to 58 t/s for all tables (one page touched per lookup either way). Prompt throughput comparisons were not stable enough on the shared host to report a difference.

Pin model

The unslothai/llama.cpp prebuilt is an aged upstream ggml-org b#### release plus the pins in scripts/unsloth/pr-set.json, merged in order (git diff3, then pure add/add only). This pin is upstream b13004 (a105b1924) plus this one change, branched from base/upstream-a105b1924. It touches only src/models/qwen4exp.cpp; the other pin on that file (#240, MTP heads) merges with it cleanly. When upstream absorbs this, the pin is deleted.

Not covered

  • Metal and the Windows / ARM legs were not run; no backend kernel is touched and the table is gathered on the host.
  • llama-quantize does not write row-paired tables yet; this change is the loader side only.

… types

A table quantized with a 256-wide block type (Q2_K..Q6_K, IQ2_XXS..IQ3_S, IQ1_S/M, IQ4_XS)
cannot be stored as [128, n_rows]. Store it as [256, n_rows/2] instead, stored row r holding
rows 2r and 2r+1, and detect that from the tensor shape. The graph gathers stored row id/2 and
then keeps half id%2 with a second get_rows on the F32 result. Tables stored as [128, n_rows]
take the same path as before.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-05T15:19:54.717036Z e2dcc9d Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen danielhanchen self-assigned this Dec 5, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: e2dcc9dfa0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

This branch has not been deployed

No deployments
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.

1 participant