okami.mom/strata

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

Niko1221Merge branch 'f-132-amd' into rel-0.1.32aebfd69a2

main
4.8 KiB101 linesraw
1// S6: the AMD fast router (router_top10's default on HIP for 64 < n_expert <= 512, k <= 32) against the portable
2// kernel, BITWISE: ids and weights of 65,536 rows of 512 logits - normal rows, rows with many exact ties, near-ties,
3// a wide spread (tiny probabilities), all-equal rows and rows with a NaN - plus the fast kernel forced onto its
4// serial-sum path, and a 256-expert / k=8 geometry. Then the time per single-token call of each.
5#include <hip/hip_runtime.h>
6#include "strata/kernels/router_top10.hpp"
7
8#include <cmath>
9#include <cstdio>
10#include <cstring>
11#include <random>
12#include <vector>
13
14#define CHECK(call)                                                                                                  \
15    do {                                                                                                             \
16        const hipError_t error = (call);                                                                             \
17        if (error != hipSuccess) {                                                                                   \
18            std::fprintf(stderr, "%s: %s\n", #call, hipGetErrorString(error));                                     \
19            return 2;                                                                                                \
20        }                                                                                                            \
21    } while (0)
22
23namespace k = strata::kernels;
24
25static int compare(int n_expert, int topk, int rows, unsigned seed) {
26    std::mt19937 rng(seed);
27    std::normal_distribution<float> nd(0.f, 2.f);
28    std::vector<float> lg((size_t) rows * n_expert);
29    for (size_t i = 0; i < lg.size(); ++i) {
30        const size_t r = i / n_expert;
31        float x = nd(rng) * (r % 7 == 5 ? 40.0f : 1.0f);
32        if (r % 3 == 1) x = std::round(x * 4.0f) / 4.0f;
33        if (r % 3 == 2) x = std::round(x * 64.0f) / 64.0f;
34        if (r % 1000 == 999 && (i % n_expert) == 77) x = NAN;
35        if (r % 1000 == 998) x = 1.0f;
36        lg[i] = x;
37    }
38    const size_t out = (size_t) rows * topk;
39    float *d_l = nullptr, *d_w[3] = {};
40    int* d_i[3] = {};
41    CHECK(hipMalloc(&d_l, lg.size() * 4));
42    CHECK(hipMemcpy(d_l, lg.data(), lg.size() * 4, hipMemcpyHostToDevice));
43    std::vector<int> ids[3];
44    std::vector<float> w[3];
45    for (int v = 0; v < 3; ++v) {
46        CHECK(hipMalloc(&d_w[v], out * 4));
47        CHECK(hipMalloc(&d_i[v], out * 4));
48        CHECK(hipMemset(d_w[v], 0x3c, out * 4));   // what an unwritten rank keeps (NaN rows)
49        CHECK(hipMemset(d_i[v], 0x11, out * 4));
50        if (!k::router_top10_variant(d_l, rows, n_expert, topk, d_i[v], d_w[v], nullptr, v)) {
51            std::fprintf(stderr, "router_top10_variant %d refused\n", v);
52            return 2;
53        }
54        CHECK(hipDeviceSynchronize());
55        ids[v].resize(out);
56        w[v].resize(out);
57        CHECK(hipMemcpy(ids[v].data(), d_i[v], out * 4, hipMemcpyDeviceToHost));
58        CHECK(hipMemcpy(w[v].data(), d_w[v], out * 4, hipMemcpyDeviceToHost));
59    }
60    int fails = 0;
61    for (int v = 1; v < 3; ++v) {
62        int bad = 0;
63        for (int r = 0; r < rows; ++r)
64            if (std::memcmp(&ids[0][(size_t) r * topk], &ids[v][(size_t) r * topk], topk * 4) ||
65                std::memcmp(&w[0][(size_t) r * topk], &w[v][(size_t) r * topk], topk * 4)) {
66                if (bad < 3)
67                    std::printf("  row %d differs: id0 %d/%d w0 %.9g/%.9g\n", r, ids[0][(size_t) r * topk],
68                                ids[v][(size_t) r * topk], w[0][(size_t) r * topk], w[v][(size_t) r * topk]);
69                ++bad;
70            }
71        std::printf("%s %d experts, k %d, %d rows: %s vs the portable kernel: %d rows differ\n", bad ? "FAIL" : "PASS",
72                    n_expert, topk, rows, v == 1 ? "fast kernel" : "fast kernel, serial path", bad);
73        fails += bad != 0;
74    }
75    // time: single-token calls back to back
76    hipEvent_t e0, e1;
77    CHECK(hipEventCreate(&e0));
78    CHECK(hipEventCreate(&e1));
79    for (int v = 0; v < 2; ++v) {
80        const int reps = 2000;
81        CHECK(hipEventRecord(e0));
82        for (int i = 0; i < reps; ++i)
83            k::router_top10_variant(d_l + (size_t) (i % 64) * n_expert, 1, n_expert, topk, d_i[v], d_w[v], nullptr, v);
84        CHECK(hipEventRecord(e1));
85        CHECK(hipEventSynchronize(e1));
86        float ms = 0;
87        CHECK(hipEventElapsedTime(&ms, e0, e1));
88        std::printf("  %s: %.2f us per single-token call\n", v ? "fast" : "portable", 1000.0f * ms / reps);
89    }
90    for (int v = 0; v < 3; ++v) { (void) hipFree(d_w[v]); (void) hipFree(d_i[v]); }
91    (void) hipFree(d_l);
92    return fails;
93}
94
95int main() {
96    int fails = 0;
97    fails += compare(512, 10, 65536, 17);
98    fails += compare(256, 8, 8192, 5);
99    std::printf("FAILURES: %d\n", fails);
100    return fails ? 1 : 0;
101}