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.

Develop 2026-10-14 (3): the MXFP4_MOE files run - MXFP4 experts read as they are, output heads in BF16 or F16 read as Q8_0

camauria234d57e3688
diff --git a/ChangeLog.md b/ChangeLog.md
index 8592b5d..e8937bb 100644
--- a/ChangeLog.md
+++ b/ChangeLog.md
@@ -2,6 +2,12 @@
> Curated, user-facing summary of completed work, newest first.
+## [2026-10-14] - MXFP4 experts, and output heads in BF16
+
+- **The "MXFP4_MOE" files run.** Their routed experts are MXFP4 (OCP's microscaling format: 32 four-bit values and a power of two a block), which gguf2jns refused and the engine could not read. The engine has MXFP4 products now - IQ4_NL's kernel with the block's power of two for a scale, the same bits on AVX2 and AVX-VNNI, for one vector or eight, and the same bits as the exact Q8_0 rewrite of the same blocks - and gguf2jns keeps such experts as they are. noctrex's Qwen3.6-35B-A3B-MXFP4_MOE against llama.cpp on the same file: 199 of 200 most likely tokens agree, mean logit difference 0.105 (on Italian prose 63 of 64, 0.085), closer than bartowski's Q4_K_M to its own llama.cpp (196, 0.146; 60, 0.135). The file itself, against llama.cpp's Q8_0 of the same model, is further than that Q4_K_M (378 against 387 of 400 most likely tokens, 364 against 370 on Italian prose) and 9% smaller (20.3 GB against 22.3). On 12 threads of the development laptop the MXFP4 product reads 66 GB/s where IQ4_NL reads 73: 4% fewer weights a second, a block of it being 17 bytes to IQ4_NL's 18 (its scales come from a table: computed in the kernel, they took registers the unpacking needs and it ran 14% slower).
+- **An output head in BF16 or F16 is read as Q8_0.** The "MXFP4_MOE" files keep theirs in BF16 (1,017 MB on Qwen3.6-35B-A3B), which the engine refused. It is rewritten once, as the model opens, the way llama.cpp's quantization makes Q8_0, into 540 MB - the head is read whole at every token - and the model's description says so; the file keeps the BF16. A token embedding table in BF16 or F16 is rewritten the same way.
+
+
## [2026-10-14] - janas-try's answers, and the GPU's subgroup operations
- **janas-try's answers match again, on every machine** (issue #23, reported by clethbridgedaniel): since 1 October janas-chat starts Janas's own services and writes their catalog into the system message, even with `--system ""`, and janas-try asked its fixed question that way, while the expected fingerprints date from 25 September: no level could match, on any machine. The engine was right to the bit. Built from clethbridgedaniel's commit, here on an Intel Core Ultra 9 185H with AVX-VNNI and GCC 14, CPU only, Qwen3-Next-80B answers with `f393d4bfb674402f`, the fingerprint the report gave from a Ryzen 9 5900HX with AVX2 and GCC 16 - the same without the MTP drafts and with the AVX2 kernels forced - and with `4aae064b918a0e75`, the expected one, without the services; Qwen3-4B likewise gives its expected `c08f0f936eeba5cb` without them. janas-try now asks without MCP servers and without the machine's measure (`--no-mcp --no-tune`), in configuration, cache and data directories of its own, so that neither settings saved with `/save-config` nor a system message kept from an earlier chat can change the answer. The medium level expects `e49b0016cedf6c50` now: Qwen3.6-35B-A3B's Q8_0 tensors take the activations with a scale per 32 since 27 September (the fix that brought Gemma-4-26B from 147 to 156 of 200 against an exact float64 pass), and janas-try still expected the answer from before. All four levels give their fingerprints here, with the GPU and without it.
diff --git a/MODELS.md b/MODELS.md
index 739e53e..3b123af 100644
--- a/MODELS.md
+++ b/MODELS.md
@@ -15,9 +15,11 @@ Converting a GGUF into `.jns` does not retrain, re-quantize or prune anything. I
- one slot per (layer, expert), so the experts a token needs can be read from disk without reading anything else;
- the experts laid out by co-activation, so the reads fall near each other;
- the model's metadata carried over byte for byte;
-- a tensor in MXFP4 (unsloth's Qwen3-Coder-Next files have their shared experts so) written as Q8_0, which holds each of its weights exactly: the same values in another encoding, at twice the bytes;
+- a resident tensor in MXFP4 (unsloth's Qwen3-Coder-Next files have their shared experts so) written as Q8_0, which holds each of its weights exactly: the same values in another encoding, at twice the bytes; routed experts in MXFP4 (the "MXFP4_MOE" files) stay as they are, the engine reads MXFP4;
- optionally (`tools/jns_planes`) the experts' down matrix cut into three planes of two bits, which is a reordering of the same bits: with all three planes the weights are the ones the file came with, to the last bit.
+One thing changes when a model is opened, in memory and not in the file: an output head in BF16 or F16 (the "MXFP4_MOE" files keep theirs in BF16) is read as Q8_0, rewritten once as llama.cpp's quantization makes Q8_0: about half the bytes, and the head is read whole at every token. That is a re-quantization, and the model's description says so. Its cost, measured on noctrex's Qwen3.6-35B-A3B-MXFP4_MOE against llama.cpp on the same file, which keeps the head in BF16: 199 of 200 most likely tokens agree, mean logit difference 0.105, closer than a Q4_K_M file to its own llama.cpp, where nothing is re-quantized (196, 0.146).
+
Anyone redistributing a converted file has to say this, because the licences that allow redistribution (Apache 2.0 among them) require that a modified work state that it was modified.
## Converting twice gives the same file
diff --git a/docs/features.md b/docs/features.md
index 1e29538..0f8ca3c 100644
--- a/docs/features.md
+++ b/docs/features.md
@@ -1,6 +1,6 @@
# What works now
-- **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). 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.
+- **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.
- **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.
@@ -24,4 +24,4 @@
- **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, the experts of a mixture in one launch per layer, 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 agrees as often as the exact way (within 2-3 of 256 on every model measured, the mean difference of the scores within 2%). On the development laptop Qwen3-4B reads the table's prompt (below) at 220 tok/s with it and at 85 without the GPU, and every model compared but one reads faster than llama.cpp does on the same GPU (the exception: unsloth's 3-bit Qwen3-30B, whose Q3_K experts stay on the CPU). `JANAS_GPU_FAST_PROMPT=0` reads them the exact way.
- **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.
- **Light experts not in memory are left out** when the expert cache holds less than a fifth of the experts: an expert the router weighs below 0.8/k of a token (0.95/k under a tenth), and that would have to be read from disk, is not read and not computed, and the token's other experts take its share — the cache-conditional routing of Skliar et al. (TMLR 2025). Until 8 October 2026 the engine dropped the lightest experts whether in memory or not (six of ten, or eight). Qwen3-Next-80B, four bits, 400 tokens against llama.cpp, most likely token agreeing and tokens/s: with a 2 GiB cache all ten 393 at 11.0, six 383 at 20.9, now 380 at 26.4; with 4 GiB 393 at 20.7, six 383 at 27.6, now 384 at 27.7, with the mean logit difference 0.36 against 0.53; on Italian prose with 8 GiB 382 at 18.7, eight 361 at 21.4, now 372 at 23.7. On Qwen3-30B-A3B and Qwen3.6-35B-A3B with caches of 9% and 18% of their experts (1.5-3.5 GiB), on code and Italian prose, it was faster than the old way every time, by 9-37% (Qwen3-30B-A3B with 3 GiB: 28.7 tokens/s against 24.4, 381 tokens agreeing of 400 against 377), with a mean logit difference as low or lower in seven of the eight; it agreed on fewer most likely tokens in two (Italian, 64 tokens: 47 against 54 on the 30B with 3 GiB). `/experts n` (or `0`) asks for a number of experts and turns it off.
-- **Every weight type near the memory's speed**, and the same results to the bit on every path (AVX2, AVX-VNNI, GPU) for a reply's tokens (and for a prompt with `JANAS_GPU_FAST_PROMPT=0`). `tests/bench_kernels` measures each type's product on its own: writing a token, on 12 threads of the development laptop, Q8_0 84 GB/s, Q6_K 73, Q4_K 69, the other 4- and 5-bit types 59-68, Q3_K 61 (48 until 9 October 2026, when its unpacking went through the stack), IQ3_S 25, against the 81 GB/s the memory gives; and a token of Qwen3-Next-80B-A3B, Qwen3.6-35B-A3B, Qwen3-30B-A3B or a 27B, phase by phase, reads its weights at 59-62 GB/s on average. Until 6 October 2026 the Q3_K, IQ4_XS and IQ3_S products were six to thirteen times slower than that (their kernels converted a scale by calling a function), which made unsloth's UD files, which mix those types in, write at half their speed: Qwen3.8-27B UD-Q4_K_XL went from 1.7 tokens/s to 3.6.
+- **Every weight type near the memory's speed**, and the same results to the bit on every path (AVX2, AVX-VNNI, GPU) for a reply's tokens (and for a prompt with `JANAS_GPU_FAST_PROMPT=0`). `tests/bench_kernels` measures each type's product on its own: writing a token, on 12 threads of the development laptop, Q8_0 84 GB/s, Q6_K 73, Q4_K 69, the other 4- and 5-bit types 59-68, Q3_K 61 (48 until 9 October 2026, when its unpacking went through the stack), IQ3_S 25, against the 81 GB/s the memory gives (MXFP4, since 14 October 2026, at 66 GB/s where IQ4_NL reads at 73 in the same run: 4% fewer weights a second, a block of it being 17 bytes to IQ4_NL's 18); and a token of Qwen3-Next-80B-A3B, Qwen3.6-35B-A3B, Qwen3-30B-A3B or a 27B, phase by phase, reads its weights at 59-62 GB/s on average. Until 6 October 2026 the Q3_K, IQ4_XS and IQ3_S products were six to thirteen times slower than that (their kernels converted a scale by calling a function), which made unsloth's UD files, which mix those types in, write at half their speed: Qwen3.8-27B UD-Q4_K_XL went from 1.7 tokens/s to 3.6.
diff --git a/docs/models.md b/docs/models.md
index bd22cd7..8f49dfc 100644
--- a/docs/models.md
+++ b/docs/models.md
@@ -10,7 +10,7 @@ bin/x86_64-linux/janas-chat ~/.local/share/janas/models/gemma-4-e4b-it-q4km.jns
A download stopped halfway resumes where it stopped, and a file already there is checked rather than fetched again. `--from-gguf` takes the other way: the original GGUF the catalog names, downloaded and converted on your machine to the very same fingerprints (the GGUF goes once converted, `--keep` keeps it); `janas-get` takes it by itself where a converted file is not on Hugging Face. Any other file: `janas-get hf:<owner>/<repo>/<file.jns|file.gguf>`, checked against the SHA-256 Hugging Face lists for it. The memory column of `janas-get list` is the smallest machine each model is meant for. The rest of this section is the same by hand.
-Janas-LLM reads its own format, `.jns`, converted from a **Q4_K_M** GGUF (Q3_K, IQ3_S, IQ4_NL, IQ4_XS, Q4_0, Q4_1 and Q5_1 convert too, and so do unsloth's "UD" files made of them; IQ2 and IQ1 types, not yet). Keep both in `models/` next to the sources — the tools take a path, so anywhere works, but that is where these examples put them, and where the project keeps its own.
+Janas-LLM reads its own format, `.jns`, converted from a **Q4_K_M** GGUF (Q3_K, IQ3_S, IQ4_NL, IQ4_XS, Q4_0, Q4_1 and Q5_1 convert too, and so do unsloth's "UD" files made of them and the "MXFP4_MOE" files, whose experts are MXFP4; IQ2 and IQ1 types, not yet). Keep both in `models/` next to the sources — the tools take a path, so anywhere works, but that is where these examples put them, and where the project keeps its own.
| Model | GGUF converted here | GGUF | `.jns` | Memory it likes |
|---|---|---|---|---|
diff --git a/src/llm/api.c b/src/llm/api.c
index 3d41cba..2114698 100644
--- a/src/llm/api.c
+++ b/src/llm/api.c
@@ -510,9 +510,14 @@ int32_t janas_llm_open(const char *path, const struct janas_llm_params *pp,
uint64_t d0, d1;
janas_llm_model_head_requant(llm->m, &d0, &d1);
if (d0 && w >= 0 && (size_t)w < sizeof(bits))
+ w += snprintf(bits + w, sizeof(bits) - (size_t)w,
+ ", output head at 4 bits (%.0f -> %.0f MB)", 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,
- ", output head at 4 bits (%.0f -> %.0f MB)", d0 / 1e6,
- d1 / 1e6);
+ ", output head %s read as Q8_0 (%.0f -> %.0f MB)",
+ h16 == 30 ? "BF16" : "F16", d0 / 1e6, d1 / 1e6);
llm->cache_bytes = cache;
llm->reserve_bytes = reserve;
llm->kv_token = kv_token;
diff --git a/src/llm/model.h b/src/llm/model.h
index 8823a32..90219e2 100644
--- a/src/llm/model.h
+++ b/src/llm/model.h
@@ -87,6 +87,10 @@ int janas_llm_model_expert_bits_auto(const struct janas_llm_model *m);
and after; 0 and 0 if it was not. */
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. */
+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
engine chose it. */
int janas_llm_model_attn_scores(const struct janas_llm_model *m);
diff --git a/src/llm/model_impl.h b/src/llm/model_impl.h
index 26a248c..bfcabcd 100644
--- a/src/llm/model_impl.h
+++ b/src/llm/model_impl.h
@@ -279,6 +279,10 @@ struct janas_llm_model {
int warm_use; /* preload the most used experts, and count them */
uint32_t *use_count; /* experts used this session, n_layer * n_expert */
uint64_t head_before, head_after; /* output head requantized (head_bits) */
+ /* the head's type in the file (BF16 30, F16 1) when rewritten as Q8_0 at
+ load (head16_to_q8_0), else 0; and the bytes before and after */
+ int head16;
+ uint64_t head16_before, head16_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];
diff --git a/src/llm/model_load.c b/src/llm/model_load.c
index cbb2ab9..5d997ce 100644
--- a/src/llm/model_load.c
+++ b/src/llm/model_load.c
@@ -212,6 +212,85 @@ static void *rq_worker(void *arg)
return NULL;
}
+/*
+ * An output head or embedding table in BF16 or F16 - the MXFP4_MOE files
+ * keep output.weight in BF16, 1.02 GB for Qwen3.6-35B-A3B's 248,320 rows -
+ * rewritten once, at load, as Q8_0, as llama.cpp's quantize would make it:
+ * the products read quantized types, and the head, read whole at every
+ * token, is read at half the bytes (BF16 would cost some 12 ms a token at
+ * 81 GB/s). Built beside and copied in place, Q8_0 being the smaller.
+ */
+struct h16_job {
+ const uint8_t *src;
+ struct janas_block_q8_0 *dst;
+ size_t first, count; /* blocks of 32 weights */
+ int bf16;
+};
+
+static void *h16_worker(void *arg)
+{
+ struct h16_job *r = arg;
+ float buf[32];
+ for (size_t b = r->first; b < r->first + r->count; b++) {
+ const uint16_t *s = (const uint16_t *)(r->src + b * 64);
+ for (int j = 0; j < 32; j++) {
+ if (r->bf16) {
+ uint32_t bits = (uint32_t)s[j] << 16;
+ memcpy(&buf[j], &bits, sizeof(float));
+ } else
+ buf[j] = janas_fp16_to_fp32(s[j]);
+ }
+ janas_q8_0_quantize(buf, r->dst + b, 32);
+ }
+ return NULL;
+}
+
+static void head16_to_q8_0(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->type != 1) || 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 / 64;
+ uint8_t *src = (uint8_t *)data(m, t);
+ struct janas_block_q8_0 *dst = malloc(nb * sizeof(*dst));
+ if (!dst)
+ return;
+ pthread_t th[NT];
+ struct h16_job jb[NT];
+ int started = 0;
+ for (int k = 0; k < nt; k++) {
+ size_t a = nb * k / nt, b = nb * (k + 1) / nt;
+ jb[k] = (struct h16_job){.src = src,
+ .dst = dst,
+ .first = a,
+ .count = b - a,
+ .bf16 = t->type == 30};
+ if (pthread_create(&th[k], NULL, h16_worker, &jb[k]) == 0)
+ started |= 1 << k;
+ else
+ h16_worker(&jb[k]);
+ }
+ for (int k = 0; k < nt; k++)
+ if (started & 1 << k)
+ pthread_join(th[k], NULL);
+ if (!strcmp(name, "output.weight") ||
+ !janas_jns_tensor(&m->j, "output.weight")) {
+ m->head16 = (int)t->type;
+ m->head16_before = t->bytes;
+ m->head16_after = nb * sizeof(*dst);
+ }
+ t->type = JANAS_Q8_0;
+ t->bytes = nb * sizeof(*dst);
+ memcpy(src, dst, t->bytes);
+ free(dst);
+}
+
static void requant_head(struct janas_llm_model *m)
{
/* tied embeddings (the small Qwen): the table is the head */
@@ -744,6 +823,8 @@ struct janas_llm_model *janas_llm_model_load(const char *path,
}
done += (uint64_t)r;
}
+ head16_to_q8_0(m, "output.weight");
+ head16_to_q8_0(m, "token_embd.weight");
const char *hbe = getenv("JANAS_HEAD_BITS");
if ((hbe ? atoi(hbe) : o->head_bits) == 4 && !m->resident_gpu)
requant_head(m);
@@ -1441,6 +1522,14 @@ void janas_llm_model_head_requant(const struct janas_llm_model *m,
*after = m->head_after;
}
+int janas_llm_model_head16(const struct janas_llm_model *m, uint64_t *before,
+ uint64_t *after)
+{
+ *before = m->head16_before;
+ *after = m->head16_after;
+ return m->head16;
+}
+
int janas_llm_model_experts_auto(const struct janas_llm_model *m)
{
return m->k_auto;
diff --git a/src/llm/quant.c b/src/llm/quant.c
index 1b09ce3..3c4a9c7 100644
--- a/src/llm/quant.c
+++ b/src/llm/quant.c
@@ -75,11 +75,21 @@ uint16_t janas_fp32_to_fp16(float f)
return (uint16_t)half;
}
+const int8_t janas_mxfp4_values[16] = {0, 1, 2, 3, 4, 6, 8, 12,
+ 0, -1, -2, -3, -4, -6, -8, -12};
+
+float janas_e8m0_half(uint8_t e)
+{
+ uint32_t bits = e < 2 ? 0x00200000u << e : (uint32_t)(e - 1) << 23;
+ float f;
+ memcpy(&f, &bits, sizeof(f));
+ return f;
+}
+
int janas_mxfp4_to_q8_0(const uint8_t *in, struct janas_block_q8_0 *out,
size_t n, size_t *tiny)
{
- static const int8_t kv[16] = {0, 1, 2, 3, 4, 6, 8, 12,
- 0, -1, -2, -3, -4, -6, -8, -12};
+ const int8_t *kv = janas_mxfp4_values;
for (size_t b = 0; b < n; b++) {
const uint8_t *x = in + b * JANAS_MXFP4_BLOCK;
int p = (int)x[0] - 128; /* the block's power of two */
@@ -723,6 +733,19 @@ float janas_q5k_dot_ref(const struct janas_block_q5k *w,
return acc - accm;
}
+void janas_q8_0_quantize(const float *x, struct janas_block_q8_0 *y, size_t n)
+{
+ for (size_t b = 0; b < n / 32; b++, x += 32) {
+ float amax = 0.0f;
+ for (int j = 0; j < 32; j++)
+ amax = fmaxf(amax, fabsf(x[j]));
+ float d = amax / 127.0f, id = d != 0.0f ? 1.0f / d : 0.0f;
+ y[b].d = janas_fp32_to_fp16(d);
+ for (int j = 0; j < 32; j++)
+ y[b].qs[j] = (int8_t)roundf(x[j] * id);
+ }
+}
+
void janas_q8_0_dequantize(const struct janas_block_q8_0 *x, float *y, size_t n)
{
for (size_t b = 0; b < n / 32; b++) {
@@ -806,6 +829,36 @@ float janas_iq4nl_dot_ref(const struct janas_block_iq4nl *w,
return nib_dot_ref(w, x, nb, janas_iq4_values);
}
+void janas_mxfp4_dequantize(const struct janas_block_mxfp4 *x, float *y,
+ size_t n)
+{
+ for (size_t b = 0; b < n / 32; b++, y += 32) {
+ float d = janas_e8m0_half(x[b].e);
+ for (int l = 0; l < 16; l++) {
+ y[l] = d * janas_mxfp4_values[x[b].qs[l] & 15];
+ y[l + 16] = d * janas_mxfp4_values[x[b].qs[l] >> 4];
+ }
+ }
+}
+
+/* As IQ4_NL: one lane per block position, each block's exact sum times its
+ power of two and the activations' scale. */
+float janas_mxfp4_dot_ref(const struct janas_block_mxfp4 *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_mxfp4 *wk = w + 8 * b + k;
+ int32_t dot =
+ nib_sum32(wk->qs, x[b].qs32 + 32 * k, janas_mxfp4_values);
+ acc[k] =
+ fmaf(janas_e8m0_half(wk->e) * 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]));
+}
+
_Static_assert(sizeof(struct janas_block_q4_0) ==
sizeof(struct janas_block_iq4nl) &&
offsetof(struct janas_block_q4_0, qs) ==
@@ -1066,6 +1119,12 @@ static inline void unpack_scales_u32(const uint8_t *q, uint32_t out[4])
out[3] = ((c >> 4) & low4) | (((b >> 6) & top2) << 4); /* mins 4-7 */
}
+/* MXFP4's scales, janas_e8m0_half of every byte, for the kernels: read
+ from a table they keep the vector registers the unpacking needs (the
+ shifts and the blend held three constants, spilled on every block: 14%
+ slower than IQ4_NL on 12 threads). Filled before main. */
+static float e8m0_tab[256];
+
/* The kernels proper: quant_dotn.h, once for AVX2 and once for AVX-VNNI. */
#define DOTN(name) name##_avx2
#define DOTN_TARGET __attribute__((target("avx2,fma,f16c")))
@@ -1102,6 +1161,9 @@ typedef float (*dotnl_fn)(const struct janas_block_iq4nl *,
typedef float (*dotxs_fn)(const struct janas_block_iq4xs *,
const struct janas_block_q8k *, size_t);
static dotnl_fn dotnl_impl = janas_iq4nl_dot_ref;
+typedef float (*dotmx_fn)(const struct janas_block_mxfp4 *,
+ const struct janas_block_q8k *, size_t);
+static dotmx_fn dotmx_impl = janas_mxfp4_dot_ref;
static dotxs_fn dotxs_impl = janas_iq4xs_dot_ref;
typedef float (*dot40_fn)(const struct janas_block_q4_0 *,
const struct janas_block_q8k *, size_t);
@@ -1128,6 +1190,8 @@ static int use_vnni;
/* Resolved before main: the kernels are first called from pool threads. */
__attribute__((constructor)) static void dot_kernels_init(void)
{
+ for (int e = 0; e < 256; e++)
+ e8m0_tab[e] = janas_e8m0_half((uint8_t)e);
#if defined(__x86_64__)
__builtin_cpu_init();
if (__builtin_cpu_supports("avx2") && __builtin_cpu_supports("fma") &&
@@ -1137,6 +1201,7 @@ __attribute__((constructor)) static void dot_kernels_init(void)
dot5_impl = q5k_dot_avx2;
dot80_impl = q8_0_dot_avx2;
dotnl_impl = iq4nl_dot_avx2;
+ dotmx_impl = mxfp4_dot_avx2;
dotxs_impl = iq4xs_dot_avx2;
dot40_impl = q4_0_dot_avx2;
dot51_impl = q5_1_dot_avx2;
@@ -1153,6 +1218,7 @@ __attribute__((constructor)) static void dot_kernels_init(void)
dot5_impl = q5k_dot_vnni;
dot80_impl = q8_0_dot_vnni;
dotnl_impl = iq4nl_dot_vnni;
+ dotmx_impl = mxfp4_dot_vnni;
dotxs_impl = iq4xs_dot_vnni;
dot40_impl = q4_0_dot_vnni;
dot51_impl = q5_1_dot_vnni;
@@ -1201,6 +1267,12 @@ float janas_iq4xs_dot(const struct janas_block_iq4xs *w,
return dotxs_impl(w, x, nb);
}
+float janas_mxfp4_dot(const struct janas_block_mxfp4 *w,
+ const struct janas_block_q8k *x, size_t nb)
+{
+ return dotmx_impl(w, x, nb);
+}
+
float janas_q4_0_dot(const struct janas_block_q4_0 *w,
const struct janas_block_q8k *x, size_t nb)
{
@@ -1291,6 +1363,8 @@ size_t janas_qtype_block_size(int type)
return 8 * sizeof(struct janas_block_q8_0);
case JANAS_IQ4_NL:
return 8 * sizeof(struct janas_block_iq4nl);
+ case JANAS_MXFP4:
+ return 8 * sizeof(struct janas_block_mxfp4);
case JANAS_Q4_0:
return 8 * sizeof(struct janas_block_q4_0);
case JANAS_Q5_1:
@@ -1326,6 +1400,9 @@ void janas_dequantize(int type, const void *w, float *y, size_t n)
case JANAS_IQ4_NL:
janas_iq4nl_dequantize(w, y, n);
return;
+ case JANAS_MXFP4:
+ janas_mxfp4_dequantize(w, y, n);
+ return;
case JANAS_IQ4_XS:
janas_iq4xs_dequantize(w, y, n);
return;
@@ -1364,6 +1441,8 @@ float janas_dot(int type, const void *w, const struct janas_block_q8k *x,
return janas_q8_0_dot(w, x, nb);
case JANAS_IQ4_NL:
return janas_iq4nl_dot(w, x, nb);
+ case JANAS_MXFP4:
+ return janas_mxfp4_dot(w, x, nb);
case JANAS_IQ4_XS:
return janas_iq4xs_dot(w, x, nb);
case JANAS_Q4_0:
@@ -1493,6 +1572,9 @@ void janas_dot_rows(int type, const void *w, size_t row_bytes,
case JANAS_IQ4_NL:
f = use_vnni ? iq4nl_rows_vnni : iq4nl_rows_avx2;
break;
+ case JANAS_MXFP4:
+ f = use_vnni ? mxfp4_rows_vnni : mxfp4_rows_avx2;
+ break;
case JANAS_IQ4_XS:
f = use_vnni ? iq4xs_rows_vnni : iq4xs_rows_avx2;
break;
@@ -1532,7 +1614,7 @@ 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_Q3_K || type == JANAS_IQ3_S || type == JANAS_MXFP4)) {
float r[8];
int g = 1;
for (; v < nv; v += (size_t)g) {
@@ -1556,6 +1638,10 @@ void janas_dot_multi(int type, const void *w, const struct janas_block_q8k *x,
(use_vnni ? iq4nl_dotn_vnni : iq4nl_dotn_avx2)(w, xv, x_stride,
g, nb, r);
break;
+ case JANAS_MXFP4:
+ (use_vnni ? mxfp4_dotn_vnni : mxfp4_dotn_avx2)(w, xv, x_stride,
+ g, nb, r);
+ break;
case JANAS_IQ4_XS:
(use_vnni ? iq4xs_dotn_vnni : iq4xs_dotn_avx2)(w, xv, x_stride,
g, nb, r);
diff --git a/src/llm/quant.h b/src/llm/quant.h
index d96782a..e55808c 100644
--- a/src/llm/quant.h
+++ b/src/llm/quant.h
@@ -84,6 +84,22 @@ struct janas_block_q4_0 {
};
extern const int8_t janas_q4_0_values[16];
+/*
+ * MXFP4 (ggml's type 39, the OCP microscaling format): 32 weights, each a
+ * 4-bit E2M1 code into janas_mxfp4_values (0, 1, 2, 3, 4, 6, 8, 12 and
+ * their opposites: twice the format's values), times 2^(e - 128), e the
+ * block's E8M0 byte. IQ4_NL's layout with that byte for a scale: eight
+ * blocks cover a Q8_K block, byte l holds weight l low and l + 16 high.
+ */
+struct janas_block_mxfp4 {
+ uint8_t e; /* E8M0: the power of two, 127 the unit */
+ uint8_t qs[16];
+};
+extern const int8_t janas_mxfp4_values[16];
+/* 2^(e - 128) exactly, as ggml's GGML_E8M0_TO_FP32_HALF (subnormal below
+ e = 2): an MXFP4 block's scale */
+float janas_e8m0_half(uint8_t e);
+
/*
* Q5_1: 32 weights d * q + m, q of five bits: the low four as in Q4_0,
* the fifth of weight l in bit l of qh (little-endian).
@@ -177,6 +193,7 @@ _Static_assert(sizeof(struct janas_block_q6kp) == 82, "q6kp block size");
_Static_assert(sizeof(struct janas_block_q5k) == 176, "q5k block size");
_Static_assert(sizeof(struct janas_block_q8_0) == 34, "q8_0 block size");
_Static_assert(sizeof(struct janas_block_iq4nl) == 18, "iq4_nl block size");
+_Static_assert(sizeof(struct janas_block_mxfp4) == 17, "mxfp4 block size");
_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");
@@ -198,6 +215,7 @@ enum janas_qtype {
JANAS_IQ4_NL = 20,
JANAS_IQ3_S = 21, /* sizes per 256 weights: 8 blocks, 144 bytes */
JANAS_IQ4_XS = 23,
+ JANAS_MXFP4 = 39, /* sizes per 256 weights: 8 blocks, 136 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. */
@@ -351,6 +369,10 @@ float janas_q5k_dot_ref(const struct janas_block_q5k *w,
const struct janas_block_q8k *x, size_t nb);
void janas_q8_0_dequantize(const struct janas_block_q8_0 *x, float *y,
size_t n);
+/* n floats (n a multiple of 32) into Q8_0 blocks as llama.cpp's
+ quantize_row_q8_0_ref makes them: d = amax / 127, weights rounded half
+ away from zero */
+void janas_q8_0_quantize(const float *x, struct janas_block_q8_0 *y, size_t n);
/* IQ4_NL (nb groups of eight blocks, as Q8_0: each block's sum in its own
accumulator, the eight added as janas_hsum8 does) and IQ4_XS (one exact
integer sum per 256 weights, scales included, then one fma: as Q4_K). */
@@ -360,6 +382,13 @@ float janas_iq4nl_dot(const struct janas_block_iq4nl *w,
const struct janas_block_q8k *x, size_t nb);
float janas_iq4nl_dot_ref(const struct janas_block_iq4nl *w,
const struct janas_block_q8k *x, size_t nb);
+/* MXFP4: IQ4_NL's products, the block's power of two for its scale */
+void janas_mxfp4_dequantize(const struct janas_block_mxfp4 *x, float *y,
+ size_t n);
+float janas_mxfp4_dot(const struct janas_block_mxfp4 *w,
+ const struct janas_block_q8k *x, size_t nb);
+float janas_mxfp4_dot_ref(const struct janas_block_mxfp4 *w,
+ const struct janas_block_q8k *x, size_t nb);
void janas_iq4xs_dequantize(const struct janas_block_iq4xs *x, float *y,
size_t n);
float janas_iq4xs_dot(const struct janas_block_iq4xs *w,
diff --git a/src/llm/quant_dotn.h b/src/llm/quant_dotn.h
index 6fbdbfa..e9a8f49 100644
--- a/src/llm/quant_dotn.h
+++ b/src/llm/quant_dotn.h
@@ -1131,6 +1131,111 @@ static DOTN_TARGET float DOTN(q4_0_dot)(const struct janas_block_q4_0 *w,
return r;
}
+/* MXFP4: IQ4_NL's body with the block's E8M0 byte for its scale, 2^(e -
+ 128) from e8m0_tab - janas_e8m0_half's bits, subnormal below e = 2 - and
+ the codes' levels for the table. */
+static inline DOTN_TARGET __attribute__((always_inline)) void
+DOTN(mx_dotn_body)(const struct janas_block_mxfp4 *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),
+ levels = _mm256_broadcastsi128_si256(
+ _mm_loadu_si128((const __m128i *)janas_mxfp4_values));
+ __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_mxfp4 *wb = w + 8 * b;
+ __m256 dk = _mm256_setr_ps(e8m0_tab[wb[0].e], e8m0_tab[wb[1].e],
+ e8m0_tab[wb[2].e], e8m0_tab[wb[3].e],
+ e8m0_tab[wb[4].e], e8m0_tab[wb[5].e],
+ e8m0_tab[wb[6].e], e8m0_tab[wb[7].e]);
+ __m256i q[8], aq[8];
+ _Pragma("GCC unroll 8") for (int k = 0; k < 8; k++)
+ {
+ q[k] = DOTN(iq4_unpack32)(wb[k].qs, levels);
+ aq[k] = _mm256_abs_epi8(q[k]);
+ }
+ _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));
+ __m256i p =
+ _mm256_maddubs_epi16(aq[k], _mm256_sign_epi8(a, q[k]));
+ s[k] = _mm256_madd_epi16(p, ones);
+ }
+ __m256i s01 = _mm256_hadd_epi32(s[0], s[1]);
+ __m256i s23 = _mm256_hadd_epi32(s[2], s[3]);
+ __m256i s45 = _mm256_hadd_epi32(s[4], s[5]);
+ __m256i s67 = _mm256_hadd_epi32(s[6], s[7]);
+ __m256i t0 = _mm256_hadd_epi32(s01, s23);
+ __m256i t1 = _mm256_hadd_epi32(s45, s67);
+ __m256i is8 =
+ _mm256_add_epi32(_mm256_permute2x128_si256(t0, t1, 0x20),
+ _mm256_permute2x128_si256(t0, t1, 0x31));
+ 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++)
+ {
+ __m128 t = _mm_add_ps(_mm256_castps256_ps128(acc[v]),
+ _mm256_extractf128_ps(acc[v], 1));
+ t = _mm_add_ps(t, _mm_movehl_ps(t, t));
+ t = _mm_add_ss(t, _mm_movehdup_ps(t));
+ out[v] = _mm_cvtss_f32(t);
+ }
+}
+
+static DOTN_TARGET void DOTN(mxfp4_dotn)(const struct janas_block_mxfp4 *w,
+ const struct janas_block_q8k *x,
+ size_t xs, int nv, size_t nb,
+ float *out)
+{
+ switch (nv) {
+ case 8:
+ DOTN(mx_dotn_body)(w, x, xs, nb, out, 8);
+ break;
+ case 7:
+ DOTN(mx_dotn_body)(w, x, xs, nb, out, 7);
+ break;
+ case 6:
+ DOTN(mx_dotn_body)(w, x, xs, nb, out, 6);
+ break;
+ case 5:
+ DOTN(mx_dotn_body)(w, x, xs, nb, out, 5);
+ break;
+ case 4:
+ DOTN(mx_dotn_body)(w, x, xs, nb, out, 4);
+ break;
+ case 3:
+ DOTN(mx_dotn_body)(w, x, xs, nb, out, 3);
+ break;
+ case 2:
+ DOTN(mx_dotn_body)(w, x, xs, nb, out, 2);
+ break;
+ default:
+ DOTN(mx_dotn_body)(w, x, xs, nb, out, 1);
+ }
+}
+
+static DOTN_TARGET float DOTN(mxfp4_dot)(const struct janas_block_mxfp4 *w,
+ const struct janas_block_q8k *x,
+ size_t nb)
+{
+ float r;
+ DOTN(mx_dotn_body)(w, x, 0, nb, &r, 1);
+ return r;
+}
+
/* The eight sums of eight int32 lanes each, lane k the sum of s[k]: the
reduction tree of Q8_0. */
static inline DOTN_TARGET __attribute__((always_inline)) __m256i
@@ -1576,6 +1681,7 @@ DOTN_ROWS(q6k, struct janas_block_q6k)
DOTN_ROWS(q5k, struct janas_block_q5k)
DOTN_ROWS(q8_0, struct janas_block_q8_0)
DOTN_ROWS(iq4nl, struct janas_block_iq4nl)
+DOTN_ROWS(mxfp4, struct janas_block_mxfp4)
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)
diff --git a/tests/bench_kernels.c b/tests/bench_kernels.c
index d61aa67..b78555d 100644
--- a/tests/bench_kernels.c
+++ b/tests/bench_kernels.c
@@ -63,10 +63,10 @@ static const struct {
const char *name;
int type;
} TYPES[] = {
- {"Q4_K", JANAS_Q4_K}, {"Q5_K", JANAS_Q5_K}, {"Q6_K", JANAS_Q6_K},
- {"Q3_K", JANAS_Q3_K}, {"Q8_0", JANAS_Q8_0}, {"Q4_0", JANAS_Q4_0},
- {"Q4_1", JANAS_Q4_1}, {"Q5_1", JANAS_Q5_1}, {"IQ4_NL", JANAS_IQ4_NL},
- {"IQ4_XS", JANAS_IQ4_XS}, {"IQ3_S", JANAS_IQ3_S}};
+ {"Q4_K", JANAS_Q4_K}, {"Q5_K", JANAS_Q5_K}, {"Q6_K", JANAS_Q6_K},
+ {"Q3_K", JANAS_Q3_K}, {"Q8_0", JANAS_Q8_0}, {"Q4_0", JANAS_Q4_0},
+ {"Q4_1", JANAS_Q4_1}, {"Q5_1", JANAS_Q5_1}, {"IQ4_NL", JANAS_IQ4_NL},
+ {"IQ4_XS", JANAS_IQ4_XS}, {"IQ3_S", JANAS_IQ3_S}, {"MXFP4", JANAS_MXFP4}};
/* Seconds per product, the best of passes run for at least 0.3 s. */
static double timed(struct janas_pool *pool, struct janas_matvec_task *t)
@@ -142,6 +142,11 @@ int main(int argc, char **argv)
}
for (size_t i = 0; i < bytes; i++)
w[i] = (unsigned char)(rand() % 0x7c);
+ /* MXFP4's scale byte is a power of two: the ones of real weights,
+ not subnormal floats, whose products take the slow path */
+ if (TYPES[k].type == JANAS_MXFP4)
+ for (size_t i = 0; i < bytes; i += sizeof(struct janas_block_mxfp4))
+ w[i] = (unsigned char)(118 + rand() % 12);
#if defined(__linux__)
if (mem_kind == 2)
madvise(w, bytes, MADV_COLLAPSE);
diff --git a/tests/test_quant.c b/tests/test_quant.c
index 1056354..5f1182a 100644
--- a/tests/test_quant.c
+++ b/tests/test_quant.c
@@ -36,7 +36,7 @@ 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_Q5_1 || t == JANAS_IQ4_NL || t == JANAS_MXFP4;
return b32 ? (double)x[b].d32[i / 32] * x[b].qs32[i]
: (double)x[b].d * x[b].qs[i];
}
@@ -666,6 +666,92 @@ static void test_dot_iq4(void)
/* Q4_0 and Q5_1: the reference against a float product over the
dequantized weights (Q5_1's minimum times the activations' sums
included), the kernels on 1-17 vectors equal to it to the bit. */
+/*
+ * MXFP4: the reference against a float product over the dequantized
+ * weights, the kernels (single, 1-17 vectors, rows) equal to it to the bit,
+ * the scale exactly 2^(e - 128) for every byte, and the same bits as its
+ * exact rewrite into Q8_0 (janas_mxfp4_to_q8_0, gguf2jns's for resident
+ * tensors), whose products take the activations the same way.
+ */
+static void test_dot_mxfp4(void)
+{
+ enum { NB = 8, N = NB * JANAS_QK, NV = 17 };
+ for (int e = 0; e < 256; e++)
+ CHECK(janas_e8m0_half((uint8_t)e) == ldexpf(1.0f, e - 128),
+ "e8m0 %d: %g", e, janas_e8m0_half((uint8_t)e));
+ struct janas_block_mxfp4 *w = malloc(NB * 8 * sizeof(*w));
+ struct janas_block_q8_0 *w8 = malloc(NB * 8 * sizeof(*w8));
+ 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++) {
+ unsigned char *pw = (unsigned char *)w;
+ for (size_t i = 0; i < NB * 8 * sizeof(*w); i++)
+ pw[i] = (unsigned char)rnd();
+ /* the powers of two of real weights (2^-10 to 2^3), and in some
+ trials the subnormal and the largest ones as well */
+ for (int b = 0; b < NB * 8; b++)
+ w[b].e = (uint8_t)(118 + rnd() % 14);
+ if (trial % 4 == 3) {
+ w[0].e = 0;
+ w[1].e = 1;
+ w[2].e = 2;
+ w[3].e = 254;
+ }
+ 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_mxfp4_dequantize(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 p =
+ (double)wf[b * JANAS_QK + i] * act(x, b, i, JANAS_MXFP4);
+ expect += p;
+ mag += fabs(p);
+ }
+ float ref = janas_mxfp4_dot_ref(w, x, NB);
+ if (trial % 4 != 3) /* 2^126 times the rest: no float to compare */
+ CHECK(fabs(ref - expect) <= 1e-5 * mag + 1e-6, "mxfp4 ref %g vs %g",
+ ref, expect);
+ float fast = janas_dot(JANAS_MXFP4, w, x, NB);
+ CHECK(fast == ref || (isnan(fast) && isnan(ref)),
+ "mxfp4 fast %g vs ref %g", fast, ref);
+ for (int nv = 1; nv <= NV; nv++) {
+ float out[NV];
+ janas_dot_multi(JANAS_MXFP4, w, x, NB, (size_t)nv, NB, out, 1);
+ for (int v = 0; v < nv; v++) {
+ float r = janas_mxfp4_dot_ref(w, x + v * NB, NB);
+ CHECK(out[v] == r || (isnan(out[v]) && isnan(r)),
+ "mxfp4 multi nv %d v %d: %g vs %g", nv, v, out[v], r);
+ }
+ }
+ /* four rows of two super-blocks each, the matrix read row by row */
+ janas_dot_rows(JANAS_MXFP4, w, 2 * 8 * sizeof(*w), x, 2, 4, rows);
+ for (int r = 0; r < 4; r++) {
+ float one = janas_mxfp4_dot_ref(w + 16 * r, x, 2);
+ CHECK(rows[r] == one || (isnan(rows[r]) && isnan(one)),
+ "mxfp4 rows %d: %g vs %g", r, rows[r], one);
+ }
+ if (trial % 4 == 3)
+ continue;
+ /* the exact rewrite into Q8_0: the same products, bit for bit */
+ size_t tiny = 0;
+ CHECK(janas_mxfp4_to_q8_0((const uint8_t *)w, w8, NB * 8, &tiny) == 0 &&
+ tiny == 0,
+ "mxfp4 to q8_0 (%zu tiny)", tiny);
+ float q8 = janas_dot(JANAS_Q8_0, w8, x, NB);
+ CHECK(q8 == fast, "mxfp4 %g vs its q8_0 rewrite %g", fast, q8);
+ }
+ free(w);
+ free(w8);
+ free(x);
+ free(xf);
+ free(wf);
+}
+
static void test_dot_q4_0_q5_1(void)
{
enum { NB = 8, N = NB * JANAS_QK, NV = 17 };
@@ -764,9 +850,9 @@ static void test_dot_q4_0_q5_1(void)
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};
+ 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};
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];
@@ -807,6 +893,13 @@ static void test_dequantize(void)
q->d = janas_fp32_to_fp16(rndf(0.0001f, 0.01f));
break;
}
+ case JANAS_MXFP4: { /* eight blocks cover one super-block */
+ struct janas_block_mxfp4 *q =
+ (struct janas_block_mxfp4 *)w + i * 8;
+ for (int j = 0; j < 8; j++)
+ q[j].e = (uint8_t)(118 + rnd() % 14);
+ break;
+ }
case JANAS_Q4_0: {
struct janas_block_q4_0 *q =
(struct janas_block_q4_0 *)w + i * 8;
@@ -855,6 +948,9 @@ static void test_dequantize(void)
case JANAS_IQ4_XS:
janas_iq4xs_dequantize((const void *)w, a, N);
break;
+ case JANAS_MXFP4:
+ janas_mxfp4_dequantize((const void *)w, a, N);
+ break;
case JANAS_Q4_0:
janas_q4_0_dequantize((const void *)w, a, N);
break;
@@ -1100,6 +1196,7 @@ int main(void)
test_det();
test_dot_q5k_q8_0();
test_dot_iq4();
+ test_dot_mxfp4();
test_dot_q4_0_q5_1();
test_dot_q3k();
test_dot_iq3s();
diff --git a/tools/gguf2jns.c b/tools/gguf2jns.c
index 405326d..e83656f 100644
--- a/tools/gguf2jns.c
+++ b/tools/gguf2jns.c
@@ -23,8 +23,8 @@
* one (E8M0, 2^(e - 128)), and so is Q8_0's with the power as its fp16
* scale. Only a block whose power is below fp16's least, 2^-24, and whose
* weights are so below 3.6e-7, is written as zeros (counted). Routed
- * experts in MXFP4 are refused: twice their bytes a token would undo what
- * the format is for.
+ * experts in MXFP4 (the MXFP4_MOE files) stay MXFP4, which the engine reads
+ * as it is: as Q8_0 they would read twice the bytes at every token.
*
* A model split into several GGUF files (name-00001-of-0000N.gguf) is read
* from all of them: give the first. Data is copied in streaming; the model
@@ -353,10 +353,6 @@ int main(int argc, char **argv)
die("%s missing: only models with routed experts in every "
"layer, or dense ones, are supported",
key);
- if (t->t->type == GGUF_MXFP4)
- die("%s: routed experts in MXFP4 are not supported yet; a "
- "file with K-quants (Q4_K_M, Q3_K_M) works",
- key);
uint64_t ne = t->t->n_dims > 2 ? t->t->dims[2] : 1;
if (ne != n_expert || t->t->bytes % n_expert ||
t->t->dims[0] > UINT32_MAX || t->t->dims[1] > UINT32_MAX ||