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// tests/ds4_cpu_bench.cpp - how fast does this CPU run DeepSeek-V4.1-Flash's EXL3 experts (5120 -> 2304 -> 5120, K 3)? 4// 5// ds4_cpu_bench [--threads N] [--node 0|1|-1] [--mode band|rows] [--experts N] [--per-call N] [--iters N] 6// 7// Random trellis words in memory bound to --node (-1: wherever the first touch puts them), --threads workers pinned to 8// that node's physical cores (CPU i is on node i % 2 here, CPUs 0..43 the physical cores), each call computing 9// --per-call experts' gate and up (all columns) from one activation: the time of a matrix pass and its GB/s. 10#include "strata/dsv41/exl3_cpu.hpp" 11#include "../../src/dsv41/ds4_pool.hpp" 12 13#include <sys/mman.h> 14#include <sys/syscall.h> 15#include <unistd.h> 16 17#include <chrono> 18#include <cstdio> 19#include <cstdlib> 20#include <cstring> 21#include <random> 22#include <string> 23#include <vector> 24 25namespace kc= strata::dsv41k::cpu; 26 27static void * node_alloc (size_t bytes ,int node ) { 28void * p = mmap (nullptr ,bytes ,PROT_READ |PROT_WRITE ,MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); 29if (p == MAP_FAILED )return nullptr ; 30madvise (p ,bytes ,MADV_HUGEPAGE ); 31if (node >=0 ) { 32unsigned long mask = 1ul <<node ; 33if (syscall (SYS_mbind ,p ,bytes ,2 /*MPOL_BIND*/ ,& mask ,64 ,0 )!= 0 ) std::perror ("mbind" ); 34 } 35return p ; 36} 37 38int main (int argc ,char ** argv ) { 39int threads = 22 ,node = 0 ,n_exp = 64 ,per_call = 2 ,iters = 40 ; 40 std::string mode = "rows" ; 41for (int i = 1 ;i + 1 < argc ;i += 2 ) { 42const std::string a = argv [i ]; 43if (a == "--threads" )threads = std::atoi (argv [i + 1 ]); 44else if (a == "--node" )node = std::atoi (argv [i + 1 ]); 45else if (a == "--mode" )mode = argv [i + 1 ]; 46else if (a == "--experts" )n_exp = std::atoi (argv [i + 1 ]); 47else if (a == "--per-call" )per_call = std::atoi (argv [i + 1 ]); 48else if (a == "--iters" )iters = std::atoi (argv [i + 1 ]); 49 } 50const int E = 5120 ,FF = 2304 ,K = 3 ; 51const size_t tb = (size_t )E /16 * FF /16 * 32 * K ;// one matrix's trellis 52 std::vector < int > cpus ; 53for (int c = 0 ;c < 44 ;++ c ) 54if (node < 0 || c %2 == node )cpus .push_back (c ); 55 strata::ds4::Pool pool (threads - 1 , std::vector < int > (cpus .begin ()+ 1 ,cpus .end ())); 56 { 57cpu_set_t set ; 58CPU_ZERO (& set ); 59CPU_SET (cpus [0 ],& set ); 60sched_setaffinity (0 ,sizeof set ,& set ); 61 } 62uint8_t * mem = (uint8_t * )node_alloc (tb * n_exp ,node ); 63 std::vector < uint16_t > suh (E ),svh (FF ); 64for (auto & v :suh )v = 0x3c00 ;// 1.0 65for (auto & v :svh )v = 0x3c00 ; 66pool .run (n_exp , [& ](int e ) { 67 std::mt19937 r (e ); 68uint32_t * w = (uint32_t * ) (mem + tb * e ); 69for (size_t i = 0 ;i < tb /4 ;++ i )w [i ]= r (); 70 }); 71 std::vector < float > x (E ,0.5f ),xh (E ),acc ((size_t )64 * FF ),y ((size_t )per_call * FF ); 72const float xs = 0.0f ; 73 kc::Exl3Part p0 ; 74p0 .suh = suh .data (); 75p0 .svh = svh .data (); 76p0 .k = E ; 77p0 .n = FF ; 78p0 .K = K ; 79 kc::exl3_cpu_prepare (p0 ,x .data (),xh .data ()); 80const int GQ = E /256 ;// rows mode: pieces of 256 inputs over every column 81 std::vector < float > accp ((size_t )per_call * GQ * FF ); 82auto t0 = std::chrono::steady_clock::now (); 83long long bytes = 0 ; 84for (int it = 0 ;it < iters ;++ it ) { 85const int base = (it * per_call ) % (n_exp - per_call + 1 ); 86if (mode == "band" ) { 87pool .run (per_call * (FF /128 ), [& ](int j ) { 88 kc::Exl3Part p = p0 ; 89p .trellis = (const uint32_t * ) (mem + tb * (base + j / (FF /128 ))); 90 kc::exl3_cpu_band (p ,xh .data (),xs , (j % (FF /128 ))* 128 , 91y .data ()+ (size_t ) (j / (FF /128 ))* FF + (j % (FF /128 ))* 128 ); 92 }); 93 }else { 94pool .run (per_call * GQ , [& ](int j ) { 95 kc::Exl3Part p = p0 ; 96p .trellis = (const uint32_t * ) (mem + tb * (base + j /GQ )); 97const int q = j %GQ ; 98 kc::exl3_cpu_rows_acc (p ,xh .data (),q * 16 , (q + 1 )* 16 ,accp .data ()+ (size_t )j * FF ); 99 }); 100 } 101bytes += (long long )tb * per_call ; 102 } 103const double ms = std::chrono::duration < double , std::milli > (std::chrono::steady_clock::now ()- t0 ).count (); 104 std::printf ("%s node %d threads %d per-call %d: %.3f ms per matrix, %.1f GB/s\n" ,mode .c_str (),node ,threads , 105per_call ,ms / (iters * per_call ),bytes /1e6 /ms ); 106return 0 ; 107}