Files
leleka/scripts/cuda-projection-family-oracle/native_fixture.cu
T

61 lines
5.6 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 "strata/kernels/iq_kernels.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_COMMON_QUANTIZER_PTX");
std::ifstream in(argv[1],std::ios::binary);uint32_t h[4];in.read(reinterpret_cast<char*>(h),sizeof h);
if(!in||h[0]!=0x31464b51||h[1]>9||h[2]<1||h[2]>10240||h[3]<32||h[3]>6144||h[3]%32)throw std::runtime_error("bounded synthetic geometry");
const auto mode=h[1],m=h[2],n=h[3];
static const unsigned be[]={32,256,256,256,256,32,32,256,256,64};
static const unsigned bb[]={34,144,98,110,82,18,18,136,136,18};
if(n%be[mode])throw std::runtime_error("format block geometry");
size_t wb=size_t(m)*(n/be[mode])*bb[mode],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_quantize_q8_1(static_cast<float*>(dx),df,n,1,stream);
strata::kernels::iq_set_old_kernels(true); // exact original row_dot, not grouped expert combine
switch(mode){
case 0:strata::kernels::native_q8_0_f32(dw,static_cast<float*>(dx),df,static_cast<float*>(dy),n,m,1,stream);break;
case 1:strata::kernels::native_q4_k_f32(dw,static_cast<float*>(dx),df,static_cast<float*>(dy),n,m,1,stream);break;
case 5:strata::kernels::native_iq4_nl_f32(dw,static_cast<float*>(dx),df,static_cast<float*>(dy),n,m,1,stream);break;
case 7:strata::kernels::native_iq4_xs_f32(dw,static_cast<float*>(dx),df,static_cast<float*>(dy),n,m,1,stream);break;
default:{static const int tag[]={8,12,18,21,22,20,20,23,23,42};
strata::kernels::iq_mmvq(tag[mode],dw,df,static_cast<float*>(dy),n,m,1,stream);break;}
}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||ptx.find(char(0))!=std::string::npos)throw std::runtime_error("bounded trusted test asset");for(unsigned char c:ptx)if(c>127)throw std::runtime_error("trusted PTX must be ASCII");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[]={0x31464f51,mode,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;}