okami.mom/strata

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

Niko1221Merge f-141-quick: CPU prefill share, file tier batched reads, tokenizer cache, server hardening, small fixes073e2fe28

main
5.4 KiB118 linesraw
1// src/kernels/cpu/expert_multi_test.cpp - plan v0.3 P6: one expert, several tokens (CPU only, synthetic data).
2//
3//   expert_multi_test              bitwise check: s2_expert_vnni_multi vs s2_expert_vnni_q per token, n = 1..8
4//   expert_multi_test --bench [E]  time per expert for n = 1..5 over E synthetic experts (default 256 = 354 MB,
5//                                  far beyond L3, so the blob comes from DRAM as it does in the engine); 1 thread
6//
7// The bench measures the speculation model's `extra_use_cost`: the CPU time of each extra token routed to an
8// expert, as a fraction of reading the expert once (tools/spec_economics.py assumes 0.2).
9#include "strata/kernels/cpu/expert.hpp"
10
11#include <chrono>
12#include <cstdio>
13#include <cstdlib>
14#include <cstring>
15#include <random>
16#include <vector>
17
18namespace c = strata::kernels::cpu;
19
20namespace {
21// the pool's path: the gate/up rows, the second activation quantization, the down rows (what the engine's expert
22// jobs run), for one expert and n tokens
23void rows_path(const uint8_t* blob, const c::ActQ* const* a1, int n, float* const* out, c::ExpertScratchMulti& ws) {
24    float* ffp[c::MAXT];
25    const c::ActQ* a2[c::MAXT];
26    for (int t = 0; t < n; ++t) ffp[t] = ws.ff[t];
27    c::s2_expert_gu_rows_multi(blob, a1, n, ffp, 0, c::FF);
28    for (int t = 0; t < n; ++t) {
29        c::act_quant_q8_1(ws.ff[t], c::FF, ws.a2[t]);
30        a2[t] = &ws.a2[t];
31    }
32    c::s2_expert_down_rows_multi(blob, a2, n, out, 0, c::H);
33}
34
35void make_blob(uint8_t* b, std::mt19937& rng) {
36    for (size_t i = 0; i < c::O_GU_SCALES; ++i) b[i] = (uint8_t) rng();              // codes
37    for (size_t i = c::O_GU_SCALES; i < c::BLOB; i += 2) {                            // fp16 scales ~0.004-0.016
38        const uint16_t h = (uint16_t) (0x1C00 + rng() % 0x0800);
39        std::memcpy(b + i, &h, 2);
40    }
41}
42
43double now_ms() {
44    return std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now().time_since_epoch()).count();
45}
46}  // namespace
47
48int main(int argc, char** argv) {
49    if (const c::CpuFeatures feat = c::cpu_features(); !feat.usable()) {   // the AVX-512 kernel: nothing to test here
50        std::printf("expert_multi_test: CPU lacks %s: SKIPPED\n", feat.reason());
51        return 77;
52    }
53    std::mt19937 rng(9);
54    const bool bench = argc > 1 && std::strcmp(argv[1], "--bench") == 0;
55    const int E = bench ? (argc > 2 ? std::atoi(argv[2]) : 256) : 4;
56    std::vector<uint8_t> blobs((size_t) E * c::BLOB);
57    for (int e = 0; e < E; ++e) make_blob(&blobs[(size_t) e * c::BLOB], rng);
58    std::normal_distribution<float> nd(0.f, 1.f);
59    static c::ActQ acts[c::MAXT];
60    static float xs[c::MAXT][c::H];
61    for (int t = 0; t < c::MAXT; ++t) {
62        for (float& v : xs[t]) v = nd(rng);
63        c::act_quant_q8_1(xs[t], c::H, acts[t]);
64    }
65    const c::ActQ* a1[c::MAXT];
66    for (int t = 0; t < c::MAXT; ++t) a1[t] = &acts[t];
67    static float out_multi[c::MAXT][c::H], out_single[c::H];
68    float* outs[c::MAXT];
69    for (int t = 0; t < c::MAXT; ++t) outs[t] = out_multi[t];
70    static c::ExpertScratchMulti wsm;
71    static c::ExpertScratch ws;
72
73    if (!bench) {
74        int fail = 0;
75        for (int e = 0; e < E; ++e) {
76            const uint8_t* blob = &blobs[(size_t) e * c::BLOB];
77            for (int n = 1; n <= c::MAXT; ++n) {
78                c::s2_expert_vnni_multi(blob, a1, n, outs, wsm);
79                for (int t = 0; t < n; ++t) {
80                    c::s2_expert_vnni_q(blob, acts[t], out_single, ws);
81                    if (std::memcmp(out_single, out_multi[t], sizeof out_single) != 0) {
82                        std::fprintf(stderr, "FAIL expert %d n=%d token %d differs\n", e, n, t);
83                        ++fail;
84                    }
85                }
86                static float out_rows[c::MAXT][c::H];
87                float* orow[c::MAXT];
88                for (int t = 0; t < c::MAXT; ++t) orow[t] = out_rows[t];
89                rows_path(blob, a1, n, orow, wsm);
90                for (int t = 0; t < n; ++t)
91                    if (std::memcmp(out_rows[t], out_multi[t], sizeof out_rows[t]) != 0) {
92                        std::fprintf(stderr, "FAIL expert %d n=%d token %d: rows path differs from the multi kernel\n", e, n, t);
93                        ++fail;
94                    }
95            }
96        }
97        std::printf("expert_multi_test: %s\n", fail ? "FAILED" : "OK (every token bitwise equal to the single-token kernel)");
98        return fail ? 1 : 0;
99    }
100
101    std::printf("%d experts x %.2f MB, one thread\n", E, c::BLOB / 1e6);
102    double base = 0;
103    for (int n = 1; n <= 5; ++n) {
104        c::s2_expert_vnni_multi(&blobs[0], a1, n, outs, wsm);                     // warm the code path
105        const double t0 = now_ms();
106        for (int e = 0; e < E; ++e) c::s2_expert_vnni_multi(&blobs[(size_t) e * c::BLOB], a1, n, outs, wsm);
107        const double us = 1000.0 * (now_ms() - t0) / E;
108        if (n == 1) base = us;
109        std::printf("tokens per expert %d: %7.1f us per expert (%.2fx one token; extra token = %.2f of one read), %.1f GB/s\n",
110                    n, us, us / base, n > 1 ? (us / base - 1.0) / (n - 1) : 0.0, c::BLOB / (us * 1e3));
111        rows_path(&blobs[0], a1, n, outs, wsm);
112        const double r0 = now_ms();
113        for (int e = 0; e < E; ++e) rows_path(&blobs[(size_t) e * c::BLOB], a1, n, outs, wsm);
114        const double rus = 1000.0 * (now_ms() - r0) / E;
115        std::printf("   rows path (the pool's): %7.1f us per expert, %.1f GB/s\n", rus, c::BLOB / (rus * 1e3));
116    }
117    return 0;
118}