okami.mom/strata

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

Niko1221Merge f-023-int into f-023-guifcb0c2d0e

main
5.2 KiB95 linesraw
1// src/core/dflash2_parity.cpp - the engine's DFlash2 drafter against the torch port, on a synthetic case (GPU, no model).
2//
3//     dflash2_parity <pack> <case.bin> [rounds]
4//
5// case.bin comes from tools/dflash2/mk_parity_case.py: n_ctx random tap rows, an anchor, a random Q8_0 head over a draft-vocabulary subset, the
6// target embedding rows of the anchor and the mask token, and the torch answer (tokens, probabilities, the 8 final normalized rows).  The engine's
7// drafter reads the same pack (Q8_0 weights, Q8_1 activations), so the rows agree to quantization noise, not bitwise: the test checks the rows'
8// cosine similarity, the walk's tokens and the time of a round.
9#include "strata/core/dflash2.hpp"
10
11#include <cuda_runtime.h>
12
13#include <chrono>
14#include <cmath>
15#include <cstdint>
16#include <cstdio>
17#include <cstdlib>
18#include <vector>
19
20using strata::core::Dflash2Drafter;
21
22int main(int argc, char** argv) {
23    if (argc < 3) { std::fprintf(stderr, "usage: dflash2_parity <pack> <case.bin> [rounds]\n"); return 2; }
24    const int rounds = argc > 3 ? std::atoi(argv[3]) : 20;
25    std::FILE* f = std::fopen(argv[2], "rb");
26    if (!f) { std::fprintf(stderr, "cannot open %s\n", argv[2]); return 2; }
27    int32_t hdr[4];
28    if (std::fread(hdr, 4, 4, f) != 4) return 2;
29    const int n_ctx = hdr[0], anchor = hdr[1], vp = hdr[2];
30    const int H = 2560, tapn = 5 * H;
31    std::vector<float> taps((size_t) n_ctx * tapn), emb(2 * H), fn_ref(8 * H);
32    std::vector<int32_t> dvocab(vp), tok_ref(7);
33    std::vector<float> prob_ref(7);
34    const size_t row_bytes = (size_t) (H / 32) * 34;
35    std::vector<uint8_t> head((size_t) vp * row_bytes);
36    bool ok = std::fread(taps.data(), 4, taps.size(), f) == taps.size() && std::fread(emb.data(), 4, emb.size(), f) == emb.size() &&
37              std::fread(dvocab.data(), 4, dvocab.size(), f) == dvocab.size() && std::fread(head.data(), 1, head.size(), f) == head.size() &&
38              std::fread(tok_ref.data(), 4, 7, f) == 7 && std::fread(prob_ref.data(), 4, 7, f) == 7 && std::fread(fn_ref.data(), 4, fn_ref.size(), f) == fn_ref.size();
39    std::fclose(f);
40    if (!ok) { std::fprintf(stderr, "case file is cut short\n"); return 2; }
41
42    std::string err;
43    Dflash2Drafter d;
44    if (!d.load(argv[1], 0, err)) { std::fprintf(stderr, "load: %s\n", err.c_str()); return 1; }
45    float *d_taps = nullptr, *d_emb = nullptr;
46    uint8_t* d_head = nullptr;
47    cudaMalloc((void**) &d_taps, taps.size() * 4);
48    cudaMalloc((void**) &d_emb, emb.size() * 4);
49    cudaMalloc((void**) &d_head, head.size());
50    cudaMemcpy(d_taps, taps.data(), taps.size() * 4, cudaMemcpyHostToDevice);
51    cudaMemcpy(d_emb, emb.data(), emb.size() * 4, cudaMemcpyHostToDevice);
52    cudaMemcpy(d_head, head.data(), head.size(), cudaMemcpyHostToDevice);
53    auto embed = [&](const int32_t*, int n, float* out, cudaStream_t cs) {
54        cudaMemcpyAsync(out, d_emb, (size_t) H * 4, cudaMemcpyDeviceToDevice, cs);
55        for (int i = 1; i < n; ++i) cudaMemcpyAsync(out + (size_t) i * H, d_emb + H, (size_t) H * 4, cudaMemcpyDeviceToDevice, cs);
56    };
57    if (!d.bind_raw(embed, d_head, 8, vp, dvocab, err)) { std::fprintf(stderr, "bind: %s\n", err.c_str()); return 1; }
58    std::printf("drafter VRAM %.0f MiB\n", (double) d.vram_bytes() / 1048576.0);
59
60    // the context but the last row, then one draft round that adds the last row
61    if (!d.context(d_taps, n_ctx - 1, 0, err)) { std::fprintf(stderr, "context: %s\n", err.c_str()); return 1; }
62    int32_t drafts[7];
63    float probs[7];
64    const float* last = d_taps + (size_t) (n_ctx - 1) * tapn;
65    if (!d.draft(last, 1, n_ctx - 1, anchor, drafts, probs, err)) { std::fprintf(stderr, "draft: %s\n", err.c_str()); return 1; }
66    std::vector<float> rows;
67    d.debug_rows(rows, err);
68    int bad = 0;
69    double min_cos = 1.0;
70    for (int r = 0; r < 8; ++r) {
71        double dot = 0, na = 0, nb = 0;
72        for (int c = 0; c < H; ++c) {
73            const double a = rows[(size_t) r * H + c], b = fn_ref[(size_t) r * H + c];
74            dot += a * b; na += a * a; nb += b * b;
75        }
76        const double cs = dot / std::sqrt(na * nb + 1e-30);
77        min_cos = std::min(min_cos, cs);
78        std::printf("row %d: cos %.5f  norm ratio %.4f\n", r, cs, std::sqrt(na / (nb + 1e-30)));
79    }
80    int same = 0;
81    for (int i = 0; i < 7; ++i) {
82        same += drafts[i] == tok_ref[i];
83        std::printf("draft %d: engine %d (p %.3f)  torch %d (p %.3f)\n", i, drafts[i], probs[i], tok_ref[i], prob_ref[i]);
84    }
85    if (min_cos < 0.98) { std::printf("FAIL: a final row's cosine similarity %.4f is below 0.98\n", min_cos); ++bad; }
86    // timing: the same round again (the graph replays; the ring rewrites the same slot)
87    cudaDeviceSynchronize();
88    const auto t0 = std::chrono::steady_clock::now();
89    for (int i = 0; i < rounds; ++i)
90        if (!d.draft(last, 1, n_ctx - 1, anchor, drafts, probs, err)) { std::fprintf(stderr, "draft: %s\n", err.c_str()); return 1; }
91    const double ms = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count() / rounds;
92    std::printf("tokens equal %d of 7; %.3f ms per round (1 context row + the block), GPU %.3f ms\n", same, ms, d.rounds > 0 ? d.ms_gpu / (double) d.rounds : 0.0);
93    std::printf(bad ? "dflash2_parity: FAIL\n" : "dflash2_parity: ok\n");
94    return bad ? 1 : 0;
95}