okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

waveringanaRestore the DeepSeek V4.1 engine and CPU tests removed upstream8ac8ce32e

main
4.6 KiB107 linesraw
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) {
28    void* p = mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
29    if (p == MAP_FAILED) return nullptr;
30    madvise(p, bytes, MADV_HUGEPAGE);
31    if (node >= 0) {
32        unsigned long mask = 1ul << node;
33        if (syscall(SYS_mbind, p, bytes, 2 /*MPOL_BIND*/, &mask, 64, 0) != 0) std::perror("mbind");
34    }
35    return p;
36}
37
38int main(int argc, char** argv) {
39    int threads = 22, node = 0, n_exp = 64, per_call = 2, iters = 40;
40    std::string mode = "rows";
41    for (int i = 1; i + 1 < argc; i += 2) {
42        const std::string a = argv[i];
43        if (a == "--threads") threads = std::atoi(argv[i + 1]);
44        else if (a == "--node") node = std::atoi(argv[i + 1]);
45        else if (a == "--mode") mode = argv[i + 1];
46        else if (a == "--experts") n_exp = std::atoi(argv[i + 1]);
47        else if (a == "--per-call") per_call = std::atoi(argv[i + 1]);
48        else if (a == "--iters") iters = std::atoi(argv[i + 1]);
49    }
50    const int E = 5120, FF = 2304, K = 3;
51    const size_t tb = (size_t) E / 16 * FF / 16 * 32 * K;   // one matrix's trellis
52    std::vector<int> cpus;
53    for (int c = 0; c < 44; ++c)
54        if (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    {
57        cpu_set_t set;
58        CPU_ZERO(&set);
59        CPU_SET(cpus[0], &set);
60        sched_setaffinity(0, sizeof set, &set);
61    }
62    uint8_t* mem = (uint8_t*) node_alloc(tb * n_exp, node);
63    std::vector<uint16_t> suh(E), svh(FF);
64    for (auto& v : suh) v = 0x3c00;   // 1.0
65    for (auto& v : svh) v = 0x3c00;
66    pool.run(n_exp, [&](int e) {
67        std::mt19937 r(e);
68        uint32_t* w = (uint32_t*) (mem + tb * e);
69        for (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);
72    const float xs = 0.0f;
73    kc::Exl3Part p0;
74    p0.suh = suh.data();
75    p0.svh = svh.data();
76    p0.k = E;
77    p0.n = FF;
78    p0.K = K;
79    kc::exl3_cpu_prepare(p0, x.data(), xh.data());
80    const int GQ = E / 256;   // rows mode: pieces of 256 inputs over every column
81    std::vector<float> accp((size_t) per_call * GQ * FF);
82    auto t0 = std::chrono::steady_clock::now();
83    long long bytes = 0;
84    for (int it = 0; it < iters; ++it) {
85        const int base = (it * per_call) % (n_exp - per_call + 1);
86        if (mode == "band") {
87            pool.run(per_call * (FF / 128), [&](int j) {
88                kc::Exl3Part p = p0;
89                p.trellis = (const uint32_t*) (mem + tb * (base + j / (FF / 128)));
90                kc::exl3_cpu_band(p, xh.data(), xs, (j % (FF / 128)) * 128,
91                                  y.data() + (size_t) (j / (FF / 128)) * FF + (j % (FF / 128)) * 128);
92            });
93        } else {
94            pool.run(per_call * GQ, [&](int j) {
95                kc::Exl3Part p = p0;
96                p.trellis = (const uint32_t*) (mem + tb * (base + j / GQ));
97                const 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        }
101        bytes += (long long) tb * per_call;
102    }
103    const 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,
105                per_call, ms / (iters * per_call), bytes / 1e6 / ms);
106    return 0;
107}