Skip to content

perf(exl3 cuda): EXL3 prompts and multi-stream verify for the 27B on CUDA, same bits per commit - #260

Closed
BHCC2025 wants to merge 9 commits into
ashhart:mainfrom
BHCC2025:exl3-cuda-speed
Closed

BHCC2025 wants to merge 9 commits into
ashhart:mainfrom
BHCC2025:exl3-cuda-speed

Conversation

@BHCC2025

@BHCC2025 BHCC2025 commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On a DGX Spark the 27B's EXL3 packs decode well for one user but were slow at prompts and with several users.
Prompts ran at about two thirds of the MLX 4-bit checkpoint's speed: every prompt chunk decoded every weight again,
rotated its input in a separate pass and ran a Hadamard epilogue. With several users, DFlash2 verify windows of
32-128 rows went through linear_kernel, which decodes every trellis tile again for each 16 rows. A short prompt
also paid a whole-model decode.

This series fixes both without changing what the model computes beyond rounding. Every commit is
row-invariant, so drafted == serial, concurrent == solo and resumed == fresh hold as before. Four of the seven
commits leave every output bit unchanged. The other three change the prompt path's rounding: W'' is held in bf16,
and the raw bf16 rows go into the GEMM. KL to BF16 is unchanged within noise (0.01806 against 0.01841 on 49K
held-out tokens, on 0.6.2; MLX 4-bit is at 0.0568).

The commits stand alone, so you can take any subset. Each one builds, has its own tests and an env switch that
turns it off. The only dependency between them is that 2 → 3 → 7 build on each other (3 and 7 need the folded path
of 2). 1, 4, 5 and 6 are independent of the rest, and 6 needs 5's dispatch argument.

Commits

Speeds are on one DGX Spark (GB10). The model is turboderp/Qwen3.8-27B-exl3 at 4.00bpw with z-lab's DFlash2. The
numbers for each commit come from the session where it was measured, mostly on 0.6.0 before the port. The results
table below is one session on 0.6.2.

# Commit Bits Effect, as measured
1 perf(exl3 cuda): the prompt GEMM steps K by 64 same cold prompts +4.0..+5.5% at 8K-126K
2 perf(exl3 cuda): prompts on W'' with both rotations folded in, for families that opt in bf16 W'' GEMM on a decoded W'' 66.7 ms vs 95.0 (layers 2+3, 4,096 rows; MLX 4-bit 71.6). unpack_fold2 decodes the whole model's W'' in 315 ms, at GB10's DRAM rate
3 perf(27b cuda): an EXL3 prompt of several chunks runs layer by layer same each layer's W'' decoded once a prompt, one layer held. Cold 8K / 32K / 64K / 126K: 1,309 / 1,323 / 1,199 / 1,004 tok/s (MLX 4-bit 1,430 / 1,310 / 1,170 / 979)
4 perf(27b cuda): a short tail after the last message start fills in the same call same one prefill call a prompt instead of two. 1K 682 → 896 tok/s, 8K 1,309 → 1,424
5 perf(exl3 cuda): verify windows of 17-128 rows decode each tile once per 16 or 32 rows same 4 users +28%, 8 users +37.5% aggregate (code greedy)
6 perf(exl3 cuda): linear_wc for 17-128-row verify windows of 4- and 6-bit mul1 layers same one 128-row verify round 213 → 145 ms (MLX 4-bit 140). 8 streams steady 279 → 351 tok/s
7 perf(exl3 cuda): 4-bit prompt calls of up to 48 rows rebuild W'' inside the GEMM off by default (see 8) when on: first token of a 41-token prompt 0.53 → 0.36 s
8 fix(exl3 cuda): fdirect off by default (TENSORFOLD_EXL3_FDIRECT_ROWS=0) same see below

What each one changes:

  1. BK 64. tiles() steps K by 64. tl.dot runs the same k16 chain, so the bits are unchanged.
  2. Folded prompt weights. unpack_fold2 decodes the trellis and applies both 128-point Hadamards and suh in
    one kernel, rounding once to bf16. _gemm_fold (fixed tiles by (K, N), fp32 accumulation, K in order) multiplies
    the raw bf16 rows by that. There is no rot_in and no epilogue. unpack_fold is the plain reference kernel;
    unpack_fold2 reproduces its bits. Only a family that passes Workspace(fold=True) takes this path, which here
    is the 27B's EXL3 loader. Flash Next's EXL3 projections keep the W_q path, since we haven't measured them.
  3. Layer-major prefill. prefill_rows' forward becomes _Rows, which runs one layer at a time. Your batched
    fill and the layer-major prefill share one per-layer body. A multi-chunk EXL3 prompt on one GPU runs every chunk
    through layer l before layer l + 1, and a scope in cuda/exl3/prefill.py holds each W'' only while its layer
    runs. TP, images, MoE layers and non-EXL3 packs stay chunk by chunk.
  4. Tail merge. When a step ends at the last stop with fewer than MIN_GAP rows left, _step and _steps
    prefill to the end with keep_at at the stop. The stop's cache entry comes from the kept state. Any chunk bounds
    give the same bits, and every pack takes this rule.
  5. Mid-M kernels. linear_mpg_kernel<P> gives each CTA 16 P rows of one column block. CTAs of a block sit side
    by side in the grid, so they read its words from L2 together. It keeps linear_kernel's K ranges, chain and
    fixed-order sums. ext.linear gains a mode argument for the kernel family.
  6. linear_wc. For 4- and 6-bit mul1 layers, this follows the tiling of MLX's qmm. Each warp decodes its tile
    once per CTA into mma fragments, through a 4-stage cp.async ring. Split layers with 32+ column blocks add their
    split sums in registers in split order, with no Z buffer. Other codebooks and widths keep 5.
  7. fdirect. 4-bit calls of up to 48 rows rebuild each W'' block exactly as unpack_fold2 does and feed it
    straight into one bf16 mma chain over K in k16 order, which is the chain tl.dot runs. The output is
    unpack_fold2 + _gemm_fold bit for bit on most shapes, but not all: on 16 rows of a 4-bit 5120x6144 layer,
    and on layers with a bias, it rounds differently from the GEMM. So commit 8 turns it off by default, and
    every prompt row keeps one arithmetic at any row count. TENSORFOLD_EXL3_FDIRECT_ROWS=48 turns it back on.
    Its test marks the bias cases xfail until fdirect matches the GEMM's epilogue. We're happy to drop commit 7
    instead if you'd rather not carry it until then.

Not included:

  • The GDN gate GEMM is already on 0.6.2 (b16.prompt/prompt_pair).
  • A whole-model W'' cache. It was faster at 1K but kept 47.7 GiB.
  • A fused decode-in-GEMM kernel. It was correct but slower than decode + GEMM on GB10.
  • Short-request routing. Prompts of up to 512 rows a message on the direct path, exact under resumed == fresh,
    but it moves the server's cache entry to the last message start, so it is a follow-up if you want it.

Results: one session on 0.6.2

These runs had fdirect (commit 7) on. With commit 8's default (off), a lone short prompt's first token
goes back to about 0.5 s, as on stock; the cold-prompt and 8-user figures are not affected by it.

EXL3 4.0 bpw on TensorFold 0.6.2: stock vs these changes vs MLX 4-bit

Same Spark, same session: stock 0.6.2 with the MLX 4-bit checkpoint (Vontra/Qwen3.8-27B-MLX-4bit), stock 0.6.2
with the EXL3 4.00bpw pack, and this branch with the same pack. DFlash2 on.

Cold prompts (--parallel 4 --context 131072), tok/s:

prompt MLX 4-bit, 0.6.2 EXL3, 0.6.2 EXL3, these changes
8K 1,435 992 1,356 (+37%)
32K 1,315 968 1,287 (+33%)
64K 1,168 899 1,163 (+29%)
126K 975 783 975 (+25%)

Your tools/bench_concurrent.py --levels 1,2,4,8 --alone --serial --reps 3 (--parallel 8 --context 32768),
aggregate tok/s at 1 / 2 / 4 / 8 users:

cell MLX 4-bit, 0.6.2 EXL3, 0.6.2 EXL3, these changes
code greedy 69 / 124 / 207 / 319 76 / 114 / 159 / 175 74 / 145 / 242 / 347
code sampled, mean of 3 seeds 81 / 132 / 210 / 309 91 / 126 / 148 / 166 90 / 152 / 236 / 335
chat greedy 51 / 92 / 152 / 230 41 / 71 / 109 / 136 45 / 87 / 141 / 229
chat sampled, mean of 3 seeds 52 / 88 / 140 / 216 44 / 74 / 112 / 128 45 / 87 / 137 / 217
TTFT, 1 user / 8-user max (s) 0.11 / 0.16 0.50 / 0.56 0.23 / 0.30

0 unequal replies (alone and serial) in every cell for all three. Against stock EXL3 the branch is 2.0x at 8 users
on code and +69% on chat. Against MLX 4-bit it is level or ahead from 2 users up; one user on chat is behind
(45 vs 51 greedy), and a lone short prompt's first token takes 0.23 s against 0.11. A second Spark, same settings,
gave the branch 365 / 229 at 8 users (code / chat greedy) and 1,370 / 1,300 / 981 tok/s cold at 8K / 32K / 126K.

For reference, the same kernels on 0.6.0 (one session, same box) gave the following. Cold prompts ran +0.7% /
+3.9% / +4.2% / +3.8% against MLX 4-bit at 8K / 32K / 64K / 126K. With --parallel 8 the aggregate ran +2..+36%
in every multi-user cell. The one-user sampled-chat cell was -11%, which is seed variance: over 16 seeds it is
45.9 against 46.7 tok/s. A lone short prompt's first token took 0.23 s against MLX 4-bit's 0.11 s.

Exactness

  • Drafted == serial: test_checkpoint_drafted_equals_serial on the real pack (greedy and two seeds) passes on the branch tip and at every commit.

  • Concurrent == solo: tools/bench_concurrent.py --alone --serial gave 0 unequal replies in every cell, for every commit's build, for the full branch's kernels on 0.6.0, and in the 0.6.2 session above.

  • Resumed == fresh: every prompt row's bits depend only on its tokens, at any chunking, step or batch. The tests
    in each commit check this on the kernels (rows alone, in windows, at offsets, across the fdirect and mid-M
    switches) and on the prompt path. For the prompt path that covers layer-major against chunk by chunk, kept states,
    drafter taps, and prefill_batch pieces against each prompt alone. Your test_prompts_ignore_chunking_and_resume_as_fresh
    and the prompt-end-cache tests pass as before. Greedy replies to the 1K-64K prompts are equal across the
    bit-identical commits.

  • Quality: KL(BF16 || EXL3) over 49,152 held-out tokens (96 windows of 512; prose, code and chat), on
    TensorFold's own prompt path:

    build KL vs BF16 top-1 ppl
    EXL3, stock 0.6.2 0.01841 96.74% 6.369
    this branch 0.01806 96.76% 6.372
    MLX 4-bit, stock 0.6.2 0.05681 92.31% 6.240

    The branch's KL to stock 0.6.2's own EXL3 output is 0.00091 (top-1 agreement 99.49%), about 1/20 of the
    quantization's own drift, in both directions around BF16. Stock EXL3 gives the same logits on 0.6.0 and 0.6.2.

Memory

The layer-major path holds one layer's W'' (about 0.75 GiB in bf16 for the 27B) instead of the whole model's (47.7
GiB). Host memory available while serving, from the minimum over the run:

setting MLX 4-bit EXL3, stock EXL3, this branch
--parallel 4 --context 131072, after the long prompts 71.9 GiB 70.5 66.3
--parallel 8 --context 32768, during the bench 95.6 GiB 95.1 95.2

One gap we left for you to decide. Admission (exl3_workspace) still counts one decoded matrix. During a
layer-major prompt the scope holds a whole layer's W'' (about 0.45 GiB more on the 27B), and a long prompt's
activations stay alive across chunks. We can add both to the estimate if you prefer.

Method

  • Hardware: one DGX Spark (GB10, 128 GB unified), NVIDIA's pytorch:26.07-py3-based container, torch 2.13,
    CUDA 13.3.
  • Model: turboderp/Qwen3.8-27B-exl3 at 4.00bpw with z-lab/Qwen3.8-27B-DFlash2. The MLX 4-bit reference was
    Vontra/Qwen3.8-27B-MLX-4bit on the same engine.
  • Serving: tensorfold serve with --parallel 4 --context 131072 for prompts and --parallel 8 --context 32768 for concurrency. Cold prompts were 8K-126K tokens of public text, a fresh server per build, and
    servers ran one at a time on an otherwise idle GPU.
  • Concurrency: your tools/bench_concurrent.py --levels 1,2,4,8 --alone --serial --reps 3 (seed 1234). The
    sampled cells were also run at seeds 2234 and 3234.
  • Kernels: microbenchmarks used CUDA graphs over L2-busting copies and the loader's PLANS splits. Exactness
    used torch.equal.

Env switches

All are read at import, and each restores the previous behaviour of its commit:

switch default off means
TENSORFOLD_EXL3_FOLD 1 every family on the W_q prompt path (commit 2)
TENSORFOLD_EXL3_FOLD_BF16 1 W'' in fp16 and fp16 rows in the GEMM (also disables fdirect)
TENSORFOLD_EXL3_FOLD2 1 unpack_fold, the plain kernel (same bits)
TENSORFOLD_PREFILL_LAYER_MAJOR 1 chunk by chunk (commit 3)
TENSORFOLD_PREFILL_TAIL_MERGE 1 two prefill calls (commit 4)
TENSORFOLD_EXL3_MIDM 1 linear_kernel at every row count (commits 5 and 6)
TENSORFOLD_EXL3_WC 1 the mid-M kernels for 17-128 rows (commit 6)
TENSORFOLD_EXL3_FDIRECT_ROWS 0 48: fdirect on for calls of up to 48 rows (commit 7; see commit 8)

We're glad to drop any of these switches, rename them, or fold them into your own settings.

Checks

  • New tests:
    • tests/cuda/test_exl3_prefill.py: BK 32 == BK 64; fold2 == fold; float64 references; folded rows alone, in
      windows and at offsets; opt-in; fdirect == fold2 + GEMM (bias cases xfail; fdirect is off by default).
    • tests/cuda/test_qwen27_exl3_prefill.py: layer-major == chunk by chunk, with a drafter, no W'' held after a
      prompt, and prefill_batch == each prompt alone.
    • tests/cuda/test_exl3_linear.py: every kernel mode gives linear_kernel's bits at 1..128 rows.
    • tests/cuda/test_qwen27_multi.py: tail merge.
  • CUDA tests on the branch with the real checkpoint and drafter (test_exl3_linear.py, test_exl3_checkpoint.py,
    test_exl3_experts.py, test_qwen27_exl3.py, test_glm_exl3.py, test_qwen4_exp_exl3.py plus the new files):
    163 passed / 60 skipped. Stock 0.6.2 gives 147 / 60 (the 16 more are this branch's new tests). The skips need
    exllamav3 installed.
  • Host tests (no GPU, Linux container): the same outcome as main test by test (3,118 passed; the same 133 failures
    and 13 errors on main in that container, which has no MLX).
  • ruff check on the touched files: no new findings against main.
  • Every commit's CUDA sources compile for sm_121 on their own.

Happy to rework any of it, split it into separate PRs, or port the folded path to Flash Next's EXL3 projections
if you want that measured too.

tiles() takes BK 64 in place of 32 for every shape. tl.dot runs the same
mma.m16n8k16 chain over K in k16 order either way, so every output bit is
unchanged (a sweep of the 27B's 13 projection shapes found no differing
bit); the loads are wider and the loop half as long.

Measured on a DGX Spark (GB10), turboderp/Qwen3.8-27B-exl3 4.00bpw through
tensorfold serve, cold prompts: 8K 981 -> 1,034 tok/s (+5.4%), 32K 954 ->
1,005, 64K 885 -> 923, 126K 773 -> 804 (+4.0%). Greedy replies are
hash-identical to before at 1K-64K.

Tests: tests/cuda/test_exl3_prefill.py checks that K steps of 32 and 64
give the same bits on four codebook/width/shape cases at 300 rows.
…milies that opt in

A family that passes Workspace(fold=True) (the 27B's EXL3 loader does) runs its
bf16 prompt matmuls on W'' = diag(suh) H W_q H / 128: unpack_fold2 decodes the
trellis and applies both 128-point Hadamards and suh in one kernel, rounding
once to bf16, and _gemm_fold (a fixed-tile bf16 Triton GEMM, fp32 accumulation,
K in order) multiplies the raw bf16 rows by it, then svh and the bias. No rot_in
pass and no Hadamard epilogue run. Tiles depend on (K, N) alone (FOLD_TILES,
swept on a GB10 for the 27B's shapes), so a row's bits never depend on its
chunk and resumed prompts still equal fresh ones. Other families (Flash Next's
EXL3 projections) keep the W_q path unchanged.

unpack_fold is the straightforward kernel (fp32 shared tile, fwht128 along rows
then columns); unpack_fold2 gives the same bits faster: the N side runs in
registers on the decoded fragments, the K side through shared memory in two
64-column halves with XOR swizzles, two blocks an SM. On the 27B's 409 groups it
is bit-identical in bf16 and fp16 and decodes the whole model in 315 ms against
552 (207 GB/s read+write, GB10's DRAM floor).

Measured on a DGX Spark (GB10), 27B 4.00bpw, layers 2+3 at 4,096 rows: the GEMM
on a decoded W'' takes 66.7 ms against 95.0 for the W_q path (BK 64), where
MLX 4-bit's prompt matmul takes 71.6. The decode itself is paid once a call
here; the next commit holds a layer's W'' for a whole prompt.

Accuracy: bf16 weights round more coarsely than fp16 (2.28e-3 mean relative
error per matmul against float64, 1.44e-3 before), which does not show end to
end: KL(BF16 || EXL3) over 49,152 held-out tokens 0.01838 against 0.01841
before, top-1 96.77% against 96.74%.

Switches: TENSORFOLD_EXL3_FOLD=0 (every family on the W_q path),
TENSORFOLD_EXL3_FOLD_BF16=0 (fp16 W'' and fp16 rows), TENSORFOLD_EXL3_FOLD2=0
(unpack_fold). docs/recipes/exl3.md describes both paths.

Tests: tests/cuda/test_exl3_prefill.py: unpack_fold2 == unpack_fold on seven
codebook/width cases in bf16 and fp16; W'' and the folded matmul against
float64 references; rows on the folded path bit-identical alone, in windows of
1-300 rows and at an offset, on four shapes; a plain Workspace keeps the W_q
path's bits, and TENSORFOLD_EXL3_FOLD=0 restores them.
…(same bits)

On one GPU, an EXL3 pack's prompt of two or more chunks (dense layers, no image)
now runs every chunk through layer l before layer l + 1. The folded prompt path
decodes each projection's W'' once a prompt instead of once a chunk and holds
it only while its layer runs (cuda/exl3/prefill.py scope_begin / release /
end), so a 64K prompt decodes the weights once, not 16 times, without keeping
the whole model's W'' (47.7 GiB in bf16 for the 27B).

prefill_rows' forward becomes _Rows, set up once and run a layer at a time, so
prefill_rows (one forward over several streams' rows, upstream's batched fill)
and the layer-major prefill share one per-layer body; each chunk is one _Rows of
one item and makes the same calls on the same rows as chunk by chunk, so every
state, kept state, last row and drafter tap has the same bits. A kept point at
a chunk start moves that chunk's bound one row on (any bounds give the same
bits). TP, image prompts, MoE layers and non-EXL3 packs stay chunk by chunk.

Measured on a DGX Spark (GB10), 27B 4.00bpw through tensorfold serve
(--parallel 4 --context 131072, DFlash2), cold prompt tok/s at 1K / 8K / 32K /
64K / 126K, with the previous commit and without the next one's tail merge:
682 / 1,309 / 1,323 / 1,199 / 1,004, against MLX 4-bit on the same engine in
the same session 1,219 / 1,430 / 1,310 / 1,170 / 979 (0.6.0's EXL3 path, an
earlier session: - / 1,003 / 950 / 890 / 773). Host memory available while
serving: 65.4 GiB against 71.9 for MLX 4-bit. Short prompts are behind: one
chunk still pays a whole-model decode (0.21 s at GB10's DRAM rate); the next
commits address that.

TENSORFOLD_PREFILL_LAYER_MAJOR=0 runs chunk by chunk.

Tests: tests/cuda/test_qwen27_exl3_prefill.py on a synthetic GDN + attention
EXL3 model: layer-major == chunk by chunk (last row, every layer's conv,
recurrent and KV state, the kept state) from a fresh and a resumed start,
kept points inside a chunk, at chunk starts and at the end; with 64 layers a
recording drafter gets the same taps, skips and snapshots; no W'' is held
after the prompt; prefill_batch pieces equal each prompt alone.
…e same call (same bits)

The server filled a one-message prompt in two calls: to its last stop (the
assistant start, usually 3-5 rows before the end) and then the tail. On an EXL3
pack each call decodes every weight, so the tail paid a whole-model decode for a
few rows. When the step ends at the last stop and fewer than MIN_GAP rows are
left, _step and _steps now prefill to the end with keep_at at the stop (a cut
chunk), and the stop's cache entry is the state kept there. Any chunk bounds
give the same bits, so the final state, the kept state, the drafter's snapshot
and the first token equal the two-call ones; every pack takes the same rule.

Measured on a DGX Spark (GB10), 27B EXL3 4.00bpw through tensorfold serve
(--parallel 4 --context 131072, DFlash2), cold prompt tok/s at 1K / 8K / 32K /
64K / 126K, same session: 682 / 1,309 / 1,323 / 1,199 / 1,004 without this,
896 / 1,424 / 1,350 / 1,212 / 1,011 with it (MLX 4-bit 1,219 / 1,430 / 1,310 /
1,170 / 979). Greedy replies equal at 1K-64K and tools/bench_concurrent.py found
0 unequal replies (alone and serial) in both builds.

TENSORFOLD_PREFILL_TAIL_MERGE=0 fills in two calls.

Tests: tests/cuda/test_qwen27_multi.py: a 3-row tail with MIN_GAP 4 fills in
one call (two with the switch off), keeps the shared prefix, and both later
prompts resume from it to their serial tokens.
…per 16 or 32 rows (same bits)

linear_kernel runs 16 rows a pass and reads and decodes every trellis word
again each pass; a big layer's words (the 27B's gate: 44 MB) exceed L2, so a
128-row call costs eight DRAM sweeps. linear_mpg_kernel<P> gives each CTA one
16 P-row group of one column block (P = 1 for 17-48 rows, 2 for 49-128), decodes
each k step's tiles once for its P passes, and puts the CTAs of a block side by
side in the grid so they read its words from L2 together. The warps keep
linear_kernel's K ranges, mma chain from zero, warp-order reduction and split
fold, so every output bit equals linear_kernel's at any row count; fp16
operands are unchanged.

Measured on a DGX Spark (GB10), 27B 4.00bpw, layers 2+3 under CUDA graphs with
L2-busting copies, EXL3 time over MLX 4-bit's: 32 rows 1.50x -> 0.96x, 64 rows
2.44x -> 1.20x, 128 rows 3.04x -> 1.48x; 1-16 rows unchanged (0.92-0.96x).
Through tensorfold serve (--parallel 8 --context 32768, DFlash2,
tools/bench_concurrent.py --reps 3), aggregate tok/s: code greedy 4 users
122.6 -> 157.1 (+28%), 8 users 129.4 -> 177.9 (+37.5%); chat greedy 8 users
108.0 -> 139.2 (+28.9%); single-user cells within noise. 0 unequal replies
(alone and serial) and greedy replies hash-identical before and after.

TENSORFOLD_EXL3_MIDM=0 keeps linear_kernel at every row count. The new mode
argument of ext.linear names the kernel family so the next commit can add one.

Tests: tests/cuda/test_exl3_linear.py: at 1..128 rows the mid-M kernels give
linear_kernel's bits on the 27B's projections with the loader's splits and
four other shapes, mul1 and 3inst; a row alone equals it inside 128.
…bit mul1 layers (same bits)

linear_wc.cuh follows the tiling of MLX's 4-bit qmm: a CTA holds one 128-column
block, one K split and 32 or 64 rows; warp w owns column tile w for every row
and decodes each of its trellis tiles once a CTA straight into mma B fragments;
the words and the fp16 rows stream through a 4-stage cp.async ring (A by
ldmatrix from XOR-swizzled rows), with branch-free stages since the stage size
divides the K range. Layers split over K with 32+ column blocks (the 27B's
in_proj_qkv / in_proj_z at SK 5, out_proj / o_proj at SK 16) run every split
in one CTA and add the split sums in registers, so no Z buffer goes through L2.
Per row the arithmetic is linear_kernel's: the same K ranges, the same mma
chain from zero, range sums in range order and split sums in split order, so
every output bit is the same at any row count. Rows: 17-32 one 32-row group,
33-40 the mid-M kernels (4-bit, unsplit), 41-64 one 64-row group, 65-96 three
32-row groups, 97-128 two 64-row groups. Other codebooks and widths keep the
mid-M kernels.

Measured on a DGX Spark (GB10), 27B 4.00bpw, one verify round
(multi_tree_forward with DFlash2 taps and full logits), ms with the mid-M
kernels -> with linear_wc (MLX 4-bit): 32 rows 84.6 -> 75.1 (80.0), 64 rows
115.3 -> 86.8 (93.1), 128 rows 213.0 -> 144.7 (139.6). At 128 rows out_proj
took 470 us a call against 204 for MLX 4-bit, mostly the 42 MB of split
partials through L2; that is gone. Through tensorfold serve (--parallel 8
--context 32768, DFlash2), steady tok/s with 8 streams decoding, code greedy /
code sampled / chat greedy / chat sampled: 279 / 283 / 187 / 178 -> 351 / 357
/ 236 / 222 (MLX 4-bit 278 / 331 / 214 / 212). 0 unequal replies in
tools/bench_concurrent.py, greedy replies equal before and after at 1K-64K,
and KL to BF16 unchanged (same logits hash).

TENSORFOLD_EXL3_WC=0 leaves these rows to the mid-M kernels.

Tests: tests/cuda/test_exl3_linear.py's mode test runs linear_wc too
(MODES 0, 6, 7), including a 6-bit split layer of 32 column blocks.
…de the GEMM (same bits)

A short prompt (or a short follow-up) on the folded path paid a whole W''
decode and write per call, about 0.27 s for the 27B before its first token.
fdirect_kernel takes 4-bit calls of up to TENSORFOLD_EXL3_FDIRECT_ROWS (48)
rows when no W'' is held: each program owns one 128-column block and walks K
in 128-row blocks in order, rebuilding each W'' block exactly as unpack_fold2
does (same decode, butterflies, scale and rounding) into shared memory, and
feeds it straight into one mma.m16n8k16 bf16 chain over K in k16 order from
zero, the chain tl.dot runs in _gemm_fold. The output equals unpack_fold2 +
_gemm_fold bit for bit, so the choice by row count keeps rows independent of
their call. Above 128 rows a call runs in 128-row slices.

Measured on a DGX Spark (GB10), 27B 4.00bpw, layers 2+3: 1 row 6.4 ms
(decode + GEMM 16.2, MLX 4-bit 2.7), 16 rows 7.8, 32 rows 9.4, 48 rows 12.1;
from 64 rows (18.5) the decode + GEMM wins. A whole prefill of 1 token 504 ->
212 ms, 17 tokens 257 ms, 48 tokens 371 ms. Through tensorfold serve, a cold
41-token prompt's first token comes in 0.36 s against 0.53 s, and 8 users'
slowest first token in tools/bench_concurrent.py in 0.30 s against 0.58 s.
0 unequal replies, greedy replies unchanged.

Limits: each program walks K in order (the bit-exact chain), so narrow,
long-K projections under-fill the GPU; one program an SM.

TENSORFOLD_EXL3_FDIRECT_ROWS=0 turns it off. fdirect only runs with bf16 W''
(TENSORFOLD_EXL3_FOLD_BF16=1, the default), whose bits it reproduces.

Tests: tests/cuda/test_exl3_prefill.py: fdirect == unpack_fold2 + _gemm_fold
at 1-200 rows on five shapes (mul1, 3inst, mcg; with and without bias); the
folded row-invariance test now crosses the 48/49-row switch.
@BHCC2025
BHCC2025 marked this pull request as ready for review October 2, 2026 17:02
At some shapes fdirect does not reproduce fold2 + _gemm_fold bit for bit (16 rows of a
4-bit 5120x6144 layer, and layers with a bias), so a short call could round differently
from the same rows in a longer call. Off by default every prompt row keeps one arithmetic
at any row count; TENSORFOLD_EXL3_FDIRECT_ROWS=48 turns it back on. The bias cases of its
test are marked xfail until fdirect matches the GEMM epilogue.
@grearjake-star

Copy link
Copy Markdown
Contributor

A receipt for commits 5 and 6 (cba4a0f mid-M, 18e2e82 linear_wc) on Flash Next, which isn't the 27B but runs its dense projections through the same cuda/exl3/linear.cu. On turboderp's EXL3 4.05 pack every dense projection is 6-bit mul1 (the attention indexer's index_qk_proj is 4-bit), so every verify window of 17 rows or more takes linear_wc. The bits stay the same, and aggregate decode gains about 3% at 4 and 8 streams.

Environment.

  • One DGX Spark (GB10, sm_121), driver 580.178.04, PyTorch 2.13.0+cu130, Triton 3.7.1, CUDA 13.0.
  • turboderp/Qwen3.8-Flash-Next-exl3, 4.05bpw_h6_ng6 at revision 55a732e.
  • Our build, not this branch: v0.6.4 plus our local patches, with linear_mpg_kernel from cba4a0f and linear_wc.cuh from 18e2e82 copied verbatim into our linear.cu, behind a switch (on: modes 6/7 for 17-128 rows; off: linear_kernel at every row count). Rows 1-16 always take linear_kernel. We did not port the W'' prompt path (b1d3e73): it changes numerics for our family. Our other patches: a retrained MTP head, FP8 copies of the HC faces, a fixed n-gram pin budget, a measured verify-cost table, shared-round graphs, warm starts, and our fixes for Flash Next (MLX 4-bit + MTP) on a single DGX Spark: v0.6.4 decodes ~24% slower than v0.6.3, with a single stream #317.
  • Server: --parallel 8 --context 262144 --decode-share 0.25. The unpinned part of the n-gram table was dropped from the page cache after each load.

Exactness.

  • Microbench on the pack's own projections (in_proj_qkv / in_proj_z / out_proj, q/k/v/o, index_qk, lm_head), rows 1-128, modes 0 / 6 / 7: every output torch.equal to linear_kernel's.
  • GPU tests: our tall-tile test (rows 17-128, modes 6 and 7, bf16 and fp32 outputs, two offsets, counters back to 0, row-alone invariance, a CUDA-graph capture and replay at 48 rows): 12 passed. This PR's synthetic mode test plus our four pack shapes and a bias variant: 24 passed.
  • Server: at 4 and 8 concurrent streams, each stream equals its solo run, 8/8 in every arm; the solo runs equal the single-stream server, 8/8.

Decode speed. Aggregate tok/s, median, alternated on, off, off, on:

c1 c4 c8
on, arm 1 54.4 92.3 122.6
off, arm 2 57.1 94.2 122.1
off, arm 3 58.5 90.1 117.6
on, arm 4 58.8 97.4 125.3
on vs off (mean of arms) - +2.9% +3.4%
  • c1 doesn't change kernels (a lone stream's window is 1-11 rows) and moves with the arm order. c8 is one group of 8 jobs per arm, so single cells are noisy.
  • EXL3 linear time of one verify round on the pack (sum over the round's projections, L2 flushed, median of 20), mode 0 → mode 7: 16 rows 18.39 → 18.35 ms; 17 rows 21.76 → 17.62 (0.81×); 32 rows 22.52 → 18.01 (0.80×); 48 rows 27.64 → 18.91 (0.68×); 64 rows 32.39 → 19.41 (0.60×); 128 rows 52.36 → 23.04 (0.44×). Mid-M alone (mode 6) gets 58-77% of that saving, depending on the row count.
  • @tournierjc's port on the same pack found the counters equal and +0.7% at 4 clients. Our larger gain at c4 and c8 fits more rounds above 16 rows at --parallel 8.

Prompt speed. Not measured. These kernels take verify windows of 17-128 rows; our prompt passes are 256 rows or more and take the prompt GEMM.

Tests. As above, on GB10: 12 + 24 passed, 0 failed. Our host dispatch test (row ranges, modes, shared-memory and counter bounds per pack shape): 70 passed.

What we did not run.

  • This branch as it is, and stock v0.6.4 plus these two commits without our other patches.
  • bench_openai and prefill_cold (decode-only kernels; see above).
  • The 27B, other packs (3.05, 5.05, 6.05), and the 4-bit linear_wc path beyond index_qk_proj.
  • TENSORFOLD_EXL3_FDIRECT_ROWS and b198413: not ported.
  • Two ranks, Metal.

Measured on our DGX Spark with AI assistance (Claude Code).

@Arminova

Arminova commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

A one-stream receipt on 0.6.5 from one DGX Spark (GB10), with turboderp's 4.00bpw and 5.00bpw packs.

Setup. 0.6.5 (609ca41) with the eight commits cherry-picked. The only conflict was tests/cuda/test_exl3_linear.py, where 0.6.5 and the PR each appended a test; I kept both, with MODES = (0, 6, 7). --parallel 1 --context 131072, z-lab/Qwen3.8-27B-DFlash2, defaults otherwise (fdirect off). Stock 0.6.5 ran twice, alternated, and the branch once.

Tests. test_exl3_linear.py, test_exl3_prefill.py, test_qwen27_exl3_prefill.py, test_qwen27_exl3.py, test_qwen27_multi.py: 152 passed, 0 failed, 3 skipped, 2 xfailed.

4.00 stock 4.00 #260 5.00 stock 5.00 #260
cold prompt at 32K, tok/s 942 1,228 940 1,224
cold ~62K prompt, s 69.8 53.0 69.7 53.1
drafted round at 2K / 32K / 90K, ms 85.2 / 101.0 / 131.1 85.3 / 101.4 / 129.9 101.8 / 115.8 / 146.6 100.3 / 115.8 / 145.6
bench_openai.py code / chat sampled 68.9 / 41.5 69.1 / 42.0 58.5 / 37.7 58.6 / 37.7
code / chat greedy 53.6 / 47.0 54.4 / 47.2 53.5 / 42.8 54.0 / 43.0
KL against BF16 / top-1 0.0085 / 97.35% 0.0085 / 97.37% 0.0027 / 98.39% 0.0027 / 98.42%
  • Prompts are 30% faster (cold 62K is 24% shorter), and decode is unchanged.
  • On the branch, drafted == "draft": false on both packs for a 250-line rewrite, an edit block and a code reply.
  • Against stock:
    • On 4.00, all three token_sha match stock.
    • On 5.00, the rewrite and the edit block match, and the code reply differs (142ce5701d9a stock, a1a9b5048a42 here), as expected from commit 2's bf16 W''.
    • The KL row (110,155 positions, teacher-forced through the prompt and verify paths) shows no cost on either pack. The PR hadn't measured 5.00.
  • Commit 4 only changes multi.py, so a one-stream server doesn't take it. A single stream reaches commits 5 and 6 only on copy windows wider than 16 rows; the rewrite above is one.

A question on commit 3: _Rows is built for every chunk up front, so each chunk's x lives until the prompt ends. That's about 1.3 GB at 128K tokens for x alone, if I count right. Is that in the startup estimate? A 127K-token prompt ran fine here at --context 131072 with one stream, and the estimate read 29.13 GiB, the same as stock.

Not run: --parallel above 1 (the main target of commits 4–6), resumed == fresh on real packs, peak memory.

…ual stream (same bits)

A prompt of several chunks runs layer by layer (cuda prefill, LAYER_MAJOR), so one _Rows per
chunk is built up front and every row's x and pending (bf16, hidden wide) live until the
prompt ends: 4 * hidden bytes a row, 2.4 GiB at 126K tokens for the 27B. The startup estimate
counted one chunk's scratch only, so it printed the same figure with layer-major on and off.

admission(layer_major=True) adds 4 * hidden bytes a cache slot, the engine passes LAYER_MAJOR,
for one stream and for --parallel alike. Nothing else changes: no kernel, no rounding, no row
sees different bits; with TENSORFOLD_PREFILL_LAYER_MAJOR=0 the estimate is as before.

Measured on a DGX Spark (GB10), 27B EXL3 4.00bpw, a cold 126K-token prompt, --context 131072:
peak torch allocation above its starting level 11.97 GiB layer-major against 9.14 GiB chunk by
chunk at --parallel 1 (+2.83), 4.23 against 1.34 at --parallel 4 (+2.89). Estimate printed
29.13 / 30.34 GiB either way before; 31.64 / 32.84 GiB with layer-major, 29.13 / 30.34 without.
Replies equal: drafted == "draft": false in 12 of 12 pairs (3 prompts of 20K tokens, 4 configs)
and layer-major == chunk by chunk by token_sha.

Tests: tests/test_exl3_engines.py::test_admission_counts_a_layer_major_prompts_residual_stream_by_slots
fails before (admission() has no layer_major) and passes after.
@BHCC2025

BHCC2025 commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

@Arminova, thank you for the receipt, and for asking about commit 3.

Short answer: no, the startup estimate does not count it. With layer-major on, x and pending (bf16, hidden wide) of every chunk live until the prompt ends. For the 27B at 126,035 rows that is 1.20 GiB for x, as you worked out, and the same for pending: 2.40 GiB together. The estimate counts one chunk's scratch, so it printed the same figure with layer-major on and off.

Measured on one DGX Spark (GB10), 27B EXL3 4.00bpw, one cold 126K-token prompt, --context 131072, peak torch allocation above its starting level:

layer-major (default) TENSORFOLD_PREFILL_LAYER_MAJOR=0 difference
--parallel 1 11.97 GiB 9.14 GiB +2.83
--parallel 4 4.23 GiB 1.34 GiB +2.89

The estimate printed 29.13 GiB at --parallel 1 and 30.34 at --parallel 4, in both modes. Peak allocation stayed under it (28.6 GiB in both runs) and the allocator's reserved peak was 30.2 and 29.5 GiB. Stock 0.6.5 on the same prompt reserved 31.1 GiB at --parallel 1 (peak 9.23 GiB above start), so the reserve above the estimate there is not from this commit. The run fits with room under this Spark's 98.6 GiB budget. The term matters where the budget is tight, because the estimate also picks the context window when --context is not given.

What we are changing: the admission adds 4 x hidden bytes a cache slot when layer-major is on, for one stream and for --parallel (2.50 GiB at 131,200 slots: the estimate goes from 29.13 to 31.64 GiB, and from 30.34 to 32.84 at --parallel 4). With TENSORFOLD_PREFILL_LAYER_MAJOR=0 it is as before. No kernel and no rounding changes. It is one small commit on #260:

  • Test: the new test_admission_counts_a_layer_major_prompts_residual_stream_by_slots fails on the current branch and passes with the change. The five files you listed pass on our branch: 146 passed, 3 skipped, 2 xfailed.
  • Exactness, on the changed code: drafted equals "draft": false in 12 of 12 pairs (3 prompts of 20K tokens, layer-major on and off, --parallel 1 and 4), and layer-major equals chunk by chunk by token_sha.
  • Speed, same prompt and machine: cold 126K prompt 971-992 tok/s layer-major, 917-921 chunk by chunk (our branch), 785-795 on stock 0.6.5. The branch is based on an older release than 0.6.5, so the last row is across versions.

On the 5.00 code reply that differs from stock: the PR text lists commit 2 (W'' held in bf16) among the commits that change the prompt path's rounding, and your KL row shows no cost. We have not run your 5.00 prompts, so we can't say that is the cause.

Not run: only the 4.00bpw pack, on one Spark. Resumed equals fresh on real packs and concurrent equals solo were not rerun here; the existing tests cover them. Nothing was rebased onto 0.6.5.

@ashhart

ashhart commented Oct 6, 2026

Copy link
Copy Markdown
Owner

Thank you, @BHCC2025. This is a careful series. The mid-M kernels decode each tile once per 16 or 32 rows, linear_wc covers 4- and 6-bit mul1 layers, layer-major prefill holds one layer's decoded weights, and the tail merge saves a prefill call, each behind its own switch with its own tests.

The Python engine is frozen (#286), so this won't merge. The Zig engine has no EXL3 reader or 27B family yet, and Zig CUDA runs only Nemotron. When the 27B's EXL3 packs serve on Zig CUDA, the bit-identical commits come across first, with your credit. The folded weights rounded to bf16 hold fewer bits than the fp16 path they replace, so that part would come across as an opt-in mode, off by default, with the replies it changes documented.

@ashhart ashhart closed this Oct 6, 2026
BHCC2025 added a commit to BHCC2025/TensorFold that referenced this pull request Oct 31, 2026
…ows, projection groups, PDL (same bits)

The Python engine's EXL3 speed series (ashhart#260) on the native Dense module. Weight.sk/wk let a family fix a shape's
plan (K splits, warps); the mid-M kernels and linear_wc give the linear's bits at 17-128 rows; matmulGroup runs up to
three layers of one K over the same rows in one rotation and one linear launch; the rotations and decode-row linears
launch with programmatic dependent launch (TF_EXL3_PDL=0: plain), and counters are zeroed once.
tf-cuda-test exl3-plans, exl3-midm and exl3-shapes check and time them.
BHCC2025 added a commit to BHCC2025/TensorFold that referenced this pull request Oct 31, 2026
…ows, projection groups, PDL (same bits)

The Python engine's EXL3 speed series (ashhart#260) on the native Dense module. Weight.sk/wk let a family fix a shape's
plan (K splits, warps); the mid-M kernels and linear_wc give the linear's bits at 17-128 rows; matmulGroup runs up to
three layers of one K over the same rows in one rotation and one linear launch; the rotations and decode-row linears
launch with programmatic dependent launch (TF_EXL3_PDL=0: plain), and counters are zeroed once.
tf-cuda-test exl3-plans, exl3-midm and exl3-shapes check and time them.
BHCC2025 added a commit to BHCC2025/TensorFold that referenced this pull request Nov 1, 2026
…ows, projection groups, PDL (same bits)

The Python engine's EXL3 speed series (ashhart#260) on the native Dense module. Weight.sk/wk let a family fix a shape's
plan (K splits, warps); the mid-M kernels and linear_wc give the linear's bits at 17-128 rows; matmulGroup runs up to
three layers of one K over the same rows in one rotation and one linear launch; the rotations and decode-row linears
launch with programmatic dependent launch (TF_EXL3_PDL=0: plain), and counters are zeroed once.
tf-cuda-test exl3-plans, exl3-midm and exl3-shapes check and time them.
ashhart pushed a commit to BHCC2025/TensorFold that referenced this pull request Nov 2, 2026
…ows, projection groups, PDL (same bits)

The Python engine's EXL3 speed series (ashhart#260) on the native Dense module. Weight.sk/wk let a family fix a shape's
plan (K splits, warps); the mid-M kernels and linear_wc give the linear's bits at 17-128 rows; matmulGroup runs up to
three layers of one K over the same rows in one rotation and one linear launch; the rotations and decode-row linears
launch with programmatic dependent launch (TF_EXL3_PDL=0: plain), and counters are zeroed once.
tf-cuda-test exl3-plans, exl3-midm and exl3-shapes check and time them.
ashhart pushed a commit that referenced this pull request Nov 2, 2026
…ows, projection groups, PDL (same bits)

The Python engine's EXL3 speed series (#260) on the native Dense module. Weight.sk/wk let a family fix a shape's
plan (K splits, warps); the mid-M kernels and linear_wc give the linear's bits at 17-128 rows; matmulGroup runs up to
three layers of one K over the same rows in one rotation and one linear launch; the rotations and decode-row linears
launch with programmatic dependent launch (TF_EXL3_PDL=0: plain), and counters are zeroed once.
tf-cuda-test exl3-plans, exl3-midm and exl3-shapes check and time them.
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.

4 participants