okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8ac8ce32e
main
1// Copyright (c) 2026 Peasant Smith and the Project Maya contributors (MIT; https://github.com/mw00/project-maya, v1.1.17). 2// Ported into Strata (third_party/project-maya/NOTICE.md, LICENSE); changes are in the git history of this file. 3// src/ds4/ds4_gguf_cpu.hpp - a GGUF's MXFP4 experts on the CPU lane of maya-ds4 (the format: ds4_gguf.cuh). The 4// weights are read where they lie (the GGUF file's mapping), each block decoded once for all the activations of a 5// call (mx_rows: int8 dots, the CPU's best vector tier at run time; a scalar loop otherwise). 6#pragma once 7 8#include <cstddef> 9#include <cstdint> 10#include <string> 11 12namespace strata::ds4::gguf { 13 14constexpr int kMxCpuMaxN = 8 ;// activations a call 15 16/// y[i][r - r0] = (row r of W) . x[i] for r0 <= r < r1 and i < n (n <= kMxCpuMaxN): W's rows hold k values 17/// (k % 32 == 0) as MXFP4 blocks, rb bytes apart. (F32 activations: the tests' and the benchmark's old path.) 18void mx_cpu_rows (const uint8_t * w ,size_t rb ,int k ,int r0 ,int r1 ,const float * const * x ,int n ,float * const * y ); 19 20/// A GGUF's low-bit experts (ds4_iq.cuh's types) on the CPU lane - ggml-cpu's arithmetic through the GLM CPU lane's 21/// native experts (strata::dsv41k::cpu::native_*: its AVX-512 / AVX2 multi-token rows where they are ggml's bit for bit, 22/// ggml's dots otherwise; ds4_iq_cpu.cpp): an activation quantized once to the weight type's vec_dot_type (Q8_K: an F32 23/// scale per 256 values - the device takes one per 32, so the two sides agree to that rounding), each row one dot. 24struct IqCpu { 25int tg = 0 ,td = 0 ,n_embd = 0 ,n_ff = 0 ; 26int gu_act = 0 ,d_act = 0 ;// the activations' ggml types 27size_t gu_row = 0 ,d_row = 0 ;// bytes a weight row 28size_t act_bytes = 0 ,h_bytes = 0 ;// bytes of x (n_embd) and of h (n_ff) as those 29}; 30/// The layer's geometry; false (err) when this build or ggml-cpu cannot take it. 31bool iq_cpu_fmt (int tg ,int td ,int n_embd ,int n_ff ,IqCpu & f , std::string & err ); 32/// x (k values) as the activation type `act` (f.gu_act / f.d_act) into dst. 33void iq_cpu_quant (int act ,const float * x ,int k ,void * dst ); 34/// ff[t][r] = clamped swiglu(gate_r . a[t], up_r . a[t]) for rows [r0, r1) (limit 0: no clamp), nt tokens. 35void iq_cpu_gate_up (const IqCpu & f ,const uint8_t * gate ,const uint8_t * up ,const void * const * act ,int nt , 36float * const * ff ,int r0 ,int r1 ,float limit ); 37/// out[t][r] = down_r . hq[t] for rows [r0, r1). 38void iq_cpu_down (const IqCpu & f ,const uint8_t * down ,const void * const * hq ,int nt ,float * const * out ,int r0 ,int r1 ); 39/// The trellis types (ds4_iq.cuh's is_tq; ds4_tq_cpu.cpp, Strata's tq_cpu kernels): the same four calls, reached from the 40/// iq_cpu_* ones above for those types (their activations: F32 scale a 32 values, then the int8 values). 41bool tq_cpu_type (int type ); 42int tq_cpu_act (); 43bool tq_cpu_fmt (int tg ,int td ,int n_embd ,int n_ff ,IqCpu & f , std::string & err ); 44void tq_cpu_quant (const float * x ,int k ,void * dst ); 45void tq_cpu_gate_up (const IqCpu & f ,const uint8_t * gate ,const uint8_t * up ,const void * const * act ,int nt ,float * const * ff , 46int r0 ,int r1 ,float limit ); 47void tq_cpu_down (const IqCpu & f ,const uint8_t * down ,const void * const * hq ,int nt ,float * const * out ,int r0 ,int r1 ); 48 49/// ggml block types on the CPU lane through ggml-cpu's row dots (ds4_gg_cpu.cpp), for the IQ / K-quant experts. 50bool gg_cpu_type (int type ); 51/// One row of a GGUF block type (Q4_K / Q5_K / Q6_K / Q8_0 / any type ggml has a to_float for) -> k values as BF16 52/// (round to nearest even), the host embedding's format. False: ggml has no decode for this type. 53bool gg_row_to_bf16 (int type ,const uint8_t * row ,int k ,uint16_t * out ); 54bool gg_cpu_fmt (int tg ,int td ,int n_embd ,int n_ff ,IqCpu & f , std::string & err ); 55void gg_cpu_quant (int act ,const float * x ,int k ,void * dst ); 56void gg_cpu_gate_up (const IqCpu & f ,const uint8_t * gate ,const uint8_t * up ,const void * const * act ,int nt ,float * const * ff , 57int r0 ,int r1 ,float limit ); 58/// -1: from the environment (default: Strata's AVX2 i-quant rows, bit-identical to ggml-cpu's dot, from 2 tokens - 1 on IQ3_S, 3 on IQ1_M; 59/// DS4_GG_IQ256=0 never, =1 always), 0 ggml-cpu, 1 always (benches), 2 the default rule. 60void gg_cpu_set_iq256 (int mode ); 61void gg_cpu_down (const IqCpu & f ,const uint8_t * down ,const void * const * hq ,int nt ,float * const * out ,int r0 ,int r1 ); 62 63/// The CPU lane's MXFP4 rows in the device's arithmetic (ds4_gguf.cu's mx_dot): the activations rounded to int8 per 32 64/// values as the device rounds them (q8_round's q and scale), each block's products summed exactly in integers, then 65/// times (weight scale x activation scale) into F32 sums - the same numbers as the device up to the order of the 66/// blocks' sums. The vector tiers (ds4_mx_kern.inc): AVX2 vpmaddubsw, AVX-VNNI and AVX-512 (VNNI) vpdpbusd; the CPU's 67/// fastest is taken at run time (DS4_MX_TIER=<name> picks another one it runs). Every tier, the scalar one included, 68/// sums in the same order: they give the same bits, so the choice never changes an answer. 69enum MxTier {kMxScalar = 0 ,kMxAvx2 ,kMxAvxVnni ,kMxAvx512 ,kMxAvx512Vnni ,kMxTiers }; 70/// Bytes of an activation of k values (k % 32 == 0) for mx_rows: the int8 values in quads of blocks, then a scale and 71/// an offset correction a block in groups of 8 (ds4_mx_kern.inc) - 1.25 k for k % 256 == 0. 72inline size_t mx_act_bytes (int k ) { 73const size_t nb = (size_t )k /32 ; 74return 128 * ((nb + 3 ) /4 )+ 64 * ((nb + 7 ) /8 ); 75} 76/// (tests) value j (0..31) of block b of an activation, and the block's scale 77int mx_act_value (const void * act ,int b ,int j ); 78float mx_act_scale (const void * act ,int k ,int b ); 79/// Blocks [b0, b1) of a k-value activation (x at block b0's first value: x[32 (b - b0) + j]) into act. 80void mx_quant_blocks (const float * x ,int b0 ,int b1 ,void * act ,int k ); 81void mx_quant_blocks_scalar (const float * x ,int b0 ,int b1 ,void * act ,int k );// (the same bytes, no vectors) 82inline void mx_quant (const float * x ,int k ,void * act ) {mx_quant_blocks (x ,0 ,k /32 ,act ,k ); } 83/// y[i][r - r0] = (row r of W) . act[i] for r0 <= r < r1, i < n (any n; act[i] mx_quant's of k values). 84void mx_rows (const uint8_t * w ,size_t rb ,int k ,int r0 ,int r1 ,const void * const * act ,int n ,float * const * y ); 85/// The same with tier t (the scalar one when the CPU does not run t, or k % 256 != 0); pf: bytes prefetched ahead. 86void mx_rows_tier (int t ,const uint8_t * w ,size_t rb ,int k ,int r0 ,int r1 ,const void * const * act ,int n , 87float * const * y ,int pf = 0 ); 88int mx_tier ();// the tier mx_rows takes (measured on first use: the fastest a core, ds4_gguf_cpu.cpp) 89const char * mx_tier_report ();// the measurement behind it (each candidate's time), or how it was picked 90bool mx_tier_ok (int t );// this CPU runs tier t 91const char * mx_tier_name (int t ); 92/// Whether the rows of `type` are ds4's own here (IQ3_XXS / IQ3_S on AVX2 / AVX-512 VBMI: ggml-cpu's numbers bit for 93/// bit, checked against its dot on first use; DS4_IQ_OWN=0 turns them off) rather than ggml's / the GLM lane's. 94bool iq_cpu_own_rows (int type ); 95 96/// x rounded as the device's MXFP4 kernels round their activations (q8_quant: per 32 values sc = max|x| / 127, q = 97/// round-half-even(x / sc), the same F32 operations, so the same q), into out as q sc - an expert gives the same sums 98/// on the CPU lane as in VRAM (up to their order), whichever side the expert cache puts it on. 99void q8_round (const float * x ,int k ,float * out ); 100 101}// namespace strata::ds4::gguf