[PR #194] EXL3 CPU lane: an AVX2 + FMA tier (Zen 2/3 and Intel CPUs without AVX-512 ran it in scalar code) #387

Closed
opened 2026-10-29 21:24:54 +07:00 by bsakaguchi · 0 comments
Owner

Original Pull Request: https://github.com/mw00/project-maya/pull/194

State: closed
Merged: No


What

exl3_cpu.cpp has the AVX-512 BW kernel and a scalar loop. A CPU with AVX2 and no AVX-512 - Zen 2/3 desktops,
Threadripper and EPYC 7002/7003, Intel consumer parts - runs the scalar loop: on a Ryzen 9 5900X an EXL3 expert takes
6.7 ms on a card's 6 lane threads against 0.94 ms over PCIe, so the start-up timing switches the lane off (or nearly) and
every RAM-tier expert goes over PCIe. This adds an AVX2 + FMA tier: 0.41 ms an expert on the same 6 threads, and the lane
takes most RAM-tier experts again.

It is what #187 ran into on a Threadripper PRO 3955WX (Zen 2): "the engine disables it here (7.05 ms/expert CPU vs
0.36 ms PCIe, 20x)". The lane itself does run beside the PCIe fetches - the start-up timing splits a route's RAM-tier
experts between the two so both finish together - it was the scalar loop that made every CPU share a loss there.

  • The AVX-512 tier on 8-lane registers. A tile row's 16 columns are two halves of 8. The funnel shift, the mul1 byte
    sum (vpmulld, vpmaddubsw, vpmaddwd) and one F32 FMA per weight are the AVX-512 tier's, lane for lane, in the same
    (slice, row) order - so the two tiers give the same bits.
  • The gather: one vpermd per half-row. AVX2 shuffles only within one 8-word register and a tile spans K of them;
    exllamav3's AVX2 tier walks every register that holds a lane (K permutes and K - 1 blends a gather). In the tile layout a
    half-row's 8 words are exactly K apart, so they lie in one residue class of the word index mod K, with one shift for all
    8. So each tile's K registers are regrouped once into K class registers (odd K: blends only - a class takes each lane
    from exactly one register; K 2 / 4 / 8: shuffles / a 4x4 / an 8x8 transpose; K 6: a permute and a blend a register), and
    each gather is then one vpermd (none where the lanes are in order). That is exllamav3's planar layout built in
    registers: the RAM tier and the GPU kernels keep the tiles as they are. Against the register walk: +11 % at 3 bits,
    +27 % at 4 bits on one Zen 3 core.
  • Several tokens at once (band_avx2_n, a verify window's tokens sharing an expert): each tile row decoded once, one
    FMA per token; up to 3 tokens both halves of a tile at once (2N accumulators), more one half after the other.
    Activation i's FMAs keep their order: the one-token bits.
  • Dispatch: AVX-512 where it runs today (unchanged), else AVX2 + FMA (cpu_avx2_ok), else scalar.
    STRATA_FORCE_AVX2=1 now selects this tier on an AVX-512 CPU (it selected the scalar loop), so both tiers can be
    compared on one host. exl3_cpu_tier() and exl3_cpu_band_acc_on(tier, ...) for tests and benchmarks. No new setting;
    nothing on the GPU side, the GGUF lanes or HIP changes. MSVC: intrinsics only, the target attribute only for GCC/Clang
    (not compiled on Windows here).
  • exl3_cpu_bench (built with the tests, not a ctest): a band's speed on one core, and an expert from DRAM on T threads
    in lane_experts' job shape (--per-call experts a call).

Same bits

  • exl3_test: every tier this CPU runs against an exact model of the arithmetic (the window read bit by bit from the
    tile's bit ring, the mul1 byte sum, one std::fma a weight in (slice, row) order) - K 1..8, 1..11 tokens, whole and
    partial slice ranges, a 37-slice matrix: the AVX2 tier 0 differ (on an AVX-512 host the test runs both vector tiers
    against the model, i.e. against each other); the scalar loop within 3e-7 (it rounds each product before adding).
  • Real weights: 18 expert matrices from turboderp's GLM-5.3-Flash 3.05bpw, 4.05bpw and SC 3.10bpw branches (3- and 4-bit,
    the MTP layer's too): 0 differ from the model; against Maya's reference the same error as before (rel 2.8-3.0e-4).
  • glm_*_parity --selftest and tools/check_hip_compat.py pass on v1.1.11; a GGUF model's (Maya-M's) KL / nll gates
    equal their references to the last digit (checked on v1.1.9 - no GGUF code is touched).
  • Does the output change? Not between the vector tiers. On an AVX2 CPU, yes, in the way the CPU lane always does: the
    lane now takes RAM-tier experts that went over PCIe, and the lane computes the codebook in F32 without the device's FP16
    rounding of each weight (plus FMA instead of the scalar loop's separate rounding). On 1,135 tokens of held-out prose
    (greedy, every RAM-tier expert on the CPU): KL 0.0108 against every expert on the GPU, the same top token 96.7 %; the
    scalar loop against this tier KL 0.0103 - a near-tie in the routing or the sparse attention flips for any last-bit
    change. The same run twice: KL 0.

Measured

Ryzen 9 5900X (12 cores, capped at 4.3 GHz), 128 GB DDR4, RTX 3090 + RTX 3060 (PCIe 4.0 x8 each), Linux, CUDA 12.8.
EXL3-SC 3.10bpw (sc_3.10bpw @ b570edf, packed with tools/exl3_pack.py), --max-context 524288, two-card settings:
STRATA_GLM_SPLIT=28 STRATA_GLM_RAM_RESIDENT=1 STRATA_GLM_RAM_SLACK=8 STRATA_GLM_PREFILL_LAND=16 STRATA_GLM_KV_INT8=1 STRATA_GLM_KV_RESIDENT=32768 STRATA_GLM_MTP_KEEP=2 STRATA_GLM_MTP_PIPELINE=1 STRATA_GLM_SPEC_SAMPLE=1 STRATA_GLM_SPEC_CANCEL=1 STRATA_GLM_PREFETCH_N=1 STRATA_GLM_PREFETCH_AT=fetch STRATA_GLM_PREFETCH_BLOCKS=4 STRATA_GLM_PREFETCH_RANK=2 STRATA_GLM_PREFILL_MB=3072 STRATA_GLM_PREFILL_MIN_CHUNK=100000 STRATA_GLM_TRUNK_KEEP_MAP=8888888888888888888888855555554444444444444448 (the slack and landing ring so the RAM tier holds
every non-VRAM expert; the CPU plan left to the start-up timing). v1.1.11 against this branch, same build options.

./maya.sh --bench's protocol (three answers of 256 tokens, greedy, thinking off; prefill at 2K / 8K):

decode tok/s prefill 2K / 8K tok/s
v1.1.11 14.4 (13.7 / 15.0 / 14.5) 220 / 443
this branch 20.1 (19.5 / 21.1 / 19.6) 221 / 445

The same on v1.1.9 and v1.1.10: 14.5 -> 20.7 and 14.2 -> 20.0. A 21K-token prompt, then 2,500 tokens at 21K depth (default
sampling, 2 runs, on v1.1.9): 13.96 -> 19.76 tok/s. The lane at start-up: 6.72 -> 0.41 ms an expert (the 3090's plan of
0..8 RAM-tier experts on the CPU: 0 0 0 0 0 0 0 0 1 -> 0 1 2 2 3 4 4 5 6). In decode it times 0.59 / 0.76 ms an expert
(3090 / 3060): both cards' lanes and the GPUs' copies share the memory system there.

The kernel (exl3_cpu_bench, 3-bit, from DRAM):

scalar AVX2
one core, one token 641 M weights/s 11,167 M weights/s
6 threads, ms an expert 7.00 0.410 (23 GB/s)
12 threads 3.52 0.321 (29 GB/s)
6 threads, 2 tokens an expert - 0.497

Tried and dropped (no gain): two tiles interleaved, other prefetch distances (2 / 8 / 16 slices), several bands a job,
pinned threads, 8 lane threads a card, one pool for both cards.

Overlap with #188

#188 rebuilds the same lane (split-K pieces, a band GEMM for prompts, vectorised helpers) in AVX-512 with scalar
fallbacks. On this AVX2 CPU it alone measured 14.4 tok/s (--bench; its kernels fall back to the scalar loop and the lane
switches off). If #188 goes in first, a rebased version of this PR is ready:
sociolog:exl3-cpu-avx2-on-188 - the same tier plus
AVX2 versions of #188's new kernels: rows_avx2_n (split-K), decode_chunk_avx2 + gemm_tile_avx2 (the GEMM),
had128_avx2, prepare_block_avx2, band_out_avx2, each the same operations in the same order as #188's AVX-512 version
(the block sum reduced in GCC's _mm512_reduce_add_pd order), so the tiers stay bit-identical. #188's own tests pass on
it (split-K and GEMM vs bands: 0 differ; the transform: 0 of 8192), and Maya-M's gates stay exact. On this box #188 + the
AVX2 tier: 22.6 tok/s (--bench on v1.1.9), prefill 256 / 461 - the best of the variants. Happy to rebase whichever way
you prefer.

Credit: the kernel follows exllamav3's CPU expert kernel (MIT, turboderp) - its AVX2 tier's structure and tables.

🤖 Generated with Claude Code

**Original Pull Request:** https://github.com/mw00/project-maya/pull/194 **State:** closed **Merged:** No --- ## What `exl3_cpu.cpp` has the AVX-512 BW kernel and a scalar loop. A CPU with AVX2 and no AVX-512 - Zen 2/3 desktops, Threadripper and EPYC 7002/7003, Intel consumer parts - runs the scalar loop: on a Ryzen 9 5900X an EXL3 expert takes 6.7 ms on a card's 6 lane threads against 0.94 ms over PCIe, so the start-up timing switches the lane off (or nearly) and every RAM-tier expert goes over PCIe. This adds an AVX2 + FMA tier: 0.41 ms an expert on the same 6 threads, and the lane takes most RAM-tier experts again. It is what #187 ran into on a Threadripper PRO 3955WX (Zen 2): "the engine disables it here (7.05 ms/expert CPU vs 0.36 ms PCIe, 20x)". The lane itself does run beside the PCIe fetches - the start-up timing splits a route's RAM-tier experts between the two so both finish together - it was the scalar loop that made every CPU share a loss there. - **The AVX-512 tier on 8-lane registers.** A tile row's 16 columns are two halves of 8. The funnel shift, the mul1 byte sum (`vpmulld`, `vpmaddubsw`, `vpmaddwd`) and one F32 FMA per weight are the AVX-512 tier's, lane for lane, in the same (slice, row) order - so **the two tiers give the same bits**. - **The gather: one `vpermd` per half-row.** AVX2 shuffles only within one 8-word register and a tile spans K of them; exllamav3's AVX2 tier walks every register that holds a lane (K permutes and K - 1 blends a gather). In the tile layout a half-row's 8 words are exactly K apart, so they lie in one residue class of the word index mod K, with one shift for all 8. So each tile's K registers are regrouped once into K class registers (odd K: blends only - a class takes each lane from exactly one register; K 2 / 4 / 8: shuffles / a 4x4 / an 8x8 transpose; K 6: a permute and a blend a register), and each gather is then one `vpermd` (none where the lanes are in order). That is exllamav3's planar layout built in registers: the RAM tier and the GPU kernels keep the tiles as they are. Against the register walk: +11 % at 3 bits, +27 % at 4 bits on one Zen 3 core. - **Several tokens at once** (`band_avx2_n`, a verify window's tokens sharing an expert): each tile row decoded once, one FMA per token; up to 3 tokens both halves of a tile at once (2N accumulators), more one half after the other. Activation i's FMAs keep their order: the one-token bits. - **Dispatch:** AVX-512 where it runs today (unchanged), else AVX2 + FMA (`cpu_avx2_ok`), else scalar. `STRATA_FORCE_AVX2=1` now selects this tier on an AVX-512 CPU (it selected the scalar loop), so both tiers can be compared on one host. `exl3_cpu_tier()` and `exl3_cpu_band_acc_on(tier, ...)` for tests and benchmarks. No new setting; nothing on the GPU side, the GGUF lanes or HIP changes. MSVC: intrinsics only, the target attribute only for GCC/Clang (not compiled on Windows here). - `exl3_cpu_bench` (built with the tests, not a ctest): a band's speed on one core, and an expert from DRAM on T threads in `lane_experts`' job shape (`--per-call` experts a call). ## Same bits - `exl3_test`: every tier this CPU runs against an exact model of the arithmetic (the window read bit by bit from the tile's bit ring, the mul1 byte sum, one `std::fma` a weight in (slice, row) order) - K 1..8, 1..11 tokens, whole and partial slice ranges, a 37-slice matrix: the AVX2 tier 0 differ (on an AVX-512 host the test runs both vector tiers against the model, i.e. against each other); the scalar loop within 3e-7 (it rounds each product before adding). - Real weights: 18 expert matrices from turboderp's GLM-5.3-Flash 3.05bpw, 4.05bpw and SC 3.10bpw branches (3- and 4-bit, the MTP layer's too): 0 differ from the model; against Maya's reference the same error as before (rel 2.8-3.0e-4). - `glm_*_parity --selftest` and `tools/check_hip_compat.py` pass on v1.1.11; a GGUF model's (Maya-M's) KL / nll gates equal their references to the last digit (checked on v1.1.9 - no GGUF code is touched). - **Does the output change?** Not between the vector tiers. On an AVX2 CPU, yes, in the way the CPU lane always does: the lane now takes RAM-tier experts that went over PCIe, and the lane computes the codebook in F32 without the device's FP16 rounding of each weight (plus FMA instead of the scalar loop's separate rounding). On 1,135 tokens of held-out prose (greedy, every RAM-tier expert on the CPU): KL 0.0108 against every expert on the GPU, the same top token 96.7 %; the scalar loop against this tier KL 0.0103 - a near-tie in the routing or the sparse attention flips for any last-bit change. The same run twice: KL 0. ## Measured Ryzen 9 5900X (12 cores, capped at 4.3 GHz), 128 GB DDR4, RTX 3090 + RTX 3060 (PCIe 4.0 x8 each), Linux, CUDA 12.8. EXL3-SC 3.10bpw (`sc_3.10bpw` @ b570edf, packed with `tools/exl3_pack.py`), `--max-context 524288`, two-card settings: `STRATA_GLM_SPLIT=28 STRATA_GLM_RAM_RESIDENT=1 STRATA_GLM_RAM_SLACK=8 STRATA_GLM_PREFILL_LAND=16 STRATA_GLM_KV_INT8=1 STRATA_GLM_KV_RESIDENT=32768 STRATA_GLM_MTP_KEEP=2 STRATA_GLM_MTP_PIPELINE=1 STRATA_GLM_SPEC_SAMPLE=1 STRATA_GLM_SPEC_CANCEL=1 STRATA_GLM_PREFETCH_N=1 STRATA_GLM_PREFETCH_AT=fetch STRATA_GLM_PREFETCH_BLOCKS=4 STRATA_GLM_PREFETCH_RANK=2 STRATA_GLM_PREFILL_MB=3072 STRATA_GLM_PREFILL_MIN_CHUNK=100000 STRATA_GLM_TRUNK_KEEP_MAP=8888888888888888888888855555554444444444444448` (the slack and landing ring so the RAM tier holds every non-VRAM expert; the CPU plan left to the start-up timing). v1.1.11 against this branch, same build options. `./maya.sh --bench`'s protocol (three answers of 256 tokens, greedy, thinking off; prefill at 2K / 8K): | | decode tok/s | prefill 2K / 8K tok/s | |---|---|---| | v1.1.11 | 14.4 (13.7 / 15.0 / 14.5) | 220 / 443 | | this branch | **20.1** (19.5 / 21.1 / 19.6) | 221 / 445 | The same on v1.1.9 and v1.1.10: 14.5 -> 20.7 and 14.2 -> 20.0. A 21K-token prompt, then 2,500 tokens at 21K depth (default sampling, 2 runs, on v1.1.9): 13.96 -> 19.76 tok/s. The lane at start-up: 6.72 -> 0.41 ms an expert (the 3090's plan of 0..8 RAM-tier experts on the CPU: 0 0 0 0 0 0 0 0 1 -> 0 1 2 2 3 4 4 5 6). In decode it times 0.59 / 0.76 ms an expert (3090 / 3060): both cards' lanes and the GPUs' copies share the memory system there. The kernel (`exl3_cpu_bench`, 3-bit, from DRAM): | | scalar | AVX2 | |---|---|---| | one core, one token | 641 M weights/s | 11,167 M weights/s | | 6 threads, ms an expert | 7.00 | 0.410 (23 GB/s) | | 12 threads | 3.52 | 0.321 (29 GB/s) | | 6 threads, 2 tokens an expert | - | 0.497 | Tried and dropped (no gain): two tiles interleaved, other prefetch distances (2 / 8 / 16 slices), several bands a job, pinned threads, 8 lane threads a card, one pool for both cards. ## Overlap with #188 #188 rebuilds the same lane (split-K pieces, a band GEMM for prompts, vectorised helpers) in AVX-512 with scalar fallbacks. On this AVX2 CPU it alone measured 14.4 tok/s (`--bench`; its kernels fall back to the scalar loop and the lane switches off). If #188 goes in first, a rebased version of this PR is ready: [sociolog:exl3-cpu-avx2-on-188](https://github.com/sociolog/project-maya/tree/exl3-cpu-avx2-on-188) - the same tier plus AVX2 versions of #188's new kernels: `rows_avx2_n` (split-K), `decode_chunk_avx2` + `gemm_tile_avx2` (the GEMM), `had128_avx2`, `prepare_block_avx2`, `band_out_avx2`, each the same operations in the same order as #188's AVX-512 version (the block sum reduced in GCC's `_mm512_reduce_add_pd` order), so the tiers stay bit-identical. #188's own tests pass on it (split-K and GEMM vs bands: 0 differ; the transform: 0 of 8192), and Maya-M's gates stay exact. On this box #188 + the AVX2 tier: 22.6 tok/s (`--bench` on v1.1.9), prefill 256 / 461 - the best of the variants. Happy to rebase whichever way you prefer. Credit: the kernel follows exllamav3's CPU expert kernel (MIT, turboderp) - its AVX2 tier's structure and tables. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
bsakaguchi 2026-10-29 21:24:54 +07:00
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
mw00/project-maya#387
No description provided.