okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
d39d20f52
main
1// tools/vision/own/cpu_tier.h - what a kernel tier of the CPU tower sees: the kernel table type, the tier getters, and the few out-of-line helpers the tiers call. 2// 3// The tier files cpu_kernels_avx2.cpp and cpu_kernels_avx512.cpp are compiled with wider instruction-set flags than the rest of the program. An inline function 4// with external linkage (a C++ standard-library template, an inline helper of another header) compiled there can be the copy the linker keeps for the WHOLE 5// program, and then a CPU without that instruction set dies in code that has nothing to do with the tier. So those files, and cpu_kernels_impl.h, include only 6// this header (plain C headers) and <immintrin.h>, keep everything of their own in an anonymous namespace (internal linkage), and call library code only through 7// the out-of-line functions declared here, which are compiled at the baseline (cpu_ops.cpp). 8#pragma once 9 10#include <stddef.h> 11#include <stdint.h> 12 13namespace strata_vit { 14 15struct CpuOps { 16const char * name ; 17int MR ,NR ;// the register tile of the product: MR rows of A against NR columns of B 18 19// Packs the weight rows n0 .. n0 + nvalid (nvalid <= NR; the rest of the NR columns are zero) of W[N][K] (row-major, row stride ldw elements), columns 20// k0 .. k0 + kc, into dst[kc][NR] float32. kind: 0 float32, 1 BF16, 2 F16 (W points at elements of that type). 21void (* pack_w )(const void * W ,int kind ,size_t ldw ,int n0 ,int nvalid ,int k0 ,int kc ,float * dst ); 22 23// n contiguous 16-bit weights (kind 1 BF16, 2 F16) to float32 (bit exact). The tower keeps its matrices as 16-bit panels [tile][K][NR] made once at load, 24// and a product task converts the panel it needs into a float buffer: no transposition in the hot path. 25void (* conv )(const void * src ,int kind ,size_t n ,float * dst ); 26 27// C[M][N] = init-mode + A[M][K] * B[K][N]; A rows lda apart, B rows ldb apart (B must be readable up to the next multiple of NR columns), C rows ldc apart. 28// init 0: C = bias (or zero) + A*B; 1: C += A*B; 2: C = C + bias + A*B. bias: N floats or null. 29void (* gemm )(const float * A ,size_t lda ,int M ,const float * B ,size_t ldb ,int K ,int N ,float * C ,size_t ldc ,int init ,const float * bias ); 30 31// x = gelu_tanh(x) on rows x n of a matrix with row stride ldx 32void (* gelu )(float * x ,size_t ldx ,int rows ,int n ); 33// y = LayerNorm(x) * w + b for one row of D 34void (* layernorm )(const float * x ,float * y ,int D ,const float * w ,const float * b ,float eps ); 35// x[i] += y[i], i < n 36void (* add )(float * x ,const float * y ,size_t n ); 37// p[i] rounded to the 16-bit type and back (kind 1 BF16, 2 F16; round to nearest even): STRATA_VIT_REF_ROUND, the inputs of the products as ggml's CPU path hands them 38void (* round16 )(float * p ,size_t n ,int kind ); 39// One key tile of the online softmax for qrows query rows: S[qrows][nkeys] (row stride lds, already scaled) becomes P = exp(S - m_new) in place, the running 40// max m[r] and sum l[r] are updated and the context rows O[r][0..hd) (row stride ldo) are rescaled by exp(m_old - m_new) (O's P*V is added afterwards). 41void (* softmax_update )(float * S ,size_t lds ,int qrows ,int nkeys ,float * m ,float * l ,float * O ,size_t ldo ,int hd ); 42// The two loops of the tower whose floating-point result depends on the compiler's multiply-add contraction, so they are compiled with each tier's flags 43// (vit_cpu.cpp itself is compiled for the baseline CPU, RESULTS-OWN-3V.md PART H): the rotary of one token row (q at row[0 .. heads*hd), k at 44// row[kstride ..); pair j of a head turns by (c1, s1)[j] for j < hd/4, else by (c2, s2)[j - hd/4]), and the position table's bilinear add of one row. 45void (* rope_row )(float * row ,size_t kstride ,int heads ,int hd ,const float * c1 ,const float * s1 ,const float * c2 ,const float * s2 ); 46void (* pos_add )(float * xr ,const float * a ,const float * b ,const float * c ,const float * d ,float dx ,float dy ,int D ); 47}; 48 49const CpuOps & cpu_ops_scalar (); 50const CpuOps & cpu_ops_sse41 ();// x86-64 only (both return the scalar table elsewhere): the tiers for CPUs without AVX2, multiply then add (no FMA) 51const CpuOps & cpu_ops_avx (); 52const CpuOps & cpu_ops_avx2 ();// defined only on x86-64 (cpu_ops_avx2 returns the scalar table elsewhere) 53const CpuOps & cpu_ops_avx512 (); 54const CpuOps & cpu_ops_neon ();// AArch64 only (cpu_ops_neon returns the scalar table elsewhere) 55 56// expf of the C library, out of line and compiled at the baseline (cpu_ops.cpp): the scalar tails of the tiers' softmax 57float cpu_tier_expf (float x ); 58 59// false when STRATA_VIT_CPU_TAIL1=0 (a diagnostic): the raw-vector tiers then take a last column tile of one vector or less through the two-vector product as 60// before; out of line, compiled at the baseline (cpu_ops.cpp), read once 61bool cpu_tier_one_vector_tail (); 62 63}// namespace strata_vit