Desktop foundation / core (ubuntu-latest) (push) Failing after 28s
Desktop foundation / core (macos-latest) (push) Canceled after 0s
Desktop foundation / core (windows-latest) (push) Canceled after 0s
Linux / verify (push) Canceled after 0s
Linux / keyless-cuda (push) Canceled after 0s
Linux / static-cuda-package (push) Canceled after 0s
Linux / hosted-cuda-package (push) Canceled after 0s
Linux / linux-static-cli (push) Canceled after 0s
Linux / linux-cli-container (push) Canceled after 0s
Linux / release-cli (push) Canceled after 0s
120 lines
7.0 KiB
C++
120 lines
7.0 KiB
C++
// 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 <cuda_runtime.h>
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include <stdexcept>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
void check(cudaError_t e) { if (e != cudaSuccess) throw std::runtime_error(cudaGetErrorString(e)); }
|
|
template<class T> T* device(const std::vector<T>& 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<float> query(BATCH*H*D), routing(H*D*D), routed(query.size()), co(CELLS*32), si(co.size());
|
|
for (size_t i=0;i<query.size();++i) query[i]=(float)((int)(i*17%103)-51)/51;
|
|
for (int h=0;h<H;++h) for(int d=0;d<D;++d) for(int j=0;j<D;++j)
|
|
routing[((size_t)h*D+d)*D+j]=(d==j?.8f:0.f)+(float)(((h*19+d*3+j*11)%17)-8)*.001f;
|
|
for (int b=0;b<BATCH;++b) for(int h=0;h<H;++h) for(int d=0;d<D;++d) {
|
|
double sum=0; for(int j=0;j<D;++j) sum+=(double)query[(b*H+h)*D+j]*routing[(h*D+d)*D+j];
|
|
routed[(b*H+h)*D+d]=(float)sum;
|
|
}
|
|
float *dq=device(query), *da=device(routing);
|
|
keyless_route_query(dq,da,BATCH,H,D,nullptr);
|
|
std::vector<float> actual(query.size()); check(cudaMemcpy(actual.data(),dq,actual.size()*4,cudaMemcpyDeviceToHost));
|
|
double route_error=0; for(size_t i=0;i<actual.size();++i) route_error=std::max(route_error,(double)std::abs(actual[i]-routed[i]));
|
|
if(route_error>2e-6) throw std::runtime_error("query routing disagrees with independent matrix product");
|
|
const int pos[BATCH]={31,63,95};
|
|
for(int p=0;p<CELLS;++p) for(int d=0;d<32;++d) {
|
|
const double angle=p*std::pow(1e7,-2.0*d/64);
|
|
co[p*32+d]=(float)std::cos(angle); si[p*32+d]=(float)std::sin(angle);
|
|
}
|
|
const std::vector<float> unrotated=routed;
|
|
std::vector<int32_t> positions(CELLS*3);
|
|
for(int cell=0;cell<CELLS;++cell) for(int sector=0;sector<3;++sector) positions[cell*3+sector]=cell/(sector+2);
|
|
const int32_t* dp=device(positions);
|
|
for(int vision=0;vision<2;++vision) {
|
|
routed=unrotated;
|
|
for(int b=0;b<BATCH;++b) for(int h=0;h<H;++h) for(int d=0;d<32;++d) {
|
|
const int i=(b*H+h)*D+d;
|
|
const float a=routed[i], v=routed[i+32], c=co[(vision?positions[pos[b]*3+d%3]:pos[b])*32+d], s=si[(vision?positions[pos[b]*3+d%3]:pos[b])*32+d];
|
|
routed[i]=a*c-v*s; routed[i+32]=a*s+v*c;
|
|
}
|
|
check(cudaMemcpy(dq,routed.data(),routed.size()*4,cudaMemcpyHostToDevice));
|
|
std::vector<int32_t> table(CELLS/PAGE), ids(BATCH*CAP,-1), steps(BATCH*kStepCount);
|
|
for(int page=0;page<CELLS/PAGE;++page) table[page]=(page*5+7)%(CELLS/PAGE);
|
|
std::vector<int8_t> values(CELLS*KV*D);
|
|
std::vector<uint16_t> scales(CELLS*KV*4);
|
|
for(int cell=0;cell<CELLS;++cell) for(int h=0;h<KV;++h) {
|
|
const int row=(table[cell/PAGE]*KV+h)*PAGE+cell%PAGE;
|
|
for(int d=0;d<D;++d) values[row*D+d]=(int8_t)((cell*7+h*37+d*13)%127-63);
|
|
for(int d=0;d<4;++d) scales[row*4+d]=f16_from_f32(.002f*(1+h+d));
|
|
}
|
|
for(int b=0;b<BATCH;++b) {
|
|
qsa_step_fill(steps.data()+b*kStepCount,pos[b],qsa_real_shapes());
|
|
int width=0; for(int cell=0;cell<=pos[b];++cell) if(cell%3!=1) ids[b*CAP+width++]=cell;
|
|
steps[b*kStepCount+kStepWidth]=width;
|
|
}
|
|
QsaAttnPools pools;
|
|
pools.without_key=true; pools.keyless_n_rot=64; pools.keyless_mrope=vision?dp:nullptr;
|
|
pools.v_q=device(values); pools.v_scale=device(scales); pools.page_table=device(table);
|
|
pools.keyless_cos=device(co); pools.keyless_sin=device(si);
|
|
auto *di=device(ids), *ds=device(steps);
|
|
std::vector<float> 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<BATCH;++b) for(int h=0;h<H;++h) {
|
|
const int width=steps[b*kStepCount+kStepWidth], kvh=h/12;
|
|
std::vector<double> scores(width), weights(width), reference(D,0);
|
|
for(int j=0;j<width;++j) {
|
|
const int cell=ids[b*CAP+j], row=(table[cell/PAGE]*KV+kvh)*PAGE+cell%PAGE;
|
|
double sum=0;
|
|
for(int d=0;d<D;++d) {
|
|
double v=(double)values[row*D+d]*f32_from_f16(scales[row*4+d/64]);
|
|
if(d<64) {
|
|
const int other=d<32?d+32:d-32;
|
|
const double pair=(double)values[row*D+other]*f32_from_f16(scales[row*4+other/64]);
|
|
const int rotary_pos=vision?positions[cell*3+(d%32)%3]:cell;
|
|
v=v*co[rotary_pos*32+d%32]+(d<32?-1:1)*pair*si[rotary_pos*32+d%32];
|
|
}
|
|
sum+=routed[(b*H+h)*D+d]*v;
|
|
}
|
|
scores[j]=sum/16;
|
|
}
|
|
const double max=*std::max_element(scores.begin(),scores.end()); double total=0;
|
|
for(int j=0;j<width;++j) { weights[j]=std::exp(scores[j]-max); total+=weights[j]; }
|
|
for(int j=0;j<width;++j) {
|
|
const int cell=ids[b*CAP+j], row=(table[cell/PAGE]*KV+kvh)*PAGE+cell%PAGE;
|
|
for(int d=0;d<D;++d) reference[d]+=weights[j]/total*values[row*D+d]*f32_from_f16(scales[row*4+d/64]);
|
|
}
|
|
for(int d=0;d<D;++d) {
|
|
const double error=result[(b*H+h)*D+d]-reference[d];
|
|
error_sq+=error*error; reference_sq+=reference[d]*reference[d]; max_error=std::max(max_error,std::abs(error));
|
|
}
|
|
}
|
|
const double l2=std::sqrt(error_sq/reference_sq);
|
|
if(l2>1e-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; }
|
|
}
|