Repository navigation
[RFC / Benchmarks] Fitting 284B DeepSeek-V4-Flash in 57.61 GiB RAM (256K ctx, 0 GPUs): SwiGLU-H2 Hessian Quantization, AVX-512BW _1x8 Register Tiling (+77.1% pp), & _MM_HINT_NTA L3 Isolation #31699
synchopate
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi everyone,
Over the past few weeks we have been working on fitting and accelerating 284.33B DeepSeek-V4-Flash (61 layers, 256 routed experts, top-8 active, MLA + Engram + MQA8 Sparse Indexer) and 30B-A3B Hybrid MoEs (
Nemotron-3.5-30B-A3B,Qwen3-30B-A3B) on standard dual-channel 64 GB DDR5 CPU workstations (0 GPUs) at full 256K context.All patches, reproducible standalone C transcoders, and AVX2/AVX-512BW/NEON kernels are open-source (MIT) in
synchopate/llama-modchip. Below is a purely technical breakdown of the 5 most impactful low-level optimizations, with exact microbenchmark telemetry and file references in case any of these are useful to upstreamggml-cpu.1. Closed-Form Second-Order SwiGLU Hessian Diagonal (
SwiGLU-H2) + Block-64Q2_0(3.75 bpw) Transcoding (-20.31 GiB)Standard$W_g$ ) and Up ($W_u$ ) projections multiplicatively:
IQ1_S/IQ2_XXSconversion of 284.33B DeepSeek-V4-Flash occupies 76.87 GiB on disk, which immediately triggers Linux OOM killer on a 64 GB RAM box (or collapses into133 tok/sSSD page-cache thrashing). However, naively dropping routed experts to 1-bit without cross-projection sensitivity weighting destroys coding/math perplexity because SwiGLU couples the Gate (In$x \sim \mathcal{N}(0, \sigma_x^2 I)$ :
scripts/transcode_dsv4.c(transcode_moe_exps, lines 610–742), we derive the exact expected diagonal of the Gauss-Newton Hessian over an isotropic spherical activation priorThis gives a closed-form per-row importance metric across all 256 routed experts in layers 3–60 without requiring an external calibration dataset:
Combined with transcoding the dense
Q5_KMLA key-LORA (attn_k_b,3.70 GiB->1.62 GiB) and Sparse Indexer (indexer.wk,indexer.weights_proj) into a custom 64-weight blockGGML_TYPE_Q2_0(3.75 bpw, 30 bytes/64 weights) with a 24-iteration alternating Lloyd-Max solver:76.87 GiB56.56 GiB(60,729,886,080bytes,-20.31 GiB/-26.4%)VmRSS(/proc/pid/statusat256Kctx):57.61 GiB(60,408,048 kB), leaving6.39 GiBof free headroom inside 64 GiB RAM with 0 bytes of swap.2. Whole-Model 4-Level Cache-Tiled Prefill ($\rightarrow$ L1D $\rightarrow$ L2 $\rightarrow$ L3) (
ZMMRegisters+77.1%Prefill Throughput)On CPU, multi-token prompt evaluation (
pp) in MoE models suffers from severe DDR5 read amplification: token-major execution streams the same quantized expert/attention weights from RAM repeatedly across tokens, while standardstep_k = 2(.out_1x2) matrix-matrix kernels only reuse a decoded weight super-block across 2 activation columns.In
patches/deepseek-v4-flash-fractal-256k.patch(ggml/src/ggml-cpu/arch/x86/quants.candggml-cpu.c), we implemented a 4-level cache hierarchy optimization across all 6 quantized tensor types (Q2_0,IQ1_S,IQ2_XXS,IQ1_M,Q5_K,Q6_K):ZMMRegister-Stationary_1x8,_1x4,_1x3,_1x2Multi-Column Kernels):__m512i), folding two 256-elementQK_Ksuper-blocks perZMMiteration and keeping 8ZMMaccumulators (acc0..acc7) pinned in registers across 8 activation columns (step_k = 8). This cuts weight decompression +maddubsoverhead by4.0xvs_1x2(8.0xvs1x1).{8, 4, 3, 2}Greedy Column Decomposition (ggml_compute_forward_mul_mat_one_chunk): In 256-expert MoEs (top-8), individual experts frequently receive odd token counts ({8, 4, 2, 1}), odd tails drop to scalar1x1. Adding_1x3enables exact decomposition (1x1fallback for every batch size_pdep_u64+ SWAR XOR-Parity Sign Expansion (IQ2_XXS): Replaces the 8-loadkeven_signs_q2xs[aux32]lookup table bottleneck with_pdep_u64(s56 ^ (s56 << 1) ^ ... ^ (s56 << 6), 0x0101010101010101ULL) | (s56 & 0xFEFEFEFEFEFEFEFEULL)followed by_mm512_movm_epi8, expanding 64 signs into aZMMregister in 7 ALU cycles with zero L1D cache ports used.32 KBL1D +1 MBL2 Token-StationaryQ8_KTiling + AVX-512quantize_row_q8_K):quantize_row_q8_K(quants.c:505) with 16ZMMregisters +_mm512_cvtepi32_epi8, keeping theQ8_Kactivation tile (8 cols × 9 KB = 72 KB) hot inside each core's1 MBL2 cache.by_tok = (n_tokens >= nth)) inggml_compute_forward_dsv4_hc_pre_f32andggml_compute_forward_dsv4_hc_post_f32(ops.cpp), preserving per-core L1D/L2 locality across the 4-stream Hyper-Connection (HC) residual width expansion/collapse.64 MBL3MQA8FlashAttention Target-Query Blocking):ne02 / ne12 = 8). OurMQA8fast-path (ops.cpp:9452) loads each cached KV token once from L3/RAM and evaluates all 8 query heads simultaneously in registers (8xKV bandwidth reduction), while tiling by target query tokenic(n_queries >= nth) so each thread keeps its 8 query heads (8 × 576 × 2B = 9.2 KB) inside its32 KBL1D.pp)v2.3.0(tok/s)v2.4.0(tok/s)100% Bit-Exact)pp = 29825.20 tok/s43.16 tok/s+71.3%pp = 39225.10 tok/s43.03 tok/s+71.4%pp = 403(Slot-0 Warm)25.05 tok/s44.36 tok/s+77.1%pp = 58024.73 tok/s43.98 tok/s+77.8%3. Single-Cycle AVX2 Variable Bit-Shift
dot_q2_0_q8_0(+55%) & 3D L1D Tiling (TILE_I0 = 64)For CPUs without AVX-512 (or in our standalone
Q2_0AVX2 path inpatches/q2_0-avx2-3d-tiling.patch):_mm256_srlv_epi32Unpacking: Instead of 4 sequential scalar shifts (>> 0, >> 2, >> 4, >> 6) +_mm256_set_m128ilane insertions to unpack 2-bit weights, we broadcast the 128-bitqspayload (_mm256_broadcastsi128_si256) and apply a single-cycle per-dword variable shift_mm256_srlv_epi32(q256, _mm256_setr_epi32(0, 0, 2, 2, 4, 4, 6, 6))(+55%isolated kernel speedup:4.42 -> 2.85 cycles/block).TILE_I0 = 64): Standard 2D tiling streams the entirexactivation vector (16 KBforK = 16384) for every output row, overflowing Zen 5's48 KBL1D (11.2 GB/sL1D-to-L2 spill traffic per token). Adding an inner output-row tile (TILE_I0 = 64) bounds the working set to18.5 KB < 48 KBL1D, reducingxreloads by64x.4.
_MM_HINT_NTAL3 Isolation (HARPCache) + 8-Step Sparse Indexer Lookahead Prefetch (ic_pf = i + 8)HARPCache): During decode (1.88 GiBof cold routed MoE expert weights per token through standard temporal cache lines evicts the hotQ8_Kactivation vectors, shared experts, and MLA KV-cache from the64 MBL3 cache. Inserting_mm_prefetch(..., _MM_HINT_NTA)in the expert dot-product inner loops isolates cold expert weights to a single L3 way while pinning hot activations in L1D/L2/L3.ic_pf = i + 8): Inggml_compute_forward_flash_attn_ext_f16_one_chunk(ops.cpp), when theindex_masktensor ([n_kv, n_queries]) selects top-$k$ sparse KV tokens, irregular row jumps stall the hardware prefetcher. Scanning ahead toic_pf = i + 8and issuing_mm_prefetch(k_data + kv_idx_pf * nb11, _MM_HINT_T0)hides the ~90 ns DDR5 latency behind ongoing AVX-512 FMA execution.5.$L_2$ -Quadrature Adaptive MoE Pruning (
p_early = 0.025) + Gate-First Lazy SwiGLUIn$< 1%$ of the layer norm.
Nemotron-3.5-30B-A3B(128experts,top-6active) andDeepSeek-V4-Flash(256experts,top-8active), trailing experts in the softmax tail frequently contributeKL = 0.000517vs0.004810for3.16%DRAM Elimination): Evaluatesffn_gate_expsfirst, computesSiLU(gate)in L1D, and skips fetchingffn_up_expsandffn_down_expswheneverAll code and clean git patches are available in
patches/. Happy to split any of these (especially the AVX-512BW_1x8/_1x4/_1x3/_1x2quants.ckernels or theMQA8FlashAttention fast-path) into standalone PRs if there is interest!All reactions