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
88 lines
4.3 KiB
C++
88 lines
4.3 KiB
C++
#include "kar_expert.hpp"
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <stdexcept>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
void require(bool ok, const char* what) { if(!ok)throw std::runtime_error(what); }
|
|
void u16(std::vector<uint8_t>& b,size_t at,uint16_t n) { b[at]=n; b[at+1]=n>>8; }
|
|
void u32(std::vector<uint8_t>& b,size_t at,uint32_t n) {
|
|
for(int i=0;i<4;++i)b[at+i]=n>>(8*i);
|
|
}
|
|
}
|
|
int main() try {
|
|
constexpr uint32_t H=64,F=32,E=2,W=H*F/32;
|
|
// Independent synthetic decoded weights are +/-1 times 1/32. Each of
|
|
// three roles and two experts has its own alternating bit pattern.
|
|
std::vector<uint8_t> archive(3*4096,0);
|
|
kar_tensor_info tensors[3]{};
|
|
std::vector<float> reference[3];
|
|
for(uint32_t role=0;role<3;++role) {
|
|
auto& t=tensors[role]; const size_t base=role*4096;
|
|
t.storage_class=KAR_NESTED;t.nesting=KAR_TOPDOWN;t.group=32;t.n_dims=3;
|
|
t.dims[0]=role==2?F:H;t.dims[1]=role==2?H:F;t.dims[2]=E;
|
|
t.n_elements=H*F*E;t.planes_min=t.planes_max=1;t.planes_stored=1;
|
|
t.books_offset=base;t.scales_offset=base+32;t.planes_offset=base+512;
|
|
t.plane_stride_bytes=W*E*4;
|
|
u16(archive,base,0xbc00);u16(archive,base+2,0x3c00); // -1,+1
|
|
reference[role].resize(H*F*E);
|
|
for(uint32_t word=0;word<W*E;++word) {
|
|
u16(archive,t.scales_offset+word*2,0x2800); // exact 1/32
|
|
uint32_t bits=0;
|
|
for(uint32_t bit=0;bit<32;++bit) {
|
|
const uint32_t index=word*32+bit;
|
|
const bool positive=((index*7 + index/11 + role*3 + word/W)%13)<6;
|
|
if(positive)bits|=1u<<bit;
|
|
reference[role][index]=(positive?1.0f:-1.0f)/32;
|
|
}
|
|
u32(archive,t.planes_offset+word*4,bits);
|
|
}
|
|
}
|
|
std::string error;
|
|
strata::kar::ResidentExpert invalid;
|
|
require(!invalid.open(63,F,1,8,error),"unaligned hidden accepted");
|
|
require(!invalid.open(H,F,0,8,error),"zero level accepted");
|
|
require(!invalid.open(H,F,1,9,error),"oversized batch accepted");
|
|
strata::kar::ResidentExpert expert;
|
|
require(expert.open(H,F,1,8,error),error.c_str());
|
|
require(!expert.open(H,F,1,8,error),"double open accepted");
|
|
std::vector<float> matrix(H*F),input(8*H),output(8*H);
|
|
for(size_t i=0;i<input.size();++i)input[i]=float(int(i%17)-8)/16;
|
|
for(uint32_t id : {0u,1u,0u}) {
|
|
require(expert.stage(archive.data(),archive.size(),tensors[0],tensors[1],tensors[2],id,error),error.c_str());
|
|
for(uint32_t role=0;role<3;++role) {
|
|
require(expert.inspect_matrix(role,matrix.data(),error),error.c_str());
|
|
require(std::memcmp(matrix.data(),reference[role].data()+id*H*F,H*F*4)==0,"decode byte mismatch");
|
|
}
|
|
for(uint32_t tokens : {1u,8u}) {
|
|
require(expert.run(input.data(),tokens,error),error.c_str());
|
|
require(!expert.run(input.data(),tokens,error),"overlapping run accepted");
|
|
require(expert.finish(output.data(),error),error.c_str());
|
|
for(uint32_t token=0;token<tokens;++token) {
|
|
std::vector<double> ff(F);
|
|
for(uint32_t row=0;row<F;++row) {
|
|
double g=0,u=0;
|
|
for(uint32_t c=0;c<H;++c) {
|
|
g+=double(reference[0][id*H*F+row*H+c])*input[token*H+c];
|
|
u+=double(reference[1][id*H*F+row*H+c])*input[token*H+c];
|
|
}
|
|
ff[row]=g/(1+std::exp(-g))*u;
|
|
}
|
|
for(uint32_t row=0;row<H;++row) {
|
|
double expected=0;
|
|
for(uint32_t c=0;c<F;++c)expected+=reference[2][id*H*F+row*F+c]*ff[c];
|
|
require(std::isfinite(output[token*H+row]) &&
|
|
std::abs(output[token*H+row]-expected)<1e-6,"multiply oracle mismatch");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
auto bad=tensors[0];bad.books_offset=UINT64_MAX-1;
|
|
require(!expert.stage(archive.data(),archive.size(),bad,tensors[1],tensors[2],0,error),"wrapped offset accepted");
|
|
require(!expert.run(input.data(),1,error),"run after failed stage accepted");
|
|
std::puts("PASS: exact matrix bytes, FP64 multiply tolerance1e-6, 1/8 tokens, expert0->1->0, geometry/state/overflow guards");
|
|
return 0;
|
|
} catch(const std::exception& e) {std::fprintf(stderr,"KAR expert test: %s\n",e.what());return 1;}
|