okami.mom/strata

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

waveringanafill activation sums in latent projection fixtureseb8f0b710

main
3.8 KiB89 linesraw
1#include "strata/prefill/moe_fused_q3k.hpp"
2#include <cuda_runtime.h>
3#include <cuda_fp16.h>
4#include <cstdint>
5#include <cstdio>
6#include <cstdlib>
7#include <cstring>
8#include <vector>
9
10static void check(cudaError_t error) {
11    if (error != cudaSuccess) {
12        std::fprintf(stderr, "%s\n", cudaGetErrorString(error));
13        std::exit(1);
14    }
15}
16
17static void check_rows(bool same_inputs) {
18    constexpr int K = 2560, G = 8, ELL = 1792, M = 576;
19    constexpr size_t xrow = K / 32 * 36, yrow = G * ELL / 32 * 36;
20    std::vector<uint8_t> x(M * xrow);
21    for (int row = 0; row < M; ++row)
22        for (int block = 0; block < K / 32; ++block) {
23            uint8_t* p = x.data() + row * xrow + block * 36;
24            const uint16_t scale = 0x3000;
25            std::memcpy(p, &scale, 2);
26            int sum = 0;
27            for (int j = 0; j < 32; ++j) {
28                const int q = ((same_inputs ? 0 : row) + block + j) % 63 - 31;
29                p[4 + j] = (uint8_t) q;
30                sum += q;
31            }
32            const __half scaled_sum = __float2half_rn((float) sum * 0.125f);
33            std::memcpy(p + 2, &scaled_sum, 2);
34        }
35    void *dx = nullptr, *dw = nullptr, *dy = nullptr;
36    float* dinv = nullptr;
37    std::vector<float> inv(ELL);
38    for (int j = 0; j < ELL; ++j) inv[j] = 0.5f + (float) (j % 17) / 16.0f;
39    check(cudaMalloc((void**) &dinv, inv.size() * sizeof(float)));
40    check(cudaMemcpy(dinv, inv.data(), inv.size() * sizeof(float), cudaMemcpyHostToDevice));
41    check(cudaMalloc(&dx, x.size()));
42    check(cudaMalloc(&dy, M * yrow));
43    check(cudaMemcpy(dx, x.data(), x.size(), cudaMemcpyHostToDevice));
44    std::vector<uint8_t> expected(M * yrow), actual(M * yrow);
45    for (int fmt : {4, 8}) {
46        const size_t block_bytes = fmt == 4 ? 18 : 34;
47        std::vector<uint8_t> w(G * ELL * (K / 32) * block_bytes);
48        for (size_t block = 0; block < w.size() / block_bytes; ++block) {
49            uint8_t* p = w.data() + block * block_bytes;
50            const uint16_t scale = 0x3800;
51            std::memcpy(p, &scale, 2);
52            for (size_t j = 2; j < block_bytes; ++j) p[j] = (uint8_t) ((block * 13 + j * 7) % 251);
53        }
54        check(cudaMalloc(&dw, w.size()));
55        check(cudaMemcpy(dw, w.data(), w.size(), cudaMemcpyHostToDevice));
56        strata::prefill::fused_q3k::latent_dense_d(dw, dx, M, K, G, ELL, dy, nullptr, fmt, dinv);
57        check(cudaDeviceSynchronize());
58        check(cudaMemcpy(expected.data(), dy, expected.size(), cudaMemcpyDeviceToHost));
59        if (same_inputs)
60            for (int row = 1; row < M; ++row)
61                if (std::memcmp(expected.data(), expected.data() + row * yrow, yrow) != 0) {
62                    std::fprintf(stderr, "identical inputs differ at row %d, fmt %d\n", row, fmt);
63                    std::exit(1);
64                }
65        for (int rows : {1, 2, 3, 5, 7, 8, 9, 15, 16, 17, 63, 64, 65, 69, 70, 71,
66                         127, 128, 129, 511, 512, 513}) {
67            check(cudaMemset(dy, 0, M * yrow));
68            strata::prefill::fused_q3k::latent_dense_d(
69                dw, dx, rows, K, G, ELL, dy, nullptr, fmt, dinv);
70            check(cudaDeviceSynchronize());
71            check(cudaMemcpy(actual.data(), dy, rows * yrow, cudaMemcpyDeviceToHost));
72            if (std::memcmp(actual.data(), expected.data(), rows * yrow) != 0) {
73                std::fprintf(stderr, "fmt %d, rows %d differ from full tiles\n", fmt, rows);
74                std::exit(1);
75            }
76        }
77        check(cudaFree(dw));
78    }
79    check(cudaFree(dinv));
80    check(cudaFree(dx));
81    check(cudaFree(dy));
82}
83
84int main(int argc, char** argv) {
85    check(cudaSetDevice(argc > 1 ? std::atoi(argv[1]) : 0));
86    check_rows(false);
87    check_rows(true);
88    std::puts("partial and identical latent D rows match, Q4 and Q8");
89}