Skip to content

[Engine] Inkling 2: --kv q8_0, an 8-bit attention cache without FP8 hardware - #214

Open
okada-shogo wants to merge 5 commits into
simplexai-labs:mainfrom
okada-shogo:port/kv-q8
Open

okada-shogo wants to merge 5 commits into
simplexai-labs:mainfrom
okada-shogo:port/kv-q8

Conversation

@okada-shogo

@okada-shogo okada-shogo commented Nov 22, 2026 •

Copy link
Copy Markdown

Problem and result

--kv fp8 halves Inkling 2's attention cache, but needs NVIDIA Ampere or newer. This adds an opt-in --kv q8_0 cache
that saves about the same VRAM and does not need FP8 hardware, so it can also serve Turing (sm_75) and AMD (HIP).
fp16 stays the default and is unchanged.

fp16 fp8 q8_0
Bytes per cache row (8 heads x 128) 2,048 1,056 1,088
Cards all NVIDIA Ampere+ Turing (kernel test); AMD RX 9070 XT (kernel test + end-to-end, on top of #217); Metal refuses it

The size is from the layout (int8 codes plus one fp16 scale per 32 values).

Speed (RTX 2070 Super 8 GB, IQ4_NL, default placement): at a 115.5K prompt, q8_0 keeps 654 experts in VRAM against fp16's 266: prompt +43.5%, decode +9.8%. From 8K to 64K both run at the same speed within run-to-run noise. The fp16 default runs at main's speed.

The same build, --kv q8_0 against its fp16 default (= main's, see Speed under Validation):

Prompt speed, tok/s, higher is better, mean of 3 runs (115.5K: 1 run). Change is computed from the unrounded means:

Card Model Measure #214 --kv fp16 (= main's default) this PR alone --kv q8_0 Change Experts in VRAM, of 3,072 (baseline → this PR)
RTX 2070 Super 8 GB IQ4_NL prompt 8K tokens 495.5 486.9 −1.7% 1042 → 1071
RTX 2070 Super 8 GB IQ4_NL prompt 32K tokens 482.1 477.4 −1.0% 850 → 969
RTX 2070 Super 8 GB IQ4_NL prompt 64K tokens 434.6 432.8 −0.4% 611 → 828
RTX 2070 Super 8 GB IQ4_NL prompt 115.5K tokens 260.9 374.3 +43.5% 266 → 654

Decode speed, tok/s, higher is better, mean of 3 runs (115.5K: 1 run). Change is computed from the unrounded means:

Card Model Measure #214 --kv fp16 (= main's default) this PR alone --kv q8_0 Change Experts in VRAM, of 3,072 (baseline → this PR)
RTX 2070 Super 8 GB IQ4_NL decode after an 8K-token prompt 30.5 28.8 −5.6% 1042 → 1071
RTX 2070 Super 8 GB IQ4_NL decode after a 32K-token prompt 24.0 26.6 +11.0% 850 → 969
RTX 2070 Super 8 GB IQ4_NL decode after a 64K-token prompt 22.3 22.7 +2.1% 611 → 828
RTX 2070 Super 8 GB IQ4_NL decode after a 115.5K-token prompt 16.3 17.9 +9.8% 266 → 654

RTX 2070 (8 GB, sm_75), IQ4_NL, q8_0 against fp16 (details under Validation):

  • With every expert on the CPU, q8_0 fed fp16's 128 greedy tokens: KL mean 1.1e-5 (max 8.2e-4), top-1 128 of 128 after
    an 8,192-token prompt; KL mean 0.034 (max 0.82), top-1 125 of 128 after the 2,048-token prompt (the sensitive one,
    see below). fp16 against itself is 0.
  • 131,072 context with --kv-whole: 518 experts fit in VRAM with q8_0, 129 with fp16. A 115,500-token prompt loads and
    answers with q8_0 (941 experts in VRAM, prompt 386 tok/s, decode 13.8 tok/s).

Related discussion

Split out of #200, as the maintainer asked there:
#200 (comment)

Approach

  • Cache row: the int8 codes, then one fp16 scale for each block of 32 values (llama.cpp's quantize_row_q8_0 rule).
  • Writers (ink_qkv, ink_clips) store codes and scales. Decode (ink_attend) and the Ampere flash kernel turn the
    codes into floats as they read them. On Turing/HIP, attend_batch() dequantizes blocks of rows into a 16 MiB
    workspace. Kvq8 gets a scale stride (ss, 0 = the old rule), so other models are unchanged.
  • On HIP, attend_batch()'s FP32 products (STRATA_HIP_PREFILL_FP32=1 or 2) read a q8 cache as two plain arrays,
    so a cache with its own strides (this one) keeps the half path there.
  • The 16 MiB workspace is allocated on Ampere+ too, where ink_attend_batch reads the cache directly: it is what
    attend_batch() needs under STRATA_INK_FLASH=0.
  • New test poly_inkling2_kvq8_test.
  • Lossy (rounding of K and V), opt-in only. Shaped like VecSzn's --kv fp8.

Validation

Passed:

  • CUDA 12.9 build, sm_75, GCC 11.4, at this branch's head: strata-poly and poly_inkling2_kvq8_test build.

  • poly_inkling2_kvq8_test, RTX 2070 Super (sm_75): 13 checks pass, 1 skipped (Ampere-only), PASS: 0 failed:

    • stores equal the host's quantize_row_q8_0 byte for byte (full and ring caches, 0 of 2,506,752 / 1,114,112 bytes
      differ);
    • ink_attend within 2.7e-7 relative RMS of a float64 reference (1 and 4 rows, full and window);
    • attend_batch within 5.3e-4 relative RMS (halves of queries and weights), and the same bits as an fp16 cache of
      the dequantized halves.
    • This run used the build of the commit before the head; the head only adds (void) casts to cudaFree calls in the
      test (for HIP's nodiscard).
  • HIP build (compile only, no AMD card), built on top of [Engine] build Inkling 2's DSpark screen on HIP #206 (this head merged with b681c69), ROCm 7.2.4 container,
    gfx1100;gfx1201: cmake -S engine -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DSTRATA_ENABLE_HIP=ON -DSTRATA_ENABLE_CUDA=OFF -DCMAKE_HIP_ARCHITECTURES="gfx1100;gfx1201" -DSTRATA_BUILD_TESTS=ON -DSTRATA_NATIVE_EXPERTS=ON -DCMAKE_HIP_COMPILER=/opt/rocm/llvm/bin/clang++ -DCMAKE_PREFIX_PATH=/opt/rocm && ninja -C build -k 0 strata-poly poly_inkling2_kvq8_test: both build, 0 errors.

  • Head (d57f5d0): CUDA 12.9 sm_75 build of strata-poly and poly_inkling2_kvq8_test. HIP (ROCm 7.2.4, gfx1201): kernels.cu compiles for strata-poly, poly_hip_prefill_fp32_test and poly_inkling2_kvq8_test (the full HIP link needs [Engine] build Inkling 2's DSpark screen on HIP #206). python3 tools/check --only docs: 6 of 6.

  • AMD, RX 9070 XT (gfx1201), SaladCloud container, ROCm 7.2.4: this branch on top of [Engine] Inkling 2 on AMD (RDNA3/RDNA4): HIP support #217's tip 64830fb, with the
    merge fix below (fork branch port/kvq8-on-hip, 141963a). HIP build of strata-poly and poly_inkling2_kvq8_test
    0 errors. poly_inkling2_kvq8_test: 13 checks pass, 1 skipped (Ampere-only), PASS: 0 failed (stores 0 of
    2,506,752 / 1,114,112 bytes differ; ink_attend rel RMS 2.2-2.7e-7; attend_batch 4.9-5.3e-4).
    End to end, IQ4_NL, strata-poly --tokens <N ids> --max-new 16 --top-probs 20 --kv q8_0 against --kv fp16, q8_0
    also fed fp16's tokens (--follow). KL over the top 20 in nats (lower is better), top-1 as a count of 16 tokens:

    Card Model Prompt + written tokens Where the experts are Measure fp16 this PR alone --kv q8_0
    RX 9070 XT IQ4_NL 302 + 16 2,427 in VRAM KL(top 20) mean vs fp16, nats 0 0.0012
    RX 9070 XT IQ4_NL 302 + 16 2,427 in VRAM top-1 same, of 16 tokens 16 16
    RX 9070 XT IQ4_NL 302 + 16 all on the CPU (--no-gpu-experts) KL(top 20) mean vs fp16, nats 0 0.0017
    RX 9070 XT IQ4_NL 302 + 16 all on the CPU (--no-gpu-experts) top-1 same, of 16 tokens 16 16
    RX 9070 XT IQ4_NL 2,048 + 16 2,427 in VRAM KL(top 20) mean vs fp16, nats 0 0.249
    RX 9070 XT IQ4_NL 2,048 + 16 2,427 in VRAM top-1 same, of 16 tokens 16 15
    RX 9070 XT IQ4_NL 2,048 + 16 all on the CPU (--no-gpu-experts) KL(top 20) mean vs fp16, nats 0 0.081
    RX 9070 XT IQ4_NL 2,048 + 16 all on the CPU (--no-gpu-experts) top-1 same, of 16 tokens 16 15
    RX 9070 XT IQ4_NL 8,192 + 16 all on the CPU (--no-gpu-experts) KL(top 20) mean vs fp16, nats 0 0.00000
    RX 9070 XT IQ4_NL 8,192 + 16 all on the CPU (--no-gpu-experts) top-1 same, of 16 tokens 16 16

    Greedy tokens: the same as fp16's at 302 (--no-gpu-experts) and 8,192; first differ at 302 with experts in VRAM
    (token 6, ln p1/p2 = 0.07), 2,048 with experts in VRAM (token 2, ln p1/p2 = 3.87) and 2,048 --no-gpu-experts
    (token 5, ln p1/p2 = 0.89). Largest step's KL: 0.0079, 0.0068, 2.53, 0.46, 0.00000 (rows in order). At 8,192 the
    top-20 values differ slightly but the next tokens are near-certain. No fp16-vs-fp16 noise floor on this card.

    The 2,048 prompt is the one that is unusually sensitive on every engine ([Engine] Inkling 2 on AMD (RDNA3/RDNA4): HIP support #217: HIP against CUDA, fp16, KL 0.215 on
    this card; the CUDA card against itself, experts on the card vs on the CPU, 0.095). Logs:
    kit44 progress44.log and res/ (container /work/c44).

  • CUDA end to end, RTX 2070 8 GB (sm_75), Ryzen 7 5800X, IQ4_NL, CUDA 12.9 sm_75 build of e4cce18 with a test-only
    patch that dumps each step's full logits (the head's later commits are a test cast, a HIP-only fix and docs):

    • fp16 default against main d1f711e, every expert on the CPU, 64 greedy tokens: ids and top lines the same at
      2,048 and 8,192-token prompts (and the branch against itself).
    • KL over the full logits, every expert on the CPU, 128 tokens. F1 = fp16 greedy, F2 = fp16 fed F1's tokens (the
      floor), Q = q8_0 fed F1's tokens, Qg = q8_0 greedy:

    KL against F1 in nats (lower is better), top-1 as a count of 128 tokens:

    Card Model Prompt Measure fp16, F2 (floor) this PR alone --kv q8_0, Q
    RTX 2070 8 GB IQ4_NL 2,048 + 128 KL mean, nats 0 0.034
    RTX 2070 8 GB IQ4_NL 2,048 + 128 KL median, nats 0 4.6e-4
    RTX 2070 8 GB IQ4_NL 2,048 + 128 KL max, nats 0 0.82
    RTX 2070 8 GB IQ4_NL 2,048 + 128 top-1 same, of 128 tokens 128 125
    RTX 2070 8 GB IQ4_NL 8,192 + 128 KL mean, nats 0 1.1e-5
    RTX 2070 8 GB IQ4_NL 8,192 + 128 KL median, nats 0 5.0e-7
    RTX 2070 8 GB IQ4_NL 8,192 + 128 KL max, nats 0 8.2e-4
    RTX 2070 8 GB IQ4_NL 8,192 + 128 top-1 same, of 128 tokens 128 128

    Greedy q8_0 (Qg) against F1: first differs at token 8 after the 2,048 prompt; the same 128 of 128 after 8,192.

    • Default placement (experts on the card, 937 / 941 in VRAM), 8,192-token prompt + 128 greedy tokens, fp16 against
      q8_0: the same 128 tokens; prompt 505 -> 512 tok/s, decode 28.7 -> 28.9 tok/s.
    • --max-context 131072 --kv-whole, 2,048-token prompt: fp16 loads with 129 experts in VRAM, q8_0 with 518.
      --max-context 131072 --kv q8_0, a 115,500-token prompt + 32 tokens: runs, 941 experts in VRAM, prompt 385.9 tok/s,
      decode 13.8 tok/s.
  • CUDA, RTX 3060 12 GB (sm_86), a 75;86 build of d97dd7e: poly_inkling2_kvq8_test PASS: 0 failed, now with the
    Ampere-only ink_attend_batch (flash kernel) checks too (full rel RMS 5.3e-4, window 4.9e-4). The fp16 default
    against main (frozen split, IQ4_NL, 2,048 + 32 and 8,192 + 16 tokens): ids and every top line the same.

  • Speed, RTX 2070 Super 8 GB (sm_75), Ryzen 7 5800X, IQ4_NL, CUDA 12.9 sm_75 builds of main d1f711e and this head d57f5d0. One strata-poly --serve engine per run, one greedy request: the prompt, then 128 tokens. Default placement (no expert profile, no freeze, no --kv-whole in any mode: the attention cache grows and takes VRAM from the expert cache). --max-context 16384 / 36864 / 69632 / 131072 for the 8K / 32K / 64K / 115.5K prompts, the same in each row. 3 rounds, the order rotated each round. Main was left out at 64K and 115.5K to keep the job near 60 min. "Experts in VRAM" is the expert cache after the request. fp16 fits at 115.5K too (no failure), with 266 experts left.
    Decode at 8K and 32K moves by about 10% between runs (single slow runs: 26.1 for q8_0 at 8K, 20.5 for fp16 at 32K), so the decode differences there are noise. Prompt and 64K/115.5K figures are steady.

    Default (--kv fp16) against main:

Prompt speed, tok/s, higher is better, mean of 3 runs. Change is computed from the unrounded means:

Card Model Measure main this PR alone --kv fp16 Change
RTX 2070 Super 8 GB IQ4_NL prompt 8K tokens 495.8 495.5 −0.1%
RTX 2070 Super 8 GB IQ4_NL prompt 32K tokens 480.1 482.1 +0.4%

Decode speed, tok/s, higher is better, mean of 3 runs. Change is computed from the unrounded means:

Card Model Measure main this PR alone --kv fp16 Change
RTX 2070 Super 8 GB IQ4_NL decode after an 8K-token prompt 30.0 30.5 +1.4%
RTX 2070 Super 8 GB IQ4_NL decode after a 32K-token prompt 26.0 24.0 −7.8%
Each run (tok/s)

Prompt speed, tok/s, each run:

Card Model Measure Build Run 1 Run 2 Run 3
RTX 2070 Super 8 GB IQ4_NL prompt 8K tokens main 494.4 496.0 496.9
RTX 2070 Super 8 GB IQ4_NL prompt 8K tokens this PR alone --kv fp16 492.5 496.9 497.1
RTX 2070 Super 8 GB IQ4_NL prompt 8K tokens this PR alone --kv q8_0 492.5 493.3 474.9
RTX 2070 Super 8 GB IQ4_NL prompt 32K tokens main 473.3 483.7 483.4
RTX 2070 Super 8 GB IQ4_NL prompt 32K tokens this PR alone --kv fp16 478.9 483.5 483.9
RTX 2070 Super 8 GB IQ4_NL prompt 32K tokens this PR alone --kv q8_0 475.8 479.7 476.6
RTX 2070 Super 8 GB IQ4_NL prompt 64K tokens this PR alone --kv fp16 433.8 433.7 436.2
RTX 2070 Super 8 GB IQ4_NL prompt 64K tokens this PR alone --kv q8_0 432.0 432.9 433.4
RTX 2070 Super 8 GB IQ4_NL prompt 115.5K tokens this PR alone --kv fp16 260.9 not run not run
RTX 2070 Super 8 GB IQ4_NL prompt 115.5K tokens this PR alone --kv q8_0 374.3 not run not run

Decode speed, tok/s, each run:

Card Model Measure Build Run 1 Run 2 Run 3
RTX 2070 Super 8 GB IQ4_NL decode after an 8K-token prompt main 30.2 30.8 29.1
RTX 2070 Super 8 GB IQ4_NL decode after an 8K-token prompt this PR alone --kv fp16 30.7 30.8 29.9
RTX 2070 Super 8 GB IQ4_NL decode after an 8K-token prompt this PR alone --kv q8_0 29.1 31.1 26.1
RTX 2070 Super 8 GB IQ4_NL decode after a 32K-token prompt main 24.7 26.7 26.6
RTX 2070 Super 8 GB IQ4_NL decode after a 32K-token prompt this PR alone --kv fp16 24.9 26.5 20.5
RTX 2070 Super 8 GB IQ4_NL decode after a 32K-token prompt this PR alone --kv q8_0 25.6 27.3 26.9
RTX 2070 Super 8 GB IQ4_NL decode after a 64K-token prompt this PR alone --kv fp16 22.2 22.1 22.5
RTX 2070 Super 8 GB IQ4_NL decode after a 64K-token prompt this PR alone --kv q8_0 22.6 22.7 22.9
RTX 2070 Super 8 GB IQ4_NL decode after a 115.5K-token prompt this PR alone --kv fp16 16.3 not run not run
RTX 2070 Super 8 GB IQ4_NL decode after a 115.5K-token prompt this PR alone --kv q8_0 17.9 not run not run

Experts in VRAM after each request, of 3,072:

Card Model Prompt Build Run 1 Run 2 Run 3
RTX 2070 Super 8 GB IQ4_NL 8K tokens main 1042 1042 1042
RTX 2070 Super 8 GB IQ4_NL 8K tokens this PR alone --kv fp16 1042 1042 1042
RTX 2070 Super 8 GB IQ4_NL 8K tokens this PR alone --kv q8_0 1071 1071 1071
RTX 2070 Super 8 GB IQ4_NL 32K tokens main 850 850 850
RTX 2070 Super 8 GB IQ4_NL 32K tokens this PR alone --kv fp16 850 850 850
RTX 2070 Super 8 GB IQ4_NL 32K tokens this PR alone --kv q8_0 969 969 969
RTX 2070 Super 8 GB IQ4_NL 64K tokens this PR alone --kv fp16 611 611 611
RTX 2070 Super 8 GB IQ4_NL 64K tokens this PR alone --kv q8_0 828 828 828
RTX 2070 Super 8 GB IQ4_NL 115.5K tokens this PR alone --kv fp16 266 not run not run
RTX 2070 Super 8 GB IQ4_NL 115.5K tokens this PR alone --kv q8_0 654 not run not run

Job: kvq8-speed.sh (NAME=sp1), log ~/inkling-work/kvq8-speed/sp1/progress.log; window 64 min, rc=0, server restored, live expert profiles unchanged.

Failed: none observed.

Not run:

  • poly_inkling2_kvq8_test with STRATA_HIP_PREFILL_FP32=1, and any AMD run of this branch without [Engine] Inkling 2 on AMD (RDNA3/RDNA4): HIP support #217 (main alone
    does not run Inkling 2 on AMD). RDNA3 (RX 7900 XT): not run.
  • A test of the decode store (ink_qkv with q8_0); the test checks stores through ink_clips only.
  • Speed on UD-Q2_K_XL and AD-IQ3_S; main at 64K and 115.5K (the fp16 default stands in, equal to main at 8K and 32K); a 2nd and 3rd run at 115.5K.

Limits

Credits: ported from our engine's --kv q8_0 (#200) onto VecSzn's Inkling 2 engine, following his --kv fp8.

🤖 Generated with Claude Code

okada-shogo and others added 3 commits November 23, 2026 04:26
The row layout other work builds on: a cache row is the n_kv heads' dim
int8 codes, then one half scale for each block of 32 values in the codes'
order (ink_kvq8_row(): 1,088 bytes for Inkling 2 Mini's 8 heads of 128,
fp16 takes 2,048).  A block's scale is its largest magnitude over 127 and
a code round(x / scale), llama.cpp's quantize_row_q8_0 rule.  Codes start
4-aligned in a row (row bytes are a multiple of 16).  Rows are allocated
like the fp8 ones (kv_alloc(kv_row(), 1), seq_alloc for the rings), so
save/load of rows is unchanged.  Token kernels (ink_qkv, ink_clips,
score, value) and the sm80 flash kernel decode the codes as they read
them; attend_batch() (Turing, HIP) dequantizes a block of rows into a
workspace through Kvq8 with a new scale stride (Kvq8::ss, 0 = old rule).

Not built or tested yet.

Port of our simplexai-labs#200 engine's --kv q8_0 (src/poly/inkling.cpp) onto VecSzn's
Inkling 2 engine, shaped like his --kv fp8 (abdb3b4).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…HIP nodiscard)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
okada-shogo and others added 2 commits November 23, 2026 08:29
Under STRATA_HIP_PREFILL_FP32=1 or 2, attend_batch() on HIP took
dequant_q8_f32_kernel or gemm_q8 (hip_prefill_gemm.cuh), which read a q8
cache as width codes and width / 32 scales a row in two arrays.  Inkling
2's --kv q8_0 cache keeps a row's codes and scales together
(Kvq8::rs / ss), so a prompt batch on an AMD card would have read them at
the wrong offsets.  A cache with its own strides now keeps the half
path, which takes them.  Laguna, Kolibri, LFM2-MoE and Qwen3.5 on HIP
pass rs = ss = 0 and keep the path they had.

Found by an independent review of the PR.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
docs/models/inkling.md said q8_0 works on every card, Turing and AMD
included, and the usage text said Inkling 2 takes it on every card, but
only the Turing kernel test has run, nothing on AMD, and a Mac refuses
it (inkling2.cpp).  Say so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@okada-shogo
okada-shogo marked this pull request as ready for review November 23, 2026 01:47
okada-shogo added a commit to okada-shogo/PolyStrata that referenced this pull request Nov 23, 2026
…ention cache without FP8 hardware

# Conflicts:
#	engine/cmake/strata_poly.cmake
#	engine/src/poly/inkling2.cpp
#	engine/src/poly/inkling2.cu

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
okada-shogo added a commit to okada-shogo/PolyStrata that referenced this pull request Nov 23, 2026
simplexai-labs#209 sends a Turing prompt batch to ink_flash75_kernel unless the caches
are FP8; simplexai-labs#214 adds Q8_0 caches, which that kernel would read as halves
(poly_inkling2_kvq8_test on an RTX 2070: ink_attend_batch rel rms nan).
With --kv q8_0 on Turing the batch goes to attend_batch() again, simplexai-labs#214's
checked path there.  Only the combination of the two changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
okada-shogo added a commit to okada-shogo/PolyStrata that referenced this pull request Nov 23, 2026
simplexai-labs#229's ink_fast_kernel loads fp16 and FP8 rows; simplexai-labs#214's Q8_0 rows would be
read as halves there.  With --kv q8_0 and STRATA_FAST_ATTN=1 a full layer
stays on ink_flash_kernel, which decodes them.  Only the combination of
the two opt-in changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant