okami.mom/strata

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

Niko1221Merge f-0216-cpu into f-0216-allf2eedfe9f

main
9.0 KiB167 linesraw
1// src/kernels/cpu/cpu_dispatch_dump.cpp - round 0216: what the CPU pool's expert calls (native_gu_rows + native_down_rows, both
2// arithmetics) put out, as a hash per (pair, token count, row range).  Run the same binary with a change switched off and on (or a
3// baseline build and a candidate build) and compare the lines: an E change (bit-exact against the code it replaces) gives the same text.
4//
5//     cpu_dispatch_dump [--pairs iq4_xs/q5_0,...] [--nt-max 8] [--experts 3]
6//
7// For every pair: --experts random experts (ggml-quantized random weights, valid scales), nt = 1..nt-max tokens, whole rows and
8// row ranges that start and end off any vector boundary (tails), the Default and the WindowInvariant entry points.
9#include "strata/kernels/cpu/expert_layout.hpp"
10#include "strata/kernels/cpu/native_expert.hpp"
11
12#include "ggml.h"
13#include "ggml-cpu.h"
14
15#include <cmath>
16#include <cstdint>
17#include <cstdio>
18#include <cstdlib>
19#include <cstring>
20#include <random>
21#include <string>
22#include <vector>
23
24namespace cpu = strata::kernels::cpu;
25
26namespace {
27
28constexpr int64_t kH = 2560, kFF = 640;
29constexpr int kMaxT = 8;
30
31int type_of(const std::string& s) {
32    for (int t = 0; t < GGML_TYPE_COUNT; ++t) {
33        const char* n = ggml_type_name((ggml_type) t);
34        if (n != nullptr && s == n) return t;
35    }
36    return -1;
37}
38
39std::vector<std::string> split(const std::string& s) {
40    std::vector<std::string> v;
41    size_t from = 0;
42    for (;;) {
43        const size_t c = s.find(',', from);
44        v.push_back(s.substr(from, c == std::string::npos ? std::string::npos : c - from));
45        if (c == std::string::npos) break;
46        from = c + 1;
47    }
48    return v;
49}
50
51uint64_t fnv(uint64_t h, const void* p, size_t n) {
52    const uint8_t* b = (const uint8_t*) p;
53    for (size_t i = 0; i < n; ++i) h = (h ^ b[i]) * 1099511628211ull;
54    return h;
55}
56
57}  // namespace
58
59int main(int argc, char** argv) {
60    setvbuf(stdout, nullptr, _IONBF, 0);
61    std::vector<std::string> pairs = split("iq3_xxs/q5_0,iq3_xxs/iq4_nl,iq4_xs/q5_0,iq4_xs/iq4_nl,iq3_s/iq4_nl,iq2_s/iq4_nl,iq4_xs/q8_0,iq2_s/q2_0,iq3_xxs/q2_0");
62    int nt_max = 8, experts = 3;
63    for (int i = 1; i < argc; ++i) {
64        const std::string a = argv[i];
65        if (a == "--pairs" && i + 1 < argc) pairs = split(argv[++i]);
66        else if (a == "--nt-max" && i + 1 < argc) nt_max = std::atoi(argv[++i]);
67        else if (a == "--experts" && i + 1 < argc) experts = std::atoi(argv[++i]);
68        else { std::fprintf(stderr, "usage: cpu_dispatch_dump [--pairs a/b,...] [--nt-max 8] [--experts 3]\n"); return 2; }
69    }
70    ggml_cpu_init();
71    for (const std::string& pair : pairs) {
72        const size_t slash = pair.find('/');
73        const int gt = slash == std::string::npos ? -1 : type_of(pair.substr(0, slash));
74        const int dt = slash == std::string::npos ? -1 : type_of(pair.substr(slash + 1));
75        cpu::NativeFmt f;
76        std::string err;
77        if (gt < 0 || dt < 0 || !cpu::native_fmt(gt, dt, kH, kFF, f, err)) {
78            std::printf("%s: %s\n", pair.c_str(), err.empty() ? "unknown types" : err.c_str());
79            continue;
80        }
81        ggml_quantize_init((ggml_type) gt);
82        ggml_quantize_init((ggml_type) dt);
83        std::mt19937 rng(4321 + gt * 64 + dt);
84        std::normal_distribution<float> nd(0.f, 0.02f), xd(0.f, 1.f);
85        std::vector<float> row((size_t) std::max(kH, kFF));
86        const std::vector<float> imatrix((size_t) std::max(kH, kFF), 1.0f);
87        auto quant = [&](int type, uint8_t* dst, int64_t n) {
88            ggml_quantize_chunk((ggml_type) type, row.data(), dst, 0, 1, n, ggml_quantize_requires_imatrix((ggml_type) type) ? imatrix.data() : nullptr);
89        };
90        std::vector<uint8_t> blob(f.bytes + 64);
91        std::vector<std::vector<uint8_t>> xa(kMaxT, std::vector<uint8_t>(cpu::kNativeActBytes)), ha(kMaxT, std::vector<uint8_t>(cpu::kNativeHBytes));
92        std::vector<std::vector<float>> ffv(kMaxT, std::vector<float>(kFF)), outv(kMaxT, std::vector<float>(kH));
93        const void* act[kMaxT];
94        const void* hq[kMaxT];
95        float* ff[kMaxT];
96        float* out[kMaxT];
97        for (int t = 0; t < kMaxT; ++t) { act[t] = xa[(size_t) t].data(); hq[t] = ha[(size_t) t].data(); ff[t] = ffv[(size_t) t].data(); out[t] = outv[(size_t) t].data(); }
98        // a Q2_0 down (or gate/up) takes the pool's own path (pool.cpp): the activation as an ActQ and Strata's Q2_0 rows, not ggml's dot
99        const bool q2_down = cpu::q2_native_kernels(f.d_type), q2_gu = cpu::q2_native_kernels(f.gu_type);
100        std::vector<cpu::ActQ> h2(q2_down ? kMaxT : 0), x2(q2_gu ? kMaxT : 0);
101        const cpu::ActQ* a2[kMaxT] = {};
102        const cpu::ActQ* ax[kMaxT] = {};
103        std::vector<std::vector<float>> gv(q2_gu ? kMaxT : 0, std::vector<float>(kFF)), uv(q2_gu ? kMaxT : 0, std::vector<float>(kFF));
104        float* gp2[kMaxT] = {};
105        float* up2[kMaxT] = {};
106        uint64_t total = 1469598103934665603ull;
107        for (int e = 0; e < experts; ++e) {
108            for (int64_t r = 0; r < 2 * kFF; ++r) {
109                for (int64_t i = 0; i < kH; ++i) row[(size_t) i] = nd(rng);
110                quant(f.gu_type, blob.data() + (size_t) r * f.gu_row, kH);
111            }
112            for (int64_t r = 0; r < kH; ++r) {
113                for (int64_t i = 0; i < kFF; ++i) row[(size_t) i] = nd(rng);
114                quant(f.d_type, blob.data() + f.down_off + (size_t) r * f.d_row, kFF);
115            }
116            for (int t = 0; t < kMaxT; ++t) {
117                std::vector<float> x(kH), h(kFF);
118                for (auto& v : x) v = xd(rng);
119                for (auto& v : h) v = 0.3f * xd(rng);
120                if (q2_gu) { cpu::act_quant_any(x.data(), (int) kH, x2[(size_t) t]); ax[t] = &x2[(size_t) t]; gp2[t] = gv[(size_t) t].data(); up2[t] = uv[(size_t) t].data(); }
121                else cpu::native_quant_act(f, x.data(), xa[(size_t) t].data());
122                if (q2_down) { cpu::act_quant_any(h.data(), (int) kFF, h2[(size_t) t]); a2[t] = &h2[(size_t) t]; }
123                else cpu::native_quant_h(f, h.data(), ha[(size_t) t].data());
124            }
125            for (int nt = 1; nt <= nt_max && nt <= kMaxT; ++nt) {
126                // whole range, then a range starting and ending off any 8/16/64 boundary
127                const int ranges[2][4] = {{0, (int) kFF, 0, (int) kH}, {3, (int) kFF - 5, 7, (int) kH - 9}};
128                for (int inv = 0; inv < 2; ++inv) {
129                    for (int rg = 0; rg < 2; ++rg) {
130                        for (auto& v : ffv) std::fill(v.begin(), v.end(), 0.f);
131                        for (auto& v : outv) std::fill(v.begin(), v.end(), 0.f);
132                        if (q2_gu || q2_down) {
133                            if (inv) continue;   // the Q2_0 rows have one arithmetic
134                            if (q2_gu) {
135                                cpu::q2_rows_any(blob.data(), f.gu_row, (int) (kH / 64), ax, nt, gp2, ranges[rg][0], ranges[rg][1]);
136                                cpu::q2_rows_any(blob.data() + f.up_off, f.gu_row, (int) (kH / 64), ax, nt, up2, ranges[rg][0], ranges[rg][1]);
137                                for (int t = 0; t < nt; ++t)
138                                    for (int r = ranges[rg][0]; r < ranges[rg][1]; ++r)
139                                        ffv[(size_t) t][(size_t) r] = (gp2[t][r] / (1.f + std::exp(-gp2[t][r]))) * up2[t][r];
140                            } else {
141                                cpu::native_gu_rows(f, blob.data(), act, nt, ff, ranges[rg][0], ranges[rg][1]);
142                            }
143                            if (q2_down) cpu::q2_rows_any(blob.data() + f.down_off, f.d_row, (int) (f.n_ff / 64), a2, nt, out, ranges[rg][2], ranges[rg][3]);
144                            else cpu::native_down_rows(f, blob.data(), hq, nt, out, ranges[rg][2], ranges[rg][3]);
145                        } else if (inv) {
146                            cpu::native_gu_rows_invariant(f, blob.data(), act, nt, ff, ranges[rg][0], ranges[rg][1]);
147                            cpu::native_down_rows_invariant(f, blob.data(), hq, nt, out, ranges[rg][2], ranges[rg][3]);
148                        } else {
149                            cpu::native_gu_rows(f, blob.data(), act, nt, ff, ranges[rg][0], ranges[rg][1]);
150                            cpu::native_down_rows(f, blob.data(), hq, nt, out, ranges[rg][2], ranges[rg][3]);
151                        }
152                        uint64_t hg = 1469598103934665603ull, hd = 1469598103934665603ull;
153                        for (int t = 0; t < nt; ++t) {
154                            hg = fnv(hg, ffv[(size_t) t].data(), sizeof(float) * (size_t) kFF);
155                            hd = fnv(hd, outv[(size_t) t].data(), sizeof(float) * (size_t) kH);
156                        }
157                        total = fnv(fnv(total, &hg, 8), &hd, 8);
158                        std::printf("%s e%d nt%d %s range%d gu %016llx down %016llx\n", pair.c_str(), e, nt, inv ? "inv" : "def", rg,
159                                    (unsigned long long) hg, (unsigned long long) hd);
160                    }
161                }
162            }
163        }
164        std::printf("%s TOTAL %016llx\n", pair.c_str(), (unsigned long long) total);
165    }
166    return 0;
167}