// Independent CPU reference for query routing and physical V-only attention. #include "strata/core/keyless.hpp" #include "strata/core/layer.hpp" #include "strata/kernels/qsa_decode_attn.hpp" #include "strata/kernels/f16_bits.hpp" #include "strata/kernels/kv_stream.hpp" #include #include #include #include #include #include namespace { void check(cudaError_t e) { if (e != cudaSuccess) throw std::runtime_error(cudaGetErrorString(e)); } template T* device(const std::vector& values) { T* p; check(cudaMalloc((void**)&p, values.size()*sizeof(T))); check(cudaMemcpy(p, values.data(), values.size()*sizeof(T), cudaMemcpyHostToDevice)); return p; } } int main() { using namespace strata::kernels; try { int count=0; if (cudaGetDeviceCount(&count)!=cudaSuccess || count==0) return 77; constexpr int H=24, D=256, KV=2, CELLS=96, PAGE=4, BATCH=3, CAP=96; std::vector query(BATCH*H*D), routing(H*D*D), routed(query.size()), co(CELLS*32), si(co.size()); for (size_t i=0;i actual(query.size()); check(cudaMemcpy(actual.data(),dq,actual.size()*4,cudaMemcpyDeviceToHost)); double route_error=0; for(size_t i=0;i2e-6) throw std::runtime_error("query routing disagrees with independent matrix product"); const int pos[BATCH]={31,63,95}; for(int p=0;p unrotated=routed; std::vector positions(CELLS*3); for(int cell=0;cell table(CELLS/PAGE), ids(BATCH*CAP,-1), steps(BATCH*kStepCount); for(int page=0;page values(CELLS*KV*D); std::vector scales(CELLS*KV*4); for(int cell=0;cell scratch(BATCH*qsa_decode_attn_scratch_floats(CAP,qsa_real_shapes())), result(BATCH*H*D); float *dsc=device(scratch), *dout=device(result); qsa_decode_attn_batch(dq,pools,di,ds,CAP,qsa_real_shapes(),dsc,dout,BATCH,nullptr); check(cudaMemcpy(result.data(),dout,result.size()*4,cudaMemcpyDeviceToHost)); double error_sq=0, reference_sq=0, max_error=0; for(int b=0;b scores(width), weights(width), reference(D,0); for(int j=0;j1e-5 || max_error>1e-5) throw std::runtime_error("V-only attention disagrees with positional CPU reference"); if(kv_block_bytes(qsa_real_shapes(),kKvValueInt8)*2!=kv_block_bytes(qsa_real_shapes(),kKvInt8)) throw std::runtime_error("V-only block accounting is not half of K+V"); std::printf("mrope=%d query_max_abs_error=%.9g attention_relative_l2=%.9g attention_max_abs_error=%.9g\n",vision,route_error,l2,max_error); } std::printf("PASS: routed queries; three causal selections; permuted pages; raw value retrieval; no K allocation\n"); return 0; } catch(const std::exception& e) { std::fprintf(stderr,"keyless parity: %s\n",e.what()); return 1; } }