mirror of
https://github.com/mw00/project-maya.git
synced 2026-12-03 21:08:26 +07:00
[PR #194] EXL3 CPU lane: an AVX2 + FMA tier (Zen 2/3 and Intel CPUs without AVX-512 ran it in scalar code) #387
Labels
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
mw00/project-maya#387
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Original Pull Request: https://github.com/mw00/project-maya/pull/194
State: closed
Merged: No
What
exl3_cpu.cpphas 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.
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.
vpermdper 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 inregisters: 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.
band_avx2_n, a verify window's tokens sharing an expert): each tile row decoded once, oneFMA 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.
cpu_avx2_ok), else scalar.STRATA_FORCE_AVX2=1now selects this tier on an AVX-512 CPU (it selected the scalar loop), so both tiers can becompared on one host.
exl3_cpu_tier()andexl3_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 threadsin
lane_experts' job shape (--per-callexperts 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 thetile's bit ring, the mul1 byte sum, one
std::fmaa weight in (slice, row) order) - K 1..8, 1..11 tokens, whole andpartial 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).
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 --selftestandtools/check_hip_compat.pypass on v1.1.11; a GGUF model's (Maya-M's) KL / nll gatesequal their references to the last digit (checked on v1.1.9 - no GGUF code is touched).
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 withtools/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 holdsevery 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):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):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 laneswitches 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_pdorder), so the tiers stay bit-identical. #188's own tests pass onit (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 (
--benchon v1.1.9), prefill 256 / 461 - the best of the variants. Happy to rebase whichever wayyou prefer.
Credit: the kernel follows exllamav3's CPU expert kernel (MIT, turboderp) - its AVX2 tier's structure and tables.
🤖 Generated with Claude Code