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.
C avx2ccpu-inferenceggufinference-enginelinuxllmlocal-llmmixture-of-expertsmoenvmequantizationqwen3simdspeculative-decodingvulkan
Release 2026-10-13 (3): Qwen3-Coder-Next as llama.cpp converts it now, its MXFP4 shared experts written as Q8_0 exactly (issue #18)
camaurib51ed1993a47
diff --git a/ChangeLog.md b/ChangeLog.mdindex 855c912..ddb62ef 100644--- a/ChangeLog.md+++ b/ChangeLog.md@@ -2,6 +2,12 @@> Curated, user-facing summary of completed work, newest first.+## [2026-10-13] - Qwen3-Coder-Next as llama.cpp writes it now++- **qwen3next models converted by llama.cpp since early 2026 load** (issue #18, reported by nica-f with unsloth's Qwen3-Coder-Next-Q3_K_M): the recurrent layers' input projection comes as two tensors, `attn_qkv` and `attn_gate`, where the engine wanted the single `ssm_in`, its rows interleaved per key head. Both layouts are read; Qwen3-Coder-Next rewritten in the new one gives the very logits of the old, to the bit, token by token and in blocks.+- **MXFP4 tensors written as Q8_0, exactly**: the same file has its shared experts in MXFP4 (ggml's type 39), which the engine did not know. An MXFP4 weight is an integer of -12 to 12 times its block's power of two, and Q8_0 holds both, so `gguf2jns` rewrites such a tensor with every weight unchanged, at twice the bytes (80 MB more for Qwen3-Coder-Next's shared experts). Checked on a Qwen3-4B with four layers' attention in MXFP4: the converted tensors give the GGUF's weights to the bit. Routed experts in MXFP4 are refused with a message for now: twice their bytes on every token would undo what the format is for.++## [2026-10-13] - janas-bench says what the GPU does- **`janas-bench` says that the GPU reads a prompt in every configuration** (issue #16, a Radeon 780M): with the fast prompt a prompt's blocks of 64 tokens or more go to the GPU whether a row says "+ GPU" or not, so that a prompt gives the same results in each, and the rows without it had been read as the CPU alone, the GPU as of no help where it read Qwen3-4B's prompt 5.2 times faster (477 against 92 tokens/s). A note under the table now says so; "+ GPU" is the GPU's part in the shorter passes.diff --git a/MODELS.md b/MODELS.mdindex 0286e75..739e53e 100644--- a/MODELS.md+++ b/MODELS.md@@ -15,13 +15,14 @@ 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;- 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.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-`tools/gguf2jns` and `tools/jns_planes` are deterministic. The layout of the file follows from the sizes in the source, nothing is timed, drawn at random or read from the environment, and the weights are copied, never recomputed. The same GGUF, converted on another machine, gives the same bytes. The converter was a Python script until 25 September 2026; the C one that took its place writes the very same bytes, and the fingerprints below hold for both.+`tools/gguf2jns` and `tools/jns_planes` are deterministic. The layout of the file follows from the sizes in the source, nothing is timed, drawn at random or read from the environment, and the weights are copied, never recomputed (an MXFP4 tensor's re-encoded as Q8_0, exactly). The same GGUF, converted on another machine, gives the same bytes. The converter was a Python script until 25 September 2026; the C one that took its place writes the very same bytes, and the fingerprints below hold for both.That is what this project publishes about its models: the recipe, and the fingerprint of what the recipe produces — not forty gigabytes. Convert your own copy, compare, and you know you are running the file the numbers in this repository were measured on.diff --git a/src/llm/model_load.c b/src/llm/model_load.cindex da0bf54..cbb2ab9 100644--- a/src/llm/model_load.c+++ b/src/llm/model_load.c@@ -286,7 +286,18 @@ static int load_layer(struct janas_llm_model *m, const struct janas_jns *j,&ly->beta_own, err, err_len))return -1;} else {- NEED(ssm_in, "ssm_in.weight", T_QUANT, dm, m->qkvz_dim)+ /* qwen3next's fused projection, q, k, v and z interleaved per+ key head; llama.cpp's converter writes it as two since 2026,+ q|k|v and z each in the heads' order, as qwen35 has them+ (issue #18: unsloth's Qwen3-Coder-Next files). ssm_ba is the+ same in both. */+ snprintf(name, sizeof(name), "blk.%u.attn_qkv.weight", l);+ if (janas_jns_tensor(j, name)) {+ NEED(ssm_qkv, "attn_qkv.weight", T_QUANT, dm, m->conv_ch)+ NEED(ssm_z, "attn_gate.weight", T_QUANT, dm, m->d_inner)+ } else {+ NEED(ssm_in, "ssm_in.weight", T_QUANT, dm, m->qkvz_dim)+ }NEED(ssm_ba, "ssm_ba.weight", T_QUANT, dm, 2 * m->n_vh)}NEED(conv, "ssm_conv1d.weight", T_F32, m->d_conv, m->conv_ch)diff --git a/src/llm/model_rec.c b/src/llm/model_rec.cindex df24896..d421380 100644--- a/src/llm/model_rec.c+++ b/src/llm/model_rec.c@@ -227,7 +227,7 @@ static void gate_worker(void *arg, int tid, int n_threads)uint32_t j = it / nv, h = it % nv, g = h / rf, r = h % rf;float *o = m->att + (size_t)j * m->d_inner + (size_t)h * ds;const float *z =- m->a->rec == JANAS_REC_Q35+ a->ly->ssm_qkv? m->big + (size_t)j * m->qkvz_dim + m->conv_ch + (size_t)h * ds: m->big + (size_t)j * m->qkvz_dim + (size_t)g * gsz + 2 * ds +ds * rf + r * ds;@@ -280,6 +280,33 @@ void janas_m_rec_layer(struct janas_llm_model *m, const struct layer *ly,m->ba[(size_t)j * 2 * nv + nv + h] =janas_dot_f32(wa + (size_t)h * dm, xj, dm);}+ } else if (ly->ssm_qkv) {+ /* qwen3next as llama.cpp writes it since 2026: q|k|v and z as for+ qwen35, the betas and alphas fused and grouped as before */+ struct janas_matvec_task t[3] = {{.type = (int)ly->ssm_qkv->type,+ .w = data(m, ly->ssm_qkv),+ .x = m->xq,+ .y = m->big,+ .rows = cc,+ .cols = dm,+ .n_vec = n,+ .y_stride = m->qkvz_dim},+ {.type = (int)ly->ssm_z->type,+ .w = data(m, ly->ssm_z),+ .x = m->xq,+ .y = m->big + cc,+ .rows = m->d_inner,+ .cols = dm,+ .n_vec = n,+ .y_stride = m->qkvz_dim},+ {.type = (int)ly->ssm_ba->type,+ .w = data(m, ly->ssm_ba),+ .x = m->xq,+ .y = m->ba,+ .rows = 2 * nv,+ .cols = dm,+ .n_vec = n}};+ janas_gpu_matvec_group(janas_m_gpu(m, 0), m->compute, t, 3);} else {struct janas_matvec_task t[2] = {{.type = (int)ly->ssm_in->type,.w = data(m, ly->ssm_in),@@ -324,12 +351,16 @@ void janas_m_rec_layer(struct janas_llm_model *m, const struct layer *ly,}continue;}+ if (ly->ssm_qkv) /* q|k|v in the convolution's order already */+ memcpy(mx, src, (size_t)cc * sizeof(float));for (uint32_t g = 0; g < m->n_kh; g++) {const float *s = src + (size_t)g * gsz;- memcpy(mx + (size_t)g * ds, s, ds * sizeof(float));- memcpy(mx + kd + (size_t)g * ds, s + ds, ds * sizeof(float));- memcpy(mx + 2 * kd + (size_t)g * ds * rf, s + 2 * ds,- ds * rf * sizeof(float));+ if (!ly->ssm_qkv) {+ memcpy(mx + (size_t)g * ds, s, ds * sizeof(float));+ memcpy(mx + kd + (size_t)g * ds, s + ds, ds * sizeof(float));+ memcpy(mx + 2 * kd + (size_t)g * ds * rf, s + 2 * ds,+ ds * rf * sizeof(float));+ }for (uint32_t r = 0; r < rf; r++) {uint32_t h = g * rf + r;float al = b[2 * g * rf + rf + r] + dt[h];@@ -344,7 +375,7 @@ void janas_m_rec_layer(struct janas_llm_model *m, const struct layer *ly,trace_row(m, "qkv_mixed", m->mixed + (size_t)(a + n - 1) * cc, cc);trace_row(m, "z",m->big + (size_t)(n - 1) * m->qkvz_dim +- (m->a->rec == JANAS_REC_Q35 ? m->conv_ch : 0),+ (ly->ssm_qkv ? m->conv_ch : 0),m->d_inner);struct ssm_job sj = {m, ly, n};janas_pool_run(m->compute, conv_worker, &sj);@@ -377,8 +408,7 @@ void janas_m_rec_layer(struct janas_llm_model *m, const struct layer *ly,m->att + (size_t)(n - 1) * m->d_inner + (size_t)31 * ds, ds);trace_row(m, "z_h31",m->big + (size_t)(n - 1) * m->qkvz_dim +- (m->a->rec == JANAS_REC_Q35 ? m->conv_ch : 0) +- (size_t)31 * ds,+ (ly->ssm_qkv ? m->conv_ch : 0) + (size_t)31 * ds,ds);}/* logged for a call that may be taken back; a longer one (a prefilldiff --git a/src/llm/quant.c b/src/llm/quant.cindex 17d3e64..1b09ce3 100644--- a/src/llm/quant.c+++ b/src/llm/quant.c@@ -75,6 +75,31 @@ uint16_t janas_fp32_to_fp16(float f)return (uint16_t)half;}+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};+ 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 */+ if (x[0] == 0xFF || p > 15)+ return -1;+ /* fp16 of 2^p: normal from 2^-14, subnormal down to 2^-24 */+ uint16_t d = p >= -14 ? (uint16_t)((p + 15) << 10)+ : p >= -24 ? (uint16_t)(1u << (p + 24))+ : 0;+ if (!d)+ ++*tiny;+ out[b].d = d;+ for (int j = 0; j < 16; j++) {+ out[b].qs[j] = d ? kv[x[1 + j] & 15] : 0;+ out[b].qs[j + 16] = d ? kv[x[1 + j] >> 4] : 0;+ }+ }+ return 0;+}+static int use_avx2_fma; /* set by float_helpers_init, further down *//* The eight blocks of 32 (d32, qs32, bsums32) of one block, as the 256diff --git a/src/llm/quant.h b/src/llm/quant.hindex 62cfddc..d96782a 100644--- a/src/llm/quant.h+++ b/src/llm/quant.h@@ -248,6 +248,20 @@ void janas_axpy_f32(float *y, float a, const float *x, size_t n);float janas_fp16_to_fp32(uint16_t h);uint16_t janas_fp32_to_fp16(float f);+/* MXFP4 (ggml's type 39): a block of 32 weights is an E8M0 scale byte e and+ 16 bytes of 4-bit codes, the weight kv[code] * 2^(e - 128) with kv the 16+ integers 0, 1, 2, 3, 4, 6, 8, 12 and their opposites; codes j and j + 16+ share byte j, low and high half. */+#define JANAS_MXFP4_BLOCK 17++/* n MXFP4 blocks as Q8_0 blocks, exactly: the codes' integers as the+ weights, the power of two as the fp16 scale. A block whose power is below+ fp16's least, 2^-24 (weights under 3.6e-7), is written as zeros and+ counted in *tiny. -1 for a scale beyond fp16 (2^16 and more, or E8M0's+ NaN), which no model's weights have. */+int janas_mxfp4_to_q8_0(const uint8_t *in, struct janas_block_q8_0 *out,+ size_t n, size_t *tiny);+/* Quantizes n floats (n multiple of JANAS_QK) into n / JANAS_QK blocks. */void janas_q8k_quantize(const float *x, struct janas_block_q8k *y, size_t n);diff --git a/tests/test_mxfp4.c b/tests/test_mxfp4.cnew file mode 100644index 0000000..68b9113--- /dev/null+++ b/tests/test_mxfp4.c@@ -0,0 +1,80 @@+/* SPDX-License-Identifier: GPL-3.0-or-later+ Copyright (C) 2026 Maurizio Cammalleri */+/*+ * test_mxfp4.c - MXFP4 blocks rewritten as Q8_0 (janas_mxfp4_to_q8_0, used+ * by gguf2jns for unsloth's Qwen3-Coder-Next files, issue #18) give every+ * weight exactly: each code's value times the block's power of two, as+ * ggml's dequantize_row_mxfp4 computes it, for powers from 2^15 down to+ * fp16's least, 2^-24 (normal and subnormal scales); a block below that is+ * zeros and counted, a scale beyond fp16 refused.+ */+#include <math.h>+#include <stdio.h>+#include <string.h>++#include "llm/quant.h"++static const int8_t kv[16] = {0, 1, 2, 3, 4, 6, 8, 12,+ 0, -1, -2, -3, -4, -6, -8, -12};++/* ggml's value of weight i of an MXFP4 block */+static float ref(const uint8_t *b, int i)+{+ int code = i < 16 ? b[1 + i] & 15 : b[1 + i - 16] >> 4;+ return (float)kv[code] * ldexpf(1.0f, (int)b[0] - 128);+}++int main(void)+{+ /* scales: 2^0, 2^-8, 2^15 (the largest), 2^-14 (the least normal+ fp16), 2^-15 and 2^-24 (subnormal), 2^-25 (below: zeros) */+ static const uint8_t es[] = {128, 120, 143, 114, 113, 104, 103};+ enum { N = sizeof(es) };+ uint8_t in[N * JANAS_MXFP4_BLOCK];+ unsigned seed = 12345;+ for (int b = 0; b < N; b++) {+ uint8_t *x = in + b * JANAS_MXFP4_BLOCK;+ x[0] = es[b];+ for (int j = 0; j < 16; j++) {+ seed = seed * 1103515245u + 12345u;+ x[1 + j] = (uint8_t)(seed >> 16);+ }+ }+ in[1] = 0x70; /* codes 0 and 7 (12) in the first block, whatever came */+ struct janas_block_q8_0 out[N];+ size_t tiny = 0;+ int rc = 0;+ if (janas_mxfp4_to_q8_0(in, out, N, &tiny) != 0) {+ printf("FAIL: valid scales refused\n");+ return 1;+ }+ for (int b = 0; b < N; b++) {+ const uint8_t *x = in + b * JANAS_MXFP4_BLOCK;+ float d = janas_fp16_to_fp32(out[b].d);+ for (int i = 0; i < 32; i++) {+ float want = es[b] < 104 ? 0.0f : ref(x, i);+ float got = (float)out[b].qs[i] * d;+ if (got != want) {+ printf("FAIL: block %d (2^%d) weight %d: %g, not %g\n", b,+ (int)es[b] - 128, i, got, want);+ rc = 1;+ break;+ }+ }+ }+ if (tiny != 1) {+ printf("FAIL: %zu blocks counted below fp16's scales, not 1\n", tiny);+ rc = 1;+ }+ /* beyond fp16: 2^16, and E8M0's NaN */+ for (int k = 0; k < 2; k++) {+ uint8_t bad[JANAS_MXFP4_BLOCK] = {k ? 0xFF : 144};+ if (janas_mxfp4_to_q8_0(bad, out, 1, &tiny) != -1) {+ printf("FAIL: scale byte %d not refused\n", bad[0]);+ rc = 1;+ }+ }+ if (!rc)+ printf("test_mxfp4: ok\n");+ return rc;+}diff --git a/tools/gguf2jns.c b/tools/gguf2jns.cindex 23bf739..405326d 100644--- a/tools/gguf2jns.c+++ b/tools/gguf2jns.c@@ -17,6 +17,15 @@* 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.*+ * 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:+ * an MXFP4 weight is an integer of -12 to 12 times a power of two, a block's+ * 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.+ ** 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* is never held in memory.@@ -35,11 +44,15 @@#include "llm/gguf.h"#include "llm/jns.h"+#include "llm/quant.h"#define ALIGN 4096#define MATRIX_ALIGN 256#define COPY_CHUNK ((size_t)16 << 20)#define MAX_PARTS 256+#define GGUF_MXFP4 39 /* ggml's type number */+#define MXFP4_BLOCK JANAS_MXFP4_BLOCK+#define Q8_0_BLOCK sizeof(struct janas_block_q8_0)static uint64_t align_up(uint64_t x, uint64_t a){@@ -63,6 +76,7 @@ struct tensor {uint64_t new_offset;/* a resident matrix widened with zeros: its rows, then as written */uint64_t rows, rb, new_rows, new_rb, new_dims[2], new_bytes;+ uint32_t new_type; /* MXFP4 is written as Q8_0 */};/* A matrix of a slot: from which tensor, where in an expert's part of it,@@ -216,6 +230,30 @@ static uint32_t copy_rows(const struct tensor *t, uint64_t src, uint64_t rows,return crc;}+static size_t tiny_blocks; /* MXFP4 blocks below fp16's scales */++/* n bytes of MXFP4 at src of t, to dst as Q8_0 (twice the bytes). */+static void copy_mxfp4(const struct tensor *t, uint64_t src, uint64_t n,+ uint64_t dst)+{+ int fd = parts[t->part].fd;+ uint64_t per = COPY_CHUNK / MXFP4_BLOCK * MXFP4_BLOCK;+ for (uint64_t at = 0; at < n; at += per) {+ uint64_t k = n - at < per ? n - at : per;+ if (pread(fd, buf, k, (off_t)(t->base + src + at)) != (ssize_t)k)+ die("%s", "unexpected end of the GGUF file");+ uint64_t nb = k / MXFP4_BLOCK;+ if (janas_mxfp4_to_q8_0(buf, (struct janas_block_q8_0 *)obuf, nb,+ &tiny_blocks) != 0)+ die("%s", "an MXFP4 scale beyond fp16's (NaN, or 2^16 and more)");+ if (pwrite(out_fd, obuf, nb * Q8_0_BLOCK,+ (off_t)(dst + at / MXFP4_BLOCK * Q8_0_BLOCK)) !=+ (ssize_t)(nb * Q8_0_BLOCK))+ die("write failed: %s", strerror(errno));+ copied += k;+ }+}+/* A width rounded up to a multiple of 256 (the engine's activations). */static uint64_t wide(uint64_t n){@@ -315,6 +353,10 @@ 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 ||@@ -378,10 +420,22 @@ int main(int argc, char **argv)int gu = nl > 16 && (!strcmp(name + nl - 16, ".ffn_gate.weight") ||!strcmp(name + nl - 14, ".ffn_up.weight"));int dn = nl > 16 && !strcmp(name + nl - 16, ".ffn_down.weight");- free(name);t->new_bytes = t->t->bytes;t->new_dims[0] = t->t->dims[0];t->new_dims[1] = t->t->dims[1];+ t->new_type = t->t->type;+ if (t->t->type == GGUF_MXFP4) {+ if (t->t->bytes % MXFP4_BLOCK || t->t->dims[0] % 32 ||+ (t->t->n_dims == 2 && (gu || dn) &&+ (wide(t->t->dims[0]) != t->t->dims[0] ||+ wide(t->t->dims[1]) != t->t->dims[1])))+ die("%s: an MXFP4 tensor this converter cannot rewrite", name);+ t->new_type = JANAS_Q8_0;+ t->new_bytes = t->t->bytes / MXFP4_BLOCK * Q8_0_BLOCK;+ free(name);+ continue;+ }+ free(name);if (t->t->n_dims != 2 || !(gu || dn))continue;uint64_t rows = t->t->dims[1], cols = t->t->dims[0];@@ -449,7 +503,9 @@ int main(int argc, char **argv)for (size_t i = 0; i < n_res; i++) {const struct tensor *t = resident[i];- if (t->new_bytes != t->t->bytes)+ if (t->new_type != t->t->type)+ copy_mxfp4(t, t->t->offset, t->t->bytes, t->new_offset);+ else if (t->new_bytes != t->t->bytes)copy_rows(t, t->t->offset, t->rows, t->rb, t->new_rows, t->new_rb,t->new_offset, 0, 0);else@@ -479,6 +535,10 @@ int main(int argc, char **argv)fflush(stdout);}printf("\n");+ if (tiny_blocks)+ printf("MXFP4: %llu blocks below fp16's least scale written as zeros "+ "(their weights under 3.6e-7)\n",+ (unsigned long long)tiny_blocks);/* tables, in file order, and their checksum */uint64_t tbl_len =@@ -498,7 +558,7 @@ int main(int argc, char **argv)die("tensor name too long: %s", name);}memcpy(d.name, t->name.p, t->name.n);- d.type = t->type;+ d.type = resident[i]->new_type;d.n_dims = t->n_dims;memcpy(d.dims, t->dims, sizeof(d.dims));d.dims[0] = resident[i]->new_dims[0];