Skip to content

[Engine] Inkling 2: cached IQ3_S / IQ3_XXS experts with all lanes busy (CUDA) - #223

Open
okada-shogo wants to merge 3 commits into
simplexai-labs:mainfrom
okada-shogo:port/cuda-iq3-lanes
Open

okada-shogo wants to merge 3 commits into
simplexai-labs:mainfrom
okada-shogo:port/cuda-iq3-lanes

Conversation

@okada-shogo

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

Copy link
Copy Markdown

Problem and result

For NVIDIA cards that hold most or all experts in VRAM, with an IQ3_S or IQ3_XXS file (for example merbanan's
AD-IQ3_S file, whose expert gate|up matrices are IQ3_S). It makes the decode kernel for cached experts (experts kept
in VRAM) faster. Same bits.

Decode +6.5% after 302 tokens and +6.1% after 8K on a card that holds every AD-IQ3_S expert; prompt unchanged.

On by default for Inkling 2 on CUDA; STRATA_IQ3_LANES=0 keeps the old kernel.

What it changes, in plain words: these experts go through mmvq_slots_kernel, where one warp (32 GPU threads,
called lanes) takes one row. That wastes time in three ways:

  • A 2304-value row is 72 calls of 32 values, one call per lane. So the third pass over the lanes keeps only 8 of 32
    lanes busy.
  • The grid lookups (IQ3's code table) go to global memory.
  • The signs take several instructions each on Turing.

With this PR a warp takes 4 rows as one run of calls (4 x 72 calls = 9 full passes), the grid sits in shared memory,
and the signs are made with a multiply and xor + add. The bits stay the same: each row's partial sums go back to the
lane that held them before, then the same warp_sum runs.

Speed in tokens per second (tok/s), higher is better, mean of 3 interleaved runs. RTX 2080 Ti 22 GB (sm_75) + EPYC 7K62, AD-IQ3_S, fp16 KV, default settings (all 3072
experts in VRAM in every run), one --serve request per run. main d1f711e vs this PR 0211ccf.

Prompt tok/s, higher is better:

Card Model Measure main (tok/s) this PR alone (tok/s) Change
RTX 2080 Ti 22 GB AD-IQ3_S prompt of 302 tokens 286.9 285.8 −0.4%
RTX 2080 Ti 22 GB AD-IQ3_S prompt of 2K tokens 623.6 622.6 −0.2%
RTX 2080 Ti 22 GB AD-IQ3_S prompt of 8K tokens 623.9 623.9 0%

Decode tok/s, higher is better:

Card Model Measure main (tok/s) this PR alone (tok/s) Change
RTX 2080 Ti 22 GB AD-IQ3_S decode after a 302-token prompt 51.5 54.9 +6.5%
RTX 2080 Ti 22 GB AD-IQ3_S decode after an 8K-token prompt 47.9 50.8 +6.1%
Each run (tok/s)

Prompt tok/s, higher is better, each run:

Card Model Measure Build Run 1 (tok/s) Run 2 (tok/s) Run 3 (tok/s)
RTX 2080 Ti 22 GB AD-IQ3_S prompt of 302 tokens main 286.8 287.6 286.3
RTX 2080 Ti 22 GB AD-IQ3_S prompt of 302 tokens this PR alone 290.3 286.7 280.5
RTX 2080 Ti 22 GB AD-IQ3_S prompt of 2K tokens main 623.7 623.6 623.5
RTX 2080 Ti 22 GB AD-IQ3_S prompt of 2K tokens this PR alone 621.8 623.5 622.4
RTX 2080 Ti 22 GB AD-IQ3_S prompt of 8K tokens main 623.6 623.9 624.1
RTX 2080 Ti 22 GB AD-IQ3_S prompt of 8K tokens this PR alone 623.6 624.2 624.0

Decode tok/s, higher is better, each run:

Card Model Measure Build Run 1 (tok/s) Run 2 (tok/s) Run 3 (tok/s)
RTX 2080 Ti 22 GB AD-IQ3_S decode after a 302-token prompt main 51.7 51.4 51.4
RTX 2080 Ti 22 GB AD-IQ3_S decode after a 302-token prompt this PR alone 55.0 54.8 54.8
RTX 2080 Ti 22 GB AD-IQ3_S decode after an 8K-token prompt main 47.9 47.9 47.8
RTX 2080 Ti 22 GB AD-IQ3_S decode after an 8K-token prompt this PR alone 50.8 50.7 50.8

On an 8 GB card the change is within noise (decode 21.1-21.5 tok/s both ways, AD-IQ3_S with 35% of expert calls
from VRAM), because most of a token's time there is CPU experts; see Validation.

Kernel time, in microseconds (µs), lower is better. What it times: one call of the cached-expert kernel for 8 experts' gate|up products (2304 inputs -> 2304 outputs each), poly_iq3_lanes_test --bench, Quadro RTX 4000 8 GB (sm_75):

Card Expert format old kernel, main (µs) this PR alone (µs) Change
Quadro RTX 4000 8 GB (sm_75) IQ3_S 188.8 µs 141.9 µs −24.8%
Quadro RTX 4000 8 GB (sm_75) IQ3_XXS 165.2 µs 136.5 µs −17.4%

The same calls as weight bytes read per second, in GB/s, higher is better:

Card Expert format old kernel, main (GB/s) this PR alone (GB/s) Change
Quadro RTX 4000 8 GB (sm_75) IQ3_S 96.6 128.6 +33.1%
Quadro RTX 4000 8 GB (sm_75) IQ3_XXS 98.4 119.1 +21.0%

Related discussion

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

Independent of the other split PRs (based on main, not stacked).

Approach

  • Scope: Inkling 2 only (Engine::lane_slots_, set in Inkling2::load_geometry()), CUDA only. HIP, Metal, other
    formats and other model families are unchanged.
  • Off switch: STRATA_IQ3_LANES=0 keeps the old kernel. It is registered in terminal/data/settings-register.json
    as a rollback switch (one entry added by hand, docs/SETTINGS.md regenerated).
  • Why the bits stay the same: lane p takes call 32 i + p of the warp's run of 4 C calls, so row r's calls
    c = L (mod 32) all land on lane (L + r C) mod 32, in increasing c: that lane's sum is the old lane L's sum, term for
    term. One __shfl_sync moves it back, then the same warp_sum.
  • Signs: (nibble * 0x00204081) & 0x01010101 puts bit k in byte k; (g ^ (ones * 0xFF)) + ones gives the same bytes as
    __vsub4(g ^ m, m) (no grid byte is 0, so no carry). The loop is not unrolled (unrolling let nvcc contract the sum).
  • New test poly_iq3_lanes_test (CUDA, random blocks, no model): 24 byte compares against native_mmvq(), the picks
    launch and the launch without lanes; --bench times 8 experts. It skips (77) under STRATA_IQ3_LANES=0, where it
    would compare the old kernel with itself.

Validation

Passed:

  • End-to-end speed, the table above: RTX 2080 Ti 22 GB (sm_75, vast.ai box, EPYC 7K62), e2e-ab.sh (main and this PR
    interleaved, order reversed every other round, one warm-up; strata-poly --serve --prompt-cache 0 --conversation-cache-mib 0 --kv fp16 --max-context 32768, 302 + 256, 2,048 + 1 and 8,192 + 256 tokens; both builds
    CUDA 12.9, 75;86, at d1f711e and 0211ccf).
  • CUDA 12.9 build, sm_75: strata-poly and poly_iq3_lanes_test build at the first commit; poly_iq3_lanes_test again at the head.
  • python3 tools/settings_register.py --check: exit 0; python3 tools/test_settings_register.py: OK; python3 tools/check --only docs: 6 of 6 (it failed before the register commit: STRATA_IQ3_LANES was not registered).
  • Independent review (no GPU): the old and new kernels' sm_75 SASS both contract d * sumi + acc into one FFMA a row, so the float steps match.
  • Quadro RTX 4000 (sm_75, 8 GB, TU104), CUDA 12.9 build of 6e32ef1 (the head adds only the register entry and the
    test's skip under STRATA_IQ3_LANES=0):
    • poly_iq3_lanes_test: 24 of 24 byte compares, 0 values differ (ok: 0 failures). --bench: the table above.
    • AD-IQ3_S end to end, split frozen (STRATA_FREEZE_EXPERTS=1 --kv-whole --cpu-share 0 --expert-swaps 0), 302-token
      prompt, 64 greedy tokens, --top-probs 20, fp16 cache, 32K, 1,110 of 3,072 experts in VRAM in every run, order
      off / on / off / on: the ids and all 64 top lines are identical in all four runs.
Each run on the 8 GB card (tok/s)

Quadro RTX 4000 8 GB, AD-IQ3_S, expert placement fixed at load (1,110 of 3,072 experts in VRAM), the old kernel (STRATA_IQ3_LANES=0) against the new one in the same build. Prompt tok/s, higher is better, each run:

Card Model Measure Build Run 1 (tok/s) Run 2 (tok/s)
Quadro RTX 4000 8 GB AD-IQ3_S prompt of 302 tokens, placement fixed at load this PR, STRATA_IQ3_LANES=0 (old kernel) 148.0 148.6
Quadro RTX 4000 8 GB AD-IQ3_S prompt of 302 tokens, placement fixed at load this PR alone 148.5 147.7

Decode tok/s, higher is better, each run:

Card Model Measure Build Run 1 (tok/s) Run 2 (tok/s)
Quadro RTX 4000 8 GB AD-IQ3_S decode after a 302-token prompt, placement fixed at load this PR, STRATA_IQ3_LANES=0 (old kernel) 21.54 21.37
Quadro RTX 4000 8 GB AD-IQ3_S decode after a 302-token prompt, placement fixed at load this PR alone 21.12 21.43

Order: off, on, off, on.

Failed: none observed.

Not run:

  • Other sm targets than sm_75 for the bit check.
  • HIP / Metal builds: not run. The new code is under #if !defined(__HIPCC__), but iq_kernels.cu is also compiled for HIP, so a HIP build is still owed.

Limits

  • Only IQ3_S and IQ3_XXS cached experts on CUDA.
  • Unrolling or 2 / 8 rows a warp were within 2% in our earlier engine; not re-measured here.

Credits: the shared-memory grid and the multiply for the signs are merbanan's ideas (his inkling2-turing decode kernels in Strata). The code is our own.

🤖 Generated with Claude Code

okada-shogo and others added 3 commits November 23, 2026 04:31
The cached experts' slots launches of IQ3_S and IQ3_XXS (merbanan's
AD-IQ3_S file has IQ3_S gate|up) run mmvq_slots_kernel, a row a warp.
A 2304-value row is 72 calls, so the third pass keeps 8 of 32 lanes
busy; its grid lookups go to global memory, and its sign bytes come
from __vcmpne4 / __vsub4, several instructions each on Turing.

With MmvqSlots::lanes / MmvqIds::lanes (set from Engine::lane_slots_,
which only Inkling2 turns on; CUDA only) a warp takes 4 rows as one
run of 4 x 72 calls, lane p taking call 32 i + p.  A row's calls
c = L (mod 32) all land on lane (L + r C) mod 32 in increasing c, so
that lane holds the old kernel's lane-L sum term for term; a shuffle
moves it back to lane L before the same warp_sum, and a row's sum has
the old kernel's bits.  The grid is read from shared memory, and the
sign bytes come from a multiply (nibble bit k to byte k) and xor + add:
the same integers (no grid byte is 0, so no byte carries).  The
shared-memory grid and the multiply for the signs are merbanan's ideas
(his inkling2-turing decode kernels in Strata); the code is our own.
STRATA_IQ3_LANES=0 (or STRATA_OLD_IQ_MMVQ=1) keeps mmvq_slots_kernel.
Other formats, other families, HIP and Metal are unchanged.

Class: same bits.  poly_iq3_lanes_test (new, CUDA, random blocks):
24 byte compares (IQ3_S and IQ3_XXS, 1-8 experts, short last rows,
n_in 256-2304) of the lanes launch against native_mmvq, the picks
launch and the launch without lanes; --bench times the 8-expert
gate|up launch with and without lanes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tools/check's docs step and tools/test_settings_register.py failed:
the engine reads STRATA_IQ3_LANES (=0 keeps mmvq_slots_kernel for
Inkling 2's IQ3_S / IQ3_XXS cached experts) and it was not in
terminal/data/settings-register.json.

Add its entry as python tools/settings_register.py writes it, with
status rollback (on by default, =0 goes back), owner kernels/cuda and a
note, and leave every other entry as it was (as fd20bfc did).
docs/SETTINGS.md is regenerated with --docs; it also gains the three
rollback switches fd20bfc registered on main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With the lane kernel turned off, every case compared mmvq_slots_kernel
with itself and passed without testing anything.  Return 77 (skip) and
say why instead.

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
…experts with all lanes busy (CUDA)

# Conflicts:
#	docs/SETTINGS.md

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