Files
leleka/scripts/cuda-q6-oracle/native_fixture.cu
T

47 lines
4.5 KiB
Plaintext

// Independent test-only runner. Compile the ORIGINAL native_mmvq.cu unchanged.
// Does not decode native weights, share candidate scratch, or select arbitrary entries.
#include "strata/kernels/native_mmvq.hpp"
#include <cuda_runtime.h>
#include <cuda.h>
#include <dlfcn.h>
#include <fstream>
#include <iterator>
#include <vector>
#include <stdexcept>
#include <iostream>
#include <cstdint>
#include <cstring>
#include <cmath>
static void ck(cudaError_t e){if(e!=cudaSuccess)throw std::runtime_error(cudaGetErrorString(e));}
static void dc(CUresult e){if(e!=CUDA_SUCCESS)throw std::runtime_error("Driver test fixture status "+std::to_string(e));}
template<class T> static T sym(void* lib,const char* name){void* p=dlsym(lib,name);if(!p)throw std::runtime_error(name);return reinterpret_cast<T>(p);}
int main(int argc,char**argv) try {
if(argc!=4)throw std::runtime_error("Usage: native_fixture INPUT OUTPUT TRUSTED_Q6_PTX");
std::ifstream in(argv[1],std::ios::binary);uint32_t h[3];in.read(reinterpret_cast<char*>(h),sizeof h);
if(!in||h[0]!=0x36314b51||h[1]<1||h[1]>10240||h[2]<256||h[2]>6144||h[2]%256)throw std::runtime_error("bounded synthetic geometry");
const auto m=h[1],n=h[2];size_t wb=size_t(m)*(n/256)*210,qb=size_t(n/32)*36;
std::vector<unsigned char>w(wb),q(qb),fresh(qb),candidate(qb);std::vector<float>x(n),y(m);
in.read(reinterpret_cast<char*>(w.data()),wb);in.read(reinterpret_cast<char*>(x.data()),n*4);if(!in||in.peek()!=EOF)throw std::runtime_error("fixture length");
// Invalid activation is tested in Rust/candidate admission; never feed it into
// the original unchecked native float-to-integer quantizer.
for(float v:x)if(!std::isfinite(v))throw std::runtime_error("native fixture requires finite input");
ck(cudaSetDevice(0));cudaStream_t stream;ck(cudaStreamCreate(&stream));void *dw,*dx,*dq,*df,*dcq,*dy,*ds;
ck(cudaMalloc(&dw,wb));ck(cudaMalloc(&dx,n*4));ck(cudaMalloc(&dq,qb));ck(cudaMalloc(&df,qb));ck(cudaMalloc(&dcq,qb));ck(cudaMalloc(&dy,m*4));ck(cudaMalloc(&ds,4));
ck(cudaMemcpy(dw,w.data(),wb,cudaMemcpyHostToDevice));ck(cudaMemcpy(dx,x.data(),n*4,cudaMemcpyHostToDevice));
strata::kernels::native_quantize_q8_1(static_cast<float*>(dx),dq,n,1,stream);ck(cudaStreamSynchronize(stream));ck(cudaMemcpy(q.data(),dq,qb,cudaMemcpyDeviceToHost));
// Composition independently quantizes a second fresh native scratch allocation.
strata::kernels::native_q6_k_f32(dw,static_cast<float*>(dx),df,static_cast<float*>(dy),n,m,1,stream);ck(cudaStreamSynchronize(stream));
ck(cudaMemcpy(fresh.data(),df,qb,cudaMemcpyDeviceToHost));ck(cudaMemcpy(y.data(),dy,m*4,cudaMemcpyDeviceToHost));if(q!=fresh)throw std::runtime_error("fresh native quantization disagrees");
std::ifstream pt(argv[3],std::ios::binary);std::string ptx((std::istreambuf_iterator<char>(pt)),{});if(ptx.empty()||ptx.size()>262144)throw std::runtime_error("bounded trusted test asset");ptx.push_back(0);
void* lib=dlopen("libcuda.so.1",RTLD_NOW|RTLD_LOCAL);if(!lib)throw std::runtime_error("installed Driver missing");
auto load=sym<CUresult(*)(CUmodule*,const void*)>(lib,"cuModuleLoadData");auto get=sym<CUresult(*)(CUfunction*,CUmodule,const char*)>(lib,"cuModuleGetFunction");
auto launch=sym<CUresult(*)(CUfunction,unsigned,unsigned,unsigned,unsigned,unsigned,unsigned,unsigned,CUstream,void**,void**)>(lib,"cuLaunchKernel");auto unload=sym<CUresult(*)(CUmodule)>(lib,"cuModuleUnload");
CUmodule mod;CUfunction f;dc(load(&mod,ptx.data()));dc(get(&f,mod,"leleka_quantize_q8_1"));ck(cudaMemset(ds,0,4));
CUdeviceptr px=reinterpret_cast<CUdeviceptr>(dx),pq=reinterpret_cast<CUdeviceptr>(dcq),ps=reinterpret_cast<CUdeviceptr>(ds);uint32_t cols=n;void*args[]={&px,&pq,&ps,&cols};
dc(launch(f,(n+255)/256,1,1,256,1,1,0,reinterpret_cast<CUstream>(stream),args,nullptr));ck(cudaStreamSynchronize(stream));
uint32_t status;ck(cudaMemcpy(&status,ds,4,cudaMemcpyDeviceToHost));ck(cudaMemcpy(candidate.data(),dcq,qb,cudaMemcpyDeviceToHost));dc(unload(mod));dlclose(lib);
std::ofstream out(argv[2],std::ios::binary);uint32_t oh[]={0x36314f51,m,n,status};out.write(reinterpret_cast<char*>(oh),sizeof oh);out.write(reinterpret_cast<char*>(q.data()),qb);out.write(reinterpret_cast<char*>(candidate.data()),qb);out.write(reinterpret_cast<char*>(y.data()),m*4);if(!out)throw std::runtime_error("output write");
for(void*p:{dw,dx,dq,df,dcq,dy,ds})ck(cudaFree(p));ck(cudaStreamDestroy(stream));ck(cudaDeviceReset());
std::cout<<"NATIVE_ORACLE rows="<<m<<" columns="<<n<<" q8_bytes="<<qb<<" status="<<status<<" independent_scratch=true cleanup=ok\n";return 0;
} catch(const std::exception&e){std::cerr<<e.what()<<'\n';return 1;}