okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2ec7c3443
main
1// The split engine's decode dots (src/qdot.cuh) against stored reference digests (src/split/tools/qdot_golden.txt): the inputs of every group are drawn 2// again from fixed seeds (the same draws as qdot_test.cu's), the dots run on the card, and the FNV-1a 64-bit digest of the group's float bits must equal 3// the stored one. So the check needs no ggml: the file was written by qdot_test.cu --write-golden, which refuses to write unless every dot of qdot.cuh is 4// bit for bit ggml-cuda's (vecdotq.cuh at the vendored pin) on the same inputs. 5// 6// File: '#' comment lines, then one line a group: <family> <format> <group> <dots> <input digest> <output digest> [% reason]. Family "cuda" holds every 7// group; another toolchain family ("hip") has a line only where its bits differ from the cuda line, with the reason (measured ulp range) after '%'; a 8// check under family F takes F's line where there is one, else the cuda line. Shared by qdot_test.cu (writer, oracle build) and qdot_golden.cu (check). 9#pragma once 10#include <cuda_fp16.h> 11#include <cuda_runtime.h> 12 13#include <algorithm> 14#include <cstdint> 15#include <cstdio> 16#include <cstring> 17#include <map> 18#include <random> 19#include <string> 20#include <vector> 21 22#include "qdot.cuh" 23 24namespace fnx_golden { 25 26using namespace fnx; 27 28constexpr int kGroupDots = 4096; // dots a group (the last group of a format may be shorter) 29 30template <GgmlType T> struct Dot; 31#define FNX_GD_DOT(T, NAME, VDR) \ 32 template <> struct Dot<T> { \ 33 static constexpr int vdr = VDR; \ 34 __device__ static float ours(const void* v, const block_q8_1* y, int k, int i) { return fnx::NAME(v, y, k, i); } \ 35 }; 36FNX_GD_DOT(T_Q4_0, vec_dot_q4_0_q8_1, VDR_Q4_0) 37FNX_GD_DOT(T_Q5_1, vec_dot_q5_1_q8_1, VDR_Q5_1) 38FNX_GD_DOT(T_Q8_0, vec_dot_q8_0_q8_1, VDR_Q8_0) 39FNX_GD_DOT(T_Q4_K, vec_dot_q4_K_q8_1, VDR_Q4_K) 40FNX_GD_DOT(T_Q5_K, vec_dot_q5_K_q8_1, VDR_Q5_K) 41FNX_GD_DOT(T_Q6_K, vec_dot_q6_K_q8_1, VDR_Q6_K) 42FNX_GD_DOT(T_IQ4_NL, vec_dot_iq4_nl_q8_1, VDR_IQ4_NL) 43FNX_GD_DOT(T_IQ4_XS, vec_dot_iq4_xs_q8_1, VDR_IQ4_XS) 44FNX_GD_DOT(T_IQ3_S, vec_dot_iq3_s_q8_1, VDR_IQ3_S) 45FNX_GD_DOT(T_IQ3_XXS, vec_dot_iq3_xxs_q8_1, VDR_IQ3_XXS) 46FNX_GD_DOT(T_IQ2_S, vec_dot_iq2_s_q8_1, VDR_IQ2_S) 47#undef FNX_GD_DOT 48 49// one thread a dot, in qdot_test.cu's order: dot e = (weight block kbx = e / per, iqs = vdr (e % per)), the call the decode kernels make 50template <GgmlType T> __global__ void k_ours(const char* w, const block_q8_1* y, int nblocks, float* out) { 51 constexpr int qk = qtraits<T>::qk, per = qtraits<T>::qi / Dot<T>::vdr; 52 const int e = blockIdx.x * blockDim.x + threadIdx.x; 53 if (e >= nblocks * per) return; 54 const int kbx = e / per, iqs = Dot<T>::vdr * (e % per); 55 out[e] = Dot<T>::ours(w, y + (size_t) kbx * (qk / QK8_1), kbx, iqs); 56} 57 58inline uint64_t fnv1a64(const void* p, size_t n, uint64_t h = 1469598103934665603ull) { 59 const unsigned char* b = (const unsigned char*) p; 60 for (size_t i = 0; i < n; ++i) h = (h ^ b[i]) * 1099511628211ull; 61 return h; 62} 63 64// the inputs of one format: random weight bytes with finite fp16 fields, random q8_1 activations (qdot_test.cu's draws for the same seed and size) 65struct Inputs { 66 std::vector<unsigned char> w; 67 std::vector<block_q8_1> y; 68}; 69inline __half finite_half(std::mt19937& rng) { return __float2half((float) ((int) (rng() % 2001) - 1000) / 400.0f); } 70inline Inputs make_inputs(int B, const std::vector<int>& h16, int nblocks, int qk, unsigned seed) { 71 Inputs in; 72 std::mt19937 rng(seed); 73 in.w.resize((size_t) nblocks * B); 74 for (auto& c : in.w) c = (unsigned char) rng(); 75 for (int k = 0; k < nblocks; ++k) 76 for (int off : h16) { 77 const __half h = finite_half(rng); 78 std::memcpy(&in.w[(size_t) k * B + off], &h, 2); 79 } 80 in.y = std::vector<block_q8_1>((size_t) nblocks * (qk / QK8_1)); // (constructed at its size: block_q8_1 has no copy constructor, so no resize) 81 for (auto& b : in.y) { 82 for (int i = 0; i < 32; ++i) b.qs[i] = (int8_t) (int) (rng() % 255 - 127); 83 // s drawn first, then d, in named locals: as two arguments of one call their order was unspecified (g++ under nvcc drew s first, 84 // clang under HIP d first); this is g++'s order, so qdot_golden.txt is unchanged 85 const float s = (float) ((int) (rng() % 200) - 100); 86 const float d = (float) (rng() % 1000) / 500.0f; 87 b.ds = make_half2(__float2half(d), __float2half(s)); 88 } 89 return in; 90} 91 92// the formats, their block bytes, fp16 fields, sizes and seeds (blocks: 4,096 super-blocks or 32,768 32-value blocks a format) 93struct Format { 94 const char* name; 95 int B; 96 std::vector<int> h16; 97 int nblocks; 98 unsigned seed; 99}; 100inline std::vector<Format> formats() { 101 constexpr int n = 4096; 102 return {{"Q4_0", (int) sizeof(block_q4_0), {0}, n * 8, 1}, {"Q5_1", (int) sizeof(block_q5_1), {0, 2}, n * 8, 2}, 103 {"Q8_0", (int) sizeof(block_q8_0), {0}, n * 8, 3}, {"Q4_K", (int) sizeof(block_q4_K), {0, 2}, n, 4}, 104 {"Q5_K", (int) sizeof(block_q5_K), {0, 2}, n, 5}, {"Q6_K", (int) sizeof(block_q6_K), {(int) sizeof(block_q6_K) - 2}, n, 6}, 105 {"IQ4_NL", (int) sizeof(block_iq4_nl), {0}, n * 8, 7}, {"IQ4_XS", (int) sizeof(block_iq4_xs), {0}, n, 8}, 106 {"IQ3_S", (int) sizeof(block_iq3_s), {0}, n, 9}, {"IQ3_XXS", (int) sizeof(block_iq3_xxs), {0}, n, 10}, 107 {"IQ2_S", (int) sizeof(block_iq2_s), {0}, n, 11}}; 108} 109 110// one group's line 111struct Line { 112 std::string family, format; 113 int group = 0, dots = 0; 114 uint64_t in_digest = 0, out_digest = 0; 115 std::string reason; 116}; 117inline std::string line_text(const Line& l) { 118 char b[256]; 119 std::snprintf(b, sizeof b, "%s %s %d %d %016llx %016llx", l.family.c_str(), l.format.c_str(), l.group, l.dots, 120 (unsigned long long) l.in_digest, (unsigned long long) l.out_digest); 121 return std::string(b) + (l.reason.empty() ? "" : " % " + l.reason); 122} 123 124// the dots of format T on the card (ours), as floats in dot order; empty on a CUDA error 125template <GgmlType T> std::vector<float> run_ours(const Inputs& in, int nblocks) { 126 const int ndots = nblocks * (qtraits<T>::qi / Dot<T>::vdr); 127 char* dw = nullptr; 128 block_q8_1* dy = nullptr; 129 float* dout = nullptr; 130 std::vector<float> out((size_t) ndots); 131 bool ok = cudaMalloc(&dw, in.w.size()) == cudaSuccess && cudaMalloc(&dy, in.y.size() * sizeof(block_q8_1)) == cudaSuccess && 132 cudaMalloc(&dout, out.size() * 4) == cudaSuccess; 133 if (ok) { 134 cudaMemcpy(dw, in.w.data(), in.w.size(), cudaMemcpyHostToDevice); 135 cudaMemcpy(dy, in.y.data(), in.y.size() * sizeof(block_q8_1), cudaMemcpyHostToDevice); 136 k_ours<T><<<(ndots + 255) / 256, 256>>>(dw, dy, nblocks, dout); 137 ok = cudaDeviceSynchronize() == cudaSuccess && cudaMemcpy(out.data(), dout, out.size() * 4, cudaMemcpyDeviceToHost) == cudaSuccess; 138 } 139 cudaFree(dw); cudaFree(dy); cudaFree(dout); 140 if (!ok) out.clear(); 141 return out; 142} 143 144// the groups of one format from its dots: kGroupDots each, input digest = the whole format's inputs (weights then activations) 145inline std::vector<Line> group_lines(const char* family, const char* format, const Inputs& in, const std::vector<float>& dots) { 146 const uint64_t ind = fnv1a64(in.y.data(), in.y.size() * sizeof(block_q8_1), fnv1a64(in.w.data(), in.w.size())); 147 std::vector<Line> v; 148 for (size_t g0 = 0, gi = 0; g0 < dots.size(); g0 += kGroupDots, ++gi) { 149 const size_t n = std::min<size_t>(kGroupDots, dots.size() - g0); 150 Line l; 151 l.family = family; l.format = format; l.group = (int) gi; l.dots = (int) n; 152 l.in_digest = ind; 153 l.out_digest = fnv1a64(dots.data() + g0, n * 4); 154 v.push_back(l); 155 } 156 return v; 157} 158 159// every format's dots on this card, in the file's order (fn: run_ours<T> or an oracle-checked variant with the same signature) 160template <class Fn> bool for_each_format(Fn&& fn) { 161 bool ok = true; 162 const std::vector<Format> fs = formats(); 163 auto one = [&](auto tag, const Format& f) { 164 constexpr GgmlType T = decltype(tag)::value; 165 const Inputs in = make_inputs(f.B, f.h16, f.nblocks, qtraits<T>::qk, f.seed); 166 ok &= fn.template operator()<T>(f, in); 167 }; 168 one(std::integral_constant<GgmlType, T_Q4_0>{}, fs[0]); 169 one(std::integral_constant<GgmlType, T_Q5_1>{}, fs[1]); 170 one(std::integral_constant<GgmlType, T_Q8_0>{}, fs[2]); 171 one(std::integral_constant<GgmlType, T_Q4_K>{}, fs[3]); 172 one(std::integral_constant<GgmlType, T_Q5_K>{}, fs[4]); 173 one(std::integral_constant<GgmlType, T_Q6_K>{}, fs[5]); 174 one(std::integral_constant<GgmlType, T_IQ4_NL>{}, fs[6]); 175 one(std::integral_constant<GgmlType, T_IQ4_XS>{}, fs[7]); 176 one(std::integral_constant<GgmlType, T_IQ3_S>{}, fs[8]); 177 one(std::integral_constant<GgmlType, T_IQ3_XXS>{}, fs[9]); 178 one(std::integral_constant<GgmlType, T_IQ2_S>{}, fs[10]); 179 return ok; 180} 181 182inline const char* kHeader = 183 "# src/split/tools/qdot_golden.txt - reference digests of the split engine's decode dots (src/split/src/qdot.cuh); checked by split_qdot_golden\n" 184 "# (src/split/tools/qdot_golden.cu), written by split_qdot_test --write-golden (src/split/tools/qdot_test.cu), which writes only when every dot\n" 185 "# of qdot.cuh is bit for bit ggml-cuda's vec_dot_*_q8_1 (vecdotq.cuh, third_party/strata-ggml, llama.cpp pin 3cf03257f219afbe7334045ff7c6a06ac68c627d)\n" 186 "# on the same inputs. Inputs: fixed seeds per format (qdot_golden.cuh make_inputs); a group = 4096 dots in the decode kernels' call order.\n" 187 "# Line: <family> <format> <group> <dots> <input digest> <output digest> [% reason]; digests FNV-1a 64 (inputs: weight bytes then q8_1 blocks of\n" 188 "# the format; outputs: the group's float bits). Family cuda: every group (written with nvcc --use_fast_math, the split's flags). Another\n" 189 "# family (hip) has a line only where its bits differ from the cuda line, with the reason after '%'.\n"; 190 191// the file's lines (comments skipped); false if it cannot be read 192inline bool read_lines(const char* path, std::vector<Line>& out) { 193 FILE* f = std::fopen(path, "r"); 194 if (!f) return false; 195 char buf[1024]; 196 while (std::fgets(buf, sizeof buf, f)) { 197 if (buf[0] == '#' || buf[0] == '\n') continue; 198 char fam[32], fmt[32]; 199 int g = 0, n = 0; 200 unsigned long long a = 0, b = 0; 201 if (std::sscanf(buf, "%31s %31s %d %d %llx %llx", fam, fmt, &g, &n, &a, &b) != 6) { std::fclose(f); return false; } 202 Line l; 203 l.family = fam; l.format = fmt; l.group = g; l.dots = n; l.in_digest = a; l.out_digest = b; 204 const char* pct = std::strchr(buf, '%'); 205 if (pct) { l.reason = pct + 1; while (!l.reason.empty() && (l.reason.back() == '\n' || l.reason.back() == '\r')) l.reason.pop_back(); } 206 out.push_back(l); 207 } 208 std::fclose(f); 209 return true; 210} 211 212} // namespace fnx_golden