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
Desktop foundation / core (ubuntu-latest) (push) Failing after 28s
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
113 lines
5.6 KiB
C++
113 lines
5.6 KiB
C++
#include "kar_expert_miss.hpp"
|
|
#include "kar_expert.hpp"
|
|
#include <algorithm>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <vector>
|
|
#include <fcntl.h>
|
|
#include <sys/mman.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
namespace strata::kar {
|
|
struct ExpertMiss::Impl {
|
|
int fd=-1;
|
|
const uint8_t* mapping=nullptr;
|
|
size_t bytes=0;
|
|
kar_archive* archive=nullptr;
|
|
uint32_t layers=0, experts=0, hidden=0, ff=0;
|
|
uint64_t computed=0;
|
|
std::vector<const kar_tensor_info*> records;
|
|
std::vector<float> input, output;
|
|
ResidentExpert resident;
|
|
~Impl() {
|
|
if(mapping) munmap(const_cast<uint8_t*>(mapping),bytes);
|
|
if(fd>=0) close(fd);
|
|
if(archive) kar_close(archive);
|
|
}
|
|
};
|
|
ExpertMiss::ExpertMiss() = default;
|
|
ExpertMiss::~ExpertMiss() {
|
|
if(impl_)std::fprintf(stderr,"strata KAR experimental misses: %llu entries computed; compressed=%llu scratch=%llu bytes\n",
|
|
(unsigned long long)impl_->computed,(unsigned long long)impl_->resident.resident_bytes(),
|
|
(unsigned long long)impl_->resident.scratch_bytes());
|
|
}
|
|
uint64_t ExpertMiss::computed() const {return impl_?impl_->computed:0;}
|
|
bool ExpertMiss::open(const char* path,uint32_t layers,uint32_t experts,uint32_t hidden,
|
|
uint32_t ff,uint32_t level,std::string& error) {
|
|
if(impl_ || !path || !*path || layers<1 || layers>512 || experts<1 || experts>65536) {
|
|
error="invalid KAR miss configuration";return false;
|
|
}
|
|
auto p=std::make_unique<Impl>();
|
|
// Keep a descriptor to the exact inode used by both mappings. Replacing the
|
|
// configured path during validation cannot select a different payload.
|
|
p->fd=::open(path,O_RDONLY|O_CLOEXEC);
|
|
struct stat st{};
|
|
if(p->fd<0 || fstat(p->fd,&st)!=0 || !S_ISREG(st.st_mode) || st.st_size<=0) {
|
|
error="KAR miss archive is not a readable regular file";return false;
|
|
}
|
|
const std::string pinned="/proc/self/fd/"+std::to_string(p->fd);
|
|
if(kar_open(pinned.c_str(),KAR_OPEN_VERIFY_PAYLOAD,&p->archive)!=KAR_OK) {
|
|
error=std::string("KAR miss archive validation: ")+kar_last_error();return false;
|
|
}
|
|
p->bytes=static_cast<size_t>(st.st_size);
|
|
void* mapped=mmap(nullptr,p->bytes,PROT_READ,MAP_PRIVATE,p->fd,0);
|
|
if(mapped==MAP_FAILED) {error="cannot map KAR miss archive";return false;}
|
|
p->mapping=static_cast<const uint8_t*>(mapped);
|
|
p->layers=layers;p->experts=experts;p->hidden=hidden;p->ff=ff;
|
|
const char* roles[]={"gate","up","down"};
|
|
for(uint32_t layer=0;layer<layers;++layer)for(uint32_t role=0;role<3;++role) {
|
|
const std::string name="blk."+std::to_string(layer)+".ffn_"+roles[role]+"_exps.weight";
|
|
const int index=kar_find(p->archive,name.c_str());
|
|
if(index<0) {error="missing KAR expert tensor: "+name;return false;}
|
|
const auto* t=kar_tensor(p->archive,index);
|
|
if(t->n_dims!=3 || t->dims[0]!=(role==2?ff:hidden) ||
|
|
t->dims[1]!=(role==2?hidden:ff) || t->dims[2]!=experts ||
|
|
t->group!=32 || t->storage_class!=KAR_NESTED || t->nesting!=KAR_TOPDOWN ||
|
|
level<t->planes_min || level>t->planes_max) {
|
|
error="KAR expert tensor geometry/level mismatch: "+name;return false;
|
|
}
|
|
p->records.push_back(t);
|
|
}
|
|
if(!p->resident.open(hidden,ff,level,8,error))return false;
|
|
p->input.resize(size_t(hidden)*8);p->output.resize(size_t(hidden)*8);
|
|
std::fprintf(stderr,"strata KAR experimental: native verifier misses (short prompt and decode), level=%u, compressed=%llu scratch=%llu; quality not qualified\n",
|
|
level,(unsigned long long)p->resident.resident_bytes(),(unsigned long long)p->resident.scratch_bytes());
|
|
impl_=std::move(p);return true;
|
|
}
|
|
bool ExpertMiss::compute(int64_t layer,const float* input,const int32_t* ids,int64_t tokens,
|
|
int64_t k,int32_t* kind,float* output,std::string& error) {
|
|
if(!impl_ || layer<0 || layer>=impl_->layers || tokens<1 || tokens>8 || k<1 || k>16 ||
|
|
!input || !ids || !kind || !output) {error="invalid KAR miss dispatch";return false;}
|
|
auto& p=*impl_;
|
|
for(int64_t i=0;i<tokens*k;++i)
|
|
if(ids[i]<0 || ids[i]>=int64_t(p.experts)) {error="KAR routed expert out of range";return false;}
|
|
for(int64_t i=0;i<tokens*k;++i) {
|
|
if(kind[i]>=0)continue;
|
|
const uint32_t expert=ids[i];
|
|
int64_t positions[8];uint32_t count=0;
|
|
// Native top-k routing never duplicates an expert within one token.
|
|
for(int64_t j=i;j<tokens*k;++j)if(kind[j]<0 && ids[j]==int32_t(expert)) {
|
|
if(count==8) {error="duplicate/oversized KAR routed expert group";return false;}
|
|
positions[count]=j;
|
|
std::memcpy(p.input.data()+size_t(count)*p.hidden,input+size_t(j/k)*p.hidden,p.hidden*4);
|
|
++count;
|
|
}
|
|
const auto off=size_t(layer)*3;
|
|
if(!p.resident.stage(p.mapping,p.bytes,*p.records[off],*p.records[off+1],*p.records[off+2],expert,error) ||
|
|
!p.resident.run(p.input.data(),count,error) || !p.resident.finish(p.output.data(),error))return false;
|
|
for(uint32_t t=0;t<count;++t) {
|
|
std::memcpy(output+size_t(positions[t])*p.hidden,p.output.data()+size_t(t)*p.hidden,p.hidden*4);
|
|
kind[positions[t]]=3; // owned host result; the native pool must not zero it
|
|
++p.computed;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
bool ExpertMiss::dispatch(void* user,int64_t layer,const float* input,const int32_t* ids,
|
|
int64_t tokens,int64_t k,int32_t* kind,float* output,std::string& error) {
|
|
if(!user) {error="missing KAR miss owner";return false;}
|
|
return static_cast<ExpertMiss*>(user)->compute(layer,input,ids,tokens,k,kind,output,error);
|
|
}
|
|
}
|