Files
leleka/engine/strata/tools/dsv41_lab/wmma_emu_test.cpp
T
Pavlo RatushnyiandClaude Sonnet 5.5 87e2633afe engine/strata: bring the Strata subtree to upstream 41cf05a0 (engine 0.1.50) with the local patches re-applied
Documented tree replacement (the history has no git-subtree metadata): 3-way merge of the old subtree
(570201a:engine/strata) against upstream 41cf05a06a25dd29103b7b27a2279a3f60c21876 (2026-10-25), merge base =
the old pin d9ab8435 (tree-identical to upstream 5b734e42; d9ab8435 is not an ancestor of upstream main).

Local patches re-applied by hand where they conflicted (15 files): STRATA_CUDA_STATIC profile, experimental KAR
experts and KAR PLE arms, keyless V-only arm (now beside upstream's K8V4 hybrid KV and fused norm+rope). Details,
conflict table and left-out files are in docs/upstream.md.

Left out (weights, captures, logs, media): new bench/results (2022 files), binary media and app assets (61),
docs/work/evidence (24), *.bin / *.gguf data files, and the 7 files the first import already left out.

scripts/build-cpu.sh: build the 11 CPU tests upstream registered since the old pin (they ran as "Not Run").
Checked: build-cpu.sh 16/16 ctest in WSL; sm_86 CUDA objects of the patched files compile, also with the KAR
macros on. No GPU run, no quality gate.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 12:49:15 +01:00

65 lines
3.1 KiB
C++

// tools/dsv41_lab/wmma_emu_test.cpp - the gfx12 port's data flow (wmma_emu.hpp) against the double reference on decoded weights.
// ./wmma_emu_test table codebook (shipped K5/K6/K7)
// TQE_CODE=3single ./wmma_emu_test computed code
// Host only; no GPU needed.
#include "../trellis/tqenc.hpp"
#include "wmma_emu.hpp"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <random>
#include <vector>
static void step_table(uint32_t s, uint32_t out[2]) {
static std::vector<uint32_t> lut;
if (lut.empty()) {
const float* pt = tqenc::point_table();
lut.resize(2048);
for (int i = 0; i < 2048; ++i) lut[i] = tqw::f2h_rn(pt[2 * i]) | ((uint32_t) tqw::f2h_rn(pt[2 * i + 1]) << 16);
}
const uint32_t x = (s & 0x7fffu) * 0x9e3779b1u;
out[0] = lut[x >> 21];
out[1] = lut[(x >> 10) & 2047u];
}
static void step_3single(uint32_t s, uint32_t out[2]) { out[0] = tqcode::word3(s & 0x7fffu, 0); out[1] = tqcode::word3(s & 0x7fffu, 1); }
int main() {
const bool computed = tqcode::kind_of(std::getenv("TQE_CODE")) == tqcode::k3single;
if (std::getenv("TQE_CODE") && !computed) { std::printf("only TQE_CODE=3single is wired here\n"); return 2; }
std::mt19937_64 rng(7);
int fails = 0;
const int Ks[4] = {4, 5, 6, 7}, types[4] = {225, 224, 145, 146};
for (int ki = 0; ki < 4; ++ki) {
const int K = Ks[ki], type = types[ki], bsz = 2 + 4 * K;
const int rows = 48, in = 768, nblk = in / 128;
std::vector<uint8_t> w((size_t) rows * nblk * bsz);
for (auto& b : w) b = (uint8_t) rng();
for (int64_t b = 0; b < (int64_t) rows * nblk; ++b) { const uint16_t d = tqw::f2h_rn(0.02f + 0.001f * (float) (rng() % 400)); std::memcpy(&w[(size_t) b * bsz], &d, 2); }
std::vector<float> dec((size_t) rows * in);
tqenc::decode_blocks(type, w.data(), (int64_t) rows * nblk, dec.data());
std::vector<uint32_t> words; std::vector<uint16_t> sc;
tqw::repack_tqw(K, w.data(), rows, nblk, words, sc);
for (int nb : {1, 3, 5}) {
std::vector<float> x((size_t) nb * in);
std::uniform_real_distribution<float> u(-1.f, 1.f);
for (auto& v : x) v = u(rng);
std::vector<float> y;
tqw::gemv_tqw(K, nb, rows, in, words, sc, x, computed ? step_3single : step_table, y);
double worst = 0;
for (int c = 0; c < nb; ++c)
for (int r = 0; r < rows; ++r) {
double s = 0, sa = 0;
for (int i = 0; i < in; ++i) { const double t = (double) dec[(size_t) r * in + i] * x[(size_t) c * in + i]; s += t; sa += std::fabs(t); }
worst = std::max(worst, std::fabs(y[(size_t) c * rows + r] - s) / (sa + 1e-30));
}
const bool ok = worst < 3e-7;
std::printf("K=%d nb=%d (%s): emulated gfx12 flow vs double reference %.2e %s\n", K, nb, computed ? "3single" : "table", worst, ok ? "ok" : "FAIL");
fails += !ok;
}
}
std::printf("%s\n", fails ? "FAILED" : "ALL OK");
return fails;
}