okami.mom/strata

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

waveringanaedge0-strata: add the engine for Edge0 145B and Edge0 120Bfd19246dc

main
7.4 KiB170 linesraw
1// lat_q3k_batch_self_test.cpp - L2a of the q3k acceptance sheet: the batch / row / blob assembly.
2//
3// Same inputs, two ways of cutting them: every routed expert in one WideBatch per kMaxBatch (the engine's walk,
4// resident blobs) versus one WideBatch per expert.  The outputs must be bit-identical: if they are not, the
5// tables, the placement rows (off[]) or the blob-by-expert-id mapping is wrong, and no amount of comparing the
6// engine's rows against the native path can say which.
7//
8// The inputs are the engine's own: g-cnt/g-off/g-tok and d-xlq dumped from a real layer 0, and the pack's layer 0
9// blobs.  Nothing here runs the engine.
10#include <cstdio>
11#include <cmath>
12#include <cstdlib>
13#include <cstring>
14#include <string>
15#include <vector>
16
17#include <cuda_runtime.h>
18
19#include "strata/prefill/moe_fused.hpp"
20#include "strata/prefill/moe_fused_q3k.hpp"
21
22namespace fused = strata::prefill::fused;
23namespace fused_q3k = strata::prefill::fused_q3k;
24
25namespace {
26constexpr int64_t kEll = 1280, kIff = 640;
27constexpr size_t kStride = 1164800;            // one q3k expert's blob (gu 640*550 + up 352000 + down 640*360)
28const char* kDump = "/home/user/lat-s26/q3k/dumps4k";            // g-cnt.bin g-off.bin g-tok.bin d-xlq.bin
29const char* kPack = "/home/user/models/v7_l1280_q3k_pack/latent_a_pack/experts.bin";
30constexpr int kLayer = 0;                      // native_experts.txt's first line: offset 0
31
32std::vector<uint8_t> slurp(const std::string& p) {
33    std::vector<uint8_t> v;
34    if (std::FILE* f = std::fopen(p.c_str(), "rb")) {
35        std::fseek(f, 0, SEEK_END);
36        v.resize((size_t) std::ftell(f));
37        std::fseek(f, 0, SEEK_SET);
38        if (std::fread(v.data(), 1, v.size(), f) != v.size()) v.clear();
39        std::fclose(f);
40    }
41    return v;
42}
43
44void ck(cudaError_t e, const char* what) {
45    if (e != cudaSuccess) {
46        std::fprintf(stderr, "FAIL %s: %s\n", what, cudaGetErrorString(e));
47        std::exit(2);
48    }
49}
50}  // namespace
51
52int main() {
53    auto cnt_b = slurp(std::string(kDump) + "/g-cnt.bin");
54    auto off_b = slurp(std::string(kDump) + "/g-off.bin");
55    auto tok_b = slurp(std::string(kDump) + "/g-tok.bin");
56    auto xlq_b = slurp(std::string(kDump) + "/d-xlq.bin");
57    if (cnt_b.empty() || off_b.empty() || tok_b.empty() || xlq_b.empty()) {
58        std::fprintf(stderr, "FAIL: the layer 0 dumps are missing under %s\n", kDump);
59        return 2;
60    }
61    const int E = (int) (cnt_b.size() / 4);
62    const int32_t* cnt = (const int32_t*) cnt_b.data();
63    const int32_t* off = (const int32_t*) off_b.data();
64    const int32_t* tok = (const int32_t*) tok_b.data();
65    const int64_t n = (int64_t) (tok_b.size() / 4);       // T * K entries
66    const size_t xrow = (size_t) kEll / 32 * 36;
67    std::printf("L2a: %d experts, %lld entries, xlq %zu rows of %zu B, off[E] %d\n", E, (long long) n,
68                xlq_b.size() / xrow, xrow, off[E]);
69    if (off[E] != n) {
70        std::fprintf(stderr, "FAIL: off[E] %d != T*K %lld (the placement does not cover the entries)\n", off[E],
71                     (long long) n);
72        return 2;
73    }
74
75    // the pack's layer 0: whole experts, one stride each
76    std::vector<uint8_t> blobs((size_t) E * kStride);
77    {
78        std::FILE* f = std::fopen(kPack, "rb");
79        if (f == nullptr) {
80            std::fprintf(stderr, "FAIL: cannot open %s\n", kPack);
81            return 2;
82        }
83        std::fseek(f, (long) kLayer * 512 * (long) kStride, SEEK_SET);
84        const size_t got = std::fread(blobs.data(), 1, blobs.size(), f);
85        std::fclose(f);
86        if (got != blobs.size()) {
87            std::fprintf(stderr, "FAIL: the pack gave %zu of %zu B\n", got, blobs.size());
88            return 2;
89        }
90    }
91
92    // the device side: the tables, the H rows, the xl rows, the blobs and two sets of output rows
93    const size_t tables_b = fused::latent_group_bytes(n, E);
94    const size_t scratch_b = fused::latent_scratch_bytes(n, E, kIff);
95    const size_t lat_h = fused::latent_h_off(n, E);
96    std::vector<char> tables(tables_b);
97    fused::group_latent(cnt, off, E, tables.data(), tables.size());
98    uint8_t* scratch_d = nullptr;
99    uint8_t* xlq_d = nullptr;
100    uint8_t* blobs_d = nullptr;
101    int32_t* tok_d = nullptr;
102    float *yA = nullptr, *yB = nullptr;
103    ck(cudaMalloc(&scratch_d, scratch_b), "scratch");
104    ck(cudaMalloc(&xlq_d, xlq_b.size()), "xlq");
105    ck(cudaMalloc(&blobs_d, blobs.size()), "blobs");
106    ck(cudaMalloc(&tok_d, (size_t) n * 4), "tok");
107    ck(cudaMalloc(&yA, (size_t) n * kEll * 4), "yA");
108    ck(cudaMalloc(&yB, (size_t) n * kEll * 4), "yB");
109    ck(cudaMemcpy(scratch_d, tables.data(), tables_b, cudaMemcpyHostToDevice), "tables");
110    ck(cudaMemcpy(xlq_d, xlq_b.data(), xlq_b.size(), cudaMemcpyHostToDevice), "xlq");
111    ck(cudaMemcpy(blobs_d, blobs.data(), blobs.size(), cudaMemcpyHostToDevice), "blobs");
112    ck(cudaMemcpy(tok_d, tok, (size_t) n * 4, cudaMemcpyHostToDevice), "tok");
113    ck(cudaMemset(yA, 0, (size_t) n * kEll * 4), "yA zero");
114    ck(cudaMemset(yB, 0, (size_t) n * kEll * 4), "yB zero");
115    const fused::LatentGeom lg{kEll, kIff, 550, 360, 352000, 704000, 0};
116
117    // A: the engine's walk - kMaxBatch experts a launch, every blob of the layer present
118    std::printf("L2a: A = batches of %d experts, B = one expert a launch\n", (int) fused::kMaxBatch);
119    for (int e0 = 0; e0 < E; e0 += (int) fused::kMaxBatch) {
120        fused::WideBatch fb;
121        fb.e0 = e0;
122        fb.e1 = std::min(E, e0 + (int) fused::kMaxBatch);
123        for (int e = fb.e0; e < fb.e1; ++e) fb.blob[e - fb.e0] = blobs_d + (size_t) e * kStride;
124        fused_q3k::experts_latent_q3k(fb, lg, E, n, scratch_d, xlq_d, tok_d, scratch_d + lat_h, yA, nullptr);
125    }
126    ck(cudaDeviceSynchronize(), "A sync");
127
128    // B: the same experts, one launch each
129    for (int e = 0; e < E; ++e) {
130        if (cnt[e] <= 0) continue;
131        fused::WideBatch fb;
132        fb.e0 = e;
133        fb.e1 = e + 1;
134        fb.blob[0] = blobs_d + (size_t) e * kStride;
135        fused_q3k::experts_latent_q3k(fb, lg, E, n, scratch_d, xlq_d, tok_d, scratch_d + lat_h, yB, nullptr);
136    }
137    ck(cudaDeviceSynchronize(), "B sync");
138
139    // compare row for row
140    std::vector<float> hA((size_t) n * kEll), hB((size_t) n * kEll);
141    ck(cudaMemcpy(hA.data(), yA, hA.size() * 4, cudaMemcpyDeviceToHost), "yA back");
142    ck(cudaMemcpy(hB.data(), yB, hB.size() * 4, cudaMemcpyDeviceToHost), "yB back");
143    size_t bad_rows = 0, bad_vals = 0, first = (size_t) -1;
144    double worst = 0;
145    for (int64_t r = 0; r < n; ++r) {
146        bool bad = false;
147        for (int64_t c = 0; c < kEll; ++c) {
148            const size_t i = (size_t) r * kEll + (size_t) c;
149            if (hA[i] != hB[i]) {
150                ++bad_vals;
151                bad = true;
152                const double d = std::fabs((double) hA[i] - (double) hB[i]);
153                if (d > worst) worst = d;
154            }
155        }
156        if (bad) {
157            ++bad_rows;
158            if (first == (size_t) -1) first = (size_t) r;
159        }
160    }
161    std::printf("L2a rows %lld: differing rows %zu, differing values %zu, first row %lld, max|d| %.3e\n",
162                (long long) n, bad_rows, bad_vals, (long long) first, worst);
163    if (bad_rows == 0) {
164        std::printf("L2a PASS: the batch and the single-expert launches agree bit for bit\n");
165    } else {
166        std::printf("L2a FAIL: the batch assembly changes the result (%zu rows)\n", bad_rows);
167    }
168    cudaFree(scratch_d); cudaFree(xlq_d); cudaFree(blobs_d); cudaFree(tok_d); cudaFree(yA); cudaFree(yB);
169    return bad_rows == 0 ? 0 : 1;
170}