Skip to content

armgit / janas

Public Imported from GitHub
0

37 tok/s from Qwen3-Next-80B (48 GB) on a 32 GB laptop, 24 without drafts vs 2.8 for llama.cpp; reading 62 vs 16 tok/s. Faster than llama.cpp on all 13 models compared in Q4_K_M. LLMs bigger than your RAM, pure C, CPU or GPU.

Release 2026-10-23: Qwen3.8-Flash-Next, 177 billion parameters on a laptop with 32 GB - on the CPU alone 24.0 tok/s reading 1,024 tokens and 10.95 writing, where llama.cpp's CPU backend reads at 4.82 and writes at 3.12 and its Vulkan backend cannot run the file; 32.7 tok/s reading with the integrated GPU; its MTP block in the chat, a reply 23% faster greedy and 39% at the chat's temperature, the same text; the model converted on Hugging Face (prabanta-dev/Qwen3.8-Flash-Next-JNS, Qwen Community License 1.0), janas-get qwen3.8-flash-next; its four residual streams, n-gram embeddings and sparse attention on the CPU, the GPU and the NPU; JANAS_HC_MIX=q8 (11.9 tok/s writing), --head 4; Q2_K and the IQ1, IQ2 and IQ3 types on the GPU

camauri724479caf827
diff --git a/ChangeLog.md b/ChangeLog.md
index 12ddade..b3e17a4 100644
--- a/ChangeLog.md
+++ b/ChangeLog.md
@@ -2,6 +2,66 @@
> Curated, user-facing summary of completed work, newest first.
+## [2026-10-23] - Qwen3.8-Flash-Next's mixes of the streams faster on the CPU, at eight bits faster than BF16
+
+- **A reply 8.7% faster, a prompt 10% faster, the same bits**: on the CPU (Core Ultra 9 185H, 12 threads, a 12 GiB expert cache), 1,024 tokens of Italian prose and then 200 generated, two runs each alternated with the commit before, a reply went from 10.07 to 10.95 tokens/s and the prompt from 21.75 to 24.0; the mixes of the streams from about 30 ms a token to 19.7. Against llama.cpp, 400 tokens of C and 400 of Italian prose, every position gives the same digits as before.
+- **Products of four rows or four tokens with their sums in registers**: GCC did not unroll the short loops over rows and tokens, kept the accumulators in memory and made every fused multiply-add a load and a store; unrolled, the Q8_0 mixes' product read from L3 at 40 GB/s ran at a quarter of its speed before. Among them `janas_bf16_dot4`, the four tokens of a prompt's BF16 products.
+- **A single token's rows four at a time** (`janas_bf16_dot4r`, `janas_q8w_dot4r`), so that their chains of fused multiply-adds overlap, and rows shorter than 2 KB with their weights prefetched 2 KB ahead, where the next rows lie: a mix's up (10240 x 320) on 12 threads from 131 to 93 us in BF16, from 107 to 56 in Q8_0; its down (320 x 10240) at 82 GB/s.
+- **Rows claimed as a share of those left** in the BF16 and Q8_0 products (4 to 256, as `matvec.c` claims them), not in fixed chunks of 16: a down of 320 rows no longer ends on threads waiting for the last chunks.
+- **A mix's own work on the threads**: each stream of each token normalized by a thread of its own, a single token's mean split in pieces of 256, the rounding to bf16 eight at a time (`janas_round_bf16_n`): a single token's norm from 24.8 to 6.1 us, its mean from 8.6 to 5.2.
+- **`JANAS_HC_MIX=q8` now faster than BF16**: a reply at 11.9 tokens/s (BF16 10.95; 7.68 before these changes), a prompt at 24.0; the scale of each Q8_0 block by F16C, exact. Its quality is as measured: the most likely token at 392 and 364 of 400 positions against BF16's 390 and 367. The default stays BF16.
+- `tests/bench_long` and `tests/llm_eval` count the mixes in a phase of their own, `mix` (with the output's mix, which went into `output` before).
+- `test_quant` checks the four-row products against one row at a time and the rounding to bf16 against the scalar rule, ties, infinities and NaNs among the values.
+
+## [2026-10-23] - Qwen3.8-Flash-Next's prompt whole on the GPU, its mixes of the streams at eight bits
+
+- **`JANAS_HC_MIX=q8` in the whole token on the GPU**: the mixes' down and up rewritten as Q8_0 are multiplied there too (`dense_mm.comp`'s third kind, Q8_0 weights on float inputs rounded to bf16), so the mixes at eight bits no longer keep a prompt's blocks on the CPU. Blocks of 256 tokens against llama.cpp: on C code the most likely token agrees at 247 of 256 positions (the BF16 mixes on the GPU 247), on Italian prose at 238 (230), and the mean divergence from llama.cpp's probabilities is the CPU's with the same mixes, KL 0.0180.
+- **Qwen3.8-Flash-Next's sparse attention on the GPU too**: in each attention layer the indexer's keys and queries, the blocks the run completes, every token's scores of the blocks before it and its choice of the best 512 (a threshold found a byte at a time, the blocks in their order, those tied with it the earliest, as the CPU chooses) - `ix_prep`, `ix_pool`, `ix_score`, `ix_select` - and the attention over the positions chosen (`attn_decode.comp` and `attn_merge.comp` read them from a list; the dense positions are a list too). Past 2,051 positions a prompt's blocks no longer go back to the CPU. The blocks chosen are the CPU's, every one, at every token looked at. Against llama.cpp, the last 512 positions of 8,192 tokens - 512 of 2,048 blocks kept: the most likely token agrees at 481 (the CPU 482), mean logit difference 0.183 (0.191); of 2,200 tokens the 149 positions past the dense ones agree at all 149 (0.111; the CPU 0.117).
+- **Fixed: after a prompt's blocks on the GPU the CPU's indexer keys were zero** (since the prompt went whole to the GPU, earlier today): every attention layer wrote its input for the indexer into the first layer's row, so past 2,051 positions the CPU chose its blocks from keys of zero, decoding too; at 8,192 tokens the most likely token agreed at 381 of 512 positions instead of 481.
+- **Qwen3.8-Flash-Next's prompt blocks whole on the GPU** (the integrated GPU here, which shares the host's memory): its four streams, their mixes in BF16 (`hc_norm`, `hc_act`, `hc_mean`, `hc_comb`), the n-gram layer (`ng_gate`, `ng_conv`), the Q2_0 experts and the shared expert at their width of 640, a layer a submission with the CPU choosing the experts in between. A mixture whose cache holds fewer than half its experts, as here with 12 GiB, goes there with `JANAS_GPU_MOE=1`. Against llama.cpp, blocks of 256 tokens: on C code the most likely token agrees at 247 of 256 positions (the GPU's products beside the CPU, 248); on Italian prose at 230 (the same 235 and 238 in two runs, the CPU alone 235), and the mean divergence from llama.cpp's probabilities (KL) is the lowest of the three, 0.0172 against 0.0176 and 0.0186. Speed not measured yet.
+- **Q8_0 rows ending inside a group of 256 on the GPU** (`q8_0_matvec.comp`): the whole groups' sum and then the tail's, as the CPU adds them, bit for bit (`gpu_check` now checks them); Qwen3.8-Flash-Next's shared experts' down matrices are such rows.
+- **More than 256 experts at once from the cache** (`janas_expert_cache_fetch_many`): a block of 256 tokens of prose asked the first layer for 368 of its 512; requests of 256 that count as one, so that none takes a slot an earlier one gave. With `JANAS_GPU_VERBOSE` a block that goes back to the CPU says so.
+- `tests/llm_eval` writes the logits of the blocks too (`bdump=F`).
+
+- **`JANAS_HC_MIX=q8`**: the mixes of the four streams (each layer's two, and the output's), 1.26 GB of BF16 read at every token, rewritten as Q8_0 when the model opens - half the bytes - and multiplied by a product of their own, Q8_0 weights against float inputs rounded to bf16 as the BF16 products have them (`janas_q8w_dot`, eight AVX2 lanes, the same bits without AVX2; on the CPU). Against llama.cpp, 400 tokens of C and 400 of Italian prose: the most likely token agrees at 392 and 364 positions, mean logit difference 0.138 and 0.173, where in BF16 at 390 and 367 (0.135, 0.166). The way the other quantized products go - inputs quantized to eight bits - cost more than the weights' eight bits did: 388 and 359 (0.185, 0.204), whatever the weights' type (Q6_K gave the same), and four-bit weights (Q4_K, Q5_0) 382 and 349 even with float inputs; neither is kept. A prompt read in blocks gives the same bits as token by token, and the reply the same tokens with MTP drafts and without. The default stays BF16 until the speed is measured.
+- **`--head 4` on an output head at five bits** (Q5_K, Qwen3.8-Flash-Next's): requantized to Q4_K at load as a Q6_K head is, 437 MB a token to 358; against llama.cpp 387 and 351 of 400 (0.184, 0.234), so a lever of the fast kind, off by default.
+
+## [2026-10-22] - Qwen3.8-Flash-Next in janas-get, its long context checked, its memory counted
+
+- **`janas-get qwen3.8-flash-next`**: downloads ISTA-DASLab's two-bit GGUF (two parts, 66.4 GB) and converts it here, then unsloth's GGUF of the MTP block from unsloth's repository, which `hf2jns_mtp` copies; every file checked against the SHA-256 in `MODELS.md`. Its weights are under the Qwen Community License 1.0, not Apache 2.0, and no converted file of it is published. The catalog takes an MTP block from a GGUF of its own in another repository (`JANAS_EXTRA_BLOCK`, `extra_repo`). Checked on the files already here: the model's fingerprint, the block's GGUF and the block converted, all as `MODELS.md` says.
+- **`MODELS.md`**: Qwen3.8-Flash-Next's sources, revisions and fingerprints, and its licence in the paragraph on what this project redistributes; `docs/features.md`: what runs and what was checked.
+- **The memory estimate** (`janas_llm_estimate`, and `janas_llm_open`'s automatic cache) counts qwen4exp's buffers for a block's residual streams and n-grams, about 94 MB on Qwen3.8-Flash-Next. Measured with a cache of 4 GiB, a context of 8,192 and a prompt of 4,873 tokens: 7.79 GiB estimated, 8.02 GiB at the peak; what is left over (0.2 GiB, there from the open: the tokenizer, converted copies of some weights) is not counted for any model, and the open leaves a fifth of the memory to other programs.
+- **A long context checked**: 8,192 tokens, the last 512 positions against llama.cpp on the same file - the most likely token agrees at 482, mean logit difference 0.191; the same 512 tokens read after only 512 others: 482 again, 0.178. llama.cpp's own logits are the same read in batches of 256 and of 512. The prediction file is found above 2 GiB (`janas_llm_find_mtp`): a block of 3 GiB beside the model is taken, one larger than a quarter of the model is not.
+
+## [2026-10-22] - Qwen3.8-Flash-Next in the chat: its reasoning, its tools, a prompt the same every run
+
+- **The prompt Qwen3.8's template writes, token for token**: with the reasoning on, the template's words on the reasoning effort at the top of the system message ("Reasoning effort is set to xhigh. ...", read from the template, not written into Janas: Qwen3.8-27B's too), with or without a system message of the caller's, before the tools; and in a conversation loaded whole, every earlier reply of the assistant with its empty reasoning (the template's `preserve_thinking`), not only those after the user's last question as on Qwen3.5 and 3.6. Six cases checked against the template rendered by Jinja - reasoning on and off, a system message, a tool, a conversation with a call, its answer and a reply - the same tokens.
+- **A service opened in the chat is then called, not opened again**: after `janas__open_service` the chat requires the next reply to call one of that service's tools. Qwen3.8-Flash-Next (two bits a weight) opened the `system` service again and again, until the chat stopped it - the service shares its name with its tools' prefix (`system__`), and the model's choice fell on "jan" at -0.27 against "system" at -1.44. `janas_llm_chat_tool_choice` takes a name with `JANAS_LLM_TOOLS_REQUIRED` for this: the calls only to the functions whose names begin with it (NULL or "" as before). The catalog of the services tells the model that most questions need none of them - writing, code, explanations - and the system service is "this computer itself, when the user asks about it": a question of C code opens nothing now, one about the free memory opens the service and calls its tool. An opened service's tools come one a line in `<tools>`, as the system message lists them.
+- **`JANAS_GPU_REPEATABLE=1`**: every product of a mixture's experts on the CPU, exact, and in a prompt's blocks the GPU only with the fast prompt's own products, so the same request gives the same reply from one run to the next, and the same with drafts and without. By default an expert's product went to the GPU's rounded tiles when the GPU could read its slot of the cache, and to the CPU when it could not - which changes as the cache fills - and products shared with the CPU, begun as the tuner tried one configuration or another, left the GPU busy for the fast ones asked meanwhile: Qwen3.8-Flash-Next's reply to the same question changed from run to run, from the first token's probability on, with drafts or without them. Found by halves (the dense products, the tiles by weight type, rows ending inside a group), the CPU alone giving the same digits every run on the same busy machine. The three ways and their difference are in [features](docs/features.md). The chains of drafts the GPU verifies no longer take the experts' rounded products in one launch (a chain of 13 tokens or more on a mixture of ten experts a token did).
+- Checked on Qwen3.8-Flash-Next: its reasoning (on by the chat's default for a reasoning model) closed by the model, kept apart from the answer, the same text with and without drafts on the CPU; the conversations kept in memory and on the disk (`tests/llm_keep`, which now takes `cache=MiB` for a machine busy with other work), the model's saved state with them.
+
+## [2026-10-22] - Qwen3.8-Flash-Next's MTP block
+
+- **Drafts from Qwen3.8-Flash-Next's own prediction block**, the one unsloth publishes beside its GGUF files (`MTP/mtp-Qwen3.8-Flash-Next-Q4_K_M.gguf`, 2.8 GB): `hf2jns_mtp` now takes such a file of the block alone, besides a checkpoint, and copies the block as it is, in its own types (Q4_K, Q5_0, Q6_K and Q8_0), into a file of one layer (1.9 GB, the same bytes on every run). The block works on the model's four streams, as llama.cpp's graph does: each stream of the last layer's residual normalized with weights of its own, beside the token's normalized embedding, and projected back a stream at a time - not pooled - into streams of its own; then its layer as the model's, its attention dense, its streams kept for the next draft of a chain, and a final mix of its own before the model's output head. On 158 positions of C code its guess was the model's next choice at 130 (82%), the text's next token at 116; a greedy reply of 48 tokens with chains of three drafts came out the same, token for token, as without them, 32 of its 51 drafts kept. In the chat, to `janas-bench`'s question on bread, 64 tokens of the reply with no reasoning: 29 of 40 drafts kept greedy (72.5%), 33 of 41 sampled as the chat samples (80.5%), each reply word for word the one written without drafts; unsloth measured 64.4% for its Q4_K_M block, greedy, with another file of the model and texts of its own. On the GPU with its products bit for bit (`JANAS_GPU_FAST_PROMPT=0`) the four replies are the CPU's own; with the fast prompt, the default, the GPU computes a group's experts in one dispatch, summed in another order, and the replies change, with drafts or without. With the NPU reading the model's blocks of the C code the guesses agreed at 130 of the 158 positions too. The block's own indexer for the sparse attention is copied but left unused, as llama.cpp leaves it.
+- **An MTP file's experts are all kept in memory unless the caller asks for less** (`mtp_cache_bytes`): the memory the API plans for a model already counted the whole file. The old cache of 256 MiB held 74 of this block's experts of 3.6 MB, fewer than a block of tokens asks for at once (128), and the block failed; now a cache is never smaller than that. Qwen3-Next-80B-A3B's block keeps its 0.9 GB of experts whole too, where 256 MiB held 151 of its 512.
+- **The chat finds a prediction file beside a large model up to a quarter of the model's size**, and at least 2 GiB as before: the fixed 2 GiB left out the block unsloth recommends for Qwen3.8-Flash-Next, its Q8_0 (2.6 GB). Qwen3.5-2B, Qwen3.5-9B, Gemma-4-E4B, Gemma-4-26B-A4B and Qwen3.8-Flash-Next find the files they found before, Qwen3-4B none; a file above 2 GiB was not tried, there being none here.
+- `tests/mtp_eval` reads a row of hidden state as wide as the model gives it (`janas_llm_model_hidden_width`: Qwen3.8-Flash-Next's four streams, 10,240 numbers), takes the expert cache's size (`cache=MiB`), and says which step failed.
+
+## [2026-10-22] - Q2_K and the IQ1, IQ2 and IQ3 types on the GPU
+
+- **Weights in Q2_K, IQ2_XXS, IQ2_XS, IQ2_S, IQ3_XXS, IQ1_S and IQ1_M on the GPU**, the types of Qwen3.8-Flash-Next's and Qwen3.8-27B's other GSQ-RCO files and of unsloth's 1- and 2-bit ones: their products shared with the CPU, bit for bit as the CPU computes them (`iq2_matvec.comp`, `iq3xxs_matvec.comp`, `iq1_matvec.comp`, `q2k_matvec.comp`, with ggml's grids copied as data), a prompt's tiles (`iqk_mmq.comp`) and a mixture's experts in one dispatch (`iqk_mmid.comp`), which also takes experts in IQ3_S and IQ4_XS, until now left to the CPU. `tests/gpu_check` finds 980 more shapes the CPU's bits, `tests/gpu_fast_check` 60 more products within 1e-4 of the CPU's. No file of the catalog has these types: none of its models changes.
+
+## [2026-10-22] - Qwen3.8-Flash-Next
+
+- **Qwen3.8-Flash-Next (`qwen4exp`), a mixture of 512 experts of 177 billion parameters**, from ISTA-DASLab's GSQ-RCO files: its residual in four streams, mixed into the input of each layer's token mixer and feed-forward by gates of low rank in place of the norms, each sublayer's output added back to every stream with a weight of its own (hyper-connections); its table of n-gram embeddings at the second layer, 51 billion numbers read a row at a time from the file, sixteen rows of 160 a token; its DeltaNet layers' output gated by a sigmoid; and its attention's sparse selection, which past 2,051 positions attends only to the 512 blocks of four positions its indexer scores highest and to the query's own incomplete block. Against llama.cpp (build `2b18470`, the same Q2_0 file), the most likely token agreed at 390 of 400 positions of C code (mean absolute difference of the logits 0.135), at 367 of 400 of Italian prose (0.166), and at 2,171 of 2,200 positions of a longer text of code - all 148 past 2,051, where the attention is sparse (0.117). A pass over a block gives the same logits, bit for bit, as token by token, also when drafts are taken back.
+- **Weights in Q2_0 (ggml's type 42: two bits in blocks of 64) and Q5_0**, and **IQ1_S, IQ1_M, IQ2_XXS, IQ2_XS, IQ2_S, IQ3_XXS and Q2_K**: the types of ISTA-DASLab's four GSQ-RCO files of Qwen3.8-Flash-Next and of Qwen3.8-27B, and of unsloth's 1- and 2-bit files. Unpacked to the same numbers as ggml's, bit for bit; their products on AVX2 and AVX-VNNI, on one to eight vectors, the same bits as the scalar reference.
+- **Rows that end inside a group of 256 weights**: Qwen3.8-Flash-Next's experts are 640 wide, which only the types in blocks of 32 or 64 can hold. `gguf2jns` keeps that width (widened with zeros to 768, a fifth more bytes would be read at every token), and the products take the last group as one padded with zero blocks.
+- A saved state of `qwen4exp` (the chat's conversations kept) holds what its n-gram embeddings and its indexer read back, and when the context fills up and the oldest exchanges are forgotten, the history of those embeddings and the indexer's keys move along with the rest. The memory the engine sets aside for a context counts those keys too, 4.5 KB a position, out of the expert cache's share.
+- **Qwen3.8-Flash-Next's prompts on the GPU and on the NPU.** Blocks of 256 tokens against llama.cpp, the most likely token: on the GPU at 248 of 256 positions of C code and 232 of 256 of Italian, on the NPU at 252 and 236, where the CPU token by token gives 246 and 235. The GPU takes Q2_0 and Q5_0 in its products shared with the CPU, bit for bit as the CPU computes them, rows of 640 among them; in the fast prompt's tiles, and the experts of a block in one dispatch (`b32_mmid.comp`: Q2_0, Q5_0, IQ4_NL); and the bf16 mixes of the streams, 1.25 GB of weights read at every token. The NPU takes every type the engine unpacks, Q2_0, Q5_0 and the 1- to 3-bit kinds among them, rows of 640, a shared expert 640 wide in one run, and the bf16 mixes with their weights in FP16. `tests/gpu_check` covers the new types and rows, `tests/gpu_fast_check` the fast prompt's tiles of the blocks of 32 and 64 against the CPU. Not yet for it: the MTP block.
+- **A reply's tokens on a card with memory of its own take the bf16 mixes too** (`bf16_matvec.comp`), bit for bit as the CPU computes them: its eight lanes of fused multiply-adds (`src/llm/bf16.c`), summed across a subgroup in the same order, the inputs and outputs through the GPU's stages. With the GPU taking single tokens (`--reply-on gpu`), Qwen3.8-Flash-Next gives the same logits over 32 tokens, byte for byte, as on the CPU alone - 219 bf16 products a token on the GPU - also with its weights copied into the GPU's memory as on a card (`JANAS_GPU_COPY=1`). `tests/gpu_check` compares 96 of them bit for bit, in place and through the stages. An integrated GPU, which does not take single tokens by default, is left as it was.
+- **The NPU also takes the products whose outputs lie further apart than their rows** - a recurrent layer's q|k|v and z side by side in one buffer, on Qwen3.5, Qwen3.6 and Qwen3.8 - which stayed on the CPU: on Qwen3.6-35B-A3B its blocks of 256 give the CPU's most likely token at 251 of 256 positions. The memory set aside for the NPU's copies counts every type it takes (only Q4_K, Q5_K, Q6_K and Q8_0 were counted), so that a model of other types does not have them on top of its expert cache.
+- **A per-layer table in memory where the machine holds it** - Gemma 4's per-layer embeddings, Qwen3.8-Flash-Next's n-grams (28.8 GB) - read into memory at load when what is free holds it beside a cache of every expert, read a row at a time from the file only where memory is short; `JANAS_PLE_RAM=1` asks for it, `0` never. Gemma 4 E2B gives the same logits, byte for byte, either way. The GPU is never given such a table, which it does not read: an integrated GPU's driver pins the pages of what it is given, the table's 28.8 GB among them.
+
## [2026-10-22] - Janas's classes for FreeBASIC
- **`include/janas/janas_oop.bi`: Janas-LLM and Janas-MCP with objects, for FreeBASIC** - `Janas.Model`, `Janas.Chat`, their options, a reply's counters, a conversation kept by the program, a handler that receives a reply as it is written through its overridden methods, and an MCP server's tools; objects that close what they hold at the end of their scope and cannot be copied, a Chat that knows when its Model closed; methods that return False and say why. `include/janas/janas.bi` takes `llm.bi` and `mcp.bi` in one include. `tests/test_fb`, in the suite where FreeBASIC is found, tries every class without a model; `tests/fb_check`, on a model, finds the classes' replies the library's own in C, byte for byte.
diff --git a/MODELS.md b/MODELS.md
index 3b123af..682c510 100644
--- a/MODELS.md
+++ b/MODELS.md
@@ -123,6 +123,16 @@ e40ec6c58ef2390c7a4ce8c9b7b6b384ed4e55c9980f646d1c98e83e2c018115 qwen3.5-9b-q4k
138d1db3af7e48d3bd36a428f7a81dfd8813434521aced84459c546e4830ab5b qwen3.8-27b-ud-q4kxl.jns (gguf2jns)
```
+**Qwen3.8-Flash-Next**, a mixture of 512 experts, from [`ISTA-DASLab/Qwen3.8-Flash-Next-GSQ-RCO-GGUF`](https://huggingface.co/ISTA-DASLab/Qwen3.8-Flash-Next-GSQ-RCO-GGUF) at revision `ed59f920`, ISTA's GSQ-RCO quantization in Q2_0 (folder `Q2_0`), a quantization of [`Qwen/Qwen3.8-Flash-Next`](https://huggingface.co/Qwen/Qwen3.8-Flash-Next) at `de4b8e4d`. Two parts, the first given to `gguf2jns`, which reads the second beside it; converted in one step. Its multi-token prediction block is a GGUF file of its own, from [`unsloth/Qwen3.8-Flash-Next-GGUF`](https://huggingface.co/unsloth/Qwen3.8-Flash-Next-GGUF) at `766911a6` (folder `MTP`), copied by `hf2jns_mtp` with the converted model ([how](docs/models.md)); two runs of it give the same bytes. Under the Qwen Community License 1.0, not Apache 2.0 ([below](#what-this-project-redistributes)).
+
+```
+69820c02ec7d0b45ef2ebb19d6620299db749fe2aded7f39f93c6b88b199b720 Qwen3.8-Flash-Next-GSQ-RCO-Q2_0-00001-of-00002.gguf
+316b46f3a2dbd68c900f43136ab9449f9dcc3725dfd8c794847c204bc161e113 Qwen3.8-Flash-Next-GSQ-RCO-Q2_0-00002-of-00002.gguf
+b5b4dbe4e3ae01bffc41acefa0d1e62afdfe4b5b9bc24c2a3fab591a41f9087f qwen3.8-flash-next-gsq-q20.jns (gguf2jns)
+8087dbb39fc73f79ac069b1debe4069a44b73ada900c8bd761569d43a5a90231 mtp-Qwen3.8-Flash-Next-Q4_K_M.gguf
+d9c9dd94ef2e52391613db98c2e34c574341c48ab8752454696534d0f69a29ca qwen3.8-flash-next-mtp-q4km.jns (hf2jns_mtp)
+```
+
**Qwen3-30B-A3B**, from [`Qwen/Qwen3-30B-A3B-GGUF`](https://huggingface.co/Qwen/Qwen3-30B-A3B-GGUF):
```
@@ -222,7 +232,7 @@ The same tensors from shards on disk give the same bytes. A private or gated rep
**The converter, always. The weights, only when their licence plainly allows it.** In order:
-1. **Models under Apache 2.0, MIT or a similar licence.** Every model Janas supports today is under **Apache 2.0** and none is gated: Qwen3-Next (including Qwen3-Coder-Next), Qwen3.5, Qwen3.6 and Qwen3.8-27B, and Qwen3 MoE — checked on their pages, not in their files. Converted files may be published, and when they are, they carry:
+1. **Models under Apache 2.0, MIT or a similar licence.** Every model Janas supports but one is under **Apache 2.0** and none is gated: Qwen3-Next (including Qwen3-Coder-Next), Qwen3.5, Qwen3.6 and Qwen3.8-27B, Qwen3 MoE and Gemma 4 — checked on their pages, not in their files. The one is **Qwen3.8-Flash-Next**, under the **Qwen Community License 1.0** (on [its page](https://huggingface.co/Qwen/Qwen3.8-Flash-Next)): use, copies, changes and redistribution are allowed with its notice, but a product above 100 million monthly users or US$ 20 million a month must show the model's name, and a business offering models as a service or an AI work assistant needs a separate licence from Qwen for commercial use. ISTA-DASLab's GGUF of it says Apache 2.0 in its card; the model's page is the licence. Its converted files are published with that notice: [`prabanta-dev/Qwen3.8-Flash-Next-JNS`](https://huggingface.co/prabanta-dev/Qwen3.8-Flash-Next-JNS) carries the Qwen Community License as the model's repository gives it, and `janas-get qwen3.8-flash-next --from-gguf` converts ISTA's GGUF on your machine instead. Converted files of all of them may be published, and when they are, they carry:
- a link to the original model and to its licence;
- the original `LICENSE` and, where there is one, the `NOTICE`;
- a statement of what was changed (the list above) and which version of the tools did it;
diff --git a/README.md b/README.md
index 7a9449d..3836b47 100644
--- a/README.md
+++ b/README.md
@@ -17,6 +17,7 @@
| Model | reading 1,024 tokens: Janas-LLM | how | llama.cpp | writing: Janas-LLM | how | llama.cpp |
|---|---:|---|---:|---:|---|---:|
+| Qwen3.8-Flash-Next (66.5 GB, 177B, more than the memory) | 32.7 tok/s | GPU, 12 threads | 4.82 tok/s‡ | 10.95 tok/s | CPU, 12 threads | 3.12 tok/s‡ |
| Qwen3-Next-80B-A3B (48.4 GB, more than the memory) | 68.4 tok/s | GPU, 20 threads | 17.7 tok/s | 26.4 tok/s | CPU, 20 threads | 3.0 tok/s |
| Qwen3-Coder-Next (48.4 GB, more than the memory) | 71.4 tok/s | GPU, 12 threads | 14.0 tok/s | 26.5 tok/s | CPU, 20 threads | 2.9 tok/s |
| Qwen3.6-35B-A3B | 136 tok/s | GPU, 12 threads | 81.8 tok/s | 28.0 tok/s | CPU, 20 threads | 12.1 tok/s |
@@ -35,7 +36,7 @@
| Qwen3.5-0.8B | 1,403 tok/s | NPU, 6 threads | 514 tok/s | 110 tok/s | CPU, 12 threads | 31.1 tok/s |
| Qwen3-0.6B | 1,421 tok/s | NPU, 6 threads | 710 tok/s | 133 tok/s | CPU, 20 threads | 46.0 tok/s |
-Janas-LLM's figures are the best of the configurations `janas-bench` measures, and "how" names it: the CPU's threads, with the GPU reading a prompt's blocks (its fast prompt) or the NPU reading them; a reply is written by the CPU. The configuration the engine chose by itself, as a chat runs, was within 1.5% of the best reading and within 2.6% writing, on every model. llama.cpp ran with its Vulkan backend on the same GPU, the API Janas-LLM uses, for reading and for writing; on the two models larger than the memory it wrote with its CPU backend, faster there than with the GPU, as Janas-LLM writes. Where the conditions could not be the same, the table says so: Janas-LLM writes the other models' replies on the CPU, where llama.cpp's writing on the CPU was not measured; and where Janas-LLM reads on the NPU, llama.cpp's figure is from the GPU, its OpenVINO backend, the one that uses the NPU, having stopped on Gemma 4, on the Qwen3.5 models and on every prompt of 768 tokens and more (the two compared on the same NPU: [Intel's NPU](#intels-npu)). Its SYCL backend, Intel's own for Arc GPUs, was not used: Janas-LLM reaches every GPU through Vulkan. † llama.cpp's figures of 11 October: the file it read is no longer on this machine. The drafts, which bring a reply sooner (37 tok/s from Qwen3-Next-80B-A3B in `janas-bench` on 10 October, the expert cache warm; 31-35 in the chat on 21 October), and how every figure was taken: [How the figures were measured](#how-the-figures-were-measured). On another machine they will be other ones: `janas-bench` measures yours.
+Janas-LLM's figures are the best of the configurations `janas-bench` measures, and "how" names it: the CPU's threads, with the GPU reading a prompt's blocks (its fast prompt) or the NPU reading them; a reply is written by the CPU. The configuration the engine chose by itself, as a chat runs, was within 1.5% of the best reading and within 2.6% writing, on every model. llama.cpp ran with its Vulkan backend on the same GPU, the API Janas-LLM uses, for reading and for writing; on the two models larger than the memory it wrote with its CPU backend, faster there than with the GPU, as Janas-LLM writes. Where the conditions could not be the same, the table says so: Janas-LLM writes the other models' replies on the CPU, where llama.cpp's writing on the CPU was not measured; and where Janas-LLM reads on the NPU, llama.cpp's figure is from the GPU, its OpenVINO backend, the one that uses the NPU, having stopped on Gemma 4, on the Qwen3.5 models and on every prompt of 768 tokens and more (the two compared on the same NPU: [Intel's NPU](#intels-npu)). Its SYCL backend, Intel's own for Arc GPUs, was not used: Janas-LLM reaches every GPU through Vulkan. † llama.cpp's figures of 11 October: the file it read is no longer on this machine. ‡ Qwen3.8-Flash-Next, 23 October: llama.cpp on its CPU backend, the faster way it has here - its Vulkan backend cannot run the file on 32 GB (it copies 35.9 GB of weights into pinned host buffers; with `no_host`, 63 GB into ordinary memory) - and Janas-LLM through `tests/bench_long`, not `janas-bench`: the same 1,024 tokens of Italian prose and 200 written, 12 threads on CPUs 0-11, a 12 GiB expert cache; one run of llama.cpp, the median of three of Janas-LLM reading with the GPU and the mean of two writing. The drafts, which bring a reply sooner (37 tok/s from Qwen3-Next-80B-A3B in `janas-bench` on 10 October, the expert cache warm; 31-35 in the chat on 21 October), and how every figure was taken: [How the figures were measured](#how-the-figures-were-measured). On another machine they will be other ones: `janas-bench` measures yours.
**New: Intel's NPU reads the prompts.** On the same laptop's NPU Janas-LLM reads 1,024 tokens of Qwen3.5-2B at 729 tok/s, of Gemma-4-E2B at 508 and of Qwen3-4B at 333, 1.5 to 2.6 times as fast as without it, with answers as close to llama.cpp's as the CPU's. It takes the NPU by itself where it measures faster, and three options choose by hand: [Intel's NPU](#intels-npu), [Choosing what runs where](#choosing-what-runs-where).
diff --git a/docs/features.md b/docs/features.md
index 6d1ef2d..865272a 100644
--- a/docs/features.md
+++ b/docs/features.md
@@ -2,6 +2,7 @@
- **Models:** Qwen3.5 and Qwen3.6 (`qwen35moe`, e.g. Qwen3.6-35B-A3B), Qwen3-Next (`qwen3next`, e.g. Qwen3-Next-80B-A3B-Instruct and Qwen3-Coder-Next), Qwen3 MoE (`qwen3moe`, e.g. Qwen3-30B-A3B), the **dense** Qwen3.5 models (`qwen35`) and the **dense** Qwen3 models (`qwen3`), from the usual **Q4_K_M** GGUF files, or **Q3_K**, **IQ3_S**, **IQ4_NL**, **IQ4_XS**, **Q4_0**, **Q4_1** and **Q5_1** ones, and unsloth's "UD" mixes of them (checked on Qwen3.5-0.8B and 9B against llama.cpp), and the "MXFP4_MOE" files, whose experts are MXFP4 (checked on Qwen3.6-35B-A3B against llama.cpp on the same file: 199 of 200 most likely tokens agree). A dense feed-forward is a mixture with one expert and no router, so the same path serves both and there is no second engine to keep in step. Of the dense ones **Qwen3.5-0.8B, 2B and 9B, Qwen3-4B and Qwen3-0.6B are the ones that have been run here**; the Qwen3.5 three were checked against llama.cpp on the same files. **Nearly everything here was measured on Qwen3-Next-80B-A3B-Instruct**: it is the one the engine was shaped around, and the one whose numbers are quoted below. The others have been converted, checked against a reference and used, but far less.
- **Gemma 4** (`gemma4`): **Gemma-4-12B-it** (dense) and **Gemma-4-26B-A4B-it** (a mixture of 128 experts beside a dense feed-forward) convert and chat, with their reasoning on or off (`--think`); the prompts are, token for token, the ones the model's own chat template writes, and a full context slides as it does for the other models. Its sliding-window layers keep only the last positions they can see, so a long context costs mostly the full layers: for the 12B about 16 KB a token and half a gigabyte fixed, instead of 175 KB a token. Keys and values are kept at sixteen bits (Gemma is far more sensitive to them than Qwen). Tools work in Gemma's own notation - declarations, calls and the tools' answers inside the model's turn, token for token as its template writes them. The small **Gemma-4-E2B-it** and **E4B-it** run too, with chat and tools: their per-layer embeddings - a table as large as half of E2B's file - are read from the file a row at a time and never loaded, so E2B ran here in 1.5 GB of memory (a 3.1 GB file) and E4B in 3.1 GB (5.0 GB). Each of the four has an **assistant**, a small drafting model of its own that reads the main model's keys and values, taken as its `--mtp` file: on an Italian chat reply of 110 tokens, greedy, it took E2B from 34.7 tokens/s to 51.0, E4B from 18.3 to 26.3, the 12B from 8.5 to 12.9 and the 26B from 16.9 to 24.5 on the development laptop, the same tokens with and without it. Against llama.cpp on 200 tokens of prose the most likely token agrees at 160 positions on the 12B (mean logit difference 0.84) and at 160 on the 26B (0.97); on another 200-token text at 191 on E2B (0.45) and 195 on E4B (0.28). Against an exact float64 computation over the same weights (a NumPy forward pass written to check them), on the prose: the 12B agrees at 167 positions (llama.cpp 173), the 26B at 156 (llama.cpp 160), with mean errors of 0.75 and 0.87 (llama.cpp 0.67 and 0.90). The larger two turn small numerical differences into large ones, and llama.cpp is not a steady reference for them: against itself with an eight-bit cache it agrees at 153 positions on the 12B, and with a 32-bit cache and no flash attention, at 112. The reference has to be computed in batches of at most 32 tokens: with larger ones llama.cpp's logits for the early positions change (it looks ahead), for a reason not yet found.
+- **Qwen3.8-Flash-Next** (`qwen4exp`, since 22 October 2026): a mixture of 512 experts and 177 billion parameters, from ISTA-DASLab's two-bit GGUF (GSQ-RCO, Q2_0, 66.4 GB), on the CPU, the GPU and the NPU. Its residual runs in four streams (hyper-connections), its n-gram embeddings are read from the file a row at a time, and past 2,051 positions its attention is sparse, an indexer choosing the blocks each token reads. Against llama.cpp on the same file, 2,200 tokens of prose: the most likely token agrees at 2,171 positions (mean logit difference 0.134), and at all 148 positions read by the sparse attention. On 8,192 tokens, its last 512 positions (an Italian table of links and numbers) agree at 482, mean difference 0.191, and the same 512 tokens read after only 512 others agree at 482 too (0.178): the long context costs no agreement. Its multi-token prediction block comes from unsloth's GGUF of the block ([models](models.md)): its draft is the model's own next token at 130 of 158 positions of code, and on a chat reply on the CPU 29 of 40 drafts were kept, the text the same with drafts and without. The chat writes, token for token, the prompt the model's own template writes - the reasoning effort's words at the top of the system message, the earlier replies with their reasoning - and calls tools in its XML notation. `JANAS_HC_MIX=q8` rewrites its mixes of the streams - 1.26 GB of BF16 matrices read at every token, a third of what a token reads besides the experts - as Q8_0 when the model opens, half the bytes, their inputs kept at sixteen bits: against llama.cpp on 400 tokens of C the most likely token agreed at 392 positions (BF16: 390), on 400 of Italian prose at 364 (367), the mean logit difference 0.138 and 0.173 (0.135, 0.166); with the inputs quantized to eight bits as the quantized products take them, 388 and 359, and weights at four bits, 382 and 349. Those products run on the CPU, and in a prompt's blocks on the GPU (below). On the CPU (Core Ultra 9 185H, 12 threads, a 12 GiB expert cache, 1,024 tokens of prose and then 200) a reply runs at 11.9 tokens/s with them and at 10.95 with the BF16 mixes, the prompt at 24.0 either way; the default stays BF16. `--head 4` (`JANAS_HEAD_BITS=4`) takes its output head from Q5_K to Q4_K, 437 MB a token to 358, at a price in quality as on the other models: 387 and 351 positions (0.184, 0.234). A prompt's blocks run whole on a GPU that shares the host's memory (`JANAS_GPU_MOE=1` where the cache holds fewer than half the experts), its sparse attention too, choosing the same blocks as the CPU: on the last 512 of 8,192 tokens the most likely token agreed with llama.cpp at 481 positions (the CPU 482). Against llama.cpp, blocks of 256 tokens of C agree at 247 of 256 positions (the GPU's products beside the CPU: 248), of Italian prose at 230 (235 to 238; the CPU alone 235) with the lowest divergence of the three (KL 0.0172, against 0.0176 and 0.0186); its speed is not measured yet. The weights are under the Qwen Community License 1.0 ([MODELS.md](../MODELS.md#what-this-project-redistributes)); `janas-get qwen3.8-flash-next` downloads it converted, with its MTP block, from [`prabanta-dev/Qwen3.8-Flash-Next-JNS`](https://huggingface.co/prabanta-dev/Qwen3.8-Flash-Next-JNS) (68.4 GB, under the same licence); with `--from-gguf` it downloads ISTA's GGUF and converts it.
- **Reasoning models:** the reasoning is shown apart, in grey, and can be turned off.
- **`janas-chat`:** a terminal chat with a line editor of its own, history, colours, a status line and a context that slides instead of ending.
- **`libjanas_llm`:** a C library ([`include/janas/llm.h`](../include/janas/llm.h)) and a FreeBASIC binding ([`include/janas/llm.bi`](../include/janas/llm.bi)).
@@ -21,7 +22,7 @@
- **`janas-bench`:** measures the machine, fills its profile and writes a report.
- **Faster replies** from drafts the model verifies, so the text is exactly the one it would have written: from the model's own multi-token prediction block where it has one, from the conversation where it does not, or from a small model of the same family given with `--draft`.
- **Optional GPU** (Vulkan 1.3, integrated or discrete), used only where the engine measures that it helps, for every quantized type the CPU reads. A reply's tokens are identical with and without it; a prompt is read with the fast prompt (next).
-- **The fast prompt**, on by default since 10 October 2026: a prompt's blocks go to the GPU's tiled products (Q4_K, Q5_K, Q6_K, Q8_0, Q3_K, IQ4_XS, IQ3_S, IQ4_NL, MXFP4, the experts of a mixture in one launch per layer - Q4_K, Q6_K, MXFP4 and, since 21 October 2026, Q3_K - f32 and bf16 weights), and on the dense Qwen3 and Qwen3.5 models the whole block - every layer, attention, Qwen3.5's recurrent layers - goes to the GPU in one submission while the CPU rests, and on the Qwen3 and Qwen3.6 mixtures a layer a submission, the CPU only sorting the router's choice and reading the next layer's experts ahead (where the expert cache holds half the experts or more; `JANAS_GPU_MOE=0` or `1` decides instead): on a laptop the CPU working beside the GPU takes the power the GPU would use. They round the sums group by group, so a prompt is not read to the bit as the CPU reads it. Against the references the most likely token agreed with the exact way's within 2-3 of 256 positions on the models measured on 10 October 2026, the mean difference of the scores within 2%; on 21 October, Qwen3-30B-A3B against llama.cpp's scores of Qwen's Q8_0, on Italian prose and code, within 5 either way (unsloth's 3-bit file 216 and 246 against the exact way's 221 and 245, Qwen's Q4_K_M 210 and 239 against 207 and 244) and the mean difference within 3%: 8 to 27 of a text's 256 positions change their most likely token with the rounding, and they fall on either side. On the development laptop Qwen3-4B read a prompt of 256 tokens at 220 tok/s with it and at 85 without the GPU (10 October 2026), and on 21 October every model compared read a prompt faster than llama.cpp did on the same GPU ([the table](../README.md)). `JANAS_GPU_FAST_PROMPT=0` reads them the exact way.
+- **The fast prompt**, on by default since 10 October 2026: a prompt's blocks go to the GPU's tiled products (Q4_K, Q5_K, Q6_K, Q8_0, Q3_K, IQ4_XS, IQ3_S, IQ4_NL, MXFP4, the experts of a mixture in one launch per layer - Q4_K, Q6_K, MXFP4 and, since 21 October 2026, Q3_K - f32 and bf16 weights), and on the dense Qwen3 and Qwen3.5 models the whole block - every layer, attention, Qwen3.5's recurrent layers - goes to the GPU in one submission while the CPU rests, and on the Qwen3 and Qwen3.6 mixtures a layer a submission, the CPU only sorting the router's choice and reading the next layer's experts ahead (where the expert cache holds half the experts or more; `JANAS_GPU_MOE=0` or `1` decides instead): on a laptop the CPU working beside the GPU takes the power the GPU would use. They round the sums group by group, so a prompt is not read to the bit as the CPU reads it. Against the references the most likely token agreed with the exact way's within 2-3 of 256 positions on the models measured on 10 October 2026, the mean difference of the scores within 2%; on 21 October, Qwen3-30B-A3B against llama.cpp's scores of Qwen's Q8_0, on Italian prose and code, within 5 either way (unsloth's 3-bit file 216 and 246 against the exact way's 221 and 245, Qwen's Q4_K_M 210 and 239 against 207 and 244) and the mean difference within 3%: 8 to 27 of a text's 256 positions change their most likely token with the rounding, and they fall on either side. On the development laptop Qwen3-4B read a prompt of 256 tokens at 220 tok/s with it and at 85 without the GPU (10 October 2026), and on 21 October every model compared read a prompt faster than llama.cpp did on the same GPU ([the table](../README.md)). `JANAS_GPU_FAST_PROMPT=0` reads them the exact way. On a mixture larger than its expert cache the fast prompt is also not repeatable: a group of experts goes to the GPU's rounded products only when all of them are in the memory given to the GPU, which changes as the cache fills, so the same prompt read twice can have some groups rounded and others exact, and a reply that follows it can differ from one run to the next (Qwen3.8-Flash-Next on the development laptop, 22 October 2026: the reply to the same question changed after a dozen tokens). `JANAS_GPU_REPEATABLE=1` keeps every product of a mixture's experts on the CPU, exact, and in a prompt's blocks gives the GPU only the fast prompt's products: the same request then gives the same reply from one run to the next, to the last digit of every token's probability, and the same reply with drafts and without (Qwen3.8-Flash-Next on the development laptop, 22 October 2026), at the price of the experts' share of the GPU's speed (not measured yet). The difference, in short: by default, the fastest prompt, and on a mixture larger than its cache replies that can vary slightly from run to run, with drafts or without them; with `JANAS_GPU_REPEATABLE=1`, the same reply every run, the experts computed on the CPU; with `JANAS_GPU_FAST_PROMPT=0`, the same results as the CPU alone, to the bit. On the CPU alone drafts never change the text. Since that day the chains of drafts the GPU verifies also stay off the experts' rounded products in one launch (until then a chain of 13 tokens or more on a mixture could take them).
- **Intel's NPU** (the Core Ultra's, on Linux; since 21 October 2026) reads a prompt's blocks of 64 tokens and more. A dense model whose layers fit in its memory, nine in ten at least, goes to it layer by layer, whole - Qwen3, Qwen3.5 and Gemma 4's dense models - and other models give it the products of the weights that stay in memory; a reply's tokens stay with the CPU. Janas-LLM writes the NPU's graphs itself, in OpenVINO's IR format, and opens Intel's Level Zero driver when a model opens: nothing is linked against it. The engine takes the NPU by itself where it reads faster and its answers measured as close to llama.cpp's as the CPU's (the Qwen models, and Gemma 4's whose layers go whole); where the expert cache cannot hold all of a model's feed-forward weights (the larger mixtures, Qwen3.8-27B) it stays off unless asked, its copies' memory being the cache's. On the development laptop, 1,024 tokens, with it and without: Qwen3-0.6B 1,421 and 974 tok/s, Qwen3.5-0.8B 1,403 and 950, Qwen3.5-2B 729 and 477, Qwen3-4B 333 and 201, Gemma-4-E2B 508 and 196, Gemma-4-E4B 245 and 129; on the mixtures and the dense models too large for it the GPU's fast prompt reads faster there. Its first blocks in a session wait while it is made ready, 7.5 to 53 s, and the chat's status line says so. How it works and what it needs: [the README](../README.md#intels-npu).
- **What runs where, by hand**: `--prompt-on`, `--drafts-on` and `--reply-on` (`auto`, `cpu`, `gpu`, `npu`, `npu+gpu`, or a list the engine chooses from) in `janas-chat`, `janas-server` and `janas-bench` ([docs/chat.md](chat.md)).
- **Experts read ahead** when the expert cache is too small for the model: the next layer's router is applied early and the experts it picks that are not in memory are read from disk while the current layer computes. It turns itself on only when the cache misses many experts a token (Qwen3-Next-80B with a 4 GiB cache: +6%) and changes no result. A long prompt is read the same way, block by block: the next layer's router is applied to the whole block and the experts most of its tokens will want are read while the current layer computes; on after a block that missed many experts. Qwen3-Next-80B reading 4,096 tokens: 36.8 to 42.0 tokens/s with a 4 GiB cache, 48.5 to 50.0 with 16 GiB (the 256 tokens written after it 1.4% slower, the cache holding other experts); Qwen3-30B-A3B, whose experts all fit, even. `JANAS_PREFETCH_PREFILL=0` turns it off.
diff --git a/docs/models.md b/docs/models.md
index 8f49dfc..5c0cd17 100644
--- a/docs/models.md
+++ b/docs/models.md
@@ -87,6 +87,15 @@ bin/x86_64-linux/hf2jns_mtp models/qwen3-next.jns \
For the dense Qwen3.5-9B that is 487 MB instead of 14 GB of shards, in under a minute. It also takes the shards themselves, when you have them on disk (`hf2jns_mtp <model.jns> <shard>... <out.jns>`). Qwen3.6-35B-A3B needs none of this: its GGUF keeps the block.
+Qwen3.8-Flash-Next's block comes in a GGUF file of its own, which unsloth publishes beside its quantizations of the model ([`unsloth/Qwen3.8-Flash-Next-GGUF`](https://huggingface.co/unsloth/Qwen3.8-Flash-Next-GGUF), folder `MTP`). `hf2jns_mtp` copies the block as it is, in its own types, and leaves out the file's token embeddings and output head - the engine takes the model's own:
+
+```sh
+bin/x86_64-linux/hf2jns_mtp models/qwen3.8-flash-next.jns \
+ mtp-Qwen3.8-Flash-Next-Q4_K_M.gguf models/qwen3.8-flash-next-mtp.jns
+```
+
+From the Q4_K_M file (2.8 GB) that is 1.9 GB, the same bytes on every run. In the chat, on one reply of 64 tokens to a question on bread, 72.5% of its drafts were kept greedy and 80.5% sampled as the chat samples, and the reply was the one written without them.
+
**Gemma 4** comes from unsloth's GGUF files, converted in one step, and so does its assistant (the `mtp-gemma-4-*.gguf` file beside the model, under 0.5 GB), which serves as the multi-token prediction file:
```sh
diff --git a/docs/tools.md b/docs/tools.md
index 82b1e82..43418dd 100644
--- a/docs/tools.md
+++ b/docs/tools.md
@@ -3,7 +3,7 @@
| | |
|---|---|
| `tools/gguf2jns` | GGUF → the `.jns` format the engine reads |
-| `tools/hf2jns_mtp` | the multi-token prediction block, from the original checkpoint: its shards, or just the block's tensors from Hugging Face (`hf:<owner>/<repo>`) |
+| `tools/hf2jns_mtp` | the multi-token prediction block, from the original checkpoint: its shards, or just the block's tensors from Hugging Face (`hf:<owner>/<repo>`); or from a GGUF file of the block alone (unsloth's `mtp-*.gguf` of Qwen3.8-Flash-Next), copied as it is |
| `tools/jns_planes` | rewrites a model with the experts' down matrix in bit planes, so a machine short of memory can read part of it |
| `jns_check` | checks a model file, with `--verify` every expert's checksum |
| `tools/openapi_routes` | `janas-server`'s route table, from OpenAI's specification |
diff --git a/include/janas/llm.h b/include/janas/llm.h
index 2d790f4..876b1b8 100644
--- a/include/janas/llm.h
+++ b/include/janas/llm.h
@@ -557,8 +557,11 @@ JANAS_LLM_API int32_t janas_llm_chat_aside(
/* Whether the model calls a tool, from the next reply on: as it judges
(AUTO, default), never (NONE), at least one (REQUIRED), or the function
- named (FUNCTION, name of len bytes). parallel 0 allows one call at most
- where calls are required. */
+ named (FUNCTION, name of len bytes). REQUIRED with a name (len bytes, or
+ to its end with -1; NULL or "": any): the calls only to the functions
+ whose names begin with it, where one does - janas-chat holds the reply
+ after a service is opened to that service's tools. parallel 0 allows one
+ call at most where calls are required. */
#define JANAS_LLM_TOOLS_AUTO 0
#define JANAS_LLM_TOOLS_NONE 1
#define JANAS_LLM_TOOLS_REQUIRED 2
diff --git a/src/chat/main.c b/src/chat/main.c
index bd6c019..c06114a 100644
--- a/src/chat/main.c
+++ b/src/chat/main.c
@@ -1546,9 +1546,12 @@ static int run_calls(janas_llm_chat *c, int autorun)
term_printf(&term, "%s\n", janas_llm_last_error());
free(more);
/* and the next reply is a call: told to call one, a small
- model (Qwen3.5-0.8B) still made up the weather instead */
- janas_llm_chat_tool_choice(c, JANAS_LLM_TOOLS_REQUIRED, NULL, 0,
- 1);
+ model (Qwen3.5-0.8B) still made up the weather instead;
+ and to one of the service's own tools: Qwen3.8-Flash-Next
+ (two bits a weight) opened the service again and again,
+ its name being that of its tools' prefix (22 Oct 2026) */
+ janas_llm_chat_tool_choice(c, JANAS_LLM_TOOLS_REQUIRED,
+ mcpc_opened_prefix(), -1, 1);
}
if (rc != 0 && key && failed.n < FAILED_MAX) {
failed.call[failed.n++] = key;
diff --git a/src/chat/mcp_chat.c b/src/chat/mcp_chat.c
index 260e56d..f84cae7 100644
--- a/src/chat/mcp_chat.c
+++ b/src/chat/mcp_chat.c
@@ -53,9 +53,10 @@ static const struct {
"electricity, the cheapest fuel near a place"},
{"quakes", "earthquakes: the latest near a place or where the user is, "
"the strongest in the world, one in detail"},
- {"system", "this computer: how it is (memory, processor, disks, "
- "battery, temperatures), what fills a folder, the programs "
- "running, the errors, the updates, the network"},
+ {"system", "this computer itself, when the user asks about it: how it "
+ "is (memory, processor, disks, battery, temperatures), what "
+ "fills a folder, the programs running, the errors, the "
+ "updates, the network"},
{"weather", "the weather now and the forecast, the sea and its waves, "
"weather warnings"},
{"wiki", "Wikipedia, when the user asks for it: a page shown to the "
@@ -75,6 +76,7 @@ static const struct {
#define OPEN_TOOL "janas__open_service"
static int deferred = 1;
static unsigned opened_gen; /* changes when a service is opened or all closed */
+static const char *opened_prefix; /* of the service opened last */
static struct server *servers;
static size_t n_servers;
@@ -210,8 +212,10 @@ static void catalog(FILE *f)
"listed here: to use a service, call " OPEN_TOOL " with its name, "
"by yourself whenever a question is on its subject (the user need "
"not name it), then call the tools it gives you; an open service "
- "stays open for the conversation. Never name the services or the "
- "tools in your reply. The services:\n",
+ "stays open for the conversation. Most questions need none - "
+ "writing, code, explanations, what you know: answer those "
+ "yourself, without opening a service. Never name the services or "
+ "the tools in your reply. The services:\n",
f);
for (size_t i = 0; i < n_servers; i++) {
if (!servers[i].own)
@@ -265,18 +269,33 @@ static int open_service(const char *args, char **text)
len = 0;
janas_mcp_tools(s->m, s->prefix, -1, NULL, 0, &len);
char *t = malloc((size_t)len + 1);
- if (t &&
- janas_mcp_tools(s->m, s->prefix, -1, t, len + 1, &len) == JANAS_MCP_OK)
+ if (t && janas_mcp_tools(s->m, s->prefix, -1, t, len + 1, &len) ==
+ JANAS_MCP_OK) {
+ /* as the system message lists tools, one a line in <tools>: given
+ them as one array in the text, Qwen3.8-Flash-Next opened the
+ service again and again instead of calling them (22 Oct 2026) */
janas_buf_printf(&o,
"The service %s is open. Its tools, to call as "
- "any other:\n%s\n\nNow call the one that "
- "answers the user's question: do not answer "
- "before it has, and never make up what it "
- "would say.",
- s->name, t);
+ "any other:\n\n<tools>",
+ s->name);
+ struct janas_json_doc *td = janas_json_parse(t, (size_t)len, NULL, 0);
+ const struct janas_json *a = td ? janas_json_root(td) : NULL;
+ if (a && a->type == JANAS_JSON_ARRAY)
+ for (const struct janas_json *it = a->child; it; it = it->next) {
+ janas_buf_puts(&o, "\n");
+ janas_json_write(&o, it);
+ }
+ else
+ janas_buf_printf(&o, "\n%s", t);
+ janas_json_free(td);
+ janas_buf_puts(&o, "\n</tools>\n\nNow call the one that answers "
+ "the user's question: do not answer before it "
+ "has, and never make up what it would say.");
+ }
free(t);
s->opened = 1;
opened_gen++;
+ opened_prefix = s->prefix;
*text = o.oom ? NULL : o.p;
if (o.oom)
janas_buf_free(&o);
@@ -288,6 +307,11 @@ unsigned mcpc_opened(void)
return opened_gen;
}
+const char *mcpc_opened_prefix(void)
+{
+ return opened_prefix;
+}
+
char *mcpc_opened_tools(void)
{
struct janas_buf o = {0};
@@ -322,6 +346,7 @@ void mcpc_close_services(void)
for (size_t i = 0; i < n_servers; i++)
servers[i].opened = 0;
opened_gen++;
+ opened_prefix = NULL;
}
static int add_server(const char *line, janas_mcp *m, int own)
diff --git a/src/chat/mcp_chat.h b/src/chat/mcp_chat.h
index 191b7af..40159cd 100644
--- a/src/chat/mcp_chat.h
+++ b/src/chat/mcp_chat.h
@@ -37,6 +37,9 @@ void mcpc_defer(int on);
of those open (mcpc_opened_tools, malloc'd JSON array, NULL: none) go
to janas_llm_chat_tools_add. mcpc_close_services: a new conversation. */
unsigned mcpc_opened(void);
+/* The prefix of the tools of the service opened last ("system__"), or NULL:
+ the reply after it is held to them. */
+const char *mcpc_opened_prefix(void);
char *mcpc_opened_tools(void);
void mcpc_close_services(void);
diff --git a/src/common/catalog.c b/src/common/catalog.c
index 09647b6..7a1a8c2 100644
--- a/src/common/catalog.c
+++ b/src/common/catalog.c
@@ -147,6 +147,32 @@ const struct janas_model_entry janas_catalog[] = {
.flat_sha =
"138d1db3af7e48d3bd36a428f7a81dfd8813434521aced84459c546e4830ab5b",
.ram_gb = 32},
+ /* Qwen Community License 1.0 (MODELS.md): converted files may be
+ published with its notice, which the repository carries */
+ {.name = "qwen3.8-flash-next",
+ .what = "Qwen3.8-Flash-Next, 512 experts, ISTA's 2-bit GSQ, with its MTP "
+ "block",
+ .repo = "ISTA-DASLab/Qwen3.8-Flash-Next-GSQ-RCO-GGUF",
+ .parts = {"Q2_0/Qwen3.8-Flash-Next-GSQ-RCO-Q2_0-00001-of-00002.gguf",
+ "Q2_0/Qwen3.8-Flash-Next-GSQ-RCO-Q2_0-00002-of-00002.gguf"},
+ .part_sha =
+ {"69820c02ec7d0b45ef2ebb19d6620299db749fe2aded7f39f93c6b88b199b720",
+ "316b46f3a2dbd68c900f43136ab9449f9dcc3725dfd8c794847c204bc161e113"},
+ .gguf_gb = 66.4,
+ .out = "qwen3.8-flash-next-gsq-q20.jns",
+ .jns_repo = "prabanta-dev/Qwen3.8-Flash-Next-JNS",
+ .jns_gb = 68.38,
+ .flat_sha =
+ "b5b4dbe4e3ae01bffc41acefa0d1e62afdfe4b5b9bc24c2a3fab591a41f9087f",
+ .extra = JANAS_EXTRA_BLOCK,
+ .extra_repo = "unsloth/Qwen3.8-Flash-Next-GGUF",
+ .extra_src = "MTP/mtp-Qwen3.8-Flash-Next-Q4_K_M.gguf",
+ .extra_src_sha =
+ "8087dbb39fc73f79ac069b1debe4069a44b73ada900c8bd761569d43a5a90231",
+ .extra_out = "qwen3.8-flash-next-mtp-q4km.jns",
+ .extra_sha =
+ "d9c9dd94ef2e52391613db98c2e34c574341c48ab8752454696534d0f69a29ca",
+ .ram_gb = 32},
{.name = "qwen3-30b-a3b",
.what = "Qwen3-30B-A3B, mixture of experts",
.repo = "Qwen/Qwen3-30B-A3B-GGUF",
diff --git a/src/common/catalog.h b/src/common/catalog.h
index 421d128..7ac0ddb 100644
--- a/src/common/catalog.h
+++ b/src/common/catalog.h
@@ -16,9 +16,12 @@
/* how the model's multi-token prediction file is made, if it has one */
enum janas_model_extra {
JANAS_EXTRA_NONE,
- JANAS_EXTRA_GGUF, /* a GGUF of its own (Gemma 4's assistant), gguf2jns */
- JANAS_EXTRA_HF2JNS /* the block of the original checkpoint, hf2jns_mtp hf:
- */
+ JANAS_EXTRA_GGUF, /* a GGUF of its own (Gemma 4's assistant), gguf2jns */
+ JANAS_EXTRA_HF2JNS, /* the block of the original checkpoint, hf2jns_mtp
+ hf: */
+ JANAS_EXTRA_BLOCK /* a GGUF of the block alone (unsloth's of
+ Qwen3.8-Flash-Next), perhaps from another
+ repository, copied by hf2jns_mtp */
};
struct janas_model_entry {
@@ -39,8 +42,10 @@ struct janas_model_entry {
there are no planes) */
const char *jns_sha; /* what jns_planes writes, NULL: one step */
enum janas_model_extra extra;
- const char *extra_src; /* GGUF file of repo, or hf:repo[@rev] */
- const char *extra_src_sha; /* of that GGUF (JANAS_EXTRA_GGUF) */
+ const char *extra_src; /* GGUF file of repo (of extra_repo, if
+ given), or hf:repo[@rev] */
+ const char *extra_repo;
+ const char *extra_src_sha; /* of that GGUF (JANAS_EXTRA_GGUF, _BLOCK) */
const char *extra_out;
const char *extra_sha;
int ram_gb; /* the smallest machine it is meant for */
diff --git a/src/common/env_names.c b/src/common/env_names.c
index 7cd478b..f7ec2be 100644
--- a/src/common/env_names.c
+++ b/src/common/env_names.c
@@ -62,12 +62,14 @@ const char *const JANAS_ENV_NAMES[] = {"JANAS_ALPHAVANTAGE_KEY",
"JANAS_GPU_NO_ROW",
"JANAS_GPU_NOSPIN",
"JANAS_GPU_REPACK",
+ "JANAS_GPU_REPEATABLE",
"JANAS_GPU_ROW_NV",
"JANAS_GPU_SG",
"JANAS_GPU_TOKEN",
"JANAS_GPU_TOKEN_PROFILE",
"JANAS_GPU_VERBOSE",
"JANAS_GPU_XCOH",
+ "JANAS_HC_MIX",
"JANAS_HEAD_BITS",
"JANAS_JANET_FFN",
"JANAS_JANET_REC",
@@ -100,6 +102,7 @@ const char *const JANAS_ENV_NAMES[] = {"JANAS_ALPHAVANTAGE_KEY",
"JANAS_OVERPASS_URL",
"JANAS_OVERPASS_URL2",
"JANAS_PHOTON_URL",
+ "JANAS_PLE_RAM",
"JANAS_POOLTRACE",
"JANAS_PREFETCH",
"JANAS_PREFETCH_PREFILL",
diff --git a/src/get/main.c b/src/get/main.c
index 333836c..de4d6c5 100644
--- a/src/get/main.c
+++ b/src/get/main.c
@@ -149,6 +149,21 @@ static int from_gguf(const struct janas_model_entry *m, const char *dir,
snprintf(xout, sizeof(xout), "%s/%s", dir, m->extra_out);
if (exists(xout) && get_same(xout, m->extra_sha) == 1) {
printf("%s: already here, fingerprint checked\n", xout);
+ } else if (m->extra == JANAS_EXTRA_BLOCK) {
+ char src[4096];
+ printf("== its MTP block, from %s (hf2jns_mtp)\n",
+ m->extra_repo ? m->extra_repo : m->repo);
+ if (get_fetch(m->extra_repo ? m->extra_repo : m->repo, m->extra_src,
+ dir, m->extra_src_sha, 0) != 0)
+ return 1;
+ get_local(src, sizeof(src), dir, m->extra_src);
+ char *argv[] = {"hf2jns_mtp", out, src, xout, NULL};
+ fflush(stdout);
+ if (get_run("hf2jns_mtp", argv) != 0 ||
+ check(xout, m->extra_sha, "hf2jns_mtp") != 0)
+ return 1;
+ if (!keep)
+ remove(src);
} else if (m->extra == JANAS_EXTRA_GGUF) {
char src[4096];
printf("== its assistant\n");
diff --git a/src/llm/api.c b/src/llm/api.c
index 9af2c19..7122bcc 100644
--- a/src/llm/api.c
+++ b/src/llm/api.c
@@ -95,6 +95,42 @@ static uint64_t file_size(const char *path)
return stat(path, &st) == 0 ? (uint64_t)st.st_size : 0;
}
+/* The words a template puts at the top of the system message while the
+ model reasons, at the effort it takes when none is asked for, into
+ llm->think_note; empty where it has none. Qwen3.8's:
+ {%- set resolved_reasoning_effort = reasoning_effort|default('xhigh') %}
+ ...
+ {%- if resolved_reasoning_effort == 'xhigh' %}
+ {%- set reasoning_instructions = 'Reasoning effort is set to ...' %}
+ read from the template rather than written here, so that another
+ model's words, or another default, are its own. */
+static void find_think_note(janas_llm *llm, const char *tmpl)
+{
+ static const char DEF[] = "reasoning_effort|default('",
+ SET[] = "set reasoning_instructions = '";
+ llm->think_note[0] = 0;
+ const char *d = strstr(tmpl, DEF);
+ if (!d)
+ return;
+ d += sizeof DEF - 1;
+ const char *e = strchr(d, '\'');
+ if (!e || e - d > 32)
+ return;
+ char key[64];
+ snprintf(key, sizeof key, "== '%.*s' %%}", (int)(e - d), d);
+ const char *k = strstr(tmpl, key);
+ const char *s = k ? strstr(k, SET) : NULL;
+ if (!s)
+ return;
+ s += sizeof SET - 1;
+ const char *q = strchr(s, '\'');
+ if (!q || (size_t)(q - s) >= sizeof llm->think_note ||
+ memchr(s, '\\', (size_t)(q - s)))
+ return; /* too long, or escaped: left out rather than cut */
+ memcpy(llm->think_note, s, (size_t)(q - s));
+ llm->think_note[q - s] = 0;
+}
+
/* Recognizes the chat format from the template in tmpl; returns 0 or an
error code. */
static int32_t find_format_in(janas_llm *llm, const struct janas_jns *j,
@@ -152,6 +188,11 @@ static int32_t find_format_in(janas_llm *llm, const struct janas_jns *j,
llm->think_close = janas_tokenizer_find(llm->tok, "</think>");
llm->thinker = strstr(tmpl, "enable_thinking") &&
llm->think_open >= 0 && llm->think_close >= 0;
+ if (llm->thinker)
+ find_think_note(llm, tmpl);
+ llm->think_kept =
+ llm->thinker && strstr(tmpl, "preserve_thinking is undefined or "
+ "preserve_thinking is true") != NULL;
/* tools: the template says how calls are written, the vocabulary
must have the markers as tokens of their own */
llm->call_open = janas_tokenizer_find(llm->tok, "<tool_call>");
@@ -216,6 +257,8 @@ struct mem_need {
uint64_t attn_part; /* attention's partial results beside the cache */
uint64_t scratch; /* a pass's scratch */
uint64_t npu; /* the NPU's copies of its matrices (model_npu.c) */
+ uint64_t table; /* the per-layer table, read a row at a time unless the
+ memory holds it (janas_llm_options ple_ram) */
};
/*
@@ -226,7 +269,11 @@ struct mem_need {
* on Meteor Lake, 20 Oct 2026), and 128 MB for the compiler. Taken from the
* expert cache's share: without it the copies came on top of a cache sized
* to the memory, and Qwen3-Next-80B ran out of it as the NPU's
- * configurations began.
+ * configurations began. Every type the NPU takes (until 22 Oct 2026 only
+ * Q4_K, Q5_K, Q6_K and Q8_0 were counted, and a model of other types,
+ * Qwen3.8-Flash-Next's Q2_0, Q3_K and IQ4_XS, would have had its copies on
+ * top of the cache), and qwen4exp's bf16 mixes at two bytes a weight
+ * (model_hc.c, in FP16 on the NPU).
*/
static uint64_t npu_need(const struct janas_jns *j)
{
@@ -237,12 +284,19 @@ static uint64_t npu_need(const struct janas_jns *j)
for (uint64_t i = 0; i < j->h.n_tensor; i++) {
const struct janas_jns_tensor *t = &j->tensors[i];
int type = (int)t->type;
+ /* not the tables (token_embd, and per_layer_token_embd: read a row
+ at a time) */
if (t->n_dims != 2 || t->offset < r0 || t->offset >= r1 ||
- !(type == JANAS_Q4_K || type == JANAS_Q5_K || type == JANAS_Q6_K ||
- type == JANAS_Q8_0) ||
strstr(t->name, "token_embd") || !strcmp(t->name, "output.weight"))
continue;
- n += t->dims[0] * t->dims[1];
+ /* bf16: the mixes of the streams and the n-gram layer's value */
+ int mix = type == 30 && t->dims[1] >= 64 &&
+ (strstr(t->name, "hc_down") || strstr(t->name, "hc_up") ||
+ strstr(t->name, "ple_value") || strstr(t->name, "indexer"));
+ if (mix)
+ n += 2 * t->dims[0] * t->dims[1];
+ else if (janas_llm_npu_type(type))
+ n += t->dims[0] * t->dims[1];
}
for (uint32_t l = 0; j->h.n_expert <= 1 && l < j->h.n_layer; l++)
for (int k = 0; k < 3; k++)
@@ -259,13 +313,16 @@ static void mem_need(const struct janas_jns *j, uint32_t n_ctx, int mtp_file,
{
char arch[32] = "";
janas_jns_meta_str(j, "general.architecture", arch, sizeof(arch));
- uint64_t resident = j->h.resident_bytes;
- /* less Gemma 4's per-layer embeddings, read from the file a row at a
- time (model_ple.c) */
+ uint64_t resident = j->h.resident_bytes, table = 0;
+ /* less the per-layer table (Gemma 4's embeddings, Qwen3.8-Flash-Next's
+ n-grams), read from the file a row at a time unless the memory holds
+ it (janas_llm_open) */
const struct janas_jns_tensor *ple =
janas_jns_tensor(j, "per_layer_token_embd.weight");
- if (ple && ple->bytes < resident)
+ if (ple && ple->bytes < resident) {
resident -= ple->bytes;
+ table = ple->bytes;
+ }
/* what the cache would need to hold every slot at once. Not the size of
the expert region: the cache's slots are all as big as the largest, so
where the layers differ (a down matrix in Q6_K in one and Q4_K in the
@@ -362,6 +419,30 @@ static void mem_need(const struct janas_jns *j, uint32_t n_ctx, int mtp_file,
}
if (!kv_token || kv_token > (1 << 20))
kv_token = 64 << 10; /* unsaid or odd: the old bound */
+ /* qwen4exp's sparse attention: its indexer keeps every position's key
+ at half precision and each block's pooled key at full in every
+ attention layer (model_qsa.c) - 4.5 KB a position on
+ Qwen3.8-Flash-Next, 1.2 GB at 262,144 tokens, uncounted until 22 Oct
+ 2026 */
+ uint32_t ix_heads = 0, ix_dim = 0;
+ snprintf(key, sizeof(key), "%.32s.attention.indexer.head_count", arch);
+ if (janas_jns_meta_u32(j, key, &ix_heads) == 0 && ix_heads) {
+ snprintf(key, sizeof(key), "%.32s.attention.indexer.key_length", arch);
+ janas_jns_meta_u32(j, key, &ix_dim);
+ uint64_t ratio = 0;
+ snprintf(key, sizeof(key), "%.32s.attention.compress_ratios", arch);
+ for (uint32_t l = 0; !ratio && l < n_layer; l++)
+ if (janas_jns_meta_uint_at(j, key, l, &ratio, NULL) != 0)
+ break;
+ uint32_t interval = 1;
+ snprintf(key, sizeof(key), "%.32s.full_attention_interval", arch);
+ if (a && a->rec != JANAS_REC_NONE &&
+ (janas_jns_meta_u32(j, key, &interval) != 0 || interval == 0))
+ interval = 4;
+ uint64_t n_attn = n_layer / interval;
+ if (ratio)
+ kv_token += n_attn * ix_dim * 2 + n_attn * ix_dim * 4 / ratio;
+ }
/* and what attention keeps beside the cache, which grows with the
context as well: uncounted, 262,144 tokens of Qwen3-Next-80B put
1.08 GB of partial results in the margin left to other programs */
@@ -405,6 +486,14 @@ static void mem_need(const struct janas_jns *j, uint32_t n_ctx, int mtp_file,
uint64_t scratch =
blk * (((uint64_t)k_used + 1) * (3ull * ff + embd) + 32ull * embd) *
sizeof(float);
+ /* qwen4exp: a block's residual streams and n-gram rows beside it
+ (model_hc.c janas_m_hc_alloc), some seven rows of hc times the width
+ a token - 81 MB on Qwen3.8-Flash-Next, uncounted until 22 Oct 2026,
+ when the estimate fell 0.27 GiB short of the memory measured */
+ uint32_t hc = 0;
+ snprintf(key, sizeof(key), "%.32s.hyper_connection.count", arch);
+ if (janas_jns_meta_u32(j, key, &hc) == 0 && hc)
+ scratch += blk * (8ull * hc * embd + 4ull * embd) * sizeof(float);
*e = (struct mem_need){.resident = resident,
.experts = experts,
.kv_token = kv_token,
@@ -412,12 +501,14 @@ static void mem_need(const struct janas_jns *j, uint32_t n_ctx, int mtp_file,
.ring = ring,
.attn_part = attn_part,
.scratch = scratch,
- .npu = npu_need(j)};
+ .npu = npu_need(j),
+ .table = table};
}
/* What a model needs whatever its expert cache: the resident weights, the
keys and values of p->n_ctx tokens, the attention's partial results, a
- pass's scratch and the MTP file (with 256 MiB of its own cache). */
+ pass's scratch and the MTP file, which the engine holds whole (its
+ experts' cache takes every one), with 256 MiB beside it. */
static uint64_t mem_fixed(const struct mem_need *e,
const struct janas_llm_params *p)
{
@@ -526,6 +617,28 @@ int32_t janas_llm_open(const char *path, const struct janas_llm_params *pp,
if (cache > e.experts)
cache = e.experts;
}
+ /*
+ * The per-layer table in memory where the machine holds it beside a
+ * cache of every expert - the experts first: a token reads hundreds of
+ * them and a few rows of the table (Qwen3.8-Flash-Next: 480 experts,
+ * 16 rows of 90 bytes) - read a row at a time from the file where the
+ * memory is short (the development laptop's 30 GB). JANAS_PLE_RAM=1
+ * asks for it, the cache giving it the room; 0 never.
+ */
+ int table_ram = 0;
+ if (e.table) {
+ const char *pr = getenv("JANAS_PLE_RAM");
+ /* the NPU's copies only where it stays on */
+ uint64_t avail = janas_mem_available();
+ uint64_t need =
+ mem_fixed(&e, &p) - (no_npu ? e.npu : 0) + reserve + e.table;
+ table_ram = pr ? atoi(pr) > 0
+ : p.cache_bytes == 0 &&
+ avail > need + e.experts + ((uint64_t)1 << 30);
+ if (table_ram && p.cache_bytes == 0 && cache + need > avail)
+ cache = avail > need + ((uint64_t)1 << 30) ? avail - need
+ : (uint64_t)1 << 30;
+ }
/* compute: every usable core (the tuner picks how many per pass);
I/O: the efficiency cores, or unpinned */
int ccpus[256], icpus[256], nc = 0, ni = 0;
@@ -549,7 +662,8 @@ int32_t janas_llm_open(const char *path, const struct janas_llm_params *pp,
.expert_bits = p.expert_bits,
.attn_scores = p.attn_scores,
.head_bits = p.head_bits,
- .no_npu = no_npu};
+ .no_npu = no_npu,
+ .ple_ram = table_ram};
llm->m = janas_llm_model_load(path, &mo, err, sizeof(err));
if (!llm->m) {
janas_tokenizer_destroy(llm->tok);
@@ -620,6 +734,11 @@ int32_t janas_llm_open(const char *path, const struct janas_llm_params *pp,
w += snprintf(bits + w, sizeof(bits) - (size_t)w,
", output head at 4 bits (%.0f -> %.0f MB)", d0 / 1e6,
d1 / 1e6);
+ const char *hq = janas_llm_model_hc_quant(llm->m, &d0, &d1);
+ if (hq && w >= 0 && (size_t)w < sizeof(bits))
+ w += snprintf(bits + w, sizeof(bits) - (size_t)w,
+ ", mixes of the streams %s (%.0f -> %.0f MB)", hq,
+ d0 / 1e6, d1 / 1e6);
int h16 = janas_llm_model_head16(llm->m, &d0, &d1);
if (h16 && w >= 0 && (size_t)w < sizeof(bits))
snprintf(bits + w, sizeof(bits) - (size_t)w,
diff --git a/src/llm/api_chat.c b/src/llm/api_chat.c
index 975b018..d97e64c 100644
--- a/src/llm/api_chat.c
+++ b/src/llm/api_chat.c
@@ -361,12 +361,48 @@ int janas_api_add_markup(janas_llm_chat *c, const char *s, size_t n)
return janas_api_add_text(c, s + from, n - from);
}
+const char *janas_api_think_note(const janas_llm_chat *c)
+{
+ const janas_llm *llm = c->llm;
+ return llm->format == FORMAT_CHATML && llm->thinker && c->p.thinking != 0 &&
+ llm->think_note[0]
+ ? llm->think_note
+ : NULL;
+}
+
/* The system message, with the tools when there are: the dialect's
- template writes them into it. */
+ template writes them into it. While the model reasons, its words on
+ reasoning (janas_api_think_note) at the top, before the tools, as its
+ template writes them: the message trimmed after them. */
int janas_api_add_system(janas_llm_chat *c, const char *sys, size_t n)
{
const janas_llm *llm = c->llm;
c->tools_tokens = 0;
+ const char *note = janas_api_think_note(c);
+ struct janas_buf nb = {0};
+ if (note && llm->format == FORMAT_CHATML) {
+ while (n && sys &&
+ (*sys == ' ' || *sys == '\n' || *sys == '\t' || *sys == '\r'))
+ sys++, n--;
+ while (n && (sys[n - 1] == ' ' || sys[n - 1] == '\n' ||
+ sys[n - 1] == '\t' || sys[n - 1] == '\r'))
+ n--;
+ janas_buf_puts(&nb, note);
+ if (n && (!c->tools_sys || llm->tools == JANAS_TOOLS_NONE)) {
+ janas_buf_puts(&nb, "\n\n");
+ janas_buf_put(&nb, sys, n);
+ }
+ if (nb.oom) {
+ janas_buf_free(&nb);
+ return -1;
+ }
+ if (!c->tools_sys || llm->tools == JANAS_TOOLS_NONE) {
+ int err = janas_api_add_message(c, "system", nb.p, nb.n);
+ janas_buf_free(&nb);
+ return err;
+ }
+ janas_buf_puts(&nb, "\n\n"); /* before the tools */
+ }
if (llm->format == FORMAT_GEMMA) {
/* a system turn only when there is something to say: the message,
or <|think|> at its top when reasoning is asked for */
@@ -392,15 +428,28 @@ int janas_api_add_system(janas_llm_chat *c, const char *sys, size_t n)
/* how many of its tokens the tools take: the message without them is
built and taken back, for the count alone */
size_t ids0 = c->n_ids;
- if (janas_api_flush_text(c) != 0)
+ if (janas_api_flush_text(c) != 0) {
+ janas_buf_free(&nb);
return -1;
+ }
ids0 = c->n_ids;
- if (janas_api_add_message(c, "system", sys ? sys : "", n) != 0 ||
- janas_api_flush_text(c) != 0)
+ /* the words on reasoning are the message's, not the tools' */
+ size_t nn = nb.n;
+ if (n)
+ janas_buf_put(&nb, sys, n);
+ if (nb.oom ||
+ janas_api_add_message(c, "system", nb.p ? nb.p : "", nb.n) != 0 ||
+ janas_api_flush_text(c) != 0) {
+ janas_buf_free(&nb);
return -1;
+ }
+ nb.n = nn;
size_t plain = c->n_ids - ids0;
c->n_ids = ids0;
struct janas_buf b = {0};
+ if (nb.n)
+ janas_buf_put(&b, nb.p, nb.n);
+ janas_buf_free(&nb);
janas_tools_system(&b, llm->tools, c->tools_sys, sys, n);
int err = b.oom || janas_api_add_special(c, llm->im_start) ||
janas_api_add_text(c, "system\n", 7) ||
@@ -551,7 +600,8 @@ int32_t janas_llm_chat_prepare(janas_llm_chat *c, int32_t *done, int32_t *total)
return 0; /* the conversation has started */
c->n_ids = c->n_text = 0;
int err = janas_api_begin_sequence(c);
- if (c->system || c->tools_sys || c->llm->format == FORMAT_GEMMA)
+ if (c->system || c->tools_sys || c->llm->format == FORMAT_GEMMA ||
+ janas_api_think_note(c))
err |= janas_api_add_system(c, c->system,
c->system ? strlen(c->system) : 0);
err |= janas_api_flush_text(c);
@@ -796,7 +846,8 @@ static int32_t send_turn(janas_llm_chat *c, const char *text, size_t n,
} else {
/* the start: <bos> where the format has one, the system message */
err |= janas_api_begin_sequence(c);
- if (c->system || c->tools_sys || c->llm->format == FORMAT_GEMMA)
+ if (c->system || c->tools_sys || c->llm->format == FORMAT_GEMMA ||
+ janas_api_think_note(c))
err |= janas_api_add_system(c, c->system,
c->system ? strlen(c->system) : 0);
c->sys_tokens = (uint32_t)c->n_ids;
diff --git a/src/llm/api_find.c b/src/llm/api_find.c
index 8510316..d06d963 100644
--- a/src/llm/api_find.c
+++ b/src/llm/api_find.c
@@ -22,9 +22,12 @@
#include "llm/chat.h"
#include "jns.h"
-/* no MTP block or assistant is near this size; the models beside one are
- larger, and their headers are not worth reading */
-#define MTP_MAX_BYTES ((uint64_t)2 << 30)
+/* An MTP block or an assistant is a small part of its model - Qwen3.5-2B's
+ 45 MB beside 1.3 GB, Qwen3.8-Flash-Next's 1.9 GB beside 66 GB - so a file
+ larger than a quarter of the model, and than 2 GiB, is another model,
+ whose header is not worth reading. The 2 GiB alone left out the block
+ unsloth recommends for Qwen3.8-Flash-Next, its Q8_0, 2.6 GB. */
+#define MTP_MIN_CAP ((uint64_t)2 << 30)
struct model_shape {
char arch[32];
@@ -74,6 +77,11 @@ int32_t janas_llm_find_mtp(const char *model_path, char *out, int32_t cap)
.n_vocab = vocab_of(&j)};
snprintf(m.arch, sizeof(m.arch), "%.31s", j.h.arch);
janas_jns_close(&j);
+ /* the largest file worth a look */
+ struct stat ms;
+ uint64_t most = MTP_MIN_CAP;
+ if (stat(model_path, &ms) == 0 && (uint64_t)ms.st_size / 4 > most)
+ most = (uint64_t)ms.st_size / 4;
/* the model's directory and its own name */
char dir[4096];
@@ -99,7 +107,7 @@ int32_t janas_llm_find_mtp(const char *model_path, char *out, int32_t cap)
snprintf(path, sizeof(path), "%s/%s", dir, e->d_name);
struct stat st;
if (stat(path, &st) != 0 || !S_ISREG(st.st_mode) ||
- (uint64_t)st.st_size > MTP_MAX_BYTES)
+ (uint64_t)st.st_size > most)
continue;
if (janas_jns_open(path, &j, err, sizeof(err)) != 0)
continue;
diff --git a/src/llm/api_load.c b/src/llm/api_load.c
index 35e9238..9b73da8 100644
--- a/src/llm/api_load.c
+++ b/src/llm/api_load.c
@@ -136,7 +136,7 @@ static int add_ids(janas_llm_chat *c, const int32_t *ids, uint32_t n)
* An assistant's message the model did not write here (or not lately): as
* the template writes it, with its calls. A Qwen3.5 or 3.6 template gives
* the turns after the user's last question their reasoning back, empty
- * when there is none to give.
+ * when there is none to give; a Qwen3.8 one every turn (think_kept).
*/
static int add_assistant(janas_llm_chat *c, const struct cmsg *m, int after_q)
{
@@ -148,7 +148,7 @@ static int add_assistant(janas_llm_chat *c, const struct cmsg *m, int after_q)
err |= janas_api_add_text(c, llm->asst_role, strlen(llm->asst_role));
if (!bare)
err |= janas_api_add_text(c, "\n", 1);
- if (d == JANAS_TOOLS_XML && llm->thinker && after_q)
+ if (d == JANAS_TOOLS_XML && llm->thinker && (after_q || llm->think_kept))
err |= janas_api_add_markup(c, "<think>\n\n</think>\n\n", 19);
if (m->n_calls && d != JANAS_TOOLS_NONE) {
struct janas_buf b = {0};
@@ -210,9 +210,10 @@ static int render(janas_llm_chat *c, uint32_t *reply_off)
if (c->msgs[i].role == JANAS_LLM_ROLE_USER)
last_q = i;
int err = janas_api_begin_sequence(c);
- if (llm->format == FORMAT_GEMMA && !c->tools_sys &&
- c->msgs[0].role != JANAS_LLM_ROLE_SYSTEM)
- err |= janas_api_add_system(c, NULL, 0); /* <|think|>, if asked */
+ if ((llm->format == FORMAT_GEMMA || janas_api_think_note(c)) &&
+ !c->tools_sys && c->msgs[0].role != JANAS_LLM_ROLE_SYSTEM)
+ err |= janas_api_add_system(c, NULL, 0); /* <|think|>, or the words
+ on reasoning, if asked */
if (c->tools_sys && c->msgs[0].role != JANAS_LLM_ROLE_SYSTEM) {
/* with tools there is a system message whatever the caller says */
err |= janas_api_add_system(c, NULL, 0);
diff --git a/src/llm/api_tools.c b/src/llm/api_tools.c
index 543b59e..b0c8dd8 100644
--- a/src/llm/api_tools.c
+++ b/src/llm/api_tools.c
@@ -141,8 +141,16 @@ int32_t janas_llm_chat_tool_choice(janas_llm_chat *c, int32_t choice,
return janas_api_fail(JANAS_LLM_EINVAL, "no tool is called '%.*s'",
(int)janas_api_text_len(name, len), name);
}
+ size_t pn = 0;
+ if (choice == JANAS_LLM_TOOLS_REQUIRED && name) {
+ pn = janas_api_text_len(name, len);
+ if (pn >= sizeof c->tool_prefix)
+ return janas_api_fail(JANAS_LLM_EINVAL, "too long a name");
+ memcpy(c->tool_prefix, name, pn);
+ }
c->tool_choice = choice;
c->tool_only = only;
+ c->tool_prefix_n = pn;
c->parallel = parallel != 0;
return JANAS_LLM_OK;
}
@@ -266,9 +274,11 @@ int janas_api_reply_setup(janas_llm_chat *c)
int only = choice == JANAS_LLM_TOOLS_FUNCTION ? c->tool_only : -1;
if (tools && choice != JANAS_LLM_TOOLS_NONE)
call = llm->tools == JANAS_TOOLS_GEMMA
- ? janas_tools_rule_gemma(b, c->tools, only, llm->call_close,
+ ? janas_tools_rule_gemma(b, c->tools, only, c->tool_prefix,
+ c->tool_prefix_n, llm->call_close,
llm->quote)
: janas_tools_rule(b, j, llm->tools, c->tools, only,
+ c->tool_prefix, c->tool_prefix_n,
llm->call_close);
if (must) {
answer = calls_rule(b, llm, call, c->parallel);
diff --git a/src/llm/arch.c b/src/llm/arch.c
index 1e4beb0..d4bfab1 100644
--- a/src/llm/arch.c
+++ b/src/llm/arch.c
@@ -29,6 +29,13 @@ static const struct janas_arch archs[] = {
.post_attn_norm = 1,
.vmap_mod = 1,
.nextn_in_file = 1},
+ /* Qwen3.8-Flash-Next: qwen35moe's layers inside hyper-connections */
+ {.name = "qwen4exp",
+ .rec = JANAS_REC_Q35,
+ .attn_gate = 1,
+ .shared_expert = 1,
+ .vmap_mod = 1,
+ .hc = 1},
/* its scores are not scaled down, and an eight-bit key moves them: at
eight bits llama.cpp agrees with its own f16 cache at 169 of 200 most
likely tokens */
diff --git a/src/llm/arch.h b/src/llm/arch.h
index 8965b35..05e9e84 100644
--- a/src/llm/arch.h
+++ b/src/llm/arch.h
@@ -36,6 +36,13 @@ struct janas_arch {
weights, V = K where attn_v is missing, scores not
scaled (the q and k norms do it) */
int kv16; /* keys and values kept at sixteen bits, not eight */
+ /* Qwen3.8-Flash-Next (qwen4exp, model_hc.c): hyper-connections - the
+ residual in several streams, mixed into each sublayer's input by a
+ gate of low rank, in place of its norm, and each sublayer's output
+ added to every stream with a weight of its own - n-gram embeddings
+ at the layers the file names, the DeltaNet's output gated by a
+ sigmoid instead of SiLU, and the attention's sparse selection */
+ int hc;
};
/* The architecture called name, or NULL if Janas does not know it. */
diff --git a/src/llm/bf16.c b/src/llm/bf16.c
new file mode 100644
index 0000000..1929279
--- /dev/null
+++ b/src/llm/bf16.c
@@ -0,0 +1,403 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * bf16.c - products of bf16 weights with float vectors (bf16.h): Gemma 4's
+ * per-layer projection (model_ple.c) and Qwen3.8-Flash-Next's mixes of the
+ * streams and n-gram embeddings (model_hc.c).
+ */
+#include "bf16.h"
+
+#include <math.h>
+#include <stdatomic.h>
+#include <string.h>
+
+#include "quant.h"
+#include "vmath.h"
+
+#if defined(__x86_64__)
+#include <immintrin.h>
+
+__attribute__((target("avx2,fma"))) static float
+dot_avx2(const uint16_t *w, const float *x, uint32_t n)
+{
+ __m256 acc = _mm256_setzero_ps();
+ uint32_t i = 0;
+ for (; i + 8 <= n; i += 8) {
+ __m128i h = _mm_loadu_si128((const __m128i *)(const void *)(w + i));
+ __m256 wf = _mm256_castsi256_ps(
+ _mm256_slli_epi32(_mm256_cvtepu16_epi32(h), 16));
+ acc = _mm256_fmadd_ps(wf, _mm256_loadu_ps(x + i), acc);
+ }
+ __m128 s =
+ _mm_add_ps(_mm256_castps256_ps128(acc), _mm256_extractf128_ps(acc, 1));
+ s = _mm_add_ps(s, _mm_movehl_ps(s, s));
+ s = _mm_add_ss(s, _mm_movehdup_ps(s));
+ float r = _mm_cvtss_f32(s);
+ for (; i < n; i++)
+ r += janas_u2f((uint32_t)w[i] << 16) * x[i];
+ return r;
+}
+
+/* four tokens against one row, the row converted once: for each token the
+ same accumulator and the same order as dot_avx2, so the same bits */
+__attribute__((target("avx2,fma"))) static void
+dot4_avx2(const uint16_t *w, const float *const x[4], uint32_t n, float out[4])
+{
+ __m256 acc[4] = {_mm256_setzero_ps(), _mm256_setzero_ps(),
+ _mm256_setzero_ps(), _mm256_setzero_ps()};
+ uint32_t i = 0;
+ for (; i + 8 <= n; i += 8) {
+ __m128i h = _mm_loadu_si128((const __m128i *)(const void *)(w + i));
+ __m256 wf = _mm256_castsi256_ps(
+ _mm256_slli_epi32(_mm256_cvtepu16_epi32(h), 16));
+#pragma GCC unroll 4
+ for (int t = 0; t < 4; t++)
+ acc[t] = _mm256_fmadd_ps(wf, _mm256_loadu_ps(x[t] + i), acc[t]);
+ }
+#pragma GCC unroll 4
+ for (int t = 0; t < 4; t++) {
+ __m128 s = _mm_add_ps(_mm256_castps256_ps128(acc[t]),
+ _mm256_extractf128_ps(acc[t], 1));
+ s = _mm_add_ps(s, _mm_movehl_ps(s, s));
+ s = _mm_add_ss(s, _mm_movehdup_ps(s));
+ float r = _mm_cvtss_f32(s);
+ for (uint32_t k = i; k < n; k++)
+ r += janas_u2f((uint32_t)w[k] << 16) * x[t][k];
+ out[t] = r;
+ }
+}
+
+/*
+ * Rows shorter than this many bytes (a mix's up, 320 numbers) have their
+ * weights asked for this far ahead, into L1: in memory those are the next
+ * rows', which the hardware prefetcher, following each row, did not fetch
+ * in time. 10240 x 320 on 12 threads, from RAM: BF16 104 us a product
+ * without, 92 with 2 KB ahead, 89.5 with 4 KB; Q8_0 66 and 57. Long rows
+ * (a mix's down, 10240 numbers) are fetched in time already, and with it
+ * slow down a little (23 Oct 2026).
+ */
+#define SHORT_ROW_AHEAD 2048
+
+/* four rows, row elements apart, against one vector: each row with
+ dot_avx2's accumulator, order and tail, so the same bits, the four
+ chains of fused multiply-adds side by side; pf bytes ahead (0: none) */
+__attribute__((target("avx2,fma"), always_inline)) static inline void
+dot4r_body(const uint16_t *w, size_t row, const float *x, uint32_t n,
+ float out[4], int pf)
+{
+ __m256 acc[4] = {_mm256_setzero_ps(), _mm256_setzero_ps(),
+ _mm256_setzero_ps(), _mm256_setzero_ps()};
+ uint32_t i = 0;
+ for (; i + 8 <= n; i += 8) {
+ __m256 xv = _mm256_loadu_ps(x + i);
+#pragma GCC unroll 4
+ for (int r = 0; r < 4; r++) {
+ if (pf && (i & 31) == 0) /* a line a row every 32 numbers */
+ _mm_prefetch((const char *)(w + r * row + i) + pf, _MM_HINT_T0);
+ __m128i h = _mm_loadu_si128(
+ (const __m128i *)(const void *)(w + r * row + i));
+ __m256 wf = _mm256_castsi256_ps(
+ _mm256_slli_epi32(_mm256_cvtepu16_epi32(h), 16));
+ acc[r] = _mm256_fmadd_ps(wf, xv, acc[r]);
+ }
+ }
+#pragma GCC unroll 4
+ for (int r = 0; r < 4; r++) {
+ __m128 s = _mm_add_ps(_mm256_castps256_ps128(acc[r]),
+ _mm256_extractf128_ps(acc[r], 1));
+ s = _mm_add_ps(s, _mm_movehl_ps(s, s));
+ s = _mm_add_ss(s, _mm_movehdup_ps(s));
+ float v = _mm_cvtss_f32(s);
+ for (uint32_t k = i; k < n; k++)
+ v += janas_u2f((uint32_t)w[r * row + k] << 16) * x[k];
+ out[r] = v;
+ }
+}
+
+__attribute__((target("avx2,fma"))) static void
+dot4r_avx2(const uint16_t *w, size_t row, const float *x, uint32_t n,
+ float out[4])
+{
+ dot4r_body(w, row, x, n, out, 0);
+}
+
+__attribute__((target("avx2,fma"))) static void
+dot4r_ahead_avx2(const uint16_t *w, size_t row, const float *x, uint32_t n,
+ float out[4])
+{
+ dot4r_body(w, row, x, n, out, SHORT_ROW_AHEAD);
+}
+#endif
+
+/* ------------------------------------------------- Q8_0 x float */
+
+#define Q8W_BLOCK 34 /* fp16 scale + 32 int8 */
+
+static float q8w_scale(const uint8_t *b)
+{
+ uint16_t h;
+ memcpy(&h, b, 2);
+ return janas_fp16_to_fp32(h);
+}
+
+/* the eight lanes added as janas_hsum8 does */
+static float sum8(const float a[8])
+{
+ return ((a[0] + a[4]) + (a[2] + a[6])) + ((a[1] + a[5]) + (a[3] + a[7]));
+}
+
+static float q8w_dot_ref(const uint8_t *w, const float *x, uint32_t n)
+{
+ float acc[8] = {0};
+ for (uint32_t b = 0; b < n / 32; b++, w += Q8W_BLOCK, x += 32) {
+ const int8_t *q = (const int8_t *)(w + 2);
+ float d = q8w_scale(w), bs[8] = {0};
+ for (int g = 0; g < 4; g++)
+ for (int k = 0; k < 8; k++)
+ bs[k] = fmaf((float)q[8 * g + k], x[8 * g + k], bs[k]);
+ for (int k = 0; k < 8; k++)
+ acc[k] = fmaf(bs[k], d, acc[k]);
+ }
+ return sum8(acc);
+}
+
+#if defined(__x86_64__)
+__attribute__((target("avx2,fma"))) static float hsum_avx2(__m256 acc)
+{
+ __m128 s =
+ _mm_add_ps(_mm256_castps256_ps128(acc), _mm256_extractf128_ps(acc, 1));
+ s = _mm_add_ps(s, _mm_movehl_ps(s, s));
+ s = _mm_add_ss(s, _mm_movehdup_ps(s));
+ return _mm_cvtss_f32(s);
+}
+
+/* nr rows w[r] against nx vectors x[t] (nr, nx constants where it is
+ inlined), out[r * nx + t]: each pair the reference's order; the rows and
+ vectors side by side only so that their chains of fused multiply-adds
+ overlap. The scale by F16C, exact as janas_fp16_to_fp32. The loops over
+ rows and vectors unrolled: rolled, GCC kept the accumulators in memory,
+ every fused multiply-add a load and a store, and the kernel ran at a
+ quarter of its speed (40 GB/s from L3 on 12 threads, 23 Oct 2026).
+ pf: SHORT_ROW_AHEAD's prefetch, or 0. */
+__attribute__((target("avx2,fma,f16c"), always_inline)) static inline void
+q8w_kern(const uint8_t *const w[], int nr, const float *const x[], int nx,
+ uint32_t n, float *out, int pf)
+{
+ __m256 acc[4][4];
+#pragma GCC unroll 4
+ for (int r = 0; r < nr; r++)
+#pragma GCC unroll 4
+ for (int t = 0; t < nx; t++)
+ acc[r][t] = _mm256_setzero_ps();
+ for (uint32_t b = 0; b < n / 32; b++) {
+ __m256 d[4], bs[4][4];
+#pragma GCC unroll 4
+ for (int r = 0; r < nr; r++) {
+ uint16_t h;
+ if (pf && (b & 1) == 0) /* a line a row every 68 bytes */
+ _mm_prefetch((const char *)(w[r] + (size_t)b * Q8W_BLOCK) + pf,
+ _MM_HINT_T0);
+ memcpy(&h, w[r] + (size_t)b * Q8W_BLOCK, 2);
+ d[r] = _mm256_set1_ps(_cvtsh_ss(h));
+#pragma GCC unroll 4
+ for (int t = 0; t < nx; t++)
+ bs[r][t] = _mm256_setzero_ps();
+ }
+#pragma GCC unroll 4
+ for (int g = 0; g < 4; g++) {
+ __m256 xv[4];
+#pragma GCC unroll 4
+ for (int t = 0; t < nx; t++)
+ xv[t] = _mm256_loadu_ps(x[t] + 32 * b + 8 * g);
+#pragma GCC unroll 4
+ for (int r = 0; r < nr; r++) {
+ __m128i q8 = _mm_loadl_epi64(
+ (const __m128i *)(const void *)(w[r] +
+ (size_t)b * Q8W_BLOCK + 2 +
+ 8 * g));
+ __m256 qf = _mm256_cvtepi32_ps(_mm256_cvtepi8_epi32(q8));
+#pragma GCC unroll 4
+ for (int t = 0; t < nx; t++)
+ bs[r][t] = _mm256_fmadd_ps(qf, xv[t], bs[r][t]);
+ }
+ }
+#pragma GCC unroll 4
+ for (int r = 0; r < nr; r++)
+#pragma GCC unroll 4
+ for (int t = 0; t < nx; t++)
+ acc[r][t] = _mm256_fmadd_ps(bs[r][t], d[r], acc[r][t]);
+ }
+#pragma GCC unroll 4
+ for (int r = 0; r < nr; r++)
+#pragma GCC unroll 4
+ for (int t = 0; t < nx; t++)
+ out[r * nx + t] = hsum_avx2(acc[r][t]);
+}
+
+__attribute__((target("avx2,fma,f16c"))) static void
+q8w_1x1(const uint8_t *w, const float *x, uint32_t n, float *out)
+{
+ q8w_kern(&w, 1, &x, 1, n, out, 0);
+}
+
+__attribute__((target("avx2,fma,f16c"))) static void
+q8w_1x4(const uint8_t *w, const float *const x[4], uint32_t n, float *out)
+{
+ q8w_kern(&w, 1, x, 4, n, out, 0);
+}
+
+__attribute__((target("avx2,fma,f16c"))) static void
+q8w_4x1(const uint8_t *const w[4], const float *x, uint32_t n, float *out)
+{
+ q8w_kern(w, 4, &x, 1, n, out, 0);
+}
+
+__attribute__((target("avx2,fma,f16c"))) static void
+q8w_4x1_ahead(const uint8_t *const w[4], const float *x, uint32_t n, float *out)
+{
+ q8w_kern(w, 4, &x, 1, n, out, SHORT_ROW_AHEAD);
+}
+
+/* AVX2, FMA and F16C, asked once; atomic, as every thread of a product
+ may be the first to ask */
+static int q8w_lanes(void)
+{
+ static atomic_int lanes = -1;
+ int l = atomic_load_explicit(&lanes, memory_order_relaxed);
+ if (l < 0) {
+ l = janas_bf16_lanes() && __builtin_cpu_supports("f16c");
+ atomic_store_explicit(&lanes, l, memory_order_relaxed);
+ }
+ return l;
+}
+#endif
+
+float janas_q8w_dot(const uint8_t *w, const float *x, uint32_t n)
+{
+#if defined(__x86_64__)
+ if (q8w_lanes()) {
+ float o;
+ q8w_1x1(w, x, n, &o);
+ return o;
+ }
+#endif
+ return q8w_dot_ref(w, x, n);
+}
+
+void janas_q8w_dot4(const uint8_t *w, const float *const x[4], uint32_t n,
+ float out[4])
+{
+#if defined(__x86_64__)
+ if (q8w_lanes()) {
+ q8w_1x4(w, x, n, out);
+ return;
+ }
+#endif
+ for (int t = 0; t < 4; t++)
+ out[t] = q8w_dot_ref(w, x[t], n);
+}
+
+void janas_q8w_dot4r(const uint8_t *w, size_t row, const float *x, uint32_t n,
+ float out[4])
+{
+#if defined(__x86_64__)
+ if (q8w_lanes()) {
+ const uint8_t *ws[4] = {w, w + row, w + 2 * row, w + 3 * row};
+ if (row < SHORT_ROW_AHEAD)
+ q8w_4x1_ahead(ws, x, n, out);
+ else
+ q8w_4x1(ws, x, n, out);
+ return;
+ }
+#endif
+ for (int r = 0; r < 4; r++)
+ out[r] = q8w_dot_ref(w + r * row, x, n);
+}
+
+#if defined(__x86_64__)
+__attribute__((target("avx2"))) static size_t
+round_bf16_avx2(const float *x, float *xb, size_t n)
+{
+ const __m256i one = _mm256_set1_epi32(1), half = _mm256_set1_epi32(0x7fff),
+ hi = _mm256_set1_epi32((int)0xffff0000u),
+ abs = _mm256_set1_epi32(0x7fffffff),
+ inf = _mm256_set1_epi32(0x7f800000);
+ size_t i = 0;
+ for (; i + 8 <= n; i += 8) {
+ __m256i u = _mm256_loadu_si256((const __m256i *)(const void *)(x + i));
+ __m256i lsb = _mm256_and_si256(_mm256_srli_epi32(u, 16), one);
+ __m256i r = _mm256_and_si256(
+ _mm256_add_epi32(u, _mm256_add_epi32(half, lsb)), hi);
+ __m256i nan = _mm256_cmpgt_epi32(_mm256_and_si256(u, abs), inf);
+ _mm256_storeu_si256((__m256i *)(void *)(xb + i),
+ _mm256_blendv_epi8(r, u, nan));
+ }
+ return i;
+}
+#endif
+
+void janas_round_bf16_n(const float *x, float *xb, size_t n)
+{
+ size_t i = 0;
+#if defined(__x86_64__)
+ if (__builtin_cpu_supports("avx2"))
+ i = round_bf16_avx2(x, xb, n);
+#endif
+ for (; i < n; i++) {
+ uint32_t u;
+ memcpy(&u, x + i, 4);
+ if ((u & 0x7fffffffu) <= 0x7f800000u) /* not a NaN */
+ u = (u + (0x7fffu + ((u >> 16) & 1u))) & 0xffff0000u;
+ memcpy(xb + i, &u, 4);
+ }
+}
+
+int janas_bf16_lanes(void)
+{
+#if defined(__x86_64__)
+ return __builtin_cpu_supports("avx2") && __builtin_cpu_supports("fma");
+#else
+ return 0;
+#endif
+}
+
+float janas_bf16_dot(const uint16_t *w, const float *x, uint32_t n)
+{
+#if defined(__x86_64__)
+ if (__builtin_cpu_supports("avx2") && __builtin_cpu_supports("fma"))
+ return dot_avx2(w, x, n);
+#endif
+ float r = 0.0f;
+ for (uint32_t i = 0; i < n; i++)
+ r += janas_u2f((uint32_t)w[i] << 16) * x[i];
+ return r;
+}
+
+void janas_bf16_dot4r(const uint16_t *w, size_t row, const float *x, uint32_t n,
+ float out[4])
+{
+#if defined(__x86_64__)
+ if (janas_bf16_lanes()) {
+ if (row * sizeof(uint16_t) < SHORT_ROW_AHEAD)
+ dot4r_ahead_avx2(w, row, x, n, out);
+ else
+ dot4r_avx2(w, row, x, n, out);
+ return;
+ }
+#endif
+ for (int r = 0; r < 4; r++)
+ out[r] = janas_bf16_dot(w + r * row, x, n);
+}
+
+void janas_bf16_dot4(const uint16_t *w, const float *const x[4], uint32_t n,
+ float out[4])
+{
+#if defined(__x86_64__)
+ if (janas_bf16_lanes()) {
+ dot4_avx2(w, x, n, out);
+ return;
+ }
+#endif
+ for (int t = 0; t < 4; t++)
+ out[t] = janas_bf16_dot(w, x[t], n);
+}
diff --git a/src/llm/bf16.h b/src/llm/bf16.h
new file mode 100644
index 0000000..423c0c9
--- /dev/null
+++ b/src/llm/bf16.h
@@ -0,0 +1,57 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * bf16.h - products of bf16 weights with float vectors, as the CPU computes
+ * them: with AVX2 and FMA in eight lanes, each the fused multiply-adds of
+ * its own elements (8k + lane) in order, then the eight added as
+ * janas_hsum8 does, ((a0 + a4) + (a2 + a6)) + ((a1 + a5) + (a3 + a7)), and
+ * the elements past the last eight multiplied and added one at a time;
+ * without them one at a time throughout. The GPU computes the eight lanes'
+ * way bit for bit (bf16_matvec.comp, gpu.h janas_gpu_bf16_mv): only where
+ * the CPU does too (janas_bf16_lanes).
+ */
+#ifndef JANAS_LLM_BF16_H
+#define JANAS_LLM_BF16_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+/* w . x over n elements, w as bf16 bits (the high half of a float) */
+float janas_bf16_dot(const uint16_t *w, const float *x, uint32_t n);
+
+/* The same for four vectors against one row, read and converted once: for
+ each vector the same bits as janas_bf16_dot. */
+void janas_bf16_dot4(const uint16_t *w, const float *const x[4], uint32_t n,
+ float out[4]);
+/* four rows, row elements apart from w, against one vector: out[r] the bits
+ of janas_bf16_dot(w + r * row, x, n) */
+void janas_bf16_dot4r(const uint16_t *w, size_t row, const float *x, uint32_t n,
+ float out[4]);
+
+/* xb[i] = x[i] rounded to bf16 as ggml does (to nearest, ties to even, a
+ NaN kept), kept as a float; xb may be x */
+void janas_round_bf16_n(const float *x, float *xb, size_t n);
+
+/* 1 where the products take the eight lanes (AVX2 and FMA), 0 where they
+ go one element at a time. */
+int janas_bf16_lanes(void);
+
+/*
+ * Q8_0 weights (blocks of 32: an fp16 scale, 32 int8) with float vectors,
+ * the vectors kept as they are, not quantized: Qwen3.8-Flash-Next's mixes
+ * rewritten at load (model_hc_quant.c), whose inputs at eight bits cost
+ * what their weights at eight bits do not. Eight lanes, lane k the
+ * elements 8i + k: in each block the fused multiply-adds of its four, then
+ * that times the block's scale fused into the lane's sum; the eight added
+ * as above. The same bits with AVX2 or without. n a multiple of 32; w is
+ * n / 32 blocks of 34 bytes.
+ */
+float janas_q8w_dot(const uint8_t *w, const float *x, uint32_t n);
+void janas_q8w_dot4(const uint8_t *w, const float *const x[4], uint32_t n,
+ float out[4]);
+/* four rows, row bytes apart from w, against one vector: out[r] the bits
+ of janas_q8w_dot(w + r * row, x, n) */
+void janas_q8w_dot4r(const uint8_t *w, size_t row, const float *x, uint32_t n,
+ float out[4]);
+
+#endif
diff --git a/src/llm/chat.h b/src/llm/chat.h
index c5ac24b..573d6ea 100644
--- a/src/llm/chat.h
+++ b/src/llm/chat.h
@@ -38,7 +38,15 @@ struct janas_llm {
int32_t think_on; /* Gemma's <|think|>: reasoning asked for, or -1 */
int32_t quote, decl_open, decl_close; /* Gemma's <|"|>, <|tool>, <tool|> */
int32_t think_open, think_close; /* reasoning markers, or -1 */
- int thinker; /* the template knows enable_thinking */
+ int thinker; /* the template knows enable_thinking */
+ /* the template's words at the top of the system message while the
+ model reasons, at its default effort (Qwen3.8: "Reasoning effort is
+ set to xhigh. ..."); empty where it has none */
+ char think_note[512];
+ /* the template gives every earlier turn of the assistant its reasoning
+ back (Qwen3.8: preserve_thinking unless told otherwise), not only
+ those after the user's last question (Qwen3.5 and 3.6) */
+ int think_kept;
int empty_thought; /* Gemma 4: an empty channel when not reasoning */
enum janas_tool_dialect tools; /* how its template writes tool calls */
int32_t call_open, call_close; /* <tool_call>, </tool_call>, or -1 */
@@ -165,6 +173,9 @@ struct janas_llm_chat {
struct janas_toolset *tools, *tools_sys;
char *tools_json, *more_json;
int32_t tool_choice, tool_only, parallel;
+ char tool_prefix[128]; /* REQUIRED with a name: the calls' names begin
+ with it (tool_prefix_n bytes; 0: any) */
+ size_t tool_prefix_n;
int32_t format;
struct janas_json_doc *schema;
struct janas_grammar *g; /* of the reply being read, with ... */
@@ -244,6 +255,10 @@ int janas_api_add_special(janas_llm_chat *c, int32_t id);
int janas_api_add_message(janas_llm_chat *c, const char *role, const char *text,
size_t n);
int janas_api_add_system(janas_llm_chat *c, const char *sys, size_t n);
+/* The model's words on reasoning for the system message's top
+ (janas_llm.think_note) when this chat's replies reason, else NULL: where
+ it is not NULL there is a system message whatever the caller says. */
+const char *janas_api_think_note(const janas_llm_chat *c);
int janas_api_start_reply(janas_llm_chat *c);
int janas_api_begin_sequence(janas_llm_chat *c);
int janas_api_note_turn(janas_llm_chat *c, uint32_t at);
diff --git a/src/llm/expert_cache.c b/src/llm/expert_cache.c
index b9b41dc..6ec1e83 100644
--- a/src/llm/expert_cache.c
+++ b/src/llm/expert_cache.c
@@ -1099,24 +1099,20 @@ int janas_expert_cache_can_prefetch(const struct janas_expert_cache *c)
return c->pf_ring[0] != NULL;
}
-int janas_expert_cache_begin(struct janas_expert_cache *c, uint32_t layer,
- const uint32_t *ids, size_t n,
- const uint8_t **slots, uint8_t *ready)
+/* begin, and with same set the request before's still: its slots are not
+ taken for this one's (victim) */
+static int begin_req(struct janas_expert_cache *c, uint32_t layer,
+ const uint32_t *ids, size_t n, const uint8_t **slots,
+ uint8_t *ready, int same)
{
- if (c->wide && layer >= c->n_layer) {
- if (c->outstanding || c->asked_wide)
- return -1;
- int rc = janas_expert_cache_begin(c->wide, layer, ids, n, slots, ready);
- c->asked_wide = rc >= 0;
- return rc;
- }
if (layer >= c->n_layer || n > c->n_slots || n > 256 || c->outstanding ||
c->asked_wide)
return -1;
/* held until finish: the background filling waits for its turn */
pthread_mutex_lock(&c->api_lock);
c->last_request = now();
- c->request++;
+ if (!same)
+ c->request++;
/* a layer before the last one asked: a new pass */
if (layer < c->cur_layer) {
c->last_pass_asked = c->pass_asked;
@@ -1178,13 +1174,28 @@ int janas_expert_cache_begin(struct janas_expert_cache *c, uint32_t layer,
}
c->st.requests += n;
for (size_t i = 0; i < n; i++)
- slots[i] = c->arena + (size_t)s[i] * c->slot_bytes;
+ slots[i] =
+ s[i] != NONE ? c->arena + (size_t)s[i] * c->slot_bytes : NULL;
c->outstanding = 1;
if (start_jobs(c) != 0)
c->batch_rc = -1; /* finish() reports it, and waits all the same */
return queued;
}
+int janas_expert_cache_begin(struct janas_expert_cache *c, uint32_t layer,
+ const uint32_t *ids, size_t n,
+ const uint8_t **slots, uint8_t *ready)
+{
+ if (c->wide && layer >= c->n_layer) {
+ if (c->outstanding || c->asked_wide)
+ return -1;
+ int rc = janas_expert_cache_begin(c->wide, layer, ids, n, slots, ready);
+ c->asked_wide = rc >= 0;
+ return rc;
+ }
+ return begin_req(c, layer, ids, n, slots, ready, 0);
+}
+
void janas_expert_cache_present(struct janas_expert_cache *c, uint32_t layer,
const uint32_t *ids, size_t n, uint8_t *in)
{
@@ -1229,6 +1240,29 @@ int janas_expert_cache_fetch(struct janas_expert_cache *c, uint32_t layer,
return janas_expert_cache_finish(c);
}
+int janas_expert_cache_fetch_many(struct janas_expert_cache *c, uint32_t layer,
+ const uint32_t *ids, size_t n,
+ const uint8_t **slots)
+{
+ if (c->wide && layer >= c->n_layer)
+ return janas_expert_cache_fetch_many(c->wide, layer, ids, n, slots);
+ /* room besides the slots read ahead, which a request never takes */
+ if (2 * n > c->n_slots)
+ return -1;
+ uint8_t ready[256];
+ for (size_t i = 0; i < n; i += 256) {
+ size_t k = n - i < 256 ? n - i : 256;
+ if (begin_req(c, layer, ids + i, k, slots + i, ready, i > 0) < 0)
+ return -1;
+ if (janas_expert_cache_finish(c) != 0)
+ return -1;
+ }
+ for (size_t i = 0; i < n; i++)
+ if (!slots[i])
+ return -1;
+ return 0;
+}
+
void janas_expert_cache_stats(const struct janas_expert_cache *c,
struct janas_expert_cache_stats *s)
{
diff --git a/src/llm/expert_cache.h b/src/llm/expert_cache.h
index 6e194bc..cb4a145 100644
--- a/src/llm/expert_cache.h
+++ b/src/llm/expert_cache.h
@@ -165,6 +165,13 @@ void janas_expert_cache_warm_progress(const struct janas_expert_cache *c,
int janas_expert_cache_fetch(struct janas_expert_cache *c, uint32_t layer,
const uint32_t *ids, size_t n,
const uint8_t **slots);
+/* The same for more than 256 experts (a prompt's block on the GPU asks for
+ up to all of a layer's), up to half the slots: requests of 256 that count
+ as one, so that none takes a slot an earlier one gave. 0, or -1 (a slot
+ not given, or I/O error). */
+int janas_expert_cache_fetch_many(struct janas_expert_cache *c, uint32_t layer,
+ const uint32_t *ids, size_t n,
+ const uint8_t **slots);
/*
* The same request in two steps, so that computing on the experts already in
diff --git a/src/llm/generate.c b/src/llm/generate.c
index 9460aeb..2e7f737 100644
--- a/src/llm/generate.c
+++ b/src/llm/generate.c
@@ -215,7 +215,8 @@ static int mtp_step(struct janas_llm_model *m, const int32_t *hist,
float min_conf, struct planner *pl, float *hrow,
int32_t *draft, float *survs, int *n_draft)
{
- uint32_t dm = janas_llm_model_n_embd(m);
+ /* a row of hidden state: d_model, or qwen4exp's streams */
+ uint32_t dm = janas_llm_model_hidden_width(m);
*n_draft = 0;
janas_llm_mtp_note_tokens(m, hist + pos + 1, acc + 1);
if (!drafting)
@@ -442,7 +443,7 @@ janas_llm_session_create(struct janas_llm_model *m,
s->cap = janas_llm_model_n_ctx(m);
s->hist = malloc((s->cap + MAX_SPEC_K + 2) * sizeof(int32_t));
s->logits = malloc((size_t)(MAX_SPEC_K + 1) * s->nv * sizeof(float));
- s->hrow = malloc(janas_llm_model_n_embd(m) * sizeof(float));
+ s->hrow = malloc(janas_llm_model_hidden_width(m) * sizeof(float));
s->smp = janas_sampler_create(s->nv);
if (o->draft)
s->dlogits = malloc((size_t)s->nv * sizeof(float));
diff --git a/src/llm/gguf.c b/src/llm/gguf.c
index 5d98d8a..a054731 100644
--- a/src/llm/gguf.c
+++ b/src/llm/gguf.c
@@ -32,7 +32,9 @@ int janas_gguf_type(uint32_t type, uint64_t *blck, uint64_t *size)
{6, 32, 22}, {7, 32, 24}, {8, 32, 34}, {10, 256, 84},
{11, 256, 110}, {12, 256, 144}, {13, 256, 176}, {14, 256, 210},
{15, 256, 292}, {20, 32, 18}, {21, 256, 110}, {23, 256, 136},
- {30, 1, 2}, {39, 32, 17},
+ {30, 1, 2}, {39, 32, 17}, {42, 64, 18}, {18, 256, 98},
+ {16, 256, 66}, {17, 256, 74}, {22, 256, 82}, {29, 256, 56},
+ {19, 256, 50},
};
for (size_t i = 0; i < sizeof(t) / sizeof(t[0]); i++)
if (t[i].type == type) {
diff --git a/src/llm/gpu.c b/src/llm/gpu.c
index 35e5d47..bf4a5e6 100644
--- a/src/llm/gpu.c
+++ b/src/llm/gpu.c
@@ -18,40 +18,48 @@
#include "gpu_impl.h"
-#include "q4k_matvec.h" /* janas_spv_q4k_matvec, generated by build.sh */
-#include "q6k_matvec.h" /* janas_spv_q6k_matvec */
-#include "q6kp_matvec.h" /* janas_spv_q6kp_matvec */
-#include "q5k_matvec.h" /* janas_spv_q5k_matvec */
-#include "q8_0_matvec.h" /* janas_spv_q8_0_matvec */
-#include "b32_matvec.h" /* janas_spv_b32_matvec */
-#include "iq4xs_matvec.h" /* janas_spv_iq4xs_matvec */
-#include "q3k_matvec.h" /* janas_spv_q3k_matvec */
-#include "q4k_mmq.h" /* janas_spv_q4k_mmq */
-#include "q6kp_mmq.h" /* janas_spv_q6kp_mmq */
-#include "q6k_mmq.h" /* janas_spv_q6k_mmq */
-#include "q3k_mmq.h" /* janas_spv_q3k_mmq */
-#include "iq4xs_mmq.h" /* janas_spv_iq4xs_mmq */
-#include "iq3s_mmq.h" /* janas_spv_iq3s_mmq */
-#include "iq4nl_mmq.h" /* janas_spv_iq4nl_mmq */
-#include "mxfp4_mmid.h" /* janas_spv_mxfp4_mmid */
-#include "q80_mmq.h" /* janas_spv_q80_mmq */
-#include "q5k_mmq.h" /* janas_spv_q5k_mmq */
-#include "dense_mm.h" /* janas_spv_dense_mm */
-#include "q3k_mmid.h" /* janas_spv_q3k_mmid */
-#include "q4k_mmid.h" /* janas_spv_q4k_mmid */
-#include "q6kp_mmid.h" /* janas_spv_q6kp_mmid */
-#include "q6k_mmid.h" /* janas_spv_q6k_mmid */
-#include "iq3s_matvec.h" /* janas_spv_iq3s_matvec */
-#include "expert_act.h" /* janas_spv_expert_act */
-#include "q4k_row.h" /* janas_spv_q4k_row */
-#include "q6k_row.h" /* janas_spv_q6k_row */
-#include "q6kp_row.h" /* janas_spv_q6kp_row */
-#include "q6ka_row.h" /* janas_spv_q6ka_row */
-#include "q4k_mv.h" /* janas_spv_q4k_mv */
-#include "q4k_mvd.h" /* janas_spv_q4k_mvd */
-#include "q6k_mvd.h" /* janas_spv_q6k_mvd */
-#include "q5k_mvd.h" /* janas_spv_q5k_mvd */
-#include "iq4xs_mvd.h" /* janas_spv_iq4xs_mvd */
+#include "q4k_matvec.h" /* janas_spv_q4k_matvec, generated by build.sh */
+#include "q6k_matvec.h" /* janas_spv_q6k_matvec */
+#include "q6kp_matvec.h" /* janas_spv_q6kp_matvec */
+#include "q5k_matvec.h" /* janas_spv_q5k_matvec */
+#include "q8_0_matvec.h" /* janas_spv_q8_0_matvec */
+#include "b32_matvec.h" /* janas_spv_b32_matvec */
+#include "iq4xs_matvec.h" /* janas_spv_iq4xs_matvec */
+#include "q3k_matvec.h" /* janas_spv_q3k_matvec */
+#include "q4k_mmq.h" /* janas_spv_q4k_mmq */
+#include "q6kp_mmq.h" /* janas_spv_q6kp_mmq */
+#include "q6k_mmq.h" /* janas_spv_q6k_mmq */
+#include "q3k_mmq.h" /* janas_spv_q3k_mmq */
+#include "iq4xs_mmq.h" /* janas_spv_iq4xs_mmq */
+#include "iq3s_mmq.h" /* janas_spv_iq3s_mmq */
+#include "iq4nl_mmq.h" /* janas_spv_iq4nl_mmq */
+#include "mxfp4_mmid.h" /* janas_spv_mxfp4_mmid */
+#include "b32_mmid.h" /* janas_spv_b32_mmid */
+#include "bf16_matvec.h" /* janas_spv_bf16_matvec */
+#include "iq1_matvec.h" /* janas_spv_iq1_matvec */
+#include "iq2_matvec.h" /* janas_spv_iq2_matvec */
+#include "iq3xxs_matvec.h" /* janas_spv_iq3xxs_matvec */
+#include "iqk_mmid.h" /* janas_spv_iqk_mmid */
+#include "iqk_mmq.h" /* janas_spv_iqk_mmq */
+#include "q2k_matvec.h" /* janas_spv_q2k_matvec */
+#include "q80_mmq.h" /* janas_spv_q80_mmq */
+#include "q5k_mmq.h" /* janas_spv_q5k_mmq */
+#include "dense_mm.h" /* janas_spv_dense_mm */
+#include "q3k_mmid.h" /* janas_spv_q3k_mmid */
+#include "q4k_mmid.h" /* janas_spv_q4k_mmid */
+#include "q6kp_mmid.h" /* janas_spv_q6kp_mmid */
+#include "q6k_mmid.h" /* janas_spv_q6k_mmid */
+#include "iq3s_matvec.h" /* janas_spv_iq3s_matvec */
+#include "expert_act.h" /* janas_spv_expert_act */
+#include "q4k_row.h" /* janas_spv_q4k_row */
+#include "q6k_row.h" /* janas_spv_q6k_row */
+#include "q6kp_row.h" /* janas_spv_q6kp_row */
+#include "q6ka_row.h" /* janas_spv_q6ka_row */
+#include "q4k_mv.h" /* janas_spv_q4k_mv */
+#include "q4k_mvd.h" /* janas_spv_q4k_mvd */
+#include "q6k_mvd.h" /* janas_spv_q6k_mvd */
+#include "q5k_mvd.h" /* janas_spv_q5k_mvd */
+#include "iq4xs_mvd.h" /* janas_spv_iq4xs_mvd */
static double now(void)
{
@@ -427,6 +435,8 @@ uint32_t janas_g_make_pipeline_sg(struct janas_gpu *g, const uint32_t *code,
struct janas_gpu *janas_gpu_create(char *err, size_t err_len)
{
struct janas_gpu *g = calloc(1, sizeof(*g));
+ if (g)
+ g->pass_n = UINT32_MAX;
/* vectors a product's workgroup takes (JANAS_GPU_NV, being measured:
with 8 the compiler spills the products' registers) */
if (g) {
@@ -591,6 +601,9 @@ struct janas_gpu *janas_gpu_create(char *err, size_t err_len)
(r = make_pipeline(g, janas_spv_dense_mm, sizeof(janas_spv_dense_mm),
&g->sm_dm[1], &g->pipe_dm[1], 1, NV_PER_GROUP,
g->sg)) != VK_SUCCESS ||
+ (r = make_pipeline(g, janas_spv_dense_mm, sizeof(janas_spv_dense_mm),
+ &g->sm_dm[2], &g->pipe_dm[2], 2, NV_PER_GROUP,
+ g->sg)) != VK_SUCCESS ||
(r = make_pipeline(g, janas_spv_q4k_mmid, sizeof(janas_spv_q4k_mmid),
&g->sm_id4, &g->pipe_id4, 0, NV_PER_GROUP, g->sg)) !=
VK_SUCCESS ||
@@ -612,6 +625,68 @@ struct janas_gpu *janas_gpu_create(char *err, size_t err_len)
&g->sm_id3, &g->pipe_id3, 0, NV_PER_GROUP,
g->sg) != VK_SUCCESS)
g->pipe_id3 = VK_NULL_HANDLE;
+ /* qwen4exp's types for the fast prompt (Q5_0 and Q2_0 tiles, iq4nl_mmq
+ KIND 2 and 3) and its experts' (b32_mmid KIND 0, 2, 3: IQ4_NL, Q5_0,
+ Q2_0): without them those products stay with the CPU */
+ if (make_pipeline(g, janas_spv_iq4nl_mmq, sizeof(janas_spv_iq4nl_mmq),
+ &g->sm_mm50, &g->pipe_mm50, 2, NV_PER_GROUP,
+ g->sg) != VK_SUCCESS)
+ g->pipe_mm50 = VK_NULL_HANDLE;
+ if (make_pipeline(g, janas_spv_iq4nl_mmq, sizeof(janas_spv_iq4nl_mmq),
+ &g->sm_mm20, &g->pipe_mm20, 3, NV_PER_GROUP,
+ g->sg) != VK_SUCCESS)
+ g->pipe_mm20 = VK_NULL_HANDLE;
+ for (int i = 0; i < 3; i++)
+ if (make_pipeline(g, janas_spv_b32_mmid, sizeof(janas_spv_b32_mmid),
+ &g->sm_idb[i], &g->pipe_idb[i],
+ i ? (uint32_t)i + 1 : 0, NV_PER_GROUP,
+ g->sg) != VK_SUCCESS)
+ g->pipe_idb[i] = VK_NULL_HANDLE;
+ /* the 256-block types of Qwen3.8-Flash-Next's other files and of
+ unsloth's (iqk_index), bit for bit with the CPU: without one, its
+ products stay with the CPU */
+ {
+ const struct {
+ const uint32_t *code;
+ size_t bytes;
+ uint32_t kind;
+ } iqk[N_IQK] = {
+ {janas_spv_q2k_matvec, sizeof(janas_spv_q2k_matvec), 0},
+ {janas_spv_iq2_matvec, sizeof(janas_spv_iq2_matvec), 0},
+ {janas_spv_iq2_matvec, sizeof(janas_spv_iq2_matvec), 1},
+ {janas_spv_iq2_matvec, sizeof(janas_spv_iq2_matvec), 2},
+ {janas_spv_iq3xxs_matvec, sizeof(janas_spv_iq3xxs_matvec), 0},
+ {janas_spv_iq1_matvec, sizeof(janas_spv_iq1_matvec), 0},
+ {janas_spv_iq1_matvec, sizeof(janas_spv_iq1_matvec), 1}};
+ for (int i = 0; i < N_IQK; i++) {
+ if (janas_g_make_pipeline(g, iqk[i].code, iqk[i].bytes,
+ &g->sm_iqk[i], &g->pipe_iqk[i],
+ iqk[i].kind) != VK_SUCCESS)
+ g->pipe_iqk[i] = VK_NULL_HANDLE;
+ if (janas_g_make_pipeline_nv(g, iqk[i].code, iqk[i].bytes,
+ &g->sm1_iqk[i], &g->pp1_iqk[i],
+ iqk[i].kind, 1) != VK_SUCCESS)
+ g->pp1_iqk[i] = VK_NULL_HANDLE;
+ }
+ /* their tiles for the fast prompt */
+ for (int i = 0; i < N_IQK; i++)
+ if (make_pipeline(g, janas_spv_iqk_mmq, sizeof(janas_spv_iqk_mmq),
+ &g->sm_iqq[i], &g->pipe_iqq[i], (uint32_t)i,
+ NV_PER_GROUP, g->sg) != VK_SUCCESS)
+ g->pipe_iqq[i] = VK_NULL_HANDLE;
+ /* their experts' tiles, and IQ3_S's and IQ4_XS's (kinds 7, 8) */
+ for (int i = 0; i < N_IQK + 2; i++)
+ if (make_pipeline(g, janas_spv_iqk_mmid, sizeof(janas_spv_iqk_mmid),
+ &g->sm_iqm[i], &g->pipe_iqm[i], (uint32_t)i,
+ NV_PER_GROUP, g->sg) != VK_SUCCESS)
+ g->pipe_iqm[i] = VK_NULL_HANDLE;
+ }
+ /* bf16 products bit for bit (janas_gpu_bf16_mv): a row's eight lanes
+ in one subgroup, so subgroups of a multiple of 8 */
+ if (g->sg % 8 != 0 ||
+ make_pipeline(g, janas_spv_bf16_matvec, sizeof(janas_spv_bf16_matvec),
+ &g->sm_b16, &g->pipe_b16, 0, B16_NV, g->sg) != VK_SUCCESS)
+ g->pipe_b16 = VK_NULL_HANDLE;
/* each product shader made for a single vector too */
{
const struct {
@@ -631,7 +706,9 @@ struct janas_gpu *janas_gpu_create(char *err, size_t err_len)
{janas_spv_iq4xs_matvec, sizeof(janas_spv_iq4xs_matvec), 0},
{janas_spv_q3k_matvec, sizeof(janas_spv_q3k_matvec), 0},
{janas_spv_iq3s_matvec, sizeof(janas_spv_iq3s_matvec), 0},
- {janas_spv_b32_matvec, sizeof(janas_spv_b32_matvec), 4}};
+ {janas_spv_b32_matvec, sizeof(janas_spv_b32_matvec), 4},
+ {janas_spv_b32_matvec, sizeof(janas_spv_b32_matvec), 5},
+ {janas_spv_b32_matvec, sizeof(janas_spv_b32_matvec), 6}};
for (int i = 0; i < N_PROD; i++)
if ((r = janas_g_make_pipeline_nv(g, one[i].code, one[i].bytes,
&g->pm1[i], &g->pp1[i],
@@ -788,6 +865,12 @@ void janas_gpu_destroy(struct janas_gpu *g)
if (g->verbose)
fprintf(stderr, "gpu begin: convert %.3f s, submit %.3f s\n", g->t_conv,
g->t_sub);
+ if (g->verbose && g->b16_calls)
+ fprintf(stderr,
+ "gpu bf16: %llu products bit for bit, %llu through the "
+ "stages\n",
+ (unsigned long long)g->b16_calls,
+ (unsigned long long)g->b16_staged);
/* the key: (group's products << 48) ^ (rows << 24) ^ (columns << 8) ^
vectors, of its first product. Calls with every row on the GPU (the
fast prompt's) apart: there the CPU waits having nothing to do, and
@@ -920,11 +1003,51 @@ void janas_gpu_destroy(struct janas_gpu *g)
g->vkDestroyPipeline(g->dev, g->pipe_mmmx, NULL);
if (g->pipe_mm80)
g->vkDestroyPipeline(g->dev, g->pipe_mm80, NULL);
+ if (g->pipe_mm50)
+ g->vkDestroyPipeline(g->dev, g->pipe_mm50, NULL);
+ if (g->sm_mm50)
+ g->vkDestroyShaderModule(g->dev, g->sm_mm50, NULL);
+ if (g->pipe_mm20)
+ g->vkDestroyPipeline(g->dev, g->pipe_mm20, NULL);
+ if (g->sm_mm20)
+ g->vkDestroyShaderModule(g->dev, g->sm_mm20, NULL);
+ for (int i = 0; i < 3; i++) {
+ if (g->pipe_idb[i])
+ g->vkDestroyPipeline(g->dev, g->pipe_idb[i], NULL);
+ if (g->sm_idb[i])
+ g->vkDestroyShaderModule(g->dev, g->sm_idb[i], NULL);
+ }
+ if (g->pipe_b16)
+ g->vkDestroyPipeline(g->dev, g->pipe_b16, NULL);
+ if (g->sm_b16)
+ g->vkDestroyShaderModule(g->dev, g->sm_b16, NULL);
+ for (int i = 0; i < N_IQK; i++) {
+ if (g->pipe_iqq[i])
+ g->vkDestroyPipeline(g->dev, g->pipe_iqq[i], NULL);
+ if (g->sm_iqq[i])
+ g->vkDestroyShaderModule(g->dev, g->sm_iqq[i], NULL);
+ }
+ for (int i = 0; i < N_IQK + 2; i++) {
+ if (g->pipe_iqm[i])
+ g->vkDestroyPipeline(g->dev, g->pipe_iqm[i], NULL);
+ if (g->sm_iqm[i])
+ g->vkDestroyShaderModule(g->dev, g->sm_iqm[i], NULL);
+ }
+ for (int i = 0; i < N_IQK; i++) {
+ if (g->pipe_iqk[i])
+ g->vkDestroyPipeline(g->dev, g->pipe_iqk[i], NULL);
+ if (g->sm_iqk[i])
+ g->vkDestroyShaderModule(g->dev, g->sm_iqk[i], NULL);
+ if (g->pp1_iqk[i])
+ g->vkDestroyPipeline(g->dev, g->pp1_iqk[i], NULL);
+ if (g->sm1_iqk[i])
+ g->vkDestroyShaderModule(g->dev, g->sm1_iqk[i], NULL);
+ }
if (g->pipe_mm5)
g->vkDestroyPipeline(g->dev, g->pipe_mm5, NULL);
if (g->sm_mm5)
g->vkDestroyShaderModule(g->dev, g->sm_mm5, NULL);
- for (int i = 0; i < 2; i++) {
+ for (int i = 0; i < 3; i++) {
if (g->pipe_dm[i])
g->vkDestroyPipeline(g->dev, g->pipe_dm[i], NULL);
if (g->sm_dm[i])
@@ -1306,18 +1429,26 @@ const struct region *janas_g_find_region(const struct janas_gpu *g,
return NULL;
}
+static int iqk_index(int type);
+
/* Bytes the shader may read for rows of a task: Q6_K, the high plane of
Q6_K_P, Q3_K and IQ3_S read up to 2 bytes past the last block (whole
- words). */
+ words). A row ending inside a group of 256 is as long as it is. */
static size_t read_bytes(const struct janas_matvec_task *t, size_t rows)
{
size_t nb = t->cols / JANAS_QK;
+ if (t->cols % JANAS_QK)
+ return rows * janas_qtype_row_bytes(t->type, t->cols);
if (t->type == JANAS_Q4_K_T || t->type == JANAS_Q6_K_AT)
return (t->rows + JANAS_TILE - 1) / JANAS_TILE * JANAS_TILE * nb *
(t->type == JANAS_Q4_K_T ? janas_qtype_block_size(JANAS_Q4_K)
: JANAS_Q6KA_BLOCK);
size_t bs = t->type == JANAS_Q6_K_A ? JANAS_Q6KA_BLOCK
: janas_qtype_block_size(t->type);
+ /* iq2, iq3xxs and iq1_matvec read a word past a block, realigned */
+ int ik = iqk_index(t->type);
+ if (ik > 0)
+ return rows * nb * bs + 8;
return rows * nb * bs + (t->type == JANAS_Q6_K || t->type == JANAS_Q6_K_P ||
t->type == JANAS_Q3_K ||
t->type == JANAS_IQ3_S
@@ -1342,9 +1473,45 @@ static const struct region *plane_region(const struct janas_gpu *g,
static int b32_kind(int type);
+/* The index of a 256-block type in pipe_iqk, -1 if none. */
+static int iqk_index(int type)
+{
+ static const int types[N_IQK] = {JANAS_Q2_K, JANAS_IQ2_XXS, JANAS_IQ2_XS,
+ JANAS_IQ2_S, JANAS_IQ3_XXS, JANAS_IQ1_S,
+ JANAS_IQ1_M};
+ for (int i = 0; i < N_IQK; i++)
+ if (types[i] == type)
+ return i;
+ return -1;
+}
+
+/* The kind of iqk_mmid.comp for a type's experts: iqk_index's, IQ3_S 7,
+ IQ4_XS 8; -1 if none. */
+static int iqm_kind(int type)
+{
+ return type == JANAS_IQ3_S ? N_IQK
+ : type == JANAS_IQ4_XS ? N_IQK + 1
+ : iqk_index(type);
+}
+
+/* Whether rows of t that end inside a group of 256 (qwen4exp's widths of
+ 640) are for the GPU: the types of b32_matvec.comp, rows of whole
+ words. */
+static int tail_ok(const struct janas_matvec_task *t)
+{
+ /* 0 bytes: a width the type cannot hold */
+ size_t rb = janas_qtype_row_bytes(t->type, t->cols);
+ return t->cols % JANAS_QK == 0 ||
+ ((b32_kind(t->type) >= 0 || t->type == JANAS_Q8_0) && rb > 0 &&
+ rb % 4 == 0);
+}
+
int janas_gpu_can(const struct janas_gpu *g, const struct janas_matvec_task *t)
{
- size_t nb = t->cols / JANAS_QK, nv = t->n_vec ? t->n_vec : 1;
+ /* the activations of a row ending inside a group: its last group
+ whole, past the row's end zero (model_ffn.c) */
+ size_t nb = (t->cols + JANAS_QK - 1) / JANAS_QK,
+ nv = t->n_vec ? t->n_vec : 1;
int typed =
(t->type == JANAS_Q4_K && (uintptr_t)t->w % 16 == 0) ||
(t->type == JANAS_Q5_K && (uintptr_t)t->w % 16 == 0) ||
@@ -1360,7 +1527,9 @@ int janas_gpu_can(const struct janas_gpu *g, const struct janas_matvec_task *t)
(t->type == JANAS_Q6_K_P && (uintptr_t)t->w % 4 == 0 &&
(plane_level(t) < 2 || plane_region(g, t->plane[0], t->rows, nb)) &&
(plane_level(t) < 3 || plane_region(g, t->plane[1], t->rows, nb)));
- return !g->broken && typed && t->cols % JANAS_QK == 0 &&
+ int ik = iqk_index(t->type);
+ typed |= ik >= 0 && g->pipe_iqk[ik] && (uintptr_t)t->w % 4 == 0;
+ return !g->broken && typed && tail_ok(t) &&
nv * nb * GPU_BLOCK <= X_STAGE_BYTES &&
t->rows * nv * sizeof(float) <= Y_STAGE_BYTES &&
janas_g_find_region(g, t->w, read_bytes(t, t->rows));
@@ -1397,6 +1566,8 @@ static int b32_kind(int type)
: type == JANAS_Q4_1 ? 2
: type == JANAS_Q5_1 ? 3
: type == JANAS_MXFP4 ? 4
+ : type == JANAS_Q5_0 ? 5
+ : type == JANAS_Q2_0 ? 6
: -1;
}
@@ -1432,7 +1603,9 @@ int janas_gpu_begin(struct janas_gpu *g, const struct janas_matvec_task *tasks,
if (gpu_rows[i] == 0)
continue;
const struct janas_matvec_task *t = &tasks[i];
- size_t nb = t->cols / JANAS_QK, nv = t->n_vec ? t->n_vec : 1;
+ /* a row ending inside a group: its activations' last group whole */
+ size_t nb = (t->cols + JANAS_QK - 1) / JANAS_QK,
+ nv = t->n_vec ? t->n_vec : 1;
size_t xs = t->x_stride ? t->x_stride : nb;
size_t k = 0;
int k32 = janas_g_x_in_32(t->type);
@@ -1543,13 +1716,13 @@ void janas_g_push_addr(uint32_t out[2], VkDeviceAddress a)
/* The index of a type's product shader in the single-vector table, -1 if
none: Q4_K, Q6_K, Q6_K_P, Q5_K, Q8_0, four of b32 (Q4_0, IQ4_NL, Q4_1,
- Q5_1), IQ4_XS, Q3_K, IQ3_S, and b32's MXFP4. */
+ Q5_1), IQ4_XS, Q3_K, IQ3_S, and b32's MXFP4, Q5_0 and Q2_0. */
static int prod_index(int type)
{
static const int types[N_PROD] = {
JANAS_Q4_K, JANAS_Q6_K, JANAS_Q6_K_P, JANAS_Q5_K, JANAS_Q8_0,
JANAS_Q4_0, JANAS_IQ4_NL, JANAS_Q4_1, JANAS_Q5_1, JANAS_IQ4_XS,
- JANAS_Q3_K, JANAS_IQ3_S, JANAS_MXFP4};
+ JANAS_Q3_K, JANAS_IQ3_S, JANAS_MXFP4, JANAS_Q5_0, JANAS_Q2_0};
for (int i = 0; i < N_PROD; i++)
if (types[i] == type)
return i;
@@ -1578,6 +1751,11 @@ void janas_gpu_set_only_fast(struct janas_gpu *g, int on)
g->only_fast = on;
}
+void janas_gpu_set_pass(struct janas_gpu *g, uint32_t n)
+{
+ g->pass_n = n;
+}
+
static int mm_on(void)
{
static int on = -1;
@@ -1646,6 +1824,9 @@ static int vectors_first(const struct janas_gpu *g, VkPipeline p)
for (int i = 0; i < N_PROD; i++)
if (p == g->pp1[i])
return 1;
+ for (int i = 0; i < N_IQK; i++)
+ if (p == g->pipe_iqk[i] || p == g->pp1_iqk[i])
+ return 1;
return 0;
}
@@ -1656,10 +1837,23 @@ static int fast_type(const struct janas_matvec_task *t)
t->type == JANAS_Q8_0 || t->type == JANAS_Q5_K ||
t->type == JANAS_Q3_K || t->type == JANAS_IQ4_XS ||
t->type == JANAS_IQ3_S || t->type == JANAS_IQ4_NL ||
- t->type == JANAS_MXFP4 ||
+ t->type == JANAS_MXFP4 || t->type == JANAS_Q5_0 ||
+ t->type == JANAS_Q2_0 || iqk_index(t->type) >= 0 ||
(t->type == JANAS_Q6_K_P && plane_level(t) == 3);
}
+/* The fast prompt's tiles for the types in blocks of 32 or 64
+ (iq4nl_mmq.comp, which takes rows ending inside a group too), or
+ none. */
+static VkPipeline b32_mm_pipe(const struct janas_gpu *g, int type)
+{
+ return type == JANAS_IQ4_NL ? g->pipe_mmn4
+ : type == JANAS_MXFP4 ? g->pipe_mmmx
+ : type == JANAS_Q5_0 ? g->pipe_mm50
+ : type == JANAS_Q2_0 ? g->pipe_mm20
+ : VK_NULL_HANDLE;
+}
+
int janas_g_record_task(struct janas_gpu *g, VkPipeline *bound,
const struct janas_matvec_task *t, size_t rows,
VkDeviceAddress xa, VkDeviceAddress ya, size_t ys)
@@ -1674,6 +1868,10 @@ int janas_g_record_task(struct janas_gpu *g, VkPipeline *bound,
: b32_kind(t->type) >= 0 ? g->pipe32[b32_kind(t->type)]
: g->pipe;
size_t nb = t->cols / JANAS_QK, nv = t->n_vec ? t->n_vec : 1;
+ /* blocks of 32 past the whole groups (a row ending inside one): the
+ activations' stride counts that group whole */
+ uint32_t tail = (uint32_t)(t->cols % JANAS_QK / 32);
+ size_t xnb = nb + (tail != 0);
/* a single vector: a subgroup per row where there is such a shader
(rows_wg rows a workgroup), else the shader made for one vector */
uint32_t nvg = g->nvg, rows_wg = ROWS_PER_GROUP;
@@ -1691,7 +1889,29 @@ int janas_g_record_task(struct janas_gpu *g, VkPipeline *bound,
return -1;
VkDeviceAddress wa =
rg->addr + (VkDeviceAddress)((const uint8_t *)t->w - rg->host);
- if (t->type == JANAS_Q4_K_T || t->type == JANAS_Q6_K_AT) {
+ int ik = iqk_index(t->type);
+ if (ik >= 0 && nv >= JANAS_GPU_FAST_N && mm_on() && g->pipe_iqq[ik]) {
+ /* the fast prompt's tiles (iqk_mmq.comp) */
+ want = g->pipe_iqq[ik];
+ nvg = 64;
+ rows_wg = 64;
+ } else if (ik >= 0) {
+ /* a row an invocation, for groups of vectors or for one */
+ if (!g->pipe_iqk[ik])
+ return -1;
+ want = nv == 1 && g->pp1_iqk[ik] ? g->pp1_iqk[ik] : g->pipe_iqk[ik];
+ nvg = nv == 1 && g->pp1_iqk[ik] ? 1 : g->nvg;
+ } else if (nv >= JANAS_GPU_FAST_N && mm_on() && b32_mm_pipe(g, t->type)) {
+ /* the fast prompt's tiles, rows ending inside a group too */
+ want = b32_mm_pipe(g, t->type);
+ nvg = 64;
+ rows_wg = 64;
+ } else if (tail) {
+ /* else only b32_matvec.comp and q8_0_matvec.comp sum a tail
+ (janas_gpu_can) */
+ want = nv == 1 && pk >= 0 && g->pp1[pk] ? g->pp1[pk] : want;
+ nvg = nv == 1 ? 1 : nvg;
+ } else if (t->type == JANAS_Q4_K_T || t->type == JANAS_Q6_K_AT) {
VkPipeline rp = g->rp_tile[t->type == JANAS_Q6_K_AT];
if (nv != 1 || !rp)
return -1;
@@ -1789,8 +2009,9 @@ int janas_g_record_task(struct janas_gpu *g, VkPipeline *bound,
struct push pc = {.rows = (uint32_t)rows,
.nb = (uint32_t)nb,
.nv = (uint32_t)nv,
- .xs = (uint32_t)nb,
- .ys = (uint32_t)ys};
+ .xs = (uint32_t)xnb,
+ .ys = (uint32_t)ys,
+ .tail = tail};
janas_g_push_addr(pc.w, wa);
janas_g_push_addr(pc.x, xa);
janas_g_push_addr(pc.y, ya);
@@ -2080,10 +2301,32 @@ static int experts_off(const struct janas_gpu *g, size_t n, int type,
return -1;
}
+/* JANAS_GPU_REPEATABLE=1: a mixture's experts never on the fast prompt's
+ shaders. A group goes to them only when all its experts are in the memory
+ given to the GPU, which changes as the cache fills (and the GPU gets the
+ cache after the first pass): the same prompt read twice had some groups
+ rounded on the GPU and others exact on the CPU, and Qwen3.8-Flash-Next's
+ reply to it changed from one run to the next (22 Oct 2026). With it the
+ experts stay exact on the CPU, the dense products (weights that never
+ move) on the GPU: a prompt gives the same results every time. */
+int janas_gpu_repeatable(void)
+{
+ static int on = -1;
+ if (on < 0) {
+ const char *e = getenv("JANAS_GPU_REPEATABLE");
+ on = e && atoi(e) > 0;
+ }
+ return on;
+}
+
static int experts_id(struct janas_gpu *g, struct janas_pool *pool,
const struct janas_matvec_task *t, size_t n)
{
- if (!mm_on() || g->busy || g->broken || n < 2 || !g->pipe_id4)
+ /* not in a chain of drafts to verify (janas_gpu_set_pass): with
+ Qwen3.8-Flash-Next's ten experts a token, a chain of 13 made the 128
+ tokens below, and its text was not the same as without drafts */
+ if (!mm_on() || g->busy || g->broken || n < 2 || !g->pipe_id4 ||
+ g->pass_n < JANAS_GPU_FAST_N || janas_gpu_repeatable())
return -1;
/* a group may open with the layer's shared expert (model_ffn.c: its gate
and up, or its down) in a type of its own - Q8_0 beside the routed
@@ -2097,13 +2340,27 @@ static int experts_id(struct janas_gpu *g, struct janas_pool *pool,
int type = t[s0].type;
if (n * 48 > g->ts.bytes || n > 2 * 128 + 8)
return experts_off(g, n, type, "more products than its table");
+ /* b32_mmid.comp: IQ4_NL, Q5_0, Q2_0 (pipe_idb 0, 1, 2); iqk_mmid.comp:
+ Q2_K, the IQ1, IQ2 and IQ3 kinds, IQ4_XS */
+ int ib = type == JANAS_IQ4_NL ? 0
+ : type == JANAS_Q5_0 ? 1
+ : type == JANAS_Q2_0 ? 2
+ : -1;
+ int im = iqm_kind(type);
if (!(type == JANAS_Q4_K || type == JANAS_Q6_K ||
(type == JANAS_MXFP4 && g->pipe_idmx) ||
- (type == JANAS_Q3_K && g->pipe_id3) ||
+ (type == JANAS_Q3_K && g->pipe_id3) || (ib >= 0 && g->pipe_idb[ib]) ||
+ (im >= 0 && g->pipe_iqm[im]) ||
(type == JANAS_Q6_K_P && plane_level(&t[s0]) == 3)))
return experts_off(g, n, type, "a type without its shader");
const struct janas_matvec_task *t0 = &t[s0];
- size_t nb = t0->cols / JANAS_QK, xs = t0->x_stride ? t0->x_stride : nb;
+ /* rows ending inside a group of 256 (qwen4exp's down matrices, 640
+ wide): b32_mmid.comp's types only, their activations' last group
+ whole */
+ if (t0->cols % JANAS_QK && ib < 0)
+ return experts_off(g, n, type, "rows ending inside a group of 256");
+ size_t nb = (t0->cols + JANAS_QK - 1) / JANAS_QK,
+ xs = t0->x_stride ? t0->x_stride : nb;
const struct janas_block_q8k *x0 = t0->x, *x1 = t0->x;
for (size_t i = s0; i < n; i++) {
size_t nv = t[i].n_vec ? t[i].n_vec : 1;
@@ -2229,19 +2486,24 @@ static int experts_id(struct janas_gpu *g, struct janas_pool *pool,
if (g->vkBeginCommandBuffer(g->cb, &bb) != VK_SUCCESS)
return -1;
g->vkCmdBindPipeline(g->cb, VK_PIPELINE_BIND_POINT_COMPUTE,
- type == JANAS_Q4_K ? g->pipe_id4
+ ib >= 0 ? g->pipe_idb[ib]
+ : im >= 0 ? g->pipe_iqm[im]
+ : type == JANAS_Q4_K ? g->pipe_id4
: type == JANAS_Q6_K ? g->pipe_id6s
: type == JANAS_MXFP4 ? g->pipe_idmx
: type == JANAS_Q3_K ? g->pipe_id3
: g->pipe_id6);
- uint32_t pc[5];
+ /* b32_mmid.comp and iqk_mmid.comp also take the groups of 32 a row
+ (pc[5]) */
+ uint32_t pc[6];
janas_g_push_addr(pc, g->ts.addr);
- pc[2] = (uint32_t)t[0].rows;
+ pc[2] = (uint32_t)t0->rows;
pc[3] = (uint32_t)nb;
pc[4] = (uint32_t)(3 * ng);
+ pc[5] = (uint32_t)(t0->cols / 32);
g->vkCmdPushConstants(g->cb, g->pl, VK_SHADER_STAGE_COMPUTE_BIT, 0,
sizeof(pc), pc);
- g->vkCmdDispatch(g->cb, (uint32_t)((t[0].rows + 63) / 64), 1, (uint32_t)nt);
+ g->vkCmdDispatch(g->cb, (uint32_t)((t0->rows + 63) / 64), 1, (uint32_t)nt);
VkMemoryBarrier mb = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER,
.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
.dstAccessMask = VK_ACCESS_HOST_READ_BIT};
@@ -2346,6 +2608,103 @@ int janas_gpu_dense_mm(struct janas_gpu *g, int bf16, const void *w,
return janas_gpu_finish(g, &waited) == 0 ? 0 : -1;
}
+int janas_gpu_bf16_mv(struct janas_gpu *g, const uint16_t *w, const float *x,
+ float *y, size_t rows, size_t cols, size_t n, float scale)
+{
+ /* not in a pass the tuner gave the CPU (janas_gpu_set_only_fast) */
+ if (!g || g->busy || g->broken || g->only_fast || !g->pipe_b16 || n == 0 ||
+ rows == 0 || cols == 0 || n < g->min_n)
+ return -1;
+ const struct region *wr = janas_g_find_region(g, w, rows * cols * 2);
+ if (!wr || (uintptr_t)w % 4 != 0)
+ return -1;
+ /* x read and y written in place where the GPU shares them with the
+ host, else through the stages (a card's own memory) */
+ size_t xb = n * cols * sizeof(float), yb = n * rows * sizeof(float);
+ const struct region *xr = janas_g_find_region(g, x, xb),
+ *yr = janas_g_find_region(g, y, yb);
+ int xin = xr && xr->host_shared, yin = yr && yr->host_shared;
+ if ((!xin && xb > X_STAGE_BYTES) || (!yin && yb > Y_STAGE_BYTES))
+ return -1;
+ if (!xin) {
+ memcpy(g->xs.map, x, xb);
+ if (!g->xs.coherent) {
+ VkDeviceSize sz = (xb + g->atom - 1) / g->atom * g->atom;
+ VkMappedMemoryRange mr = {
+ .sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
+ .memory = g->xs.mem,
+ .size = sz < g->xs.bytes ? sz : VK_WHOLE_SIZE};
+ g->vkFlushMappedMemoryRanges(g->dev, 1, &mr);
+ }
+ }
+ if (g->vkResetCommandBuffer(g->cb, 0) != VK_SUCCESS)
+ return -1;
+ VkCommandBufferBeginInfo bb = {
+ .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
+ .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT};
+ if (g->vkBeginCommandBuffer(g->cb, &bb) != VK_SUCCESS)
+ return -1;
+ g->vkCmdBindPipeline(g->cb, VK_PIPELINE_BIND_POINT_COMPUTE, g->pipe_b16);
+ struct {
+ uint32_t w[2], x[2], y[2];
+ uint32_t rows, cols, nv, xs, ys;
+ float scale;
+ } pc = {.rows = (uint32_t)rows,
+ .cols = (uint32_t)cols,
+ .nv = (uint32_t)n,
+ .xs = (uint32_t)cols,
+ .ys = (uint32_t)rows,
+ .scale = scale};
+ janas_g_push_addr(
+ pc.w, wr->addr + (VkDeviceAddress)((const uint8_t *)w - wr->host));
+ janas_g_push_addr(
+ pc.x, xin ? xr->addr + (VkDeviceAddress)((const uint8_t *)x - xr->host)
+ : g->xs.addr);
+ janas_g_push_addr(
+ pc.y, yin ? yr->addr + (VkDeviceAddress)((const uint8_t *)y - yr->host)
+ : g->ys.addr);
+ g->vkCmdPushConstants(g->cb, g->pl, VK_SHADER_STAGE_COMPUTE_BIT, 0,
+ sizeof(pc), &pc);
+ /* vectors on x, rows on y: eight rows a workgroup (64 invocations) */
+ g->vkCmdDispatch(g->cb, (uint32_t)((n + B16_NV - 1) / B16_NV),
+ (uint32_t)((rows + 7) / 8), 1);
+ VkMemoryBarrier mb = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER,
+ .srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
+ .dstAccessMask = VK_ACCESS_HOST_READ_BIT};
+ g->vkCmdPipelineBarrier(g->cb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
+ VK_PIPELINE_STAGE_HOST_BIT, 0, 1, &mb, 0, NULL, 0,
+ NULL);
+ if (g->vkEndCommandBuffer(g->cb) != VK_SUCCESS)
+ return -1;
+ /* a staged y: copied out by janas_gpu_finish */
+ g->n_pend = 0;
+ g->y_used = 0;
+ if (!yin) {
+ g->pend[0] = (struct pending){
+ .y = y, .rows = rows, .nv = n, .y_stride = rows, .off = 0};
+ g->n_pend = 1;
+ g->y_used = yb;
+ }
+ VkSubmitInfo sub = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
+ .commandBufferCount = 1,
+ .pCommandBuffers = &g->cb};
+ pthread_mutex_lock(&g->qlock);
+ VkResult sr = g->vkQueueSubmit(g->queue, 1, &sub, g->fence);
+ pthread_mutex_unlock(&g->qlock);
+ if (sr != VK_SUCCESS) {
+ g->broken = 1;
+ g->n_pend = 0;
+ return -1;
+ }
+ g->busy = 1;
+ int waited;
+ if (janas_gpu_finish(g, &waited) != 0)
+ return -1;
+ g->b16_calls++;
+ g->b16_staged += !xin || !yin;
+ return 0;
+}
+
void janas_gpu_matvec_group(struct janas_gpu *g, struct janas_pool *pool,
const struct janas_matvec_task *tasks, size_t n)
{
@@ -2410,7 +2769,7 @@ void janas_gpu_matvec_group(struct janas_gpu *g, struct janas_pool *pool,
size_t nb = t->cols / JANAS_QK;
cpu[i] = *t;
cpu[i].w =
- (const uint8_t *)t->w + r * nb * janas_qtype_block_size(t->type);
+ (const uint8_t *)t->w + r * janas_qtype_row_bytes(t->type, t->cols);
for (int q = 0; q < 2; q++) /* Q6_K_P: its bare planes follow */
if (t->plane[q])
cpu[i].plane[q] =
@@ -2538,6 +2897,17 @@ void janas_gpu_set_only_fast(struct janas_gpu *g, int on)
(void)on;
}
+void janas_gpu_set_pass(struct janas_gpu *g, uint32_t n)
+{
+ (void)g;
+ (void)n;
+}
+
+int janas_gpu_repeatable(void)
+{
+ return 0;
+}
+
int janas_gpu_import(struct janas_gpu *g, const void *p, size_t bytes)
{
(void)g;
@@ -2614,6 +2984,20 @@ size_t janas_gpu_few_max(const struct janas_gpu *g)
return 0;
}
+int janas_gpu_bf16_mv(struct janas_gpu *g, const uint16_t *w, const float *x,
+ float *y, size_t rows, size_t cols, size_t n, float scale)
+{
+ (void)g;
+ (void)w;
+ (void)x;
+ (void)y;
+ (void)rows;
+ (void)cols;
+ (void)n;
+ (void)scale;
+ return -1;
+}
+
int janas_gpu_dense_mm(struct janas_gpu *g, int bf16, const void *w,
const float *x, float *y, size_t rows, size_t cols,
size_t n, float scale)
diff --git a/src/llm/gpu.h b/src/llm/gpu.h
index 88f55dc..f87fdcc 100644
--- a/src/llm/gpu.h
+++ b/src/llm/gpu.h
@@ -18,6 +18,7 @@
#define JANAS_LLM_GPU_H
#include <stddef.h>
+#include <stdint.h>
#include "matvec.h"
@@ -114,6 +115,21 @@ int janas_gpu_dense_mm(struct janas_gpu *g, int bf16, const void *w,
const float *x, float *y, size_t rows, size_t cols,
size_t n, float scale);
+/*
+ * y = scale w x for bf16 weights (rows x cols, in memory the GPU holds),
+ * with n vectors of cols floats in x and y[v * rows + r], computed as the
+ * CPU computes them with AVX2 and FMA (bf16.h): the same bits, so for any
+ * n the GPU takes (janas_gpu_min_n); x and y in place where the GPU shares
+ * them with the host, else through its stages. Waited for. Not in a pass
+ * the tuner gave the CPU (janas_gpu_set_only_fast). 0, or -1 with nothing
+ * written (the caller computes it). Qwen3.8-Flash-Next's mixes of the
+ * streams, 1.25 GB of weights a token: a reply's tokens on a card with
+ * memory of its own.
+ */
+int janas_gpu_bf16_mv(struct janas_gpu *g, const uint16_t *w, const float *x,
+ float *y, size_t rows, size_t cols, size_t n,
+ float scale);
+
/*
* Whether the fast prompt is on (unless JANAS_GPU_FAST_PROMPT=0): blocks of
* JANAS_GPU_FAST_N vectors and more of the types it has shaders for go to
@@ -129,6 +145,21 @@ int janas_gpu_fast_prompt(void);
/* For the passes that follow: only the fast prompt's products on the GPU,
the rest on the CPU (a pass the tuner gave the CPU alone). */
void janas_gpu_set_only_fast(struct janas_gpu *g, int on);
+/* The tokens of the pass that follows. A mixture's experts go to the fast
+ prompt's shaders (experts_id) only in a prompt's blocks, of
+ JANAS_GPU_FAST_N tokens and more: a chain of drafts to verify stays bit
+ for bit with the CPU, so that drafts never change the text. Not said
+ (a probe calling the products itself): any pass. */
+void janas_gpu_set_pass(struct janas_gpu *g, uint32_t n);
+/* JANAS_GPU_REPEATABLE=1: a prompt gives the same results every time - a
+ mixture's experts never on the GPU (janas_m_gpu: an expert's product went
+ to the fast prompt's tiles where the GPU could read its slot, to the CPU
+ where it could not, as the cache filled), and in a prompt's blocks the
+ GPU only the fast prompt's products (janas_m_apply_candidate):
+ products shared with the CPU, begun beside them, made the GPU busy, and
+ the fast ones asked meanwhile went to the CPU, as the tuner tried one
+ configuration or another. */
+int janas_gpu_repeatable(void);
/* Keeps the GPU out of its sleep states (an empty dispatch every 0.5 ms,
JANAS_GPU_KEEPALIVE_US) while on: for the passes that use it. */
diff --git a/src/llm/gpu_impl.h b/src/llm/gpu_impl.h
index 131982c..dede18b 100644
--- a/src/llm/gpu_impl.h
+++ b/src/llm/gpu_impl.h
@@ -16,9 +16,12 @@
/* The push constants a shader may take: every Vulkan device gives 128. */
#define JANAS_G_PUSH_BYTES 128
/* the kinds of product shader (gpu.c's prod_index) */
-#define N_PROD 13
+#define N_PROD 15
/* the kinds of b32_matvec.comp (gpu.c's b32_kind) */
-#define N_B32 5
+#define N_B32 7
+/* the 256-block types of iq2_matvec, iq3xxs_matvec, iq1_matvec and
+ q2k_matvec.comp (gpu.c's iqk_index) */
+#define N_IQK 7
#define VK_NO_PROTOTYPES
#include <dlfcn.h>
@@ -37,7 +40,10 @@
#define gpu_relax() ((void)0)
#endif
-#define MAX_REGIONS 48
+/* the resident region, the experts' arena (4 GiB a region: 15 on a
+ machine whose cache holds 60 GB), the engine's buffers (some 30), the
+ KV cache */
+#define MAX_REGIONS 80
#define MAX_PENDING 64
#define MAX_XCACHE 8
#define X_STAGE_BYTES \
@@ -46,6 +52,7 @@
#define ROWS_PER_GROUP 64 /* local_size_x of the shader */
#define NV_PER_GROUP 8 /* its NV */
#define MV_NV 8 /* q4k_mv's */
+#define B16_NV 4 /* bf16_matvec's vectors a workgroup */
#define MVD_ROWS 32 /* q4k_mvd's rows a workgroup */
#define GPU_BLOCK 320 /* a Q8_K block in the shader's layout */
#define SHARE_SLOTS 64
@@ -143,6 +150,9 @@ struct push {
uint32_t w[2], x[2], y[2];
uint32_t rows, nb, nv, xs, ys, level;
uint32_t p1[2], p0[2];
+ /* b32_matvec.comp, q8_0_matvec.comp: blocks of 32 past the row's
+ whole groups of 256 */
+ uint32_t tail;
};
struct janas_gpu {
@@ -171,16 +181,37 @@ struct janas_gpu {
VkPipeline pipex4, pipe3, pipei3; /* IQ4_XS, Q3_K, IQ3_S */
/* Q4_K on many vectors in tiles (q4k_mm.comp), MM_ROWS x MM_VECS */
VkShaderModule sm_mm, sm_mm6, sm_mm6s, sm_mm80, sm_mm5, sm_mm3, sm_mmx4,
- sm_mmi3, sm_mmn4, sm_mmmx,
- sm_dm[2]; /* sm_mmx4 IQ4_XS, mmi3 IQ3_S, mmn4 IQ4_NL, mmmx MXFP4 */
+ sm_mmi3, sm_mmn4, sm_mmmx, sm_mm50, sm_mm20,
+ sm_dm[3]; /* sm_mmx4 IQ4_XS, mmi3 IQ3_S, mmn4 IQ4_NL, mmmx MXFP4,
+ mm50 Q5_0, mm20 Q2_0; dm f32, bf16 and Q8_0 weights on
+ float vectors */
VkPipeline pipe_mm, pipe_mm6, pipe_mm6s, pipe_mm80, pipe_mm5, pipe_mm3,
- pipe_mmx4, pipe_mmi3, pipe_mmn4, pipe_mmmx, pipe_dm[2];
- int only_fast; /* janas_gpu_set_only_fast */
- uint32_t nvg; /* vectors a product's workgroup takes (NV_PER_GROUP) */
+ pipe_mmx4, pipe_mmi3, pipe_mmn4, pipe_mmmx, pipe_mm50, pipe_mm20,
+ pipe_dm[3];
+ int only_fast; /* janas_gpu_set_only_fast */
+ uint32_t pass_n; /* janas_gpu_set_pass; UINT32_MAX until said */
+ uint32_t nvg; /* vectors a product's workgroup takes (NV_PER_GROUP) */
/* a mixture's experts in one dispatch (experts_id): Q4_K, Q6_K_P,
Q6_K, MXFP4 */
VkShaderModule sm_id4, sm_id6, sm_id6s, sm_idmx, sm_id3;
VkPipeline pipe_id4, pipe_id6, pipe_id6s, pipe_idmx, pipe_id3;
+ /* ... and in blocks of 32 or 64 (b32_mmid.comp): IQ4_NL, Q5_0, Q2_0 */
+ VkShaderModule sm_idb[3];
+ VkPipeline pipe_idb[3];
+ /* bf16 weights on float vectors, bit for bit (bf16_matvec.comp) */
+ VkShaderModule sm_b16;
+ VkPipeline pipe_b16;
+ /* Q2_K, IQ2_XXS, IQ2_XS, IQ2_S, IQ3_XXS, IQ1_S, IQ1_M, a row an
+ invocation, for groups of vectors and for one (NULL: the CPU's) */
+ VkShaderModule sm_iqk[N_IQK], sm1_iqk[N_IQK];
+ VkPipeline pipe_iqk[N_IQK], pp1_iqk[N_IQK];
+ /* ... and their experts in one dispatch, IQ3_S's and IQ4_XS's too
+ (iqk_mmid.comp, gpu.c's iqm_kind) */
+ VkShaderModule sm_iqm[N_IQK + 2];
+ VkPipeline pipe_iqm[N_IQK + 2];
+ /* ... and their tiles for the fast prompt (iqk_mmq.comp) */
+ VkShaderModule sm_iqq[N_IQK];
+ VkPipeline pipe_iqq[N_IQK];
struct stage ts; /* their table, 3 uvec4 an expert */
/* the product shaders made for a single vector (prod_index) */
VkShaderModule pm1[N_PROD];
@@ -235,9 +266,10 @@ struct janas_gpu {
int verbose, nospin, block_wait;
double t_waited; /* last janas_gpu_finish */
size_t y_used; /* bytes of the Y stage written by the last group */
- double t_conv, t_rec, t_sub; /* verbose: time in janas_gpu_begin */
- VkDeviceSize atom; /* nonCoherentAtomSize */
- float ts_period; /* nanoseconds a timestamp tick */
+ double t_conv, t_rec, t_sub; /* verbose: time in janas_gpu_begin */
+ uint64_t b16_calls, b16_staged; /* janas_gpu_bf16_mv's, staged among them */
+ VkDeviceSize atom; /* nonCoherentAtomSize */
+ float ts_period; /* nanoseconds a timestamp tick */
/* keep-alive: an empty dispatch every keep_us while running, so that
the GPU does not fall asleep between products (waking it costs up to
~0.5 ms per submission) */
diff --git a/src/llm/gpu_token.c b/src/llm/gpu_token.c
index 04b30ee..644d9dc 100644
--- a/src/llm/gpu_token.c
+++ b/src/llm/gpu_token.c
@@ -95,15 +95,18 @@ static const char *check_ffn(const struct janas_gpu *g,
return NULL;
}
int gt = ly->gate.type, dt = ly->down.type;
+ /* Q2_0 (qwen4exp's): b32_mmid.comp, rows of 640 too */
+ int q20 = g->pipe_idb[2] != VK_NULL_HANDLE;
if (!(gt == JANAS_Q4_K || (gt == JANAS_MXFP4 && g->pipe_idmx) ||
- (gt == JANAS_Q3_K && g->pipe_id3)) ||
+ (gt == JANAS_Q3_K && g->pipe_id3) || (gt == JANAS_Q2_0 && q20)) ||
ly->up.type != gt ||
!(dt == JANAS_Q4_K || dt == JANAS_Q6_K || dt == JANAS_Q6_K_P ||
(dt == JANAS_MXFP4 && g->pipe_idmx) ||
- (dt == JANAS_Q3_K && g->pipe_id3)) ||
- ly->gate.rows != md->eff || ly->up.rows != md->eff ||
- ly->gate.cols != md->dm || ly->up.cols != md->dm ||
- ly->down.rows != md->dm || ly->down.cols != md->eff)
+ (dt == JANAS_Q3_K && g->pipe_id3) || (dt == JANAS_Q2_0 && q20)) ||
+ (md->eff % JANAS_QK && dt != JANAS_Q2_0) || ly->gate.rows != md->eff ||
+ ly->up.rows != md->eff || ly->gate.cols != md->dm ||
+ ly->up.cols != md->dm || ly->down.rows != md->dm ||
+ ly->down.cols != md->eff)
return "experts of a type or shape the GPU's shaders do not take";
if (!ly->router16)
return "a mixture's layer without its router";
@@ -120,22 +123,95 @@ static const char *check_ffn(const struct janas_gpu *g,
return NULL;
}
+/* qwen4exp's streams: every mix's matrices where the GPU reads them, bf16
+ (down and up Q8_0 with JANAS_HC_MIX=q8), the n-gram layer's pieces, the ring
+ of the conv's inputs and the attention's inputs kept for the indexer. */
+static int check_hc(const struct janas_gpu *g,
+ const struct janas_gpu_tok_model *md, char *err, size_t len)
+{
+ size_t hd = (size_t)md->hc * md->dm, r = md->hc_rank;
+ if (md->hc < 2 || md->hc > 16 || r % 32 || hd % 256 || !g->pipe_dm[1] ||
+ (md->hc_q8 && !g->pipe_dm[2]))
+ return fail(err, len, "streams of a shape the shaders do not take");
+ /* down and up: bf16, or Q8_0 */
+ size_t mb = md->hc_q8 ? r * hd / 32 * 34 : r * hd * 2;
+ for (uint32_t l = 0; l <= md->n_layer; l++)
+ for (int k = 0; k < (l < md->n_layer ? 2 : 1); k++) {
+ const struct janas_gpu_tok_layer *ly =
+ l < md->n_layer ? &md->layers[l] : NULL;
+ const float *nw = ly ? ly->hc_norm[k] : md->hco_norm;
+ const uint16_t *dw = ly ? ly->hc_down[k] : md->hco_down,
+ *uw = ly ? ly->hc_up[k] : md->hco_up,
+ *iw = ly ? ly->hc_inject[k] : NULL;
+ if (!janas_gt_addr_of(g, nw, hd * 4) ||
+ !janas_gt_addr_of(g, dw, mb) || !janas_gt_addr_of(g, uw, mb) ||
+ (ly && !janas_gt_addr_of(g, iw, md->hc * hd * 2)))
+ return fail(err, len,
+ "a mix of the streams not in bf16 where the GPU "
+ "reads it");
+ }
+ if (md->ng_layer < md->n_layer) {
+ const struct janas_gpu_tok_layer *ly = &md->layers[md->ng_layer];
+ if (!md->ng_emb || md->ng_kernel == 0 || md->ng_kernel > 8 ||
+ !janas_gpu_can(g, &ly->ng_key) ||
+ !janas_gt_addr_of(g, ly->ng_value, (size_t)md->dm * md->dm * 2) ||
+ !janas_gt_addr_of(g, ly->ng_nk, hd * 4) ||
+ !janas_gt_addr_of(g, ly->ng_nq, hd * 4) ||
+ !janas_gt_addr_of(g, ly->ng_nc, hd * 4) || !ly->ng_conv ||
+ !janas_gt_addr_of(g, md->ng_hist, md->ng_cap * hd * 4))
+ return fail(err, len, "n-gram embeddings out of the GPU's reach");
+ }
+ /* the sparse attention's indexer: its queries in the score shader's
+ shared memory, a head's width a workgroup's, the positions of a
+ token within the cache's runs */
+ if (md->ix_heads) {
+ uint32_t d = md->ix_dim, r = md->ix_ratio;
+ if (!md->att_in || !md->ix_rope || d > 256 || d % 8 ||
+ md->ix_heads * d > 2048 || md->n_rot > d || md->n_rot % 2 ||
+ r == 0 || r > 256 || md->ix_top % r || md->ix_top == 0)
+ return fail(err, len,
+ "a sparse attention of a shape the shaders do not "
+ "take");
+ for (uint32_t l = 0; l < md->n_layer; l++) {
+ const struct janas_gpu_tok_layer *ly = &md->layers[l];
+ if (ly->rec)
+ continue;
+ if (!ly->ix_q || !ly->ix_k || !ly->ix_qn || !ly->ix_kn ||
+ !janas_gt_addr_of(g, ly->ix_q,
+ (size_t)md->ix_heads * d * md->dm * 2) ||
+ !janas_gt_addr_of(g, ly->ix_k, (size_t)d * md->dm * 2) ||
+ !janas_gt_addr_of(g, ly->ix_qn, (size_t)d * 4) ||
+ !janas_gt_addr_of(g, ly->ix_kn, (size_t)d * 4) ||
+ !janas_gt_addr_of(g, ly->ix_raw, (size_t)md->cap * d * 2) ||
+ !janas_gt_addr_of(g, ly->ix_pool,
+ (size_t)((md->cap + r - 1) / r) * d * 4))
+ return fail(err, len,
+ "the sparse attention's indexer out of the GPU's "
+ "reach");
+ }
+ }
+ return 0;
+}
+
/* Whether the model's pieces are all where the GPU can read them. */
static int check_model(const struct janas_gpu *g,
const struct janas_gpu_tok_model *md, char *err,
size_t len)
{
uint32_t qd = md->n_head * md->hd, kvd = md->n_kv * md->hd;
- if (md->dm % JANAS_QK || qd % JANAS_QK || md->ff % JANAS_QK ||
- md->dm > 8192 || qd > 8192 || md->hd > 512 || md->hd % 16 ||
- md->n_rot > md->hd || md->n_kv == 0 || md->n_head % md->n_kv ||
- md->n_head / md->n_kv * md->hd > ATT_MAXQ ||
- md->n_head / md->n_kv * ATT_RUN > ATT_MAXS)
+ /* feed-forwards may end inside a group of 256 (qwen4exp's 640): their
+ down matrices in blocks of 32, checked by janas_gpu_can and
+ check_ffn */
+ if (md->dm % JANAS_QK || qd % JANAS_QK || md->ff % 32 || md->dm > 8192 ||
+ qd > 8192 || md->hd > 512 || md->hd % 16 || md->n_rot > md->hd ||
+ md->n_kv == 0 || md->n_head % md->n_kv ||
+ md->n_head / md->n_kv * md->hd > ATT_MAXQ_G(md->n_head / md->n_kv) ||
+ md->n_head / md->n_kv * ATT_RUN > ATT_MAXS_G(md->n_head / md->n_kv))
return fail(err, len, "a shape the token's shaders do not take");
- if (md->n_expert && (md->n_expert > 512 || md->k == 0 || md->k > 16 ||
- md->k > md->n_expert || md->eff % JANAS_QK ||
- !md->fetch || !g->pipe_id4 || !g->pipe_id6 ||
- !g->pipe_id6s || !g->pipea || !g->pipe_dm[0]))
+ if (md->n_expert &&
+ (md->n_expert > 512 || md->k == 0 || md->k > 16 ||
+ md->k > md->n_expert || md->eff % 32 || !md->fetch || !g->pipe_id4 ||
+ !g->pipe_id6 || !g->pipe_id6s || !g->pipea || !g->pipe_dm[0]))
return fail(err, len, "a mixture the token's shaders do not take");
if (md->n_rec &&
(md->ds != 128 || md->conv_ch % 256 || md->d_inner % 256 ||
@@ -169,8 +245,8 @@ static int check_model(const struct janas_gpu *g,
l, i, r[i]->type);
return -1;
}
- if (!janas_gt_addr_of(g, ly->attn_norm, md->dm * 4) ||
- !janas_gt_addr_of(g, ly->ffn_norm, md->dm * 4))
+ if (!md->hc && (!janas_gt_addr_of(g, ly->attn_norm, md->dm * 4) ||
+ !janas_gt_addr_of(g, ly->ffn_norm, md->dm * 4)))
return fail(err, len, "a norm out of the GPU's reach");
continue;
}
@@ -187,8 +263,8 @@ static int check_model(const struct janas_gpu *g,
if (md->gated_q && (ly->wq.type == JANAS_Q6_K_P || ly->wq.plane[0]))
return fail(err, len, "a gated query in planes");
size_t cache = (size_t)md->n_kv * md->cap * md->hd;
- if (!janas_gt_addr_of(g, ly->attn_norm, md->dm * 4) ||
- !janas_gt_addr_of(g, ly->ffn_norm, md->dm * 4) ||
+ if ((!md->hc && (!janas_gt_addr_of(g, ly->attn_norm, md->dm * 4) ||
+ !janas_gt_addr_of(g, ly->ffn_norm, md->dm * 4))) ||
!janas_gt_addr_of(g, ly->q_norm, md->hd * 4) ||
!janas_gt_addr_of(g, ly->k_norm, md->hd * 4) ||
!janas_gt_addr_of(g, ly->kc, cache) ||
@@ -200,8 +276,10 @@ static int check_model(const struct janas_gpu *g,
"reach");
}
if (!janas_gpu_can(g, &md->output) ||
- !janas_gt_addr_of(g, md->out_norm, md->dm * 4))
+ (!md->hc && !janas_gt_addr_of(g, md->out_norm, md->dm * 4)))
return fail(err, len, "the output head out of the GPU's reach");
+ if (md->hc && check_hc(g, md, err, len) != 0)
+ return -1;
(void)kvd;
return 0;
}
@@ -231,19 +309,33 @@ struct janas_gpu_tok *janas_gpu_tok_create(struct janas_gpu *g,
long at any context - sized for the cache, 16384 positions left
Qwen3-4B 30 tokens a block, too few for the fast prompt's tiles */
t->nch = (md->cap + ATT_RUN - 1) / ATT_RUN;
+ /* a sparse attention: a token's positions are at most its budget and
+ its own block's, the runs that take them (ix_select.comp) */
+ if (md->ix_heads) {
+ uint32_t r = md->ix_ratio;
+ t->ix_runs = (md->ix_top + r - 1 + ATT_RUN - 1) / ATT_RUN;
+ if (t->ix_runs < t->nch)
+ t->nch = t->ix_runs;
+ t->ix_nbs = (md->cap + r - 1) / r;
+ t->ix_ls = md->ix_top + r;
+ }
size_t full = (size_t)md->n_head * t->nch * (md->hd + 2);
t->part_floats = TOK_PART_BUDGET / 4 > full ? TOK_PART_BUDGET / 4 : full;
t->nb = TOK_MAX_BLOCK;
t->n_seg = md->n_layer + 1;
size_t nb = t->nb;
+ /* feed-forward widths ending inside a group of 256 (qwen4exp's 640):
+ their buffers in whole groups, the rest zero */
+ t->ffp = (md->ff + JANAS_QK - 1) / JANAS_QK * JANAS_QK;
+ t->effp = (md->eff + JANAS_QK - 1) / JANAS_QK * JANAS_QK;
t->o_x = take(&at, nb * md->dm * 4);
t->o_x1 = take(&at, nb * md->dm * 4); /* the residual's other buffer */
t->o_q256 = take(&at, nb * big / JANAS_QK * 320);
t->o_q32 = take(&at, nb * big / JANAS_QK * 320);
t->o_qkv = take(&at, nb * (qd + 2 * kvd) * 4);
t->o_ywo = take(&at, nb * md->dm * 4);
- t->o_gu = take(&at, nb * md->ff * 2 * 4);
- t->o_h = take(&at, nb * md->ff / JANAS_QK * 320);
+ t->o_gu = take(&at, nb * t->ffp * 2 * 4);
+ t->o_h = take(&at, nb * t->ffp / JANAS_QK * 320);
t->o_yd = take(&at, nb * md->dm * 4);
t->o_rope = take(&at, nb * md->n_rot * 4);
t->o_logits = take(&at, nb * md->n_vocab * 4);
@@ -251,6 +343,33 @@ struct janas_gpu_tok *janas_gpu_tok_create(struct janas_gpu *g,
t->o_part = take(&at, t->part_floats * 4);
t->o_xnf = take(&at, nb * md->dm * 4);
t->o_hid = take(&at, nb * md->dm * 4);
+ if (md->hc) {
+ size_t hd = (size_t)md->hc * md->dm, r = md->hc_rank;
+ t->o_res = take(&at, nb * hd * 4);
+ t->o_hxn = take(&at, nb * hd * 4);
+ t->o_hxb = take(&at, nb * hd * 4);
+ t->o_lo = take(&at, nb * r * 4);
+ t->o_lob = take(&at, nb * r * 4);
+ t->o_hg = take(&at, nb * hd * 4);
+ t->o_inj = take(&at, nb * md->hc * 4);
+ t->o_hmean = take(&at, nb * md->dm * 4);
+ if (md->ix_heads) {
+ uint32_t d = md->ix_dim, r = md->ix_ratio;
+ t->o_ixq = take(&at, nb * md->ix_heads * d * 4);
+ t->o_ixk = take(&at, nb * d * 4);
+ t->o_ixrope = take(&at, (nb + nb / r + 2) * md->n_rot * 4);
+ t->o_ixsc = take(&at, nb * t->ix_nbs * 4);
+ t->o_ixlist = take(&at, nb * t->ix_ls * 4);
+ }
+ if (md->ng_layer < md->n_layer) {
+ t->o_ngk = take(&at, nb * hd * 4);
+ t->o_ngv = take(&at, nb * md->dm * 4);
+ t->o_ngeb = take(&at, nb * md->dm * 4);
+ t->o_nggv = take(&at, nb * hd * 4);
+ t->o_ngemb = take(&at, nb * md->dm * 4);
+ t->o_ngw = take(&at, hd * md->ng_kernel * 4);
+ }
+ }
if (md->gated_q)
t->o_gate = take(&at, nb * qd * 4);
if (md->n_rec) {
@@ -275,14 +394,14 @@ struct janas_gpu_tok *janas_gpu_tok_create(struct janas_gpu *g,
/* the pairs of a block of nb tokens, k each; at most a tile of 16
of them per expert more than if they were all whole tiles */
size_t np = nb * md->k, nbd = md->dm / JANAS_QK,
- nbf = md->eff / JANAS_QK;
+ nbf = t->effp / JANAS_QK;
t->o_r = take(&at, nb * (ne + 1) * 4);
t->o_pe = take(&at, np * 4);
t->o_pw = take(&at, np * 4);
t->o_shw = take(&at, nb * 4);
t->o_sh = take(&at, nb * md->dm * 4);
t->o_xg = take(&at, np * nbd * 320);
- t->o_egu = take(&at, np * md->eff * 2 * 4);
+ t->o_egu = take(&at, np * t->effp * 2 * 4);
t->o_eh = take(&at, np * nbf * 320);
t->o_dout = take(&at, np * md->dm * 4);
t->o_gt = take(&at, np * 4);
@@ -305,8 +424,8 @@ struct janas_gpu_tok *janas_gpu_tok_create(struct janas_gpu *g,
d32 |= janas_g_x_in_32(ne ? md->layers[l].sh_down.type
: md->layers[l].down.type);
if (d32) {
- t->o_hf = take(&at, nb * md->ff * 4);
- t->o_h32 = take(&at, nb * md->ff / JANAS_QK * 320);
+ t->o_hf = take(&at, nb * t->ffp * 4);
+ t->o_h32 = take(&at, nb * t->ffp / JANAS_QK * 320);
}
if (md->n_rec || ne) {
t->par = calloc((size_t)md->n_layer * PAR_N, sizeof(size_t));
@@ -378,6 +497,19 @@ struct janas_gpu_tok *janas_gpu_tok_create(struct janas_gpu *g,
janas_gpu_tok_destroy(g, t);
return NULL;
}
+ /* zero: the padding of the feed-forwards' widths is never written */
+ memset(t->buf.map, 0, at);
+ janas_gt_sync_range(g, t, 0, at, 1);
+ if (md->hc && janas_gt_hc_create(g, t, md) != 0) {
+ fail(err, err_len, "cannot create the streams' shaders");
+ janas_gpu_tok_destroy(g, t);
+ return NULL;
+ }
+ if (md->ix_heads && janas_gt_qsa_create(g, t) != 0) {
+ fail(err, err_len, "cannot create the sparse attention's shaders");
+ janas_gpu_tok_destroy(g, t);
+ return NULL;
+ }
/* the recurrent layers' parameters, copied once */
for (uint32_t l = 0; md->n_rec && l < md->n_layer; l++) {
const struct janas_gpu_tok_layer *ly = &md->layers[l];
@@ -458,6 +590,8 @@ void janas_gpu_tok_destroy(struct janas_gpu *g, struct janas_gpu_tok *t)
VkShaderModule s[15] = {t->sm_nq, t->sm_qk, t->sm_at, t->sm_am, t->sm_gc,
t->sm_gs, t->sm_gl, t->sm_gr, t->sm_gg, t->sm_tk,
t->sm_ga, t->sm_cb, t->sm_sm, t->sm_gb, t->sm_gx};
+ janas_gt_hc_destroy(g, t);
+ janas_gt_qsa_destroy(g, t);
free(t->par);
free(t->cnt);
free(t->fill);
@@ -507,6 +641,89 @@ static uint32_t runs_for(const struct janas_gpu_tok *t, uint32_t npos)
return r < t->nch ? r : t->nch;
}
+/* qwen4exp's layers (record's, for a model with hc streams), as
+ model_hc.c's janas_m_hc_layers: the streams set to the embeddings; in
+ each layer the n-gram embeddings where they are, the mix into the token
+ mixer's input, the DeltaNet or the attention (the attention's input at
+ bf16 kept for the CPU's indexer, att_in), its output combined into the
+ streams, the mix into the feed-forward's, the router - the experts and
+ their combine in the next submission - then the output's mix and head.
+ seg as record's. */
+static int record_hc(struct janas_gpu *g, struct janas_gpu_tok *t,
+ const struct janas_gpu_tok_model *md,
+ const struct janas_gpu_tok_layer *layers,
+ const struct janas_matvec_task *output, uint32_t n,
+ uint32_t first, int hid, int seg, VkPipeline *bound)
+{
+ VkDeviceAddress B = t->buf.addr, XNF = B + t->o_xnf, YWO = B + t->o_ywo,
+ YD = B + t->o_yd;
+ uint32_t nl = md->n_layer;
+ for (uint32_t l = 0; l < nl; l++) {
+ const struct janas_gpu_tok_layer *ly = &layers[l];
+ if (seg >= 0 && seg != (int)l)
+ continue;
+ if (l == 0) {
+ janas_gt_rec_hc_combine(g, t, md, bound, B + t->o_x, 1, n);
+ } else {
+ janas_gt_rec_experts(g, t, md, &layers[l - 1], bound, YD, n);
+ janas_gt_rec_hc_combine(g, t, md, bound, YD, 0, n);
+ }
+ if (l == md->ng_layer && janas_gt_rec_ng(g, t, md, ly, bound, n) != 0)
+ return -1;
+ janas_gt_rec_hc_mix(g, t, md, ly, 0, bound, XNF, n);
+ janas_gt_stamp(g, t, PH_NORM);
+ if (ly->rec) {
+ if (janas_gt_rec_gdn(g, t, md, ly, l, bound, YWO, n) != 0)
+ return -1;
+ } else {
+ if (md->att_in) {
+ VkDeviceAddress A = janas_gt_addr_of(
+ g, md->att_in, (size_t)(ly->slot + 1) * t->nb * md->dm * 4);
+ if (!A)
+ return -1;
+ struct push_ha pa = {.n = n * md->dm, .silu = 0};
+ janas_g_push_addr(pa.x, XNF);
+ janas_g_push_addr(pa.xb, A + (VkDeviceAddress)ly->slot * t->nb *
+ md->dm * 4);
+ janas_gt_bind(g, bound, t->p_ha);
+ janas_gt_push(g, &pa, sizeof(pa));
+ g->vkCmdDispatch(g->cb, (pa.n + 255) / 256, 1, 1);
+ janas_g_compute_barrier(g);
+ /* the sparse attention's positions, from that input */
+ if (md->ix_heads &&
+ janas_gt_rec_qsa(g, t, md, ly, bound,
+ A + (VkDeviceAddress)ly->slot * t->nb *
+ md->dm * 4,
+ n) != 0)
+ return -1;
+ }
+ if (janas_gt_rec_attn(g, t, md, ly, bound, n) != 0)
+ return -1;
+ }
+ janas_gt_rec_hc_combine(g, t, md, bound, YWO, 0, n);
+ janas_gt_rec_hc_mix(g, t, md, ly, 1, bound, XNF, n);
+ janas_gt_stamp(g, t, PH_NORM);
+ if (janas_gt_rec_router(g, t, md, ly, l, bound, n) != 0)
+ return -1;
+ }
+ if (seg < 0 || seg == (int)nl) {
+ janas_gt_rec_experts(g, t, md, &layers[nl - 1], bound, YD, n);
+ janas_gt_rec_hc_combine(g, t, md, bound, YD, 0, n);
+ /* the streams themselves are the hidden states (an MTP block's
+ input): run copies them from o_res */
+ (void)hid;
+ if (first < n) {
+ janas_gt_rec_hc_mix(g, t, md, NULL, 2, bound, XNF, n);
+ if (janas_gt_rec_product(g, t, bound, output,
+ (size_t)first * (md->dm / JANAS_QK) * 320,
+ B + t->o_logits, n - first, 0) != 0)
+ return -1;
+ }
+ janas_gt_stamp(g, t, PH_OUT);
+ }
+ return 0;
+}
+
/* The commands of a block of n tokens into g->cb (the caller points it at
the block's buffer): every layer, then the output head for the tokens
from first on (none if first is n); the position of the first token
@@ -543,6 +760,12 @@ static int record(struct janas_gpu *g, struct janas_gpu_tok *t,
janas_gt_stamp(g, t, PH_N); /* the start */
}
uint32_t nl = md->n_layer;
+ if (md->hc) {
+ if (record_hc(g, t, md, layers, output, n, first, hid, seg, &bound) !=
+ 0)
+ return -1;
+ nl = 0; /* nothing more of the loops below */
+ }
for (uint32_t l = 0; l < nl; l++) {
const struct janas_gpu_tok_layer *ly = &layers[l];
int on = seg < 0 || seg == (int)l;
@@ -579,7 +802,7 @@ static int record(struct janas_gpu *g, struct janas_gpu_tok *t,
/* the last feed-forward added, the final norm, the output head for
tokens first .. n - 1 (none: the residual left in X, the next block's
keys and values already in the cache) */
- if (seg < 0 || seg == (int)nl) {
+ if (!md->hc && (seg < 0 || seg == (int)nl)) {
if (md->n_expert)
janas_gt_rec_experts(g, t, md, &layers[nl - 1], &bound, YD, n);
if (first < n || hid) {
@@ -692,6 +915,17 @@ int janas_gpu_tok_run(struct janas_gpu *g, struct janas_gpu_tok *t,
return -1;
uint32_t half = md->n_rot / 2, first = all == 1 ? 0 : all == 2 ? n : n - 1;
memcpy(t->buf.map + t->o_x, x, (size_t)n * md->dm * sizeof(float));
+ if (md->hc && md->ng_layer < md->n_layer) {
+ memcpy(t->buf.map + t->o_ngemb, md->ng_emb,
+ (size_t)n * md->dm * sizeof(float));
+ janas_gt_sync_range(g, t, t->o_ngemb,
+ (size_t)n * md->dm * sizeof(float), 1);
+ }
+ if (md->ix_heads) {
+ size_t rb = ((size_t)n + n / md->ix_ratio + 2) * md->n_rot * 4;
+ memcpy(t->buf.map + t->o_ixrope, md->ix_rope, rb);
+ janas_gt_sync_range(g, t, t->o_ixrope, rb, 1);
+ }
for (uint32_t j = 0; j < n; j++) {
uint8_t *r = t->buf.map + t->o_rope + (size_t)j * md->n_rot * 4;
memcpy(r, cs + (size_t)j * half, half * sizeof(float));
@@ -738,9 +972,11 @@ int janas_gpu_tok_run(struct janas_gpu *g, struct janas_gpu_tok *t,
memcpy(logits, t->buf.map + t->o_logits, lb);
}
if (hidden) {
- size_t hb = (size_t)n * md->dm * 4;
- janas_gt_sync_range(g, t, t->o_hid, hb, 0);
- memcpy(hidden, t->buf.map + t->o_hid, hb);
+ /* qwen4exp's: the streams after the last layer, hc dm a token */
+ size_t hb = (size_t)n * (md->hc ? md->hc : 1) * md->dm * 4,
+ ho = md->hc ? t->o_res : t->o_hid;
+ janas_gt_sync_range(g, t, ho, hb, 0);
+ memcpy(hidden, t->buf.map + ho, hb);
}
struct timespec w2;
clock_gettime(CLOCK_MONOTONIC, &w2);
diff --git a/src/llm/gpu_token.h b/src/llm/gpu_token.h
index d025010..2de43fb 100644
--- a/src/llm/gpu_token.h
+++ b/src/llm/gpu_token.h
@@ -36,7 +36,8 @@ struct janas_gpu_tok_layer {
/* a Gated DeltaNet layer (qwen3.5) in place of the attention: q|k|v and
z products, beta then alpha (2 nv x dm, f32), the convolution
(d_conv x conv_ch, transposed), dt and A (nv), the heads' norm (ds),
- the output product; its state slot */
+ the output product; its state slot (an attention layer's: its KV
+ slot, its row of att_in) */
int rec;
struct janas_matvec_task ssm_qkv, ssm_z, ssm_out;
const float *ssm_beta, *ssm_alpha, *conv_w, *dt, *ssm_a, *ssm_norm;
@@ -52,6 +53,28 @@ struct janas_gpu_tok_layer {
const float *sh_gate;
size_t off[3], poff[2];
struct janas_matvec_task sh_gate_w, sh_up, sh_down;
+ /* qwen4exp (the model's hc): its mixes of the streams in place of the
+ norms, [0] before the token mixer and [1] before the feed-forward:
+ each stream's norm (hc x dm), down (rank x hc dm), up (hc dm x rank)
+ and inject (hc x hc dm), bf16; on its n-gram layer the key product
+ (hc dm x dm), the value (dm x dm, bf16), the key's, query's and
+ conv's norms (hc dm each) and the conv's taps (hc dm x kernel,
+ channel-major) */
+ const float *hc_norm[2];
+ const uint16_t *hc_down[2], *hc_up[2], *hc_inject[2];
+ struct janas_matvec_task ng_key;
+ const uint16_t *ng_value;
+ const float *ng_nk, *ng_nq, *ng_nc, *ng_conv;
+ /* qwen4exp's indexer on an attention layer (the model's ix_heads; or
+ NULL): its query and key products (ix_heads ix_dim x dm and ix_dim x
+ dm, bf16), their norms (ix_dim), the raw keys of every position
+ (cap x ix_dim, half precision) and the pooled keys of every block
+ (cap / ix_ratio x ix_dim), the model's: the CPU reads and writes
+ them too */
+ const uint16_t *ix_q, *ix_k;
+ const float *ix_qn, *ix_kn;
+ uint16_t *ix_raw;
+ float *ix_pool;
};
struct janas_gpu_tok_model {
@@ -96,10 +119,43 @@ struct janas_gpu_tok_model {
of them, so none is read for nothing */
void (*prefetch)(void *ctx, uint32_t layer);
void *ctx;
+ /* qwen4exp: hc streams of the residual (0: one, the others), the
+ mixes' rank, the output's mix (no inject); ng_layer the n-gram
+ layer (n_layer: none), its conv's kernel and dilation, its ring of
+ inputs (ng_cap positions of hc dm floats, the model's: the CPU's
+ passes read and write it too) and the block's embeddings, which the
+ caller writes before each run (n x dm); att_in each attention
+ layer's input as the GPU made it (n_attn x the block's n x dm), for
+ the CPU's indexer keys (model_qsa.c) after the run, or NULL. The
+ feed-forwards' widths (eff, ff) may end inside a group of 256: then
+ their buffers are padded to whole groups of zeros. */
+ uint32_t hc, hc_rank;
+ /* the mixes' down and up rewritten as Q8_0 (JANAS_HC_MIX=q8: blocks
+ of 32, rows of a whole number of them), their injects still bf16 */
+ int hc_q8;
+ const float *hco_norm;
+ const uint16_t *hco_down, *hco_up;
+ uint32_t ng_layer, ng_kernel, ng_len, ng_cap;
+ float *ng_hist;
+ const float *ng_emb;
+ float *att_in;
+ /* qwen4exp's sparse attention (0 heads: none): the indexer's heads,
+ their width, the positions a token keeps past the dense ones and the
+ blocks' length; ix_rope the cos and sin (n_rot floats a row) of the
+ block's n tokens' positions, then of the first position of each
+ block from pos / ix_ratio on (n / ix_ratio + 2 rows), which the
+ caller writes before each run as model_qsa.c turns them; it needs
+ att_in */
+ uint32_t ix_heads, ix_dim, ix_top, ix_ratio;
+ const float *ix_rope;
};
struct janas_gpu_tok;
+/* The most tokens a run takes (janas_gpu_tok_max_block's), and the rows a
+ layer has in att_in. */
+#define JANAS_GPU_TOK_BLOCK 256
+
/* The most tokens of the last call a block replays before its own (see
janas_gpu_tok_run): a chain of drafts is shorter. */
#define JANAS_GPU_TOK_MAX_REPLAY 16
diff --git a/src/llm/gpu_token_hc.c b/src/llm/gpu_token_hc.c
new file mode 100644
index 0000000..5f31ea9
--- /dev/null
+++ b/src/llm/gpu_token_hc.c
@@ -0,0 +1,223 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * gpu_token_hc.c - Qwen3.8-Flash-Next's streams in the whole token on the
+ * GPU (gpu_token_impl.h): the mixes that make each sublayer's input from
+ * the four streams of the residual, the combines that add its output back,
+ * and the n-gram embeddings of its second layer, as model_hc.c computes
+ * them. The mixes' products are bf16 (or Q8_0, JANAS_HC_MIX=q8) on inputs
+ * rounded to bf16, in dense_mm.comp's tiles; the norms and sums in the GPU's
+ * order, NOT bit for bit with the CPU.
+ */
+#include "gpu_token_impl.h"
+
+#include <string.h>
+
+#if defined(JANAS_GPU_SPV) && __has_include(<vulkan/vulkan.h>)
+
+#include "hc_norm.h" /* janas_spv_hc_norm */
+#include "hc_act.h" /* janas_spv_hc_act */
+#include "hc_mean.h" /* janas_spv_hc_mean */
+#include "hc_comb.h" /* janas_spv_hc_comb */
+#include "ng_gate.h" /* janas_spv_ng_gate */
+#include "ng_conv.h" /* janas_spv_ng_conv */
+
+void janas_gt_rec_bf16(struct janas_gpu *g, VkPipeline *bound,
+ VkDeviceAddress w, VkDeviceAddress x, VkDeviceAddress y,
+ uint32_t rows, uint32_t cols, uint32_t ys, uint32_t n,
+ float scale)
+{
+ janas_gt_rec_dense(g, bound, 1, w, x, y, rows, cols, ys, n, scale);
+}
+
+void janas_gt_rec_dense(struct janas_gpu *g, VkPipeline *bound, int kind,
+ VkDeviceAddress w, VkDeviceAddress x, VkDeviceAddress y,
+ uint32_t rows, uint32_t cols, uint32_t ys, uint32_t n,
+ float scale)
+{
+ struct push_dm pd = {
+ .rows = rows, .cols = cols, .nv = n, .ys = ys, .scale = scale};
+ janas_g_push_addr(pd.w, w);
+ janas_g_push_addr(pd.x, x);
+ janas_g_push_addr(pd.y, y);
+ janas_gt_bind(g, bound, g->pipe_dm[kind]);
+ janas_gt_push(g, &pd, sizeof(pd));
+ g->vkCmdDispatch(g->cb, (rows + 63) / 64, (n + 63) / 64, 1);
+}
+
+/* x rounded to bf16 into xb (silu 0), or SiLU(x) in place and that into
+ xb (silu 1): n floats */
+static void rec_act(struct janas_gpu *g, struct janas_gpu_tok *t,
+ VkPipeline *bound, VkDeviceAddress x, VkDeviceAddress xb,
+ uint32_t n, int silu)
+{
+ struct push_ha pa = {.n = n, .silu = (uint32_t)silu};
+ janas_g_push_addr(pa.x, x);
+ janas_g_push_addr(pa.xb, xb);
+ janas_gt_bind(g, bound, t->p_ha);
+ janas_gt_push(g, &pa, sizeof(pa));
+ g->vkCmdDispatch(g->cb, (n + 255) / 256, 1, 1);
+}
+
+void janas_gt_rec_hc_mix(struct janas_gpu *g, struct janas_gpu_tok *t,
+ const struct janas_gpu_tok_model *md,
+ const struct janas_gpu_tok_layer *ly, int k,
+ VkPipeline *bound, VkDeviceAddress xnf, uint32_t n)
+{
+ VkDeviceAddress B = t->buf.addr;
+ uint32_t dm = md->dm, hc = md->hc, hd = hc * dm, r = md->hc_rank;
+ const float *norm = k < 2 ? ly->hc_norm[k] : md->hco_norm;
+ const uint16_t *down = k < 2 ? ly->hc_down[k] : md->hco_down,
+ *up = k < 2 ? ly->hc_up[k] : md->hco_up,
+ *inject = k < 2 ? ly->hc_inject[k] : NULL;
+ /* each stream normalized, and at bf16 */
+ struct push_hn pn = {.d = dm, .hc = hc, .eps = md->eps};
+ janas_g_push_addr(pn.res, B + t->o_res);
+ janas_g_push_addr(pn.w, janas_gt_addr_of(g, norm, (size_t)hd * 4));
+ janas_g_push_addr(pn.xn, B + t->o_hxn);
+ janas_g_push_addr(pn.xb, B + t->o_hxb);
+ janas_gt_bind(g, bound, t->p_hn);
+ janas_gt_push(g, &pn, sizeof(pn));
+ g->vkCmdDispatch(g->cb, hc, n, 1);
+ janas_g_compute_barrier(g);
+ /* the gates' low rank: down / hc, its SiLU and bf16; up; and inject
+ beside them, on the same input. Down and up bf16, or Q8_0 (hc_q8) */
+ int kind = md->hc_q8 ? 2 : 1;
+ size_t mb = md->hc_q8 ? (size_t)r * hd / 32 * 34 : (size_t)r * hd * 2;
+ janas_gt_rec_dense(g, bound, kind, janas_gt_addr_of(g, down, mb),
+ B + t->o_hxb, B + t->o_lo, r, hd, r, n,
+ 1.0f / (float)hc);
+ if (inject)
+ janas_gt_rec_bf16(g, bound,
+ janas_gt_addr_of(g, inject, (size_t)hc * hd * 2),
+ B + t->o_hxb, B + t->o_inj, hc, hd, hc, n, 1.0f);
+ janas_g_compute_barrier(g);
+ rec_act(g, t, bound, B + t->o_lo, B + t->o_lob, n * r, 1);
+ janas_g_compute_barrier(g);
+ janas_gt_rec_dense(g, bound, kind, janas_gt_addr_of(g, up, mb),
+ B + t->o_lob, B + t->o_hg, hd, r, hd, n, 1.0f);
+ janas_g_compute_barrier(g);
+ /* the input: the streams' mean, each times sigmoid(its gate) */
+ struct push_hm pm = {.d = dm, .hc = hc};
+ janas_g_push_addr(pm.xn, B + t->o_hxn);
+ janas_g_push_addr(pm.g, B + t->o_hg);
+ janas_g_push_addr(pm.out, B + t->o_hmean);
+ janas_gt_bind(g, bound, t->p_hm);
+ janas_gt_push(g, &pm, sizeof(pm));
+ g->vkCmdDispatch(g->cb, (dm + 255) / 256, n, 1);
+ janas_g_compute_barrier(g);
+ /* quantized for the products, and as floats */
+ janas_gt_rec_norm_quant(g, t, bound, B + t->o_hmean, 0, 0, 0, xnf, dm,
+ md->eps, n);
+}
+
+void janas_gt_rec_hc_combine(struct janas_gpu *g, struct janas_gpu_tok *t,
+ const struct janas_gpu_tok_model *md,
+ VkPipeline *bound, VkDeviceAddress out, int init,
+ uint32_t n)
+{
+ VkDeviceAddress B = t->buf.addr;
+ uint32_t hd = md->hc * md->dm;
+ struct push_hcb pc = {.d = md->dm, .hc = md->hc, .init = (uint32_t)init};
+ janas_g_push_addr(pc.res, B + t->o_res);
+ janas_g_push_addr(pc.out, out);
+ janas_g_push_addr(pc.inj, B + t->o_inj);
+ janas_gt_bind(g, bound, t->p_hcb);
+ janas_gt_push(g, &pc, sizeof(pc));
+ g->vkCmdDispatch(g->cb, (hd + 255) / 256, n, 1);
+ janas_g_compute_barrier(g);
+}
+
+int janas_gt_rec_ng(struct janas_gpu *g, struct janas_gpu_tok *t,
+ const struct janas_gpu_tok_model *md,
+ const struct janas_gpu_tok_layer *ly, VkPipeline *bound,
+ uint32_t n)
+{
+ VkDeviceAddress B = t->buf.addr;
+ uint32_t dm = md->dm, hc = md->hc, hd = hc * dm;
+ VkDeviceAddress HIST =
+ janas_gt_addr_of(g, md->ng_hist, (size_t)md->ng_cap * hd * 4);
+ if (!HIST)
+ return -1;
+ /* the embeddings (the caller's rows of the table) quantized for the
+ key product, and at bf16 for the value's */
+ janas_gt_rec_norm_quant(g, t, bound, B + t->o_ngemb, 0, 0, 0, 0, dm,
+ md->eps, n);
+ if (janas_gt_rec_product(g, t, bound, &ly->ng_key, 0, B + t->o_ngk, n,
+ hd) != 0)
+ return -1;
+ rec_act(g, t, bound, B + t->o_ngemb, B + t->o_ngeb, n * dm, 0);
+ janas_g_compute_barrier(g);
+ janas_gt_rec_bf16(g, bound,
+ janas_gt_addr_of(g, ly->ng_value, (size_t)dm * dm * 2),
+ B + t->o_ngeb, B + t->o_ngv, dm, dm, dm, n, 1.0f);
+ janas_g_compute_barrier(g);
+ struct push_ngg pg = {.d = dm, .hc = hc, .cap = md->ng_cap, .eps = md->eps};
+ janas_g_push_addr(pg.kq, B + t->o_ngk);
+ janas_g_push_addr(pg.res, B + t->o_res);
+ janas_g_push_addr(pg.val, B + t->o_ngv);
+ janas_g_push_addr(pg.nk, janas_gt_addr_of(g, ly->ng_nk, (size_t)hd * 4));
+ janas_g_push_addr(pg.nq, janas_gt_addr_of(g, ly->ng_nq, (size_t)hd * 4));
+ janas_g_push_addr(pg.nc, janas_gt_addr_of(g, ly->ng_nc, (size_t)hd * 4));
+ janas_g_push_addr(pg.gv, B + t->o_nggv);
+ janas_g_push_addr(pg.hist, HIST);
+ janas_g_push_addr(pg.pos, B + t->o_pos);
+ janas_gt_bind(g, bound, t->p_ngg);
+ janas_gt_push(g, &pg, sizeof(pg));
+ g->vkCmdDispatch(g->cb, hc, n, 1);
+ janas_g_compute_barrier(g);
+ struct push_ngc pc = {
+ .hd = hd, .cap = md->ng_cap, .k = md->ng_kernel, .len = md->ng_len};
+ janas_g_push_addr(pc.res, B + t->o_res);
+ janas_g_push_addr(pc.gv, B + t->o_nggv);
+ janas_g_push_addr(pc.w, B + t->o_ngw);
+ janas_g_push_addr(pc.hist, HIST);
+ janas_g_push_addr(pc.pos, B + t->o_pos);
+ janas_gt_bind(g, bound, t->p_ngc);
+ janas_gt_push(g, &pc, sizeof(pc));
+ g->vkCmdDispatch(g->cb, (hd + 255) / 256, n, 1);
+ janas_g_compute_barrier(g);
+ return 0;
+}
+
+int janas_gt_hc_create(struct janas_gpu *g, struct janas_gpu_tok *t,
+ const struct janas_gpu_tok_model *md)
+{
+ if (janas_g_make_pipeline(g, janas_spv_hc_norm, sizeof(janas_spv_hc_norm),
+ &t->sm_hn, &t->p_hn, 0) != VK_SUCCESS ||
+ janas_g_make_pipeline(g, janas_spv_hc_act, sizeof(janas_spv_hc_act),
+ &t->sm_ha, &t->p_ha, 0) != VK_SUCCESS ||
+ janas_g_make_pipeline(g, janas_spv_hc_mean, sizeof(janas_spv_hc_mean),
+ &t->sm_hm, &t->p_hm, 0) != VK_SUCCESS ||
+ janas_g_make_pipeline(g, janas_spv_hc_comb, sizeof(janas_spv_hc_comb),
+ &t->sm_hcb, &t->p_hcb, 0) != VK_SUCCESS ||
+ janas_g_make_pipeline(g, janas_spv_ng_gate, sizeof(janas_spv_ng_gate),
+ &t->sm_ngg, &t->p_ngg, 0) != VK_SUCCESS ||
+ janas_g_make_pipeline(g, janas_spv_ng_conv, sizeof(janas_spv_ng_conv),
+ &t->sm_ngc, &t->p_ngc, 0) != VK_SUCCESS ||
+ !g->pipe_dm[1])
+ return -1;
+ /* the n-gram layer's conv taps, copied once (the model's own are not
+ where the GPU reads) */
+ if (md->ng_layer < md->n_layer) {
+ size_t bytes = (size_t)md->hc * md->dm * md->ng_kernel * 4;
+ memcpy(t->buf.map + t->o_ngw, md->layers[md->ng_layer].ng_conv, bytes);
+ janas_gt_sync_range(g, t, t->o_ngw, bytes, 1);
+ }
+ return 0;
+}
+
+void janas_gt_hc_destroy(struct janas_gpu *g, struct janas_gpu_tok *t)
+{
+ VkPipeline p[6] = {t->p_hn, t->p_ha, t->p_hm, t->p_hcb, t->p_ngg, t->p_ngc};
+ VkShaderModule s[6] = {t->sm_hn, t->sm_ha, t->sm_hm,
+ t->sm_hcb, t->sm_ngg, t->sm_ngc};
+ for (int i = 0; i < 6; i++) {
+ if (p[i])
+ g->vkDestroyPipeline(g->dev, p[i], NULL);
+ if (s[i])
+ g->vkDestroyShaderModule(g->dev, s[i], NULL);
+ }
+}
+
+#endif
diff --git a/src/llm/gpu_token_impl.h b/src/llm/gpu_token_impl.h
index ee33e09..b590ef8 100644
--- a/src/llm/gpu_token_impl.h
+++ b/src/llm/gpu_token_impl.h
@@ -33,13 +33,17 @@ struct push_at {
uint32_t n_head, n_kv, hd, cap, nch;
float scale;
uint32_t qs, ps;
- uint32_t pos[2]; /* the position's buffer */
+ uint32_t pos[2]; /* the position's buffer */
+ uint32_t list[2]; /* qwen4exp's positions a token (ix_select.comp) */
+ uint32_t ls, pad;
};
struct push_am {
uint32_t part[2], q256[2], q32[2];
uint32_t hd, nch, ps, gs;
uint32_t pos[2]; /* 8-byte aligned, as the GLSL block puts it */
uint32_t gate[2];
+ uint32_t list[2]; /* as push_at's */
+ uint32_t ls, pad;
};
/* the Gated DeltaNet shaders (gdn_*.comp) */
struct push_gconv {
@@ -73,6 +77,7 @@ struct push_ggate {
uint32_t att[2], z[2], w[2], q256[2], q32[2], bd[2], lb[2], ld[2], pos[2];
uint32_t d, nv;
float eps;
+ uint32_t sig; /* sigmoid(z) in place of SiLU(z) (qwen4exp) */
};
struct push_dm { /* gpu.c's dense_mm.comp */
uint32_t w[2], x[2], y[2];
@@ -95,17 +100,70 @@ struct push_cb {
struct push_id {
uint32_t t[2];
uint32_t rows, nb, tiles;
+ uint32_t ngroups; /* b32_mmid.comp: the groups of 32 a row */
};
struct push_sm {
uint32_t g[2], u[2], h[2];
uint32_t n;
};
+/* qwen4exp's mixes and n-gram embeddings (hc_*.comp, ng_*.comp) */
+struct push_hn {
+ uint32_t res[2], w[2], xn[2], xb[2];
+ uint32_t d, hc;
+ float eps;
+};
+struct push_ha {
+ uint32_t x[2], xb[2];
+ uint32_t n, silu;
+};
+struct push_hm {
+ uint32_t xn[2], g[2], out[2];
+ uint32_t d, hc;
+};
+struct push_hcb {
+ uint32_t res[2], out[2], inj[2];
+ uint32_t d, hc, init;
+};
+struct push_ngg {
+ uint32_t kq[2], res[2], val[2], nk[2], nq[2], nc[2], gv[2], hist[2], pos[2];
+ uint32_t d, hc, cap;
+ float eps;
+};
+struct push_ngc {
+ uint32_t res[2], gv[2], w[2], hist[2], pos[2];
+ uint32_t hd, cap, k, len;
+};
+/* qwen4exp's indexer (ix_*.comp, gpu_token_qsa.c) */
+struct push_ixp {
+ uint32_t q[2], k[2], qn[2], rope[2], raw[2], pos[2];
+ uint32_t nh, d, n_rot, qs;
+ float eps;
+};
+struct push_ixl {
+ uint32_t raw[2], kn[2], rope[2], pool[2], pos[2];
+ uint32_t d, r, n, n_rot, rb;
+ float eps;
+};
+struct push_ixs {
+ uint32_t q[2], pool[2], sc[2], pos[2];
+ uint32_t nh, d, r, keep, nbs, qs;
+};
+struct push_ixc {
+ uint32_t sc[2], list[2], pos[2];
+ uint32_t r, keep, nbs, ls;
+};
+_Static_assert(sizeof(struct push_ngg) <= JANAS_G_PUSH_BYTES, "push size");
+_Static_assert(sizeof(struct push_at) <= JANAS_G_PUSH_BYTES, "push size");
/* The attention's positions a run (attn_decode.comp C), and the shapes its
shared memory takes: the query heads of a KV head times hd, and times a
run. */
#define ATT_RUN 128
#define ATT_MAXQ 2048
#define ATT_MAXS 1024
+/* ... larger where a KV head has more than 8 query heads (attn_decode.comp
+ MAXQ, MAXS) */
+#define ATT_MAXQ_G(g) ((g) * 256u > ATT_MAXQ ? (g) * 256u : ATT_MAXQ)
+#define ATT_MAXS_G(g) ((g) * ATT_RUN > ATT_MAXS ? (g) * ATT_RUN : ATT_MAXS)
struct push_ac {
uint32_t g[2], u[2], x[2];
@@ -127,7 +185,7 @@ _Static_assert(sizeof(struct push_cb) <= JANAS_G_PUSH_BYTES, "push size");
/* The most tokens a block takes (the model's own blocks: model.h
JANAS_LLM_MAX_BLOCK), and the memory the attention's runs may take for
them. */
-#define TOK_MAX_BLOCK 256
+#define TOK_MAX_BLOCK JANAS_GPU_TOK_BLOCK
#define TOK_PART_BUDGET ((size_t)64 << 20)
/* JANAS_GPU_TOKEN_PROFILE=1: the GPU's own timestamps after each phase of
@@ -219,6 +277,26 @@ struct janas_gpu_tok {
slots */
uint32_t *cnt, *fill, *ids;
const uint8_t **slots;
+ /* the feed-forwards' widths padded to whole groups of 256: the
+ experts' (effp) and the shared expert's or dense one's (ffp) */
+ uint32_t effp, ffp;
+ /* qwen4exp: the streams (hc dm a token), each normalized and at bf16,
+ the mixes' low rank and its bf16, their gates, injects and means;
+ the n-gram layer's key products, values, the embeddings at bf16 and
+ the gated values; their shaders (gpu_token_hc.c) */
+ size_t o_res, o_hxn, o_hxb, o_lo, o_lob, o_hg, o_inj, o_hmean, o_ngk, o_ngv,
+ o_ngeb, o_nggv, o_ngemb, o_ngw;
+ VkShaderModule sm_hn, sm_ha, sm_hm, sm_hcb, sm_ngg, sm_ngc;
+ VkPipeline p_hn, p_ha, p_hm, p_hcb, p_ngg, p_ngc;
+ /* qwen4exp's indexer: the block's queries and keys, the cos and sin of
+ the tokens' positions and the blocks' (the caller's), the scores of
+ every block for each token (nbs a token), the positions each attends
+ to (ls words a token), the runs that take them; its shaders
+ (gpu_token_qsa.c) */
+ size_t o_ixq, o_ixk, o_ixrope, o_ixsc, o_ixlist;
+ uint32_t ix_nbs, ix_ls, ix_runs;
+ VkShaderModule sm_ixp, sm_ixl, sm_ixs, sm_ixc;
+ VkPipeline p_ixp, p_ixl, p_ixs, p_ixc;
};
/* A recurrent layer's parameters copied into buf: beta then alpha (2 nv x
@@ -269,6 +347,50 @@ int janas_gt_rec_ffn(struct janas_gpu *g, struct janas_gpu_tok *t,
const struct janas_matvec_task *down, uint32_t n,
VkDeviceAddress out);
+/* gpu_token_hc.c: qwen4exp's streams. A mix (k 0 before the token mixer,
+ 1 before the feed-forward, 2 the output's) of the streams into the
+ sublayer's input, quantized in q256 / q32 and as floats in xnf, its
+ inject (none for the output's) into o_inj; a combine of the sublayer's
+ output (out) into the streams; the n-gram layer; the streams set to the
+ embeddings at x. */
+void janas_gt_rec_hc_mix(struct janas_gpu *g, struct janas_gpu_tok *t,
+ const struct janas_gpu_tok_model *md,
+ const struct janas_gpu_tok_layer *ly, int k,
+ VkPipeline *bound, VkDeviceAddress xnf, uint32_t n);
+void janas_gt_rec_hc_combine(struct janas_gpu *g, struct janas_gpu_tok *t,
+ const struct janas_gpu_tok_model *md,
+ VkPipeline *bound, VkDeviceAddress out, int init,
+ uint32_t n);
+int janas_gt_rec_ng(struct janas_gpu *g, struct janas_gpu_tok *t,
+ const struct janas_gpu_tok_model *md,
+ const struct janas_gpu_tok_layer *ly, VkPipeline *bound,
+ uint32_t n);
+int janas_gt_hc_create(struct janas_gpu *g, struct janas_gpu_tok *t,
+ const struct janas_gpu_tok_model *md);
+void janas_gt_hc_destroy(struct janas_gpu *g, struct janas_gpu_tok *t);
+/* y (n rows of ys) = scale w x (n rows of cols floats): w bf16, rows x
+ cols, in dense_mm.comp's tiles */
+void janas_gt_rec_bf16(struct janas_gpu *g, VkPipeline *bound,
+ VkDeviceAddress w, VkDeviceAddress x, VkDeviceAddress y,
+ uint32_t rows, uint32_t cols, uint32_t ys, uint32_t n,
+ float scale);
+/* the same for w of dense_mm.comp's kind: 1 bf16, 2 Q8_0 */
+void janas_gt_rec_dense(struct janas_gpu *g, VkPipeline *bound, int kind,
+ VkDeviceAddress w, VkDeviceAddress x, VkDeviceAddress y,
+ uint32_t rows, uint32_t cols, uint32_t ys, uint32_t n,
+ float scale);
+
+/* gpu_token_qsa.c: qwen4exp's indexer for an attention layer, on its input
+ at bf16 (xb, n rows of dm): the keys and queries, the blocks the block
+ completes, the scores and the positions each token attends to, which
+ janas_gt_rec_attn's runs then read */
+int janas_gt_rec_qsa(struct janas_gpu *g, struct janas_gpu_tok *t,
+ const struct janas_gpu_tok_model *md,
+ const struct janas_gpu_tok_layer *ly, VkPipeline *bound,
+ VkDeviceAddress xb, uint32_t n);
+int janas_gt_qsa_create(struct janas_gpu *g, struct janas_gpu_tok *t);
+void janas_gt_qsa_destroy(struct janas_gpu *g, struct janas_gpu_tok *t);
+
/* gpu_token_moe.c: a mixture's layers */
int janas_gt_rec_router(struct janas_gpu *g, struct janas_gpu_tok *t,
const struct janas_gpu_tok_model *md,
diff --git a/src/llm/gpu_token_moe.c b/src/llm/gpu_token_moe.c
index 16a4640..7646075 100644
--- a/src/llm/gpu_token_moe.c
+++ b/src/llm/gpu_token_moe.c
@@ -79,8 +79,8 @@ void janas_gt_rec_experts(struct janas_gpu *g, struct janas_gpu_tok *t,
VkPipeline *bound, VkDeviceAddress YD, uint32_t n)
{
VkDeviceAddress B = t->buf.addr;
- uint32_t np = n * md->k, nbd = md->dm / JANAS_QK, nbf = md->eff / JANAS_QK;
- int dt = ly->down.type;
+ uint32_t np = n * md->k, nbd = md->dm / JANAS_QK, nbf = t->effp / JANAS_QK;
+ int dt = ly->down.type, gtp = ly->gate.type;
struct push_ga pg = {.nb = nbd};
janas_g_push_addr(
pg.src, B + (janas_g_x_in_32(ly->gate.type) ? t->o_q32 : t->o_q256));
@@ -90,30 +90,34 @@ void janas_gt_rec_experts(struct janas_gpu *g, struct janas_gpu_tok *t,
janas_gt_push(g, &pg, sizeof(pg));
g->vkCmdDispatch(g->cb, np, 1, 1);
janas_g_compute_barrier(g);
- struct push_id pi = {.rows = md->eff, .nb = nbd, .tiles = t->tiles1};
+ struct push_id pi = {
+ .rows = md->eff, .nb = nbd, .tiles = t->tiles1, .ngroups = md->dm / 32};
janas_g_push_addr(pi.t, B + t->o_tab1);
janas_gt_bind(g, bound,
- ly->gate.type == JANAS_MXFP4 ? g->pipe_idmx
- : ly->gate.type == JANAS_Q3_K ? g->pipe_id3
- : g->pipe_id4);
+ gtp == JANAS_MXFP4 ? g->pipe_idmx
+ : gtp == JANAS_Q3_K ? g->pipe_id3
+ : gtp == JANAS_Q2_0 ? g->pipe_idb[2]
+ : g->pipe_id4);
janas_gt_push(g, &pi, sizeof(pi));
g->vkCmdDispatchIndirect(g->cb, t->buf.buf, t->o_ind);
janas_g_compute_barrier(g);
struct push_ac ac = {.blocks = np * nbf};
janas_g_push_addr(ac.g, B + t->o_egu);
- janas_g_push_addr(ac.u, B + t->o_egu + (size_t)np * md->eff * 4);
+ janas_g_push_addr(ac.u, B + t->o_egu + (size_t)np * t->effp * 4);
janas_g_push_addr(ac.x, B + t->o_eh);
janas_gt_bind(g, bound, janas_g_x_in_32(dt) ? g->pipea32 : g->pipea);
janas_gt_push(g, &ac, sizeof(ac));
g->vkCmdDispatch(g->cb, np * nbf, 1, 1);
janas_g_compute_barrier(g);
- pi = (struct push_id){.rows = md->dm, .nb = nbf, .tiles = t->tiles2};
+ pi = (struct push_id){
+ .rows = md->dm, .nb = nbf, .tiles = t->tiles2, .ngroups = md->eff / 32};
janas_g_push_addr(pi.t, B + t->o_tab2);
janas_gt_bind(g, bound,
dt == JANAS_Q4_K ? g->pipe_id4
: dt == JANAS_Q6_K ? g->pipe_id6s
: dt == JANAS_MXFP4 ? g->pipe_idmx
: dt == JANAS_Q3_K ? g->pipe_id3
+ : dt == JANAS_Q2_0 ? g->pipe_idb[2]
: g->pipe_id6);
janas_gt_push(g, &pi, sizeof(pi));
g->vkCmdDispatchIndirect(g->cb, t->buf.buf, t->o_ind + 16);
@@ -159,7 +163,7 @@ static int plan(struct janas_gpu *g, struct janas_gpu_tok *t,
{
const struct janas_gpu_tok_layer *ly = &md->layers[l];
uint32_t k = md->k, ne = md->n_expert, np = n * k, nd = 0;
- uint32_t nbd = md->dm / JANAS_QK, nbf = md->eff / JANAS_QK;
+ uint32_t nbd = md->dm / JANAS_QK, nbf = t->effp / JANAS_QK, ep = t->effp;
janas_gt_sync_range(g, t, t->o_pe, (size_t)np * 4, 0);
const uint32_t *pe = (const uint32_t *)(t->buf.map + t->o_pe);
uint32_t *cnt = t->cnt, *gt = (uint32_t *)(t->buf.map + t->o_gt),
@@ -189,9 +193,10 @@ static int plan(struct janas_gpu *g, struct janas_gpu_tok *t,
uint32_t *t1 = (uint32_t *)(t->buf.map + t->o_tab1),
*t2 = (uint32_t *)(t->buf.map + t->o_tab2);
uint32_t n1 = 0, n2 = 0; /* tiles */
- size_t gb = (size_t)md->eff * nbd * janas_qtype_block_size(ly->gate.type),
- db =
- (size_t)md->dm * nbf * janas_qtype_block_size(ly->down.type) + 4,
+ /* the matrices' bytes; a down matrix's rows may end inside a group */
+ size_t gb = (size_t)md->eff * janas_qtype_row_bytes(ly->gate.type, md->dm),
+ db = (size_t)md->dm * janas_qtype_row_bytes(ly->down.type, md->eff) +
+ 4,
pb = (size_t)md->dm * nbf * JANAS_Q6KP_PLANE;
for (uint32_t i = 0; i < nd; i++) {
uint32_t e = t->ids[i], a = cnt[e], nv = cnt[e + 1] - a;
@@ -209,12 +214,12 @@ static int plan(struct janas_gpu *g, struct janas_gpu_tok *t,
if (!wg || !wu || !wd)
return -1;
VkDeviceAddress x = B + t->o_xg + (VkDeviceAddress)a * nbd * 320;
+ /* gate and up at ep floats a pair: the rows past eff stay zero */
table_entry(t1 + 12 * (2 * i), wg, x,
- B + t->o_egu + (VkDeviceAddress)a * md->eff * 4, nv,
- md->eff, 0, 0);
+ B + t->o_egu + (VkDeviceAddress)a * ep * 4, nv, ep, 0, 0);
table_entry(t1 + 12 * (2 * i + 1), wu, x,
- B + t->o_egu + ((VkDeviceAddress)np + a) * md->eff * 4, nv,
- md->eff, 0, 0);
+ B + t->o_egu + ((VkDeviceAddress)np + a) * ep * 4, nv, ep,
+ 0, 0);
table_entry(t2 + 12 * i, wd,
B + t->o_eh + (VkDeviceAddress)a * nbf * 320,
B + t->o_dout + (VkDeviceAddress)a * md->dm * 4, nv, md->dm,
diff --git a/src/llm/gpu_token_qsa.c b/src/llm/gpu_token_qsa.c
new file mode 100644
index 0000000..6141a50
--- /dev/null
+++ b/src/llm/gpu_token_qsa.c
@@ -0,0 +1,122 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * gpu_token_qsa.c - Qwen3.8-Flash-Next's sparse attention in the whole token
+ * on the GPU (gpu_token_impl.h), as model_qsa.c computes it: in each
+ * attention layer the indexer's keys and queries from the layer's input at
+ * bf16 (dense_mm.comp), the queries normalized and turned and the raw keys
+ * kept (ix_prep.comp), the blocks the run completes pooled (ix_pool.comp),
+ * each token's scores of the blocks before it (ix_score.comp) and the
+ * positions it attends to (ix_select.comp): every one while its blocks are
+ * no more than the budget, else the best blocks and its own block's
+ * positions. The attention's runs then read those positions. Sums in the
+ * GPU's order: a block whose score ties with another's on the CPU may lose
+ * here, as llama.cpp's choice differs from both.
+ */
+#include "gpu_token_impl.h"
+
+#if defined(JANAS_GPU_SPV) && __has_include(<vulkan/vulkan.h>)
+
+#include "ix_pool.h" /* janas_spv_ix_pool */
+#include "ix_prep.h" /* janas_spv_ix_prep */
+#include "ix_score.h" /* janas_spv_ix_score */
+#include "ix_select.h" /* janas_spv_ix_select */
+
+int janas_gt_rec_qsa(struct janas_gpu *g, struct janas_gpu_tok *t,
+ const struct janas_gpu_tok_model *md,
+ const struct janas_gpu_tok_layer *ly, VkPipeline *bound,
+ VkDeviceAddress xb, uint32_t n)
+{
+ VkDeviceAddress B = t->buf.addr, POS = B + t->o_pos;
+ uint32_t dm = md->dm, d = md->ix_dim, nh = md->ix_heads, r = md->ix_ratio;
+ VkDeviceAddress RAW = janas_gt_addr_of(g, ly->ix_raw,
+ (size_t)md->cap * d * 2),
+ POOL = janas_gt_addr_of(g, ly->ix_pool,
+ (size_t)t->ix_nbs * d * 4),
+ WQ = janas_gt_addr_of(g, ly->ix_q, (size_t)nh * d * dm * 2),
+ WK = janas_gt_addr_of(g, ly->ix_k, (size_t)d * dm * 2),
+ QN = janas_gt_addr_of(g, ly->ix_qn, (size_t)d * 4),
+ KN = janas_gt_addr_of(g, ly->ix_kn, (size_t)d * 4);
+ if (!RAW || !POOL || !WQ || !WK || !QN || !KN)
+ return -1;
+ /* the queries and keys */
+ janas_gt_rec_bf16(g, bound, WQ, xb, B + t->o_ixq, nh * d, dm, nh * d, n,
+ 1.0f);
+ janas_gt_rec_bf16(g, bound, WK, xb, B + t->o_ixk, d, dm, d, n, 1.0f);
+ janas_g_compute_barrier(g);
+ struct push_ixp pp = {
+ .nh = nh, .d = d, .n_rot = md->n_rot, .qs = nh * d, .eps = md->eps};
+ janas_g_push_addr(pp.q, B + t->o_ixq);
+ janas_g_push_addr(pp.k, B + t->o_ixk);
+ janas_g_push_addr(pp.qn, QN);
+ janas_g_push_addr(pp.rope, B + t->o_ixrope);
+ janas_g_push_addr(pp.raw, RAW);
+ janas_g_push_addr(pp.pos, POS);
+ janas_gt_bind(g, bound, t->p_ixp);
+ janas_gt_push(g, &pp, sizeof(pp));
+ g->vkCmdDispatch(g->cb, nh + 1, n, 1);
+ janas_g_compute_barrier(g);
+ /* the blocks completed: at most n / r + 1 of them from pos / r on */
+ struct push_ixl pl = {
+ .d = d, .r = r, .n = n, .n_rot = md->n_rot, .rb = n, .eps = md->eps};
+ janas_g_push_addr(pl.raw, RAW);
+ janas_g_push_addr(pl.kn, KN);
+ janas_g_push_addr(pl.rope, B + t->o_ixrope);
+ janas_g_push_addr(pl.pool, POOL);
+ janas_g_push_addr(pl.pos, POS);
+ janas_gt_bind(g, bound, t->p_ixl);
+ janas_gt_push(g, &pl, sizeof(pl));
+ g->vkCmdDispatch(g->cb, n / r + 2, 1, 1);
+ janas_g_compute_barrier(g);
+ uint32_t keep = md->ix_top / r;
+ struct push_ixs ps = {
+ .nh = nh, .d = d, .r = r, .keep = keep, .nbs = t->ix_nbs, .qs = nh * d};
+ janas_g_push_addr(ps.q, B + t->o_ixq);
+ janas_g_push_addr(ps.pool, POOL);
+ janas_g_push_addr(ps.sc, B + t->o_ixsc);
+ janas_g_push_addr(ps.pos, POS);
+ janas_gt_bind(g, bound, t->p_ixs);
+ janas_gt_push(g, &ps, sizeof(ps));
+ g->vkCmdDispatch(g->cb, (t->ix_nbs + 255) / 256, n, 1);
+ janas_g_compute_barrier(g);
+ struct push_ixc pc = {
+ .r = r, .keep = keep, .nbs = t->ix_nbs, .ls = t->ix_ls};
+ janas_g_push_addr(pc.sc, B + t->o_ixsc);
+ janas_g_push_addr(pc.list, B + t->o_ixlist);
+ janas_g_push_addr(pc.pos, POS);
+ janas_gt_bind(g, bound, t->p_ixc);
+ janas_gt_push(g, &pc, sizeof(pc));
+ g->vkCmdDispatch(g->cb, 1, n, 1);
+ janas_g_compute_barrier(g);
+ return 0;
+}
+
+int janas_gt_qsa_create(struct janas_gpu *g, struct janas_gpu_tok *t)
+{
+ if (janas_g_make_pipeline(g, janas_spv_ix_prep, sizeof(janas_spv_ix_prep),
+ &t->sm_ixp, &t->p_ixp, 0) != VK_SUCCESS ||
+ janas_g_make_pipeline(g, janas_spv_ix_pool, sizeof(janas_spv_ix_pool),
+ &t->sm_ixl, &t->p_ixl, 0) != VK_SUCCESS ||
+ janas_g_make_pipeline(g, janas_spv_ix_score, sizeof(janas_spv_ix_score),
+ &t->sm_ixs, &t->p_ixs, 0) != VK_SUCCESS ||
+ janas_g_make_pipeline(g, janas_spv_ix_select,
+ sizeof(janas_spv_ix_select), &t->sm_ixc,
+ &t->p_ixc, 0) != VK_SUCCESS ||
+ !g->pipe_dm[1])
+ return -1;
+ return 0;
+}
+
+void janas_gt_qsa_destroy(struct janas_gpu *g, struct janas_gpu_tok *t)
+{
+ VkPipeline p[4] = {t->p_ixp, t->p_ixl, t->p_ixs, t->p_ixc};
+ VkShaderModule s[4] = {t->sm_ixp, t->sm_ixl, t->sm_ixs, t->sm_ixc};
+ for (int i = 0; i < 4; i++) {
+ if (p[i])
+ g->vkDestroyPipeline(g->dev, p[i], NULL);
+ if (s[i])
+ g->vkDestroyShaderModule(g->dev, s[i], NULL);
+ }
+}
+
+#endif
diff --git a/src/llm/gpu_token_rec.c b/src/llm/gpu_token_rec.c
index aa53a81..23cc0f2 100644
--- a/src/llm/gpu_token_rec.c
+++ b/src/llm/gpu_token_rec.c
@@ -219,7 +219,8 @@ int janas_gt_rec_gdn(struct janas_gpu *g, struct janas_gpu_tok *t,
g->vkCmdDispatch(g->cb, md->nv * (md->ds / 16), 1, 1);
janas_g_compute_barrier(g);
janas_gt_stamp(g, t, PH_SSM_REC);
- struct push_ggate pq = {.d = md->d_inner, .nv = md->nv, .eps = md->eps};
+ struct push_ggate pq = {
+ .d = md->d_inner, .nv = md->nv, .eps = md->eps, .sig = md->hc != 0};
janas_g_push_addr(pq.att, ATT);
janas_g_push_addr(pq.z, Z);
janas_g_push_addr(pq.w, B + pr[PAR_NORM]);
@@ -312,8 +313,12 @@ int janas_gt_rec_attn(struct janas_gpu *g, struct janas_gpu_tok *t,
.nch = t->rec_nch,
.scale = md->attn_scale,
.qs = (uint32_t)qs,
- .ps = (uint32_t)janas_gt_part_tok(t, md, t->rec_nch)};
+ .ps = (uint32_t)janas_gt_part_tok(t, md, t->rec_nch),
+ .ls = t->ix_ls};
janas_g_push_addr(pa.pos, POS);
+ /* qwen4exp's sparse attention: the positions janas_gt_rec_qsa chose */
+ if (md->ix_heads)
+ janas_g_push_addr(pa.list, B + t->o_ixlist);
janas_g_push_addr(pa.q, QKV);
janas_g_push_addr(pa.kc, janas_gt_addr_of(g, ly->kc, cache));
janas_g_push_addr(pa.vc, janas_gt_addr_of(g, ly->vc, cache));
@@ -330,8 +335,11 @@ int janas_gt_rec_attn(struct janas_gpu *g, struct janas_gpu_tok *t,
struct push_am pm = {.hd = md->hd,
.nch = t->rec_nch,
.ps = (uint32_t)janas_gt_part_tok(t, md, t->rec_nch),
- .gs = qd};
+ .gs = qd,
+ .ls = t->ix_ls};
janas_g_push_addr(pm.gate, md->gated_q ? B + t->o_gate : 0);
+ if (md->ix_heads)
+ janas_g_push_addr(pm.list, B + t->o_ixlist);
janas_g_push_addr(pm.part, B + t->o_part);
janas_g_push_addr(pm.q256, B + t->o_q256);
janas_g_push_addr(pm.q32, B + t->o_q32);
@@ -379,14 +387,17 @@ int janas_gt_rec_ffn(struct janas_gpu *g, struct janas_gpu_tok *t,
VkDeviceAddress out)
{
VkDeviceAddress B = t->buf.addr, GU = B + t->o_gu, H = B + t->o_h, XA = H;
- VkDeviceAddress UP = GU + (size_t)n * gate->rows * 4;
- if (janas_gt_rec_product(g, t, bound, gate, 0, GU, n, 0) != 0 ||
- janas_gt_rec_product(g, t, bound, up, 0, UP, n, 0) != 0)
+ /* a width ending inside a group of 256 (qwen4exp's 640): each token's
+ rows at whole groups, the rest zero */
+ uint32_t fr = (gate->rows + JANAS_QK - 1) / JANAS_QK * JANAS_QK;
+ VkDeviceAddress UP = GU + (size_t)n * fr * 4;
+ if (janas_gt_rec_product(g, t, bound, gate, 0, GU, n, fr) != 0 ||
+ janas_gt_rec_product(g, t, bound, up, 0, UP, n, fr) != 0)
return -1;
janas_g_compute_barrier(g);
janas_gt_stamp(g, t, PH_GATEUP);
if (janas_g_x_in_32(down->type)) {
- struct push_sm ps = {.n = n * gate->rows};
+ struct push_sm ps = {.n = n * fr};
janas_g_push_addr(ps.g, GU);
janas_g_push_addr(ps.u, UP);
janas_g_push_addr(ps.h, B + t->o_hf);
@@ -394,16 +405,16 @@ int janas_gt_rec_ffn(struct janas_gpu *g, struct janas_gpu_tok *t,
janas_gt_push(g, &ps, sizeof(ps));
g->vkCmdDispatch(g->cb, (ps.n + 255) / 256, 1, 1);
janas_g_compute_barrier(g);
- rec_quant(g, t, bound, B + t->o_hf, H, B + t->o_h32, gate->rows, n);
+ rec_quant(g, t, bound, B + t->o_hf, H, B + t->o_h32, fr, n);
XA = B + t->o_h32;
} else {
- struct push_ac ac = {.blocks = n * gate->rows / JANAS_QK};
+ struct push_ac ac = {.blocks = n * fr / JANAS_QK};
janas_g_push_addr(ac.g, GU);
janas_g_push_addr(ac.u, UP);
janas_g_push_addr(ac.x, H);
janas_gt_bind(g, bound, g->pipea);
janas_gt_push(g, &ac, sizeof(ac));
- g->vkCmdDispatch(g->cb, n * gate->rows / JANAS_QK, 1, 1);
+ g->vkCmdDispatch(g->cb, n * fr / JANAS_QK, 1, 1);
janas_g_compute_barrier(g);
}
janas_gt_stamp(g, t, PH_ACT);
diff --git a/src/llm/iq1s_grid.h b/src/llm/iq1s_grid.h
new file mode 100644
index 0000000..401d39c
--- /dev/null
+++ b/src/llm/iq1s_grid.h
@@ -0,0 +1,531 @@
+/* SPDX-License-Identifier: MIT
+ The iq1s_grid table of ggml (ggml/src/ggml-common.h), copied as data:
+ it defines the format. Copyright (c) 2023-2026 The ggml authors, under
+ the MIT licence (see the ggml repository). Generated - not formatted by
+ hand, not by clang-format either. */
+/*
+ * iq1s_grid.h - the 2048 entries an IQ1_S or IQ1_M index points to, each
+ * eight signed levels (-1, 0 or 1) in the bytes of a uint64, lowest byte
+ * first (private to quant.c).
+ */
+#ifndef JANAS_LLM_IQ1S_GRID_H
+#define JANAS_LLM_IQ1S_GRID_H
+
+#include <stdint.h>
+
+static const uint64_t iq1s_grid[2048] = {
+ 0xffffffffffffffff, 0xffffffffffffff01, 0xffffffffffff0000, 0xffffffffffff01ff,
+ 0xffffffffffff0101, 0xffffffffff00ff00, 0xffffffffff000000, 0xffffffffff01ffff,
+ 0xffffffffff01ff01, 0xffffffffff0101ff, 0xffffffffff010101, 0xffffffff00ff0000,
+ 0xffffffff0000ff00, 0xffffffff000000ff, 0xffffffff00000001, 0xffffffff00010000,
+ 0xffffffff01ffffff, 0xffffffff01ffff01, 0xffffffff01ff01ff, 0xffffffff01ff0101,
+ 0xffffffff01000000, 0xffffffff0101ffff, 0xffffffff0101ff01, 0xffffffff010101ff,
+ 0xffffffff01010101, 0xffffff00ffff00ff, 0xffffff00ffff0000, 0xffffff00ff00ff00,
+ 0xffffff00ff0000ff, 0xffffff00ff000001, 0xffffff00ff000100, 0xffffff00ff000101,
+ 0xffffff00ff010000, 0xffffff0000ffff00, 0xffffff0000ff0001, 0xffffff0000ff0100,
+ 0xffffff000000ff01, 0xffffff0000000000, 0xffffff0000000101, 0xffffff000001ff00,
+ 0xffffff00000100ff, 0xffffff0000010001, 0xffffff00000101ff, 0xffffff0001ff0000,
+ 0xffffff000100ff00, 0xffffff00010000ff, 0xffffff0001000001, 0xffffff0001010000,
+ 0xffffff01ffffffff, 0xffffff01ffffff01, 0xffffff01ffff01ff, 0xffffff01ffff0101,
+ 0xffffff01ff000000, 0xffffff01ff01ffff, 0xffffff01ff01ff01, 0xffffff01ff0101ff,
+ 0xffffff01ff010101, 0xffffff0100ff0000, 0xffffff010000ff00, 0xffffff0100000100,
+ 0xffffff01000100ff, 0xffffff0100010100, 0xffffff0101ffffff, 0xffffff0101ffff01,
+ 0xffffff0101ff01ff, 0xffffff0101ff0101, 0xffffff010100ff00, 0xffffff0101000000,
+ 0xffffff0101000100, 0xffffff010101ffff, 0xffffff010101ff01, 0xffffff01010101ff,
+ 0xffffff0101010101, 0xffff00ffff00ff00, 0xffff00ffff0000ff, 0xffff00ffff000001,
+ 0xffff00ffff010000, 0xffff00ff00ffff00, 0xffff00ff00ff0100, 0xffff00ff00000000,
+ 0xffff00ff00000101, 0xffff00ff000100ff, 0xffff00ff00010000, 0xffff00ff0100ff00,
+ 0xffff00ff01000100, 0xffff00ff01010000, 0xffff0000ffffff00, 0xffff0000ffff00ff,
+ 0xffff0000ffff0000, 0xffff0000ffff0001, 0xffff0000ff000000, 0xffff0000ff0001ff,
+ 0xffff0000ff000101, 0xffff0000ff010100, 0xffff000000ffffff, 0xffff000000ff0000,
+ 0xffff000000ff0101, 0xffff00000000ffff, 0xffff00000000ff00, 0xffff0000000000ff,
+ 0xffff000000000000, 0xffff000000000001, 0xffff000000000100, 0xffff00000001ffff,
+ 0xffff00000001ff01, 0xffff000000010000, 0xffff0000000101ff, 0xffff000000010101,
+ 0xffff000001ffff00, 0xffff00000100ff00, 0xffff000001000000, 0xffff0000010001ff,
+ 0xffff000001000101, 0xffff00000101ff00, 0xffff0000010100ff, 0xffff000001010000,
+ 0xffff000001010001, 0xffff000001010100, 0xffff0001ff0000ff, 0xffff0001ff000100,
+ 0xffff000100ffff00, 0xffff000100ff00ff, 0xffff00010000ffff, 0xffff00010000ff01,
+ 0xffff000100000000, 0xffff0001000001ff, 0xffff00010001ffff, 0xffff00010001ff00,
+ 0xffff000100010001, 0xffff000100010100, 0xffff000101ff0000, 0xffff00010100ff00,
+ 0xffff0001010000ff, 0xffff000101000100, 0xffff01ffffffffff, 0xffff01ffffffff01,
+ 0xffff01ffffff01ff, 0xffff01ffffff0101, 0xffff01ffff000000, 0xffff01ffff01ffff,
+ 0xffff01ffff01ff01, 0xffff01ffff0101ff, 0xffff01ffff010101, 0xffff01ff00ff0000,
+ 0xffff01ff0000ff00, 0xffff01ff00000001, 0xffff01ff00010000, 0xffff01ff01ffffff,
+ 0xffff01ff01ffff01, 0xffff01ff01ff01ff, 0xffff01ff01ff0101, 0xffff01ff01000000,
+ 0xffff01ff0101ffff, 0xffff01ff0101ff01, 0xffff01ff010101ff, 0xffff01ff01010101,
+ 0xffff0100ffff0000, 0xffff0100ff00ff00, 0xffff0100ff0000ff, 0xffff0100ff000100,
+ 0xffff0100ff0100ff, 0xffff0100ff010000, 0xffff010000ffff00, 0xffff01000000ffff,
+ 0xffff01000000ff00, 0xffff010000000000, 0xffff01000001ff00, 0xffff0100000100ff,
+ 0xffff010000010100, 0xffff01000100ff00, 0xffff0100010000ff, 0xffff010001000001,
+ 0xffff010001000100, 0xffff010001010000, 0xffff0101ffffffff, 0xffff0101ffffff01,
+ 0xffff0101ffff01ff, 0xffff0101ffff0101, 0xffff0101ff000000, 0xffff0101ff01ffff,
+ 0xffff0101ff01ff01, 0xffff0101ff0101ff, 0xffff0101ff010101, 0xffff010100ff0000,
+ 0xffff01010000ff00, 0xffff010100000100, 0xffff01010001ff00, 0xffff010100010000,
+ 0xffff010101ffffff, 0xffff010101ffff01, 0xffff010101ff0000, 0xffff010101ff01ff,
+ 0xffff010101ff0101, 0xffff010101000000, 0xffff01010101ffff, 0xffff01010101ff01,
+ 0xffff0101010101ff, 0xffff010101010101, 0xff00ffffff00ffff, 0xff00ffffff00ff00,
+ 0xff00ffffff0000ff, 0xff00ffffff000100, 0xff00ffffff0100ff, 0xff00ffffff010000,
+ 0xff00ffff00ffff00, 0xff00ffff00ff00ff, 0xff00ffff0000ffff, 0xff00ffff00000000,
+ 0xff00ffff000001ff, 0xff00ffff0001ff00, 0xff00ffff000100ff, 0xff00ffff00010000,
+ 0xff00ffff00010100, 0xff00ffff0100ff00, 0xff00ffff010000ff, 0xff00ffff01000001,
+ 0xff00ffff0101ff00, 0xff00ffff01010000, 0xff00ff00ffffff00, 0xff00ff00ffff00ff,
+ 0xff00ff00ffff0001, 0xff00ff00ffff0100, 0xff00ff00ff00ffff, 0xff00ff00ff00ff01,
+ 0xff00ff00ff000000, 0xff00ff00ff0001ff, 0xff00ff00ff01ff00, 0xff00ff00ff0100ff,
+ 0xff00ff00ff010100, 0xff00ff0000ff0000, 0xff00ff0000ff0101, 0xff00ff000000ffff,
+ 0xff00ff000000ff00, 0xff00ff000000ff01, 0xff00ff00000000ff, 0xff00ff0000000000,
+ 0xff00ff0000000001, 0xff00ff0000000100, 0xff00ff000001ffff, 0xff00ff0000010000,
+ 0xff00ff0001ff00ff, 0xff00ff000100ff01, 0xff00ff0001000000, 0xff00ff000101ff00,
+ 0xff00ff00010100ff, 0xff00ff01ff00ff00, 0xff00ff01ff0000ff, 0xff00ff01ff000001,
+ 0xff00ff01ff010000, 0xff00ff0100ffffff, 0xff00ff0100ff0001, 0xff00ff0100ff0100,
+ 0xff00ff010000ff01, 0xff00ff0100000000, 0xff00ff01000001ff, 0xff00ff0100000101,
+ 0xff00ff01000100ff, 0xff00ff0100010001, 0xff00ff0101ff0000, 0xff00ff010100ff00,
+ 0xff00ff01010000ff, 0xff00ff0101000001, 0xff00ff0101010000, 0xff0000ffffffff00,
+ 0xff0000ffffff0001, 0xff0000ffffff0100, 0xff0000ffff0000ff, 0xff0000ffff000000,
+ 0xff0000ffff0001ff, 0xff0000ffff000100, 0xff0000ffff01ff00, 0xff0000ffff010001,
+ 0xff0000ff00ffff00, 0xff0000ff00ff0000, 0xff0000ff00ff0001, 0xff0000ff00ff01ff,
+ 0xff0000ff00ff0101, 0xff0000ff0000ff00, 0xff0000ff000000ff, 0xff0000ff00000000,
+ 0xff0000ff00000001, 0xff0000ff00000100, 0xff0000ff0001ff01, 0xff0000ff00010000,
+ 0xff0000ff000101ff, 0xff0000ff01ff00ff, 0xff0000ff01ff0100, 0xff0000ff0100ffff,
+ 0xff0000ff010000ff, 0xff0000ff01000000, 0xff0000ff010001ff, 0xff0000ff01000100,
+ 0xff0000ff01000101, 0xff0000ff0101ff00, 0xff0000ff010100ff, 0xff0000ff01010000,
+ 0xff0000ff01010100, 0xff000000ffffff01, 0xff000000ffff0000, 0xff000000ffff0101,
+ 0xff000000ff00ff00, 0xff000000ff0000ff, 0xff000000ff000000, 0xff000000ff000001,
+ 0xff000000ff000100, 0xff000000ff01ffff, 0xff000000ff01ff01, 0xff000000ff010000,
+ 0xff000000ff0101ff, 0xff000000ff010101, 0xff00000000ffff00, 0xff00000000ff00ff,
+ 0xff00000000ff0000, 0xff00000000ff0001, 0xff0000000000ff00, 0xff0000000000ff01,
+ 0xff000000000000ff, 0xff00000000000000, 0xff00000000000001, 0xff00000000000100,
+ 0xff00000000000101, 0xff0000000001ff00, 0xff000000000100ff, 0xff00000000010000,
+ 0xff00000000010001, 0xff00000000010100, 0xff00000001ffffff, 0xff00000001ffff01,
+ 0xff00000001ff00ff, 0xff00000001ff0000, 0xff00000001ff01ff, 0xff00000001ff0101,
+ 0xff0000000100ffff, 0xff0000000100ff00, 0xff000000010000ff, 0xff00000001000000,
+ 0xff00000001000001, 0xff00000001000100, 0xff00000001000101, 0xff0000000101ffff,
+ 0xff0000000101ff01, 0xff00000001010000, 0xff000001ffffff00, 0xff000001ffff00ff,
+ 0xff000001ffff0000, 0xff000001ffff0001, 0xff000001ff000000, 0xff000001ff000001,
+ 0xff000001ff0001ff, 0xff000001ff000101, 0xff000001ff01ff00, 0xff000001ff010001,
+ 0xff00000100ffffff, 0xff00000100ffff01, 0xff00000100ff00ff, 0xff00000100ff0000,
+ 0xff00000100ff01ff, 0xff00000100ff0101, 0xff0000010000ff00, 0xff00000100000000,
+ 0xff00000100000001, 0xff000001000001ff, 0xff00000100000100, 0xff0000010001ff00,
+ 0xff000001000100ff, 0xff00000100010000, 0xff000001000101ff, 0xff00000100010100,
+ 0xff00000100010101, 0xff00000101ff0001, 0xff00000101ff0101, 0xff0000010100ff01,
+ 0xff00000101000000, 0xff000001010100ff, 0xff00000101010100, 0xff0001ffff00ff00,
+ 0xff0001ffff000001, 0xff0001ffff010000, 0xff0001ff00ffff00, 0xff0001ff00ff00ff,
+ 0xff0001ff00ff0001, 0xff0001ff00ff0100, 0xff0001ff0000ffff, 0xff0001ff00000000,
+ 0xff0001ff000001ff, 0xff0001ff00000101, 0xff0001ff0001ffff, 0xff0001ff0001ff00,
+ 0xff0001ff000100ff, 0xff0001ff00010001, 0xff0001ff00010100, 0xff0001ff01ff0000,
+ 0xff0001ff0100ff00, 0xff0001ff010000ff, 0xff0001ff01010000, 0xff000100ff00ffff,
+ 0xff000100ff00ff01, 0xff000100ff000000, 0xff000100ff000101, 0xff000100ff01ff00,
+ 0xff000100ff010000, 0xff00010000ffff01, 0xff00010000ff00ff, 0xff00010000ff0000,
+ 0xff00010000ff01ff, 0xff0001000000ff00, 0xff000100000000ff, 0xff00010000000000,
+ 0xff00010000000001, 0xff00010000000100, 0xff00010000000101, 0xff0001000001ffff,
+ 0xff00010000010000, 0xff00010000010101, 0xff00010001ff0100, 0xff0001000100ff00,
+ 0xff0001000100ff01, 0xff00010001000000, 0xff000100010001ff, 0xff0001000101ff00,
+ 0xff00010001010001, 0xff00010001010100, 0xff000101ffff0100, 0xff000101ff000001,
+ 0xff000101ff0100ff, 0xff000101ff010001, 0xff00010100ff00ff, 0xff00010100ff0001,
+ 0xff00010100ff0100, 0xff0001010000ffff, 0xff0001010000ff01, 0xff00010100000000,
+ 0xff000101000001ff, 0xff0001010001ff00, 0xff00010100010001, 0xff00010100010100,
+ 0xff00010101ff0000, 0xff0001010100ff00, 0xff00010101000001, 0xff00010101000101,
+ 0xff01ffffffffffff, 0xff01ffffffffff01, 0xff01ffffffff01ff, 0xff01ffffffff0101,
+ 0xff01ffffff000000, 0xff01ffffff01ffff, 0xff01ffffff01ff01, 0xff01ffffff010000,
+ 0xff01ffffff0101ff, 0xff01ffffff010101, 0xff01ffff00ff0000, 0xff01ffff0000ff00,
+ 0xff01ffff00000100, 0xff01ffff0001ff00, 0xff01ffff00010000, 0xff01ffff01ffffff,
+ 0xff01ffff01ffff01, 0xff01ffff01ff01ff, 0xff01ffff01ff0101, 0xff01ffff01000000,
+ 0xff01ffff0101ffff, 0xff01ffff0101ff01, 0xff01ffff01010000, 0xff01ffff010101ff,
+ 0xff01ffff01010101, 0xff01ff00ffff0000, 0xff01ff00ff00ff00, 0xff01ff00ff0000ff,
+ 0xff01ff00ff000100, 0xff01ff00ff010000, 0xff01ff0000ffff01, 0xff01ff0000ff00ff,
+ 0xff01ff0000ff0100, 0xff01ff0000000000, 0xff01ff00000001ff, 0xff01ff0000000101,
+ 0xff01ff000001ff00, 0xff01ff00000100ff, 0xff01ff0000010000, 0xff01ff0000010001,
+ 0xff01ff0001ff0000, 0xff01ff000100ffff, 0xff01ff0001000001, 0xff01ff0001000100,
+ 0xff01ff0001010000, 0xff01ff01ffffff00, 0xff01ff01ffff01ff, 0xff01ff01ffff0101,
+ 0xff01ff01ff00ff00, 0xff01ff01ff000000, 0xff01ff01ff01ffff, 0xff01ff01ff01ff01,
+ 0xff01ff01ff0101ff, 0xff01ff01ff010101, 0xff01ff0100ff0000, 0xff01ff010000ff00,
+ 0xff01ff0100000001, 0xff01ff0100000100, 0xff01ff0100010000, 0xff01ff0101ffff00,
+ 0xff01ff0101ff01ff, 0xff01ff0101ff0101, 0xff01ff010100ff00, 0xff01ff0101000000,
+ 0xff01ff010101ffff, 0xff01ff010101ff01, 0xff01ff01010101ff, 0xff01ff0101010101,
+ 0xff0100ffffff0000, 0xff0100ffff0000ff, 0xff0100ffff000001, 0xff0100ffff000100,
+ 0xff0100ffff010000, 0xff0100ff00ff00ff, 0xff0100ff00ff0000, 0xff0100ff00ff0001,
+ 0xff0100ff00ff0100, 0xff0100ff0000ff01, 0xff0100ff00000000, 0xff0100ff000001ff,
+ 0xff0100ff00000101, 0xff0100ff00010001, 0xff0100ff01ff0000, 0xff0100ff0100ff00,
+ 0xff0100ff010000ff, 0xff0100ff01000100, 0xff0100ff0101ff00, 0xff0100ff01010000,
+ 0xff010000ffff0100, 0xff010000ff000000, 0xff010000ff01ff00, 0xff010000ff010100,
+ 0xff01000000ffffff, 0xff01000000ff0000, 0xff01000000ff01ff, 0xff0100000000ff00,
+ 0xff010000000000ff, 0xff01000000000000, 0xff01000000000100, 0xff0100000001ff01,
+ 0xff01000000010000, 0xff010000000101ff, 0xff01000001ff0100, 0xff0100000100ffff,
+ 0xff010000010000ff, 0xff01000001000000, 0xff010000010001ff, 0xff01000001000101,
+ 0xff0100000101ff00, 0xff010000010100ff, 0xff01000001010001, 0xff01000001010100,
+ 0xff010001ffff0000, 0xff010001ff00ffff, 0xff010001ff00ff01, 0xff010001ff000100,
+ 0xff010001ff010000, 0xff01000100ffff00, 0xff01000100ff0100, 0xff01000100000000,
+ 0xff0100010001ffff, 0xff0100010001ff00, 0xff01000100010100, 0xff01000101ff00ff,
+ 0xff01000101ff0001, 0xff0100010100ffff, 0xff01000101000101, 0xff0101ffffffffff,
+ 0xff0101ffffffff01, 0xff0101ffffff01ff, 0xff0101ffffff0101, 0xff0101ffff000000,
+ 0xff0101ffff01ffff, 0xff0101ffff01ff01, 0xff0101ffff0101ff, 0xff0101ffff010101,
+ 0xff0101ff00ff0000, 0xff0101ff0000ff00, 0xff0101ff000000ff, 0xff0101ff00010000,
+ 0xff0101ff01ffffff, 0xff0101ff01ffff01, 0xff0101ff01ff01ff, 0xff0101ff01ff0101,
+ 0xff0101ff0101ffff, 0xff0101ff0101ff01, 0xff0101ff010101ff, 0xff0101ff01010101,
+ 0xff010100ffff0100, 0xff010100ff00ff00, 0xff010100ff0000ff, 0xff010100ff000100,
+ 0xff010100ff010000, 0xff01010000ff0001, 0xff01010000ff0100, 0xff0101000000ff01,
+ 0xff01010000000000, 0xff0101000001ff00, 0xff010100000100ff, 0xff01010000010001,
+ 0xff01010000010100, 0xff01010001ff0000, 0xff0101000100ffff, 0xff01010001000001,
+ 0xff01010001000100, 0xff010100010100ff, 0xff01010001010000, 0xff010101ffffffff,
+ 0xff010101ffffff01, 0xff010101ffff01ff, 0xff010101ffff0101, 0xff010101ff01ffff,
+ 0xff010101ff01ff01, 0xff010101ff0101ff, 0xff010101ff010101, 0xff01010100ff0000,
+ 0xff0101010000ff00, 0xff01010100000001, 0xff01010100000100, 0xff01010100010000,
+ 0xff01010101ffffff, 0xff01010101ffff01, 0xff01010101ff01ff, 0xff01010101ff0101,
+ 0xff01010101000000, 0xff0101010101ffff, 0xff0101010101ff01, 0xff010101010101ff,
+ 0xff01010101010101, 0x00ffffffffff0000, 0x00ffffffff00ff00, 0x00ffffffff000001,
+ 0x00ffffffff010000, 0x00ffffff00ff0100, 0x00ffffff0000ff01, 0x00ffffff00000000,
+ 0x00ffffff000001ff, 0x00ffffff00000101, 0x00ffffff0001ff00, 0x00ffffff000100ff,
+ 0x00ffffff00010001, 0x00ffffff010000ff, 0x00ffffff01000100, 0x00ffffff0101ff00,
+ 0x00ffffff01010001, 0x00ffff00ffffffff, 0x00ffff00ffffff00, 0x00ffff00ffff00ff,
+ 0x00ffff00ffff0001, 0x00ffff00ffff0100, 0x00ffff00ff00ff01, 0x00ffff00ff000000,
+ 0x00ffff00ff000001, 0x00ffff00ff0001ff, 0x00ffff00ff000101, 0x00ffff00ff01ff00,
+ 0x00ffff00ff010001, 0x00ffff00ff010100, 0x00ffff0000ff0000, 0x00ffff0000ff01ff,
+ 0x00ffff0000ff0101, 0x00ffff000000ff00, 0x00ffff00000000ff, 0x00ffff0000000000,
+ 0x00ffff0000000001, 0x00ffff0000000100, 0x00ffff0000000101, 0x00ffff0000010000,
+ 0x00ffff00000101ff, 0x00ffff0000010101, 0x00ffff0001ffff00, 0x00ffff0001ff00ff,
+ 0x00ffff0001ff0001, 0x00ffff000100ffff, 0x00ffff000100ff01, 0x00ffff0001000000,
+ 0x00ffff000101ffff, 0x00ffff000101ff00, 0x00ffff000101ff01, 0x00ffff01ffff0000,
+ 0x00ffff01ff00ff00, 0x00ffff01ff0000ff, 0x00ffff01ff000001, 0x00ffff01ff010000,
+ 0x00ffff0100ffff00, 0x00ffff010000ff01, 0x00ffff0100000000, 0x00ffff0100000101,
+ 0x00ffff01000100ff, 0x00ffff0100010100, 0x00ffff0101ff0100, 0x00ffff01010000ff,
+ 0x00ffff0101010000, 0x00ff00ffffffff00, 0x00ff00ffff000000, 0x00ff00ffff000100,
+ 0x00ff00ffff010100, 0x00ff00ff00ff0000, 0x00ff00ff00ff01ff, 0x00ff00ff00ff0101,
+ 0x00ff00ff0000ff00, 0x00ff00ff000000ff, 0x00ff00ff00000000, 0x00ff00ff00000001,
+ 0x00ff00ff0001ff00, 0x00ff00ff0001ff01, 0x00ff00ff00010000, 0x00ff00ff000101ff,
+ 0x00ff00ff00010101, 0x00ff00ff01ffff00, 0x00ff00ff01ff0001, 0x00ff00ff01ff0100,
+ 0x00ff00ff0100ffff, 0x00ff00ff0100ff01, 0x00ff00ff01000000, 0x00ff00ff0101ffff,
+ 0x00ff00ff0101ff00, 0x00ff00ff01010100, 0x00ff0000ffffff00, 0x00ff0000ffffff01,
+ 0x00ff0000ffff0000, 0x00ff0000ffff0101, 0x00ff0000ff00ff00, 0x00ff0000ff0000ff,
+ 0x00ff0000ff000000, 0x00ff0000ff000001, 0x00ff0000ff000100, 0x00ff0000ff01ffff,
+ 0x00ff0000ff010000, 0x00ff0000ff010101, 0x00ff000000ffff00, 0x00ff000000ff00ff,
+ 0x00ff000000ff0000, 0x00ff000000ff0001, 0x00ff000000ff0100, 0x00ff00000000ffff,
+ 0x00ff00000000ff00, 0x00ff0000000000ff, 0x00ff000000000000, 0x00ff000000000001,
+ 0x00ff0000000001ff, 0x00ff000000000100, 0x00ff00000001ff00, 0x00ff0000000100ff,
+ 0x00ff000000010000, 0x00ff000000010001, 0x00ff000000010100, 0x00ff000001ffff01,
+ 0x00ff000001ff00ff, 0x00ff000001ff0000, 0x00ff000001ff01ff, 0x00ff00000100ff00,
+ 0x00ff0000010000ff, 0x00ff000001000000, 0x00ff000001000001, 0x00ff000001000100,
+ 0x00ff000001000101, 0x00ff000001010000, 0x00ff0000010101ff, 0x00ff000001010101,
+ 0x00ff0001ffffff00, 0x00ff0001ffff0000, 0x00ff0001ffff0100, 0x00ff0001ff0000ff,
+ 0x00ff0001ff000000, 0x00ff0001ff0001ff, 0x00ff0001ff000101, 0x00ff0001ff01ff00,
+ 0x00ff0001ff0100ff, 0x00ff0001ff010100, 0x00ff000100ffffff, 0x00ff000100ffff01,
+ 0x00ff000100ff0000, 0x00ff000100ff01ff, 0x00ff00010000ffff, 0x00ff00010000ff00,
+ 0x00ff00010000ff01, 0x00ff000100000000, 0x00ff000100000001, 0x00ff000100000100,
+ 0x00ff00010001ff01, 0x00ff000100010000, 0x00ff0001000101ff, 0x00ff000101ffff00,
+ 0x00ff000101ff0000, 0x00ff000101ff0101, 0x00ff0001010000ff, 0x00ff000101000000,
+ 0x00ff00010101ff00, 0x00ff0001010100ff, 0x00ff000101010001, 0x00ff01ffffff0000,
+ 0x00ff01ffff00ff00, 0x00ff01ffff000000, 0x00ff01ffff000101, 0x00ff01ffff010000,
+ 0x00ff01ff00ffff01, 0x00ff01ff00ff0100, 0x00ff01ff0000ffff, 0x00ff01ff00000000,
+ 0x00ff01ff000001ff, 0x00ff01ff0001ff00, 0x00ff01ff000100ff, 0x00ff01ff00010001,
+ 0x00ff01ff00010100, 0x00ff01ff01ff0000, 0x00ff01ff0100ff00, 0x00ff01ff010000ff,
+ 0x00ff01ff01000001, 0x00ff01ff01000100, 0x00ff01ff01010000, 0x00ff0100ffffff00,
+ 0x00ff0100ffff0000, 0x00ff0100ffff0001, 0x00ff0100ffff0101, 0x00ff0100ff00ffff,
+ 0x00ff0100ff0000ff, 0x00ff0100ff000000, 0x00ff0100ff0001ff, 0x00ff0100ff01ff00,
+ 0x00ff0100ff0100ff, 0x00ff0100ff010001, 0x00ff010000ffffff, 0x00ff010000ff0000,
+ 0x00ff010000ff0101, 0x00ff01000000ff00, 0x00ff01000000ff01, 0x00ff0100000000ff,
+ 0x00ff010000000000, 0x00ff010000000001, 0x00ff010000000100, 0x00ff01000001ffff,
+ 0x00ff01000001ff01, 0x00ff010000010000, 0x00ff010000010001, 0x00ff010000010101,
+ 0x00ff010001ff0001, 0x00ff010001ff0100, 0x00ff01000100ff01, 0x00ff010001000000,
+ 0x00ff010001000001, 0x00ff0100010001ff, 0x00ff01000101ff00, 0x00ff0100010100ff,
+ 0x00ff010001010001, 0x00ff010001010100, 0x00ff0101ff000001, 0x00ff010100ff00ff,
+ 0x00ff010100ff0001, 0x00ff010100ff0100, 0x00ff010100000000, 0x00ff0101000001ff,
+ 0x00ff010100000101, 0x00ff0101000100ff, 0x00ff010100010100, 0x00ff0101010000ff,
+ 0x00ff010101010000, 0x0000ffffffffff00, 0x0000ffffffff00ff, 0x0000ffffffff0000,
+ 0x0000ffffffff0001, 0x0000ffffffff0100, 0x0000ffffff00ff01, 0x0000ffffff000000,
+ 0x0000ffffff000101, 0x0000ffffff01ff00, 0x0000ffffff0100ff, 0x0000ffffff010100,
+ 0x0000ffff00ffffff, 0x0000ffff00ff0000, 0x0000ffff00ff01ff, 0x0000ffff0000ff00,
+ 0x0000ffff000000ff, 0x0000ffff00000000, 0x0000ffff00000001, 0x0000ffff00000100,
+ 0x0000ffff00010000, 0x0000ffff000101ff, 0x0000ffff01ff0001, 0x0000ffff01ff0100,
+ 0x0000ffff01000000, 0x0000ffff010001ff, 0x0000ffff0101ffff, 0x0000ffff0101ff00,
+ 0x0000ffff01010001, 0x0000ffff01010100, 0x0000ff00ffff0000, 0x0000ff00ffff01ff,
+ 0x0000ff00ffff0100, 0x0000ff00ffff0101, 0x0000ff00ff00ff00, 0x0000ff00ff0000ff,
+ 0x0000ff00ff000000, 0x0000ff00ff000001, 0x0000ff00ff0001ff, 0x0000ff00ff000100,
+ 0x0000ff00ff01ffff, 0x0000ff00ff010000, 0x0000ff00ff010001, 0x0000ff00ff0101ff,
+ 0x0000ff00ff010101, 0x0000ff0000ffff00, 0x0000ff0000ff00ff, 0x0000ff0000ff0000,
+ 0x0000ff0000ff0001, 0x0000ff0000ff0100, 0x0000ff000000ffff, 0x0000ff000000ff00,
+ 0x0000ff000000ff01, 0x0000ff00000000ff, 0x0000ff0000000000, 0x0000ff0000000001,
+ 0x0000ff00000001ff, 0x0000ff0000000100, 0x0000ff0000000101, 0x0000ff000001ff00,
+ 0x0000ff00000100ff, 0x0000ff0000010000, 0x0000ff0000010001, 0x0000ff0000010100,
+ 0x0000ff0001ffff01, 0x0000ff0001ff0000, 0x0000ff000100ff00, 0x0000ff00010000ff,
+ 0x0000ff0001000000, 0x0000ff0001000001, 0x0000ff0001000100, 0x0000ff000101ffff,
+ 0x0000ff0001010000, 0x0000ff0001010101, 0x0000ff01ffffff00, 0x0000ff01ffff0001,
+ 0x0000ff01ff00ff01, 0x0000ff01ff000000, 0x0000ff01ff000101, 0x0000ff01ff01ff00,
+ 0x0000ff01ff0100ff, 0x0000ff0100ffff01, 0x0000ff0100ff0000, 0x0000ff0100ff0101,
+ 0x0000ff010000ff00, 0x0000ff01000000ff, 0x0000ff0100000000, 0x0000ff0100000001,
+ 0x0000ff0100000100, 0x0000ff010001ff01, 0x0000ff0100010000, 0x0000ff0101ff0000,
+ 0x0000ff010100ffff, 0x0000ff010100ff01, 0x0000ff0101000000, 0x0000ff0101000100,
+ 0x0000ff0101000101, 0x0000ff01010100ff, 0x000000ffffff00ff, 0x000000ffffff0000,
+ 0x000000ffff00ff00, 0x000000ffff0000ff, 0x000000ffff000000, 0x000000ffff000001,
+ 0x000000ffff0001ff, 0x000000ffff000100, 0x000000ffff01ff00, 0x000000ffff010000,
+ 0x000000ffff0101ff, 0x000000ffff010101, 0x000000ff00ffff00, 0x000000ff00ff00ff,
+ 0x000000ff00ff0000, 0x000000ff00ff0001, 0x000000ff00ff0100, 0x000000ff00ff0101,
+ 0x000000ff0000ffff, 0x000000ff0000ff00, 0x000000ff000000ff, 0x000000ff00000000,
+ 0x000000ff00000001, 0x000000ff000001ff, 0x000000ff00000100, 0x000000ff00000101,
+ 0x000000ff0001ff00, 0x000000ff0001ff01, 0x000000ff000100ff, 0x000000ff00010000,
+ 0x000000ff00010001, 0x000000ff00010100, 0x000000ff01ffffff, 0x000000ff01ff01ff,
+ 0x000000ff01ff0101, 0x000000ff0100ff00, 0x000000ff010000ff, 0x000000ff01000000,
+ 0x000000ff01000001, 0x000000ff01000100, 0x000000ff0101ff00, 0x000000ff010100ff,
+ 0x000000ff01010000, 0x000000ff01010101, 0x00000000ffffff00, 0x00000000ffffff01,
+ 0x00000000ffff00ff, 0x00000000ffff0000, 0x00000000ffff0001, 0x00000000ffff0100,
+ 0x00000000ff00ffff, 0x00000000ff00ff00, 0x00000000ff00ff01, 0x00000000ff0000ff,
+ 0x00000000ff000000, 0x00000000ff000001, 0x00000000ff000100, 0x00000000ff000101,
+ 0x00000000ff01ff00, 0x00000000ff0100ff, 0x00000000ff010000, 0x00000000ff010001,
+ 0x00000000ff010100, 0x0000000000ffffff, 0x0000000000ffff00, 0x0000000000ffff01,
+ 0x0000000000ff00ff, 0x0000000000ff0000, 0x0000000000ff0001, 0x0000000000ff01ff,
+ 0x0000000000ff0100, 0x000000000000ffff, 0x000000000000ff00, 0x000000000000ff01,
+ 0x00000000000000ff, 0x0000000000000000, 0x0000000000000001, 0x00000000000001ff,
+ 0x0000000000000100, 0x0000000000000101, 0x000000000001ffff, 0x000000000001ff00,
+ 0x00000000000100ff, 0x0000000000010000, 0x0000000000010001, 0x00000000000101ff,
+ 0x0000000000010100, 0x0000000000010101, 0x0000000001ffff00, 0x0000000001ff00ff,
+ 0x0000000001ff0000, 0x0000000001ff0100, 0x0000000001ff0101, 0x000000000100ffff,
+ 0x000000000100ff00, 0x00000000010000ff, 0x0000000001000000, 0x0000000001000001,
+ 0x00000000010001ff, 0x0000000001000100, 0x000000000101ff00, 0x00000000010100ff,
+ 0x0000000001010000, 0x0000000001010001, 0x0000000001010100, 0x00000001ffffffff,
+ 0x00000001ffffff00, 0x00000001ffffff01, 0x00000001ffff00ff, 0x00000001ffff0001,
+ 0x00000001ffff01ff, 0x00000001ffff0100, 0x00000001ff00ff00, 0x00000001ff0000ff,
+ 0x00000001ff000000, 0x00000001ff0001ff, 0x00000001ff000100, 0x00000001ff01ffff,
+ 0x00000001ff01ff00, 0x00000001ff01ff01, 0x00000001ff0100ff, 0x00000001ff010000,
+ 0x00000001ff010001, 0x00000001ff0101ff, 0x00000001ff010100, 0x0000000100ffff00,
+ 0x0000000100ff0000, 0x0000000100ff0001, 0x0000000100ff01ff, 0x0000000100ff0100,
+ 0x0000000100ff0101, 0x000000010000ffff, 0x000000010000ff00, 0x000000010000ff01,
+ 0x00000001000000ff, 0x0000000100000000, 0x0000000100000001, 0x00000001000001ff,
+ 0x0000000100000100, 0x0000000100000101, 0x000000010001ff00, 0x00000001000100ff,
+ 0x0000000100010000, 0x0000000100010100, 0x0000000101ffff01, 0x0000000101ff0000,
+ 0x0000000101ff0001, 0x0000000101ff01ff, 0x0000000101ff0100, 0x0000000101ff0101,
+ 0x000000010100ff00, 0x0000000101000000, 0x0000000101000101, 0x000000010101ff01,
+ 0x0000000101010000, 0x0000000101010001, 0x00000001010101ff, 0x0000000101010100,
+ 0x000001ffffff00ff, 0x000001ffffff0000, 0x000001ffffff0001, 0x000001ffffff0100,
+ 0x000001ffff00ffff, 0x000001ffff000000, 0x000001ffff0001ff, 0x000001ffff01ff00,
+ 0x000001ffff010101, 0x000001ff00ff0000, 0x000001ff00ff01ff, 0x000001ff00ff0101,
+ 0x000001ff0000ff00, 0x000001ff000000ff, 0x000001ff00000000, 0x000001ff00000001,
+ 0x000001ff000001ff, 0x000001ff00000100, 0x000001ff0001ffff, 0x000001ff0001ff01,
+ 0x000001ff000100ff, 0x000001ff00010000, 0x000001ff01ffff01, 0x000001ff01ff0100,
+ 0x000001ff0100ffff, 0x000001ff0100ff01, 0x000001ff01000000, 0x000001ff010001ff,
+ 0x000001ff0101ff00, 0x000001ff01010100, 0x00000100ffffff00, 0x00000100ffffff01,
+ 0x00000100ffff0000, 0x00000100ffff0101, 0x00000100ff00ff00, 0x00000100ff0000ff,
+ 0x00000100ff000000, 0x00000100ff000001, 0x00000100ff000100, 0x00000100ff010000,
+ 0x0000010000ffff00, 0x0000010000ff00ff, 0x0000010000ff0000, 0x0000010000ff0001,
+ 0x0000010000ff0100, 0x000001000000ffff, 0x000001000000ff00, 0x000001000000ff01,
+ 0x00000100000000ff, 0x0000010000000000, 0x0000010000000001, 0x00000100000001ff,
+ 0x0000010000000100, 0x0000010000000101, 0x000001000001ff00, 0x00000100000100ff,
+ 0x0000010000010000, 0x0000010000010001, 0x0000010000010100, 0x0000010001ffff00,
+ 0x0000010001ff0000, 0x0000010001ff0100, 0x000001000100ff00, 0x00000100010000ff,
+ 0x0000010001000000, 0x0000010001000001, 0x00000100010001ff, 0x0000010001000100,
+ 0x0000010001010000, 0x00000101ffff00ff, 0x00000101ffff01ff, 0x00000101ff000000,
+ 0x00000101ff000101, 0x00000101ff01ffff, 0x00000101ff010000, 0x00000101ff010001,
+ 0x00000101ff010100, 0x0000010100ff0000, 0x0000010100ff01ff, 0x0000010100ff0100,
+ 0x000001010000ff00, 0x0000010100000000, 0x0000010100000001, 0x00000101000001ff,
+ 0x0000010100000100, 0x000001010001ff01, 0x0000010100010000, 0x00000101000101ff,
+ 0x0000010100010101, 0x0000010101ffff00, 0x0000010101ff0101, 0x000001010100ff01,
+ 0x0000010101000000, 0x0000010101000001, 0x00000101010001ff, 0x0000010101000101,
+ 0x000001010101ff00, 0x0001ffffffff0000, 0x0001ffffff0000ff, 0x0001ffffff000001,
+ 0x0001ffffff000100, 0x0001ffffff010000, 0x0001ffff00ff00ff, 0x0001ffff0000ffff,
+ 0x0001ffff00000000, 0x0001ffff00000001, 0x0001ffff000001ff, 0x0001ffff00000101,
+ 0x0001ffff0001ff00, 0x0001ffff000100ff, 0x0001ffff00010001, 0x0001ffff00010100,
+ 0x0001ffff01ffff00, 0x0001ffff01000001, 0x0001ffff01010000, 0x0001ff00ffffff00,
+ 0x0001ff00ffff00ff, 0x0001ff00ffff0001, 0x0001ff00ffff0100, 0x0001ff00ff00ff01,
+ 0x0001ff00ff000000, 0x0001ff00ff01ff00, 0x0001ff00ff01ff01, 0x0001ff00ff010001,
+ 0x0001ff00ff010100, 0x0001ff0000ff0000, 0x0001ff0000ff0100, 0x0001ff000000ff00,
+ 0x0001ff0000000000, 0x0001ff0000000001, 0x0001ff0000000100, 0x0001ff0000010000,
+ 0x0001ff0000010001, 0x0001ff0000010101, 0x0001ff0001ff00ff, 0x0001ff0001ff0101,
+ 0x0001ff000100ff01, 0x0001ff0001000000, 0x0001ff000101ff00, 0x0001ff0001010001,
+ 0x0001ff0001010100, 0x0001ff01ff00ff00, 0x0001ff01ff000001, 0x0001ff01ff000100,
+ 0x0001ff0100ffffff, 0x0001ff0100ffff00, 0x0001ff0100ff0001, 0x0001ff0100000000,
+ 0x0001ff0100000001, 0x0001ff01000001ff, 0x0001ff010001ffff, 0x0001ff0101ff0000,
+ 0x0001ff010100ff00, 0x0001ff0101000001, 0x0001ff0101010000, 0x000100ffff00ff00,
+ 0x000100ffff00ff01, 0x000100ffff000000, 0x000100ffff000001, 0x000100ffff000101,
+ 0x000100ffff01ff00, 0x000100ffff010001, 0x000100ffff010100, 0x000100ff00ffffff,
+ 0x000100ff00ffff01, 0x000100ff00ff0000, 0x000100ff00ff01ff, 0x000100ff00ff0101,
+ 0x000100ff0000ff00, 0x000100ff000000ff, 0x000100ff00000000, 0x000100ff00000001,
+ 0x000100ff00000100, 0x000100ff00000101, 0x000100ff0001ffff, 0x000100ff0001ff01,
+ 0x000100ff00010000, 0x000100ff01ff00ff, 0x000100ff01ff0000, 0x000100ff01ff0100,
+ 0x000100ff0100ffff, 0x000100ff0100ff01, 0x000100ff010000ff, 0x000100ff01000000,
+ 0x000100ff01000001, 0x000100ff010001ff, 0x000100ff01000101, 0x000100ff0101ff00,
+ 0x000100ff010100ff, 0x000100ff01010100, 0x00010000ffff0000, 0x00010000ffff01ff,
+ 0x00010000ffff0101, 0x00010000ff00ff00, 0x00010000ff000000, 0x00010000ff000001,
+ 0x00010000ff000100, 0x0001000000ff00ff, 0x0001000000ff0000, 0x0001000000ff0001,
+ 0x0001000000ff0100, 0x000100000000ffff, 0x000100000000ff00, 0x00010000000000ff,
+ 0x0001000000000000, 0x0001000000000001, 0x0001000000000100, 0x000100000001ff00,
+ 0x00010000000100ff, 0x0001000000010000, 0x0001000000010001, 0x0001000000010100,
+ 0x0001000001ff0001, 0x0001000001ff0100, 0x0001000001ff0101, 0x000100000100ff00,
+ 0x0001000001000000, 0x0001000001000001, 0x0001000001000100, 0x0001000001000101,
+ 0x000100000101ff01, 0x0001000001010000, 0x0001000001010001, 0x00010000010101ff,
+ 0x00010001ffffff01, 0x00010001ffff0100, 0x00010001ff000000, 0x00010001ff01ffff,
+ 0x00010001ff010001, 0x00010001ff0101ff, 0x00010001ff010100, 0x0001000100ffffff,
+ 0x0001000100ff0000, 0x0001000100ff01ff, 0x0001000100ff0101, 0x000100010000ff00,
+ 0x00010001000000ff, 0x0001000100000000, 0x0001000100000001, 0x00010001000001ff,
+ 0x0001000100000101, 0x000100010001ffff, 0x0001000100010000, 0x00010001000101ff,
+ 0x0001000101ffffff, 0x0001000101ffff01, 0x0001000101ff0000, 0x0001000101ff0101,
+ 0x00010001010000ff, 0x0001000101000001, 0x00010001010001ff, 0x0001000101000100,
+ 0x000100010101ffff, 0x00010001010100ff, 0x0001000101010001, 0x0001000101010101,
+ 0x000101ffff000001, 0x000101ffff000100, 0x000101ffff010000, 0x000101ff00ffff00,
+ 0x000101ff0000ff01, 0x000101ff00000000, 0x000101ff00000101, 0x000101ff0001ff00,
+ 0x000101ff00010100, 0x000101ff01ff0000, 0x000101ff0100ff00, 0x000101ff010001ff,
+ 0x000101ff01010001, 0x00010100ffffff00, 0x00010100ffff00ff, 0x00010100ff00ffff,
+ 0x00010100ff000000, 0x00010100ff01ff00, 0x00010100ff0100ff, 0x00010100ff010001,
+ 0x00010100ff010100, 0x0001010000ffffff, 0x0001010000ffff00, 0x0001010000ff0000,
+ 0x0001010000ff0001, 0x0001010000ff01ff, 0x000101000000ff00, 0x00010100000000ff,
+ 0x0001010000000000, 0x0001010000000001, 0x0001010000000100, 0x000101000001ffff,
+ 0x0001010000010000, 0x0001010000010101, 0x0001010001ffff01, 0x0001010001ff00ff,
+ 0x0001010001ff0101, 0x0001010001000000, 0x000101000101ff00, 0x00010100010100ff,
+ 0x0001010001010000, 0x0001010001010100, 0x00010101ff00ff00, 0x00010101ff000001,
+ 0x00010101ff0001ff, 0x0001010100ffff00, 0x0001010100ff00ff, 0x0001010100ff0100,
+ 0x000101010000ffff, 0x0001010100000000, 0x00010101000001ff, 0x0001010100000101,
+ 0x00010101000100ff, 0x0001010100010000, 0x0001010100010100, 0x0001010101ff0001,
+ 0x00010101010000ff, 0x00010101010001ff, 0x0001010101000101, 0x0001010101010001,
+ 0x01ffffffffffffff, 0x01ffffffffffff01, 0x01ffffffffff01ff, 0x01ffffffffff0101,
+ 0x01ffffffff01ffff, 0x01ffffffff01ff01, 0x01ffffffff0101ff, 0x01ffffffff010101,
+ 0x01ffffff00ff0000, 0x01ffffff0000ffff, 0x01ffffff0000ff00, 0x01ffffff000000ff,
+ 0x01ffffff00000001, 0x01ffffff00000100, 0x01ffffff00010000, 0x01ffffff01ffffff,
+ 0x01ffffff01ffff01, 0x01ffffff01ff01ff, 0x01ffffff01ff0101, 0x01ffffff01000000,
+ 0x01ffffff0101ffff, 0x01ffffff0101ff01, 0x01ffffff010101ff, 0x01ffffff01010101,
+ 0x01ffff00ffff0000, 0x01ffff00ff00ff00, 0x01ffff00ff0000ff, 0x01ffff00ff000001,
+ 0x01ffff00ff000100, 0x01ffff00ff010000, 0x01ffff0000ffff00, 0x01ffff0000ff00ff,
+ 0x01ffff0000ff0100, 0x01ffff000000ffff, 0x01ffff000000ff01, 0x01ffff0000000000,
+ 0x01ffff0000000001, 0x01ffff00000001ff, 0x01ffff0000000100, 0x01ffff00000100ff,
+ 0x01ffff0000010001, 0x01ffff0000010100, 0x01ffff0001ff0000, 0x01ffff0001ff0100,
+ 0x01ffff00010000ff, 0x01ffff0001000001, 0x01ffff0001000100, 0x01ffff0001010000,
+ 0x01ffff01ffffffff, 0x01ffff01ffffff01, 0x01ffff01ffff01ff, 0x01ffff01ffff0101,
+ 0x01ffff01ff000000, 0x01ffff01ff01ffff, 0x01ffff01ff01ff01, 0x01ffff01ff0101ff,
+ 0x01ffff01ff010101, 0x01ffff010000ff00, 0x01ffff01000000ff, 0x01ffff0100000100,
+ 0x01ffff0100010000, 0x01ffff0101ffffff, 0x01ffff0101ffff01, 0x01ffff0101ff01ff,
+ 0x01ffff0101ff0101, 0x01ffff0101000000, 0x01ffff010101ffff, 0x01ffff010101ff01,
+ 0x01ffff01010101ff, 0x01ffff0101010101, 0x01ff00ffff0000ff, 0x01ff00ffff000100,
+ 0x01ff00ff00ffff00, 0x01ff00ff00ff00ff, 0x01ff00ff0000ff00, 0x01ff00ff00000000,
+ 0x01ff00ff00000101, 0x01ff00ff0001ff00, 0x01ff00ff000100ff, 0x01ff00ff00010100,
+ 0x01ff00ff010000ff, 0x01ff00ff01000100, 0x01ff0000ffffff00, 0x01ff0000ffff0100,
+ 0x01ff0000ff00ff01, 0x01ff0000ff000000, 0x01ff0000ff000101, 0x01ff0000ff010001,
+ 0x01ff0000ff010100, 0x01ff000000ffffff, 0x01ff000000ffff00, 0x01ff000000ff0000,
+ 0x01ff000000ff01ff, 0x01ff00000000ff00, 0x01ff0000000000ff, 0x01ff000000000000,
+ 0x01ff000000000001, 0x01ff000000000100, 0x01ff000000000101, 0x01ff000000010000,
+ 0x01ff000000010001, 0x01ff0000000101ff, 0x01ff000000010101, 0x01ff000001ffff00,
+ 0x01ff000001ff00ff, 0x01ff000001ff0001, 0x01ff000001ff0100, 0x01ff00000100ffff,
+ 0x01ff00000100ff01, 0x01ff000001000000, 0x01ff0000010001ff, 0x01ff000001010001,
+ 0x01ff0001ff00ff00, 0x01ff0001ff000001, 0x01ff0001ff000100, 0x01ff0001ff010000,
+ 0x01ff000100ffff00, 0x01ff000100ff00ff, 0x01ff000100ff0100, 0x01ff000100ff0101,
+ 0x01ff00010000ffff, 0x01ff000100000000, 0x01ff000100000100, 0x01ff000100000101,
+ 0x01ff00010001ff00, 0x01ff000100010001, 0x01ff000100010101, 0x01ff000101ff0000,
+ 0x01ff00010100ff00, 0x01ff000101000101, 0x01ff0001010100ff, 0x01ff01ffffffffff,
+ 0x01ff01ffffffff01, 0x01ff01ffffff01ff, 0x01ff01ffffff0101, 0x01ff01ffff000000,
+ 0x01ff01ffff01ffff, 0x01ff01ffff01ff01, 0x01ff01ffff0101ff, 0x01ff01ffff010101,
+ 0x01ff01ff00ffff00, 0x01ff01ff00ff0000, 0x01ff01ff0000ff00, 0x01ff01ff000000ff,
+ 0x01ff01ff00000100, 0x01ff01ff00010000, 0x01ff01ff00010100, 0x01ff01ff01ffffff,
+ 0x01ff01ff01ffff01, 0x01ff01ff01ff01ff, 0x01ff01ff01ff0101, 0x01ff01ff01000000,
+ 0x01ff01ff0101ffff, 0x01ff01ff0101ff01, 0x01ff01ff010101ff, 0x01ff01ff01010101,
+ 0x01ff0100ffff0000, 0x01ff0100ffff0001, 0x01ff0100ff00ff00, 0x01ff0100ff0000ff,
+ 0x01ff0100ff000001, 0x01ff0100ff010000, 0x01ff010000ffff00, 0x01ff010000ff00ff,
+ 0x01ff010000ff0001, 0x01ff010000ff0100, 0x01ff01000000ffff, 0x01ff01000000ff01,
+ 0x01ff010000000000, 0x01ff010000000101, 0x01ff01000001ff00, 0x01ff0100000100ff,
+ 0x01ff010001ff0000, 0x01ff010001000001, 0x01ff010001000100, 0x01ff010001010000,
+ 0x01ff0101ffffffff, 0x01ff0101ffffff01, 0x01ff0101ffff01ff, 0x01ff0101ffff0101,
+ 0x01ff0101ff000000, 0x01ff0101ff01ffff, 0x01ff0101ff01ff01, 0x01ff0101ff0101ff,
+ 0x01ff0101ff010101, 0x01ff010100ff0000, 0x01ff01010000ff00, 0x01ff0101000000ff,
+ 0x01ff010100000001, 0x01ff010101ffffff, 0x01ff010101ffff01, 0x01ff010101ff01ff,
+ 0x01ff010101ff0101, 0x01ff010101000000, 0x01ff01010101ffff, 0x01ff01010101ff01,
+ 0x01ff0101010101ff, 0x01ff010101010101, 0x0100ffffffff0000, 0x0100ffffff00ff00,
+ 0x0100ffffff000001, 0x0100ffffff0001ff, 0x0100ffffff000100, 0x0100ffffff010000,
+ 0x0100ffff00ffff00, 0x0100ffff00ff0001, 0x0100ffff00ff0100, 0x0100ffff00000000,
+ 0x0100ffff000001ff, 0x0100ffff00000101, 0x0100ffff00010100, 0x0100ffff00010101,
+ 0x0100ffff01ff0000, 0x0100ffff0100ff00, 0x0100ffff010000ff, 0x0100ffff01000001,
+ 0x0100ffff01000100, 0x0100ffff01010000, 0x0100ff00ffffff00, 0x0100ff00ffff00ff,
+ 0x0100ff00ffff0001, 0x0100ff00ffff0100, 0x0100ff00ff00ffff, 0x0100ff00ff000000,
+ 0x0100ff00ff0001ff, 0x0100ff00ff000101, 0x0100ff00ff01ff00, 0x0100ff00ff0100ff,
+ 0x0100ff00ff010001, 0x0100ff00ff010100, 0x0100ff0000ffffff, 0x0100ff0000ff0000,
+ 0x0100ff000000ffff, 0x0100ff000000ff00, 0x0100ff00000000ff, 0x0100ff0000000000,
+ 0x0100ff0000000001, 0x0100ff0000000100, 0x0100ff000001ff01, 0x0100ff0000010000,
+ 0x0100ff0001ff00ff, 0x0100ff0001ff0001, 0x0100ff000100ff01, 0x0100ff0001000000,
+ 0x0100ff00010001ff, 0x0100ff000101ff00, 0x0100ff00010100ff, 0x0100ff0001010001,
+ 0x0100ff0001010100, 0x0100ff01ffff0000, 0x0100ff01ff00ff00, 0x0100ff01ff0000ff,
+ 0x0100ff01ff000100, 0x0100ff01ff010000, 0x0100ff0100ff00ff, 0x0100ff0100ff0001,
+ 0x0100ff0100ff0100, 0x0100ff010000ffff, 0x0100ff010000ff01, 0x0100ff0100000000,
+ 0x0100ff01000001ff, 0x0100ff0100010001, 0x0100ff0100010100, 0x0100ff0101ff0000,
+ 0x0100ff01010000ff, 0x0100ff0101000001, 0x0100ff0101010100, 0x010000ffffffff00,
+ 0x010000ffffff00ff, 0x010000ffffff0001, 0x010000ffff00ffff, 0x010000ffff000000,
+ 0x010000ffff0001ff, 0x010000ffff010001, 0x010000ff00ffffff, 0x010000ff00ff0101,
+ 0x010000ff0000ff00, 0x010000ff000000ff, 0x010000ff00000000, 0x010000ff00000001,
+ 0x010000ff000001ff, 0x010000ff00000100, 0x010000ff0001ffff, 0x010000ff0001ff00,
+ 0x010000ff0001ff01, 0x010000ff00010000, 0x010000ff01ff00ff, 0x010000ff01ff0001,
+ 0x010000ff0100ff01, 0x010000ff010000ff, 0x010000ff01000000, 0x010000ff010001ff,
+ 0x010000ff0101ff00, 0x010000ff01010100, 0x01000000ffffffff, 0x01000000ffff0000,
+ 0x01000000ffff01ff, 0x01000000ffff0101, 0x01000000ff00ffff, 0x01000000ff00ff00,
+ 0x01000000ff0000ff, 0x01000000ff000000, 0x01000000ff000001, 0x01000000ff000100,
+ 0x01000000ff01ff00, 0x01000000ff010000, 0x01000000ff010100, 0x01000000ff010101,
+ 0x0100000000ffff00, 0x0100000000ff00ff, 0x0100000000ff0000, 0x0100000000ff0001,
+ 0x0100000000ff0100, 0x010000000000ffff, 0x010000000000ff00, 0x010000000000ff01,
+ 0x01000000000000ff, 0x0100000000000000, 0x0100000000000001, 0x01000000000001ff,
+ 0x0100000000000100, 0x0100000000000101, 0x010000000001ff00, 0x01000000000100ff,
+ 0x0100000000010000, 0x0100000000010001, 0x0100000000010100, 0x0100000001ffff00,
+ 0x0100000001ff0000, 0x0100000001ff01ff, 0x010000000100ff00, 0x010000000100ff01,
+ 0x01000000010000ff, 0x0100000001000000, 0x0100000001000001, 0x0100000001000100,
+ 0x0100000001000101, 0x010000000101ffff, 0x010000000101ff01, 0x0100000001010000,
+ 0x01000000010101ff, 0x0100000001010101, 0x01000001ffffff00, 0x01000001ffff00ff,
+ 0x01000001ff00ffff, 0x01000001ff000000, 0x01000001ff000100, 0x01000001ff01ffff,
+ 0x01000001ff010001, 0x01000001ff010100, 0x0100000100ff0000, 0x0100000100ff01ff,
+ 0x0100000100ff0100, 0x010000010000ff00, 0x010000010000ff01, 0x0100000100000000,
+ 0x0100000100000001, 0x0100000100000100, 0x0100000100010000, 0x01000001000101ff,
+ 0x0100000101ffff01, 0x0100000101ff00ff, 0x0100000101ff0100, 0x0100000101ff0101,
+ 0x010000010100ff01, 0x01000001010000ff, 0x0100000101000000, 0x01000001010100ff,
+ 0x0100000101010001, 0x0100000101010100, 0x010001ffffff0000, 0x010001ffff000001,
+ 0x010001ffff000100, 0x010001ffff010000, 0x010001ff00ffff00, 0x010001ff00ff0001,
+ 0x010001ff0000ffff, 0x010001ff0000ff01, 0x010001ff00000000, 0x010001ff00000001,
+ 0x010001ff00000101, 0x010001ff000100ff, 0x010001ff00010000, 0x010001ff01ff0000,
+ 0x010001ff0100ff00, 0x010001ff01000001, 0x010001ff01000100, 0x010001ff01010000,
+ 0x01000100ffff00ff, 0x01000100ffff0001, 0x01000100ffff0100, 0x01000100ff00ffff,
+ 0x01000100ff00ff01, 0x01000100ff000000, 0x01000100ff0001ff, 0x01000100ff000101,
+ 0x01000100ff01ffff, 0x01000100ff01ff00, 0x01000100ff0100ff, 0x01000100ff010001,
+ 0x0100010000ffffff, 0x0100010000ffff01, 0x0100010000ff0000, 0x0100010000ff01ff,
+ 0x0100010000ff0101, 0x010001000000ff00, 0x01000100000000ff, 0x0100010000000000,
+ 0x0100010000000001, 0x0100010000000100, 0x010001000001ff01, 0x0100010000010000,
+ 0x0100010000010001, 0x0100010000010101, 0x0100010001ffff00, 0x0100010001ff00ff,
+ 0x010001000100ffff, 0x010001000100ff01, 0x0100010001000000, 0x0100010001000101,
+ 0x010001000101ff00, 0x0100010001010001, 0x01000101ffff0000, 0x01000101ff000000,
+ 0x01000101ff010000, 0x0100010100ff00ff, 0x0100010100ff0001, 0x0100010100ff0100,
+ 0x010001010000ffff, 0x0100010100000000, 0x01000101000001ff, 0x010001010001ff00,
+ 0x0100010101ff0000, 0x010001010100ff00, 0x01000101010000ff, 0x0100010101000000,
+ 0x0100010101000001, 0x0101ffffffffffff, 0x0101ffffffffff01, 0x0101ffffffff01ff,
+ 0x0101ffffffff0101, 0x0101ffffff000000, 0x0101ffffff01ffff, 0x0101ffffff01ff01,
+ 0x0101ffffff0101ff, 0x0101ffffff010101, 0x0101ffff00ff0000, 0x0101ffff0000ff00,
+ 0x0101ffff000000ff, 0x0101ffff00000001, 0x0101ffff00000100, 0x0101ffff01ffffff,
+ 0x0101ffff01ffff01, 0x0101ffff01ff01ff, 0x0101ffff01ff0101, 0x0101ffff01000000,
+ 0x0101ffff0101ffff, 0x0101ffff0101ff01, 0x0101ffff010101ff, 0x0101ffff01010101,
+ 0x0101ff00ffff0000, 0x0101ff00ffff0100, 0x0101ff00ff00ff00, 0x0101ff00ff0000ff,
+ 0x0101ff00ff000001, 0x0101ff00ff000100, 0x0101ff00ff000101, 0x0101ff0000ff0001,
+ 0x0101ff0000ff0100, 0x0101ff000000ff00, 0x0101ff0000000000, 0x0101ff00000001ff,
+ 0x0101ff0000000101, 0x0101ff000001ff00, 0x0101ff00000100ff, 0x0101ff0001ff0000,
+ 0x0101ff000100ffff, 0x0101ff000100ff01, 0x0101ff0001000001, 0x0101ff0001000100,
+ 0x0101ff01ffffff01, 0x0101ff01ffff01ff, 0x0101ff01ffff0101, 0x0101ff01ff00ffff,
+ 0x0101ff01ff000100, 0x0101ff01ff01ff01, 0x0101ff01ff0101ff, 0x0101ff01ff010101,
+ 0x0101ff0100ff0000, 0x0101ff010000ff00, 0x0101ff0100000001, 0x0101ff0100000100,
+ 0x0101ff0100010000, 0x0101ff0101ffffff, 0x0101ff0101ffff01, 0x0101ff0101ff01ff,
+ 0x0101ff0101ff0101, 0x0101ff0101000000, 0x0101ff010101ffff, 0x0101ff010101ff01,
+ 0x0101ff01010101ff, 0x0101ff0101010101, 0x010100ffff000100, 0x010100ffff010000,
+ 0x010100ff00ffff00, 0x010100ff00ff00ff, 0x010100ff0000ffff, 0x010100ff000000ff,
+ 0x010100ff00000000, 0x010100ff000001ff, 0x010100ff00000101, 0x010100ff0001ff00,
+ 0x010100ff00010000, 0x010100ff00010001, 0x010100ff000101ff, 0x010100ff00010100,
+ 0x010100ff01ff0000, 0x01010000ffff0001, 0x01010000ffff0100, 0x01010000ff00ffff,
+ 0x01010000ff00ff01, 0x01010000ff000000, 0x01010000ff0001ff, 0x01010000ff010001,
+ 0x01010000ff010100, 0x0101000000ffff01, 0x0101000000ff0000, 0x010100000000ff00,
+ 0x01010000000000ff, 0x0101000000000000, 0x0101000000000001, 0x0101000000000100,
+ 0x0101000000010000, 0x0101000000010101, 0x0101000001ffff00, 0x0101000001ff00ff,
+ 0x0101000001ff0000, 0x0101000001ff0001, 0x0101000001ff0100, 0x010100000100ff01,
+ 0x0101000001000000, 0x01010000010001ff, 0x01010001ffff0000, 0x01010001ff00ff00,
+ 0x01010001ff000001, 0x01010001ff000101, 0x01010001ff01ff00, 0x01010001ff010000,
+ 0x0101000100ff00ff, 0x0101000100ff0001, 0x0101000100ff0101, 0x010100010000ff01,
+ 0x0101000100000000, 0x0101000100000001, 0x01010001000001ff, 0x010100010001ffff,
+ 0x010100010001ff01, 0x0101000101ff0001, 0x010100010100ffff, 0x0101000101000000,
+ 0x0101000101000001, 0x0101000101000100, 0x010100010101ff00, 0x01010001010100ff,
+ 0x0101000101010001, 0x010101ffffffffff, 0x010101ffffffff01, 0x010101ffffff01ff,
+ 0x010101ffffff0101, 0x010101ffff01ffff, 0x010101ffff01ff01, 0x010101ffff0101ff,
+ 0x010101ffff010101, 0x010101ff0000ff00, 0x010101ff000000ff, 0x010101ff00000001,
+ 0x010101ff00000100, 0x010101ff01ffffff, 0x010101ff01ffff01, 0x010101ff01ff01ff,
+ 0x010101ff01ff0101, 0x010101ff01000000, 0x010101ff0101ffff, 0x010101ff0101ff01,
+ 0x010101ff010101ff, 0x010101ff01010101, 0x01010100ffff0000, 0x01010100ff0000ff,
+ 0x01010100ff000100, 0x01010100ff01ff00, 0x01010100ff010000, 0x0101010000ffff00,
+ 0x010101000000ffff, 0x0101010000000000, 0x0101010000000101, 0x010101000001ff00,
+ 0x0101010000010001, 0x0101010000010100, 0x010101000100ffff, 0x0101010001000001,
+ 0x01010101ffffffff, 0x01010101ffffff01, 0x01010101ffff01ff, 0x01010101ffff0101,
+ 0x01010101ff01ffff, 0x01010101ff01ff01, 0x01010101ff0101ff, 0x01010101ff010101,
+ 0x010101010000ff00, 0x01010101000000ff, 0x0101010100000001, 0x0101010101ffffff,
+ 0x0101010101ffff01, 0x0101010101ff01ff, 0x0101010101ff0101, 0x0101010101000000,
+ 0x010101010101ffff, 0x010101010101ff01, 0x01010101010101ff, 0x0101010101010101,
+};
+
+#endif
diff --git a/src/llm/iq2s_grid.h b/src/llm/iq2s_grid.h
new file mode 100644
index 0000000..3484865
--- /dev/null
+++ b/src/llm/iq2s_grid.h
@@ -0,0 +1,275 @@
+/* SPDX-License-Identifier: MIT
+ The iq2s_grid table of ggml (ggml/src/ggml-common.h), copied as data:
+ it defines the format. Copyright (c) 2023-2026 The ggml authors, under
+ the MIT licence (see the ggml repository). Generated - not formatted by
+ hand, not by clang-format either. */
+/*
+ * iq2s_grid.h - the 1024 entries an IQ2_S index points to, each eight
+ * unsigned levels in the bytes of a uint64, lowest byte first (private to
+ * quant.c).
+ */
+#ifndef JANAS_LLM_IQ2S_GRID_H
+#define JANAS_LLM_IQ2S_GRID_H
+
+#include <stdint.h>
+
+static const uint64_t iq2s_grid[1024] = {
+ 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
+ 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
+ 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
+ 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
+ 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
+ 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x08080808192b192b,
+ 0x08080808192b2b19, 0x080808082b080808, 0x080808082b08082b, 0x080808082b081919,
+ 0x080808082b082b08, 0x080808082b190819, 0x080808082b191908, 0x080808082b2b0808,
+ 0x080808082b2b1919, 0x080808082b2b2b2b, 0x0808081908080819, 0x0808081908081908,
+ 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808, 0x080808190819082b,
+ 0x0808081908191919, 0x0808081908192b08, 0x08080819082b0819, 0x08080819082b1908,
+ 0x0808081919080808, 0x080808191908082b, 0x0808081919081919, 0x0808081919082b08,
+ 0x0808081919190819, 0x0808081919191908, 0x080808191919192b, 0x0808081919192b19,
+ 0x08080819192b0808, 0x08080819192b1919, 0x08080819192b2b08, 0x080808192b080819,
+ 0x080808192b081908, 0x080808192b190808, 0x080808192b19082b, 0x080808192b191919,
+ 0x080808192b2b0819, 0x080808192b2b1908, 0x0808082b08080808, 0x0808082b0808082b,
+ 0x0808082b08081919, 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908,
+ 0x0808082b082b0808, 0x0808082b082b2b2b, 0x0808082b19080819, 0x0808082b19081908,
+ 0x0808082b1908192b, 0x0808082b19082b19, 0x0808082b19190808, 0x0808082b19191919,
+ 0x0808082b2b080808, 0x0808082b2b081919, 0x0808082b2b082b2b, 0x0808082b2b191908,
+ 0x0808082b2b2b082b, 0x0808190808080819, 0x0808190808081908, 0x080819080808192b,
+ 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b, 0x0808190808191919,
+ 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908, 0x08081908082b192b,
+ 0x08081908082b2b19, 0x0808190819080808, 0x080819081908082b, 0x0808190819081919,
+ 0x0808190819082b08, 0x0808190819082b2b, 0x0808190819190819, 0x0808190819191908,
+ 0x080819081919192b, 0x0808190819192b19, 0x08081908192b0808, 0x08081908192b082b,
+ 0x08081908192b1919, 0x080819082b080819, 0x080819082b081908, 0x080819082b08192b,
+ 0x080819082b082b19, 0x080819082b190808, 0x080819082b191919, 0x080819082b192b08,
+ 0x080819082b2b0819, 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b,
+ 0x0808191908081919, 0x0808191908082b08, 0x0808191908082b2b, 0x0808191908190819,
+ 0x0808191908191908, 0x080819190819192b, 0x0808191908192b19, 0x08081919082b0808,
+ 0x08081919082b1919, 0x08081919082b2b08, 0x0808191919080819, 0x0808191919081908,
+ 0x080819191908192b, 0x0808191919082b19, 0x0808191919190808, 0x080819191919082b,
+ 0x0808191919191919, 0x0808191919192b08, 0x08081919192b0819, 0x08081919192b1908,
+ 0x080819192b080808, 0x080819192b08082b, 0x080819192b081919, 0x080819192b082b08,
+ 0x080819192b190819, 0x080819192b191908, 0x080819192b2b0808, 0x0808192b08080819,
+ 0x0808192b08081908, 0x0808192b0808192b, 0x0808192b08082b19, 0x0808192b08190808,
+ 0x0808192b08191919, 0x0808192b19080808, 0x0808192b19081919, 0x0808192b19082b08,
+ 0x0808192b19190819, 0x0808192b19191908, 0x0808192b192b0808, 0x0808192b2b080819,
+ 0x0808192b2b081908, 0x0808192b2b190808, 0x08082b0808080808, 0x08082b080808082b,
+ 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808190819, 0x08082b0808191908,
+ 0x08082b080819192b, 0x08082b0808192b19, 0x08082b08082b0808, 0x08082b08082b1919,
+ 0x08082b08082b2b2b, 0x08082b0819080819, 0x08082b0819081908, 0x08082b081908192b,
+ 0x08082b0819082b19, 0x08082b0819190808, 0x08082b081919082b, 0x08082b0819191919,
+ 0x08082b0819192b08, 0x08082b08192b0819, 0x08082b08192b1908, 0x08082b082b080808,
+ 0x08082b082b081919, 0x08082b082b191908, 0x08082b082b2b2b2b, 0x08082b1908080819,
+ 0x08082b1908081908, 0x08082b1908190808, 0x08082b190819082b, 0x08082b1908191919,
+ 0x08082b1908192b08, 0x08082b19082b0819, 0x08082b1919080808, 0x08082b1919081919,
+ 0x08082b1919082b08, 0x08082b1919190819, 0x08082b1919191908, 0x08082b19192b0808,
+ 0x08082b192b080819, 0x08082b192b190808, 0x08082b2b08080808, 0x08082b2b08190819,
+ 0x08082b2b08191908, 0x08082b2b082b082b, 0x08082b2b082b2b08, 0x08082b2b082b2b2b,
+ 0x08082b2b19190808, 0x08082b2b2b192b19, 0x0819080808080819, 0x0819080808081908,
+ 0x081908080808192b, 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b,
+ 0x0819080808191919, 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908,
+ 0x08190808082b192b, 0x0819080819080808, 0x081908081908082b, 0x0819080819081919,
+ 0x0819080819082b08, 0x0819080819190819, 0x0819080819191908, 0x081908081919192b,
+ 0x0819080819192b19, 0x08190808192b0808, 0x08190808192b082b, 0x08190808192b1919,
+ 0x08190808192b2b08, 0x081908082b080819, 0x081908082b081908, 0x081908082b08192b,
+ 0x081908082b190808, 0x081908082b191919, 0x081908082b192b08, 0x081908082b2b0819,
+ 0x081908082b2b1908, 0x0819081908080808, 0x081908190808082b, 0x0819081908081919,
+ 0x0819081908082b08, 0x0819081908082b2b, 0x0819081908190819, 0x0819081908191908,
+ 0x081908190819192b, 0x0819081908192b19, 0x08190819082b0808, 0x08190819082b082b,
+ 0x08190819082b1919, 0x08190819082b2b08, 0x0819081919080819, 0x0819081919081908,
+ 0x081908191908192b, 0x0819081919082b19, 0x0819081919190808, 0x081908191919082b,
+ 0x0819081919191919, 0x0819081919192b08, 0x08190819192b0819, 0x08190819192b1908,
+ 0x081908192b080808, 0x081908192b08082b, 0x081908192b081919, 0x081908192b082b08,
+ 0x081908192b190819, 0x081908192b191908, 0x0819082b08080819, 0x0819082b08081908,
+ 0x0819082b08082b19, 0x0819082b08190808, 0x0819082b08191919, 0x0819082b082b0819,
+ 0x0819082b082b1908, 0x0819082b19080808, 0x0819082b19081919, 0x0819082b19190819,
+ 0x0819082b19191908, 0x0819082b2b080819, 0x0819082b2b081908, 0x0819082b2b190808,
+ 0x0819190808080808, 0x081919080808082b, 0x0819190808081919, 0x0819190808082b08,
+ 0x0819190808190819, 0x0819190808191908, 0x081919080819192b, 0x0819190808192b19,
+ 0x08191908082b0808, 0x08191908082b1919, 0x08191908082b2b08, 0x0819190819080819,
+ 0x0819190819081908, 0x081919081908192b, 0x0819190819082b19, 0x0819190819190808,
+ 0x081919081919082b, 0x0819190819191919, 0x0819190819192b08, 0x08191908192b0819,
+ 0x08191908192b1908, 0x081919082b080808, 0x081919082b08082b, 0x081919082b081919,
+ 0x081919082b082b08, 0x081919082b190819, 0x081919082b191908, 0x081919082b2b0808,
+ 0x0819191908080819, 0x0819191908081908, 0x081919190808192b, 0x0819191908082b19,
+ 0x0819191908190808, 0x081919190819082b, 0x0819191908191919, 0x0819191908192b08,
+ 0x08191919082b0819, 0x08191919082b1908, 0x0819191919080808, 0x081919191908082b,
+ 0x0819191919081919, 0x0819191919082b08, 0x0819191919190819, 0x0819191919191908,
+ 0x08191919192b0808, 0x081919192b080819, 0x081919192b081908, 0x081919192b190808,
+ 0x0819192b08080808, 0x0819192b08081919, 0x0819192b08082b08, 0x0819192b08190819,
+ 0x0819192b08191908, 0x0819192b082b0808, 0x0819192b19080819, 0x0819192b19081908,
+ 0x0819192b19190808, 0x0819192b2b080808, 0x0819192b2b2b2b2b, 0x08192b0808080819,
+ 0x08192b0808081908, 0x08192b080808192b, 0x08192b0808082b19, 0x08192b0808190808,
+ 0x08192b0808191919, 0x08192b0808192b08, 0x08192b08082b0819, 0x08192b0819080808,
+ 0x08192b081908082b, 0x08192b0819081919, 0x08192b0819082b08, 0x08192b0819190819,
+ 0x08192b0819191908, 0x08192b08192b0808, 0x08192b082b080819, 0x08192b082b081908,
+ 0x08192b1908080808, 0x08192b190808082b, 0x08192b1908081919, 0x08192b1908082b08,
+ 0x08192b1908190819, 0x08192b1908191908, 0x08192b19082b0808, 0x08192b1919080819,
+ 0x08192b1919081908, 0x08192b1919190808, 0x08192b19192b2b19, 0x08192b192b2b082b,
+ 0x08192b2b08081908, 0x08192b2b08190808, 0x08192b2b19080808, 0x08192b2b1919192b,
+ 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919, 0x082b080808082b08,
+ 0x082b080808190819, 0x082b080808191908, 0x082b08080819192b, 0x082b080808192b19,
+ 0x082b0808082b0808, 0x082b0808082b1919, 0x082b0808082b2b2b, 0x082b080819080819,
+ 0x082b080819081908, 0x082b080819190808, 0x082b08081919082b, 0x082b080819191919,
+ 0x082b0808192b1908, 0x082b08082b080808, 0x082b08082b082b2b, 0x082b08082b191908,
+ 0x082b08082b2b2b2b, 0x082b081908080819, 0x082b081908081908, 0x082b081908190808,
+ 0x082b08190819082b, 0x082b081908191919, 0x082b0819082b0819, 0x082b081919080808,
+ 0x082b08191908082b, 0x082b081919081919, 0x082b081919190819, 0x082b081919191908,
+ 0x082b0819192b0808, 0x082b08192b080819, 0x082b08192b081908, 0x082b08192b190808,
+ 0x082b082b08080808, 0x082b082b08082b2b, 0x082b082b082b082b, 0x082b082b082b2b08,
+ 0x082b082b082b2b2b, 0x082b082b19081908, 0x082b082b19190808, 0x082b082b2b082b08,
+ 0x082b082b2b082b2b, 0x082b082b2b2b2b08, 0x082b190808080819, 0x082b190808081908,
+ 0x082b19080808192b, 0x082b190808082b19, 0x082b190808190808, 0x082b190808191919,
+ 0x082b190808192b08, 0x082b1908082b0819, 0x082b1908082b1908, 0x082b190819080808,
+ 0x082b19081908082b, 0x082b190819081919, 0x082b190819082b08, 0x082b190819190819,
+ 0x082b190819191908, 0x082b1908192b0808, 0x082b19082b080819, 0x082b19082b081908,
+ 0x082b19082b190808, 0x082b191908080808, 0x082b191908081919, 0x082b191908082b08,
+ 0x082b191908190819, 0x082b191908191908, 0x082b1919082b0808, 0x082b191919080819,
+ 0x082b191919081908, 0x082b191919190808, 0x082b1919192b192b, 0x082b19192b080808,
+ 0x082b192b08080819, 0x082b192b08081908, 0x082b192b08190808, 0x082b192b19080808,
+ 0x082b192b19192b19, 0x082b2b0808080808, 0x082b2b0808081919, 0x082b2b0808190819,
+ 0x082b2b0808191908, 0x082b2b0819080819, 0x082b2b0819081908, 0x082b2b0819190808,
+ 0x082b2b082b082b2b, 0x082b2b082b2b2b2b, 0x082b2b1908080819, 0x082b2b1908081908,
+ 0x082b2b1908190808, 0x082b2b192b191919, 0x082b2b2b08082b2b, 0x082b2b2b082b082b,
+ 0x082b2b2b192b1908, 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819,
+ 0x1908080808081908, 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808,
+ 0x190808080819082b, 0x1908080808191919, 0x1908080808192b08, 0x1908080808192b2b,
+ 0x19080808082b0819, 0x19080808082b1908, 0x19080808082b192b, 0x1908080819080808,
+ 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08, 0x1908080819082b2b,
+ 0x1908080819190819, 0x1908080819191908, 0x190808081919192b, 0x1908080819192b19,
+ 0x19080808192b0808, 0x19080808192b082b, 0x19080808192b1919, 0x190808082b080819,
+ 0x190808082b081908, 0x190808082b190808, 0x190808082b191919, 0x190808082b192b08,
+ 0x190808082b2b0819, 0x190808082b2b1908, 0x1908081908080808, 0x190808190808082b,
+ 0x1908081908081919, 0x1908081908082b08, 0x1908081908190819, 0x1908081908191908,
+ 0x190808190819192b, 0x1908081908192b19, 0x19080819082b0808, 0x19080819082b082b,
+ 0x19080819082b1919, 0x1908081919080819, 0x1908081919081908, 0x190808191908192b,
+ 0x1908081919082b19, 0x1908081919190808, 0x190808191919082b, 0x1908081919191919,
+ 0x1908081919192b08, 0x19080819192b0819, 0x19080819192b1908, 0x190808192b080808,
+ 0x190808192b08082b, 0x190808192b081919, 0x190808192b082b08, 0x190808192b190819,
+ 0x190808192b191908, 0x190808192b2b0808, 0x1908082b08080819, 0x1908082b08081908,
+ 0x1908082b08190808, 0x1908082b0819082b, 0x1908082b08191919, 0x1908082b08192b08,
+ 0x1908082b082b1908, 0x1908082b19080808, 0x1908082b19081919, 0x1908082b19082b08,
+ 0x1908082b19190819, 0x1908082b19191908, 0x1908082b192b0808, 0x1908082b2b080819,
+ 0x1908082b2b081908, 0x1908190808080808, 0x190819080808082b, 0x1908190808081919,
+ 0x1908190808082b08, 0x1908190808082b2b, 0x1908190808190819, 0x1908190808191908,
+ 0x190819080819192b, 0x1908190808192b19, 0x19081908082b0808, 0x19081908082b082b,
+ 0x19081908082b1919, 0x19081908082b2b08, 0x1908190819080819, 0x1908190819081908,
+ 0x190819081908192b, 0x1908190819082b19, 0x1908190819190808, 0x190819081919082b,
+ 0x1908190819191919, 0x1908190819192b08, 0x19081908192b0819, 0x19081908192b1908,
+ 0x190819082b080808, 0x190819082b08082b, 0x190819082b081919, 0x190819082b082b08,
+ 0x190819082b190819, 0x190819082b191908, 0x190819082b2b0808, 0x1908191908080819,
+ 0x1908191908081908, 0x190819190808192b, 0x1908191908082b19, 0x1908191908190808,
+ 0x190819190819082b, 0x1908191908191919, 0x1908191908192b08, 0x19081919082b0819,
+ 0x19081919082b1908, 0x1908191919080808, 0x190819191908082b, 0x1908191919081919,
+ 0x1908191919082b08, 0x1908191919190819, 0x1908191919191908, 0x19081919192b0808,
+ 0x19081919192b2b2b, 0x190819192b080819, 0x190819192b081908, 0x190819192b190808,
+ 0x1908192b08080808, 0x1908192b0808082b, 0x1908192b08081919, 0x1908192b08082b08,
+ 0x1908192b08190819, 0x1908192b08191908, 0x1908192b082b0808, 0x1908192b19080819,
+ 0x1908192b19081908, 0x1908192b19190808, 0x1908192b2b080808, 0x1908192b2b2b1919,
+ 0x19082b0808080819, 0x19082b0808081908, 0x19082b0808082b19, 0x19082b0808190808,
+ 0x19082b080819082b, 0x19082b0808191919, 0x19082b0808192b08, 0x19082b08082b0819,
+ 0x19082b08082b1908, 0x19082b0819080808, 0x19082b081908082b, 0x19082b0819081919,
+ 0x19082b0819082b08, 0x19082b0819190819, 0x19082b0819191908, 0x19082b08192b0808,
+ 0x19082b082b081908, 0x19082b082b190808, 0x19082b1908080808, 0x19082b190808082b,
+ 0x19082b1908081919, 0x19082b1908082b08, 0x19082b1908190819, 0x19082b1908191908,
+ 0x19082b19082b0808, 0x19082b1919080819, 0x19082b1919081908, 0x19082b1919190808,
+ 0x19082b192b080808, 0x19082b192b19192b, 0x19082b2b08080819, 0x19082b2b08081908,
+ 0x19082b2b08190808, 0x19082b2b19080808, 0x1919080808080808, 0x191908080808082b,
+ 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819, 0x1919080808191908,
+ 0x191908080819192b, 0x1919080808192b19, 0x19190808082b0808, 0x19190808082b082b,
+ 0x19190808082b1919, 0x19190808082b2b08, 0x1919080819080819, 0x1919080819081908,
+ 0x191908081908192b, 0x1919080819082b19, 0x1919080819190808, 0x191908081919082b,
+ 0x1919080819191919, 0x1919080819192b08, 0x19190808192b0819, 0x19190808192b1908,
+ 0x191908082b080808, 0x191908082b08082b, 0x191908082b081919, 0x191908082b082b08,
+ 0x191908082b190819, 0x191908082b191908, 0x1919081908080819, 0x1919081908081908,
+ 0x191908190808192b, 0x1919081908082b19, 0x1919081908190808, 0x191908190819082b,
+ 0x1919081908191919, 0x1919081908192b08, 0x19190819082b0819, 0x19190819082b1908,
+ 0x1919081919080808, 0x191908191908082b, 0x1919081919081919, 0x1919081919082b08,
+ 0x1919081919190819, 0x1919081919191908, 0x19190819192b0808, 0x191908192b080819,
+ 0x191908192b081908, 0x191908192b190808, 0x1919082b08080808, 0x1919082b08081919,
+ 0x1919082b08082b08, 0x1919082b08190819, 0x1919082b08191908, 0x1919082b082b0808,
+ 0x1919082b19080819, 0x1919082b19081908, 0x1919082b19190808, 0x1919082b192b2b19,
+ 0x1919082b2b080808, 0x1919190808080819, 0x1919190808081908, 0x191919080808192b,
+ 0x1919190808082b19, 0x1919190808190808, 0x191919080819082b, 0x1919190808191919,
+ 0x1919190808192b08, 0x19191908082b0819, 0x19191908082b1908, 0x1919190819080808,
+ 0x191919081908082b, 0x1919190819081919, 0x1919190819082b08, 0x1919190819190819,
+ 0x1919190819191908, 0x19191908192b0808, 0x191919082b080819, 0x191919082b081908,
+ 0x191919082b190808, 0x1919191908080808, 0x191919190808082b, 0x1919191908081919,
+ 0x1919191908082b08, 0x1919191908190819, 0x1919191908191908, 0x19191919082b0808,
+ 0x1919191919080819, 0x1919191919081908, 0x1919191919190808, 0x191919192b080808,
+ 0x1919192b08080819, 0x1919192b08081908, 0x1919192b08190808, 0x1919192b082b192b,
+ 0x1919192b19080808, 0x19192b0808080808, 0x19192b080808082b, 0x19192b0808081919,
+ 0x19192b0808082b08, 0x19192b0808190819, 0x19192b0808191908, 0x19192b08082b0808,
+ 0x19192b0819080819, 0x19192b0819081908, 0x19192b0819190808, 0x19192b0819192b2b,
+ 0x19192b082b080808, 0x19192b1908080819, 0x19192b1908081908, 0x19192b1908190808,
+ 0x19192b1919080808, 0x19192b2b08080808, 0x19192b2b08192b19, 0x19192b2b2b081919,
+ 0x19192b2b2b2b2b08, 0x192b080808080819, 0x192b080808081908, 0x192b08080808192b,
+ 0x192b080808190808, 0x192b08080819082b, 0x192b080808191919, 0x192b080808192b08,
+ 0x192b0808082b0819, 0x192b0808082b1908, 0x192b080819080808, 0x192b080819081919,
+ 0x192b080819082b08, 0x192b080819190819, 0x192b080819191908, 0x192b0808192b0808,
+ 0x192b08082b081908, 0x192b08082b190808, 0x192b081908080808, 0x192b08190808082b,
+ 0x192b081908081919, 0x192b081908082b08, 0x192b081908190819, 0x192b081908191908,
+ 0x192b0819082b0808, 0x192b081919080819, 0x192b081919081908, 0x192b081919190808,
+ 0x192b08192b080808, 0x192b08192b192b19, 0x192b082b08081908, 0x192b082b08190808,
+ 0x192b082b19080808, 0x192b082b1919192b, 0x192b082b2b2b0819, 0x192b190808080808,
+ 0x192b190808081919, 0x192b190808082b08, 0x192b190808190819, 0x192b190808191908,
+ 0x192b1908082b0808, 0x192b190819080819, 0x192b190819081908, 0x192b190819190808,
+ 0x192b19082b080808, 0x192b191908080819, 0x192b191908081908, 0x192b191908190808,
+ 0x192b191919080808, 0x192b191919082b2b, 0x192b1919192b2b08, 0x192b19192b19082b,
+ 0x192b192b08080808, 0x192b192b2b191908, 0x192b2b0808080819, 0x192b2b0808081908,
+ 0x192b2b0808190808, 0x192b2b08192b1919, 0x192b2b082b192b08, 0x192b2b1908080808,
+ 0x192b2b19082b2b2b, 0x192b2b2b1908082b, 0x192b2b2b2b2b0819, 0x2b08080808080808,
+ 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08, 0x2b08080808190819,
+ 0x2b08080808191908, 0x2b08080808192b19, 0x2b080808082b0808, 0x2b080808082b1919,
+ 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808081919082b,
+ 0x2b08080819191919, 0x2b08080819192b08, 0x2b080808192b0819, 0x2b0808082b080808,
+ 0x2b0808082b081919, 0x2b0808082b190819, 0x2b0808082b191908, 0x2b08081908080819,
+ 0x2b08081908081908, 0x2b08081908082b19, 0x2b08081908190808, 0x2b0808190819082b,
+ 0x2b08081908191919, 0x2b08081908192b08, 0x2b080819082b0819, 0x2b080819082b1908,
+ 0x2b08081919080808, 0x2b0808191908082b, 0x2b08081919081919, 0x2b08081919082b08,
+ 0x2b08081919190819, 0x2b08081919191908, 0x2b0808192b080819, 0x2b0808192b081908,
+ 0x2b0808192b190808, 0x2b0808192b2b2b19, 0x2b08082b08080808, 0x2b08082b08081919,
+ 0x2b08082b08082b2b, 0x2b08082b08190819, 0x2b08082b08191908, 0x2b08082b19080819,
+ 0x2b08082b19081908, 0x2b08082b19190808, 0x2b08190808080819, 0x2b08190808081908,
+ 0x2b0819080808192b, 0x2b08190808082b19, 0x2b08190808190808, 0x2b0819080819082b,
+ 0x2b08190808191919, 0x2b08190808192b08, 0x2b081908082b0819, 0x2b08190819080808,
+ 0x2b0819081908082b, 0x2b08190819081919, 0x2b08190819082b08, 0x2b08190819190819,
+ 0x2b08190819191908, 0x2b081908192b0808, 0x2b0819082b080819, 0x2b0819082b081908,
+ 0x2b0819082b190808, 0x2b08191908080808, 0x2b0819190808082b, 0x2b08191908081919,
+ 0x2b08191908082b08, 0x2b08191908190819, 0x2b08191908191908, 0x2b081919082b0808,
+ 0x2b08191919080819, 0x2b08191919081908, 0x2b08191919190808, 0x2b0819192b080808,
+ 0x2b0819192b082b2b, 0x2b08192b08080819, 0x2b08192b08081908, 0x2b08192b08190808,
+ 0x2b08192b082b2b19, 0x2b08192b19080808, 0x2b082b0808080808, 0x2b082b0808081919,
+ 0x2b082b0808190819, 0x2b082b0808191908, 0x2b082b0819080819, 0x2b082b0819081908,
+ 0x2b082b0819190808, 0x2b082b082b2b082b, 0x2b082b1908080819, 0x2b082b1908081908,
+ 0x2b082b1919080808, 0x2b082b19192b1919, 0x2b082b2b082b082b, 0x2b082b2b19192b08,
+ 0x2b082b2b19192b2b, 0x2b082b2b2b08082b, 0x2b082b2b2b2b082b, 0x2b19080808080819,
+ 0x2b19080808081908, 0x2b19080808082b19, 0x2b19080808190808, 0x2b1908080819082b,
+ 0x2b19080808191919, 0x2b19080808192b08, 0x2b190808082b1908, 0x2b19080819080808,
+ 0x2b1908081908082b, 0x2b19080819081919, 0x2b19080819082b08, 0x2b19080819190819,
+ 0x2b19080819191908, 0x2b190808192b0808, 0x2b1908082b080819, 0x2b1908082b081908,
+ 0x2b1908082b190808, 0x2b19081908080808, 0x2b19081908081919, 0x2b19081908190819,
+ 0x2b19081908191908, 0x2b19081919080819, 0x2b19081919081908, 0x2b19081919190808,
+ 0x2b19081919192b2b, 0x2b19082b08080819, 0x2b19082b08081908, 0x2b19082b08190808,
+ 0x2b19082b19080808, 0x2b19082b2b2b192b, 0x2b19190808080808, 0x2b1919080808082b,
+ 0x2b19190808081919, 0x2b19190808082b08, 0x2b19190808190819, 0x2b19190808191908,
+ 0x2b191908082b0808, 0x2b19190819080819, 0x2b19190819081908, 0x2b19190819190808,
+ 0x2b1919082b080808, 0x2b1919082b19192b, 0x2b19191908080819, 0x2b19191908081908,
+ 0x2b19191908190808, 0x2b19191919080808, 0x2b1919192b192b08, 0x2b1919192b2b0819,
+ 0x2b19192b08080808, 0x2b19192b1908192b, 0x2b19192b192b1908, 0x2b192b0808080819,
+ 0x2b192b0808081908, 0x2b192b0808190808, 0x2b192b08082b192b, 0x2b192b0819080808,
+ 0x2b192b082b2b2b19, 0x2b192b1908080808, 0x2b192b1919082b19, 0x2b192b191919082b,
+ 0x2b192b2b2b190808, 0x2b2b080808080808, 0x2b2b080808081919, 0x2b2b080808082b2b,
+ 0x2b2b080808191908, 0x2b2b0808082b082b, 0x2b2b0808082b2b2b, 0x2b2b080819080819,
+ 0x2b2b080819081908, 0x2b2b080819190808, 0x2b2b08082b2b082b, 0x2b2b08082b2b2b2b,
+ 0x2b2b081919080808, 0x2b2b0819192b1919, 0x2b2b082b0808082b, 0x2b2b082b08082b2b,
+ 0x2b2b082b082b082b, 0x2b2b082b082b2b08, 0x2b2b082b082b2b2b, 0x2b2b082b2b08082b,
+ 0x2b2b082b2b082b08, 0x2b2b082b2b082b2b, 0x2b2b082b2b2b2b08, 0x2b2b190808080819,
+ 0x2b2b190808081908, 0x2b2b190808190808, 0x2b2b190819080808, 0x2b2b19082b082b19,
+ 0x2b2b19082b2b1908, 0x2b2b191908080808, 0x2b2b191908192b19, 0x2b2b192b19190819,
+ 0x2b2b2b0808082b2b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b082b, 0x2b2b2b1919191908,
+ 0x2b2b2b192b08192b, 0x2b2b2b2b08082b08, 0x2b2b2b2b08082b2b, 0x2b2b2b2b082b0808,
+ 0x2b2b2b2b082b082b, 0x2b2b2b2b082b2b08, 0x2b2b2b2b2b082b08, 0x2b2b2b2b2b2b2b2b,
+};
+
+#endif
diff --git a/src/llm/iq2xs_grid.h b/src/llm/iq2xs_grid.h
new file mode 100644
index 0000000..9f40cc9
--- /dev/null
+++ b/src/llm/iq2xs_grid.h
@@ -0,0 +1,147 @@
+/* SPDX-License-Identifier: MIT
+ The iq2xs_grid table of ggml (ggml/src/ggml-common.h), copied as data:
+ it defines the format. Copyright (c) 2023-2026 The ggml authors, under
+ the MIT licence (see the ggml repository). Generated - not formatted by
+ hand, not by clang-format either. */
+/*
+ * iq2xs_grid.h - the 512 entries an IQ2_XS index points to, each eight
+ * unsigned levels in the bytes of a uint64, lowest byte first (private to
+ * quant.c).
+ */
+#ifndef JANAS_LLM_IQ2XS_GRID_H
+#define JANAS_LLM_IQ2XS_GRID_H
+
+#include <stdint.h>
+
+static const uint64_t iq2xs_grid[512] = {
+ 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
+ 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
+ 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
+ 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
+ 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
+ 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808,
+ 0x080808082b08082b, 0x080808082b081919, 0x080808082b082b08, 0x080808082b190819,
+ 0x080808082b191908, 0x080808082b192b19, 0x080808082b2b0808, 0x0808081908080819,
+ 0x0808081908081908, 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808,
+ 0x080808190819082b, 0x0808081908191919, 0x0808081908192b08, 0x0808081908192b2b,
+ 0x08080819082b0819, 0x08080819082b1908, 0x0808081919080808, 0x080808191908082b,
+ 0x0808081919081919, 0x0808081919082b08, 0x0808081919190819, 0x0808081919191908,
+ 0x08080819192b0808, 0x08080819192b2b08, 0x080808192b080819, 0x080808192b081908,
+ 0x080808192b190808, 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b08081919,
+ 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908, 0x0808082b082b0808,
+ 0x0808082b19080819, 0x0808082b19081908, 0x0808082b19190808, 0x0808082b19191919,
+ 0x0808082b2b080808, 0x0808082b2b082b2b, 0x0808190808080819, 0x0808190808081908,
+ 0x080819080808192b, 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b,
+ 0x0808190808191919, 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908,
+ 0x0808190819080808, 0x080819081908082b, 0x0808190819081919, 0x0808190819082b08,
+ 0x0808190819190819, 0x0808190819191908, 0x080819081919192b, 0x08081908192b0808,
+ 0x080819082b080819, 0x080819082b081908, 0x080819082b190808, 0x0808191908080808,
+ 0x080819190808082b, 0x0808191908081919, 0x0808191908082b08, 0x0808191908190819,
+ 0x0808191908191908, 0x08081919082b0808, 0x0808191919080819, 0x0808191919081908,
+ 0x0808191919190808, 0x08081919192b0819, 0x080819192b080808, 0x0808192b08080819,
+ 0x0808192b08081908, 0x0808192b08190808, 0x0808192b082b192b, 0x0808192b19080808,
+ 0x0808192b1908082b, 0x0808192b2b081908, 0x08082b0808080808, 0x08082b080808082b,
+ 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808082b2b, 0x08082b0808190819,
+ 0x08082b0808191908, 0x08082b08082b0808, 0x08082b08082b1919, 0x08082b0819080819,
+ 0x08082b0819081908, 0x08082b0819190808, 0x08082b0819192b08, 0x08082b082b080808,
+ 0x08082b082b2b0808, 0x08082b082b2b2b2b, 0x08082b1908080819, 0x08082b1908081908,
+ 0x08082b1908190808, 0x08082b1919080808, 0x08082b192b080819, 0x08082b192b082b19,
+ 0x08082b2b08080808, 0x08082b2b082b0808, 0x08082b2b082b2b08, 0x08082b2b2b19192b,
+ 0x08082b2b2b2b0808, 0x0819080808080819, 0x0819080808081908, 0x081908080808192b,
+ 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b, 0x0819080808191919,
+ 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908, 0x0819080819080808,
+ 0x081908081908082b, 0x0819080819081919, 0x0819080819082b08, 0x0819080819190819,
+ 0x0819080819191908, 0x08190808192b0808, 0x08190808192b2b2b, 0x081908082b080819,
+ 0x081908082b081908, 0x081908082b190808, 0x0819081908080808, 0x081908190808082b,
+ 0x0819081908081919, 0x0819081908082b08, 0x0819081908190819, 0x0819081908191908,
+ 0x08190819082b0808, 0x0819081919080819, 0x0819081919081908, 0x0819081919190808,
+ 0x081908192b080808, 0x081908192b191908, 0x081908192b19192b, 0x0819082b08080819,
+ 0x0819082b08081908, 0x0819082b0808192b, 0x0819082b08190808, 0x0819082b19080808,
+ 0x0819082b192b0808, 0x0819190808080808, 0x081919080808082b, 0x0819190808081919,
+ 0x0819190808082b08, 0x0819190808190819, 0x0819190808191908, 0x08191908082b0808,
+ 0x0819190819080819, 0x0819190819081908, 0x0819190819082b19, 0x0819190819190808,
+ 0x08191908192b1908, 0x081919082b080808, 0x0819191908080819, 0x0819191908081908,
+ 0x0819191908190808, 0x0819191919080808, 0x0819192b08080808, 0x0819192b08191908,
+ 0x0819192b19082b19, 0x08192b0808080819, 0x08192b0808081908, 0x08192b0808190808,
+ 0x08192b080819082b, 0x08192b0819080808, 0x08192b0819191908, 0x08192b082b08192b,
+ 0x08192b1908080808, 0x08192b1908081919, 0x08192b19192b192b, 0x08192b2b19190819,
+ 0x08192b2b2b2b2b19, 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919,
+ 0x082b080808082b08, 0x082b080808082b2b, 0x082b080808190819, 0x082b080808191908,
+ 0x082b0808082b0808, 0x082b080819080819, 0x082b080819081908, 0x082b080819190808,
+ 0x082b08082b080808, 0x082b08082b2b0808, 0x082b081908080819, 0x082b081908081908,
+ 0x082b081908190808, 0x082b081919080808, 0x082b081919082b08, 0x082b0819192b1919,
+ 0x082b082b08080808, 0x082b082b082b082b, 0x082b082b2b080808, 0x082b082b2b2b2b08,
+ 0x082b190808080819, 0x082b190808081908, 0x082b190808190808, 0x082b1908082b2b19,
+ 0x082b190819080808, 0x082b191908080808, 0x082b191919080819, 0x082b19191919082b,
+ 0x082b19192b192b19, 0x082b192b08080819, 0x082b192b08192b2b, 0x082b192b2b2b192b,
+ 0x082b2b0808080808, 0x082b2b0808082b08, 0x082b2b0808082b2b, 0x082b2b08082b0808,
+ 0x082b2b0819191919, 0x082b2b082b082b08, 0x082b2b082b2b082b, 0x082b2b19192b2b08,
+ 0x082b2b192b190808, 0x082b2b2b08082b08, 0x082b2b2b082b0808, 0x082b2b2b2b08082b,
+ 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819, 0x1908080808081908,
+ 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808, 0x190808080819082b,
+ 0x1908080808191919, 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908,
+ 0x1908080819080808, 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08,
+ 0x1908080819082b2b, 0x1908080819190819, 0x1908080819191908, 0x19080808192b0808,
+ 0x19080808192b1919, 0x190808082b080819, 0x190808082b081908, 0x190808082b190808,
+ 0x1908081908080808, 0x190808190808082b, 0x1908081908081919, 0x1908081908082b08,
+ 0x1908081908190819, 0x1908081908191908, 0x19080819082b0808, 0x1908081919080819,
+ 0x1908081919081908, 0x1908081919190808, 0x190808192b080808, 0x190808192b081919,
+ 0x190808192b2b082b, 0x1908082b08080819, 0x1908082b08081908, 0x1908082b08190808,
+ 0x1908082b0819082b, 0x1908082b082b2b19, 0x1908082b19080808, 0x1908190808080808,
+ 0x190819080808082b, 0x1908190808081919, 0x1908190808082b08, 0x1908190808190819,
+ 0x1908190808191908, 0x1908190808192b19, 0x19081908082b0808, 0x1908190819080819,
+ 0x1908190819081908, 0x1908190819190808, 0x190819082b080808, 0x190819082b191908,
+ 0x1908191908080819, 0x1908191908081908, 0x1908191908190808, 0x19081919082b1908,
+ 0x1908191919080808, 0x190819192b192b2b, 0x1908192b08080808, 0x1908192b08082b2b,
+ 0x1908192b19081908, 0x1908192b19190808, 0x19082b0808080819, 0x19082b0808081908,
+ 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919, 0x19082b0819191908,
+ 0x19082b08192b082b, 0x19082b1908080808, 0x19082b1908190819, 0x19082b1919081908,
+ 0x19082b1919190808, 0x19082b19192b2b19, 0x19082b2b08081908, 0x1919080808080808,
+ 0x191908080808082b, 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819,
+ 0x1919080808191908, 0x19190808082b0808, 0x19190808082b2b08, 0x1919080819080819,
+ 0x1919080819081908, 0x1919080819190808, 0x191908082b080808, 0x1919081908080819,
+ 0x1919081908081908, 0x1919081908190808, 0x1919081908191919, 0x1919081919080808,
+ 0x191908191908082b, 0x1919082b08080808, 0x1919082b19081908, 0x1919082b2b2b2b2b,
+ 0x1919190808080819, 0x1919190808081908, 0x1919190808190808, 0x19191908082b0819,
+ 0x1919190819080808, 0x19191908192b0808, 0x191919082b080819, 0x191919082b2b0819,
+ 0x1919191908080808, 0x1919191908082b08, 0x191919192b080808, 0x191919192b082b08,
+ 0x1919192b082b0819, 0x1919192b192b2b08, 0x1919192b2b2b0819, 0x19192b0808080808,
+ 0x19192b0808191908, 0x19192b0819080819, 0x19192b0819190808, 0x19192b082b192b19,
+ 0x19192b1908192b2b, 0x19192b1919080808, 0x19192b191908082b, 0x19192b2b2b081919,
+ 0x192b080808080819, 0x192b080808081908, 0x192b080808190808, 0x192b080819080808,
+ 0x192b080819191908, 0x192b0808192b082b, 0x192b08082b08192b, 0x192b08082b2b2b19,
+ 0x192b081908080808, 0x192b082b082b1908, 0x192b082b19082b2b, 0x192b082b2b19082b,
+ 0x192b190808080808, 0x192b19080819192b, 0x192b191908190808, 0x192b191919080808,
+ 0x192b191919081919, 0x192b19192b2b1908, 0x192b2b0808080819, 0x192b2b08192b2b2b,
+ 0x192b2b19082b1919, 0x192b2b2b0808192b, 0x192b2b2b19191908, 0x192b2b2b192b082b,
+ 0x2b08080808080808, 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08,
+ 0x2b08080808190819, 0x2b08080808191908, 0x2b080808082b0808, 0x2b080808082b2b2b,
+ 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808082b080808,
+ 0x2b0808082b08082b, 0x2b0808082b2b2b08, 0x2b0808082b2b2b2b, 0x2b08081908080819,
+ 0x2b08081908081908, 0x2b0808190808192b, 0x2b08081908190808, 0x2b08081919080808,
+ 0x2b08081919190819, 0x2b08081919192b19, 0x2b08082b08080808, 0x2b08082b082b0808,
+ 0x2b08082b2b080808, 0x2b08082b2b08082b, 0x2b08082b2b2b0808, 0x2b08082b2b2b2b08,
+ 0x2b08190808080819, 0x2b08190808081908, 0x2b08190808190808, 0x2b0819080819082b,
+ 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808, 0x2b0819082b082b19,
+ 0x2b08191908080808, 0x2b08191919081908, 0x2b0819192b2b1919, 0x2b08192b08192b08,
+ 0x2b08192b192b2b2b, 0x2b082b0808080808, 0x2b082b0808082b08, 0x2b082b08082b1919,
+ 0x2b082b0819192b2b, 0x2b082b082b080808, 0x2b082b082b08082b, 0x2b082b082b2b2b08,
+ 0x2b082b190808192b, 0x2b082b2b082b082b, 0x2b082b2b2b080808, 0x2b082b2b2b082b08,
+ 0x2b082b2b2b19192b, 0x2b082b2b2b2b2b08, 0x2b19080808080819, 0x2b19080808081908,
+ 0x2b19080808190808, 0x2b19080819080808, 0x2b1908081919192b, 0x2b1908082b081908,
+ 0x2b19081908080808, 0x2b190819082b082b, 0x2b190819192b1908, 0x2b19082b1919192b,
+ 0x2b19082b2b082b19, 0x2b19190808080808, 0x2b19190808081919, 0x2b19190819081908,
+ 0x2b19190819190808, 0x2b19190819192b08, 0x2b191919082b2b19, 0x2b1919192b190808,
+ 0x2b1919192b19082b, 0x2b19192b19080819, 0x2b192b0819190819, 0x2b192b082b2b192b,
+ 0x2b192b1919082b19, 0x2b192b2b08191919, 0x2b192b2b192b0808, 0x2b2b080808080808,
+ 0x2b2b08080808082b, 0x2b2b080808082b08, 0x2b2b080808082b2b, 0x2b2b0808082b0808,
+ 0x2b2b0808082b2b2b, 0x2b2b08082b2b0808, 0x2b2b081919190819, 0x2b2b081919192b19,
+ 0x2b2b08192b2b192b, 0x2b2b082b08080808, 0x2b2b082b0808082b, 0x2b2b082b08082b08,
+ 0x2b2b082b082b2b2b, 0x2b2b082b2b080808, 0x2b2b082b2b2b0808, 0x2b2b190819080808,
+ 0x2b2b19082b191919, 0x2b2b192b192b1919, 0x2b2b192b2b192b08, 0x2b2b2b0808082b2b,
+ 0x2b2b2b08082b0808, 0x2b2b2b08082b082b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b0808,
+ 0x2b2b2b082b2b2b08, 0x2b2b2b1908081908, 0x2b2b2b192b081908, 0x2b2b2b192b08192b,
+ 0x2b2b2b2b082b2b08, 0x2b2b2b2b082b2b2b, 0x2b2b2b2b2b190819, 0x2b2b2b2b2b2b2b2b,
+};
+
+#endif
diff --git a/src/llm/iq2xxs_grid.h b/src/llm/iq2xxs_grid.h
new file mode 100644
index 0000000..4941592
--- /dev/null
+++ b/src/llm/iq2xxs_grid.h
@@ -0,0 +1,83 @@
+/* SPDX-License-Identifier: MIT
+ The iq2xxs_grid table of ggml (ggml/src/ggml-common.h), copied as data:
+ it defines the format. Copyright (c) 2023-2026 The ggml authors, under
+ the MIT licence (see the ggml repository). Generated - not formatted by
+ hand, not by clang-format either. */
+/*
+ * iq2xxs_grid.h - the 256 entries an IQ2_XXS index points to, each eight
+ * unsigned levels (8, 25 or 43) in the bytes of a uint64, lowest byte first
+ * (private to quant.c).
+ */
+#ifndef JANAS_LLM_IQ2XXS_GRID_H
+#define JANAS_LLM_IQ2XXS_GRID_H
+
+#include <stdint.h>
+
+static const uint64_t iq2xxs_grid[256] = {
+ 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
+ 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,
+ 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,
+ 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,
+ 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,
+ 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,
+ 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,
+ 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,
+ 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,
+ 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,
+ 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,
+ 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,
+ 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,
+ 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,
+ 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,
+ 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,
+ 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,
+ 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,
+ 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,
+ 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,
+ 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,
+ 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,
+ 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,
+ 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,
+ 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,
+ 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,
+ 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,
+ 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,
+ 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,
+ 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,
+ 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,
+ 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,
+ 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,
+ 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,
+ 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,
+ 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,
+ 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,
+ 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,
+ 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,
+ 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,
+ 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,
+ 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,
+ 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,
+ 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,
+ 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,
+ 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,
+ 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,
+ 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,
+ 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,
+ 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,
+ 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,
+ 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,
+ 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,
+ 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,
+ 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,
+ 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,
+ 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,
+ 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,
+ 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,
+ 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,
+ 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,
+ 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,
+ 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,
+ 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,
+};
+
+#endif
diff --git a/src/llm/iq3xxs_grid.h b/src/llm/iq3xxs_grid.h
new file mode 100644
index 0000000..bf06e4d
--- /dev/null
+++ b/src/llm/iq3xxs_grid.h
@@ -0,0 +1,62 @@
+/* SPDX-License-Identifier: MIT
+ The iq3xxs_grid table of ggml (ggml/src/ggml-common.h), copied as data:
+ it defines the format. Copyright (c) 2023-2026 The ggml authors, under
+ the MIT licence (see the ggml repository). Generated - not formatted by
+ hand, not by clang-format either. */
+/*
+ * iq3xxs_grid.h - the 256 entries an IQ3_XXS index points to, each four
+ * unsigned levels (4 to 62) in the bytes of a uint32, lowest byte first
+ * (private to quant.c).
+ */
+#ifndef JANAS_LLM_IQ3XXS_GRID_H
+#define JANAS_LLM_IQ3XXS_GRID_H
+
+#include <stdint.h>
+
+static const uint32_t iq3xxs_grid[256] = {
+ 0x04040404, 0x04040414, 0x04040424, 0x04040c0c, 0x04040c1c, 0x04040c3e,
+ 0x04041404, 0x04041414, 0x04041c0c, 0x04042414, 0x04043e1c, 0x04043e2c,
+ 0x040c040c, 0x040c041c, 0x040c0c04, 0x040c0c14, 0x040c140c, 0x040c142c,
+ 0x040c1c04, 0x040c1c14, 0x040c240c, 0x040c2c24, 0x040c3e04, 0x04140404,
+ 0x04140414, 0x04140424, 0x04140c0c, 0x04141404, 0x04141414, 0x04141c0c,
+ 0x04141c1c, 0x04141c3e, 0x04142c0c, 0x04142c3e, 0x04143e2c, 0x041c040c,
+ 0x041c043e, 0x041c0c04, 0x041c0c14, 0x041c142c, 0x041c3e04, 0x04240c1c,
+ 0x04241c3e, 0x04242424, 0x04242c3e, 0x04243e1c, 0x04243e2c, 0x042c040c,
+ 0x042c043e, 0x042c1c14, 0x042c2c14, 0x04341c2c, 0x04343424, 0x043e0c04,
+ 0x043e0c24, 0x043e0c34, 0x043e241c, 0x043e340c, 0x0c04040c, 0x0c04041c,
+ 0x0c040c04, 0x0c040c14, 0x0c04140c, 0x0c04141c, 0x0c041c04, 0x0c041c14,
+ 0x0c041c24, 0x0c04243e, 0x0c042c04, 0x0c0c0404, 0x0c0c0414, 0x0c0c0c0c,
+ 0x0c0c1404, 0x0c0c1414, 0x0c14040c, 0x0c14041c, 0x0c140c04, 0x0c140c14,
+ 0x0c14140c, 0x0c141c04, 0x0c143e14, 0x0c1c0404, 0x0c1c0414, 0x0c1c1404,
+ 0x0c1c1c0c, 0x0c1c2434, 0x0c1c3434, 0x0c24040c, 0x0c24042c, 0x0c242c04,
+ 0x0c2c1404, 0x0c2c1424, 0x0c2c2434, 0x0c2c3e0c, 0x0c34042c, 0x0c3e1414,
+ 0x0c3e2404, 0x14040404, 0x14040414, 0x14040c0c, 0x14040c1c, 0x14041404,
+ 0x14041414, 0x14041434, 0x14041c0c, 0x14042414, 0x140c040c, 0x140c041c,
+ 0x140c042c, 0x140c0c04, 0x140c0c14, 0x140c140c, 0x140c1c04, 0x140c341c,
+ 0x140c343e, 0x140c3e04, 0x14140404, 0x14140414, 0x14140c0c, 0x14140c3e,
+ 0x14141404, 0x14141414, 0x14141c3e, 0x14142404, 0x14142c2c, 0x141c040c,
+ 0x141c0c04, 0x141c0c24, 0x141c3e04, 0x141c3e24, 0x14241c2c, 0x14242c1c,
+ 0x142c041c, 0x142c143e, 0x142c240c, 0x142c3e24, 0x143e040c, 0x143e041c,
+ 0x143e0c34, 0x143e242c, 0x1c04040c, 0x1c040c04, 0x1c040c14, 0x1c04140c,
+ 0x1c04141c, 0x1c042c04, 0x1c04342c, 0x1c043e14, 0x1c0c0404, 0x1c0c0414,
+ 0x1c0c1404, 0x1c0c1c0c, 0x1c0c2424, 0x1c0c2434, 0x1c14040c, 0x1c14041c,
+ 0x1c140c04, 0x1c14142c, 0x1c142c14, 0x1c143e14, 0x1c1c0c0c, 0x1c1c1c1c,
+ 0x1c241c04, 0x1c24243e, 0x1c243e14, 0x1c2c0404, 0x1c2c0434, 0x1c2c1414,
+ 0x1c2c2c2c, 0x1c340c24, 0x1c341c34, 0x1c34341c, 0x1c3e1c1c, 0x1c3e3404,
+ 0x24040424, 0x24040c3e, 0x24041c2c, 0x24041c3e, 0x24042c1c, 0x24042c3e,
+ 0x240c3e24, 0x24141404, 0x24141c3e, 0x24142404, 0x24143404, 0x24143434,
+ 0x241c043e, 0x241c242c, 0x24240424, 0x24242c0c, 0x24243424, 0x242c142c,
+ 0x242c241c, 0x242c3e04, 0x243e042c, 0x243e0c04, 0x243e0c14, 0x243e1c04,
+ 0x2c040c14, 0x2c04240c, 0x2c043e04, 0x2c0c0404, 0x2c0c0434, 0x2c0c1434,
+ 0x2c0c2c2c, 0x2c140c24, 0x2c141c14, 0x2c143e14, 0x2c1c0414, 0x2c1c2c1c,
+ 0x2c240c04, 0x2c24141c, 0x2c24143e, 0x2c243e14, 0x2c2c0414, 0x2c2c1c0c,
+ 0x2c342c04, 0x2c3e1424, 0x2c3e2414, 0x34041424, 0x34042424, 0x34042434,
+ 0x34043424, 0x340c140c, 0x340c340c, 0x34140c3e, 0x34143424, 0x341c1c04,
+ 0x341c1c34, 0x34242424, 0x342c042c, 0x342c2c14, 0x34341c1c, 0x343e041c,
+ 0x343e140c, 0x3e04041c, 0x3e04042c, 0x3e04043e, 0x3e040c04, 0x3e041c14,
+ 0x3e042c14, 0x3e0c1434, 0x3e0c2404, 0x3e140c14, 0x3e14242c, 0x3e142c14,
+ 0x3e1c0404, 0x3e1c0c2c, 0x3e1c1c1c, 0x3e1c3404, 0x3e24140c, 0x3e24240c,
+ 0x3e2c0404, 0x3e2c0414, 0x3e2c1424, 0x3e341c04,
+};
+
+#endif
diff --git a/src/llm/jns.c b/src/llm/jns.c
index 860641d..79ea743 100644
--- a/src/llm/jns.c
+++ b/src/llm/jns.c
@@ -87,6 +87,13 @@ static int type_geometry(uint32_t type, uint64_t *blck, uint64_t *size)
{23, 256, 136}, /* IQ4_XS */
{30, 1, 2}, /* BF16 */
{39, 32, 17}, /* MXFP4 */
+ {42, 64, 18}, /* Q2_0 */
+ {18, 256, 98}, /* IQ3_XXS */
+ {16, 256, 66}, /* IQ2_XXS */
+ {17, 256, 74}, /* IQ2_XS */
+ {22, 256, 82}, /* IQ2_S */
+ {29, 256, 56}, /* IQ1_M */
+ {19, 256, 50}, /* IQ1_S */
};
for (size_t i = 0; i < sizeof(t) / sizeof(t[0]); i++)
if (t[i].type == type) {
@@ -348,6 +355,33 @@ int janas_jns_meta_ints(const struct janas_jns *j, const char *key,
return 0;
}
+int janas_jns_meta_uint_at(const struct janas_jns *j, const char *key,
+ uint64_t i, uint64_t *out, uint64_t *count)
+{
+ uint32_t t, et;
+ uint64_t p, n;
+ if (!find_key(j, key, &t, &p) || t != GG_ARRAY)
+ return -1;
+ memcpy(&et, j->metadata + p, 4);
+ memcpy(&n, j->metadata + p + 4, 8);
+ uint64_t sz = et == GG_U8 || et == GG_I8 ? 1
+ : et == GG_U16 || et == GG_I16 ? 2
+ : et == GG_U32 || et == GG_I32 ? 4
+ : et == GG_U64 || et == GG_I64 ? 8
+ : 0;
+ if (!sz || i >= n)
+ return -1;
+ const uint8_t *e = j->metadata + p + 12 + i * sz; /* validated at open */
+ uint64_t v = 0;
+ memcpy(&v, e, sz); /* little-endian, as the file */
+ if ((et == GG_I8 || et == GG_I16 || et == GG_I32) && (e[sz - 1] & 0x80))
+ v |= ~0ull << (8 * sz); /* sign-extended */
+ *out = v;
+ if (count)
+ *count = n;
+ return 0;
+}
+
int janas_jns_meta_bytes(const struct janas_jns *j, const char *key,
uint64_t *count, const uint8_t **data)
{
diff --git a/src/llm/jns.h b/src/llm/jns.h
index 21d488c..a7b7aa2 100644
--- a/src/llm/jns.h
+++ b/src/llm/jns.h
@@ -181,6 +181,11 @@ int janas_jns_meta_ints(const struct janas_jns *j, const char *key,
/* An array of bytes: bool, u8 or i8 (sliding_window_pattern). */
int janas_jns_meta_bytes(const struct janas_jns *j, const char *key,
uint64_t *count, const uint8_t **data);
+/* Element i of an array of integers of any of GGUF's widths, as a u64 (a
+ signed one cast), and the array's length: 0, or -1 if the key is no such
+ array or i is past its end (qwen4exp's n-gram hashes, 64 bits). */
+int janas_jns_meta_uint_at(const struct janas_jns *j, const char *key,
+ uint64_t i, uint64_t *out, uint64_t *count);
/* The tensor of the resident region with this name, or NULL. */
const struct janas_jns_tensor *janas_jns_tensor(const struct janas_jns *j,
diff --git a/src/llm/matvec.c b/src/llm/matvec.c
index 1031791..a3e2d18 100644
--- a/src/llm/matvec.c
+++ b/src/llm/matvec.c
@@ -114,11 +114,12 @@ static void group_worker(void *arg, int tid, int n_threads)
size_t end =
job->first_row[t + 1] < g1 ? job->first_row[t + 1] : g1;
size_t nr = end - g0;
- size_t nb = k->cols / JANAS_QK;
- size_t row_bytes = nb * janas_qtype_block_size(k->type);
+ /* a row may end inside a group of 256 (janas_dot_rows_n) */
+ size_t nb = k->cols / JANAS_QK, tail = k->cols % JANAS_QK;
+ size_t row_bytes = janas_qtype_row_bytes(k->type, k->cols);
const char *w0 = (const char *)k->w + r0 * row_bytes;
size_t nv = k->n_vec ? k->n_vec : 1;
- size_t xs = k->x_stride ? k->x_stride : nb;
+ size_t xs = k->x_stride ? k->x_stride : nb + (tail != 0);
size_t ys = k->y_stride ? k->y_stride : k->rows;
if (k->type == JANAS_Q6_K_P) {
size_t pb = nb * JANAS_Q6KP_PLANE;
@@ -133,6 +134,13 @@ static void group_worker(void *arg, int tid, int n_threads)
janas_q6kp_dot_multi(row, p1, p0, k->x, xs, nv, nb,
k->y + r, ys);
}
+ } else if (tail && nv == 1) {
+ janas_dot_rows_n(k->type, w0, row_bytes, k->x, k->cols, nr,
+ k->y + r0);
+ } else if (tail) {
+ for (size_t i = 0; i < nr; i++)
+ janas_dot_multi_n(k->type, w0 + i * row_bytes, k->x, xs, nv,
+ k->cols, k->y + r0 + i, ys);
} else if (nv == 1) {
janas_dot_rows(k->type, w0, row_bytes, k->x, nb, nr, k->y + r0);
} else {
diff --git a/src/llm/model.c b/src/llm/model.c
index 25ffed6..4e91ee3 100644
--- a/src/llm/model.c
+++ b/src/llm/model.c
@@ -252,6 +252,9 @@ int janas_llm_model_shift(struct janas_llm_model *m, uint32_t keep,
{
if (!drop || n_kv > m->n_ctx || keep + drop > n_kv || m->hd_max > 512)
return -1;
+ /* qwen4exp: its n-gram history first (the one part that can refuse) */
+ if (m->hc_n && janas_m_hc_shift(m, keep, drop, n_kv) != 0)
+ return -1;
uint32_t move = n_kv - keep - drop, slots = m->n_attn + (m->mtp ? 1u : 0u);
size_t elt = (size_t)1 << m->kv16;
float cs[256], sn[256];
@@ -316,6 +319,8 @@ int janas_llm_model_shift(struct janas_llm_model *m, uint32_t keep,
}
}
free(ring_tmp);
+ if (m->hc_n) /* and the indexer's keys and blocks */
+ janas_m_qsa_shift(m, keep, drop, n_kv);
if (m->ring_hi > keep + drop)
m->ring_hi -= drop;
/* the recurrent state holds what it has seen, dropped tokens included -
@@ -470,6 +475,7 @@ static int forward(struct janas_llm_model *m, const int32_t *tokens, uint32_t n,
where it can (model_gpu.c) */
/* not where the NPU takes this pass's products: then the block goes
product by product, the NPU's to it, the others as before */
+ m->gtok_tokens = tokens; /* qwen4exp's n-gram embeddings */
int gpu = logits && !janas_m_npu_takes(m, n)
? janas_m_gpu_token(m, pos0, n, all_logits, logits)
: -1;
@@ -490,6 +496,12 @@ static int forward(struct janas_llm_model *m, const int32_t *tokens, uint32_t n,
}
return 0;
}
+ /* qwen4exp: its layers on the streams of the residual (model_hc.c) */
+ if (m->hc_n) {
+ if (janas_m_hc_layers(m, tokens, n, pos0) != 0)
+ return -1;
+ goto output;
+ }
/* a dense block's first layers on the NPU, graph by graph, as many as
it holds (model_npu.c); the others here */
int npu_layers = janas_m_npu_layers(m, n, pos0);
@@ -576,13 +588,22 @@ static int forward(struct janas_llm_model *m, const int32_t *tokens, uint32_t n,
}
}
janas_m_ana_frame(m, n); /* the Ana-Net probe's picture */
- m->ring_hi = pos0 + n; /* the rings hold up to here */
+output:
+ m->ring_hi = pos0 + n; /* the rings hold up to here */
double td = now();
uint32_t first = all_logits ? 0 : n - 1;
- /* with an MTP block, every token's final hidden state is kept for it */
+ /* with an MTP block, every token's final hidden state is kept for it:
+ qwen4exp's is its residual's streams before the last mix, which is
+ the output norm (llama.cpp's h_nextn) */
int keep = m->mtp || m->asst;
- janas_m_norm_quant(m, f32(m, m->out_norm), keep ? 0 : first, n);
- if (keep)
+ if (m->hc_n && keep)
+ memcpy(m->hid, m->hc_res, (size_t)n * m->hc_n * dm * sizeof(float));
+ if (m->hc_n) {
+ janas_m_hc_head(m, first, n);
+ td = now(); /* the mix's time is the mixes' */
+ } else
+ janas_m_norm_quant(m, f32(m, m->out_norm), keep ? 0 : first, n);
+ if (keep && !m->hc_n)
memcpy(m->hid, m->xn, (size_t)n * dm * sizeof(float));
struct janas_matvec_task to = {.type = (int)m->output->type,
.w = data(m, m->output),
@@ -637,6 +658,11 @@ const float *janas_llm_model_hidden(const struct janas_llm_model *m)
return m->hid;
}
+uint32_t janas_llm_model_hidden_width(const struct janas_llm_model *m)
+{
+ return m->hc_n ? m->hc_n * m->d_model : m->d_model;
+}
+
const float *janas_llm_model_normed(const struct janas_llm_model *m)
{
return m->xn;
@@ -756,6 +782,28 @@ int janas_llm_mtp_forward(struct janas_llm_model *m, const int32_t *tokens,
size_t nbd = dm / JANAS_QK, nbc = 2 * dm / JANAS_QK;
const struct layer *ly = &m->mtp_layer;
double ta = now();
+ if (m->hc_n) { /* qwen4exp's block, on the residual's streams */
+ uint32_t first = all_logits ? 0 : n - 1;
+ for (uint32_t j = 0; j < n; j++)
+ embed(m, tokens[j], m->x + (size_t)j * dm);
+ set_rope(m, n, pos0);
+ if (janas_m_hc_mtp(m, hidden, n, pos0, first) != 0)
+ return -1;
+ m->phase[JANAS_PH_QKV] += now() - ta;
+ if (!logits)
+ return 0;
+ double tl = now();
+ struct janas_matvec_task to = {.type = (int)m->output->type,
+ .w = data(m, m->output),
+ .x = m->xq + first * nbd,
+ .y = logits,
+ .rows = m->n_vocab,
+ .cols = dm,
+ .n_vec = n - first};
+ janas_m_products(m, janas_m_gpu(m, 0), &to, 1);
+ m->phase[JANAS_PH_OUTPUT] += now() - tl;
+ return 0;
+ }
/*
* [norm(embedding); norm(hidden)], projected back to d_model. Settled on
* data (tests/mtp_eval.c, 398 positions of C source): this order, with
diff --git a/src/llm/model.h b/src/llm/model.h
index 57a16a6..2543c69 100644
--- a/src/llm/model.h
+++ b/src/llm/model.h
@@ -32,7 +32,7 @@ struct janas_llm_options {
*/
struct janas_pool *shared_compute;
const char *mtp_path; /* MTP block (tools/hf2jns_mtp), or NULL */
- uint64_t mtp_cache_bytes; /* RAM for its experts; 0: 256 MiB */
+ uint64_t mtp_cache_bytes; /* RAM for its experts; 0: all of them */
uint32_t draft_vocab; /* lowest token ids in the draft head; 0: 32768 */
int use_gpu; /* integrated GPU for products over blocks, if usable
(environment JANAS_GPU=0/1 overrides) */
@@ -55,6 +55,12 @@ struct janas_llm_options {
(environment JANAS_HEAD_BITS overrides) */
int no_npu; /* 1: the NPU left off, its copies' memory being the
expert cache's (janas_llm_open) */
+ int ple_ram; /* 1: the per-layer table (Gemma 4's per-layer
+ embeddings, Qwen3.8-Flash-Next's n-grams) read into
+ memory at load, where the machine holds it beside the
+ experts (janas_llm_open); 0: read a row at a time
+ from the file (environment JANAS_PLE_RAM=0/1
+ overrides) */
};
struct janas_llm_model;
@@ -72,9 +78,9 @@ uint32_t janas_llm_model_n_ctx(const struct janas_llm_model *m);
beyond that length falls apart. */
uint32_t janas_llm_model_n_ctx_train(const struct janas_llm_model *m);
/* The memory the model holds: its resident weights as read (a table read
- from the file a row at a time, Gemma 4's per-layer embeddings, is not
- held), its expert cache's arena, and its keys and values with their
- scales. */
+ from the file a row at a time, Gemma 4's per-layer embeddings or
+ Qwen3.8-Flash-Next's n-grams, is not held unless read into memory), its
+ expert cache's arena, and its keys and values with their scales. */
void janas_llm_model_memory(const struct janas_llm_model *m, uint64_t *resident,
uint64_t *cache, uint64_t *kv);
@@ -97,6 +103,11 @@ void janas_llm_model_head_requant(const struct janas_llm_model *m,
uint64_t *before, uint64_t *after);
/* The output head's type in the file (BF16 30, F16 1) when it was rewritten
as Q8_0 at load, else 0; and its bytes before and after. */
+/* qwen4exp's mixes of the streams as JANAS_HC_MIX rewrote them at load:
+ its value ("q8"), or NULL when they are as in the file; and
+ their bytes before and after. */
+const char *janas_llm_model_hc_quant(const struct janas_llm_model *m,
+ uint64_t *before, uint64_t *after);
int janas_llm_model_head16(const struct janas_llm_model *m, uint64_t *before,
uint64_t *after);
/* How the attention scores are computed (janas_attn_scores), and whether the
@@ -216,8 +227,10 @@ int janas_llm_model_decode(struct janas_llm_model *m, int32_t token,
/*
* Multi-token prediction, when the model was loaded with an MTP block.
- * janas_llm_model_hidden: the final hidden states (n x d_model) of the
- * tokens of the last forward call. janas_llm_mtp_forward runs the MTP block
+ * janas_llm_model_hidden: the final hidden states (n rows of
+ * janas_llm_model_hidden_width floats: d_model, or for qwen4exp its
+ * residual's streams before the last mix) of the tokens of the last forward
+ * call. janas_llm_mtp_forward runs the MTP block
* on n rows at positions pos0 .. pos0 + n - 1: row j combines tokens[j]
* with the hidden state in row j of hidden (the main model's state for the
* token before it), and its logits guess the token after tokens[j].
@@ -236,6 +249,9 @@ int janas_llm_mtp_chain_fixed(const struct janas_llm_model *m);
go back no further than the start of its last forward call. */
int janas_llm_model_recurrent(const struct janas_llm_model *m);
const float *janas_llm_model_hidden(const struct janas_llm_model *m);
+/* The floats of a row of janas_llm_model_hidden and janas_llm_mtp_hidden:
+ d_model, or qwen4exp's residual of hc_n streams. */
+uint32_t janas_llm_model_hidden_width(const struct janas_llm_model *m);
/* The final hidden states after the output norm, rows of d_model floats,
of the last forward call: its last row, or all of them when it was asked
for all logits. With logits NULL that call computes these and no logits
@@ -268,6 +284,7 @@ enum {
JANAS_PH_EXPERTS,
JANAS_PH_OUTPUT,
JANAS_PH_SSM, /* Gated DeltaNet convolution and recurrence */
+ JANAS_PH_MIX, /* qwen4exp's mixes of the residual's streams */
JANAS_PH_COUNT
};
/* The model's compute pool, to hand to a second model as shared_compute. */
@@ -327,6 +344,8 @@ int janas_llm_model_candidates(const struct janas_llm_model *m, int *threads,
int janas_llm_model_npu_used(const struct janas_llm_model *m);
/* The NPU's name, or NULL when none is used. */
const char *janas_llm_model_npu_name(const struct janas_llm_model *m);
+/* Whether the NPU takes products of weights of this type (model_npu.c). */
+int janas_llm_npu_type(int type);
/* Seconds spent making the NPU's graphs so far (a matrix's or a layer's
first block: its weights prepared, the graph compiled and run once):
kept out of the tuner's costs, as the waits for the disk are, and out of
diff --git a/src/llm/model_attn.c b/src/llm/model_attn.c
index 8e27581..16ebdd6 100644
--- a/src/llm/model_attn.c
+++ b/src/llm/model_attn.c
@@ -253,6 +253,10 @@ void janas_m_attn_layer(struct janas_llm_model *m, const struct layer *ly,
if (nt == 2) /* Gemma 4's full layers: V is K, before K's norm */
memcpy(m->vv, m->kk, (size_t)n * kvd * sizeof(float));
janas_m_attn_core(m, ly, n, pos0, ta);
+ /* qwen4exp: past its budget, the sparse attention - not in its MTP
+ block, whose indexer is left unused: dense, as llama.cpp's */
+ if (m->ix_heads && ly->ix_q)
+ janas_m_qsa(m, ly, n, pos0);
if (m->npu) /* the NPU's calibration (JANAS_NPU_A8=1) */
janas_m_npu_calib(m, 0, m->ana_layer, m->att, n, qd, qd);
double tc = now();
diff --git a/src/llm/model_ffn.c b/src/llm/model_ffn.c
index e34c178..69082ae 100644
--- a/src/llm/model_ffn.c
+++ b/src/llm/model_ffn.c
@@ -148,7 +148,12 @@ static void act_worker(void *arg, int tid, int n_threads)
janas_m_ana_mask(m, g, h, ff);
if (m->nskipq && m->nskipq[g]) /* down by neuron reads h itself */
continue;
- janas_q8k_quantize_det(h, m->hq + g * stride / JANAS_QK, ff);
+ /* a width no multiple of 256 (qwen4exp's 640) quantized with zeros
+ after it: the down matrix's rows end inside the last group */
+ uint32_t fw = (ff + JANAS_QK - 1) / JANAS_QK * JANAS_QK;
+ if (fw > ff)
+ memset(h + ff, 0, (fw - ff) * sizeof(float));
+ janas_q8k_quantize_det(h, m->hq + g * stride / JANAS_QK, fw);
}
}
diff --git a/src/llm/model_gpu.c b/src/llm/model_gpu.c
index f28736e..8e930ca 100644
--- a/src/llm/model_gpu.c
+++ b/src/llm/model_gpu.c
@@ -19,6 +19,7 @@
#include <stdlib.h>
#include <string.h>
+#include "common/aligned.h"
#include "expert_cache.h"
#include "gpu.h"
#include "gpu_token.h"
@@ -34,10 +35,32 @@ static const char *not_for_gpu(const struct janas_llm_model *m)
/* qwen3.5: its recurrent layers and gated queries; an MTP block or a
drafting model gets the final hidden states */
int q35 = m->a->rec == JANAS_REC_Q35 && m->a->attn_gate;
+ /* qwen4exp: qwen3.5's layers inside its streams (model_hc.c), with its
+ mixes in bf16 as the file has them, or their down and up rewritten
+ as Q8_0 (JANAS_HC_MIX=q8) */
if ((m->a->rec && !q35) || (m->a->attn_gate && !q35) || m->a->swa ||
m->a->sandwich || m->a->gelu || m->kv16 || m->softcap > 0.0f ||
m->ple_dim)
return "an architecture the token's shaders do not take yet";
+ if (m->a->hc) {
+ int mt = m->hc_quant[0] ? JANAS_Q8_0 : 30; /* down and up's */
+ for (uint32_t l = 0; l <= m->n_layer; l++)
+ for (int k = 0; k < (l < m->n_layer ? 2 : 1); k++) {
+ const struct layer *ly = l < m->n_layer ? &m->layers[l] : NULL;
+ const struct janas_jns_tensor *t[3] = {
+ ly ? ly->hc_down[k] : m->hco_down,
+ ly ? ly->hc_up[k] : m->hco_up,
+ ly ? ly->hc_inject[k] : NULL};
+ for (int i = 0; i < 3; i++)
+ if ((i < 2 || ly) &&
+ (!t[i] || (int)t[i]->type != (i < 2 ? mt : 30)))
+ return "mixes of the streams of a type the GPU does "
+ "not take";
+ }
+ if (m->ng_layer < m->n_layer &&
+ m->layers[m->ng_layer].ng_value->type != 30)
+ return "n-gram values not in bf16";
+ }
if (!m->cache)
return "no feed-forward weights";
if (m->n_expert > 1) {
@@ -242,14 +265,13 @@ static int repack(struct janas_llm_model *m, int tiles)
}
/* A mixture's experts for the GPU (gpu_token.h fetch): read into the cache
- if they are not in it. */
+ if they are not in it - more than 256 too (a block of 256 tokens of prose
+ asked qwen4exp's first layer for 368 of its 512) */
static int gtok_fetch(void *ctx, uint32_t layer, const uint32_t *ids, size_t n,
const uint8_t **slots)
{
struct janas_llm_model *m = ctx;
- if (n > janas_expert_cache_slots(m->cache))
- return -1;
- return janas_expert_cache_fetch(m->cache, layer, ids, n, slots);
+ return janas_expert_cache_fetch_many(m->cache, layer, ids, n, slots);
}
/* ... and the router's choice, for those who watch it and for the counts
@@ -282,6 +304,82 @@ static void gtok_prefetch(void *ctx, uint32_t layer)
janas_expert_cache_prefetch(m->cache, layer, ids, m->n_expert, m->n_expert);
}
+/* qwen4exp's streams for the GPU: the mixes, the n-gram layer (its ring
+ imported once), the attention's inputs for the indexer (allocated and
+ imported once). 0, or -1. */
+static int hc_build(struct janas_llm_model *m, char *err, size_t len)
+{
+ struct janas_gpu_tok_model *md = &m->gtok_md;
+ size_t hd = (size_t)m->hc_n * m->d_model;
+ md->hc = m->hc_n;
+ md->hc_rank = m->hc_rank;
+ md->hc_q8 = m->hc_quant[0] != 0;
+ md->hco_norm = f32(m, m->hco_norm);
+ md->hco_down = data(m, m->hco_down);
+ md->hco_up = data(m, m->hco_up);
+ if (m->ng_layer < m->n_layer) {
+ size_t hb = ((size_t)m->ng_cap * hd * 4 + 4095) & ~(size_t)4095;
+ if (!m->gtok_ng_in) {
+ if (janas_gpu_import(m->gpu, m->ng_hist, hb) != 0) {
+ snprintf(err, len, "the n-gram ring cannot be imported");
+ return -1;
+ }
+ m->gtok_ng_in = 1;
+ }
+ md->ng_layer = m->ng_layer;
+ md->ng_kernel = m->ng_kernel;
+ md->ng_len = m->ng_len;
+ md->ng_cap = m->ng_cap;
+ md->ng_hist = m->ng_hist;
+ md->ng_emb = m->ng_emb;
+ }
+ if (m->ix_heads && m->n_attn) {
+ size_t ab =
+ ((size_t)m->n_attn * JANAS_GPU_TOK_BLOCK * m->d_model * 4 + 4095) &
+ ~(size_t)4095;
+ if (!m->gtok_att_in) {
+ m->gtok_att_in = janas_aligned_alloc(4096, ab);
+ if (!m->gtok_att_in ||
+ janas_gpu_import(m->gpu, m->gtok_att_in, ab) != 0) {
+ janas_aligned_free(m->gtok_att_in);
+ m->gtok_att_in = NULL;
+ snprintf(err, len, "no room for the indexer's inputs");
+ return -1;
+ }
+ }
+ md->att_in = m->gtok_att_in;
+ /* the sparse attention on the GPU: the indexer's keys where it
+ reads them, the cos and sin of a piece's positions */
+ uint32_t r = m->ix_ratio, d = m->ix_dim;
+ size_t nbk = (m->n_ctx + r - 1) / r,
+ rb = ((size_t)m->n_attn * m->n_ctx * d * 2 + 4095) &
+ ~(size_t)4095,
+ pb = ((size_t)m->n_attn * nbk * d * 4 + 4095) & ~(size_t)4095;
+ if (!m->gtok_ix_in) {
+ if (janas_gpu_import(m->gpu, m->ix_raw, rb) != 0 ||
+ janas_gpu_import(m->gpu, m->ix_pool, pb) != 0) {
+ snprintf(err, len, "the indexer's keys cannot be imported");
+ return -1;
+ }
+ m->gtok_ix_in = 1;
+ }
+ if (!m->gtok_ix_rope)
+ m->gtok_ix_rope = malloc(
+ ((size_t)JANAS_GPU_TOK_BLOCK + JANAS_GPU_TOK_BLOCK / r + 2) *
+ m->n_rot * sizeof(float));
+ if (!m->gtok_ix_rope) {
+ snprintf(err, len, "out of memory");
+ return -1;
+ }
+ md->ix_heads = m->ix_heads;
+ md->ix_dim = d;
+ md->ix_top = m->ix_top;
+ md->ix_ratio = r;
+ md->ix_rope = m->gtok_ix_rope;
+ }
+ return 0;
+}
+
/* The layers described for the GPU, the KV cache imported: 0, or -1. */
static int build(struct janas_llm_model *m, char *err, size_t len)
{
@@ -316,8 +414,25 @@ static int build(struct janas_llm_model *m, char *err, size_t len)
snprintf(err, len, "the feed-forward of layer %u unreadable", l);
return -1;
}
- g->attn_norm = f32(m, ly->attn_norm);
- g->ffn_norm = f32(m, ly->ffn_norm);
+ if (!m->a->hc) {
+ g->attn_norm = f32(m, ly->attn_norm);
+ g->ffn_norm = f32(m, ly->ffn_norm);
+ } else {
+ for (int k = 0; k < 2; k++) {
+ g->hc_norm[k] = f32(m, ly->hc_norm[k]);
+ g->hc_down[k] = data(m, ly->hc_down[k]);
+ g->hc_up[k] = data(m, ly->hc_up[k]);
+ g->hc_inject[k] = data(m, ly->hc_inject[k]);
+ }
+ if (l == m->ng_layer) {
+ g->ng_key = task_of(m, ly->ng_key, m->hc_n * dm, dm);
+ g->ng_value = data(m, ly->ng_value);
+ g->ng_nk = f32(m, ly->ng_nkey);
+ g->ng_nq = f32(m, ly->ng_nquery);
+ g->ng_nc = f32(m, ly->ng_nconv);
+ g->ng_conv = ly->ng_conv;
+ }
+ }
if (ly->rec) {
g->rec = 1;
g->slot = ly->slot;
@@ -331,12 +446,22 @@ static int build(struct janas_llm_model *m, char *err, size_t len)
g->ssm_a = f32(m, ly->ssm_a);
g->ssm_norm = f32(m, ly->ssm_norm);
} else {
+ g->slot = ly->slot; /* its KV slot: att_in's row (qwen4exp) */
g->q_norm = f32(m, ly->q_norm);
g->k_norm = f32(m, ly->k_norm);
g->wq = task_of(m, ly->wq, m->a->attn_gate ? 2 * qd : qd, dm);
g->wk = task_of(m, ly->wk, kvd, dm);
g->wv = task_of(m, ly->wv, kvd, dm);
g->wo = task_of(m, ly->wo, dm, qd);
+ if (m->ix_heads && ly->ix_q) {
+ size_t nbk = (m->n_ctx + m->ix_ratio - 1) / m->ix_ratio;
+ g->ix_q = data(m, ly->ix_q);
+ g->ix_k = data(m, ly->ix_k);
+ g->ix_qn = f32(m, ly->ix_qn);
+ g->ix_kn = f32(m, ly->ix_kn);
+ g->ix_raw = m->ix_raw + (size_t)ly->slot * m->n_ctx * m->ix_dim;
+ g->ix_pool = m->ix_pool + (size_t)ly->slot * nbk * m->ix_dim;
+ }
}
for (int p = 0; p < 3; p++) {
const struct janas_jns_matrix *mx = &jl->m[p];
@@ -413,7 +538,7 @@ static int build(struct janas_llm_model *m, char *err, size_t len)
.eps = m->eps,
.attn_scale = 1.0f / sqrtf((float)hd),
.layers = m->gtok_layers,
- .out_norm = f32(m, m->out_norm),
+ .out_norm = m->a->hc ? NULL : f32(m, m->out_norm),
.output = task_of(m, m->output, m->n_vocab, dm),
.gated_q = m->a->attn_gate,
.n_rec = m->a->rec ? m->n_rec : 0,
@@ -438,7 +563,10 @@ static int build(struct janas_llm_model *m, char *err, size_t len)
.prefetch = moe && janas_expert_cache_can_prefetch(m->cache)
? gtok_prefetch
: NULL,
- .ctx = m};
+ .ctx = m,
+ .ng_layer = m->n_layer};
+ if (m->a->hc && hc_build(m, err, len) != 0)
+ return -1;
free(m->gblk_layers);
m->gblk_layers = NULL;
if (!moe && getenv("JANAS_GPU_REPACK") &&
@@ -582,11 +710,18 @@ int janas_m_gpu_token(struct janas_llm_model *m, uint32_t pos, uint32_t n,
uint32_t src = m->src, dst = 1 - m->src, rows = m->a->rec ? m->d_conv : 1;
int logged = m->a->rec && n <= JANAS_LLM_MAX_BLOCK;
float *hid = m->mtp || m->asst ? m->hid : NULL;
+ size_t hw = (size_t)(m->hc_n ? m->hc_n : 1) * m->d_model; /* hidden's */
+ /* qwen4exp's n-gram embeddings: the block's rows of the table */
+ if (m->hc_n && janas_m_hc_ng_begin(m, m->gtok_tokens, n, pos) != 0)
+ return -1;
for (uint32_t j = 0, k; j < n; j += k) {
uint32_t nb = janas_gpu_tok_block_at(m->gtok, &m->gtok_md, pos + j);
k = n - j < nb ? n - j : nb;
if (n - j - k > 0 && n - j - k + 1 < rows)
k -= rows; /* the last piece long enough */
+ m->gtok_md.ng_emb = m->ng_emb + (size_t)j * m->d_model;
+ if (m->gtok_md.ix_heads)
+ janas_m_qsa_rope(m, m->gtok_ix_rope, pos + j, k);
int rc = janas_gpu_tok_run(
m->gpu, m->gtok, &m->gtok_md, m->x + (size_t)j * m->d_model,
m->rope_cos + (size_t)j * half, m->rope_sin + (size_t)j * half,
@@ -594,12 +729,31 @@ int janas_m_gpu_token(struct janas_llm_model *m, uint32_t pos, uint32_t n,
j + k < n ? 2
: all ? 1
: 0,
- logits, hid ? hid + (size_t)j * m->d_model : NULL, j ? dst : src,
- dst, j ? 0 : m->replay, logged ? j : UINT32_MAX);
+ logits, hid ? hid + (size_t)j * hw : NULL, j ? dst : src, dst,
+ j ? 0 : m->replay, logged ? j : UINT32_MAX);
/* -2, or any failure after a first piece that replayed: the base
was updated in place, the CPU cannot replay on it again */
- if (rc != 0)
+ if (rc != 0) {
+ /* a block back to the CPU (its experts not all in the cache at
+ once, say): said where asked */
+ if (getenv("JANAS_GPU_VERBOSE"))
+ fprintf(stderr,
+ "janas: the whole token on the GPU failed at position "
+ "%u (%u tokens), the CPU's\n",
+ pos + j, k);
return rc == -2 || (j && m->replay) ? -2 : -1;
+ }
+ /* qwen4exp: the indexer's keys of the piece's positions, from each
+ attention layer's input the GPU kept */
+ for (uint32_t l = 0; m->gtok_att_in && l < m->n_layer; l++)
+ if (!m->layers[l].rec)
+ janas_m_qsa_keys(m, &m->layers[l],
+ m->gtok_att_in + (size_t)m->layers[l].slot *
+ JANAS_GPU_TOK_BLOCK *
+ m->d_model,
+ k, pos + j);
}
+ if (m->hc_n)
+ m->ng_hi = pos + n;
return 0;
}
diff --git a/src/llm/model_hc.c b/src/llm/model_hc.c
new file mode 100644
index 0000000..8cc4d62
--- /dev/null
+++ b/src/llm/model_hc.c
@@ -0,0 +1,864 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * model_hc.c - Qwen3.8-Flash-Next (qwen4exp): the residual in several
+ * streams (hyper-connections) and the n-gram embeddings.
+ *
+ * The residual is hc_n vectors of d_model (four), not one. Before each
+ * sublayer - the token mixer, DeltaNet or attention, and the feed-forward -
+ * a mix makes its input in place of a norm: every stream normalized with
+ * weights of its own, then a gate of low rank over all of them, gate =
+ * up(silu(down(streams) / hc_n)), and the input is the mean over the
+ * streams of each one times sigmoid(its gate); beside it inject = W
+ * streams, hc_n numbers. After the sublayer every stream gets its output
+ * times 2 sigmoid(inject / hc_n), a weight centred on 1: a zero inject is
+ * the plain residual. The last mix, without inject, is the output norm.
+ *
+ * The n-gram embeddings (the file's "ple", at layer 1, before its mix):
+ * each token's context - itself and the ng_len - 1 tokens before it, an EOS
+ * or the start of the sequence cutting it, what lies before reading as EOS
+ * - hashed for each length from 2 up into ng_per rows of one table of
+ * ng_dim numbers each (16 rows of 160, d_model, a token: 51 billion numbers
+ * in the table, read a row at a time from the file). Their concatenation e
+ * gives a key per stream and one value: stream c's gate is
+ * sigmoid(signed sqrt(norm(key_c) . norm(stream_c) / sqrt(d_model))), and
+ * the stream gets value times its gate, plus the silu of a causal conv over
+ * that normalized - kernel ng_kernel, dilated by ng_len: positions p, p - 3,
+ * p - 6, p - 9, the conv's inputs of earlier positions kept in a ring.
+ *
+ * What is computed is llama.cpp's src/models/qwen4exp.cpp (build_hc_mix,
+ * build_hc_combine, build_ple, llm_graph_input_ple), the reference the
+ * logits are compared against; its bf16 matrices meet their inputs rounded
+ * to bf16, as there.
+ */
+#define _GNU_SOURCE
+#include <math.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "common/aligned.h"
+#include "matvec.h"
+#include "vmath.h"
+
+#include "bf16.h"
+#include "model_impl.h"
+
+/* ------------------------------------------------------------------ load */
+
+static int meta_u32(struct janas_llm_model *m, const char *suffix, uint32_t *v)
+{
+ char key[128];
+ snprintf(key, sizeof(key), "%.32s.%s", m->j.h.arch, suffix);
+ return janas_jns_meta_u32(&m->j, key, v);
+}
+
+/* Element i of an integer array of the architecture's metadata. */
+static int meta_at(struct janas_llm_model *m, const char *suffix, uint64_t i,
+ uint64_t *v, uint64_t *count)
+{
+ char key[128];
+ snprintf(key, sizeof(key), "%.32s.%s", m->j.h.arch, suffix);
+ return janas_jns_meta_uint_at(&m->j, key, i, v, count);
+}
+
+/* A tensor of file j of type type (0 f32, 1 f16, 30 bf16, -1 any the
+ products read, -2 any float: f32, f16, bf16) and shape d0 x d1 (d1 0:
+ one dimension). */
+static const struct janas_jns_tensor *tensor_of(const struct janas_jns *j,
+ const char *name, int type,
+ uint64_t d0, uint64_t d1,
+ char *err, size_t err_len)
+{
+ const struct janas_jns_tensor *t = janas_jns_tensor(j, name);
+ int ok = t && t->dims[0] == d0 && (d1 ? t->dims[1] == d1 : t->n_dims == 1);
+ if (ok && type >= 0)
+ ok = t->type == (uint32_t)type;
+ else if (ok && type == -2)
+ ok = t->type == 0 || t->type == 1 || t->type == 30;
+ else if (ok)
+ ok = t->type == 30 || janas_qtype_block_size((int)t->type) != 0;
+ if (!ok)
+ snprintf(err, err_len, "tensor %s missing or unexpected", name);
+ return ok ? t : NULL;
+}
+
+/* ... of the main model's file */
+static const struct janas_jns_tensor *tensor(struct janas_llm_model *m,
+ const char *name, int type,
+ uint64_t d0, uint64_t d1,
+ char *err, size_t err_len)
+{
+ return tensor_of(&m->j, name, type, d0, d1, err, err_len);
+}
+
+int janas_m_hc_load(struct janas_llm_model *m, char *err, size_t err_len)
+{
+ uint32_t dm = m->d_model;
+ m->ng_layer = m->n_layer;
+ if (meta_u32(m, "hyper_connection.count", &m->hc_n) ||
+ meta_u32(m, "hyper_connection.low_rank", &m->hc_rank) || m->hc_n < 2 ||
+ m->hc_n > 16 || m->hc_rank == 0) {
+ snprintf(err, err_len, "hyper-connections: inconsistent counts");
+ return -1;
+ }
+ uint32_t hd = m->hc_n * dm;
+ if (!(m->hco_norm =
+ tensor(m, "output_hc_norm.weight", 0, hd, 0, err, err_len)) ||
+ !(m->hco_down = tensor(m, "output_hc_down.weight", -1, hd, m->hc_rank,
+ err, err_len)) ||
+ !(m->hco_up = tensor(m, "output_hc_up.weight", -1, m->hc_rank, hd, err,
+ err_len)))
+ return -1;
+ /* the n-gram embeddings: one layer, as llama.cpp reads them */
+ uint64_t v, count = 0;
+ if (meta_at(m, "ple.layers", 0, &v, &count) != 0)
+ return 0; /* none */
+ if (count != 1 || v >= m->n_layer ||
+ meta_u32(m, "ple.ngram_size", &m->ng_len) ||
+ meta_u32(m, "ple.heads_per_ngram", &m->ng_per) ||
+ meta_u32(m, "ple.conv_kernel", &m->ng_kernel) ||
+ meta_u32(m, "embedding_length_per_layer_input", &m->ng_dim) ||
+ m->ng_len < 2 || m->ng_len > JANAS_NG_MAX_LEN || m->ng_per == 0 ||
+ (m->ng_len - 1) * m->ng_per > JANAS_NG_MAX_HEADS || m->ng_kernel == 0 ||
+ m->ng_kernel > 8 || (m->ng_len - 1) * m->ng_per * m->ng_dim != dm) {
+ snprintf(err, err_len, "n-gram embeddings: inconsistent sizes");
+ return -1;
+ }
+ m->ng_layer = (uint32_t)v;
+ uint32_t eos;
+ if (meta_u32(m, "ple.eos_token_id", &eos)) {
+ snprintf(err, err_len, "n-gram embeddings: no EOS");
+ return -1;
+ }
+ m->ng_eos = (int32_t)eos;
+ uint32_t heads = (m->ng_len - 1) * m->ng_per;
+ uint64_t rows = 0;
+ for (uint32_t i = 0; i < m->ng_len; i++)
+ if (meta_at(m, "ple.layer_multipliers", i, &m->ng_mult[i], NULL)) {
+ snprintf(err, err_len, "n-gram embeddings: multipliers");
+ return -1;
+ }
+ for (uint32_t h = 0; h < heads; h++) {
+ uint64_t off, size;
+ if (meta_at(m, "ple.head_offsets", h, &off, NULL) ||
+ meta_at(m, "ple.head_vocab_sizes", h, &size, NULL) || size == 0 ||
+ off + size > INT32_MAX) {
+ snprintf(err, err_len, "n-gram embeddings: head ranges");
+ return -1;
+ }
+ m->ng_off[h] = (uint32_t)off;
+ m->ng_vocab[h] = (uint32_t)size;
+ if (off + size > rows)
+ rows = off + size;
+ }
+ const struct janas_jns_tensor *t =
+ janas_jns_tensor(&m->j, "per_layer_token_embd.weight");
+ if (!t || t->dims[0] != m->ng_dim || t->dims[1] < rows ||
+ janas_qtype_row_bytes((int)t->type, m->ng_dim) == 0 ||
+ janas_qtype_row_bytes((int)t->type, m->ng_dim) * t->dims[1] !=
+ t->bytes) {
+ snprintf(err, err_len,
+ "tensor per_layer_token_embd.weight missing or "
+ "unexpected");
+ return -1;
+ }
+ m->ng_tab = t;
+ /* the table is read a row at a time where it is (model_load.c) */
+ m->ple_tok = t;
+ return 0;
+}
+
+int janas_m_hc_layer_load(struct janas_llm_model *m, const struct janas_jns *j,
+ uint32_t l, struct layer *ly, char *err,
+ size_t err_len)
+{
+ uint32_t dm = m->d_model, hd = m->hc_n * dm;
+ static const char *const part[2] = {"attn", "ffn"};
+ char name[64];
+ /* the mixes' matrices bf16 (the trunk's) or quantized (unsloth's MTP
+ block: Q4_K, Q5_0, Q6_K) */
+ for (int p = 0; p < 2; p++) {
+ snprintf(name, sizeof(name), "blk.%u.hc_%s_norm.weight", l, part[p]);
+ if (!(ly->hc_norm[p] = tensor_of(j, name, 0, hd, 0, err, err_len)))
+ return -1;
+ snprintf(name, sizeof(name), "blk.%u.hc_%s_down.weight", l, part[p]);
+ if (!(ly->hc_down[p] =
+ tensor_of(j, name, -1, hd, m->hc_rank, err, err_len)))
+ return -1;
+ snprintf(name, sizeof(name), "blk.%u.hc_%s_up.weight", l, part[p]);
+ if (!(ly->hc_up[p] =
+ tensor_of(j, name, -1, m->hc_rank, hd, err, err_len)))
+ return -1;
+ snprintf(name, sizeof(name), "blk.%u.hc_%s_inject.weight", l, part[p]);
+ if (!(ly->hc_inject[p] =
+ tensor_of(j, name, -1, hd, m->hc_n, err, err_len)))
+ return -1;
+ }
+ /* the n-gram layer is the trunk's */
+ if (j != &m->j || l != m->ng_layer)
+ return 0;
+ if (!ly->rec) {
+ /* llama.cpp keeps the conv's history in the recurrent state */
+ snprintf(err, err_len, "n-gram embeddings on an attention layer");
+ return -1;
+ }
+ const struct janas_jns_tensor *conv;
+ uint32_t ed = (m->ng_len - 1) * m->ng_per * m->ng_dim; /* = dm */
+#define NG(field, suffix, type, d0, d1) \
+ snprintf(name, sizeof(name), "blk.%u." suffix, l); \
+ if (!(field = tensor(m, name, type, d0, d1, err, err_len))) \
+ return -1;
+ NG(ly->ng_key, "ple_key.weight", -1, ed, hd)
+ NG(ly->ng_value, "ple_value.weight", -1, ed, dm)
+ NG(ly->ng_nkey, "ple_norm_key.weight", 0, hd, 0)
+ NG(ly->ng_nquery, "ple_norm_query.weight", 0, hd, 0)
+ NG(ly->ng_nconv, "ple_norm_conv.weight", 0, hd, 0)
+ NG(conv, "ple_conv1d.weight", -2, m->ng_kernel, hd)
+#undef NG
+ size_t nw = (size_t)m->ng_kernel * hd;
+ if (!(ly->ng_conv = malloc(nw * sizeof(float)))) {
+ snprintf(err, err_len, "out of memory");
+ return -1;
+ }
+ /* channel c's taps k at c * kernel + k in the file, as f32 */
+ const uint16_t *h16 = data(m, conv);
+ for (size_t i = 0; i < nw; i++)
+ ly->ng_conv[i] = conv->type == 0 ? f32(m, conv)[i]
+ : conv->type == 1 ? janas_fp16_to_fp32(h16[i])
+ : janas_u2f((uint32_t)h16[i] << 16);
+ return 0;
+}
+
+/* The buffers of a block of blk tokens; the rings hold the conv's history
+ and the tokens' contexts for a block and what comes before it. */
+int janas_m_hc_alloc(struct janas_llm_model *m, char *err, size_t err_len)
+{
+ size_t B = m->blk, hd = (size_t)m->hc_n * m->d_model;
+ /* the bf16 products' inputs and outputs where the GPU can be given
+ them (janas_gpu_dense_mm: a prompt's blocks) */
+ m->hc_res = malloc(B * hd * sizeof(float));
+ /* the norms' streams, and at bf16 after them */
+ m->hc_xn = janas_m_out_alloc(m, 2 * B * hd * sizeof(float));
+ m->hc_lo = janas_m_out_alloc(m, 2 * B * m->hc_rank * sizeof(float));
+ m->hc_gate = janas_m_out_alloc(m, B * hd * sizeof(float));
+ m->hc_inj = janas_m_out_alloc(m, B * m->hc_n * sizeof(float));
+ m->hc_q = malloc(B * hd / JANAS_QK * sizeof(*m->hc_q));
+ if (!m->hc_res || !m->hc_xn || !m->hc_lo || !m->hc_gate || !m->hc_inj ||
+ !m->hc_q) {
+ snprintf(err, err_len, "out of memory");
+ return -1;
+ }
+ if (m->ng_layer < m->n_layer) {
+ size_t dm = m->d_model, hist = (size_t)(m->ng_kernel - 1) * m->ng_len;
+ m->ng_cap = (uint32_t)(B + hist + m->ng_len);
+ size_t rb = janas_qtype_row_bytes((int)m->ng_tab->type, m->ng_dim);
+ m->ng_tok = malloc((size_t)m->ng_cap * sizeof(int32_t));
+ /* whole pages: the GPU may be given it (model_gpu.c) */
+ m->ng_hist = janas_aligned_alloc(
+ 4096, ((size_t)m->ng_cap * hd * sizeof(float) + 4095) & ~4095ul);
+ /* the embeddings, and at bf16 after them */
+ m->ng_emb = janas_m_out_alloc(m, 2 * B * dm * sizeof(float));
+ m->ng_kq = janas_m_out_alloc(m, B * hd * sizeof(float));
+ m->ng_val = janas_m_out_alloc(m, B * dm * sizeof(float));
+ m->ng_gv = malloc(B * hd * sizeof(float));
+ m->ng_raw = malloc(rb + 64);
+ m->ng_q = malloc(B * dm / JANAS_QK * sizeof(*m->ng_q));
+ if (!m->ng_tok || !m->ng_hist || !m->ng_emb || !m->ng_kq ||
+ !m->ng_val || !m->ng_gv || !m->ng_raw || !m->ng_q) {
+ snprintf(err, err_len, "out of memory");
+ return -1;
+ }
+ }
+ return 0;
+}
+
+void janas_m_hc_free(struct janas_llm_model *m)
+{
+ free(m->hc_res);
+ free(m->hc_q);
+ janas_aligned_free(m->hc_xn);
+ janas_aligned_free(m->hc_lo);
+ janas_aligned_free(m->hc_gate);
+ janas_aligned_free(m->hc_inj);
+ free(m->ng_tok);
+ janas_aligned_free(m->ng_hist);
+ janas_aligned_free(m->ng_emb);
+ janas_aligned_free(m->ng_kq);
+ janas_aligned_free(m->ng_val);
+ free(m->ng_gv);
+ free(m->ng_raw);
+ free(m->ng_q);
+ for (uint32_t l = 0; m->layers && l < m->n_layer; l++)
+ free(m->layers[l].ng_conv);
+}
+
+/* --------------------------------------------------------- the products */
+
+/* y (n rows of t's rows) = scale t x (n rows of t's columns): bf16 on x at
+ bf16 (xb), or a quantized matrix on x in Q8_K (q) */
+static void product(struct janas_llm_model *m, const struct janas_jns_tensor *t,
+ const float *x, const float *xb, struct janas_block_q8k *q,
+ float *y, uint32_t n, float scale)
+{
+ uint32_t cols = (uint32_t)t->dims[0], rows = (uint32_t)t->dims[1];
+ if (t->type == 30) {
+ janas_m_bf16_product(m, data(m, t), xb, y, rows, cols, n, scale);
+ return;
+ }
+ /* a bf16 mix rewritten as Q8_0 at load (model_hc_quant.c): its inputs
+ at bf16 still, not quantized */
+ if (t->reserved == JANAS_TENSOR_FLOAT_IN && t->type == JANAS_Q8_0 &&
+ m->hc_quant[0]) {
+ janas_m_q8w_product(m, data(m, t), xb, y, rows, cols, n, scale);
+ return;
+ }
+ /* a width no multiple of 256 (the gates' rank, 320, in an MTP block's
+ Q5_0): the last group padded with zeros, as model_ffn.c does */
+ size_t nb = (cols + JANAS_QK - 1) / JANAS_QK;
+ for (uint32_t j = 0; j < n; j++) {
+ if (cols % JANAS_QK == 0) {
+ janas_q8k_quantize(x + (size_t)j * cols, q + j * nb, cols);
+ continue;
+ }
+ float pad[JANAS_QK];
+ size_t whole = cols / JANAS_QK * JANAS_QK;
+ if (whole)
+ janas_q8k_quantize(x + (size_t)j * cols, q + j * nb,
+ (uint32_t)whole);
+ memset(pad, 0, sizeof(pad));
+ memcpy(pad, x + (size_t)j * cols + whole,
+ (cols - whole) * sizeof(float));
+ janas_q8k_quantize(pad, q + j * nb + nb - 1, JANAS_QK);
+ }
+ struct janas_matvec_task tk = {.type = (int)t->type,
+ .w = data(m, t),
+ .x = q,
+ .y = y,
+ .rows = rows,
+ .cols = cols,
+ .n_vec = n,
+ .x_stride = nb};
+ janas_m_products(m, janas_m_gpu(m, 0), &tk, 1);
+ if (scale != 1.0f)
+ for (size_t i = 0; i < (size_t)n * rows; i++)
+ y[i] *= scale;
+}
+
+static void to_bf16(const float *x, float *xb, size_t n)
+{
+ janas_round_bf16_n(x, xb, n);
+}
+
+/* ---------------------------------------------------- the streams' mixes */
+
+struct hc_job {
+ struct janas_llm_model *m;
+ const float *w; /* a norm's weights, hc_n x d_model */
+ uint32_t first, n;
+};
+
+/* each stream normalized with its own weights, then the copy at bf16; a
+ thread a stream of a token, so that a single token's four go side by
+ side (their sums, in double, one after the other) */
+static void norm_worker(void *arg, int tid, int n_threads)
+{
+ struct hc_job *t = arg;
+ struct janas_llm_model *m = t->m;
+ size_t dm = m->d_model, hd = m->hc_n * dm;
+ float *xb = m->hc_xn + (size_t)m->blk * hd;
+ uint32_t hc = m->hc_n, end = t->n * hc;
+ for (uint32_t i = t->first * hc + (uint32_t)tid; i < end;
+ i += (uint32_t)n_threads) {
+ size_t o = (size_t)i * dm; /* token i / hc, stream i % hc */
+ rms_norm(m->hc_xn + o, m->hc_res + o, t->w + (i % hc) * dm,
+ (uint32_t)dm, m->eps);
+ to_bf16(m->hc_xn + o, xb + o, dm);
+ }
+}
+
+#if defined(__x86_64__)
+/* the same eight at a time: products and sums in the same order */
+__attribute__((target("avx2,fma"))) static size_t
+mean_avx2(const float *xn, const float *g, float *out, size_t dm, size_t len,
+ uint32_t hc, float inv)
+{
+ const __m256 one = _mm256_set1_ps(1.0f), zero = _mm256_setzero_ps();
+ size_t i = 0;
+ for (; i + 8 <= len; i += 8) {
+ __m256 s = zero;
+ for (uint32_t c = 0; c < hc; c++) {
+ __m256 e = janas_exp8(
+ _mm256_sub_ps(zero, _mm256_loadu_ps(g + (size_t)c * dm + i)));
+ __m256 p = _mm256_mul_ps(_mm256_loadu_ps(xn + (size_t)c * dm + i),
+ _mm256_div_ps(one, _mm256_add_ps(one, e)));
+ s = c ? _mm256_add_ps(s, p) : p;
+ }
+ _mm256_storeu_ps(out + i, _mm256_mul_ps(s, _mm256_set1_ps(inv)));
+ }
+ return i;
+}
+#endif
+
+/* elements a .. a + len of token j's mean */
+static void mean_part(struct janas_llm_model *m, uint32_t j, size_t a,
+ size_t len)
+{
+ size_t dm = m->d_model, hd = m->hc_n * dm;
+ float inv = 1.0f / (float)m->hc_n;
+ const float *xn = m->hc_xn + j * hd + a, *g = m->hc_gate + j * hd + a;
+ float *out = m->xn + j * dm + a;
+ size_t i = 0;
+#if defined(__x86_64__)
+ if (m->avx2)
+ i = mean_avx2(xn, g, out, dm, len, m->hc_n, inv);
+#endif
+ for (; i < len; i++) {
+ float s = xn[i] * sigmoid(g[i]);
+ for (uint32_t c = 1; c < m->hc_n; c++)
+ s += xn[c * dm + i] * sigmoid(g[c * dm + i]);
+ out[i] = s * inv;
+ }
+}
+
+/* the input: the mean of the streams, each times sigmoid(its gate), a
+ thread a token */
+static void mean_worker(void *arg, int tid, int n_threads)
+{
+ struct hc_job *t = arg;
+ struct janas_llm_model *m = t->m;
+ size_t dm = m->d_model;
+ for (uint32_t j = t->first + (uint32_t)tid; j < t->n;
+ j += (uint32_t)n_threads) {
+ mean_part(m, j, 0, dm);
+ janas_q8k_quantize(m->xn + j * dm, m->xq + j * (dm / JANAS_QK),
+ (uint32_t)dm);
+ }
+}
+
+/* a single token's mean, a thread a piece of MEAN_PIECE elements (each
+ element the same operations as on its own); quantized by the caller */
+#define MEAN_PIECE 256
+static void mean_one_worker(void *arg, int tid, int n_threads)
+{
+ struct hc_job *t = arg;
+ struct janas_llm_model *m = t->m;
+ size_t dm = m->d_model;
+ for (size_t a = (size_t)tid * MEAN_PIECE; a < dm;
+ a += (size_t)n_threads * MEAN_PIECE)
+ mean_part(m, t->first, a, dm - a < MEAN_PIECE ? dm - a : MEAN_PIECE);
+}
+
+static void run(struct janas_llm_model *m, void (*fn)(void *, int, int),
+ struct hc_job *t)
+{
+ if (t->n - t->first < 2)
+ fn(t, 0, 1);
+ else
+ janas_pool_run(m->compute, fn, t);
+}
+
+/*
+ * A mix of the block's streams, tokens first .. n - 1: the sublayer's input
+ * in xn (and xq), inject (NULL: none, the last mix) in hc_inj.
+ */
+static void
+hc_mix(struct janas_llm_model *m, const struct janas_jns_tensor *norm,
+ const struct janas_jns_tensor *down, const struct janas_jns_tensor *up,
+ const struct janas_jns_tensor *inject, uint32_t first, uint32_t n)
+{
+ double t0 = now();
+ size_t hd = (size_t)m->hc_n * m->d_model, r = m->hc_rank, k = n - first;
+ struct hc_job t = {.m = m, .w = f32(m, norm), .first = first, .n = n};
+ if ((size_t)k * m->hc_n < 2)
+ norm_worker(&t, 0, 1);
+ else
+ janas_pool_run(m->compute, norm_worker, &t);
+ /* each product on the inputs at bf16 for a bf16 matrix (the trunk's),
+ in Q8_K for a quantized one (an MTP block's), as llama.cpp gives
+ them */
+ const float *x = m->hc_xn + first * hd;
+ const float *xb = m->hc_xn + (size_t)m->blk * hd + first * hd;
+ float *lo = m->hc_lo + first * r, *lob = m->hc_lo + (size_t)m->blk * r;
+ product(m, down, x, xb, m->hc_q, lo, (uint32_t)k, 1.0f / (float)m->hc_n);
+ for (size_t i = 0; i < k * r; i++) {
+ lo[i] = lo[i] / (1.0f + janas_expf(-lo[i]));
+ lob[i] = janas_m_round_bf16(lo[i]);
+ }
+ product(m, up, lo, lob, m->hc_q, m->hc_gate + first * hd, (uint32_t)k,
+ 1.0f);
+ if (k == 1) {
+ size_t dm = m->d_model;
+ janas_pool_run(m->compute, mean_one_worker, &t);
+ janas_q8k_quantize(m->xn + (size_t)first * dm,
+ m->xq + first * (dm / JANAS_QK), (uint32_t)dm);
+ } else
+ run(m, mean_worker, &t);
+ if (inject)
+ product(m, inject, x, xb, m->hc_q, m->hc_inj + first * m->hc_n,
+ (uint32_t)k, 1.0f);
+ m->phase[JANAS_PH_MIX] += now() - t0;
+}
+
+/* every stream += the sublayer's output (in x) times 2 sigmoid(inject /
+ hc_n) */
+static void combine_worker(void *arg, int tid, int n_threads)
+{
+ struct hc_job *t = arg;
+ struct janas_llm_model *m = t->m;
+ size_t dm = m->d_model, hd = m->hc_n * dm;
+ float inv = 1.0f / (float)m->hc_n;
+ for (uint32_t j = (uint32_t)tid; j < t->n; j += (uint32_t)n_threads) {
+ const float *b = m->x + j * dm;
+ for (uint32_t c = 0; c < m->hc_n; c++) {
+ float w = 2.0f * sigmoid(m->hc_inj[j * m->hc_n + c] * inv);
+ float *s = m->hc_res + j * hd + c * dm;
+ for (size_t i = 0; i < dm; i++)
+ s[i] += b[i] * w;
+ }
+ }
+}
+
+static void hc_combine(struct janas_llm_model *m, uint32_t n)
+{
+ struct hc_job t = {.m = m, .n = n};
+ run(m, combine_worker, &t);
+}
+
+void janas_m_hc_head(struct janas_llm_model *m, uint32_t first, uint32_t n)
+{
+ hc_mix(m, m->hco_norm, m->hco_down, m->hco_up, NULL, first, n);
+}
+
+/* ------------------------------------------------------- the MTP block */
+
+/*
+ * As llama.cpp's graph_mtp (PR #28243): each stream of the trunk's residual
+ * normalized with weights of its own, beside the token's normalized
+ * embedding - "per stream, not pooled: pooling before the projection
+ * discards the hyper-connection residual" - projected back to d_model: the
+ * block's own streams; then its layer as the trunk's, its attention dense
+ * (QSA left for later there too, "a numerical superset below the
+ * 2048-token budget"); its streams kept for the next draft, then its final
+ * mix.
+ */
+int janas_m_hc_mtp(struct janas_llm_model *m, const float *hidden, uint32_t n,
+ uint32_t pos0, uint32_t first)
+{
+ size_t dm = m->d_model, hd = (size_t)m->hc_n * dm, nbc = 2 * dm / JANAS_QK;
+ const struct layer *ly = &m->mtp_layer;
+ const float *en = f32(m, m->enorm), *hn = f32(m, m->hnorm);
+ float *c = m->cat; /* a row of [embedding; stream] */
+ for (uint32_t j = 0; j < n; j++) {
+ rms_norm(c, m->x + (size_t)j * dm, en, (uint32_t)dm, m->eps);
+ for (uint32_t s = 0; s < m->hc_n; s++) {
+ rms_norm(c + dm, hidden + (size_t)j * hd + s * dm, hn + s * dm,
+ (uint32_t)dm, m->eps);
+ janas_q8k_quantize(c, m->catq + ((size_t)j * m->hc_n + s) * nbc,
+ (uint32_t)(2 * dm));
+ }
+ }
+ /* stream s of token j at row j hc_n + s: the residual's layout */
+ struct janas_matvec_task tp = {.type = (int)m->eh_proj->type,
+ .w = data(m, m->eh_proj),
+ .x = m->catq,
+ .y = m->hc_res,
+ .rows = dm,
+ .cols = 2 * dm,
+ .n_vec = (size_t)n * m->hc_n};
+ janas_m_products(m, janas_m_gpu(m, 0), &tp, 1);
+ hc_mix(m, ly->hc_norm[0], ly->hc_down[0], ly->hc_up[0], ly->hc_inject[0], 0,
+ n);
+ memset(m->x, 0, (size_t)n * dm * sizeof(float));
+ janas_m_attn_layer(m, ly, n, pos0);
+ hc_combine(m, n);
+ hc_mix(m, ly->hc_norm[1], ly->hc_down[1], ly->hc_up[1], ly->hc_inject[1], 0,
+ n);
+ memset(m->x, 0, (size_t)n * dm * sizeof(float));
+ if (janas_m_moe_block(m, ly, m->mtp_cache, m->mtp_jl, m->mtp_cl, n,
+ MOE_ALL) != 0)
+ return -1;
+ hc_combine(m, n);
+ /* the wide streams, which the next draft step reads (h_nextn) */
+ memcpy(m->mtp_hid, m->hc_res, (size_t)n * hd * sizeof(float));
+ hc_mix(m, m->mtp_hco_norm, m->mtp_hco_down, m->mtp_hco_up, NULL, first, n);
+ return 0;
+}
+
+/* ---------------------------------------------------- the n-gram layer */
+
+/* token at position p (ng_hi and what came before it, in the ring) */
+static int32_t ring_tok(const struct janas_llm_model *m, uint32_t p)
+{
+ return m->ng_tok[p % m->ng_cap];
+}
+
+/* the rows of the table the token at position p reads: heads in the order
+ of the lengths, 2 first */
+static void ng_rows(const struct janas_llm_model *m, uint32_t p, uint32_t *row)
+{
+ int64_t ctx[JANAS_NG_MAX_LEN];
+ ctx[0] = ring_tok(m, p);
+ int cut = 0;
+ for (uint32_t s = 1; s < m->ng_len; s++) {
+ /* the token s back; none before the start: EOS from there on, as
+ an EOS among them - not the token's own */
+ int32_t t = !cut && p >= s ? ring_tok(m, p - s) : -1;
+ cut = cut || t < 0 || t == m->ng_eos;
+ ctx[s] = cut ? m->ng_eos : t;
+ }
+ for (uint32_t len = 2; len <= m->ng_len; len++) {
+ uint64_t mixed = (uint64_t)ctx[0] * m->ng_mult[0];
+ for (uint32_t j = 1; j < len; j++)
+ mixed ^= (uint64_t)ctx[j] * m->ng_mult[j];
+ for (uint32_t g = 0; g < m->ng_per; g++) {
+ uint32_t h = (len - 2) * m->ng_per + g;
+ row[h] = (uint32_t)(mixed % m->ng_vocab[h]) + m->ng_off[h];
+ }
+ }
+}
+
+/* a token's embeddings: its rows of the table, read from the file */
+static int ng_embed(struct janas_llm_model *m, uint32_t p, float *e)
+{
+ uint32_t row[JANAS_NG_MAX_HEADS], heads = (m->ng_len - 1) * m->ng_per;
+ size_t rb = janas_qtype_row_bytes((int)m->ng_tab->type, m->ng_dim);
+ ng_rows(m, p, row);
+ for (uint32_t h = 0; h < heads; h++) {
+ if (m->ple_ram) { /* the table in memory (janas_llm_options) */
+ janas_dequantize((int)m->ng_tab->type,
+ (const uint8_t *)data(m, m->ng_tab) +
+ (uint64_t)row[h] * rb,
+ e + (size_t)h * m->ng_dim, m->ng_dim);
+ continue;
+ }
+ off_t at = (off_t)(m->ng_tab->offset + (uint64_t)row[h] * rb);
+ for (size_t done = 0; done < rb;) {
+ ssize_t r =
+ pread(m->j.fd, m->ng_raw + done, rb - done, at + (off_t)done);
+ if (r <= 0)
+ return -1;
+ done += (size_t)r;
+ }
+ janas_dequantize((int)m->ng_tab->type, m->ng_raw,
+ e + (size_t)h * m->ng_dim, m->ng_dim);
+ }
+ return 0;
+}
+
+struct ng_job {
+ struct janas_llm_model *m;
+ const struct layer *ly;
+ uint32_t n, pos0;
+};
+
+/* the streams' gates, the gated values and the conv's inputs */
+static void gate_worker(void *arg, int tid, int n_threads)
+{
+ struct ng_job *a = arg;
+ struct janas_llm_model *m = a->m;
+ const struct layer *ly = a->ly;
+ size_t dm = m->d_model, hd = m->hc_n * dm;
+ const float *nk = f32(m, ly->ng_nkey), *nq = f32(m, ly->ng_nquery),
+ *nc = f32(m, ly->ng_nconv);
+ float isd = 1.0f / sqrtf((float)dm);
+ for (uint32_t j = (uint32_t)tid; j < a->n; j += (uint32_t)n_threads) {
+ float *key = m->ng_kq + j * hd, *gv = m->ng_gv + j * hd;
+ float *in = m->ng_hist + (size_t)((a->pos0 + j) % m->ng_cap) * hd;
+ const float *val = m->ng_val + j * dm;
+ for (uint32_t c = 0; c < m->hc_n; c++) {
+ float *kc = key + c * dm, *g = gv + c * dm;
+ rms_norm(kc, kc, nk + c * dm, (uint32_t)dm, m->eps);
+ /* the stream as a query, normalized, into gv for a moment */
+ rms_norm(g, m->hc_res + j * hd + c * dm, nq + c * dm, (uint32_t)dm,
+ m->eps);
+ double dot = 0.0;
+ for (size_t i = 0; i < dm; i++)
+ dot += (double)kc[i] * g[i];
+ float s = (float)dot * isd;
+ float mag = sqrtf(fmaxf(fabsf(s), 1e-6f));
+ float gt = sigmoid(s > 0 ? mag : s < 0 ? -mag : 0.0f);
+ for (size_t i = 0; i < dm; i++)
+ g[i] = val[i] * gt;
+ rms_norm(in + c * dm, g, nc + c * dm, (uint32_t)dm, m->eps);
+ }
+ }
+}
+
+/* the conv over positions, dilated by ng_len, its silu, and the streams'
+ new values: stream += gated value + silu(conv) */
+static void conv_worker(void *arg, int tid, int n_threads)
+{
+ struct ng_job *a = arg;
+ struct janas_llm_model *m = a->m;
+ const float *w = a->ly->ng_conv;
+ size_t hd = (size_t)m->hc_n * m->d_model, K = m->ng_kernel;
+ for (uint32_t j = (uint32_t)tid; j < a->n; j += (uint32_t)n_threads) {
+ uint32_t p = a->pos0 + j;
+ const float *tap[8];
+ for (size_t k = 0; k < K; k++) {
+ /* tap k reads (K - 1 - k) * ng_len positions back */
+ uint32_t back = (uint32_t)(K - 1 - k) * m->ng_len;
+ tap[k] = p >= back
+ ? m->ng_hist + (size_t)((p - back) % m->ng_cap) * hd
+ : NULL; /* before the start: zeros */
+ }
+ float *s = m->hc_res + j * hd;
+ const float *gv = m->ng_gv + j * hd;
+ for (size_t c = 0; c < hd; c++) {
+ float acc = 0.0f;
+ for (size_t k = 0; k < K; k++) {
+ float term = tap[k] ? w[c * K + k] * tap[k][c] : 0.0f;
+ acc = k ? acc + term : term;
+ }
+ float silu = acc / (1.0f + janas_expf(-acc));
+ s[c] += gv[c] + silu;
+ }
+ }
+}
+
+int janas_m_hc_ng_begin(struct janas_llm_model *m, const int32_t *tokens,
+ uint32_t n, uint32_t pos0)
+{
+ if (m->ng_layer >= m->n_layer)
+ return 0;
+ uint32_t hist = (m->ng_kernel - 1) * m->ng_len;
+ uint32_t need = pos0 < hist ? 0 : pos0 - hist; /* oldest position read */
+ if (pos0 == 0)
+ m->ng_hi = 0;
+ /* the history must be there: not past what the ring has seen, nor
+ older than it keeps */
+ if (pos0 > m->ng_hi ||
+ (pos0 && m->ng_hi > m->ng_cap && need < m->ng_hi - m->ng_cap))
+ return -1;
+ for (uint32_t j = 0; j < n; j++)
+ m->ng_tok[(pos0 + j) % m->ng_cap] = tokens[j];
+ for (uint32_t j = 0; j < n; j++)
+ if (ng_embed(m, pos0 + j, m->ng_emb + (size_t)j * m->d_model) != 0)
+ return -1;
+ return 0;
+}
+
+static int ng_layer(struct janas_llm_model *m, const struct layer *ly,
+ const int32_t *tokens, uint32_t n, uint32_t pos0)
+{
+ if (janas_m_hc_ng_begin(m, tokens, n, pos0) != 0)
+ return -1;
+ size_t dm = m->d_model, hd = (size_t)m->hc_n * dm;
+ float *eb = m->ng_emb + (size_t)m->blk * dm;
+ to_bf16(m->ng_emb, eb, (size_t)n * dm);
+ product(m, ly->ng_key, m->ng_emb, eb, m->ng_q, m->ng_kq, n, 1.0f);
+ product(m, ly->ng_value, m->ng_emb, eb, m->ng_q, m->ng_val, n, 1.0f);
+ struct ng_job a = {.m = m, .ly = ly, .n = n, .pos0 = pos0};
+ if (n < 2)
+ gate_worker(&a, 0, 1);
+ else
+ janas_pool_run(m->compute, gate_worker, &a);
+ if (n < 2)
+ conv_worker(&a, 0, 1);
+ else
+ janas_pool_run(m->compute, conv_worker, &a);
+ m->ng_hi = pos0 + n;
+ (void)hd;
+ return 0;
+}
+
+/*
+ * The context moved along (janas_llm_model_shift): positions keep .. keep +
+ * drop - 1 forgotten, those after them drop places down. The conv's
+ * history is a summary of the last positions, as llama.cpp keeps it in its
+ * recurrent state: the last ones, whatever their places, are moved to their
+ * new places - from a copy, since in the ring a place may still hold an
+ * entry to be moved - and the tokens with them.
+ */
+int janas_m_hc_shift(struct janas_llm_model *m, uint32_t keep, uint32_t drop,
+ uint32_t n_kv)
+{
+ if (m->ng_layer >= m->n_layer || n_kv == 0)
+ return 0;
+ uint32_t hist = (m->ng_kernel - 1) * m->ng_len, n = n_kv - drop;
+ uint32_t from = n > hist ? n - hist : 0;
+ size_t hd = (size_t)m->hc_n * m->d_model;
+ /* the ring must reach n_kv (places past it, drafts taken back, are
+ computed again) and still hold every place read */
+ if (m->ng_hi < n_kv)
+ return -1;
+ float *tmp = malloc((size_t)(n - from) * hd * sizeof(float));
+ int32_t *tok = malloc((size_t)(n - from) * sizeof(int32_t) + 1);
+ if (!tmp || !tok) {
+ free(tmp);
+ free(tok);
+ return -1;
+ }
+ for (uint32_t q = from; q < n; q++) {
+ uint32_t old = q < keep ? q : q + drop; /* where it was */
+ if (m->ng_hi > m->ng_cap && old < m->ng_hi - m->ng_cap) {
+ free(tmp);
+ free(tok);
+ return -1;
+ }
+ memcpy(tmp + (size_t)(q - from) * hd,
+ m->ng_hist + (size_t)(old % m->ng_cap) * hd, hd * sizeof(float));
+ tok[q - from] = m->ng_tok[old % m->ng_cap];
+ }
+ for (uint32_t q = from; q < n; q++) {
+ memcpy(m->ng_hist + (size_t)(q % m->ng_cap) * hd,
+ tmp + (size_t)(q - from) * hd, hd * sizeof(float));
+ m->ng_tok[q % m->ng_cap] = tok[q - from];
+ }
+ free(tmp);
+ free(tok);
+ m->ng_hi = n;
+ return 0;
+}
+
+/* ------------------------------------------------------------ the layers */
+
+int janas_m_hc_layers(struct janas_llm_model *m, const int32_t *tokens,
+ uint32_t n, uint32_t pos0)
+{
+ size_t dm = m->d_model, hd = (size_t)m->hc_n * dm;
+ /* every stream starts as the embedding */
+ for (uint32_t j = 0; j < n; j++)
+ for (uint32_t c = 0; c < m->hc_n; c++)
+ memcpy(m->hc_res + j * hd + c * dm, m->x + j * dm,
+ dm * sizeof(float));
+ for (uint32_t l = 0; l < m->n_layer; l++) {
+ m->ana_layer = l;
+ const struct layer *ly = &m->layers[l];
+ if (l == m->ng_layer && ng_layer(m, ly, tokens, n, pos0) != 0)
+ return -1;
+ hc_mix(m, ly->hc_norm[0], ly->hc_down[0], ly->hc_up[0],
+ ly->hc_inject[0], 0, n);
+ trace_row(m, "hc_attn_mixed", m->xn + (size_t)(n - 1) * dm,
+ (uint32_t)dm);
+ /* the sublayers add their outputs to x: zero, the output alone */
+ memset(m->x, 0, (size_t)n * dm * sizeof(float));
+ if (ly->rec)
+ janas_m_rec_layer(m, ly, n);
+ else
+ janas_m_attn_layer(m, ly, n, pos0);
+ hc_combine(m, n);
+ hc_mix(m, ly->hc_norm[1], ly->hc_down[1], ly->hc_up[1],
+ ly->hc_inject[1], 0, n);
+ trace_row(m, "hc_ffn_mixed", m->xn + (size_t)(n - 1) * dm,
+ (uint32_t)dm);
+ memset(m->x, 0, (size_t)n * dm * sizeof(float));
+ if (janas_m_moe_block(m, ly, m->cache, &m->j.layers[l], l, n,
+ MOE_ALL) != 0)
+ return -1;
+ trace_row(m, "ffn_out", m->x + (size_t)(n - 1) * dm, (uint32_t)dm);
+ hc_combine(m, n);
+ if (m->trace) {
+ /* JANAS_TRACE=1: the last stream after each layer, last token,
+ as llama.cpp's l_last row (job/tests/c/layer_trace.c) */
+ const float *v = m->hc_res + (size_t)(n - 1) * hd + hd - dm;
+ double nn = 0;
+ for (size_t i = 0; i < dm; i++)
+ nn += (double)v[i] * v[i];
+ fprintf(stderr, "l_out-%-3u norm %10.4f %9.4f %9.4f %9.4f %9.4f\n",
+ l, sqrt(nn), v[0], v[1], v[2], v[3]);
+ }
+ }
+ return 0;
+}
diff --git a/src/llm/model_hc_quant.c b/src/llm/model_hc_quant.c
new file mode 100644
index 0000000..0b752d5
--- /dev/null
+++ b/src/llm/model_hc_quant.c
@@ -0,0 +1,168 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * model_hc_quant.c - Qwen3.8-Flash-Next's mixes of the streams rewritten
+ * as Q8_0 once, at load (JANAS_HC_MIX=q8).
+ *
+ * Every layer mixes its four streams twice, before the token mixer and
+ * before the feed-forward, with matrices kept in BF16 in the file - down
+ * (10240 x 320), up (320 x 10240) - and the output with two more: 1.26 GB
+ * read at every token, a third of what a token reads besides the experts.
+ * As Q8_0 they are read at half the bytes. Their products keep the inputs
+ * as the BF16 products have them, rounded to bf16, not quantized
+ * (janas_m_q8w_product): against llama.cpp on 400 tokens of C and 400 of
+ * Italian prose, the most likely token agreed at 393 and 370 positions
+ * (mean logit difference 0.137 and 0.164) where in BF16 at 390 and 367
+ * (0.135, 0.166); with the inputs quantized to eight bits as the quantized
+ * kernels take them, 388 and 359 (0.185, 0.204), and with the weights in
+ * Q4_K and Q5_0, as unsloth gave them in its MTP block, 382 and 349 (0.310,
+ * 0.294) (23 Oct 2026). The inject matrices (10240 x 4, a fraction of a
+ * percent of the bytes) stay in BF16.
+ *
+ * Each matrix is rewritten in place, Q8_0 being the smaller, before the GPU
+ * and the NPU look at the types; those products are the CPU's, and in the
+ * whole token on the GPU dense_mm.comp's (gpu_token_hc.c).
+ */
+#define _GNU_SOURCE
+#include <pthread.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "vmath.h"
+
+#include "model_impl.h"
+
+struct hcq_job {
+ struct janas_llm_model *m;
+ struct janas_jns_tensor **t;
+ int n, next, failed;
+ pthread_mutex_t lock;
+};
+
+/* t, BF16, rewritten as Q8_0 in place: 0, or -1 (out of memory) */
+static int requant(struct janas_llm_model *m, struct janas_jns_tensor *t)
+{
+ size_t n = t->dims[0] * t->dims[1], bytes = n / 32 * 34;
+ float *f = malloc(n * sizeof(float));
+ struct janas_block_q8_0 *q = malloc(n / 32 * sizeof(*q));
+ if (!f || !q) {
+ free(f);
+ free(q);
+ return -1;
+ }
+ uint16_t *src = (uint16_t *)data(m, t);
+ for (size_t i = 0; i < n; i++)
+ f[i] = janas_u2f((uint32_t)src[i] << 16);
+ /* rows of a multiple of 32: the matrix quantizes as one run of blocks */
+ janas_q8_0_quantize(f, q, n);
+ memcpy(src, q, bytes);
+ t->type = JANAS_Q8_0;
+ t->bytes = bytes;
+ t->reserved = JANAS_TENSOR_FLOAT_IN;
+ free(f);
+ free(q);
+ return 0;
+}
+
+static void *hcq_worker(void *arg)
+{
+ struct hcq_job *jb = arg;
+ for (;;) {
+ pthread_mutex_lock(&jb->lock);
+ int i = jb->next++;
+ pthread_mutex_unlock(&jb->lock);
+ if (i >= jb->n)
+ return NULL;
+ if (requant(jb->m, jb->t[i]) != 0) {
+ pthread_mutex_lock(&jb->lock);
+ jb->failed = 1;
+ pthread_mutex_unlock(&jb->lock);
+ }
+ }
+}
+
+/* the tensor if it is BF16, resident, its rows a multiple of 32, else
+ NULL */
+static struct janas_jns_tensor *mix(struct janas_llm_model *m, const char *name)
+{
+ struct janas_jns_tensor *t =
+ (struct janas_jns_tensor *)janas_jns_tensor(&m->j, name);
+ if (!t || t->type != 30 || t->n_dims != 2 || t->dims[0] % 32 ||
+ t->offset < m->j.h.resident_offset ||
+ t->offset + t->bytes > m->j.h.resident_offset + m->j.h.resident_bytes)
+ return NULL;
+ return t;
+}
+
+int janas_m_hc_requant(struct janas_llm_model *m, char *err, size_t err_len)
+{
+ const char *e = getenv("JANAS_HC_MIX");
+ if (!e || !strcmp(e, "bf16"))
+ return 0;
+ if (strcmp(e, "q8") != 0) {
+ snprintf(err, err_len, "JANAS_HC_MIX=%s: bf16 or q8", e);
+ return -1;
+ }
+ size_t cap = (size_t)m->n_layer * 4 + 2;
+ struct janas_jns_tensor **t = malloc(cap * sizeof(*t));
+ if (!t) {
+ snprintf(err, err_len, "out of memory");
+ return -1;
+ }
+ static const char *const part[2] = {"attn", "ffn"}, *const what[2] = {
+ "down", "up"};
+ char name[64];
+ int n = 0;
+ uint64_t before = 0, after = 0;
+ for (uint32_t l = 0; l <= m->n_layer; l++)
+ for (int p = 0; p < (l < m->n_layer ? 2 : 1); p++)
+ for (int k = 0; k < 2; k++) {
+ if (l < m->n_layer)
+ snprintf(name, sizeof(name), "blk.%u.hc_%s_%s.weight", l,
+ part[p], what[k]);
+ else /* the output's mix */
+ snprintf(name, sizeof(name), "output_hc_%s.weight",
+ what[k]);
+ struct janas_jns_tensor *x = mix(m, name);
+ if (x) {
+ before += x->bytes;
+ t[n++] = x;
+ }
+ }
+ enum { NT = 16 };
+ long cpus = sysconf(_SC_NPROCESSORS_ONLN);
+ int nt = cpus < 1 ? 1 : cpus > NT ? NT : (int)cpus;
+ struct hcq_job jb = {.m = m, .t = t, .n = n};
+ pthread_mutex_init(&jb.lock, NULL);
+ pthread_t th[NT];
+ int started = 0;
+ for (int k = 0; k < nt; k++)
+ if (pthread_create(&th[k], NULL, hcq_worker, &jb) == 0)
+ started |= 1 << k;
+ if (!started)
+ hcq_worker(&jb);
+ for (int k = 0; k < nt; k++)
+ if (started & 1 << k)
+ pthread_join(th[k], NULL);
+ pthread_mutex_destroy(&jb.lock);
+ for (int i = 0; i < n; i++)
+ after += t[i]->bytes;
+ free(t);
+ if (jb.failed) {
+ snprintf(err, err_len, "out of memory");
+ return -1;
+ }
+ snprintf(m->hc_quant, sizeof(m->hc_quant), "%s", e);
+ m->hc_quant_before = before;
+ m->hc_quant_after = after;
+ return 0;
+}
+
+const char *janas_llm_model_hc_quant(const struct janas_llm_model *m,
+ uint64_t *before, uint64_t *after)
+{
+ *before = m->hc_quant_before;
+ *after = m->hc_quant_after;
+ return m->hc_quant[0] ? m->hc_quant : NULL;
+}
diff --git a/src/llm/model_impl.h b/src/llm/model_impl.h
index 623d9fc..fbce1a3 100644
--- a/src/llm/model_impl.h
+++ b/src/llm/model_impl.h
@@ -27,7 +27,10 @@
#define MAX_K 64
#define MAX_CONV 8
#define MAX_SSM_DIM 256
-#define SSM_ROWS 32 /* state rows per recurrence work item */
+#define SSM_ROWS 32 /* state rows per recurrence work item */
+#define JANAS_NG_MAX_LEN 4 /* qwen4exp's n-grams: the longest */
+#define JANAS_NG_MAX_HEADS 32 /* and their heads, all lengths together */
+#define JANAS_QSA_MAX 8192 /* qwen4exp's sparse attention: positions kept */
struct layer {
int rec; /* Gated DeltaNet layer (qwen3next), else attention */
@@ -67,6 +70,21 @@ struct layer {
const uint16_t *router16; /* router weights, f16 (in place of the f32) */
uint16_t *router_own; /* the zero router of a dense layer, if any */
float *conv_t; /* conv weights transposed: d_conv x conv_ch */
+ /* qwen4exp (model_hc.c): the hyper-connections before the token mixer
+ (0) and before the feed-forward (1) - the streams' norms (f32, hc_n
+ x d_model), the gate's two matrices of low rank and the inject (bf16)
+ - and the n-gram embeddings' at the layer that has them: the key and
+ value projections, the norms of the keys, of the streams as queries
+ and of the conv's inputs, the conv's weights (kernel x channels, as
+ f32) */
+ const struct janas_jns_tensor *hc_norm[2], *hc_down[2], *hc_up[2],
+ *hc_inject[2];
+ const struct janas_jns_tensor *ng_key, *ng_value, *ng_nkey, *ng_nquery,
+ *ng_nconv;
+ float *ng_conv;
+ /* qwen4exp's sparse attention (model_qsa.c): the indexer's query and key
+ projections (bf16) and their norms */
+ const struct janas_jns_tensor *ix_q, *ix_k, *ix_qn, *ix_kn;
};
_Static_assert(JANAS_ATTN_MAX_BLOCK == JANAS_LLM_MAX_BLOCK, "block size");
@@ -74,6 +92,12 @@ _Static_assert(JANAS_ATTN_MAX_BLOCK == JANAS_LLM_MAX_BLOCK, "block size");
struct janas_llm_model {
struct janas_jns j;
uint8_t *resident; /* the resident region, read at load */
+ /* its bytes [res_hole[0], res_hole[1]) never read there: the
+ per-layer table, read a row at a time from the file (model_ple.c,
+ model_hc.c), kept out of the GPU's imports, which would pin its
+ untouched pages (28.8 GB on Qwen3.8-Flash-Next) */
+ uint64_t res_hole[2];
+ int ple_ram; /* that table read into memory all the same (ple_ram) */
struct janas_pool *compute, *io;
int own_compute; /* zero when the pool came from the caller */
/* compute threads: the pool lists one CPU per performance core, their
@@ -181,6 +205,15 @@ struct janas_llm_model {
with a GPU copy */
int gtok_state, gtok_kv;
uint64_t gtok_loads;
+ /* qwen4exp on the GPU: the pass's tokens (its n-gram embeddings), each
+ attention layer's input at bf16 as the GPU made it (n_attn x blk x
+ d_model, for the indexer's keys), the ring of the n-gram conv
+ imported; the indexer's raw and pooled keys imported, the cos and
+ sin of a piece's positions for it (janas_gpu_tok_model ix_rope) */
+ const int32_t *gtok_tokens;
+ float *gtok_att_in;
+ int gtok_ng_in, gtok_ix_in;
+ float *gtok_ix_rope;
/* the aligned copy of its Q6_K weights for the GPU (JANAS_GPU_REPACK),
in the GPU driver's memory, kept for a rebuild */
uint8_t *gtok_repack;
@@ -338,7 +371,7 @@ struct janas_llm_model {
struct janas_out_buf {
float *p;
size_t bytes;
- } out[24];
+ } out[40];
int n_out;
int warm_use; /* preload the most used experts, and count them */
uint32_t *use_count; /* experts used this session, n_layer * n_expert */
@@ -347,6 +380,11 @@ struct janas_llm_model {
load (head16_to_q8_0), else 0; and the bytes before and after */
int head16;
uint64_t head16_before, head16_after;
+ /* qwen4exp's mixes of the streams rewritten at load (JANAS_HC_MIX:
+ "q8"; empty: as in the file), their bytes before and
+ after (model_hc_quant.c) */
+ char hc_quant[8];
+ uint64_t hc_quant_before, hc_quant_after;
/* the caller's CPUs before the load pinned it (a cpu_set_t), given
back when the model is freed */
unsigned char caller_cpus[128];
@@ -373,6 +411,46 @@ struct janas_llm_model {
uint64_t warm_experts; /* experts preloaded at open from the profile */
double warm_seconds;
+ /* qwen4exp (model_hc.c): hc_n streams of the residual (0: the usual
+ one) and the rank of their gates; a block's streams (blk x hc_n x
+ d_model), the same normalized and rounded to bf16, the gates' inner
+ activations (blk x hc_rank) and outputs (blk x hc_n x d_model), the
+ inject weights (blk x hc_n); the final mix's tensors */
+ uint32_t hc_n, hc_rank;
+ float *hc_res, *hc_xn, *hc_lo, *hc_gate, *hc_inj;
+ const struct janas_jns_tensor *hco_norm, *hco_down, *hco_up;
+ /* the inputs of the mixes' quantized matrices (an MTP block's), in Q8_K
+ (blk x hc_n x d_model / 256); its MTP block's final mix */
+ struct janas_block_q8k *hc_q;
+ const struct janas_jns_tensor *mtp_hco_norm, *mtp_hco_down, *mtp_hco_up;
+ /* its n-gram embeddings: the layer (n_layer: none), the longest n-gram,
+ heads per length and a head's numbers (a row of the table), the
+ conv's kernel (its dilation is the longest n-gram), the hash's
+ multipliers, the heads' rows in the table, the token that cuts a
+ context. Tokens and the conv's inputs kept by position in rings of
+ ng_cap, ng_hi one past the last written. A block's embeddings, keys
+ (later the queries' norms), values, gated values and conv inputs, a
+ row as read; the table read a row at a time from the file */
+ uint32_t ng_layer, ng_len, ng_per, ng_dim, ng_kernel, ng_cap, ng_hi;
+ uint64_t ng_mult[JANAS_NG_MAX_LEN];
+ uint32_t ng_off[JANAS_NG_MAX_HEADS], ng_vocab[JANAS_NG_MAX_HEADS];
+ int32_t ng_eos;
+ const struct janas_jns_tensor *ng_tab;
+ int32_t *ng_tok;
+ float *ng_hist, *ng_emb, *ng_kq, *ng_val, *ng_gv;
+ uint8_t *ng_raw;
+ struct janas_block_q8k *ng_q;
+ /* its sparse attention (model_qsa.c; ix_heads 0: none): the indexer's
+ heads and their size, the positions a query keeps in whole blocks
+ and the block; the raw keys of every position (f16, n_attn x n_ctx x
+ ix_dim), the pooled keys of every block, a block's queries and keys,
+ its input at bf16, a query's blocks' scores, a selection, the
+ positions chosen */
+ uint32_t ix_heads, ix_dim, ix_top, ix_ratio;
+ uint16_t *ix_raw;
+ float *ix_pool, *ix_qs, *ix_kb, *ix_xb, *ix_score;
+ uint32_t *ix_sel, *ix_pos;
+
double phase[JANAS_PH_COUNT];
};
@@ -508,6 +586,9 @@ uint64_t janas_m_import_next(uint64_t at, uint64_t bytes, uint64_t unit,
int huge, uint64_t *from);
int janas_m_gpu_import(struct janas_llm_model *m, const uint8_t *p,
uint64_t bytes, uint64_t unit);
+/* A buffer the products read or write, in whole pages, listed for the GPU
+ to be given (model_load.c); janas_aligned_free releases it. */
+float *janas_m_out_alloc(struct janas_llm_model *m, size_t bytes);
void janas_m_arena_to_gpu(struct janas_llm_model *m);
/* A block on the GPU whole (model_gpu.c): 0; -1, the CPU to run it; -2,
the GPU failed after replaying on the base in place, the state lost. */
@@ -555,6 +636,15 @@ int janas_m_npu_ffn_begin(struct janas_llm_model *m, const uint8_t *const w[3],
float *dout, struct janas_m_npu_ffn *job);
/* Waits for it and stores its rows; -1 if the NPU failed (the run is then
the caller's to compute). */
+/*
+ * y (nv rows of rows) = scale w x (nv rows of cols floats) for bf16 weights
+ * in the resident region, on the NPU (its candidate has it, nv reaches
+ * JANAS_NPU_MIN_N): the weights in FP16, the result close to the CPU's.
+ * 0, or -1 with nothing written (the caller computes it).
+ */
+int janas_m_npu_dense(struct janas_llm_model *m, const uint16_t *w,
+ const float *x, float *y, uint32_t rows, uint32_t cols,
+ uint32_t nv, float scale);
int janas_m_npu_ffn_finish(struct janas_llm_model *m,
struct janas_m_npu_ffn *job);
/*
@@ -614,10 +704,84 @@ int janas_m_assist_draft(struct janas_llm_model *m, int32_t token,
float *conf);
int janas_m_ple_inputs(struct janas_llm_model *m, const int32_t *tokens,
uint32_t n);
+/* y (n rows of rows) = scale w x (n rows of cols): w bf16, rows of cols,
+ x already at bf16 (janas_m_round_bf16), as llama.cpp meets a bf16 matrix;
+ each row and token one thread's, the same bits whatever the threads
+ (model_ple.c) */
+void janas_m_bf16_product(struct janas_llm_model *m, const uint16_t *w,
+ const float *x, float *y, uint32_t rows,
+ uint32_t cols, uint32_t n, float scale);
+/* the same for Q8_0 weights, x kept as it is (janas_q8w_dot): the CPU's,
+ the same bits whatever the threads */
+void janas_m_q8w_product(struct janas_llm_model *m, const void *w,
+ const float *x, float *y, uint32_t rows, uint32_t cols,
+ uint32_t n, float scale);
+/* a tensor's reserved field, in memory: its products on float inputs
+ (janas_m_q8w_product), a mix rewritten at load from BF16 */
+#define JANAS_TENSOR_FLOAT_IN 1
+float janas_m_round_bf16(float f);
void janas_m_ple_layer(struct janas_llm_model *m, const struct layer *ly,
uint32_t l, uint32_t n);
int janas_m_exp_tracing(void);
void janas_m_exp_report(void);
+/* qwen4exp (model_hc.c): its tensors and buffers at load (0, or -1 with
+ err), freed with the model; a block's layers on the embeddings in x,
+ the final mix of the streams into xn and xq (tokens first .. n - 1) */
+int janas_m_hc_load(struct janas_llm_model *m, char *err, size_t err_len);
+/* layer l's tensors from file j: the main model's, or its MTP block's */
+int janas_m_hc_layer_load(struct janas_llm_model *m, const struct janas_jns *j,
+ uint32_t l, struct layer *ly, char *err,
+ size_t err_len);
+int janas_m_hc_alloc(struct janas_llm_model *m, char *err, size_t err_len);
+/* the mixes' BF16 matrices quantized in place as JANAS_HC_MIX asks, once
+ the resident region is read (model_hc_quant.c): 0, or -1 with err */
+int janas_m_hc_requant(struct janas_llm_model *m, char *err, size_t err_len);
+void janas_m_hc_free(struct janas_llm_model *m);
+int janas_m_hc_layers(struct janas_llm_model *m, const int32_t *tokens,
+ uint32_t n, uint32_t pos0);
+void janas_m_hc_head(struct janas_llm_model *m, uint32_t first, uint32_t n);
+/* The MTP block of qwen4exp (janas_llm_mtp_forward): the tokens'
+ embeddings in x and the rope set, hidden the trunk's residual after its
+ last layer (hc_n x d_model a row, janas_llm_model_hidden); the block's
+ own residual into mtp_hid, its final mix of rows first .. n - 1 into xn
+ and xq. 0, or -1. */
+int janas_m_hc_mtp(struct janas_llm_model *m, const float *hidden, uint32_t n,
+ uint32_t pos0, uint32_t first);
+/* the n-gram layer's start for a block (the GPU's, model_gpu.c): its
+ history checked, its tokens into the ring, their rows of the table into
+ ng_emb (n x d_model); the caller sets ng_hi to pos0 + n once the block
+ is done. 0, or -1 (the history is not there). */
+int janas_m_hc_ng_begin(struct janas_llm_model *m, const int32_t *tokens,
+ uint32_t n, uint32_t pos0);
+/* The cos and sin (n_rot floats a row: the cos, then the sin) of
+ positions pos0 .. pos0 + n - 1, then of the first position of each block
+ from pos0 / ix_ratio on (n / ix_ratio + 2 rows), as the indexer turns its
+ queries and its blocks' keys: janas_gpu_tok_model's ix_rope */
+void janas_m_qsa_rope(const struct janas_llm_model *m, float *out,
+ uint32_t pos0, uint32_t n);
+/* the indexer's keys of n positions from pos0 of attention layer ly, from
+ its input at bf16 (xb, n x d_model) - the CPU's own, after a block
+ on the GPU (model_gpu.c; model_qsa.c) */
+void janas_m_qsa_keys(struct janas_llm_model *m, const struct layer *ly,
+ const float *xb, uint32_t n, uint32_t pos0);
+/* the n-gram rings when the context moves along (janas_llm_model_shift):
+ 0, or -1 if the history to move is no longer in them */
+int janas_m_hc_shift(struct janas_llm_model *m, uint32_t keep, uint32_t drop,
+ uint32_t n_kv);
+/* qwen4exp's sparse attention (model_qsa.c): its tensors and buffers once
+ the layers are loaded (0, or -1 with err), freed; after a block's dense
+ attention, the rows of the queries that see more than it keeps, again
+ over the positions chosen */
+int janas_m_qsa_load(struct janas_llm_model *m, char *err, size_t err_len);
+void janas_m_qsa_free(struct janas_llm_model *m);
+void janas_m_qsa(struct janas_llm_model *m, const struct layer *ly, uint32_t n,
+ uint32_t pos0);
+/* The pooled keys of the blocks before n, from the raw keys put back (a
+ state loaded) */
+void janas_m_qsa_restore(struct janas_llm_model *m, uint32_t n);
+/* the raw keys and the blocks when the context moves along */
+void janas_m_qsa_shift(struct janas_llm_model *m, uint32_t keep, uint32_t drop,
+ uint32_t n_kv);
/* which experts a moe_block call runs */
enum { MOE_ALL, MOE_SHARED, MOE_ROUTED };
/* The router of layer l applied ahead to the state x (for the predictor
@@ -647,7 +811,12 @@ int janas_m_moe_block(struct janas_llm_model *m, const struct layer *ly,
static inline struct janas_gpu *janas_m_gpu(const struct janas_llm_model *m,
int arena)
{
- if (!m->gpu || m->gpu_off || (arena && m->arena_stale))
+ /* JANAS_GPU_REPEATABLE=1: the experts' products never on the GPU - the
+ memory it can read of the arena changes as the cache fills, and a
+ product it could read went to the fast prompt's tiles, one it could
+ not to the CPU (gpu.h) */
+ if (!m->gpu || m->gpu_off || (arena && m->arena_stale) ||
+ (arena && janas_gpu_repeatable()))
return NULL;
if (m->gpu_use ||
(m->tuner.allow_gpu && janas_gpu_fast_prompt() && !m->prompts_off_gpu))
diff --git a/src/llm/model_load.c b/src/llm/model_load.c
index 9ab1d89..1938e2c 100644
--- a/src/llm/model_load.c
+++ b/src/llm/model_load.c
@@ -190,23 +190,26 @@ static int rows_f32(struct janas_llm_model *m, const struct janas_jns *j,
* Where the head is the token embeddings (Qwen3-0.6B and 4B, Qwen3.5-0.8B
* and 2B) the table is requantized in place, so the embeddings read at four
* bits too, as in the larger models' files: Qwen3.5-2B +9%, the most
- * likely token 352 where 376 (8 Oct 2026).
+ * likely token 352 where 376 (8 Oct 2026). A head at five bits (Q5_K,
+ * Qwen3.8-Flash-Next's from ISTA, 437 MB read at every token) is
+ * requantized the same way.
*/
struct rq_job {
const uint8_t *src;
uint8_t *dst;
size_t first, count; /* blocks of JANAS_QK weights */
+ int type; /* the source's: Q6_K or Q5_K */
};
static void *rq_worker(void *arg)
{
struct rq_job *r = arg;
float buf[JANAS_QK * 16];
- size_t sb = janas_qtype_block_size(JANAS_Q6_K),
+ size_t sb = janas_qtype_block_size(r->type),
db = janas_qtype_block_size(JANAS_Q4_K);
for (size_t b = r->first; b < r->first + r->count; b += 16) {
size_t nb = r->first + r->count - b < 16 ? r->first + r->count - b : 16;
- janas_dequantize(JANAS_Q6_K, r->src + b * sb, buf, nb * JANAS_QK);
+ janas_dequantize(r->type, r->src + b * sb, buf, nb * JANAS_QK);
janas_q4k_quantize_fast(
buf, (struct janas_block_q4k *)(r->dst + b * db), nb * JANAS_QK);
}
@@ -300,14 +303,15 @@ static void requant_head(struct janas_llm_model *m)
if (!t)
t = (struct janas_jns_tensor *)janas_jns_tensor(&m->j,
"token_embd.weight");
- if (!t || t->type != JANAS_Q6_K || t->dims[0] % JANAS_QK ||
- t->offset < m->j.h.resident_offset ||
+ if (!t || (t->type != JANAS_Q6_K && t->type != JANAS_Q5_K) ||
+ t->dims[0] % JANAS_QK || t->offset < m->j.h.resident_offset ||
t->offset + t->bytes > m->j.h.resident_offset + m->j.h.resident_bytes)
return;
enum { NT = 16 };
long cpus = sysconf(_SC_NPROCESSORS_ONLN);
int nt = cpus < 1 ? 1 : cpus > NT ? NT : (int)cpus;
- size_t nb = t->bytes / janas_qtype_block_size(JANAS_Q6_K);
+ int type = (int)t->type;
+ size_t nb = t->bytes / janas_qtype_block_size(type);
uint8_t *src = (uint8_t *)data(m, t);
uint8_t *dst = malloc(nb * janas_qtype_block_size(JANAS_Q4_K));
if (!dst)
@@ -317,8 +321,8 @@ static void requant_head(struct janas_llm_model *m)
int started = 0;
for (int k = 0; k < nt; k++) {
size_t a = nb * k / nt, b = nb * (k + 1) / nt;
- jb[k] =
- (struct rq_job){.src = src, .dst = dst, .first = a, .count = b - a};
+ jb[k] = (struct rq_job){
+ .src = src, .dst = dst, .first = a, .count = b - a, .type = type};
if (pthread_create(&th[k], NULL, rq_worker, &jb[k]) == 0)
started |= 1 << k;
else
@@ -354,7 +358,10 @@ static int load_layer(struct janas_llm_model *m, const struct janas_jns *j,
snprintf(name, sizeof(name), "blk.%u." suffix, l); \
if (!(var = need(j, name, T_F32, d0, 0, err, err_len))) \
return -1;
- NEED(attn_norm, "attn_norm.weight", T_F32, dm, 0)
+ /* qwen4exp: the hyper-connections' mixes in place of the norms */
+ if (!m->a->hc) {
+ NEED(attn_norm, "attn_norm.weight", T_F32, dm, 0)
+ }
if (ly->rec) {
uint32_t s = m->ds;
if (m->a->rec == JANAS_REC_Q35) {
@@ -434,7 +441,10 @@ static int load_layer(struct janas_llm_model *m, const struct janas_jns *j,
if (os && os->type == 0 && os->dims[0] == 1)
ly->out_scale = f32(m, os)[0];
}
- if (m->a->post_attn_norm) {
+ if (m->a->hc) {
+ if (janas_m_hc_layer_load(m, j, l, ly, err, err_len) != 0)
+ return -1;
+ } else if (m->a->post_attn_norm) {
NEED(ffn_norm, "post_attention_norm.weight", T_F32, dm, 0)
} else {
NEED(ffn_norm, "ffn_norm.weight", T_F32, dm, 0)
@@ -594,9 +604,34 @@ static int load_mtp(struct janas_llm_model *m, const char *path, char *err,
if (load_layer(m, &m->mtp_j, 0, &m->mtp_layer, err, err_len) != 0 ||
!(m->eh_proj = need(&m->mtp_j, "mtp.eh_proj.weight", T_QUANT, 2 * dm,
dm, err, err_len)) ||
- !(m->enorm = need(&m->mtp_j, "mtp.enorm.weight", T_F32, dm, 0, err,
- err_len)) ||
- !(m->hnorm = need(&m->mtp_j, "mtp.hnorm.weight", T_F32, dm, 0, err,
+ !(m->enorm =
+ need(&m->mtp_j, "mtp.enorm.weight", T_F32, dm, 0, err, err_len)))
+ return -1;
+ if (m->a->hc) {
+ /* qwen4exp's block (unsloth's GGUF heads, hf2jns_mtp): a norm for
+ each of the trunk's streams, and a final mix of its own in place
+ of a norm */
+ uint32_t hd = m->hc_n * dm;
+ if (!(m->hnorm = need(&m->mtp_j, "mtp.hnorm.weight", T_F32, hd, 0, err,
+ err_len)) ||
+ !(m->mtp_hco_norm = need(&m->mtp_j, "mtp.hc_head_norm.weight",
+ T_F32, hd, 0, err, err_len)))
+ return -1;
+ m->mtp_hco_down =
+ janas_jns_tensor(&m->mtp_j, "mtp.hc_head_down.weight");
+ m->mtp_hco_up = janas_jns_tensor(&m->mtp_j, "mtp.hc_head_up.weight");
+ if (!m->mtp_hco_down || !m->mtp_hco_up ||
+ m->mtp_hco_down->dims[0] != hd ||
+ m->mtp_hco_down->dims[1] != m->hc_rank ||
+ m->mtp_hco_up->dims[0] != m->hc_rank ||
+ m->mtp_hco_up->dims[1] != hd) {
+ snprintf(err, err_len, "%s: its final mix missing or unexpected",
+ path);
+ return -1;
+ }
+ return 0;
+ }
+ if (!(m->hnorm = need(&m->mtp_j, "mtp.hnorm.weight", T_F32, dm, 0, err,
err_len)) ||
!(m->mtp_norm =
need(&m->mtp_j, "mtp.norm.weight", T_F32, dm, 0, err, err_len)))
@@ -686,6 +721,11 @@ static float *out_alloc(struct janas_llm_model *m, size_t bytes)
return p;
}
+float *janas_m_out_alloc(struct janas_llm_model *m, size_t bytes)
+{
+ return out_alloc(m, bytes);
+}
+
struct janas_llm_model *janas_llm_model_load(const char *path,
const struct janas_llm_options *o,
char *err, size_t err_len)
@@ -717,7 +757,8 @@ struct janas_llm_model *janas_llm_model_load(const char *path,
meta_f32(m, "rope.freq_base", &m->rope_base) || m->k > MAX_K ||
m->n_head_kv == 0 || m->n_head % m->n_head_kv || m->head_dim % 8 ||
m->d_model % JANAS_QK || (m->n_head * m->head_dim) % JANAS_QK ||
- m->d_ff % JANAS_QK || (m->a->rec && load_ssm_params(m) != 0)) {
+ (m->d_ff % JANAS_QK && !m->a->hc) ||
+ (m->a->rec && load_ssm_params(m) != 0)) {
snprintf(err, err_len, "missing or inconsistent hyperparameters");
goto fail;
}
@@ -730,6 +771,8 @@ struct janas_llm_model *janas_llm_model_load(const char *path,
snprintf(err, err_len, "inconsistent rotary or layer pattern");
goto fail;
}
+ if (m->a->hc && janas_m_hc_load(m, err, err_len) != 0)
+ goto fail;
m->hd_max = m->head_dim > m->head_swa ? m->head_dim : m->head_swa;
m->kvd_max = m->n_head_kv * m->head_dim;
if (m->n_kv_swa * m->head_swa > m->kvd_max)
@@ -742,7 +785,13 @@ struct janas_llm_model *janas_llm_model_load(const char *path,
const struct janas_jns_matrix *dn = &m->j.layers[l].m[JANAS_JNS_DOWN];
/* by neuron (tools/jns_neurons.c), its rows are the gate's */
int by_neuron = dn->type == JANAS_Q6_K_PN;
- if ((r != m->d_ff && m->n_expert > 1) || r % JANAS_QK ||
+ /* a width that is no multiple of 256 (qwen4exp's 640): the down
+ matrix's rows end inside a group, which the types in blocks of
+ 32 or 64 can do (janas_dot_rows_n) */
+ int narrow = r % JANAS_QK != 0;
+ if ((r != m->d_ff && m->n_expert > 1) ||
+ (narrow &&
+ (!m->a->hc || janas_qtype_row_bytes((int)dn->type, r) == 0)) ||
(by_neuron ? dn->rows != r || dn->cols != m->d_model
: dn->rows != m->d_model)) {
snprintf(err, err_len, "layer %u: expert shapes differ", l);
@@ -812,7 +861,11 @@ struct janas_llm_model *janas_llm_model_load(const char *path,
goto fail;
}
/* all of it but the per-layer embeddings' table, read a row at a time
- where it is (model_ple.c): its pages, never touched, take no memory */
+ where it is (model_ple.c, model_hc.c): its pages, never touched, take
+ no memory - unless the machine holds it (ple_ram, JANAS_PLE_RAM):
+ then it is read with the rest, and its rows come from memory */
+ const char *pre = getenv("JANAS_PLE_RAM");
+ m->ple_ram = m->ple_tok && (pre ? atoi(pre) > 0 : o->ple_ram);
uint64_t skip_a = m->j.h.resident_bytes, skip_b = skip_a;
if (m->ple_tok && m->ple_tok->offset >= m->j.h.resident_offset) {
skip_a = (m->ple_tok->offset - m->j.h.resident_offset) & ~4095ull;
@@ -821,13 +874,15 @@ struct janas_llm_model *janas_llm_model_load(const char *path,
~4095ull;
if (skip_b < skip_a)
skip_b = skip_a;
+ m->res_hole[0] = skip_a, m->res_hole[1] = skip_b;
}
for (uint64_t done = 0; done < m->j.h.resident_bytes;) {
- if (done == skip_a && skip_b > skip_a) {
+ if (done == skip_a && skip_b > skip_a && !m->ple_ram) {
done = skip_b;
continue;
}
- uint64_t end = done < skip_a ? skip_a : m->j.h.resident_bytes;
+ uint64_t end =
+ done < skip_a && !m->ple_ram ? skip_a : m->j.h.resident_bytes;
ssize_t r = pread(m->j.fd, m->resident + done, end - done,
(off_t)(m->j.h.resident_offset + done));
if (r <= 0) {
@@ -841,13 +896,15 @@ struct janas_llm_model *janas_llm_model_load(const char *path,
const char *hbe = getenv("JANAS_HEAD_BITS");
if ((hbe ? atoi(hbe) : o->head_bits) == 4 && !m->resident_gpu)
requant_head(m);
+ if (m->a->hc && janas_m_hc_requant(m, err, err_len) != 0)
+ goto fail;
uint32_t dm = m->d_model, qd = m->n_head * m->hd_max, kvd = m->kvd_max,
ff = m->d_ff;
if (!(m->tok_embd =
need(&m->j, "token_embd.weight", T_QUANT, dm, 0, err, err_len)) ||
- !(m->out_norm =
- need(&m->j, "output_norm.weight", T_F32, dm, 0, err, err_len)))
+ (!m->a->hc && !(m->out_norm = need(&m->j, "output_norm.weight", T_F32,
+ dm, 0, err, err_len))))
goto fail;
/* tied embeddings (the dense Qwen3): the table is also the output head */
m->output = janas_jns_tensor(&m->j, "output.weight")
@@ -985,6 +1042,9 @@ struct janas_llm_model *janas_llm_model_load(const char *path,
for (uint32_t l = 0; l < m->n_layer; l++)
if (m->layers[l].sh_w[0] && m->layers[l].sh_mx[0].rows > m->ff_buf)
m->ff_buf = m->layers[l].sh_mx[0].rows;
+ /* in whole groups of 256: a narrower width's activations are quantized
+ with zeros after them (model_ffn.c) */
+ m->ff_buf = (m->ff_buf + JANAS_QK - 1) / JANAS_QK * JANAS_QK;
ff = m->ff_buf;
size_t B = m->blk, P = B * (m->k + 1);
size_t inner = m->d_inner > qd ? m->d_inner : qd;
@@ -1138,6 +1198,9 @@ struct janas_llm_model *janas_llm_model_load(const char *path,
goto fail;
}
}
+ if (m->a->hc && (janas_m_hc_alloc(m, err, err_len) != 0 ||
+ janas_m_qsa_load(m, err, err_len) != 0))
+ goto fail;
if (m->a->rec) {
/* the GPU reads and writes them too (gpu_token.c): imported */
size_t sb = 2 * (size_t)m->n_rec * m->n_vh * m->ds * m->ds *
@@ -1294,17 +1357,35 @@ struct janas_llm_model *janas_llm_model_load(const char *path,
}
}
if (m->mtp) {
- m->hid = malloc(B * dm * sizeof(float));
- m->mtp_hid = malloc(B * dm * sizeof(float));
+ /* qwen4exp: the residual's streams a row, and the block's input a
+ stream at a time (model_hc.c janas_m_hc_mtp) */
+ size_t hw = m->hc_n ? m->hc_n : 1;
+ m->hid = malloc(B * hw * dm * sizeof(float));
+ m->mtp_hid = malloc(B * hw * dm * sizeof(float));
m->cat = malloc(B * 2 * dm * sizeof(float));
- m->catq = malloc(B * 2 * dm / JANAS_QK * sizeof(*m->catq));
- /* its own experts' cache, or the main one (block in the file) */
+ m->catq = malloc(B * hw * 2 * dm / JANAS_QK * sizeof(*m->catq));
+ /* its own experts' cache, or the main one (block in the file). Its
+ own holds every expert unless the caller says otherwise - the
+ API counts the whole file in the memory it plans (api.c
+ mem_fixed) - and never fewer than a block of tokens asks for at
+ once (model_ffn.c): 256 MiB, the old default, held 74 of
+ Qwen3.8-Flash-Next's experts of 3.6 MB, and a block asks for up
+ to JANAS_EXPERT_CHUNK */
+ uint64_t mtp_budget = 0;
+ if (m->mtp_own_cache) {
+ uint32_t ne = (uint32_t)m->mtp_j.h.n_expert;
+ uint64_t all =
+ janas_expert_cache_room(&m->mtp_j, 1, 1, m->exp_level);
+ uint64_t least =
+ all / ne * (ne < JANAS_EXPERT_CHUNK ? ne : JANAS_EXPERT_CHUNK);
+ mtp_budget = o->mtp_cache_bytes ? o->mtp_cache_bytes : all;
+ if (mtp_budget < least)
+ mtp_budget = least;
+ }
m->mtp_cache =
m->mtp_own_cache
- ? janas_expert_cache_create(
- &m->mtp_j,
- o->mtp_cache_bytes ? o->mtp_cache_bytes : 256ull << 20,
- m->exp_level, m->io, err, err_len)
+ ? janas_expert_cache_create(&m->mtp_j, mtp_budget, m->exp_level,
+ m->io, err, err_len)
: m->cache;
if (!m->hid || !m->mtp_hid || !m->cat || !m->catq || !m->mtp_cache ||
init_draft_head(m, dvocab) != 0)
@@ -1317,7 +1398,13 @@ struct janas_llm_model *janas_llm_model_load(const char *path,
flushed out of the CPU's caches before the GPU reads it */
if (m->resident_gpu)
janas_gpu_flush(m->gpu, m->resident, m->j.h.resident_bytes);
- else if (janas_m_huge_pages(m->resident, m->j.h.resident_bytes),
+ else if (janas_m_huge_pages(m->resident, m->res_hole[1] > 0
+ ? m->res_hole[0]
+ : m->j.h.resident_bytes),
+ janas_m_huge_pages(m->resident + m->res_hole[1],
+ m->res_hole[1] > 0
+ ? m->j.h.resident_bytes - m->res_hole[1]
+ : 0),
janas_m_gpu_import(m, m->resident, m->j.h.resident_bytes, 0) !=
0)
m->gpu_off = 1;
@@ -1380,6 +1467,8 @@ void janas_llm_model_free(struct janas_llm_model *m)
free(m->gblk_layers);
janas_gpu_destroy(m->gpu); /* before the memory it imported; frees
the resident region it allocated */
+ janas_aligned_free(m->gtok_att_in);
+ free(m->gtok_ix_rope);
janas_expert_cache_destroy(m->cache);
if (m->mtp_own_cache)
janas_expert_cache_destroy(m->mtp_cache);
@@ -1390,6 +1479,8 @@ void janas_llm_model_free(struct janas_llm_model *m)
janas_pool_destroy(m->io);
if (!m->resident_gpu)
janas_aligned_free(m->resident);
+ janas_m_hc_free(m);
+ janas_m_qsa_free(m);
if (m->layers)
for (uint32_t l = 0; l < m->n_layer; l++) {
free(m->layers[l].sh_gate);
@@ -1674,9 +1765,19 @@ int janas_m_gpu_import(struct janas_llm_model *m, const uint8_t *p,
int huge =
((uintptr_t)p & (JANAS_HUGE_PAGE - 1)) == 0 &&
!(getenv("JANAS_GPU_HUGE") && atoi(getenv("JANAS_GPU_HUGE")) == 0);
+ /* the resident region's hole left out (a region after it may start
+ up to 2 MB into it, its huge page's boundary) */
+ uint64_t ha = bytes, hb = bytes;
+ if (p == m->resident && m->res_hole[1] > m->res_hole[0])
+ ha = m->res_hole[0], hb = m->res_hole[1];
for (uint64_t at = 0; at < bytes;) {
- uint64_t from, end = janas_m_import_next(at, bytes, unit, huge, &from);
- if (!unit && end < bytes)
+ if (at == ha) {
+ at = hb;
+ continue;
+ }
+ uint64_t lim = at < ha ? ha : bytes;
+ uint64_t from, end = janas_m_import_next(at, lim, unit, huge, &from);
+ if (!unit && end < lim)
end = resident_cut(m, at, end - at);
/* whole pages, the last one's length rounded up (the allocations
are) */
diff --git a/src/llm/model_npu.c b/src/llm/model_npu.c
index ad17564..8a090ad 100644
--- a/src/llm/model_npu.c
+++ b/src/llm/model_npu.c
@@ -36,6 +36,7 @@
#include "gpu.h"
#include "model_impl.h"
#include "quant.h"
+#include "vmath.h"
/* the inputs of a layer's products, for INT8 activations: the attention's
output (wo), the FFN's norm (gate and up), the FFN's intermediate
@@ -330,7 +331,8 @@ void janas_m_npu_close(struct janas_llm_model *m)
/* the types janas_dequantize unpacks (it aborts on any other), and the
down matrix in planes: every quantization of the catalog's files - Q3_K
- of the 30B in three bits, Q5_1 and Q8_0 of Gemma 4 26B-A4B among them */
+ of the 30B in three bits, Q5_1 and Q8_0 of Gemma 4 26B-A4B among them -
+ and Qwen3.8-Flash-Next's (Q2_0, Q5_0, the IQ1, IQ2 and IQ3 kinds, Q2_K) */
static int dequant_type(int type)
{
switch (type) {
@@ -346,12 +348,26 @@ static int dequant_type(int type)
case JANAS_IQ4_XS:
case JANAS_IQ3_S:
case JANAS_MXFP4:
+ case JANAS_Q5_0:
+ case JANAS_Q2_0:
+ case JANAS_Q2_K:
+ case JANAS_IQ3_XXS:
+ case JANAS_IQ2_XXS:
+ case JANAS_IQ2_XS:
+ case JANAS_IQ2_S:
+ case JANAS_IQ1_M:
+ case JANAS_IQ1_S:
return 1;
default:
return 0;
}
}
+int janas_llm_npu_type(int type)
+{
+ return dequant_type(type);
+}
+
static int npu_type(int type)
{
return dequant_type(type) || type == JANAS_Q6_K_P;
@@ -541,7 +557,11 @@ static void refused(struct janas_m_npu *n, const struct janas_matvec_task *t,
static int eligible(const struct janas_llm_model *m, struct janas_m_npu *n,
const struct janas_matvec_task *t)
{
- size_t nb = t->cols / JANAS_QK;
+ /* a row may end inside a group of 256 (qwen4exp's 640): its
+ activations' last group whole. Outputs further apart than their rows
+ (a recurrent layer's q|k|v and z side by side in one buffer) are
+ copied there: until 22 Oct 2026 they stayed on the CPU */
+ size_t nb = (t->cols + JANAS_QK - 1) / JANAS_QK;
/* the output head stays on the CPU: a chat's prompt asks it for one
token, and over many (an evaluation's every logit) its INT8 copy
would add an error no chat has */
@@ -550,9 +570,12 @@ static int eligible(const struct janas_llm_model *m, struct janas_m_npu *n,
if (!t->w || (int)t->n_vec < n->min_n || (int)t->n_vec > n->block) {
return refused(n, t, 0), n->no_n++, 0;
}
- if (!npu_type(t->type) || t->cols % JANAS_QK ||
+ if (!npu_type(t->type) ||
+ (t->cols % JANAS_QK &&
+ (t->type == JANAS_Q6_K_P ||
+ janas_qtype_row_bytes(t->type, t->cols) == 0)) ||
(t->x_stride && t->x_stride != nb) ||
- (t->y_stride && t->y_stride != t->rows))
+ (t->y_stride && t->y_stride < t->rows))
return refused(n, t, 1), n->no_type++, 0;
int st = stable(m, n, t->w);
if (t->type == JANAS_Q6_K_P && st &&
@@ -579,13 +602,17 @@ static int expand(struct janas_m_npu *n, const struct janas_matvec_task *t)
return 0;
if (n->xb[i].src)
return -1;
- size_t nb = t->cols / JANAS_QK;
+ /* a row ending inside a group: that group's first values only */
+ size_t nb = (t->cols + JANAS_QK - 1) / JANAS_QK;
float *x = n->xb[i].x;
for (size_t v = 0; v < t->n_vec; v++)
for (size_t b = 0; b < nb; b++) {
const struct janas_block_q8k *q = t->x + v * nb + b;
float *o = x + v * t->cols + b * JANAS_QK;
- for (int k = 0; k < JANAS_QK; k++)
+ size_t e = t->cols - b * JANAS_QK < JANAS_QK
+ ? t->cols - b * JANAS_QK
+ : JANAS_QK;
+ for (size_t k = 0; k < e; k++)
o[k] = q->d * (float)q->qs[k];
}
n->xb[i].src = t->x, n->xb[i].n_vec = t->n_vec;
@@ -697,6 +724,96 @@ void janas_m_products(struct janas_llm_model *m, struct janas_gpu *g,
npu_loud(m, spin);
}
+/* ---- bf16 weights on float inputs (qwen4exp's mixes, model_hc.c) ---- */
+
+/* the matrix's entry, compiled on first use with its weights in FP16 -
+ bf16's values exactly, save those below FP16's normal range - keyed by
+ w as the products'; NULL if it stays on the CPU */
+static struct npu_entry *entry16(struct janas_m_npu *n, const uint16_t *w,
+ size_t rows, size_t cols)
+{
+ size_t h = ((uintptr_t)w >> 6) % NPU_SLOTS;
+ for (size_t i = 0; i < NPU_SLOTS; i++, h = (h + 1) % NPU_SLOTS) {
+ struct npu_entry *e = &n->e[h];
+ if (e->w == w && e->mm)
+ return e;
+ if (e->w == w && e->bad)
+ return NULL;
+ if (e->w && e->w != w)
+ continue;
+ double t_setup = now();
+ e->w = w, e->bad = 1, e->in_arena = 0;
+ e->plane[0] = e->plane[1] = NULL;
+ e->elems = 2 * rows * cols; /* two bytes a weight, against the
+ budget of INT8 copies */
+ if (n->used + e->elems > n->budget)
+ return NULL; /* stays on the CPU for good */
+ prep_begin(n, 0);
+ float *x = xbuf(n, cols);
+ uint16_t *f16 = x ? malloc(2 * rows * cols) : NULL;
+ e->y = f16 ? ybuf(n, rows, 0) : NULL;
+ if (e->y) {
+ for (size_t k = 0; k < rows * cols; k++)
+ f16[k] = janas_fp32_to_fp16(janas_u2f((uint32_t)w[k] << 16));
+ char err[256];
+ e->mm = janas_npu_mm_create(n->u, n->block, (int)rows, (int)cols,
+ JANAS_NPU_F16, f16, NULL, x, e->y, err,
+ sizeof err);
+ if (!e->mm)
+ fprintf(stderr, "janas: NPU: %zu x %zu (bf16) on the CPU: %s\n",
+ rows, cols, err);
+ }
+ free(f16);
+ atomic_fetch_add(&n->prep_done, 1);
+ e->bad = !e->mm;
+ if (e->mm)
+ n->used += e->elems;
+ if (e->bad)
+ e->y = NULL;
+ n->setup_s += now() - t_setup;
+ return e->bad ? NULL : e;
+ }
+ return NULL;
+}
+
+int janas_m_npu_dense(struct janas_llm_model *m, const uint16_t *w,
+ const float *x, float *y, uint32_t rows, uint32_t cols,
+ uint32_t nv, float scale)
+{
+ struct janas_m_npu *n =
+ m->npu_use && m->npu && m->npu->lmode != 1 ? m->npu : NULL;
+ /* not the narrow ones (a mix's inject, 4 rows): a run's fixed cost,
+ some 0.45 ms, is more than the CPU takes */
+ if (!n || (int)nv < n->min_n || (int)nv > n->block || rows < 64 ||
+ stable(m, n, w) != 1)
+ return -1;
+ struct npu_entry *e = entry16(n, w, rows, cols);
+ if (!e)
+ return -1;
+ int i = 0;
+ while (n->xb[i].cols != cols)
+ i++;
+ double te = now();
+ memcpy(n->xb[i].x, x, sizeof(float) * nv * cols);
+ n->xb[i].src = NULL; /* floats, not a Q8_K input expanded */
+ n->t_expand += now() - te;
+ int spin = npu_quiet(m);
+ double tw = now();
+ int r = janas_npu_mm_run(e->mm);
+ double tc = now();
+ n->t_wait += tc - tw;
+ npu_loud(m, spin);
+ if (r != 0) {
+ n->cpu_fallbacks++;
+ return -1;
+ }
+ for (size_t k = 0; k < (size_t)nv * rows; k++)
+ y[k] = e->y[k] * scale;
+ n->t_copy += now() - tc;
+ n->runs++;
+ return 0;
+}
+
/* ---- a feed-forward in one run ---- */
static int ffn_type(int type, int down)
@@ -837,7 +954,10 @@ int janas_m_npu_ffn_begin(struct janas_llm_model *m, const uint8_t *const w[3],
if (!ffn_type((int)g->type, 0) || !ffn_type((int)u->type, 0) ||
!ffn_type((int)d->type, 1) || g->rows != u->rows ||
g->cols != u->cols || d->cols != g->rows || d->rows != g->cols ||
- g->cols % JANAS_QK || g->rows % JANAS_QK) {
+ g->cols % JANAS_QK ||
+ (g->rows % JANAS_QK &&
+ (d->type == JANAS_Q6_K_P ||
+ janas_qtype_row_bytes((int)d->type, d->cols) == 0))) {
n->ffn_refused++;
return -1;
}
@@ -1392,8 +1512,8 @@ static int layers_fit(const struct janas_llm_model *m)
int gemma = a->sandwich && a->swa && a->rec == JANAS_REC_NONE;
if ((a->rec != JANAS_REC_NONE && a->rec != JANAS_REC_Q35) ||
(a->attn_gate && a->rec != JANAS_REC_Q35) || a->shared_expert ||
- ((a->sandwich || a->swa || m->ple_dim) && !gemma) || m->n_expert > 1 ||
- m->janet || m->pred_fn || !m->cache)
+ a->hc || ((a->sandwich || a->swa || m->ple_dim) && !gemma) ||
+ m->n_expert > 1 || m->janet || m->pred_fn || !m->cache)
return 0;
uint32_t qd = 0;
for (uint32_t l = 0; l < m->n_layer; l++) {
diff --git a/src/llm/model_ple.c b/src/llm/model_ple.c
index 51f1336..dfd9d86 100644
--- a/src/llm/model_ple.c
+++ b/src/llm/model_ple.c
@@ -11,7 +11,8 @@
* projected back to the model's width, normalized, added.
*
* The table is read a row at a time from the file, when a token needs it:
- * a few kilobytes a token, never gigabytes in memory.
+ * a few kilobytes a token - or held in memory where the machine has room
+ * for it (janas_llm_options ple_ram).
*/
#define _GNU_SOURCE
#include <math.h>
@@ -20,6 +21,7 @@
#include <string.h>
#include <unistd.h>
+#include "bf16.h"
#include "matvec.h"
#include "vmath.h"
@@ -41,6 +43,27 @@ static float round_bf16(float f)
efficiency cores */
#define PLE_CHUNK 16
+/* the bf16 and Q8_0 products' rows: a share of those left, as matvec.c
+ claims them - 4 to 256, a multiple of 4 for the four rows a single
+ token's kernels take at once - so that a matrix of a few hundred rows
+ (a mix's down, 320) does not end on threads left waiting for the last
+ fixed chunks; r0, or rows when there is nothing left, and the end in
+ *end */
+static uint32_t claim_rows(atomic_uint *next, uint32_t rows, int n_threads,
+ uint32_t *end)
+{
+ uint32_t seen = atomic_load_explicit(next, memory_order_relaxed);
+ if (seen >= rows)
+ return rows;
+ uint32_t c = (rows - seen) / (2u * (uint32_t)n_threads);
+ c = c < 4 ? 4 : c > 256 ? 256 : c & ~3u;
+ uint32_t r0 = atomic_fetch_add_explicit(next, c, memory_order_relaxed);
+ if (r0 >= rows)
+ return rows;
+ *end = r0 + c < rows ? r0 + c : rows;
+ return r0;
+}
+
struct bf16_job {
atomic_uint next;
const uint16_t *w;
@@ -52,55 +75,6 @@ struct bf16_job {
#if defined(__x86_64__)
#include <immintrin.h>
-
-__attribute__((target("avx2,fma"))) static float
-dot_bf16_avx2(const uint16_t *w, const float *x, uint32_t n)
-{
- __m256 acc = _mm256_setzero_ps();
- uint32_t i = 0;
- for (; i + 8 <= n; i += 8) {
- __m128i h = _mm_loadu_si128((const __m128i *)(const void *)(w + i));
- __m256 wf = _mm256_castsi256_ps(
- _mm256_slli_epi32(_mm256_cvtepu16_epi32(h), 16));
- acc = _mm256_fmadd_ps(wf, _mm256_loadu_ps(x + i), acc);
- }
- __m128 s =
- _mm_add_ps(_mm256_castps256_ps128(acc), _mm256_extractf128_ps(acc, 1));
- s = _mm_add_ps(s, _mm_movehl_ps(s, s));
- s = _mm_add_ss(s, _mm_movehdup_ps(s));
- float r = _mm_cvtss_f32(s);
- for (; i < n; i++)
- r += janas_u2f((uint32_t)w[i] << 16) * x[i];
- return r;
-}
-
-/* four tokens against one row, the row converted once: for each token the
- same accumulator and the same order as dot_bf16_avx2, so the same bits */
-__attribute__((target("avx2,fma"))) static void
-dot4_bf16_avx2(const uint16_t *w, const float *const x[4], uint32_t n,
- float out[4])
-{
- __m256 acc[4] = {_mm256_setzero_ps(), _mm256_setzero_ps(),
- _mm256_setzero_ps(), _mm256_setzero_ps()};
- uint32_t i = 0;
- for (; i + 8 <= n; i += 8) {
- __m128i h = _mm_loadu_si128((const __m128i *)(const void *)(w + i));
- __m256 wf = _mm256_castsi256_ps(
- _mm256_slli_epi32(_mm256_cvtepu16_epi32(h), 16));
- for (int t = 0; t < 4; t++)
- acc[t] = _mm256_fmadd_ps(wf, _mm256_loadu_ps(x[t] + i), acc[t]);
- }
- for (int t = 0; t < 4; t++) {
- __m128 s = _mm_add_ps(_mm256_castps256_ps128(acc[t]),
- _mm256_extractf128_ps(acc[t], 1));
- s = _mm_add_ps(s, _mm_movehl_ps(s, s));
- s = _mm_add_ss(s, _mm_movehdup_ps(s));
- float r = _mm_cvtss_f32(s);
- for (uint32_t k = i; k < n; k++)
- r += janas_u2f((uint32_t)w[k] << 16) * x[t][k];
- out[t] = r;
- }
-}
#endif
static int have_avx2(void)
@@ -112,51 +86,144 @@ static int have_avx2(void)
#endif
}
-static float dot_bf16(const uint16_t *w, const float *x, uint32_t n)
+/* rows claimed by the threads, each row read once for all the tokens:
+ four tokens at a time against a row, those left (a reply's one) against
+ four rows at a time, so that the sums' chains overlap */
+static void bf16_worker(void *arg, int tid, int n_threads)
{
-#if defined(__x86_64__)
- if (__builtin_cpu_supports("avx2") && __builtin_cpu_supports("fma"))
- return dot_bf16_avx2(w, x, n);
-#endif
- float r = 0.0f;
- for (uint32_t i = 0; i < n; i++)
- r += janas_u2f((uint32_t)w[i] << 16) * x[i];
- return r;
+ (void)tid;
+ struct bf16_job *b = arg;
+ int v = janas_bf16_lanes();
+ uint32_t n4 = v ? b->n / 4 * 4 : 0;
+ for (;;) {
+ uint32_t r1, r0 = claim_rows(&b->next, b->rows, n_threads, &r1);
+ if (r0 >= b->rows)
+ break;
+ for (uint32_t r = r0; r < r1; r++) {
+ const uint16_t *w = b->w + (size_t)r * b->cols;
+ for (uint32_t j = 0; j < n4; j += 4) {
+ const float *x[4];
+ float o[4];
+ for (int t = 0; t < 4; t++)
+ x[t] = b->x + (size_t)(j + t) * b->cols;
+ janas_bf16_dot4(w, x, b->cols, o);
+ for (int t = 0; t < 4; t++)
+ b->y[(size_t)(j + t) * b->rows + r] = o[t] * b->scale;
+ }
+ }
+ for (uint32_t j = n4; j < b->n; j++) {
+ const float *x = b->x + (size_t)j * b->cols;
+ float *y = b->y + (size_t)j * b->rows;
+ uint32_t r = r0;
+ for (; v && r + 4 <= r1; r += 4) {
+ float o[4];
+ janas_bf16_dot4r(b->w + (size_t)r * b->cols, b->cols, x,
+ b->cols, o);
+ for (int k = 0; k < 4; k++)
+ y[r + k] = o[k] * b->scale;
+ }
+ for (; r < r1; r++)
+ y[r] = janas_bf16_dot(b->w + (size_t)r * b->cols, x, b->cols) *
+ b->scale;
+ }
+ }
}
-/* rows split in equal runs, each row read once for all the tokens */
-static void bf16_worker(void *arg, int tid, int n_threads)
+void janas_m_bf16_product(struct janas_llm_model *m, const uint16_t *w,
+ const float *x, float *y, uint32_t rows,
+ uint32_t cols, uint32_t n, float scale)
+{
+ struct bf16_job b = {.w = w,
+ .x = x,
+ .y = y,
+ .rows = rows,
+ .cols = cols,
+ .n = n,
+ .scale = scale};
+ atomic_init(&b.next, 0);
+ /* a prompt's block on the NPU, where the pass has it, or on the GPU
+ where it can (the fast prompt): summed in another order; else, on a
+ GPU that takes single tokens (a card with memory of its own, or
+ replies asked of it: janas_gpu_min_n 1), the GPU where the pass has
+ it, bit for bit as the CPU's eight lanes - not on an integrated one
+ by default, where it is not measured */
+ struct janas_gpu *g = janas_m_gpu(m, 0);
+ if ((size_t)rows * cols * n < 65536)
+ bf16_worker(&b, 0, 1);
+ else if (janas_m_npu_dense(m, w, x, y, rows, cols, n, scale) != 0 &&
+ janas_gpu_dense_mm(g, 1, w, x, y, rows, cols, n, scale) != 0 &&
+ (!g || !janas_bf16_lanes() || janas_gpu_min_n(g) > 1 ||
+ janas_gpu_bf16_mv(g, w, x, y, rows, cols, n, scale) != 0))
+ janas_pool_run(m->compute, bf16_worker, &b);
+}
+
+/* the same for Q8_0 weights (janas_q8w_dot): rows of cols / 32 blocks;
+ tokens four at a time against a row, those left (a reply's one) against
+ four rows at a time, so that the sums' chains overlap */
+static void q8w_worker(void *arg, int tid, int n_threads)
{
(void)tid;
- (void)n_threads;
struct bf16_job *b = arg;
- int v = have_avx2();
+ size_t rb = (size_t)b->cols / 32 * 34;
+ uint32_t n4 = b->n / 4 * 4;
for (;;) {
- uint32_t r0 = atomic_fetch_add(&b->next, PLE_CHUNK);
+ uint32_t r1, r0 = claim_rows(&b->next, b->rows, n_threads, &r1);
if (r0 >= b->rows)
break;
- uint32_t r1 = r0 + PLE_CHUNK < b->rows ? r0 + PLE_CHUNK : b->rows;
for (uint32_t r = r0; r < r1; r++) {
- const uint16_t *w = b->w + (size_t)r * b->cols;
- uint32_t j = 0;
-#if defined(__x86_64__)
- for (; v && j + 4 <= b->n; j += 4) {
+ const uint8_t *w = (const uint8_t *)b->w + r * rb;
+ for (uint32_t j = 0; j < n4; j += 4) {
const float *x[4];
float o[4];
for (int t = 0; t < 4; t++)
x[t] = b->x + (size_t)(j + t) * b->cols;
- dot4_bf16_avx2(w, x, b->cols, o);
+ janas_q8w_dot4(w, x, b->cols, o);
for (int t = 0; t < 4; t++)
b->y[(size_t)(j + t) * b->rows + r] = o[t] * b->scale;
}
-#endif
- for (; j < b->n; j++)
- b->y[(size_t)j * b->rows + r] =
- dot_bf16(w, b->x + (size_t)j * b->cols, b->cols) * b->scale;
+ }
+ for (uint32_t j = n4; j < b->n; j++) {
+ const float *x = b->x + (size_t)j * b->cols;
+ float *y = b->y + (size_t)j * b->rows;
+ uint32_t r = r0;
+ for (; r + 4 <= r1; r += 4) {
+ float o[4];
+ janas_q8w_dot4r((const uint8_t *)b->w + r * rb, rb, x, b->cols,
+ o);
+ for (int k = 0; k < 4; k++)
+ y[r + k] = o[k] * b->scale;
+ }
+ for (; r < r1; r++)
+ y[r] =
+ janas_q8w_dot((const uint8_t *)b->w + r * rb, x, b->cols) *
+ b->scale;
}
}
}
+void janas_m_q8w_product(struct janas_llm_model *m, const void *w,
+ const float *x, float *y, uint32_t rows, uint32_t cols,
+ uint32_t n, float scale)
+{
+ struct bf16_job b = {.w = w,
+ .x = x,
+ .y = y,
+ .rows = rows,
+ .cols = cols,
+ .n = n,
+ .scale = scale};
+ atomic_init(&b.next, 0);
+ if ((size_t)rows * cols * n < 65536)
+ q8w_worker(&b, 0, 1);
+ else
+ janas_pool_run(m->compute, q8w_worker, &b);
+}
+
+float janas_m_round_bf16(float f)
+{
+ return round_bf16(f);
+}
+
struct f32_job {
atomic_uint next;
const float *w, *x;
@@ -316,14 +383,18 @@ int janas_m_ple_inputs(struct janas_llm_model *m, const int32_t *tokens,
for (uint32_t j = 0; j < n; j++) {
off_t at =
(off_t)(m->ple_tok->offset + (uint64_t)tokens[j] * row_bytes);
- for (size_t done = 0; done < row_bytes;) {
+ const uint8_t *src = raw;
+ if (m->ple_ram) /* the table in memory (janas_llm_options) */
+ src = (const uint8_t *)data(m, m->ple_tok) +
+ (uint64_t)tokens[j] * row_bytes;
+ for (size_t done = 0; !m->ple_ram && done < row_bytes;) {
ssize_t r =
pread(m->j.fd, raw + done, row_bytes - done, at + (off_t)done);
if (r <= 0)
return -1;
done += (size_t)r;
}
- janas_dequantize((int)m->ple_tok->type, raw, m->ple_row, W);
+ janas_dequantize((int)m->ple_tok->type, src, m->ple_row, W);
float *p = m->ple + (size_t)j * W;
for (uint32_t l = 0; l < m->n_layer; l++) {
float *pl = p + (size_t)l * P;
diff --git a/src/llm/model_qsa.c b/src/llm/model_qsa.c
new file mode 100644
index 0000000..344b393
--- /dev/null
+++ b/src/llm/model_qsa.c
@@ -0,0 +1,439 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * model_qsa.c - qwen4exp's sparse attention: past ix_top + ix_ratio - 1
+ * positions a query attends only to the ix_top / ix_ratio blocks of
+ * ix_ratio positions its indexer scores highest, and to the positions of
+ * its own block while that is incomplete.
+ *
+ * The indexer, in each attention layer: a key of ix_dim numbers per position
+ * (index_k_proj of the layer's input), kept raw; a block's ix_ratio keys
+ * pooled into one, their mean normalized (index_k_norm) and turned for the
+ * block's first position; a query per head (index_q_proj, normalized and
+ * turned for its own position); a block's score the sum over the heads of
+ * relu(q_h . key). The tail - positions (q + 1) / ratio * ratio to q, the
+ * query's own block until it is complete - is always attended. With no more
+ * complete blocks than ix_top / ix_ratio every position is, and the
+ * attention is the dense one: only past that is a query's row worked out
+ * again here, over the positions chosen.
+ *
+ * llama.cpp's build_qsa_top_k (src/models/qwen4exp.cpp) takes its top_k +
+ * ratio - 1 positions by score, so where the tail is shorter than ratio - 1
+ * it adds a few positions of one more block, chosen among equal scores by
+ * its sort; the reference it follows, and this file, keep whole blocks.
+ */
+#define _GNU_SOURCE
+#include <math.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "common/aligned.h"
+#include "common/pool.h"
+#include "jmath.h"
+#include "vmath.h"
+
+#include "model_impl.h"
+
+int janas_m_qsa_load(struct janas_llm_model *m, char *err, size_t err_len)
+{
+ char key[128];
+ uint32_t heads = 0, dim = 0, top = 0;
+ snprintf(key, sizeof(key), "%.32s.attention.indexer.head_count",
+ m->j.h.arch);
+ if (janas_jns_meta_u32(&m->j, key, &heads) != 0)
+ return 0; /* no indexer: dense attention throughout */
+ snprintf(key, sizeof(key), "%.32s.attention.indexer.key_length",
+ m->j.h.arch);
+ janas_jns_meta_u32(&m->j, key, &dim);
+ snprintf(key, sizeof(key), "%.32s.attention.indexer.top_k", m->j.h.arch);
+ janas_jns_meta_u32(&m->j, key, &top);
+ /* one ratio for every attention layer that has one: llama.cpp's input
+ set is per ratio, and Flash-Next's are all 4 */
+ uint32_t ratio = 0;
+ snprintf(key, sizeof(key), "%.32s.attention.compress_ratios", m->j.h.arch);
+ for (uint32_t l = 0; l < m->n_layer; l++) {
+ uint64_t r = 0;
+ if (janas_jns_meta_uint_at(&m->j, key, l, &r, NULL) != 0)
+ break;
+ if (m->layers[l].rec || r == 0)
+ continue;
+ if (ratio && r != ratio) {
+ snprintf(err, err_len, "sparse attention: ratios differ");
+ return -1;
+ }
+ ratio = (uint32_t)r;
+ }
+ if (!ratio)
+ return 0;
+ if (heads == 0 || heads > 64 || dim == 0 || dim % 8 || dim > m->head_dim ||
+ top == 0 || top % ratio || ratio > 64 || m->n_rot > dim ||
+ top + ratio > JANAS_QSA_MAX) {
+ snprintf(err, err_len, "sparse attention: inconsistent sizes");
+ return -1;
+ }
+ m->ix_heads = heads;
+ m->ix_dim = dim;
+ m->ix_top = top;
+ m->ix_ratio = ratio;
+ uint32_t dm = m->d_model;
+ char name[64];
+ for (uint32_t l = 0; l < m->n_layer; l++) {
+ struct layer *ly = &m->layers[l];
+ if (ly->rec)
+ continue;
+ static const char *const nm[4] = {"q_proj", "k_proj", "q_norm",
+ "k_norm"};
+ const struct janas_jns_tensor *t[4];
+ for (int i = 0; i < 4; i++) {
+ snprintf(name, sizeof(name), "blk.%u.indexer.%s.weight", l, nm[i]);
+ t[i] = janas_jns_tensor(&m->j, name);
+ uint64_t d0 = i < 2 ? dm : dim, d1 = i == 0 ? heads * dim : dim;
+ int ok = t[i] && t[i]->dims[0] == d0 &&
+ (i < 2 ? t[i]->dims[1] == d1 && t[i]->type == 30
+ : t[i]->n_dims == 1 && t[i]->type == 0);
+ if (!ok) {
+ snprintf(err, err_len, "tensor %s missing or unexpected", name);
+ return -1;
+ }
+ }
+ ly->ix_q = t[0];
+ ly->ix_k = t[1];
+ ly->ix_qn = t[2];
+ ly->ix_kn = t[3];
+ }
+ /* the raw keys of every position, at half precision as llama.cpp keeps
+ them, the pooled keys of every block, a block's queries and keys */
+ size_t B = m->blk, ctx = m->n_ctx, nbk = (ctx + ratio - 1) / ratio;
+ /* on whole pages: the GPU's whole token imports them (model_gpu.c) */
+ m->ix_raw = janas_aligned_alloc(
+ 4096, ((size_t)m->n_attn * ctx * dim * sizeof(uint16_t) + 4095) &
+ ~(size_t)4095);
+ m->ix_pool = janas_aligned_alloc(
+ 4096,
+ ((size_t)m->n_attn * nbk * dim * sizeof(float) + 4095) & ~(size_t)4095);
+ /* the bf16 products' (janas_gpu_dense_mm: a prompt's blocks) */
+ m->ix_qs = janas_m_out_alloc(m, B * heads * dim * sizeof(float));
+ m->ix_kb = janas_m_out_alloc(m, B * dim * sizeof(float));
+ m->ix_xb = janas_m_out_alloc(m, B * dm * sizeof(float));
+ m->ix_score = malloc(nbk * sizeof(float));
+ m->ix_sel = malloc((nbk + 1) * sizeof(uint32_t));
+ m->ix_pos = malloc(((size_t)top + ratio) * sizeof(uint32_t));
+ if (!m->ix_raw || !m->ix_pool || !m->ix_qs || !m->ix_kb || !m->ix_xb ||
+ !m->ix_score || !m->ix_sel || !m->ix_pos) {
+ snprintf(err, err_len, "out of memory for the sparse attention");
+ return -1;
+ }
+ return 0;
+}
+
+void janas_m_qsa_free(struct janas_llm_model *m)
+{
+ janas_aligned_free(m->ix_raw);
+ janas_aligned_free(m->ix_pool);
+ janas_aligned_free(m->ix_qs);
+ janas_aligned_free(m->ix_kb);
+ janas_aligned_free(m->ix_xb);
+ free(m->ix_score);
+ free(m->ix_sel);
+ free(m->ix_pos);
+}
+
+/* NeoX rotation of the first n_rot numbers of v for position p, as the
+ attention turns its heads (model.c rope_table, model_attn.c) */
+static void rope_at(const struct janas_llm_model *m, float *v, uint32_t p)
+{
+ uint32_t half = m->n_rot / 2;
+ for (uint32_t i = 0; i < half; i++) {
+ float s, c,
+ theta = (float)p * janas_powf(m->rope_base,
+ -2.0f * (float)i / (float)m->n_rot);
+ janas_sincosf(theta, &s, &c);
+ float a = v[i], b = v[i + half];
+ v[i] = a * c - b * s;
+ v[i + half] = a * s + b * c;
+ }
+}
+
+void janas_m_qsa_rope(const struct janas_llm_model *m, float *out,
+ uint32_t pos0, uint32_t n)
+{
+ uint32_t half = m->n_rot / 2, r = m->ix_ratio;
+ for (uint32_t j = 0; j < n + n / r + 2; j++) {
+ uint32_t p = j < n ? pos0 + j : (pos0 / r + (j - n)) * r;
+ float *row = out + (size_t)j * m->n_rot;
+ for (uint32_t i = 0; i < half; i++) {
+ float theta =
+ (float)p *
+ janas_powf(m->rope_base, -2.0f * (float)i / (float)m->n_rot);
+ janas_sincosf(theta, &row[half + i], &row[i]);
+ }
+ }
+}
+
+/* block b of slot sl pooled: the mean of its raw keys, normalized, turned
+ for its first position */
+static void pool_block(struct janas_llm_model *m, const struct layer *ly,
+ uint32_t b)
+{
+ uint32_t d = m->ix_dim, r = m->ix_ratio;
+ size_t nbk = (m->n_ctx + r - 1) / r;
+ const uint16_t *raw =
+ m->ix_raw + ((size_t)ly->slot * m->n_ctx + (size_t)b * r) * d;
+ float *out = m->ix_pool + ((size_t)ly->slot * nbk + b) * d;
+ for (uint32_t i = 0; i < d; i++) {
+ float s = 0.0f;
+ for (uint32_t k = 0; k < r; k++)
+ s += janas_fp16_to_fp32(raw[(size_t)k * d + i]);
+ out[i] = s / (float)r;
+ }
+ rms_norm(out, out, f32(m, ly->ix_kn), d, m->eps);
+ rope_at(m, out, b * r);
+}
+
+void janas_m_qsa_restore(struct janas_llm_model *m, uint32_t n)
+{
+ for (uint32_t l = 0; m->ix_heads && l < m->n_layer; l++)
+ if (m->layers[l].ix_q)
+ for (uint32_t b = 0; (b + 1) * m->ix_ratio <= n; b++)
+ pool_block(m, &m->layers[l], b);
+}
+
+/* The context moved along: the raw keys after keep + drop come down drop
+ places (they carry no RoPE), and every block from keep's on is pooled
+ again, at its new place */
+void janas_m_qsa_shift(struct janas_llm_model *m, uint32_t keep, uint32_t drop,
+ uint32_t n_kv)
+{
+ if (!m->ix_heads)
+ return;
+ uint32_t d = m->ix_dim, r = m->ix_ratio, n = n_kv - drop;
+ for (uint32_t l = 0; l < m->n_layer; l++) {
+ const struct layer *ly = &m->layers[l];
+ if (!ly->ix_q)
+ continue;
+ uint16_t *raw = m->ix_raw + (size_t)ly->slot * m->n_ctx * d;
+ memmove(raw + (size_t)keep * d, raw + (size_t)(keep + drop) * d,
+ (size_t)(n - keep) * d * sizeof(uint16_t));
+ for (uint32_t b = keep / r; (b + 1) * r <= n; b++)
+ pool_block(m, ly, b);
+ }
+}
+
+struct score_job {
+ struct janas_llm_model *m;
+ const float *pool, *q; /* the layer's pooled keys, the query's heads */
+ uint32_t n_blocks;
+};
+
+static void score_worker(void *arg, int tid, int n_threads)
+{
+ struct score_job *s = arg;
+ struct janas_llm_model *m = s->m;
+ uint32_t d = m->ix_dim;
+ uint32_t per =
+ (s->n_blocks + (uint32_t)n_threads - 1) / (uint32_t)n_threads;
+ uint32_t a = (uint32_t)tid * per,
+ e = a + per < s->n_blocks ? a + per : s->n_blocks;
+ for (uint32_t b = a; b < e; b++) {
+ const float *k = s->pool + (size_t)b * d;
+ float sum = 0.0f;
+ for (uint32_t h = 0; h < m->ix_heads; h++) {
+ float dot = janas_dot_f32(s->q + (size_t)h * d, k, d);
+ sum += dot > 0.0f ? dot : 0.0f;
+ }
+ m->ix_score[b] = sum;
+ }
+}
+
+/* The k-th largest of v[0 .. n) (k < n), v reordered: Hoare's selection */
+static float kth_largest(float *v, uint32_t n, uint32_t k)
+{
+ uint32_t lo = 0, hi = n - 1;
+ while (lo < hi) {
+ float pivot = v[lo + (hi - lo) / 2];
+ uint32_t i = lo, j = hi;
+ while (i <= j) {
+ while (v[i] > pivot)
+ i++;
+ while (v[j] < pivot)
+ j--;
+ if (i <= j) {
+ float t = v[i];
+ v[i] = v[j];
+ v[j] = t;
+ i++;
+ if (j == 0)
+ break;
+ j--;
+ }
+ }
+ if (k <= j)
+ hi = j;
+ else if (k >= i)
+ lo = i;
+ else
+ break;
+ }
+ return v[k];
+}
+
+/* The keep blocks of highest score among blocks 0 .. n - 1 (n > keep), in
+ their order into sel: those above the keep-th score, then among those
+ equal to it the earliest */
+static void choose(struct janas_llm_model *m, uint32_t n, uint32_t keep)
+{
+ float *tmp = (float *)(void *)m->ix_sel; /* as many floats as blocks */
+ memcpy(tmp, m->ix_score, n * sizeof(float));
+ float th = kth_largest(tmp, n, keep - 1);
+ uint32_t above = 0;
+ for (uint32_t b = 0; b < n; b++)
+ above += m->ix_score[b] > th;
+ uint32_t equal = keep - above, k = 0;
+ for (uint32_t b = 0; b < n && k < keep; b++)
+ if (m->ix_score[b] > th || (m->ix_score[b] == th && equal && equal--))
+ m->ix_pos[k++] = b;
+}
+
+struct head_job {
+ struct janas_llm_model *m;
+ const struct layer *ly;
+ const float *q; /* the token's query heads, after norm and RoPE */
+ float *out; /* its attention row */
+ const uint32_t *pos;
+ uint32_t n_pos;
+};
+
+/* one query head over the chosen positions */
+static void head_worker(void *arg, int tid, int n_threads)
+{
+ struct head_job *t = arg;
+ struct janas_llm_model *m = t->m;
+ const struct layer *ly = t->ly;
+ uint32_t hd = ly->hd, nkv = ly->n_kv, rep = m->n_head / nkv;
+ uint32_t cap = m->kv_cap[ly->slot];
+ float scale = 1.0f / sqrtf((float)hd);
+ float sc[JANAS_QSA_MAX], kf[512];
+ for (uint32_t h = (uint32_t)tid; h < m->n_head; h += (uint32_t)n_threads) {
+ uint32_t g = h / rep;
+ const float *q = t->q + (size_t)h * hd;
+ const float *ks = m->kscale + m->ks_off[ly->slot] + (size_t)g * cap;
+ const float *vs = m->vscale + m->ks_off[ly->slot] + (size_t)g * cap;
+ float mx = -INFINITY;
+ for (uint32_t i = 0; i < t->n_pos; i++) {
+ uint32_t p = t->pos[i];
+ size_t at = ((size_t)g * cap + p) * hd;
+ if (m->kv16) {
+ const int16_t *k =
+ (const int16_t *)(const void *)(m->kcache +
+ m->kv_off[ly->slot]) +
+ at;
+ for (uint32_t e = 0; e < hd; e++)
+ kf[e] = (float)k[e];
+ } else {
+ const int8_t *k = m->kcache + m->kv_off[ly->slot] + at;
+ for (uint32_t e = 0; e < hd; e++)
+ kf[e] = (float)k[e];
+ }
+ sc[i] = janas_dot_f32(q, kf, hd) * ks[p] * scale;
+ if (sc[i] > mx)
+ mx = sc[i];
+ }
+ double sum = 0.0;
+ for (uint32_t i = 0; i < t->n_pos; i++) {
+ sc[i] = janas_expf(sc[i] - mx);
+ sum += sc[i];
+ }
+ float *out = t->out + (size_t)h * hd;
+ memset(out, 0, hd * sizeof(float));
+ for (uint32_t i = 0; i < t->n_pos; i++) {
+ uint32_t p = t->pos[i];
+ size_t at = ((size_t)g * cap + p) * hd;
+ float w = (float)(sc[i] / sum) * vs[p];
+ if (m->kv16) {
+ const int16_t *v =
+ (const int16_t *)(const void *)(m->vcache +
+ m->kv_off[ly->slot]) +
+ at;
+ for (uint32_t e = 0; e < hd; e++)
+ out[e] += w * (float)v[e];
+ } else {
+ const int8_t *v = m->vcache + m->kv_off[ly->slot] + at;
+ for (uint32_t e = 0; e < hd; e++)
+ out[e] += w * (float)v[e];
+ }
+ }
+ }
+}
+
+void janas_m_qsa_keys(struct janas_llm_model *m, const struct layer *ly,
+ const float *xb, uint32_t n, uint32_t pos0)
+{
+ if (!m->ix_heads || !ly->ix_q)
+ return;
+ uint32_t dm = m->d_model, d = m->ix_dim, r = m->ix_ratio;
+ janas_m_bf16_product(m, data(m, ly->ix_k), xb, m->ix_kb, d, dm, n, 1.0f);
+ uint16_t *raw = m->ix_raw + (size_t)ly->slot * m->n_ctx * d;
+ for (uint32_t j = 0; j < n; j++)
+ for (uint32_t i = 0; i < d; i++)
+ raw[(size_t)(pos0 + j) * d + i] =
+ janas_fp32_to_fp16(m->ix_kb[(size_t)j * d + i]);
+ for (uint32_t b = pos0 / r; (b + 1) * r <= pos0 + n; b++)
+ pool_block(m, ly, b);
+}
+
+void janas_m_qsa(struct janas_llm_model *m, const struct layer *ly, uint32_t n,
+ uint32_t pos0)
+{
+ if (!m->ix_heads || !ly->ix_q)
+ return;
+ uint32_t dm = m->d_model, d = m->ix_dim, r = m->ix_ratio, nh = m->ix_heads;
+ size_t nbk = (m->n_ctx + r - 1) / r;
+ /* the indexer's keys and queries from the layer's input, at bf16 */
+ for (size_t i = 0; i < (size_t)n * dm; i++)
+ m->ix_xb[i] = janas_m_round_bf16(m->xn[i]);
+ janas_m_bf16_product(m, data(m, ly->ix_k), m->ix_xb, m->ix_kb, d, dm, n,
+ 1.0f);
+ janas_m_bf16_product(m, data(m, ly->ix_q), m->ix_xb, m->ix_qs, nh * d, dm,
+ n, 1.0f);
+ uint16_t *raw = m->ix_raw + (size_t)ly->slot * m->n_ctx * d;
+ for (uint32_t j = 0; j < n; j++) {
+ for (uint32_t i = 0; i < d; i++)
+ raw[(size_t)(pos0 + j) * d + i] =
+ janas_fp32_to_fp16(m->ix_kb[(size_t)j * d + i]);
+ for (uint32_t h = 0; h < nh; h++) {
+ float *q = m->ix_qs + ((size_t)j * nh + h) * d;
+ rms_norm(q, q, f32(m, ly->ix_qn), d, m->eps);
+ rope_at(m, q, pos0 + j);
+ }
+ }
+ /* the blocks this call completed, or wrote again */
+ for (uint32_t b = pos0 / r; (b + 1) * r <= pos0 + n; b++)
+ pool_block(m, ly, b);
+ const float *pool = m->ix_pool + (size_t)ly->slot * nbk * d;
+ uint32_t keep = m->ix_top / r, qd = m->n_head * ly->hd;
+ for (uint32_t j = 0; j < n; j++) {
+ uint32_t p = pos0 + j, tail = (p + 1) / r * r, blocks = tail / r;
+ if (blocks <= keep)
+ continue; /* every position: the dense attention stands */
+ struct score_job sj = {.m = m,
+ .pool = pool,
+ .q = m->ix_qs + (size_t)j * nh * d,
+ .n_blocks = blocks};
+ janas_pool_run(m->compute, score_worker, &sj);
+ /* the blocks chosen, then their positions, in place from the end */
+ choose(m, blocks, keep);
+ uint32_t np = keep * r;
+ for (uint32_t i = keep; i-- > 0;)
+ for (uint32_t k = r; k-- > 0;)
+ m->ix_pos[i * r + k] = m->ix_pos[i] * r + k;
+ for (uint32_t t = tail; t <= p; t++)
+ m->ix_pos[np++] = t;
+ struct head_job hj = {.m = m,
+ .ly = ly,
+ .q = m->q + (size_t)j * qd,
+ .out = m->att + (size_t)j * qd,
+ .pos = m->ix_pos,
+ .n_pos = np};
+ janas_pool_run(m->compute, head_worker, &hj);
+ }
+}
diff --git a/src/llm/model_rec.c b/src/llm/model_rec.c
index e9e46bd..1b5dfe9 100644
--- a/src/llm/model_rec.c
+++ b/src/llm/model_rec.c
@@ -197,9 +197,11 @@ static void rec_worker(void *arg, int tid, int n_threads)
}
#if defined(__x86_64__)
-/* rms_norm(o) * w * SiLU(z), in place, n a multiple of 8. */
+/* rms_norm(o) * w * SiLU(z), in place, n a multiple of 8; with sig, times
+ sigmoid(z) instead (qwen4exp) */
__attribute__((target("avx2,fma"))) static void
-gate_avx2(float *o, const float *z, const float *w, uint32_t n, float eps)
+gate_avx2(float *o, const float *z, const float *w, uint32_t n, float eps,
+ int sig)
{
__m256 acc = _mm256_setzero_ps();
for (uint32_t i = 0; i < n; i += 8) {
@@ -210,13 +212,21 @@ gate_avx2(float *o, const float *z, const float *w, uint32_t n, float eps)
for (uint32_t i = 0; i < n; i += 8) {
__m256 v = _mm256_mul_ps(_mm256_mul_ps(_mm256_loadu_ps(o + i), sc),
_mm256_loadu_ps(w + i));
- _mm256_storeu_ps(o + i,
- _mm256_mul_ps(v, janas_silu8(_mm256_loadu_ps(z + i))));
+ __m256 zv = _mm256_loadu_ps(z + i);
+ /* sigmoid(z) as silu's: 1 / (1 + e^-z) */
+ __m256 g =
+ sig ? _mm256_div_ps(_mm256_set1_ps(1.0f),
+ _mm256_add_ps(_mm256_set1_ps(1.0f),
+ janas_exp8(_mm256_sub_ps(
+ _mm256_setzero_ps(), zv))))
+ : janas_silu8(zv);
+ _mm256_storeu_ps(o + i, _mm256_mul_ps(v, g));
}
}
#endif
-/* 3. RMS norm (ssm_norm) of each head's output, gated by SiLU(z): item =
+/* 3. RMS norm (ssm_norm) of each head's output, gated by SiLU(z) - by
+ sigmoid(z) in qwen4exp, the one difference from Qwen3.5's - item =
(token, value head). */
static void gate_worker(void *arg, int tid, int n_threads)
{
@@ -236,13 +246,14 @@ static void gate_worker(void *arg, int tid, int n_threads)
ds * rf + r * ds;
#if defined(__x86_64__)
if (m->avx2) {
- gate_avx2(o, z, w, ds, m->eps);
+ gate_avx2(o, z, w, ds, m->eps, m->a->hc);
continue;
}
#endif
rms_norm(o, o, w, ds, m->eps);
for (uint32_t i = 0; i < ds; i++)
- o[i] *= z[i] / (1.0f + janas_expf(-z[i]));
+ o[i] *= m->a->hc ? 1.0f / (1.0f + janas_expf(-z[i]))
+ : z[i] / (1.0f + janas_expf(-z[i]));
}
}
diff --git a/src/llm/model_state.c b/src/llm/model_state.c
index 67056a9..b62aaa1 100644
--- a/src/llm/model_state.c
+++ b/src/llm/model_state.c
@@ -11,6 +11,11 @@
* stands exactly there; otherwise - a draft was partly rejected, and the
* model reaches position n by replaying the accepted tokens of its log -
* both buffers and the log are kept as they are.
+ *
+ * qwen4exp adds what its n-gram embeddings read back (the conv's inputs and
+ * the tokens of the positions its history reaches, out of their rings) and
+ * its sparse attention's raw keys for positions 0 .. n - 1; the blocks'
+ * pooled keys are made again from them.
*/
#include <stdio.h>
#include <stdlib.h>
@@ -37,9 +42,77 @@ struct janas_llm_state {
float *log_mixed, *log_beta, *log_decay;
int base;
uint32_t base_pos, log_n;
+ /* qwen4exp: the n-gram rings' last ng_n positions before n, and the
+ indexer's raw keys (n_attn x n x ix_dim, f16) */
+ uint32_t ng_n;
+ int32_t *ng_tok;
+ float *ng_hist;
+ uint16_t *ix;
size_t bytes;
};
+/* qwen4exp: the bytes of a position's conv inputs, and of a slot's raw
+ keys for n positions */
+static size_t ng_row(const struct janas_llm_model *m)
+{
+ return (size_t)m->hc_n * m->d_model * sizeof(float);
+}
+
+static size_t ix_len(const struct janas_llm_model *m, size_t n)
+{
+ return (size_t)m->n_attn * n * m->ix_dim * sizeof(uint16_t);
+}
+
+/* the positions of the n-gram history a state of n needs */
+static uint32_t ng_keep(const struct janas_llm_model *m, uint32_t n)
+{
+ if (!m->hc_n || m->ng_layer >= m->n_layer)
+ return 0;
+ uint32_t hist = (m->ng_kernel - 1) * m->ng_len;
+ return n < hist ? n : hist;
+}
+
+/* the n-gram history and the raw keys of a state of n, out of the model
+ (out) or into it; -1 if the rings no longer hold that history */
+static int copy_hc(struct janas_llm_model *m, struct janas_llm_state *st,
+ int out)
+{
+ uint32_t n = st->n, from = n - st->ng_n;
+ if (st->ng_n) {
+ if (out && (m->ng_hi < n ||
+ (m->ng_hi > m->ng_cap && from < m->ng_hi - m->ng_cap)))
+ return -1;
+ for (uint32_t i = 0; i < st->ng_n; i++) {
+ uint32_t r = (from + i) % m->ng_cap;
+ float *h = m->ng_hist + (size_t)r * ng_row(m) / sizeof(float);
+ if (out) {
+ st->ng_tok[i] = m->ng_tok[r];
+ memcpy(st->ng_hist + (size_t)i * ng_row(m) / sizeof(float), h,
+ ng_row(m));
+ } else {
+ m->ng_tok[r] = st->ng_tok[i];
+ memcpy(h, st->ng_hist + (size_t)i * ng_row(m) / sizeof(float),
+ ng_row(m));
+ }
+ }
+ if (!out)
+ m->ng_hi = n;
+ }
+ if (st->ix) {
+ size_t row = (size_t)n * m->ix_dim;
+ for (uint32_t sl = 0; sl < m->n_attn; sl++) {
+ uint16_t *c = m->ix_raw + (size_t)sl * m->n_ctx * m->ix_dim;
+ if (out)
+ memcpy(st->ix + sl * row, c, row * sizeof(uint16_t));
+ else
+ memcpy(c, st->ix + sl * row, row * sizeof(uint16_t));
+ }
+ if (!out)
+ janas_m_qsa_restore(m, n);
+ }
+ return 0;
+}
+
static size_t ssm_len(const struct janas_llm_model *m)
{
return (size_t)m->n_rec * m->n_vh * m->ds * m->ds;
@@ -63,6 +136,9 @@ void janas_llm_state_free(struct janas_llm_state *st)
free(st->log_mixed);
free(st->log_beta);
free(st->log_decay);
+ free(st->ng_tok);
+ free(st->ng_hist);
+ free(st->ix);
free(st);
}
@@ -215,7 +291,20 @@ struct janas_llm_state *janas_llm_model_state_save(struct janas_llm_model *m,
st->bytes += 2 * ssm_len(m) * sizeof(uint16_t) +
2 * conv_len(m) * sizeof(float) + lm + 2 * lb;
}
- if (bad) {
+ /* qwen4exp's n-gram history and raw keys */
+ st->ng_n = ng_keep(m, n);
+ if (st->ng_n) {
+ st->ng_tok = malloc(st->ng_n * sizeof(int32_t));
+ st->ng_hist = malloc(st->ng_n * ng_row(m));
+ bad |= !st->ng_tok || !st->ng_hist;
+ st->bytes += st->ng_n * (sizeof(int32_t) + ng_row(m));
+ }
+ if (m->ix_heads) {
+ st->ix = malloc(ix_len(m, n) ? ix_len(m, n) : 1);
+ bad |= !st->ix;
+ st->bytes += ix_len(m, n);
+ }
+ if (bad || copy_hc(m, st, 1) != 0) {
janas_llm_state_free(st);
return NULL;
}
@@ -229,7 +318,8 @@ int janas_llm_model_state_load(struct janas_llm_model *m,
being long enough */
if (st->n > m->n_ctx || st->n_slots != m->n_attn + (m->mtp ? 1u : 0u) ||
st->k_use != m->k_use || st->exp_level != m->exp_level ||
- (m->a->rec != 0) != (st->nbuf != 0))
+ (m->a->rec != 0) != (st->nbuf != 0) || st->ng_n != ng_keep(m, st->n) ||
+ (m->ix_heads != 0) != (st->ix != 0))
return -1;
copy_kv(m, (struct janas_llm_state *)st, 0);
m->ring_hi = st->n;
@@ -255,6 +345,7 @@ int janas_llm_model_state_load(struct janas_llm_model *m,
m->base_pos = st->base_pos;
m->log_n = st->log_n;
}
+ copy_hc(m, (struct janas_llm_state *)st, 0);
return 0;
}
@@ -343,12 +434,16 @@ int janas_llm_state_write(const struct janas_llm_model *m,
h[H_LEVEL] = (uint32_t)st->exp_level;
size_t kv, rows, ssm, conv, lm, lb;
lengths(m, h, &kv, &rows, &ssm, &conv, &lm, &lb);
+ /* qwen4exp's part last, its sizes the model's (ng_keep, ix_len) */
return put(f, h, sizeof(h)) || put(f, st->k, kv) || put(f, st->v, kv) ||
put(f, st->ks, rows * sizeof(float)) ||
put(f, st->vs, rows * sizeof(float)) ||
put(f, st->ssm, ssm) || put(f, st->conv, conv) ||
put(f, st->log_mixed, lm) || put(f, st->log_beta, lb) ||
- put(f, st->log_decay, lb)
+ put(f, st->log_decay, lb) ||
+ put(f, st->ng_tok, st->ng_n * sizeof(int32_t)) ||
+ put(f, st->ng_hist, st->ng_n * ng_row(m)) ||
+ (st->ix && put(f, st->ix, ix_len(m, st->n)))
? -1
: 0;
}
@@ -403,6 +498,19 @@ struct janas_llm_state *janas_llm_state_read(const struct janas_llm_model *m,
get(f, st->vs, rows * sizeof(float)) || get(f, st->ssm, ssm) ||
get(f, st->conv, conv) || get(f, st->log_mixed, lm) ||
get(f, st->log_beta, lb) || get(f, st->log_decay, lb);
+ if (!bad && (st->ng_n = ng_keep(m, st->n)) != 0) {
+ st->ng_tok = malloc(st->ng_n * sizeof(int32_t));
+ st->ng_hist = malloc(st->ng_n * ng_row(m));
+ bad = !st->ng_tok || !st->ng_hist ||
+ get(f, st->ng_tok, st->ng_n * sizeof(int32_t)) ||
+ get(f, st->ng_hist, st->ng_n * ng_row(m));
+ st->bytes += st->ng_n * (sizeof(int32_t) + ng_row(m));
+ }
+ if (!bad && m->ix_heads) {
+ st->ix = malloc(ix_len(m, st->n) ? ix_len(m, st->n) : 1);
+ bad = !st->ix || get(f, st->ix, ix_len(m, st->n));
+ st->bytes += ix_len(m, st->n);
+ }
if (bad || (st->nbuf == 2 && (st->base < 0 || st->base > 1))) {
janas_llm_state_free(st);
return NULL;
@@ -414,8 +522,8 @@ void janas_llm_model_memory(const struct janas_llm_model *m, uint64_t *resident,
uint64_t *cache, uint64_t *kv)
{
uint64_t r = m->j.h.resident_bytes;
- if (m->ple_tok && m->ple_tok->offset >= m->j.h.resident_offset &&
- m->ple_tok->bytes < r)
+ if (m->ple_tok && !m->ple_ram &&
+ m->ple_tok->offset >= m->j.h.resident_offset && m->ple_tok->bytes < r)
r -= m->ple_tok->bytes;
uint64_t c = 0;
if (m->cache)
diff --git a/src/llm/model_tune.c b/src/llm/model_tune.c
index 12a47f2..c6364d1 100644
--- a/src/llm/model_tune.c
+++ b/src/llm/model_tune.c
@@ -245,6 +245,9 @@ void janas_m_apply_candidate(struct janas_llm_model *m, int cand, uint32_t nv)
if (m->own_compute)
janas_pool_set_active(m->compute, m->tuner.cand[cand].threads);
m->gpu_use = m->tuner.cand[cand].gpu;
+ if (m->gpu && nv >= JANAS_GPU_FAST_N && janas_gpu_repeatable() &&
+ janas_gpu_fast_prompt())
+ m->gpu_use = 0; /* the fast products alone: gpu.h */
m->npu_use = m->tuner.cand[cand].npu;
janas_m_npu_awake(m, nv);
if (m->gpu) {
@@ -252,6 +255,7 @@ void janas_m_apply_candidate(struct janas_llm_model *m, int cand, uint32_t nv)
m->gpu_use && (nv >= janas_gpu_min_n(m->gpu) ||
janas_m_gpu_token_asked()));
janas_gpu_set_only_fast(m->gpu, !m->gpu_use);
+ janas_gpu_set_pass(m->gpu, nv);
}
}
diff --git a/src/llm/quant.c b/src/llm/quant.c
index 31e2ff5..20ecdd6 100644
--- a/src/llm/quant.c
+++ b/src/llm/quant.c
@@ -7,6 +7,11 @@
#include "quant_q6kp.h"
#include "vmath.h"
#include "iq3s_grid.h"
+#include "iq1s_grid.h"
+#include "iq2s_grid.h"
+#include "iq2xs_grid.h"
+#include "iq2xxs_grid.h"
+#include "iq3xxs_grid.h"
#include <math.h>
#include <stddef.h>
@@ -944,6 +949,116 @@ float janas_q4_1_dot_ref(const struct janas_block_q4_1 *w,
return dm_dot_ref((const uint8_t *)w, sizeof(*w), 0, x, nb);
}
+/* The value of weight l of a Q5_0 block, 0-31: the weight is d (q - 16). */
+static inline int q5_0_value(const struct janas_block_q5_0 *b, int l)
+{
+ uint32_t qh;
+ memcpy(&qh, b->qh, 4);
+ int lo = l < 16 ? b->qs[l] & 15 : b->qs[l - 16] >> 4;
+ return lo | (int)((qh >> l) & 1) << 4;
+}
+
+void janas_q5_0_dequantize(const struct janas_block_q5_0 *x, float *y, size_t n)
+{
+ for (size_t b = 0; b < n / 32; b++, y += 32) {
+ float d = janas_fp16_to_fp32(x[b].d);
+ for (int l = 0; l < 32; l++)
+ y[l] = d * (float)(q5_0_value(&x[b], l) - 16);
+ }
+}
+
+/* One lane per block position, as Q8_0. */
+float janas_q5_0_dot_ref(const struct janas_block_q5_0 *w,
+ const struct janas_block_q8k *x, size_t nb)
+{
+ float acc[8] = {0};
+ for (size_t b = 0; b < nb; b++)
+ for (int k = 0; k < 8; k++) {
+ const struct janas_block_q5_0 *wk = w + 8 * b + k;
+ int32_t dot = 0;
+ for (int l = 0; l < 32; l++)
+ dot += (q5_0_value(wk, l) - 16) * x[b].qs32[32 * k + l];
+ acc[k] = fmaf(janas_fp16_to_fp32(wk->d) * x[b].d32[k], (float)dot,
+ acc[k]);
+ }
+ return ((acc[0] + acc[4]) + (acc[2] + acc[6])) +
+ ((acc[1] + acc[5]) + (acc[3] + acc[7]));
+}
+
+/* The value of weight i of a Q2_0 block, 0-3: the weight is d (q - 1). */
+static inline int q2_0_value(const struct janas_block_q2_0 *b, int i)
+{
+ return b->qs[i / 4] >> 2 * (i % 4) & 3;
+}
+
+void janas_q2_0_dequantize(const struct janas_block_q2_0 *x, float *y, size_t n)
+{
+ for (size_t b = 0; b < n / 64; b++, y += 64) {
+ float d = janas_fp16_to_fp32(x[b].d);
+ for (int i = 0; i < 64; i++)
+ y[i] = (float)(q2_0_value(&x[b], i) - 1) * d;
+ }
+}
+
+/* One lane per block of 32 activations, two per Q2_0 block, as Q8_0. */
+float janas_q2_0_dot_ref(const struct janas_block_q2_0 *w,
+ const struct janas_block_q8k *x, size_t nb)
+{
+ float acc[8] = {0};
+ for (size_t b = 0; b < nb; b++)
+ for (int k = 0; k < 8; k++) {
+ const struct janas_block_q2_0 *wk = w + 4 * b + k / 2;
+ int32_t dot = 0;
+ for (int l = 0; l < 32; l++)
+ dot += (q2_0_value(wk, 32 * (k % 2) + l) - 1) *
+ x[b].qs32[32 * k + l];
+ acc[k] = fmaf(janas_fp16_to_fp32(wk->d) * x[b].d32[k], (float)dot,
+ acc[k]);
+ }
+ return ((acc[0] + acc[4]) + (acc[2] + acc[6])) +
+ ((acc[1] + acc[5]) + (acc[3] + acc[7]));
+}
+
+/* The two-bit value of weight e of a Q2_K block. */
+static inline int q2k_value(const struct janas_block_q2k *w, int e)
+{
+ int n = e / 128, j = e % 128 / 32, l = e % 32;
+ return w->qs[32 * n + l] >> 2 * j & 3;
+}
+
+void janas_q2k_dequantize(const struct janas_block_q2k *x, float *y, size_t n)
+{
+ for (size_t b = 0; b < n / JANAS_QK; b++, y += JANAS_QK) {
+ float d = janas_fp16_to_fp32(x[b].d),
+ mn = janas_fp16_to_fp32(x[b].dmin);
+ for (int e = 0; e < JANAS_QK; e++) {
+ /* as ggml's dequantize_row_q2_K */
+ uint8_t sc = x[b].scales[e / 16];
+ float dl = d * (float)(sc & 15), ml = mn * (float)(sc >> 4);
+ y[e] = dl * (float)q2k_value(&x[b], e) - ml;
+ }
+ }
+}
+
+float janas_q2k_dot_ref(const struct janas_block_q2k *w,
+ const struct janas_block_q8k *x, size_t nb)
+{
+ float acc = 0.0f, accm = 0.0f;
+ for (size_t b = 0; b < nb; b++) {
+ int32_t sumi = 0, summ = 0;
+ for (int g = 0; g < 16; g++) {
+ int32_t s = 0;
+ for (int e = 16 * g; e < 16 * g + 16; e++)
+ s += q2k_value(&w[b], e) * x[b].qs[e];
+ sumi += (w[b].scales[g] & 15) * s;
+ summ += (w[b].scales[g] >> 4) * x[b].bsums[g];
+ }
+ acc = fmaf(janas_fp16_to_fp32(w[b].d) * x[b].d, (float)sumi, acc);
+ accm = fmaf(janas_fp16_to_fp32(w[b].dmin) * x[b].d, (float)summ, accm);
+ }
+ return acc - accm;
+}
+
/* The sixteen 6-bit scales of a Q3_K block, less their offset of 32. */
static inline void q3k_scales(const uint8_t *q, int8_t sc[16])
{
@@ -1041,6 +1156,253 @@ float janas_iq3s_dot_ref(const struct janas_block_iq3s *w,
return acc;
}
+/* The sign byte of an IQ2 and IQ3 7-bit index: the eighth sign makes the
+ count of negative weights even (ggml's ksigns_iq2xs) */
+static inline uint32_t iq_signs(uint32_t i)
+{
+ return i | (uint32_t)__builtin_parity(i) << 7;
+}
+
+/* Sub-group l (0-31) of an IQ2 block of the type: its grid entry (eight
+ magnitudes), its signs (bit j for weight j), its scale 1 + 2 s */
+static inline void iq2_sub(int type, const void *blk, int l, uint64_t *grid,
+ uint32_t *signs, int *scale)
+{
+ int g = l / 4, k = l % 4;
+ if (type == JANAS_IQ2_XXS) {
+ const struct janas_block_iq2xxs *w = blk;
+ uint32_t aux[2];
+ memcpy(aux, w->qs + 4 * g, 8);
+ *grid = iq2xxs_grid[aux[0] >> 8 * k & 255];
+ *signs = iq_signs(aux[1] >> 7 * k & 127);
+ *scale = 1 + 2 * (int)(aux[1] >> 28);
+ } else if (type == JANAS_IQ2_XS) {
+ const struct janas_block_iq2xs *w = blk;
+ uint32_t q = w->qs[4 * g + k];
+ *grid = iq2xs_grid[q & 511];
+ *signs = iq_signs(q >> 9);
+ *scale = 1 + 2 * (w->scales[g] >> 4 * (k / 2) & 15);
+ } else {
+ const struct janas_block_iq2s *w = blk;
+ *grid = iq2s_grid[w->qs[4 * g + k] |
+ ((uint32_t)w->qh[g] << (8 - 2 * k) & 0x300)];
+ *signs = w->qs[32 + 4 * g + k];
+ *scale = 1 + 2 * (w->scales[g] >> 4 * (k / 2) & 15);
+ }
+}
+
+static size_t iq2_size(int type)
+{
+ return type == JANAS_IQ2_XXS ? sizeof(struct janas_block_iq2xxs)
+ : type == JANAS_IQ2_XS ? sizeof(struct janas_block_iq2xs)
+ : sizeof(struct janas_block_iq2s);
+}
+
+void janas_iq2_dequantize(int type, const void *x, float *y, size_t n)
+{
+ const uint8_t *b8 = x;
+ for (size_t b = 0; b < n / JANAS_QK; b++, b8 += iq2_size(type)) {
+ uint16_t h;
+ memcpy(&h, b8, 2);
+ float d = janas_fp16_to_fp32(h);
+ for (int l = 0; l < 32; l++, y += 8) {
+ uint64_t grid;
+ uint32_t signs;
+ int scale;
+ iq2_sub(type, b8, l, &grid, &signs, &scale);
+ /* as ggml's dequantize_row_iq2_*: d (0.5 + s) / 4 */
+ float dl = d * (0.5f + (float)((scale - 1) / 2)) * 0.25f;
+ for (int j = 0; j < 8; j++)
+ y[j] = dl * (float)(grid >> 8 * j & 0xff) *
+ (signs >> j & 1 ? -1.0f : 1.0f);
+ }
+ }
+}
+
+float janas_iq2_dot_ref(int type, const void *w,
+ const struct janas_block_q8k *x, size_t nb)
+{
+ const uint8_t *b8 = w;
+ float acc = 0.0f;
+ for (size_t b = 0; b < nb; b++, b8 += iq2_size(type)) {
+ int32_t sumi = 0;
+ for (int l = 0; l < 32; l++) {
+ uint64_t grid;
+ uint32_t signs;
+ int scale, s = 0;
+ iq2_sub(type, b8, l, &grid, &signs, &scale);
+ for (int j = 0; j < 8; j++) {
+ int v = (int)(grid >> 8 * j & 0xff);
+ s += (signs >> j & 1 ? -v : v) * x[b].qs[8 * l + j];
+ }
+ sumi += scale * s;
+ }
+ uint16_t h;
+ memcpy(&h, b8, 2);
+ acc = fmaf(janas_fp16_to_fp32(h) * 0.125f * x[b].d, (float)sumi, acc);
+ }
+ return acc;
+}
+
+/* IQ1_M: the block's d, from the top nibbles of its four scale words */
+static inline float iq1m_d(const struct janas_block_iq1m *w)
+{
+ uint16_t sc[4];
+ memcpy(sc, w->scales, 8);
+ uint16_t h = (uint16_t)((sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) |
+ ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000));
+ return janas_fp16_to_fp32(h);
+}
+
+/* sub-group l (0-31) of an IQ1_M block: its grid entry, whether its shift
+ is negative, its half's 2 s + 1 */
+static inline void iq1m_sub(const struct janas_block_iq1m *w, int l,
+ uint64_t *grid, int *neg, int *scale)
+{
+ int g = l / 4, k = l % 4;
+ uint8_t qh = w->qh[2 * g + k / 2];
+ uint32_t idx = w->qs[4 * g + k] | ((uint32_t)qh << (k % 2 ? 4 : 8) & 0x700);
+ *grid = iq1s_grid[idx];
+ *neg = qh >> (k % 2 ? 7 : 3) & 1;
+ uint16_t sc;
+ memcpy(&sc, w->scales + 2 * (g / 2), 2);
+ *scale = 2 * (sc >> (6 * (g % 2) + 3 * (k / 2)) & 7) + 1;
+}
+
+/* the same for IQ1_S: one scale and one shift for the group */
+static inline void iq1s_sub(const struct janas_block_iq1s *w, int l,
+ uint64_t *grid, int *neg, int *scale)
+{
+ int g = l / 4, k = l % 4;
+ uint32_t qh = w->qh[g];
+ *grid = iq1s_grid[w->qs[4 * g + k] | (qh >> 3 * k & 7) << 8];
+ *neg = qh >> 15 & 1;
+ *scale = 2 * (int)(qh >> 12 & 7) + 1;
+}
+
+void janas_iq1s_dequantize(const struct janas_block_iq1s *x, float *y, size_t n)
+{
+ for (size_t b = 0; b < n / JANAS_QK; b++) {
+ float d = janas_fp16_to_fp32(x[b].d);
+ for (int l = 0; l < 32; l++, y += 8) {
+ uint64_t grid;
+ int neg, scale;
+ iq1s_sub(&x[b], l, &grid, &neg, &scale);
+ /* as ggml's dequantize_row_iq1_s */
+ float dl = d * (float)scale, delta = neg ? -0.125f : 0.125f;
+ for (int j = 0; j < 8; j++)
+ y[j] = dl * ((float)(int8_t)(grid >> 8 * j & 0xff) + delta);
+ }
+ }
+}
+
+float janas_iq1s_dot_ref(const struct janas_block_iq1s *w,
+ const struct janas_block_q8k *x, size_t nb)
+{
+ float acc = 0.0f;
+ for (size_t b = 0; b < nb; b++) {
+ int32_t sumi = 0;
+ for (int l = 0; l < 32; l++) {
+ uint64_t grid;
+ int neg, scale, s = 0;
+ iq1s_sub(&w[b], l, &grid, &neg, &scale);
+ for (int j = 0; j < 8; j++)
+ s += (8 * (int8_t)(grid >> 8 * j & 0xff) + (neg ? -1 : 1)) *
+ x[b].qs[8 * l + j];
+ sumi += scale * s;
+ }
+ acc = fmaf(janas_fp16_to_fp32(w[b].d) * 0.125f * x[b].d, (float)sumi,
+ acc);
+ }
+ return acc;
+}
+
+void janas_iq1m_dequantize(const struct janas_block_iq1m *x, float *y, size_t n)
+{
+ for (size_t b = 0; b < n / JANAS_QK; b++) {
+ float d = iq1m_d(&x[b]);
+ for (int l = 0; l < 32; l++, y += 8) {
+ uint64_t grid;
+ int neg, scale;
+ iq1m_sub(&x[b], l, &grid, &neg, &scale);
+ /* as ggml's dequantize_row_iq1_m */
+ float dl = d * (float)scale, delta = neg ? -0.125f : 0.125f;
+ for (int j = 0; j < 8; j++)
+ y[j] = dl * ((float)(int8_t)(grid >> 8 * j & 0xff) + delta);
+ }
+ }
+}
+
+float janas_iq1m_dot_ref(const struct janas_block_iq1m *w,
+ const struct janas_block_q8k *x, size_t nb)
+{
+ float acc = 0.0f;
+ for (size_t b = 0; b < nb; b++) {
+ int32_t sumi = 0;
+ for (int l = 0; l < 32; l++) {
+ uint64_t grid;
+ int neg, scale, s = 0;
+ iq1m_sub(&w[b], l, &grid, &neg, &scale);
+ for (int j = 0; j < 8; j++)
+ s += (8 * (int8_t)(grid >> 8 * j & 0xff) + (neg ? -1 : 1)) *
+ x[b].qs[8 * l + j];
+ sumi += scale * s;
+ }
+ acc = fmaf(iq1m_d(&w[b]) * 0.125f * x[b].d, (float)sumi, acc);
+ }
+ return acc;
+}
+
+/* group g's word of an IQ3_XXS block: four sign indices and the scale */
+static inline uint32_t iq3xxs_aux(const struct janas_block_iq3xxs *w, int g)
+{
+ uint32_t aux;
+ memcpy(&aux, w->qs + 64 + 4 * g, 4);
+ return aux;
+}
+
+static inline int iq3xxs_value(const struct janas_block_iq3xxs *w, int g, int e)
+{
+ int k = e / 8, j = e % 8;
+ int v =
+ (int)(iq3xxs_grid[w->qs[8 * g + 2 * k + j / 4]] >> 8 * (j % 4) & 0xff);
+ return iq_signs(iq3xxs_aux(w, g) >> 7 * k & 127) >> j & 1 ? -v : v;
+}
+
+void janas_iq3xxs_dequantize(const struct janas_block_iq3xxs *x, float *y,
+ size_t n)
+{
+ for (size_t b = 0; b < n / JANAS_QK; b++) {
+ float d = janas_fp16_to_fp32(x[b].d);
+ for (int g = 0; g < 8; g++, y += 32) {
+ /* as ggml's dequantize_row_iq3_xxs */
+ float db = d * (0.5f + (float)(iq3xxs_aux(&x[b], g) >> 28)) * 0.5f;
+ for (int e = 0; e < 32; e++) {
+ int v = iq3xxs_value(&x[b], g, e);
+ y[e] = db * (float)(v < 0 ? -v : v) * (v < 0 ? -1.0f : 1.0f);
+ }
+ }
+ }
+}
+
+float janas_iq3xxs_dot_ref(const struct janas_block_iq3xxs *w,
+ const struct janas_block_q8k *x, size_t nb)
+{
+ float acc = 0.0f;
+ for (size_t b = 0; b < nb; b++) {
+ int32_t sumi = 0;
+ for (int g = 0; g < 8; g++) {
+ int32_t s = 0;
+ for (int e = 0; e < 32; e++)
+ s += iq3xxs_value(&w[b], g, e) * x[b].qs[32 * g + e];
+ sumi += (1 + 2 * (int32_t)(iq3xxs_aux(&w[b], g) >> 28)) * s;
+ }
+ acc =
+ fmaf(janas_fp16_to_fp32(w[b].d) * 0.25f * x[b].d, (float)sumi, acc);
+ }
+ return acc;
+}
+
/* The 6-bit scale of group g of an IQ4_XS block, less its offset of 32. */
static inline int iq4xs_scale(const struct janas_block_iq4xs *w, int g)
{
@@ -1152,6 +1514,27 @@ static dot51_fn dot51_impl = janas_q5_1_dot_ref;
typedef float (*dot41_fn)(const struct janas_block_q4_1 *,
const struct janas_block_q8k *, size_t);
static dot41_fn dot41_impl = janas_q4_1_dot_ref;
+typedef float (*dot2k_fn)(const struct janas_block_q2k *,
+ const struct janas_block_q8k *, size_t);
+static dot2k_fn dot2k_impl = janas_q2k_dot_ref;
+typedef float (*dotx1s_fn)(const struct janas_block_iq1s *,
+ const struct janas_block_q8k *, size_t);
+static dotx1s_fn dotx1s_impl = janas_iq1s_dot_ref;
+typedef float (*dotx1_fn)(const struct janas_block_iq1m *,
+ const struct janas_block_q8k *, size_t);
+static dotx1_fn dotx1_impl = janas_iq1m_dot_ref;
+typedef float (*dotx2_fn)(int, const void *, const struct janas_block_q8k *,
+ size_t);
+static dotx2_fn dotx2_impl = janas_iq2_dot_ref;
+typedef float (*dotx3_fn)(const struct janas_block_iq3xxs *,
+ const struct janas_block_q8k *, size_t);
+static dotx3_fn dotx3_impl = janas_iq3xxs_dot_ref;
+typedef float (*dot50_fn)(const struct janas_block_q5_0 *,
+ const struct janas_block_q8k *, size_t);
+static dot50_fn dot50_impl = janas_q5_0_dot_ref;
+typedef float (*dot20_fn)(const struct janas_block_q2_0 *,
+ const struct janas_block_q8k *, size_t);
+static dot20_fn dot20_impl = janas_q2_0_dot_ref;
typedef float (*dot3_fn)(const struct janas_block_q3k *,
const struct janas_block_q8k *, size_t);
static dot3_fn dot3_impl = janas_q3k_dot_ref;
@@ -1184,6 +1567,13 @@ __attribute__((constructor)) static void dot_kernels_init(void)
dot40_impl = q4_0_dot_avx2;
dot51_impl = q5_1_dot_avx2;
dot41_impl = q4_1_dot_avx2;
+ dot50_impl = q5_0_dot_avx2;
+ dotx3_impl = iq3xxs_dot_avx2;
+ dotx2_impl = iq2_dot_avx2;
+ dotx1_impl = iq1m_dot_avx2;
+ dotx1s_impl = iq1s_dot_avx2;
+ dot2k_impl = q2k_dot_avx2;
+ dot20_impl = q2_0_dot_avx2;
dot3_impl = q3k_dot_avx2;
dotis_impl = iq3s_dot_avx2;
dot6p_impl = q6kp_dotn_avx2;
@@ -1201,6 +1591,13 @@ __attribute__((constructor)) static void dot_kernels_init(void)
dot40_impl = q4_0_dot_vnni;
dot51_impl = q5_1_dot_vnni;
dot41_impl = q4_1_dot_vnni;
+ dot50_impl = q5_0_dot_vnni;
+ dotx3_impl = iq3xxs_dot_vnni;
+ dotx2_impl = iq2_dot_vnni;
+ dotx1_impl = iq1m_dot_vnni;
+ dotx1s_impl = iq1s_dot_vnni;
+ dot2k_impl = q2k_dot_vnni;
+ dot20_impl = q2_0_dot_vnni;
dot3_impl = q3k_dot_vnni;
dotis_impl = iq3s_dot_vnni;
dot6p_impl = q6kp_dotn_vnni;
@@ -1269,6 +1666,48 @@ float janas_q4_1_dot(const struct janas_block_q4_1 *w,
return dot41_impl(w, x, nb);
}
+float janas_q5_0_dot(const struct janas_block_q5_0 *w,
+ const struct janas_block_q8k *x, size_t nb)
+{
+ return dot50_impl(w, x, nb);
+}
+
+float janas_iq3xxs_dot(const struct janas_block_iq3xxs *w,
+ const struct janas_block_q8k *x, size_t nb)
+{
+ return dotx3_impl(w, x, nb);
+}
+
+float janas_iq2_dot(int type, const void *w, const struct janas_block_q8k *x,
+ size_t nb)
+{
+ return dotx2_impl(type, w, x, nb);
+}
+
+float janas_q2k_dot(const struct janas_block_q2k *w,
+ const struct janas_block_q8k *x, size_t nb)
+{
+ return dot2k_impl(w, x, nb);
+}
+
+float janas_iq1s_dot(const struct janas_block_iq1s *w,
+ const struct janas_block_q8k *x, size_t nb)
+{
+ return dotx1s_impl(w, x, nb);
+}
+
+float janas_iq1m_dot(const struct janas_block_iq1m *w,
+ const struct janas_block_q8k *x, size_t nb)
+{
+ return dotx1_impl(w, x, nb);
+}
+
+float janas_q2_0_dot(const struct janas_block_q2_0 *w,
+ const struct janas_block_q8k *x, size_t nb)
+{
+ return dot20_impl(w, x, nb);
+}
+
float janas_q3k_dot(const struct janas_block_q3k *w,
const struct janas_block_q8k *x, size_t nb)
{
@@ -1350,10 +1789,26 @@ size_t janas_qtype_block_size(int type)
return 8 * sizeof(struct janas_block_q5_1);
case JANAS_Q4_1:
return 8 * sizeof(struct janas_block_q4_1);
+ case JANAS_Q5_0:
+ return 8 * sizeof(struct janas_block_q5_0);
+ case JANAS_Q2_0:
+ return 4 * sizeof(struct janas_block_q2_0);
case JANAS_Q3_K:
return sizeof(struct janas_block_q3k);
+ case JANAS_Q2_K:
+ return sizeof(struct janas_block_q2k);
case JANAS_IQ3_S:
return sizeof(struct janas_block_iq3s);
+ case JANAS_IQ3_XXS:
+ return sizeof(struct janas_block_iq3xxs);
+ case JANAS_IQ2_XXS:
+ case JANAS_IQ2_XS:
+ case JANAS_IQ2_S:
+ return iq2_size(type);
+ case JANAS_IQ1_M:
+ return sizeof(struct janas_block_iq1m);
+ case JANAS_IQ1_S:
+ return sizeof(struct janas_block_iq1s);
case JANAS_IQ4_XS:
return sizeof(struct janas_block_iq4xs);
default:
@@ -1394,12 +1849,35 @@ void janas_dequantize(int type, const void *w, float *y, size_t n)
case JANAS_Q4_1:
janas_q4_1_dequantize(w, y, n);
return;
+ case JANAS_Q5_0:
+ janas_q5_0_dequantize(w, y, n);
+ return;
+ case JANAS_Q2_0:
+ janas_q2_0_dequantize(w, y, n);
+ return;
case JANAS_Q3_K:
janas_q3k_dequantize(w, y, n);
return;
+ case JANAS_Q2_K:
+ janas_q2k_dequantize(w, y, n);
+ return;
case JANAS_IQ3_S:
janas_iq3s_dequantize(w, y, n);
return;
+ case JANAS_IQ3_XXS:
+ janas_iq3xxs_dequantize(w, y, n);
+ return;
+ case JANAS_IQ2_XXS:
+ case JANAS_IQ2_XS:
+ case JANAS_IQ2_S:
+ janas_iq2_dequantize(type, w, y, n);
+ return;
+ case JANAS_IQ1_M:
+ janas_iq1m_dequantize(w, y, n);
+ return;
+ case JANAS_IQ1_S:
+ janas_iq1s_dequantize(w, y, n);
+ return;
default:
/* callers check janas_qtype_block_size first: this is a bug */
abort();
@@ -1430,10 +1908,26 @@ float janas_dot(int type, const void *w, const struct janas_block_q8k *x,
return janas_q5_1_dot(w, x, nb);
case JANAS_Q4_1:
return janas_q4_1_dot(w, x, nb);
+ case JANAS_Q5_0:
+ return janas_q5_0_dot(w, x, nb);
+ case JANAS_Q2_0:
+ return janas_q2_0_dot(w, x, nb);
case JANAS_Q3_K:
return janas_q3k_dot(w, x, nb);
+ case JANAS_Q2_K:
+ return janas_q2k_dot(w, x, nb);
case JANAS_IQ3_S:
return janas_iq3s_dot(w, x, nb);
+ case JANAS_IQ3_XXS:
+ return janas_iq3xxs_dot(w, x, nb);
+ case JANAS_IQ2_XXS:
+ case JANAS_IQ2_XS:
+ case JANAS_IQ2_S:
+ return janas_iq2_dot(type, w, x, nb);
+ case JANAS_IQ1_M:
+ return janas_iq1m_dot(w, x, nb);
+ case JANAS_IQ1_S:
+ return janas_iq1s_dot(w, x, nb);
case JANAS_Q6_K_P: /* the base plane alone: two bits per weight */
return janas_q6kp_dot(w, NULL, NULL, x, nb);
default:
@@ -1566,12 +2060,30 @@ void janas_dot_rows(int type, const void *w, size_t row_bytes,
case JANAS_Q4_1:
f = use_vnni ? q4_1_rows_vnni : q4_1_rows_avx2;
break;
+ case JANAS_Q5_0:
+ f = use_vnni ? q5_0_rows_vnni : q5_0_rows_avx2;
+ break;
+ case JANAS_Q2_0:
+ f = use_vnni ? q2_0_rows_vnni : q2_0_rows_avx2;
+ break;
case JANAS_Q3_K:
f = use_vnni ? q3k_rows_vnni : q3k_rows_avx2;
break;
+ case JANAS_Q2_K:
+ f = use_vnni ? q2k_rows_vnni : q2k_rows_avx2;
+ break;
case JANAS_IQ3_S:
f = use_vnni ? iq3s_rows_vnni : iq3s_rows_avx2;
break;
+ case JANAS_IQ3_XXS:
+ f = use_vnni ? iq3xxs_rows_vnni : iq3xxs_rows_avx2;
+ break;
+ case JANAS_IQ1_M:
+ f = use_vnni ? iq1m_rows_vnni : iq1m_rows_avx2;
+ break;
+ case JANAS_IQ1_S:
+ f = use_vnni ? iq1s_rows_vnni : iq1s_rows_avx2;
+ break;
}
if (f) {
f(w, row_bytes, x, nb, rows, y);
@@ -1593,7 +2105,10 @@ void janas_dot_multi(int type, const void *w, const struct janas_block_q8k *x,
(type == JANAS_Q4_K || type == JANAS_Q6_K || type == JANAS_Q5_K ||
type == JANAS_Q8_0 || type == JANAS_IQ4_NL || type == JANAS_IQ4_XS ||
type == JANAS_Q4_0 || type == JANAS_Q5_1 || type == JANAS_Q4_1 ||
- type == JANAS_Q3_K || type == JANAS_IQ3_S || type == JANAS_MXFP4)) {
+ type == JANAS_Q3_K || type == JANAS_IQ3_S || type == JANAS_MXFP4 ||
+ type == JANAS_Q5_0 || type == JANAS_Q2_0 || type == JANAS_IQ3_XXS ||
+ type == JANAS_IQ2_XXS || type == JANAS_IQ2_XS || type == JANAS_IQ2_S ||
+ type == JANAS_IQ1_M || type == JANAS_IQ1_S || type == JANAS_Q2_K)) {
float r[8];
int g = 1;
for (; v < nv; v += (size_t)g) {
@@ -1637,14 +2152,44 @@ void janas_dot_multi(int type, const void *w, const struct janas_block_q8k *x,
(use_vnni ? q4_1_dotn_vnni : q4_1_dotn_avx2)(w, xv, x_stride, g,
nb, r);
break;
+ case JANAS_Q5_0:
+ (use_vnni ? q5_0_dotn_vnni : q5_0_dotn_avx2)(w, xv, x_stride, g,
+ nb, r);
+ break;
+ case JANAS_Q2_0:
+ (use_vnni ? q2_0_dotn_vnni : q2_0_dotn_avx2)(w, xv, x_stride, g,
+ nb, r);
+ break;
case JANAS_Q3_K:
(use_vnni ? q3k_dotn_vnni : q3k_dotn_avx2)(w, xv, x_stride, g,
nb, r);
break;
+ case JANAS_Q2_K:
+ (use_vnni ? q2k_dotn_vnni : q2k_dotn_avx2)(w, xv, x_stride, g,
+ nb, r);
+ break;
case JANAS_IQ3_S:
(use_vnni ? iq3s_dotn_vnni : iq3s_dotn_avx2)(w, xv, x_stride, g,
nb, r);
break;
+ case JANAS_IQ3_XXS:
+ (use_vnni ? iq3xxs_dotn_vnni
+ : iq3xxs_dotn_avx2)(w, xv, x_stride, g, nb, r);
+ break;
+ case JANAS_IQ2_XXS:
+ case JANAS_IQ2_XS:
+ case JANAS_IQ2_S:
+ (use_vnni ? iq2_dotn_vnni : iq2_dotn_avx2)(type, w, xv,
+ x_stride, g, nb, r);
+ break;
+ case JANAS_IQ1_M:
+ (use_vnni ? iq1m_dotn_vnni : iq1m_dotn_avx2)(w, xv, x_stride, g,
+ nb, r);
+ break;
+ case JANAS_IQ1_S:
+ (use_vnni ? iq1s_dotn_vnni : iq1s_dotn_avx2)(w, xv, x_stride, g,
+ nb, r);
+ break;
default:
(use_vnni ? q8_0_dotn_vnni : q8_0_dotn_avx2)(w, xv, x_stride, g,
nb, r);
@@ -1658,3 +2203,115 @@ void janas_dot_multi(int type, const void *w, const struct janas_block_q8k *x,
for (; v < nv; v++)
out[v * out_stride] = janas_dot(type, w, x + v * x_stride, nb);
}
+
+size_t janas_qtype_row_bytes(int type, size_t n)
+{
+ size_t sub, bytes; /* the types in blocks of 32 or 64 weights */
+ switch (type) {
+ case JANAS_Q8_0:
+ sub = 32, bytes = sizeof(struct janas_block_q8_0);
+ break;
+ case JANAS_IQ4_NL:
+ sub = 32, bytes = sizeof(struct janas_block_iq4nl);
+ break;
+ case JANAS_MXFP4:
+ sub = 32, bytes = sizeof(struct janas_block_mxfp4);
+ break;
+ case JANAS_Q4_0:
+ sub = 32, bytes = sizeof(struct janas_block_q4_0);
+ break;
+ case JANAS_Q5_1:
+ sub = 32, bytes = sizeof(struct janas_block_q5_1);
+ break;
+ case JANAS_Q4_1:
+ sub = 32, bytes = sizeof(struct janas_block_q4_1);
+ break;
+ case JANAS_Q5_0:
+ sub = 32, bytes = sizeof(struct janas_block_q5_0);
+ break;
+ case JANAS_Q2_0:
+ sub = 64, bytes = sizeof(struct janas_block_q2_0);
+ break;
+ default:
+ sub = JANAS_QK, bytes = janas_qtype_block_size(type);
+ }
+ return n % sub == 0 ? n / sub * bytes : 0;
+}
+
+/* The largest group of a type in blocks of 32 or 64: Q8_0's. */
+#define TAIL_GROUP (8 * sizeof(struct janas_block_q8_0))
+
+/* The tail of a row, its bytes past `full`, into pad padded with zero
+ blocks to a whole group. */
+static void tail_group(int type, const void *row, size_t full, size_t row_bytes,
+ uint8_t *pad)
+{
+ size_t tb = row_bytes - full;
+ memcpy(pad, (const uint8_t *)row + full, tb);
+ memset(pad + tb, 0, janas_qtype_block_size(type) - tb);
+}
+
+float janas_dot_n(int type, const void *w, const struct janas_block_q8k *x,
+ size_t n)
+{
+ size_t nb = n / JANAS_QK;
+ if (n % JANAS_QK == 0)
+ return janas_dot(type, w, x, nb);
+ _Alignas(64) uint8_t pad[TAIL_GROUP];
+ tail_group(type, w, nb * janas_qtype_block_size(type),
+ janas_qtype_row_bytes(type, n), pad);
+ float s = nb ? janas_dot(type, w, x, nb) : 0.0f;
+ return s + janas_dot(type, pad, x + nb, 1);
+}
+
+void janas_dot_rows_n(int type, const void *w, size_t row_bytes,
+ const struct janas_block_q8k *x, size_t n, size_t rows,
+ float *y)
+{
+ size_t nb = n / JANAS_QK;
+ if (n % JANAS_QK == 0) {
+ janas_dot_rows(type, w, row_bytes, x, nb, rows, y);
+ return;
+ }
+ /* the whole groups row by row, then the tails a few rows at a time */
+ enum { CHUNK = 32 };
+ size_t gb = janas_qtype_block_size(type), full = nb * gb;
+ _Alignas(64) uint8_t pad[CHUNK * TAIL_GROUP];
+ float t[CHUNK];
+ if (nb)
+ janas_dot_rows(type, w, row_bytes, x, nb, rows, y);
+ for (size_t r0 = 0; r0 < rows; r0 += CHUNK) {
+ size_t c = rows - r0 < CHUNK ? rows - r0 : CHUNK;
+ for (size_t i = 0; i < c; i++)
+ tail_group(type, (const uint8_t *)w + (r0 + i) * row_bytes, full,
+ row_bytes, pad + i * gb);
+ janas_dot_rows(type, pad, gb, x + nb, 1, c, t);
+ for (size_t i = 0; i < c; i++)
+ y[r0 + i] = (nb ? y[r0 + i] : 0.0f) + t[i];
+ }
+}
+
+void janas_dot_multi_n(int type, const void *w, const struct janas_block_q8k *x,
+ size_t x_stride, size_t nv, size_t n, float *out,
+ size_t out_stride)
+{
+ size_t nb = n / JANAS_QK;
+ if (n % JANAS_QK == 0) {
+ janas_dot_multi(type, w, x, x_stride, nv, nb, out, out_stride);
+ return;
+ }
+ _Alignas(64) uint8_t pad[TAIL_GROUP];
+ tail_group(type, w, nb * janas_qtype_block_size(type),
+ janas_qtype_row_bytes(type, n), pad);
+ if (nb)
+ janas_dot_multi(type, w, x, x_stride, nv, nb, out, out_stride);
+ float t[8];
+ for (size_t v = 0; v < nv; v += 8) {
+ size_t g = nv - v < 8 ? nv - v : 8;
+ janas_dot_multi(type, pad, x + v * x_stride + nb, x_stride, g, 1, t, 1);
+ for (size_t i = 0; i < g; i++) {
+ float *o = out + (v + i) * out_stride;
+ *o = (nb ? *o : 0.0f) + t[i];
+ }
+ }
+}
diff --git a/src/llm/quant.h b/src/llm/quant.h
index 8a30bab..e23a80f 100644
--- a/src/llm/quant.h
+++ b/src/llm/quant.h
@@ -140,12 +140,108 @@ struct janas_block_iq3s {
uint8_t scales[JANAS_QK / 64];
};
+/*
+ * IQ3_XXS: 256 weights in eight groups of 32, 98 bytes. Group g: eight 8-bit
+ * indices into a grid of 256 entries of four magnitudes each (qs[8g + k],
+ * weights 4k to 4k + 3), then a 32-bit word at qs[64 + 4g]: four 7-bit
+ * indices of sign bytes (bits 7k, for weights 8k to 8k + 7; the eighth sign
+ * makes the count of negative ones even) and the scale s in its top four
+ * bits. The weight is d (1 + 2 s) / 4 times the signed magnitude.
+ */
+struct janas_block_iq3xxs {
+ uint16_t d; /* fp16 */
+ uint8_t qs[3 * JANAS_QK / 8];
+};
+
+/*
+ * IQ2_XXS, IQ2_XS, IQ2_S: 256 weights in 32 sub-groups of eight, each an
+ * entry of a grid (eight magnitudes) and a sign byte (bit j for weight j),
+ * a scale s for every one or two of them; the weight is d (1 + 2 s) / 8
+ * times the signed magnitude. IQ2_XXS (66 bytes): per group of 32 two
+ * words, four 8-bit indices into a grid of 256, then four 7-bit indices of
+ * sign bytes (the eighth sign making the count of negative ones even) and
+ * the group's scale in the top four bits. IQ2_XS (74 bytes): per
+ * sub-group a 16-bit word, a 9-bit index into a grid of 512 and a 7-bit
+ * index of a sign byte; scales[g]: the scales of sub-groups 4g, 4g + 1
+ * (low nibble) and 4g + 2, 4g + 3. IQ2_S (82 bytes): qs[4g + k] the low
+ * eight bits of a 10-bit index into a grid of 1024 (the high two in bits
+ * 2k of qh[g]), qs[32 + 4g + k] its sign byte as it is, scales as IQ2_XS.
+ */
+struct janas_block_iq2xxs {
+ uint16_t d; /* fp16 */
+ uint16_t qs[JANAS_QK / 8];
+};
+struct janas_block_iq2xs {
+ uint16_t d; /* fp16 */
+ uint16_t qs[JANAS_QK / 8];
+ uint8_t scales[JANAS_QK / 32];
+};
+struct janas_block_iq2s {
+ uint16_t d; /* fp16 */
+ uint8_t qs[JANAS_QK / 4];
+ uint8_t qh[JANAS_QK / 32];
+ uint8_t scales[JANAS_QK / 32];
+};
+
+/*
+ * IQ1_M: 256 weights in 32 sub-groups of eight, 56 bytes: each an 11-bit
+ * index into a grid of 2048 entries of eight levels -1, 0, 1 (qs[4g + k]
+ * the low eight bits; the high three in bits 0-2 or 4-6 of qh[2g + k / 2],
+ * for k even or odd) plus a shift of +1/8, or -1/8 where bit 3 or 7 of that
+ * byte is set; a 3-bit scale s for each half of a group (the 16-bit word
+ * g / 2 of scales, bits 6 (g % 2) and 3 more), the weight d (2 s + 1)
+ * (level + shift). d is an fp16 whose four nibbles are the top four bits
+ * of the four scale words.
+ */
+/* IQ1_S (50 bytes): IQ1_M's grid with one scale and one shift per group of
+ 32: qh[g] holds the high three bits of its four indices (bits 3k), the
+ scale s in bits 12-14 and the shift's sign in bit 15; d an fp16 first. */
+struct janas_block_iq1s {
+ uint16_t d; /* fp16 */
+ uint8_t qs[JANAS_QK / 8];
+ uint16_t qh[JANAS_QK / 32];
+};
+struct janas_block_iq1m {
+ uint8_t qs[JANAS_QK / 8];
+ uint8_t qh[JANAS_QK / 16];
+ uint8_t scales[JANAS_QK / 32];
+};
+
+/*
+ * Q2_K: 256 weights in sixteen groups of 16, 84 bytes. scales[g]: group g's
+ * scale (low nibble) and min (high); qs: in each half of the block (32
+ * bytes) bits 2j of byte l are weight 32 j + l of that half, l < 32. Weight
+ * i of group g is d * scale * q - dmin * min, q in 0..3.
+ */
+struct janas_block_q2k {
+ uint8_t scales[JANAS_QK / 16];
+ uint8_t qs[JANAS_QK / 4];
+ uint16_t d, dmin; /* fp16 */
+};
+
/* Q4_1: Q5_1 without the fifth bits, q of four (0-15). */
struct janas_block_q4_1 {
uint16_t d, m; /* fp16 */
uint8_t qs[16];
};
+/* Q5_0: Q5_1 without the minimum, the weight d * (q - 16). */
+struct janas_block_q5_0 {
+ uint16_t d; /* fp16 */
+ uint8_t qh[4];
+ uint8_t qs[16];
+};
+
+/*
+ * Q2_0 (ggml's type 42): 64 weights d * (q - 1), q of two bits, so -1 to 2:
+ * weight i in bits 2 (i % 4) of qs[i / 4]. Four blocks cover a Q8_K block,
+ * each two of its blocks of 32.
+ */
+struct janas_block_q2_0 {
+ uint16_t d; /* fp16 */
+ uint8_t qs[16];
+};
+
struct janas_block_q6k {
uint8_t ql[JANAS_QK / 2];
uint8_t qh[JANAS_QK / 4];
@@ -198,6 +294,15 @@ _Static_assert(sizeof(struct janas_block_iq4xs) == 136, "iq4_xs block size");
_Static_assert(sizeof(struct janas_block_q4_0) == 18, "q4_0 block size");
_Static_assert(sizeof(struct janas_block_q5_1) == 24, "q5_1 block size");
_Static_assert(sizeof(struct janas_block_q4_1) == 20, "q4_1 block size");
+_Static_assert(sizeof(struct janas_block_q5_0) == 22, "q5_0 block size");
+_Static_assert(sizeof(struct janas_block_q2_0) == 18, "q2_0 block size");
+_Static_assert(sizeof(struct janas_block_iq3xxs) == 98, "iq3_xxs block size");
+_Static_assert(sizeof(struct janas_block_iq2xxs) == 66, "iq2_xxs block size");
+_Static_assert(sizeof(struct janas_block_iq2xs) == 74, "iq2_xs block size");
+_Static_assert(sizeof(struct janas_block_iq2s) == 82, "iq2_s block size");
+_Static_assert(sizeof(struct janas_block_iq1m) == 56, "iq1_m block size");
+_Static_assert(sizeof(struct janas_block_iq1s) == 50, "iq1_s block size");
+_Static_assert(sizeof(struct janas_block_q2k) == 84, "q2_k block size");
_Static_assert(sizeof(struct janas_block_q3k) == 110, "q3k block size");
_Static_assert(sizeof(struct janas_block_iq3s) == 110, "iq3_s block size");
_Static_assert(sizeof(struct janas_block_q8k) == 628, "q8k block size");
@@ -206,16 +311,25 @@ _Static_assert(sizeof(struct janas_block_q8k) == 628, "q8k block size");
enum janas_qtype {
JANAS_Q4_0 = 2, /* sizes per 256 weights: 8 blocks, 144 bytes */
JANAS_Q4_1 = 3, /* 8 blocks, 160 bytes */
+ JANAS_Q5_0 = 6, /* 8 blocks, 176 bytes */
JANAS_Q5_1 = 7, /* 8 blocks, 192 bytes */
JANAS_Q8_0 = 8,
+ JANAS_Q2_K = 10, /* 84 bytes per 256 weights */
JANAS_Q3_K = 11, /* sizes counted per 256 weights: 8 blocks, 272 bytes */
JANAS_Q4_K = 12,
JANAS_Q5_K = 13,
JANAS_Q6_K = 14,
+ JANAS_IQ2_XXS = 16, /* 66 bytes per 256 weights */
+ JANAS_IQ2_XS = 17, /* 74 */
+ JANAS_IQ3_XXS = 18, /* 98 */
JANAS_IQ4_NL = 20,
JANAS_IQ3_S = 21, /* sizes per 256 weights: 8 blocks, 144 bytes */
+ JANAS_IQ2_S = 22, /* 82 bytes per 256 weights */
+ JANAS_IQ1_S = 19, /* 50 bytes per 256 weights */
+ JANAS_IQ1_M = 29, /* 56 */
JANAS_IQ4_XS = 23,
JANAS_MXFP4 = 39, /* sizes per 256 weights: 8 blocks, 136 bytes */
+ JANAS_Q2_0 = 42, /* 4 blocks of 64, 72 bytes */
/* Janas' own, outside the GGUF numbering: the high plane of a Q6_K row
cut into bit planes. Its block size counts that plane alone; the other
two are JANAS_Q6KP_PLANE bytes each and are passed separately. */
@@ -234,6 +348,27 @@ enum janas_qtype {
/* Bytes of one 256-weight block of the type, 0 if unsupported. */
size_t janas_qtype_block_size(int type);
+/*
+ * A row of n weights with n no multiple of JANAS_QK, as the 640 of
+ * Qwen3.8-Flash-Next's experts: only in the types stored in blocks of 32 or
+ * 64, its bytes the n / JANAS_QK whole groups, then the tail's blocks. Its
+ * product is the whole groups' as janas_dot gives it, plus the tail's as
+ * janas_dot gives the tail padded with zero blocks to a whole group (no
+ * whole group: the tail's alone); the activations are (n + JANAS_QK - 1) /
+ * JANAS_QK Q8_K blocks, those past n zero. A row whose n is a multiple of
+ * JANAS_QK is janas_dot's, janas_dot_rows', janas_dot_multi's.
+ */
+/* The bytes of a row of n weights of the type, 0 if it cannot end at n. */
+size_t janas_qtype_row_bytes(int type, size_t n);
+float janas_dot_n(int type, const void *w, const struct janas_block_q8k *x,
+ size_t n);
+void janas_dot_rows_n(int type, const void *w, size_t row_bytes,
+ const struct janas_block_q8k *x, size_t n, size_t rows,
+ float *y);
+void janas_dot_multi_n(int type, const void *w, const struct janas_block_q8k *x,
+ size_t x_stride, size_t nv, size_t n, float *out,
+ size_t out_stride);
+
/*
* Dot product of nb blocks of a supported type with nb Q8_K blocks. Aborts on
* an unsupported type: check janas_qtype_block_size() first.
@@ -472,6 +607,59 @@ float janas_q4_1_dot(const struct janas_block_q4_1 *w,
const struct janas_block_q8k *x, size_t nb);
float janas_q4_1_dot_ref(const struct janas_block_q4_1 *w,
const struct janas_block_q8k *x, size_t nb);
+/* IQ2_XXS, IQ2_XS, IQ2_S: one exact integer sum per 256 weights, scales
+ included, then one fma, as IQ3_S; w is nb blocks of the type */
+void janas_iq2_dequantize(int type, const void *x, float *y, size_t n);
+float janas_iq2_dot(int type, const void *w, const struct janas_block_q8k *x,
+ size_t nb);
+float janas_iq2_dot_ref(int type, const void *w,
+ const struct janas_block_q8k *x, size_t nb);
+/* Q2_K: Q4_K's scheme, an exact sum for the scales and one for the mins,
+ then an fma each */
+void janas_q2k_dequantize(const struct janas_block_q2k *x, float *y, size_t n);
+float janas_q2k_dot(const struct janas_block_q2k *w,
+ const struct janas_block_q8k *x, size_t nb);
+float janas_q2k_dot_ref(const struct janas_block_q2k *w,
+ const struct janas_block_q8k *x, size_t nb);
+/* IQ1_M: (8 level + shift sign) times the half's 2 s + 1, one exact integer
+ sum per 256 weights, then one fma with d / 8 */
+void janas_iq1m_dequantize(const struct janas_block_iq1m *x, float *y,
+ size_t n);
+void janas_iq1s_dequantize(const struct janas_block_iq1s *x, float *y,
+ size_t n);
+float janas_iq1s_dot(const struct janas_block_iq1s *w,
+ const struct janas_block_q8k *x, size_t nb);
+float janas_iq1s_dot_ref(const struct janas_block_iq1s *w,
+ const struct janas_block_q8k *x, size_t nb);
+float janas_iq1m_dot(const struct janas_block_iq1m *w,
+ const struct janas_block_q8k *x, size_t nb);
+float janas_iq1m_dot_ref(const struct janas_block_iq1m *w,
+ const struct janas_block_q8k *x, size_t nb);
+/* IQ3_XXS: one exact integer sum per 256 weights, scales included, then
+ one fma, as IQ3_S */
+void janas_iq3xxs_dequantize(const struct janas_block_iq3xxs *x, float *y,
+ size_t n);
+float janas_iq3xxs_dot(const struct janas_block_iq3xxs *w,
+ const struct janas_block_q8k *x, size_t nb);
+float janas_iq3xxs_dot_ref(const struct janas_block_iq3xxs *w,
+ const struct janas_block_q8k *x, size_t nb);
+/* Q5_0 (nb groups of eight blocks): per block position the exact sum of
+ (q - 16) x a in a lane of its own, the eight added as janas_hsum8 does */
+void janas_q5_0_dequantize(const struct janas_block_q5_0 *x, float *y,
+ size_t n);
+float janas_q5_0_dot(const struct janas_block_q5_0 *w,
+ const struct janas_block_q8k *x, size_t nb);
+float janas_q5_0_dot_ref(const struct janas_block_q5_0 *w,
+ const struct janas_block_q8k *x, size_t nb);
+/* Q2_0 (nb groups of four blocks): a lane for each block of 32
+ activations, the exact sum of (q - 1) x a times d and their scale, the
+ eight added as janas_hsum8 does */
+void janas_q2_0_dequantize(const struct janas_block_q2_0 *x, float *y,
+ size_t n);
+float janas_q2_0_dot(const struct janas_block_q2_0 *w,
+ const struct janas_block_q8k *x, size_t nb);
+float janas_q2_0_dot_ref(const struct janas_block_q2_0 *w,
+ const struct janas_block_q8k *x, size_t nb);
float janas_q8_0_dot(const struct janas_block_q8_0 *w,
const struct janas_block_q8k *x, size_t nb);
float janas_q8_0_dot_ref(const struct janas_block_q8_0 *w,
diff --git a/src/llm/quant_dotn.h b/src/llm/quant_dotn.h
index e9a8f49..f80e7e6 100644
--- a/src/llm/quant_dotn.h
+++ b/src/llm/quant_dotn.h
@@ -1427,6 +1427,219 @@ static DOTN_TARGET float DOTN(q4_1_dot)(const struct janas_block_q4_1 *w,
return r;
}
+/*
+ * Q5_0 (see janas_q5_0_dot_ref in quant.c): Q5_1's unpacking, the values
+ * 0-31 unsigned for maddubs, then 16 times the activations' sum taken off:
+ * the exact sum of (q - 16) x a, one lane per block position.
+ */
+static inline DOTN_TARGET __attribute__((always_inline)) void
+DOTN(q5_0_dotn_body)(const struct janas_block_q5_0 *w,
+ const struct janas_block_q8k *x, size_t xs, size_t nb,
+ float *out, const int nv)
+{
+ const __m256i ones = _mm256_set1_epi16(1), low4 = _mm256_set1_epi8(0x0f);
+ /* as dm_dotn_body: byte l of a lane takes byte l / 8 of qh, then keeps
+ bit l % 8 of it */
+ const __m256i sel =
+ _mm256_setr_epi8(0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
+ 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3);
+ const __m256i bit = _mm256_set1_epi64x((long long)0x8040201008040201ull);
+ const __m256i fifth = _mm256_set1_epi8(0x10);
+ __m256 acc[8];
+ _Pragma("GCC unroll 8") for (int v = 0; v < nv; v++) acc[v] =
+ _mm256_setzero_ps();
+ for (size_t b = 0; b < nb; b++) {
+ if (nv <= 2)
+ DOTN(prefetch_ahead)(w + 8 * b, 8 * sizeof(*w));
+ const struct janas_block_q5_0 *wb = w + 8 * b;
+ __m256 dk = _mm256_cvtph_ps(_mm_setr_epi16(
+ (short)wb[0].d, (short)wb[1].d, (short)wb[2].d, (short)wb[3].d,
+ (short)wb[4].d, (short)wb[5].d, (short)wb[6].d, (short)wb[7].d));
+ __m256i q[8];
+ _Pragma("GCC unroll 8") for (int k = 0; k < 8; k++)
+ {
+ __m128i b16 = _mm_loadu_si128((const __m128i *)wb[k].qs);
+ uint32_t qh;
+ memcpy(&qh, wb[k].qh, 4);
+ __m256i h = _mm256_shuffle_epi8(_mm256_set1_epi32((int)qh), sel);
+ h = _mm256_and_si256(
+ _mm256_cmpeq_epi8(_mm256_and_si256(h, bit), bit), fifth);
+ q[k] = _mm256_or_si256(
+ _mm256_and_si256(_mm256_set_m128i(_mm_srli_epi16(b16, 4), b16),
+ low4),
+ h);
+ }
+ _Pragma("GCC unroll 8") for (int v = 0; v < nv; v++)
+ {
+ const struct janas_block_q8k *xb = x + (size_t)v * xs + b;
+ __m256i s[8];
+ _Pragma("GCC unroll 8") for (int k = 0; k < 8; k++)
+ {
+ __m256i a =
+ _mm256_loadu_si256((const __m256i *)(xb->qs32 + 32 * k));
+ s[k] = _mm256_madd_epi16(_mm256_maddubs_epi16(q[k], a), ones);
+ }
+ /* bsums 2k and 2k + 1 are block k's 32 activations */
+ __m256i as8 = _mm256_madd_epi16(
+ _mm256_loadu_si256((const __m256i *)xb->bsums32), ones);
+ __m256i is8 =
+ _mm256_sub_epi32(DOTN(sum8x8)(s), _mm256_slli_epi32(as8, 4));
+ acc[v] =
+ _mm256_fmadd_ps(_mm256_mul_ps(dk, _mm256_loadu_ps(xb->d32)),
+ _mm256_cvtepi32_ps(is8), acc[v]);
+ }
+ }
+ _Pragma("GCC unroll 8") for (int v = 0; v < nv; v++) out[v] =
+ DOTN(hsum8_ps)(acc[v]);
+}
+
+/* The 64 two-bit values of a Q2_0 block in their order, 0-3: the four
+ planes of qs (byte i of plane j is weight 4i + j) interleaved, eight bits
+ then sixteen. lo holds weights 0-31, hi 32-63. */
+static inline DOTN_TARGET __attribute__((always_inline)) void
+DOTN(q2_0_unpack)(const uint8_t *qs, __m256i *lo, __m256i *hi)
+{
+ const __m128i m3 = _mm_set1_epi8(3);
+ __m128i v = _mm_loadu_si128((const __m128i *)qs);
+ __m128i p0 = _mm_and_si128(v, m3);
+ __m128i p1 = _mm_and_si128(_mm_srli_epi16(v, 2), m3);
+ __m128i p2 = _mm_and_si128(_mm_srli_epi16(v, 4), m3);
+ __m128i p3 = _mm_and_si128(_mm_srli_epi16(v, 6), m3);
+ __m128i a = _mm_unpacklo_epi8(p0, p1), b = _mm_unpackhi_epi8(p0, p1);
+ __m128i c = _mm_unpacklo_epi8(p2, p3), d = _mm_unpackhi_epi8(p2, p3);
+ *lo = _mm256_set_m128i(_mm_unpackhi_epi16(a, c), _mm_unpacklo_epi16(a, c));
+ *hi = _mm256_set_m128i(_mm_unpackhi_epi16(b, d), _mm_unpacklo_epi16(b, d));
+}
+
+/*
+ * Q2_0 (see janas_q2_0_dot_ref in quant.c): the values unsigned for
+ * maddubs, then the activations' sum taken off: the exact sum of (q - 1) x
+ * a for each block of 32 activations, one lane each, a block's d in its two
+ * lanes.
+ */
+static inline DOTN_TARGET __attribute__((always_inline)) void
+DOTN(q2_0_dotn_body)(const struct janas_block_q2_0 *w,
+ const struct janas_block_q8k *x, size_t xs, size_t nb,
+ float *out, const int nv)
+{
+ const __m256i ones = _mm256_set1_epi16(1);
+ __m256 acc[8];
+ _Pragma("GCC unroll 8") for (int v = 0; v < nv; v++) acc[v] =
+ _mm256_setzero_ps();
+ for (size_t b = 0; b < nb; b++) {
+ if (nv <= 2)
+ DOTN(prefetch_ahead)(w + 4 * b, 4 * sizeof(*w));
+ const struct janas_block_q2_0 *wb = w + 4 * b;
+ __m256 dk = _mm256_cvtph_ps(_mm_setr_epi16(
+ (short)wb[0].d, (short)wb[0].d, (short)wb[1].d, (short)wb[1].d,
+ (short)wb[2].d, (short)wb[2].d, (short)wb[3].d, (short)wb[3].d));
+ __m256i q[8];
+ _Pragma("GCC unroll 4") for (int k = 0; k < 4; k++)
+ DOTN(q2_0_unpack)(wb[k].qs, &q[2 * k], &q[2 * k + 1]);
+ _Pragma("GCC unroll 8") for (int v = 0; v < nv; v++)
+ {
+ const struct janas_block_q8k *xb = x + (size_t)v * xs + b;
+ __m256i s[8];
+ _Pragma("GCC unroll 8") for (int k = 0; k < 8; k++)
+ {
+ __m256i a =
+ _mm256_loadu_si256((const __m256i *)(xb->qs32 + 32 * k));
+ s[k] = _mm256_madd_epi16(_mm256_maddubs_epi16(q[k], a), ones);
+ }
+ __m256i as8 = _mm256_madd_epi16(
+ _mm256_loadu_si256((const __m256i *)xb->bsums32), ones);
+ __m256i is8 = _mm256_sub_epi32(DOTN(sum8x8)(s), as8);
+ acc[v] =
+ _mm256_fmadd_ps(_mm256_mul_ps(dk, _mm256_loadu_ps(xb->d32)),
+ _mm256_cvtepi32_ps(is8), acc[v]);
+ }
+ }
+ _Pragma("GCC unroll 8") for (int v = 0; v < nv; v++) out[v] =
+ DOTN(hsum8_ps)(acc[v]);
+}
+
+static DOTN_TARGET void DOTN(q5_0_dotn)(const struct janas_block_q5_0 *w,
+ const struct janas_block_q8k *x,
+ size_t xs, int nv, size_t nb,
+ float *out)
+{
+ switch (nv) {
+ case 8:
+ DOTN(q5_0_dotn_body)(w, x, xs, nb, out, 8);
+ break;
+ case 7:
+ DOTN(q5_0_dotn_body)(w, x, xs, nb, out, 7);
+ break;
+ case 6:
+ DOTN(q5_0_dotn_body)(w, x, xs, nb, out, 6);
+ break;
+ case 5:
+ DOTN(q5_0_dotn_body)(w, x, xs, nb, out, 5);
+ break;
+ case 4:
+ DOTN(q5_0_dotn_body)(w, x, xs, nb, out, 4);
+ break;
+ case 3:
+ DOTN(q5_0_dotn_body)(w, x, xs, nb, out, 3);
+ break;
+ case 2:
+ DOTN(q5_0_dotn_body)(w, x, xs, nb, out, 2);
+ break;
+ default:
+ DOTN(q5_0_dotn_body)(w, x, xs, nb, out, 1);
+ }
+}
+
+static DOTN_TARGET float DOTN(q5_0_dot)(const struct janas_block_q5_0 *w,
+ const struct janas_block_q8k *x,
+ size_t nb)
+{
+ float r;
+ DOTN(q5_0_dotn_body)(w, x, 0, nb, &r, 1);
+ return r;
+}
+
+static DOTN_TARGET void DOTN(q2_0_dotn)(const struct janas_block_q2_0 *w,
+ const struct janas_block_q8k *x,
+ size_t xs, int nv, size_t nb,
+ float *out)
+{
+ switch (nv) {
+ case 8:
+ DOTN(q2_0_dotn_body)(w, x, xs, nb, out, 8);
+ break;
+ case 7:
+ DOTN(q2_0_dotn_body)(w, x, xs, nb, out, 7);
+ break;
+ case 6:
+ DOTN(q2_0_dotn_body)(w, x, xs, nb, out, 6);
+ break;
+ case 5:
+ DOTN(q2_0_dotn_body)(w, x, xs, nb, out, 5);
+ break;
+ case 4:
+ DOTN(q2_0_dotn_body)(w, x, xs, nb, out, 4);
+ break;
+ case 3:
+ DOTN(q2_0_dotn_body)(w, x, xs, nb, out, 3);
+ break;
+ case 2:
+ DOTN(q2_0_dotn_body)(w, x, xs, nb, out, 2);
+ break;
+ default:
+ DOTN(q2_0_dotn_body)(w, x, xs, nb, out, 1);
+ }
+}
+
+static DOTN_TARGET float DOTN(q2_0_dot)(const struct janas_block_q2_0 *w,
+ const struct janas_block_q8k *x,
+ size_t nb)
+{
+ float r;
+ DOTN(q2_0_dotn_body)(w, x, 0, nb, &r, 1);
+ return r;
+}
+
/* The 16 scales of a Q3_K block, -32..31, as q3k_scales() gives them,
unpacked in a register: the low and high nibbles of the first eight
bytes, and the two bits of the last four each group takes. */
@@ -1617,6 +1830,473 @@ DOTN(iq3s_dotn_body)(const struct janas_block_iq3s *w,
_Pragma("GCC unroll 8") for (int v = 0; v < nv; v++) out[v] = acc[v];
}
+/* IQ2_XXS, IQ2_XS, IQ2_S: a group's four grid entries in a register, its
+ four sign bytes as IQ3_S's, one scale (IQ2_XXS) or one for each half */
+static inline DOTN_TARGET __attribute__((always_inline)) void
+DOTN(iq2_dotn_body)(const int type, const void *w,
+ const struct janas_block_q8k *x, size_t xs, size_t nb,
+ float *out, const int nv)
+{
+ const __m256i sel =
+ _mm256_setr_epi8(0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
+ 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3);
+ const __m256i bit = _mm256_set1_epi64x((long long)0x8040201008040201ull);
+ const __m256i one = _mm256_set1_epi8(1);
+ const size_t size = iq2_size(type);
+ float acc[8] = {0};
+ for (size_t b = 0; b < nb; b++) {
+ const uint8_t *blk = (const uint8_t *)w + b * size;
+ if (nv <= 2)
+ DOTN(prefetch_ahead)(blk, size);
+ __m256i lv[8], sg[8], sc[8];
+ _Pragma("GCC unroll 8") for (int g = 0; g < 8; g++)
+ {
+ uint64_t gr[4];
+ uint32_t sb = 0;
+ int sl[4];
+ for (int k = 0; k < 4; k++) {
+ uint32_t s8;
+ iq2_sub(type, blk, 4 * g + k, &gr[k], &s8, &sl[k]);
+ sb |= s8 << 8 * k;
+ }
+ lv[g] = _mm256_setr_epi64x((long long)gr[0], (long long)gr[1],
+ (long long)gr[2], (long long)gr[3]);
+ __m256i m = _mm256_shuffle_epi8(_mm256_set1_epi32((int)sb), sel);
+ m = _mm256_cmpeq_epi8(_mm256_and_si256(m, bit), bit);
+ sg[g] = _mm256_or_si256(m, one); /* -1 where the sign bit is set */
+ /* the products' lanes 0-7 are weights 0-15, sub-groups 0 and 1 */
+ sc[g] = _mm256_set_m128i(_mm_set1_epi16((short)sl[2]),
+ _mm_set1_epi16((short)sl[0]));
+ }
+ uint16_t h;
+ memcpy(&h, blk, 2);
+ float d = _cvtsh_ss(h) * 0.125f;
+ _Pragma("GCC unroll 8") for (int v = 0; v < nv; v++)
+ {
+ const struct janas_block_q8k *xb = x + (size_t)v * xs + b;
+ __m256i sum = _mm256_setzero_si256();
+ _Pragma("GCC unroll 8") for (int g = 0; g < 8; g++)
+ {
+ __m256i a =
+ _mm256_loadu_si256((const __m256i *)(xb->qs + 32 * g));
+ __m256i p =
+ _mm256_maddubs_epi16(lv[g], _mm256_sign_epi8(a, sg[g]));
+ sum = DOTN_ACC(sum, p, sc[g]);
+ }
+ acc[v] = fmaf(d * xb->d, (float)DOTN(hsum_epi32)(sum), acc[v]);
+ }
+ }
+ _Pragma("GCC unroll 8") for (int v = 0; v < nv; v++) out[v] = acc[v];
+}
+
+static DOTN_TARGET void DOTN(iq2_dotn)(int type, const void *w,
+ const struct janas_block_q8k *x,
+ size_t xs, int nv, size_t nb, float *out)
+{
+ switch (nv) {
+ case 8:
+ if (type == JANAS_IQ2_XXS)
+ DOTN(iq2_dotn_body)(JANAS_IQ2_XXS, w, x, xs, nb, out, 8);
+ else if (type == JANAS_IQ2_XS)
+ DOTN(iq2_dotn_body)(JANAS_IQ2_XS, w, x, xs, nb, out, 8);
+ else
+ DOTN(iq2_dotn_body)(JANAS_IQ2_S, w, x, xs, nb, out, 8);
+ break;
+ case 7:
+ if (type == JANAS_IQ2_XXS)
+ DOTN(iq2_dotn_body)(JANAS_IQ2_XXS, w, x, xs, nb, out, 7);
+ else if (type == JANAS_IQ2_XS)
+ DOTN(iq2_dotn_body)(JANAS_IQ2_XS, w, x, xs, nb, out, 7);
+ else
+ DOTN(iq2_dotn_body)(JANAS_IQ2_S, w, x, xs, nb, out, 7);
+ break;
+ case 6:
+ if (type == JANAS_IQ2_XXS)
+ DOTN(iq2_dotn_body)(JANAS_IQ2_XXS, w, x, xs, nb, out, 6);
+ else if (type == JANAS_IQ2_XS)
+ DOTN(iq2_dotn_body)(JANAS_IQ2_XS, w, x, xs, nb, out, 6);
+ else
+ DOTN(iq2_dotn_body)(JANAS_IQ2_S, w, x, xs, nb, out, 6);
+ break;
+ case 5:
+ if (type == JANAS_IQ2_XXS)
+ DOTN(iq2_dotn_body)(JANAS_IQ2_XXS, w, x, xs, nb, out, 5);
+ else if (type == JANAS_IQ2_XS)
+ DOTN(iq2_dotn_body)(JANAS_IQ2_XS, w, x, xs, nb, out, 5);
+ else
+ DOTN(iq2_dotn_body)(JANAS_IQ2_S, w, x, xs, nb, out, 5);
+ break;
+ case 4:
+ if (type == JANAS_IQ2_XXS)
+ DOTN(iq2_dotn_body)(JANAS_IQ2_XXS, w, x, xs, nb, out, 4);
+ else if (type == JANAS_IQ2_XS)
+ DOTN(iq2_dotn_body)(JANAS_IQ2_XS, w, x, xs, nb, out, 4);
+ else
+ DOTN(iq2_dotn_body)(JANAS_IQ2_S, w, x, xs, nb, out, 4);
+ break;
+ case 3:
+ if (type == JANAS_IQ2_XXS)
+ DOTN(iq2_dotn_body)(JANAS_IQ2_XXS, w, x, xs, nb, out, 3);
+ else if (type == JANAS_IQ2_XS)
+ DOTN(iq2_dotn_body)(JANAS_IQ2_XS, w, x, xs, nb, out, 3);
+ else
+ DOTN(iq2_dotn_body)(JANAS_IQ2_S, w, x, xs, nb, out, 3);
+ break;
+ case 2:
+ if (type == JANAS_IQ2_XXS)
+ DOTN(iq2_dotn_body)(JANAS_IQ2_XXS, w, x, xs, nb, out, 2);
+ else if (type == JANAS_IQ2_XS)
+ DOTN(iq2_dotn_body)(JANAS_IQ2_XS, w, x, xs, nb, out, 2);
+ else
+ DOTN(iq2_dotn_body)(JANAS_IQ2_S, w, x, xs, nb, out, 2);
+ break;
+ default:
+ if (type == JANAS_IQ2_XXS)
+ DOTN(iq2_dotn_body)(JANAS_IQ2_XXS, w, x, xs, nb, out, 1);
+ else if (type == JANAS_IQ2_XS)
+ DOTN(iq2_dotn_body)(JANAS_IQ2_XS, w, x, xs, nb, out, 1);
+ else
+ DOTN(iq2_dotn_body)(JANAS_IQ2_S, w, x, xs, nb, out, 1);
+ }
+}
+
+static DOTN_TARGET float DOTN(iq2_dot)(int type, const void *w,
+ const struct janas_block_q8k *x,
+ size_t nb)
+{
+ float r;
+ DOTN(iq2_dotn)(type, w, x, 0, 1, nb, &r);
+ return r;
+}
+
+/* IQ1_M and IQ1_S (iq1s): a group's four grid entries as eight times
+ their levels plus the shifts' signs, signed for maddubs as Q8_0's, one
+ scale for each half (IQ1_S's two the same) */
+static inline DOTN_TARGET __attribute__((always_inline)) void
+DOTN(iq1_dotn_body)(const int iq1s, const void *w,
+ const struct janas_block_q8k *x, size_t xs, size_t nb,
+ float *out, const int nv)
+{
+ const struct janas_block_iq1m *wm = w;
+ const struct janas_block_iq1s *ws = w;
+ const __m256i top5 = _mm256_set1_epi8((char)0xf8);
+ float acc[8] = {0};
+ for (size_t b = 0; b < nb; b++) {
+ if (nv <= 2)
+ DOTN(prefetch_ahead)(iq1s ? (const void *)&ws[b]
+ : (const void *)&wm[b],
+ iq1s ? sizeof(ws[b]) : sizeof(wm[b]));
+ __m256i lv[8], al[8], sc[8];
+ _Pragma("GCC unroll 8") for (int g = 0; g < 8; g++)
+ {
+ uint64_t gr[4], sh[4];
+ int sl[4];
+ for (int k = 0; k < 4; k++) {
+ int neg;
+ if (iq1s)
+ iq1s_sub(&ws[b], 4 * g + k, &gr[k], &neg, &sl[k]);
+ else
+ iq1m_sub(&wm[b], 4 * g + k, &gr[k], &neg, &sl[k]);
+ sh[k] = neg ? 0xffffffffffffffffull : 0x0101010101010101ull;
+ }
+ __m256i lev =
+ _mm256_setr_epi64x((long long)gr[0], (long long)gr[1],
+ (long long)gr[2], (long long)gr[3]);
+ /* 8 level, byte by byte: a shift of the 16-bit lanes by three,
+ the bits it brought from the neighbouring byte masked off */
+ __m256i v8 = _mm256_and_si256(_mm256_slli_epi16(lev, 3), top5);
+ lv[g] = _mm256_add_epi8(
+ v8, _mm256_setr_epi64x((long long)sh[0], (long long)sh[1],
+ (long long)sh[2], (long long)sh[3]));
+ al[g] = _mm256_abs_epi8(lv[g]);
+ sc[g] = _mm256_set_m128i(_mm_set1_epi16((short)sl[2]),
+ _mm_set1_epi16((short)sl[0]));
+ }
+ float d = (iq1s ? _cvtsh_ss(ws[b].d) : iq1m_d(&wm[b])) * 0.125f;
+ _Pragma("GCC unroll 8") for (int v = 0; v < nv; v++)
+ {
+ const struct janas_block_q8k *xb = x + (size_t)v * xs + b;
+ __m256i sum = _mm256_setzero_si256();
+ _Pragma("GCC unroll 8") for (int g = 0; g < 8; g++)
+ {
+ __m256i a =
+ _mm256_loadu_si256((const __m256i *)(xb->qs + 32 * g));
+ __m256i p =
+ _mm256_maddubs_epi16(al[g], _mm256_sign_epi8(a, lv[g]));
+ sum = DOTN_ACC(sum, p, sc[g]);
+ }
+ acc[v] = fmaf(d * xb->d, (float)DOTN(hsum_epi32)(sum), acc[v]);
+ }
+ }
+ _Pragma("GCC unroll 8") for (int v = 0; v < nv; v++) out[v] = acc[v];
+}
+
+static DOTN_TARGET void DOTN(iq1m_dotn)(const struct janas_block_iq1m *w,
+ const struct janas_block_q8k *x,
+ size_t xs, int nv, size_t nb,
+ float *out)
+{
+ switch (nv) {
+ case 8:
+ DOTN(iq1_dotn_body)(0, w, x, xs, nb, out, 8);
+ break;
+ case 7:
+ DOTN(iq1_dotn_body)(0, w, x, xs, nb, out, 7);
+ break;
+ case 6:
+ DOTN(iq1_dotn_body)(0, w, x, xs, nb, out, 6);
+ break;
+ case 5:
+ DOTN(iq1_dotn_body)(0, w, x, xs, nb, out, 5);
+ break;
+ case 4:
+ DOTN(iq1_dotn_body)(0, w, x, xs, nb, out, 4);
+ break;
+ case 3:
+ DOTN(iq1_dotn_body)(0, w, x, xs, nb, out, 3);
+ break;
+ case 2:
+ DOTN(iq1_dotn_body)(0, w, x, xs, nb, out, 2);
+ break;
+ default:
+ DOTN(iq1_dotn_body)(0, w, x, xs, nb, out, 1);
+ }
+}
+
+static DOTN_TARGET float DOTN(iq1m_dot)(const struct janas_block_iq1m *w,
+ const struct janas_block_q8k *x,
+ size_t nb)
+{
+ float r;
+ DOTN(iq1_dotn_body)(0, w, x, 0, nb, &r, 1);
+ return r;
+}
+
+static DOTN_TARGET void DOTN(iq1s_dotn)(const struct janas_block_iq1s *w,
+ const struct janas_block_q8k *x,
+ size_t xs, int nv, size_t nb,
+ float *out)
+{
+ switch (nv) {
+ case 8:
+ DOTN(iq1_dotn_body)(1, w, x, xs, nb, out, 8);
+ break;
+ case 7:
+ DOTN(iq1_dotn_body)(1, w, x, xs, nb, out, 7);
+ break;
+ case 6:
+ DOTN(iq1_dotn_body)(1, w, x, xs, nb, out, 6);
+ break;
+ case 5:
+ DOTN(iq1_dotn_body)(1, w, x, xs, nb, out, 5);
+ break;
+ case 4:
+ DOTN(iq1_dotn_body)(1, w, x, xs, nb, out, 4);
+ break;
+ case 3:
+ DOTN(iq1_dotn_body)(1, w, x, xs, nb, out, 3);
+ break;
+ case 2:
+ DOTN(iq1_dotn_body)(1, w, x, xs, nb, out, 2);
+ break;
+ default:
+ DOTN(iq1_dotn_body)(1, w, x, xs, nb, out, 1);
+ }
+}
+
+static DOTN_TARGET float DOTN(iq1s_dot)(const struct janas_block_iq1s *w,
+ const struct janas_block_q8k *x,
+ size_t nb)
+{
+ float r;
+ DOTN(iq1_dotn_body)(1, w, x, 0, nb, &r, 1);
+ return r;
+}
+
+/* Q2_K: each half of the block's 32 bytes shifted by 0, 2, 4, 6 gives its
+ weights in order, 32 at a time; the sixteen scales against the exact
+ sums of each group of 16, the mins against the activations' sums, then
+ Q4_K's two fmas */
+static inline DOTN_TARGET __attribute__((always_inline)) void
+DOTN(q2k_dotn_body)(const struct janas_block_q2k *w,
+ const struct janas_block_q8k *x, size_t xs, size_t nb,
+ float *out, const int nv)
+{
+ const __m256i three = _mm256_set1_epi8(3);
+ float acc[8] = {0}, accm[8] = {0};
+ for (size_t b = 0; b < nb; b++) {
+ if (nv <= 2)
+ DOTN(prefetch_ahead)(&w[b], sizeof(w[b]));
+ __m256i q[8], sc[8];
+ _Pragma("GCC unroll 2") for (int n = 0; n < 2; n++)
+ {
+ __m256i bytes =
+ _mm256_loadu_si256((const __m256i *)(w[b].qs + 32 * n));
+ _Pragma("GCC unroll 4") for (int j = 0; j < 4; j++)
+ {
+ q[4 * n + j] = _mm256_and_si256(
+ _mm256_srli_epi16(bytes, (int)(2 * j)), three);
+ int g = 8 * n + 2 * j;
+ sc[4 * n + j] = _mm256_set_m128i(
+ _mm_set1_epi16((short)(w[b].scales[g + 1] & 15)),
+ _mm_set1_epi16((short)(w[b].scales[g] & 15)));
+ }
+ }
+ __m128i m8 = _mm_loadu_si128((const __m128i *)w[b].scales);
+ __m256i mins = _mm256_cvtepu8_epi16(
+ _mm_srli_epi16(_mm_and_si128(m8, _mm_set1_epi8((char)0xf0)), 4));
+ float d = _cvtsh_ss(w[b].d), dmin = _cvtsh_ss(w[b].dmin);
+ _Pragma("GCC unroll 8") for (int v = 0; v < nv; v++)
+ {
+ const struct janas_block_q8k *xb = x + (size_t)v * xs + b;
+ __m256i sum = _mm256_setzero_si256();
+ _Pragma("GCC unroll 8") for (int k = 0; k < 8; k++)
+ {
+ __m256i a =
+ _mm256_loadu_si256((const __m256i *)(xb->qs + 32 * k));
+ sum = DOTN_ACC(sum, _mm256_maddubs_epi16(q[k], a), sc[k]);
+ }
+ __m256i mn = _mm256_madd_epi16(
+ mins, _mm256_loadu_si256((const __m256i *)xb->bsums));
+ acc[v] = fmaf(d * xb->d, (float)DOTN(hsum_epi32)(sum), acc[v]);
+ accm[v] = fmaf(dmin * xb->d, (float)DOTN(hsum_epi32)(mn), accm[v]);
+ }
+ }
+ _Pragma("GCC unroll 8") for (int v = 0; v < nv; v++) out[v] =
+ acc[v] - accm[v];
+}
+
+static DOTN_TARGET void DOTN(q2k_dotn)(const struct janas_block_q2k *w,
+ const struct janas_block_q8k *x,
+ size_t xs, int nv, size_t nb, float *out)
+{
+ switch (nv) {
+ case 8:
+ DOTN(q2k_dotn_body)(w, x, xs, nb, out, 8);
+ break;
+ case 7:
+ DOTN(q2k_dotn_body)(w, x, xs, nb, out, 7);
+ break;
+ case 6:
+ DOTN(q2k_dotn_body)(w, x, xs, nb, out, 6);
+ break;
+ case 5:
+ DOTN(q2k_dotn_body)(w, x, xs, nb, out, 5);
+ break;
+ case 4:
+ DOTN(q2k_dotn_body)(w, x, xs, nb, out, 4);
+ break;
+ case 3:
+ DOTN(q2k_dotn_body)(w, x, xs, nb, out, 3);
+ break;
+ case 2:
+ DOTN(q2k_dotn_body)(w, x, xs, nb, out, 2);
+ break;
+ default:
+ DOTN(q2k_dotn_body)(w, x, xs, nb, out, 1);
+ }
+}
+
+static DOTN_TARGET float DOTN(q2k_dot)(const struct janas_block_q2k *w,
+ const struct janas_block_q8k *x,
+ size_t nb)
+{
+ float r;
+ DOTN(q2k_dotn_body)(w, x, 0, nb, &r, 1);
+ return r;
+}
+
+/* IQ3_XXS: IQ3_S's products, its indices of eight bits and its signs and
+ scales in a word per group */
+static inline DOTN_TARGET __attribute__((always_inline)) void
+DOTN(iq3xxs_dotn_body)(const struct janas_block_iq3xxs *w,
+ const struct janas_block_q8k *x, size_t xs, size_t nb,
+ float *out, const int nv)
+{
+ const __m256i sel =
+ _mm256_setr_epi8(0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
+ 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3);
+ const __m256i bit = _mm256_set1_epi64x((long long)0x8040201008040201ull);
+ const __m256i one = _mm256_set1_epi8(1);
+ float acc[8] = {0};
+ for (size_t b = 0; b < nb; b++) {
+ if (nv <= 2)
+ DOTN(prefetch_ahead)(&w[b], sizeof(w[b]));
+ __m256i lv[8], sg[8], sc[8];
+ _Pragma("GCC unroll 8") for (int g = 0; g < 8; g++)
+ {
+ __m128i q8 = _mm_loadl_epi64((const __m128i *)(w[b].qs + 8 * g));
+ lv[g] = _mm256_i32gather_epi32((const int *)iq3xxs_grid,
+ _mm256_cvtepu8_epi32(q8), 4);
+ uint32_t aux = iq3xxs_aux(&w[b], g);
+ uint32_t sb = iq_signs(aux & 127) | iq_signs(aux >> 7 & 127) << 8 |
+ iq_signs(aux >> 14 & 127) << 16 |
+ iq_signs(aux >> 21 & 127) << 24;
+ __m256i m = _mm256_shuffle_epi8(_mm256_set1_epi32((int)sb), sel);
+ m = _mm256_cmpeq_epi8(_mm256_and_si256(m, bit), bit);
+ sg[g] = _mm256_or_si256(m, one); /* -1 where the sign bit is set */
+ sc[g] = _mm256_set1_epi16((short)(1 + 2 * (aux >> 28)));
+ }
+ float d = _cvtsh_ss(w[b].d) * 0.25f;
+ _Pragma("GCC unroll 8") for (int v = 0; v < nv; v++)
+ {
+ const struct janas_block_q8k *xb = x + (size_t)v * xs + b;
+ __m256i sum = _mm256_setzero_si256();
+ _Pragma("GCC unroll 8") for (int g = 0; g < 8; g++)
+ {
+ __m256i a =
+ _mm256_loadu_si256((const __m256i *)(xb->qs + 32 * g));
+ __m256i p =
+ _mm256_maddubs_epi16(lv[g], _mm256_sign_epi8(a, sg[g]));
+ sum = DOTN_ACC(sum, p, sc[g]);
+ }
+ acc[v] = fmaf(d * xb->d, (float)DOTN(hsum_epi32)(sum), acc[v]);
+ }
+ }
+ _Pragma("GCC unroll 8") for (int v = 0; v < nv; v++) out[v] = acc[v];
+}
+
+static DOTN_TARGET void DOTN(iq3xxs_dotn)(const struct janas_block_iq3xxs *w,
+ const struct janas_block_q8k *x,
+ size_t xs, int nv, size_t nb,
+ float *out)
+{
+ switch (nv) {
+ case 8:
+ DOTN(iq3xxs_dotn_body)(w, x, xs, nb, out, 8);
+ break;
+ case 7:
+ DOTN(iq3xxs_dotn_body)(w, x, xs, nb, out, 7);
+ break;
+ case 6:
+ DOTN(iq3xxs_dotn_body)(w, x, xs, nb, out, 6);
+ break;
+ case 5:
+ DOTN(iq3xxs_dotn_body)(w, x, xs, nb, out, 5);
+ break;
+ case 4:
+ DOTN(iq3xxs_dotn_body)(w, x, xs, nb, out, 4);
+ break;
+ case 3:
+ DOTN(iq3xxs_dotn_body)(w, x, xs, nb, out, 3);
+ break;
+ case 2:
+ DOTN(iq3xxs_dotn_body)(w, x, xs, nb, out, 2);
+ break;
+ default:
+ DOTN(iq3xxs_dotn_body)(w, x, xs, nb, out, 1);
+ }
+}
+
+static DOTN_TARGET float DOTN(iq3xxs_dot)(const struct janas_block_iq3xxs *w,
+ const struct janas_block_q8k *x,
+ size_t nb)
+{
+ float r;
+ DOTN(iq3xxs_dotn_body)(w, x, 0, nb, &r, 1);
+ return r;
+}
+
static DOTN_TARGET void DOTN(iq3s_dotn)(const struct janas_block_iq3s *w,
const struct janas_block_q8k *x,
size_t xs, int nv, size_t nb,
@@ -1686,6 +2366,12 @@ DOTN_ROWS(iq4xs, struct janas_block_iq4xs)
DOTN_ROWS(q4_0, struct janas_block_q4_0)
DOTN_ROWS(q5_1, struct janas_block_q5_1)
DOTN_ROWS(q4_1, struct janas_block_q4_1)
+DOTN_ROWS(q5_0, struct janas_block_q5_0)
+DOTN_ROWS(q2_0, struct janas_block_q2_0)
+DOTN_ROWS(iq3xxs, struct janas_block_iq3xxs)
+DOTN_ROWS(iq1m, struct janas_block_iq1m)
+DOTN_ROWS(iq1s, struct janas_block_iq1s)
+DOTN_ROWS(q2k, struct janas_block_q2k)
DOTN_ROWS(q3k, struct janas_block_q3k)
DOTN_ROWS(iq3s, struct janas_block_iq3s)
#undef DOTN_ROWS
diff --git a/src/llm/shaders/attn_decode.comp b/src/llm/shaders/attn_decode.comp
index 4a8cc9b..60fdea3 100644
--- a/src/llm/shaders/attn_decode.comp
+++ b/src/llm/shaders/attn_decode.comp
@@ -13,8 +13,11 @@
// - m) (and the value's scale), written to part at (h nch + c)(hd + 2):
// the hd sums, then m and l. attn_merge.comp joins the runs. Workgroup x =
// kh nch + c; the runs past the token's position leave at once, so that
-// the dispatch recorded once serves every position. The GPU's own order of
-// the sums: not the CPU's bits (attention.c), the same result to rounding.
+// the dispatch recorded once serves every position. With list set
+// (qwen4exp's sparse attention, ix_select.comp) token j reads the positions
+// of row j of list (ls words: their count, then the positions) instead of
+// 0 .. pos + j, run c the c-th C of them. The GPU's own order of the sums:
+// not the CPU's bits (attention.c), the same result to rounding.
#extension GL_EXT_buffer_reference : require
#extension GL_EXT_buffer_reference_uvec2 : require
#extension GL_KHR_shader_subgroup_basic : require
@@ -25,8 +28,10 @@ layout(local_size_x = 256) in;
layout(constant_id = 2) const uint G = 4; /* query heads a KV head */
const uint C = 128u; /* positions a run: gpu_token.c ATT_RUN */
-const uint MAXQ = 2048u; /* G x hd */
-const uint MAXS = 1024u; /* G x C */
+/* G x hd and G x C: 2048 and 1024, or G x 256 and G x 128 where G is
+ larger than 8 (Qwen3.8-Flash-Next's 12; gpu_token.c att_maxq) */
+const uint MAXQ = G * 256u > 2048u ? G * 256u : 2048u;
+const uint MAXS = G * C > 1024u ? G * C : 1024u;
layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer R { float v[]; };
layout(buffer_reference, std430, buffer_reference_align = 16) readonly buffer B4 { uvec4 v[]; };
@@ -40,6 +45,8 @@ layout(push_constant) uniform P {
float scale;
uint qs, ps; /* floats from a token's qkv, and its runs, to the next's */
uvec2 pos; /* the token's position, read at run time: npos = pos + 1 */
+ uvec2 list;
+ uint ls;
} p;
shared float sq[MAXQ]; /* the G query heads */
@@ -48,6 +55,7 @@ shared float r8[MAXS / 8u]; /* the maxima of eight scores */
shared float r8s[MAXS / 8u]; /* and the sums of eight weights */
shared uint sqw[MAXQ / 4u]; /* the query heads at eight bits, four a word */
shared float sqs[16]; /* and their scales */
+shared uint spos[C]; /* the run's positions in the cache */
int byte_of(uint w, uint i) { return bitfieldExtract(int(w), int(8u * i), 8); }
@@ -59,10 +67,15 @@ void main()
uint kh = gl_WorkGroupID.x / p.nch, c = gl_WorkGroupID.x % p.nch;
/* token j of a block (workgroup y): positions 0 .. pos + j */
uint j0 = gl_WorkGroupID.y;
- uint npos = RP(p.pos).v[0] + j0 + 1u, p0 = c * C;
+ bool lst = p.list.x != 0u || p.list.y != 0u;
+ RP LP = RP(p.list);
+ uint npos = lst ? LP.v[j0 * p.ls] : RP(p.pos).v[0] + j0 + 1u, p0 = c * C;
if (p0 >= npos)
return; /* the whole workgroup: past the token */
uint n = min(C, npos - p0);
+ /* each of the run's positions, as an index of the cache's rows */
+ for (uint k = t; k < C; k += 256u)
+ spos[k] = kh * p.cap + (lst ? LP.v[j0 * p.ls + 1u + p0 + min(k, n - 1u)] : p0 + min(k, n - 1u));
R Q = R(p.q), KS = R(p.ks), VS = R(p.vs);
RP QW = RP(p.q);
B4 KC = B4(p.kc), VC = B4(p.vc); /* 16 bytes a load: hd % 16 == 0 */
@@ -74,13 +87,12 @@ void main()
if (t < G)
sqs[t] = Q.v[qo + t * hd + nw];
barrier();
- uint base = kh * p.cap + p0; /* the run's first position */
/* scores: four threads a position (256 threads: C / 64 rounds), each
a quarter of the key for all G heads at once - the key read once -
the quarters added across the cluster */
for (uint r0 = 0; r0 < C; r0 += 64u) {
uint j = r0 + t / 4u, qq = t % 4u, jj = min(j, n - 1u);
- uint w0 = (base + jj) * hd / 16u;
+ uint w0 = spos[jj] * hd / 16u;
int dg[G];
for (uint g = 0; g < G; g++)
dg[g] = 0;
@@ -94,7 +106,7 @@ void main()
dotPacked4x8EXT(int(sqw[o + 3u]), int(k4.w));
}
}
- float ksc = KS.v[base + jj] * p.scale;
+ float ksc = KS.v[spos[jj]] * p.scale;
for (uint g = 0; g < G; g++) {
int d = subgroupClusteredAdd(dg[g], 4u);
if (qq == 0u && j < n)
@@ -124,7 +136,7 @@ void main()
m = max(m, r8[g * (C / 8u) + q]);
float e = on ? exp(sc[k] - m) : 0.0;
if (in_g)
- sc[k] = on ? e * VS.v[base + j] : 0.0;
+ sc[k] = on ? e * VS.v[spos[j]] : 0.0;
float ce = subgroupClusteredAdd(e, 8u);
if (t % 8u == 0u && in_g)
r8s[k / 8u] = ce;
@@ -149,7 +161,7 @@ void main()
for (uint k = 0; k < G * 8u; k++)
a[k] = 0.0;
for (uint j = slo + 4u * shi; j < n; j += 4u * SH) {
- uvec2 v2 = VC2.v[((base + j) * hd + d0) / 8u];
+ uvec2 v2 = VC2.v[(spos[j] * hd + d0) / 8u];
for (uint k = 0; k < 8u; k++) {
float vb = float(byte_of(k < 4u ? v2.x : v2.y, k % 4u));
for (uint g = 0; g < G; g++)
@@ -172,7 +184,7 @@ void main()
for (uint k = 0; k < 16u; k++)
a[k] = 0.0;
for (uint j = sl_; j < n; j += SH) {
- uvec4 v4 = VC.v[((base + j) * hd + d0) / 16u];
+ uvec4 v4 = VC.v[(spos[j] * hd + d0) / 16u];
float wj = sc[g * C + j];
for (uint k = 0; k < 16u; k++)
a[k] += wj * float(byte_of(comp(v4, k / 4u), k % 4u));
diff --git a/src/llm/shaders/attn_merge.comp b/src/llm/shaders/attn_merge.comp
index 40ae7d9..2d26021 100644
--- a/src/llm/shaders/attn_merge.comp
+++ b/src/llm/shaders/attn_merge.comp
@@ -29,6 +29,8 @@ layout(push_constant) uniform P {
uint gs; /* the gate's floats a token */
uvec2 pos;
uvec2 gate;
+ uvec2 list; /* attn_decode.comp's, if set: the positions' count */
+ uint ls;
} p;
shared float m8[32]; /* the block's maxima, eight values each */
@@ -56,7 +58,9 @@ void main()
U Q = U(p.q256), Q32 = U(p.q32);
/* token j of a block (workgroup y): positions 0 .. pos + j */
uint j0 = gl_WorkGroupID.y;
- uint runs = (RP(p.pos).v[0] + j0 + C) / C; /* npos / C, rounded up */
+ bool lst = set(p.list);
+ uint npos = lst ? RP(p.list).v[j0 * p.ls] : RP(p.pos).v[0] + j0 + 1u;
+ uint runs = (npos + C - 1u) / C; /* rounded up */
R PR = R(p.part);
uint e = 256u * b + t, h = e / hd, i = e % hd;
uint at = j0 * p.ps + h * p.nch * (hd + 2u);
diff --git a/src/llm/shaders/b32_matvec.comp b/src/llm/shaders/b32_matvec.comp
index 32566e0..aa1d6b6 100644
--- a/src/llm/shaders/b32_matvec.comp
+++ b/src/llm/shaders/b32_matvec.comp
@@ -6,13 +6,22 @@
// levels), 2 Q4_1 and 3 Q5_1 (d q + m, Q5_1 with a fifth bit per weight in
// qh), 4 MXFP4 (17 bytes a block: its E8M0 byte, the power of two
// 2^(e - 128) for a scale, then the nibbles, through MXFP4's table of 0 to
-// 12 and their negatives). As the CPU computes them (nib_dot_ref,
-// janas_mxfp4_dot_ref and dm_dot_ref in quant.c):
-// each block's integer dot in one fma into the accumulator of its position
-// in the group of eight, the minimum of Q4_1/Q5_1 (m times the block's
-// activation sum) in accumulators of its own, the eight added at the end as
-// janas_hsum8 does - the same float operations, so the results agree bit
-// for bit. Q4_0's q - 8 is folded as the exact integer sum q.a - 8 sum(a).
+// 12 and their negatives), 5 Q5_0 (22 bytes: d, qh, the nibbles; q - 16),
+// 6 Q2_0 (18 bytes for 64 weights: d, then two bits a weight, weight i in
+// bits 2 (i % 4) of byte i / 4; q - 1: each half of a block one block of
+// 32 here, with the block's d). As the CPU computes them (nib_dot_ref,
+// janas_mxfp4_dot_ref, dm_dot_ref, janas_q5_0_dot_ref and
+// janas_q2_0_dot_ref in quant.c): each block's integer dot in one fma into
+// the accumulator of its position in the group of eight, the minimum of
+// Q4_1/Q5_1 (m times the block's activation sum) in accumulators of its
+// own, the eight added at the end as janas_hsum8 does - the same float
+// operations, so the results agree bit for bit. Q4_0's q - 8, Q5_0's q - 16
+// and Q2_0's q - 1 are folded as the exact integer sum q.a - c sum(a).
+// A row may end inside a group of 256 (tail, blocks of 32 past the whole
+// groups: qwen4exp's widths of 640): those blocks are summed apart, from
+// zero, and added to the whole groups' sum, as janas_dot_rows_n sums the
+// group the CPU pads with empty blocks; the row is read no further than it
+// goes, rows of whole words (gpu.c janas_gpu_can).
// The activations come in the 32-block layout (20 uvec4 a Q8_K block: qs32
// in 16, bsums32 in 2, d32 in 2), shared across the subgroup as in
// q4k_matvec.comp. The low nibbles of a block's 16 bytes are weights 0-15,
@@ -31,17 +40,31 @@ layout(constant_id = 0) const uint NV = 8;
layout(constant_id = 1) const uint SG = 8;
layout(constant_id = 2) const uint KIND = 0;
const uint NX = (20u + SG - 1u) / SG;
-/* bytes of a block, and of a group of eight in words */
-const uint S = KIND == 4u ? 17u : KIND == 3u ? 24u : KIND == 2u ? 20u : 18u;
+/* bytes of a block of 32 weights (Q2_0: half a block), and of a group of
+ eight in words */
+const uint S = KIND == 6u ? 9u
+ : KIND == 5u ? 22u
+ : KIND == 4u ? 17u
+ : KIND == 3u ? 24u
+ : KIND == 2u ? 20u
+ : 18u;
const uint GW = 2u * S;
const bool DM = KIND == 2u || KIND == 3u;
const bool TAB = KIND == 1u || KIND == 4u; /* levels through a table */
+const bool FIVE = KIND == 3u || KIND == 5u; /* a fifth bit in qh */
+/* the constant folded out of the weights' integer dot */
+const int OFF = KIND == 0u ? 8 : KIND == 5u ? 16 : KIND == 6u ? 1 : 0;
layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer U1 { uint v[]; };
layout(buffer_reference, std430, buffer_reference_align = 16) readonly buffer U4 { uvec4 v[]; };
layout(buffer_reference, std430, buffer_reference_align = 4) writeonly buffer F { float v[]; };
-layout(push_constant) uniform P { uvec2 w, x, y; uint rows, nb, nv, xs, ys; } p;
+layout(push_constant) uniform P {
+ uvec2 w, x, y;
+ uint rows, nb, nv, xs, ys, level;
+ uvec2 p1, p0;
+ uint tail; /* blocks of 32 past the whole groups, 0 to 7 */
+} p;
const int IQ4[16] = int[16](-127, -104, -83, -65, -49, -35, -22, -10,
1, 13, 25, 38, 53, 69, 89, 113);
@@ -63,6 +86,13 @@ uint spread5(uint b)
return ((b & 1u) << 4) | ((b & 2u) << 11) | ((b & 4u) << 18) | ((b & 8u) << 25);
}
+/* a byte of Q2_0 (four weights of two bits) spread to a byte each */
+uint spread2(uint b)
+{
+ uint t = (b | (b << 12)) & 0x000f000fu;
+ return (t | (t << 6)) & 0x03030303u;
+}
+
/* the table in the workgroup's shared memory: a constant array read at a
varying index is far slower on some GPUs (iq4xs_matvec.comp) */
shared uint T[16];
@@ -79,6 +109,12 @@ float hsum8(float a[8])
return ((a[0] + a[4]) + (a[2] + a[6])) + ((a[1] + a[5]) + (a[3] + a[7]));
}
+/* the word of wg at byte b, realigned by the half word */
+uint hword(uint wg[48], uint b)
+{
+ return (b & 2u) != 0u ? (wg[b >> 2] >> 16) | (wg[(b >> 2) + 1u] << 16) : wg[b >> 2];
+}
+
void main()
{
if (gl_LocalInvocationID.x < 16u)
@@ -98,41 +134,61 @@ void main()
uint xo[NV];
for (uint v = 0; v < NV; v++)
xo[v] = min(v0 + v, p.nv - 1u) * p.xs * 20u;
- float acc[NV][8], accm[NV][8];
- for (uint v = 0; v < NV; v++)
+ float acc[NV][8], accm[NV][8], res[NV];
+ for (uint v = 0; v < NV; v++) {
+ res[v] = 0.0;
for (uint k = 0; k < 8u; k++) {
acc[v][k] = 0.0;
accm[v][k] = 0.0;
}
- uint gw = row * p.nb * GW;
- for (uint b = 0; b < p.nb; b++, gw += GW) {
+ }
+ /* words of the tail: whole Q2_0 blocks there, two blocks of 32 each */
+ uint tw = (KIND == 6u ? p.tail / 2u * 18u : p.tail * S) / 4u;
+ uint gw = row * (p.nb * GW + tw);
+ /* the whole groups, then the tail (p.nb: past the last whole group) */
+ for (uint b = 0; b <= p.nb; b++, gw += GW) {
+ bool last = b == p.nb;
+ if (last && p.tail == 0u)
+ break;
+ uint nk = last ? p.tail : 8u, nw = last ? tw : GW;
uint wg[48];
for (uint i = 0; i < GW; i++)
- wg[i] = W.v[gw + i];
+ wg[i] = i < nw ? W.v[gw + i] : 0u;
float d[8], m[8];
uvec4 lo[8], hi[8];
for (uint k = 0; k < 8u; k++) {
- uint B = S * k; /* the block's first byte */
- uint hw = wg[B >> 2];
+ /* the block's first byte, and that of its 16 bytes of quants
+ (Q2_0: of the 8 of its half) */
+ uint B = KIND == 6u ? 18u * (k / 2u) : S * k;
+ uint qb = KIND == 6u ? B + 2u + 8u * (k % 2u) : B + S - 16u;
if (KIND == 4u) {
- d[k] = e8m0_half((hw >> (8u * (B & 3u))) & 0xffu);
+ d[k] = e8m0_half((wg[B >> 2] >> (8u * (B & 3u))) & 0xffu);
m[k] = 0.0;
} else {
- vec2 dm = unpackHalf2x16((B & 2u) != 0u ? (hw >> 16) | (wg[(B >> 2) + 1u] << 16) : hw);
+ vec2 dm = unpackHalf2x16(hword(wg, B));
d[k] = dm.x;
m[k] = dm.y;
}
- uint qb = B + S - 16u, wi = qb >> 2, sh = 8u * (qb & 3u);
+ uint wi = qb >> 2, sh = 8u * (qb & 3u);
+ if (KIND == 6u) {
+ uint q0 = sh != 0u ? (wg[wi] >> sh) | (wg[wi + 1u] << (32u - sh)) : wg[wi];
+ uint q1 = sh != 0u ? (wg[wi + 1u] >> sh) | (wg[wi + 2u] << (32u - sh)) : wg[wi + 1u];
+ lo[k] = uvec4(spread2(q0 & 0xffu), spread2((q0 >> 8) & 0xffu),
+ spread2((q0 >> 16) & 0xffu), spread2(q0 >> 24));
+ hi[k] = uvec4(spread2(q1 & 0xffu), spread2((q1 >> 8) & 0xffu),
+ spread2((q1 >> 16) & 0xffu), spread2(q1 >> 24));
+ continue;
+ }
uint q[4];
for (uint i = 0; i < 4u; i++)
q[i] = sh != 0u ? (wg[wi + i] >> sh) | (wg[wi + i + 1u] << (32u - sh)) : wg[wi + i];
- uint qh = KIND == 3u ? wg[(B + 4u) >> 2] : 0u;
+ uint qh = KIND == 3u ? wg[(B + 4u) >> 2] : KIND == 5u ? hword(wg, B + 2u) : 0u;
for (uint i = 0; i < 4u; i++) {
uint l = q[i] & 0x0f0f0f0fu, h = (q[i] >> 4) & 0x0f0f0f0fu;
if (TAB) {
l = iq4(l);
h = iq4(h);
- } else if (KIND == 3u) {
+ } else if (FIVE) {
l |= spread5((qh >> (4u * i)) & 15u);
h |= spread5((qh >> (16u + 4u * i)) & 15u);
}
@@ -143,6 +199,16 @@ void main()
if (i == 3u) { lo[k].w = l; hi[k].w = h; }
}
}
+ if (last)
+ /* the whole groups' sum aside, the tail's from zero */
+ for (uint v = 0; v < NV; v++) {
+ if (p.nb > 0u)
+ res[v] = DM ? hsum8(acc[v]) + hsum8(accm[v]) : hsum8(acc[v]);
+ for (uint k = 0; k < 8u; k++) {
+ acc[v][k] = 0.0;
+ accm[v][k] = 0.0;
+ }
+ }
for (uint v = 0; v < NV; v++) {
uvec4 xr[NX];
for (uint j = 0; j < NX; j++)
@@ -158,11 +224,14 @@ void main()
int dot = dp4(lo[k], bc(xr, 2u * k)) + dp4(hi[k], bc(xr, 2u * k + 1u));
/* the block's activation sum: bsums32 of its two halves */
int asum = bitfieldExtract(int(bw[k]), 0, 16) + bitfieldExtract(int(bw[k]), 16, 16);
- if (KIND == 0u)
- dot -= 8 * asum;
- acc[v][k] = fma(d[k] * d32[k], float(dot), acc[v][k]);
- if (DM)
- accm[v][k] = fma(m[k] * d32[k], float(asum), accm[v][k]);
+ if (OFF != 0)
+ dot -= OFF * asum;
+ /* the tail's empty blocks: the CPU's zero blocks add 0 */
+ if (k < nk) {
+ acc[v][k] = fma(d[k] * d32[k], float(dot), acc[v][k]);
+ if (DM)
+ accm[v][k] = fma(m[k] * d32[k], float(asum), accm[v][k]);
+ }
}
xo[v] += 20u;
}
@@ -173,6 +242,8 @@ void main()
float r = hsum8(acc[v]);
if (DM)
r = r + hsum8(accm[v]);
+ if (p.tail != 0u)
+ r = res[v] + r;
Y.v[(v0 + v) * p.ys + row] = r;
}
}
diff --git a/src/llm/shaders/b32_mmid.comp b/src/llm/shaders/b32_mmid.comp
new file mode 100644
index 0000000..53e52df
--- /dev/null
+++ b/src/llm/shaders/b32_mmid.comp
@@ -0,0 +1,196 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+#version 450
+// The fast prompt for a mixture's experts in blocks of 32 or 64 (gpu.c
+// experts_id): mxfp4_mmid.comp's table and tiles (64 rows by 16 vectors,
+// each invocation 2 rows by 4, groups of 32 staged 4 at a time), the
+// weights read as iq4nl_mmq.comp reads them, KIND as there: 0 IQ4_NL, 1
+// MXFP4, 2 Q5_0 (q - 16), 3 Q2_0 (each half of a block of 64 a group,
+// q - 1, the block's d); the activations in blocks of 32, each group with
+// its own scale. A row may end inside a group of 256 (qwen4exp's experts'
+// down matrices, 640 wide): ngroups groups of 32 a row, the stages past
+// its end empty, the activations nb blocks a vector (the last group whole,
+// zero past the row's end). NOT bit for bit with the CPU: rounded group by
+// group.
+#extension GL_EXT_control_flow_attributes : require
+#extension GL_EXT_integer_dot_product : require
+#extension GL_EXT_buffer_reference : require
+#extension GL_EXT_buffer_reference_uvec2 : require
+
+layout(local_size_x = 128) in;
+layout(constant_id = 0) const uint NV = 8; // unused: the table's constants
+layout(constant_id = 1) const uint SG = 8;
+layout(constant_id = 2) const uint KIND = 0;
+
+/* bytes a group of 32 weights (Q2_0: half a block) */
+const uint S = KIND == 3u ? 9u : KIND == 2u ? 22u : KIND == 1u ? 17u : 18u;
+const uint BM = 64u, BN = 16u, KS = 4u, XB = 20u;
+
+layout(buffer_reference, std430, buffer_reference_align = 16) readonly buffer U4 { uvec4 v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer U1 { uint v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) writeonly buffer F { float v[]; };
+
+// t: the table, 3 uvec4 an expert: (w, x), (y, nv, ys), (unused); then
+// from uvec4 tiles on, one a workgroup's z: (expert, first vector)
+layout(push_constant) uniform P { uvec2 t; uint rows, nb, tiles, ngroups; } p;
+
+struct ACache { int qs[8]; float d; }; // a group: its 32 levels, its scale
+struct BCache { int qs[8]; float d; }; // a group: quants, its d32
+shared ACache sa[BM * KS];
+shared BCache sb[BN * KS];
+
+const int IQ4[16] = int[16](-127, -104, -83, -65, -49, -35, -22, -10,
+ 1, 13, 25, 38, 53, 69, 89, 113);
+const int MX[16] = int[16](0, 1, 2, 3, 4, 6, 8, 12, 0, -1, -2, -3, -4, -6, -8, -12);
+
+/* the levels in the workgroup's shared memory: a constant array read at a
+ varying index is far slower on some GPUs (iq4xs_matvec.comp) */
+shared uint T[16];
+
+/* each byte of w (a nibble) through the table */
+uint lv(uint w)
+{
+ return T[w & 15u] | (T[(w >> 8) & 15u] << 8) | (T[(w >> 16) & 15u] << 16) |
+ (T[(w >> 24) & 15u] << 24);
+}
+
+// word i of the 32-bit words from byte off of w
+uint bword(U1 W, uint off, uint i)
+{
+ uint a = (off >> 2) + i, sh = 8u * (off & 3u);
+ return sh != 0u ? (W.v[a] >> sh) | (W.v[a + 1u] << (32u - sh)) : W.v[a];
+}
+
+/* four bits (weights 4j..4j+3) spread to bit 4 of each byte */
+uint spread5(uint b)
+{
+ return ((b & 1u) << 4) | ((b & 2u) << 11) | ((b & 4u) << 18) | ((b & 8u) << 25);
+}
+
+/* a byte of Q2_0 (four weights of two bits) spread to a byte each */
+uint spread2(uint b)
+{
+ uint t = (b | (b << 12)) & 0x000f000fu;
+ return (t | (t << 6)) & 0x03030303u;
+}
+
+/* c taken from each byte of v (bytes of 0 to 127, results as signed) */
+uint sub_bytes(uint v, uint c)
+{
+ return ((v | 0x80808080u) - c) ^ 0x80808080u;
+}
+
+/* the scale of the block at byte off */
+float scale(U1 W, uint off)
+{
+ uint w = bword(W, off, 0u);
+ return KIND == 1u ? uintBitsToFloat((w & 0xffu) < 2u ? 0x00200000u << (w & 0xffu)
+ : ((w & 0xffu) - 1u) << 23)
+ : unpackHalf2x16(w & 0xffffu).x;
+}
+
+void main()
+{
+ if (gl_LocalInvocationID.x < 16u)
+ T[gl_LocalInvocationID.x] =
+ uint(KIND == 1u ? MX[gl_LocalInvocationID.x] : IQ4[gl_LocalInvocationID.x]) & 0xffu;
+ barrier();
+ U4 TB = U4(p.t);
+ uvec4 tl = TB.v[p.tiles + gl_WorkGroupID.z];
+ uint e = tl.x;
+ uvec4 t0 = TB.v[3u * e], t1 = TB.v[3u * e + 1u];
+ uint nv = t1.z, ys = t1.w;
+ uint r0 = gl_WorkGroupID.x * BM, v0 = tl.y;
+ U4 X = U4(t0.zw);
+ U1 W1 = U1(t0.xy);
+ F Y = F(t1.xy);
+ uint id = gl_LocalInvocationID.x;
+ uint rq = (id % 32u) * 2u, vq = (id / 32u) * 4u;
+ float sums[8];
+ [[unroll]] for (uint i = 0; i < 8u; i++) sums[i] = 0.0;
+ uint ngroups = p.ngroups;
+ for (uint g0 = 0; g0 < ngroups; g0 += KS) {
+ // weights: 4 invocations a row, 2 words each of the group's 8
+ {
+ uint iqs = id % 4u, rl = id / 4u;
+ [[unroll]] for (uint l = 0; l < BM; l += 32u) {
+ uint r = min(r0 + rl + l, p.rows - 1u);
+ [[unroll]] for (uint k = 0; k < KS; k++) {
+ uint g = g0 + k;
+ if (g >= ngroups) { /* past the row's end: nothing */
+ [[unroll]] for (uint t = 0; t < 2u; t++)
+ sa[k * BM + rl + l].qs[2u * iqs + t] = 0;
+ if (iqs == 0u)
+ sa[k * BM + rl + l].d = 0.0;
+ continue;
+ }
+ uint off = (r * ngroups + g) * S;
+ if (KIND == 3u) {
+ /* the block's first byte, its half's quants */
+ uint bo = off - (g % 2u) * S, qo = bo + 2u + 8u * (g % 2u);
+ uint q = bword(W1, qo, iqs / 2u);
+ [[unroll]] for (uint t = 0; t < 2u; t++) {
+ uint kw = 2u * iqs + t;
+ sa[k * BM + rl + l].qs[kw] =
+ int(sub_bytes(spread2((q >> (8u * (kw % 4u))) & 0xffu), 0x01010101u));
+ }
+ if (iqs == 0u)
+ sa[k * BM + rl + l].d = scale(W1, bo);
+ continue;
+ }
+ uint qh = KIND == 2u ? bword(W1, off + 2u, 0u) : 0u;
+ [[unroll]] for (uint t = 0; t < 2u; t++) {
+ uint kw = 2u * iqs + t, q = bword(W1, off + S - 16u, kw & 3u);
+ uint nib = kw < 4u ? q & 0x0f0f0f0fu : (q >> 4) & 0x0f0f0f0fu;
+ if (KIND == 2u)
+ nib = sub_bytes(nib | spread5((qh >> (4u * kw)) & 15u), 0x10101010u);
+ else
+ nib = lv(nib);
+ sa[k * BM + rl + l].qs[kw] = int(nib);
+ }
+ if (iqs == 0u)
+ sa[k * BM + rl + l].d = scale(W1, off);
+ }
+ }
+ }
+ // activations: 2 invocations a vector, each half a group's quants
+ {
+ uint k = id / 32u, c = (id % 32u) / 2u, iqs = id % 2u;
+ uint v = min(v0 + c, nv - 1u);
+ uint g = min(g0 + k, ngroups - 1u), b = g / 8u, j = g % 8u;
+ uint xo = (v * p.nb + b) * XB;
+ uvec4 q = X.v[xo + 2u * j + iqs];
+ sb[k * BN + c].qs[4u * iqs] = int(q.x);
+ sb[k * BN + c].qs[4u * iqs + 1u] = int(q.y);
+ sb[k * BN + c].qs[4u * iqs + 2u] = int(q.z);
+ sb[k * BN + c].qs[4u * iqs + 3u] = int(q.w);
+ if (iqs == 0u) {
+ uvec4 dq = X.v[xo + 18u + j / 4u];
+ uint dw = j % 4u == 0u ? dq.x : j % 4u == 1u ? dq.y : j % 4u == 2u ? dq.z : dq.w;
+ sb[k * BN + c].d = uintBitsToFloat(dw);
+ }
+ }
+ barrier();
+ for (uint k = 0; k < KS; k++) {
+ ACache ca[2];
+ [[unroll]] for (uint r = 0; r < 2u; r++)
+ ca[r] = sa[k * BM + rq + r];
+ [[unroll]] for (uint c = 0; c < 4u; c++) {
+ BCache cb = sb[k * BN + vq + c];
+ [[unroll]] for (uint r = 0; r < 2u; r++) {
+ int q = 0;
+ [[unroll]] for (uint i = 0; i < 8u; i++)
+ q += dotPacked4x8EXT(ca[r].qs[i], cb.qs[i]);
+ sums[c * 2u + r] += ca[r].d * cb.d * float(q);
+ }
+ }
+ }
+ barrier();
+ }
+ [[unroll]] for (uint c = 0; c < 4u; c++)
+ [[unroll]] for (uint r = 0; r < 2u; r++) {
+ uint row = r0 + rq + r, v = v0 + vq + c;
+ if (row < p.rows && v < nv)
+ Y.v[v * ys + row] = sums[c * 2u + r];
+ }
+}
diff --git a/src/llm/shaders/bf16_matvec.comp b/src/llm/shaders/bf16_matvec.comp
new file mode 100644
index 0000000..c1ab753
--- /dev/null
+++ b/src/llm/shaders/bf16_matvec.comp
@@ -0,0 +1,80 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+#version 450
+// bf16 weights (rows x cols, row after row, two to a word) times NV float
+// vectors at a time, as the CPU computes them with AVX2 (bf16.h
+// janas_bf16_dot): eight lanes of a subgroup a row, lane l the fused
+// multiply-adds of the elements 8k + l in order; the eight added as
+// janas_hsum8 does through the subgroup - lane l with l ^ 4, then ^ 2,
+// then ^ 1, which gives ((a0 + a4) + (a2 + a6)) + ((a1 + a5) + (a3 + a7)),
+// additions that commute - the elements past the last eight multiplied and
+// added one at a time by the row's first lane, then the scale: the same
+// float operations as the CPU, so the same bits (gpu.c janas_gpu_bf16_mv).
+// precise keeps the compiler from fusing those multiplies and adds. The
+// subgroup a multiple of 8 lanes, a row's eight its own.
+#extension GL_KHR_shader_subgroup_basic : require
+#extension GL_KHR_shader_subgroup_shuffle : require
+#extension GL_EXT_buffer_reference : require
+#extension GL_EXT_buffer_reference_uvec2 : require
+
+layout(local_size_x = 64) in;
+layout(constant_id = 0) const uint NV = 4;
+layout(constant_id = 1) const uint SG = 8; // unused: the subgroup is asked
+
+layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer U1 { uint v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer F1 { float v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) writeonly buffer FO { float v[]; };
+
+// w: row r from element r * cols; x: vector v from float v * xs; y: row r
+// of vector v at y[v * ys + r]
+layout(push_constant) uniform P { uvec2 w, x, y; uint rows, cols, nv, xs, ys; float scale; } p;
+
+/* weight i, from its bf16 bits */
+float wt(U1 W, uint i)
+{
+ uint u = W.v[i >> 1];
+ return uintBitsToFloat((i & 1u) != 0u ? u & 0xffff0000u : u << 16);
+}
+
+void main()
+{
+ U1 W = U1(p.w);
+ F1 X = F1(p.x);
+ FO Y = FO(p.y);
+ /* the row's eight lanes: eight neighbours of the subgroup */
+ uint lane = gl_SubgroupInvocationID % 8u;
+ uint rl = (gl_SubgroupID * gl_SubgroupSize + gl_SubgroupInvocationID) / 8u;
+ uint grow = gl_WorkGroupID.y * (gl_WorkGroupSize.x / 8u) + rl;
+ uint row = min(grow, p.rows - 1u);
+ uint v0 = gl_WorkGroupID.x * NV;
+ uint wo = row * p.cols, n8 = p.cols / 8u * 8u;
+ uint xo[NV];
+ precise float acc[NV];
+ for (uint v = 0; v < NV; v++) {
+ xo[v] = min(v0 + v, p.nv - 1u) * p.xs;
+ acc[v] = 0.0;
+ }
+ for (uint i = lane; i < n8; i += 8u) {
+ float w = wt(W, wo + i);
+ for (uint v = 0; v < NV; v++)
+ acc[v] = fma(w, X.v[xo[v] + i], acc[v]);
+ }
+ /* every lane in the shuffles, then the first alone */
+ precise float s[NV];
+ for (uint v = 0; v < NV; v++) {
+ s[v] = acc[v] + subgroupShuffleXor(acc[v], 4u);
+ s[v] = s[v] + subgroupShuffleXor(s[v], 2u);
+ s[v] = s[v] + subgroupShuffleXor(s[v], 1u);
+ }
+ if (lane != 0u)
+ return;
+ for (uint v = 0; v < NV; v++) {
+ for (uint i = n8; i < p.cols; i++) {
+ precise float t = wt(W, wo + i) * X.v[xo[v] + i];
+ s[v] = s[v] + t;
+ }
+ precise float r = s[v] * p.scale;
+ if (grow < p.rows && v0 + v < p.nv)
+ Y.v[(v0 + v) * p.ys + row] = r;
+ }
+}
diff --git a/src/llm/shaders/dense_mm.comp b/src/llm/shaders/dense_mm.comp
index d3f41af..6d71b33 100644
--- a/src/llm/shaders/dense_mm.comp
+++ b/src/llm/shaders/dense_mm.comp
@@ -3,7 +3,10 @@
#version 450
// The fast prompt for weights left as they are, f32 (KIND 0) or bf16 (1):
// the per-layer branches of Gemma 4's E2B and E4B (gpu.c
-// janas_gpu_dense_mm). Tiles of 64 rows by 64 vectors, 16 columns staged
+// janas_gpu_dense_mm); or Q8_0 (2: blocks of 32, an fp16 scale and 32
+// int8, rows of cols / 32 blocks), on float vectors as the CPU's
+// janas_q8w_dot takes them (Qwen3.8-Flash-Next's mixes rewritten by
+// JANAS_HC_MIX=q8, gpu_token_hc.c). Tiles of 64 rows by 64 vectors, 16 columns staged
// per barrier, each invocation 4 rows by 4 vectors. y[v * ys + r] = scale x
// the dot product of row r with vector v (vectors cols floats apart). NOT
// bit for bit with the CPU: summed in another order.
@@ -33,6 +36,16 @@ float wt(uint r, uint k)
uint i = r * p.cols + k;
if (KIND == 0u)
return F1(p.w).v[i];
+ if (KIND == 2u) {
+ /* the block's first byte: its scale, then its quants */
+ uint b = (r * (p.cols / 32u) + k / 32u) * 34u;
+ U1 W = U1(p.w);
+ uint dw = W.v[b >> 2];
+ float d = unpackHalf2x16((b & 2u) != 0u ? dw >> 16 : dw & 0xffffu).x;
+ uint qb = b + 2u + k % 32u;
+ int q = bitfieldExtract(int(W.v[qb >> 2]), int(8u * (qb & 3u)), 8);
+ return d * float(q);
+ }
uint u = U1(p.w).v[i >> 1];
return uintBitsToFloat((i & 1u) != 0u ? u & 0xffff0000u : u << 16);
}
diff --git a/src/llm/shaders/gdn_gate.comp b/src/llm/shaders/gdn_gate.comp
index d6dd0b6..08698bd 100644
--- a/src/llm/shaders/gdn_gate.comp
+++ b/src/llm/shaders/gdn_gate.comp
@@ -24,7 +24,8 @@ layout(buffer_reference, std430, buffer_reference_align = 4) writeonly buffer W
layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer RP { uint v[]; };
// att, z: n rows of d floats; w: ds floats
-layout(push_constant) uniform P { uvec2 att, z, w, q256, q32, bd, lb, ld, pos; uint d, nv; float eps; } p;
+// sig: sigmoid(z) in place of SiLU(z) (qwen4exp)
+layout(push_constant) uniform P { uvec2 att, z, w, q256, q32, bd, lb, ld, pos; uint d, nv; float eps; uint sig; } p;
shared float sq_[256];
shared float hs_[2];
@@ -72,7 +73,7 @@ void main()
precise float sc = 1.0 / sqrt(hs_[t / 128u] / 128.0 + p.eps);
precise float vs = a * sc;
float z = F(p.z).v[o];
- float v = vs * F(p.w).v[e % 128u] * (z / (1.0 + exp(-z)));
+ float v = vs * F(p.w).v[e % 128u] * ((p.sig != 0u ? 1.0 : z) / (1.0 + exp(-z)));
/* the maxima of the groups of 32 and of the block */
float mc = subgroupClusteredMax(abs(v), 8u);
if (t % 8u == 0u)
diff --git a/src/llm/shaders/hc_act.comp b/src/llm/shaders/hc_act.comp
new file mode 100644
index 0000000..702ff08
--- /dev/null
+++ b/src/llm/shaders/hc_act.comp
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+#version 450
+// qwen4exp's mix, between its two low-rank products (model_hc.c hc_mix):
+// lo = SiLU(lo) in place and its bf16 rounding into lob (silu 1); or only
+// the rounding, x into xb (silu 0: the n-gram embeddings before their bf16
+// value product). n floats, a thread each.
+#extension GL_EXT_buffer_reference : require
+#extension GL_EXT_buffer_reference_uvec2 : require
+
+layout(local_size_x = 256) in;
+
+layout(buffer_reference, std430, buffer_reference_align = 4) buffer F { float v[]; };
+
+layout(push_constant) uniform P { uvec2 x, xb; uint n, silu; } p;
+
+float round_bf16(float f)
+{
+ uint u = floatBitsToUint(f);
+ if ((u & 0x7fffffffu) > 0x7f800000u)
+ return f; /* NaN */
+ u = (u + (0x7fffu + ((u >> 16) & 1u))) & 0xffff0000u;
+ return uintBitsToFloat(u);
+}
+
+void main()
+{
+ uint i = gl_GlobalInvocationID.x;
+ if (i >= p.n)
+ return;
+ F X = F(p.x);
+ float v = X.v[i];
+ if (p.silu != 0u) {
+ v = v / (1.0 + exp(-v));
+ X.v[i] = v;
+ }
+ F(p.xb).v[i] = round_bf16(v);
+}
diff --git a/src/llm/shaders/hc_comb.comp b/src/llm/shaders/hc_comb.comp
new file mode 100644
index 0000000..fb544d3
--- /dev/null
+++ b/src/llm/shaders/hc_comb.comp
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+#version 450
+// qwen4exp's streams after a sublayer (model_hc.c combine_worker): each of
+// a token's hc streams += the sublayer's output (out, d floats a token)
+// times 2 sigmoid(inject_c / hc) (inj: hc a token). With init 1, every
+// stream set to out instead (the embeddings, before the first layer). A
+// thread per number of the hc x d (x) and token (y).
+#extension GL_EXT_buffer_reference : require
+#extension GL_EXT_buffer_reference_uvec2 : require
+
+layout(local_size_x = 256) in;
+
+layout(buffer_reference, std430, buffer_reference_align = 4) buffer F { float v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer R { float v[]; };
+
+layout(push_constant) uniform P { uvec2 res, out_, inj; uint d, hc, init; } p;
+
+void main()
+{
+ uint e = gl_GlobalInvocationID.x, j = gl_WorkGroupID.y, hd = p.hc * p.d;
+ if (e >= hd)
+ return;
+ uint c = e / p.d, i = e % p.d;
+ float b = R(p.out_).v[j * p.d + i];
+ F S = F(p.res);
+ if (p.init != 0u) {
+ S.v[j * hd + e] = b;
+ return;
+ }
+ float w = 2.0 / (1.0 + exp(-R(p.inj).v[j * p.hc + c] / float(p.hc)));
+ S.v[j * hd + e] += b * w;
+}
diff --git a/src/llm/shaders/hc_mean.comp b/src/llm/shaders/hc_mean.comp
new file mode 100644
index 0000000..d5ff17b
--- /dev/null
+++ b/src/llm/shaders/hc_mean.comp
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+#version 450
+// qwen4exp's mix, its last step (model_hc.c mean_worker): the sublayer's
+// input, the mean over a token's hc streams of each normalized stream (xn)
+// times sigmoid of its gate (g), into out (d floats a token), for
+// norm_quant.comp to quantize. A thread per number (x) and token (y).
+#extension GL_EXT_buffer_reference : require
+#extension GL_EXT_buffer_reference_uvec2 : require
+
+layout(local_size_x = 256) in;
+
+layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer F { float v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) writeonly buffer W { float v[]; };
+
+// xn, g: n rows of hc x d floats; out: n rows of d
+layout(push_constant) uniform P { uvec2 xn, g, out_; uint d, hc; } p;
+
+void main()
+{
+ uint i = gl_GlobalInvocationID.x, j = gl_WorkGroupID.y;
+ if (i >= p.d)
+ return;
+ F XN = F(p.xn), G = F(p.g);
+ uint o = j * p.hc * p.d + i;
+ float s = 0.0;
+ for (uint c = 0u; c < p.hc; c++)
+ s += XN.v[o + c * p.d] / (1.0 + exp(-G.v[o + c * p.d]));
+ W(p.out_).v[j * p.d + i] = s / float(p.hc);
+}
diff --git a/src/llm/shaders/hc_norm.comp b/src/llm/shaders/hc_norm.comp
new file mode 100644
index 0000000..3450b39
--- /dev/null
+++ b/src/llm/shaders/hc_norm.comp
@@ -0,0 +1,65 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+#version 450
+// qwen4exp's mix, its first step (model_hc.c norm_worker): each of a
+// token's hc streams of d floats normalized by its RMS and its own weights
+// (w: hc x d), into xn, and the same rounded to bf16 (kept as floats, to
+// nearest, ties to even) into xb, the input of the mix's bf16 products. A
+// workgroup per stream (x) and token (y); the sum of squares in the GPU's
+// order, NOT bit for bit with the CPU.
+#extension GL_EXT_buffer_reference : require
+#extension GL_KHR_shader_subgroup_arithmetic : require
+#extension GL_KHR_shader_subgroup_basic : require
+#extension GL_EXT_buffer_reference_uvec2 : require
+
+layout(local_size_x = 256) in;
+
+layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer F { float v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) writeonly buffer W { float v[]; };
+
+// res, xn, xb: n rows of hc x d floats
+layout(push_constant) uniform P { uvec2 res, w, xn, xb; uint d, hc; float eps; } p;
+
+shared float sg_part[64];
+
+float wg_sum(float s)
+{
+ float r = subgroupAdd(s);
+ if (subgroupElect())
+ sg_part[gl_SubgroupID] = r;
+ barrier();
+ float tot = sg_part[0];
+ for (uint i = 1u; i < gl_NumSubgroups; i++)
+ tot += sg_part[i];
+ barrier();
+ return tot;
+}
+
+float round_bf16(float f)
+{
+ uint u = floatBitsToUint(f);
+ if ((u & 0x7fffffffu) > 0x7f800000u)
+ return f; /* NaN */
+ u = (u + (0x7fffu + ((u >> 16) & 1u))) & 0xffff0000u;
+ return uintBitsToFloat(u);
+}
+
+void main()
+{
+ uint t = gl_LocalInvocationID.x, c = gl_WorkGroupID.x, j = gl_WorkGroupID.y;
+ uint d = p.d, o = (j * p.hc + c) * d;
+ F X = F(p.res), Wt = F(p.w);
+ float ss = 0.0;
+ for (uint i = t; i < d; i += 256u) {
+ float v = X.v[o + i];
+ ss += v * v;
+ }
+ ss = wg_sum(ss);
+ precise float sc = 1.0 / sqrt(ss / float(d) + p.eps);
+ for (uint i = t; i < d; i += 256u) {
+ precise float v = X.v[o + i] * sc;
+ v = v * Wt.v[c * d + i];
+ W(p.xn).v[o + i] = v;
+ W(p.xb).v[o + i] = round_bf16(v);
+ }
+}
diff --git a/src/llm/shaders/iq1_matvec.comp b/src/llm/shaders/iq1_matvec.comp
new file mode 100644
index 0000000..42c204e
--- /dev/null
+++ b/src/llm/shaders/iq1_matvec.comp
@@ -0,0 +1,318 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri
+ The table IQ1S_GRID below is the IQ1 grid of ggml (ggml/src/ggml-common.h,
+ iq1s_grid), copied as data from src/llm/iq1s_grid.h - each entry's eight
+ levels of -1, 0 or 1 as two bits (the level plus one), an entry in 16
+ bits, two a word: it defines the formats. Copyright (c) 2023-2026 The
+ ggml authors, under the MIT licence (see the ggml repository).
+ Generated - not formatted by hand. */
+#version 450
+// IQ1 weights times up to NV Q8_K vectors, one row per invocation, as
+// janas_iq1s_dot_ref and janas_iq1m_dot_ref compute them, KIND chosen when
+// the pipeline is made: 0 IQ1_S (50 bytes a block: d, 32 grid indices'
+// low bytes, a 16-bit word a group of 32 - their top three bits each, the
+// scale, the shift's sign), 1 IQ1_M (56: the indices' low bytes, a byte a
+// pair of sub-groups - their top three bits and shifts' signs - and four
+// 16-bit words of scales of three bits each, whose top nibbles together
+// are d). A weight is 8 times its level plus or minus 1, -9 to 9, the
+// sub-group's integer dot times its scale 2 s + 1, the block's exact
+// integer sum in one fma with (d / 8) d8: the CPU's float operations, so
+// the results agree bit for bit. IQ1_S's blocks start on 2-byte
+// boundaries: their words are realigned. Activations shared across the
+// subgroup as in q4k_matvec.comp.
+#extension GL_EXT_integer_dot_product : require
+#extension GL_KHR_shader_subgroup_basic : require
+#extension GL_KHR_shader_subgroup_ballot : require
+#extension GL_EXT_buffer_reference : require
+#extension GL_EXT_buffer_reference_uvec2 : require
+
+layout(local_size_x = 64) in;
+layout(constant_id = 0) const uint NV = 8;
+layout(constant_id = 1) const uint SG = 8;
+layout(constant_id = 2) const uint KIND = 0;
+const uint NX = (19u + SG - 1u) / SG;
+const uint BS = KIND == 0u ? 50u : 56u, NW = (BS + 3u) / 4u + 1u;
+
+layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer U1 { uint v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 16) readonly buffer U4 { uvec4 v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) writeonly buffer F { float v[]; };
+
+layout(push_constant) uniform P { uvec2 w, x, y; uint rows, nb, nv, xs, ys; } p;
+
+const uint IQ1S_GRID[1024] = uint[1024](
+ 0x00020000u, 0x00080005u, 0x0011000au, 0x00200015u, 0x00280022u, 0x0045002au,
+ 0x00540051u, 0x00650056u, 0x00820080u, 0x008a0088u, 0x00a00095u, 0x00a800a2u,
+ 0x010400aau, 0x01110105u, 0x01160114u, 0x011a0119u, 0x01410125u, 0x01490146u,
+ 0x01550152u, 0x0161015au, 0x01660164u, 0x01850168u, 0x01940191u, 0x01a50196u,
+ 0x02020200u, 0x020a0208u, 0x02200215u, 0x02280222u, 0x0245022au, 0x02590251u,
+ 0x02690264u, 0x02820280u, 0x028a0288u, 0x02950291u, 0x02a00299u, 0x02a802a2u,
+ 0x041102aau, 0x04160414u, 0x04410425u, 0x04550449u, 0x0464045au, 0x04910465u,
+ 0x04a50499u, 0x05040501u, 0x05060505u, 0x05180515u, 0x0529051au, 0x05450540u,
+ 0x0550054au, 0x05540551u, 0x05560555u, 0x05600559u, 0x05650562u, 0x056a0568u,
+ 0x05910581u, 0x05980595u, 0x05a1059au, 0x05a505a4u, 0x05a905a6u, 0x06190614u,
+ 0x06440641u, 0x06520650u, 0x06580655u, 0x06610660u, 0x06690666u, 0x06910685u,
+ 0x06990694u, 0x08020800u, 0x080a0808u, 0x08200815u, 0x08280822u, 0x0845082au,
+ 0x08560851u, 0x08800865u, 0x08880882u, 0x0895088au, 0x08a208a0u, 0x08aa08a8u,
+ 0x09110905u, 0x09190914u, 0x09250924u, 0x09500941u, 0x09550951u, 0x09640961u,
+ 0x09910969u, 0x09960994u, 0x09a50999u, 0x0a020a00u, 0x0a0a0a08u, 0x0a200a15u,
+ 0x0a280a22u, 0x0a450a2au, 0x0a590a51u, 0x0a650a61u, 0x0a820a80u, 0x0a880a85u,
+ 0x0a950a8au, 0x0aa20aa0u, 0x0aaa0aa8u, 0x10111010u, 0x10191014u, 0x10251024u,
+ 0x10441041u, 0x10551050u, 0x10611058u, 0x10651064u, 0x10911069u, 0x10961094u,
+ 0x10a510a1u, 0x11041101u, 0x11091106u, 0x11121110u, 0x11181115u, 0x11241121u,
+ 0x11451129u, 0x1150114au, 0x11521151u, 0x11551154u, 0x11591156u, 0x11651160u,
+ 0x11921184u, 0x11a11195u, 0x121111a4u, 0x12161214u, 0x12401225u, 0x12491246u,
+ 0x12551252u, 0x125a1258u, 0x12661264u, 0x12911285u, 0x12961294u, 0x140112a5u,
+ 0x14091406u, 0x14151414u, 0x14191418u, 0x14261421u, 0x14451441u, 0x14481446u,
+ 0x1451144au, 0x14551454u, 0x14591456u, 0x14651462u, 0x14841468u, 0x14901489u,
+ 0x14951494u, 0x14991498u, 0x14a1149au, 0x14a514a4u, 0x150214a9u, 0x150a1505u,
+ 0x15141511u, 0x15161515u, 0x15201519u, 0x15251522u, 0x152a1528u, 0x15441541u,
+ 0x15461545u, 0x15521551u, 0x15551554u, 0x15591556u, 0x1561155au, 0x15651564u,
+ 0x15691566u, 0x15821580u, 0x15851584u, 0x158a1588u, 0x15911590u, 0x15951594u,
+ 0x15991596u, 0x15a0159au, 0x15a515a2u, 0x16041601u, 0x16061605u, 0x16161615u,
+ 0x161a1618u, 0x16261621u, 0x16421640u, 0x16451644u, 0x164a1648u, 0x16551651u,
+ 0x16581656u, 0x16611659u, 0x16651664u, 0x16691668u, 0x1686166au, 0x1692168au,
+ 0x16a41695u, 0x181116a9u, 0x18251816u, 0x18441841u, 0x18491846u, 0x18551850u,
+ 0x185a1858u, 0x18611860u, 0x18661864u, 0x18851869u, 0x18941891u, 0x191018a5u,
+ 0x19151912u, 0x1921191au, 0x19421925u, 0x19451944u, 0x19511948u, 0x19551954u,
+ 0x19591956u, 0x1960195au, 0x196a1965u, 0x19911989u, 0x19951992u, 0x19a11998u,
+ 0x19a919a6u, 0x1a161a09u, 0x1a261a24u, 0x1a461a44u, 0x1a501a49u, 0x1a551a52u,
+ 0x1a611a58u, 0x1a691a66u, 0x1a911a85u, 0x1a9a1a96u, 0x20022000u, 0x200a2008u,
+ 0x20202015u, 0x20252022u, 0x202a2028u, 0x20512045u, 0x20612059u, 0x20802065u,
+ 0x20882082u, 0x2095208au, 0x20a220a0u, 0x20a820a5u, 0x210520aau, 0x21142111u,
+ 0x21252119u, 0x21442142u, 0x21552149u, 0x215a2158u, 0x21642161u, 0x21662165u,
+ 0x21902185u, 0x21992196u, 0x220121a5u, 0x220a2208u, 0x22152211u, 0x22222220u,
+ 0x222a2228u, 0x22512245u, 0x22592256u, 0x22812265u, 0x228a2288u, 0x22952291u,
+ 0x22a222a0u, 0x22aa22a8u, 0x24142405u, 0x24192416u, 0x24442425u, 0x24462445u,
+ 0x24522449u, 0x24582455u, 0x2466245au, 0x24912485u, 0x24992494u, 0x24a524a1u,
+ 0x25152509u, 0x25292521u, 0x25452540u, 0x25512548u, 0x25552554u, 0x25622559u,
+ 0x25682565u, 0x25902589u, 0x25952594u, 0x259a2598u, 0x25a425a1u, 0x25a925a6u,
+ 0x26102605u, 0x26192612u, 0x26412625u, 0x26552649u, 0x26612660u, 0x26842669u,
+ 0x26902686u, 0x2800269au, 0x28082802u, 0x2815280au, 0x28222820u, 0x282a2828u,
+ 0x28512845u, 0x28652854u, 0x28822880u, 0x288a2888u, 0x28a228a0u, 0x28aa28a8u,
+ 0x29112909u, 0x29192914u, 0x29462925u, 0x29522949u, 0x29612955u, 0x29662964u,
+ 0x29852969u, 0x29962990u, 0x29a42999u, 0x2a0029a5u, 0x2a082a02u, 0x2a202a0au,
+ 0x2a282a22u, 0x2a452a2au, 0x2a562a51u, 0x2a652a59u, 0x2a822a80u, 0x2a8a2a88u,
+ 0x2aa02a95u, 0x2aa82aa2u, 0x40052aaau, 0x40164011u, 0x40494025u, 0x40554052u,
+ 0x405a4058u, 0x40644061u, 0x40944066u, 0x40a14099u, 0x410040a6u, 0x41044101u,
+ 0x41094106u, 0x41154112u, 0x41184116u, 0x4121411au, 0x41294126u, 0x41484145u,
+ 0x4151414au, 0x41554154u, 0x41594156u, 0x4165415au, 0x416a4168u, 0x41844181u,
+ 0x41904186u, 0x41954192u, 0x41a141a0u, 0x420541a2u, 0x42144211u, 0x42254216u,
+ 0x42524241u, 0x425a4255u, 0x42694264u, 0x42944289u, 0x440142a5u, 0x44194415u,
+ 0x44454429u, 0x444a4448u, 0x44544451u, 0x44564455u, 0x44624461u, 0x44684465u,
+ 0x4481446au, 0x44894486u, 0x44924490u, 0x44a04495u, 0x44a944a1u, 0x45024501u,
+ 0x450a4505u, 0x45144511u, 0x45164515u, 0x45204519u, 0x452a4525u, 0x45444541u,
+ 0x45464545u, 0x45504549u, 0x45544551u, 0x45564555u, 0x45594558u, 0x45644561u,
+ 0x45664565u, 0x45824569u, 0x45854584u, 0x45914588u, 0x45954594u, 0x45994596u,
+ 0x45a5459au, 0x45aa45a8u, 0x46054601u, 0x46144609u, 0x46184615u, 0x4621461au,
+ 0x46294624u, 0x46424640u, 0x46484645u, 0x46514650u, 0x46554652u, 0x46594656u,
+ 0x46654662u, 0x46814668u, 0x468a4685u, 0x46954694u, 0x46a446a1u, 0x480546a6u,
+ 0x48154811u, 0x4825481au, 0x48494842u, 0x48554850u, 0x48614858u, 0x48664864u,
+ 0x48854869u, 0x48944891u, 0x48994896u, 0x490148a5u, 0x49064905u, 0x4910490au,
+ 0x49154914u, 0x49214918u, 0x49264924u, 0x49454940u, 0x4951494au, 0x49544952u,
+ 0x49564955u, 0x49604959u, 0x49654962u, 0x496a4966u, 0x49894986u, 0x49954992u,
+ 0x49984996u, 0x49a449a1u, 0x49a949a6u, 0x4a444a16u, 0x4a494a46u, 0x4a584a55u,
+ 0x4a644a5au, 0x4a944a69u, 0x50014aa5u, 0x50055004u, 0x50095006u, 0x50155012u,
+ 0x5021501au, 0x50295024u, 0x50455040u, 0x50515048u, 0x50555054u, 0x50595056u,
+ 0x50685065u, 0x50895086u, 0x50985095u, 0x50a150a0u, 0x50a950a6u, 0x51085105u,
+ 0x510a5109u, 0x51145111u, 0x51165115u, 0x51195118u, 0x51255120u, 0x51285126u,
+ 0x5141512au, 0x51455144u, 0x51495146u, 0x51515150u, 0x51545152u, 0x51565155u,
+ 0x51595158u, 0x5161515au, 0x51655164u, 0x51695166u, 0x51855182u, 0x51945191u,
+ 0x51965195u, 0x51a05199u, 0x51aa51a5u, 0x52065201u, 0x52155212u, 0x5221521au,
+ 0x52425224u, 0x524a5245u, 0x52545251u, 0x52565255u, 0x52625259u, 0x52855265u,
+ 0x52925290u, 0x52995295u, 0x52a4529au, 0x54055404u, 0x54145411u, 0x54165415u,
+ 0x54195418u, 0x54255421u, 0x542a5428u, 0x54445441u, 0x54465445u, 0x544a5449u,
+ 0x54515450u, 0x54555454u, 0x54585456u, 0x545a5459u, 0x54625461u, 0x54655464u,
+ 0x54695466u, 0x54885480u, 0x5491548au, 0x54955494u, 0x54995496u, 0x54a454a1u,
+ 0x54aa54a5u, 0x55025501u, 0x55055504u, 0x55095506u, 0x55115510u, 0x55145512u,
+ 0x55165515u, 0x551a5519u, 0x55245521u, 0x55265525u, 0x55405529u, 0x55425541u,
+ 0x55455544u, 0x55485546u, 0x55505549u, 0x55525551u, 0x55555554u, 0x55585556u,
+ 0x555a5559u, 0x55615560u, 0x55655564u, 0x55685566u, 0x556a5569u, 0x55845581u,
+ 0x55895585u, 0x5590558au, 0x55945591u, 0x55965595u, 0x55995598u, 0x55a455a1u,
+ 0x55a655a5u, 0x560055a9u, 0x56025601u, 0x56065604u, 0x56095608u, 0x56145611u,
+ 0x56185615u, 0x56205619u, 0x56225621u, 0x56255624u, 0x56285626u, 0x56415629u,
+ 0x56465645u, 0x56495648u, 0x5650564au, 0x56525651u, 0x56555654u, 0x56585656u,
+ 0x565a5659u, 0x56645661u, 0x56695665u, 0x56855682u, 0x56885686u, 0x568a5689u,
+ 0x56955691u, 0x56a2569au, 0x56a656a5u, 0x56a956a8u, 0x58055804u, 0x58095806u,
+ 0x58155810u, 0x58215818u, 0x5845582au, 0x584a5848u, 0x58545851u, 0x58565855u,
+ 0x58595858u, 0x58625860u, 0x58655864u, 0x58895882u, 0x58925890u, 0x58985895u,
+ 0x58a958a1u, 0x59025901u, 0x590a5905u, 0x59145911u, 0x59165915u, 0x59255919u,
+ 0x59445941u, 0x59465945u, 0x59505949u, 0x59525951u, 0x59555954u, 0x59585956u,
+ 0x595a5959u, 0x59645961u, 0x59665965u, 0x59815969u, 0x59895985u, 0x59945991u,
+ 0x59965995u, 0x59995998u, 0x5a0459a5u, 0x5a155a08u, 0x5a205a1au, 0x5a265a25u,
+ 0x5a455a29u, 0x5a495a48u, 0x5a555a51u, 0x5a585a56u, 0x5a625a59u, 0x5a685a65u,
+ 0x5a815a6au, 0x5a925a8au, 0x5a965a95u, 0x5a9a5a98u, 0x60055aa1u, 0x60166014u,
+ 0x60256019u, 0x60506044u, 0x60566055u, 0x605a6058u, 0x60646061u, 0x60696066u,
+ 0x60966081u, 0x610160a5u, 0x61066104u, 0x61126109u, 0x61216115u, 0x61266122u,
+ 0x61456129u, 0x61516149u, 0x61566155u, 0x61656159u, 0x616a6166u, 0x618a6184u,
+ 0x61956192u, 0x61a661a1u, 0x621161a9u, 0x62196216u, 0x62416240u, 0x62556246u,
+ 0x62586256u, 0x62856260u, 0x62966291u, 0x641162a5u, 0x64156412u, 0x641a6416u,
+ 0x64266421u, 0x64406429u, 0x64456442u, 0x644a6448u, 0x64546451u, 0x64566455u,
+ 0x645a6459u, 0x64626460u, 0x64846465u, 0x64896485u, 0x64926490u, 0x64956494u,
+ 0x64986496u, 0x64a1649au, 0x64a964a4u, 0x65086505u, 0x6511650au, 0x65166515u,
+ 0x65446519u, 0x65466545u, 0x65506549u, 0x65546551u, 0x65566555u, 0x65616559u,
+ 0x65656564u, 0x65696566u, 0x65896586u, 0x6591658au, 0x65966595u, 0x659a6599u,
+ 0x65a565a2u, 0x65a865a6u, 0x66096602u, 0x66206615u, 0x66286626u, 0x66406629u,
+ 0x66486645u, 0x6651664au, 0x66556654u, 0x66586656u, 0x6660665au, 0x66686665u,
+ 0x66826680u, 0x668a6685u, 0x66966694u, 0x66996698u, 0x66a466a0u, 0x66aa66a6u,
+ 0x68196816u, 0x68416825u, 0x68556852u, 0x6861685au, 0x68856869u, 0x68986891u,
+ 0x690168a6u, 0x69106904u, 0x69216915u, 0x69266924u, 0x69406929u, 0x69456941u,
+ 0x69486946u, 0x69546951u, 0x69566955u, 0x69606959u, 0x696a6965u, 0x69846982u,
+ 0x6995698au, 0x69a469a1u, 0x69a969a5u, 0x6a166a11u, 0x6a416a18u, 0x6a496a44u,
+ 0x6a556a50u, 0x6a5a6a58u, 0x6a656a64u, 0x6a866a69u, 0x6a986a94u, 0x6aa66a9au,
+ 0x80028000u, 0x800a8008u, 0x80228020u, 0x802a8028u, 0x80508045u, 0x80548051u,
+ 0x80598056u, 0x80808065u, 0x80888082u, 0x8095808au, 0x80a280a0u, 0x80aa80a8u,
+ 0x81118105u, 0x81168114u, 0x81258119u, 0x81448141u, 0x81508149u, 0x81558152u,
+ 0x81588156u, 0x81648159u, 0x81698166u, 0x81898185u, 0x81968194u, 0x81a58199u,
+ 0x82028200u, 0x820a8208u, 0x82208215u, 0x82288222u, 0x8251822au, 0x82598254u,
+ 0x82808265u, 0x82888282u, 0x8295828au, 0x82a282a0u, 0x82aa82a8u, 0x84198414u,
+ 0x84448441u, 0x84558451u, 0x8461845au, 0x84698464u, 0x84998494u, 0x85098501u,
+ 0x85158512u, 0x8526851au, 0x85408529u, 0x85458541u, 0x85518548u, 0x85558554u,
+ 0x85598556u, 0x8565855au, 0x85688566u, 0x8581856au, 0x85868584u, 0x85908589u,
+ 0x85958592u, 0x85a68598u, 0x86168611u, 0x86258619u, 0x86448641u, 0x864a8649u,
+ 0x86558650u, 0x865a8659u, 0x86668661u, 0x8685866au, 0x869a8691u, 0x880086a4u,
+ 0x88088802u, 0x8815880au, 0x88228820u, 0x882a8828u, 0x88458841u, 0x88548851u,
+ 0x88658859u, 0x88808869u, 0x88888882u, 0x8895888au, 0x88a288a0u, 0x88aa88a8u,
+ 0x89068905u, 0x89148911u, 0x89258916u, 0x89448941u, 0x89498946u, 0x89528950u,
+ 0x895a8955u, 0x89648961u, 0x89968985u, 0x89a58999u, 0x8a028a00u, 0x8a0a8a08u,
+ 0x8a208a15u, 0x8a288a22u, 0x8a458a2au, 0x8a548a51u, 0x8a808a56u, 0x8a888a82u,
+ 0x8a958a8au, 0x8aa28aa0u, 0x8aaa8aa8u, 0x90119005u, 0x90189016u, 0x90259019u,
+ 0x90469041u, 0x90559049u, 0x905a9058u, 0x906a9069u, 0x90919085u, 0x90969094u,
+ 0x90a59099u, 0x91049101u, 0x91099106u, 0x91159110u, 0x911a9118u, 0x91249121u,
+ 0x91299126u, 0x91459140u, 0x91519150u, 0x91559154u, 0x91599156u, 0x91659162u,
+ 0x91869184u, 0x91959192u, 0x91a19198u, 0x91a691a4u, 0x920591a9u, 0x92149211u,
+ 0x92259219u, 0x92469244u, 0x92509249u, 0x92559252u, 0x92669258u, 0x92859269u,
+ 0x92969294u, 0x940192a9u, 0x94069404u, 0x94159410u, 0x94269418u, 0x944a9440u,
+ 0x94549451u, 0x94569455u, 0x94599458u, 0x94619460u, 0x94659462u, 0x94869484u,
+ 0x94949492u, 0x94989495u, 0x94a994a1u, 0x95059500u, 0x950a9508u, 0x95119510u,
+ 0x95159514u, 0x95199516u, 0x95259521u, 0x952a9529u, 0x95449541u, 0x95469545u,
+ 0x95509549u, 0x95529551u, 0x95559554u, 0x95589556u, 0x955a9559u, 0x95649561u,
+ 0x95669565u, 0x95819569u, 0x95889585u, 0x95929591u, 0x95959594u, 0x95999596u,
+ 0x95a0959au, 0x95a595a2u, 0x95aa95a8u, 0x96049601u, 0x96159610u, 0x96209619u,
+ 0x96299626u, 0x96489645u, 0x96519649u, 0x96559652u, 0x96599656u, 0x96689665u,
+ 0x96849682u, 0x968a9689u, 0x96949692u, 0x96a49695u, 0x96a996a6u, 0x98169805u,
+ 0x98259819u, 0x98469841u, 0x98529850u, 0x98569855u, 0x9864985au, 0x98859865u,
+ 0x98969891u, 0x98a59899u, 0x99069904u, 0x99109909u, 0x99159912u, 0x991a9918u,
+ 0x99219920u, 0x99269924u, 0x99429940u, 0x99489945u, 0x9951994au, 0x99559954u,
+ 0x99599956u, 0x99659962u, 0x996a9966u, 0x99849981u, 0x99929990u, 0x999a9995u,
+ 0x99a699a1u, 0x9a159a05u, 0x9a449a25u, 0x9a499a46u, 0x9a559a50u, 0x9a619a58u,
+ 0x9a919a85u, 0x9a959a94u, 0xa0009a96u, 0xa008a002u, 0xa015a00au, 0xa022a020u,
+ 0xa02aa028u, 0xa051a045u, 0xa056a054u, 0xa080a059u, 0xa088a082u, 0xa095a08au,
+ 0xa0a2a0a0u, 0xa0aaa0a8u, 0xa109a105u, 0xa114a111u, 0xa119a116u, 0xa146a11au,
+ 0xa151a149u, 0xa158a155u, 0xa161a15au, 0xa185a164u, 0xa192a190u, 0xa199a196u,
+ 0xa208a202u, 0xa210a20au, 0xa222a219u, 0xa22aa228u, 0xa251a245u, 0xa259a256u,
+ 0xa280a265u, 0xa288a282u, 0xa295a28au, 0xa2a2a2a0u, 0xa2aaa2a8u, 0xa425a419u,
+ 0xa444a441u, 0xa454a450u, 0xa458a455u, 0xa461a45au, 0xa466a465u, 0xa469a468u,
+ 0xa506a485u, 0xa510a509u, 0xa515a512u, 0xa526a518u, 0xa542a529u, 0xa551a545u,
+ 0xa555a554u, 0xa559a556u, 0xa56aa565u, 0xa584a581u, 0xa586a585u, 0xa592a589u,
+ 0xa598a595u, 0xa611a605u, 0xa61aa616u, 0xa625a621u, 0xa646a644u, 0xa652a64au,
+ 0xa656a655u, 0xa660a658u, 0xa686a662u, 0xa695a690u, 0xa699a696u, 0xa6a4a6a1u,
+ 0xa800a6a6u, 0xa808a802u, 0xa820a80au, 0xa828a822u, 0xa851a82au, 0xa856a854u,
+ 0xa880a859u, 0xa888a882u, 0xa895a88au, 0xa8a2a8a0u, 0xa8aaa8a8u, 0xa914a905u,
+ 0xa921a919u, 0xa941a925u, 0xa955a950u, 0xa961a95au, 0xa969a966u, 0xa996a990u,
+ 0xaa02aa00u, 0xaa0aaa08u, 0xaa22aa20u, 0xaa2aaa28u, 0xaa54aa51u, 0xaa80aa56u,
+ 0xaa88aa82u, 0xaa95aa8au, 0xaaa2aaa0u, 0xaaaaaaa8u);
+
+int dp(uint a, uint b) { return dotPacked4x8EXT(int(a), int(b)); }
+uvec4 bc(uvec4 xr[NX], uint k) { return subgroupBroadcast(xr[k / SG], k % SG); }
+
+uint byte_at(uint wd[NW], uint i) { return (wd[i >> 2] >> (8u * (i & 3u))) & 0xffu; }
+uint half_at(uint wd[NW], uint i) { return byte_at(wd, i) | (byte_at(wd, i + 1u) << 8); }
+
+/* the grid in the workgroup's shared memory (iq4xs_matvec.comp) */
+shared uint GS[1024];
+
+/* weights 4h..4h+3 of a sub-group, 8 level + delta, as signed bytes */
+uint weights4(uint idx, uint h, int delta)
+{
+ uint e = (GS[idx >> 1] >> (16u * (idx & 1u))) & 0xffffu, r = 0u;
+ for (uint i = 0; i < 4u; i++) {
+ int lv = int((e >> (2u * (4u * h + i))) & 3u) - 1;
+ r |= (uint(8 * lv + delta) & 0xffu) << (8u * i);
+ }
+ return r;
+}
+
+void main()
+{
+ for (uint i = gl_LocalInvocationID.x; i < 1024u; i += 64u)
+ GS[i] = IQ1S_GRID[i];
+ barrier();
+ U1 W = U1(p.w);
+ U4 X = U4(p.x);
+ F Y = F(p.y);
+ uint lane = gl_SubgroupInvocationID;
+ uint grow = gl_WorkGroupID.y * 64u + gl_LocalInvocationID.x;
+ uint row = min(grow, p.rows - 1u);
+ uint v0 = gl_WorkGroupID.x * NV;
+ uint xo[NV];
+ for (uint v = 0; v < NV; v++)
+ xo[v] = min(v0 + v, p.nv - 1u) * p.xs * 20u;
+ float acc[NV];
+ for (uint v = 0; v < NV; v++)
+ acc[v] = 0.0;
+ uint off = row * p.nb * BS;
+ for (uint b = 0; b < p.nb; b++, off += BS) {
+ uint base = off >> 2;
+ uint wd[NW];
+ for (uint k = 0; k < NW; k++)
+ wd[k] = W.v[base + k];
+ if ((off & 3u) != 0u) {
+ for (uint k = 0; k + 1u < NW; k++)
+ wd[k] = (wd[k] >> 16) | (wd[k + 1u] << 16);
+ wd[NW - 1u] = wd[NW - 1u] >> 16;
+ }
+ float d;
+ if (KIND == 0u) {
+ d = unpackHalf2x16(wd[0] & 0xffffu).x;
+ } else {
+ uint s0 = half_at(wd, 48u), s1 = half_at(wd, 50u), s2 = half_at(wd, 52u),
+ s3 = half_at(wd, 54u);
+ d = unpackHalf2x16((s0 >> 12) | ((s1 >> 8) & 0x00f0u) | ((s2 >> 4) & 0x0f00u) |
+ (s3 & 0xf000u)).x;
+ }
+ /* each sub-group of 8 two signed words, and its scale */
+ uint q[64];
+ int sc[32];
+ for (uint g = 0; g < 8u; g++)
+ for (uint k = 0; k < 4u; k++) {
+ uint l = 4u * g + k, idx;
+ int neg;
+ if (KIND == 0u) {
+ uint qh = half_at(wd, 34u + 2u * g);
+ idx = byte_at(wd, 2u + l) | (((qh >> (3u * k)) & 7u) << 8);
+ neg = int((qh >> 15) & 1u);
+ sc[l] = 2 * int((qh >> 12) & 7u) + 1;
+ } else {
+ uint qh = byte_at(wd, 32u + 2u * g + k / 2u);
+ idx = byte_at(wd, l) | ((qh << ((k & 1u) != 0u ? 4u : 8u)) & 0x700u);
+ neg = int((qh >> ((k & 1u) != 0u ? 7u : 3u)) & 1u);
+ uint s = half_at(wd, 48u + 2u * (g / 2u));
+ sc[l] = 2 * int((s >> (6u * (g & 1u) + 3u * (k / 2u))) & 7u) + 1;
+ }
+ int delta = neg != 0 ? -1 : 1;
+ q[2u * l] = weights4(idx, 0u, delta);
+ q[2u * l + 1u] = weights4(idx, 1u, delta);
+ }
+ for (uint v = 0; v < NV; v++) {
+ uvec4 xr[NX];
+ for (uint j = 0; j < NX; j++)
+ xr[j] = X.v[xo[v] + min(j * SG + lane, 19u)];
+ int isum = 0;
+ for (uint g = 0; g < 8u; g++) {
+ uvec4 a0 = bc(xr, 2u * g), a1 = bc(xr, 2u * g + 1u);
+ uint l = 4u * g;
+ isum += sc[l] * (dp(q[2u * l], a0.x) + dp(q[2u * l + 1u], a0.y));
+ isum += sc[l + 1u] * (dp(q[2u * l + 2u], a0.z) + dp(q[2u * l + 3u], a0.w));
+ isum += sc[l + 2u] * (dp(q[2u * l + 4u], a1.x) + dp(q[2u * l + 5u], a1.y));
+ isum += sc[l + 3u] * (dp(q[2u * l + 6u], a1.z) + dp(q[2u * l + 7u], a1.w));
+ }
+ float d8 = uintBitsToFloat(bc(xr, 18u).x);
+ acc[v] = fma(d * 0.125 * d8, float(isum), acc[v]);
+ xo[v] += 20u;
+ }
+ }
+ if (grow < p.rows)
+ for (uint v = 0; v < NV; v++)
+ if (v0 + v < p.nv)
+ Y.v[(v0 + v) * p.ys + row] = acc[v];
+}
diff --git a/src/llm/shaders/iq2_matvec.comp b/src/llm/shaders/iq2_matvec.comp
new file mode 100644
index 0000000..a93a2e0
--- /dev/null
+++ b/src/llm/shaders/iq2_matvec.comp
@@ -0,0 +1,761 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri
+ The tables IQ2XXS_GRID, IQ2XS_GRID and IQ2S_GRID below are the IQ2 grids
+ of ggml (ggml/src/ggml-common.h, iq2xxs_grid, iq2xs_grid, iq2s_grid),
+ copied as data from src/llm/iq2xxs_grid.h, iq2xs_grid.h and iq2s_grid.h,
+ each 64-bit entry as two words, low first: they define the formats.
+ Copyright (c) 2023-2026 The ggml authors, under the MIT licence (see the
+ ggml repository). Generated - not formatted by hand. */
+#version 450
+// IQ2 weights times up to NV Q8_K vectors, one row per invocation, as
+// janas_iq2_dot_ref computes them, KIND chosen when the pipeline is made:
+// 0 IQ2_XXS (66 bytes a block: d, then for each group of 32 a word of four
+// grid indices and a word of four 7-bit sign indices and the scale), 1
+// IQ2_XS (74: d, a 16-bit word a sub-group of 8 - its 9-bit grid index and
+// 7-bit sign index - and a nibble of scale for each 16), 2 IQ2_S (82: d,
+// the grid indices' low bytes, the signs' bytes, their top two bits, the
+// scales). A 7-bit sign index takes an eighth bit that makes the negatives
+// even (ggml's ksigns_iq2xs). Each sub-group of 8 its eight grid levels
+// with their signs; the block's exact integer sum (scale 1 + 2 s times
+// each sub-group's dot) in one fma, with (d / 8) d8: the CPU's float
+// operations, so the results agree bit for bit. Blocks start on 2-byte
+// boundaries: their words are realigned. Activations shared across the
+// subgroup as in q4k_matvec.comp.
+#extension GL_EXT_integer_dot_product : require
+#extension GL_KHR_shader_subgroup_basic : require
+#extension GL_KHR_shader_subgroup_ballot : require
+#extension GL_EXT_buffer_reference : require
+#extension GL_EXT_buffer_reference_uvec2 : require
+
+layout(local_size_x = 64) in;
+layout(constant_id = 0) const uint NV = 8;
+layout(constant_id = 1) const uint SG = 8;
+layout(constant_id = 2) const uint KIND = 0;
+const uint NX = (19u + SG - 1u) / SG;
+/* bytes of a block, words read for it (one more: the realignment) */
+const uint BS = KIND == 0u ? 66u : KIND == 1u ? 74u : 82u;
+const uint NW = (BS + 3u) / 4u + 1u;
+/* entries of the grid */
+const uint NG = KIND == 0u ? 256u : KIND == 1u ? 512u : 1024u;
+
+layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer U1 { uint v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 16) readonly buffer U4 { uvec4 v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) writeonly buffer F { float v[]; };
+
+layout(push_constant) uniform P { uvec2 w, x, y; uint rows, nb, nv, xs, ys; } p;
+
+const uint IQ2XXS_GRID[512] = uint[512](
+ 0x08080808u, 0x08080808u, 0x0808082bu, 0x08080808u, 0x08081919u, 0x08080808u,
+ 0x08082b08u, 0x08080808u, 0x08082b2bu, 0x08080808u, 0x08190819u, 0x08080808u,
+ 0x08191908u, 0x08080808u, 0x082b0808u, 0x08080808u, 0x082b082bu, 0x08080808u,
+ 0x082b2b08u, 0x08080808u, 0x082b2b2bu, 0x08080808u, 0x19080819u, 0x08080808u,
+ 0x19081908u, 0x08080808u, 0x19190808u, 0x08080808u, 0x19192b08u, 0x08080808u,
+ 0x192b0819u, 0x08080808u, 0x192b1908u, 0x08080808u, 0x2b080808u, 0x08080808u,
+ 0x2b08082bu, 0x08080808u, 0x2b082b2bu, 0x08080808u, 0x2b2b082bu, 0x08080808u,
+ 0x08080819u, 0x08080819u, 0x08081908u, 0x08080819u, 0x08190808u, 0x08080819u,
+ 0x08191919u, 0x08080819u, 0x19080808u, 0x08080819u, 0x2b081908u, 0x08080819u,
+ 0x2b192b08u, 0x08080819u, 0x08080808u, 0x0808082bu, 0x0808082bu, 0x0808082bu,
+ 0x082b082bu, 0x0808082bu, 0x2b08082bu, 0x0808082bu, 0x08080819u, 0x08081908u,
+ 0x08081908u, 0x08081908u, 0x08190808u, 0x08081908u, 0x082b0819u, 0x08081908u,
+ 0x082b1908u, 0x08081908u, 0x19080808u, 0x08081908u, 0x1908082bu, 0x08081908u,
+ 0x19082b08u, 0x08081908u, 0x192b0808u, 0x08081908u, 0x2b080819u, 0x08081908u,
+ 0x2b081908u, 0x08081908u, 0x2b190808u, 0x08081908u, 0x2b2b1908u, 0x08081908u,
+ 0x08080808u, 0x08081919u, 0x0808082bu, 0x08081919u, 0x08082b08u, 0x08081919u,
+ 0x082b0808u, 0x08081919u, 0x1908192bu, 0x08081919u, 0x192b2b19u, 0x08081919u,
+ 0x2b080808u, 0x08081919u, 0x2b190819u, 0x08081919u, 0x08082b19u, 0x0808192bu,
+ 0x08190808u, 0x0808192bu, 0x19080808u, 0x0808192bu, 0x2b081908u, 0x0808192bu,
+ 0x2b2b1908u, 0x0808192bu, 0x08080808u, 0x08082b08u, 0x08081919u, 0x08082b08u,
+ 0x08082b08u, 0x08082b08u, 0x08191908u, 0x08082b08u, 0x082b2b08u, 0x08082b08u,
+ 0x19080819u, 0x08082b08u, 0x19081908u, 0x08082b08u, 0x19190808u, 0x08082b08u,
+ 0x1919082bu, 0x08082b08u, 0x2b082b08u, 0x08082b08u, 0x08081908u, 0x08082b19u,
+ 0x19080808u, 0x08082b19u, 0x0808082bu, 0x08082b2bu, 0x08191908u, 0x08082b2bu,
+ 0x08080819u, 0x08190808u, 0x08081908u, 0x08190808u, 0x08190808u, 0x08190808u,
+ 0x082b0819u, 0x08190808u, 0x19080808u, 0x08190808u, 0x192b0808u, 0x08190808u,
+ 0x2b081908u, 0x08190808u, 0x2b190808u, 0x08190808u, 0x2b191919u, 0x08190808u,
+ 0x08080808u, 0x08190819u, 0x08082b08u, 0x08190819u, 0x082b0808u, 0x08190819u,
+ 0x19190808u, 0x08190819u, 0x19192b2bu, 0x08190819u, 0x2b080808u, 0x08190819u,
+ 0x082b1908u, 0x0819082bu, 0x19081919u, 0x0819082bu, 0x08080808u, 0x08191908u,
+ 0x08082b08u, 0x08191908u, 0x082b0808u, 0x08191908u, 0x082b1919u, 0x08191908u,
+ 0x19082b19u, 0x08191908u, 0x2b080808u, 0x08191908u, 0x08192b08u, 0x08191919u,
+ 0x192b082bu, 0x08191919u, 0x08080808u, 0x0819192bu, 0x0819192bu, 0x0819192bu,
+ 0x08080819u, 0x08192b08u, 0x08081908u, 0x08192b08u, 0x08190808u, 0x08192b08u,
+ 0x19080808u, 0x08192b08u, 0x2b080819u, 0x08192b08u, 0x08080808u, 0x08192b19u,
+ 0x08081919u, 0x08192b19u, 0x2b2b0808u, 0x08192b19u, 0x19190819u, 0x08192b2bu,
+ 0x08080808u, 0x082b0808u, 0x0808082bu, 0x082b0808u, 0x08082b2bu, 0x082b0808u,
+ 0x19081908u, 0x082b0808u, 0x192b0819u, 0x082b0808u, 0x2b080808u, 0x082b0808u,
+ 0x2b08082bu, 0x082b0808u, 0x082b2b19u, 0x082b0819u, 0x19082b08u, 0x082b0819u,
+ 0x08080808u, 0x082b082bu, 0x0808082bu, 0x082b082bu, 0x08080819u, 0x082b1908u,
+ 0x08081908u, 0x082b1908u, 0x08190808u, 0x082b1908u, 0x19080808u, 0x082b1908u,
+ 0x1919192bu, 0x082b1908u, 0x08080808u, 0x082b1919u, 0x19080819u, 0x082b1919u,
+ 0x192b1908u, 0x082b1919u, 0x2b190808u, 0x082b192bu, 0x08082b08u, 0x082b2b08u,
+ 0x082b0808u, 0x082b2b08u, 0x2b191908u, 0x082b2b08u, 0x19081908u, 0x082b2b2bu,
+ 0x08080819u, 0x19080808u, 0x08081908u, 0x19080808u, 0x08190808u, 0x19080808u,
+ 0x08192b08u, 0x19080808u, 0x082b0819u, 0x19080808u, 0x082b1908u, 0x19080808u,
+ 0x19080808u, 0x19080808u, 0x19082b08u, 0x19080808u, 0x1919192bu, 0x19080808u,
+ 0x192b0808u, 0x19080808u, 0x2b080819u, 0x19080808u, 0x2b081908u, 0x19080808u,
+ 0x2b190808u, 0x19080808u, 0x08080808u, 0x19080819u, 0x082b0808u, 0x19080819u,
+ 0x192b0819u, 0x19080819u, 0x2b080808u, 0x19080819u, 0x2b081919u, 0x19080819u,
+ 0x08080819u, 0x1908082bu, 0x08190808u, 0x1908082bu, 0x19082b08u, 0x1908082bu,
+ 0x1919192bu, 0x1908082bu, 0x192b2b08u, 0x1908082bu, 0x08080808u, 0x19081908u,
+ 0x08082b08u, 0x19081908u, 0x082b0808u, 0x19081908u, 0x2b080808u, 0x19081908u,
+ 0x2b192b19u, 0x19081908u, 0x0819082bu, 0x19081919u, 0x082b1908u, 0x19081919u,
+ 0x08080808u, 0x1908192bu, 0x08080819u, 0x19082b08u, 0x08081908u, 0x19082b08u,
+ 0x08190808u, 0x19082b08u, 0x19080808u, 0x19082b08u, 0x19081919u, 0x19082b08u,
+ 0x08080808u, 0x19082b19u, 0x19192b08u, 0x19082b19u, 0x192b0819u, 0x19082b19u,
+ 0x2b08082bu, 0x19082b19u, 0x19081919u, 0x19082b2bu, 0x2b190808u, 0x19082b2bu,
+ 0x08080808u, 0x19190808u, 0x08082b08u, 0x19190808u, 0x08190819u, 0x19190808u,
+ 0x08192b19u, 0x19190808u, 0x082b0808u, 0x19190808u, 0x2b080808u, 0x19190808u,
+ 0x2b082b08u, 0x19190808u, 0x08081908u, 0x19190819u, 0x1908082bu, 0x19190819u,
+ 0x2b2b1908u, 0x19190819u, 0x2b190819u, 0x1919082bu, 0x2b190808u, 0x19191908u,
+ 0x2b19082bu, 0x19191908u, 0x08082b2bu, 0x19191919u, 0x08080819u, 0x1919192bu,
+ 0x19191908u, 0x1919192bu, 0x08080808u, 0x19192b08u, 0x08190819u, 0x19192b08u,
+ 0x08192b19u, 0x19192b08u, 0x192b1908u, 0x19192b08u, 0x19080808u, 0x19192b19u,
+ 0x08082b08u, 0x19192b2bu, 0x08081908u, 0x192b0808u, 0x08190808u, 0x192b0808u,
+ 0x19080808u, 0x192b0808u, 0x192b2b08u, 0x192b0808u, 0x08080808u, 0x192b0819u,
+ 0x19191919u, 0x192b0819u, 0x08192b08u, 0x192b082bu, 0x192b0808u, 0x192b082bu,
+ 0x08080808u, 0x192b1908u, 0x08081919u, 0x192b1908u, 0x08190808u, 0x192b1919u,
+ 0x0819082bu, 0x192b1919u, 0x2b081908u, 0x192b1919u, 0x1908082bu, 0x192b2b08u,
+ 0x08080808u, 0x2b080808u, 0x0808082bu, 0x2b080808u, 0x08082b2bu, 0x2b080808u,
+ 0x19080819u, 0x2b080808u, 0x2b08082bu, 0x2b080808u, 0x08081908u, 0x2b080819u,
+ 0x08192b08u, 0x2b080819u, 0x19080808u, 0x2b080819u, 0x08190819u, 0x2b08082bu,
+ 0x08080819u, 0x2b081908u, 0x08081908u, 0x2b081908u, 0x08190808u, 0x2b081908u,
+ 0x08191919u, 0x2b081908u, 0x19080808u, 0x2b081908u, 0x192b0808u, 0x2b081908u,
+ 0x08080808u, 0x2b081919u, 0x1908192bu, 0x2b081919u, 0x2b191908u, 0x2b081919u,
+ 0x08082b19u, 0x2b08192bu, 0x19080808u, 0x2b08192bu, 0x192b0808u, 0x2b08192bu,
+ 0x0808082bu, 0x2b082b08u, 0x08081908u, 0x2b082b19u, 0x08190819u, 0x2b082b2bu,
+ 0x08081908u, 0x2b190808u, 0x08190808u, 0x2b190808u, 0x082b1908u, 0x2b190808u,
+ 0x19080808u, 0x2b190808u, 0x2b2b0819u, 0x2b190808u, 0x0819192bu, 0x2b190819u,
+ 0x2b080808u, 0x2b190819u, 0x19081919u, 0x2b19082bu, 0x08080808u, 0x2b191908u,
+ 0x082b082bu, 0x2b191908u, 0x19081908u, 0x2b191908u, 0x19190819u, 0x2b191919u,
+ 0x2b080819u, 0x2b192b08u, 0x082b0808u, 0x2b192b19u, 0x0808082bu, 0x2b2b0808u,
+ 0x19190808u, 0x2b2b0808u, 0x2b081919u, 0x2b2b0808u, 0x08082b19u, 0x2b2b0819u,
+ 0x08080808u, 0x2b2b082bu, 0x08192b08u, 0x2b2b1908u, 0x19190808u, 0x2b2b2b08u,
+ 0x08081908u, 0x2b2b2b19u);
+
+const uint IQ2XS_GRID[1024] = uint[1024](
+ 0x08080808u, 0x08080808u, 0x0808082bu, 0x08080808u, 0x08081919u, 0x08080808u,
+ 0x08082b08u, 0x08080808u, 0x08082b2bu, 0x08080808u, 0x08190819u, 0x08080808u,
+ 0x08191908u, 0x08080808u, 0x0819192bu, 0x08080808u, 0x08192b19u, 0x08080808u,
+ 0x082b0808u, 0x08080808u, 0x082b082bu, 0x08080808u, 0x082b1919u, 0x08080808u,
+ 0x082b2b08u, 0x08080808u, 0x19080819u, 0x08080808u, 0x19081908u, 0x08080808u,
+ 0x1908192bu, 0x08080808u, 0x19082b19u, 0x08080808u, 0x19190808u, 0x08080808u,
+ 0x1919082bu, 0x08080808u, 0x19191919u, 0x08080808u, 0x19192b08u, 0x08080808u,
+ 0x192b0819u, 0x08080808u, 0x192b1908u, 0x08080808u, 0x2b080808u, 0x08080808u,
+ 0x2b08082bu, 0x08080808u, 0x2b081919u, 0x08080808u, 0x2b082b08u, 0x08080808u,
+ 0x2b190819u, 0x08080808u, 0x2b191908u, 0x08080808u, 0x2b192b19u, 0x08080808u,
+ 0x2b2b0808u, 0x08080808u, 0x08080819u, 0x08080819u, 0x08081908u, 0x08080819u,
+ 0x0808192bu, 0x08080819u, 0x08082b19u, 0x08080819u, 0x08190808u, 0x08080819u,
+ 0x0819082bu, 0x08080819u, 0x08191919u, 0x08080819u, 0x08192b08u, 0x08080819u,
+ 0x08192b2bu, 0x08080819u, 0x082b0819u, 0x08080819u, 0x082b1908u, 0x08080819u,
+ 0x19080808u, 0x08080819u, 0x1908082bu, 0x08080819u, 0x19081919u, 0x08080819u,
+ 0x19082b08u, 0x08080819u, 0x19190819u, 0x08080819u, 0x19191908u, 0x08080819u,
+ 0x192b0808u, 0x08080819u, 0x192b2b08u, 0x08080819u, 0x2b080819u, 0x08080819u,
+ 0x2b081908u, 0x08080819u, 0x2b190808u, 0x08080819u, 0x08080808u, 0x0808082bu,
+ 0x0808082bu, 0x0808082bu, 0x08081919u, 0x0808082bu, 0x08082b08u, 0x0808082bu,
+ 0x08190819u, 0x0808082bu, 0x08191908u, 0x0808082bu, 0x082b0808u, 0x0808082bu,
+ 0x19080819u, 0x0808082bu, 0x19081908u, 0x0808082bu, 0x19190808u, 0x0808082bu,
+ 0x19191919u, 0x0808082bu, 0x2b080808u, 0x0808082bu, 0x2b082b2bu, 0x0808082bu,
+ 0x08080819u, 0x08081908u, 0x08081908u, 0x08081908u, 0x0808192bu, 0x08081908u,
+ 0x08082b19u, 0x08081908u, 0x08190808u, 0x08081908u, 0x0819082bu, 0x08081908u,
+ 0x08191919u, 0x08081908u, 0x08192b08u, 0x08081908u, 0x082b0819u, 0x08081908u,
+ 0x082b1908u, 0x08081908u, 0x19080808u, 0x08081908u, 0x1908082bu, 0x08081908u,
+ 0x19081919u, 0x08081908u, 0x19082b08u, 0x08081908u, 0x19190819u, 0x08081908u,
+ 0x19191908u, 0x08081908u, 0x1919192bu, 0x08081908u, 0x192b0808u, 0x08081908u,
+ 0x2b080819u, 0x08081908u, 0x2b081908u, 0x08081908u, 0x2b190808u, 0x08081908u,
+ 0x08080808u, 0x08081919u, 0x0808082bu, 0x08081919u, 0x08081919u, 0x08081919u,
+ 0x08082b08u, 0x08081919u, 0x08190819u, 0x08081919u, 0x08191908u, 0x08081919u,
+ 0x082b0808u, 0x08081919u, 0x19080819u, 0x08081919u, 0x19081908u, 0x08081919u,
+ 0x19190808u, 0x08081919u, 0x192b0819u, 0x08081919u, 0x2b080808u, 0x08081919u,
+ 0x08080819u, 0x0808192bu, 0x08081908u, 0x0808192bu, 0x08190808u, 0x0808192bu,
+ 0x082b192bu, 0x0808192bu, 0x19080808u, 0x0808192bu, 0x1908082bu, 0x0808192bu,
+ 0x2b081908u, 0x0808192bu, 0x08080808u, 0x08082b08u, 0x0808082bu, 0x08082b08u,
+ 0x08081919u, 0x08082b08u, 0x08082b08u, 0x08082b08u, 0x08082b2bu, 0x08082b08u,
+ 0x08190819u, 0x08082b08u, 0x08191908u, 0x08082b08u, 0x082b0808u, 0x08082b08u,
+ 0x082b1919u, 0x08082b08u, 0x19080819u, 0x08082b08u, 0x19081908u, 0x08082b08u,
+ 0x19190808u, 0x08082b08u, 0x19192b08u, 0x08082b08u, 0x2b080808u, 0x08082b08u,
+ 0x2b2b0808u, 0x08082b08u, 0x2b2b2b2bu, 0x08082b08u, 0x08080819u, 0x08082b19u,
+ 0x08081908u, 0x08082b19u, 0x08190808u, 0x08082b19u, 0x19080808u, 0x08082b19u,
+ 0x2b080819u, 0x08082b19u, 0x2b082b19u, 0x08082b19u, 0x08080808u, 0x08082b2bu,
+ 0x082b0808u, 0x08082b2bu, 0x082b2b08u, 0x08082b2bu, 0x2b19192bu, 0x08082b2bu,
+ 0x2b2b0808u, 0x08082b2bu, 0x08080819u, 0x08190808u, 0x08081908u, 0x08190808u,
+ 0x0808192bu, 0x08190808u, 0x08082b19u, 0x08190808u, 0x08190808u, 0x08190808u,
+ 0x0819082bu, 0x08190808u, 0x08191919u, 0x08190808u, 0x08192b08u, 0x08190808u,
+ 0x082b0819u, 0x08190808u, 0x082b1908u, 0x08190808u, 0x19080808u, 0x08190808u,
+ 0x1908082bu, 0x08190808u, 0x19081919u, 0x08190808u, 0x19082b08u, 0x08190808u,
+ 0x19190819u, 0x08190808u, 0x19191908u, 0x08190808u, 0x192b0808u, 0x08190808u,
+ 0x192b2b2bu, 0x08190808u, 0x2b080819u, 0x08190808u, 0x2b081908u, 0x08190808u,
+ 0x2b190808u, 0x08190808u, 0x08080808u, 0x08190819u, 0x0808082bu, 0x08190819u,
+ 0x08081919u, 0x08190819u, 0x08082b08u, 0x08190819u, 0x08190819u, 0x08190819u,
+ 0x08191908u, 0x08190819u, 0x082b0808u, 0x08190819u, 0x19080819u, 0x08190819u,
+ 0x19081908u, 0x08190819u, 0x19190808u, 0x08190819u, 0x2b080808u, 0x08190819u,
+ 0x2b191908u, 0x08190819u, 0x2b19192bu, 0x08190819u, 0x08080819u, 0x0819082bu,
+ 0x08081908u, 0x0819082bu, 0x0808192bu, 0x0819082bu, 0x08190808u, 0x0819082bu,
+ 0x19080808u, 0x0819082bu, 0x192b0808u, 0x0819082bu, 0x08080808u, 0x08191908u,
+ 0x0808082bu, 0x08191908u, 0x08081919u, 0x08191908u, 0x08082b08u, 0x08191908u,
+ 0x08190819u, 0x08191908u, 0x08191908u, 0x08191908u, 0x082b0808u, 0x08191908u,
+ 0x19080819u, 0x08191908u, 0x19081908u, 0x08191908u, 0x19082b19u, 0x08191908u,
+ 0x19190808u, 0x08191908u, 0x192b1908u, 0x08191908u, 0x2b080808u, 0x08191908u,
+ 0x08080819u, 0x08191919u, 0x08081908u, 0x08191919u, 0x08190808u, 0x08191919u,
+ 0x19080808u, 0x08191919u, 0x08080808u, 0x0819192bu, 0x08191908u, 0x0819192bu,
+ 0x19082b19u, 0x0819192bu, 0x08080819u, 0x08192b08u, 0x08081908u, 0x08192b08u,
+ 0x08190808u, 0x08192b08u, 0x0819082bu, 0x08192b08u, 0x19080808u, 0x08192b08u,
+ 0x19191908u, 0x08192b08u, 0x2b08192bu, 0x08192b08u, 0x08080808u, 0x08192b19u,
+ 0x08081919u, 0x08192b19u, 0x192b192bu, 0x08192b19u, 0x19190819u, 0x08192b2bu,
+ 0x2b2b2b19u, 0x08192b2bu, 0x08080808u, 0x082b0808u, 0x0808082bu, 0x082b0808u,
+ 0x08081919u, 0x082b0808u, 0x08082b08u, 0x082b0808u, 0x08082b2bu, 0x082b0808u,
+ 0x08190819u, 0x082b0808u, 0x08191908u, 0x082b0808u, 0x082b0808u, 0x082b0808u,
+ 0x19080819u, 0x082b0808u, 0x19081908u, 0x082b0808u, 0x19190808u, 0x082b0808u,
+ 0x2b080808u, 0x082b0808u, 0x2b2b0808u, 0x082b0808u, 0x08080819u, 0x082b0819u,
+ 0x08081908u, 0x082b0819u, 0x08190808u, 0x082b0819u, 0x19080808u, 0x082b0819u,
+ 0x19082b08u, 0x082b0819u, 0x192b1919u, 0x082b0819u, 0x08080808u, 0x082b082bu,
+ 0x082b082bu, 0x082b082bu, 0x2b080808u, 0x082b082bu, 0x2b2b2b08u, 0x082b082bu,
+ 0x08080819u, 0x082b1908u, 0x08081908u, 0x082b1908u, 0x08190808u, 0x082b1908u,
+ 0x082b2b19u, 0x082b1908u, 0x19080808u, 0x082b1908u, 0x08080808u, 0x082b1919u,
+ 0x19080819u, 0x082b1919u, 0x1919082bu, 0x082b1919u, 0x2b192b19u, 0x082b1919u,
+ 0x08080819u, 0x082b192bu, 0x08192b2bu, 0x082b192bu, 0x2b2b192bu, 0x082b192bu,
+ 0x08080808u, 0x082b2b08u, 0x08082b08u, 0x082b2b08u, 0x08082b2bu, 0x082b2b08u,
+ 0x082b0808u, 0x082b2b08u, 0x19191919u, 0x082b2b08u, 0x2b082b08u, 0x082b2b08u,
+ 0x2b2b082bu, 0x082b2b08u, 0x192b2b08u, 0x082b2b19u, 0x2b190808u, 0x082b2b19u,
+ 0x08082b08u, 0x082b2b2bu, 0x082b0808u, 0x082b2b2bu, 0x2b08082bu, 0x082b2b2bu,
+ 0x2b082b08u, 0x082b2b2bu, 0x2b082b2bu, 0x082b2b2bu, 0x08080819u, 0x19080808u,
+ 0x08081908u, 0x19080808u, 0x0808192bu, 0x19080808u, 0x08082b19u, 0x19080808u,
+ 0x08190808u, 0x19080808u, 0x0819082bu, 0x19080808u, 0x08191919u, 0x19080808u,
+ 0x08192b08u, 0x19080808u, 0x082b0819u, 0x19080808u, 0x082b1908u, 0x19080808u,
+ 0x19080808u, 0x19080808u, 0x1908082bu, 0x19080808u, 0x19081919u, 0x19080808u,
+ 0x19082b08u, 0x19080808u, 0x19082b2bu, 0x19080808u, 0x19190819u, 0x19080808u,
+ 0x19191908u, 0x19080808u, 0x192b0808u, 0x19080808u, 0x192b1919u, 0x19080808u,
+ 0x2b080819u, 0x19080808u, 0x2b081908u, 0x19080808u, 0x2b190808u, 0x19080808u,
+ 0x08080808u, 0x19080819u, 0x0808082bu, 0x19080819u, 0x08081919u, 0x19080819u,
+ 0x08082b08u, 0x19080819u, 0x08190819u, 0x19080819u, 0x08191908u, 0x19080819u,
+ 0x082b0808u, 0x19080819u, 0x19080819u, 0x19080819u, 0x19081908u, 0x19080819u,
+ 0x19190808u, 0x19080819u, 0x2b080808u, 0x19080819u, 0x2b081919u, 0x19080819u,
+ 0x2b2b082bu, 0x19080819u, 0x08080819u, 0x1908082bu, 0x08081908u, 0x1908082bu,
+ 0x08190808u, 0x1908082bu, 0x0819082bu, 0x1908082bu, 0x082b2b19u, 0x1908082bu,
+ 0x19080808u, 0x1908082bu, 0x08080808u, 0x19081908u, 0x0808082bu, 0x19081908u,
+ 0x08081919u, 0x19081908u, 0x08082b08u, 0x19081908u, 0x08190819u, 0x19081908u,
+ 0x08191908u, 0x19081908u, 0x08192b19u, 0x19081908u, 0x082b0808u, 0x19081908u,
+ 0x19080819u, 0x19081908u, 0x19081908u, 0x19081908u, 0x19190808u, 0x19081908u,
+ 0x2b080808u, 0x19081908u, 0x2b191908u, 0x19081908u, 0x08080819u, 0x19081919u,
+ 0x08081908u, 0x19081919u, 0x08190808u, 0x19081919u, 0x082b1908u, 0x19081919u,
+ 0x19080808u, 0x19081919u, 0x2b192b2bu, 0x19081919u, 0x08080808u, 0x1908192bu,
+ 0x08082b2bu, 0x1908192bu, 0x19081908u, 0x1908192bu, 0x19190808u, 0x1908192bu,
+ 0x08080819u, 0x19082b08u, 0x08081908u, 0x19082b08u, 0x08190808u, 0x19082b08u,
+ 0x19080808u, 0x19082b08u, 0x19081919u, 0x19082b08u, 0x19191908u, 0x19082b08u,
+ 0x192b082bu, 0x19082b08u, 0x08080808u, 0x19082b19u, 0x08190819u, 0x19082b19u,
+ 0x19081908u, 0x19082b19u, 0x19190808u, 0x19082b19u, 0x192b2b19u, 0x19082b19u,
+ 0x08081908u, 0x19082b2bu, 0x08080808u, 0x19190808u, 0x0808082bu, 0x19190808u,
+ 0x08081919u, 0x19190808u, 0x08082b08u, 0x19190808u, 0x08190819u, 0x19190808u,
+ 0x08191908u, 0x19190808u, 0x082b0808u, 0x19190808u, 0x082b2b08u, 0x19190808u,
+ 0x19080819u, 0x19190808u, 0x19081908u, 0x19190808u, 0x19190808u, 0x19190808u,
+ 0x2b080808u, 0x19190808u, 0x08080819u, 0x19190819u, 0x08081908u, 0x19190819u,
+ 0x08190808u, 0x19190819u, 0x08191919u, 0x19190819u, 0x19080808u, 0x19190819u,
+ 0x1908082bu, 0x19190819u, 0x08080808u, 0x1919082bu, 0x19081908u, 0x1919082bu,
+ 0x2b2b2b2bu, 0x1919082bu, 0x08080819u, 0x19191908u, 0x08081908u, 0x19191908u,
+ 0x08190808u, 0x19191908u, 0x082b0819u, 0x19191908u, 0x19080808u, 0x19191908u,
+ 0x192b0808u, 0x19191908u, 0x2b080819u, 0x19191908u, 0x2b2b0819u, 0x19191908u,
+ 0x08080808u, 0x19191919u, 0x08082b08u, 0x19191919u, 0x2b080808u, 0x19191919u,
+ 0x2b082b08u, 0x19191919u, 0x082b0819u, 0x1919192bu, 0x192b2b08u, 0x1919192bu,
+ 0x2b2b0819u, 0x1919192bu, 0x08080808u, 0x19192b08u, 0x08191908u, 0x19192b08u,
+ 0x19080819u, 0x19192b08u, 0x19190808u, 0x19192b08u, 0x2b192b19u, 0x19192b08u,
+ 0x08192b2bu, 0x19192b19u, 0x19080808u, 0x19192b19u, 0x1908082bu, 0x19192b19u,
+ 0x2b081919u, 0x19192b2bu, 0x08080819u, 0x192b0808u, 0x08081908u, 0x192b0808u,
+ 0x08190808u, 0x192b0808u, 0x19080808u, 0x192b0808u, 0x19191908u, 0x192b0808u,
+ 0x192b082bu, 0x192b0808u, 0x2b08192bu, 0x192b0808u, 0x2b2b2b19u, 0x192b0808u,
+ 0x08080808u, 0x192b0819u, 0x082b1908u, 0x192b082bu, 0x19082b2bu, 0x192b082bu,
+ 0x2b19082bu, 0x192b082bu, 0x08080808u, 0x192b1908u, 0x0819192bu, 0x192b1908u,
+ 0x08190808u, 0x192b1919u, 0x19080808u, 0x192b1919u, 0x19081919u, 0x192b1919u,
+ 0x2b2b1908u, 0x192b1919u, 0x08080819u, 0x192b2b08u, 0x192b2b2bu, 0x192b2b08u,
+ 0x082b1919u, 0x192b2b19u, 0x0808192bu, 0x192b2b2bu, 0x19191908u, 0x192b2b2bu,
+ 0x192b082bu, 0x192b2b2bu, 0x08080808u, 0x2b080808u, 0x0808082bu, 0x2b080808u,
+ 0x08081919u, 0x2b080808u, 0x08082b08u, 0x2b080808u, 0x08190819u, 0x2b080808u,
+ 0x08191908u, 0x2b080808u, 0x082b0808u, 0x2b080808u, 0x082b2b2bu, 0x2b080808u,
+ 0x19080819u, 0x2b080808u, 0x19081908u, 0x2b080808u, 0x19190808u, 0x2b080808u,
+ 0x2b080808u, 0x2b080808u, 0x2b08082bu, 0x2b080808u, 0x2b2b2b08u, 0x2b080808u,
+ 0x2b2b2b2bu, 0x2b080808u, 0x08080819u, 0x2b080819u, 0x08081908u, 0x2b080819u,
+ 0x0808192bu, 0x2b080819u, 0x08190808u, 0x2b080819u, 0x19080808u, 0x2b080819u,
+ 0x19190819u, 0x2b080819u, 0x19192b19u, 0x2b080819u, 0x08080808u, 0x2b08082bu,
+ 0x082b0808u, 0x2b08082bu, 0x2b080808u, 0x2b08082bu, 0x2b08082bu, 0x2b08082bu,
+ 0x2b2b0808u, 0x2b08082bu, 0x2b2b2b08u, 0x2b08082bu, 0x08080819u, 0x2b081908u,
+ 0x08081908u, 0x2b081908u, 0x08190808u, 0x2b081908u, 0x0819082bu, 0x2b081908u,
+ 0x08191919u, 0x2b081908u, 0x19080808u, 0x2b081908u, 0x192b0808u, 0x2b081908u,
+ 0x2b082b19u, 0x2b081908u, 0x08080808u, 0x2b081919u, 0x19081908u, 0x2b081919u,
+ 0x2b2b1919u, 0x2b081919u, 0x08192b08u, 0x2b08192bu, 0x192b2b2bu, 0x2b08192bu,
+ 0x08080808u, 0x2b082b08u, 0x08082b08u, 0x2b082b08u, 0x082b1919u, 0x2b082b08u,
+ 0x19192b2bu, 0x2b082b08u, 0x2b080808u, 0x2b082b08u, 0x2b08082bu, 0x2b082b08u,
+ 0x2b2b2b08u, 0x2b082b08u, 0x0808192bu, 0x2b082b19u, 0x082b082bu, 0x2b082b2bu,
+ 0x2b080808u, 0x2b082b2bu, 0x2b082b08u, 0x2b082b2bu, 0x2b19192bu, 0x2b082b2bu,
+ 0x2b2b2b08u, 0x2b082b2bu, 0x08080819u, 0x2b190808u, 0x08081908u, 0x2b190808u,
+ 0x08190808u, 0x2b190808u, 0x19080808u, 0x2b190808u, 0x1919192bu, 0x2b190808u,
+ 0x2b081908u, 0x2b190808u, 0x08080808u, 0x2b190819u, 0x082b082bu, 0x2b190819u,
+ 0x192b1908u, 0x2b190819u, 0x1919192bu, 0x2b19082bu, 0x2b082b19u, 0x2b19082bu,
+ 0x08080808u, 0x2b191908u, 0x08081919u, 0x2b191908u, 0x19081908u, 0x2b191908u,
+ 0x19190808u, 0x2b191908u, 0x19192b08u, 0x2b191908u, 0x082b2b19u, 0x2b191919u,
+ 0x2b190808u, 0x2b191919u, 0x2b19082bu, 0x2b191919u, 0x19080819u, 0x2b19192bu,
+ 0x19190819u, 0x2b192b08u, 0x2b2b192bu, 0x2b192b08u, 0x19082b19u, 0x2b192b19u,
+ 0x08191919u, 0x2b192b2bu, 0x192b0808u, 0x2b192b2bu, 0x08080808u, 0x2b2b0808u,
+ 0x0808082bu, 0x2b2b0808u, 0x08082b08u, 0x2b2b0808u, 0x08082b2bu, 0x2b2b0808u,
+ 0x082b0808u, 0x2b2b0808u, 0x082b2b2bu, 0x2b2b0808u, 0x2b2b0808u, 0x2b2b0808u,
+ 0x19190819u, 0x2b2b0819u, 0x19192b19u, 0x2b2b0819u, 0x2b2b192bu, 0x2b2b0819u,
+ 0x08080808u, 0x2b2b082bu, 0x0808082bu, 0x2b2b082bu, 0x08082b08u, 0x2b2b082bu,
+ 0x082b2b2bu, 0x2b2b082bu, 0x2b080808u, 0x2b2b082bu, 0x2b2b0808u, 0x2b2b082bu,
+ 0x19080808u, 0x2b2b1908u, 0x2b191919u, 0x2b2b1908u, 0x192b1919u, 0x2b2b192bu,
+ 0x2b192b08u, 0x2b2b192bu, 0x08082b2bu, 0x2b2b2b08u, 0x082b0808u, 0x2b2b2b08u,
+ 0x082b082bu, 0x2b2b2b08u, 0x082b2b08u, 0x2b2b2b08u, 0x2b2b0808u, 0x2b2b2b08u,
+ 0x2b2b2b08u, 0x2b2b2b08u, 0x08081908u, 0x2b2b2b19u, 0x2b081908u, 0x2b2b2b19u,
+ 0x2b08192bu, 0x2b2b2b19u, 0x082b2b08u, 0x2b2b2b2bu, 0x082b2b2bu, 0x2b2b2b2bu,
+ 0x2b190819u, 0x2b2b2b2bu, 0x2b2b2b2bu, 0x2b2b2b2bu);
+
+const uint IQ2S_GRID[2048] = uint[2048](
+ 0x08080808u, 0x08080808u, 0x0808082bu, 0x08080808u, 0x08081919u, 0x08080808u,
+ 0x08082b08u, 0x08080808u, 0x08082b2bu, 0x08080808u, 0x08190819u, 0x08080808u,
+ 0x08191908u, 0x08080808u, 0x0819192bu, 0x08080808u, 0x08192b19u, 0x08080808u,
+ 0x082b0808u, 0x08080808u, 0x082b082bu, 0x08080808u, 0x082b1919u, 0x08080808u,
+ 0x082b2b08u, 0x08080808u, 0x19080819u, 0x08080808u, 0x19081908u, 0x08080808u,
+ 0x1908192bu, 0x08080808u, 0x19082b19u, 0x08080808u, 0x19190808u, 0x08080808u,
+ 0x1919082bu, 0x08080808u, 0x19191919u, 0x08080808u, 0x19192b08u, 0x08080808u,
+ 0x192b0819u, 0x08080808u, 0x192b1908u, 0x08080808u, 0x192b192bu, 0x08080808u,
+ 0x192b2b19u, 0x08080808u, 0x2b080808u, 0x08080808u, 0x2b08082bu, 0x08080808u,
+ 0x2b081919u, 0x08080808u, 0x2b082b08u, 0x08080808u, 0x2b190819u, 0x08080808u,
+ 0x2b191908u, 0x08080808u, 0x2b2b0808u, 0x08080808u, 0x2b2b1919u, 0x08080808u,
+ 0x2b2b2b2bu, 0x08080808u, 0x08080819u, 0x08080819u, 0x08081908u, 0x08080819u,
+ 0x0808192bu, 0x08080819u, 0x08082b19u, 0x08080819u, 0x08190808u, 0x08080819u,
+ 0x0819082bu, 0x08080819u, 0x08191919u, 0x08080819u, 0x08192b08u, 0x08080819u,
+ 0x082b0819u, 0x08080819u, 0x082b1908u, 0x08080819u, 0x19080808u, 0x08080819u,
+ 0x1908082bu, 0x08080819u, 0x19081919u, 0x08080819u, 0x19082b08u, 0x08080819u,
+ 0x19190819u, 0x08080819u, 0x19191908u, 0x08080819u, 0x1919192bu, 0x08080819u,
+ 0x19192b19u, 0x08080819u, 0x192b0808u, 0x08080819u, 0x192b1919u, 0x08080819u,
+ 0x192b2b08u, 0x08080819u, 0x2b080819u, 0x08080819u, 0x2b081908u, 0x08080819u,
+ 0x2b190808u, 0x08080819u, 0x2b19082bu, 0x08080819u, 0x2b191919u, 0x08080819u,
+ 0x2b2b0819u, 0x08080819u, 0x2b2b1908u, 0x08080819u, 0x08080808u, 0x0808082bu,
+ 0x0808082bu, 0x0808082bu, 0x08081919u, 0x0808082bu, 0x08082b08u, 0x0808082bu,
+ 0x08190819u, 0x0808082bu, 0x08191908u, 0x0808082bu, 0x082b0808u, 0x0808082bu,
+ 0x082b2b2bu, 0x0808082bu, 0x19080819u, 0x0808082bu, 0x19081908u, 0x0808082bu,
+ 0x1908192bu, 0x0808082bu, 0x19082b19u, 0x0808082bu, 0x19190808u, 0x0808082bu,
+ 0x19191919u, 0x0808082bu, 0x2b080808u, 0x0808082bu, 0x2b081919u, 0x0808082bu,
+ 0x2b082b2bu, 0x0808082bu, 0x2b191908u, 0x0808082bu, 0x2b2b082bu, 0x0808082bu,
+ 0x08080819u, 0x08081908u, 0x08081908u, 0x08081908u, 0x0808192bu, 0x08081908u,
+ 0x08082b19u, 0x08081908u, 0x08190808u, 0x08081908u, 0x0819082bu, 0x08081908u,
+ 0x08191919u, 0x08081908u, 0x08192b08u, 0x08081908u, 0x082b0819u, 0x08081908u,
+ 0x082b1908u, 0x08081908u, 0x082b192bu, 0x08081908u, 0x082b2b19u, 0x08081908u,
+ 0x19080808u, 0x08081908u, 0x1908082bu, 0x08081908u, 0x19081919u, 0x08081908u,
+ 0x19082b08u, 0x08081908u, 0x19082b2bu, 0x08081908u, 0x19190819u, 0x08081908u,
+ 0x19191908u, 0x08081908u, 0x1919192bu, 0x08081908u, 0x19192b19u, 0x08081908u,
+ 0x192b0808u, 0x08081908u, 0x192b082bu, 0x08081908u, 0x192b1919u, 0x08081908u,
+ 0x2b080819u, 0x08081908u, 0x2b081908u, 0x08081908u, 0x2b08192bu, 0x08081908u,
+ 0x2b082b19u, 0x08081908u, 0x2b190808u, 0x08081908u, 0x2b191919u, 0x08081908u,
+ 0x2b192b08u, 0x08081908u, 0x2b2b0819u, 0x08081908u, 0x2b2b1908u, 0x08081908u,
+ 0x08080808u, 0x08081919u, 0x0808082bu, 0x08081919u, 0x08081919u, 0x08081919u,
+ 0x08082b08u, 0x08081919u, 0x08082b2bu, 0x08081919u, 0x08190819u, 0x08081919u,
+ 0x08191908u, 0x08081919u, 0x0819192bu, 0x08081919u, 0x08192b19u, 0x08081919u,
+ 0x082b0808u, 0x08081919u, 0x082b1919u, 0x08081919u, 0x082b2b08u, 0x08081919u,
+ 0x19080819u, 0x08081919u, 0x19081908u, 0x08081919u, 0x1908192bu, 0x08081919u,
+ 0x19082b19u, 0x08081919u, 0x19190808u, 0x08081919u, 0x1919082bu, 0x08081919u,
+ 0x19191919u, 0x08081919u, 0x19192b08u, 0x08081919u, 0x192b0819u, 0x08081919u,
+ 0x192b1908u, 0x08081919u, 0x2b080808u, 0x08081919u, 0x2b08082bu, 0x08081919u,
+ 0x2b081919u, 0x08081919u, 0x2b082b08u, 0x08081919u, 0x2b190819u, 0x08081919u,
+ 0x2b191908u, 0x08081919u, 0x2b2b0808u, 0x08081919u, 0x08080819u, 0x0808192bu,
+ 0x08081908u, 0x0808192bu, 0x0808192bu, 0x0808192bu, 0x08082b19u, 0x0808192bu,
+ 0x08190808u, 0x0808192bu, 0x08191919u, 0x0808192bu, 0x19080808u, 0x0808192bu,
+ 0x19081919u, 0x0808192bu, 0x19082b08u, 0x0808192bu, 0x19190819u, 0x0808192bu,
+ 0x19191908u, 0x0808192bu, 0x192b0808u, 0x0808192bu, 0x2b080819u, 0x0808192bu,
+ 0x2b081908u, 0x0808192bu, 0x2b190808u, 0x0808192bu, 0x08080808u, 0x08082b08u,
+ 0x0808082bu, 0x08082b08u, 0x08081919u, 0x08082b08u, 0x08082b08u, 0x08082b08u,
+ 0x08190819u, 0x08082b08u, 0x08191908u, 0x08082b08u, 0x0819192bu, 0x08082b08u,
+ 0x08192b19u, 0x08082b08u, 0x082b0808u, 0x08082b08u, 0x082b1919u, 0x08082b08u,
+ 0x082b2b2bu, 0x08082b08u, 0x19080819u, 0x08082b08u, 0x19081908u, 0x08082b08u,
+ 0x1908192bu, 0x08082b08u, 0x19082b19u, 0x08082b08u, 0x19190808u, 0x08082b08u,
+ 0x1919082bu, 0x08082b08u, 0x19191919u, 0x08082b08u, 0x19192b08u, 0x08082b08u,
+ 0x192b0819u, 0x08082b08u, 0x192b1908u, 0x08082b08u, 0x2b080808u, 0x08082b08u,
+ 0x2b081919u, 0x08082b08u, 0x2b191908u, 0x08082b08u, 0x2b2b2b2bu, 0x08082b08u,
+ 0x08080819u, 0x08082b19u, 0x08081908u, 0x08082b19u, 0x08190808u, 0x08082b19u,
+ 0x0819082bu, 0x08082b19u, 0x08191919u, 0x08082b19u, 0x08192b08u, 0x08082b19u,
+ 0x082b0819u, 0x08082b19u, 0x19080808u, 0x08082b19u, 0x19081919u, 0x08082b19u,
+ 0x19082b08u, 0x08082b19u, 0x19190819u, 0x08082b19u, 0x19191908u, 0x08082b19u,
+ 0x192b0808u, 0x08082b19u, 0x2b080819u, 0x08082b19u, 0x2b190808u, 0x08082b19u,
+ 0x08080808u, 0x08082b2bu, 0x08190819u, 0x08082b2bu, 0x08191908u, 0x08082b2bu,
+ 0x082b082bu, 0x08082b2bu, 0x082b2b08u, 0x08082b2bu, 0x082b2b2bu, 0x08082b2bu,
+ 0x19190808u, 0x08082b2bu, 0x2b192b19u, 0x08082b2bu, 0x08080819u, 0x08190808u,
+ 0x08081908u, 0x08190808u, 0x0808192bu, 0x08190808u, 0x08082b19u, 0x08190808u,
+ 0x08190808u, 0x08190808u, 0x0819082bu, 0x08190808u, 0x08191919u, 0x08190808u,
+ 0x08192b08u, 0x08190808u, 0x082b0819u, 0x08190808u, 0x082b1908u, 0x08190808u,
+ 0x082b192bu, 0x08190808u, 0x19080808u, 0x08190808u, 0x1908082bu, 0x08190808u,
+ 0x19081919u, 0x08190808u, 0x19082b08u, 0x08190808u, 0x19190819u, 0x08190808u,
+ 0x19191908u, 0x08190808u, 0x1919192bu, 0x08190808u, 0x19192b19u, 0x08190808u,
+ 0x192b0808u, 0x08190808u, 0x192b082bu, 0x08190808u, 0x192b1919u, 0x08190808u,
+ 0x192b2b08u, 0x08190808u, 0x2b080819u, 0x08190808u, 0x2b081908u, 0x08190808u,
+ 0x2b08192bu, 0x08190808u, 0x2b190808u, 0x08190808u, 0x2b191919u, 0x08190808u,
+ 0x2b192b08u, 0x08190808u, 0x2b2b0819u, 0x08190808u, 0x2b2b1908u, 0x08190808u,
+ 0x08080808u, 0x08190819u, 0x0808082bu, 0x08190819u, 0x08081919u, 0x08190819u,
+ 0x08082b08u, 0x08190819u, 0x08082b2bu, 0x08190819u, 0x08190819u, 0x08190819u,
+ 0x08191908u, 0x08190819u, 0x0819192bu, 0x08190819u, 0x08192b19u, 0x08190819u,
+ 0x082b0808u, 0x08190819u, 0x082b082bu, 0x08190819u, 0x082b1919u, 0x08190819u,
+ 0x082b2b08u, 0x08190819u, 0x19080819u, 0x08190819u, 0x19081908u, 0x08190819u,
+ 0x1908192bu, 0x08190819u, 0x19082b19u, 0x08190819u, 0x19190808u, 0x08190819u,
+ 0x1919082bu, 0x08190819u, 0x19191919u, 0x08190819u, 0x19192b08u, 0x08190819u,
+ 0x192b0819u, 0x08190819u, 0x192b1908u, 0x08190819u, 0x2b080808u, 0x08190819u,
+ 0x2b08082bu, 0x08190819u, 0x2b081919u, 0x08190819u, 0x2b082b08u, 0x08190819u,
+ 0x2b190819u, 0x08190819u, 0x2b191908u, 0x08190819u, 0x08080819u, 0x0819082bu,
+ 0x08081908u, 0x0819082bu, 0x08082b19u, 0x0819082bu, 0x08190808u, 0x0819082bu,
+ 0x08191919u, 0x0819082bu, 0x082b0819u, 0x0819082bu, 0x082b1908u, 0x0819082bu,
+ 0x19080808u, 0x0819082bu, 0x19081919u, 0x0819082bu, 0x19190819u, 0x0819082bu,
+ 0x19191908u, 0x0819082bu, 0x2b080819u, 0x0819082bu, 0x2b081908u, 0x0819082bu,
+ 0x2b190808u, 0x0819082bu, 0x08080808u, 0x08191908u, 0x0808082bu, 0x08191908u,
+ 0x08081919u, 0x08191908u, 0x08082b08u, 0x08191908u, 0x08190819u, 0x08191908u,
+ 0x08191908u, 0x08191908u, 0x0819192bu, 0x08191908u, 0x08192b19u, 0x08191908u,
+ 0x082b0808u, 0x08191908u, 0x082b1919u, 0x08191908u, 0x082b2b08u, 0x08191908u,
+ 0x19080819u, 0x08191908u, 0x19081908u, 0x08191908u, 0x1908192bu, 0x08191908u,
+ 0x19082b19u, 0x08191908u, 0x19190808u, 0x08191908u, 0x1919082bu, 0x08191908u,
+ 0x19191919u, 0x08191908u, 0x19192b08u, 0x08191908u, 0x192b0819u, 0x08191908u,
+ 0x192b1908u, 0x08191908u, 0x2b080808u, 0x08191908u, 0x2b08082bu, 0x08191908u,
+ 0x2b081919u, 0x08191908u, 0x2b082b08u, 0x08191908u, 0x2b190819u, 0x08191908u,
+ 0x2b191908u, 0x08191908u, 0x2b2b0808u, 0x08191908u, 0x08080819u, 0x08191919u,
+ 0x08081908u, 0x08191919u, 0x0808192bu, 0x08191919u, 0x08082b19u, 0x08191919u,
+ 0x08190808u, 0x08191919u, 0x0819082bu, 0x08191919u, 0x08191919u, 0x08191919u,
+ 0x08192b08u, 0x08191919u, 0x082b0819u, 0x08191919u, 0x082b1908u, 0x08191919u,
+ 0x19080808u, 0x08191919u, 0x1908082bu, 0x08191919u, 0x19081919u, 0x08191919u,
+ 0x19082b08u, 0x08191919u, 0x19190819u, 0x08191919u, 0x19191908u, 0x08191919u,
+ 0x192b0808u, 0x08191919u, 0x2b080819u, 0x08191919u, 0x2b081908u, 0x08191919u,
+ 0x2b190808u, 0x08191919u, 0x08080808u, 0x0819192bu, 0x08081919u, 0x0819192bu,
+ 0x08082b08u, 0x0819192bu, 0x08190819u, 0x0819192bu, 0x08191908u, 0x0819192bu,
+ 0x082b0808u, 0x0819192bu, 0x19080819u, 0x0819192bu, 0x19081908u, 0x0819192bu,
+ 0x19190808u, 0x0819192bu, 0x2b080808u, 0x0819192bu, 0x2b2b2b2bu, 0x0819192bu,
+ 0x08080819u, 0x08192b08u, 0x08081908u, 0x08192b08u, 0x0808192bu, 0x08192b08u,
+ 0x08082b19u, 0x08192b08u, 0x08190808u, 0x08192b08u, 0x08191919u, 0x08192b08u,
+ 0x08192b08u, 0x08192b08u, 0x082b0819u, 0x08192b08u, 0x19080808u, 0x08192b08u,
+ 0x1908082bu, 0x08192b08u, 0x19081919u, 0x08192b08u, 0x19082b08u, 0x08192b08u,
+ 0x19190819u, 0x08192b08u, 0x19191908u, 0x08192b08u, 0x192b0808u, 0x08192b08u,
+ 0x2b080819u, 0x08192b08u, 0x2b081908u, 0x08192b08u, 0x08080808u, 0x08192b19u,
+ 0x0808082bu, 0x08192b19u, 0x08081919u, 0x08192b19u, 0x08082b08u, 0x08192b19u,
+ 0x08190819u, 0x08192b19u, 0x08191908u, 0x08192b19u, 0x082b0808u, 0x08192b19u,
+ 0x19080819u, 0x08192b19u, 0x19081908u, 0x08192b19u, 0x19190808u, 0x08192b19u,
+ 0x192b2b19u, 0x08192b19u, 0x2b2b082bu, 0x08192b19u, 0x08081908u, 0x08192b2bu,
+ 0x08190808u, 0x08192b2bu, 0x19080808u, 0x08192b2bu, 0x1919192bu, 0x08192b2bu,
+ 0x08080808u, 0x082b0808u, 0x0808082bu, 0x082b0808u, 0x08081919u, 0x082b0808u,
+ 0x08082b08u, 0x082b0808u, 0x08190819u, 0x082b0808u, 0x08191908u, 0x082b0808u,
+ 0x0819192bu, 0x082b0808u, 0x08192b19u, 0x082b0808u, 0x082b0808u, 0x082b0808u,
+ 0x082b1919u, 0x082b0808u, 0x082b2b2bu, 0x082b0808u, 0x19080819u, 0x082b0808u,
+ 0x19081908u, 0x082b0808u, 0x19190808u, 0x082b0808u, 0x1919082bu, 0x082b0808u,
+ 0x19191919u, 0x082b0808u, 0x192b1908u, 0x082b0808u, 0x2b080808u, 0x082b0808u,
+ 0x2b082b2bu, 0x082b0808u, 0x2b191908u, 0x082b0808u, 0x2b2b2b2bu, 0x082b0808u,
+ 0x08080819u, 0x082b0819u, 0x08081908u, 0x082b0819u, 0x08190808u, 0x082b0819u,
+ 0x0819082bu, 0x082b0819u, 0x08191919u, 0x082b0819u, 0x082b0819u, 0x082b0819u,
+ 0x19080808u, 0x082b0819u, 0x1908082bu, 0x082b0819u, 0x19081919u, 0x082b0819u,
+ 0x19190819u, 0x082b0819u, 0x19191908u, 0x082b0819u, 0x192b0808u, 0x082b0819u,
+ 0x2b080819u, 0x082b0819u, 0x2b081908u, 0x082b0819u, 0x2b190808u, 0x082b0819u,
+ 0x08080808u, 0x082b082bu, 0x08082b2bu, 0x082b082bu, 0x082b082bu, 0x082b082bu,
+ 0x082b2b08u, 0x082b082bu, 0x082b2b2bu, 0x082b082bu, 0x19081908u, 0x082b082bu,
+ 0x19190808u, 0x082b082bu, 0x2b082b08u, 0x082b082bu, 0x2b082b2bu, 0x082b082bu,
+ 0x2b2b2b08u, 0x082b082bu, 0x08080819u, 0x082b1908u, 0x08081908u, 0x082b1908u,
+ 0x0808192bu, 0x082b1908u, 0x08082b19u, 0x082b1908u, 0x08190808u, 0x082b1908u,
+ 0x08191919u, 0x082b1908u, 0x08192b08u, 0x082b1908u, 0x082b0819u, 0x082b1908u,
+ 0x082b1908u, 0x082b1908u, 0x19080808u, 0x082b1908u, 0x1908082bu, 0x082b1908u,
+ 0x19081919u, 0x082b1908u, 0x19082b08u, 0x082b1908u, 0x19190819u, 0x082b1908u,
+ 0x19191908u, 0x082b1908u, 0x192b0808u, 0x082b1908u, 0x2b080819u, 0x082b1908u,
+ 0x2b081908u, 0x082b1908u, 0x2b190808u, 0x082b1908u, 0x08080808u, 0x082b1919u,
+ 0x08081919u, 0x082b1919u, 0x08082b08u, 0x082b1919u, 0x08190819u, 0x082b1919u,
+ 0x08191908u, 0x082b1919u, 0x082b0808u, 0x082b1919u, 0x19080819u, 0x082b1919u,
+ 0x19081908u, 0x082b1919u, 0x19190808u, 0x082b1919u, 0x192b192bu, 0x082b1919u,
+ 0x2b080808u, 0x082b1919u, 0x08080819u, 0x082b192bu, 0x08081908u, 0x082b192bu,
+ 0x08190808u, 0x082b192bu, 0x19080808u, 0x082b192bu, 0x19192b19u, 0x082b192bu,
+ 0x08080808u, 0x082b2b08u, 0x08081919u, 0x082b2b08u, 0x08190819u, 0x082b2b08u,
+ 0x08191908u, 0x082b2b08u, 0x19080819u, 0x082b2b08u, 0x19081908u, 0x082b2b08u,
+ 0x19190808u, 0x082b2b08u, 0x2b082b2bu, 0x082b2b08u, 0x2b2b2b2bu, 0x082b2b08u,
+ 0x08080819u, 0x082b2b19u, 0x08081908u, 0x082b2b19u, 0x08190808u, 0x082b2b19u,
+ 0x2b191919u, 0x082b2b19u, 0x08082b2bu, 0x082b2b2bu, 0x082b082bu, 0x082b2b2bu,
+ 0x192b1908u, 0x082b2b2bu, 0x2b082b08u, 0x082b2b2bu, 0x2b082b2bu, 0x082b2b2bu,
+ 0x08080819u, 0x19080808u, 0x08081908u, 0x19080808u, 0x0808192bu, 0x19080808u,
+ 0x08082b19u, 0x19080808u, 0x08190808u, 0x19080808u, 0x0819082bu, 0x19080808u,
+ 0x08191919u, 0x19080808u, 0x08192b08u, 0x19080808u, 0x08192b2bu, 0x19080808u,
+ 0x082b0819u, 0x19080808u, 0x082b1908u, 0x19080808u, 0x082b192bu, 0x19080808u,
+ 0x19080808u, 0x19080808u, 0x1908082bu, 0x19080808u, 0x19081919u, 0x19080808u,
+ 0x19082b08u, 0x19080808u, 0x19082b2bu, 0x19080808u, 0x19190819u, 0x19080808u,
+ 0x19191908u, 0x19080808u, 0x1919192bu, 0x19080808u, 0x19192b19u, 0x19080808u,
+ 0x192b0808u, 0x19080808u, 0x192b082bu, 0x19080808u, 0x192b1919u, 0x19080808u,
+ 0x2b080819u, 0x19080808u, 0x2b081908u, 0x19080808u, 0x2b190808u, 0x19080808u,
+ 0x2b191919u, 0x19080808u, 0x2b192b08u, 0x19080808u, 0x2b2b0819u, 0x19080808u,
+ 0x2b2b1908u, 0x19080808u, 0x08080808u, 0x19080819u, 0x0808082bu, 0x19080819u,
+ 0x08081919u, 0x19080819u, 0x08082b08u, 0x19080819u, 0x08190819u, 0x19080819u,
+ 0x08191908u, 0x19080819u, 0x0819192bu, 0x19080819u, 0x08192b19u, 0x19080819u,
+ 0x082b0808u, 0x19080819u, 0x082b082bu, 0x19080819u, 0x082b1919u, 0x19080819u,
+ 0x19080819u, 0x19080819u, 0x19081908u, 0x19080819u, 0x1908192bu, 0x19080819u,
+ 0x19082b19u, 0x19080819u, 0x19190808u, 0x19080819u, 0x1919082bu, 0x19080819u,
+ 0x19191919u, 0x19080819u, 0x19192b08u, 0x19080819u, 0x192b0819u, 0x19080819u,
+ 0x192b1908u, 0x19080819u, 0x2b080808u, 0x19080819u, 0x2b08082bu, 0x19080819u,
+ 0x2b081919u, 0x19080819u, 0x2b082b08u, 0x19080819u, 0x2b190819u, 0x19080819u,
+ 0x2b191908u, 0x19080819u, 0x2b2b0808u, 0x19080819u, 0x08080819u, 0x1908082bu,
+ 0x08081908u, 0x1908082bu, 0x08190808u, 0x1908082bu, 0x0819082bu, 0x1908082bu,
+ 0x08191919u, 0x1908082bu, 0x08192b08u, 0x1908082bu, 0x082b1908u, 0x1908082bu,
+ 0x19080808u, 0x1908082bu, 0x19081919u, 0x1908082bu, 0x19082b08u, 0x1908082bu,
+ 0x19190819u, 0x1908082bu, 0x19191908u, 0x1908082bu, 0x192b0808u, 0x1908082bu,
+ 0x2b080819u, 0x1908082bu, 0x2b081908u, 0x1908082bu, 0x08080808u, 0x19081908u,
+ 0x0808082bu, 0x19081908u, 0x08081919u, 0x19081908u, 0x08082b08u, 0x19081908u,
+ 0x08082b2bu, 0x19081908u, 0x08190819u, 0x19081908u, 0x08191908u, 0x19081908u,
+ 0x0819192bu, 0x19081908u, 0x08192b19u, 0x19081908u, 0x082b0808u, 0x19081908u,
+ 0x082b082bu, 0x19081908u, 0x082b1919u, 0x19081908u, 0x082b2b08u, 0x19081908u,
+ 0x19080819u, 0x19081908u, 0x19081908u, 0x19081908u, 0x1908192bu, 0x19081908u,
+ 0x19082b19u, 0x19081908u, 0x19190808u, 0x19081908u, 0x1919082bu, 0x19081908u,
+ 0x19191919u, 0x19081908u, 0x19192b08u, 0x19081908u, 0x192b0819u, 0x19081908u,
+ 0x192b1908u, 0x19081908u, 0x2b080808u, 0x19081908u, 0x2b08082bu, 0x19081908u,
+ 0x2b081919u, 0x19081908u, 0x2b082b08u, 0x19081908u, 0x2b190819u, 0x19081908u,
+ 0x2b191908u, 0x19081908u, 0x2b2b0808u, 0x19081908u, 0x08080819u, 0x19081919u,
+ 0x08081908u, 0x19081919u, 0x0808192bu, 0x19081919u, 0x08082b19u, 0x19081919u,
+ 0x08190808u, 0x19081919u, 0x0819082bu, 0x19081919u, 0x08191919u, 0x19081919u,
+ 0x08192b08u, 0x19081919u, 0x082b0819u, 0x19081919u, 0x082b1908u, 0x19081919u,
+ 0x19080808u, 0x19081919u, 0x1908082bu, 0x19081919u, 0x19081919u, 0x19081919u,
+ 0x19082b08u, 0x19081919u, 0x19190819u, 0x19081919u, 0x19191908u, 0x19081919u,
+ 0x192b0808u, 0x19081919u, 0x192b2b2bu, 0x19081919u, 0x2b080819u, 0x19081919u,
+ 0x2b081908u, 0x19081919u, 0x2b190808u, 0x19081919u, 0x08080808u, 0x1908192bu,
+ 0x0808082bu, 0x1908192bu, 0x08081919u, 0x1908192bu, 0x08082b08u, 0x1908192bu,
+ 0x08190819u, 0x1908192bu, 0x08191908u, 0x1908192bu, 0x082b0808u, 0x1908192bu,
+ 0x19080819u, 0x1908192bu, 0x19081908u, 0x1908192bu, 0x19190808u, 0x1908192bu,
+ 0x2b080808u, 0x1908192bu, 0x2b2b1919u, 0x1908192bu, 0x08080819u, 0x19082b08u,
+ 0x08081908u, 0x19082b08u, 0x08082b19u, 0x19082b08u, 0x08190808u, 0x19082b08u,
+ 0x0819082bu, 0x19082b08u, 0x08191919u, 0x19082b08u, 0x08192b08u, 0x19082b08u,
+ 0x082b0819u, 0x19082b08u, 0x082b1908u, 0x19082b08u, 0x19080808u, 0x19082b08u,
+ 0x1908082bu, 0x19082b08u, 0x19081919u, 0x19082b08u, 0x19082b08u, 0x19082b08u,
+ 0x19190819u, 0x19082b08u, 0x19191908u, 0x19082b08u, 0x192b0808u, 0x19082b08u,
+ 0x2b081908u, 0x19082b08u, 0x2b190808u, 0x19082b08u, 0x08080808u, 0x19082b19u,
+ 0x0808082bu, 0x19082b19u, 0x08081919u, 0x19082b19u, 0x08082b08u, 0x19082b19u,
+ 0x08190819u, 0x19082b19u, 0x08191908u, 0x19082b19u, 0x082b0808u, 0x19082b19u,
+ 0x19080819u, 0x19082b19u, 0x19081908u, 0x19082b19u, 0x19190808u, 0x19082b19u,
+ 0x2b080808u, 0x19082b19u, 0x2b19192bu, 0x19082b19u, 0x08080819u, 0x19082b2bu,
+ 0x08081908u, 0x19082b2bu, 0x08190808u, 0x19082b2bu, 0x19080808u, 0x19082b2bu,
+ 0x08080808u, 0x19190808u, 0x0808082bu, 0x19190808u, 0x08081919u, 0x19190808u,
+ 0x08082b08u, 0x19190808u, 0x08190819u, 0x19190808u, 0x08191908u, 0x19190808u,
+ 0x0819192bu, 0x19190808u, 0x08192b19u, 0x19190808u, 0x082b0808u, 0x19190808u,
+ 0x082b082bu, 0x19190808u, 0x082b1919u, 0x19190808u, 0x082b2b08u, 0x19190808u,
+ 0x19080819u, 0x19190808u, 0x19081908u, 0x19190808u, 0x1908192bu, 0x19190808u,
+ 0x19082b19u, 0x19190808u, 0x19190808u, 0x19190808u, 0x1919082bu, 0x19190808u,
+ 0x19191919u, 0x19190808u, 0x19192b08u, 0x19190808u, 0x192b0819u, 0x19190808u,
+ 0x192b1908u, 0x19190808u, 0x2b080808u, 0x19190808u, 0x2b08082bu, 0x19190808u,
+ 0x2b081919u, 0x19190808u, 0x2b082b08u, 0x19190808u, 0x2b190819u, 0x19190808u,
+ 0x2b191908u, 0x19190808u, 0x08080819u, 0x19190819u, 0x08081908u, 0x19190819u,
+ 0x0808192bu, 0x19190819u, 0x08082b19u, 0x19190819u, 0x08190808u, 0x19190819u,
+ 0x0819082bu, 0x19190819u, 0x08191919u, 0x19190819u, 0x08192b08u, 0x19190819u,
+ 0x082b0819u, 0x19190819u, 0x082b1908u, 0x19190819u, 0x19080808u, 0x19190819u,
+ 0x1908082bu, 0x19190819u, 0x19081919u, 0x19190819u, 0x19082b08u, 0x19190819u,
+ 0x19190819u, 0x19190819u, 0x19191908u, 0x19190819u, 0x192b0808u, 0x19190819u,
+ 0x2b080819u, 0x19190819u, 0x2b081908u, 0x19190819u, 0x2b190808u, 0x19190819u,
+ 0x08080808u, 0x1919082bu, 0x08081919u, 0x1919082bu, 0x08082b08u, 0x1919082bu,
+ 0x08190819u, 0x1919082bu, 0x08191908u, 0x1919082bu, 0x082b0808u, 0x1919082bu,
+ 0x19080819u, 0x1919082bu, 0x19081908u, 0x1919082bu, 0x19190808u, 0x1919082bu,
+ 0x192b2b19u, 0x1919082bu, 0x2b080808u, 0x1919082bu, 0x08080819u, 0x19191908u,
+ 0x08081908u, 0x19191908u, 0x0808192bu, 0x19191908u, 0x08082b19u, 0x19191908u,
+ 0x08190808u, 0x19191908u, 0x0819082bu, 0x19191908u, 0x08191919u, 0x19191908u,
+ 0x08192b08u, 0x19191908u, 0x082b0819u, 0x19191908u, 0x082b1908u, 0x19191908u,
+ 0x19080808u, 0x19191908u, 0x1908082bu, 0x19191908u, 0x19081919u, 0x19191908u,
+ 0x19082b08u, 0x19191908u, 0x19190819u, 0x19191908u, 0x19191908u, 0x19191908u,
+ 0x192b0808u, 0x19191908u, 0x2b080819u, 0x19191908u, 0x2b081908u, 0x19191908u,
+ 0x2b190808u, 0x19191908u, 0x08080808u, 0x19191919u, 0x0808082bu, 0x19191919u,
+ 0x08081919u, 0x19191919u, 0x08082b08u, 0x19191919u, 0x08190819u, 0x19191919u,
+ 0x08191908u, 0x19191919u, 0x082b0808u, 0x19191919u, 0x19080819u, 0x19191919u,
+ 0x19081908u, 0x19191919u, 0x19190808u, 0x19191919u, 0x2b080808u, 0x19191919u,
+ 0x08080819u, 0x1919192bu, 0x08081908u, 0x1919192bu, 0x08190808u, 0x1919192bu,
+ 0x082b192bu, 0x1919192bu, 0x19080808u, 0x1919192bu, 0x08080808u, 0x19192b08u,
+ 0x0808082bu, 0x19192b08u, 0x08081919u, 0x19192b08u, 0x08082b08u, 0x19192b08u,
+ 0x08190819u, 0x19192b08u, 0x08191908u, 0x19192b08u, 0x082b0808u, 0x19192b08u,
+ 0x19080819u, 0x19192b08u, 0x19081908u, 0x19192b08u, 0x19190808u, 0x19192b08u,
+ 0x19192b2bu, 0x19192b08u, 0x2b080808u, 0x19192b08u, 0x08080819u, 0x19192b19u,
+ 0x08081908u, 0x19192b19u, 0x08190808u, 0x19192b19u, 0x19080808u, 0x19192b19u,
+ 0x08080808u, 0x19192b2bu, 0x08192b19u, 0x19192b2bu, 0x2b081919u, 0x19192b2bu,
+ 0x2b2b2b08u, 0x19192b2bu, 0x08080819u, 0x192b0808u, 0x08081908u, 0x192b0808u,
+ 0x0808192bu, 0x192b0808u, 0x08190808u, 0x192b0808u, 0x0819082bu, 0x192b0808u,
+ 0x08191919u, 0x192b0808u, 0x08192b08u, 0x192b0808u, 0x082b0819u, 0x192b0808u,
+ 0x082b1908u, 0x192b0808u, 0x19080808u, 0x192b0808u, 0x19081919u, 0x192b0808u,
+ 0x19082b08u, 0x192b0808u, 0x19190819u, 0x192b0808u, 0x19191908u, 0x192b0808u,
+ 0x192b0808u, 0x192b0808u, 0x2b081908u, 0x192b0808u, 0x2b190808u, 0x192b0808u,
+ 0x08080808u, 0x192b0819u, 0x0808082bu, 0x192b0819u, 0x08081919u, 0x192b0819u,
+ 0x08082b08u, 0x192b0819u, 0x08190819u, 0x192b0819u, 0x08191908u, 0x192b0819u,
+ 0x082b0808u, 0x192b0819u, 0x19080819u, 0x192b0819u, 0x19081908u, 0x192b0819u,
+ 0x19190808u, 0x192b0819u, 0x2b080808u, 0x192b0819u, 0x2b192b19u, 0x192b0819u,
+ 0x08081908u, 0x192b082bu, 0x08190808u, 0x192b082bu, 0x19080808u, 0x192b082bu,
+ 0x1919192bu, 0x192b082bu, 0x2b2b0819u, 0x192b082bu, 0x08080808u, 0x192b1908u,
+ 0x08081919u, 0x192b1908u, 0x08082b08u, 0x192b1908u, 0x08190819u, 0x192b1908u,
+ 0x08191908u, 0x192b1908u, 0x082b0808u, 0x192b1908u, 0x19080819u, 0x192b1908u,
+ 0x19081908u, 0x192b1908u, 0x19190808u, 0x192b1908u, 0x2b080808u, 0x192b1908u,
+ 0x08080819u, 0x192b1919u, 0x08081908u, 0x192b1919u, 0x08190808u, 0x192b1919u,
+ 0x19080808u, 0x192b1919u, 0x19082b2bu, 0x192b1919u, 0x192b2b08u, 0x192b1919u,
+ 0x2b19082bu, 0x192b1919u, 0x08080808u, 0x192b192bu, 0x2b191908u, 0x192b192bu,
+ 0x08080819u, 0x192b2b08u, 0x08081908u, 0x192b2b08u, 0x08190808u, 0x192b2b08u,
+ 0x192b1919u, 0x192b2b08u, 0x2b192b08u, 0x192b2b08u, 0x08080808u, 0x192b2b19u,
+ 0x082b2b2bu, 0x192b2b19u, 0x1908082bu, 0x192b2b2bu, 0x2b2b0819u, 0x192b2b2bu,
+ 0x08080808u, 0x2b080808u, 0x0808082bu, 0x2b080808u, 0x08081919u, 0x2b080808u,
+ 0x08082b08u, 0x2b080808u, 0x08190819u, 0x2b080808u, 0x08191908u, 0x2b080808u,
+ 0x08192b19u, 0x2b080808u, 0x082b0808u, 0x2b080808u, 0x082b1919u, 0x2b080808u,
+ 0x19080819u, 0x2b080808u, 0x19081908u, 0x2b080808u, 0x19190808u, 0x2b080808u,
+ 0x1919082bu, 0x2b080808u, 0x19191919u, 0x2b080808u, 0x19192b08u, 0x2b080808u,
+ 0x192b0819u, 0x2b080808u, 0x2b080808u, 0x2b080808u, 0x2b081919u, 0x2b080808u,
+ 0x2b190819u, 0x2b080808u, 0x2b191908u, 0x2b080808u, 0x08080819u, 0x2b080819u,
+ 0x08081908u, 0x2b080819u, 0x08082b19u, 0x2b080819u, 0x08190808u, 0x2b080819u,
+ 0x0819082bu, 0x2b080819u, 0x08191919u, 0x2b080819u, 0x08192b08u, 0x2b080819u,
+ 0x082b0819u, 0x2b080819u, 0x082b1908u, 0x2b080819u, 0x19080808u, 0x2b080819u,
+ 0x1908082bu, 0x2b080819u, 0x19081919u, 0x2b080819u, 0x19082b08u, 0x2b080819u,
+ 0x19190819u, 0x2b080819u, 0x19191908u, 0x2b080819u, 0x2b080819u, 0x2b080819u,
+ 0x2b081908u, 0x2b080819u, 0x2b190808u, 0x2b080819u, 0x2b2b2b19u, 0x2b080819u,
+ 0x08080808u, 0x2b08082bu, 0x08081919u, 0x2b08082bu, 0x08082b2bu, 0x2b08082bu,
+ 0x08190819u, 0x2b08082bu, 0x08191908u, 0x2b08082bu, 0x19080819u, 0x2b08082bu,
+ 0x19081908u, 0x2b08082bu, 0x19190808u, 0x2b08082bu, 0x08080819u, 0x2b081908u,
+ 0x08081908u, 0x2b081908u, 0x0808192bu, 0x2b081908u, 0x08082b19u, 0x2b081908u,
+ 0x08190808u, 0x2b081908u, 0x0819082bu, 0x2b081908u, 0x08191919u, 0x2b081908u,
+ 0x08192b08u, 0x2b081908u, 0x082b0819u, 0x2b081908u, 0x19080808u, 0x2b081908u,
+ 0x1908082bu, 0x2b081908u, 0x19081919u, 0x2b081908u, 0x19082b08u, 0x2b081908u,
+ 0x19190819u, 0x2b081908u, 0x19191908u, 0x2b081908u, 0x192b0808u, 0x2b081908u,
+ 0x2b080819u, 0x2b081908u, 0x2b081908u, 0x2b081908u, 0x2b190808u, 0x2b081908u,
+ 0x08080808u, 0x2b081919u, 0x0808082bu, 0x2b081919u, 0x08081919u, 0x2b081919u,
+ 0x08082b08u, 0x2b081919u, 0x08190819u, 0x2b081919u, 0x08191908u, 0x2b081919u,
+ 0x082b0808u, 0x2b081919u, 0x19080819u, 0x2b081919u, 0x19081908u, 0x2b081919u,
+ 0x19190808u, 0x2b081919u, 0x2b080808u, 0x2b081919u, 0x2b082b2bu, 0x2b081919u,
+ 0x08080819u, 0x2b08192bu, 0x08081908u, 0x2b08192bu, 0x08190808u, 0x2b08192bu,
+ 0x082b2b19u, 0x2b08192bu, 0x19080808u, 0x2b08192bu, 0x08080808u, 0x2b082b08u,
+ 0x08081919u, 0x2b082b08u, 0x08190819u, 0x2b082b08u, 0x08191908u, 0x2b082b08u,
+ 0x19080819u, 0x2b082b08u, 0x19081908u, 0x2b082b08u, 0x19190808u, 0x2b082b08u,
+ 0x2b2b082bu, 0x2b082b08u, 0x08080819u, 0x2b082b19u, 0x08081908u, 0x2b082b19u,
+ 0x19080808u, 0x2b082b19u, 0x192b1919u, 0x2b082b19u, 0x082b082bu, 0x2b082b2bu,
+ 0x19192b08u, 0x2b082b2bu, 0x19192b2bu, 0x2b082b2bu, 0x2b08082bu, 0x2b082b2bu,
+ 0x2b2b082bu, 0x2b082b2bu, 0x08080819u, 0x2b190808u, 0x08081908u, 0x2b190808u,
+ 0x08082b19u, 0x2b190808u, 0x08190808u, 0x2b190808u, 0x0819082bu, 0x2b190808u,
+ 0x08191919u, 0x2b190808u, 0x08192b08u, 0x2b190808u, 0x082b1908u, 0x2b190808u,
+ 0x19080808u, 0x2b190808u, 0x1908082bu, 0x2b190808u, 0x19081919u, 0x2b190808u,
+ 0x19082b08u, 0x2b190808u, 0x19190819u, 0x2b190808u, 0x19191908u, 0x2b190808u,
+ 0x192b0808u, 0x2b190808u, 0x2b080819u, 0x2b190808u, 0x2b081908u, 0x2b190808u,
+ 0x2b190808u, 0x2b190808u, 0x08080808u, 0x2b190819u, 0x08081919u, 0x2b190819u,
+ 0x08190819u, 0x2b190819u, 0x08191908u, 0x2b190819u, 0x19080819u, 0x2b190819u,
+ 0x19081908u, 0x2b190819u, 0x19190808u, 0x2b190819u, 0x19192b2bu, 0x2b190819u,
+ 0x08080819u, 0x2b19082bu, 0x08081908u, 0x2b19082bu, 0x08190808u, 0x2b19082bu,
+ 0x19080808u, 0x2b19082bu, 0x2b2b192bu, 0x2b19082bu, 0x08080808u, 0x2b191908u,
+ 0x0808082bu, 0x2b191908u, 0x08081919u, 0x2b191908u, 0x08082b08u, 0x2b191908u,
+ 0x08190819u, 0x2b191908u, 0x08191908u, 0x2b191908u, 0x082b0808u, 0x2b191908u,
+ 0x19080819u, 0x2b191908u, 0x19081908u, 0x2b191908u, 0x19190808u, 0x2b191908u,
+ 0x2b080808u, 0x2b191908u, 0x2b19192bu, 0x2b191908u, 0x08080819u, 0x2b191919u,
+ 0x08081908u, 0x2b191919u, 0x08190808u, 0x2b191919u, 0x19080808u, 0x2b191919u,
+ 0x2b192b08u, 0x2b191919u, 0x2b2b0819u, 0x2b191919u, 0x08080808u, 0x2b19192bu,
+ 0x1908192bu, 0x2b19192bu, 0x192b1908u, 0x2b19192bu, 0x08080819u, 0x2b192b08u,
+ 0x08081908u, 0x2b192b08u, 0x08190808u, 0x2b192b08u, 0x082b192bu, 0x2b192b08u,
+ 0x19080808u, 0x2b192b08u, 0x2b2b2b19u, 0x2b192b08u, 0x08080808u, 0x2b192b19u,
+ 0x19082b19u, 0x2b192b19u, 0x1919082bu, 0x2b192b19u, 0x2b190808u, 0x2b192b2bu,
+ 0x08080808u, 0x2b2b0808u, 0x08081919u, 0x2b2b0808u, 0x08082b2bu, 0x2b2b0808u,
+ 0x08191908u, 0x2b2b0808u, 0x082b082bu, 0x2b2b0808u, 0x082b2b2bu, 0x2b2b0808u,
+ 0x19080819u, 0x2b2b0808u, 0x19081908u, 0x2b2b0808u, 0x19190808u, 0x2b2b0808u,
+ 0x2b2b082bu, 0x2b2b0808u, 0x2b2b2b2bu, 0x2b2b0808u, 0x19080808u, 0x2b2b0819u,
+ 0x192b1919u, 0x2b2b0819u, 0x0808082bu, 0x2b2b082bu, 0x08082b2bu, 0x2b2b082bu,
+ 0x082b082bu, 0x2b2b082bu, 0x082b2b08u, 0x2b2b082bu, 0x082b2b2bu, 0x2b2b082bu,
+ 0x2b08082bu, 0x2b2b082bu, 0x2b082b08u, 0x2b2b082bu, 0x2b082b2bu, 0x2b2b082bu,
+ 0x2b2b2b08u, 0x2b2b082bu, 0x08080819u, 0x2b2b1908u, 0x08081908u, 0x2b2b1908u,
+ 0x08190808u, 0x2b2b1908u, 0x19080808u, 0x2b2b1908u, 0x2b082b19u, 0x2b2b1908u,
+ 0x2b2b1908u, 0x2b2b1908u, 0x08080808u, 0x2b2b1919u, 0x08192b19u, 0x2b2b1919u,
+ 0x19190819u, 0x2b2b192bu, 0x08082b2bu, 0x2b2b2b08u, 0x082b2b08u, 0x2b2b2b08u,
+ 0x2b2b082bu, 0x2b2b2b08u, 0x19191908u, 0x2b2b2b19u, 0x2b08192bu, 0x2b2b2b19u,
+ 0x08082b08u, 0x2b2b2b2bu, 0x08082b2bu, 0x2b2b2b2bu, 0x082b0808u, 0x2b2b2b2bu,
+ 0x082b082bu, 0x2b2b2b2bu, 0x082b2b08u, 0x2b2b2b2bu, 0x2b082b08u, 0x2b2b2b2bu,
+ 0x2b2b2b2bu, 0x2b2b2b2bu);
+
+int dp(uint a, uint b) { return dotPacked4x8EXT(int(a), int(b)); }
+uvec4 bc(uvec4 xr[NX], uint k) { return subgroupBroadcast(xr[k / SG], k % SG); }
+
+/* byte i, and the 16-bit word at byte i, of the block's realigned words */
+uint byte_at(uint wd[NW], uint i) { return (wd[i >> 2] >> (8u * (i & 3u))) & 0xffu; }
+uint half_at(uint wd[NW], uint i) { return byte_at(wd, i) | (byte_at(wd, i + 1u) << 8); }
+
+/* a 7-bit sign index with its eighth bit, the parity */
+uint signs7(uint i) { return i | ((uint(bitCount(i)) & 1u) << 7); }
+
+/* four levels of a grid word, each negated where its sign bit is set (bits
+ 0-3 of s), as signed bytes */
+uint signed4(uint g, uint s)
+{
+ uint r = 0u;
+ for (uint i = 0; i < 4u; i++) {
+ int v = int((g >> (8u * i)) & 0xffu);
+ if (((s >> i) & 1u) != 0u)
+ v = -v;
+ r |= (uint(v) & 0xffu) << (8u * i);
+ }
+ return r;
+}
+
+/* the grid in the workgroup's shared memory, two words an entry: a
+ constant array read at a varying index is far slower on some GPUs
+ (iq4xs_matvec.comp) */
+shared uint GS[2u * NG];
+
+void main()
+{
+ for (uint i = gl_LocalInvocationID.x; i < 2u * NG; i += 64u)
+ GS[i] = KIND == 0u ? IQ2XXS_GRID[i] : KIND == 1u ? IQ2XS_GRID[i] : IQ2S_GRID[i];
+ barrier();
+ U1 W = U1(p.w);
+ U4 X = U4(p.x);
+ F Y = F(p.y);
+ uint lane = gl_SubgroupInvocationID;
+ /* workgroup y: 64 rows; x: a group of NV vectors, so that the groups
+ of the same rows go out together and the weights the first reads
+ from memory the others find in the L3 */
+ uint grow = gl_WorkGroupID.y * 64u + gl_LocalInvocationID.x;
+ uint row = min(grow, p.rows - 1u);
+ uint v0 = gl_WorkGroupID.x * NV;
+ uint xo[NV];
+ for (uint v = 0; v < NV; v++)
+ xo[v] = min(v0 + v, p.nv - 1u) * p.xs * 20u;
+ float acc[NV];
+ for (uint v = 0; v < NV; v++)
+ acc[v] = 0.0;
+ uint off = row * p.nb * BS; /* bytes */
+ for (uint b = 0; b < p.nb; b++, off += BS) {
+ uint base = off >> 2;
+ uint wd[NW];
+ for (uint k = 0; k < NW; k++)
+ wd[k] = W.v[base + k];
+ if ((off & 3u) != 0u) {
+ for (uint k = 0; k + 1u < NW; k++)
+ wd[k] = (wd[k] >> 16) | (wd[k + 1u] << 16);
+ wd[NW - 1u] = wd[NW - 1u] >> 16;
+ }
+ float d = unpackHalf2x16(wd[0] & 0xffffu).x;
+ /* each sub-group of 8 two signed words, and its scale */
+ uint q[64];
+ int sc[32];
+ for (uint g = 0; g < 8u; g++)
+ for (uint k = 0; k < 4u; k++) {
+ uint l = 4u * g + k, idx, sg;
+ if (KIND == 0u) {
+ uint a0 = byte_at(wd, 2u + 8u * g + k);
+ uint a1 = half_at(wd, 6u + 8u * g) | (half_at(wd, 8u + 8u * g) << 16);
+ idx = a0;
+ sg = signs7((a1 >> (7u * k)) & 127u);
+ sc[l] = 1 + 2 * int(a1 >> 28);
+ } else if (KIND == 1u) {
+ uint qw = half_at(wd, 2u + 2u * l);
+ idx = qw & 511u;
+ sg = signs7(qw >> 9);
+ sc[l] = 1 + 2 * int((byte_at(wd, 66u + g) >> (4u * (k / 2u))) & 15u);
+ } else {
+ idx = byte_at(wd, 2u + l) | ((byte_at(wd, 66u + g) << (8u - 2u * k)) & 0x300u);
+ sg = byte_at(wd, 34u + l);
+ sc[l] = 1 + 2 * int((byte_at(wd, 74u + g) >> (4u * (k / 2u))) & 15u);
+ }
+ q[2u * l] = signed4(GS[2u * idx], sg & 15u);
+ q[2u * l + 1u] = signed4(GS[2u * idx + 1u], sg >> 4);
+ }
+ for (uint v = 0; v < NV; v++) {
+ uvec4 xr[NX];
+ for (uint j = 0; j < NX; j++)
+ xr[j] = X.v[xo[v] + min(j * SG + lane, 19u)];
+ int isum = 0;
+ for (uint g = 0; g < 8u; g++) {
+ uvec4 a0 = bc(xr, 2u * g), a1 = bc(xr, 2u * g + 1u);
+ uint l = 4u * g;
+ isum += sc[l] * (dp(q[2u * l], a0.x) + dp(q[2u * l + 1u], a0.y));
+ isum += sc[l + 1u] * (dp(q[2u * l + 2u], a0.z) + dp(q[2u * l + 3u], a0.w));
+ isum += sc[l + 2u] * (dp(q[2u * l + 4u], a1.x) + dp(q[2u * l + 5u], a1.y));
+ isum += sc[l + 3u] * (dp(q[2u * l + 6u], a1.z) + dp(q[2u * l + 7u], a1.w));
+ }
+ float d8 = uintBitsToFloat(bc(xr, 18u).x);
+ acc[v] = fma(d * 0.125 * d8, float(isum), acc[v]);
+ xo[v] += 20u;
+ }
+ }
+ if (grow < p.rows)
+ for (uint v = 0; v < NV; v++)
+ if (v0 + v < p.nv)
+ Y.v[(v0 + v) * p.ys + row] = acc[v];
+}
diff --git a/src/llm/shaders/iq3xxs_matvec.comp b/src/llm/shaders/iq3xxs_matvec.comp
new file mode 100644
index 0000000..8cc2c2b
--- /dev/null
+++ b/src/llm/shaders/iq3xxs_matvec.comp
@@ -0,0 +1,168 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri
+ The table IQ3XXS_GRID below is the IQ3_XXS grid of ggml
+ (ggml/src/ggml-common.h, iq3xxs_grid), copied as data from
+ src/llm/iq3xxs_grid.h: it defines the format. Copyright (c) 2023-2026
+ The ggml authors, under the MIT licence (see the ggml repository).
+ Generated - not formatted by hand. */
+#version 450
+// IQ3_XXS weights times up to NV Q8_K vectors, one row per invocation, as
+// janas_iq3xxs_dot_ref computes them: 98 bytes a block (d, 64 grid indices
+// of four levels each, then for each group of 32 a word of four 7-bit sign
+// indices - the eighth bit their parity, ggml's ksigns_iq2xs - and the
+// scale in its top four bits), each group's integer dot times 1 + 2 s, the
+// block's exact integer sum in one fma with (d / 4) d8: the CPU's float
+// operations, so the results agree bit for bit. Blocks start on 2-byte
+// boundaries: their words are realigned. Activations shared across the
+// subgroup as in q4k_matvec.comp.
+#extension GL_EXT_integer_dot_product : require
+#extension GL_KHR_shader_subgroup_basic : require
+#extension GL_KHR_shader_subgroup_ballot : require
+#extension GL_EXT_buffer_reference : require
+#extension GL_EXT_buffer_reference_uvec2 : require
+
+layout(local_size_x = 64) in;
+layout(constant_id = 0) const uint NV = 8;
+layout(constant_id = 1) const uint SG = 8;
+const uint NX = (19u + SG - 1u) / SG;
+const uint BS = 98u, NW = (BS + 3u) / 4u + 1u;
+
+layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer U1 { uint v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 16) readonly buffer U4 { uvec4 v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) writeonly buffer F { float v[]; };
+
+layout(push_constant) uniform P { uvec2 w, x, y; uint rows, nb, nv, xs, ys; } p;
+
+const uint IQ3XXS_GRID[256] = uint[256](
+ 0x04040404u, 0x04040414u, 0x04040424u, 0x04040c0cu, 0x04040c1cu, 0x04040c3eu,
+ 0x04041404u, 0x04041414u, 0x04041c0cu, 0x04042414u, 0x04043e1cu, 0x04043e2cu,
+ 0x040c040cu, 0x040c041cu, 0x040c0c04u, 0x040c0c14u, 0x040c140cu, 0x040c142cu,
+ 0x040c1c04u, 0x040c1c14u, 0x040c240cu, 0x040c2c24u, 0x040c3e04u, 0x04140404u,
+ 0x04140414u, 0x04140424u, 0x04140c0cu, 0x04141404u, 0x04141414u, 0x04141c0cu,
+ 0x04141c1cu, 0x04141c3eu, 0x04142c0cu, 0x04142c3eu, 0x04143e2cu, 0x041c040cu,
+ 0x041c043eu, 0x041c0c04u, 0x041c0c14u, 0x041c142cu, 0x041c3e04u, 0x04240c1cu,
+ 0x04241c3eu, 0x04242424u, 0x04242c3eu, 0x04243e1cu, 0x04243e2cu, 0x042c040cu,
+ 0x042c043eu, 0x042c1c14u, 0x042c2c14u, 0x04341c2cu, 0x04343424u, 0x043e0c04u,
+ 0x043e0c24u, 0x043e0c34u, 0x043e241cu, 0x043e340cu, 0x0c04040cu, 0x0c04041cu,
+ 0x0c040c04u, 0x0c040c14u, 0x0c04140cu, 0x0c04141cu, 0x0c041c04u, 0x0c041c14u,
+ 0x0c041c24u, 0x0c04243eu, 0x0c042c04u, 0x0c0c0404u, 0x0c0c0414u, 0x0c0c0c0cu,
+ 0x0c0c1404u, 0x0c0c1414u, 0x0c14040cu, 0x0c14041cu, 0x0c140c04u, 0x0c140c14u,
+ 0x0c14140cu, 0x0c141c04u, 0x0c143e14u, 0x0c1c0404u, 0x0c1c0414u, 0x0c1c1404u,
+ 0x0c1c1c0cu, 0x0c1c2434u, 0x0c1c3434u, 0x0c24040cu, 0x0c24042cu, 0x0c242c04u,
+ 0x0c2c1404u, 0x0c2c1424u, 0x0c2c2434u, 0x0c2c3e0cu, 0x0c34042cu, 0x0c3e1414u,
+ 0x0c3e2404u, 0x14040404u, 0x14040414u, 0x14040c0cu, 0x14040c1cu, 0x14041404u,
+ 0x14041414u, 0x14041434u, 0x14041c0cu, 0x14042414u, 0x140c040cu, 0x140c041cu,
+ 0x140c042cu, 0x140c0c04u, 0x140c0c14u, 0x140c140cu, 0x140c1c04u, 0x140c341cu,
+ 0x140c343eu, 0x140c3e04u, 0x14140404u, 0x14140414u, 0x14140c0cu, 0x14140c3eu,
+ 0x14141404u, 0x14141414u, 0x14141c3eu, 0x14142404u, 0x14142c2cu, 0x141c040cu,
+ 0x141c0c04u, 0x141c0c24u, 0x141c3e04u, 0x141c3e24u, 0x14241c2cu, 0x14242c1cu,
+ 0x142c041cu, 0x142c143eu, 0x142c240cu, 0x142c3e24u, 0x143e040cu, 0x143e041cu,
+ 0x143e0c34u, 0x143e242cu, 0x1c04040cu, 0x1c040c04u, 0x1c040c14u, 0x1c04140cu,
+ 0x1c04141cu, 0x1c042c04u, 0x1c04342cu, 0x1c043e14u, 0x1c0c0404u, 0x1c0c0414u,
+ 0x1c0c1404u, 0x1c0c1c0cu, 0x1c0c2424u, 0x1c0c2434u, 0x1c14040cu, 0x1c14041cu,
+ 0x1c140c04u, 0x1c14142cu, 0x1c142c14u, 0x1c143e14u, 0x1c1c0c0cu, 0x1c1c1c1cu,
+ 0x1c241c04u, 0x1c24243eu, 0x1c243e14u, 0x1c2c0404u, 0x1c2c0434u, 0x1c2c1414u,
+ 0x1c2c2c2cu, 0x1c340c24u, 0x1c341c34u, 0x1c34341cu, 0x1c3e1c1cu, 0x1c3e3404u,
+ 0x24040424u, 0x24040c3eu, 0x24041c2cu, 0x24041c3eu, 0x24042c1cu, 0x24042c3eu,
+ 0x240c3e24u, 0x24141404u, 0x24141c3eu, 0x24142404u, 0x24143404u, 0x24143434u,
+ 0x241c043eu, 0x241c242cu, 0x24240424u, 0x24242c0cu, 0x24243424u, 0x242c142cu,
+ 0x242c241cu, 0x242c3e04u, 0x243e042cu, 0x243e0c04u, 0x243e0c14u, 0x243e1c04u,
+ 0x2c040c14u, 0x2c04240cu, 0x2c043e04u, 0x2c0c0404u, 0x2c0c0434u, 0x2c0c1434u,
+ 0x2c0c2c2cu, 0x2c140c24u, 0x2c141c14u, 0x2c143e14u, 0x2c1c0414u, 0x2c1c2c1cu,
+ 0x2c240c04u, 0x2c24141cu, 0x2c24143eu, 0x2c243e14u, 0x2c2c0414u, 0x2c2c1c0cu,
+ 0x2c342c04u, 0x2c3e1424u, 0x2c3e2414u, 0x34041424u, 0x34042424u, 0x34042434u,
+ 0x34043424u, 0x340c140cu, 0x340c340cu, 0x34140c3eu, 0x34143424u, 0x341c1c04u,
+ 0x341c1c34u, 0x34242424u, 0x342c042cu, 0x342c2c14u, 0x34341c1cu, 0x343e041cu,
+ 0x343e140cu, 0x3e04041cu, 0x3e04042cu, 0x3e04043eu, 0x3e040c04u, 0x3e041c14u,
+ 0x3e042c14u, 0x3e0c1434u, 0x3e0c2404u, 0x3e140c14u, 0x3e14242cu, 0x3e142c14u,
+ 0x3e1c0404u, 0x3e1c0c2cu, 0x3e1c1c1cu, 0x3e1c3404u, 0x3e24140cu, 0x3e24240cu,
+ 0x3e2c0404u, 0x3e2c0414u, 0x3e2c1424u, 0x3e341c04u);
+
+int dp(uint a, uint b) { return dotPacked4x8EXT(int(a), int(b)); }
+uvec4 bc(uvec4 xr[NX], uint k) { return subgroupBroadcast(xr[k / SG], k % SG); }
+
+uint byte_at(uint wd[NW], uint i) { return (wd[i >> 2] >> (8u * (i & 3u))) & 0xffu; }
+uint half_at(uint wd[NW], uint i) { return byte_at(wd, i) | (byte_at(wd, i + 1u) << 8); }
+
+uint signs7(uint i) { return i | ((uint(bitCount(i)) & 1u) << 7); }
+
+uint signed4(uint g, uint s)
+{
+ uint r = 0u;
+ for (uint i = 0; i < 4u; i++) {
+ int v = int((g >> (8u * i)) & 0xffu);
+ if (((s >> i) & 1u) != 0u)
+ v = -v;
+ r |= (uint(v) & 0xffu) << (8u * i);
+ }
+ return r;
+}
+
+/* the grid in the workgroup's shared memory (iq4xs_matvec.comp) */
+shared uint GS[256];
+
+void main()
+{
+ for (uint i = gl_LocalInvocationID.x; i < 256u; i += 64u)
+ GS[i] = IQ3XXS_GRID[i];
+ barrier();
+ U1 W = U1(p.w);
+ U4 X = U4(p.x);
+ F Y = F(p.y);
+ uint lane = gl_SubgroupInvocationID;
+ uint grow = gl_WorkGroupID.y * 64u + gl_LocalInvocationID.x;
+ uint row = min(grow, p.rows - 1u);
+ uint v0 = gl_WorkGroupID.x * NV;
+ uint xo[NV];
+ for (uint v = 0; v < NV; v++)
+ xo[v] = min(v0 + v, p.nv - 1u) * p.xs * 20u;
+ float acc[NV];
+ for (uint v = 0; v < NV; v++)
+ acc[v] = 0.0;
+ uint off = row * p.nb * BS;
+ for (uint b = 0; b < p.nb; b++, off += BS) {
+ uint base = off >> 2;
+ uint wd[NW];
+ for (uint k = 0; k < NW; k++)
+ wd[k] = W.v[base + k];
+ if ((off & 3u) != 0u) {
+ for (uint k = 0; k + 1u < NW; k++)
+ wd[k] = (wd[k] >> 16) | (wd[k + 1u] << 16);
+ wd[NW - 1u] = wd[NW - 1u] >> 16;
+ }
+ float d = unpackHalf2x16(wd[0] & 0xffffu).x;
+ /* group g: eight signed words (weights 4i..4i+3), its scale */
+ uint q[64];
+ int sc[8];
+ for (uint g = 0; g < 8u; g++) {
+ uint aux = half_at(wd, 66u + 4u * g) | (half_at(wd, 68u + 4u * g) << 16);
+ sc[g] = 1 + 2 * int(aux >> 28);
+ for (uint k = 0; k < 4u; k++) {
+ uint sg = signs7((aux >> (7u * k)) & 127u);
+ q[8u * g + 2u * k] = signed4(GS[byte_at(wd, 2u + 8u * g + 2u * k)], sg & 15u);
+ q[8u * g + 2u * k + 1u] = signed4(GS[byte_at(wd, 3u + 8u * g + 2u * k)], sg >> 4);
+ }
+ }
+ for (uint v = 0; v < NV; v++) {
+ uvec4 xr[NX];
+ for (uint j = 0; j < NX; j++)
+ xr[j] = X.v[xo[v] + min(j * SG + lane, 19u)];
+ int isum = 0;
+ for (uint g = 0; g < 8u; g++) {
+ uvec4 a0 = bc(xr, 2u * g), a1 = bc(xr, 2u * g + 1u);
+ uint q0 = 8u * g;
+ int s = dp(q[q0], a0.x) + dp(q[q0 + 1u], a0.y) + dp(q[q0 + 2u], a0.z) +
+ dp(q[q0 + 3u], a0.w) + dp(q[q0 + 4u], a1.x) + dp(q[q0 + 5u], a1.y) +
+ dp(q[q0 + 6u], a1.z) + dp(q[q0 + 7u], a1.w);
+ isum += sc[g] * s;
+ }
+ float d8 = uintBitsToFloat(bc(xr, 18u).x);
+ acc[v] = fma(d * 0.25 * d8, float(isum), acc[v]);
+ xo[v] += 20u;
+ }
+ }
+ if (grow < p.rows)
+ for (uint v = 0; v < NV; v++)
+ if (v0 + v < p.nv)
+ Y.v[(v0 + v) * p.ys + row] = acc[v];
+}
diff --git a/src/llm/shaders/iq4nl_mmq.comp b/src/llm/shaders/iq4nl_mmq.comp
index fe50b50..a323ab5 100644
--- a/src/llm/shaders/iq4nl_mmq.comp
+++ b/src/llm/shaders/iq4nl_mmq.comp
@@ -1,13 +1,18 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
Copyright (C) 2026 Maurizio Cammalleri */
#version 450
-// The fast prompt for IQ4_NL (KIND 0) and MXFP4 (KIND 1): q80_mmq.comp's
-// tiling and activations (blocks of 32), a group's weights its 16 bytes of
-// nibbles through the sixteen signed levels of its table (weights 0-15 the
-// low nibbles, 16-31 the high), its scale the block's d (IQ4_NL) or the
-// power of two of its E8M0 byte (MXFP4, quant.c janas_e8m0_half). A block
-// is 18 bytes (IQ4_NL) or 17 (MXFP4), so its words are read realigned.
-// NOT bit for bit with the CPU: rounded group by group.
+// The fast prompt for IQ4_NL (KIND 0), MXFP4 (1), Q5_0 (2) and Q2_0 (3):
+// q80_mmq.comp's tiling and activations (blocks of 32), a group's weights
+// its 16 bytes of nibbles through the sixteen signed levels of its table
+// (weights 0-15 the low nibbles, 16-31 the high), its scale the block's d
+// (IQ4_NL) or the power of two of its E8M0 byte (MXFP4, quant.c
+// janas_e8m0_half). Q5_0: the nibbles and qh's fifth bits, q - 16; Q2_0:
+// each half of a block of 64 a group, its 8 bytes of two-bit weights
+// spread a byte each, q - 1, the block's d. A block is 18 bytes (IQ4_NL,
+// Q2_0), 17 (MXFP4) or 22 (Q5_0), so its words are read realigned. A row
+// may end inside a group of 256 (tail blocks of 32 past the whole groups:
+// qwen4exp's widths of 640), the stages past its end empty. NOT bit for bit
+// with the CPU: rounded group by group.
#extension GL_EXT_control_flow_attributes : require
#extension GL_EXT_integer_dot_product : require
#extension GL_EXT_buffer_reference : require
@@ -17,7 +22,8 @@ layout(local_size_x = 128) in;
layout(constant_id = 0) const uint NV = 8; // unused: the table's constants
layout(constant_id = 1) const uint SG = 8;
layout(constant_id = 2) const uint KIND = 0;
-const uint S = KIND == 1u ? 17u : 18u; // bytes a block
+/* bytes a group of 32 weights (Q2_0: half a block) */
+const uint S = KIND == 3u ? 9u : KIND == 2u ? 22u : KIND == 1u ? 17u : 18u;
const uint BM = 64u, BN = 64u, BK = 32u, KS = 4u;
const uint WM = 32u, WN = 32u, TM = 2u, TN = 2u, WARP = 32u;
@@ -29,9 +35,14 @@ layout(buffer_reference, std430, buffer_reference_align = 16) readonly buffer U4
layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer U1 { uint v[]; };
layout(buffer_reference, std430, buffer_reference_align = 4) writeonly buffer F { float v[]; };
-// w: the first row (rows of nb * 8 blocks of S bytes); x: vector v, block
-// b at uvec4 (v * xs + b) * 20; y: row r of vector v at y[v * ys + r]
-layout(push_constant) uniform P { uvec2 w, x, y; uint rows, nb, nv, xs, ys; } p;
+// w: the first row (rows of nb * 8 + tail groups of S bytes); x: vector v,
+// block b at uvec4 (v * xs + b) * 20; y: row r of vector v at y[v * ys + r]
+layout(push_constant) uniform P {
+ uvec2 w, x, y;
+ uint rows, nb, nv, xs, ys, level;
+ uvec2 p1, p0;
+ uint tail; /* blocks of 32 past the whole groups */
+} p;
struct ACache { int qs[8]; float d; }; // a group: its 32 levels, d
struct BCache { int qs[8]; float d; }; // a group: quants, its d32
@@ -59,6 +70,25 @@ uint iq4(uint w)
((uint(lvl((w >> 24) & 15u)) & 0xffu) << 24);
}
+/* four bits (weights 4j..4j+3) spread to bit 4 of each byte */
+uint spread5(uint b)
+{
+ return ((b & 1u) << 4) | ((b & 2u) << 11) | ((b & 4u) << 18) | ((b & 8u) << 25);
+}
+
+/* a byte of Q2_0 (four weights of two bits) spread to a byte each */
+uint spread2(uint b)
+{
+ uint t = (b | (b << 12)) & 0x000f000fu;
+ return (t | (t << 6)) & 0x03030303u;
+}
+
+/* c taken from each byte of v (bytes of 0 to 127, results as signed) */
+uint sub_bytes(uint v, uint c)
+{
+ return ((v | 0x80808080u) - c) ^ 0x80808080u;
+}
+
/* the scale of the block at byte off */
float scale(U1 W, uint off)
{
@@ -80,7 +110,7 @@ void main()
uint warp_r = warp % (BM / WM), warp_c = warp / (BM / WM);
float sums[WNITER * TN * TM];
[[unroll]] for (uint i = 0; i < WNITER * TN * TM; i++) sums[i] = 0.0;
- uint ngroups = p.nb * 8u;
+ uint ngroups = p.nb * 8u + p.tail;
for (uint g0 = 0; g0 < ngroups; g0 += KS) {
// weights: 4 invocations a row, 2 words each of the group's 8
{
@@ -88,11 +118,37 @@ void main()
[[unroll]] for (uint l = 0; l < BM; l += 32u) {
uint r = min(r0 + rl + l, p.rows - 1u);
[[unroll]] for (uint k = 0; k < KS; k++) {
- uint off = (r * ngroups + g0 + k) * S;
+ uint g = g0 + k;
+ if (g >= ngroups) { /* past the row's end: nothing */
+ [[unroll]] for (uint t = 0; t < 2u; t++)
+ sa[k * BM + rl + l].qs[2u * iqs + t] = 0;
+ if (iqs == 0u)
+ sa[k * BM + rl + l].d = 0.0;
+ continue;
+ }
+ uint off = (r * ngroups + g) * S;
+ if (KIND == 3u) {
+ /* the block's first byte, its half's quants */
+ uint bo = off - (g % 2u) * S, qo = bo + 2u + 8u * (g % 2u);
+ uint q = bword(W1, qo, iqs / 2u);
+ [[unroll]] for (uint t = 0; t < 2u; t++) {
+ uint kw = 2u * iqs + t;
+ sa[k * BM + rl + l].qs[kw] =
+ int(sub_bytes(spread2((q >> (8u * (kw % 4u))) & 0xffu), 0x01010101u));
+ }
+ if (iqs == 0u)
+ sa[k * BM + rl + l].d = scale(W1, bo);
+ continue;
+ }
+ uint qh = KIND == 2u ? bword(W1, off + 2u, 0u) : 0u;
[[unroll]] for (uint t = 0; t < 2u; t++) {
uint kw = 2u * iqs + t, q = bword(W1, off + S - 16u, kw & 3u);
- sa[k * BM + rl + l].qs[kw] =
- int(iq4(kw < 4u ? q & 0x0f0f0f0fu : (q >> 4) & 0x0f0f0f0fu));
+ uint nib = kw < 4u ? q & 0x0f0f0f0fu : (q >> 4) & 0x0f0f0f0fu;
+ if (KIND == 2u)
+ nib = sub_bytes(nib | spread5((qh >> (4u * kw)) & 15u), 0x10101010u);
+ else
+ nib = iq4(nib);
+ sa[k * BM + rl + l].qs[kw] = int(nib);
}
if (iqs == 0u)
sa[k * BM + rl + l].d = scale(W1, off);
@@ -104,7 +160,7 @@ void main()
uint iqs = id % 2u, c = id / 2u;
uint v = min(v0 + c, p.nv - 1u);
[[unroll]] for (uint k = 0; k < KS; k++) {
- uint g = g0 + k, b = g / 8u, j = g % 8u;
+ uint g = min(g0 + k, ngroups - 1u), b = g / 8u, j = g % 8u;
uint xo = (v * p.xs + b) * XB;
uvec4 q = X.v[xo + 2u * j + iqs];
sb[k * BN + c].qs[4u * iqs] = int(q.x);
diff --git a/src/llm/shaders/iqk_mmid.comp b/src/llm/shaders/iqk_mmid.comp
new file mode 100644
index 0000000..12fc034
--- /dev/null
+++ b/src/llm/shaders/iqk_mmid.comp
@@ -0,0 +1,1187 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri
+ The tables IQ2XXS_GRID, IQ2XS_GRID, IQ2S_GRID, IQ3XXS_GRID, IQ1S_GRID and
+ IQ3S_GRID below are ggml's grids (ggml/src/ggml-common.h), copied as data
+ from src/llm/iq2xxs_grid.h, iq2xs_grid.h, iq2s_grid.h, iq3xxs_grid.h,
+ iq1s_grid.h and iq3s_grid.h (64-bit entries as two words, low first;
+ iq1s_grid's eight levels of -1, 0 or 1 as two bits each, an entry in 16
+ bits): they define the formats. Copyright (c) 2023-2026 The ggml
+ authors, under the MIT licence (see the ggml repository). Generated -
+ not formatted by hand. */
+#version 450
+// The fast prompt for a mixture's experts in the 256-block types without
+// tiles of their own (gpu.c experts_id): mxfp4_mmid.comp's table and tiles
+// (64 rows by 16 vectors, each invocation 2 rows by 4, groups of 32 staged
+// 4 at a time), KIND chosen when the pipeline is made: 0 Q2_K, 1 IQ2_XXS,
+// 2 IQ2_XS, 3 IQ2_S, 4 IQ3_XXS, 5 IQ1_S, 6 IQ1_M, 7 IQ3_S, 8 IQ4_XS, read
+// as their *_matvec.comp read them. A group of 32 its weights as signed
+// bytes (Q2_K's unsigned) and a scale for each of its halves of 16 - d
+// times the type's factor and the sub-scale - and for Q2_K a minimum for
+// each, taken off with the activations' sums of 16. NOT bit for bit with
+// the CPU: rounded group by group.
+#extension GL_EXT_control_flow_attributes : require
+#extension GL_EXT_integer_dot_product : require
+#extension GL_EXT_buffer_reference : require
+#extension GL_EXT_buffer_reference_uvec2 : require
+
+layout(local_size_x = 128) in;
+layout(constant_id = 0) const uint NV = 8; // unused: the table's constants
+layout(constant_id = 1) const uint SG = 8;
+layout(constant_id = 2) const uint KIND = 0;
+
+/* bytes of a block, and words of the grid in shared memory */
+const uint BS = KIND == 0u ? 84u : KIND == 1u ? 66u : KIND == 2u ? 74u : KIND == 3u ? 82u
+ : KIND == 4u ? 98u : KIND == 5u ? 50u : KIND == 6u ? 56u : KIND == 7u ? 110u
+ : 136u;
+const uint NGW = KIND == 1u ? 512u : KIND == 2u ? 1024u : KIND == 3u ? 2048u : KIND == 4u ? 256u
+ : KIND == 5u || KIND == 6u ? 1024u : KIND == 7u ? 512u : KIND == 8u ? 16u : 1u;
+const uint BM = 64u, BN = 16u, KS = 4u, XB = 20u;
+
+layout(buffer_reference, std430, buffer_reference_align = 16) readonly buffer U4 { uvec4 v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer U1 { uint v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) writeonly buffer F { float v[]; };
+
+// t: the table, 3 uvec4 an expert: (w, x), (y, nv, ys), (unused); then
+// from uvec4 tiles on, one a workgroup's z: (expert, first vector)
+layout(push_constant) uniform P { uvec2 t; uint rows, nb, tiles, ngroups; } p;
+
+struct ACache { int qs[8]; vec4 sc; }; // a group: its 32 weights, scales of its halves, minima
+struct BCache { int qs[8]; float d; vec2 bs; }; // a group: quants, the block's d8, its sums of 16
+shared ACache sa[BM * KS];
+shared BCache sb[BN * KS];
+
+const uint IQ2XXS_GRID[512] = uint[512](
+ 0x08080808u, 0x08080808u, 0x0808082bu, 0x08080808u, 0x08081919u, 0x08080808u,
+ 0x08082b08u, 0x08080808u, 0x08082b2bu, 0x08080808u, 0x08190819u, 0x08080808u,
+ 0x08191908u, 0x08080808u, 0x082b0808u, 0x08080808u, 0x082b082bu, 0x08080808u,
+ 0x082b2b08u, 0x08080808u, 0x082b2b2bu, 0x08080808u, 0x19080819u, 0x08080808u,
+ 0x19081908u, 0x08080808u, 0x19190808u, 0x08080808u, 0x19192b08u, 0x08080808u,
+ 0x192b0819u, 0x08080808u, 0x192b1908u, 0x08080808u, 0x2b080808u, 0x08080808u,
+ 0x2b08082bu, 0x08080808u, 0x2b082b2bu, 0x08080808u, 0x2b2b082bu, 0x08080808u,
+ 0x08080819u, 0x08080819u, 0x08081908u, 0x08080819u, 0x08190808u, 0x08080819u,
+ 0x08191919u, 0x08080819u, 0x19080808u, 0x08080819u, 0x2b081908u, 0x08080819u,
+ 0x2b192b08u, 0x08080819u, 0x08080808u, 0x0808082bu, 0x0808082bu, 0x0808082bu,
+ 0x082b082bu, 0x0808082bu, 0x2b08082bu, 0x0808082bu, 0x08080819u, 0x08081908u,
+ 0x08081908u, 0x08081908u, 0x08190808u, 0x08081908u, 0x082b0819u, 0x08081908u,
+ 0x082b1908u, 0x08081908u, 0x19080808u, 0x08081908u, 0x1908082bu, 0x08081908u,
+ 0x19082b08u, 0x08081908u, 0x192b0808u, 0x08081908u, 0x2b080819u, 0x08081908u,
+ 0x2b081908u, 0x08081908u, 0x2b190808u, 0x08081908u, 0x2b2b1908u, 0x08081908u,
+ 0x08080808u, 0x08081919u, 0x0808082bu, 0x08081919u, 0x08082b08u, 0x08081919u,
+ 0x082b0808u, 0x08081919u, 0x1908192bu, 0x08081919u, 0x192b2b19u, 0x08081919u,
+ 0x2b080808u, 0x08081919u, 0x2b190819u, 0x08081919u, 0x08082b19u, 0x0808192bu,
+ 0x08190808u, 0x0808192bu, 0x19080808u, 0x0808192bu, 0x2b081908u, 0x0808192bu,
+ 0x2b2b1908u, 0x0808192bu, 0x08080808u, 0x08082b08u, 0x08081919u, 0x08082b08u,
+ 0x08082b08u, 0x08082b08u, 0x08191908u, 0x08082b08u, 0x082b2b08u, 0x08082b08u,
+ 0x19080819u, 0x08082b08u, 0x19081908u, 0x08082b08u, 0x19190808u, 0x08082b08u,
+ 0x1919082bu, 0x08082b08u, 0x2b082b08u, 0x08082b08u, 0x08081908u, 0x08082b19u,
+ 0x19080808u, 0x08082b19u, 0x0808082bu, 0x08082b2bu, 0x08191908u, 0x08082b2bu,
+ 0x08080819u, 0x08190808u, 0x08081908u, 0x08190808u, 0x08190808u, 0x08190808u,
+ 0x082b0819u, 0x08190808u, 0x19080808u, 0x08190808u, 0x192b0808u, 0x08190808u,
+ 0x2b081908u, 0x08190808u, 0x2b190808u, 0x08190808u, 0x2b191919u, 0x08190808u,
+ 0x08080808u, 0x08190819u, 0x08082b08u, 0x08190819u, 0x082b0808u, 0x08190819u,
+ 0x19190808u, 0x08190819u, 0x19192b2bu, 0x08190819u, 0x2b080808u, 0x08190819u,
+ 0x082b1908u, 0x0819082bu, 0x19081919u, 0x0819082bu, 0x08080808u, 0x08191908u,
+ 0x08082b08u, 0x08191908u, 0x082b0808u, 0x08191908u, 0x082b1919u, 0x08191908u,
+ 0x19082b19u, 0x08191908u, 0x2b080808u, 0x08191908u, 0x08192b08u, 0x08191919u,
+ 0x192b082bu, 0x08191919u, 0x08080808u, 0x0819192bu, 0x0819192bu, 0x0819192bu,
+ 0x08080819u, 0x08192b08u, 0x08081908u, 0x08192b08u, 0x08190808u, 0x08192b08u,
+ 0x19080808u, 0x08192b08u, 0x2b080819u, 0x08192b08u, 0x08080808u, 0x08192b19u,
+ 0x08081919u, 0x08192b19u, 0x2b2b0808u, 0x08192b19u, 0x19190819u, 0x08192b2bu,
+ 0x08080808u, 0x082b0808u, 0x0808082bu, 0x082b0808u, 0x08082b2bu, 0x082b0808u,
+ 0x19081908u, 0x082b0808u, 0x192b0819u, 0x082b0808u, 0x2b080808u, 0x082b0808u,
+ 0x2b08082bu, 0x082b0808u, 0x082b2b19u, 0x082b0819u, 0x19082b08u, 0x082b0819u,
+ 0x08080808u, 0x082b082bu, 0x0808082bu, 0x082b082bu, 0x08080819u, 0x082b1908u,
+ 0x08081908u, 0x082b1908u, 0x08190808u, 0x082b1908u, 0x19080808u, 0x082b1908u,
+ 0x1919192bu, 0x082b1908u, 0x08080808u, 0x082b1919u, 0x19080819u, 0x082b1919u,
+ 0x192b1908u, 0x082b1919u, 0x2b190808u, 0x082b192bu, 0x08082b08u, 0x082b2b08u,
+ 0x082b0808u, 0x082b2b08u, 0x2b191908u, 0x082b2b08u, 0x19081908u, 0x082b2b2bu,
+ 0x08080819u, 0x19080808u, 0x08081908u, 0x19080808u, 0x08190808u, 0x19080808u,
+ 0x08192b08u, 0x19080808u, 0x082b0819u, 0x19080808u, 0x082b1908u, 0x19080808u,
+ 0x19080808u, 0x19080808u, 0x19082b08u, 0x19080808u, 0x1919192bu, 0x19080808u,
+ 0x192b0808u, 0x19080808u, 0x2b080819u, 0x19080808u, 0x2b081908u, 0x19080808u,
+ 0x2b190808u, 0x19080808u, 0x08080808u, 0x19080819u, 0x082b0808u, 0x19080819u,
+ 0x192b0819u, 0x19080819u, 0x2b080808u, 0x19080819u, 0x2b081919u, 0x19080819u,
+ 0x08080819u, 0x1908082bu, 0x08190808u, 0x1908082bu, 0x19082b08u, 0x1908082bu,
+ 0x1919192bu, 0x1908082bu, 0x192b2b08u, 0x1908082bu, 0x08080808u, 0x19081908u,
+ 0x08082b08u, 0x19081908u, 0x082b0808u, 0x19081908u, 0x2b080808u, 0x19081908u,
+ 0x2b192b19u, 0x19081908u, 0x0819082bu, 0x19081919u, 0x082b1908u, 0x19081919u,
+ 0x08080808u, 0x1908192bu, 0x08080819u, 0x19082b08u, 0x08081908u, 0x19082b08u,
+ 0x08190808u, 0x19082b08u, 0x19080808u, 0x19082b08u, 0x19081919u, 0x19082b08u,
+ 0x08080808u, 0x19082b19u, 0x19192b08u, 0x19082b19u, 0x192b0819u, 0x19082b19u,
+ 0x2b08082bu, 0x19082b19u, 0x19081919u, 0x19082b2bu, 0x2b190808u, 0x19082b2bu,
+ 0x08080808u, 0x19190808u, 0x08082b08u, 0x19190808u, 0x08190819u, 0x19190808u,
+ 0x08192b19u, 0x19190808u, 0x082b0808u, 0x19190808u, 0x2b080808u, 0x19190808u,
+ 0x2b082b08u, 0x19190808u, 0x08081908u, 0x19190819u, 0x1908082bu, 0x19190819u,
+ 0x2b2b1908u, 0x19190819u, 0x2b190819u, 0x1919082bu, 0x2b190808u, 0x19191908u,
+ 0x2b19082bu, 0x19191908u, 0x08082b2bu, 0x19191919u, 0x08080819u, 0x1919192bu,
+ 0x19191908u, 0x1919192bu, 0x08080808u, 0x19192b08u, 0x08190819u, 0x19192b08u,
+ 0x08192b19u, 0x19192b08u, 0x192b1908u, 0x19192b08u, 0x19080808u, 0x19192b19u,
+ 0x08082b08u, 0x19192b2bu, 0x08081908u, 0x192b0808u, 0x08190808u, 0x192b0808u,
+ 0x19080808u, 0x192b0808u, 0x192b2b08u, 0x192b0808u, 0x08080808u, 0x192b0819u,
+ 0x19191919u, 0x192b0819u, 0x08192b08u, 0x192b082bu, 0x192b0808u, 0x192b082bu,
+ 0x08080808u, 0x192b1908u, 0x08081919u, 0x192b1908u, 0x08190808u, 0x192b1919u,
+ 0x0819082bu, 0x192b1919u, 0x2b081908u, 0x192b1919u, 0x1908082bu, 0x192b2b08u,
+ 0x08080808u, 0x2b080808u, 0x0808082bu, 0x2b080808u, 0x08082b2bu, 0x2b080808u,
+ 0x19080819u, 0x2b080808u, 0x2b08082bu, 0x2b080808u, 0x08081908u, 0x2b080819u,
+ 0x08192b08u, 0x2b080819u, 0x19080808u, 0x2b080819u, 0x08190819u, 0x2b08082bu,
+ 0x08080819u, 0x2b081908u, 0x08081908u, 0x2b081908u, 0x08190808u, 0x2b081908u,
+ 0x08191919u, 0x2b081908u, 0x19080808u, 0x2b081908u, 0x192b0808u, 0x2b081908u,
+ 0x08080808u, 0x2b081919u, 0x1908192bu, 0x2b081919u, 0x2b191908u, 0x2b081919u,
+ 0x08082b19u, 0x2b08192bu, 0x19080808u, 0x2b08192bu, 0x192b0808u, 0x2b08192bu,
+ 0x0808082bu, 0x2b082b08u, 0x08081908u, 0x2b082b19u, 0x08190819u, 0x2b082b2bu,
+ 0x08081908u, 0x2b190808u, 0x08190808u, 0x2b190808u, 0x082b1908u, 0x2b190808u,
+ 0x19080808u, 0x2b190808u, 0x2b2b0819u, 0x2b190808u, 0x0819192bu, 0x2b190819u,
+ 0x2b080808u, 0x2b190819u, 0x19081919u, 0x2b19082bu, 0x08080808u, 0x2b191908u,
+ 0x082b082bu, 0x2b191908u, 0x19081908u, 0x2b191908u, 0x19190819u, 0x2b191919u,
+ 0x2b080819u, 0x2b192b08u, 0x082b0808u, 0x2b192b19u, 0x0808082bu, 0x2b2b0808u,
+ 0x19190808u, 0x2b2b0808u, 0x2b081919u, 0x2b2b0808u, 0x08082b19u, 0x2b2b0819u,
+ 0x08080808u, 0x2b2b082bu, 0x08192b08u, 0x2b2b1908u, 0x19190808u, 0x2b2b2b08u,
+ 0x08081908u, 0x2b2b2b19u);
+
+const uint IQ2XS_GRID[1024] = uint[1024](
+ 0x08080808u, 0x08080808u, 0x0808082bu, 0x08080808u, 0x08081919u, 0x08080808u,
+ 0x08082b08u, 0x08080808u, 0x08082b2bu, 0x08080808u, 0x08190819u, 0x08080808u,
+ 0x08191908u, 0x08080808u, 0x0819192bu, 0x08080808u, 0x08192b19u, 0x08080808u,
+ 0x082b0808u, 0x08080808u, 0x082b082bu, 0x08080808u, 0x082b1919u, 0x08080808u,
+ 0x082b2b08u, 0x08080808u, 0x19080819u, 0x08080808u, 0x19081908u, 0x08080808u,
+ 0x1908192bu, 0x08080808u, 0x19082b19u, 0x08080808u, 0x19190808u, 0x08080808u,
+ 0x1919082bu, 0x08080808u, 0x19191919u, 0x08080808u, 0x19192b08u, 0x08080808u,
+ 0x192b0819u, 0x08080808u, 0x192b1908u, 0x08080808u, 0x2b080808u, 0x08080808u,
+ 0x2b08082bu, 0x08080808u, 0x2b081919u, 0x08080808u, 0x2b082b08u, 0x08080808u,
+ 0x2b190819u, 0x08080808u, 0x2b191908u, 0x08080808u, 0x2b192b19u, 0x08080808u,
+ 0x2b2b0808u, 0x08080808u, 0x08080819u, 0x08080819u, 0x08081908u, 0x08080819u,
+ 0x0808192bu, 0x08080819u, 0x08082b19u, 0x08080819u, 0x08190808u, 0x08080819u,
+ 0x0819082bu, 0x08080819u, 0x08191919u, 0x08080819u, 0x08192b08u, 0x08080819u,
+ 0x08192b2bu, 0x08080819u, 0x082b0819u, 0x08080819u, 0x082b1908u, 0x08080819u,
+ 0x19080808u, 0x08080819u, 0x1908082bu, 0x08080819u, 0x19081919u, 0x08080819u,
+ 0x19082b08u, 0x08080819u, 0x19190819u, 0x08080819u, 0x19191908u, 0x08080819u,
+ 0x192b0808u, 0x08080819u, 0x192b2b08u, 0x08080819u, 0x2b080819u, 0x08080819u,
+ 0x2b081908u, 0x08080819u, 0x2b190808u, 0x08080819u, 0x08080808u, 0x0808082bu,
+ 0x0808082bu, 0x0808082bu, 0x08081919u, 0x0808082bu, 0x08082b08u, 0x0808082bu,
+ 0x08190819u, 0x0808082bu, 0x08191908u, 0x0808082bu, 0x082b0808u, 0x0808082bu,
+ 0x19080819u, 0x0808082bu, 0x19081908u, 0x0808082bu, 0x19190808u, 0x0808082bu,
+ 0x19191919u, 0x0808082bu, 0x2b080808u, 0x0808082bu, 0x2b082b2bu, 0x0808082bu,
+ 0x08080819u, 0x08081908u, 0x08081908u, 0x08081908u, 0x0808192bu, 0x08081908u,
+ 0x08082b19u, 0x08081908u, 0x08190808u, 0x08081908u, 0x0819082bu, 0x08081908u,
+ 0x08191919u, 0x08081908u, 0x08192b08u, 0x08081908u, 0x082b0819u, 0x08081908u,
+ 0x082b1908u, 0x08081908u, 0x19080808u, 0x08081908u, 0x1908082bu, 0x08081908u,
+ 0x19081919u, 0x08081908u, 0x19082b08u, 0x08081908u, 0x19190819u, 0x08081908u,
+ 0x19191908u, 0x08081908u, 0x1919192bu, 0x08081908u, 0x192b0808u, 0x08081908u,
+ 0x2b080819u, 0x08081908u, 0x2b081908u, 0x08081908u, 0x2b190808u, 0x08081908u,
+ 0x08080808u, 0x08081919u, 0x0808082bu, 0x08081919u, 0x08081919u, 0x08081919u,
+ 0x08082b08u, 0x08081919u, 0x08190819u, 0x08081919u, 0x08191908u, 0x08081919u,
+ 0x082b0808u, 0x08081919u, 0x19080819u, 0x08081919u, 0x19081908u, 0x08081919u,
+ 0x19190808u, 0x08081919u, 0x192b0819u, 0x08081919u, 0x2b080808u, 0x08081919u,
+ 0x08080819u, 0x0808192bu, 0x08081908u, 0x0808192bu, 0x08190808u, 0x0808192bu,
+ 0x082b192bu, 0x0808192bu, 0x19080808u, 0x0808192bu, 0x1908082bu, 0x0808192bu,
+ 0x2b081908u, 0x0808192bu, 0x08080808u, 0x08082b08u, 0x0808082bu, 0x08082b08u,
+ 0x08081919u, 0x08082b08u, 0x08082b08u, 0x08082b08u, 0x08082b2bu, 0x08082b08u,
+ 0x08190819u, 0x08082b08u, 0x08191908u, 0x08082b08u, 0x082b0808u, 0x08082b08u,
+ 0x082b1919u, 0x08082b08u, 0x19080819u, 0x08082b08u, 0x19081908u, 0x08082b08u,
+ 0x19190808u, 0x08082b08u, 0x19192b08u, 0x08082b08u, 0x2b080808u, 0x08082b08u,
+ 0x2b2b0808u, 0x08082b08u, 0x2b2b2b2bu, 0x08082b08u, 0x08080819u, 0x08082b19u,
+ 0x08081908u, 0x08082b19u, 0x08190808u, 0x08082b19u, 0x19080808u, 0x08082b19u,
+ 0x2b080819u, 0x08082b19u, 0x2b082b19u, 0x08082b19u, 0x08080808u, 0x08082b2bu,
+ 0x082b0808u, 0x08082b2bu, 0x082b2b08u, 0x08082b2bu, 0x2b19192bu, 0x08082b2bu,
+ 0x2b2b0808u, 0x08082b2bu, 0x08080819u, 0x08190808u, 0x08081908u, 0x08190808u,
+ 0x0808192bu, 0x08190808u, 0x08082b19u, 0x08190808u, 0x08190808u, 0x08190808u,
+ 0x0819082bu, 0x08190808u, 0x08191919u, 0x08190808u, 0x08192b08u, 0x08190808u,
+ 0x082b0819u, 0x08190808u, 0x082b1908u, 0x08190808u, 0x19080808u, 0x08190808u,
+ 0x1908082bu, 0x08190808u, 0x19081919u, 0x08190808u, 0x19082b08u, 0x08190808u,
+ 0x19190819u, 0x08190808u, 0x19191908u, 0x08190808u, 0x192b0808u, 0x08190808u,
+ 0x192b2b2bu, 0x08190808u, 0x2b080819u, 0x08190808u, 0x2b081908u, 0x08190808u,
+ 0x2b190808u, 0x08190808u, 0x08080808u, 0x08190819u, 0x0808082bu, 0x08190819u,
+ 0x08081919u, 0x08190819u, 0x08082b08u, 0x08190819u, 0x08190819u, 0x08190819u,
+ 0x08191908u, 0x08190819u, 0x082b0808u, 0x08190819u, 0x19080819u, 0x08190819u,
+ 0x19081908u, 0x08190819u, 0x19190808u, 0x08190819u, 0x2b080808u, 0x08190819u,
+ 0x2b191908u, 0x08190819u, 0x2b19192bu, 0x08190819u, 0x08080819u, 0x0819082bu,
+ 0x08081908u, 0x0819082bu, 0x0808192bu, 0x0819082bu, 0x08190808u, 0x0819082bu,
+ 0x19080808u, 0x0819082bu, 0x192b0808u, 0x0819082bu, 0x08080808u, 0x08191908u,
+ 0x0808082bu, 0x08191908u, 0x08081919u, 0x08191908u, 0x08082b08u, 0x08191908u,
+ 0x08190819u, 0x08191908u, 0x08191908u, 0x08191908u, 0x082b0808u, 0x08191908u,
+ 0x19080819u, 0x08191908u, 0x19081908u, 0x08191908u, 0x19082b19u, 0x08191908u,
+ 0x19190808u, 0x08191908u, 0x192b1908u, 0x08191908u, 0x2b080808u, 0x08191908u,
+ 0x08080819u, 0x08191919u, 0x08081908u, 0x08191919u, 0x08190808u, 0x08191919u,
+ 0x19080808u, 0x08191919u, 0x08080808u, 0x0819192bu, 0x08191908u, 0x0819192bu,
+ 0x19082b19u, 0x0819192bu, 0x08080819u, 0x08192b08u, 0x08081908u, 0x08192b08u,
+ 0x08190808u, 0x08192b08u, 0x0819082bu, 0x08192b08u, 0x19080808u, 0x08192b08u,
+ 0x19191908u, 0x08192b08u, 0x2b08192bu, 0x08192b08u, 0x08080808u, 0x08192b19u,
+ 0x08081919u, 0x08192b19u, 0x192b192bu, 0x08192b19u, 0x19190819u, 0x08192b2bu,
+ 0x2b2b2b19u, 0x08192b2bu, 0x08080808u, 0x082b0808u, 0x0808082bu, 0x082b0808u,
+ 0x08081919u, 0x082b0808u, 0x08082b08u, 0x082b0808u, 0x08082b2bu, 0x082b0808u,
+ 0x08190819u, 0x082b0808u, 0x08191908u, 0x082b0808u, 0x082b0808u, 0x082b0808u,
+ 0x19080819u, 0x082b0808u, 0x19081908u, 0x082b0808u, 0x19190808u, 0x082b0808u,
+ 0x2b080808u, 0x082b0808u, 0x2b2b0808u, 0x082b0808u, 0x08080819u, 0x082b0819u,
+ 0x08081908u, 0x082b0819u, 0x08190808u, 0x082b0819u, 0x19080808u, 0x082b0819u,
+ 0x19082b08u, 0x082b0819u, 0x192b1919u, 0x082b0819u, 0x08080808u, 0x082b082bu,
+ 0x082b082bu, 0x082b082bu, 0x2b080808u, 0x082b082bu, 0x2b2b2b08u, 0x082b082bu,
+ 0x08080819u, 0x082b1908u, 0x08081908u, 0x082b1908u, 0x08190808u, 0x082b1908u,
+ 0x082b2b19u, 0x082b1908u, 0x19080808u, 0x082b1908u, 0x08080808u, 0x082b1919u,
+ 0x19080819u, 0x082b1919u, 0x1919082bu, 0x082b1919u, 0x2b192b19u, 0x082b1919u,
+ 0x08080819u, 0x082b192bu, 0x08192b2bu, 0x082b192bu, 0x2b2b192bu, 0x082b192bu,
+ 0x08080808u, 0x082b2b08u, 0x08082b08u, 0x082b2b08u, 0x08082b2bu, 0x082b2b08u,
+ 0x082b0808u, 0x082b2b08u, 0x19191919u, 0x082b2b08u, 0x2b082b08u, 0x082b2b08u,
+ 0x2b2b082bu, 0x082b2b08u, 0x192b2b08u, 0x082b2b19u, 0x2b190808u, 0x082b2b19u,
+ 0x08082b08u, 0x082b2b2bu, 0x082b0808u, 0x082b2b2bu, 0x2b08082bu, 0x082b2b2bu,
+ 0x2b082b08u, 0x082b2b2bu, 0x2b082b2bu, 0x082b2b2bu, 0x08080819u, 0x19080808u,
+ 0x08081908u, 0x19080808u, 0x0808192bu, 0x19080808u, 0x08082b19u, 0x19080808u,
+ 0x08190808u, 0x19080808u, 0x0819082bu, 0x19080808u, 0x08191919u, 0x19080808u,
+ 0x08192b08u, 0x19080808u, 0x082b0819u, 0x19080808u, 0x082b1908u, 0x19080808u,
+ 0x19080808u, 0x19080808u, 0x1908082bu, 0x19080808u, 0x19081919u, 0x19080808u,
+ 0x19082b08u, 0x19080808u, 0x19082b2bu, 0x19080808u, 0x19190819u, 0x19080808u,
+ 0x19191908u, 0x19080808u, 0x192b0808u, 0x19080808u, 0x192b1919u, 0x19080808u,
+ 0x2b080819u, 0x19080808u, 0x2b081908u, 0x19080808u, 0x2b190808u, 0x19080808u,
+ 0x08080808u, 0x19080819u, 0x0808082bu, 0x19080819u, 0x08081919u, 0x19080819u,
+ 0x08082b08u, 0x19080819u, 0x08190819u, 0x19080819u, 0x08191908u, 0x19080819u,
+ 0x082b0808u, 0x19080819u, 0x19080819u, 0x19080819u, 0x19081908u, 0x19080819u,
+ 0x19190808u, 0x19080819u, 0x2b080808u, 0x19080819u, 0x2b081919u, 0x19080819u,
+ 0x2b2b082bu, 0x19080819u, 0x08080819u, 0x1908082bu, 0x08081908u, 0x1908082bu,
+ 0x08190808u, 0x1908082bu, 0x0819082bu, 0x1908082bu, 0x082b2b19u, 0x1908082bu,
+ 0x19080808u, 0x1908082bu, 0x08080808u, 0x19081908u, 0x0808082bu, 0x19081908u,
+ 0x08081919u, 0x19081908u, 0x08082b08u, 0x19081908u, 0x08190819u, 0x19081908u,
+ 0x08191908u, 0x19081908u, 0x08192b19u, 0x19081908u, 0x082b0808u, 0x19081908u,
+ 0x19080819u, 0x19081908u, 0x19081908u, 0x19081908u, 0x19190808u, 0x19081908u,
+ 0x2b080808u, 0x19081908u, 0x2b191908u, 0x19081908u, 0x08080819u, 0x19081919u,
+ 0x08081908u, 0x19081919u, 0x08190808u, 0x19081919u, 0x082b1908u, 0x19081919u,
+ 0x19080808u, 0x19081919u, 0x2b192b2bu, 0x19081919u, 0x08080808u, 0x1908192bu,
+ 0x08082b2bu, 0x1908192bu, 0x19081908u, 0x1908192bu, 0x19190808u, 0x1908192bu,
+ 0x08080819u, 0x19082b08u, 0x08081908u, 0x19082b08u, 0x08190808u, 0x19082b08u,
+ 0x19080808u, 0x19082b08u, 0x19081919u, 0x19082b08u, 0x19191908u, 0x19082b08u,
+ 0x192b082bu, 0x19082b08u, 0x08080808u, 0x19082b19u, 0x08190819u, 0x19082b19u,
+ 0x19081908u, 0x19082b19u, 0x19190808u, 0x19082b19u, 0x192b2b19u, 0x19082b19u,
+ 0x08081908u, 0x19082b2bu, 0x08080808u, 0x19190808u, 0x0808082bu, 0x19190808u,
+ 0x08081919u, 0x19190808u, 0x08082b08u, 0x19190808u, 0x08190819u, 0x19190808u,
+ 0x08191908u, 0x19190808u, 0x082b0808u, 0x19190808u, 0x082b2b08u, 0x19190808u,
+ 0x19080819u, 0x19190808u, 0x19081908u, 0x19190808u, 0x19190808u, 0x19190808u,
+ 0x2b080808u, 0x19190808u, 0x08080819u, 0x19190819u, 0x08081908u, 0x19190819u,
+ 0x08190808u, 0x19190819u, 0x08191919u, 0x19190819u, 0x19080808u, 0x19190819u,
+ 0x1908082bu, 0x19190819u, 0x08080808u, 0x1919082bu, 0x19081908u, 0x1919082bu,
+ 0x2b2b2b2bu, 0x1919082bu, 0x08080819u, 0x19191908u, 0x08081908u, 0x19191908u,
+ 0x08190808u, 0x19191908u, 0x082b0819u, 0x19191908u, 0x19080808u, 0x19191908u,
+ 0x192b0808u, 0x19191908u, 0x2b080819u, 0x19191908u, 0x2b2b0819u, 0x19191908u,
+ 0x08080808u, 0x19191919u, 0x08082b08u, 0x19191919u, 0x2b080808u, 0x19191919u,
+ 0x2b082b08u, 0x19191919u, 0x082b0819u, 0x1919192bu, 0x192b2b08u, 0x1919192bu,
+ 0x2b2b0819u, 0x1919192bu, 0x08080808u, 0x19192b08u, 0x08191908u, 0x19192b08u,
+ 0x19080819u, 0x19192b08u, 0x19190808u, 0x19192b08u, 0x2b192b19u, 0x19192b08u,
+ 0x08192b2bu, 0x19192b19u, 0x19080808u, 0x19192b19u, 0x1908082bu, 0x19192b19u,
+ 0x2b081919u, 0x19192b2bu, 0x08080819u, 0x192b0808u, 0x08081908u, 0x192b0808u,
+ 0x08190808u, 0x192b0808u, 0x19080808u, 0x192b0808u, 0x19191908u, 0x192b0808u,
+ 0x192b082bu, 0x192b0808u, 0x2b08192bu, 0x192b0808u, 0x2b2b2b19u, 0x192b0808u,
+ 0x08080808u, 0x192b0819u, 0x082b1908u, 0x192b082bu, 0x19082b2bu, 0x192b082bu,
+ 0x2b19082bu, 0x192b082bu, 0x08080808u, 0x192b1908u, 0x0819192bu, 0x192b1908u,
+ 0x08190808u, 0x192b1919u, 0x19080808u, 0x192b1919u, 0x19081919u, 0x192b1919u,
+ 0x2b2b1908u, 0x192b1919u, 0x08080819u, 0x192b2b08u, 0x192b2b2bu, 0x192b2b08u,
+ 0x082b1919u, 0x192b2b19u, 0x0808192bu, 0x192b2b2bu, 0x19191908u, 0x192b2b2bu,
+ 0x192b082bu, 0x192b2b2bu, 0x08080808u, 0x2b080808u, 0x0808082bu, 0x2b080808u,
+ 0x08081919u, 0x2b080808u, 0x08082b08u, 0x2b080808u, 0x08190819u, 0x2b080808u,
+ 0x08191908u, 0x2b080808u, 0x082b0808u, 0x2b080808u, 0x082b2b2bu, 0x2b080808u,
+ 0x19080819u, 0x2b080808u, 0x19081908u, 0x2b080808u, 0x19190808u, 0x2b080808u,
+ 0x2b080808u, 0x2b080808u, 0x2b08082bu, 0x2b080808u, 0x2b2b2b08u, 0x2b080808u,
+ 0x2b2b2b2bu, 0x2b080808u, 0x08080819u, 0x2b080819u, 0x08081908u, 0x2b080819u,
+ 0x0808192bu, 0x2b080819u, 0x08190808u, 0x2b080819u, 0x19080808u, 0x2b080819u,
+ 0x19190819u, 0x2b080819u, 0x19192b19u, 0x2b080819u, 0x08080808u, 0x2b08082bu,
+ 0x082b0808u, 0x2b08082bu, 0x2b080808u, 0x2b08082bu, 0x2b08082bu, 0x2b08082bu,
+ 0x2b2b0808u, 0x2b08082bu, 0x2b2b2b08u, 0x2b08082bu, 0x08080819u, 0x2b081908u,
+ 0x08081908u, 0x2b081908u, 0x08190808u, 0x2b081908u, 0x0819082bu, 0x2b081908u,
+ 0x08191919u, 0x2b081908u, 0x19080808u, 0x2b081908u, 0x192b0808u, 0x2b081908u,
+ 0x2b082b19u, 0x2b081908u, 0x08080808u, 0x2b081919u, 0x19081908u, 0x2b081919u,
+ 0x2b2b1919u, 0x2b081919u, 0x08192b08u, 0x2b08192bu, 0x192b2b2bu, 0x2b08192bu,
+ 0x08080808u, 0x2b082b08u, 0x08082b08u, 0x2b082b08u, 0x082b1919u, 0x2b082b08u,
+ 0x19192b2bu, 0x2b082b08u, 0x2b080808u, 0x2b082b08u, 0x2b08082bu, 0x2b082b08u,
+ 0x2b2b2b08u, 0x2b082b08u, 0x0808192bu, 0x2b082b19u, 0x082b082bu, 0x2b082b2bu,
+ 0x2b080808u, 0x2b082b2bu, 0x2b082b08u, 0x2b082b2bu, 0x2b19192bu, 0x2b082b2bu,
+ 0x2b2b2b08u, 0x2b082b2bu, 0x08080819u, 0x2b190808u, 0x08081908u, 0x2b190808u,
+ 0x08190808u, 0x2b190808u, 0x19080808u, 0x2b190808u, 0x1919192bu, 0x2b190808u,
+ 0x2b081908u, 0x2b190808u, 0x08080808u, 0x2b190819u, 0x082b082bu, 0x2b190819u,
+ 0x192b1908u, 0x2b190819u, 0x1919192bu, 0x2b19082bu, 0x2b082b19u, 0x2b19082bu,
+ 0x08080808u, 0x2b191908u, 0x08081919u, 0x2b191908u, 0x19081908u, 0x2b191908u,
+ 0x19190808u, 0x2b191908u, 0x19192b08u, 0x2b191908u, 0x082b2b19u, 0x2b191919u,
+ 0x2b190808u, 0x2b191919u, 0x2b19082bu, 0x2b191919u, 0x19080819u, 0x2b19192bu,
+ 0x19190819u, 0x2b192b08u, 0x2b2b192bu, 0x2b192b08u, 0x19082b19u, 0x2b192b19u,
+ 0x08191919u, 0x2b192b2bu, 0x192b0808u, 0x2b192b2bu, 0x08080808u, 0x2b2b0808u,
+ 0x0808082bu, 0x2b2b0808u, 0x08082b08u, 0x2b2b0808u, 0x08082b2bu, 0x2b2b0808u,
+ 0x082b0808u, 0x2b2b0808u, 0x082b2b2bu, 0x2b2b0808u, 0x2b2b0808u, 0x2b2b0808u,
+ 0x19190819u, 0x2b2b0819u, 0x19192b19u, 0x2b2b0819u, 0x2b2b192bu, 0x2b2b0819u,
+ 0x08080808u, 0x2b2b082bu, 0x0808082bu, 0x2b2b082bu, 0x08082b08u, 0x2b2b082bu,
+ 0x082b2b2bu, 0x2b2b082bu, 0x2b080808u, 0x2b2b082bu, 0x2b2b0808u, 0x2b2b082bu,
+ 0x19080808u, 0x2b2b1908u, 0x2b191919u, 0x2b2b1908u, 0x192b1919u, 0x2b2b192bu,
+ 0x2b192b08u, 0x2b2b192bu, 0x08082b2bu, 0x2b2b2b08u, 0x082b0808u, 0x2b2b2b08u,
+ 0x082b082bu, 0x2b2b2b08u, 0x082b2b08u, 0x2b2b2b08u, 0x2b2b0808u, 0x2b2b2b08u,
+ 0x2b2b2b08u, 0x2b2b2b08u, 0x08081908u, 0x2b2b2b19u, 0x2b081908u, 0x2b2b2b19u,
+ 0x2b08192bu, 0x2b2b2b19u, 0x082b2b08u, 0x2b2b2b2bu, 0x082b2b2bu, 0x2b2b2b2bu,
+ 0x2b190819u, 0x2b2b2b2bu, 0x2b2b2b2bu, 0x2b2b2b2bu);
+
+const uint IQ2S_GRID[2048] = uint[2048](
+ 0x08080808u, 0x08080808u, 0x0808082bu, 0x08080808u, 0x08081919u, 0x08080808u,
+ 0x08082b08u, 0x08080808u, 0x08082b2bu, 0x08080808u, 0x08190819u, 0x08080808u,
+ 0x08191908u, 0x08080808u, 0x0819192bu, 0x08080808u, 0x08192b19u, 0x08080808u,
+ 0x082b0808u, 0x08080808u, 0x082b082bu, 0x08080808u, 0x082b1919u, 0x08080808u,
+ 0x082b2b08u, 0x08080808u, 0x19080819u, 0x08080808u, 0x19081908u, 0x08080808u,
+ 0x1908192bu, 0x08080808u, 0x19082b19u, 0x08080808u, 0x19190808u, 0x08080808u,
+ 0x1919082bu, 0x08080808u, 0x19191919u, 0x08080808u, 0x19192b08u, 0x08080808u,
+ 0x192b0819u, 0x08080808u, 0x192b1908u, 0x08080808u, 0x192b192bu, 0x08080808u,
+ 0x192b2b19u, 0x08080808u, 0x2b080808u, 0x08080808u, 0x2b08082bu, 0x08080808u,
+ 0x2b081919u, 0x08080808u, 0x2b082b08u, 0x08080808u, 0x2b190819u, 0x08080808u,
+ 0x2b191908u, 0x08080808u, 0x2b2b0808u, 0x08080808u, 0x2b2b1919u, 0x08080808u,
+ 0x2b2b2b2bu, 0x08080808u, 0x08080819u, 0x08080819u, 0x08081908u, 0x08080819u,
+ 0x0808192bu, 0x08080819u, 0x08082b19u, 0x08080819u, 0x08190808u, 0x08080819u,
+ 0x0819082bu, 0x08080819u, 0x08191919u, 0x08080819u, 0x08192b08u, 0x08080819u,
+ 0x082b0819u, 0x08080819u, 0x082b1908u, 0x08080819u, 0x19080808u, 0x08080819u,
+ 0x1908082bu, 0x08080819u, 0x19081919u, 0x08080819u, 0x19082b08u, 0x08080819u,
+ 0x19190819u, 0x08080819u, 0x19191908u, 0x08080819u, 0x1919192bu, 0x08080819u,
+ 0x19192b19u, 0x08080819u, 0x192b0808u, 0x08080819u, 0x192b1919u, 0x08080819u,
+ 0x192b2b08u, 0x08080819u, 0x2b080819u, 0x08080819u, 0x2b081908u, 0x08080819u,
+ 0x2b190808u, 0x08080819u, 0x2b19082bu, 0x08080819u, 0x2b191919u, 0x08080819u,
+ 0x2b2b0819u, 0x08080819u, 0x2b2b1908u, 0x08080819u, 0x08080808u, 0x0808082bu,
+ 0x0808082bu, 0x0808082bu, 0x08081919u, 0x0808082bu, 0x08082b08u, 0x0808082bu,
+ 0x08190819u, 0x0808082bu, 0x08191908u, 0x0808082bu, 0x082b0808u, 0x0808082bu,
+ 0x082b2b2bu, 0x0808082bu, 0x19080819u, 0x0808082bu, 0x19081908u, 0x0808082bu,
+ 0x1908192bu, 0x0808082bu, 0x19082b19u, 0x0808082bu, 0x19190808u, 0x0808082bu,
+ 0x19191919u, 0x0808082bu, 0x2b080808u, 0x0808082bu, 0x2b081919u, 0x0808082bu,
+ 0x2b082b2bu, 0x0808082bu, 0x2b191908u, 0x0808082bu, 0x2b2b082bu, 0x0808082bu,
+ 0x08080819u, 0x08081908u, 0x08081908u, 0x08081908u, 0x0808192bu, 0x08081908u,
+ 0x08082b19u, 0x08081908u, 0x08190808u, 0x08081908u, 0x0819082bu, 0x08081908u,
+ 0x08191919u, 0x08081908u, 0x08192b08u, 0x08081908u, 0x082b0819u, 0x08081908u,
+ 0x082b1908u, 0x08081908u, 0x082b192bu, 0x08081908u, 0x082b2b19u, 0x08081908u,
+ 0x19080808u, 0x08081908u, 0x1908082bu, 0x08081908u, 0x19081919u, 0x08081908u,
+ 0x19082b08u, 0x08081908u, 0x19082b2bu, 0x08081908u, 0x19190819u, 0x08081908u,
+ 0x19191908u, 0x08081908u, 0x1919192bu, 0x08081908u, 0x19192b19u, 0x08081908u,
+ 0x192b0808u, 0x08081908u, 0x192b082bu, 0x08081908u, 0x192b1919u, 0x08081908u,
+ 0x2b080819u, 0x08081908u, 0x2b081908u, 0x08081908u, 0x2b08192bu, 0x08081908u,
+ 0x2b082b19u, 0x08081908u, 0x2b190808u, 0x08081908u, 0x2b191919u, 0x08081908u,
+ 0x2b192b08u, 0x08081908u, 0x2b2b0819u, 0x08081908u, 0x2b2b1908u, 0x08081908u,
+ 0x08080808u, 0x08081919u, 0x0808082bu, 0x08081919u, 0x08081919u, 0x08081919u,
+ 0x08082b08u, 0x08081919u, 0x08082b2bu, 0x08081919u, 0x08190819u, 0x08081919u,
+ 0x08191908u, 0x08081919u, 0x0819192bu, 0x08081919u, 0x08192b19u, 0x08081919u,
+ 0x082b0808u, 0x08081919u, 0x082b1919u, 0x08081919u, 0x082b2b08u, 0x08081919u,
+ 0x19080819u, 0x08081919u, 0x19081908u, 0x08081919u, 0x1908192bu, 0x08081919u,
+ 0x19082b19u, 0x08081919u, 0x19190808u, 0x08081919u, 0x1919082bu, 0x08081919u,
+ 0x19191919u, 0x08081919u, 0x19192b08u, 0x08081919u, 0x192b0819u, 0x08081919u,
+ 0x192b1908u, 0x08081919u, 0x2b080808u, 0x08081919u, 0x2b08082bu, 0x08081919u,
+ 0x2b081919u, 0x08081919u, 0x2b082b08u, 0x08081919u, 0x2b190819u, 0x08081919u,
+ 0x2b191908u, 0x08081919u, 0x2b2b0808u, 0x08081919u, 0x08080819u, 0x0808192bu,
+ 0x08081908u, 0x0808192bu, 0x0808192bu, 0x0808192bu, 0x08082b19u, 0x0808192bu,
+ 0x08190808u, 0x0808192bu, 0x08191919u, 0x0808192bu, 0x19080808u, 0x0808192bu,
+ 0x19081919u, 0x0808192bu, 0x19082b08u, 0x0808192bu, 0x19190819u, 0x0808192bu,
+ 0x19191908u, 0x0808192bu, 0x192b0808u, 0x0808192bu, 0x2b080819u, 0x0808192bu,
+ 0x2b081908u, 0x0808192bu, 0x2b190808u, 0x0808192bu, 0x08080808u, 0x08082b08u,
+ 0x0808082bu, 0x08082b08u, 0x08081919u, 0x08082b08u, 0x08082b08u, 0x08082b08u,
+ 0x08190819u, 0x08082b08u, 0x08191908u, 0x08082b08u, 0x0819192bu, 0x08082b08u,
+ 0x08192b19u, 0x08082b08u, 0x082b0808u, 0x08082b08u, 0x082b1919u, 0x08082b08u,
+ 0x082b2b2bu, 0x08082b08u, 0x19080819u, 0x08082b08u, 0x19081908u, 0x08082b08u,
+ 0x1908192bu, 0x08082b08u, 0x19082b19u, 0x08082b08u, 0x19190808u, 0x08082b08u,
+ 0x1919082bu, 0x08082b08u, 0x19191919u, 0x08082b08u, 0x19192b08u, 0x08082b08u,
+ 0x192b0819u, 0x08082b08u, 0x192b1908u, 0x08082b08u, 0x2b080808u, 0x08082b08u,
+ 0x2b081919u, 0x08082b08u, 0x2b191908u, 0x08082b08u, 0x2b2b2b2bu, 0x08082b08u,
+ 0x08080819u, 0x08082b19u, 0x08081908u, 0x08082b19u, 0x08190808u, 0x08082b19u,
+ 0x0819082bu, 0x08082b19u, 0x08191919u, 0x08082b19u, 0x08192b08u, 0x08082b19u,
+ 0x082b0819u, 0x08082b19u, 0x19080808u, 0x08082b19u, 0x19081919u, 0x08082b19u,
+ 0x19082b08u, 0x08082b19u, 0x19190819u, 0x08082b19u, 0x19191908u, 0x08082b19u,
+ 0x192b0808u, 0x08082b19u, 0x2b080819u, 0x08082b19u, 0x2b190808u, 0x08082b19u,
+ 0x08080808u, 0x08082b2bu, 0x08190819u, 0x08082b2bu, 0x08191908u, 0x08082b2bu,
+ 0x082b082bu, 0x08082b2bu, 0x082b2b08u, 0x08082b2bu, 0x082b2b2bu, 0x08082b2bu,
+ 0x19190808u, 0x08082b2bu, 0x2b192b19u, 0x08082b2bu, 0x08080819u, 0x08190808u,
+ 0x08081908u, 0x08190808u, 0x0808192bu, 0x08190808u, 0x08082b19u, 0x08190808u,
+ 0x08190808u, 0x08190808u, 0x0819082bu, 0x08190808u, 0x08191919u, 0x08190808u,
+ 0x08192b08u, 0x08190808u, 0x082b0819u, 0x08190808u, 0x082b1908u, 0x08190808u,
+ 0x082b192bu, 0x08190808u, 0x19080808u, 0x08190808u, 0x1908082bu, 0x08190808u,
+ 0x19081919u, 0x08190808u, 0x19082b08u, 0x08190808u, 0x19190819u, 0x08190808u,
+ 0x19191908u, 0x08190808u, 0x1919192bu, 0x08190808u, 0x19192b19u, 0x08190808u,
+ 0x192b0808u, 0x08190808u, 0x192b082bu, 0x08190808u, 0x192b1919u, 0x08190808u,
+ 0x192b2b08u, 0x08190808u, 0x2b080819u, 0x08190808u, 0x2b081908u, 0x08190808u,
+ 0x2b08192bu, 0x08190808u, 0x2b190808u, 0x08190808u, 0x2b191919u, 0x08190808u,
+ 0x2b192b08u, 0x08190808u, 0x2b2b0819u, 0x08190808u, 0x2b2b1908u, 0x08190808u,
+ 0x08080808u, 0x08190819u, 0x0808082bu, 0x08190819u, 0x08081919u, 0x08190819u,
+ 0x08082b08u, 0x08190819u, 0x08082b2bu, 0x08190819u, 0x08190819u, 0x08190819u,
+ 0x08191908u, 0x08190819u, 0x0819192bu, 0x08190819u, 0x08192b19u, 0x08190819u,
+ 0x082b0808u, 0x08190819u, 0x082b082bu, 0x08190819u, 0x082b1919u, 0x08190819u,
+ 0x082b2b08u, 0x08190819u, 0x19080819u, 0x08190819u, 0x19081908u, 0x08190819u,
+ 0x1908192bu, 0x08190819u, 0x19082b19u, 0x08190819u, 0x19190808u, 0x08190819u,
+ 0x1919082bu, 0x08190819u, 0x19191919u, 0x08190819u, 0x19192b08u, 0x08190819u,
+ 0x192b0819u, 0x08190819u, 0x192b1908u, 0x08190819u, 0x2b080808u, 0x08190819u,
+ 0x2b08082bu, 0x08190819u, 0x2b081919u, 0x08190819u, 0x2b082b08u, 0x08190819u,
+ 0x2b190819u, 0x08190819u, 0x2b191908u, 0x08190819u, 0x08080819u, 0x0819082bu,
+ 0x08081908u, 0x0819082bu, 0x08082b19u, 0x0819082bu, 0x08190808u, 0x0819082bu,
+ 0x08191919u, 0x0819082bu, 0x082b0819u, 0x0819082bu, 0x082b1908u, 0x0819082bu,
+ 0x19080808u, 0x0819082bu, 0x19081919u, 0x0819082bu, 0x19190819u, 0x0819082bu,
+ 0x19191908u, 0x0819082bu, 0x2b080819u, 0x0819082bu, 0x2b081908u, 0x0819082bu,
+ 0x2b190808u, 0x0819082bu, 0x08080808u, 0x08191908u, 0x0808082bu, 0x08191908u,
+ 0x08081919u, 0x08191908u, 0x08082b08u, 0x08191908u, 0x08190819u, 0x08191908u,
+ 0x08191908u, 0x08191908u, 0x0819192bu, 0x08191908u, 0x08192b19u, 0x08191908u,
+ 0x082b0808u, 0x08191908u, 0x082b1919u, 0x08191908u, 0x082b2b08u, 0x08191908u,
+ 0x19080819u, 0x08191908u, 0x19081908u, 0x08191908u, 0x1908192bu, 0x08191908u,
+ 0x19082b19u, 0x08191908u, 0x19190808u, 0x08191908u, 0x1919082bu, 0x08191908u,
+ 0x19191919u, 0x08191908u, 0x19192b08u, 0x08191908u, 0x192b0819u, 0x08191908u,
+ 0x192b1908u, 0x08191908u, 0x2b080808u, 0x08191908u, 0x2b08082bu, 0x08191908u,
+ 0x2b081919u, 0x08191908u, 0x2b082b08u, 0x08191908u, 0x2b190819u, 0x08191908u,
+ 0x2b191908u, 0x08191908u, 0x2b2b0808u, 0x08191908u, 0x08080819u, 0x08191919u,
+ 0x08081908u, 0x08191919u, 0x0808192bu, 0x08191919u, 0x08082b19u, 0x08191919u,
+ 0x08190808u, 0x08191919u, 0x0819082bu, 0x08191919u, 0x08191919u, 0x08191919u,
+ 0x08192b08u, 0x08191919u, 0x082b0819u, 0x08191919u, 0x082b1908u, 0x08191919u,
+ 0x19080808u, 0x08191919u, 0x1908082bu, 0x08191919u, 0x19081919u, 0x08191919u,
+ 0x19082b08u, 0x08191919u, 0x19190819u, 0x08191919u, 0x19191908u, 0x08191919u,
+ 0x192b0808u, 0x08191919u, 0x2b080819u, 0x08191919u, 0x2b081908u, 0x08191919u,
+ 0x2b190808u, 0x08191919u, 0x08080808u, 0x0819192bu, 0x08081919u, 0x0819192bu,
+ 0x08082b08u, 0x0819192bu, 0x08190819u, 0x0819192bu, 0x08191908u, 0x0819192bu,
+ 0x082b0808u, 0x0819192bu, 0x19080819u, 0x0819192bu, 0x19081908u, 0x0819192bu,
+ 0x19190808u, 0x0819192bu, 0x2b080808u, 0x0819192bu, 0x2b2b2b2bu, 0x0819192bu,
+ 0x08080819u, 0x08192b08u, 0x08081908u, 0x08192b08u, 0x0808192bu, 0x08192b08u,
+ 0x08082b19u, 0x08192b08u, 0x08190808u, 0x08192b08u, 0x08191919u, 0x08192b08u,
+ 0x08192b08u, 0x08192b08u, 0x082b0819u, 0x08192b08u, 0x19080808u, 0x08192b08u,
+ 0x1908082bu, 0x08192b08u, 0x19081919u, 0x08192b08u, 0x19082b08u, 0x08192b08u,
+ 0x19190819u, 0x08192b08u, 0x19191908u, 0x08192b08u, 0x192b0808u, 0x08192b08u,
+ 0x2b080819u, 0x08192b08u, 0x2b081908u, 0x08192b08u, 0x08080808u, 0x08192b19u,
+ 0x0808082bu, 0x08192b19u, 0x08081919u, 0x08192b19u, 0x08082b08u, 0x08192b19u,
+ 0x08190819u, 0x08192b19u, 0x08191908u, 0x08192b19u, 0x082b0808u, 0x08192b19u,
+ 0x19080819u, 0x08192b19u, 0x19081908u, 0x08192b19u, 0x19190808u, 0x08192b19u,
+ 0x192b2b19u, 0x08192b19u, 0x2b2b082bu, 0x08192b19u, 0x08081908u, 0x08192b2bu,
+ 0x08190808u, 0x08192b2bu, 0x19080808u, 0x08192b2bu, 0x1919192bu, 0x08192b2bu,
+ 0x08080808u, 0x082b0808u, 0x0808082bu, 0x082b0808u, 0x08081919u, 0x082b0808u,
+ 0x08082b08u, 0x082b0808u, 0x08190819u, 0x082b0808u, 0x08191908u, 0x082b0808u,
+ 0x0819192bu, 0x082b0808u, 0x08192b19u, 0x082b0808u, 0x082b0808u, 0x082b0808u,
+ 0x082b1919u, 0x082b0808u, 0x082b2b2bu, 0x082b0808u, 0x19080819u, 0x082b0808u,
+ 0x19081908u, 0x082b0808u, 0x19190808u, 0x082b0808u, 0x1919082bu, 0x082b0808u,
+ 0x19191919u, 0x082b0808u, 0x192b1908u, 0x082b0808u, 0x2b080808u, 0x082b0808u,
+ 0x2b082b2bu, 0x082b0808u, 0x2b191908u, 0x082b0808u, 0x2b2b2b2bu, 0x082b0808u,
+ 0x08080819u, 0x082b0819u, 0x08081908u, 0x082b0819u, 0x08190808u, 0x082b0819u,
+ 0x0819082bu, 0x082b0819u, 0x08191919u, 0x082b0819u, 0x082b0819u, 0x082b0819u,
+ 0x19080808u, 0x082b0819u, 0x1908082bu, 0x082b0819u, 0x19081919u, 0x082b0819u,
+ 0x19190819u, 0x082b0819u, 0x19191908u, 0x082b0819u, 0x192b0808u, 0x082b0819u,
+ 0x2b080819u, 0x082b0819u, 0x2b081908u, 0x082b0819u, 0x2b190808u, 0x082b0819u,
+ 0x08080808u, 0x082b082bu, 0x08082b2bu, 0x082b082bu, 0x082b082bu, 0x082b082bu,
+ 0x082b2b08u, 0x082b082bu, 0x082b2b2bu, 0x082b082bu, 0x19081908u, 0x082b082bu,
+ 0x19190808u, 0x082b082bu, 0x2b082b08u, 0x082b082bu, 0x2b082b2bu, 0x082b082bu,
+ 0x2b2b2b08u, 0x082b082bu, 0x08080819u, 0x082b1908u, 0x08081908u, 0x082b1908u,
+ 0x0808192bu, 0x082b1908u, 0x08082b19u, 0x082b1908u, 0x08190808u, 0x082b1908u,
+ 0x08191919u, 0x082b1908u, 0x08192b08u, 0x082b1908u, 0x082b0819u, 0x082b1908u,
+ 0x082b1908u, 0x082b1908u, 0x19080808u, 0x082b1908u, 0x1908082bu, 0x082b1908u,
+ 0x19081919u, 0x082b1908u, 0x19082b08u, 0x082b1908u, 0x19190819u, 0x082b1908u,
+ 0x19191908u, 0x082b1908u, 0x192b0808u, 0x082b1908u, 0x2b080819u, 0x082b1908u,
+ 0x2b081908u, 0x082b1908u, 0x2b190808u, 0x082b1908u, 0x08080808u, 0x082b1919u,
+ 0x08081919u, 0x082b1919u, 0x08082b08u, 0x082b1919u, 0x08190819u, 0x082b1919u,
+ 0x08191908u, 0x082b1919u, 0x082b0808u, 0x082b1919u, 0x19080819u, 0x082b1919u,
+ 0x19081908u, 0x082b1919u, 0x19190808u, 0x082b1919u, 0x192b192bu, 0x082b1919u,
+ 0x2b080808u, 0x082b1919u, 0x08080819u, 0x082b192bu, 0x08081908u, 0x082b192bu,
+ 0x08190808u, 0x082b192bu, 0x19080808u, 0x082b192bu, 0x19192b19u, 0x082b192bu,
+ 0x08080808u, 0x082b2b08u, 0x08081919u, 0x082b2b08u, 0x08190819u, 0x082b2b08u,
+ 0x08191908u, 0x082b2b08u, 0x19080819u, 0x082b2b08u, 0x19081908u, 0x082b2b08u,
+ 0x19190808u, 0x082b2b08u, 0x2b082b2bu, 0x082b2b08u, 0x2b2b2b2bu, 0x082b2b08u,
+ 0x08080819u, 0x082b2b19u, 0x08081908u, 0x082b2b19u, 0x08190808u, 0x082b2b19u,
+ 0x2b191919u, 0x082b2b19u, 0x08082b2bu, 0x082b2b2bu, 0x082b082bu, 0x082b2b2bu,
+ 0x192b1908u, 0x082b2b2bu, 0x2b082b08u, 0x082b2b2bu, 0x2b082b2bu, 0x082b2b2bu,
+ 0x08080819u, 0x19080808u, 0x08081908u, 0x19080808u, 0x0808192bu, 0x19080808u,
+ 0x08082b19u, 0x19080808u, 0x08190808u, 0x19080808u, 0x0819082bu, 0x19080808u,
+ 0x08191919u, 0x19080808u, 0x08192b08u, 0x19080808u, 0x08192b2bu, 0x19080808u,
+ 0x082b0819u, 0x19080808u, 0x082b1908u, 0x19080808u, 0x082b192bu, 0x19080808u,
+ 0x19080808u, 0x19080808u, 0x1908082bu, 0x19080808u, 0x19081919u, 0x19080808u,
+ 0x19082b08u, 0x19080808u, 0x19082b2bu, 0x19080808u, 0x19190819u, 0x19080808u,
+ 0x19191908u, 0x19080808u, 0x1919192bu, 0x19080808u, 0x19192b19u, 0x19080808u,
+ 0x192b0808u, 0x19080808u, 0x192b082bu, 0x19080808u, 0x192b1919u, 0x19080808u,
+ 0x2b080819u, 0x19080808u, 0x2b081908u, 0x19080808u, 0x2b190808u, 0x19080808u,
+ 0x2b191919u, 0x19080808u, 0x2b192b08u, 0x19080808u, 0x2b2b0819u, 0x19080808u,
+ 0x2b2b1908u, 0x19080808u, 0x08080808u, 0x19080819u, 0x0808082bu, 0x19080819u,
+ 0x08081919u, 0x19080819u, 0x08082b08u, 0x19080819u, 0x08190819u, 0x19080819u,
+ 0x08191908u, 0x19080819u, 0x0819192bu, 0x19080819u, 0x08192b19u, 0x19080819u,
+ 0x082b0808u, 0x19080819u, 0x082b082bu, 0x19080819u, 0x082b1919u, 0x19080819u,
+ 0x19080819u, 0x19080819u, 0x19081908u, 0x19080819u, 0x1908192bu, 0x19080819u,
+ 0x19082b19u, 0x19080819u, 0x19190808u, 0x19080819u, 0x1919082bu, 0x19080819u,
+ 0x19191919u, 0x19080819u, 0x19192b08u, 0x19080819u, 0x192b0819u, 0x19080819u,
+ 0x192b1908u, 0x19080819u, 0x2b080808u, 0x19080819u, 0x2b08082bu, 0x19080819u,
+ 0x2b081919u, 0x19080819u, 0x2b082b08u, 0x19080819u, 0x2b190819u, 0x19080819u,
+ 0x2b191908u, 0x19080819u, 0x2b2b0808u, 0x19080819u, 0x08080819u, 0x1908082bu,
+ 0x08081908u, 0x1908082bu, 0x08190808u, 0x1908082bu, 0x0819082bu, 0x1908082bu,
+ 0x08191919u, 0x1908082bu, 0x08192b08u, 0x1908082bu, 0x082b1908u, 0x1908082bu,
+ 0x19080808u, 0x1908082bu, 0x19081919u, 0x1908082bu, 0x19082b08u, 0x1908082bu,
+ 0x19190819u, 0x1908082bu, 0x19191908u, 0x1908082bu, 0x192b0808u, 0x1908082bu,
+ 0x2b080819u, 0x1908082bu, 0x2b081908u, 0x1908082bu, 0x08080808u, 0x19081908u,
+ 0x0808082bu, 0x19081908u, 0x08081919u, 0x19081908u, 0x08082b08u, 0x19081908u,
+ 0x08082b2bu, 0x19081908u, 0x08190819u, 0x19081908u, 0x08191908u, 0x19081908u,
+ 0x0819192bu, 0x19081908u, 0x08192b19u, 0x19081908u, 0x082b0808u, 0x19081908u,
+ 0x082b082bu, 0x19081908u, 0x082b1919u, 0x19081908u, 0x082b2b08u, 0x19081908u,
+ 0x19080819u, 0x19081908u, 0x19081908u, 0x19081908u, 0x1908192bu, 0x19081908u,
+ 0x19082b19u, 0x19081908u, 0x19190808u, 0x19081908u, 0x1919082bu, 0x19081908u,
+ 0x19191919u, 0x19081908u, 0x19192b08u, 0x19081908u, 0x192b0819u, 0x19081908u,
+ 0x192b1908u, 0x19081908u, 0x2b080808u, 0x19081908u, 0x2b08082bu, 0x19081908u,
+ 0x2b081919u, 0x19081908u, 0x2b082b08u, 0x19081908u, 0x2b190819u, 0x19081908u,
+ 0x2b191908u, 0x19081908u, 0x2b2b0808u, 0x19081908u, 0x08080819u, 0x19081919u,
+ 0x08081908u, 0x19081919u, 0x0808192bu, 0x19081919u, 0x08082b19u, 0x19081919u,
+ 0x08190808u, 0x19081919u, 0x0819082bu, 0x19081919u, 0x08191919u, 0x19081919u,
+ 0x08192b08u, 0x19081919u, 0x082b0819u, 0x19081919u, 0x082b1908u, 0x19081919u,
+ 0x19080808u, 0x19081919u, 0x1908082bu, 0x19081919u, 0x19081919u, 0x19081919u,
+ 0x19082b08u, 0x19081919u, 0x19190819u, 0x19081919u, 0x19191908u, 0x19081919u,
+ 0x192b0808u, 0x19081919u, 0x192b2b2bu, 0x19081919u, 0x2b080819u, 0x19081919u,
+ 0x2b081908u, 0x19081919u, 0x2b190808u, 0x19081919u, 0x08080808u, 0x1908192bu,
+ 0x0808082bu, 0x1908192bu, 0x08081919u, 0x1908192bu, 0x08082b08u, 0x1908192bu,
+ 0x08190819u, 0x1908192bu, 0x08191908u, 0x1908192bu, 0x082b0808u, 0x1908192bu,
+ 0x19080819u, 0x1908192bu, 0x19081908u, 0x1908192bu, 0x19190808u, 0x1908192bu,
+ 0x2b080808u, 0x1908192bu, 0x2b2b1919u, 0x1908192bu, 0x08080819u, 0x19082b08u,
+ 0x08081908u, 0x19082b08u, 0x08082b19u, 0x19082b08u, 0x08190808u, 0x19082b08u,
+ 0x0819082bu, 0x19082b08u, 0x08191919u, 0x19082b08u, 0x08192b08u, 0x19082b08u,
+ 0x082b0819u, 0x19082b08u, 0x082b1908u, 0x19082b08u, 0x19080808u, 0x19082b08u,
+ 0x1908082bu, 0x19082b08u, 0x19081919u, 0x19082b08u, 0x19082b08u, 0x19082b08u,
+ 0x19190819u, 0x19082b08u, 0x19191908u, 0x19082b08u, 0x192b0808u, 0x19082b08u,
+ 0x2b081908u, 0x19082b08u, 0x2b190808u, 0x19082b08u, 0x08080808u, 0x19082b19u,
+ 0x0808082bu, 0x19082b19u, 0x08081919u, 0x19082b19u, 0x08082b08u, 0x19082b19u,
+ 0x08190819u, 0x19082b19u, 0x08191908u, 0x19082b19u, 0x082b0808u, 0x19082b19u,
+ 0x19080819u, 0x19082b19u, 0x19081908u, 0x19082b19u, 0x19190808u, 0x19082b19u,
+ 0x2b080808u, 0x19082b19u, 0x2b19192bu, 0x19082b19u, 0x08080819u, 0x19082b2bu,
+ 0x08081908u, 0x19082b2bu, 0x08190808u, 0x19082b2bu, 0x19080808u, 0x19082b2bu,
+ 0x08080808u, 0x19190808u, 0x0808082bu, 0x19190808u, 0x08081919u, 0x19190808u,
+ 0x08082b08u, 0x19190808u, 0x08190819u, 0x19190808u, 0x08191908u, 0x19190808u,
+ 0x0819192bu, 0x19190808u, 0x08192b19u, 0x19190808u, 0x082b0808u, 0x19190808u,
+ 0x082b082bu, 0x19190808u, 0x082b1919u, 0x19190808u, 0x082b2b08u, 0x19190808u,
+ 0x19080819u, 0x19190808u, 0x19081908u, 0x19190808u, 0x1908192bu, 0x19190808u,
+ 0x19082b19u, 0x19190808u, 0x19190808u, 0x19190808u, 0x1919082bu, 0x19190808u,
+ 0x19191919u, 0x19190808u, 0x19192b08u, 0x19190808u, 0x192b0819u, 0x19190808u,
+ 0x192b1908u, 0x19190808u, 0x2b080808u, 0x19190808u, 0x2b08082bu, 0x19190808u,
+ 0x2b081919u, 0x19190808u, 0x2b082b08u, 0x19190808u, 0x2b190819u, 0x19190808u,
+ 0x2b191908u, 0x19190808u, 0x08080819u, 0x19190819u, 0x08081908u, 0x19190819u,
+ 0x0808192bu, 0x19190819u, 0x08082b19u, 0x19190819u, 0x08190808u, 0x19190819u,
+ 0x0819082bu, 0x19190819u, 0x08191919u, 0x19190819u, 0x08192b08u, 0x19190819u,
+ 0x082b0819u, 0x19190819u, 0x082b1908u, 0x19190819u, 0x19080808u, 0x19190819u,
+ 0x1908082bu, 0x19190819u, 0x19081919u, 0x19190819u, 0x19082b08u, 0x19190819u,
+ 0x19190819u, 0x19190819u, 0x19191908u, 0x19190819u, 0x192b0808u, 0x19190819u,
+ 0x2b080819u, 0x19190819u, 0x2b081908u, 0x19190819u, 0x2b190808u, 0x19190819u,
+ 0x08080808u, 0x1919082bu, 0x08081919u, 0x1919082bu, 0x08082b08u, 0x1919082bu,
+ 0x08190819u, 0x1919082bu, 0x08191908u, 0x1919082bu, 0x082b0808u, 0x1919082bu,
+ 0x19080819u, 0x1919082bu, 0x19081908u, 0x1919082bu, 0x19190808u, 0x1919082bu,
+ 0x192b2b19u, 0x1919082bu, 0x2b080808u, 0x1919082bu, 0x08080819u, 0x19191908u,
+ 0x08081908u, 0x19191908u, 0x0808192bu, 0x19191908u, 0x08082b19u, 0x19191908u,
+ 0x08190808u, 0x19191908u, 0x0819082bu, 0x19191908u, 0x08191919u, 0x19191908u,
+ 0x08192b08u, 0x19191908u, 0x082b0819u, 0x19191908u, 0x082b1908u, 0x19191908u,
+ 0x19080808u, 0x19191908u, 0x1908082bu, 0x19191908u, 0x19081919u, 0x19191908u,
+ 0x19082b08u, 0x19191908u, 0x19190819u, 0x19191908u, 0x19191908u, 0x19191908u,
+ 0x192b0808u, 0x19191908u, 0x2b080819u, 0x19191908u, 0x2b081908u, 0x19191908u,
+ 0x2b190808u, 0x19191908u, 0x08080808u, 0x19191919u, 0x0808082bu, 0x19191919u,
+ 0x08081919u, 0x19191919u, 0x08082b08u, 0x19191919u, 0x08190819u, 0x19191919u,
+ 0x08191908u, 0x19191919u, 0x082b0808u, 0x19191919u, 0x19080819u, 0x19191919u,
+ 0x19081908u, 0x19191919u, 0x19190808u, 0x19191919u, 0x2b080808u, 0x19191919u,
+ 0x08080819u, 0x1919192bu, 0x08081908u, 0x1919192bu, 0x08190808u, 0x1919192bu,
+ 0x082b192bu, 0x1919192bu, 0x19080808u, 0x1919192bu, 0x08080808u, 0x19192b08u,
+ 0x0808082bu, 0x19192b08u, 0x08081919u, 0x19192b08u, 0x08082b08u, 0x19192b08u,
+ 0x08190819u, 0x19192b08u, 0x08191908u, 0x19192b08u, 0x082b0808u, 0x19192b08u,
+ 0x19080819u, 0x19192b08u, 0x19081908u, 0x19192b08u, 0x19190808u, 0x19192b08u,
+ 0x19192b2bu, 0x19192b08u, 0x2b080808u, 0x19192b08u, 0x08080819u, 0x19192b19u,
+ 0x08081908u, 0x19192b19u, 0x08190808u, 0x19192b19u, 0x19080808u, 0x19192b19u,
+ 0x08080808u, 0x19192b2bu, 0x08192b19u, 0x19192b2bu, 0x2b081919u, 0x19192b2bu,
+ 0x2b2b2b08u, 0x19192b2bu, 0x08080819u, 0x192b0808u, 0x08081908u, 0x192b0808u,
+ 0x0808192bu, 0x192b0808u, 0x08190808u, 0x192b0808u, 0x0819082bu, 0x192b0808u,
+ 0x08191919u, 0x192b0808u, 0x08192b08u, 0x192b0808u, 0x082b0819u, 0x192b0808u,
+ 0x082b1908u, 0x192b0808u, 0x19080808u, 0x192b0808u, 0x19081919u, 0x192b0808u,
+ 0x19082b08u, 0x192b0808u, 0x19190819u, 0x192b0808u, 0x19191908u, 0x192b0808u,
+ 0x192b0808u, 0x192b0808u, 0x2b081908u, 0x192b0808u, 0x2b190808u, 0x192b0808u,
+ 0x08080808u, 0x192b0819u, 0x0808082bu, 0x192b0819u, 0x08081919u, 0x192b0819u,
+ 0x08082b08u, 0x192b0819u, 0x08190819u, 0x192b0819u, 0x08191908u, 0x192b0819u,
+ 0x082b0808u, 0x192b0819u, 0x19080819u, 0x192b0819u, 0x19081908u, 0x192b0819u,
+ 0x19190808u, 0x192b0819u, 0x2b080808u, 0x192b0819u, 0x2b192b19u, 0x192b0819u,
+ 0x08081908u, 0x192b082bu, 0x08190808u, 0x192b082bu, 0x19080808u, 0x192b082bu,
+ 0x1919192bu, 0x192b082bu, 0x2b2b0819u, 0x192b082bu, 0x08080808u, 0x192b1908u,
+ 0x08081919u, 0x192b1908u, 0x08082b08u, 0x192b1908u, 0x08190819u, 0x192b1908u,
+ 0x08191908u, 0x192b1908u, 0x082b0808u, 0x192b1908u, 0x19080819u, 0x192b1908u,
+ 0x19081908u, 0x192b1908u, 0x19190808u, 0x192b1908u, 0x2b080808u, 0x192b1908u,
+ 0x08080819u, 0x192b1919u, 0x08081908u, 0x192b1919u, 0x08190808u, 0x192b1919u,
+ 0x19080808u, 0x192b1919u, 0x19082b2bu, 0x192b1919u, 0x192b2b08u, 0x192b1919u,
+ 0x2b19082bu, 0x192b1919u, 0x08080808u, 0x192b192bu, 0x2b191908u, 0x192b192bu,
+ 0x08080819u, 0x192b2b08u, 0x08081908u, 0x192b2b08u, 0x08190808u, 0x192b2b08u,
+ 0x192b1919u, 0x192b2b08u, 0x2b192b08u, 0x192b2b08u, 0x08080808u, 0x192b2b19u,
+ 0x082b2b2bu, 0x192b2b19u, 0x1908082bu, 0x192b2b2bu, 0x2b2b0819u, 0x192b2b2bu,
+ 0x08080808u, 0x2b080808u, 0x0808082bu, 0x2b080808u, 0x08081919u, 0x2b080808u,
+ 0x08082b08u, 0x2b080808u, 0x08190819u, 0x2b080808u, 0x08191908u, 0x2b080808u,
+ 0x08192b19u, 0x2b080808u, 0x082b0808u, 0x2b080808u, 0x082b1919u, 0x2b080808u,
+ 0x19080819u, 0x2b080808u, 0x19081908u, 0x2b080808u, 0x19190808u, 0x2b080808u,
+ 0x1919082bu, 0x2b080808u, 0x19191919u, 0x2b080808u, 0x19192b08u, 0x2b080808u,
+ 0x192b0819u, 0x2b080808u, 0x2b080808u, 0x2b080808u, 0x2b081919u, 0x2b080808u,
+ 0x2b190819u, 0x2b080808u, 0x2b191908u, 0x2b080808u, 0x08080819u, 0x2b080819u,
+ 0x08081908u, 0x2b080819u, 0x08082b19u, 0x2b080819u, 0x08190808u, 0x2b080819u,
+ 0x0819082bu, 0x2b080819u, 0x08191919u, 0x2b080819u, 0x08192b08u, 0x2b080819u,
+ 0x082b0819u, 0x2b080819u, 0x082b1908u, 0x2b080819u, 0x19080808u, 0x2b080819u,
+ 0x1908082bu, 0x2b080819u, 0x19081919u, 0x2b080819u, 0x19082b08u, 0x2b080819u,
+ 0x19190819u, 0x2b080819u, 0x19191908u, 0x2b080819u, 0x2b080819u, 0x2b080819u,
+ 0x2b081908u, 0x2b080819u, 0x2b190808u, 0x2b080819u, 0x2b2b2b19u, 0x2b080819u,
+ 0x08080808u, 0x2b08082bu, 0x08081919u, 0x2b08082bu, 0x08082b2bu, 0x2b08082bu,
+ 0x08190819u, 0x2b08082bu, 0x08191908u, 0x2b08082bu, 0x19080819u, 0x2b08082bu,
+ 0x19081908u, 0x2b08082bu, 0x19190808u, 0x2b08082bu, 0x08080819u, 0x2b081908u,
+ 0x08081908u, 0x2b081908u, 0x0808192bu, 0x2b081908u, 0x08082b19u, 0x2b081908u,
+ 0x08190808u, 0x2b081908u, 0x0819082bu, 0x2b081908u, 0x08191919u, 0x2b081908u,
+ 0x08192b08u, 0x2b081908u, 0x082b0819u, 0x2b081908u, 0x19080808u, 0x2b081908u,
+ 0x1908082bu, 0x2b081908u, 0x19081919u, 0x2b081908u, 0x19082b08u, 0x2b081908u,
+ 0x19190819u, 0x2b081908u, 0x19191908u, 0x2b081908u, 0x192b0808u, 0x2b081908u,
+ 0x2b080819u, 0x2b081908u, 0x2b081908u, 0x2b081908u, 0x2b190808u, 0x2b081908u,
+ 0x08080808u, 0x2b081919u, 0x0808082bu, 0x2b081919u, 0x08081919u, 0x2b081919u,
+ 0x08082b08u, 0x2b081919u, 0x08190819u, 0x2b081919u, 0x08191908u, 0x2b081919u,
+ 0x082b0808u, 0x2b081919u, 0x19080819u, 0x2b081919u, 0x19081908u, 0x2b081919u,
+ 0x19190808u, 0x2b081919u, 0x2b080808u, 0x2b081919u, 0x2b082b2bu, 0x2b081919u,
+ 0x08080819u, 0x2b08192bu, 0x08081908u, 0x2b08192bu, 0x08190808u, 0x2b08192bu,
+ 0x082b2b19u, 0x2b08192bu, 0x19080808u, 0x2b08192bu, 0x08080808u, 0x2b082b08u,
+ 0x08081919u, 0x2b082b08u, 0x08190819u, 0x2b082b08u, 0x08191908u, 0x2b082b08u,
+ 0x19080819u, 0x2b082b08u, 0x19081908u, 0x2b082b08u, 0x19190808u, 0x2b082b08u,
+ 0x2b2b082bu, 0x2b082b08u, 0x08080819u, 0x2b082b19u, 0x08081908u, 0x2b082b19u,
+ 0x19080808u, 0x2b082b19u, 0x192b1919u, 0x2b082b19u, 0x082b082bu, 0x2b082b2bu,
+ 0x19192b08u, 0x2b082b2bu, 0x19192b2bu, 0x2b082b2bu, 0x2b08082bu, 0x2b082b2bu,
+ 0x2b2b082bu, 0x2b082b2bu, 0x08080819u, 0x2b190808u, 0x08081908u, 0x2b190808u,
+ 0x08082b19u, 0x2b190808u, 0x08190808u, 0x2b190808u, 0x0819082bu, 0x2b190808u,
+ 0x08191919u, 0x2b190808u, 0x08192b08u, 0x2b190808u, 0x082b1908u, 0x2b190808u,
+ 0x19080808u, 0x2b190808u, 0x1908082bu, 0x2b190808u, 0x19081919u, 0x2b190808u,
+ 0x19082b08u, 0x2b190808u, 0x19190819u, 0x2b190808u, 0x19191908u, 0x2b190808u,
+ 0x192b0808u, 0x2b190808u, 0x2b080819u, 0x2b190808u, 0x2b081908u, 0x2b190808u,
+ 0x2b190808u, 0x2b190808u, 0x08080808u, 0x2b190819u, 0x08081919u, 0x2b190819u,
+ 0x08190819u, 0x2b190819u, 0x08191908u, 0x2b190819u, 0x19080819u, 0x2b190819u,
+ 0x19081908u, 0x2b190819u, 0x19190808u, 0x2b190819u, 0x19192b2bu, 0x2b190819u,
+ 0x08080819u, 0x2b19082bu, 0x08081908u, 0x2b19082bu, 0x08190808u, 0x2b19082bu,
+ 0x19080808u, 0x2b19082bu, 0x2b2b192bu, 0x2b19082bu, 0x08080808u, 0x2b191908u,
+ 0x0808082bu, 0x2b191908u, 0x08081919u, 0x2b191908u, 0x08082b08u, 0x2b191908u,
+ 0x08190819u, 0x2b191908u, 0x08191908u, 0x2b191908u, 0x082b0808u, 0x2b191908u,
+ 0x19080819u, 0x2b191908u, 0x19081908u, 0x2b191908u, 0x19190808u, 0x2b191908u,
+ 0x2b080808u, 0x2b191908u, 0x2b19192bu, 0x2b191908u, 0x08080819u, 0x2b191919u,
+ 0x08081908u, 0x2b191919u, 0x08190808u, 0x2b191919u, 0x19080808u, 0x2b191919u,
+ 0x2b192b08u, 0x2b191919u, 0x2b2b0819u, 0x2b191919u, 0x08080808u, 0x2b19192bu,
+ 0x1908192bu, 0x2b19192bu, 0x192b1908u, 0x2b19192bu, 0x08080819u, 0x2b192b08u,
+ 0x08081908u, 0x2b192b08u, 0x08190808u, 0x2b192b08u, 0x082b192bu, 0x2b192b08u,
+ 0x19080808u, 0x2b192b08u, 0x2b2b2b19u, 0x2b192b08u, 0x08080808u, 0x2b192b19u,
+ 0x19082b19u, 0x2b192b19u, 0x1919082bu, 0x2b192b19u, 0x2b190808u, 0x2b192b2bu,
+ 0x08080808u, 0x2b2b0808u, 0x08081919u, 0x2b2b0808u, 0x08082b2bu, 0x2b2b0808u,
+ 0x08191908u, 0x2b2b0808u, 0x082b082bu, 0x2b2b0808u, 0x082b2b2bu, 0x2b2b0808u,
+ 0x19080819u, 0x2b2b0808u, 0x19081908u, 0x2b2b0808u, 0x19190808u, 0x2b2b0808u,
+ 0x2b2b082bu, 0x2b2b0808u, 0x2b2b2b2bu, 0x2b2b0808u, 0x19080808u, 0x2b2b0819u,
+ 0x192b1919u, 0x2b2b0819u, 0x0808082bu, 0x2b2b082bu, 0x08082b2bu, 0x2b2b082bu,
+ 0x082b082bu, 0x2b2b082bu, 0x082b2b08u, 0x2b2b082bu, 0x082b2b2bu, 0x2b2b082bu,
+ 0x2b08082bu, 0x2b2b082bu, 0x2b082b08u, 0x2b2b082bu, 0x2b082b2bu, 0x2b2b082bu,
+ 0x2b2b2b08u, 0x2b2b082bu, 0x08080819u, 0x2b2b1908u, 0x08081908u, 0x2b2b1908u,
+ 0x08190808u, 0x2b2b1908u, 0x19080808u, 0x2b2b1908u, 0x2b082b19u, 0x2b2b1908u,
+ 0x2b2b1908u, 0x2b2b1908u, 0x08080808u, 0x2b2b1919u, 0x08192b19u, 0x2b2b1919u,
+ 0x19190819u, 0x2b2b192bu, 0x08082b2bu, 0x2b2b2b08u, 0x082b2b08u, 0x2b2b2b08u,
+ 0x2b2b082bu, 0x2b2b2b08u, 0x19191908u, 0x2b2b2b19u, 0x2b08192bu, 0x2b2b2b19u,
+ 0x08082b08u, 0x2b2b2b2bu, 0x08082b2bu, 0x2b2b2b2bu, 0x082b0808u, 0x2b2b2b2bu,
+ 0x082b082bu, 0x2b2b2b2bu, 0x082b2b08u, 0x2b2b2b2bu, 0x2b082b08u, 0x2b2b2b2bu,
+ 0x2b2b2b2bu, 0x2b2b2b2bu);
+
+const uint IQ3XXS_GRID[256] = uint[256](
+ 0x04040404u, 0x04040414u, 0x04040424u, 0x04040c0cu, 0x04040c1cu, 0x04040c3eu,
+ 0x04041404u, 0x04041414u, 0x04041c0cu, 0x04042414u, 0x04043e1cu, 0x04043e2cu,
+ 0x040c040cu, 0x040c041cu, 0x040c0c04u, 0x040c0c14u, 0x040c140cu, 0x040c142cu,
+ 0x040c1c04u, 0x040c1c14u, 0x040c240cu, 0x040c2c24u, 0x040c3e04u, 0x04140404u,
+ 0x04140414u, 0x04140424u, 0x04140c0cu, 0x04141404u, 0x04141414u, 0x04141c0cu,
+ 0x04141c1cu, 0x04141c3eu, 0x04142c0cu, 0x04142c3eu, 0x04143e2cu, 0x041c040cu,
+ 0x041c043eu, 0x041c0c04u, 0x041c0c14u, 0x041c142cu, 0x041c3e04u, 0x04240c1cu,
+ 0x04241c3eu, 0x04242424u, 0x04242c3eu, 0x04243e1cu, 0x04243e2cu, 0x042c040cu,
+ 0x042c043eu, 0x042c1c14u, 0x042c2c14u, 0x04341c2cu, 0x04343424u, 0x043e0c04u,
+ 0x043e0c24u, 0x043e0c34u, 0x043e241cu, 0x043e340cu, 0x0c04040cu, 0x0c04041cu,
+ 0x0c040c04u, 0x0c040c14u, 0x0c04140cu, 0x0c04141cu, 0x0c041c04u, 0x0c041c14u,
+ 0x0c041c24u, 0x0c04243eu, 0x0c042c04u, 0x0c0c0404u, 0x0c0c0414u, 0x0c0c0c0cu,
+ 0x0c0c1404u, 0x0c0c1414u, 0x0c14040cu, 0x0c14041cu, 0x0c140c04u, 0x0c140c14u,
+ 0x0c14140cu, 0x0c141c04u, 0x0c143e14u, 0x0c1c0404u, 0x0c1c0414u, 0x0c1c1404u,
+ 0x0c1c1c0cu, 0x0c1c2434u, 0x0c1c3434u, 0x0c24040cu, 0x0c24042cu, 0x0c242c04u,
+ 0x0c2c1404u, 0x0c2c1424u, 0x0c2c2434u, 0x0c2c3e0cu, 0x0c34042cu, 0x0c3e1414u,
+ 0x0c3e2404u, 0x14040404u, 0x14040414u, 0x14040c0cu, 0x14040c1cu, 0x14041404u,
+ 0x14041414u, 0x14041434u, 0x14041c0cu, 0x14042414u, 0x140c040cu, 0x140c041cu,
+ 0x140c042cu, 0x140c0c04u, 0x140c0c14u, 0x140c140cu, 0x140c1c04u, 0x140c341cu,
+ 0x140c343eu, 0x140c3e04u, 0x14140404u, 0x14140414u, 0x14140c0cu, 0x14140c3eu,
+ 0x14141404u, 0x14141414u, 0x14141c3eu, 0x14142404u, 0x14142c2cu, 0x141c040cu,
+ 0x141c0c04u, 0x141c0c24u, 0x141c3e04u, 0x141c3e24u, 0x14241c2cu, 0x14242c1cu,
+ 0x142c041cu, 0x142c143eu, 0x142c240cu, 0x142c3e24u, 0x143e040cu, 0x143e041cu,
+ 0x143e0c34u, 0x143e242cu, 0x1c04040cu, 0x1c040c04u, 0x1c040c14u, 0x1c04140cu,
+ 0x1c04141cu, 0x1c042c04u, 0x1c04342cu, 0x1c043e14u, 0x1c0c0404u, 0x1c0c0414u,
+ 0x1c0c1404u, 0x1c0c1c0cu, 0x1c0c2424u, 0x1c0c2434u, 0x1c14040cu, 0x1c14041cu,
+ 0x1c140c04u, 0x1c14142cu, 0x1c142c14u, 0x1c143e14u, 0x1c1c0c0cu, 0x1c1c1c1cu,
+ 0x1c241c04u, 0x1c24243eu, 0x1c243e14u, 0x1c2c0404u, 0x1c2c0434u, 0x1c2c1414u,
+ 0x1c2c2c2cu, 0x1c340c24u, 0x1c341c34u, 0x1c34341cu, 0x1c3e1c1cu, 0x1c3e3404u,
+ 0x24040424u, 0x24040c3eu, 0x24041c2cu, 0x24041c3eu, 0x24042c1cu, 0x24042c3eu,
+ 0x240c3e24u, 0x24141404u, 0x24141c3eu, 0x24142404u, 0x24143404u, 0x24143434u,
+ 0x241c043eu, 0x241c242cu, 0x24240424u, 0x24242c0cu, 0x24243424u, 0x242c142cu,
+ 0x242c241cu, 0x242c3e04u, 0x243e042cu, 0x243e0c04u, 0x243e0c14u, 0x243e1c04u,
+ 0x2c040c14u, 0x2c04240cu, 0x2c043e04u, 0x2c0c0404u, 0x2c0c0434u, 0x2c0c1434u,
+ 0x2c0c2c2cu, 0x2c140c24u, 0x2c141c14u, 0x2c143e14u, 0x2c1c0414u, 0x2c1c2c1cu,
+ 0x2c240c04u, 0x2c24141cu, 0x2c24143eu, 0x2c243e14u, 0x2c2c0414u, 0x2c2c1c0cu,
+ 0x2c342c04u, 0x2c3e1424u, 0x2c3e2414u, 0x34041424u, 0x34042424u, 0x34042434u,
+ 0x34043424u, 0x340c140cu, 0x340c340cu, 0x34140c3eu, 0x34143424u, 0x341c1c04u,
+ 0x341c1c34u, 0x34242424u, 0x342c042cu, 0x342c2c14u, 0x34341c1cu, 0x343e041cu,
+ 0x343e140cu, 0x3e04041cu, 0x3e04042cu, 0x3e04043eu, 0x3e040c04u, 0x3e041c14u,
+ 0x3e042c14u, 0x3e0c1434u, 0x3e0c2404u, 0x3e140c14u, 0x3e14242cu, 0x3e142c14u,
+ 0x3e1c0404u, 0x3e1c0c2cu, 0x3e1c1c1cu, 0x3e1c3404u, 0x3e24140cu, 0x3e24240cu,
+ 0x3e2c0404u, 0x3e2c0414u, 0x3e2c1424u, 0x3e341c04u);
+
+const uint IQ1S_GRID[1024] = uint[1024](
+ 0x00020000u, 0x00080005u, 0x0011000au, 0x00200015u, 0x00280022u, 0x0045002au,
+ 0x00540051u, 0x00650056u, 0x00820080u, 0x008a0088u, 0x00a00095u, 0x00a800a2u,
+ 0x010400aau, 0x01110105u, 0x01160114u, 0x011a0119u, 0x01410125u, 0x01490146u,
+ 0x01550152u, 0x0161015au, 0x01660164u, 0x01850168u, 0x01940191u, 0x01a50196u,
+ 0x02020200u, 0x020a0208u, 0x02200215u, 0x02280222u, 0x0245022au, 0x02590251u,
+ 0x02690264u, 0x02820280u, 0x028a0288u, 0x02950291u, 0x02a00299u, 0x02a802a2u,
+ 0x041102aau, 0x04160414u, 0x04410425u, 0x04550449u, 0x0464045au, 0x04910465u,
+ 0x04a50499u, 0x05040501u, 0x05060505u, 0x05180515u, 0x0529051au, 0x05450540u,
+ 0x0550054au, 0x05540551u, 0x05560555u, 0x05600559u, 0x05650562u, 0x056a0568u,
+ 0x05910581u, 0x05980595u, 0x05a1059au, 0x05a505a4u, 0x05a905a6u, 0x06190614u,
+ 0x06440641u, 0x06520650u, 0x06580655u, 0x06610660u, 0x06690666u, 0x06910685u,
+ 0x06990694u, 0x08020800u, 0x080a0808u, 0x08200815u, 0x08280822u, 0x0845082au,
+ 0x08560851u, 0x08800865u, 0x08880882u, 0x0895088au, 0x08a208a0u, 0x08aa08a8u,
+ 0x09110905u, 0x09190914u, 0x09250924u, 0x09500941u, 0x09550951u, 0x09640961u,
+ 0x09910969u, 0x09960994u, 0x09a50999u, 0x0a020a00u, 0x0a0a0a08u, 0x0a200a15u,
+ 0x0a280a22u, 0x0a450a2au, 0x0a590a51u, 0x0a650a61u, 0x0a820a80u, 0x0a880a85u,
+ 0x0a950a8au, 0x0aa20aa0u, 0x0aaa0aa8u, 0x10111010u, 0x10191014u, 0x10251024u,
+ 0x10441041u, 0x10551050u, 0x10611058u, 0x10651064u, 0x10911069u, 0x10961094u,
+ 0x10a510a1u, 0x11041101u, 0x11091106u, 0x11121110u, 0x11181115u, 0x11241121u,
+ 0x11451129u, 0x1150114au, 0x11521151u, 0x11551154u, 0x11591156u, 0x11651160u,
+ 0x11921184u, 0x11a11195u, 0x121111a4u, 0x12161214u, 0x12401225u, 0x12491246u,
+ 0x12551252u, 0x125a1258u, 0x12661264u, 0x12911285u, 0x12961294u, 0x140112a5u,
+ 0x14091406u, 0x14151414u, 0x14191418u, 0x14261421u, 0x14451441u, 0x14481446u,
+ 0x1451144au, 0x14551454u, 0x14591456u, 0x14651462u, 0x14841468u, 0x14901489u,
+ 0x14951494u, 0x14991498u, 0x14a1149au, 0x14a514a4u, 0x150214a9u, 0x150a1505u,
+ 0x15141511u, 0x15161515u, 0x15201519u, 0x15251522u, 0x152a1528u, 0x15441541u,
+ 0x15461545u, 0x15521551u, 0x15551554u, 0x15591556u, 0x1561155au, 0x15651564u,
+ 0x15691566u, 0x15821580u, 0x15851584u, 0x158a1588u, 0x15911590u, 0x15951594u,
+ 0x15991596u, 0x15a0159au, 0x15a515a2u, 0x16041601u, 0x16061605u, 0x16161615u,
+ 0x161a1618u, 0x16261621u, 0x16421640u, 0x16451644u, 0x164a1648u, 0x16551651u,
+ 0x16581656u, 0x16611659u, 0x16651664u, 0x16691668u, 0x1686166au, 0x1692168au,
+ 0x16a41695u, 0x181116a9u, 0x18251816u, 0x18441841u, 0x18491846u, 0x18551850u,
+ 0x185a1858u, 0x18611860u, 0x18661864u, 0x18851869u, 0x18941891u, 0x191018a5u,
+ 0x19151912u, 0x1921191au, 0x19421925u, 0x19451944u, 0x19511948u, 0x19551954u,
+ 0x19591956u, 0x1960195au, 0x196a1965u, 0x19911989u, 0x19951992u, 0x19a11998u,
+ 0x19a919a6u, 0x1a161a09u, 0x1a261a24u, 0x1a461a44u, 0x1a501a49u, 0x1a551a52u,
+ 0x1a611a58u, 0x1a691a66u, 0x1a911a85u, 0x1a9a1a96u, 0x20022000u, 0x200a2008u,
+ 0x20202015u, 0x20252022u, 0x202a2028u, 0x20512045u, 0x20612059u, 0x20802065u,
+ 0x20882082u, 0x2095208au, 0x20a220a0u, 0x20a820a5u, 0x210520aau, 0x21142111u,
+ 0x21252119u, 0x21442142u, 0x21552149u, 0x215a2158u, 0x21642161u, 0x21662165u,
+ 0x21902185u, 0x21992196u, 0x220121a5u, 0x220a2208u, 0x22152211u, 0x22222220u,
+ 0x222a2228u, 0x22512245u, 0x22592256u, 0x22812265u, 0x228a2288u, 0x22952291u,
+ 0x22a222a0u, 0x22aa22a8u, 0x24142405u, 0x24192416u, 0x24442425u, 0x24462445u,
+ 0x24522449u, 0x24582455u, 0x2466245au, 0x24912485u, 0x24992494u, 0x24a524a1u,
+ 0x25152509u, 0x25292521u, 0x25452540u, 0x25512548u, 0x25552554u, 0x25622559u,
+ 0x25682565u, 0x25902589u, 0x25952594u, 0x259a2598u, 0x25a425a1u, 0x25a925a6u,
+ 0x26102605u, 0x26192612u, 0x26412625u, 0x26552649u, 0x26612660u, 0x26842669u,
+ 0x26902686u, 0x2800269au, 0x28082802u, 0x2815280au, 0x28222820u, 0x282a2828u,
+ 0x28512845u, 0x28652854u, 0x28822880u, 0x288a2888u, 0x28a228a0u, 0x28aa28a8u,
+ 0x29112909u, 0x29192914u, 0x29462925u, 0x29522949u, 0x29612955u, 0x29662964u,
+ 0x29852969u, 0x29962990u, 0x29a42999u, 0x2a0029a5u, 0x2a082a02u, 0x2a202a0au,
+ 0x2a282a22u, 0x2a452a2au, 0x2a562a51u, 0x2a652a59u, 0x2a822a80u, 0x2a8a2a88u,
+ 0x2aa02a95u, 0x2aa82aa2u, 0x40052aaau, 0x40164011u, 0x40494025u, 0x40554052u,
+ 0x405a4058u, 0x40644061u, 0x40944066u, 0x40a14099u, 0x410040a6u, 0x41044101u,
+ 0x41094106u, 0x41154112u, 0x41184116u, 0x4121411au, 0x41294126u, 0x41484145u,
+ 0x4151414au, 0x41554154u, 0x41594156u, 0x4165415au, 0x416a4168u, 0x41844181u,
+ 0x41904186u, 0x41954192u, 0x41a141a0u, 0x420541a2u, 0x42144211u, 0x42254216u,
+ 0x42524241u, 0x425a4255u, 0x42694264u, 0x42944289u, 0x440142a5u, 0x44194415u,
+ 0x44454429u, 0x444a4448u, 0x44544451u, 0x44564455u, 0x44624461u, 0x44684465u,
+ 0x4481446au, 0x44894486u, 0x44924490u, 0x44a04495u, 0x44a944a1u, 0x45024501u,
+ 0x450a4505u, 0x45144511u, 0x45164515u, 0x45204519u, 0x452a4525u, 0x45444541u,
+ 0x45464545u, 0x45504549u, 0x45544551u, 0x45564555u, 0x45594558u, 0x45644561u,
+ 0x45664565u, 0x45824569u, 0x45854584u, 0x45914588u, 0x45954594u, 0x45994596u,
+ 0x45a5459au, 0x45aa45a8u, 0x46054601u, 0x46144609u, 0x46184615u, 0x4621461au,
+ 0x46294624u, 0x46424640u, 0x46484645u, 0x46514650u, 0x46554652u, 0x46594656u,
+ 0x46654662u, 0x46814668u, 0x468a4685u, 0x46954694u, 0x46a446a1u, 0x480546a6u,
+ 0x48154811u, 0x4825481au, 0x48494842u, 0x48554850u, 0x48614858u, 0x48664864u,
+ 0x48854869u, 0x48944891u, 0x48994896u, 0x490148a5u, 0x49064905u, 0x4910490au,
+ 0x49154914u, 0x49214918u, 0x49264924u, 0x49454940u, 0x4951494au, 0x49544952u,
+ 0x49564955u, 0x49604959u, 0x49654962u, 0x496a4966u, 0x49894986u, 0x49954992u,
+ 0x49984996u, 0x49a449a1u, 0x49a949a6u, 0x4a444a16u, 0x4a494a46u, 0x4a584a55u,
+ 0x4a644a5au, 0x4a944a69u, 0x50014aa5u, 0x50055004u, 0x50095006u, 0x50155012u,
+ 0x5021501au, 0x50295024u, 0x50455040u, 0x50515048u, 0x50555054u, 0x50595056u,
+ 0x50685065u, 0x50895086u, 0x50985095u, 0x50a150a0u, 0x50a950a6u, 0x51085105u,
+ 0x510a5109u, 0x51145111u, 0x51165115u, 0x51195118u, 0x51255120u, 0x51285126u,
+ 0x5141512au, 0x51455144u, 0x51495146u, 0x51515150u, 0x51545152u, 0x51565155u,
+ 0x51595158u, 0x5161515au, 0x51655164u, 0x51695166u, 0x51855182u, 0x51945191u,
+ 0x51965195u, 0x51a05199u, 0x51aa51a5u, 0x52065201u, 0x52155212u, 0x5221521au,
+ 0x52425224u, 0x524a5245u, 0x52545251u, 0x52565255u, 0x52625259u, 0x52855265u,
+ 0x52925290u, 0x52995295u, 0x52a4529au, 0x54055404u, 0x54145411u, 0x54165415u,
+ 0x54195418u, 0x54255421u, 0x542a5428u, 0x54445441u, 0x54465445u, 0x544a5449u,
+ 0x54515450u, 0x54555454u, 0x54585456u, 0x545a5459u, 0x54625461u, 0x54655464u,
+ 0x54695466u, 0x54885480u, 0x5491548au, 0x54955494u, 0x54995496u, 0x54a454a1u,
+ 0x54aa54a5u, 0x55025501u, 0x55055504u, 0x55095506u, 0x55115510u, 0x55145512u,
+ 0x55165515u, 0x551a5519u, 0x55245521u, 0x55265525u, 0x55405529u, 0x55425541u,
+ 0x55455544u, 0x55485546u, 0x55505549u, 0x55525551u, 0x55555554u, 0x55585556u,
+ 0x555a5559u, 0x55615560u, 0x55655564u, 0x55685566u, 0x556a5569u, 0x55845581u,
+ 0x55895585u, 0x5590558au, 0x55945591u, 0x55965595u, 0x55995598u, 0x55a455a1u,
+ 0x55a655a5u, 0x560055a9u, 0x56025601u, 0x56065604u, 0x56095608u, 0x56145611u,
+ 0x56185615u, 0x56205619u, 0x56225621u, 0x56255624u, 0x56285626u, 0x56415629u,
+ 0x56465645u, 0x56495648u, 0x5650564au, 0x56525651u, 0x56555654u, 0x56585656u,
+ 0x565a5659u, 0x56645661u, 0x56695665u, 0x56855682u, 0x56885686u, 0x568a5689u,
+ 0x56955691u, 0x56a2569au, 0x56a656a5u, 0x56a956a8u, 0x58055804u, 0x58095806u,
+ 0x58155810u, 0x58215818u, 0x5845582au, 0x584a5848u, 0x58545851u, 0x58565855u,
+ 0x58595858u, 0x58625860u, 0x58655864u, 0x58895882u, 0x58925890u, 0x58985895u,
+ 0x58a958a1u, 0x59025901u, 0x590a5905u, 0x59145911u, 0x59165915u, 0x59255919u,
+ 0x59445941u, 0x59465945u, 0x59505949u, 0x59525951u, 0x59555954u, 0x59585956u,
+ 0x595a5959u, 0x59645961u, 0x59665965u, 0x59815969u, 0x59895985u, 0x59945991u,
+ 0x59965995u, 0x59995998u, 0x5a0459a5u, 0x5a155a08u, 0x5a205a1au, 0x5a265a25u,
+ 0x5a455a29u, 0x5a495a48u, 0x5a555a51u, 0x5a585a56u, 0x5a625a59u, 0x5a685a65u,
+ 0x5a815a6au, 0x5a925a8au, 0x5a965a95u, 0x5a9a5a98u, 0x60055aa1u, 0x60166014u,
+ 0x60256019u, 0x60506044u, 0x60566055u, 0x605a6058u, 0x60646061u, 0x60696066u,
+ 0x60966081u, 0x610160a5u, 0x61066104u, 0x61126109u, 0x61216115u, 0x61266122u,
+ 0x61456129u, 0x61516149u, 0x61566155u, 0x61656159u, 0x616a6166u, 0x618a6184u,
+ 0x61956192u, 0x61a661a1u, 0x621161a9u, 0x62196216u, 0x62416240u, 0x62556246u,
+ 0x62586256u, 0x62856260u, 0x62966291u, 0x641162a5u, 0x64156412u, 0x641a6416u,
+ 0x64266421u, 0x64406429u, 0x64456442u, 0x644a6448u, 0x64546451u, 0x64566455u,
+ 0x645a6459u, 0x64626460u, 0x64846465u, 0x64896485u, 0x64926490u, 0x64956494u,
+ 0x64986496u, 0x64a1649au, 0x64a964a4u, 0x65086505u, 0x6511650au, 0x65166515u,
+ 0x65446519u, 0x65466545u, 0x65506549u, 0x65546551u, 0x65566555u, 0x65616559u,
+ 0x65656564u, 0x65696566u, 0x65896586u, 0x6591658au, 0x65966595u, 0x659a6599u,
+ 0x65a565a2u, 0x65a865a6u, 0x66096602u, 0x66206615u, 0x66286626u, 0x66406629u,
+ 0x66486645u, 0x6651664au, 0x66556654u, 0x66586656u, 0x6660665au, 0x66686665u,
+ 0x66826680u, 0x668a6685u, 0x66966694u, 0x66996698u, 0x66a466a0u, 0x66aa66a6u,
+ 0x68196816u, 0x68416825u, 0x68556852u, 0x6861685au, 0x68856869u, 0x68986891u,
+ 0x690168a6u, 0x69106904u, 0x69216915u, 0x69266924u, 0x69406929u, 0x69456941u,
+ 0x69486946u, 0x69546951u, 0x69566955u, 0x69606959u, 0x696a6965u, 0x69846982u,
+ 0x6995698au, 0x69a469a1u, 0x69a969a5u, 0x6a166a11u, 0x6a416a18u, 0x6a496a44u,
+ 0x6a556a50u, 0x6a5a6a58u, 0x6a656a64u, 0x6a866a69u, 0x6a986a94u, 0x6aa66a9au,
+ 0x80028000u, 0x800a8008u, 0x80228020u, 0x802a8028u, 0x80508045u, 0x80548051u,
+ 0x80598056u, 0x80808065u, 0x80888082u, 0x8095808au, 0x80a280a0u, 0x80aa80a8u,
+ 0x81118105u, 0x81168114u, 0x81258119u, 0x81448141u, 0x81508149u, 0x81558152u,
+ 0x81588156u, 0x81648159u, 0x81698166u, 0x81898185u, 0x81968194u, 0x81a58199u,
+ 0x82028200u, 0x820a8208u, 0x82208215u, 0x82288222u, 0x8251822au, 0x82598254u,
+ 0x82808265u, 0x82888282u, 0x8295828au, 0x82a282a0u, 0x82aa82a8u, 0x84198414u,
+ 0x84448441u, 0x84558451u, 0x8461845au, 0x84698464u, 0x84998494u, 0x85098501u,
+ 0x85158512u, 0x8526851au, 0x85408529u, 0x85458541u, 0x85518548u, 0x85558554u,
+ 0x85598556u, 0x8565855au, 0x85688566u, 0x8581856au, 0x85868584u, 0x85908589u,
+ 0x85958592u, 0x85a68598u, 0x86168611u, 0x86258619u, 0x86448641u, 0x864a8649u,
+ 0x86558650u, 0x865a8659u, 0x86668661u, 0x8685866au, 0x869a8691u, 0x880086a4u,
+ 0x88088802u, 0x8815880au, 0x88228820u, 0x882a8828u, 0x88458841u, 0x88548851u,
+ 0x88658859u, 0x88808869u, 0x88888882u, 0x8895888au, 0x88a288a0u, 0x88aa88a8u,
+ 0x89068905u, 0x89148911u, 0x89258916u, 0x89448941u, 0x89498946u, 0x89528950u,
+ 0x895a8955u, 0x89648961u, 0x89968985u, 0x89a58999u, 0x8a028a00u, 0x8a0a8a08u,
+ 0x8a208a15u, 0x8a288a22u, 0x8a458a2au, 0x8a548a51u, 0x8a808a56u, 0x8a888a82u,
+ 0x8a958a8au, 0x8aa28aa0u, 0x8aaa8aa8u, 0x90119005u, 0x90189016u, 0x90259019u,
+ 0x90469041u, 0x90559049u, 0x905a9058u, 0x906a9069u, 0x90919085u, 0x90969094u,
+ 0x90a59099u, 0x91049101u, 0x91099106u, 0x91159110u, 0x911a9118u, 0x91249121u,
+ 0x91299126u, 0x91459140u, 0x91519150u, 0x91559154u, 0x91599156u, 0x91659162u,
+ 0x91869184u, 0x91959192u, 0x91a19198u, 0x91a691a4u, 0x920591a9u, 0x92149211u,
+ 0x92259219u, 0x92469244u, 0x92509249u, 0x92559252u, 0x92669258u, 0x92859269u,
+ 0x92969294u, 0x940192a9u, 0x94069404u, 0x94159410u, 0x94269418u, 0x944a9440u,
+ 0x94549451u, 0x94569455u, 0x94599458u, 0x94619460u, 0x94659462u, 0x94869484u,
+ 0x94949492u, 0x94989495u, 0x94a994a1u, 0x95059500u, 0x950a9508u, 0x95119510u,
+ 0x95159514u, 0x95199516u, 0x95259521u, 0x952a9529u, 0x95449541u, 0x95469545u,
+ 0x95509549u, 0x95529551u, 0x95559554u, 0x95589556u, 0x955a9559u, 0x95649561u,
+ 0x95669565u, 0x95819569u, 0x95889585u, 0x95929591u, 0x95959594u, 0x95999596u,
+ 0x95a0959au, 0x95a595a2u, 0x95aa95a8u, 0x96049601u, 0x96159610u, 0x96209619u,
+ 0x96299626u, 0x96489645u, 0x96519649u, 0x96559652u, 0x96599656u, 0x96689665u,
+ 0x96849682u, 0x968a9689u, 0x96949692u, 0x96a49695u, 0x96a996a6u, 0x98169805u,
+ 0x98259819u, 0x98469841u, 0x98529850u, 0x98569855u, 0x9864985au, 0x98859865u,
+ 0x98969891u, 0x98a59899u, 0x99069904u, 0x99109909u, 0x99159912u, 0x991a9918u,
+ 0x99219920u, 0x99269924u, 0x99429940u, 0x99489945u, 0x9951994au, 0x99559954u,
+ 0x99599956u, 0x99659962u, 0x996a9966u, 0x99849981u, 0x99929990u, 0x999a9995u,
+ 0x99a699a1u, 0x9a159a05u, 0x9a449a25u, 0x9a499a46u, 0x9a559a50u, 0x9a619a58u,
+ 0x9a919a85u, 0x9a959a94u, 0xa0009a96u, 0xa008a002u, 0xa015a00au, 0xa022a020u,
+ 0xa02aa028u, 0xa051a045u, 0xa056a054u, 0xa080a059u, 0xa088a082u, 0xa095a08au,
+ 0xa0a2a0a0u, 0xa0aaa0a8u, 0xa109a105u, 0xa114a111u, 0xa119a116u, 0xa146a11au,
+ 0xa151a149u, 0xa158a155u, 0xa161a15au, 0xa185a164u, 0xa192a190u, 0xa199a196u,
+ 0xa208a202u, 0xa210a20au, 0xa222a219u, 0xa22aa228u, 0xa251a245u, 0xa259a256u,
+ 0xa280a265u, 0xa288a282u, 0xa295a28au, 0xa2a2a2a0u, 0xa2aaa2a8u, 0xa425a419u,
+ 0xa444a441u, 0xa454a450u, 0xa458a455u, 0xa461a45au, 0xa466a465u, 0xa469a468u,
+ 0xa506a485u, 0xa510a509u, 0xa515a512u, 0xa526a518u, 0xa542a529u, 0xa551a545u,
+ 0xa555a554u, 0xa559a556u, 0xa56aa565u, 0xa584a581u, 0xa586a585u, 0xa592a589u,
+ 0xa598a595u, 0xa611a605u, 0xa61aa616u, 0xa625a621u, 0xa646a644u, 0xa652a64au,
+ 0xa656a655u, 0xa660a658u, 0xa686a662u, 0xa695a690u, 0xa699a696u, 0xa6a4a6a1u,
+ 0xa800a6a6u, 0xa808a802u, 0xa820a80au, 0xa828a822u, 0xa851a82au, 0xa856a854u,
+ 0xa880a859u, 0xa888a882u, 0xa895a88au, 0xa8a2a8a0u, 0xa8aaa8a8u, 0xa914a905u,
+ 0xa921a919u, 0xa941a925u, 0xa955a950u, 0xa961a95au, 0xa969a966u, 0xa996a990u,
+ 0xaa02aa00u, 0xaa0aaa08u, 0xaa22aa20u, 0xaa2aaa28u, 0xaa54aa51u, 0xaa80aa56u,
+ 0xaa88aa82u, 0xaa95aa8au, 0xaaa2aaa0u, 0xaaaaaaa8u);
+
+const uint IQ3S_GRID[512] = uint[512](
+ 0x01010101u, 0x01010103u, 0x01010105u, 0x0101010bu, 0x0101010fu, 0x01010301u,
+ 0x01010303u, 0x01010305u, 0x01010309u, 0x0101030du, 0x01010501u, 0x01010503u,
+ 0x0101050bu, 0x01010707u, 0x01010901u, 0x01010905u, 0x0101090bu, 0x0101090fu,
+ 0x01010b03u, 0x01010b07u, 0x01010d01u, 0x01010d05u, 0x01010f03u, 0x01010f09u,
+ 0x01010f0fu, 0x01030101u, 0x01030103u, 0x01030105u, 0x01030109u, 0x01030301u,
+ 0x01030303u, 0x0103030bu, 0x01030501u, 0x01030507u, 0x0103050fu, 0x01030703u,
+ 0x0103070bu, 0x01030909u, 0x01030d03u, 0x01030d0bu, 0x01030f05u, 0x01050101u,
+ 0x01050103u, 0x0105010bu, 0x0105010fu, 0x01050301u, 0x01050307u, 0x0105030du,
+ 0x01050503u, 0x0105050bu, 0x01050701u, 0x01050709u, 0x01050905u, 0x0105090bu,
+ 0x0105090fu, 0x01050b03u, 0x01050b07u, 0x01050f01u, 0x01050f07u, 0x01070107u,
+ 0x01070303u, 0x0107030bu, 0x01070501u, 0x01070505u, 0x01070703u, 0x01070707u,
+ 0x0107070du, 0x01070909u, 0x01070b01u, 0x01070b05u, 0x01070d0fu, 0x01070f03u,
+ 0x01070f0bu, 0x01090101u, 0x01090307u, 0x0109030fu, 0x01090503u, 0x01090509u,
+ 0x01090705u, 0x01090901u, 0x01090907u, 0x01090b03u, 0x01090f01u, 0x010b0105u,
+ 0x010b0109u, 0x010b0501u, 0x010b0505u, 0x010b050du, 0x010b0707u, 0x010b0903u,
+ 0x010b090bu, 0x010b090fu, 0x010b0d0du, 0x010b0f07u, 0x010d010du, 0x010d0303u,
+ 0x010d0307u, 0x010d0703u, 0x010d0b05u, 0x010d0f03u, 0x010f0101u, 0x010f0105u,
+ 0x010f0109u, 0x010f0501u, 0x010f0505u, 0x010f050du, 0x010f0707u, 0x010f0b01u,
+ 0x010f0b09u, 0x03010101u, 0x03010103u, 0x03010105u, 0x03010109u, 0x03010301u,
+ 0x03010303u, 0x03010307u, 0x0301030bu, 0x0301030fu, 0x03010501u, 0x03010505u,
+ 0x03010703u, 0x03010709u, 0x0301070du, 0x03010b09u, 0x03010b0du, 0x03010d03u,
+ 0x03010f05u, 0x03030101u, 0x03030103u, 0x03030107u, 0x0303010du, 0x03030301u,
+ 0x03030309u, 0x03030503u, 0x03030701u, 0x03030707u, 0x03030903u, 0x03030b01u,
+ 0x03030b05u, 0x03030f01u, 0x03030f0du, 0x03050101u, 0x03050305u, 0x0305030bu,
+ 0x0305030fu, 0x03050501u, 0x03050509u, 0x03050705u, 0x03050901u, 0x03050907u,
+ 0x03050b0bu, 0x03050d01u, 0x03050f05u, 0x03070103u, 0x03070109u, 0x0307010fu,
+ 0x03070301u, 0x03070307u, 0x03070503u, 0x0307050fu, 0x03070701u, 0x03070709u,
+ 0x03070903u, 0x03070d05u, 0x03070f01u, 0x03090107u, 0x0309010bu, 0x03090305u,
+ 0x03090309u, 0x03090703u, 0x03090707u, 0x03090905u, 0x0309090du, 0x03090b01u,
+ 0x03090b09u, 0x030b0103u, 0x030b0301u, 0x030b0307u, 0x030b0503u, 0x030b0701u,
+ 0x030b0705u, 0x030b0b03u, 0x030d0501u, 0x030d0509u, 0x030d050fu, 0x030d0909u,
+ 0x030d090du, 0x030f0103u, 0x030f0107u, 0x030f0301u, 0x030f0305u, 0x030f0503u,
+ 0x030f070bu, 0x030f0903u, 0x030f0d05u, 0x030f0f01u, 0x05010101u, 0x05010103u,
+ 0x05010107u, 0x0501010bu, 0x0501010fu, 0x05010301u, 0x05010305u, 0x05010309u,
+ 0x0501030du, 0x05010503u, 0x05010507u, 0x0501050fu, 0x05010701u, 0x05010705u,
+ 0x05010903u, 0x05010907u, 0x0501090bu, 0x05010b01u, 0x05010b05u, 0x05010d0fu,
+ 0x05010f01u, 0x05010f07u, 0x05010f0bu, 0x05030101u, 0x05030105u, 0x05030301u,
+ 0x05030307u, 0x0503030fu, 0x05030505u, 0x0503050bu, 0x05030703u, 0x05030709u,
+ 0x05030905u, 0x05030b03u, 0x05050103u, 0x05050109u, 0x0505010fu, 0x05050503u,
+ 0x05050507u, 0x05050701u, 0x0505070fu, 0x05050903u, 0x05050b07u, 0x05050b0fu,
+ 0x05050f03u, 0x05050f09u, 0x05070101u, 0x05070105u, 0x0507010bu, 0x05070303u,
+ 0x05070505u, 0x05070509u, 0x05070703u, 0x05070707u, 0x05070905u, 0x05070b01u,
+ 0x05070d0du, 0x05090103u, 0x0509010fu, 0x05090501u, 0x05090507u, 0x05090705u,
+ 0x0509070bu, 0x05090903u, 0x05090f05u, 0x05090f0bu, 0x050b0109u, 0x050b0303u,
+ 0x050b0505u, 0x050b070fu, 0x050b0901u, 0x050b0b07u, 0x050b0f01u, 0x050d0101u,
+ 0x050d0105u, 0x050d010fu, 0x050d0503u, 0x050d0b0bu, 0x050d0d03u, 0x050f010bu,
+ 0x050f0303u, 0x050f050du, 0x050f0701u, 0x050f0907u, 0x050f0b01u, 0x07010105u,
+ 0x07010303u, 0x07010307u, 0x0701030bu, 0x0701030fu, 0x07010505u, 0x07010703u,
+ 0x07010707u, 0x0701070bu, 0x07010905u, 0x07010909u, 0x0701090fu, 0x07010b03u,
+ 0x07010d07u, 0x07010f03u, 0x07030103u, 0x07030107u, 0x0703010bu, 0x07030309u,
+ 0x07030503u, 0x07030507u, 0x07030901u, 0x07030d01u, 0x07030f05u, 0x07030f0du,
+ 0x07050101u, 0x07050305u, 0x07050501u, 0x07050705u, 0x07050709u, 0x07050b01u,
+ 0x07070103u, 0x07070301u, 0x07070309u, 0x07070503u, 0x07070507u, 0x0707050fu,
+ 0x07070701u, 0x07070903u, 0x07070907u, 0x0707090fu, 0x07070b0bu, 0x07070f07u,
+ 0x07090107u, 0x07090303u, 0x0709030du, 0x07090505u, 0x07090703u, 0x07090b05u,
+ 0x07090d01u, 0x07090d09u, 0x070b0103u, 0x070b0301u, 0x070b0305u, 0x070b050bu,
+ 0x070b0705u, 0x070b0909u, 0x070b0b0du, 0x070b0f07u, 0x070d030du, 0x070d0903u,
+ 0x070f0103u, 0x070f0107u, 0x070f0501u, 0x070f0505u, 0x070f070bu, 0x09010101u,
+ 0x09010109u, 0x09010305u, 0x09010501u, 0x09010509u, 0x0901050fu, 0x09010705u,
+ 0x09010903u, 0x09010b01u, 0x09010f01u, 0x09030105u, 0x0903010fu, 0x09030303u,
+ 0x09030307u, 0x09030505u, 0x09030701u, 0x0903070bu, 0x09030907u, 0x09030b03u,
+ 0x09030b0bu, 0x09050103u, 0x09050107u, 0x09050301u, 0x0905030bu, 0x09050503u,
+ 0x09050707u, 0x09050901u, 0x09050b0fu, 0x09050d05u, 0x09050f01u, 0x09070109u,
+ 0x09070303u, 0x09070307u, 0x09070501u, 0x09070505u, 0x09070703u, 0x0907070bu,
+ 0x09090101u, 0x09090105u, 0x09090509u, 0x0909070fu, 0x09090901u, 0x09090f03u,
+ 0x090b010bu, 0x090b010fu, 0x090b0503u, 0x090b0d05u, 0x090d0307u, 0x090d0709u,
+ 0x090d0d01u, 0x090f0301u, 0x090f030bu, 0x090f0701u, 0x090f0907u, 0x090f0b03u,
+ 0x0b010105u, 0x0b010301u, 0x0b010309u, 0x0b010505u, 0x0b010901u, 0x0b010909u,
+ 0x0b01090fu, 0x0b010b05u, 0x0b010d0du, 0x0b010f09u, 0x0b030103u, 0x0b030107u,
+ 0x0b03010bu, 0x0b030305u, 0x0b030503u, 0x0b030705u, 0x0b030f05u, 0x0b050101u,
+ 0x0b050303u, 0x0b050507u, 0x0b050701u, 0x0b05070du, 0x0b050b07u, 0x0b070105u,
+ 0x0b07010fu, 0x0b070301u, 0x0b07050fu, 0x0b070909u, 0x0b070b03u, 0x0b070d0bu,
+ 0x0b070f07u, 0x0b090103u, 0x0b090109u, 0x0b090501u, 0x0b090705u, 0x0b09090du,
+ 0x0b0b0305u, 0x0b0b050du, 0x0b0b0b03u, 0x0b0b0b07u, 0x0b0d0905u, 0x0b0f0105u,
+ 0x0b0f0109u, 0x0b0f0505u, 0x0d010303u, 0x0d010307u, 0x0d01030bu, 0x0d010703u,
+ 0x0d010707u, 0x0d010d01u, 0x0d030101u, 0x0d030501u, 0x0d03050fu, 0x0d030d09u,
+ 0x0d050305u, 0x0d050709u, 0x0d050905u, 0x0d050b0bu, 0x0d050d05u, 0x0d050f01u,
+ 0x0d070101u, 0x0d070309u, 0x0d070503u, 0x0d070901u, 0x0d09050bu, 0x0d090907u,
+ 0x0d090d05u, 0x0d0b0101u, 0x0d0b0107u, 0x0d0b0709u, 0x0d0b0d01u, 0x0d0d010bu,
+ 0x0d0d0901u, 0x0d0f0303u, 0x0d0f0307u, 0x0f010101u, 0x0f010109u, 0x0f01010fu,
+ 0x0f010501u, 0x0f010505u, 0x0f01070du, 0x0f010901u, 0x0f010b09u, 0x0f010d05u,
+ 0x0f030105u, 0x0f030303u, 0x0f030509u, 0x0f030907u, 0x0f03090bu, 0x0f050103u,
+ 0x0f050109u, 0x0f050301u, 0x0f05030du, 0x0f050503u, 0x0f050701u, 0x0f050b03u,
+ 0x0f070105u, 0x0f070705u, 0x0f07070bu, 0x0f070b07u, 0x0f090103u, 0x0f09010bu,
+ 0x0f090307u, 0x0f090501u, 0x0f090b01u, 0x0f0b0505u, 0x0f0b0905u, 0x0f0d0105u,
+ 0x0f0d0703u, 0x0f0f0101u);
+
+const int IQ4[16] = int[16](-127, -104, -83, -65, -49, -35, -22, -10,
+ 1, 13, 25, 38, 53, 69, 89, 113);
+
+/* the grid (or IQ4_XS's levels) in the workgroup's shared memory: a
+ constant array read at a varying index is far slower on some GPUs
+ (iq4xs_matvec.comp) */
+shared uint GS[NGW];
+
+/* byte, 16-bit word and realigned word at byte off of the weights */
+uint bbyte(U1 W, uint off) { return (W.v[off >> 2] >> (8u * (off & 3u))) & 0xffu; }
+uint bhalf(U1 W, uint off) { return bbyte(W, off) | (bbyte(W, off + 1u) << 8); }
+uint bword(U1 W, uint off)
+{
+ uint a = off >> 2, sh = 8u * (off & 3u);
+ return sh != 0u ? (W.v[a] >> sh) | (W.v[a + 1u] << (32u - sh)) : W.v[a];
+}
+
+uint signs7(uint i) { return i | ((uint(bitCount(i)) & 1u) << 7); }
+
+/* four levels of a grid word, each negated where its sign bit is set */
+uint signed4(uint g, uint s)
+{
+ uint r = 0u;
+ for (uint i = 0; i < 4u; i++) {
+ int v = int((g >> (8u * i)) & 0xffu);
+ if (((s >> i) & 1u) != 0u)
+ v = -v;
+ r |= (uint(v) & 0xffu) << (8u * i);
+ }
+ return r;
+}
+
+/* levels 4h..4h+3 of an IQ1 grid entry, 8 level + delta, as signed bytes */
+uint iq1w(uint idx, uint h, int delta)
+{
+ uint e = (GS[idx >> 1] >> (16u * (idx & 1u))) & 0xffffu, r = 0u;
+ for (uint i = 0; i < 4u; i++) {
+ int lv = int((e >> (2u * (4u * h + i))) & 3u) - 1;
+ r |= (uint(8 * lv + delta) & 0xffu) << (8u * i);
+ }
+ return r;
+}
+
+/* each byte of w (a nibble) through IQ4_XS's levels */
+uint lv4(uint w)
+{
+ return GS[w & 15u] | (GS[(w >> 8) & 15u] << 8) | (GS[(w >> 16) & 15u] << 16) |
+ (GS[(w >> 24) & 15u] << 24);
+}
+
+/* word kw (weights 4 kw .. 4 kw + 3) of group gb of the block at byte boff */
+uint wword(U1 W, uint boff, uint gb, uint kw)
+{
+ uint k = kw / 2u, h = kw % 2u;
+ if (KIND == 0u) { /* Q2_K: weight e = 128 n + 32 j + l at bits 2 j of byte 32 n + l */
+ uint q = bword(W, boff + 16u + 32u * (gb / 4u) + 4u * kw);
+ return (q >> (2u * (gb % 4u))) & 0x03030303u;
+ }
+ if (KIND == 1u) {
+ uint aux1 = bword(W, boff + 6u + 8u * gb);
+ uint sg = signs7((aux1 >> (7u * k)) & 127u);
+ return signed4(GS[2u * bbyte(W, boff + 2u + 8u * gb + k) + h], (sg >> (4u * h)) & 15u);
+ }
+ if (KIND == 2u) {
+ uint q = bhalf(W, boff + 2u + 2u * (4u * gb + k));
+ return signed4(GS[2u * (q & 511u) + h], (signs7(q >> 9) >> (4u * h)) & 15u);
+ }
+ if (KIND == 3u) {
+ uint idx = bbyte(W, boff + 2u + 4u * gb + k) |
+ ((bbyte(W, boff + 66u + gb) << (8u - 2u * k)) & 0x300u);
+ uint sg = bbyte(W, boff + 34u + 4u * gb + k);
+ return signed4(GS[2u * idx + h], (sg >> (4u * h)) & 15u);
+ }
+ if (KIND == 4u) {
+ uint aux = bword(W, boff + 66u + 4u * gb);
+ uint sg = signs7((aux >> (7u * k)) & 127u);
+ return signed4(GS[bbyte(W, boff + 2u + 8u * gb + 2u * k + h)], (sg >> (4u * h)) & 15u);
+ }
+ if (KIND == 5u) {
+ uint qh = bhalf(W, boff + 34u + 2u * gb);
+ uint idx = bbyte(W, boff + 2u + 4u * gb + k) | (((qh >> (3u * k)) & 7u) << 8);
+ return iq1w(idx, h, ((qh >> 15) & 1u) != 0u ? -1 : 1);
+ }
+ if (KIND == 6u) {
+ uint qh = bbyte(W, boff + 32u + 2u * gb + k / 2u);
+ uint idx = bbyte(W, boff + 4u * gb + k) | ((qh << ((k & 1u) != 0u ? 4u : 8u)) & 0x700u);
+ return iq1w(idx, h, ((qh >> ((k & 1u) != 0u ? 7u : 3u)) & 1u) != 0u ? -1 : 1);
+ }
+ if (KIND == 7u) {
+ uint idx = bbyte(W, boff + 2u + 8u * gb + kw) |
+ (((bbyte(W, boff + 66u + gb) >> kw) & 1u) << 8);
+ uint sg = bbyte(W, boff + 74u + 4u * gb + kw / 2u) >> (4u * (kw & 1u));
+ return signed4(GS[idx], sg & 15u);
+ }
+ uint q = bword(W, boff + 8u + 16u * gb + 4u * (kw & 3u));
+ return lv4(kw < 4u ? q & 0x0f0f0f0fu : (q >> 4) & 0x0f0f0f0fu);
+}
+
+/* group gb's scales: its halves' (x, y) and, Q2_K's, its minima (z, w) */
+vec4 gscales(U1 W, uint boff, uint gb)
+{
+ if (KIND == 0u) {
+ vec2 dm = unpackHalf2x16(bword(W, boff + 80u));
+ uint s0 = bbyte(W, boff + 2u * gb), s1 = bbyte(W, boff + 2u * gb + 1u);
+ return vec4(dm.x * float(s0 & 15u), dm.x * float(s1 & 15u), dm.y * float(s0 >> 4),
+ dm.y * float(s1 >> 4));
+ }
+ float d;
+ if (KIND == 6u) {
+ uint s0 = bhalf(W, boff + 48u), s1 = bhalf(W, boff + 50u), s2 = bhalf(W, boff + 52u),
+ s3 = bhalf(W, boff + 54u);
+ d = unpackHalf2x16((s0 >> 12) | ((s1 >> 8) & 0x00f0u) | ((s2 >> 4) & 0x0f00u) |
+ (s3 & 0xf000u)).x;
+ } else {
+ d = unpackHalf2x16(bhalf(W, boff)).x;
+ }
+ float lo, hi;
+ if (KIND == 1u) {
+ lo = hi = d * 0.125 * float(1u + 2u * (bword(W, boff + 6u + 8u * gb) >> 28));
+ } else if (KIND == 2u || KIND == 3u) {
+ uint s = bbyte(W, boff + (KIND == 2u ? 66u : 74u) + gb);
+ lo = d * 0.125 * float(1u + 2u * (s & 15u));
+ hi = d * 0.125 * float(1u + 2u * (s >> 4));
+ } else if (KIND == 4u) {
+ lo = hi = d * 0.25 * float(1u + 2u * (bword(W, boff + 66u + 4u * gb) >> 28));
+ } else if (KIND == 5u) {
+ lo = hi = d * 0.125 * float(2u * ((bhalf(W, boff + 34u + 2u * gb) >> 12) & 7u) + 1u);
+ } else if (KIND == 6u) {
+ uint s = bhalf(W, boff + 48u + 2u * (gb / 2u)), sh = 6u * (gb & 1u);
+ lo = d * 0.125 * float(2u * ((s >> sh) & 7u) + 1u);
+ hi = d * 0.125 * float(2u * ((s >> (sh + 3u)) & 7u) + 1u);
+ } else if (KIND == 7u) {
+ lo = hi = d * float(1u + 2u * ((bbyte(W, boff + 106u + gb / 2u) >> (4u * (gb & 1u))) & 15u));
+ } else {
+ uint sl = (bbyte(W, boff + 4u + gb / 2u) >> (4u * (gb & 1u))) & 15u;
+ uint sh = (bhalf(W, boff + 2u) >> (2u * gb)) & 3u;
+ lo = hi = d * float(int(sl | (sh << 4)) - 32);
+ }
+ return vec4(lo, hi, 0.0, 0.0);
+}
+
+
+void main()
+{
+ for (uint i = gl_LocalInvocationID.x; i < NGW; i += 128u)
+ GS[i] = KIND == 1u ? IQ2XXS_GRID[i] : KIND == 2u ? IQ2XS_GRID[i]
+ : KIND == 3u ? IQ2S_GRID[i] : KIND == 4u ? IQ3XXS_GRID[i]
+ : KIND == 5u || KIND == 6u ? IQ1S_GRID[i] : KIND == 7u ? IQ3S_GRID[i]
+ : KIND == 8u ? uint(IQ4[i]) & 0xffu : 0u;
+ barrier();
+ U4 TB = U4(p.t);
+ uvec4 tl = TB.v[p.tiles + gl_WorkGroupID.z];
+ uint e = tl.x;
+ uvec4 t0 = TB.v[3u * e], t1 = TB.v[3u * e + 1u];
+ uint nv = t1.z, ys = t1.w;
+ uint r0 = gl_WorkGroupID.x * BM, v0 = tl.y;
+ U4 X = U4(t0.zw);
+ U1 W1 = U1(t0.xy);
+ F Y = F(t1.xy);
+ uint id = gl_LocalInvocationID.x;
+ uint rq = (id % 32u) * 2u, vq = (id / 32u) * 4u;
+ float sums[8];
+ [[unroll]] for (uint i = 0; i < 8u; i++) sums[i] = 0.0;
+ for (uint g0 = 0; g0 < p.ngroups; g0 += KS) {
+ // weights: 4 invocations a row, 2 words each of the group's 8
+ {
+ uint iqs = id % 4u, rl = id / 4u;
+ [[unroll]] for (uint l = 0; l < BM; l += 32u) {
+ uint r = min(r0 + rl + l, p.rows - 1u);
+ [[unroll]] for (uint k = 0; k < KS; k++) {
+ uint g = g0 + k, boff = (r * p.nb + g / 8u) * BS;
+ [[unroll]] for (uint t = 0; t < 2u; t++)
+ sa[k * BM + rl + l].qs[2u * iqs + t] = int(wword(W1, boff, g % 8u, 2u * iqs + t));
+ if (iqs == 0u)
+ sa[k * BM + rl + l].sc = gscales(W1, boff, g % 8u);
+ }
+ }
+ }
+ // activations: 2 invocations a vector, each half a group's quants
+ {
+ uint k = id / 32u, c = (id % 32u) / 2u, iqs = id % 2u;
+ uint v = min(v0 + c, nv - 1u);
+ uint g = g0 + k, b = g / 8u, j = g % 8u;
+ uint xo = (v * p.nb + b) * XB;
+ uvec4 q = X.v[xo + 2u * j + iqs];
+ sb[k * BN + c].qs[4u * iqs] = int(q.x);
+ sb[k * BN + c].qs[4u * iqs + 1u] = int(q.y);
+ sb[k * BN + c].qs[4u * iqs + 2u] = int(q.z);
+ sb[k * BN + c].qs[4u * iqs + 3u] = int(q.w);
+ if (iqs == 0u) {
+ uvec4 bq = X.v[xo + 16u + j / 4u];
+ uint bw = j % 4u == 0u ? bq.x : j % 4u == 1u ? bq.y : j % 4u == 2u ? bq.z : bq.w;
+ sb[k * BN + c].d = uintBitsToFloat(X.v[xo + 18u].x);
+ sb[k * BN + c].bs = vec2(float(bitfieldExtract(int(bw), 0, 16)),
+ float(bitfieldExtract(int(bw), 16, 16)));
+ }
+ }
+ barrier();
+ for (uint k = 0; k < KS; k++) {
+ ACache ca[2];
+ [[unroll]] for (uint r = 0; r < 2u; r++)
+ ca[r] = sa[k * BM + rq + r];
+ [[unroll]] for (uint c = 0; c < 4u; c++) {
+ BCache cb = sb[k * BN + vq + c];
+ [[unroll]] for (uint r = 0; r < 2u; r++) {
+ int q0 = 0, q1 = 0;
+ [[unroll]] for (uint i = 0; i < 4u; i++) {
+ q0 += dotPacked4x8EXT(ca[r].qs[i], cb.qs[i]);
+ q1 += dotPacked4x8EXT(ca[r].qs[i + 4u], cb.qs[i + 4u]);
+ }
+ sums[c * 2u + r] += cb.d * (ca[r].sc.x * float(q0) + ca[r].sc.y * float(q1) -
+ (ca[r].sc.z * cb.bs.x + ca[r].sc.w * cb.bs.y));
+ }
+ }
+ }
+ barrier();
+ }
+ [[unroll]] for (uint c = 0; c < 4u; c++)
+ [[unroll]] for (uint r = 0; r < 2u; r++) {
+ uint row = r0 + rq + r, v = v0 + vq + c;
+ if (row < p.rows && v < nv)
+ Y.v[v * ys + row] = sums[c * 2u + r];
+ }
+}
diff --git a/src/llm/shaders/iqk_mmq.comp b/src/llm/shaders/iqk_mmq.comp
new file mode 100644
index 0000000..dca74ce
--- /dev/null
+++ b/src/llm/shaders/iqk_mmq.comp
@@ -0,0 +1,1190 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri
+ The tables IQ2XXS_GRID, IQ2XS_GRID, IQ2S_GRID, IQ3XXS_GRID, IQ1S_GRID and
+ IQ3S_GRID below are ggml's grids (ggml/src/ggml-common.h), copied as data
+ from src/llm/iq2xxs_grid.h, iq2xs_grid.h, iq2s_grid.h, iq3xxs_grid.h,
+ iq1s_grid.h and iq3s_grid.h (64-bit entries as two words, low first;
+ iq1s_grid's eight levels of -1, 0 or 1 as two bits each, an entry in 16
+ bits): they define the formats. Copyright (c) 2023-2026 The ggml
+ authors, under the MIT licence (see the ggml repository). Generated -
+ not formatted by hand. */
+#version 450
+// The fast prompt for the 256-block types without tiles of their own:
+// iq4nl_mmq.comp's tiling (64 rows by 64 vectors, groups of 32 staged 4 at
+// a time), the weights read as iqk_mmid.comp reads them, KIND as there (0
+// Q2_K, 1 IQ2_XXS, 2 IQ2_XS, 3 IQ2_S, 4 IQ3_XXS, 5 IQ1_S, 6 IQ1_M; 7 IQ3_S
+// and 8 IQ4_XS are made but have tiles of their own): a group of 32 its
+// signed bytes (Q2_K's unsigned) and a scale for each of its halves of 16,
+// for Q2_K a minimum for each taken off with the activations' sums of 16.
+// NOT bit for bit with the CPU: rounded group by group.
+#extension GL_EXT_control_flow_attributes : require
+#extension GL_EXT_integer_dot_product : require
+#extension GL_EXT_buffer_reference : require
+#extension GL_EXT_buffer_reference_uvec2 : require
+
+layout(local_size_x = 128) in;
+layout(constant_id = 0) const uint NV = 8; // unused: the table's constants
+layout(constant_id = 1) const uint SG = 8;
+layout(constant_id = 2) const uint KIND = 0;
+
+const uint BS = KIND == 0u ? 84u : KIND == 1u ? 66u : KIND == 2u ? 74u : KIND == 3u ? 82u
+ : KIND == 4u ? 98u : KIND == 5u ? 50u : KIND == 6u ? 56u : KIND == 7u ? 110u
+ : 136u;
+const uint NGW = KIND == 1u ? 512u : KIND == 2u ? 1024u : KIND == 3u ? 2048u : KIND == 4u ? 256u
+ : KIND == 5u || KIND == 6u ? 1024u : KIND == 7u ? 512u : KIND == 8u ? 16u : 1u;
+const uint BM = 64u, BN = 64u, KS = 4u;
+const uint WM = 32u, WN = 32u, TM = 2u, TN = 2u, WARP = 32u;
+const uint WNITER = (WM * WN) / (WARP * TM * TN); // 8
+const uint WSUBN = WN / WNITER; // 4
+const uint XB = 20u;
+
+layout(buffer_reference, std430, buffer_reference_align = 16) readonly buffer U4 { uvec4 v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer U1 { uint v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) writeonly buffer F { float v[]; };
+
+// w: the first row (rows of nb blocks of BS bytes); x: vector v, block b
+// at uvec4 (v * xs + b) * 20; y: row r of vector v at y[v * ys + r]
+layout(push_constant) uniform P { uvec2 w, x, y; uint rows, nb, nv, xs, ys; } p;
+
+struct ACache { int qs[8]; vec4 sc; }; // a group: its 32 weights, scales, minima
+struct BCache { int qs[8]; float d; vec2 bs; }; // a group: quants, the block's d8, its sums of 16
+shared ACache sa[BM * KS];
+shared BCache sb[BN * KS];
+
+const uint IQ2XXS_GRID[512] = uint[512](
+ 0x08080808u, 0x08080808u, 0x0808082bu, 0x08080808u, 0x08081919u, 0x08080808u,
+ 0x08082b08u, 0x08080808u, 0x08082b2bu, 0x08080808u, 0x08190819u, 0x08080808u,
+ 0x08191908u, 0x08080808u, 0x082b0808u, 0x08080808u, 0x082b082bu, 0x08080808u,
+ 0x082b2b08u, 0x08080808u, 0x082b2b2bu, 0x08080808u, 0x19080819u, 0x08080808u,
+ 0x19081908u, 0x08080808u, 0x19190808u, 0x08080808u, 0x19192b08u, 0x08080808u,
+ 0x192b0819u, 0x08080808u, 0x192b1908u, 0x08080808u, 0x2b080808u, 0x08080808u,
+ 0x2b08082bu, 0x08080808u, 0x2b082b2bu, 0x08080808u, 0x2b2b082bu, 0x08080808u,
+ 0x08080819u, 0x08080819u, 0x08081908u, 0x08080819u, 0x08190808u, 0x08080819u,
+ 0x08191919u, 0x08080819u, 0x19080808u, 0x08080819u, 0x2b081908u, 0x08080819u,
+ 0x2b192b08u, 0x08080819u, 0x08080808u, 0x0808082bu, 0x0808082bu, 0x0808082bu,
+ 0x082b082bu, 0x0808082bu, 0x2b08082bu, 0x0808082bu, 0x08080819u, 0x08081908u,
+ 0x08081908u, 0x08081908u, 0x08190808u, 0x08081908u, 0x082b0819u, 0x08081908u,
+ 0x082b1908u, 0x08081908u, 0x19080808u, 0x08081908u, 0x1908082bu, 0x08081908u,
+ 0x19082b08u, 0x08081908u, 0x192b0808u, 0x08081908u, 0x2b080819u, 0x08081908u,
+ 0x2b081908u, 0x08081908u, 0x2b190808u, 0x08081908u, 0x2b2b1908u, 0x08081908u,
+ 0x08080808u, 0x08081919u, 0x0808082bu, 0x08081919u, 0x08082b08u, 0x08081919u,
+ 0x082b0808u, 0x08081919u, 0x1908192bu, 0x08081919u, 0x192b2b19u, 0x08081919u,
+ 0x2b080808u, 0x08081919u, 0x2b190819u, 0x08081919u, 0x08082b19u, 0x0808192bu,
+ 0x08190808u, 0x0808192bu, 0x19080808u, 0x0808192bu, 0x2b081908u, 0x0808192bu,
+ 0x2b2b1908u, 0x0808192bu, 0x08080808u, 0x08082b08u, 0x08081919u, 0x08082b08u,
+ 0x08082b08u, 0x08082b08u, 0x08191908u, 0x08082b08u, 0x082b2b08u, 0x08082b08u,
+ 0x19080819u, 0x08082b08u, 0x19081908u, 0x08082b08u, 0x19190808u, 0x08082b08u,
+ 0x1919082bu, 0x08082b08u, 0x2b082b08u, 0x08082b08u, 0x08081908u, 0x08082b19u,
+ 0x19080808u, 0x08082b19u, 0x0808082bu, 0x08082b2bu, 0x08191908u, 0x08082b2bu,
+ 0x08080819u, 0x08190808u, 0x08081908u, 0x08190808u, 0x08190808u, 0x08190808u,
+ 0x082b0819u, 0x08190808u, 0x19080808u, 0x08190808u, 0x192b0808u, 0x08190808u,
+ 0x2b081908u, 0x08190808u, 0x2b190808u, 0x08190808u, 0x2b191919u, 0x08190808u,
+ 0x08080808u, 0x08190819u, 0x08082b08u, 0x08190819u, 0x082b0808u, 0x08190819u,
+ 0x19190808u, 0x08190819u, 0x19192b2bu, 0x08190819u, 0x2b080808u, 0x08190819u,
+ 0x082b1908u, 0x0819082bu, 0x19081919u, 0x0819082bu, 0x08080808u, 0x08191908u,
+ 0x08082b08u, 0x08191908u, 0x082b0808u, 0x08191908u, 0x082b1919u, 0x08191908u,
+ 0x19082b19u, 0x08191908u, 0x2b080808u, 0x08191908u, 0x08192b08u, 0x08191919u,
+ 0x192b082bu, 0x08191919u, 0x08080808u, 0x0819192bu, 0x0819192bu, 0x0819192bu,
+ 0x08080819u, 0x08192b08u, 0x08081908u, 0x08192b08u, 0x08190808u, 0x08192b08u,
+ 0x19080808u, 0x08192b08u, 0x2b080819u, 0x08192b08u, 0x08080808u, 0x08192b19u,
+ 0x08081919u, 0x08192b19u, 0x2b2b0808u, 0x08192b19u, 0x19190819u, 0x08192b2bu,
+ 0x08080808u, 0x082b0808u, 0x0808082bu, 0x082b0808u, 0x08082b2bu, 0x082b0808u,
+ 0x19081908u, 0x082b0808u, 0x192b0819u, 0x082b0808u, 0x2b080808u, 0x082b0808u,
+ 0x2b08082bu, 0x082b0808u, 0x082b2b19u, 0x082b0819u, 0x19082b08u, 0x082b0819u,
+ 0x08080808u, 0x082b082bu, 0x0808082bu, 0x082b082bu, 0x08080819u, 0x082b1908u,
+ 0x08081908u, 0x082b1908u, 0x08190808u, 0x082b1908u, 0x19080808u, 0x082b1908u,
+ 0x1919192bu, 0x082b1908u, 0x08080808u, 0x082b1919u, 0x19080819u, 0x082b1919u,
+ 0x192b1908u, 0x082b1919u, 0x2b190808u, 0x082b192bu, 0x08082b08u, 0x082b2b08u,
+ 0x082b0808u, 0x082b2b08u, 0x2b191908u, 0x082b2b08u, 0x19081908u, 0x082b2b2bu,
+ 0x08080819u, 0x19080808u, 0x08081908u, 0x19080808u, 0x08190808u, 0x19080808u,
+ 0x08192b08u, 0x19080808u, 0x082b0819u, 0x19080808u, 0x082b1908u, 0x19080808u,
+ 0x19080808u, 0x19080808u, 0x19082b08u, 0x19080808u, 0x1919192bu, 0x19080808u,
+ 0x192b0808u, 0x19080808u, 0x2b080819u, 0x19080808u, 0x2b081908u, 0x19080808u,
+ 0x2b190808u, 0x19080808u, 0x08080808u, 0x19080819u, 0x082b0808u, 0x19080819u,
+ 0x192b0819u, 0x19080819u, 0x2b080808u, 0x19080819u, 0x2b081919u, 0x19080819u,
+ 0x08080819u, 0x1908082bu, 0x08190808u, 0x1908082bu, 0x19082b08u, 0x1908082bu,
+ 0x1919192bu, 0x1908082bu, 0x192b2b08u, 0x1908082bu, 0x08080808u, 0x19081908u,
+ 0x08082b08u, 0x19081908u, 0x082b0808u, 0x19081908u, 0x2b080808u, 0x19081908u,
+ 0x2b192b19u, 0x19081908u, 0x0819082bu, 0x19081919u, 0x082b1908u, 0x19081919u,
+ 0x08080808u, 0x1908192bu, 0x08080819u, 0x19082b08u, 0x08081908u, 0x19082b08u,
+ 0x08190808u, 0x19082b08u, 0x19080808u, 0x19082b08u, 0x19081919u, 0x19082b08u,
+ 0x08080808u, 0x19082b19u, 0x19192b08u, 0x19082b19u, 0x192b0819u, 0x19082b19u,
+ 0x2b08082bu, 0x19082b19u, 0x19081919u, 0x19082b2bu, 0x2b190808u, 0x19082b2bu,
+ 0x08080808u, 0x19190808u, 0x08082b08u, 0x19190808u, 0x08190819u, 0x19190808u,
+ 0x08192b19u, 0x19190808u, 0x082b0808u, 0x19190808u, 0x2b080808u, 0x19190808u,
+ 0x2b082b08u, 0x19190808u, 0x08081908u, 0x19190819u, 0x1908082bu, 0x19190819u,
+ 0x2b2b1908u, 0x19190819u, 0x2b190819u, 0x1919082bu, 0x2b190808u, 0x19191908u,
+ 0x2b19082bu, 0x19191908u, 0x08082b2bu, 0x19191919u, 0x08080819u, 0x1919192bu,
+ 0x19191908u, 0x1919192bu, 0x08080808u, 0x19192b08u, 0x08190819u, 0x19192b08u,
+ 0x08192b19u, 0x19192b08u, 0x192b1908u, 0x19192b08u, 0x19080808u, 0x19192b19u,
+ 0x08082b08u, 0x19192b2bu, 0x08081908u, 0x192b0808u, 0x08190808u, 0x192b0808u,
+ 0x19080808u, 0x192b0808u, 0x192b2b08u, 0x192b0808u, 0x08080808u, 0x192b0819u,
+ 0x19191919u, 0x192b0819u, 0x08192b08u, 0x192b082bu, 0x192b0808u, 0x192b082bu,
+ 0x08080808u, 0x192b1908u, 0x08081919u, 0x192b1908u, 0x08190808u, 0x192b1919u,
+ 0x0819082bu, 0x192b1919u, 0x2b081908u, 0x192b1919u, 0x1908082bu, 0x192b2b08u,
+ 0x08080808u, 0x2b080808u, 0x0808082bu, 0x2b080808u, 0x08082b2bu, 0x2b080808u,
+ 0x19080819u, 0x2b080808u, 0x2b08082bu, 0x2b080808u, 0x08081908u, 0x2b080819u,
+ 0x08192b08u, 0x2b080819u, 0x19080808u, 0x2b080819u, 0x08190819u, 0x2b08082bu,
+ 0x08080819u, 0x2b081908u, 0x08081908u, 0x2b081908u, 0x08190808u, 0x2b081908u,
+ 0x08191919u, 0x2b081908u, 0x19080808u, 0x2b081908u, 0x192b0808u, 0x2b081908u,
+ 0x08080808u, 0x2b081919u, 0x1908192bu, 0x2b081919u, 0x2b191908u, 0x2b081919u,
+ 0x08082b19u, 0x2b08192bu, 0x19080808u, 0x2b08192bu, 0x192b0808u, 0x2b08192bu,
+ 0x0808082bu, 0x2b082b08u, 0x08081908u, 0x2b082b19u, 0x08190819u, 0x2b082b2bu,
+ 0x08081908u, 0x2b190808u, 0x08190808u, 0x2b190808u, 0x082b1908u, 0x2b190808u,
+ 0x19080808u, 0x2b190808u, 0x2b2b0819u, 0x2b190808u, 0x0819192bu, 0x2b190819u,
+ 0x2b080808u, 0x2b190819u, 0x19081919u, 0x2b19082bu, 0x08080808u, 0x2b191908u,
+ 0x082b082bu, 0x2b191908u, 0x19081908u, 0x2b191908u, 0x19190819u, 0x2b191919u,
+ 0x2b080819u, 0x2b192b08u, 0x082b0808u, 0x2b192b19u, 0x0808082bu, 0x2b2b0808u,
+ 0x19190808u, 0x2b2b0808u, 0x2b081919u, 0x2b2b0808u, 0x08082b19u, 0x2b2b0819u,
+ 0x08080808u, 0x2b2b082bu, 0x08192b08u, 0x2b2b1908u, 0x19190808u, 0x2b2b2b08u,
+ 0x08081908u, 0x2b2b2b19u);
+
+const uint IQ2XS_GRID[1024] = uint[1024](
+ 0x08080808u, 0x08080808u, 0x0808082bu, 0x08080808u, 0x08081919u, 0x08080808u,
+ 0x08082b08u, 0x08080808u, 0x08082b2bu, 0x08080808u, 0x08190819u, 0x08080808u,
+ 0x08191908u, 0x08080808u, 0x0819192bu, 0x08080808u, 0x08192b19u, 0x08080808u,
+ 0x082b0808u, 0x08080808u, 0x082b082bu, 0x08080808u, 0x082b1919u, 0x08080808u,
+ 0x082b2b08u, 0x08080808u, 0x19080819u, 0x08080808u, 0x19081908u, 0x08080808u,
+ 0x1908192bu, 0x08080808u, 0x19082b19u, 0x08080808u, 0x19190808u, 0x08080808u,
+ 0x1919082bu, 0x08080808u, 0x19191919u, 0x08080808u, 0x19192b08u, 0x08080808u,
+ 0x192b0819u, 0x08080808u, 0x192b1908u, 0x08080808u, 0x2b080808u, 0x08080808u,
+ 0x2b08082bu, 0x08080808u, 0x2b081919u, 0x08080808u, 0x2b082b08u, 0x08080808u,
+ 0x2b190819u, 0x08080808u, 0x2b191908u, 0x08080808u, 0x2b192b19u, 0x08080808u,
+ 0x2b2b0808u, 0x08080808u, 0x08080819u, 0x08080819u, 0x08081908u, 0x08080819u,
+ 0x0808192bu, 0x08080819u, 0x08082b19u, 0x08080819u, 0x08190808u, 0x08080819u,
+ 0x0819082bu, 0x08080819u, 0x08191919u, 0x08080819u, 0x08192b08u, 0x08080819u,
+ 0x08192b2bu, 0x08080819u, 0x082b0819u, 0x08080819u, 0x082b1908u, 0x08080819u,
+ 0x19080808u, 0x08080819u, 0x1908082bu, 0x08080819u, 0x19081919u, 0x08080819u,
+ 0x19082b08u, 0x08080819u, 0x19190819u, 0x08080819u, 0x19191908u, 0x08080819u,
+ 0x192b0808u, 0x08080819u, 0x192b2b08u, 0x08080819u, 0x2b080819u, 0x08080819u,
+ 0x2b081908u, 0x08080819u, 0x2b190808u, 0x08080819u, 0x08080808u, 0x0808082bu,
+ 0x0808082bu, 0x0808082bu, 0x08081919u, 0x0808082bu, 0x08082b08u, 0x0808082bu,
+ 0x08190819u, 0x0808082bu, 0x08191908u, 0x0808082bu, 0x082b0808u, 0x0808082bu,
+ 0x19080819u, 0x0808082bu, 0x19081908u, 0x0808082bu, 0x19190808u, 0x0808082bu,
+ 0x19191919u, 0x0808082bu, 0x2b080808u, 0x0808082bu, 0x2b082b2bu, 0x0808082bu,
+ 0x08080819u, 0x08081908u, 0x08081908u, 0x08081908u, 0x0808192bu, 0x08081908u,
+ 0x08082b19u, 0x08081908u, 0x08190808u, 0x08081908u, 0x0819082bu, 0x08081908u,
+ 0x08191919u, 0x08081908u, 0x08192b08u, 0x08081908u, 0x082b0819u, 0x08081908u,
+ 0x082b1908u, 0x08081908u, 0x19080808u, 0x08081908u, 0x1908082bu, 0x08081908u,
+ 0x19081919u, 0x08081908u, 0x19082b08u, 0x08081908u, 0x19190819u, 0x08081908u,
+ 0x19191908u, 0x08081908u, 0x1919192bu, 0x08081908u, 0x192b0808u, 0x08081908u,
+ 0x2b080819u, 0x08081908u, 0x2b081908u, 0x08081908u, 0x2b190808u, 0x08081908u,
+ 0x08080808u, 0x08081919u, 0x0808082bu, 0x08081919u, 0x08081919u, 0x08081919u,
+ 0x08082b08u, 0x08081919u, 0x08190819u, 0x08081919u, 0x08191908u, 0x08081919u,
+ 0x082b0808u, 0x08081919u, 0x19080819u, 0x08081919u, 0x19081908u, 0x08081919u,
+ 0x19190808u, 0x08081919u, 0x192b0819u, 0x08081919u, 0x2b080808u, 0x08081919u,
+ 0x08080819u, 0x0808192bu, 0x08081908u, 0x0808192bu, 0x08190808u, 0x0808192bu,
+ 0x082b192bu, 0x0808192bu, 0x19080808u, 0x0808192bu, 0x1908082bu, 0x0808192bu,
+ 0x2b081908u, 0x0808192bu, 0x08080808u, 0x08082b08u, 0x0808082bu, 0x08082b08u,
+ 0x08081919u, 0x08082b08u, 0x08082b08u, 0x08082b08u, 0x08082b2bu, 0x08082b08u,
+ 0x08190819u, 0x08082b08u, 0x08191908u, 0x08082b08u, 0x082b0808u, 0x08082b08u,
+ 0x082b1919u, 0x08082b08u, 0x19080819u, 0x08082b08u, 0x19081908u, 0x08082b08u,
+ 0x19190808u, 0x08082b08u, 0x19192b08u, 0x08082b08u, 0x2b080808u, 0x08082b08u,
+ 0x2b2b0808u, 0x08082b08u, 0x2b2b2b2bu, 0x08082b08u, 0x08080819u, 0x08082b19u,
+ 0x08081908u, 0x08082b19u, 0x08190808u, 0x08082b19u, 0x19080808u, 0x08082b19u,
+ 0x2b080819u, 0x08082b19u, 0x2b082b19u, 0x08082b19u, 0x08080808u, 0x08082b2bu,
+ 0x082b0808u, 0x08082b2bu, 0x082b2b08u, 0x08082b2bu, 0x2b19192bu, 0x08082b2bu,
+ 0x2b2b0808u, 0x08082b2bu, 0x08080819u, 0x08190808u, 0x08081908u, 0x08190808u,
+ 0x0808192bu, 0x08190808u, 0x08082b19u, 0x08190808u, 0x08190808u, 0x08190808u,
+ 0x0819082bu, 0x08190808u, 0x08191919u, 0x08190808u, 0x08192b08u, 0x08190808u,
+ 0x082b0819u, 0x08190808u, 0x082b1908u, 0x08190808u, 0x19080808u, 0x08190808u,
+ 0x1908082bu, 0x08190808u, 0x19081919u, 0x08190808u, 0x19082b08u, 0x08190808u,
+ 0x19190819u, 0x08190808u, 0x19191908u, 0x08190808u, 0x192b0808u, 0x08190808u,
+ 0x192b2b2bu, 0x08190808u, 0x2b080819u, 0x08190808u, 0x2b081908u, 0x08190808u,
+ 0x2b190808u, 0x08190808u, 0x08080808u, 0x08190819u, 0x0808082bu, 0x08190819u,
+ 0x08081919u, 0x08190819u, 0x08082b08u, 0x08190819u, 0x08190819u, 0x08190819u,
+ 0x08191908u, 0x08190819u, 0x082b0808u, 0x08190819u, 0x19080819u, 0x08190819u,
+ 0x19081908u, 0x08190819u, 0x19190808u, 0x08190819u, 0x2b080808u, 0x08190819u,
+ 0x2b191908u, 0x08190819u, 0x2b19192bu, 0x08190819u, 0x08080819u, 0x0819082bu,
+ 0x08081908u, 0x0819082bu, 0x0808192bu, 0x0819082bu, 0x08190808u, 0x0819082bu,
+ 0x19080808u, 0x0819082bu, 0x192b0808u, 0x0819082bu, 0x08080808u, 0x08191908u,
+ 0x0808082bu, 0x08191908u, 0x08081919u, 0x08191908u, 0x08082b08u, 0x08191908u,
+ 0x08190819u, 0x08191908u, 0x08191908u, 0x08191908u, 0x082b0808u, 0x08191908u,
+ 0x19080819u, 0x08191908u, 0x19081908u, 0x08191908u, 0x19082b19u, 0x08191908u,
+ 0x19190808u, 0x08191908u, 0x192b1908u, 0x08191908u, 0x2b080808u, 0x08191908u,
+ 0x08080819u, 0x08191919u, 0x08081908u, 0x08191919u, 0x08190808u, 0x08191919u,
+ 0x19080808u, 0x08191919u, 0x08080808u, 0x0819192bu, 0x08191908u, 0x0819192bu,
+ 0x19082b19u, 0x0819192bu, 0x08080819u, 0x08192b08u, 0x08081908u, 0x08192b08u,
+ 0x08190808u, 0x08192b08u, 0x0819082bu, 0x08192b08u, 0x19080808u, 0x08192b08u,
+ 0x19191908u, 0x08192b08u, 0x2b08192bu, 0x08192b08u, 0x08080808u, 0x08192b19u,
+ 0x08081919u, 0x08192b19u, 0x192b192bu, 0x08192b19u, 0x19190819u, 0x08192b2bu,
+ 0x2b2b2b19u, 0x08192b2bu, 0x08080808u, 0x082b0808u, 0x0808082bu, 0x082b0808u,
+ 0x08081919u, 0x082b0808u, 0x08082b08u, 0x082b0808u, 0x08082b2bu, 0x082b0808u,
+ 0x08190819u, 0x082b0808u, 0x08191908u, 0x082b0808u, 0x082b0808u, 0x082b0808u,
+ 0x19080819u, 0x082b0808u, 0x19081908u, 0x082b0808u, 0x19190808u, 0x082b0808u,
+ 0x2b080808u, 0x082b0808u, 0x2b2b0808u, 0x082b0808u, 0x08080819u, 0x082b0819u,
+ 0x08081908u, 0x082b0819u, 0x08190808u, 0x082b0819u, 0x19080808u, 0x082b0819u,
+ 0x19082b08u, 0x082b0819u, 0x192b1919u, 0x082b0819u, 0x08080808u, 0x082b082bu,
+ 0x082b082bu, 0x082b082bu, 0x2b080808u, 0x082b082bu, 0x2b2b2b08u, 0x082b082bu,
+ 0x08080819u, 0x082b1908u, 0x08081908u, 0x082b1908u, 0x08190808u, 0x082b1908u,
+ 0x082b2b19u, 0x082b1908u, 0x19080808u, 0x082b1908u, 0x08080808u, 0x082b1919u,
+ 0x19080819u, 0x082b1919u, 0x1919082bu, 0x082b1919u, 0x2b192b19u, 0x082b1919u,
+ 0x08080819u, 0x082b192bu, 0x08192b2bu, 0x082b192bu, 0x2b2b192bu, 0x082b192bu,
+ 0x08080808u, 0x082b2b08u, 0x08082b08u, 0x082b2b08u, 0x08082b2bu, 0x082b2b08u,
+ 0x082b0808u, 0x082b2b08u, 0x19191919u, 0x082b2b08u, 0x2b082b08u, 0x082b2b08u,
+ 0x2b2b082bu, 0x082b2b08u, 0x192b2b08u, 0x082b2b19u, 0x2b190808u, 0x082b2b19u,
+ 0x08082b08u, 0x082b2b2bu, 0x082b0808u, 0x082b2b2bu, 0x2b08082bu, 0x082b2b2bu,
+ 0x2b082b08u, 0x082b2b2bu, 0x2b082b2bu, 0x082b2b2bu, 0x08080819u, 0x19080808u,
+ 0x08081908u, 0x19080808u, 0x0808192bu, 0x19080808u, 0x08082b19u, 0x19080808u,
+ 0x08190808u, 0x19080808u, 0x0819082bu, 0x19080808u, 0x08191919u, 0x19080808u,
+ 0x08192b08u, 0x19080808u, 0x082b0819u, 0x19080808u, 0x082b1908u, 0x19080808u,
+ 0x19080808u, 0x19080808u, 0x1908082bu, 0x19080808u, 0x19081919u, 0x19080808u,
+ 0x19082b08u, 0x19080808u, 0x19082b2bu, 0x19080808u, 0x19190819u, 0x19080808u,
+ 0x19191908u, 0x19080808u, 0x192b0808u, 0x19080808u, 0x192b1919u, 0x19080808u,
+ 0x2b080819u, 0x19080808u, 0x2b081908u, 0x19080808u, 0x2b190808u, 0x19080808u,
+ 0x08080808u, 0x19080819u, 0x0808082bu, 0x19080819u, 0x08081919u, 0x19080819u,
+ 0x08082b08u, 0x19080819u, 0x08190819u, 0x19080819u, 0x08191908u, 0x19080819u,
+ 0x082b0808u, 0x19080819u, 0x19080819u, 0x19080819u, 0x19081908u, 0x19080819u,
+ 0x19190808u, 0x19080819u, 0x2b080808u, 0x19080819u, 0x2b081919u, 0x19080819u,
+ 0x2b2b082bu, 0x19080819u, 0x08080819u, 0x1908082bu, 0x08081908u, 0x1908082bu,
+ 0x08190808u, 0x1908082bu, 0x0819082bu, 0x1908082bu, 0x082b2b19u, 0x1908082bu,
+ 0x19080808u, 0x1908082bu, 0x08080808u, 0x19081908u, 0x0808082bu, 0x19081908u,
+ 0x08081919u, 0x19081908u, 0x08082b08u, 0x19081908u, 0x08190819u, 0x19081908u,
+ 0x08191908u, 0x19081908u, 0x08192b19u, 0x19081908u, 0x082b0808u, 0x19081908u,
+ 0x19080819u, 0x19081908u, 0x19081908u, 0x19081908u, 0x19190808u, 0x19081908u,
+ 0x2b080808u, 0x19081908u, 0x2b191908u, 0x19081908u, 0x08080819u, 0x19081919u,
+ 0x08081908u, 0x19081919u, 0x08190808u, 0x19081919u, 0x082b1908u, 0x19081919u,
+ 0x19080808u, 0x19081919u, 0x2b192b2bu, 0x19081919u, 0x08080808u, 0x1908192bu,
+ 0x08082b2bu, 0x1908192bu, 0x19081908u, 0x1908192bu, 0x19190808u, 0x1908192bu,
+ 0x08080819u, 0x19082b08u, 0x08081908u, 0x19082b08u, 0x08190808u, 0x19082b08u,
+ 0x19080808u, 0x19082b08u, 0x19081919u, 0x19082b08u, 0x19191908u, 0x19082b08u,
+ 0x192b082bu, 0x19082b08u, 0x08080808u, 0x19082b19u, 0x08190819u, 0x19082b19u,
+ 0x19081908u, 0x19082b19u, 0x19190808u, 0x19082b19u, 0x192b2b19u, 0x19082b19u,
+ 0x08081908u, 0x19082b2bu, 0x08080808u, 0x19190808u, 0x0808082bu, 0x19190808u,
+ 0x08081919u, 0x19190808u, 0x08082b08u, 0x19190808u, 0x08190819u, 0x19190808u,
+ 0x08191908u, 0x19190808u, 0x082b0808u, 0x19190808u, 0x082b2b08u, 0x19190808u,
+ 0x19080819u, 0x19190808u, 0x19081908u, 0x19190808u, 0x19190808u, 0x19190808u,
+ 0x2b080808u, 0x19190808u, 0x08080819u, 0x19190819u, 0x08081908u, 0x19190819u,
+ 0x08190808u, 0x19190819u, 0x08191919u, 0x19190819u, 0x19080808u, 0x19190819u,
+ 0x1908082bu, 0x19190819u, 0x08080808u, 0x1919082bu, 0x19081908u, 0x1919082bu,
+ 0x2b2b2b2bu, 0x1919082bu, 0x08080819u, 0x19191908u, 0x08081908u, 0x19191908u,
+ 0x08190808u, 0x19191908u, 0x082b0819u, 0x19191908u, 0x19080808u, 0x19191908u,
+ 0x192b0808u, 0x19191908u, 0x2b080819u, 0x19191908u, 0x2b2b0819u, 0x19191908u,
+ 0x08080808u, 0x19191919u, 0x08082b08u, 0x19191919u, 0x2b080808u, 0x19191919u,
+ 0x2b082b08u, 0x19191919u, 0x082b0819u, 0x1919192bu, 0x192b2b08u, 0x1919192bu,
+ 0x2b2b0819u, 0x1919192bu, 0x08080808u, 0x19192b08u, 0x08191908u, 0x19192b08u,
+ 0x19080819u, 0x19192b08u, 0x19190808u, 0x19192b08u, 0x2b192b19u, 0x19192b08u,
+ 0x08192b2bu, 0x19192b19u, 0x19080808u, 0x19192b19u, 0x1908082bu, 0x19192b19u,
+ 0x2b081919u, 0x19192b2bu, 0x08080819u, 0x192b0808u, 0x08081908u, 0x192b0808u,
+ 0x08190808u, 0x192b0808u, 0x19080808u, 0x192b0808u, 0x19191908u, 0x192b0808u,
+ 0x192b082bu, 0x192b0808u, 0x2b08192bu, 0x192b0808u, 0x2b2b2b19u, 0x192b0808u,
+ 0x08080808u, 0x192b0819u, 0x082b1908u, 0x192b082bu, 0x19082b2bu, 0x192b082bu,
+ 0x2b19082bu, 0x192b082bu, 0x08080808u, 0x192b1908u, 0x0819192bu, 0x192b1908u,
+ 0x08190808u, 0x192b1919u, 0x19080808u, 0x192b1919u, 0x19081919u, 0x192b1919u,
+ 0x2b2b1908u, 0x192b1919u, 0x08080819u, 0x192b2b08u, 0x192b2b2bu, 0x192b2b08u,
+ 0x082b1919u, 0x192b2b19u, 0x0808192bu, 0x192b2b2bu, 0x19191908u, 0x192b2b2bu,
+ 0x192b082bu, 0x192b2b2bu, 0x08080808u, 0x2b080808u, 0x0808082bu, 0x2b080808u,
+ 0x08081919u, 0x2b080808u, 0x08082b08u, 0x2b080808u, 0x08190819u, 0x2b080808u,
+ 0x08191908u, 0x2b080808u, 0x082b0808u, 0x2b080808u, 0x082b2b2bu, 0x2b080808u,
+ 0x19080819u, 0x2b080808u, 0x19081908u, 0x2b080808u, 0x19190808u, 0x2b080808u,
+ 0x2b080808u, 0x2b080808u, 0x2b08082bu, 0x2b080808u, 0x2b2b2b08u, 0x2b080808u,
+ 0x2b2b2b2bu, 0x2b080808u, 0x08080819u, 0x2b080819u, 0x08081908u, 0x2b080819u,
+ 0x0808192bu, 0x2b080819u, 0x08190808u, 0x2b080819u, 0x19080808u, 0x2b080819u,
+ 0x19190819u, 0x2b080819u, 0x19192b19u, 0x2b080819u, 0x08080808u, 0x2b08082bu,
+ 0x082b0808u, 0x2b08082bu, 0x2b080808u, 0x2b08082bu, 0x2b08082bu, 0x2b08082bu,
+ 0x2b2b0808u, 0x2b08082bu, 0x2b2b2b08u, 0x2b08082bu, 0x08080819u, 0x2b081908u,
+ 0x08081908u, 0x2b081908u, 0x08190808u, 0x2b081908u, 0x0819082bu, 0x2b081908u,
+ 0x08191919u, 0x2b081908u, 0x19080808u, 0x2b081908u, 0x192b0808u, 0x2b081908u,
+ 0x2b082b19u, 0x2b081908u, 0x08080808u, 0x2b081919u, 0x19081908u, 0x2b081919u,
+ 0x2b2b1919u, 0x2b081919u, 0x08192b08u, 0x2b08192bu, 0x192b2b2bu, 0x2b08192bu,
+ 0x08080808u, 0x2b082b08u, 0x08082b08u, 0x2b082b08u, 0x082b1919u, 0x2b082b08u,
+ 0x19192b2bu, 0x2b082b08u, 0x2b080808u, 0x2b082b08u, 0x2b08082bu, 0x2b082b08u,
+ 0x2b2b2b08u, 0x2b082b08u, 0x0808192bu, 0x2b082b19u, 0x082b082bu, 0x2b082b2bu,
+ 0x2b080808u, 0x2b082b2bu, 0x2b082b08u, 0x2b082b2bu, 0x2b19192bu, 0x2b082b2bu,
+ 0x2b2b2b08u, 0x2b082b2bu, 0x08080819u, 0x2b190808u, 0x08081908u, 0x2b190808u,
+ 0x08190808u, 0x2b190808u, 0x19080808u, 0x2b190808u, 0x1919192bu, 0x2b190808u,
+ 0x2b081908u, 0x2b190808u, 0x08080808u, 0x2b190819u, 0x082b082bu, 0x2b190819u,
+ 0x192b1908u, 0x2b190819u, 0x1919192bu, 0x2b19082bu, 0x2b082b19u, 0x2b19082bu,
+ 0x08080808u, 0x2b191908u, 0x08081919u, 0x2b191908u, 0x19081908u, 0x2b191908u,
+ 0x19190808u, 0x2b191908u, 0x19192b08u, 0x2b191908u, 0x082b2b19u, 0x2b191919u,
+ 0x2b190808u, 0x2b191919u, 0x2b19082bu, 0x2b191919u, 0x19080819u, 0x2b19192bu,
+ 0x19190819u, 0x2b192b08u, 0x2b2b192bu, 0x2b192b08u, 0x19082b19u, 0x2b192b19u,
+ 0x08191919u, 0x2b192b2bu, 0x192b0808u, 0x2b192b2bu, 0x08080808u, 0x2b2b0808u,
+ 0x0808082bu, 0x2b2b0808u, 0x08082b08u, 0x2b2b0808u, 0x08082b2bu, 0x2b2b0808u,
+ 0x082b0808u, 0x2b2b0808u, 0x082b2b2bu, 0x2b2b0808u, 0x2b2b0808u, 0x2b2b0808u,
+ 0x19190819u, 0x2b2b0819u, 0x19192b19u, 0x2b2b0819u, 0x2b2b192bu, 0x2b2b0819u,
+ 0x08080808u, 0x2b2b082bu, 0x0808082bu, 0x2b2b082bu, 0x08082b08u, 0x2b2b082bu,
+ 0x082b2b2bu, 0x2b2b082bu, 0x2b080808u, 0x2b2b082bu, 0x2b2b0808u, 0x2b2b082bu,
+ 0x19080808u, 0x2b2b1908u, 0x2b191919u, 0x2b2b1908u, 0x192b1919u, 0x2b2b192bu,
+ 0x2b192b08u, 0x2b2b192bu, 0x08082b2bu, 0x2b2b2b08u, 0x082b0808u, 0x2b2b2b08u,
+ 0x082b082bu, 0x2b2b2b08u, 0x082b2b08u, 0x2b2b2b08u, 0x2b2b0808u, 0x2b2b2b08u,
+ 0x2b2b2b08u, 0x2b2b2b08u, 0x08081908u, 0x2b2b2b19u, 0x2b081908u, 0x2b2b2b19u,
+ 0x2b08192bu, 0x2b2b2b19u, 0x082b2b08u, 0x2b2b2b2bu, 0x082b2b2bu, 0x2b2b2b2bu,
+ 0x2b190819u, 0x2b2b2b2bu, 0x2b2b2b2bu, 0x2b2b2b2bu);
+
+const uint IQ2S_GRID[2048] = uint[2048](
+ 0x08080808u, 0x08080808u, 0x0808082bu, 0x08080808u, 0x08081919u, 0x08080808u,
+ 0x08082b08u, 0x08080808u, 0x08082b2bu, 0x08080808u, 0x08190819u, 0x08080808u,
+ 0x08191908u, 0x08080808u, 0x0819192bu, 0x08080808u, 0x08192b19u, 0x08080808u,
+ 0x082b0808u, 0x08080808u, 0x082b082bu, 0x08080808u, 0x082b1919u, 0x08080808u,
+ 0x082b2b08u, 0x08080808u, 0x19080819u, 0x08080808u, 0x19081908u, 0x08080808u,
+ 0x1908192bu, 0x08080808u, 0x19082b19u, 0x08080808u, 0x19190808u, 0x08080808u,
+ 0x1919082bu, 0x08080808u, 0x19191919u, 0x08080808u, 0x19192b08u, 0x08080808u,
+ 0x192b0819u, 0x08080808u, 0x192b1908u, 0x08080808u, 0x192b192bu, 0x08080808u,
+ 0x192b2b19u, 0x08080808u, 0x2b080808u, 0x08080808u, 0x2b08082bu, 0x08080808u,
+ 0x2b081919u, 0x08080808u, 0x2b082b08u, 0x08080808u, 0x2b190819u, 0x08080808u,
+ 0x2b191908u, 0x08080808u, 0x2b2b0808u, 0x08080808u, 0x2b2b1919u, 0x08080808u,
+ 0x2b2b2b2bu, 0x08080808u, 0x08080819u, 0x08080819u, 0x08081908u, 0x08080819u,
+ 0x0808192bu, 0x08080819u, 0x08082b19u, 0x08080819u, 0x08190808u, 0x08080819u,
+ 0x0819082bu, 0x08080819u, 0x08191919u, 0x08080819u, 0x08192b08u, 0x08080819u,
+ 0x082b0819u, 0x08080819u, 0x082b1908u, 0x08080819u, 0x19080808u, 0x08080819u,
+ 0x1908082bu, 0x08080819u, 0x19081919u, 0x08080819u, 0x19082b08u, 0x08080819u,
+ 0x19190819u, 0x08080819u, 0x19191908u, 0x08080819u, 0x1919192bu, 0x08080819u,
+ 0x19192b19u, 0x08080819u, 0x192b0808u, 0x08080819u, 0x192b1919u, 0x08080819u,
+ 0x192b2b08u, 0x08080819u, 0x2b080819u, 0x08080819u, 0x2b081908u, 0x08080819u,
+ 0x2b190808u, 0x08080819u, 0x2b19082bu, 0x08080819u, 0x2b191919u, 0x08080819u,
+ 0x2b2b0819u, 0x08080819u, 0x2b2b1908u, 0x08080819u, 0x08080808u, 0x0808082bu,
+ 0x0808082bu, 0x0808082bu, 0x08081919u, 0x0808082bu, 0x08082b08u, 0x0808082bu,
+ 0x08190819u, 0x0808082bu, 0x08191908u, 0x0808082bu, 0x082b0808u, 0x0808082bu,
+ 0x082b2b2bu, 0x0808082bu, 0x19080819u, 0x0808082bu, 0x19081908u, 0x0808082bu,
+ 0x1908192bu, 0x0808082bu, 0x19082b19u, 0x0808082bu, 0x19190808u, 0x0808082bu,
+ 0x19191919u, 0x0808082bu, 0x2b080808u, 0x0808082bu, 0x2b081919u, 0x0808082bu,
+ 0x2b082b2bu, 0x0808082bu, 0x2b191908u, 0x0808082bu, 0x2b2b082bu, 0x0808082bu,
+ 0x08080819u, 0x08081908u, 0x08081908u, 0x08081908u, 0x0808192bu, 0x08081908u,
+ 0x08082b19u, 0x08081908u, 0x08190808u, 0x08081908u, 0x0819082bu, 0x08081908u,
+ 0x08191919u, 0x08081908u, 0x08192b08u, 0x08081908u, 0x082b0819u, 0x08081908u,
+ 0x082b1908u, 0x08081908u, 0x082b192bu, 0x08081908u, 0x082b2b19u, 0x08081908u,
+ 0x19080808u, 0x08081908u, 0x1908082bu, 0x08081908u, 0x19081919u, 0x08081908u,
+ 0x19082b08u, 0x08081908u, 0x19082b2bu, 0x08081908u, 0x19190819u, 0x08081908u,
+ 0x19191908u, 0x08081908u, 0x1919192bu, 0x08081908u, 0x19192b19u, 0x08081908u,
+ 0x192b0808u, 0x08081908u, 0x192b082bu, 0x08081908u, 0x192b1919u, 0x08081908u,
+ 0x2b080819u, 0x08081908u, 0x2b081908u, 0x08081908u, 0x2b08192bu, 0x08081908u,
+ 0x2b082b19u, 0x08081908u, 0x2b190808u, 0x08081908u, 0x2b191919u, 0x08081908u,
+ 0x2b192b08u, 0x08081908u, 0x2b2b0819u, 0x08081908u, 0x2b2b1908u, 0x08081908u,
+ 0x08080808u, 0x08081919u, 0x0808082bu, 0x08081919u, 0x08081919u, 0x08081919u,
+ 0x08082b08u, 0x08081919u, 0x08082b2bu, 0x08081919u, 0x08190819u, 0x08081919u,
+ 0x08191908u, 0x08081919u, 0x0819192bu, 0x08081919u, 0x08192b19u, 0x08081919u,
+ 0x082b0808u, 0x08081919u, 0x082b1919u, 0x08081919u, 0x082b2b08u, 0x08081919u,
+ 0x19080819u, 0x08081919u, 0x19081908u, 0x08081919u, 0x1908192bu, 0x08081919u,
+ 0x19082b19u, 0x08081919u, 0x19190808u, 0x08081919u, 0x1919082bu, 0x08081919u,
+ 0x19191919u, 0x08081919u, 0x19192b08u, 0x08081919u, 0x192b0819u, 0x08081919u,
+ 0x192b1908u, 0x08081919u, 0x2b080808u, 0x08081919u, 0x2b08082bu, 0x08081919u,
+ 0x2b081919u, 0x08081919u, 0x2b082b08u, 0x08081919u, 0x2b190819u, 0x08081919u,
+ 0x2b191908u, 0x08081919u, 0x2b2b0808u, 0x08081919u, 0x08080819u, 0x0808192bu,
+ 0x08081908u, 0x0808192bu, 0x0808192bu, 0x0808192bu, 0x08082b19u, 0x0808192bu,
+ 0x08190808u, 0x0808192bu, 0x08191919u, 0x0808192bu, 0x19080808u, 0x0808192bu,
+ 0x19081919u, 0x0808192bu, 0x19082b08u, 0x0808192bu, 0x19190819u, 0x0808192bu,
+ 0x19191908u, 0x0808192bu, 0x192b0808u, 0x0808192bu, 0x2b080819u, 0x0808192bu,
+ 0x2b081908u, 0x0808192bu, 0x2b190808u, 0x0808192bu, 0x08080808u, 0x08082b08u,
+ 0x0808082bu, 0x08082b08u, 0x08081919u, 0x08082b08u, 0x08082b08u, 0x08082b08u,
+ 0x08190819u, 0x08082b08u, 0x08191908u, 0x08082b08u, 0x0819192bu, 0x08082b08u,
+ 0x08192b19u, 0x08082b08u, 0x082b0808u, 0x08082b08u, 0x082b1919u, 0x08082b08u,
+ 0x082b2b2bu, 0x08082b08u, 0x19080819u, 0x08082b08u, 0x19081908u, 0x08082b08u,
+ 0x1908192bu, 0x08082b08u, 0x19082b19u, 0x08082b08u, 0x19190808u, 0x08082b08u,
+ 0x1919082bu, 0x08082b08u, 0x19191919u, 0x08082b08u, 0x19192b08u, 0x08082b08u,
+ 0x192b0819u, 0x08082b08u, 0x192b1908u, 0x08082b08u, 0x2b080808u, 0x08082b08u,
+ 0x2b081919u, 0x08082b08u, 0x2b191908u, 0x08082b08u, 0x2b2b2b2bu, 0x08082b08u,
+ 0x08080819u, 0x08082b19u, 0x08081908u, 0x08082b19u, 0x08190808u, 0x08082b19u,
+ 0x0819082bu, 0x08082b19u, 0x08191919u, 0x08082b19u, 0x08192b08u, 0x08082b19u,
+ 0x082b0819u, 0x08082b19u, 0x19080808u, 0x08082b19u, 0x19081919u, 0x08082b19u,
+ 0x19082b08u, 0x08082b19u, 0x19190819u, 0x08082b19u, 0x19191908u, 0x08082b19u,
+ 0x192b0808u, 0x08082b19u, 0x2b080819u, 0x08082b19u, 0x2b190808u, 0x08082b19u,
+ 0x08080808u, 0x08082b2bu, 0x08190819u, 0x08082b2bu, 0x08191908u, 0x08082b2bu,
+ 0x082b082bu, 0x08082b2bu, 0x082b2b08u, 0x08082b2bu, 0x082b2b2bu, 0x08082b2bu,
+ 0x19190808u, 0x08082b2bu, 0x2b192b19u, 0x08082b2bu, 0x08080819u, 0x08190808u,
+ 0x08081908u, 0x08190808u, 0x0808192bu, 0x08190808u, 0x08082b19u, 0x08190808u,
+ 0x08190808u, 0x08190808u, 0x0819082bu, 0x08190808u, 0x08191919u, 0x08190808u,
+ 0x08192b08u, 0x08190808u, 0x082b0819u, 0x08190808u, 0x082b1908u, 0x08190808u,
+ 0x082b192bu, 0x08190808u, 0x19080808u, 0x08190808u, 0x1908082bu, 0x08190808u,
+ 0x19081919u, 0x08190808u, 0x19082b08u, 0x08190808u, 0x19190819u, 0x08190808u,
+ 0x19191908u, 0x08190808u, 0x1919192bu, 0x08190808u, 0x19192b19u, 0x08190808u,
+ 0x192b0808u, 0x08190808u, 0x192b082bu, 0x08190808u, 0x192b1919u, 0x08190808u,
+ 0x192b2b08u, 0x08190808u, 0x2b080819u, 0x08190808u, 0x2b081908u, 0x08190808u,
+ 0x2b08192bu, 0x08190808u, 0x2b190808u, 0x08190808u, 0x2b191919u, 0x08190808u,
+ 0x2b192b08u, 0x08190808u, 0x2b2b0819u, 0x08190808u, 0x2b2b1908u, 0x08190808u,
+ 0x08080808u, 0x08190819u, 0x0808082bu, 0x08190819u, 0x08081919u, 0x08190819u,
+ 0x08082b08u, 0x08190819u, 0x08082b2bu, 0x08190819u, 0x08190819u, 0x08190819u,
+ 0x08191908u, 0x08190819u, 0x0819192bu, 0x08190819u, 0x08192b19u, 0x08190819u,
+ 0x082b0808u, 0x08190819u, 0x082b082bu, 0x08190819u, 0x082b1919u, 0x08190819u,
+ 0x082b2b08u, 0x08190819u, 0x19080819u, 0x08190819u, 0x19081908u, 0x08190819u,
+ 0x1908192bu, 0x08190819u, 0x19082b19u, 0x08190819u, 0x19190808u, 0x08190819u,
+ 0x1919082bu, 0x08190819u, 0x19191919u, 0x08190819u, 0x19192b08u, 0x08190819u,
+ 0x192b0819u, 0x08190819u, 0x192b1908u, 0x08190819u, 0x2b080808u, 0x08190819u,
+ 0x2b08082bu, 0x08190819u, 0x2b081919u, 0x08190819u, 0x2b082b08u, 0x08190819u,
+ 0x2b190819u, 0x08190819u, 0x2b191908u, 0x08190819u, 0x08080819u, 0x0819082bu,
+ 0x08081908u, 0x0819082bu, 0x08082b19u, 0x0819082bu, 0x08190808u, 0x0819082bu,
+ 0x08191919u, 0x0819082bu, 0x082b0819u, 0x0819082bu, 0x082b1908u, 0x0819082bu,
+ 0x19080808u, 0x0819082bu, 0x19081919u, 0x0819082bu, 0x19190819u, 0x0819082bu,
+ 0x19191908u, 0x0819082bu, 0x2b080819u, 0x0819082bu, 0x2b081908u, 0x0819082bu,
+ 0x2b190808u, 0x0819082bu, 0x08080808u, 0x08191908u, 0x0808082bu, 0x08191908u,
+ 0x08081919u, 0x08191908u, 0x08082b08u, 0x08191908u, 0x08190819u, 0x08191908u,
+ 0x08191908u, 0x08191908u, 0x0819192bu, 0x08191908u, 0x08192b19u, 0x08191908u,
+ 0x082b0808u, 0x08191908u, 0x082b1919u, 0x08191908u, 0x082b2b08u, 0x08191908u,
+ 0x19080819u, 0x08191908u, 0x19081908u, 0x08191908u, 0x1908192bu, 0x08191908u,
+ 0x19082b19u, 0x08191908u, 0x19190808u, 0x08191908u, 0x1919082bu, 0x08191908u,
+ 0x19191919u, 0x08191908u, 0x19192b08u, 0x08191908u, 0x192b0819u, 0x08191908u,
+ 0x192b1908u, 0x08191908u, 0x2b080808u, 0x08191908u, 0x2b08082bu, 0x08191908u,
+ 0x2b081919u, 0x08191908u, 0x2b082b08u, 0x08191908u, 0x2b190819u, 0x08191908u,
+ 0x2b191908u, 0x08191908u, 0x2b2b0808u, 0x08191908u, 0x08080819u, 0x08191919u,
+ 0x08081908u, 0x08191919u, 0x0808192bu, 0x08191919u, 0x08082b19u, 0x08191919u,
+ 0x08190808u, 0x08191919u, 0x0819082bu, 0x08191919u, 0x08191919u, 0x08191919u,
+ 0x08192b08u, 0x08191919u, 0x082b0819u, 0x08191919u, 0x082b1908u, 0x08191919u,
+ 0x19080808u, 0x08191919u, 0x1908082bu, 0x08191919u, 0x19081919u, 0x08191919u,
+ 0x19082b08u, 0x08191919u, 0x19190819u, 0x08191919u, 0x19191908u, 0x08191919u,
+ 0x192b0808u, 0x08191919u, 0x2b080819u, 0x08191919u, 0x2b081908u, 0x08191919u,
+ 0x2b190808u, 0x08191919u, 0x08080808u, 0x0819192bu, 0x08081919u, 0x0819192bu,
+ 0x08082b08u, 0x0819192bu, 0x08190819u, 0x0819192bu, 0x08191908u, 0x0819192bu,
+ 0x082b0808u, 0x0819192bu, 0x19080819u, 0x0819192bu, 0x19081908u, 0x0819192bu,
+ 0x19190808u, 0x0819192bu, 0x2b080808u, 0x0819192bu, 0x2b2b2b2bu, 0x0819192bu,
+ 0x08080819u, 0x08192b08u, 0x08081908u, 0x08192b08u, 0x0808192bu, 0x08192b08u,
+ 0x08082b19u, 0x08192b08u, 0x08190808u, 0x08192b08u, 0x08191919u, 0x08192b08u,
+ 0x08192b08u, 0x08192b08u, 0x082b0819u, 0x08192b08u, 0x19080808u, 0x08192b08u,
+ 0x1908082bu, 0x08192b08u, 0x19081919u, 0x08192b08u, 0x19082b08u, 0x08192b08u,
+ 0x19190819u, 0x08192b08u, 0x19191908u, 0x08192b08u, 0x192b0808u, 0x08192b08u,
+ 0x2b080819u, 0x08192b08u, 0x2b081908u, 0x08192b08u, 0x08080808u, 0x08192b19u,
+ 0x0808082bu, 0x08192b19u, 0x08081919u, 0x08192b19u, 0x08082b08u, 0x08192b19u,
+ 0x08190819u, 0x08192b19u, 0x08191908u, 0x08192b19u, 0x082b0808u, 0x08192b19u,
+ 0x19080819u, 0x08192b19u, 0x19081908u, 0x08192b19u, 0x19190808u, 0x08192b19u,
+ 0x192b2b19u, 0x08192b19u, 0x2b2b082bu, 0x08192b19u, 0x08081908u, 0x08192b2bu,
+ 0x08190808u, 0x08192b2bu, 0x19080808u, 0x08192b2bu, 0x1919192bu, 0x08192b2bu,
+ 0x08080808u, 0x082b0808u, 0x0808082bu, 0x082b0808u, 0x08081919u, 0x082b0808u,
+ 0x08082b08u, 0x082b0808u, 0x08190819u, 0x082b0808u, 0x08191908u, 0x082b0808u,
+ 0x0819192bu, 0x082b0808u, 0x08192b19u, 0x082b0808u, 0x082b0808u, 0x082b0808u,
+ 0x082b1919u, 0x082b0808u, 0x082b2b2bu, 0x082b0808u, 0x19080819u, 0x082b0808u,
+ 0x19081908u, 0x082b0808u, 0x19190808u, 0x082b0808u, 0x1919082bu, 0x082b0808u,
+ 0x19191919u, 0x082b0808u, 0x192b1908u, 0x082b0808u, 0x2b080808u, 0x082b0808u,
+ 0x2b082b2bu, 0x082b0808u, 0x2b191908u, 0x082b0808u, 0x2b2b2b2bu, 0x082b0808u,
+ 0x08080819u, 0x082b0819u, 0x08081908u, 0x082b0819u, 0x08190808u, 0x082b0819u,
+ 0x0819082bu, 0x082b0819u, 0x08191919u, 0x082b0819u, 0x082b0819u, 0x082b0819u,
+ 0x19080808u, 0x082b0819u, 0x1908082bu, 0x082b0819u, 0x19081919u, 0x082b0819u,
+ 0x19190819u, 0x082b0819u, 0x19191908u, 0x082b0819u, 0x192b0808u, 0x082b0819u,
+ 0x2b080819u, 0x082b0819u, 0x2b081908u, 0x082b0819u, 0x2b190808u, 0x082b0819u,
+ 0x08080808u, 0x082b082bu, 0x08082b2bu, 0x082b082bu, 0x082b082bu, 0x082b082bu,
+ 0x082b2b08u, 0x082b082bu, 0x082b2b2bu, 0x082b082bu, 0x19081908u, 0x082b082bu,
+ 0x19190808u, 0x082b082bu, 0x2b082b08u, 0x082b082bu, 0x2b082b2bu, 0x082b082bu,
+ 0x2b2b2b08u, 0x082b082bu, 0x08080819u, 0x082b1908u, 0x08081908u, 0x082b1908u,
+ 0x0808192bu, 0x082b1908u, 0x08082b19u, 0x082b1908u, 0x08190808u, 0x082b1908u,
+ 0x08191919u, 0x082b1908u, 0x08192b08u, 0x082b1908u, 0x082b0819u, 0x082b1908u,
+ 0x082b1908u, 0x082b1908u, 0x19080808u, 0x082b1908u, 0x1908082bu, 0x082b1908u,
+ 0x19081919u, 0x082b1908u, 0x19082b08u, 0x082b1908u, 0x19190819u, 0x082b1908u,
+ 0x19191908u, 0x082b1908u, 0x192b0808u, 0x082b1908u, 0x2b080819u, 0x082b1908u,
+ 0x2b081908u, 0x082b1908u, 0x2b190808u, 0x082b1908u, 0x08080808u, 0x082b1919u,
+ 0x08081919u, 0x082b1919u, 0x08082b08u, 0x082b1919u, 0x08190819u, 0x082b1919u,
+ 0x08191908u, 0x082b1919u, 0x082b0808u, 0x082b1919u, 0x19080819u, 0x082b1919u,
+ 0x19081908u, 0x082b1919u, 0x19190808u, 0x082b1919u, 0x192b192bu, 0x082b1919u,
+ 0x2b080808u, 0x082b1919u, 0x08080819u, 0x082b192bu, 0x08081908u, 0x082b192bu,
+ 0x08190808u, 0x082b192bu, 0x19080808u, 0x082b192bu, 0x19192b19u, 0x082b192bu,
+ 0x08080808u, 0x082b2b08u, 0x08081919u, 0x082b2b08u, 0x08190819u, 0x082b2b08u,
+ 0x08191908u, 0x082b2b08u, 0x19080819u, 0x082b2b08u, 0x19081908u, 0x082b2b08u,
+ 0x19190808u, 0x082b2b08u, 0x2b082b2bu, 0x082b2b08u, 0x2b2b2b2bu, 0x082b2b08u,
+ 0x08080819u, 0x082b2b19u, 0x08081908u, 0x082b2b19u, 0x08190808u, 0x082b2b19u,
+ 0x2b191919u, 0x082b2b19u, 0x08082b2bu, 0x082b2b2bu, 0x082b082bu, 0x082b2b2bu,
+ 0x192b1908u, 0x082b2b2bu, 0x2b082b08u, 0x082b2b2bu, 0x2b082b2bu, 0x082b2b2bu,
+ 0x08080819u, 0x19080808u, 0x08081908u, 0x19080808u, 0x0808192bu, 0x19080808u,
+ 0x08082b19u, 0x19080808u, 0x08190808u, 0x19080808u, 0x0819082bu, 0x19080808u,
+ 0x08191919u, 0x19080808u, 0x08192b08u, 0x19080808u, 0x08192b2bu, 0x19080808u,
+ 0x082b0819u, 0x19080808u, 0x082b1908u, 0x19080808u, 0x082b192bu, 0x19080808u,
+ 0x19080808u, 0x19080808u, 0x1908082bu, 0x19080808u, 0x19081919u, 0x19080808u,
+ 0x19082b08u, 0x19080808u, 0x19082b2bu, 0x19080808u, 0x19190819u, 0x19080808u,
+ 0x19191908u, 0x19080808u, 0x1919192bu, 0x19080808u, 0x19192b19u, 0x19080808u,
+ 0x192b0808u, 0x19080808u, 0x192b082bu, 0x19080808u, 0x192b1919u, 0x19080808u,
+ 0x2b080819u, 0x19080808u, 0x2b081908u, 0x19080808u, 0x2b190808u, 0x19080808u,
+ 0x2b191919u, 0x19080808u, 0x2b192b08u, 0x19080808u, 0x2b2b0819u, 0x19080808u,
+ 0x2b2b1908u, 0x19080808u, 0x08080808u, 0x19080819u, 0x0808082bu, 0x19080819u,
+ 0x08081919u, 0x19080819u, 0x08082b08u, 0x19080819u, 0x08190819u, 0x19080819u,
+ 0x08191908u, 0x19080819u, 0x0819192bu, 0x19080819u, 0x08192b19u, 0x19080819u,
+ 0x082b0808u, 0x19080819u, 0x082b082bu, 0x19080819u, 0x082b1919u, 0x19080819u,
+ 0x19080819u, 0x19080819u, 0x19081908u, 0x19080819u, 0x1908192bu, 0x19080819u,
+ 0x19082b19u, 0x19080819u, 0x19190808u, 0x19080819u, 0x1919082bu, 0x19080819u,
+ 0x19191919u, 0x19080819u, 0x19192b08u, 0x19080819u, 0x192b0819u, 0x19080819u,
+ 0x192b1908u, 0x19080819u, 0x2b080808u, 0x19080819u, 0x2b08082bu, 0x19080819u,
+ 0x2b081919u, 0x19080819u, 0x2b082b08u, 0x19080819u, 0x2b190819u, 0x19080819u,
+ 0x2b191908u, 0x19080819u, 0x2b2b0808u, 0x19080819u, 0x08080819u, 0x1908082bu,
+ 0x08081908u, 0x1908082bu, 0x08190808u, 0x1908082bu, 0x0819082bu, 0x1908082bu,
+ 0x08191919u, 0x1908082bu, 0x08192b08u, 0x1908082bu, 0x082b1908u, 0x1908082bu,
+ 0x19080808u, 0x1908082bu, 0x19081919u, 0x1908082bu, 0x19082b08u, 0x1908082bu,
+ 0x19190819u, 0x1908082bu, 0x19191908u, 0x1908082bu, 0x192b0808u, 0x1908082bu,
+ 0x2b080819u, 0x1908082bu, 0x2b081908u, 0x1908082bu, 0x08080808u, 0x19081908u,
+ 0x0808082bu, 0x19081908u, 0x08081919u, 0x19081908u, 0x08082b08u, 0x19081908u,
+ 0x08082b2bu, 0x19081908u, 0x08190819u, 0x19081908u, 0x08191908u, 0x19081908u,
+ 0x0819192bu, 0x19081908u, 0x08192b19u, 0x19081908u, 0x082b0808u, 0x19081908u,
+ 0x082b082bu, 0x19081908u, 0x082b1919u, 0x19081908u, 0x082b2b08u, 0x19081908u,
+ 0x19080819u, 0x19081908u, 0x19081908u, 0x19081908u, 0x1908192bu, 0x19081908u,
+ 0x19082b19u, 0x19081908u, 0x19190808u, 0x19081908u, 0x1919082bu, 0x19081908u,
+ 0x19191919u, 0x19081908u, 0x19192b08u, 0x19081908u, 0x192b0819u, 0x19081908u,
+ 0x192b1908u, 0x19081908u, 0x2b080808u, 0x19081908u, 0x2b08082bu, 0x19081908u,
+ 0x2b081919u, 0x19081908u, 0x2b082b08u, 0x19081908u, 0x2b190819u, 0x19081908u,
+ 0x2b191908u, 0x19081908u, 0x2b2b0808u, 0x19081908u, 0x08080819u, 0x19081919u,
+ 0x08081908u, 0x19081919u, 0x0808192bu, 0x19081919u, 0x08082b19u, 0x19081919u,
+ 0x08190808u, 0x19081919u, 0x0819082bu, 0x19081919u, 0x08191919u, 0x19081919u,
+ 0x08192b08u, 0x19081919u, 0x082b0819u, 0x19081919u, 0x082b1908u, 0x19081919u,
+ 0x19080808u, 0x19081919u, 0x1908082bu, 0x19081919u, 0x19081919u, 0x19081919u,
+ 0x19082b08u, 0x19081919u, 0x19190819u, 0x19081919u, 0x19191908u, 0x19081919u,
+ 0x192b0808u, 0x19081919u, 0x192b2b2bu, 0x19081919u, 0x2b080819u, 0x19081919u,
+ 0x2b081908u, 0x19081919u, 0x2b190808u, 0x19081919u, 0x08080808u, 0x1908192bu,
+ 0x0808082bu, 0x1908192bu, 0x08081919u, 0x1908192bu, 0x08082b08u, 0x1908192bu,
+ 0x08190819u, 0x1908192bu, 0x08191908u, 0x1908192bu, 0x082b0808u, 0x1908192bu,
+ 0x19080819u, 0x1908192bu, 0x19081908u, 0x1908192bu, 0x19190808u, 0x1908192bu,
+ 0x2b080808u, 0x1908192bu, 0x2b2b1919u, 0x1908192bu, 0x08080819u, 0x19082b08u,
+ 0x08081908u, 0x19082b08u, 0x08082b19u, 0x19082b08u, 0x08190808u, 0x19082b08u,
+ 0x0819082bu, 0x19082b08u, 0x08191919u, 0x19082b08u, 0x08192b08u, 0x19082b08u,
+ 0x082b0819u, 0x19082b08u, 0x082b1908u, 0x19082b08u, 0x19080808u, 0x19082b08u,
+ 0x1908082bu, 0x19082b08u, 0x19081919u, 0x19082b08u, 0x19082b08u, 0x19082b08u,
+ 0x19190819u, 0x19082b08u, 0x19191908u, 0x19082b08u, 0x192b0808u, 0x19082b08u,
+ 0x2b081908u, 0x19082b08u, 0x2b190808u, 0x19082b08u, 0x08080808u, 0x19082b19u,
+ 0x0808082bu, 0x19082b19u, 0x08081919u, 0x19082b19u, 0x08082b08u, 0x19082b19u,
+ 0x08190819u, 0x19082b19u, 0x08191908u, 0x19082b19u, 0x082b0808u, 0x19082b19u,
+ 0x19080819u, 0x19082b19u, 0x19081908u, 0x19082b19u, 0x19190808u, 0x19082b19u,
+ 0x2b080808u, 0x19082b19u, 0x2b19192bu, 0x19082b19u, 0x08080819u, 0x19082b2bu,
+ 0x08081908u, 0x19082b2bu, 0x08190808u, 0x19082b2bu, 0x19080808u, 0x19082b2bu,
+ 0x08080808u, 0x19190808u, 0x0808082bu, 0x19190808u, 0x08081919u, 0x19190808u,
+ 0x08082b08u, 0x19190808u, 0x08190819u, 0x19190808u, 0x08191908u, 0x19190808u,
+ 0x0819192bu, 0x19190808u, 0x08192b19u, 0x19190808u, 0x082b0808u, 0x19190808u,
+ 0x082b082bu, 0x19190808u, 0x082b1919u, 0x19190808u, 0x082b2b08u, 0x19190808u,
+ 0x19080819u, 0x19190808u, 0x19081908u, 0x19190808u, 0x1908192bu, 0x19190808u,
+ 0x19082b19u, 0x19190808u, 0x19190808u, 0x19190808u, 0x1919082bu, 0x19190808u,
+ 0x19191919u, 0x19190808u, 0x19192b08u, 0x19190808u, 0x192b0819u, 0x19190808u,
+ 0x192b1908u, 0x19190808u, 0x2b080808u, 0x19190808u, 0x2b08082bu, 0x19190808u,
+ 0x2b081919u, 0x19190808u, 0x2b082b08u, 0x19190808u, 0x2b190819u, 0x19190808u,
+ 0x2b191908u, 0x19190808u, 0x08080819u, 0x19190819u, 0x08081908u, 0x19190819u,
+ 0x0808192bu, 0x19190819u, 0x08082b19u, 0x19190819u, 0x08190808u, 0x19190819u,
+ 0x0819082bu, 0x19190819u, 0x08191919u, 0x19190819u, 0x08192b08u, 0x19190819u,
+ 0x082b0819u, 0x19190819u, 0x082b1908u, 0x19190819u, 0x19080808u, 0x19190819u,
+ 0x1908082bu, 0x19190819u, 0x19081919u, 0x19190819u, 0x19082b08u, 0x19190819u,
+ 0x19190819u, 0x19190819u, 0x19191908u, 0x19190819u, 0x192b0808u, 0x19190819u,
+ 0x2b080819u, 0x19190819u, 0x2b081908u, 0x19190819u, 0x2b190808u, 0x19190819u,
+ 0x08080808u, 0x1919082bu, 0x08081919u, 0x1919082bu, 0x08082b08u, 0x1919082bu,
+ 0x08190819u, 0x1919082bu, 0x08191908u, 0x1919082bu, 0x082b0808u, 0x1919082bu,
+ 0x19080819u, 0x1919082bu, 0x19081908u, 0x1919082bu, 0x19190808u, 0x1919082bu,
+ 0x192b2b19u, 0x1919082bu, 0x2b080808u, 0x1919082bu, 0x08080819u, 0x19191908u,
+ 0x08081908u, 0x19191908u, 0x0808192bu, 0x19191908u, 0x08082b19u, 0x19191908u,
+ 0x08190808u, 0x19191908u, 0x0819082bu, 0x19191908u, 0x08191919u, 0x19191908u,
+ 0x08192b08u, 0x19191908u, 0x082b0819u, 0x19191908u, 0x082b1908u, 0x19191908u,
+ 0x19080808u, 0x19191908u, 0x1908082bu, 0x19191908u, 0x19081919u, 0x19191908u,
+ 0x19082b08u, 0x19191908u, 0x19190819u, 0x19191908u, 0x19191908u, 0x19191908u,
+ 0x192b0808u, 0x19191908u, 0x2b080819u, 0x19191908u, 0x2b081908u, 0x19191908u,
+ 0x2b190808u, 0x19191908u, 0x08080808u, 0x19191919u, 0x0808082bu, 0x19191919u,
+ 0x08081919u, 0x19191919u, 0x08082b08u, 0x19191919u, 0x08190819u, 0x19191919u,
+ 0x08191908u, 0x19191919u, 0x082b0808u, 0x19191919u, 0x19080819u, 0x19191919u,
+ 0x19081908u, 0x19191919u, 0x19190808u, 0x19191919u, 0x2b080808u, 0x19191919u,
+ 0x08080819u, 0x1919192bu, 0x08081908u, 0x1919192bu, 0x08190808u, 0x1919192bu,
+ 0x082b192bu, 0x1919192bu, 0x19080808u, 0x1919192bu, 0x08080808u, 0x19192b08u,
+ 0x0808082bu, 0x19192b08u, 0x08081919u, 0x19192b08u, 0x08082b08u, 0x19192b08u,
+ 0x08190819u, 0x19192b08u, 0x08191908u, 0x19192b08u, 0x082b0808u, 0x19192b08u,
+ 0x19080819u, 0x19192b08u, 0x19081908u, 0x19192b08u, 0x19190808u, 0x19192b08u,
+ 0x19192b2bu, 0x19192b08u, 0x2b080808u, 0x19192b08u, 0x08080819u, 0x19192b19u,
+ 0x08081908u, 0x19192b19u, 0x08190808u, 0x19192b19u, 0x19080808u, 0x19192b19u,
+ 0x08080808u, 0x19192b2bu, 0x08192b19u, 0x19192b2bu, 0x2b081919u, 0x19192b2bu,
+ 0x2b2b2b08u, 0x19192b2bu, 0x08080819u, 0x192b0808u, 0x08081908u, 0x192b0808u,
+ 0x0808192bu, 0x192b0808u, 0x08190808u, 0x192b0808u, 0x0819082bu, 0x192b0808u,
+ 0x08191919u, 0x192b0808u, 0x08192b08u, 0x192b0808u, 0x082b0819u, 0x192b0808u,
+ 0x082b1908u, 0x192b0808u, 0x19080808u, 0x192b0808u, 0x19081919u, 0x192b0808u,
+ 0x19082b08u, 0x192b0808u, 0x19190819u, 0x192b0808u, 0x19191908u, 0x192b0808u,
+ 0x192b0808u, 0x192b0808u, 0x2b081908u, 0x192b0808u, 0x2b190808u, 0x192b0808u,
+ 0x08080808u, 0x192b0819u, 0x0808082bu, 0x192b0819u, 0x08081919u, 0x192b0819u,
+ 0x08082b08u, 0x192b0819u, 0x08190819u, 0x192b0819u, 0x08191908u, 0x192b0819u,
+ 0x082b0808u, 0x192b0819u, 0x19080819u, 0x192b0819u, 0x19081908u, 0x192b0819u,
+ 0x19190808u, 0x192b0819u, 0x2b080808u, 0x192b0819u, 0x2b192b19u, 0x192b0819u,
+ 0x08081908u, 0x192b082bu, 0x08190808u, 0x192b082bu, 0x19080808u, 0x192b082bu,
+ 0x1919192bu, 0x192b082bu, 0x2b2b0819u, 0x192b082bu, 0x08080808u, 0x192b1908u,
+ 0x08081919u, 0x192b1908u, 0x08082b08u, 0x192b1908u, 0x08190819u, 0x192b1908u,
+ 0x08191908u, 0x192b1908u, 0x082b0808u, 0x192b1908u, 0x19080819u, 0x192b1908u,
+ 0x19081908u, 0x192b1908u, 0x19190808u, 0x192b1908u, 0x2b080808u, 0x192b1908u,
+ 0x08080819u, 0x192b1919u, 0x08081908u, 0x192b1919u, 0x08190808u, 0x192b1919u,
+ 0x19080808u, 0x192b1919u, 0x19082b2bu, 0x192b1919u, 0x192b2b08u, 0x192b1919u,
+ 0x2b19082bu, 0x192b1919u, 0x08080808u, 0x192b192bu, 0x2b191908u, 0x192b192bu,
+ 0x08080819u, 0x192b2b08u, 0x08081908u, 0x192b2b08u, 0x08190808u, 0x192b2b08u,
+ 0x192b1919u, 0x192b2b08u, 0x2b192b08u, 0x192b2b08u, 0x08080808u, 0x192b2b19u,
+ 0x082b2b2bu, 0x192b2b19u, 0x1908082bu, 0x192b2b2bu, 0x2b2b0819u, 0x192b2b2bu,
+ 0x08080808u, 0x2b080808u, 0x0808082bu, 0x2b080808u, 0x08081919u, 0x2b080808u,
+ 0x08082b08u, 0x2b080808u, 0x08190819u, 0x2b080808u, 0x08191908u, 0x2b080808u,
+ 0x08192b19u, 0x2b080808u, 0x082b0808u, 0x2b080808u, 0x082b1919u, 0x2b080808u,
+ 0x19080819u, 0x2b080808u, 0x19081908u, 0x2b080808u, 0x19190808u, 0x2b080808u,
+ 0x1919082bu, 0x2b080808u, 0x19191919u, 0x2b080808u, 0x19192b08u, 0x2b080808u,
+ 0x192b0819u, 0x2b080808u, 0x2b080808u, 0x2b080808u, 0x2b081919u, 0x2b080808u,
+ 0x2b190819u, 0x2b080808u, 0x2b191908u, 0x2b080808u, 0x08080819u, 0x2b080819u,
+ 0x08081908u, 0x2b080819u, 0x08082b19u, 0x2b080819u, 0x08190808u, 0x2b080819u,
+ 0x0819082bu, 0x2b080819u, 0x08191919u, 0x2b080819u, 0x08192b08u, 0x2b080819u,
+ 0x082b0819u, 0x2b080819u, 0x082b1908u, 0x2b080819u, 0x19080808u, 0x2b080819u,
+ 0x1908082bu, 0x2b080819u, 0x19081919u, 0x2b080819u, 0x19082b08u, 0x2b080819u,
+ 0x19190819u, 0x2b080819u, 0x19191908u, 0x2b080819u, 0x2b080819u, 0x2b080819u,
+ 0x2b081908u, 0x2b080819u, 0x2b190808u, 0x2b080819u, 0x2b2b2b19u, 0x2b080819u,
+ 0x08080808u, 0x2b08082bu, 0x08081919u, 0x2b08082bu, 0x08082b2bu, 0x2b08082bu,
+ 0x08190819u, 0x2b08082bu, 0x08191908u, 0x2b08082bu, 0x19080819u, 0x2b08082bu,
+ 0x19081908u, 0x2b08082bu, 0x19190808u, 0x2b08082bu, 0x08080819u, 0x2b081908u,
+ 0x08081908u, 0x2b081908u, 0x0808192bu, 0x2b081908u, 0x08082b19u, 0x2b081908u,
+ 0x08190808u, 0x2b081908u, 0x0819082bu, 0x2b081908u, 0x08191919u, 0x2b081908u,
+ 0x08192b08u, 0x2b081908u, 0x082b0819u, 0x2b081908u, 0x19080808u, 0x2b081908u,
+ 0x1908082bu, 0x2b081908u, 0x19081919u, 0x2b081908u, 0x19082b08u, 0x2b081908u,
+ 0x19190819u, 0x2b081908u, 0x19191908u, 0x2b081908u, 0x192b0808u, 0x2b081908u,
+ 0x2b080819u, 0x2b081908u, 0x2b081908u, 0x2b081908u, 0x2b190808u, 0x2b081908u,
+ 0x08080808u, 0x2b081919u, 0x0808082bu, 0x2b081919u, 0x08081919u, 0x2b081919u,
+ 0x08082b08u, 0x2b081919u, 0x08190819u, 0x2b081919u, 0x08191908u, 0x2b081919u,
+ 0x082b0808u, 0x2b081919u, 0x19080819u, 0x2b081919u, 0x19081908u, 0x2b081919u,
+ 0x19190808u, 0x2b081919u, 0x2b080808u, 0x2b081919u, 0x2b082b2bu, 0x2b081919u,
+ 0x08080819u, 0x2b08192bu, 0x08081908u, 0x2b08192bu, 0x08190808u, 0x2b08192bu,
+ 0x082b2b19u, 0x2b08192bu, 0x19080808u, 0x2b08192bu, 0x08080808u, 0x2b082b08u,
+ 0x08081919u, 0x2b082b08u, 0x08190819u, 0x2b082b08u, 0x08191908u, 0x2b082b08u,
+ 0x19080819u, 0x2b082b08u, 0x19081908u, 0x2b082b08u, 0x19190808u, 0x2b082b08u,
+ 0x2b2b082bu, 0x2b082b08u, 0x08080819u, 0x2b082b19u, 0x08081908u, 0x2b082b19u,
+ 0x19080808u, 0x2b082b19u, 0x192b1919u, 0x2b082b19u, 0x082b082bu, 0x2b082b2bu,
+ 0x19192b08u, 0x2b082b2bu, 0x19192b2bu, 0x2b082b2bu, 0x2b08082bu, 0x2b082b2bu,
+ 0x2b2b082bu, 0x2b082b2bu, 0x08080819u, 0x2b190808u, 0x08081908u, 0x2b190808u,
+ 0x08082b19u, 0x2b190808u, 0x08190808u, 0x2b190808u, 0x0819082bu, 0x2b190808u,
+ 0x08191919u, 0x2b190808u, 0x08192b08u, 0x2b190808u, 0x082b1908u, 0x2b190808u,
+ 0x19080808u, 0x2b190808u, 0x1908082bu, 0x2b190808u, 0x19081919u, 0x2b190808u,
+ 0x19082b08u, 0x2b190808u, 0x19190819u, 0x2b190808u, 0x19191908u, 0x2b190808u,
+ 0x192b0808u, 0x2b190808u, 0x2b080819u, 0x2b190808u, 0x2b081908u, 0x2b190808u,
+ 0x2b190808u, 0x2b190808u, 0x08080808u, 0x2b190819u, 0x08081919u, 0x2b190819u,
+ 0x08190819u, 0x2b190819u, 0x08191908u, 0x2b190819u, 0x19080819u, 0x2b190819u,
+ 0x19081908u, 0x2b190819u, 0x19190808u, 0x2b190819u, 0x19192b2bu, 0x2b190819u,
+ 0x08080819u, 0x2b19082bu, 0x08081908u, 0x2b19082bu, 0x08190808u, 0x2b19082bu,
+ 0x19080808u, 0x2b19082bu, 0x2b2b192bu, 0x2b19082bu, 0x08080808u, 0x2b191908u,
+ 0x0808082bu, 0x2b191908u, 0x08081919u, 0x2b191908u, 0x08082b08u, 0x2b191908u,
+ 0x08190819u, 0x2b191908u, 0x08191908u, 0x2b191908u, 0x082b0808u, 0x2b191908u,
+ 0x19080819u, 0x2b191908u, 0x19081908u, 0x2b191908u, 0x19190808u, 0x2b191908u,
+ 0x2b080808u, 0x2b191908u, 0x2b19192bu, 0x2b191908u, 0x08080819u, 0x2b191919u,
+ 0x08081908u, 0x2b191919u, 0x08190808u, 0x2b191919u, 0x19080808u, 0x2b191919u,
+ 0x2b192b08u, 0x2b191919u, 0x2b2b0819u, 0x2b191919u, 0x08080808u, 0x2b19192bu,
+ 0x1908192bu, 0x2b19192bu, 0x192b1908u, 0x2b19192bu, 0x08080819u, 0x2b192b08u,
+ 0x08081908u, 0x2b192b08u, 0x08190808u, 0x2b192b08u, 0x082b192bu, 0x2b192b08u,
+ 0x19080808u, 0x2b192b08u, 0x2b2b2b19u, 0x2b192b08u, 0x08080808u, 0x2b192b19u,
+ 0x19082b19u, 0x2b192b19u, 0x1919082bu, 0x2b192b19u, 0x2b190808u, 0x2b192b2bu,
+ 0x08080808u, 0x2b2b0808u, 0x08081919u, 0x2b2b0808u, 0x08082b2bu, 0x2b2b0808u,
+ 0x08191908u, 0x2b2b0808u, 0x082b082bu, 0x2b2b0808u, 0x082b2b2bu, 0x2b2b0808u,
+ 0x19080819u, 0x2b2b0808u, 0x19081908u, 0x2b2b0808u, 0x19190808u, 0x2b2b0808u,
+ 0x2b2b082bu, 0x2b2b0808u, 0x2b2b2b2bu, 0x2b2b0808u, 0x19080808u, 0x2b2b0819u,
+ 0x192b1919u, 0x2b2b0819u, 0x0808082bu, 0x2b2b082bu, 0x08082b2bu, 0x2b2b082bu,
+ 0x082b082bu, 0x2b2b082bu, 0x082b2b08u, 0x2b2b082bu, 0x082b2b2bu, 0x2b2b082bu,
+ 0x2b08082bu, 0x2b2b082bu, 0x2b082b08u, 0x2b2b082bu, 0x2b082b2bu, 0x2b2b082bu,
+ 0x2b2b2b08u, 0x2b2b082bu, 0x08080819u, 0x2b2b1908u, 0x08081908u, 0x2b2b1908u,
+ 0x08190808u, 0x2b2b1908u, 0x19080808u, 0x2b2b1908u, 0x2b082b19u, 0x2b2b1908u,
+ 0x2b2b1908u, 0x2b2b1908u, 0x08080808u, 0x2b2b1919u, 0x08192b19u, 0x2b2b1919u,
+ 0x19190819u, 0x2b2b192bu, 0x08082b2bu, 0x2b2b2b08u, 0x082b2b08u, 0x2b2b2b08u,
+ 0x2b2b082bu, 0x2b2b2b08u, 0x19191908u, 0x2b2b2b19u, 0x2b08192bu, 0x2b2b2b19u,
+ 0x08082b08u, 0x2b2b2b2bu, 0x08082b2bu, 0x2b2b2b2bu, 0x082b0808u, 0x2b2b2b2bu,
+ 0x082b082bu, 0x2b2b2b2bu, 0x082b2b08u, 0x2b2b2b2bu, 0x2b082b08u, 0x2b2b2b2bu,
+ 0x2b2b2b2bu, 0x2b2b2b2bu);
+
+const uint IQ3XXS_GRID[256] = uint[256](
+ 0x04040404u, 0x04040414u, 0x04040424u, 0x04040c0cu, 0x04040c1cu, 0x04040c3eu,
+ 0x04041404u, 0x04041414u, 0x04041c0cu, 0x04042414u, 0x04043e1cu, 0x04043e2cu,
+ 0x040c040cu, 0x040c041cu, 0x040c0c04u, 0x040c0c14u, 0x040c140cu, 0x040c142cu,
+ 0x040c1c04u, 0x040c1c14u, 0x040c240cu, 0x040c2c24u, 0x040c3e04u, 0x04140404u,
+ 0x04140414u, 0x04140424u, 0x04140c0cu, 0x04141404u, 0x04141414u, 0x04141c0cu,
+ 0x04141c1cu, 0x04141c3eu, 0x04142c0cu, 0x04142c3eu, 0x04143e2cu, 0x041c040cu,
+ 0x041c043eu, 0x041c0c04u, 0x041c0c14u, 0x041c142cu, 0x041c3e04u, 0x04240c1cu,
+ 0x04241c3eu, 0x04242424u, 0x04242c3eu, 0x04243e1cu, 0x04243e2cu, 0x042c040cu,
+ 0x042c043eu, 0x042c1c14u, 0x042c2c14u, 0x04341c2cu, 0x04343424u, 0x043e0c04u,
+ 0x043e0c24u, 0x043e0c34u, 0x043e241cu, 0x043e340cu, 0x0c04040cu, 0x0c04041cu,
+ 0x0c040c04u, 0x0c040c14u, 0x0c04140cu, 0x0c04141cu, 0x0c041c04u, 0x0c041c14u,
+ 0x0c041c24u, 0x0c04243eu, 0x0c042c04u, 0x0c0c0404u, 0x0c0c0414u, 0x0c0c0c0cu,
+ 0x0c0c1404u, 0x0c0c1414u, 0x0c14040cu, 0x0c14041cu, 0x0c140c04u, 0x0c140c14u,
+ 0x0c14140cu, 0x0c141c04u, 0x0c143e14u, 0x0c1c0404u, 0x0c1c0414u, 0x0c1c1404u,
+ 0x0c1c1c0cu, 0x0c1c2434u, 0x0c1c3434u, 0x0c24040cu, 0x0c24042cu, 0x0c242c04u,
+ 0x0c2c1404u, 0x0c2c1424u, 0x0c2c2434u, 0x0c2c3e0cu, 0x0c34042cu, 0x0c3e1414u,
+ 0x0c3e2404u, 0x14040404u, 0x14040414u, 0x14040c0cu, 0x14040c1cu, 0x14041404u,
+ 0x14041414u, 0x14041434u, 0x14041c0cu, 0x14042414u, 0x140c040cu, 0x140c041cu,
+ 0x140c042cu, 0x140c0c04u, 0x140c0c14u, 0x140c140cu, 0x140c1c04u, 0x140c341cu,
+ 0x140c343eu, 0x140c3e04u, 0x14140404u, 0x14140414u, 0x14140c0cu, 0x14140c3eu,
+ 0x14141404u, 0x14141414u, 0x14141c3eu, 0x14142404u, 0x14142c2cu, 0x141c040cu,
+ 0x141c0c04u, 0x141c0c24u, 0x141c3e04u, 0x141c3e24u, 0x14241c2cu, 0x14242c1cu,
+ 0x142c041cu, 0x142c143eu, 0x142c240cu, 0x142c3e24u, 0x143e040cu, 0x143e041cu,
+ 0x143e0c34u, 0x143e242cu, 0x1c04040cu, 0x1c040c04u, 0x1c040c14u, 0x1c04140cu,
+ 0x1c04141cu, 0x1c042c04u, 0x1c04342cu, 0x1c043e14u, 0x1c0c0404u, 0x1c0c0414u,
+ 0x1c0c1404u, 0x1c0c1c0cu, 0x1c0c2424u, 0x1c0c2434u, 0x1c14040cu, 0x1c14041cu,
+ 0x1c140c04u, 0x1c14142cu, 0x1c142c14u, 0x1c143e14u, 0x1c1c0c0cu, 0x1c1c1c1cu,
+ 0x1c241c04u, 0x1c24243eu, 0x1c243e14u, 0x1c2c0404u, 0x1c2c0434u, 0x1c2c1414u,
+ 0x1c2c2c2cu, 0x1c340c24u, 0x1c341c34u, 0x1c34341cu, 0x1c3e1c1cu, 0x1c3e3404u,
+ 0x24040424u, 0x24040c3eu, 0x24041c2cu, 0x24041c3eu, 0x24042c1cu, 0x24042c3eu,
+ 0x240c3e24u, 0x24141404u, 0x24141c3eu, 0x24142404u, 0x24143404u, 0x24143434u,
+ 0x241c043eu, 0x241c242cu, 0x24240424u, 0x24242c0cu, 0x24243424u, 0x242c142cu,
+ 0x242c241cu, 0x242c3e04u, 0x243e042cu, 0x243e0c04u, 0x243e0c14u, 0x243e1c04u,
+ 0x2c040c14u, 0x2c04240cu, 0x2c043e04u, 0x2c0c0404u, 0x2c0c0434u, 0x2c0c1434u,
+ 0x2c0c2c2cu, 0x2c140c24u, 0x2c141c14u, 0x2c143e14u, 0x2c1c0414u, 0x2c1c2c1cu,
+ 0x2c240c04u, 0x2c24141cu, 0x2c24143eu, 0x2c243e14u, 0x2c2c0414u, 0x2c2c1c0cu,
+ 0x2c342c04u, 0x2c3e1424u, 0x2c3e2414u, 0x34041424u, 0x34042424u, 0x34042434u,
+ 0x34043424u, 0x340c140cu, 0x340c340cu, 0x34140c3eu, 0x34143424u, 0x341c1c04u,
+ 0x341c1c34u, 0x34242424u, 0x342c042cu, 0x342c2c14u, 0x34341c1cu, 0x343e041cu,
+ 0x343e140cu, 0x3e04041cu, 0x3e04042cu, 0x3e04043eu, 0x3e040c04u, 0x3e041c14u,
+ 0x3e042c14u, 0x3e0c1434u, 0x3e0c2404u, 0x3e140c14u, 0x3e14242cu, 0x3e142c14u,
+ 0x3e1c0404u, 0x3e1c0c2cu, 0x3e1c1c1cu, 0x3e1c3404u, 0x3e24140cu, 0x3e24240cu,
+ 0x3e2c0404u, 0x3e2c0414u, 0x3e2c1424u, 0x3e341c04u);
+
+const uint IQ1S_GRID[1024] = uint[1024](
+ 0x00020000u, 0x00080005u, 0x0011000au, 0x00200015u, 0x00280022u, 0x0045002au,
+ 0x00540051u, 0x00650056u, 0x00820080u, 0x008a0088u, 0x00a00095u, 0x00a800a2u,
+ 0x010400aau, 0x01110105u, 0x01160114u, 0x011a0119u, 0x01410125u, 0x01490146u,
+ 0x01550152u, 0x0161015au, 0x01660164u, 0x01850168u, 0x01940191u, 0x01a50196u,
+ 0x02020200u, 0x020a0208u, 0x02200215u, 0x02280222u, 0x0245022au, 0x02590251u,
+ 0x02690264u, 0x02820280u, 0x028a0288u, 0x02950291u, 0x02a00299u, 0x02a802a2u,
+ 0x041102aau, 0x04160414u, 0x04410425u, 0x04550449u, 0x0464045au, 0x04910465u,
+ 0x04a50499u, 0x05040501u, 0x05060505u, 0x05180515u, 0x0529051au, 0x05450540u,
+ 0x0550054au, 0x05540551u, 0x05560555u, 0x05600559u, 0x05650562u, 0x056a0568u,
+ 0x05910581u, 0x05980595u, 0x05a1059au, 0x05a505a4u, 0x05a905a6u, 0x06190614u,
+ 0x06440641u, 0x06520650u, 0x06580655u, 0x06610660u, 0x06690666u, 0x06910685u,
+ 0x06990694u, 0x08020800u, 0x080a0808u, 0x08200815u, 0x08280822u, 0x0845082au,
+ 0x08560851u, 0x08800865u, 0x08880882u, 0x0895088au, 0x08a208a0u, 0x08aa08a8u,
+ 0x09110905u, 0x09190914u, 0x09250924u, 0x09500941u, 0x09550951u, 0x09640961u,
+ 0x09910969u, 0x09960994u, 0x09a50999u, 0x0a020a00u, 0x0a0a0a08u, 0x0a200a15u,
+ 0x0a280a22u, 0x0a450a2au, 0x0a590a51u, 0x0a650a61u, 0x0a820a80u, 0x0a880a85u,
+ 0x0a950a8au, 0x0aa20aa0u, 0x0aaa0aa8u, 0x10111010u, 0x10191014u, 0x10251024u,
+ 0x10441041u, 0x10551050u, 0x10611058u, 0x10651064u, 0x10911069u, 0x10961094u,
+ 0x10a510a1u, 0x11041101u, 0x11091106u, 0x11121110u, 0x11181115u, 0x11241121u,
+ 0x11451129u, 0x1150114au, 0x11521151u, 0x11551154u, 0x11591156u, 0x11651160u,
+ 0x11921184u, 0x11a11195u, 0x121111a4u, 0x12161214u, 0x12401225u, 0x12491246u,
+ 0x12551252u, 0x125a1258u, 0x12661264u, 0x12911285u, 0x12961294u, 0x140112a5u,
+ 0x14091406u, 0x14151414u, 0x14191418u, 0x14261421u, 0x14451441u, 0x14481446u,
+ 0x1451144au, 0x14551454u, 0x14591456u, 0x14651462u, 0x14841468u, 0x14901489u,
+ 0x14951494u, 0x14991498u, 0x14a1149au, 0x14a514a4u, 0x150214a9u, 0x150a1505u,
+ 0x15141511u, 0x15161515u, 0x15201519u, 0x15251522u, 0x152a1528u, 0x15441541u,
+ 0x15461545u, 0x15521551u, 0x15551554u, 0x15591556u, 0x1561155au, 0x15651564u,
+ 0x15691566u, 0x15821580u, 0x15851584u, 0x158a1588u, 0x15911590u, 0x15951594u,
+ 0x15991596u, 0x15a0159au, 0x15a515a2u, 0x16041601u, 0x16061605u, 0x16161615u,
+ 0x161a1618u, 0x16261621u, 0x16421640u, 0x16451644u, 0x164a1648u, 0x16551651u,
+ 0x16581656u, 0x16611659u, 0x16651664u, 0x16691668u, 0x1686166au, 0x1692168au,
+ 0x16a41695u, 0x181116a9u, 0x18251816u, 0x18441841u, 0x18491846u, 0x18551850u,
+ 0x185a1858u, 0x18611860u, 0x18661864u, 0x18851869u, 0x18941891u, 0x191018a5u,
+ 0x19151912u, 0x1921191au, 0x19421925u, 0x19451944u, 0x19511948u, 0x19551954u,
+ 0x19591956u, 0x1960195au, 0x196a1965u, 0x19911989u, 0x19951992u, 0x19a11998u,
+ 0x19a919a6u, 0x1a161a09u, 0x1a261a24u, 0x1a461a44u, 0x1a501a49u, 0x1a551a52u,
+ 0x1a611a58u, 0x1a691a66u, 0x1a911a85u, 0x1a9a1a96u, 0x20022000u, 0x200a2008u,
+ 0x20202015u, 0x20252022u, 0x202a2028u, 0x20512045u, 0x20612059u, 0x20802065u,
+ 0x20882082u, 0x2095208au, 0x20a220a0u, 0x20a820a5u, 0x210520aau, 0x21142111u,
+ 0x21252119u, 0x21442142u, 0x21552149u, 0x215a2158u, 0x21642161u, 0x21662165u,
+ 0x21902185u, 0x21992196u, 0x220121a5u, 0x220a2208u, 0x22152211u, 0x22222220u,
+ 0x222a2228u, 0x22512245u, 0x22592256u, 0x22812265u, 0x228a2288u, 0x22952291u,
+ 0x22a222a0u, 0x22aa22a8u, 0x24142405u, 0x24192416u, 0x24442425u, 0x24462445u,
+ 0x24522449u, 0x24582455u, 0x2466245au, 0x24912485u, 0x24992494u, 0x24a524a1u,
+ 0x25152509u, 0x25292521u, 0x25452540u, 0x25512548u, 0x25552554u, 0x25622559u,
+ 0x25682565u, 0x25902589u, 0x25952594u, 0x259a2598u, 0x25a425a1u, 0x25a925a6u,
+ 0x26102605u, 0x26192612u, 0x26412625u, 0x26552649u, 0x26612660u, 0x26842669u,
+ 0x26902686u, 0x2800269au, 0x28082802u, 0x2815280au, 0x28222820u, 0x282a2828u,
+ 0x28512845u, 0x28652854u, 0x28822880u, 0x288a2888u, 0x28a228a0u, 0x28aa28a8u,
+ 0x29112909u, 0x29192914u, 0x29462925u, 0x29522949u, 0x29612955u, 0x29662964u,
+ 0x29852969u, 0x29962990u, 0x29a42999u, 0x2a0029a5u, 0x2a082a02u, 0x2a202a0au,
+ 0x2a282a22u, 0x2a452a2au, 0x2a562a51u, 0x2a652a59u, 0x2a822a80u, 0x2a8a2a88u,
+ 0x2aa02a95u, 0x2aa82aa2u, 0x40052aaau, 0x40164011u, 0x40494025u, 0x40554052u,
+ 0x405a4058u, 0x40644061u, 0x40944066u, 0x40a14099u, 0x410040a6u, 0x41044101u,
+ 0x41094106u, 0x41154112u, 0x41184116u, 0x4121411au, 0x41294126u, 0x41484145u,
+ 0x4151414au, 0x41554154u, 0x41594156u, 0x4165415au, 0x416a4168u, 0x41844181u,
+ 0x41904186u, 0x41954192u, 0x41a141a0u, 0x420541a2u, 0x42144211u, 0x42254216u,
+ 0x42524241u, 0x425a4255u, 0x42694264u, 0x42944289u, 0x440142a5u, 0x44194415u,
+ 0x44454429u, 0x444a4448u, 0x44544451u, 0x44564455u, 0x44624461u, 0x44684465u,
+ 0x4481446au, 0x44894486u, 0x44924490u, 0x44a04495u, 0x44a944a1u, 0x45024501u,
+ 0x450a4505u, 0x45144511u, 0x45164515u, 0x45204519u, 0x452a4525u, 0x45444541u,
+ 0x45464545u, 0x45504549u, 0x45544551u, 0x45564555u, 0x45594558u, 0x45644561u,
+ 0x45664565u, 0x45824569u, 0x45854584u, 0x45914588u, 0x45954594u, 0x45994596u,
+ 0x45a5459au, 0x45aa45a8u, 0x46054601u, 0x46144609u, 0x46184615u, 0x4621461au,
+ 0x46294624u, 0x46424640u, 0x46484645u, 0x46514650u, 0x46554652u, 0x46594656u,
+ 0x46654662u, 0x46814668u, 0x468a4685u, 0x46954694u, 0x46a446a1u, 0x480546a6u,
+ 0x48154811u, 0x4825481au, 0x48494842u, 0x48554850u, 0x48614858u, 0x48664864u,
+ 0x48854869u, 0x48944891u, 0x48994896u, 0x490148a5u, 0x49064905u, 0x4910490au,
+ 0x49154914u, 0x49214918u, 0x49264924u, 0x49454940u, 0x4951494au, 0x49544952u,
+ 0x49564955u, 0x49604959u, 0x49654962u, 0x496a4966u, 0x49894986u, 0x49954992u,
+ 0x49984996u, 0x49a449a1u, 0x49a949a6u, 0x4a444a16u, 0x4a494a46u, 0x4a584a55u,
+ 0x4a644a5au, 0x4a944a69u, 0x50014aa5u, 0x50055004u, 0x50095006u, 0x50155012u,
+ 0x5021501au, 0x50295024u, 0x50455040u, 0x50515048u, 0x50555054u, 0x50595056u,
+ 0x50685065u, 0x50895086u, 0x50985095u, 0x50a150a0u, 0x50a950a6u, 0x51085105u,
+ 0x510a5109u, 0x51145111u, 0x51165115u, 0x51195118u, 0x51255120u, 0x51285126u,
+ 0x5141512au, 0x51455144u, 0x51495146u, 0x51515150u, 0x51545152u, 0x51565155u,
+ 0x51595158u, 0x5161515au, 0x51655164u, 0x51695166u, 0x51855182u, 0x51945191u,
+ 0x51965195u, 0x51a05199u, 0x51aa51a5u, 0x52065201u, 0x52155212u, 0x5221521au,
+ 0x52425224u, 0x524a5245u, 0x52545251u, 0x52565255u, 0x52625259u, 0x52855265u,
+ 0x52925290u, 0x52995295u, 0x52a4529au, 0x54055404u, 0x54145411u, 0x54165415u,
+ 0x54195418u, 0x54255421u, 0x542a5428u, 0x54445441u, 0x54465445u, 0x544a5449u,
+ 0x54515450u, 0x54555454u, 0x54585456u, 0x545a5459u, 0x54625461u, 0x54655464u,
+ 0x54695466u, 0x54885480u, 0x5491548au, 0x54955494u, 0x54995496u, 0x54a454a1u,
+ 0x54aa54a5u, 0x55025501u, 0x55055504u, 0x55095506u, 0x55115510u, 0x55145512u,
+ 0x55165515u, 0x551a5519u, 0x55245521u, 0x55265525u, 0x55405529u, 0x55425541u,
+ 0x55455544u, 0x55485546u, 0x55505549u, 0x55525551u, 0x55555554u, 0x55585556u,
+ 0x555a5559u, 0x55615560u, 0x55655564u, 0x55685566u, 0x556a5569u, 0x55845581u,
+ 0x55895585u, 0x5590558au, 0x55945591u, 0x55965595u, 0x55995598u, 0x55a455a1u,
+ 0x55a655a5u, 0x560055a9u, 0x56025601u, 0x56065604u, 0x56095608u, 0x56145611u,
+ 0x56185615u, 0x56205619u, 0x56225621u, 0x56255624u, 0x56285626u, 0x56415629u,
+ 0x56465645u, 0x56495648u, 0x5650564au, 0x56525651u, 0x56555654u, 0x56585656u,
+ 0x565a5659u, 0x56645661u, 0x56695665u, 0x56855682u, 0x56885686u, 0x568a5689u,
+ 0x56955691u, 0x56a2569au, 0x56a656a5u, 0x56a956a8u, 0x58055804u, 0x58095806u,
+ 0x58155810u, 0x58215818u, 0x5845582au, 0x584a5848u, 0x58545851u, 0x58565855u,
+ 0x58595858u, 0x58625860u, 0x58655864u, 0x58895882u, 0x58925890u, 0x58985895u,
+ 0x58a958a1u, 0x59025901u, 0x590a5905u, 0x59145911u, 0x59165915u, 0x59255919u,
+ 0x59445941u, 0x59465945u, 0x59505949u, 0x59525951u, 0x59555954u, 0x59585956u,
+ 0x595a5959u, 0x59645961u, 0x59665965u, 0x59815969u, 0x59895985u, 0x59945991u,
+ 0x59965995u, 0x59995998u, 0x5a0459a5u, 0x5a155a08u, 0x5a205a1au, 0x5a265a25u,
+ 0x5a455a29u, 0x5a495a48u, 0x5a555a51u, 0x5a585a56u, 0x5a625a59u, 0x5a685a65u,
+ 0x5a815a6au, 0x5a925a8au, 0x5a965a95u, 0x5a9a5a98u, 0x60055aa1u, 0x60166014u,
+ 0x60256019u, 0x60506044u, 0x60566055u, 0x605a6058u, 0x60646061u, 0x60696066u,
+ 0x60966081u, 0x610160a5u, 0x61066104u, 0x61126109u, 0x61216115u, 0x61266122u,
+ 0x61456129u, 0x61516149u, 0x61566155u, 0x61656159u, 0x616a6166u, 0x618a6184u,
+ 0x61956192u, 0x61a661a1u, 0x621161a9u, 0x62196216u, 0x62416240u, 0x62556246u,
+ 0x62586256u, 0x62856260u, 0x62966291u, 0x641162a5u, 0x64156412u, 0x641a6416u,
+ 0x64266421u, 0x64406429u, 0x64456442u, 0x644a6448u, 0x64546451u, 0x64566455u,
+ 0x645a6459u, 0x64626460u, 0x64846465u, 0x64896485u, 0x64926490u, 0x64956494u,
+ 0x64986496u, 0x64a1649au, 0x64a964a4u, 0x65086505u, 0x6511650au, 0x65166515u,
+ 0x65446519u, 0x65466545u, 0x65506549u, 0x65546551u, 0x65566555u, 0x65616559u,
+ 0x65656564u, 0x65696566u, 0x65896586u, 0x6591658au, 0x65966595u, 0x659a6599u,
+ 0x65a565a2u, 0x65a865a6u, 0x66096602u, 0x66206615u, 0x66286626u, 0x66406629u,
+ 0x66486645u, 0x6651664au, 0x66556654u, 0x66586656u, 0x6660665au, 0x66686665u,
+ 0x66826680u, 0x668a6685u, 0x66966694u, 0x66996698u, 0x66a466a0u, 0x66aa66a6u,
+ 0x68196816u, 0x68416825u, 0x68556852u, 0x6861685au, 0x68856869u, 0x68986891u,
+ 0x690168a6u, 0x69106904u, 0x69216915u, 0x69266924u, 0x69406929u, 0x69456941u,
+ 0x69486946u, 0x69546951u, 0x69566955u, 0x69606959u, 0x696a6965u, 0x69846982u,
+ 0x6995698au, 0x69a469a1u, 0x69a969a5u, 0x6a166a11u, 0x6a416a18u, 0x6a496a44u,
+ 0x6a556a50u, 0x6a5a6a58u, 0x6a656a64u, 0x6a866a69u, 0x6a986a94u, 0x6aa66a9au,
+ 0x80028000u, 0x800a8008u, 0x80228020u, 0x802a8028u, 0x80508045u, 0x80548051u,
+ 0x80598056u, 0x80808065u, 0x80888082u, 0x8095808au, 0x80a280a0u, 0x80aa80a8u,
+ 0x81118105u, 0x81168114u, 0x81258119u, 0x81448141u, 0x81508149u, 0x81558152u,
+ 0x81588156u, 0x81648159u, 0x81698166u, 0x81898185u, 0x81968194u, 0x81a58199u,
+ 0x82028200u, 0x820a8208u, 0x82208215u, 0x82288222u, 0x8251822au, 0x82598254u,
+ 0x82808265u, 0x82888282u, 0x8295828au, 0x82a282a0u, 0x82aa82a8u, 0x84198414u,
+ 0x84448441u, 0x84558451u, 0x8461845au, 0x84698464u, 0x84998494u, 0x85098501u,
+ 0x85158512u, 0x8526851au, 0x85408529u, 0x85458541u, 0x85518548u, 0x85558554u,
+ 0x85598556u, 0x8565855au, 0x85688566u, 0x8581856au, 0x85868584u, 0x85908589u,
+ 0x85958592u, 0x85a68598u, 0x86168611u, 0x86258619u, 0x86448641u, 0x864a8649u,
+ 0x86558650u, 0x865a8659u, 0x86668661u, 0x8685866au, 0x869a8691u, 0x880086a4u,
+ 0x88088802u, 0x8815880au, 0x88228820u, 0x882a8828u, 0x88458841u, 0x88548851u,
+ 0x88658859u, 0x88808869u, 0x88888882u, 0x8895888au, 0x88a288a0u, 0x88aa88a8u,
+ 0x89068905u, 0x89148911u, 0x89258916u, 0x89448941u, 0x89498946u, 0x89528950u,
+ 0x895a8955u, 0x89648961u, 0x89968985u, 0x89a58999u, 0x8a028a00u, 0x8a0a8a08u,
+ 0x8a208a15u, 0x8a288a22u, 0x8a458a2au, 0x8a548a51u, 0x8a808a56u, 0x8a888a82u,
+ 0x8a958a8au, 0x8aa28aa0u, 0x8aaa8aa8u, 0x90119005u, 0x90189016u, 0x90259019u,
+ 0x90469041u, 0x90559049u, 0x905a9058u, 0x906a9069u, 0x90919085u, 0x90969094u,
+ 0x90a59099u, 0x91049101u, 0x91099106u, 0x91159110u, 0x911a9118u, 0x91249121u,
+ 0x91299126u, 0x91459140u, 0x91519150u, 0x91559154u, 0x91599156u, 0x91659162u,
+ 0x91869184u, 0x91959192u, 0x91a19198u, 0x91a691a4u, 0x920591a9u, 0x92149211u,
+ 0x92259219u, 0x92469244u, 0x92509249u, 0x92559252u, 0x92669258u, 0x92859269u,
+ 0x92969294u, 0x940192a9u, 0x94069404u, 0x94159410u, 0x94269418u, 0x944a9440u,
+ 0x94549451u, 0x94569455u, 0x94599458u, 0x94619460u, 0x94659462u, 0x94869484u,
+ 0x94949492u, 0x94989495u, 0x94a994a1u, 0x95059500u, 0x950a9508u, 0x95119510u,
+ 0x95159514u, 0x95199516u, 0x95259521u, 0x952a9529u, 0x95449541u, 0x95469545u,
+ 0x95509549u, 0x95529551u, 0x95559554u, 0x95589556u, 0x955a9559u, 0x95649561u,
+ 0x95669565u, 0x95819569u, 0x95889585u, 0x95929591u, 0x95959594u, 0x95999596u,
+ 0x95a0959au, 0x95a595a2u, 0x95aa95a8u, 0x96049601u, 0x96159610u, 0x96209619u,
+ 0x96299626u, 0x96489645u, 0x96519649u, 0x96559652u, 0x96599656u, 0x96689665u,
+ 0x96849682u, 0x968a9689u, 0x96949692u, 0x96a49695u, 0x96a996a6u, 0x98169805u,
+ 0x98259819u, 0x98469841u, 0x98529850u, 0x98569855u, 0x9864985au, 0x98859865u,
+ 0x98969891u, 0x98a59899u, 0x99069904u, 0x99109909u, 0x99159912u, 0x991a9918u,
+ 0x99219920u, 0x99269924u, 0x99429940u, 0x99489945u, 0x9951994au, 0x99559954u,
+ 0x99599956u, 0x99659962u, 0x996a9966u, 0x99849981u, 0x99929990u, 0x999a9995u,
+ 0x99a699a1u, 0x9a159a05u, 0x9a449a25u, 0x9a499a46u, 0x9a559a50u, 0x9a619a58u,
+ 0x9a919a85u, 0x9a959a94u, 0xa0009a96u, 0xa008a002u, 0xa015a00au, 0xa022a020u,
+ 0xa02aa028u, 0xa051a045u, 0xa056a054u, 0xa080a059u, 0xa088a082u, 0xa095a08au,
+ 0xa0a2a0a0u, 0xa0aaa0a8u, 0xa109a105u, 0xa114a111u, 0xa119a116u, 0xa146a11au,
+ 0xa151a149u, 0xa158a155u, 0xa161a15au, 0xa185a164u, 0xa192a190u, 0xa199a196u,
+ 0xa208a202u, 0xa210a20au, 0xa222a219u, 0xa22aa228u, 0xa251a245u, 0xa259a256u,
+ 0xa280a265u, 0xa288a282u, 0xa295a28au, 0xa2a2a2a0u, 0xa2aaa2a8u, 0xa425a419u,
+ 0xa444a441u, 0xa454a450u, 0xa458a455u, 0xa461a45au, 0xa466a465u, 0xa469a468u,
+ 0xa506a485u, 0xa510a509u, 0xa515a512u, 0xa526a518u, 0xa542a529u, 0xa551a545u,
+ 0xa555a554u, 0xa559a556u, 0xa56aa565u, 0xa584a581u, 0xa586a585u, 0xa592a589u,
+ 0xa598a595u, 0xa611a605u, 0xa61aa616u, 0xa625a621u, 0xa646a644u, 0xa652a64au,
+ 0xa656a655u, 0xa660a658u, 0xa686a662u, 0xa695a690u, 0xa699a696u, 0xa6a4a6a1u,
+ 0xa800a6a6u, 0xa808a802u, 0xa820a80au, 0xa828a822u, 0xa851a82au, 0xa856a854u,
+ 0xa880a859u, 0xa888a882u, 0xa895a88au, 0xa8a2a8a0u, 0xa8aaa8a8u, 0xa914a905u,
+ 0xa921a919u, 0xa941a925u, 0xa955a950u, 0xa961a95au, 0xa969a966u, 0xa996a990u,
+ 0xaa02aa00u, 0xaa0aaa08u, 0xaa22aa20u, 0xaa2aaa28u, 0xaa54aa51u, 0xaa80aa56u,
+ 0xaa88aa82u, 0xaa95aa8au, 0xaaa2aaa0u, 0xaaaaaaa8u);
+
+const uint IQ3S_GRID[512] = uint[512](
+ 0x01010101u, 0x01010103u, 0x01010105u, 0x0101010bu, 0x0101010fu, 0x01010301u,
+ 0x01010303u, 0x01010305u, 0x01010309u, 0x0101030du, 0x01010501u, 0x01010503u,
+ 0x0101050bu, 0x01010707u, 0x01010901u, 0x01010905u, 0x0101090bu, 0x0101090fu,
+ 0x01010b03u, 0x01010b07u, 0x01010d01u, 0x01010d05u, 0x01010f03u, 0x01010f09u,
+ 0x01010f0fu, 0x01030101u, 0x01030103u, 0x01030105u, 0x01030109u, 0x01030301u,
+ 0x01030303u, 0x0103030bu, 0x01030501u, 0x01030507u, 0x0103050fu, 0x01030703u,
+ 0x0103070bu, 0x01030909u, 0x01030d03u, 0x01030d0bu, 0x01030f05u, 0x01050101u,
+ 0x01050103u, 0x0105010bu, 0x0105010fu, 0x01050301u, 0x01050307u, 0x0105030du,
+ 0x01050503u, 0x0105050bu, 0x01050701u, 0x01050709u, 0x01050905u, 0x0105090bu,
+ 0x0105090fu, 0x01050b03u, 0x01050b07u, 0x01050f01u, 0x01050f07u, 0x01070107u,
+ 0x01070303u, 0x0107030bu, 0x01070501u, 0x01070505u, 0x01070703u, 0x01070707u,
+ 0x0107070du, 0x01070909u, 0x01070b01u, 0x01070b05u, 0x01070d0fu, 0x01070f03u,
+ 0x01070f0bu, 0x01090101u, 0x01090307u, 0x0109030fu, 0x01090503u, 0x01090509u,
+ 0x01090705u, 0x01090901u, 0x01090907u, 0x01090b03u, 0x01090f01u, 0x010b0105u,
+ 0x010b0109u, 0x010b0501u, 0x010b0505u, 0x010b050du, 0x010b0707u, 0x010b0903u,
+ 0x010b090bu, 0x010b090fu, 0x010b0d0du, 0x010b0f07u, 0x010d010du, 0x010d0303u,
+ 0x010d0307u, 0x010d0703u, 0x010d0b05u, 0x010d0f03u, 0x010f0101u, 0x010f0105u,
+ 0x010f0109u, 0x010f0501u, 0x010f0505u, 0x010f050du, 0x010f0707u, 0x010f0b01u,
+ 0x010f0b09u, 0x03010101u, 0x03010103u, 0x03010105u, 0x03010109u, 0x03010301u,
+ 0x03010303u, 0x03010307u, 0x0301030bu, 0x0301030fu, 0x03010501u, 0x03010505u,
+ 0x03010703u, 0x03010709u, 0x0301070du, 0x03010b09u, 0x03010b0du, 0x03010d03u,
+ 0x03010f05u, 0x03030101u, 0x03030103u, 0x03030107u, 0x0303010du, 0x03030301u,
+ 0x03030309u, 0x03030503u, 0x03030701u, 0x03030707u, 0x03030903u, 0x03030b01u,
+ 0x03030b05u, 0x03030f01u, 0x03030f0du, 0x03050101u, 0x03050305u, 0x0305030bu,
+ 0x0305030fu, 0x03050501u, 0x03050509u, 0x03050705u, 0x03050901u, 0x03050907u,
+ 0x03050b0bu, 0x03050d01u, 0x03050f05u, 0x03070103u, 0x03070109u, 0x0307010fu,
+ 0x03070301u, 0x03070307u, 0x03070503u, 0x0307050fu, 0x03070701u, 0x03070709u,
+ 0x03070903u, 0x03070d05u, 0x03070f01u, 0x03090107u, 0x0309010bu, 0x03090305u,
+ 0x03090309u, 0x03090703u, 0x03090707u, 0x03090905u, 0x0309090du, 0x03090b01u,
+ 0x03090b09u, 0x030b0103u, 0x030b0301u, 0x030b0307u, 0x030b0503u, 0x030b0701u,
+ 0x030b0705u, 0x030b0b03u, 0x030d0501u, 0x030d0509u, 0x030d050fu, 0x030d0909u,
+ 0x030d090du, 0x030f0103u, 0x030f0107u, 0x030f0301u, 0x030f0305u, 0x030f0503u,
+ 0x030f070bu, 0x030f0903u, 0x030f0d05u, 0x030f0f01u, 0x05010101u, 0x05010103u,
+ 0x05010107u, 0x0501010bu, 0x0501010fu, 0x05010301u, 0x05010305u, 0x05010309u,
+ 0x0501030du, 0x05010503u, 0x05010507u, 0x0501050fu, 0x05010701u, 0x05010705u,
+ 0x05010903u, 0x05010907u, 0x0501090bu, 0x05010b01u, 0x05010b05u, 0x05010d0fu,
+ 0x05010f01u, 0x05010f07u, 0x05010f0bu, 0x05030101u, 0x05030105u, 0x05030301u,
+ 0x05030307u, 0x0503030fu, 0x05030505u, 0x0503050bu, 0x05030703u, 0x05030709u,
+ 0x05030905u, 0x05030b03u, 0x05050103u, 0x05050109u, 0x0505010fu, 0x05050503u,
+ 0x05050507u, 0x05050701u, 0x0505070fu, 0x05050903u, 0x05050b07u, 0x05050b0fu,
+ 0x05050f03u, 0x05050f09u, 0x05070101u, 0x05070105u, 0x0507010bu, 0x05070303u,
+ 0x05070505u, 0x05070509u, 0x05070703u, 0x05070707u, 0x05070905u, 0x05070b01u,
+ 0x05070d0du, 0x05090103u, 0x0509010fu, 0x05090501u, 0x05090507u, 0x05090705u,
+ 0x0509070bu, 0x05090903u, 0x05090f05u, 0x05090f0bu, 0x050b0109u, 0x050b0303u,
+ 0x050b0505u, 0x050b070fu, 0x050b0901u, 0x050b0b07u, 0x050b0f01u, 0x050d0101u,
+ 0x050d0105u, 0x050d010fu, 0x050d0503u, 0x050d0b0bu, 0x050d0d03u, 0x050f010bu,
+ 0x050f0303u, 0x050f050du, 0x050f0701u, 0x050f0907u, 0x050f0b01u, 0x07010105u,
+ 0x07010303u, 0x07010307u, 0x0701030bu, 0x0701030fu, 0x07010505u, 0x07010703u,
+ 0x07010707u, 0x0701070bu, 0x07010905u, 0x07010909u, 0x0701090fu, 0x07010b03u,
+ 0x07010d07u, 0x07010f03u, 0x07030103u, 0x07030107u, 0x0703010bu, 0x07030309u,
+ 0x07030503u, 0x07030507u, 0x07030901u, 0x07030d01u, 0x07030f05u, 0x07030f0du,
+ 0x07050101u, 0x07050305u, 0x07050501u, 0x07050705u, 0x07050709u, 0x07050b01u,
+ 0x07070103u, 0x07070301u, 0x07070309u, 0x07070503u, 0x07070507u, 0x0707050fu,
+ 0x07070701u, 0x07070903u, 0x07070907u, 0x0707090fu, 0x07070b0bu, 0x07070f07u,
+ 0x07090107u, 0x07090303u, 0x0709030du, 0x07090505u, 0x07090703u, 0x07090b05u,
+ 0x07090d01u, 0x07090d09u, 0x070b0103u, 0x070b0301u, 0x070b0305u, 0x070b050bu,
+ 0x070b0705u, 0x070b0909u, 0x070b0b0du, 0x070b0f07u, 0x070d030du, 0x070d0903u,
+ 0x070f0103u, 0x070f0107u, 0x070f0501u, 0x070f0505u, 0x070f070bu, 0x09010101u,
+ 0x09010109u, 0x09010305u, 0x09010501u, 0x09010509u, 0x0901050fu, 0x09010705u,
+ 0x09010903u, 0x09010b01u, 0x09010f01u, 0x09030105u, 0x0903010fu, 0x09030303u,
+ 0x09030307u, 0x09030505u, 0x09030701u, 0x0903070bu, 0x09030907u, 0x09030b03u,
+ 0x09030b0bu, 0x09050103u, 0x09050107u, 0x09050301u, 0x0905030bu, 0x09050503u,
+ 0x09050707u, 0x09050901u, 0x09050b0fu, 0x09050d05u, 0x09050f01u, 0x09070109u,
+ 0x09070303u, 0x09070307u, 0x09070501u, 0x09070505u, 0x09070703u, 0x0907070bu,
+ 0x09090101u, 0x09090105u, 0x09090509u, 0x0909070fu, 0x09090901u, 0x09090f03u,
+ 0x090b010bu, 0x090b010fu, 0x090b0503u, 0x090b0d05u, 0x090d0307u, 0x090d0709u,
+ 0x090d0d01u, 0x090f0301u, 0x090f030bu, 0x090f0701u, 0x090f0907u, 0x090f0b03u,
+ 0x0b010105u, 0x0b010301u, 0x0b010309u, 0x0b010505u, 0x0b010901u, 0x0b010909u,
+ 0x0b01090fu, 0x0b010b05u, 0x0b010d0du, 0x0b010f09u, 0x0b030103u, 0x0b030107u,
+ 0x0b03010bu, 0x0b030305u, 0x0b030503u, 0x0b030705u, 0x0b030f05u, 0x0b050101u,
+ 0x0b050303u, 0x0b050507u, 0x0b050701u, 0x0b05070du, 0x0b050b07u, 0x0b070105u,
+ 0x0b07010fu, 0x0b070301u, 0x0b07050fu, 0x0b070909u, 0x0b070b03u, 0x0b070d0bu,
+ 0x0b070f07u, 0x0b090103u, 0x0b090109u, 0x0b090501u, 0x0b090705u, 0x0b09090du,
+ 0x0b0b0305u, 0x0b0b050du, 0x0b0b0b03u, 0x0b0b0b07u, 0x0b0d0905u, 0x0b0f0105u,
+ 0x0b0f0109u, 0x0b0f0505u, 0x0d010303u, 0x0d010307u, 0x0d01030bu, 0x0d010703u,
+ 0x0d010707u, 0x0d010d01u, 0x0d030101u, 0x0d030501u, 0x0d03050fu, 0x0d030d09u,
+ 0x0d050305u, 0x0d050709u, 0x0d050905u, 0x0d050b0bu, 0x0d050d05u, 0x0d050f01u,
+ 0x0d070101u, 0x0d070309u, 0x0d070503u, 0x0d070901u, 0x0d09050bu, 0x0d090907u,
+ 0x0d090d05u, 0x0d0b0101u, 0x0d0b0107u, 0x0d0b0709u, 0x0d0b0d01u, 0x0d0d010bu,
+ 0x0d0d0901u, 0x0d0f0303u, 0x0d0f0307u, 0x0f010101u, 0x0f010109u, 0x0f01010fu,
+ 0x0f010501u, 0x0f010505u, 0x0f01070du, 0x0f010901u, 0x0f010b09u, 0x0f010d05u,
+ 0x0f030105u, 0x0f030303u, 0x0f030509u, 0x0f030907u, 0x0f03090bu, 0x0f050103u,
+ 0x0f050109u, 0x0f050301u, 0x0f05030du, 0x0f050503u, 0x0f050701u, 0x0f050b03u,
+ 0x0f070105u, 0x0f070705u, 0x0f07070bu, 0x0f070b07u, 0x0f090103u, 0x0f09010bu,
+ 0x0f090307u, 0x0f090501u, 0x0f090b01u, 0x0f0b0505u, 0x0f0b0905u, 0x0f0d0105u,
+ 0x0f0d0703u, 0x0f0f0101u);
+
+const int IQ4[16] = int[16](-127, -104, -83, -65, -49, -35, -22, -10,
+ 1, 13, 25, 38, 53, 69, 89, 113);
+
+/* the grid (or IQ4_XS's levels) in the workgroup's shared memory */
+shared uint GS[NGW];
+
+/* byte, 16-bit word and realigned word at byte off of the weights */
+uint bbyte(U1 W, uint off) { return (W.v[off >> 2] >> (8u * (off & 3u))) & 0xffu; }
+uint bhalf(U1 W, uint off) { return bbyte(W, off) | (bbyte(W, off + 1u) << 8); }
+uint bword(U1 W, uint off)
+{
+ uint a = off >> 2, sh = 8u * (off & 3u);
+ return sh != 0u ? (W.v[a] >> sh) | (W.v[a + 1u] << (32u - sh)) : W.v[a];
+}
+
+uint signs7(uint i) { return i | ((uint(bitCount(i)) & 1u) << 7); }
+
+/* four levels of a grid word, each negated where its sign bit is set */
+uint signed4(uint g, uint s)
+{
+ uint r = 0u;
+ for (uint i = 0; i < 4u; i++) {
+ int v = int((g >> (8u * i)) & 0xffu);
+ if (((s >> i) & 1u) != 0u)
+ v = -v;
+ r |= (uint(v) & 0xffu) << (8u * i);
+ }
+ return r;
+}
+
+/* levels 4h..4h+3 of an IQ1 grid entry, 8 level + delta, as signed bytes */
+uint iq1w(uint idx, uint h, int delta)
+{
+ uint e = (GS[idx >> 1] >> (16u * (idx & 1u))) & 0xffffu, r = 0u;
+ for (uint i = 0; i < 4u; i++) {
+ int lv = int((e >> (2u * (4u * h + i))) & 3u) - 1;
+ r |= (uint(8 * lv + delta) & 0xffu) << (8u * i);
+ }
+ return r;
+}
+
+/* each byte of w (a nibble) through IQ4_XS's levels */
+uint lv4(uint w)
+{
+ return GS[w & 15u] | (GS[(w >> 8) & 15u] << 8) | (GS[(w >> 16) & 15u] << 16) |
+ (GS[(w >> 24) & 15u] << 24);
+}
+
+/* word kw (weights 4 kw .. 4 kw + 3) of group gb of the block at byte boff */
+uint wword(U1 W, uint boff, uint gb, uint kw)
+{
+ uint k = kw / 2u, h = kw % 2u;
+ if (KIND == 0u) { /* Q2_K: weight e = 128 n + 32 j + l at bits 2 j of byte 32 n + l */
+ uint q = bword(W, boff + 16u + 32u * (gb / 4u) + 4u * kw);
+ return (q >> (2u * (gb % 4u))) & 0x03030303u;
+ }
+ if (KIND == 1u) {
+ uint aux1 = bword(W, boff + 6u + 8u * gb);
+ uint sg = signs7((aux1 >> (7u * k)) & 127u);
+ return signed4(GS[2u * bbyte(W, boff + 2u + 8u * gb + k) + h], (sg >> (4u * h)) & 15u);
+ }
+ if (KIND == 2u) {
+ uint q = bhalf(W, boff + 2u + 2u * (4u * gb + k));
+ return signed4(GS[2u * (q & 511u) + h], (signs7(q >> 9) >> (4u * h)) & 15u);
+ }
+ if (KIND == 3u) {
+ uint idx = bbyte(W, boff + 2u + 4u * gb + k) |
+ ((bbyte(W, boff + 66u + gb) << (8u - 2u * k)) & 0x300u);
+ uint sg = bbyte(W, boff + 34u + 4u * gb + k);
+ return signed4(GS[2u * idx + h], (sg >> (4u * h)) & 15u);
+ }
+ if (KIND == 4u) {
+ uint aux = bword(W, boff + 66u + 4u * gb);
+ uint sg = signs7((aux >> (7u * k)) & 127u);
+ return signed4(GS[bbyte(W, boff + 2u + 8u * gb + 2u * k + h)], (sg >> (4u * h)) & 15u);
+ }
+ if (KIND == 5u) {
+ uint qh = bhalf(W, boff + 34u + 2u * gb);
+ uint idx = bbyte(W, boff + 2u + 4u * gb + k) | (((qh >> (3u * k)) & 7u) << 8);
+ return iq1w(idx, h, ((qh >> 15) & 1u) != 0u ? -1 : 1);
+ }
+ if (KIND == 6u) {
+ uint qh = bbyte(W, boff + 32u + 2u * gb + k / 2u);
+ uint idx = bbyte(W, boff + 4u * gb + k) | ((qh << ((k & 1u) != 0u ? 4u : 8u)) & 0x700u);
+ return iq1w(idx, h, ((qh >> ((k & 1u) != 0u ? 7u : 3u)) & 1u) != 0u ? -1 : 1);
+ }
+ if (KIND == 7u) {
+ uint idx = bbyte(W, boff + 2u + 8u * gb + kw) |
+ (((bbyte(W, boff + 66u + gb) >> kw) & 1u) << 8);
+ uint sg = bbyte(W, boff + 74u + 4u * gb + kw / 2u) >> (4u * (kw & 1u));
+ return signed4(GS[idx], sg & 15u);
+ }
+ uint q = bword(W, boff + 8u + 16u * gb + 4u * (kw & 3u));
+ return lv4(kw < 4u ? q & 0x0f0f0f0fu : (q >> 4) & 0x0f0f0f0fu);
+}
+
+/* group gb's scales: its halves' (x, y) and, Q2_K's, its minima (z, w) */
+vec4 gscales(U1 W, uint boff, uint gb)
+{
+ if (KIND == 0u) {
+ vec2 dm = unpackHalf2x16(bword(W, boff + 80u));
+ uint s0 = bbyte(W, boff + 2u * gb), s1 = bbyte(W, boff + 2u * gb + 1u);
+ return vec4(dm.x * float(s0 & 15u), dm.x * float(s1 & 15u), dm.y * float(s0 >> 4),
+ dm.y * float(s1 >> 4));
+ }
+ float d;
+ if (KIND == 6u) {
+ uint s0 = bhalf(W, boff + 48u), s1 = bhalf(W, boff + 50u), s2 = bhalf(W, boff + 52u),
+ s3 = bhalf(W, boff + 54u);
+ d = unpackHalf2x16((s0 >> 12) | ((s1 >> 8) & 0x00f0u) | ((s2 >> 4) & 0x0f00u) |
+ (s3 & 0xf000u)).x;
+ } else {
+ d = unpackHalf2x16(bhalf(W, boff)).x;
+ }
+ float lo, hi;
+ if (KIND == 1u) {
+ lo = hi = d * 0.125 * float(1u + 2u * (bword(W, boff + 6u + 8u * gb) >> 28));
+ } else if (KIND == 2u || KIND == 3u) {
+ uint s = bbyte(W, boff + (KIND == 2u ? 66u : 74u) + gb);
+ lo = d * 0.125 * float(1u + 2u * (s & 15u));
+ hi = d * 0.125 * float(1u + 2u * (s >> 4));
+ } else if (KIND == 4u) {
+ lo = hi = d * 0.25 * float(1u + 2u * (bword(W, boff + 66u + 4u * gb) >> 28));
+ } else if (KIND == 5u) {
+ lo = hi = d * 0.125 * float(2u * ((bhalf(W, boff + 34u + 2u * gb) >> 12) & 7u) + 1u);
+ } else if (KIND == 6u) {
+ uint s = bhalf(W, boff + 48u + 2u * (gb / 2u)), sh = 6u * (gb & 1u);
+ lo = d * 0.125 * float(2u * ((s >> sh) & 7u) + 1u);
+ hi = d * 0.125 * float(2u * ((s >> (sh + 3u)) & 7u) + 1u);
+ } else if (KIND == 7u) {
+ lo = hi = d * float(1u + 2u * ((bbyte(W, boff + 106u + gb / 2u) >> (4u * (gb & 1u))) & 15u));
+ } else {
+ uint sl = (bbyte(W, boff + 4u + gb / 2u) >> (4u * (gb & 1u))) & 15u;
+ uint sh = (bhalf(W, boff + 2u) >> (2u * gb)) & 3u;
+ lo = hi = d * float(int(sl | (sh << 4)) - 32);
+ }
+ return vec4(lo, hi, 0.0, 0.0);
+}
+
+
+void main()
+{
+ for (uint i = gl_LocalInvocationID.x; i < NGW; i += 128u)
+ GS[i] = KIND == 1u ? IQ2XXS_GRID[i] : KIND == 2u ? IQ2XS_GRID[i]
+ : KIND == 3u ? IQ2S_GRID[i] : KIND == 4u ? IQ3XXS_GRID[i]
+ : KIND == 5u || KIND == 6u ? IQ1S_GRID[i] : KIND == 7u ? IQ3S_GRID[i]
+ : KIND == 8u ? uint(IQ4[i]) & 0xffu : 0u;
+ barrier();
+ U4 X = U4(p.x);
+ U1 W1 = U1(p.w);
+ F Y = F(p.y);
+ uint id = gl_LocalInvocationID.x;
+ uint r0 = gl_WorkGroupID.x * BM, v0 = gl_WorkGroupID.y * BN;
+ uint warp = id / WARP, tiw = id % WARP;
+ uint tiwr = tiw % (WM / TM), tiwc = tiw / (WM / TM);
+ uint warp_r = warp % (BM / WM), warp_c = warp / (BM / WM);
+ float sums[WNITER * TN * TM];
+ [[unroll]] for (uint i = 0; i < WNITER * TN * TM; i++) sums[i] = 0.0;
+ uint ngroups = p.nb * 8u;
+ for (uint g0 = 0; g0 < ngroups; g0 += KS) {
+ // weights: 4 invocations a row, 2 words each of the group's 8
+ {
+ uint iqs = id % 4u, rl = id / 4u;
+ [[unroll]] for (uint l = 0; l < BM; l += 32u) {
+ uint r = min(r0 + rl + l, p.rows - 1u);
+ [[unroll]] for (uint k = 0; k < KS; k++) {
+ uint g = g0 + k, boff = (r * p.nb + g / 8u) * BS;
+ [[unroll]] for (uint t = 0; t < 2u; t++)
+ sa[k * BM + rl + l].qs[2u * iqs + t] = int(wword(W1, boff, g % 8u, 2u * iqs + t));
+ if (iqs == 0u)
+ sa[k * BM + rl + l].sc = gscales(W1, boff, g % 8u);
+ }
+ }
+ }
+ // activations: 2 invocations a vector, each half a group's quants
+ {
+ uint iqs = id % 2u, c = id / 2u;
+ uint v = min(v0 + c, p.nv - 1u);
+ [[unroll]] for (uint k = 0; k < KS; k++) {
+ uint g = g0 + k, b = g / 8u, j = g % 8u;
+ uint xo = (v * p.xs + b) * XB;
+ uvec4 q = X.v[xo + 2u * j + iqs];
+ sb[k * BN + c].qs[4u * iqs] = int(q.x);
+ sb[k * BN + c].qs[4u * iqs + 1u] = int(q.y);
+ sb[k * BN + c].qs[4u * iqs + 2u] = int(q.z);
+ sb[k * BN + c].qs[4u * iqs + 3u] = int(q.w);
+ if (iqs == 0u) {
+ uvec4 bq = X.v[xo + 16u + j / 4u];
+ uint bw = j % 4u == 0u ? bq.x : j % 4u == 1u ? bq.y : j % 4u == 2u ? bq.z : bq.w;
+ sb[k * BN + c].d = uintBitsToFloat(X.v[xo + 18u].x);
+ sb[k * BN + c].bs = vec2(float(bitfieldExtract(int(bw), 0, 16)),
+ float(bitfieldExtract(int(bw), 16, 16)));
+ }
+ }
+ }
+ barrier();
+ for (uint k = 0; k < KS; k++) {
+ ACache ca[TM];
+ [[unroll]] for (uint cr = 0; cr < TM; cr++)
+ ca[cr] = sa[k * BM + warp_r * WM + tiwr * TM + cr];
+ [[unroll]] for (uint wsic = 0; wsic < WNITER; wsic++)
+ [[unroll]] for (uint cc = 0; cc < TN; cc++) {
+ BCache cb = sb[k * BN + warp_c * WN + wsic * WSUBN + tiwc * TN + cc];
+ [[unroll]] for (uint cr = 0; cr < TM; cr++) {
+ int q0 = 0, q1 = 0;
+ [[unroll]] for (uint i = 0; i < 4u; i++) {
+ q0 += dotPacked4x8EXT(ca[cr].qs[i], cb.qs[i]);
+ q1 += dotPacked4x8EXT(ca[cr].qs[i + 4u], cb.qs[i + 4u]);
+ }
+ sums[(wsic * TN + cc) * TM + cr] +=
+ cb.d * (ca[cr].sc.x * float(q0) + ca[cr].sc.y * float(q1) -
+ (ca[cr].sc.z * cb.bs.x + ca[cr].sc.w * cb.bs.y));
+ }
+ }
+ }
+ barrier();
+ }
+ [[unroll]] for (uint wsic = 0; wsic < WNITER; wsic++)
+ [[unroll]] for (uint cc = 0; cc < TN; cc++)
+ [[unroll]] for (uint cr = 0; cr < TM; cr++) {
+ uint row = r0 + warp_r * WM + tiwr * TM + cr;
+ uint v = v0 + warp_c * WN + wsic * WSUBN + tiwc * TN + cc;
+ if (row < p.rows && v < p.nv)
+ Y.v[v * p.ys + row] = sums[(wsic * TN + cc) * TM + cr];
+ }
+}
diff --git a/src/llm/shaders/ix_pool.comp b/src/llm/shaders/ix_pool.comp
new file mode 100644
index 0000000..de0b2b1
--- /dev/null
+++ b/src/llm/shaders/ix_pool.comp
@@ -0,0 +1,66 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+#version 450
+// qwen4exp's indexer, second step (model_qsa.c pool_block): the blocks of
+// r positions a block of n tokens at pos completes, workgroup x = w the
+// w-th from block pos / r on (those not complete by pos + n leave): the
+// mean of its r raw keys (half precision, d a row), normalized (rms norm
+// with the weights kn) and its first n_rot numbers turned for the block's
+// first position (row rb + w of rope, which the caller wrote), into row b
+// of pool.
+#extension GL_EXT_buffer_reference : require
+#extension GL_EXT_buffer_reference_uvec2 : require
+
+layout(local_size_x = 256) in;
+
+layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer R { float v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) writeonly buffer W { float v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer RU { uint v[]; };
+
+layout(push_constant) uniform P {
+ uvec2 raw, kn, rope, pool, pos;
+ uint d, r, n, n_rot, rb;
+ float eps;
+} p;
+
+shared float sv[256];
+shared float ss[256];
+
+void main()
+{
+ uint t = gl_LocalInvocationID.x, w = gl_WorkGroupID.x, d = p.d;
+ uint pos = RU(p.pos).v[0], b = pos / p.r + w;
+ if ((b + 1u) * p.r > pos + p.n)
+ return; /* the whole workgroup: not complete */
+ RU RAW = RU(p.raw);
+ float x = 0.0;
+ if (t < d) {
+ for (uint k = 0; k < p.r; k++) {
+ uint wd = RAW.v[((b * p.r + k) * d + t) / 2u];
+ x += unpackHalf2x16(wd)[t % 2u];
+ }
+ x /= float(p.r);
+ }
+ ss[t] = x * x;
+ barrier();
+ for (uint s = 128u; s > 0u; s >>= 1) {
+ if (t < s)
+ ss[t] += ss[t + s];
+ barrier();
+ }
+ float scale = 1.0 / sqrt(ss[0] / float(d) + p.eps);
+ if (t < d)
+ sv[t] = x * scale * R(p.kn).v[t];
+ barrier();
+ uint half_ = p.n_rot / 2u, row = p.rb + w;
+ if (t < d) {
+ float v = sv[t];
+ if (t < p.n_rot) {
+ R RO = R(p.rope);
+ uint i = t % half_;
+ float c = RO.v[row * p.n_rot + i], s = RO.v[row * p.n_rot + half_ + i];
+ v = t < half_ ? sv[t] * c - sv[t + half_] * s : sv[t - half_] * s + sv[t] * c;
+ }
+ W(p.pool).v[b * d + t] = v;
+ }
+}
diff --git a/src/llm/shaders/ix_prep.comp b/src/llm/shaders/ix_prep.comp
new file mode 100644
index 0000000..9457c4a
--- /dev/null
+++ b/src/llm/shaders/ix_prep.comp
@@ -0,0 +1,68 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+#version 450
+// qwen4exp's indexer (model_qsa.c janas_m_qsa), first step for token j of a
+// block (workgroup y) at position p = pos + j (pos the position buffer's
+// word 0): workgroup x = h < nh normalizes query head h (rms norm with the
+// weights qn) and turns its first n_rot numbers for p (NeoX halves, the
+// cos and sin of row j of rope), in place; x = nh writes the token's raw
+// key at half precision to row p of raw (d numbers a row). The sums in
+// the GPU's order.
+#extension GL_EXT_buffer_reference : require
+#extension GL_EXT_buffer_reference_uvec2 : require
+
+layout(local_size_x = 256) in;
+
+layout(buffer_reference, std430, buffer_reference_align = 4) buffer F { float v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer R { float v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) writeonly buffer U { uint v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer RP { uint v[]; };
+
+layout(push_constant) uniform P {
+ uvec2 q, k, qn, rope, raw, pos;
+ uint nh, d, n_rot, qs; /* qs: floats from a token's heads to the next's */
+ float eps;
+} p;
+
+shared float sv[256];
+shared float ss[256];
+
+void main()
+{
+ uint t = gl_LocalInvocationID.x, h = gl_WorkGroupID.x, j = gl_WorkGroupID.y;
+ uint d = p.d;
+ if (h == p.nh) {
+ /* the raw key, two halves a word */
+ uint row = RP(p.pos).v[0] + j;
+ R K = R(p.k);
+ if (t < d / 2u)
+ U(p.raw).v[row * (d / 2u) + t] =
+ packHalf2x16(vec2(K.v[j * d + 2u * t], K.v[j * d + 2u * t + 1u]));
+ return;
+ }
+ F Q = F(p.q);
+ uint at = j * p.qs + h * d;
+ float x = t < d ? Q.v[at + t] : 0.0;
+ ss[t] = x * x;
+ barrier();
+ for (uint s = 128u; s > 0u; s >>= 1) {
+ if (t < s)
+ ss[t] += ss[t + s];
+ barrier();
+ }
+ float scale = 1.0 / sqrt(ss[0] / float(d) + p.eps);
+ if (t < d)
+ sv[t] = x * scale * R(p.qn).v[t];
+ barrier();
+ uint half_ = p.n_rot / 2u;
+ if (t < d) {
+ float v = sv[t];
+ if (t < p.n_rot) {
+ R RO = R(p.rope);
+ uint i = t % half_;
+ float c = RO.v[j * p.n_rot + i], s = RO.v[j * p.n_rot + half_ + i];
+ v = t < half_ ? sv[t] * c - sv[t + half_] * s : sv[t - half_] * s + sv[t] * c;
+ }
+ Q.v[at + t] = v;
+ }
+}
diff --git a/src/llm/shaders/ix_score.comp b/src/llm/shaders/ix_score.comp
new file mode 100644
index 0000000..a2019f6
--- /dev/null
+++ b/src/llm/shaders/ix_score.comp
@@ -0,0 +1,50 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+#version 450
+// qwen4exp's indexer, third step (model_qsa.c score_worker): for token j
+// of a block (workgroup y) at p = pos + j, the score of each complete block
+// b < (p + 1) / r it may choose among, the sum over its nh query heads of
+// relu(q_h . pool_b), into row j of sc (nbs a row); a thread a block,
+// workgroup x the blocks 256 x .. 256 x + 255. A token whose blocks are no
+// more than keep attends to every position (the dense attention): none.
+#extension GL_EXT_buffer_reference : require
+#extension GL_EXT_buffer_reference_uvec2 : require
+
+layout(local_size_x = 256) in;
+
+layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer R { float v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 16) readonly buffer R4 { vec4 v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) writeonly buffer W { float v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer RU { uint v[]; };
+
+layout(push_constant) uniform P {
+ uvec2 q, pool, sc, pos;
+ uint nh, d, r, keep, nbs, qs;
+} p;
+
+shared vec4 sq[512]; /* nh x d numbers, at most 2048 (gpu_token.c) */
+
+void main()
+{
+ uint t = gl_LocalInvocationID.x, j = gl_WorkGroupID.y;
+ uint pp = RU(p.pos).v[0] + j, blocks = (pp + 1u) / p.r;
+ uint b = gl_WorkGroupID.x * 256u + t;
+ if (blocks <= p.keep || gl_WorkGroupID.x * 256u >= blocks)
+ return; /* the whole workgroup */
+ uint d4 = p.d / 4u;
+ R4 Q = R4(p.q);
+ for (uint k = t; k < p.nh * d4; k += 256u)
+ sq[k] = Q.v[(j * p.qs) / 4u + k];
+ barrier();
+ if (b >= blocks)
+ return;
+ R4 K = R4(p.pool);
+ float sum = 0.0;
+ for (uint h = 0; h < p.nh; h++) {
+ float e = 0.0;
+ for (uint i = 0; i < d4; i++)
+ e += dot(sq[h * d4 + i], K.v[b * d4 + i]);
+ sum += max(e, 0.0);
+ }
+ W(p.sc).v[j * p.nbs + b] = sum;
+}
diff --git a/src/llm/shaders/ix_select.comp b/src/llm/shaders/ix_select.comp
new file mode 100644
index 0000000..d0c65b2
--- /dev/null
+++ b/src/llm/shaders/ix_select.comp
@@ -0,0 +1,117 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+#version 450
+// qwen4exp's indexer, last step (model_qsa.c choose and janas_m_qsa): the
+// positions token j of a block (workgroup y) at p = pos + j attends to,
+// into row j of list (ls words: their count, then the positions). While
+// its complete blocks are no more than keep, every position 0 .. p (the
+// dense attention); past that the keep blocks of highest score (sc's row
+// j), those above the keep-th score and among those equal to it the
+// earliest, in their order, r positions each, then the positions of its
+// own block that is not complete, tail = (p + 1) / r r .. p. The keep-th
+// score by its bits (scores are sums of relus, never negative: their bits
+// order as they do), a byte at a time from the top.
+#extension GL_EXT_buffer_reference : require
+#extension GL_EXT_buffer_reference_uvec2 : require
+
+layout(local_size_x = 256) in;
+
+layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer R { float v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) writeonly buffer W { uint v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer RU { uint v[]; };
+
+layout(push_constant) uniform P {
+ uvec2 sc, list, pos;
+ uint r, keep, nbs, ls;
+} p;
+
+shared uint hist[256];
+shared uint sel[2]; /* the threshold's bits so far, and the rank left */
+shared uint scn[256]; /* a scan's sums */
+
+/* the exclusive sum of v over the workgroup's threads, and in total their
+ sum */
+uint scan(uint t, uint v, out uint total)
+{
+ scn[t] = v;
+ barrier();
+ for (uint off = 1u; off < 256u; off <<= 1) {
+ uint a = t >= off ? scn[t - off] : 0u;
+ barrier();
+ scn[t] += a;
+ barrier();
+ }
+ total = scn[255];
+ uint x = scn[t] - v;
+ barrier();
+ return x;
+}
+
+void main()
+{
+ uint t = gl_LocalInvocationID.x, j = gl_WorkGroupID.y;
+ uint pp = RU(p.pos).v[0] + j, blocks = (pp + 1u) / p.r;
+ W L = W(p.list);
+ uint lo = j * p.ls;
+ if (blocks <= p.keep) {
+ for (uint i = t; i <= pp; i += 256u)
+ L.v[lo + 1u + i] = i;
+ if (t == 0u)
+ L.v[lo] = pp + 1u;
+ return;
+ }
+ R S = R(p.sc);
+ uint so = j * p.nbs;
+ if (t == 0u) {
+ sel[0] = 0u;
+ sel[1] = p.keep;
+ }
+ uint mask = 0u;
+ for (int shift = 24; shift >= 0; shift -= 8) {
+ hist[t] = 0u;
+ barrier();
+ uint prefix = sel[0];
+ for (uint b = t; b < blocks; b += 256u) {
+ uint u = floatBitsToUint(S.v[so + b]);
+ if ((u & mask) == prefix)
+ atomicAdd(hist[(u >> uint(shift)) & 255u], 1u);
+ }
+ barrier();
+ if (t == 0u) {
+ uint k = sel[1], bin = 255u;
+ for (;; bin--) {
+ if (k <= hist[bin] || bin == 0u)
+ break;
+ k -= hist[bin];
+ }
+ sel[0] = prefix | (bin << uint(shift));
+ sel[1] = k;
+ }
+ mask |= 255u << uint(shift);
+ barrier();
+ }
+ uint th = sel[0], equal = sel[1];
+ /* in block order: every block above th, and the first `equal` of
+ those at th */
+ uint eq_base = 0u, sel_base = 0u;
+ for (uint c = 0; c < blocks; c += 256u) {
+ uint b = c + t;
+ uint u = b < blocks ? floatBitsToUint(S.v[so + b]) : 0u;
+ bool in_ = b < blocks;
+ uint eq = in_ && u == th ? 1u : 0u, eq_tot;
+ uint eqx = scan(t, eq, eq_tot) + eq_base;
+ bool take = in_ && (u > th || (eq == 1u && eqx < equal));
+ uint sel_tot;
+ uint sx = scan(t, take ? 1u : 0u, sel_tot) + sel_base;
+ if (take)
+ for (uint k = 0; k < p.r; k++)
+ L.v[lo + 1u + sx * p.r + k] = b * p.r + k;
+ eq_base += eq_tot;
+ sel_base += sel_tot;
+ }
+ uint tail = blocks * p.r, np = p.keep * p.r;
+ for (uint i = t; tail + i <= pp; i += 256u)
+ L.v[lo + 1u + np + i] = tail + i;
+ if (t == 0u)
+ L.v[lo] = np + pp + 1u - tail;
+}
diff --git a/src/llm/shaders/ng_conv.comp b/src/llm/shaders/ng_conv.comp
new file mode 100644
index 0000000..6e837bb
--- /dev/null
+++ b/src/llm/shaders/ng_conv.comp
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+#version 450
+// qwen4exp's n-gram embeddings, their conv (model_hc.c conv_worker): for
+// channel e of token j at position p = pos + j (pos the position buffer's
+// word 0), the causal conv over the ring hist (cap rows of hc x d), kernel
+// k taps, tap i reading (k - 1 - i) * len positions back (zero before the
+// start), its SiLU, and the stream += the gated value (gv) + that. The
+// taps channel-major: w[e * k + i]. A thread per channel (x) and token (y).
+#extension GL_EXT_buffer_reference : require
+#extension GL_EXT_buffer_reference_uvec2 : require
+
+layout(local_size_x = 256) in;
+
+layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer F { float v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) buffer W { float v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer RP { uint v[]; };
+
+layout(push_constant) uniform P { uvec2 res, gv, w, hist, pos; uint hd, cap, k, len; } p;
+
+void main()
+{
+ uint e = gl_GlobalInvocationID.x, j = gl_WorkGroupID.y;
+ if (e >= p.hd)
+ return;
+ uint pp = RP(p.pos).v[0] + j;
+ F H = F(p.hist), Wt = F(p.w);
+ float acc = 0.0;
+ for (uint i = 0u; i < p.k; i++) {
+ uint back = (p.k - 1u - i) * p.len;
+ float term = pp >= back ? Wt.v[e * p.k + i] * H.v[((pp - back) % p.cap) * p.hd + e] : 0.0;
+ acc = i == 0u ? term : acc + term;
+ }
+ float silu = acc / (1.0 + exp(-acc));
+ W S = W(p.res);
+ S.v[j * p.hd + e] += F(p.gv).v[j * p.hd + e] + silu;
+}
diff --git a/src/llm/shaders/ng_gate.comp b/src/llm/shaders/ng_gate.comp
new file mode 100644
index 0000000..1e261b2
--- /dev/null
+++ b/src/llm/shaders/ng_gate.comp
@@ -0,0 +1,81 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+#version 450
+// qwen4exp's n-gram embeddings, their gates (model_hc.c gate_worker): for
+// stream c of token j, the key (kq, the key product's output) normalized
+// with nk, the stream as a query normalized with nq, s = their dot product
+// / sqrt(d), the gate sigmoid(signed sqrt(max(|s|, 1e-6))); the gated
+// value (val, d floats a token, times the gate) into gv, and the same
+// normalized with nc into the conv's ring (hist: cap rows of hc x d, row
+// (pos + j) % cap; pos the position buffer's word 0). A workgroup per
+// stream (x) and token (y); sums in the GPU's order.
+#extension GL_EXT_buffer_reference : require
+#extension GL_KHR_shader_subgroup_arithmetic : require
+#extension GL_KHR_shader_subgroup_basic : require
+#extension GL_EXT_buffer_reference_uvec2 : require
+
+layout(local_size_x = 256) in;
+
+layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer F { float v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) buffer W { float v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer RP { uint v[]; };
+
+// kq, res, gv: n rows of hc x d; val: n rows of d; nk, nq, nc: hc x d
+layout(push_constant) uniform P {
+ uvec2 kq, res, val, nk, nq, nc, gv, hist, pos;
+ uint d, hc, cap;
+ float eps;
+} p;
+
+shared float sg_part[64];
+
+float wg_sum(float s)
+{
+ float r = subgroupAdd(s);
+ if (subgroupElect())
+ sg_part[gl_SubgroupID] = r;
+ barrier();
+ float tot = sg_part[0];
+ for (uint i = 1u; i < gl_NumSubgroups; i++)
+ tot += sg_part[i];
+ barrier();
+ return tot;
+}
+
+void main()
+{
+ uint t = gl_LocalInvocationID.x, c = gl_WorkGroupID.x, j = gl_WorkGroupID.y;
+ uint d = p.d, hd = p.hc * d, o = j * hd + c * d, wo = c * d;
+ F K = F(p.kq), S = F(p.res), V = F(p.val), NK = F(p.nk), NQ = F(p.nq),
+ NC = F(p.nc);
+ float sk = 0.0, sq = 0.0;
+ for (uint i = t; i < d; i += 256u) {
+ float a = K.v[o + i], b = S.v[o + i];
+ sk += a * a;
+ sq += b * b;
+ }
+ sk = wg_sum(sk);
+ sq = wg_sum(sq);
+ float ik = 1.0 / sqrt(sk / float(d) + p.eps),
+ iq = 1.0 / sqrt(sq / float(d) + p.eps);
+ float dot = 0.0;
+ for (uint i = t; i < d; i += 256u)
+ dot += (K.v[o + i] * ik * NK.v[wo + i]) * (S.v[o + i] * iq * NQ.v[wo + i]);
+ dot = wg_sum(dot);
+ float s = dot / sqrt(float(d));
+ float mag = sqrt(max(abs(s), 1e-6));
+ float gt = 1.0 / (1.0 + exp(-(s > 0.0 ? mag : s < 0.0 ? -mag : 0.0)));
+ W G = W(p.gv);
+ float sg = 0.0;
+ for (uint i = t; i < d; i += 256u) {
+ float g = V.v[j * d + i] * gt;
+ G.v[o + i] = g;
+ sg += g * g;
+ }
+ sg = wg_sum(sg);
+ float ig = 1.0 / sqrt(sg / float(d) + p.eps);
+ uint row = (RP(p.pos).v[0] + j) % p.cap;
+ W H = W(p.hist);
+ for (uint i = t; i < d; i += 256u)
+ H.v[row * hd + c * d + i] = G.v[o + i] * ig * NC.v[wo + i];
+}
diff --git a/src/llm/shaders/q2k_matvec.comp b/src/llm/shaders/q2k_matvec.comp
new file mode 100644
index 0000000..db5ab94
--- /dev/null
+++ b/src/llm/shaders/q2k_matvec.comp
@@ -0,0 +1,93 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+#version 450
+// Q2_K weights times up to NV Q8_K vectors, one row per invocation, as
+// janas_q2k_dot_ref computes them: 84 bytes a block (sixteen bytes of a
+// group of 16's scale and minimum, a nibble each; 64 of two-bit weights,
+// weight e at bits 2 j of byte 32 n + l for e = 128 n + 32 j + l; d and
+// dmin), the block's exact integer sums - the scales times each group's
+// dot, the minima times its activations' sum - each in one fma into an
+// accumulator of its own, with d d8 and dmin d8, the second taken from the
+// first at the end: the CPU's float operations, so the results agree bit
+// for bit. Activations shared across the subgroup as in q4k_matvec.comp.
+#extension GL_EXT_integer_dot_product : require
+#extension GL_KHR_shader_subgroup_basic : require
+#extension GL_KHR_shader_subgroup_ballot : require
+#extension GL_EXT_buffer_reference : require
+#extension GL_EXT_buffer_reference_uvec2 : require
+
+layout(local_size_x = 64) in;
+layout(constant_id = 0) const uint NV = 8;
+layout(constant_id = 1) const uint SG = 8;
+const uint NX = (19u + SG - 1u) / SG;
+
+layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer U1 { uint v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 16) readonly buffer U4 { uvec4 v[]; };
+layout(buffer_reference, std430, buffer_reference_align = 4) writeonly buffer F { float v[]; };
+
+layout(push_constant) uniform P { uvec2 w, x, y; uint rows, nb, nv, xs, ys; } p;
+
+int dp(uint a, uint b) { return dotPacked4x8EXT(int(a), int(b)); }
+int dp4(uvec4 a, uvec4 b) { return dp(a.x, b.x) + dp(a.y, b.y) + dp(a.z, b.z) + dp(a.w, b.w); }
+uvec4 bc(uvec4 xr[NX], uint k) { return subgroupBroadcast(xr[k / SG], k % SG); }
+
+void main()
+{
+ U1 W = U1(p.w);
+ U4 X = U4(p.x);
+ F Y = F(p.y);
+ uint lane = gl_SubgroupInvocationID;
+ uint grow = gl_WorkGroupID.y * 64u + gl_LocalInvocationID.x;
+ uint row = min(grow, p.rows - 1u);
+ uint v0 = gl_WorkGroupID.x * NV;
+ uint xo[NV];
+ for (uint v = 0; v < NV; v++)
+ xo[v] = min(v0 + v, p.nv - 1u) * p.xs * 20u;
+ float acc[NV], accm[NV];
+ for (uint v = 0; v < NV; v++) {
+ acc[v] = 0.0;
+ accm[v] = 0.0;
+ }
+ uint off = row * p.nb * 21u; /* words: 84 bytes a block */
+ for (uint b = 0; b < p.nb; b++, off += 21u) {
+ uint wd[21];
+ for (uint k = 0; k < 21u; k++)
+ wd[k] = W.v[off + k];
+ vec2 dm = unpackHalf2x16(wd[20]);
+ /* group g of 16: four words of its weights, unsigned (0-3), its
+ scale and minimum */
+ uvec4 q[16];
+ int sc[16], mn[16];
+ for (uint g = 0; g < 16u; g++) {
+ uint n = g / 8u, j = (g % 8u) / 2u, l0 = 16u * (g % 2u);
+ uint w0 = 4u + (32u * n + l0) / 4u; /* qs from byte 16 */
+ q[g] = uvec4((wd[w0] >> (2u * j)) & 0x03030303u,
+ (wd[w0 + 1u] >> (2u * j)) & 0x03030303u,
+ (wd[w0 + 2u] >> (2u * j)) & 0x03030303u,
+ (wd[w0 + 3u] >> (2u * j)) & 0x03030303u);
+ uint s = (wd[g / 4u] >> (8u * (g % 4u))) & 0xffu;
+ sc[g] = int(s & 15u);
+ mn[g] = int(s >> 4);
+ }
+ for (uint v = 0; v < NV; v++) {
+ uvec4 xr[NX];
+ for (uint j = 0; j < NX; j++)
+ xr[j] = X.v[xo[v] + min(j * SG + lane, 19u)];
+ uvec4 b0 = bc(xr, 16u), b1 = bc(xr, 17u);
+ uint bw[8] = uint[8](b0.x, b0.y, b0.z, b0.w, b1.x, b1.y, b1.z, b1.w);
+ int sumi = 0, summ = 0;
+ for (uint g = 0; g < 16u; g++) {
+ sumi += sc[g] * dp4(q[g], bc(xr, g));
+ summ += mn[g] * bitfieldExtract(int(bw[g / 2u]), 16 * int(g % 2u), 16);
+ }
+ float d8 = uintBitsToFloat(bc(xr, 18u).x);
+ acc[v] = fma(dm.x * d8, float(sumi), acc[v]);
+ accm[v] = fma(dm.y * d8, float(summ), accm[v]);
+ xo[v] += 20u;
+ }
+ }
+ if (grow < p.rows)
+ for (uint v = 0; v < NV; v++)
+ if (v0 + v < p.nv)
+ Y.v[(v0 + v) * p.ys + row] = acc[v] - accm[v];
+}
diff --git a/src/llm/shaders/q8_0_matvec.comp b/src/llm/shaders/q8_0_matvec.comp
index abfcb67..ce9e400 100644
--- a/src/llm/shaders/q8_0_matvec.comp
+++ b/src/llm/shaders/q8_0_matvec.comp
@@ -11,7 +11,10 @@
// come in the 32-block layout of convert_x (20 uvec4 a Q8_K block: qs32 in
// 16, bsums32 in 2, d32 in 2), shared across the subgroup as in
// q4k_matvec.comp. A weight block is 34 bytes, so every other one starts 2
-// bytes off a 4-byte boundary: its words are realigned.
+// bytes off a 4-byte boundary: its words are realigned. A row may end
+// inside a group of 256 (tail, an even number of blocks of 32 past the whole
+// groups, so that rows start on words): as janas_dot_rows_n, the whole
+// groups' sum, then the tail's from zero, padded with zero blocks, added.
#extension GL_EXT_integer_dot_product : require
#extension GL_KHR_shader_subgroup_basic : require
#extension GL_KHR_shader_subgroup_ballot : require
@@ -27,7 +30,12 @@ layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer U1
layout(buffer_reference, std430, buffer_reference_align = 16) readonly buffer U4 { uvec4 v[]; };
layout(buffer_reference, std430, buffer_reference_align = 4) writeonly buffer F { float v[]; };
-layout(push_constant) uniform P { uvec2 w, x, y; uint rows, nb, nv, xs, ys; } p;
+layout(push_constant) uniform P {
+ uvec2 w, x, y;
+ uint rows, nb, nv, xs, ys, level;
+ uvec2 p1, p0;
+ uint tail; /* blocks of 32 past the whole groups: 0, 2, 4 or 6 */
+} p;
int dp(uint a, uint b) { return dotPacked4x8EXT(int(a), int(b)); }
int dp4(uvec4 a, uvec4 b) { return dp(a.x, b.x) + dp(a.y, b.y) + dp(a.z, b.z) + dp(a.w, b.w); }
@@ -48,15 +56,32 @@ void main()
uint xo[NV];
for (uint v = 0; v < NV; v++)
xo[v] = min(v0 + v, p.nv - 1u) * p.xs * 20u;
- float acc[NV][8];
- for (uint v = 0; v < NV; v++)
+ float acc[NV][8], res[NV];
+ for (uint v = 0; v < NV; v++) {
+ res[v] = 0.0;
for (uint k = 0; k < 8u; k++)
acc[v][k] = 0.0;
- uint gw = row * p.nb * 68u; /* words: a group of eight blocks is 272 bytes */
- for (uint b = 0; b < p.nb; b++, gw += 68u) {
+ }
+ /* words: a group of eight blocks is 272 bytes, the tail 34 a block */
+ uint tw = p.tail * 34u / 4u;
+ uint gw = row * (p.nb * 68u + tw);
+ for (uint b = 0; b <= p.nb; b++, gw += 68u) {
+ bool last = b == p.nb;
+ if (last && p.tail == 0u)
+ break;
+ uint nk = last ? p.tail : 8u, nw = last ? tw : 68u;
uint wg[68];
for (uint i = 0; i < 68u; i++)
- wg[i] = W.v[gw + i];
+ wg[i] = i < nw ? W.v[gw + i] : 0u;
+ if (last)
+ /* the whole groups' sum aside, the tail's from zero */
+ for (uint v = 0; v < NV; v++) {
+ if (p.nb > 0u)
+ res[v] = ((acc[v][0] + acc[v][4]) + (acc[v][2] + acc[v][6])) +
+ ((acc[v][1] + acc[v][5]) + (acc[v][3] + acc[v][7]));
+ for (uint k = 0; k < 8u; k++)
+ acc[v][k] = 0.0;
+ }
float d[8];
uvec4 q[8][2];
for (uint k = 0; k < 8u; k++) {
@@ -82,15 +107,20 @@ void main()
uintBitsToFloat(s1.z), uintBitsToFloat(s1.w));
for (uint k = 0; k < 8u; k++) {
int dot = dp4(q[k][0], bc(xr, 2u * k)) + dp4(q[k][1], bc(xr, 2u * k + 1u));
- acc[v][k] = fma(d[k] * d32[k], float(dot), acc[v][k]);
+ /* the tail's empty blocks: the CPU's zero blocks add 0 */
+ if (k < nk)
+ acc[v][k] = fma(d[k] * d32[k], float(dot), acc[v][k]);
}
xo[v] += 20u;
}
}
if (grow < p.rows)
for (uint v = 0; v < NV; v++)
- if (v0 + v < p.nv)
- Y.v[(v0 + v) * p.ys + row] =
- ((acc[v][0] + acc[v][4]) + (acc[v][2] + acc[v][6])) +
- ((acc[v][1] + acc[v][5]) + (acc[v][3] + acc[v][7]));
+ if (v0 + v < p.nv) {
+ float r = ((acc[v][0] + acc[v][4]) + (acc[v][2] + acc[v][6])) +
+ ((acc[v][1] + acc[v][5]) + (acc[v][3] + acc[v][7]));
+ if (p.tail != 0u)
+ r = res[v] + r;
+ Y.v[(v0 + v) * p.ys + row] = r;
+ }
}
diff --git a/src/llm/tools.c b/src/llm/tools.c
index d36663b..f53a8b1 100644
--- a/src/llm/tools.c
+++ b/src/llm/tools.c
@@ -654,14 +654,33 @@ void janas_tools_system(struct janas_buf *out, enum janas_tool_dialect d,
static uint32_t g_value_rule(struct janas_gbuild *b, int32_t quote);
+/* Whether function i is one a call may name: `only`, or with a prefix those
+ whose names begin with it, or all where none does. */
+static int callable(const struct janas_toolset *t, uint32_t i, int only,
+ const char *prefix, size_t prefix_n)
+{
+ if (only >= 0)
+ return (uint32_t)only == i;
+ if (!prefix_n)
+ return 1;
+ int any = 0;
+ for (uint32_t k = 0; k < t->n && !any; k++) {
+ const struct janas_json *nm = janas_json_get(t->fn[k], "name");
+ any = nm->n >= prefix_n && memcmp(nm->s, prefix, prefix_n) == 0;
+ }
+ const struct janas_json *nm = janas_json_get(t->fn[i], "name");
+ return !any || (nm->n >= prefix_n && memcmp(nm->s, prefix, prefix_n) == 0);
+}
+
/* The rule of a Gemma call: call:name{members} then the closing token. */
uint32_t janas_tools_rule_gemma(struct janas_gbuild *b,
const struct janas_toolset *t, int only,
+ const char *prefix, size_t prefix_n,
int32_t close, int32_t quote)
{
uint32_t fns = janas_gb_rule(b);
for (uint32_t i = 0; i < t->n; i++) {
- if (only >= 0 && (uint32_t)only != i)
+ if (!callable(t, i, only, prefix, prefix_n))
continue;
const struct janas_json *nm = janas_json_get(t->fn[i], "name");
janas_gb_lit(b, janas_gb_alt(b, fns), nm->s, nm->n);
@@ -1102,12 +1121,12 @@ static uint32_t g_value_rule(struct janas_gbuild *b, int32_t quote)
uint32_t janas_tools_rule(struct janas_gbuild *b, struct janas_jsg *j,
enum janas_tool_dialect d,
const struct janas_toolset *t, int only,
- int32_t close)
+ const char *prefix, size_t prefix_n, int32_t close)
{
uint32_t ws = janas_jsg_ws(j);
uint32_t fns = janas_gb_rule(b); /* one alternative per function */
for (uint32_t i = 0; i < t->n; i++) {
- if (only >= 0 && (uint32_t)only != i)
+ if (!callable(t, i, only, prefix, prefix_n))
continue;
const struct janas_json *nm = janas_json_get(t->fn[i], "name");
uint32_t a = janas_gb_alt(b, fns);
diff --git a/src/llm/tools.h b/src/llm/tools.h
index 3a49455..1f46dc8 100644
--- a/src/llm/tools.h
+++ b/src/llm/tools.h
@@ -88,6 +88,7 @@ int janas_tools_parse(enum janas_tool_dialect d, const struct janas_toolset *t,
/* Gemma 4's call rule: the quote token <|"|> too. */
uint32_t janas_tools_rule_gemma(struct janas_gbuild *b,
const struct janas_toolset *t, int only,
+ const char *prefix, size_t prefix_n,
int32_t close, int32_t quote);
/* Gemma 4: a tool's answer as its template writes it, without the opening
@@ -98,9 +99,11 @@ uint32_t janas_tools_rule_gemma(struct janas_gbuild *b,
void janas_tools_gemma_response(struct janas_buf *out, const char *name,
size_t name_n, const char *text, size_t n);
+/* With prefix (prefix_n bytes, 0: none), only the functions whose names
+ begin with it - all of them where none does. */
uint32_t janas_tools_rule(struct janas_gbuild *b, struct janas_jsg *j,
enum janas_tool_dialect d,
const struct janas_toolset *t, int only,
- int32_t close);
+ const char *prefix, size_t prefix_n, int32_t close);
#endif
diff --git a/tests/bench_long.c b/tests/bench_long.c
index fd9cb0e..ce1ccd2 100644
--- a/tests/bench_long.c
+++ b/tests/bench_long.c
@@ -97,7 +97,7 @@ static void report_phases(const char *what, const double *a, const double *b,
uint32_t n)
{
static const char *names[JANAS_PH_COUNT] = {
- "qkv", "attention", "wo", "router", "experts", "output", "ssm"};
+ "qkv", "attention", "wo", "router", "experts", "output", "ssm", "mix"};
double total = 0;
printf("%s, ms/token:", what);
for (int i = 0; i < JANAS_PH_COUNT; i++) {
diff --git a/tests/gpu_check.c b/tests/gpu_check.c
index 3ed58d9..6244f32 100644
--- a/tests/gpu_check.c
+++ b/tests/gpu_check.c
@@ -3,10 +3,18 @@
/*
* gpu_check.c - products split between the GPU and the CPU must equal the
* CPU's bit for bit: random Q4_K, Q5_K, Q6_K, Q6_K_P (bit planes, at its
- * three levels), Q8_0 and the blocks of 32 (Q4_0, IQ4_NL, Q4_1, Q5_1, MXFP4)
- * matrices in shared memory, and the GPU's aligned copy of Q6_K (on the GPU
- * alone, against the CPU's Q6_K), several shapes, vector
- * counts and strides. Skips (exit 0) when no GPU is usable.
+ * three levels), Q8_0 and the blocks of 32 or 64 (Q4_0, IQ4_NL, Q4_1, Q5_1,
+ * MXFP4, Q5_0, Q2_0) matrices in shared memory, and the GPU's aligned copy
+ * of Q6_K (on the GPU alone, against the CPU's Q6_K), several shapes, vector
+ * counts and strides; for the blocks of 32 or 64 and Q8_0 also rows that
+ * end inside a group of 256 (qwen4exp's 640, its shared experts' down
+ * matrices Q8_0); Q2_K, IQ2_XXS, IQ2_XS, IQ2_S, IQ3_XXS,
+ * IQ1_S and IQ1_M (Qwen3.8-Flash-Next's other files, unsloth's 1- and 2-bit
+ * ones). Then bf16 weights on float vectors
+ * (janas_gpu_bf16_mv) against the CPU's eight lanes (bf16.h), with x and y
+ * where the GPU shares them and through its stages (JANAS_GPU_COPY=1: the
+ * weights too, as on a card with memory of its own). Skips (exit 0) when
+ * no GPU is usable.
*
* Usage: gpu_check
*/
@@ -16,6 +24,7 @@
#include <time.h>
#include "common/aligned.h"
+#include "llm/bf16.h"
#include "llm/gpu.h"
#include "llm/matvec.h"
#include "llm/quant.h"
@@ -117,11 +126,14 @@ int main(void)
w8b[b].d = janas_fp32_to_fp16(0.0001f + (rnd() % 1000) * 1e-6f);
/* blocks of 32 with four or five bits: Q4_0 and IQ4_NL (18 bytes: d,
16 of nibbles), Q4_1 (20: d, m, ...), Q5_1 (24: d, m, qh, ...), MXFP4
- (17: the E8M0 byte, 16 of nibbles); random bytes with d (and m) set
- to small scales, MXFP4's byte to powers of two from 2^-10 to 2^-3 (a
- subnormal one, under 2^-125, a GPU may flush) */
- static const int t32[5] = {JANAS_Q4_0, JANAS_IQ4_NL, JANAS_Q4_1, JANAS_Q5_1,
- JANAS_MXFP4};
+ (17: the E8M0 byte, 16 of nibbles), Q5_0 (22: d, qh, ...), and Q2_0's
+ blocks of 64 (18: d, two bits a weight; 9 bytes for 32 below); random
+ bytes with d (and m) set to small scales, MXFP4's byte to powers of
+ two from 2^-10 to 2^-3 (a subnormal one, under 2^-125, a GPU may
+ flush) */
+ static const int t32[7] = {JANAS_Q4_0, JANAS_IQ4_NL, JANAS_Q4_1,
+ JANAS_Q5_1, JANAS_MXFP4, JANAS_Q5_0,
+ JANAS_Q2_0};
/* Q3_K (d at byte 108) and IQ3_S (d at 0): 110-byte blocks */
uint8_t *w3[2];
size_t w3bytes = (size_t)MAXR * (MAXC / JANAS_QK) * 110 + 4096;
@@ -145,29 +157,69 @@ int main(void)
uint16_t d = janas_fp32_to_fp16(0.00005f + (rnd() % 1000) * 1e-7f);
memcpy(wx + b, &d, 2);
}
- static const size_t s32[5] = {18, 18, 20, 24, 17};
- uint8_t *w32[5];
- size_t w32bytes[5];
- for (int k = 0; k < 5; k++) {
+ static const size_t s32[7] = {18, 18, 20, 24, 17, 22, 9};
+ uint8_t *w32[7];
+ size_t w32bytes[7];
+ for (int k = 0; k < 7; k++) {
w32bytes[k] = (size_t)MAXR * (MAXC / JANAS_QK) * 8 * s32[k];
w32bytes[k] = (w32bytes[k] + 4095) / 4096 * 4096;
w32[k] = janas_aligned_alloc(4096, w32bytes[k]);
for (size_t i = 0; i < w32bytes[k]; i++)
w32[k][i] = (uint8_t)rnd();
- for (size_t b = 0; b + s32[k] <= w32bytes[k]; b += s32[k]) {
+ size_t step = t32[k] == JANAS_Q2_0 ? 18 : s32[k];
+ for (size_t b = 0; b + step <= w32bytes[k]; b += step) {
if (t32[k] == JANAS_MXFP4) {
w32[k][b] = (uint8_t)(118 + rnd() % 8);
continue;
}
uint16_t d = janas_fp32_to_fp16(0.0005f + (rnd() % 1000) * 1e-6f);
memcpy(w32[k] + b, &d, 2);
- if (s32[k] > 18) {
+ if (t32[k] == JANAS_Q4_1 || t32[k] == JANAS_Q5_1) {
uint16_t m =
janas_fp32_to_fp16(-0.01f + (rnd() % 1000) * 2e-5f);
memcpy(w32[k] + b + 2, &m, 2);
}
}
}
+ /* the 256-block types of iq2, iq3xxs, iq1 and q2k_matvec.comp: random
+ bytes, the scales small - d at byte 0, Q2_K's d and dmin at 80 and
+ 82, IQ1_M's d in the top nibbles of its four scale words (48-55) */
+ static const int tq[7] = {JANAS_Q2_K, JANAS_IQ2_XXS, JANAS_IQ2_XS,
+ JANAS_IQ2_S, JANAS_IQ3_XXS, JANAS_IQ1_S,
+ JANAS_IQ1_M};
+ uint8_t *wq[7];
+ size_t wqbytes[7];
+ for (int k = 0; k < 7; k++) {
+ size_t bs = janas_qtype_block_size(tq[k]);
+ wqbytes[k] = (size_t)MAXR * (MAXC / JANAS_QK) * bs + 4096;
+ wqbytes[k] = (wqbytes[k] + 4095) / 4096 * 4096;
+ wq[k] = janas_aligned_alloc(4096, wqbytes[k]);
+ for (size_t i = 0; i < wqbytes[k]; i++)
+ wq[k][i] = (uint8_t)rnd();
+ for (size_t b = 0; b + bs <= wqbytes[k] - 4096; b += bs) {
+ uint16_t d = janas_fp32_to_fp16(0.0005f + (rnd() % 1000) * 1e-6f);
+ if (tq[k] == JANAS_Q2_K) {
+ uint16_t dm =
+ janas_fp32_to_fp16(0.0002f + (rnd() % 1000) * 1e-6f);
+ memcpy(wq[k] + b + 80, &d, 2);
+ memcpy(wq[k] + b + 82, &dm, 2);
+ } else if (tq[k] == JANAS_IQ1_M) {
+ for (int s = 0; s < 4; s++) {
+ uint16_t sc;
+ memcpy(&sc, wq[k] + b + 48 + 2 * s, 2);
+ sc = (uint16_t)((sc & 0x0fff) | ((d >> 4 * s & 15) << 12));
+ memcpy(wq[k] + b + 48 + 2 * s, &sc, 2);
+ }
+ } else {
+ memcpy(wq[k] + b, &d, 2);
+ }
+ }
+ }
+ for (int k = 0; k < 7; k++)
+ if (janas_gpu_import(g, wq[k], wqbytes[k]) != 0) {
+ printf("gpu_check: import failed\n");
+ return 1;
+ }
if (janas_gpu_import(g, w, wbytes) != 0 ||
janas_gpu_import(g, w6, w6bytes) != 0 ||
janas_gpu_import(g, wa, wabytes) != 0 ||
@@ -179,6 +231,8 @@ int main(void)
janas_gpu_import(g, w32[2], w32bytes[2]) != 0 ||
janas_gpu_import(g, w32[3], w32bytes[3]) != 0 ||
janas_gpu_import(g, w32[4], w32bytes[4]) != 0 ||
+ janas_gpu_import(g, w32[5], w32bytes[5]) != 0 ||
+ janas_gpu_import(g, w32[6], w32bytes[6]) != 0 ||
janas_gpu_import(g, wx, wxbytes) != 0 ||
janas_gpu_import(g, w3[0], w3bytes) != 0 ||
janas_gpu_import(g, w3[1], w3bytes) != 0) {
@@ -203,45 +257,67 @@ int main(void)
janas_pin_current_thread(0);
size_t ysz = (size_t)MAXR * MAXV * 2;
float *ya = malloc(ysz * sizeof(float)), *yb = malloc(ysz * sizeof(float));
+ /* the last three: rows ending inside a group of 256, for the blocks
+ of 32 or 64 and Q8_0 only (640: qwen4exp's experts' down
+ matrices) */
const size_t shapes[][2] = {{4096, 2048}, {1000, 256}, {128, 4096},
{2048, 512}, {4096, 4096}, {777, 1024},
- {1500, 768}}; /* odd blocks per row */
+ {1500, 768}, /* odd blocks per row */
+ {2560, 640}, {300, 896}, {77, 128}};
const size_t nvs[] = {1, 2, 3, 5, 8, 9, 16, 17, 33, 64};
int bad = 0, runs = 0;
/* 0 Q4_K, 1 Q6_K, 2-4 Q6_K_P with three, two and one planes, 5 Q5_K,
6 Q8_0, 7-10 Q4_0, IQ4_NL, Q4_1, Q5_1, 11 IQ4_XS,
- 12 Q3_K, 13 IQ3_S, 14 the aligned Q6_K, 15 MXFP4 */
- static const char *names[] = {"Q4_K", "Q6_K", "Q6_K_P/3", "Q6_K_P/2",
- "Q6_K_P/1", "Q5_K", "Q8_0", "Q4_0",
- "IQ4_NL", "Q4_1", "Q5_1", "IQ4_XS",
- "Q3_K", "IQ3_S", "Q6_K_A", "MXFP4"};
- for (int q6 = 0; q6 < 16; q6++)
+ 12 Q3_K, 13 IQ3_S, 14 the aligned Q6_K, 15 MXFP4, 16 Q5_0, 17 Q2_0,
+ 18-24 Q2_K, IQ2_XXS, IQ2_XS, IQ2_S, IQ3_XXS, IQ1_S, IQ1_M */
+ static const char *names[] = {
+ "Q4_K", "Q6_K", "Q6_K_P/3", "Q6_K_P/2", "Q6_K_P/1",
+ "Q5_K", "Q8_0", "Q4_0", "IQ4_NL", "Q4_1",
+ "Q5_1", "IQ4_XS", "Q3_K", "IQ3_S", "Q6_K_A",
+ "MXFP4", "Q5_0", "Q2_0", "Q2_K", "IQ2_XXS",
+ "IQ2_XS", "IQ2_S", "IQ3_XXS", "IQ1_S", "IQ1_M"};
+ for (int q6 = 0; q6 < 25; q6++)
for (size_t s = 0; s < sizeof(shapes) / sizeof(shapes[0]); s++)
for (size_t k = 0; k < sizeof(nvs) / sizeof(nvs[0]); k++)
for (int strided = 0; strided < 2; strided++) {
+ /* the blocks of 32 or 64: their table's index */
+ int b32 = q6 >= 7 && q6 <= 10 ? q6 - 7
+ : q6 == 15 ? 4
+ : q6 == 16 || q6 == 17 ? q6 - 11
+ : -1;
+ int iq = q6 >= 18 ? q6 - 18 : -1; /* the 256-block kinds */
size_t rows = shapes[s][0], cols = shapes[s][1];
- size_t nb = cols / JANAS_QK, nv = nvs[k];
+ if (cols % JANAS_QK && b32 < 0 && q6 != 6)
+ continue; /* a tail: blocks of 32 or 64, Q8_0 */
+ /* the activations of a tail row: its group whole */
+ size_t nb = cols / JANAS_QK, nv = nvs[k],
+ nbx = (cols + JANAS_QK - 1) / JANAS_QK;
size_t skip = s % 2; /* a row further in, every other
shape */
+ size_t rb32 =
+ b32 >= 0 ? janas_qtype_row_bytes(t32[b32], cols) : 0;
struct janas_matvec_task t = {
- .type = q6 == 15 ? JANAS_MXFP4
+ .type = iq >= 0 ? tq[iq]
+ : b32 >= 0 ? t32[b32]
: q6 == 14 ? JANAS_Q6_K
: q6 == 13 ? JANAS_IQ3_S
: q6 == 12 ? JANAS_Q3_K
: q6 == 11 ? JANAS_IQ4_XS
- : q6 >= 7 ? t32[q6 - 7]
: q6 == 6 ? JANAS_Q8_0
: q6 == 5 ? JANAS_Q5_K
: q6 >= 2 ? JANAS_Q6_K_P
: q6 == 1 ? JANAS_Q6_K
: JANAS_Q4_K,
- .w = q6 == 15 ? w32[4] + skip * 8 * s32[4] * nb
+ .w = iq >= 0
+ ? wq[iq] + skip *
+ janas_qtype_block_size(tq[iq]) *
+ nb
+ : b32 >= 0 ? w32[b32] + skip * rb32
: q6 == 14 ? w6 + skip * 210 * nb
: q6 >= 12 ? w3[q6 - 12] + skip * 110 * nb
: q6 == 11 ? wx + skip * 136 * nb
- : q6 >= 7
- ? w32[q6 - 7] + skip * 8 * s32[q6 - 7] * nb
- : q6 == 6 ? w8 + skip * 272 * nb
+ : q6 == 6 ? w8 + skip * janas_qtype_row_bytes(
+ JANAS_Q8_0, cols)
: q6 == 5 ? w5 + skip * 176 * nb
: q6 >= 2 ? wp + skip * 82 * nb
: q6 == 1 ? w6 + skip * 210 * nb
@@ -255,7 +331,7 @@ int main(void)
.rows = rows,
.cols = cols,
.n_vec = nv,
- .x_stride = strided ? 2 * nb : 0,
+ .x_stride = strided ? 2 * nbx : 0,
.y_stride = strided ? rows + 5 : 0};
size_t ys = strided ? rows + 5 : rows;
memset(ya, 0, ysz * sizeof(float));
@@ -320,10 +396,84 @@ int main(void)
break;
}
}
- printf("gpu_check (%s, subgroups of %u): %d shapes, %s\n",
- janas_gpu_name(g), janas_gpu_subgroup(g), runs,
- bad ? "FAILED" : "ok");
+ /* bf16 products: Qwen3.8-Flash-Next's mixes (10240 wide, 320 wide), a
+ tail of three past the last eight, a row shorter than eight; single
+ vectors too (as on a card with memory of its own) */
+ int b16_runs = 0, b16_refused = 0;
+ /* freed after the GPU, which imported them */
+ uint16_t *w16 = NULL;
+ float *bx = NULL, *by = NULL;
+ if (janas_bf16_lanes()) {
+ static const size_t bsh[][2] = {{320, 10240}, {10240, 320},
+ {2560, 2560}, {512, 2560},
+ {100, 1003}, {37, 13}};
+ static const size_t bnv[] = {1, 2, 3, 4, 5, 8, 17, 63};
+ enum { BW = 2560 * 2560, BX = 63 * 10240 };
+ size_t xbytes = ((size_t)BX * sizeof(float) + 4095) / 4096 * 4096;
+ w16 = janas_aligned_alloc(4096, (size_t)BW * 2);
+ bx = janas_aligned_alloc(4096, xbytes);
+ by = janas_aligned_alloc(4096, xbytes);
+ float *bxs = malloc(xbytes), *bys = malloc(xbytes),
+ *bref = malloc(xbytes);
+ for (size_t i = 0; i < BW; i++) {
+ float f = (float)((int)(rnd() % 20001) - 10000) * 1e-5f;
+ uint32_t u;
+ memcpy(&u, &f, 4);
+ w16[i] = (uint16_t)(u >> 16);
+ }
+ for (size_t i = 0; i < BX; i++)
+ bx[i] = bxs[i] = (float)((int)(rnd() % 2001) - 1000) / 500.0f;
+ janas_gpu_set_min_n(g, 1); /* single vectors too */
+ if (janas_gpu_import(g, w16, (size_t)BW * 2) != 0 ||
+ janas_gpu_import(g, bx, xbytes) != 0 ||
+ janas_gpu_import(g, by, xbytes) != 0) {
+ printf("gpu_check: bf16 import failed\n");
+ bad++;
+ }
+ for (size_t s = 0; !bad && s < sizeof(bsh) / sizeof(*bsh); s++)
+ for (size_t k = 0; k < sizeof(bnv) / sizeof(*bnv); k++)
+ for (int staged = 0; staged < 2; staged++) {
+ size_t rows = bsh[s][0], cols = bsh[s][1], nv = bnv[k];
+ float scale = staged ? 0.25f : 1.0f;
+ const float *xin = staged ? bxs : bx;
+ float *yout = staged ? bys : by;
+ for (size_t v = 0; v < nv; v++)
+ for (size_t r = 0; r < rows; r++)
+ bref[v * rows + r] =
+ janas_bf16_dot(w16 + r * cols, xin + v * cols,
+ (uint32_t)cols) *
+ scale;
+ memset(yout, 0, nv * rows * sizeof(float));
+ if (janas_gpu_bf16_mv(g, w16, xin, yout, rows, cols, nv,
+ scale) != 0) {
+ b16_refused++;
+ continue;
+ }
+ b16_runs++;
+ size_t nbad = 0;
+ for (size_t i = 0; i < nv * rows; i++)
+ nbad += memcmp(&bref[i], &yout[i], 4) != 0;
+ if (nbad && bad < 10) {
+ printf("[%zu wrong of %zu] bf16 %zux%zu nv %zu %s: "
+ "%g vs %g\n",
+ nbad, nv * rows, rows, cols, nv,
+ staged ? "through the stages" : "in place",
+ bref[0], yout[0]);
+ bad++;
+ }
+ }
+ free(bxs);
+ free(bys);
+ free(bref);
+ }
+ printf("gpu_check (%s, subgroups of %u): %d shapes, bf16 %d (%d "
+ "refused), %s\n",
+ janas_gpu_name(g), janas_gpu_subgroup(g), runs, b16_runs,
+ b16_refused, bad || b16_refused ? "FAILED" : "ok");
janas_gpu_destroy(g);
+ janas_aligned_free(w16);
+ janas_aligned_free(bx);
+ janas_aligned_free(by);
janas_pool_destroy(pool);
janas_aligned_free(w);
janas_aligned_free(w6);
@@ -331,8 +481,10 @@ int main(void)
janas_aligned_free(wp);
janas_aligned_free(w5);
janas_aligned_free(w8);
- for (int k = 0; k < 5; k++)
+ for (int k = 0; k < 7; k++)
janas_aligned_free(w32[k]);
+ for (int k = 0; k < 7; k++)
+ janas_aligned_free(wq[k]);
janas_aligned_free(wx);
janas_aligned_free(w3[0]);
janas_aligned_free(w3[1]);
@@ -340,5 +492,5 @@ int main(void)
free(xf);
free(ya);
free(yb);
- return bad ? 1 : 0;
+ return bad || b16_refused ? 1 : 0;
}
diff --git a/tests/gpu_fast_check.c b/tests/gpu_fast_check.c
new file mode 100644
index 0000000..5fed284
--- /dev/null
+++ b/tests/gpu_fast_check.c
@@ -0,0 +1,312 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ Copyright (C) 2026 Maurizio Cammalleri */
+/*
+ * gpu_fast_check.c - the fast prompt's products of the types in blocks of
+ * 32 or 64 (iq4nl_mmq.comp: IQ4_NL, MXFP4, Q5_0, Q2_0) and a mixture's
+ * experts in them (b32_mmid.comp: IQ4_NL, Q5_0, Q2_0), rows ending inside
+ * a group of 256 among them (qwen4exp's 640), and experts in the 256-block
+ * types of iqk_mmid.comp (Q2_K, IQ2_XXS, IQ2_XS, IQ2_S, IQ3_XXS, IQ1_S,
+ * IQ1_M, IQ3_S, IQ4_XS), against the CPU. They round
+ * group by group, so not bit for bit: each output within 1e-4 of the
+ * largest of its product, and the GPU seen at work (outputs that differ in
+ * their last bits). Skips (exit 0) when no GPU is usable.
+ *
+ * Usage: gpu_fast_check
+ */
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "common/aligned.h"
+#include "llm/gpu.h"
+#include "llm/matvec.h"
+#include "llm/quant.h"
+
+static uint64_t rng = 0x2545F4914F6CDD1Dull;
+
+static uint32_t rnd(void)
+{
+ rng ^= rng << 13;
+ rng ^= rng >> 7;
+ rng ^= rng << 17;
+ return (uint32_t)rng;
+}
+
+/* bytes of a block, and the weights it holds */
+static size_t block_bytes(int type, size_t *weights)
+{
+ /* the 256-block types: their row bytes of 256 */
+ *weights = type == JANAS_Q2_0 ? 64
+ : janas_qtype_row_bytes(type, 32) ? 32
+ : 256;
+ return janas_qtype_row_bytes(type, *weights);
+}
+
+/* rows x cols weights of a type, random bytes with small scales */
+static void fill(uint8_t *w, int type, size_t rows, size_t cols)
+{
+ size_t per, bs = block_bytes(type, &per);
+ size_t n = rows * (cols / per);
+ for (size_t i = 0; i < n * bs; i++)
+ w[i] = (uint8_t)rnd();
+ for (size_t b = 0; b < n; b++) {
+ uint8_t *blk = w + b * bs;
+ if (type == JANAS_MXFP4) {
+ blk[0] = (uint8_t)(118 + rnd() % 8);
+ continue;
+ }
+ uint16_t d = janas_fp32_to_fp16(0.0005f + (rnd() % 1000) * 1e-6f);
+ if (type == JANAS_Q2_K) { /* d and dmin at 80 and 82 */
+ uint16_t dm = janas_fp32_to_fp16(0.0002f + (rnd() % 1000) * 1e-6f);
+ memcpy(blk + 80, &d, 2);
+ memcpy(blk + 82, &dm, 2);
+ } else if (type == JANAS_IQ1_M) { /* d in the scales' top nibbles */
+ for (int s = 0; s < 4; s++) {
+ uint16_t sc;
+ memcpy(&sc, blk + 48 + 2 * s, 2);
+ sc = (uint16_t)((sc & 0x0fff) | ((d >> 4 * s & 15) << 12));
+ memcpy(blk + 48 + 2 * s, &sc, 2);
+ }
+ } else {
+ memcpy(blk, &d, 2);
+ }
+ }
+}
+
+/* nv activation vectors of cols values, xs blocks apart, zero past cols
+ in their last group (as the engine's) */
+static void acts(struct janas_block_q8k *x, size_t nv, size_t cols, size_t xs)
+{
+ size_t full = (cols + JANAS_QK - 1) / JANAS_QK * JANAS_QK;
+ float *f = calloc(full, sizeof(float));
+ for (size_t v = 0; v < nv; v++) {
+ memset(f, 0, full * sizeof(float));
+ for (size_t i = 0; i < cols; i++)
+ f[i] = (float)((int)(rnd() % 2001) - 1000) / 500.0f;
+ janas_q8k_quantize(f, x + v * xs, (uint32_t)full);
+ }
+ free(f);
+}
+
+/* the outputs of a against those of b: the largest difference over the
+ largest magnitude, and how many differ at all */
+static double compare(const float *a, const float *b, size_t n, size_t *diff)
+{
+ double big = 1e-30, worst = 0;
+ for (size_t i = 0; i < n; i++)
+ if (fabs(a[i]) > big)
+ big = fabs(a[i]);
+ for (size_t i = 0; i < n; i++) {
+ double d = fabs((double)a[i] - b[i]);
+ if (d > worst)
+ worst = d;
+ *diff += memcmp(&a[i], &b[i], 4) != 0;
+ }
+ return worst / big;
+}
+
+static void *page_alloc(size_t bytes)
+{
+ bytes = (bytes + 4095) / 4096 * 4096;
+ void *p = janas_aligned_alloc(4096, bytes);
+ if (p)
+ memset(p, 0, bytes);
+ return p;
+}
+
+int main(void)
+{
+ char err[256];
+ struct janas_gpu *g = janas_gpu_create(err, sizeof(err));
+ if (!g) {
+ printf("gpu_fast_check: no GPU (%s), skipped\n", err);
+ return 0;
+ }
+ if (!janas_gpu_fast_prompt()) {
+ printf("gpu_fast_check: the fast prompt is off, skipped\n");
+ janas_gpu_destroy(g);
+ return 0;
+ }
+ struct janas_pool *pool = janas_pool_create(4, NULL);
+ /* and the 256-block types of iqk_mmq.comp (only the shapes of whole
+ groups of 256) */
+ static const int types[] = {JANAS_Q5_0, JANAS_Q2_0, JANAS_IQ4_NL,
+ JANAS_MXFP4, JANAS_Q2_K, JANAS_IQ2_XXS,
+ JANAS_IQ2_XS, JANAS_IQ2_S, JANAS_IQ3_XXS,
+ JANAS_IQ1_S, JANAS_IQ1_M};
+ static const char *names[] = {"Q5_0", "Q2_0", "IQ4_NL", "MXFP4",
+ "Q2_K", "IQ2_XXS", "IQ2_XS", "IQ2_S",
+ "IQ3_XXS", "IQ1_S", "IQ1_M"};
+ /* rows x cols; the last three end inside a group of 256 */
+ static const size_t shapes[][2] = {
+ {512, 2560}, {640, 2560}, {2560, 640}, {300, 896}, {130, 128}};
+ static const size_t nvs[] = {64, 100, 256};
+ enum { MAXW = 32 << 20, MAXX = 1024 * 10, MAXY = 2560 * 1024 };
+ uint8_t *w = page_alloc(MAXW);
+ struct janas_block_q8k *x = page_alloc(sizeof(*x) * MAXX);
+ float *ya = page_alloc(sizeof(float) * MAXY),
+ *yb = page_alloc(sizeof(float) * MAXY);
+ if (!w || !x || !ya || !yb || janas_gpu_import(g, w, MAXW) != 0 ||
+ janas_gpu_import(g, ya, (sizeof(float) * MAXY + 4095) / 4096 * 4096) !=
+ 0 ||
+ janas_gpu_import(g, yb, (sizeof(float) * MAXY + 4095) / 4096 * 4096) !=
+ 0) {
+ printf("gpu_fast_check: memory or import failed\n");
+ return 1;
+ }
+ int bad = 0, runs = 0;
+ double worst_all = 0;
+ for (size_t ti = 0; ti < sizeof(types) / sizeof(*types); ti++)
+ for (size_t s = 0; s < sizeof(shapes) / sizeof(*shapes); s++)
+ for (size_t k = 0; k < sizeof(nvs) / sizeof(*nvs); k++) {
+ int type = types[ti];
+ size_t rows = shapes[s][0], cols = shapes[s][1], nv = nvs[k];
+ size_t xs = (cols + JANAS_QK - 1) / JANAS_QK;
+ if (janas_qtype_row_bytes(type, cols) == 0 ||
+ janas_qtype_row_bytes(type, cols) * rows > MAXW ||
+ nv * xs > MAXX || rows * nv > MAXY)
+ continue;
+ fill(w, type, rows, cols);
+ acts(x, nv, cols, xs);
+ struct janas_matvec_task t = {.type = type,
+ .w = w,
+ .x = x,
+ .rows = rows,
+ .cols = cols,
+ .n_vec = nv};
+ t.y = ya;
+ janas_matvec_q4k_group(pool, &t, 1);
+ t.y = yb;
+ janas_gpu_matvec_group(g, pool, &t, 1);
+ size_t diff = 0;
+ double e = compare(ya, yb, rows * nv, &diff);
+ runs++;
+ if (e > worst_all)
+ worst_all = e;
+ if (e > 1e-4 || diff == 0) {
+ printf("%s %zux%zu nv %zu: error %.2e, %zu of %zu "
+ "outputs differ%s\n",
+ names[ti], rows, cols, nv, e, diff, rows * nv,
+ diff == 0 ? " (the GPU did not compute them?)" : "");
+ bad++;
+ }
+ }
+ /* a mixture's experts: 24 of one shape, 1 to 40 tokens each (more
+ than 128 in all, as experts_id asks), each its own activations; the
+ GPU takes those with the most tokens, the CPU the others */
+ static const int etypes[] = {JANAS_Q2_0, JANAS_Q5_0, JANAS_IQ4_NL};
+ static const char *enames[] = {"Q2_0", "Q5_0", "IQ4_NL"};
+ static const size_t eshapes[][2] = {{640, 2560}, {2560, 640}};
+ enum { NE = 24 };
+ for (size_t ti = 0; ti < sizeof(etypes) / sizeof(*etypes); ti++)
+ for (size_t s = 0; s < 2; s++) {
+ int type = etypes[ti];
+ size_t rows = eshapes[s][0], cols = eshapes[s][1];
+ size_t xs = (cols + JANAS_QK - 1) / JANAS_QK;
+ size_t rb = janas_qtype_row_bytes(type, cols);
+ size_t per = rows * rb; /* an expert's matrix */
+ if (per * NE > MAXW)
+ continue;
+ struct janas_matvec_task t[NE];
+ size_t at = 0, tokens = 0;
+ for (int e = 0; e < NE; e++) {
+ size_t nv = 1 + rnd() % 40;
+ if (at + nv > MAXX / xs || (at + nv) * rows > MAXY)
+ nv = 1;
+ fill(w + e * per, type, rows, cols);
+ t[e] = (struct janas_matvec_task){.type = type,
+ .w = w + e * per,
+ .x = x + at * xs,
+ .rows = rows,
+ .cols = cols,
+ .n_vec = nv,
+ .x_stride = xs,
+ .y_stride = rows};
+ at += nv;
+ }
+ tokens = at;
+ acts(x, tokens, cols, xs);
+ for (int e = 0, o = 0; e < NE; o += (int)t[e].n_vec, e++)
+ t[e].y = ya + (size_t)o * rows;
+ janas_matvec_q4k_group(pool, t, NE);
+ for (int e = 0, o = 0; e < NE; o += (int)t[e].n_vec, e++)
+ t[e].y = yb + (size_t)o * rows;
+ janas_gpu_matvec_group(g, pool, t, NE);
+ size_t diff = 0;
+ double err_e = compare(ya, yb, tokens * rows, &diff);
+ runs++;
+ if (err_e > worst_all)
+ worst_all = err_e;
+ if (err_e > 1e-4 || diff == 0) {
+ printf("experts %s %zux%zu, %zu tokens: error %.2e, %zu of "
+ "%zu outputs differ%s\n",
+ enames[ti], rows, cols, tokens, err_e, diff,
+ tokens * rows,
+ diff == 0 ? " (the GPU did not compute them?)" : "");
+ bad++;
+ }
+ }
+ /* the same for the 256-block types of iqk_mmid.comp */
+ static const int ktypes[] = {JANAS_Q2_K, JANAS_IQ2_XXS, JANAS_IQ2_XS,
+ JANAS_IQ2_S, JANAS_IQ3_XXS, JANAS_IQ1_S,
+ JANAS_IQ1_M, JANAS_IQ3_S, JANAS_IQ4_XS};
+ static const char *knames[] = {"Q2_K", "IQ2_XXS", "IQ2_XS",
+ "IQ2_S", "IQ3_XXS", "IQ1_S",
+ "IQ1_M", "IQ3_S", "IQ4_XS"};
+ static const size_t kshapes[][2] = {{512, 2048}, {2048, 512}};
+ for (size_t ti = 0; ti < sizeof(ktypes) / sizeof(*ktypes); ti++)
+ for (size_t s = 0; s < 2; s++) {
+ int type = ktypes[ti];
+ size_t rows = kshapes[s][0], cols = kshapes[s][1];
+ size_t xs = cols / JANAS_QK;
+ size_t rb = janas_qtype_row_bytes(type, cols), per = rows * rb;
+ if (per * NE > MAXW)
+ continue;
+ struct janas_matvec_task t[NE];
+ size_t at = 0;
+ for (int e = 0; e < NE; e++) {
+ size_t nv = 1 + rnd() % 40;
+ if (at + nv > MAXX / xs || (at + nv) * rows > MAXY)
+ nv = 1;
+ fill(w + e * per, type, rows, cols);
+ t[e] = (struct janas_matvec_task){.type = type,
+ .w = w + e * per,
+ .x = x + at * xs,
+ .rows = rows,
+ .cols = cols,
+ .n_vec = nv,
+ .x_stride = xs,
+ .y_stride = rows};
+ at += nv;
+ }
+ acts(x, at, cols, xs);
+ for (int e = 0, o = 0; e < NE; o += (int)t[e].n_vec, e++)
+ t[e].y = ya + (size_t)o * rows;
+ janas_matvec_q4k_group(pool, t, NE);
+ for (int e = 0, o = 0; e < NE; o += (int)t[e].n_vec, e++)
+ t[e].y = yb + (size_t)o * rows;
+ janas_gpu_matvec_group(g, pool, t, NE);
+ size_t diff = 0;
+ double err_e = compare(ya, yb, at * rows, &diff);
+ runs++;
+ if (err_e > worst_all)
+ worst_all = err_e;
+ if (err_e > 1e-4 || diff == 0) {
+ printf("experts %s %zux%zu, %zu tokens: error %.2e, %zu of "
+ "%zu outputs differ%s\n",
+ knames[ti], rows, cols, at, err_e, diff, at * rows,
+ diff == 0 ? " (the GPU did not compute them?)" : "");
+ bad++;
+ }
+ }
+ printf("gpu_fast_check (%s): %d products, largest error %.2e, %s\n",
+ janas_gpu_name(g), runs, worst_all, bad ? "FAILED" : "ok");
+ janas_gpu_destroy(g);
+ janas_pool_destroy(pool);
+ janas_aligned_free(w);
+ janas_aligned_free(x);
+ janas_aligned_free(ya);
+ janas_aligned_free(yb);
+ return bad ? 1 : 0;
+}
diff --git a/tests/llm_eval.c b/tests/llm_eval.c
index e45deed..e5559b3 100644
--- a/tests/llm_eval.c
+++ b/tests/llm_eval.c
@@ -6,14 +6,15 @@
*
* Usage: llm_eval <model.jns> <tokens.int32> [reference.logits]
* [cache_MiB] [compute_cpus] [io_cpus] [block=B] [reject=R]
- * [dump=F]
+ * [dump=F] [bdump=F]
* With block=B the tokens are also run in blocks of B (the prefill path) and
* the logits compared with the token-by-token ones: they must be identical.
* With reject=R (R < B) each block but the last keeps only its first B - R
* tokens and the next starts after them, as when the last R tokens of a
* chain of drafts are rejected: a recurrent model then replays the kept
* tokens of its log on the state before the block.
- * With dump=F the logits are written to F in the reference format.
+ * With dump=F the logits are written to F in the reference format; with
+ * bdump=F (and block=B) those of the blocks.
* Reports, per position, whether the most likely token agrees, the largest and
* mean absolute logit difference, and the time per token.
*/
@@ -46,7 +47,7 @@ static void print_phases(const char *what, const double *a, const double *b,
size_t n)
{
static const char *names[JANAS_PH_COUNT] = {
- "qkv", "attention", "wo", "router", "experts", "output", "ssm"};
+ "qkv", "attention", "wo", "router", "experts", "output", "ssm", "mix"};
double sum = 0;
printf("%s, ms/token:", what);
for (int i = 0; i < JANAS_PH_COUNT; i++) {
@@ -149,9 +150,15 @@ int main(int argc, char **argv)
n, dt, dt / n * 1e3, t2 / (n - n / 2) * 1e3, (n - n / 2) / t2,
100.0 * st.hits / (st.requests ? st.requests : 1));
int block = 0, reject = 0;
+ FILE *bdump = NULL;
for (int i = 7; i < argc; i++) {
if (strncmp(argv[i], "block=", 6) == 0)
block = atoi(argv[i] + 6);
+ if (strncmp(argv[i], "bdump=", 6) == 0 &&
+ !(bdump = fopen(argv[i] + 6, "wb"))) {
+ perror(argv[i] + 6);
+ return 2;
+ }
if (strncmp(argv[i], "reject=", 7) == 0)
reject = atoi(argv[i] + 7);
}
@@ -184,6 +191,10 @@ int main(int argc, char **argv)
kept = p0 + b >= nn ? b : b - (uint32_t)reject;
for (uint32_t j = 0; j < kept; j++) {
const float *a = seq + (p0 + j) * nv, *c = blk + (size_t)j * nv;
+ if (bdump && fwrite(c, sizeof(float), nv, bdump) != nv) {
+ fprintf(stderr, "cannot write the blocks' logits\n");
+ return 1;
+ }
int eq = 1;
for (uint32_t i = 0; i < nv; i++) {
double d = fabs((double)a[i] - c[i]);
@@ -226,6 +237,10 @@ int main(int argc, char **argv)
free(seq);
free(blk);
}
+ if (bdump && fclose(bdump) != 0) {
+ fprintf(stderr, "cannot write the blocks' logits\n");
+ return 1;
+ }
print_phases("second half", ph0, ph_end, n - n / 2);
if (compared)
printf("top-1 agreement %zu/%zu, max |diff| %.3f, mean |diff| %.4f\n",
diff --git a/tests/llm_keep.c b/tests/llm_keep.c
index 2953667..3692d2c 100644
--- a/tests/llm_keep.c
+++ b/tests/llm_keep.c
@@ -21,7 +21,9 @@
* message over 1024 tokens and with one under. The disk is a
* directory of the test's own (XDG_CACHE_HOME), removed at the end.
*
- * Usage: llm_keep <model.jns> [mtp=<file>] [draft=<file>]
+ * Usage: llm_keep <model.jns> [mtp=<file>] [draft=<file>] [cache=MiB]
+ * cache= sets the expert cache, below what the machine's free memory would
+ * give it, for a machine busy with other work.
*/
#define _GNU_SOURCE
#include <ftw.h>
@@ -322,7 +324,7 @@ int main(int argc, char **argv)
{
if (argc < 2) {
fprintf(stderr, "usage: llm_keep <model.jns> [mtp=<file>] "
- "[draft=<file>]\n");
+ "[draft=<file>] [cache=MiB]\n");
return 2;
}
struct janas_llm_params lp;
@@ -333,6 +335,8 @@ int main(int argc, char **argv)
lp.mtp_path = argv[i] + 4;
else if (!strncmp(argv[i], "draft=", 6))
lp.draft_path = argv[i] + 6;
+ else if (!strncmp(argv[i], "cache=", 6))
+ lp.cache_bytes = strtoull(argv[i] + 6, NULL, 10) << 20;
}
janas_llm *llm;
if (janas_llm_open(argv[1], &lp, &llm) != JANAS_LLM_OK) {
diff --git a/tests/mtp_eval.c b/tests/mtp_eval.c
index b4ec673..ada82f1 100644
--- a/tests/mtp_eval.c
+++ b/tests/mtp_eval.c
@@ -6,9 +6,12 @@
* decoding), and how often it guesses the text.
*
* Usage: mtp_eval <model.jns> <mtp.jns> <tokens.int32> [pos=h|pos=t]
+ * [cache=MiB]
* pos=h (default): the row pairing token t_{i+1} with hidden state h_i runs
* at position i; pos=t: at position i + 1 (row 0 then pairs t_0 with a zero
- * state).
+ * state). cache: the expert cache (20480 MiB by default). A row of hidden
+ * state is janas_llm_model_hidden_width wide: qwen4exp's is its residual's
+ * streams.
*/
#include <stdio.h>
#include <stdlib.h>
@@ -29,19 +32,28 @@ int main(int argc, char **argv)
{
if (argc < 4) {
fprintf(stderr, "usage: mtp_eval <model.jns> <mtp.jns> <tokens> "
- "[pos=h|pos=t]\n");
+ "[pos=h|pos=t] [cache=MiB]\n");
return 2;
}
- int pos_t = argc > 4 && strcmp(argv[4], "pos=t") == 0;
+ int pos_t = 0;
+ uint64_t cache_mib = 20480;
+ for (int i = 4; i < argc; i++) {
+ if (strcmp(argv[i], "pos=t") == 0)
+ pos_t = 1;
+ else if (strncmp(argv[i], "cache=", 6) == 0)
+ cache_mib = strtoull(argv[i] + 6, NULL, 10);
+ }
FILE *f = fopen(argv[3], "rb");
- if (!f)
+ if (!f) {
+ fprintf(stderr, "cannot open %s\n", argv[3]);
return 1;
+ }
int32_t tok[4096];
uint32_t n = (uint32_t)fread(tok, sizeof(int32_t), 4096, f);
fclose(f);
int ccpus[12] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
int icpus[8] = {12, 13, 14, 15, 16, 17, 18, 19};
- struct janas_llm_options o = {.cache_bytes = 20480ull << 20,
+ struct janas_llm_options o = {.cache_bytes = cache_mib << 20,
.n_ctx = n + 64,
.compute_cpus = ccpus,
.n_compute = 12,
@@ -54,15 +66,18 @@ int main(int argc, char **argv)
fprintf(stderr, "%s\n", err);
return 1;
}
- uint32_t nv = janas_llm_model_n_vocab(m), dm = 2048, B = 64;
+ uint32_t nv = janas_llm_model_n_vocab(m),
+ dm = janas_llm_model_hidden_width(m), B = 64;
float *logits = malloc((size_t)B * nv * sizeof(float));
float *hid = calloc((size_t)(n + 1) * dm, sizeof(float));
uint32_t *main_next = malloc(n * sizeof(uint32_t));
/* main model: its choice after every position, and its hidden states */
for (uint32_t p = 0; p < n; p += B) {
uint32_t b = n - p < B ? n - p : B;
- if (janas_llm_model_forward(m, tok + p, b, p, logits, 1) != 0)
+ if (janas_llm_model_forward(m, tok + p, b, p, logits, 1) != 0) {
+ fprintf(stderr, "the main model failed at position %u\n", p);
return 1;
+ }
for (uint32_t j = 0; j < b; j++)
main_next[p + j] = argmax(logits + (size_t)j * nv, nv);
memcpy(hid + (size_t)(p + 1) * dm, janas_llm_model_hidden(m),
@@ -78,8 +93,10 @@ int main(int argc, char **argv)
uint32_t pos0 = pos_t ? r : r - 1;
/* row k: hidden hid[r + k] = h_{r+k-1}, token t_{r+k} */
if (janas_llm_mtp_forward(m, tok + r, hid + (size_t)r * dm, b, pos0,
- logits, 1) != 0)
+ logits, 1) != 0) {
+ fprintf(stderr, "the MTP block failed at row %u\n", r);
return 1;
+ }
for (uint32_t k = 0; k < b; k++) {
uint32_t i = r + k; /* guesses t_{i+1} */
if (i < 1 || i + 1 >= n)
diff --git a/tests/test_quant.c b/tests/test_quant.c
index 58d947d..da5468e 100644
--- a/tests/test_quant.c
+++ b/tests/test_quant.c
@@ -14,6 +14,7 @@
#include <unistd.h>
#include "common/pool.h"
+#include "llm/bf16.h"
#include "llm/matvec.h"
#include "llm/quant.h"
#include "llm/vmath.h"
@@ -36,7 +37,8 @@ static int failures;
static double act(const struct janas_block_q8k *x, int b, int i, int t)
{
int b32 = t == JANAS_Q8_0 || t == JANAS_Q4_0 || t == JANAS_Q4_1 ||
- t == JANAS_Q5_1 || t == JANAS_IQ4_NL || t == JANAS_MXFP4;
+ t == JANAS_Q5_1 || t == JANAS_IQ4_NL || t == JANAS_MXFP4 ||
+ t == JANAS_Q5_0 || t == JANAS_Q2_0;
return b32 ? (double)x[b].d32[i / 32] * x[b].qs32[i]
: (double)x[b].d * x[b].qs[i];
}
@@ -1047,6 +1049,215 @@ static void test_dot_q4_0_q5_1(void)
free(wf);
}
+/* Random bytes for a row of n weights of a type in blocks of 32 or 64, each
+ block's scale a finite fp16 (sizes from janas_qtype_row_bytes). */
+static void random_blocks(int type, unsigned char *w, size_t n)
+{
+ size_t bytes = janas_qtype_row_bytes(type, n);
+ for (size_t i = 0; i < bytes; i++)
+ w[i] = (unsigned char)rnd();
+ size_t size = type == JANAS_Q2_0 ? sizeof(struct janas_block_q2_0)
+ : type == JANAS_Q5_0 ? sizeof(struct janas_block_q5_0)
+ : type == JANAS_Q8_0 ? sizeof(struct janas_block_q8_0)
+ : sizeof(struct janas_block_iq4nl);
+ for (size_t b = 0; b < bytes / size; b++) {
+ uint16_t d = janas_fp32_to_fp16(rndf(-0.01f, 0.01f));
+ memcpy(w + b * size, &d, 2);
+ }
+}
+
+/*
+ * Q5_0 and Q2_0: the reference against a float product over the
+ * dequantized weights, the kernels (single, 1-17 vectors, rows) equal to it
+ * to the bit; the values by hand as ggml's dequantize_row_q5_0 and
+ * dequantize_row_q2_0 give them.
+ */
+static void test_dot_q5_0_q2_0(void)
+{
+ enum { NB = 8, N = NB * JANAS_QK, NV = 17 };
+ static const int types[] = {JANAS_Q5_0, JANAS_Q2_0};
+ unsigned char *w = malloc(NB * janas_qtype_block_size(JANAS_Q5_0));
+ struct janas_block_q8k *x = malloc(NV * NB * sizeof(*x));
+ float *xf = malloc(N * sizeof(float)), *wf = malloc(N * sizeof(float));
+ float rows[4];
+ for (int trial = 0; trial < 100; trial++)
+ for (unsigned k = 0; k < sizeof(types) / sizeof(*types); k++) {
+ int t = types[k];
+ const char *name = t == JANAS_Q5_0 ? "q5_0" : "q2_0";
+ random_blocks(t, w, N);
+ for (int v = 0; v < NV; v++) {
+ for (int i = 0; i < N; i++)
+ xf[i] = rndf(-1.0f, 1.0f);
+ janas_q8k_quantize(xf, x + v * NB, N);
+ }
+ janas_dequantize(t, w, wf, N);
+ double expect = 0.0, mag = 0.0;
+ for (int b = 0; b < NB; b++)
+ for (int i = 0; i < JANAS_QK; i++) {
+ double e = (double)wf[b * JANAS_QK + i] * act(x, b, i, t);
+ expect += e;
+ mag += fabs(e);
+ }
+ float ref = t == JANAS_Q5_0
+ ? janas_q5_0_dot_ref((const void *)w, x, NB)
+ : janas_q2_0_dot_ref((const void *)w, x, NB);
+ CHECK(fabs(ref - expect) <= 1e-5 * mag + 1e-6, "%s ref %g vs %g",
+ name, ref, expect);
+ float fast = janas_dot(t, w, x, NB);
+ CHECK(fast == ref, "%s fast %g vs ref %g", name, fast, ref);
+ for (int nv = 1; nv <= NV; nv++) {
+ float out[NV];
+ janas_dot_multi(t, w, x, NB, (size_t)nv, NB, out, 1);
+ for (int v = 0; v < nv; v++) {
+ float r = t == JANAS_Q5_0
+ ? janas_q5_0_dot_ref((const void *)w,
+ x + v * NB, NB)
+ : janas_q2_0_dot_ref((const void *)w,
+ x + v * NB, NB);
+ CHECK(out[v] == r, "%s multi nv %d v %d: %g vs %g", name,
+ nv, v, out[v], r);
+ }
+ }
+ /* four rows of two groups each, the matrix read row by row */
+ size_t gb = janas_qtype_block_size(t);
+ janas_dot_rows(t, w, 2 * gb, x, 2, 4, rows);
+ for (int r = 0; r < 4; r++) {
+ float one = janas_dot(t, w + 2 * gb * (size_t)r, x, 2);
+ CHECK(rows[r] == one, "%s rows %d: %g vs %g", name, r, rows[r],
+ one);
+ }
+ }
+ /* by hand. Q5_0: weight 17 takes bit 17 of qh and the high nibble of
+ byte 1, weight 1 the low nibble alone, less 16 */
+ struct janas_block_q5_0 h5 = {0};
+ h5.d = janas_fp32_to_fp16(1.0f);
+ h5.qh[2] = 0x02; /* bit 17 */
+ h5.qs[1] = 0x37; /* weight 1: 7; weight 17: 3 */
+ float y[64];
+ janas_q5_0_dequantize(&h5, y, 32);
+ CHECK(y[17] == 19.0f - 16.0f && y[1] == 7.0f - 16.0f && y[0] == -16.0f,
+ "q5_0 by hand: %g %g %g", y[17], y[1], y[0]);
+ /* Q2_0: weight i in bits 2 (i % 4) of byte i / 4, 00 = -1, 11 = +2 */
+ struct janas_block_q2_0 h2 = {0};
+ h2.d = janas_fp32_to_fp16(0.5f);
+ h2.qs[0] = 0xe4; /* weights 0-3: 0, 1, 2, 3 */
+ h2.qs[15] = 0x1b; /* weights 60-63: 3, 2, 1, 0 */
+ janas_q2_0_dequantize(&h2, y, 64);
+ CHECK(y[0] == -0.5f && y[1] == 0.0f && y[2] == 0.5f && y[3] == 1.0f &&
+ y[60] == 1.0f && y[61] == 0.5f && y[62] == 0.0f &&
+ y[63] == -0.5f && y[4] == -0.5f,
+ "q2_0 by hand: %g %g %g %g / %g %g %g %g", y[0], y[1], y[2], y[3],
+ y[60], y[61], y[62], y[63]);
+ free(w);
+ free(x);
+ free(xf);
+ free(wf);
+}
+
+/*
+ * Rows that end inside a group of 256 (Qwen3.8-Flash-Next's experts, 640
+ * wide), in the types stored in blocks of 32 or 64: the product is the whole
+ * groups' plus the tail padded with zero blocks, the same bits whichever way
+ * it is asked for (one row, rows, 1-17 vectors, the grouped products on a
+ * pool); and it is the float product over the n weights.
+ */
+static void test_row_tails(void)
+{
+ enum { NMAX = 768, NBX = NMAX / JANAS_QK, NV = 17, ROWS = 37 };
+ static const int types[] = {JANAS_Q2_0, JANAS_Q5_0, JANAS_IQ4_NL,
+ JANAS_Q4_0, JANAS_Q8_0};
+ static const size_t ns[] = {640, 64, 320, 576, 704};
+ size_t big = ROWS * janas_qtype_row_bytes(JANAS_Q8_0, NMAX);
+ unsigned char *w = malloc(big);
+ struct janas_block_q8k *x = malloc(NV * NBX * sizeof(*x));
+ float xf[NMAX], *wf = malloc(NMAX * sizeof(float));
+ float y[NV * ROWS];
+ struct janas_pool *pool = janas_pool_create(3, NULL);
+ CHECK(pool != NULL, "pool");
+ for (unsigned k = 0; k < sizeof(types) / sizeof(*types); k++)
+ for (unsigned j = 0; j < sizeof(ns) / sizeof(*ns); j++) {
+ int t = types[k];
+ size_t n = ns[j], nb = n / JANAS_QK, nbx = (n + 255) / 256;
+ size_t rb = janas_qtype_row_bytes(t, n);
+ if (t == JANAS_Q2_0 && n % 64) {
+ CHECK(rb == 0, "q2_0 row of %zu: %zu bytes", n, rb);
+ continue;
+ }
+ CHECK(rb != 0 && rb < (nb + 1) * janas_qtype_block_size(t),
+ "type %d row of %zu: %zu bytes", t, n, rb);
+ for (int r = 0; r < ROWS; r++)
+ random_blocks(t, w + (size_t)r * rb, n);
+ /* activations past n zero, as the engine pads them */
+ for (int v = 0; v < NV; v++) {
+ for (size_t i = 0; i < NMAX; i++)
+ xf[i] = i < n ? rndf(-1.0f, 1.0f) : 0.0f;
+ janas_q8k_quantize(xf, x + v * nbx, nbx * JANAS_QK);
+ }
+ /* the float product of row 0 */
+ janas_dequantize(t, w, wf, n);
+ double expect = 0.0, mag = 0.0;
+ for (size_t i = 0; i < n; i++) {
+ double e =
+ (double)wf[i] * act(x, (int)(i / 256), (int)(i % 256), t);
+ expect += e;
+ mag += fabs(e);
+ }
+ float one = janas_dot_n(t, w, x, n);
+ CHECK(fabs(one - expect) <= 1e-5 * mag + 1e-6,
+ "type %d n %zu: %g vs %g", t, n, one, expect);
+ /* its definition: whole groups, then the padded tail */
+ _Alignas(64) unsigned char pad[8 * sizeof(struct janas_block_q8_0)];
+ size_t full = nb * janas_qtype_block_size(t);
+ memset(pad, 0, sizeof(pad));
+ memcpy(pad, w + full, rb - full);
+ float def = (nb ? janas_dot(t, w, x, nb) : 0.0f) +
+ janas_dot(t, pad, x + nb, 1);
+ CHECK(one == def, "type %d n %zu: %g vs its definition %g", t, n,
+ one, def);
+ janas_dot_rows_n(t, w, rb, x, n, ROWS, y);
+ for (int r = 0; r < ROWS; r++) {
+ float e = janas_dot_n(t, w + (size_t)r * rb, x, n);
+ CHECK(y[r] == e, "type %d n %zu rows %d: %g vs %g", t, n, r,
+ y[r], e);
+ }
+ for (int nv = 1; nv <= NV; nv += 4) {
+ float out[NV];
+ janas_dot_multi_n(t, w, x, nbx, (size_t)nv, n, out, 1);
+ for (int v = 0; v < nv; v++) {
+ float e = janas_dot_n(t, w, x + v * nbx, n);
+ CHECK(out[v] == e, "type %d n %zu multi %d/%d: %g vs %g", t,
+ n, v, nv, out[v], e);
+ }
+ }
+ /* the grouped products: one vector and five */
+ for (int nv = 1; pool && nv <= 5; nv += 4) {
+ struct janas_matvec_task task = {.type = t,
+ .w = w,
+ .x = x,
+ .y = y,
+ .rows = ROWS,
+ .cols = n,
+ .n_vec = (size_t)nv,
+ .x_stride = nbx,
+ .y_stride = ROWS};
+ janas_matvec_q4k_group(pool, &task, 1);
+ for (int v = 0; v < nv; v++)
+ for (int r = 0; r < ROWS; r++) {
+ float e =
+ janas_dot_n(t, w + (size_t)r * rb, x + v * nbx, n);
+ CHECK(y[v * ROWS + r] == e,
+ "type %d n %zu group nv %d v %d row %d: %g vs "
+ "%g",
+ t, n, nv, v, r, y[v * ROWS + r], e);
+ }
+ }
+ }
+ janas_pool_destroy(pool);
+ free(w);
+ free(x);
+ free(wf);
+}
+
/*
* Unpacking a whole row: janas_dequantize has to reach the right unpacker for
* every type the engine accepts in a model file, not just the two it used to
@@ -1055,12 +1266,120 @@ static void test_dot_q4_0_q5_1(void)
* passed its checksums and answered nonsense (reported from an i7-1365U, on a
* GGUF whose token_embd.weight is Q8_0).
*/
+/* Q8_0 rows on float vectors (bf16.h): one row, a row on four vectors and
+ four rows on one vector, each the bits of the order bf16.h states, scales
+ subnormal and zero among them */
+static float q8w_ref(const unsigned char *w, const float *x, uint32_t n)
+{
+ float acc[8] = {0};
+ for (uint32_t b = 0; b < n / 32; b++, w += 34, x += 32) {
+ const int8_t *q = (const int8_t *)(w + 2);
+ uint16_t h;
+ memcpy(&h, w, 2);
+ float d = janas_fp16_to_fp32(h), bs[8] = {0};
+ for (int g = 0; g < 4; g++)
+ for (int k = 0; k < 8; k++)
+ bs[k] = fmaf((float)q[8 * g + k], x[8 * g + k], bs[k]);
+ for (int k = 0; k < 8; k++)
+ acc[k] = fmaf(bs[k], d, acc[k]);
+ }
+ return ((acc[0] + acc[4]) + (acc[2] + acc[6])) +
+ ((acc[1] + acc[5]) + (acc[3] + acc[7]));
+}
+
+static void test_q8w(void)
+{
+ enum { N = 320, ROW = N / 32 * 34 };
+ unsigned char *w = malloc(4 * ROW);
+ float *x = malloc(4 * N * sizeof(float));
+ for (int rep = 0; rep < 20; rep++) {
+ for (int i = 0; i < 4 * ROW; i++)
+ w[i] = (unsigned char)rnd();
+ for (int b = 0; b < 4 * N / 32; b++) {
+ float d = rndf(0.0001f, 0.05f);
+ uint16_t h = janas_fp32_to_fp16(d);
+ if (b % 7 == 3)
+ h = (uint16_t)(rnd() & 0x83ff); /* subnormal or zero */
+ memcpy(w + b * 34, &h, 2);
+ }
+ for (int i = 0; i < 4 * N; i++)
+ x[i] = rndf(-2.0f, 2.0f);
+ float o[4], e;
+ const float *xs[4] = {x, x + N, x + 2 * N, x + 3 * N};
+ janas_q8w_dot4(w, xs, N, o);
+ for (int t = 0; t < 4; t++) {
+ e = q8w_ref(w, xs[t], N);
+ CHECK(!memcmp(&o[t], &e, 4), "q8w dot4 vector %d: %g != %g", t,
+ o[t], e);
+ }
+ janas_q8w_dot4r(w, ROW, x, N, o);
+ for (int r = 0; r < 4; r++) {
+ e = q8w_ref(w + r * ROW, x, N);
+ CHECK(!memcmp(&o[r], &e, 4), "q8w dot4r row %d: %g != %g", r, o[r],
+ e);
+ float one = janas_q8w_dot(w + r * ROW, x, N);
+ CHECK(!memcmp(&one, &e, 4), "q8w dot row %d: %g != %g", r, one, e);
+ }
+ }
+ free(w);
+ free(x);
+}
+
+/* bf16 rows on one vector four at a time (bf16.h) against one at a time,
+ lengths with and without a tail; rounding to bf16 against the scalar
+ rule, ties, infinities and NaNs among the values */
+static void test_bf16_rows(void)
+{
+ enum { N = 333, ROW = 340 };
+ uint16_t *w = malloc(4 * ROW * sizeof(uint16_t));
+ float *x = malloc(ROW * sizeof(float));
+ for (int rep = 0; rep < 20; rep++) {
+ for (int i = 0; i < 4 * ROW; i++)
+ w[i] = (uint16_t)(janas_f2u(rndf(-0.1f, 0.1f)) >> 16);
+ for (int i = 0; i < ROW; i++)
+ x[i] = rndf(-2.0f, 2.0f);
+ uint32_t n = rep % 2 ? N : 320;
+ float o[4];
+ janas_bf16_dot4r(w, ROW, x, n, o);
+ for (int r = 0; r < 4; r++) {
+ float e = janas_bf16_dot(w + r * ROW, x, n);
+ CHECK(!memcmp(&o[r], &e, 4), "bf16 dot4r row %d n %u: %g != %g", r,
+ n, o[r], e);
+ }
+ }
+ enum { M = 4099 };
+ float *a = malloc(M * sizeof(float)), *b = malloc(M * sizeof(float));
+ for (int i = 0; i < M; i++) {
+ uint32_t u = rnd();
+ if (i % 5 == 1)
+ u = (u & 0xffff0000u) | 0x8000u; /* a tie */
+ if (i % 97 == 3)
+ u = 0x7f800000u | (u & 0x80000000u); /* infinity */
+ if (i % 89 == 5)
+ u = 0x7f800001u | (u & 0x807fffffu); /* NaN */
+ a[i] = janas_u2f(u);
+ }
+ janas_round_bf16_n(a, b, M);
+ for (int i = 0; i < M; i++) {
+ uint32_t u = janas_f2u(a[i]), e = u;
+ if ((u & 0x7fffffffu) <= 0x7f800000u)
+ e = (u + (0x7fffu + ((u >> 16) & 1u))) & 0xffff0000u;
+ CHECK(janas_f2u(b[i]) == e, "round bf16 %08x: %08x != %08x", u,
+ janas_f2u(b[i]), e);
+ }
+ free(w);
+ free(x);
+ free(a);
+ free(b);
+}
+
static void test_dequantize(void)
{
enum { NB = 8, N = NB * JANAS_QK };
- static const int types[] = {
- JANAS_Q4_K, JANAS_Q5_K, JANAS_Q6_K, JANAS_Q8_0, JANAS_IQ4_NL,
- JANAS_IQ4_XS, JANAS_Q4_0, JANAS_Q5_1, JANAS_Q4_1, JANAS_MXFP4};
+ static const int types[] = {JANAS_Q4_K, JANAS_Q5_K, JANAS_Q6_K,
+ JANAS_Q8_0, JANAS_IQ4_NL, JANAS_IQ4_XS,
+ JANAS_Q4_0, JANAS_Q5_1, JANAS_Q4_1,
+ JANAS_MXFP4, JANAS_Q5_0, JANAS_Q2_0};
float *a = malloc(N * sizeof(float)), *b = malloc(N * sizeof(float));
for (unsigned k = 0; k < sizeof(types) / sizeof(*types); k++) {
int type = types[k];
@@ -1133,6 +1452,20 @@ static void test_dequantize(void)
}
break;
}
+ case JANAS_Q5_0: {
+ struct janas_block_q5_0 *q =
+ (struct janas_block_q5_0 *)w + i * 8;
+ for (int j = 0; j < 8; j++)
+ q[j].d = janas_fp32_to_fp16(rndf(0.0001f, 0.01f));
+ break;
+ }
+ case JANAS_Q2_0: { /* four blocks cover one super-block */
+ struct janas_block_q2_0 *q =
+ (struct janas_block_q2_0 *)w + i * 4;
+ for (int j = 0; j < 4; j++)
+ q[j].d = janas_fp32_to_fp16(rndf(0.0001f, 0.01f));
+ break;
+ }
default: { /* eight Q8_0 blocks cover one super-block */
struct janas_block_q8_0 *q =
(struct janas_block_q8_0 *)w + i * 8;
@@ -1168,6 +1501,12 @@ static void test_dequantize(void)
case JANAS_Q4_1:
janas_q4_1_dequantize((const void *)w, a, N);
break;
+ case JANAS_Q5_0:
+ janas_q5_0_dequantize((const void *)w, a, N);
+ break;
+ case JANAS_Q2_0:
+ janas_q2_0_dequantize((const void *)w, a, N);
+ break;
default:
janas_q8_0_dequantize((const void *)w, a, N);
}
@@ -1389,6 +1728,266 @@ static void test_dot_iq3s(void)
free(w2);
}
+static void test_dot_iq3xxs(void)
+{
+ enum { NB = 8, N = NB * JANAS_QK, NV = 17 };
+ struct janas_block_iq3xxs *w = malloc(NB * sizeof(*w));
+ struct janas_block_q8k *x = malloc(NV * NB * sizeof(*x));
+ float *xf = malloc(N * sizeof(float)), *wf = malloc(N * sizeof(float)),
+ *w2 = malloc(N * sizeof(float));
+ for (int trial = 0; trial < 100; trial++) {
+ unsigned char *p = (unsigned char *)w;
+ for (size_t i = 0; i < NB * sizeof(*w); i++)
+ p[i] = (unsigned char)rnd();
+ for (int b = 0; b < NB; b++)
+ w[b].d = janas_fp32_to_fp16(rndf(-0.01f, 0.01f));
+ for (int v = 0; v < NV; v++) {
+ for (int i = 0; i < N; i++)
+ xf[i] = rndf(-1.0f, 1.0f);
+ janas_q8k_quantize(xf, x + v * NB, N);
+ }
+ janas_iq3xxs_dequantize(w, wf, N);
+ janas_dequantize(JANAS_IQ3_XXS, w, w2, N);
+ CHECK(memcmp(wf, w2, N * sizeof(float)) == 0,
+ "iq3xxs: janas_dequantize differs from its unpacker");
+ double expect = 0.0, mag = 0.0;
+ for (int b = 0; b < NB; b++)
+ for (int i = 0; i < JANAS_QK; i++) {
+ double e = (double)wf[b * JANAS_QK + i] * x[b].d * x[b].qs[i];
+ expect += e;
+ mag += fabs(e);
+ }
+ float ref = janas_iq3xxs_dot_ref(w, x, NB);
+ CHECK(fabs(ref - expect) <= 1e-5 * mag + 1e-6, "iq3xxs ref %g vs %g",
+ ref, expect);
+ CHECK(janas_dot(JANAS_IQ3_XXS, w, x, NB) == ref, "iq3xxs fast vs ref");
+ for (int nv = 1; nv <= NV; nv++) {
+ float out[NV];
+ janas_dot_multi(JANAS_IQ3_XXS, w, x, NB, (size_t)nv, NB, out, 1);
+ for (int v = 0; v < nv; v++)
+ CHECK(out[v] == janas_iq3xxs_dot_ref(w, x + v * NB, NB),
+ "iq3xxs multi nv %d v %d", nv, v);
+ }
+ }
+ free(w);
+ free(x);
+ free(xf);
+ free(wf);
+ free(w2);
+}
+
+/* IQ2_XXS, IQ2_XS, IQ2_S: the reference against a float product over the
+ dequantized weights, the kernels (single, 1-17 vectors, rows) equal to it
+ to the bit */
+static void test_dot_iq2(void)
+{
+ enum { NB = 8, N = NB * JANAS_QK, NV = 17 };
+ static const int types[] = {JANAS_IQ2_XXS, JANAS_IQ2_XS, JANAS_IQ2_S};
+ static const char *const names[] = {"iq2_xxs", "iq2_xs", "iq2_s"};
+ size_t big = NB * janas_qtype_block_size(JANAS_IQ2_S);
+ unsigned char *w = malloc(big);
+ struct janas_block_q8k *x = malloc(NV * NB * sizeof(*x));
+ float *xf = malloc(N * sizeof(float)), *wf = malloc(N * sizeof(float));
+ float rows[4];
+ for (int trial = 0; trial < 100; trial++)
+ for (int k = 0; k < 3; k++) {
+ int t = types[k];
+ size_t bs = janas_qtype_block_size(t);
+ for (size_t i = 0; i < NB * bs; i++)
+ w[i] = (unsigned char)rnd();
+ for (int b = 0; b < NB; b++) {
+ uint16_t d = janas_fp32_to_fp16(rndf(-0.01f, 0.01f));
+ memcpy(w + b * bs, &d, 2);
+ }
+ for (int v = 0; v < NV; v++) {
+ for (int i = 0; i < N; i++)
+ xf[i] = rndf(-1.0f, 1.0f);
+ janas_q8k_quantize(xf, x + v * NB, N);
+ }
+ janas_dequantize(t, w, wf, N);
+ double expect = 0.0, mag = 0.0;
+ for (int b = 0; b < NB; b++)
+ for (int i = 0; i < JANAS_QK; i++) {
+ double e =
+ (double)wf[b * JANAS_QK + i] * x[b].d * x[b].qs[i];
+ expect += e;
+ mag += fabs(e);
+ }
+ float ref = janas_iq2_dot_ref(t, w, x, NB);
+ CHECK(fabs(ref - expect) <= 1e-5 * mag + 1e-6, "%s ref %g vs %g",
+ names[k], ref, expect);
+ CHECK(janas_dot(t, w, x, NB) == ref, "%s fast vs ref", names[k]);
+ for (int nv = 1; nv <= NV; nv++) {
+ float out[NV];
+ janas_dot_multi(t, w, x, NB, (size_t)nv, NB, out, 1);
+ for (int v = 0; v < nv; v++)
+ CHECK(out[v] == janas_iq2_dot_ref(t, w, x + v * NB, NB),
+ "%s multi nv %d v %d", names[k], nv, v);
+ }
+ janas_dot_rows(t, w, 2 * bs, x, 2, 4, rows);
+ for (int r = 0; r < 4; r++)
+ CHECK(rows[r] == janas_iq2_dot_ref(t, w + 2 * bs * r, x, 2),
+ "%s rows %d", names[k], r);
+ }
+ free(w);
+ free(x);
+ free(xf);
+ free(wf);
+}
+
+static void test_dot_iq1m(void)
+{
+ enum { NB = 8, N = NB * JANAS_QK, NV = 17 };
+ struct janas_block_iq1m *w = malloc(NB * sizeof(*w));
+ struct janas_block_q8k *x = malloc(NV * NB * sizeof(*x));
+ float *xf = malloc(N * sizeof(float)), *wf = malloc(N * sizeof(float)),
+ *w2 = malloc(N * sizeof(float));
+ for (int trial = 0; trial < 100; trial++) {
+ unsigned char *p = (unsigned char *)w;
+ for (size_t i = 0; i < NB * sizeof(*w); i++)
+ p[i] = (unsigned char)rnd();
+ for (int b = 0; b < NB; b++) {
+ /* a finite d in the scale words' top nibbles */
+ uint16_t d = janas_fp32_to_fp16(rndf(-0.01f, 0.01f)), sc[4];
+ memcpy(sc, w[b].scales, 8);
+ for (int k = 0; k < 4; k++)
+ sc[k] =
+ (uint16_t)((sc[k] & 0x0fff) | ((d >> 4 * k & 15) << 12));
+ memcpy(w[b].scales, sc, 8);
+ }
+ for (int v = 0; v < NV; v++) {
+ for (int i = 0; i < N; i++)
+ xf[i] = rndf(-1.0f, 1.0f);
+ janas_q8k_quantize(xf, x + v * NB, N);
+ }
+ janas_iq1m_dequantize(w, wf, N);
+ janas_dequantize(JANAS_IQ1_M, w, w2, N);
+ CHECK(memcmp(wf, w2, N * sizeof(float)) == 0,
+ "iq1m: janas_dequantize differs from its unpacker");
+ double expect = 0.0, mag = 0.0;
+ for (int b = 0; b < NB; b++)
+ for (int i = 0; i < JANAS_QK; i++) {
+ double e = (double)wf[b * JANAS_QK + i] * x[b].d * x[b].qs[i];
+ expect += e;
+ mag += fabs(e);
+ }
+ float ref = janas_iq1m_dot_ref(w, x, NB);
+ CHECK(fabs(ref - expect) <= 1e-5 * mag + 1e-6, "iq1m ref %g vs %g", ref,
+ expect);
+ CHECK(janas_dot(JANAS_IQ1_M, w, x, NB) == ref, "iq1m fast vs ref");
+ for (int nv = 1; nv <= NV; nv++) {
+ float out[NV];
+ janas_dot_multi(JANAS_IQ1_M, w, x, NB, (size_t)nv, NB, out, 1);
+ for (int v = 0; v < nv; v++)
+ CHECK(out[v] == janas_iq1m_dot_ref(w, x + v * NB, NB),
+ "iq1m multi nv %d v %d", nv, v);
+ }
+ }
+ free(w);
+ free(x);
+ free(xf);
+ free(wf);
+ free(w2);
+}
+
+static void test_dot_iq1s(void)
+{
+ enum { NB = 8, N = NB * JANAS_QK, NV = 17 };
+ struct janas_block_iq1s *w = malloc(NB * sizeof(*w));
+ struct janas_block_q8k *x = malloc(NV * NB * sizeof(*x));
+ float *xf = malloc(N * sizeof(float)), *wf = malloc(N * sizeof(float)),
+ *w2 = malloc(N * sizeof(float));
+ for (int trial = 0; trial < 100; trial++) {
+ unsigned char *p = (unsigned char *)w;
+ for (size_t i = 0; i < NB * sizeof(*w); i++)
+ p[i] = (unsigned char)rnd();
+ for (int b = 0; b < NB; b++)
+ w[b].d = janas_fp32_to_fp16(rndf(-0.01f, 0.01f));
+ for (int v = 0; v < NV; v++) {
+ for (int i = 0; i < N; i++)
+ xf[i] = rndf(-1.0f, 1.0f);
+ janas_q8k_quantize(xf, x + v * NB, N);
+ }
+ janas_iq1s_dequantize(w, wf, N);
+ janas_dequantize(JANAS_IQ1_S, w, w2, N);
+ CHECK(memcmp(wf, w2, N * sizeof(float)) == 0,
+ "iq1s: janas_dequantize differs from its unpacker");
+ double expect = 0.0, mag = 0.0;
+ for (int b = 0; b < NB; b++)
+ for (int i = 0; i < JANAS_QK; i++) {
+ double e = (double)wf[b * JANAS_QK + i] * x[b].d * x[b].qs[i];
+ expect += e;
+ mag += fabs(e);
+ }
+ float ref = janas_iq1s_dot_ref(w, x, NB);
+ CHECK(fabs(ref - expect) <= 1e-5 * mag + 1e-6, "iq1s ref %g vs %g", ref,
+ expect);
+ CHECK(janas_dot(JANAS_IQ1_S, w, x, NB) == ref, "iq1s fast vs ref");
+ for (int nv = 1; nv <= NV; nv++) {
+ float out[NV];
+ janas_dot_multi(JANAS_IQ1_S, w, x, NB, (size_t)nv, NB, out, 1);
+ for (int v = 0; v < nv; v++)
+ CHECK(out[v] == janas_iq1s_dot_ref(w, x + v * NB, NB),
+ "iq1s multi nv %d v %d", nv, v);
+ }
+ }
+ free(w);
+ free(x);
+ free(xf);
+ free(wf);
+ free(w2);
+}
+
+static void test_dot_q2k(void)
+{
+ enum { NB = 8, N = NB * JANAS_QK, NV = 17 };
+ struct janas_block_q2k *w = malloc(NB * sizeof(*w));
+ struct janas_block_q8k *x = malloc(NV * NB * sizeof(*x));
+ float *xf = malloc(N * sizeof(float)), *wf = malloc(N * sizeof(float)),
+ *w2 = malloc(N * sizeof(float));
+ for (int trial = 0; trial < 100; trial++) {
+ unsigned char *p = (unsigned char *)w;
+ for (size_t i = 0; i < NB * sizeof(*w); i++)
+ p[i] = (unsigned char)rnd();
+ for (int b = 0; b < NB; b++) {
+ w[b].d = janas_fp32_to_fp16(rndf(0.0001f, 0.01f));
+ w[b].dmin = janas_fp32_to_fp16(rndf(0.0001f, 0.01f));
+ }
+ for (int v = 0; v < NV; v++) {
+ for (int i = 0; i < N; i++)
+ xf[i] = rndf(-1.0f, 1.0f);
+ janas_q8k_quantize(xf, x + v * NB, N);
+ }
+ janas_q2k_dequantize(w, wf, N);
+ janas_dequantize(JANAS_Q2_K, w, w2, N);
+ CHECK(memcmp(wf, w2, N * sizeof(float)) == 0,
+ "q2k: janas_dequantize differs from its unpacker");
+ double expect = 0.0, mag = 0.0;
+ for (int b = 0; b < NB; b++)
+ for (int i = 0; i < JANAS_QK; i++) {
+ double e = (double)wf[b * JANAS_QK + i] * x[b].d * x[b].qs[i];
+ expect += e;
+ mag += fabs(e);
+ }
+ float ref = janas_q2k_dot_ref(w, x, NB);
+ CHECK(fabs(ref - expect) <= 1e-5 * mag + 1e-6, "q2k ref %g vs %g", ref,
+ expect);
+ CHECK(janas_dot(JANAS_Q2_K, w, x, NB) == ref, "q2k fast vs ref");
+ for (int nv = 1; nv <= NV; nv++) {
+ float out[NV];
+ janas_dot_multi(JANAS_Q2_K, w, x, NB, (size_t)nv, NB, out, 1);
+ for (int v = 0; v < nv; v++)
+ CHECK(out[v] == janas_q2k_dot_ref(w, x + v * NB, NB),
+ "q2k multi nv %d v %d", nv, v);
+ }
+ }
+ free(w);
+ free(x);
+ free(xf);
+ free(wf);
+ free(w2);
+}
+
int main(void)
{
/* a test that deadlocks must fail, not hold the suite for ever */
@@ -1408,12 +2007,21 @@ int main(void)
test_dot_iq4();
test_dot_mxfp4();
test_dot_q4_0_q5_1();
+ test_dot_q5_0_q2_0();
+ test_row_tails();
test_dot_q3k();
test_dot_iq3s();
+ test_dot_iq3xxs();
+ test_dot_iq2();
+ test_dot_iq1m();
+ test_dot_iq1s();
+ test_dot_q2k();
test_quantize_q4k();
test_quantize_q6k();
test_quantize_q8k();
test_dequantize();
+ test_q8w();
+ test_bf16_rows();
printf("test_quant: kernel %s, %s\n", janas_q4k_kernel_name(),
failures ? "FAILED" : "ok");
return failures ? 1 : 0;
diff --git a/tests/test_tools.c b/tests/test_tools.c
index 5e487fa..bbecce6 100644
--- a/tests/test_tools.c
+++ b/tests/test_tools.c
@@ -170,7 +170,7 @@ static int call_ok(enum janas_tool_dialect d, const char *body)
struct janas_toolset *t = tools();
struct janas_gbuild *b = janas_gbuild_new();
struct janas_jsg *j = janas_jsg_new(b);
- uint32_t r = janas_tools_rule(b, j, d, t, -1, 9);
+ uint32_t r = janas_tools_rule(b, j, d, t, -1, NULL, 0, 9);
struct janas_grammar *g = janas_grammar_make(b);
struct janas_gmatch *m = g ? janas_gmatch_new(g, r) : NULL;
int ok = m && janas_gmatch_feed(m, body, strlen(body), -1) == 0 &&
@@ -253,7 +253,7 @@ static void test_mangled(void)
janas_buf_free(&s);
struct janas_gbuild *b = janas_gbuild_new();
struct janas_jsg *j = janas_jsg_new(b);
- janas_tools_rule(b, j, d, t, -1, 9);
+ janas_tools_rule(b, j, d, t, -1, NULL, 0, 9);
struct janas_grammar *g = janas_grammar_make(b);
janas_grammar_free(g);
janas_jsg_free(j);
diff --git a/tools/gguf2jns.c b/tools/gguf2jns.c
index e83656f..09953d5 100644
--- a/tools/gguf2jns.c
+++ b/tools/gguf2jns.c
@@ -14,8 +14,13 @@
* and 2112) is widened with zeros to the next one - rows of zero bytes in
* gate and up, a zero tail on each row of down: every weight type reads zero
* bytes as zero weights, so the products are the same, and the engine's
- * activations, quantized 256 at a time, fit. Gate and up fused in one
- * tensor (ffn_gate_up_exps) are split into the two matrices of the slot.
+ * activations, quantized 256 at a time, fit. Qwen3.8-Flash-Next's experts
+ * (qwen4exp, 640 wide) keep their width: the engine reads rows that end
+ * inside a group of 256 in the types stored in blocks of 32 or 64
+ * (janas_dot_rows_n), the only ones such a down can have, and the zeros
+ * would be a fifth more bytes read at every token. Gate and up fused in
+ * one tensor (ffn_gate_up_exps) are split into the two matrices of the
+ * slot.
*
* A resident tensor in MXFP4 (unsloth's Qwen3-Coder-Next files have their
* shared experts so, issue #18) is written as Q8_0, which holds it exactly:
@@ -327,6 +332,8 @@ int main(int argc, char **argv)
uint64_t d_model = need_uint(g0, key);
if (n_layer == 0 || n_layer > 4096 || n_expert == 0 || n_expert > 65536)
die("%s: layers or experts out of range", src);
+ /* the experts' width as it is (see the top of the file) */
+ int keep_width = strcmp(arch, "qwen4exp") == 0;
/* expert geometry per layer */
struct janas_jns_layer_v2 *layers = calloc(n_layer, sizeof(*layers));
@@ -366,10 +373,13 @@ int main(int argc, char **argv)
uint64_t rb = per / t->t->dims[1];
/* widened to a multiple of 256: the rows of gate and up, the
row of down */
- uint64_t new_rows = p < 2 ? wide(rows) : rows;
- uint64_t new_cols = p < 2 ? cols : wide(cols);
+ uint64_t new_rows = p < 2 && !keep_width ? wide(rows) : rows;
+ uint64_t new_cols = p < 2 || keep_width ? cols : wide(cols);
if (new_cols != cols && (rb * new_cols) % cols)
die("%s: cannot widen this type", key);
+ if (keep_width &&
+ janas_qtype_row_bytes((int)t->t->type, cols) != rb)
+ die("%s: a row the engine cannot read at this width", key);
uint64_t new_rb = rb * new_cols / cols;
off = align_up(off, MATRIX_ALIGN);
layers[l].m[p] =
diff --git a/tools/hf2jns_mtp.c b/tools/hf2jns_mtp.c
index 397b0e6..d48ceec 100644
--- a/tools/hf2jns_mtp.c
+++ b/tools/hf2jns_mtp.c
@@ -3,7 +3,8 @@
/*
* hf2jns_mtp.c - the multi-token prediction (MTP) block of Qwen3-Next, or of
* a dense Qwen3.5, from the original safetensors checkpoint into a one-layer
- * JNS file.
+ * JNS file; or the block of a GGUF draft head (unsloth's MTP files of
+ * Qwen3.8-Flash-Next, qwen4exp), copied as it is.
*
* GGUF conversions of these models drop the MTP block; the checkpoint keeps
* it under mtp.* (bf16). This tool quantizes the matrices to Q4_K with
@@ -26,13 +27,23 @@
*
* Usage: hf2jns_mtp <main.jns> <shard.safetensors>... <out.jns>
* hf2jns_mtp <main.jns> hf:<owner>/<repo>[@revision] <out.jns>
+ * hf2jns_mtp <main.jns> <head.gguf> <out.jns>
* The block may be spread over several shards: give every one that has a
* part of it (or one file holding just the block's tensors). With hf: the
* block's tensors alone are read from the repository on Hugging Face, with
* range requests - Qwen3.5-9B's 487 MB instead of its 14 GB of shards -
* into <out.jns>.mtp.safetensors, deleted once converted.
+ *
+ * A GGUF head holds the block as its last one (block_count less
+ * nextn_predict_layers, the trunk's blocks absent): its tensors are copied
+ * in their types, blk.N.* as blk.0.* and blk.N.nextn.* as mtp.*, its
+ * experts' three-dimensional tensors cut into the slots; its token
+ * embeddings and output head left out - the engine takes the main model's,
+ * as the head's "shared" files do. Its norms are the GGUF's, already in
+ * their effective form.
*/
#define _GNU_SOURCE
+#include <fcntl.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
@@ -42,6 +53,7 @@
#include "common/hf_fetch.h"
#include "common/pool.h"
+#include "llm/gguf.h"
#include "llm/jns.h"
#include "llm/quant.h"
@@ -252,47 +264,136 @@ static int shape_of(const char *name, struct st_tensor *t)
return 0;
}
-int main(int argc, char **argv)
+/* ---- a GGUF draft head: its last block as it is ---- */
+
+static struct janas_gguf head_g;
+static int head_fd = -1;
+/* the experts' tensors: gate, up, down */
+static const struct janas_gguf_tensor *head_exp[3];
+
+static void gguf_read(uint64_t at, void *dst, uint64_t n, const char *what)
{
- if (argc < 4 || argc - 3 > (int)(sizeof(shards) / sizeof(shards[0]))) {
- fprintf(stderr, "usage: hf2jns_mtp <main.jns> <shard.safetensors>... "
- "<out.jns>\n");
- return 2;
+ for (uint64_t done = 0; done < n;) {
+ ssize_t r =
+ pread(head_fd, (uint8_t *)dst + done, n - done, (off_t)(at + done));
+ if (r <= 0)
+ die("cannot read", what);
+ done += (uint64_t)r;
}
- const char *out_path = argv[argc - 1];
- struct janas_jns main_j;
- char err[256];
- if (janas_jns_open(argv[1], &main_j, err, sizeof(err)) != 0)
- die(err, argv[1]);
- char fetched[4096] = "";
- for (int i = 2; i < argc - 1; i++) {
- if (strncmp(argv[i], "hf:", 3) == 0) {
- if (*fetched)
- die("one hf: repository at a time", argv[i]);
- char repo[512], *rev;
- snprintf(repo, sizeof(repo), "%s", argv[i] + 3);
- if ((rev = strchr(repo, '@')))
- *rev++ = 0;
- snprintf(fetched, sizeof(fetched), "%s.mtp.safetensors", out_path);
- if (janas_hf_fetch_tensors(repo, rev, "mtp.", fetched, err,
- sizeof(err)) < 0) {
- remove(fetched);
- die(err, NULL);
- }
- st_open(&shards[n_shards++], fetched);
- } else {
- st_open(&shards[n_shards++], argv[i]);
+}
+
+/* The block's tensors into outs (the experts' apart), the slot's shape into
+ ly; the main model's experts and width must be the head's. */
+static void gguf_collect(const struct janas_jns *main_j, const char *path,
+ struct janas_jns_layer *ly)
+{
+ char err[256], key[128], name[128];
+ if (janas_gguf_open(path, &head_g, err, sizeof(err)) != 0)
+ die(err, path);
+ head_fd = open(path, O_RDONLY);
+ if (head_fd < 0)
+ die("cannot open", path);
+ struct janas_gguf_str a;
+ if (janas_gguf_string(&head_g, "general.architecture", &a) != 0 ||
+ a.n != strnlen(main_j->h.arch, sizeof(main_j->h.arch)) ||
+ memcmp(a.p, main_j->h.arch, a.n) != 0)
+ die("not a head of the main model's architecture", path);
+ uint64_t blocks = 0, nextn = 0;
+ snprintf(key, sizeof(key), "%.32s.block_count", main_j->h.arch);
+ janas_gguf_uint(&head_g, key, &blocks);
+ snprintf(key, sizeof(key), "%.32s.nextn_predict_layers", main_j->h.arch);
+ janas_gguf_uint(&head_g, key, &nextn);
+ if (nextn != 1 || blocks < 2)
+ die("not a head of one prediction block", path);
+ uint64_t blk = blocks - nextn;
+ char pre[32], nx[48];
+ snprintf(pre, sizeof(pre), "blk.%llu.", (unsigned long long)blk);
+ snprintf(nx, sizeof(nx), "%snextn.", pre);
+ static const char *const ex[3] = {
+ "ffn_gate_exps.weight", "ffn_up_exps.weight", "ffn_down_exps.weight"};
+ for (uint64_t i = 0; i < head_g.n_tensors; i++) {
+ const struct janas_gguf_tensor *t = &head_g.t[i];
+ if (t->name.n >= sizeof(name))
+ continue;
+ memcpy(name, t->name.p, t->name.n);
+ name[t->name.n] = 0;
+ if (strncmp(name, pre, strlen(pre)) != 0)
+ continue; /* token_embd, output: the main model's */
+ int e = -1;
+ for (int p = 0; p < 3; p++)
+ if (!strcmp(name + strlen(pre), ex[p]))
+ e = p;
+ if (e >= 0) {
+ head_exp[e] = t;
+ continue;
}
+ if (n_outs == (int)(sizeof(outs) / sizeof(outs[0])))
+ die("too many tensors in the block", path);
+ struct out_tensor *o = &outs[n_outs++];
+ if (!strncmp(name, nx, strlen(nx)))
+ snprintf(o->name, sizeof(o->name), "mtp.%s", name + strlen(nx));
+ else
+ snprintf(o->name, sizeof(o->name), "blk.0.%s", name + strlen(pre));
+ o->type = t->type;
+ o->n_dims = t->n_dims;
+ for (int d = 0; d < 4; d++)
+ o->dims[d] = d < (int)t->n_dims ? t->dims[d] : 1;
+ o->bytes = t->bytes;
+ o->data = malloc(t->bytes);
+ if (!o->data)
+ die("out of memory", NULL);
+ gguf_read(head_g.data_start + t->offset, o->data, t->bytes, name);
+ printf(" %-34s <- %s\n", o->name, name);
}
- /* a dense model is one expert a layer with no router (gguf2jns) */
- const uint32_t n_exp = (uint32_t)main_j.h.n_expert;
- const int dense = n_exp == 1;
- int cpus[64], nt = 12;
- for (int i = 0; i < nt; i++)
- cpus[i] = i;
- struct janas_pool *pool = janas_pool_create(nt, cpus);
+ uint64_t off = 0;
+ for (int p = 0; p < 3; p++) {
+ const struct janas_gguf_tensor *t = head_exp[p];
+ if (!t || t->n_dims != 3 || t->dims[2] != main_j->h.n_expert)
+ die("experts missing or not as many as the main model's", ex[p]);
+ uint64_t per = t->bytes / t->dims[2];
+ if (per * t->dims[2] != t->bytes ||
+ janas_qtype_row_bytes((int)t->type, t->dims[0]) * t->dims[1] != per)
+ die("experts of an unexpected size", ex[p]);
+ off = align_up(off, MATRIX_ALIGN);
+ ly->m[p] = (struct janas_jns_matrix){.type = t->type,
+ .rows = (uint32_t)t->dims[1],
+ .cols = (uint32_t)t->dims[0],
+ .offset = off,
+ .bytes = per};
+ off += per;
+ }
+ if (ly->m[0].cols != main_j->h.d_model ||
+ ly->m[2].rows != main_j->h.d_model)
+ die("experts of another width than the main model's", path);
+ ly->slot_bytes = align_up(off, JANAS_JNS_ALIGN);
+ for (int i = 0; i < 3; i++)
+ ly->level_bytes[i] = ly->slot_bytes;
+}
+
+/* expert e's slot from the head's three tensors */
+static void gguf_slot(const struct janas_jns_layer *ly, uint32_t e,
+ uint8_t *slot)
+{
+ for (int p = 0; p < 3; p++)
+ gguf_read(head_g.data_start + head_exp[p]->offset + e * ly->m[p].bytes,
+ slot + ly->m[p].offset, ly->m[p].bytes, "an expert");
+}
- const char *L = "mtp.layers.0.";
+/* ---- a safetensors checkpoint: quantized here ---- */
+
+static struct janas_pool *ckpt_pool;
+static int ckpt_dense;
+static const char *const ckpt_parts[3] = {"gate_proj", "up_proj", "down_proj"};
+#define CKPT_LAYER "mtp.layers.0."
+
+static void ckpt_collect(const struct janas_jns *main_j,
+ struct janas_jns_layer *ly)
+{
+ /* a dense model is one expert a layer with no router (gguf2jns) */
+ const uint32_t n_exp = (uint32_t)main_j->h.n_expert;
+ const int dense = ckpt_dense = n_exp == 1;
+ struct janas_pool *pool = ckpt_pool;
+ const char *L = CKPT_LAYER;
char a[160];
#define SRC(x) (snprintf(a, sizeof(a), "%s%s", L, x), a)
add(pool, SRC("input_layernorm.weight"), "blk.0.attn_norm.weight", T_F32,
@@ -328,13 +429,11 @@ int main(int argc, char **argv)
/* expert slot: gate, up, down in Q4_K, shaped as the checkpoint's first
expert (Qwen3-Next: 512 x 2048, 512 x 2048, 2048 x 512), or as its
dense feed-forward */
- struct janas_jns_layer ly = {0};
uint64_t off = 0;
- const char *parts[3] = {"gate_proj", "up_proj", "down_proj"};
uint32_t rows[3], cols[3];
for (int p = 0; p < 3; p++) {
char nm[160];
- slot_name(nm, sizeof(nm), L, dense, 0, parts[p]);
+ slot_name(nm, sizeof(nm), L, dense, 0, ckpt_parts[p]);
struct st_tensor t;
if (n_shards == 0 || !shape_of(nm, &t) || t.dims != 2)
die("feed-forward matrix missing or not 2-D", nm);
@@ -343,20 +442,91 @@ int main(int argc, char **argv)
}
for (int p = 0; p < 3; p++) {
off = align_up(off, MATRIX_ALIGN);
- ly.m[p] = (struct janas_jns_matrix){
+ ly->m[p] = (struct janas_jns_matrix){
.type = JANAS_Q4_K,
.rows = rows[p],
.cols = cols[p],
.offset = off,
.bytes = (uint64_t)rows[p] * cols[p] / JANAS_QK *
sizeof(struct janas_block_q4k)};
- off += ly.m[p].bytes;
+ off += ly->m[p].bytes;
}
- ly.slot_bytes = align_up(off, JANAS_JNS_ALIGN);
+ ly->slot_bytes = align_up(off, JANAS_JNS_ALIGN);
/* no bit planes: the three levels of a version 3 slot are all of it
(left at zero, the file was refused: issue #6) */
for (int i = 0; i < 3; i++)
- ly.level_bytes[i] = ly.slot_bytes;
+ ly->level_bytes[i] = ly->slot_bytes;
+#undef SRC
+}
+
+/* expert e's slot, quantized from the checkpoint */
+static void ckpt_slot(const struct janas_jns_layer *ly, uint32_t e,
+ uint8_t *slot)
+{
+ for (int p = 0; p < 3; p++) {
+ char nm[160];
+ slot_name(nm, sizeof(nm), CKPT_LAYER, ckpt_dense, e, ckpt_parts[p]);
+ struct st_tensor t;
+ uint64_t n, bytes;
+ float *x = st_load(nm, &n, &t);
+ if (t.dims != 2 || t.shape[0] != ly->m[p].rows ||
+ t.shape[1] != ly->m[p].cols)
+ die("unexpected expert shape", nm);
+ uint8_t *q = to_q4k(ckpt_pool, x, ly->m[p].rows, ly->m[p].cols, &bytes);
+ memcpy(slot + ly->m[p].offset, q, bytes);
+ free(q);
+ free(x);
+ }
+}
+
+int main(int argc, char **argv)
+{
+ if (argc < 4 || argc - 3 > (int)(sizeof(shards) / sizeof(shards[0]))) {
+ fprintf(stderr, "usage: hf2jns_mtp <main.jns> <shard.safetensors>... "
+ "<out.jns>\n"
+ " hf2jns_mtp <main.jns> <head.gguf> <out.jns>\n");
+ return 2;
+ }
+ const char *out_path = argv[argc - 1];
+ struct janas_jns main_j;
+ char err[256];
+ if (janas_jns_open(argv[1], &main_j, err, sizeof(err)) != 0)
+ die(err, argv[1]);
+ const uint32_t n_exp = (uint32_t)main_j.h.n_expert;
+ size_t la = strlen(argv[2]);
+ int gguf = argc == 4 && la > 5 && !strcmp(argv[2] + la - 5, ".gguf");
+ char fetched[4096] = "";
+ struct janas_jns_layer ly = {0};
+ struct janas_pool *pool = NULL;
+ if (gguf) {
+ gguf_collect(&main_j, argv[2], &ly);
+ } else {
+ for (int i = 2; i < argc - 1; i++) {
+ if (strncmp(argv[i], "hf:", 3) == 0) {
+ if (*fetched)
+ die("one hf: repository at a time", argv[i]);
+ char repo[512], *rev;
+ snprintf(repo, sizeof(repo), "%s", argv[i] + 3);
+ if ((rev = strchr(repo, '@')))
+ *rev++ = 0;
+ snprintf(fetched, sizeof(fetched), "%s.mtp.safetensors",
+ out_path);
+ if (janas_hf_fetch_tensors(repo, rev, "mtp.", fetched, err,
+ sizeof(err)) < 0) {
+ remove(fetched);
+ die(err, NULL);
+ }
+ st_open(&shards[n_shards++], fetched);
+ } else {
+ st_open(&shards[n_shards++], argv[i]);
+ }
+ }
+ int cpus[64], nt = 12;
+ for (int i = 0; i < nt; i++)
+ cpus[i] = i;
+ pool = ckpt_pool = janas_pool_create(nt, cpus);
+ ckpt_collect(&main_j, &ly);
+ }
/* layout, as tools/gguf2jns.c */
uint64_t A = JANAS_JNS_ALIGN, layer_table = A;
@@ -392,21 +562,14 @@ int main(int argc, char **argv)
/* experts, one slot each, with the CRC-32 of the whole slot */
uint8_t *slot = calloc(1, ly.slot_bytes);
+ if (!slot)
+ die("out of memory", NULL);
for (uint32_t e = 0; e < n_exp; e++) {
memset(slot, 0, ly.slot_bytes);
- for (int p = 0; p < 3; p++) {
- char nm[160];
- slot_name(nm, sizeof(nm), L, dense, e, parts[p]);
- struct st_tensor t;
- uint64_t n, bytes;
- float *x = st_load(nm, &n, &t);
- if (t.dims != 2 || t.shape[0] != rows[p] || t.shape[1] != cols[p])
- die("unexpected expert shape", nm);
- uint8_t *q = to_q4k(pool, x, rows[p], cols[p], &bytes);
- memcpy(slot + ly.m[p].offset, q, bytes);
- free(q);
- free(x);
- }
+ if (gguf)
+ gguf_slot(&ly, e, slot);
+ else
+ ckpt_slot(&ly, e, slot);
sums[e] = janas_crc32(0, slot, ly.slot_bytes);
if (fseeko(out, (off_t)slot_off[e], 0) ||
fwrite(slot, 1, ly.slot_bytes, out) != ly.slot_bytes)
@@ -474,7 +637,12 @@ int main(int argc, char **argv)
free(sums);
for (int i = 0; i < n_outs; i++)
free(outs[i].data);
- janas_pool_destroy(pool);
+ if (pool)
+ janas_pool_destroy(pool);
+ if (gguf) {
+ close(head_fd);
+ janas_gguf_free(&head_g);
+ }
janas_jns_close(&main_j);
if (*fetched)
remove(fetched);