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
109 lines
5.9 KiB
C++
109 lines
5.9 KiB
C++
#include "kar_expert.hpp"
|
|
#include <charconv>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <limits>
|
|
#include <memory>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <fcntl.h>
|
|
#include <sys/mman.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
namespace {
|
|
uint32_t number(const char* s) {
|
|
uint32_t n;
|
|
const auto [p,e]=std::from_chars(s,s+std::strlen(s),n);
|
|
if(e!=std::errc() || *p) throw std::runtime_error("invalid unsigned argument");
|
|
return n;
|
|
}
|
|
struct Mapping {
|
|
int fd=-1; size_t size=0; const uint8_t* data=nullptr;
|
|
explicit Mapping(const char* path) {
|
|
fd=open(path,O_RDONLY|O_CLOEXEC); struct stat st{};
|
|
if(fd<0 || fstat(fd,&st)!=0 || !S_ISREG(st.st_mode) || st.st_size<=0) {
|
|
if(fd>=0) close(fd); fd=-1; throw std::runtime_error("mapping stat failed");
|
|
}
|
|
size=static_cast<size_t>(st.st_size);
|
|
void* p=mmap(nullptr,size,PROT_READ,MAP_PRIVATE,fd,0);
|
|
if(p==MAP_FAILED) { close(fd);fd=-1;throw std::runtime_error("mapping failed"); }
|
|
data=static_cast<const uint8_t*>(p);
|
|
}
|
|
~Mapping(){if(data)munmap(const_cast<uint8_t*>(data),size);if(fd>=0)close(fd);}
|
|
};
|
|
void write(const std::filesystem::path& p,const std::vector<float>& v) {
|
|
std::ofstream f(p,std::ios::binary); f.write(reinterpret_cast<const char*>(v.data()),v.size()*4);
|
|
if(!f)throw std::runtime_error("private evidence write failed");
|
|
}
|
|
}
|
|
int main(int argc,char** argv) try {
|
|
if(argc!=7) {
|
|
std::fprintf(stderr,"usage: strata-kar-expert-probe ARCHIVE LAYER EXPERT LEVEL TOKENS NEW_PRIVATE_OUTPUT\n");return 2;
|
|
}
|
|
umask(0077);
|
|
const uint32_t layer=number(argv[2]),expert=number(argv[3]),level=number(argv[4]),tokens=number(argv[5]);
|
|
if(layer>4096 || tokens<1 || tokens>8)throw std::runtime_error("layer/token bound");
|
|
const std::filesystem::path output(argv[6]);
|
|
if(!std::filesystem::create_directory(output))throw std::runtime_error("output already exists");
|
|
kar_archive* raw=nullptr;
|
|
if(kar_open(argv[1],KAR_OPEN_VERIFY_PAYLOAD,&raw)!=KAR_OK)throw std::runtime_error(kar_last_error());
|
|
std::unique_ptr<kar_archive,decltype(&kar_close)> archive(raw,kar_close);
|
|
const char* suffix[]={"gate","up","down"}; const kar_tensor_info* t[3];
|
|
for(int i=0;i<3;++i) {
|
|
std::string name="blk."+std::to_string(layer)+".ffn_"+suffix[i]+"_exps.weight";
|
|
const int index=kar_find(raw,name.c_str());
|
|
if(index<0)throw std::runtime_error("missing expert tensor");
|
|
t[i]=kar_tensor(raw,index);
|
|
}
|
|
if(t[0]->dims[0]>16384 || t[0]->dims[1]>16384)throw std::runtime_error("expert geometry cap");
|
|
const uint32_t hidden=t[0]->dims[0],ff=t[0]->dims[1];
|
|
Mapping file(argv[1]); strata::kar::ResidentExpert resident; std::string error;
|
|
if(!resident.open(hidden,ff,level,8,error) ||
|
|
!resident.stage(file.data,file.size,*t[0],*t[1],*t[2],expert,error))throw std::runtime_error(error);
|
|
std::vector<float> matrix(uint64_t(hidden)*ff),input(uint64_t(tokens)*hidden),result(input.size());
|
|
for(int role=0;role<3;++role) {
|
|
if(!resident.inspect_matrix(role,matrix.data(),error))throw std::runtime_error(error);
|
|
write(output/(std::string(suffix[role])+".f32"),matrix);
|
|
}
|
|
for(size_t i=0;i<input.size();++i)input[i]=float(int(i%31)-15)/32.0f;
|
|
write(output/"input.f32",input);
|
|
if(resident.finish(result.data(),error))throw std::runtime_error("finish without run accepted");
|
|
if(!resident.run(input.data(),tokens,error))throw std::runtime_error(error);
|
|
if(resident.run(input.data(),tokens,error))throw std::runtime_error("overlapping run accepted");
|
|
if(resident.stage(file.data,file.size,*t[0],*t[1],*t[2],expert,error))throw std::runtime_error("pending stage accepted");
|
|
if(!resident.finish(result.data(),error))throw std::runtime_error(error);
|
|
write(output/"output.f32",result);
|
|
std::vector<float> repeat(result.size());
|
|
if(!resident.run(input.data(),tokens,error) || !resident.finish(repeat.data(),error) || repeat!=result)
|
|
throw std::runtime_error("resident reuse failed");
|
|
if(resident.stage(file.data,file.size,*t[0],*t[1],*t[2],uint32_t(t[0]->dims[2]),error))
|
|
throw std::runtime_error("out-of-range expert accepted");
|
|
if(resident.run(input.data(),tokens,error))throw std::runtime_error("run after rejected stage accepted");
|
|
if(!resident.stage(file.data,file.size,*t[0],*t[1],*t[2],expert,error))throw std::runtime_error(error);
|
|
input[0]=std::numeric_limits<float>::infinity();
|
|
if(resident.run(input.data(),tokens,error))throw std::runtime_error("nonfinite input accepted");
|
|
auto invalid=*t[0]; invalid.books_offset=std::numeric_limits<uint64_t>::max()-3;
|
|
if(resident.stage(file.data,file.size,invalid,*t[1],*t[2],expert,error))
|
|
throw std::runtime_error("overflowing metadata accepted");
|
|
if(resident.stage(file.data,32,*t[0],*t[1],*t[2],expert,error))
|
|
throw std::runtime_error("truncated archive accepted");
|
|
std::ofstream meta(output/"metadata.json");
|
|
meta<<"{\"hidden\":"<<hidden<<",\"intermediate\":"<<ff<<",\"expert\":"<<expert
|
|
<<",\"level\":"<<level<<",\"tokens\":"<<tokens<<",\"resident_bytes\":"<<resident.resident_bytes()
|
|
<<",\"scratch_bytes\":"<<resident.scratch_bytes()<<",\"state_fault_checks\":8,\"tensors\":[";
|
|
for(int i=0;i<3;++i) {
|
|
if(i)meta<<',';
|
|
meta<<"{\"name\":\""<<t[i]->name<<"\",\"books_offset\":"<<t[i]->books_offset
|
|
<<",\"scales_offset\":"<<t[i]->scales_offset<<",\"planes_offset\":"<<t[i]->planes_offset
|
|
<<",\"plane_stride_bytes\":"<<t[i]->plane_stride_bytes<<"}";
|
|
}
|
|
meta<<"]}\n";if(!meta)throw std::runtime_error("metadata write failed");
|
|
std::printf("resident_bytes=%llu scratch_bytes=%llu tokens=%u; private oracle comparison pending\n",
|
|
(unsigned long long)resident.resident_bytes(),(unsigned long long)resident.scratch_bytes(),tokens);
|
|
return 0;
|
|
} catch(const std::exception& e) {std::fprintf(stderr,"expert probe: %s\n",e.what());return 1;}
|