okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
d689af2de
main
1// The prompt kernel's weight tiles (src/moe_mma.cuh, src/moe_mma_iq.cuh) against the file: for one expert of each routed-expert type a 2// GGUF holds, runs the tiles' fetch() and put() over the whole matrix on the GPU and writes the f16 values they put into shared memory. 3// tests/split/tile_check.py compares those with gguf-py's dequantization of the same expert (rounded once to f16). 4// bin/moe_tile_test MODEL.gguf OUTDIR [expert ...] writes OUTDIR/<tensor>.e<expert>.f16 and prints "name type nrows ncols expert" 5#include "kernels.hpp" 6 7#include <cuda_fp16.h> 8#include <cuda_runtime.h> 9#include <mma.h> 10 11#include <algorithm> 12#include <cstdio> 13#include <cstdlib> 14#include <cstring> 15#include <map> 16#include <stdexcept> 17#include <string> 18#include <vector> 19 20#include "gguf.hpp" 21#include "qdot.cuh" // block layouts and codebook tables 22 23namespace fnx { 24namespace { 25 26__device__ __forceinline__ float silu_(float x) { return x / (1.0f + __expf(-x)); } 27 28#include "moe_w.cuh" 29#include "moe_mma.cuh" 30#include "moe_mma_iq.cuh" 31 32template <int TYPE> 33__global__ void __launch_bounds__(MM_THREADS) k_tiles(const char* __restrict__ we, size_t row_a, int nrows, int ncols, __half* __restrict__ out) { 34 extern __shared__ __align__(32) __half smem[]; 35 constexpr int NP = MM_BM / MM_PASS; 36 const int row0 = blockIdx.x * MM_BM; 37 for (int k0 = 0; k0 < ncols; k0 += MM_KC) { 38 const int kc = min(MM_KC, ncols - k0); 39 MmTile<TYPE> wt[NP]; 40#pragma unroll 41 for (int p = 0; p < NP; ++p) wt[p].fetch(we, row_a, nrows, row0 + p * MM_PASS, k0, kc); 42#pragma unroll 43 for (int p = 0; p < NP; ++p) wt[p].put(k0, kc, smem + p * MM_PASS * MM_LD); 44 __syncthreads(); 45 for (int e = threadIdx.x; e < MM_BM * kc; e += MM_THREADS) { 46 const int r = e / kc, c = e % kc, row = row0 + r; 47 if (row < nrows) out[(size_t) row * ncols + k0 + c] = smem[r * MM_LD + c]; 48 } 49 __syncthreads(); 50 } 51} 52 53template <int TYPE> 54void run(const char* d_we, size_t row_a, int nrows, int ncols, __half* d_out) { 55 k_tiles<TYPE><<<(nrows + MM_BM - 1) / MM_BM, MM_THREADS, MM_BM * MM_LD * sizeof(__half)>>>(d_we, row_a, nrows, ncols, d_out); 56} 57 58} // namespace 59} // namespace fnx 60 61using namespace fnx; 62 63int main(int argc, char** argv) { 64 if (argc < 3) { 65 std::fprintf(stderr, "usage: moe_tile_test MODEL.gguf OUTDIR [expert ...]\n"); 66 return 2; 67 } 68 try { 69 Gguf g(argv[1]); 70 std::vector<int> experts; 71 for (int i = 3; i < argc; ++i) experts.push_back(std::atoi(argv[i])); 72 if (experts.empty()) experts = {5, 511}; 73 std::map<uint32_t, std::string> pick; // the first routed-expert tensor of each type, gate/up first 74 for (const auto& kv : g.tensors()) { 75 const std::string& n = kv.first; 76 if (n.find("_exps.weight") == std::string::npos) continue; 77 const uint32_t ty = kv.second.type; 78 if (!pick.count(ty)) pick[ty] = n; 79 } 80 // also a down matrix of each type that has both, as its K (640) is not a multiple of 256 81 for (const auto& kv : g.tensors()) { 82 const std::string& n = kv.first; 83 if (n.find("ffn_down_exps.weight") == std::string::npos) continue; 84 const std::string key = "down:" + std::to_string(kv.second.type); 85 static std::map<std::string, bool> seen; 86 if (!seen[key]) { 87 seen[key] = true; 88 pick[1000 + kv.second.type] = n; 89 } 90 } 91 for (const auto& pk : pick) { 92 const Tensor& t = g.t(pk.second); 93 const int ncols = (int) t.ne[0], nrows = (int) t.ne[1]; 94 const size_t row_a = t.row_bytes(); 95 for (int e : experts) { 96 if (e >= t.ne[2]) continue; 97 const char* we = (const char*) t.data + (size_t) e * row_a * nrows; 98 char* d_we; 99 __half* d_out; 100 if (cudaMalloc(&d_we, row_a * nrows) || cudaMalloc(&d_out, (size_t) nrows * ncols * 2)) throw std::runtime_error("cudaMalloc"); 101 cudaMemcpy(d_we, we, row_a * nrows, cudaMemcpyHostToDevice); 102 cudaMemset(d_out, 0xFF, (size_t) nrows * ncols * 2); 103 switch (t.type) { 104 case T_Q4_0: run<T_Q4_0>(d_we, row_a, nrows, ncols, d_out); break; 105 case T_IQ4_NL: run<T_IQ4_NL>(d_we, row_a, nrows, ncols, d_out); break; 106 case T_IQ4_XS: run<T_IQ4_XS>(d_we, row_a, nrows, ncols, d_out); break; 107 case T_IQ3_S: run<T_IQ3_S>(d_we, row_a, nrows, ncols, d_out); break; 108 case T_IQ3_XXS: run<T_IQ3_XXS>(d_we, row_a, nrows, ncols, d_out); break; 109 case T_IQ2_S: run<T_IQ2_S>(d_we, row_a, nrows, ncols, d_out); break; 110 case T_Q4_K: run<T_Q4_K>(d_we, row_a, nrows, ncols, d_out); break; 111 case T_Q5_K: run<T_Q5_K>(d_we, row_a, nrows, ncols, d_out); break; 112 case T_Q5_1: run<T_Q5_1>(d_we, row_a, nrows, ncols, d_out); break; 113 case T_Q8_0: run<T_Q8_0>(d_we, row_a, nrows, ncols, d_out); break; 114 default: std::printf("skip %s type %u\n", t.name.c_str(), t.type); cudaFree(d_we); cudaFree(d_out); continue; 115 } 116 if (cudaDeviceSynchronize() != cudaSuccess) throw std::runtime_error(std::string("kernel: ") + cudaGetErrorString(cudaGetLastError())); 117 std::vector<__half> h((size_t) nrows * ncols); 118 cudaMemcpy(h.data(), d_out, h.size() * 2, cudaMemcpyDeviceToHost); 119 cudaFree(d_we); 120 cudaFree(d_out); 121 const std::string path = std::string(argv[2]) + "/" + t.name + ".e" + std::to_string(e) + ".f16"; 122 FILE* fp = std::fopen(path.c_str(), "wb"); 123 if (!fp) throw std::runtime_error("cannot write " + path); 124 std::fwrite(h.data(), 2, h.size(), fp); 125 std::fclose(fp); 126 std::printf("%s %u %d %d %d\n", t.name.c_str(), t.type, nrows, ncols, e); 127 } 128 } 129 } catch (const std::exception& e) { 130 std::fprintf(stderr, "moe_tile_test: %s\n", e.what()); 131 return 1; 132 } 133 return 0; 134}