okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
52d2d3ee2
main
1// tests/cuda/prefill_decode_golden.cu - the fused prompt kernels' weight decode, bit for bit, against stored digests 2// (prefill_decode_golden.txt), the same file on every vendor. 3// 4// What it checks. The fused prompt-expert kernels (src/prefill/moe_fused_iq.cu: CUDA's native_kernel, HIP's native_w11_kernel) 5// share one decode: load_unit<T> reads a 32-value sub-block's bytes into registers and convert<T> turns them into 32 int8 and the 6// sub-block's two scales. This test compiles that decode as it is (moe_fused_iq.cu included with STRATA_FUSED_DECODE_ONLY: the 7// file up to grid_src, nothing else), runs it on the card over seeded blocks of every format the build decodes, and hashes the 8// outputs: the 8 int8 words, then the bits of s0 and s1, per sub-block, in block order. Where CUDA and HIP take different code 9// for the same result (the i-quants' signs: signed8 vs the gfx11 / gfx12 table; IQ4's 16-entry table: byte_perm vs amdgcn_perm) 10// the digests show it. The fused kernels' OUTPUTS are not compared across vendors (their float order and SwiGLU differ); this 11// is the shared part, which must be the same bits everywhere. 12// 13// Inputs. One generator per format (splitmix64, seeded with the FNV-1a of the format's name), every draw a statement of its own 14// (no two draws in one expression: C++ leaves their order to the compiler, and g++ and clang differ), the bytes taken from each 15// 64-bit draw lowest first. A block's scale fields (fp16) are then set to finite normal values from further draws. 16// 17// Oracle. Every format ggml knows is also compared value by value with ggml's dequantizer (to_float): our value 18// s * q (+ s1 for the formats with a minimum) within 1e-6 of ggml's, relative to the size of the terms. --write refuses to write 19// a file unless that holds; the trellis types (unknown to the pinned ggml) have no oracle here (their parity is in 20// prefill_fused_iq_test). 21// 22// prefill_decode_golden <file> check: every format this build decodes must match the file (exit 1 if not, 77 without a card) 23// prefill_decode_golden --write <file> write the file (only if the oracle holds) 24// prefill_decode_golden --print print the lines 25#define STRATA_FUSED_DECODE_ONLY 1 26#include "../../src/prefill/moe_fused_iq.cu" 27 28#include <cinttypes> 29#include <cmath> 30#include <fstream> 31#include <map> 32#include <sstream> 33#include <stdexcept> 34#include <string> 35#include <vector> 36 37namespace strata::prefill::fused { 38namespace { 39 40constexpr int kBlocks = 2048; // blocks per format 41constexpr int kGroup = 256; // blocks per digest line 42 43// sub-blocks of 32 values per block (load_unit's ib): the 256-value super-blocks 8, Q2_0 2, the trellis types 4, the 32-value formats 1 44__host__ __device__ constexpr int nsub(int t) { 45 return t == T_IQ4_NL || t == T_Q5_0 || t == T_Q5_1 || t == T_Q8_0 ? 1 : t == T_Q2_0 || t == T_NVFP4 ? 2 : is_tq(t) ? 4 : 8; 46} 47 48template <int T> __global__ void decode_probe_kernel(const uint8_t* __restrict__ blocks, int nblk, uint32_t* __restrict__ out) { 49#if defined(__HIPCC__) 50 constexpr int SGN = (T == T_IQ2_XXS || T == T_IQ2_XS || T == T_IQ2_S || T == T_IQ3_XXS || T == T_IQ3_S) ? sign_entries(T) : 1; 51 __shared__ uint4 ssign[SGN]; // (the kernels' sign table, filled as native_w11_kernel fills it) 52 if constexpr (SGN > 1) 53 for (int i = threadIdx.x; i < SGN; i += blockDim.x) ssign[i] = sign_entry(i, SGN == 128); 54 __syncthreads(); 55 const uint4* sg = ssign; 56#else 57 const uint4* sg = nullptr; 58#endif 59 uint32_t kv[4] = {0, 0, 0, 0}; 60 if constexpr (T == T_IQ4_XS || T == T_IQ4_NL) { 61#pragma unroll 62 for (int k = 0; k < 16; ++k) kv[k >> 2] |= (uint32_t) (uint8_t) kvalues_iq4nl[k] << (8 * (k & 3)); 63 } 64 if constexpr (T == T_NVFP4) { 65#pragma unroll 66 for (int k = 0; k < 16; ++k) kv[k >> 2] |= (uint32_t) (uint8_t) kvalues_fp4[k] << (8 * (k & 3)); 67 } 68 const int i =blockIdx.x * blockDim.x + threadIdx.x; 69 if (i >= nblk * nsub(T)) return; 70 const int b = i / nsub(T), ib = i % nsub(T); 71 constexpr int NW = raw_words(T) > 5 ? raw_words(T) : 5; 72 uint32_t raw[NW] = {}; 73 load_unit<T>(blocks + (size_t) b * block_bytes(T), ib, raw); 74 uint32_t q[8]; 75 float s0, s1; 76 convert<T>(raw, (const uint8_t*) grid_src<T>(), kv, q, s0, s1, sg); 77 uint32_t* o = out + (size_t) i * 10; 78#pragma unroll 79 for (int k = 0; k < 8; ++k) o[k] = q[k]; 80 o[8] = __float_as_uint(s0); 81 o[9] = __float_as_uint(s1); 82} 83 84void ck(cudaError_t e, const char* what, bool) { 85 if (e != cudaSuccess) throw std::runtime_error(std::string(what) + ": " + cudaGetErrorString(e)); 86} 87 88struct Fmt { 89 int t; 90 const char* name; 91 bool oracle; // ggml knows the type (to_float) 92}; 93const Fmt kFormats[] = { 94 {T_IQ2_XXS, "IQ2_XXS", true}, {T_IQ2_XS, "IQ2_XS", true}, {T_IQ2_S, "IQ2_S", true}, {T_IQ3_XXS, "IQ3_XXS", true}, 95 {T_IQ3_S, "IQ3_S", true}, {T_IQ4_XS, "IQ4_XS", true}, {T_IQ4_NL, "IQ4_NL", true}, {T_Q2_0, "Q2_0", true}, 96 {T_Q4_K, "Q4_K", true}, {T_Q5_K, "Q5_K", true}, {T_Q5_0, "Q5_0", true}, {T_Q5_1, "Q5_1", true}, 97 {T_Q8_0, "Q8_0", true}, {T_Q6_K, "Q6_K", true}, {T_TQ2_T, "TQ2_T", false}, {T_TQK6, "TQK6", false}, 98 {T_TQK7, "TQK7", false}, {T_NVFP4, "NVFP4", true}}; 99 100// the formats whose decode this build compiles: every one (the trellis decode on HIP too since the gfx12 port; the check keeps the list 101// so that a build without one says so instead of failing) 102bool built_here(int t) { 103 (void) t; 104 return true; 105} 106 107template <int T> void launch_probe(const uint8_t* blocks, int nblk, uint32_t* out) { 108 const int n = nblk * nsub(T); 109 decode_probe_kernel<T><<<(n + 255) / 256, 256>>>(blocks, nblk, out); 110} 111void run_probe(int t, const uint8_t* blocks, int nblk, uint32_t* out) { 112 switch (t) { 113 case T_IQ2_XXS: launch_probe<T_IQ2_XXS>(blocks, nblk, out); break; 114 case T_IQ2_XS: launch_probe<T_IQ2_XS>(blocks, nblk, out); break; 115 case T_IQ2_S: launch_probe<T_IQ2_S>(blocks, nblk, out); break; 116 case T_IQ3_XXS: launch_probe<T_IQ3_XXS>(blocks, nblk, out); break; 117 case T_IQ3_S: launch_probe<T_IQ3_S>(blocks, nblk, out); break; 118 case T_IQ4_XS: launch_probe<T_IQ4_XS>(blocks, nblk, out); break; 119 case T_IQ4_NL: launch_probe<T_IQ4_NL>(blocks, nblk, out); break; 120 case T_Q2_0: launch_probe<T_Q2_0>(blocks, nblk, out); break; 121 case T_Q4_K: launch_probe<T_Q4_K>(blocks, nblk, out); break; 122 case T_Q5_K: launch_probe<T_Q5_K>(blocks, nblk, out); break; 123 case T_Q5_0: launch_probe<T_Q5_0>(blocks, nblk, out); break; 124 case T_Q5_1: launch_probe<T_Q5_1>(blocks, nblk, out); break; 125 case T_Q8_0: launch_probe<T_Q8_0>(blocks, nblk, out); break; 126 case T_Q6_K: launch_probe<T_Q6_K>(blocks, nblk, out); break; 127 case T_TQ2_T: launch_probe<T_TQ2_T>(blocks, nblk, out); break; 128 case T_TQK6: launch_probe<T_TQK6>(blocks, nblk, out); break; 129 case T_TQK7: launch_probe<T_TQK7>(blocks, nblk, out); break; 130 case T_NVFP4: launch_probe<T_NVFP4>(blocks, nblk, out); break; 131 default: throw std::runtime_error("no probe for this type in this build"); 132 } 133} 134 135uint64_t fnv_bytes(uint64_t h, const uint8_t* p, size_t n) { 136 for (size_t i = 0; i < n; ++i) { 137 h ^= p[i]; 138 h *= 1099511628211ull; 139 } 140 return h; 141} 142constexpr uint64_t kFnv0 = 1469598103934665603ull; 143 144struct SplitMix { 145 uint64_t s; 146 uint64_t next() { 147 s += 0x9e3779b97f4a7c15ull; 148 uint64_t z = s; 149 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull; 150 z = (z ^ (z >> 27)) * 0x94d049bb133111ebull; 151 return z ^ (z >> 31); 152 } 153}; 154 155// a finite normal fp16 from one draw: sign bit 20, exponent 9..18 (2^-6 .. 2^3), mantissa bits 8..17 of the draw 156uint16_t half_from(uint64_t r) { 157 const uint16_t sign = (uint16_t) (((r >> 20) & 1u) << 15); 158 const uint16_t ex = (uint16_t) (9 + (r % 10)); 159 const uint16_t man = (uint16_t) ((r >> 8) & 0x3FFu); 160 return (uint16_t) (sign | (ex << 10) | man); 161} 162// the fp16 scale fields of a block: their byte offsets 163std::vector<int> half_fields(int t) { 164 if (t == T_Q4_K || t == T_Q5_K || t == T_Q5_1) return {0, 2}; // d and dmin (Q5_1: d and m) 165 if (t == T_Q6_K) return {208}; // d at the block's end 166 if (t == T_NVFP4) return {}; // four UE4M3 scale bytes (every byte value is a scale, 0x7F the NaN that reads as 0), no fp16 field 167 return {0}; 168} 169 170std::vector<uint8_t> make_blocks(const Fmt& f) { 171 const size_t bs = (size_t) block_bytes(f.t), n = bs * kBlocks; 172 std::vector<uint8_t> m(n); 173 SplitMix g{fnv_bytes(kFnv0, (const uint8_t*) f.name, std::strlen(f.name))}; 174 for (size_t i = 0; i < n; i += 8) { 175 const uint64_t r = g.next(); 176 for (size_t k = 0; k < 8 && i + k < n; ++k) m[i + k] = (uint8_t) (r >> (8 * k)); 177 } 178 const std::vector<int> fields = half_fields(f.t); 179 for (int b = 0; b < kBlocks; ++b) 180 for (int off : fields) { 181 const uint64_t r = g.next(); 182 const uint16_t h = half_from(r); 183 m[(size_t) b * bs + (size_t) off] = (uint8_t) (h & 0xFF); 184 m[(size_t) b * bs + (size_t) off + 1] = (uint8_t) (h >> 8); 185 } 186 return m; 187} 188 189struct Line { 190 std::string fmt; 191 int group = 0, blocks = 0; 192 uint64_t in = 0, out = 0; 193}; 194std::string str(const Line& l) { 195 char buf[160]; 196 std::snprintf(buf, sizeof buf, "%s %d %d %016" PRIx64 " %016" PRIx64, l.fmt.c_str(), l.group, l.blocks, l.in, l.out); 197 return buf; 198} 199 200// ggml's values against ours, every sub-block of every block; returns the number of values off by more than 1e-6 201int64_t oracle(const Fmt& f, const std::vector<uint8_t>& m, const std::vector<uint32_t>& o) { 202 const auto* tr = ggml_get_type_traits((ggml_type) f.t); 203 const int per = (int) tr->blck_size, bs = block_bytes(f.t); 204 std::vector<float> v((size_t) per); 205 int64_t bad = 0; 206 for (int b = 0; b < kBlocks; ++b) { 207 tr->to_float(m.data() + (size_t) b * bs, v.data(), per); 208 for (int ib = 0; ib < nsub(f.t); ++ib) { 209 const uint32_t* w = &o[((size_t) b * nsub(f.t) + ib) * 10]; 210 float s0, s1; 211 std::memcpy(&s0, &w[8], 4); 212 std::memcpy(&s1, &w[9], 4); 213 for (int j = 0; j < 32; ++j) { 214 const float q = (float) (int8_t) (w[j >> 2] >> (8 * (j & 3))); 215 float mine, size; 216 if (has_min(f.t)) { mine = s0 * q + s1; size = std::fabs(s0 * q) + std::fabs(s1); } 217 else { const float s = per16(f.t) && j >= 16 ? s1 : s0; mine = s * q; size = std::fabs(mine); } 218 const float ref = v[(size_t) (32 * ib + j)]; 219 if (!(std::fabs(mine - ref) <= 1e-6f * size) && mine != ref) { 220 if (bad < 3) 221 std::printf(" %s block %d sub-block %d value %d: ours %.9g ggml %.9g\n", f.name, b, ib, j, mine, ref); 222 ++bad; 223 } 224 } 225 } 226 } 227 return bad; 228} 229 230} // namespace 231} // namespace strata::prefill::fused 232 233int main(int argc, char** argv) { 234 using namespace strata::prefill::fused; 235 std::string file; 236 bool write = false, print = false; 237 for (int i = 1; i < argc; ++i) { 238 const std::string a = argv[i]; 239 if (a == "--write") write = true; 240 else if (a == "--print") print = true; 241 else file = a; 242 } 243 if (!print && file.empty()) { std::fprintf(stderr, "usage: prefill_decode_golden <file> | --write <file> | --print\n"); return 2; } 244 try { 245 int n = 0; 246 if (cudaGetDeviceCount(&n) != cudaSuccess || n == 0) { std::printf("no GPU: skipped\n"); return 77; } 247 std::vector<Line> lines; 248 std::vector<std::string> not_built; 249 int64_t oracle_bad = 0, oracle_vals = 0; 250 for (const Fmt& f : kFormats) { 251 if (!built_here(f.t)) { not_built.push_back(f.name); continue; } 252 const std::vector<uint8_t> m = make_blocks(f); 253 const size_t nout = (size_t) kBlocks * nsub(f.t) * 10; 254 uint8_t* dm = nullptr; 255 uint32_t* dout = nullptr; 256 ck(cudaMalloc((void**) &dm, m.size() + 16), "malloc", true); 257 ck(cudaMalloc((void**) &dout, nout * 4), "malloc", true); 258 ck(cudaMemcpy(dm, m.data(), m.size(), cudaMemcpyHostToDevice), "upload", true); 259 ck(cudaMemset(dout, 0xFF, nout * 4), "memset", true); 260 run_probe(f.t, dm, kBlocks, dout); 261 ck(cudaGetLastError(), "launch", true); 262 std::vector<uint32_t> o(nout); 263 ck(cudaMemcpy(o.data(), dout, nout * 4, cudaMemcpyDeviceToHost), "download", true); 264 ck(cudaFree(dm), "free", true); 265 ck(cudaFree(dout), "free", true); 266 const size_t bs = (size_t) block_bytes(f.t), words = (size_t) nsub(f.t) * 10; 267 for (int g = 0; g < kBlocks / kGroup; ++g) { 268 Line l; 269 l.fmt = f.name; 270 l.group = g; 271 l.blocks = kGroup; 272 l.in = fnv_bytes(kFnv0, m.data() + (size_t) g * kGroup * bs, (size_t) kGroup * bs); 273 l.out = fnv_bytes(kFnv0, (const uint8_t*) (o.data() + (size_t) g * kGroup * words), (size_t) kGroup * words * 4); 274 lines.push_back(l); 275 } 276 if (f.oracle) { 277 const int64_t b = oracle(f, m, o); 278 oracle_bad += b; 279 oracle_vals += (int64_t) kBlocks * nsub(f.t) * 32; 280 if (b) std::printf("%s: %lld values differ from ggml's dequantizer\n", f.name, (long long) b); 281 } 282 } 283 std::printf("oracle (ggml to_float): %lld of %lld values differ\n", (long long) oracle_bad, (long long) oracle_vals); 284 if (!not_built.empty()) { 285 std::printf("not decoded by this build:"); 286 for (const std::string& s : not_built) std::printf(" %s", s.c_str()); 287 std::printf("\n"); 288 } 289 if (print) 290 for (const Line& l : lines) std::printf("%s\n", str(l).c_str()); 291 if (write) { 292 if (oracle_bad) { std::fprintf(stderr, "not written: the oracle does not hold\n"); return 1; } 293 std::ofstream out(file, std::ios::binary); 294 out << "# strata fused prompt kernels: weight decode digests (tests/cuda/prefill_decode_golden.cu), v1\n" 295 << "# format group blocks input-fnv64 output-fnv64 (output: per 32-value sub-block 8 int8 words + the bits of s0, s1)\n"; 296 for (const Line& l : lines) out << str(l) << "\n"; 297 std::printf("written %s: %zu lines\n", file.c_str(), lines.size()); 298 return out ? 0 : 1; 299 } 300 if (file.empty()) return oracle_bad ? 1 : 0; 301 std::ifstream in(file); 302 if (!in) { std::fprintf(stderr, "cannot read %s\n", file.c_str()); return 1; } 303 std::map<std::string, Line> want; 304 std::string s; 305 while (std::getline(in, s)) { 306 if (s.empty() || s[0] == '#') continue; 307 std::istringstream is(s); 308 Line l; 309 std::string a, b; 310 is >> l.fmt >> l.group >> l.blocks >> a >> b; 311 l.in = std::stoull(a, nullptr, 16); 312 l.out = std::stoull(b, nullptr, 16); 313 want[l.fmt + " " + std::to_string(l.group)] = l; 314 } 315 int bad = 0, same = 0; 316 for (const Line& l : lines) { 317 const auto it = want.find(l.fmt + " " + std::to_string(l.group)); 318 if (it == want.end()) { std::printf("MISSING in the file: %s\n", str(l).c_str()); ++bad; continue; } 319 if (it->second.in != l.in) { std::printf("INPUTS DIFFER (the generator, not the decode): %s / file %016" PRIx64 "\n", str(l).c_str(), it->second.in); ++bad; continue; } 320 if (it->second.out != l.out) { std::printf("DECODE DIFFERS: %s / file %016" PRIx64 "\n", str(l).c_str(), it->second.out); ++bad; continue; } 321 ++same; 322 } 323 std::printf("decode digests: %d of %zu groups equal the file, %d differ%s\n", same, lines.size(), bad, 324 not_built.empty() ? "" : " (formats not decoded by this build are not checked)"); 325 return bad || oracle_bad ? 1 : 0; 326 } catch (const std::exception& e) { 327 std::fprintf(stderr, "prefill_decode_golden: %s\n", e.what()); 328 return 1; 329 } 330}