Fast local LLM inference on plain CPUs. No GPU, 8 GB of RAM. OpenAI-compatible server.
Python aiavx512cpu-inferenceedge-aiggufinference-enginellama-cppllmllm-inferencelocal-ailocal-llmno-gpuon-device-aiopenai-apiopenai-compatiblequantizationqwenself-hostedspeculative-decodingternary
Add --kv6 and --kv5: 6- and 5-bit attention caches for longer context on 8 GB
0xaron39bf791cccaa
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 6390949..81cc6f4 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -5,3 +5,6 @@ All notable changes to openset-llm. Dates are YYYY-MM-DD.## Unreleased- First public release.+- `./serve --kv6` and `--kv5` (experimental): 6- and 5-bit attention caches. On an 8 GB machine speculation stays on+ to 12k / 16k context and plain decoding reaches 32k / 64k. Not exact; needs AVX-512; see the README's memory section.+- `./serve` now refuses two cache flags together and contexts that cannot fit with `--kv6` / `--kv5` under 12 GB.diff --git a/README.md b/README.mdindex 1b96f5b..94ef5f5 100644--- a/README.md+++ b/README.md@@ -216,6 +216,19 @@ or at start-up (`--portable`, one binary for all x86-64). A CPU without the fastmachine. With `--ctx` above 4096 on a machine (or container limit) under 12 GB, `serve` turns speculation off byitself (above 8192 with `--kv8`): there the draft state does not fit next to the KV cache and plain decoding is faster.`--ctx 16384` then runs plain decoding with ~0.45 GB free (measured). With 16 GB, speculation stays on to 32k.+- **Longer context on 8 GB (experimental, opt-in):** a smaller attention cache leaves room for more context and+ keeps speculation on further. Pick one flag; none of them is exact, and `--kv6` / `--kv5` need AVX-512.++ | `./serve` flag | attention cache vs default | speculation stays on to | plain decoding to | WikiText PPL (default 7.5702) | GSM8K-40 / coding pass@1 (default 39 / 83.1 %) |+ |---|---|---|---|---|---|+ | `--kv8` (int8) | 57 % | 8k | 32k | 7.5678 | – |+ | `--kv6` (6-bit) | 38 % | 12k (10.5–11.0 tok/s at full 12k–14k prompts) | 32k (5.7 tok/s, 0.7 GB free) | 7.5713 | 39 / 83.8 % |+ | `--kv5` (5-bit) | 32 % | 16k (9.0–11.1 tok/s at a full 16k prompt, ~0.1 GB free) | 64k (3.9–4.5 tok/s, ~0.1 GB free) | 7.5729 | 39 / 83.1 % |++ Measured 2026-11-21 on 8 vCPU / 8 GB Ice Lake boxes (the only CPU class tested). A planted-facts recall test scored+ 8/8 at 16k, 32k and 64k with both new formats; `--multi` and agent-loop resume give the same replies with them as+ without. `serve` refuses contexts past these limits on machines under 12 GB. At 64k expect about 50 minutes to read+ a full prompt the first time.- `./serve` turns off llama-server's host prompt cache (`--cache-ram 0`, default 8 GiB) and keeps one contextcheckpoint (`--ctx-checkpoints 1`); the defaults OOM an 8 GB box.- `install.sh` refuses to run below 8 GB of RAM (or a container memory limit below that), and needs ~15 GB of free disk@@ -236,7 +249,8 @@ A patch set on PrismML's llama.cpp fork (pinned at `e3de715`), applied by `instaDefaults are the fastest settings. Switches: `./serve --no-spec` (plain decoding), `--no-resume`, `--multi N`,`--think`, `--ctx N`, and `--kv8` (experimental, opt-in: a smaller, int8 attention cache; 43 % less cache memory, which is what lets-speculation stay on at `--ctx 8192` on an 8 GB machine; not exact: WikiText PPL 7.5678 vs 7.5702).+speculation stay on at `--ctx 8192` on an 8 GB machine; not exact: WikiText PPL 7.5678 vs 7.5702) or its smaller+siblings `--kv6` / `--kv5` (6- and 5-bit; [Memory budget](#memory-budget-8-gb-machine) has the limits and numbers).## Several requests at once (`--multi`)@@ -320,7 +334,8 @@ curl http://127.0.0.1:8080/v1/chat/completions -H 'Content-Type: application/jso- **Hardware:** the fast path needs x86 with AVX-512 VNNI + VBMI (table above). Measured on Intel Ice Lake, AMD Zen 4and Zen 5. The 8 vCPU of our Ice Lake test boxes behave like 8 physical cores; a typical cloud 8 vCPU (AWS / GCP / Azure) is 4 cores, soexpect lower absolute speed there.-- **8 GB is the floor:** 4k context by default; above 4k on < 12 GB, `serve` uses plain decoding (above 8k with `--kv8`).+- **8 GB is the floor:** 4k context by default; above 4k on < 12 GB, `serve` uses plain decoding (above 8k with `--kv8`,+ 12k with `--kv6`, 16k with `--kv5`). The compact caches are experimental and were measured on one CPU class.- **Speculation changes a reply only at near-ties:** greedy replies are byte-identical to plain decoding on 17/24 ofthe benchmark prompts and 75/128 of a broader set; task scores are the same (Numbers).- **Speed depends on the text and the machine.** Speculative decoding gains most on text the model finds predictablediff --git a/kernels/attn-fast.inc b/kernels/attn-fast.incindex b6285fa..7bf7c07 100644--- a/kernels/attn-fast.inc+++ b/kernels/attn-fast.inc@@ -3,8 +3,9 @@//// One kernel for every query count M (1 decode, 4 MTP verify, up to the ubatch in prefill), GQA-grouped: the G query// heads of a KV head and all M query positions form R = M*G rows that share every K/V row read. Streaming (online)-// softmax, nothing materialised. KV types: F16 (exact mode: f32 math on converted tiles) and Q8_K (int8 mode, opt-in via-// `-ctk q8_K -ctv q8_K`: one f32 scale per (token, head) row of 256; Q·K as u8 x s8 VNNI dot products in int32, P·V in f32).+// softmax, nothing materialised. KV types: F16 (exact mode: f32 math on converted tiles), Q8_K (int8 mode, opt-in via+// `-ctk q8_K -ctv q8_K`: one f32 scale per (token, head) row of 256; Q·K as u8 x s8 VNNI dot products in int32, P·V in f32)+// and KV4 / KV5 / KV6 (kvq.h: 4 / 5 / 6-bit rows, `-ctk kv6 -ctv kv6`; unpacked to int8 tiles, then the int8 paths).//// Canonical order. Every query row is computed by the same sequence of float operations whatever M is, however the// rows are blocked and however the work is split across threads: keys are cut into absolute chunks of ATTNF_CHUNK@@ -38,11 +39,15 @@#define ATTNF_DMAX 512 // max head size (DK, DV), multiple of 64typedef struct { float d; int8_t qs[256]; int16_t bsums[16]; } attnf_q8k; // = block_q8_K (checked at run time)+#include "kvq.h" // GGML_TYPE_KV4 / KV5 / KV6: b-bit rows, unpacked to int8 tiles here, then the int8 paths+static inline int attnf_kvq_bits(enum ggml_type t) { return t == GGML_TYPE_KV4 ? 4 : t == GGML_TYPE_KV5 ? 5 : t == GGML_TYPE_KV6 ? 6 : 0; }+static inline bool attnf_is_i8(enum ggml_type t) { return t == GGML_TYPE_Q8_K || attnf_kvq_bits(t) != 0; }+static inline size_t attnf_blk(enum ggml_type t) { const int b = attnf_kvq_bits(t); return b ? kvq_block_size(b) : sizeof(attnf_q8k); }typedef struct {const char * k; size_t nbk; // key row t of this KV head: k + t*nbkconst char * v; size_t nbv;- enum ggml_type type; // GGML_TYPE_F16 or GGML_TYPE_Q8_K, same for K and V+ enum ggml_type type; // GGML_TYPE_F16, GGML_TYPE_Q8_K or GGML_TYPE_KV4/5/6, same for K and Vint dk, dv;} attnf_kv;@@ -61,13 +66,14 @@ typedef struct {float kd[ATTNF_T], vd[ATTNF_T];int32_t kc[ATTNF_T];const int8_t * kq[ATTNF_T];+ int8_t * K8; // [T][DMAX] kv4/5/6: K tile unpacked to int8 levels} attnf_ws;static attnf_ws * attnf_ws_get(void) {static thread_local attnf_ws * ws = NULL;if (ws) return ws;const size_t RD = (size_t) ATTNF_RMAX * ATTNF_DMAX, TD = (size_t) ATTNF_T * ATTNF_DMAX, RT = (size_t) ATTNF_RMAX * ATTNF_T;- const size_t bytes = sizeof(float) * (RD + ATTNF_RMAX + TD + TD + RT + RT + RD + RD) + 2 * RD + 64 * 16;+ const size_t bytes = sizeof(float) * (RD + ATTNF_RMAX + TD + TD + RT + RT + RD + RD) + 2 * RD + TD + 64 * 16;char * p = (char *) aligned_alloc(64, (bytes + 63) / 64 * 64);if (!p) return NULL;attnf_ws * w = (attnf_ws *) aligned_alloc(64, (sizeof(attnf_ws) + 63) / 64 * 64);@@ -77,6 +83,7 @@ static attnf_ws * attnf_ws_get(void) {ATTNF_TAKE(w->Q, float, RD); ATTNF_TAKE(w->Qu, uint8_t, RD); ATTNF_TAKE(w->Qu2, uint8_t, RD); ATTNF_TAKE(w->dQ, float, ATTNF_RMAX);ATTNF_TAKE(w->Kt, float, TD); ATTNF_TAKE(w->Vt, float, TD); ATTNF_TAKE(w->S, float, RT);ATTNF_TAKE(w->Mt, float, RT); ATTNF_TAKE(w->O, float, RD); ATTNF_TAKE(w->OF, float, RD);+ ATTNF_TAKE(w->K8, int8_t, TD);#undef ATTNF_TAKEws = w;return ws;@@ -105,7 +112,7 @@ static void attnf_q_prepare(attnf_ws * ws, const attnf_kv * kv, int R, const flofloat * Qr = ws->Q + (size_t) r * ATTNF_DMAX;for (int d = 0; d < DK; ++d) Qr[d] = q[r][d] * scale;if (ulp) for (int d = 0; d < DK; d += 2) Qr[d] = nextafterf(Qr[d], INFINITY);- if (kv->type == GGML_TYPE_Q8_K) {+ if (attnf_is_i8(kv->type)) {float amax = 0.0f;for (int d = 0; d < DK; ++d) amax = fmaxf(amax, fabsf(Qr[d]));const float dq = amax / 127.0f, iq = amax > 0.0f ? 127.0f / amax : 0.0f;@@ -256,13 +263,68 @@ static inline void attnf_pv(attnf_ws * ws, int r0, int d0, int nt) {}#endif // ATTNF_SIMD+// kv4/5/6 row (256 b-bit codes) -> 256 int8 levels; returns their sum+#if ATTNF_SIMD && defined(__AVX512VBMI__)+#define ATTNF_KVQ_SIMD 1+struct attnf_kvq_tab {+ __m512i perm, shift, mask, lut;+ attnf_kvq_tab(int bits) {+ alignas(64) uint8_t p[64], sh[64], l[64];+ const int nl = 1 << (bits - 1);+ const int8_t * lv = kvq_levels(bits);+ for (int q = 0; q < 8; ++q) for (int k2 = 0; k2 < 8; ++k2) {+ p[8 * q + k2] = (uint8_t) (bits * q + (k2 < bits ? k2 : 0)); // the `bits` bytes that hold 8 codes -> one qword+ sh[8 * q + k2] = (uint8_t) (k2 * bits);+ }+ for (int c = 0; c < 64; ++c) l[c] = (uint8_t) (c < nl ? lv[c] : c < 2 * nl ? -lv[c - nl] : 0);+ perm = _mm512_load_si512(p); shift = _mm512_load_si512(sh); lut = _mm512_load_si512(l);+ mask = _mm512_set1_epi8((char) ((1 << bits) - 1));+ }+};+static inline const attnf_kvq_tab * attnf_kvq_tabs(int bits) {+ static const attnf_kvq_tab t4(4), t5(5), t6(6);+ return bits == 4 ? &t4 : bits == 5 ? &t5 : &t6;+}+static inline int32_t attnf_kvq_unpack(const uint8_t * qs, int bits, int8_t * out) {+ const attnf_kvq_tab * tb = attnf_kvq_tabs(bits);+ const int nb = 8 * bits; // bytes per 64 codes+ const __mmask64 lm = (((__mmask64) 1) << nb) - 1; // masked load: never reads past the row+ const __m512i one = _mm512_set1_epi8(1);+ __m512i sum = _mm512_setzero_si512();+ for (int j = 0; j < 4; ++j) {+ const __m512i v = _mm512_permutexvar_epi8(tb->perm, _mm512_maskz_loadu_epi8(lm, qs + nb * j));+ const __m512i idx = _mm512_and_si512(_mm512_multishift_epi64_epi8(tb->shift, v), tb->mask);+ const __m512i lv = _mm512_permutexvar_epi8(idx, tb->lut);+ _mm512_storeu_si512(out + 64 * j, lv);+ sum = _mm512_dpbusd_epi32(sum, one, lv);+ }+ return _mm512_reduce_add_epi32(sum);+}+#else+#define ATTNF_KVQ_SIMD 0+static inline int32_t attnf_kvq_unpack(const uint8_t * qs, int bits, int8_t * out) {+ const int nl = 1 << (bits - 1);+ const int8_t * lv = kvq_levels(bits);+ int32_t s = 0;+ for (int i = 0; i < 256; ++i) {+ const int bit = i * bits, byte = bit >> 3, sh = bit & 7;+ const uint32_t w = (uint32_t) qs[byte] | (byte + 1 < 32 * bits ? (uint32_t) qs[byte + 1] << 8 : 0u);+ const int c = (int) ((w >> sh) & ((1u << bits) - 1));+ out[i] = (int8_t) (c < nl ? lv[c] : -lv[c - nl]);+ s += out[i];+ }+ return s;+}+#endif+// Online softmax of the R prepared rows over keys [k0, k1): returns m[r], l[r] and the unnormalised O[r] (row stride// ldo floats). Tiles start at k0 and every ATTNF_T keys after it (callers keep k0 on the canonical grid).// pos[r] indexes mrow[] (several rows can share one mask row); mrow[i] = f16 mask indexed by absolute key, or NULL.static void attnf_range(attnf_ws * ws, const attnf_kv * kv, int R, const int * pos, int npos,const ggml_fp16_t * const * mrow, int64_t k0, int64_t k1, float * m, float * l, float * O, size_t ldo) {const int DK = kv->dk, DV = kv->dv, T = ATTNF_T;- const bool i8 = kv->type == GGML_TYPE_Q8_K;+ const bool i8 = attnf_is_i8(kv->type);+ const int kqb = attnf_kvq_bits(kv->type); // 0, or the bits of a kvq.h rowfor (int r = 0; r < R; ++r) { ws->m[r] = -INFINITY; ws->l[r] = 0.0f; memset(ws->O + (size_t) r * ATTNF_DMAX, 0, DV * sizeof(float)); }for (int64_t t0 = k0; t0 < k1; t0 += T) {const int nt = (int) (k1 - t0 < T ? k1 - t0 : T);@@ -283,7 +345,7 @@ static void attnf_range(attnf_ws * ws, const attnf_kv * kv, int R, const int * p#if ATTNF_SIMDif (attnf_pf() && t0 + T < k1) { // next tile's K and V rows into L2 (decode streams them from DRAM)const int64_t n2 = k1 - (t0 + T) < T ? k1 - (t0 + T) : T;- const size_t kb = i8 ? sizeof(attnf_q8k) * (DK / 256) : (size_t) DK * 2, vb = i8 ? sizeof(attnf_q8k) * (DV / 256) : (size_t) DV * 2;+ const size_t kb = i8 ? attnf_blk(kv->type) * (DK / 256) : (size_t) DK * 2, vb = i8 ? attnf_blk(kv->type) * (DV / 256) : (size_t) DV * 2;for (int64_t t = 0; t < n2; ++t) {const char * kr = kv->k + (t0 + T + t) * kv->nbk, * vr = kv->v + (t0 + T + t) * kv->nbv;for (size_t o = 0; o < kb; o += 64) _mm_prefetch(kr + o, _MM_HINT_T1);@@ -295,7 +357,12 @@ static void attnf_range(attnf_ws * ws, const attnf_kv * kv, int R, const int * p// K tileif (i8) {for (int t = 0; t < nt4; ++t) {- if (t < nt) {+ if (t < nt && kqb) {+ const uint8_t * b = (const uint8_t *) (kv->k + (t0 + t) * kv->nbk);+ int8_t * k8 = ws->K8 + (size_t) t * ATTNF_DMAX;+ ws->kc[t] = 128 * attnf_kvq_unpack(b + 4, kqb, k8);+ ws->kq[t] = k8; memcpy(&ws->kd[t], b, 4);+ } else if (t < nt) {const attnf_q8k * b = (const attnf_q8k *) (kv->k + (t0 + t) * kv->nbk);int32_t sk = 0;for (int j = 0; j < DK / 16; ++j) sk += b[j / 16].bsums[j % 16]; // DK/256 blocks of 16 sums@@ -389,7 +456,22 @@ static void attnf_range(attnf_ws * ws, const attnf_kv * kv, int R, const int * pws->al[r] = a;}// V tile- if (i8) {+ if (i8 && kqb) {+ for (int t = 0; t < nt; ++t) {+ const uint8_t * b = (const uint8_t *) (kv->v + (t0 + t) * kv->nbv);+ float * vt = ws->Vt + (size_t) t * ATTNF_DMAX;+ alignas(64) int8_t v8[256];+ attnf_kvq_unpack(b + 4, kqb, v8);+#if ATTNF_SIMD+ for (int d = 0; d < 256; d += 16)+ _mm512_store_ps(vt + d, _mm512_cvtepi32_ps(_mm512_cvtepi8_epi32(_mm_load_si128((const __m128i *) (v8 + d)))));+#else+ for (int d = 0; d < 256; ++d) vt[d] = (float) v8[d];+#endif+ memcpy(&ws->vd[t], b, 4);+ }+ for (int r = 0; r < R; ++r) { float * Sr = ws->S + (size_t) r * T; for (int t = 0; t < nt; ++t) Sr[t] *= ws->vd[t]; }+ } else if (i8) {for (int t = 0; t < nt; ++t) {const attnf_q8k * b = (const attnf_q8k *) (kv->v + (t0 + t) * kv->nbv);float * vt = ws->Vt + (size_t) t * ATTNF_DMAX;@@ -479,7 +561,7 @@ static void attnf_visible(const ggml_fp16_t * mr, int64_t nkv, int64_t * lo, int// returns true when the op was computed herestatic bool attnf_forward(const struct ggml_compute_params * params, struct ggml_tensor * dst) {const ggml_tensor * q = dst->src[0], * k = dst->src[1], * v = dst->src[2], * mask = dst->src[3], * sinks = dst->src[4];- const bool i8 = k->type == GGML_TYPE_Q8_K;+ const bool i8 = attnf_is_i8(k->type);if (!i8 && (attnf_mode() == 0 || !ATTNF_SIMD)) return false;float scale = 1.0f, max_bias = 0.0f, softcap = 0.0f;memcpy(&scale, (const float *) dst->op_params + 0, sizeof(float));@@ -492,9 +574,10 @@ static bool attnf_forward(const struct ggml_compute_params * params, struct ggmlv->ne[2] == HKV && HKV > 0 && NH % HKV == 0 && NH / HKV <= ATTNF_RMAX && q->nb[0] == sizeof(float) &&dst->nb[0] == sizeof(float) && dst->ne[0] == DV && dst->ne[1] == NH && dst->ne[2] == M &&(!mask || (mask->type == GGML_TYPE_F16 && mask->ne[2] == 1 && mask->ne[3] == 1 && mask->ne[0] >= NKV && mask->ne[1] >= M));- if (i8) ok = ok && DK % 256 == 0 && DV % 256 == 0 && ggml_type_size(GGML_TYPE_Q8_K) == sizeof(attnf_q8k);+ if (i8) ok = ok && DK % 256 == 0 && DV % 256 == 0 && ggml_type_size(k->type) == attnf_blk(k->type);+ if (attnf_kvq_bits(k->type)) ok = ok && DK == 256 && DV == 256 && ATTNF_SIMD;if (!ok) {- if (i8) GGML_ABORT("attn-fast: q8_K KV cache needs plain GQA attention (no sinks/alibi/softcap, head size multiple of 256)");+ if (i8) GGML_ABORT("attn-fast: q8_K / kv4-6 KV cache needs plain GQA attention (no sinks/alibi/softcap, head size multiple of 256; kv4-6: head size 256, AVX-512)");return false;}const int ith = params->ith, nth = params->nth;diff --git a/kernels/kvq.h b/kernels/kvq.hnew file mode 100644index 0000000..57d6cf5--- /dev/null+++ b/kernels/kvq.h@@ -0,0 +1,53 @@+// kvq.h: low-bit KV cache rows: GGML_TYPE_KV4 / KV5 / KV6 (`-ctk kv4|kv5|kv6`, same for -ctv). `./serve --kv6 | --kv5` use two of them.+// One block = one (token, KV head) row of 256 values: an f32 gain, then 256 b-bit codes as a little-endian bit stream+// (value i in bits [i*b, (i+1)*b)): 132 / 164 / 196 B for b = 4 / 5 / 6 (f16 512 B, q8_K 292 B).+// code = sign << (b-1) | level index; value = d * LEVEL[index]: Lloyd-Max levels of a unit Gaussian, rounded to int8+// (relative MSE on a Gaussian row 0.0095 / 0.0025 / 0.00068). The KV cache Hadamard-rotates K and V rows for every+// quantized cache type before they are stored, which makes the coordinates near-Gaussian with one scale; d is the+// least-squares gain for the chosen codes.+// Included by ggml-cpu.c (the SET_ROWS quantizers) and by attn-fast.inc (the reader).+#pragma once+#include <stdint.h>+#include <string.h>+#include <math.h>++static const int8_t KVQ_LV4[8] = { 6, 18, 30, 43, 58, 74, 95, 126 };+static const int8_t KVQ_LV5[16] = { 3, 8, 13, 18, 23, 28, 34, 40, 46, 53, 60, 68, 77, 88, 102, 124 };+static const int8_t KVQ_LV6[32] = { 1, 3, 6, 8, 10, 12, 15, 17, 19, 22, 24, 27, 29, 32, 34, 37, 40, 43, 46, 49, 52, 55, 59, 63, 67, 72, 77, 83,+ 89, 97, 108, 125 };+static const float KVQ_UNIT[7] = { 0, 0, 0, 0, 46.0f, 37.81f, 33.68f }; // level units per sigma+static inline const int8_t * kvq_levels(int bits) { return bits == 4 ? KVQ_LV4 : bits == 5 ? KVQ_LV5 : KVQ_LV6; }+static inline size_t kvq_block_size(int bits) { return 4 + 32 * (size_t) bits; }++static void kvq_quantize(const float * x, void * vy, int64_t k, int bits) {+ const int nl = 1 << (bits - 1);+ const int8_t * lv = kvq_levels(bits);+ const size_t bs = kvq_block_size(bits);+ float thr[32];+ for (int t = 0; t + 1 < nl; ++t) thr[t] = 0.5f * (float) (lv[t] + lv[t + 1]); // midpoints of adjacent levels+ uint8_t * y = (uint8_t *) vy;+ for (int64_t b = 0; b < k / 256; ++b, x += 256, y += bs) {+ float ss = 0.0f;+ for (int i = 0; i < 256; ++i) ss += x[i] * x[i];+ const float rms = sqrtf(ss / 256.0f), is = rms > 0.0f ? KVQ_UNIT[bits] / rms : 0.0f;+ float a[256]; int32_t idx[256];+ for (int i = 0; i < 256; ++i) { a[i] = fabsf(x[i]) * is; idx[i] = 0; }+ for (int t = 0; t + 1 < nl; ++t) for (int i = 0; i < 256; ++i) idx[i] += a[i] > thr[t];+ float xc = 0.0f, cc = 0.0f;+ for (int i = 0; i < 256; ++i) { const float l = (float) lv[idx[i]]; xc += fabsf(x[i]) * l; cc += l * l; }+ const float d = cc > 0.0f ? xc / cc : 0.0f;+ memcpy(y, &d, 4);+ uint8_t * qs = y + 4;+ memset(qs, 0, bs - 4);+ for (int i = 0; i < 256; ++i) {+ const uint32_t code = (uint32_t) idx[i] | (x[i] < 0.0f ? (uint32_t) nl : 0u);+ const int bit = i * bits, byte = bit >> 3, sh = bit & 7;+ const uint32_t v = code << sh;+ qs[byte] |= (uint8_t) v;+ if (v >> 8) qs[byte + 1] |= (uint8_t) (v >> 8);+ }+ }+}+static void quantize_row_kv4(const float * x, void * y, int64_t k) { kvq_quantize(x, y, k, 4); }+static void quantize_row_kv5(const float * x, void * y, int64_t k) { kvq_quantize(x, y, k, 5); }+static void quantize_row_kv6(const float * x, void * y, int64_t k) { kvq_quantize(x, y, k, 6); }diff --git a/patches/0009-kvq-types.patch b/patches/0009-kvq-types.patchnew file mode 100644index 0000000..c4dc207--- /dev/null+++ b/patches/0009-kvq-types.patch@@ -0,0 +1,67 @@+diff --git a/common/arg.cpp b/common/arg.cpp+index eef8b9962..09476ec11 100644+--- a/common/arg.cpp++++ b/common/arg.cpp+@@ -311,6 +311,9 @@ const std::vector<ggml_type> kv_cache_types = {+ GGML_TYPE_IQ4_NL,+ GGML_TYPE_Q5_0,+ GGML_TYPE_Q5_1,++ GGML_TYPE_KV4, // 4 / 5 / 6-bit rows, one f32 gain each (132 / 164 / 196 B per 256): ggml-cpu/kvq.h,++ GGML_TYPE_KV5, // CPU attention kernel only++ GGML_TYPE_KV6,+ GGML_TYPE_Q8_K, // int8 rows with one f32 scale each; CPU attention kernel only (release/kernels/attn-fast.inc)+ };++diff --git a/ggml/include/ggml.h b/ggml/include/ggml.h+index c9339461c..61f879010 100644+--- a/ggml/include/ggml.h++++ b/ggml/include/ggml.h+@@ -432,6 +432,10 @@ extern "C" {+ GGML_TYPE_Q2_0 = 42,+ // Prism-private Q2_0 at group size 128 (upstream Q2_0 is group 64). High id so it+ // slots above upstream types; type_traits is sized to COUNT (143) with 43..141 unused.++ // cpu27b: low-bit KV cache rows (ggml-cpu/kvq.h; CPU attention kernel only)++ GGML_TYPE_KV6 = 139,++ GGML_TYPE_KV5 = 140,++ GGML_TYPE_KV4 = 141,+ GGML_TYPE_PQ2_0 = 142,+ GGML_TYPE_PTQ1_0 = 143, // Prism-private ternary, group 128+ GGML_TYPE_COUNT = 144,+diff --git a/ggml/src/ggml-cpu/ggml-cpu.c b/ggml/src/ggml-cpu/ggml-cpu.c+index da2575cd9..419f6a42a 100644+--- a/ggml/src/ggml-cpu/ggml-cpu.c++++ b/ggml/src/ggml-cpu/ggml-cpu.c+@@ -212,6 +212,8 @@ typedef pthread_t ggml_thread_t;+ #include <TargetConditionals.h>+ #endif+++#include "kvq.h" // quantize_row_kv4/5/6 (GGML_TYPE_KV4/5/6: low-bit KV cache rows)+++ static const struct ggml_type_traits_cpu type_traits_cpu[GGML_TYPE_COUNT] = {+ [GGML_TYPE_F32] = {+ .from_float = (ggml_from_float_t) ggml_cpu_fp32_to_fp32,+@@ -408,6 +410,9 @@ static const struct ggml_type_traits_cpu type_traits_cpu[GGML_TYPE_COUNT] = {+ [GGML_TYPE_Q8_K] = {+ .from_float = quantize_row_q8_K,+ },++ [GGML_TYPE_KV4] = { .from_float = quantize_row_kv4, },++ [GGML_TYPE_KV5] = { .from_float = quantize_row_kv5, },++ [GGML_TYPE_KV6] = { .from_float = quantize_row_kv6, },+ [GGML_TYPE_BF16] = {+ .from_float = (ggml_from_float_t) ggml_cpu_fp32_to_bf16,+ .vec_dot = (ggml_vec_dot_t) ggml_vec_dot_bf16,+diff --git a/ggml/src/ggml.c b/ggml/src/ggml.c+index 762655731..8e39abfe1 100644+--- a/ggml/src/ggml.c++++ b/ggml/src/ggml.c+@@ -898,6 +898,10 @@ static const struct ggml_type_traits type_traits[GGML_TYPE_COUNT] = {+ .type_size = sizeof(block_q8_K),+ .is_quantized = true,+ },++ // low-bit KV cache rows: f32 gain + 256 b-bit codes (ggml-cpu/kvq.h)++ [GGML_TYPE_KV4] = { .type_name = "kv4", .blck_size = 256, .type_size = 132, .is_quantized = true, },++ [GGML_TYPE_KV5] = { .type_name = "kv5", .blck_size = 256, .type_size = 164, .is_quantized = true, },++ [GGML_TYPE_KV6] = { .type_name = "kv6", .blck_size = 256, .type_size = 196, .is_quantized = true, },+ [GGML_TYPE_BF16] = {+ .type_name = "bf16",+ .blck_size = 1,diff --git a/patches/apply.sh b/patches/apply.shindex fa18387..8976e69 100755--- a/patches/apply.sh+++ b/patches/apply.sh@@ -12,13 +12,15 @@# = prefix states on disk). See README.md "What is inside".# 9. 0008-*.patch: our attention kernel (kernels/attn-fast.inc) for FLASH_ATTN_EXT (GGML_ATTN=0 = the fork's paths), and# q8_K as a KV cache type (int8 KV, opt-in). GGML_ATTN=0 selects the fork's paths.+# 10. 0009-*.patch: kv5 / kv6 KV cache types (kernels/kvq.h: 5 / 6-bit rows read by attn-fast.inc; opt-in,+# `serve --kv6` / `--kv5`).set -euhere=$(cd "$(dirname "$0")" && pwd)src=${1:?usage: apply.sh <llama.cpp checkout>}cp "$here"/../kernels/ptq1-common.h "$here"/../kernels/ptq1-drop.h "$here"/../kernels/ptq1-fast.inc "$here"/../kernels/ptq1-panel.inc \- "$here"/../kernels/ptq1-vbmi.inc "$here"/../kernels/x1-q4k.inc "$here"/../kernels/attn-fast.inc "$src/ggml/src/ggml-cpu/"+ "$here"/../kernels/ptq1-vbmi.inc "$here"/../kernels/x1-q4k.inc "$here"/../kernels/attn-fast.inc "$here"/../kernels/kvq.h "$src/ggml/src/ggml-cpu/"git -C "$src" checkout -q -- ggml/src/ggml-cpu/ggml-cpu.c ggml/src/ggml-cpu/ops.cpp src/models/qwen35.cpp \- src/llama-context.cpp tools/server/server-context.cpp common/chat.h common/common.h common/arg.cpp+ src/llama-context.cpp tools/server/server-context.cpp common/chat.h common/common.h common/arg.cpp ggml/include/ggml.h ggml/src/ggml.cfor p in "$here"/0*.patch; dogit -C "$src" apply "$p" && echo "applied: $(basename "$p")"donediff --git a/serve b/serveindex 10b5d13..a38fda4 100755--- a/serve+++ b/serve@@ -7,9 +7,13 @@# ./serve --no-webui # API only: do not serve the chat page at /# ./serve --no-spec # plain decoding (no speculation)# ./serve --spec # keep speculation on where serve would turn it off: for lack of memory-# # (< 12 GB RAM: --ctx > 4096 without --kv8, > 8192 with) or on a CPU without AVX-512 VNNI+# # (< 12 GB RAM: --ctx > 4096; > 8192 with --kv8; > 12288 with --kv6; > 16384 with --kv5)+# # or on a CPU without AVX-512 VNNI# ./serve --think # let the model think before answering (much longer replies)# ./serve --no-resume # turn off agent-loop resume (default on; saved prefix states live in $OPENSET_LLM_HOME/state)+# ./serve --kv6 | --kv5 # EXPERIMENTAL: 6- / 5-bit attention cache, 38 % / 32 % of the default cache; on 8 GB:+# # speculation up to 12k (kv6) / 16k (kv5) context, plain decoding to 32k (kv6) / 64k (kv5);+# # not exact; needs AVX-512; choose one of --kv8, --kv6, --kv5# ./serve --kv8 # EXPERIMENTAL: int8 attention cache: 43 % less cache memory, helps on long prompts; not exact# ./serve --fl128 # EXPERIMENTAL: a faster variant that is not bit-identical# ./serve --spec-adapt # EXPERIMENTAL: adaptive draft depth (--spec-depth N; default 3, 5 on >= 12 GB RAM)@@ -19,7 +23,7 @@set -euo pipefailHERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)PREFIX=${OPENSET_LLM_HOME:-$HOME/.openset-llm}-HOST=127.0.0.1; PORT=8080; CTX=; THREADS=$(nproc); SPEC=1; SPEC_FORCE=0; X1=1; MULTI=0; THINK=off; EXTRA=(); ADAPT=0; DEPTH=; FL128=0; KV8=0; RESUME=1; WEBUI=1+HOST=127.0.0.1; PORT=8080; CTX=; THREADS=$(nproc); SPEC=1; SPEC_FORCE=0; X1=1; MULTI=0; THINK=off; EXTRA=(); ADAPT=0; DEPTH=; FL128=0; KV8=0; KVQ=0; RESUME=1; WEBUI=1while [ $# -gt 0 ]; docase $1 in--host|--port|--ctx|--threads|--multi|--spec-depth) [ $# -ge 2 ] || { echo "$1 needs a value" >&2; exit 2; } ;;&@@ -36,10 +40,11 @@ while [ $# -gt 0 ]; do--spec-depth) DEPTH=$2; shift ;;--fl128) FL128=1 ;;--kv8) KV8=1 ;;+ --kv6|--kv5) [ "$KVQ" = 0 ] || { echo "choose one of --kv8, --kv6, --kv5" >&2; exit 2; }; KVQ=${1#--kv} ;;--no-resume) RESUME=0 ;;--no-webui) WEBUI=0 ;;--) shift; EXTRA=("$@"); break ;;- -h|--help) sed -n '2,18p' "$0"; exit 0 ;;+ -h|--help) sed -n '2,22p' "$0"; exit 0 ;;*) echo "unknown option: $1 (pass llama-server flags after --)" >&2; exit 2 ;;esacshift@@ -67,9 +72,9 @@ if [ "$MULTI" -ge 2 ]; thenfielseNP=1; CTX=${CTX:-4096}; CTX_TOTAL=$CTX- if [ "$CTX" -gt 8192 ]; then+ if [ "$CTX" -gt 8192 ] && [ "$KV8" = 0 ] && [ "$KVQ" = 0 ]; thenecho "warning: --ctx $CTX > 8192. On an 8 GB machine the KV cache and recurrent-state checkpoints can push the" >&2- echo " server past RAM; 8192 is the measured-safe limit there." >&2+ echo " server past RAM; 8192 is the measured-safe limit there (--kv8, --kv6 or --kv5 go further)." >&2fifiargs=(-m "$MODEL" -t "$THREADS" -c "$CTX_TOTAL" -ub 256 -np "$NP"@@ -102,16 +107,37 @@ if [ "$SPEC" = 1 ] && [ "$MULTI" -lt 2 ] && [ "$SPEC_FORCE" = 0 ]; thenSPEC=0 ;;esacfi+[ "$KV8" = 1 ] && [ "$KVQ" != 0 ] && { echo "choose one of --kv8, --kv6, --kv5" >&2; exit 2; }+if [ "$KVQ" != 0 ]; then+ # the 6- / 5-bit rows are read by the AVX-512 attention kernel only; the int8 cache (--kv8) also runs without it+ case " $(grep -m1 '^flags' /proc/cpuinfo 2>/dev/null | cut -d: -f2) " in+ *" avx512bw "*) ;;+ *) echo "--kv$KVQ needs a CPU with AVX-512; on this CPU use --kv8 (int8 attention cache) instead" >&2; exit 2 ;;+ esac+ # measured on 8 vCPU / 8 GB (2026-11-21): kv6 runs to 32k and kv5 to 64k with ~0.1-0.7 GB to spare; past that the+ # server is OOM-killed at load or stalls+ KVQ_CTX_MAX=32768; [ "$KVQ" = 5 ] && KVQ_CTX_MAX=65536+ if [ "$MEM_KB" -lt 12000000 ] && [ "$CTX_TOTAL" -gt "$KVQ_CTX_MAX" ]; then+ echo "--ctx $CTX_TOTAL with --kv$KVQ does not fit in < 12 GB of RAM: the measured limit is $KVQ_CTX_MAX" >&2; exit 2+ fi+fi[ "$KV8" = 1 ] && args+=(-ctk q8_K -ctv q8_K) # needs patch 0008 (attn-fast.inc); f16 KV is the exact default+[ "$KVQ" != 0 ] && args+=(-ctk "kv$KVQ" -ctv "kv$KVQ") # patch 0009 (kvq.h): 6- / 5-bit rows, 38 % / 32 % of the f16 cache+# measured on 8 GB. kv6 at a full 16k context pages (5.8-6.7 tok/s, slower than plain decoding); it holds to 14k, kv5 to 16k+SPEC_CTX_MAX=4096; [ "$KV8" = 1 ] && SPEC_CTX_MAX=8192; [ "$KVQ" = 6 ] && SPEC_CTX_MAX=12288; [ "$KVQ" = 5 ] && SPEC_CTX_MAX=16384if [ "$SPEC" = 1 ] && [ "$MULTI" -lt 2 ] && [ "$SPEC_FORCE" = 0 ] && \- { [ "$CTX" -gt 8192 ] || { [ "$CTX" -gt 4096 ] && [ "$KV8" = 0 ]; }; } && \+ [ "$CTX" -gt "$SPEC_CTX_MAX" ] && \[ "$MEM_KB" -lt 12000000 ]; then# with patch 0008 the verify batch is fast at any depth (16 GB: MTP 1.6-1.7x plain at 4k-32k), but on 8 GB the draft# state + f16 KV leave < 0.1 GB free above 4k and page reclaim makes MTP slower than plain decode (8k: 6.5 vs 7.5 tok/s);# with --kv8 the int8 KV leaves room and MTP wins again at 8k (10.9 tok/s); at 16k MTP does not fit 8 GB either way.# (measured 2026-11-10 on an 8 vCPU / 8 GB Ice Lake box)echo "note: --ctx $CTX on < 12 GB RAM: speculation off (it is slower than plain decode here or does not fit: memory);" >&2- echo " up to --ctx 8192, --kv8 (int8 attention cache) keeps it on and helps on long prompts; --spec forces it" >&2+ if [ "$KV8" = 0 ] && [ "$KVQ" = 0 ]; then+ echo " a compact attention cache keeps it on further: --kv8 to 8192, --kv6 to 12288, --kv5 to 16384; --spec forces it" >&2+ else+ echo " the limit with this cache is --ctx $SPEC_CTX_MAX (--kv8 8192, --kv6 12288, --kv5 16384); --spec forces it" >&2+ fiSPEC=0fiif [ "$SPEC" = 1 ]; then