okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2ec7c3443
main
1// HIP: the i-quant decode dots of src/iqdot_hip.cuh against src/qdot.cuh's vec_dot_iq3_s / iq3_xxs / iq2_s_q8_1 (ggml's arithmetic; tools/qdot_test.cu ties them to vecdotq.cuh) on random blocks (random weight bytes, random q8_1 rows, every 2// iqs the kernels use): the floats must match bit for bit. Prints one line per type and exits 1 on a mismatch. 3// build: the same compile line as src/kernels.cu's parts (-DFNX_HIP=1 ...), see phaseA-tests/round0211/r9700split/iqdot_build.sh 4#include "kernels.hpp" 5#include "env.hpp" 6 7#include <cuda_fp16.h> 8#include <cuda_runtime.h> 9 10#include <cstdint> 11#include <cstdio> 12#include <cstdlib> 13#include <cstring> 14#include <random> 15#include <vector> 16 17#include "qdot.cuh" 18#include "iqdot_hip.cuh" 19 20namespace { 21 22template <int T> struct Dots; 23template <> struct Dots<0> { // IQ3_S 24 static constexpr int BYTES = sizeof(block_iq3_s); 25 __device__ static float ref(const void* v, const block_q8_1* y, int k, int i) { return fnx::vec_dot_iq3_s_q8_1(v, y, k, i); } 26 __device__ static float mine(const void* v, const block_q8_1* y, int k, int i) { return fnx::fnx_vec_dot_iq3_s_q8_1(v, y, k, i); } 27}; 28template <> struct Dots<1> { // IQ3_XXS 29 static constexpr int BYTES = sizeof(block_iq3_xxs); 30 __device__ static float ref(const void* v, const block_q8_1* y, int k, int i) { return fnx::vec_dot_iq3_xxs_q8_1(v, y, k, i); } 31 __device__ static float mine(const void* v, const block_q8_1* y, int k, int i) { return fnx::fnx_vec_dot_iq3_xxs_q8_1(v, y, k, i); } 32}; 33template <> struct Dots<2> { // IQ2_S 34 static constexpr int BYTES = sizeof(block_iq2_s); 35 __device__ static float ref(const void* v, const block_q8_1* y, int k, int i) { return fnx::vec_dot_iq2_s_q8_1(v, y, k, i); } 36 __device__ static float mine(const void* v, const block_q8_1* y, int k, int i) { return fnx::fnx_vec_dot_iq2_s_q8_1(v, y, k, i); } 37}; 38 39// one thread per (block, iqs): the two dots against q8_1 block y[block * 8 + iqs / 2... ] (the dot reads bq8_1[iqs / 2], so 8 q8_1 blocks per weight block) 40template <int T> 41__global__ void k_cmp(const char* w, const block_q8_1* y, int nblocks, unsigned* bad, unsigned* checked) { 42 const int e = blockIdx.x * blockDim.x + threadIdx.x; 43 if (e >= nblocks * 8) return; 44 const int kbx = e / 8, iqs = (e % 8) * 2; 45 const float a = Dots<T>::ref(w, y + (size_t) kbx * 8, kbx, iqs), b = Dots<T>::mine(w, y + (size_t) kbx * 8, kbx, iqs); 46 atomicAdd(checked, 1u); 47 if (__float_as_uint(a) != __float_as_uint(b)) atomicAdd(bad, 1u); 48} 49 50template <int T> 51bool run(const char* name, int nblocks, unsigned seed) { 52 constexpr int B = Dots<T>::BYTES; 53 std::mt19937 rng(seed); 54 std::vector<unsigned char> w((size_t) nblocks * B); 55 for (auto& c : w) c = (unsigned char) rng(); 56 for (int k = 0; k < nblocks; ++k) { // d: a finite half of either sign 57 const __half h = __float2half((float) ((int) (rng() % 2001) - 1000) / 400.0f); 58 std::memcpy(&w[(size_t) k * B], &h, 2); 59 } 60 std::vector<block_q8_1> y((size_t) nblocks * 8); 61 for (auto& b : y) { 62 for (int i = 0; i < 32; ++i) b.qs[i] = (int8_t) (int) (rng() % 255 - 127); 63 // s drawn first, then d (as qdot_golden.cuh make_inputs: a fixed order on every compiler) 64 const float s = (float) ((int) (rng() % 200) - 100); 65 const float d = (float) (rng() % 1000) / 500.0f; 66 b.ds = make_half2(__float2half(d), __float2half(s)); 67 } 68 char* dw; 69 block_q8_1* dy; 70 unsigned *dbad, *dchk; 71 cudaMalloc(&dw, w.size()); 72 cudaMalloc(&dy, y.size() * sizeof(block_q8_1)); 73 cudaMalloc(&dbad, 4); 74 cudaMalloc(&dchk, 4); 75 cudaMemcpy(dw, w.data(), w.size(), cudaMemcpyHostToDevice); 76 cudaMemcpy(dy, y.data(), y.size() * sizeof(block_q8_1), cudaMemcpyHostToDevice); 77 cudaMemset(dbad, 0, 4); 78 cudaMemset(dchk, 0, 4); 79 k_cmp<T><<<(nblocks * 8 + 255) / 256, 256>>>(dw, dy, nblocks, dbad, dchk); 80 cudaDeviceSynchronize(); 81 unsigned bad = 0, chk = 0; 82 cudaMemcpy(&bad, dbad, 4, cudaMemcpyDeviceToHost); 83 cudaMemcpy(&chk, dchk, 4, cudaMemcpyDeviceToHost); 84 std::printf("%s: %u dots compared, %u differ%s\n", name, chk, bad, bad ? " <-- MISMATCH" : ""); 85 cudaFree(dw); cudaFree(dy); cudaFree(dbad); cudaFree(dchk); 86 return bad == 0 && chk == (unsigned) nblocks * 8; 87} 88 89} // namespace 90 91int main(int argc, char** argv) { 92 const int n = argc > 1 ? std::atoi(argv[1]) : 1 << 20; 93 bool ok = run<0>("IQ3_S", n, 1) & run<1>("IQ3_XXS", n, 2) & run<2>("IQ2_S", n, 3); 94 std::printf(ok ? "iqdot_test: PASS\n" : "iqdot_test: FAIL\n"); 95 return ok ? 0 : 1; 96}