feat: route experimental native expert misses through bounded KAR CUDA
This commit is contained in:
@@ -61,3 +61,11 @@ target_compile_features(strata-kar-expert-test PRIVATE cxx_std_20)
|
||||
target_link_libraries(strata-kar-expert-test PRIVATE strata_kar_expert)
|
||||
add_test(NAME kar_expert_component COMMAND strata-kar-expert-test)
|
||||
set_tests_properties(kar_expert_component PROPERTIES TIMEOUT 60)
|
||||
add_library(strata_kar_miss STATIC src/kar_expert_miss.cpp)
|
||||
target_compile_features(strata_kar_miss PRIVATE cxx_std_20)
|
||||
target_link_libraries(strata_kar_miss PUBLIC strata_kar_expert strata_kar_cuda)
|
||||
add_executable(strata-kar-expert-miss-test src/kar_expert_miss_test.cpp)
|
||||
target_compile_features(strata-kar-expert-miss-test PRIVATE cxx_std_20)
|
||||
target_link_libraries(strata-kar-expert-miss-test PRIVATE strata_kar_miss)
|
||||
add_test(NAME kar_expert_miss COMMAND strata-kar-expert-miss-test "${CMAKE_BINARY_DIR}/expert-fixture.kar")
|
||||
set_tests_properties(kar_expert_miss PROPERTIES TIMEOUT 60)
|
||||
|
||||
+31
-2
@@ -10,8 +10,8 @@ one F32 matrix scratch buffer is reused for decode, multiply, SiLU and down
|
||||
projection. Allocations happen in `open`, with a fixed geometry cap and GPU
|
||||
headroom check. Its own stream/event controls ownership and completion.
|
||||
|
||||
This is a component under qualification, **not yet a serving cache-miss
|
||||
adapter**. It has no LRU scheduler, and staging waits for its own upload.
|
||||
The component has an opt-in native decode cache-miss adapter described below.
|
||||
It has no LRU scheduler, and staging waits for its own upload.
|
||||
Its F32 activation arithmetic differs from native quantized expert arithmetic.
|
||||
No default, model quality or speed claim follows from its oracle passing.
|
||||
|
||||
@@ -43,6 +43,35 @@ for compressed weights and 6,758,400 bytes for matrix/activation scratch;
|
||||
CUDA context overhead is excluded. This is one expert component, not full-model
|
||||
serving or a comparison with the original IQ3 expert.
|
||||
|
||||
### Experimental native decode misses
|
||||
|
||||
Build the engine with `STRATA_NEXT_EXPERIMENTAL_KAR_EXPERTS=ON` and
|
||||
`STRATA_NEXT_KAR_DIR` pointing to this directory. This option is off by default
|
||||
and requires Linux CUDA. Set `STRATA_NEXT_KAR_EXPERTS_ARCHIVE` to a trusted,
|
||||
immutable, separately SHA/provenance-verified target-model KAR archive and
|
||||
`STRATA_NEXT_KAR_EXPERTS_LEVEL` explicitly to its supported level (1–8).
|
||||
The reader checks directory integrity, every payload CRC and every layer's
|
||||
expert geometry before allocating its bounded GPU buffers. This integrity
|
||||
check does not authenticate the archive's pairing with the loaded GGUF.
|
||||
Large archives can take minutes to verify; configure an appropriate worker
|
||||
startup deadline. Keep both model and archive unchanged for the worker's life.
|
||||
|
||||
The adapter requires the native pack, expert pool and a single GPU. It computes
|
||||
only native decode-window misses. Native resident hits, prefill, dense weights,
|
||||
PLE and MTP retain their existing paths. It sets the PCIe miss fraction to zero;
|
||||
use `--pcie-frac 0` in the comparison baseline as well. This is a mixed precision
|
||||
experimental arm, not full KAR model residency. Each missed expert is staged
|
||||
and computed serially on the component's own nonblocking CUDA stream. Its
|
||||
completed host rows preserve routing order and bypass the CPU expert jobs.
|
||||
The bounded matrix scratch is reused; no decoded expert bank is allocated.
|
||||
The exit diagnostic reports computed route entries and explicit GPU bytes.
|
||||
|
||||
The GPU test script also uses the production Rust archive writer to make a
|
||||
model-free expert bank. It tests eight-token grouped routing against a scalar
|
||||
oracle (absolute tolerance `1e-6`), preservation of VRAM/PCIe/remote/completed
|
||||
rows, unsupported archive shape/level and invalid routed IDs. Production
|
||||
serving does not invoke the fixture generator or Python.
|
||||
|
||||
The optional `strata_next_kar_ple_probe` target belongs to the imported
|
||||
Strata engine build when `STRATA_NEXT_EXPERIMENTAL_KAR_PLE=ON`. It exercises
|
||||
the separate CPU PLE quality arm on real KAR rows. A successful checksum does
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace strata::kar {
|
||||
|
||||
// Experimental native decode cache-miss arm. Prefill and cache hits retain
|
||||
// the native IQ weights. Not a full compressed-model serving implementation.
|
||||
class ExpertMiss {
|
||||
public:
|
||||
ExpertMiss();
|
||||
~ExpertMiss();
|
||||
bool open(const char* archive, uint32_t layers, uint32_t experts,
|
||||
uint32_t hidden, uint32_t intermediate, uint32_t level,
|
||||
std::string& error);
|
||||
bool 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);
|
||||
static bool 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);
|
||||
uint64_t computed() const;
|
||||
private:
|
||||
struct Impl;
|
||||
std::unique_ptr<Impl> impl_;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
#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 decode misses only, 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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
#include "kar_expert_miss.hpp"
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
void require(bool ok, const char* error) { if(!ok)throw std::runtime_error(error); }
|
||||
}
|
||||
int main(int argc,char** argv) try {
|
||||
require(argc==2,"expected model-free archive path");
|
||||
std::string error;
|
||||
strata::kar::ExpertMiss miss;
|
||||
require(!miss.open(argv[1],2,2,64,32,1,error),"missing layer accepted");
|
||||
require(!miss.open(argv[1],1,2,64,32,2,error),"unavailable level accepted");
|
||||
require(miss.open(argv[1],1,2,64,32,1,error),error.c_str());
|
||||
constexpr int H=64,K=2,T=8;
|
||||
std::vector<float> x(T*H),out(T*K*H,123.0f);
|
||||
std::vector<int32_t> ids(T*K),kind(T*K,-1);
|
||||
for(int t=0;t<T;++t) {
|
||||
for(int c=0;c<H;++c)x[t*H+c]=(t+1)/64.0f;
|
||||
ids[t*K]=t%2;ids[t*K+1]=1-t%2;
|
||||
}
|
||||
// Existing VRAM/PCIe/remote rows belong to their original producers.
|
||||
kind[1]=0;kind[4]=1;kind[9]=2;
|
||||
require(miss.compute(0,x.data(),ids.data(),T,K,kind.data(),out.data(),error),error.c_str());
|
||||
require(miss.computed()==T*K-3,"incorrect computed count");
|
||||
for(int i=0;i<T*K;++i)for(int c=0;c<H;++c) {
|
||||
if(i==1 || i==4 || i==9) {require(out[i*H+c]==123,"foreign row overwritten");continue;}
|
||||
const double sign=ids[i]==0?1.0:-1.0;
|
||||
const double g=sign*(i/K+1)/32.0;
|
||||
const double expected=sign*(g/(1+std::exp(-g)))*g;
|
||||
require(kind[i]==3 && std::abs(out[i*H+c]-expected)<1e-6,"scatter/multiply mismatch");
|
||||
}
|
||||
const auto saved=out;
|
||||
require(miss.compute(0,x.data(),ids.data(),T,K,kind.data(),out.data(),error),error.c_str());
|
||||
require(out==saved && miss.computed()==T*K-3,"completed rows recomputed");
|
||||
ids[0]=2;
|
||||
require(!miss.compute(0,x.data(),ids.data(),T,K,kind.data(),out.data(),error),"invalid expert accepted");
|
||||
require(out==saved,"rejected dispatch modified output");
|
||||
require(!miss.compute(1,x.data(),ids.data(),T,K,kind.data(),out.data(),error),"invalid layer accepted");
|
||||
std::puts("PASS: routed KAR misses, grouped eight-token scatter, foreign/completed rows preserved, invalid dispatch rejected; tolerance 1e-6");
|
||||
return 0;
|
||||
} catch(const std::exception& e) {std::fprintf(stderr,"KAR miss test: %s\n",e.what());return 1;}
|
||||
Reference in New Issue
Block a user