Skip to content

Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦

C

qwen38: routed experts on the Vulkan tier

JustVugga710740805de
diff --git a/c/qwen38.c b/c/qwen38.c
index 59012543..cd1e86f5 100644
--- a/c/qwen38.c
+++ b/c/qwen38.c
@@ -1647,7 +1647,7 @@ static void serve_emap(Model *m){
LCache *lc=&m->cache[i];
for(int e=0;e<E;e++){
int si=lc->by_expert?lc->by_expert[e]:-1;
- int b=(si>=0&&si<lc->n&&lc->slots[si].eid==e?1:0)<<6;
+ int b=(vkt_resident(i,e)?2:si>=0&&si<lc->n&&lc->slots[si].eid==e?1:0)<<6; /* 2 = on the Vulkan device */
hex[w++]="0123456789abcdef"[b>>4]; hex[w++]="0123456789abcdef"[b&15];
}
}
@@ -1900,6 +1900,7 @@ static void serve_loop(Model *m){
int status=serve_one(m,&q);free(q.payload);
#ifdef COLI_VULKAN
q38_vk_report(); /* stderr: the wire protocol on stdout is untouched */
+ vkt_report("turn", m->hits, m->miss);
#endif
if(status<0){q38_prefix_cache_release(m);return;}
serve_emap(m);
@@ -1914,6 +1915,9 @@ static int q38_reference_mode(const char *path,int serve_mode){
}
static void q38_expert_report(Model *m, int cap); /* below, beside the expert layout accounting */
+#ifdef COLI_VULKAN
+static void q38_vk_tier_start(Model *m, int cap); /* below: the Vulkan routed-expert tier */
+#endif
#ifndef QWEN38_TEST_SERVE
int main(int argc, char **argv) {
@@ -2039,6 +2043,7 @@ int main(int argc, char **argv) {
/* After the trunk is int8: those rows upload at their first matmul. No
* device (or COLI_VULKAN unset) leaves g_vk_ready 0, the CPU path. */
g_vk_ready=coli_vk_init_env("qwen38");
+ { const char *vd=getenv("COLI_VK_DENSE"); g_vk_dense=!(vd&&vd[0]=='0'); }
#endif
if(is_ref)ref_logits=read_reference_logits(ref_root,m.c.vocab);
g_capture_last_logit=ref_logits!=NULL||getenv("DUMP")!=NULL;
@@ -2048,6 +2053,9 @@ int main(int argc, char **argv) {
if(!g_q38_mtp_dump){perror(getenv("Q38_MTP_DUMP"));return 1;}
}
q38_telemetry_init(snap, &m);
+#ifdef COLI_VULKAN
+ q38_vk_tier_start(&m, cap); /* COLI_VULKAN=1: hot routed experts on the device (vk_tier.c) */
+#endif
fprintf(stderr, "resident weights loaded in %.1fs | RSS after load: %.2f GB\n", m.dense_load_s, rss_gb());
/* coli serve mode: speak the gateway wire protocol instead of argv
@@ -2076,6 +2084,7 @@ int main(int argc, char **argv) {
printf("Speed: %.2f tok/s (%.1fs for %d tokens) | PEAK RSS: %.2f GB\n", scored/dt, dt, scored, rss_gb());
#ifdef COLI_VULKAN
q38_vk_report();
+ vkt_report("run", m.hits, m.miss);
#endif
rt_save(g_q38_usage,0);
q38_model_free(&m); free(prompt); free(full); json_free(ref_root);
@@ -2157,6 +2166,7 @@ int main(int argc, char **argv) {
fprintf(stderr, "Speed: %.2f tok/s (%.1fs for %d tokens)\n", n_new/dt, dt, n_new);
#ifdef COLI_VULKAN
q38_vk_report();
+ vkt_report("run", m.hits, m.miss);
#endif
rt_save(g_q38_usage, 0);
free(g_last_logit); g_last_logit=NULL;
@@ -2381,6 +2391,135 @@ static void q38_expert_report(Model *m, int cap) {
fprintf(stderr, "\n");
}
+/* ---- the Vulkan routed-expert tier (vk_tier.c) ---------------------------------
+ * After the dense trunk is int8, the device is open and the history is read: tell
+ * the tier how this snapshot's experts sit in RAM, what else will live on the
+ * device, and how much RAM the expert cache may still take; then fill it from the
+ * history, reading the planned experts straight from the files (not through the
+ * LRU, which keeps its own working set). The MTP head's layer is not offered. */
+#ifdef COLI_VULKAN
+static void q38_vk_dense_add(const Q38Weight *w,size_t *bytes){
+ if(!w||!q38_vk_eligible(w))return;
+ *bytes+=w->q8?(size_t)w->rows*w->cols+(size_t)w->rows*sizeof(float):(size_t)q38_weight_bytes(w);
+}
+static size_t q38_vk_dense_bytes(Model *m){
+ size_t b=0;
+ if(!g_vk_dense)return 0;
+ q38_vk_dense_add(&m->lm_head,&b);
+ const GatedResidual *f=&m->final_gr;
+ q38_vk_dense_add(&f->down,&b);q38_vk_dense_add(&f->up,&b);q38_vk_dense_add(&f->inject,&b);
+ for(int i=0;i<q38_layer_rows(m);i++){
+ const Layer *L=&m->L[i];
+ const Q38Weight *w[]={&L->router,&L->sh_g,&L->sh_u,&L->sh_d,&L->q,&L->k,&L->v,&L->o,&L->idx_qk,
+ &L->dn_qkv,&L->dn_z,&L->dn_b,&L->dn_a,&L->dn_out,&L->ple_key,&L->ple_value,
+ &L->attn_gr.down,&L->attn_gr.up,&L->attn_gr.inject,
+ &L->mlp_gr.down,&L->mlp_gr.up,&L->mlp_gr.inject};
+ for(size_t k=0;k<sizeof w/sizeof *w;k++)q38_vk_dense_add(w[k],&b);
+ }
+ if(m->mtp){
+ q38_vk_dense_add(&m->mtp_fc_emb,&b);q38_vk_dense_add(&m->mtp_fc_hid,&b);
+ q38_vk_dense_add(&m->mtp_mixer.down,&b);q38_vk_dense_add(&m->mtp_mixer.up,&b);
+ }
+ return b;
+}
+/* One planned expert read without the LRU: the int4 record, or the native FP8
+ * ranges with the layer's scale bank. buf holds the bytes; 0 when this layout has
+ * no direct read (the caller loads it through a temporary slot instead). */
+static int q38_vk_read_direct(Model *m,int layer,int e,uint8_t *buf,int64_t cap,VktExpertSrc *src){
+ Cfg *c=&m->c;
+ if(q38_layer_int4(m,layer)){
+ Q38Int4Experts *x=m->x4;
+ if(x->record_bytes>cap)return 0;
+ st_read_range_raw_cap(&x->S,x->fd[layer],x->off[(int64_t)layer*c->experts+e],x->record_bytes,buf,cap,1,
+ "pread Qwen3.8 int4 expert (Vulkan tier)");
+ const float *sc=(const float*)(buf+x->scale_off);
+ *src=(VktExpertSrc){buf,buf+x->code_bytes[0],buf+x->code_bytes[0]+x->code_bytes[1],
+ sc,sc+x->scale_count[0],sc+x->scale_count[0]+x->scale_count[1]};
+ return 1;
+ }
+ st_tensor *w[3];
+ if(!q38_native_fp8_expert_tensors(m,layer,e,w)||m->expert_scales[layer].ready!=1)return 0;
+ int64_t pair=w[0]->nbytes+w[1]->nbytes;
+ if(pair+w[2]->nbytes>cap)return 0;
+ st_read_range_raw_cap(&m->S,w[0]->fd,w[0]->off,pair,buf,cap,1,"pread Qwen3.8 gate/up expert (Vulkan tier)");
+ st_read_range_raw_cap(&m->S,w[2]->fd,w[2]->off,w[2]->nbytes,buf+pair,cap-pair,1,"pread Qwen3.8 down expert (Vulkan tier)");
+ const Q38ExpertScaleCache *bank=&m->expert_scales[layer];
+ const float *sc=bank->values+(int64_t)e*3*bank->scale_count;
+ *src=(VktExpertSrc){buf,buf+w[0]->nbytes,buf+pair,sc,sc+bank->scale_count,sc+2*bank->scale_count};
+ return 1;
+}
+static void q38_vk_tier_start(Model *m,int cap){
+ if(!g_vk_ready||qt_ready()){
+ if(g_vk_ready&&qt_ready())
+ fprintf(stderr,"[VK] tier qwen38: the CUDA expert tier is on and wins; the Vulkan tier stays off\n");
+ return;
+ }
+ Cfg *c=&m->c;int H=c->hidden,F=c->inter;
+ uint64_t per_capacity=0,fixed=0;unsigned kinds=0;
+ VktFmt f={VKT_SRC_NONE,0};size_t ram_expert=0;
+ if(m->x4){ f.kind=VKT_SRC_I4U_PLANAR64; f.gs=64; ram_expert=(size_t)m->x4->record_bytes; }
+ else if(!q38_segment_expert_layout(m,0,(uint32_t)c->layers,&per_capacity,&fixed,&kinds)){
+ if(kinds==Q38_EXPERT_FP8_BLOCK&&m->native_fp8){
+ for(int l=0;l<c->layers;l++)
+ if(!q38_prepare_expert_scale_bank(m,l)){ kinds=0; break; }
+ if(kinds){ f.kind=VKT_SRC_FP8_BLOCK; f.gs=FP8_BLOCK; ram_expert=(size_t)3*H*F; }
+ } else if(kinds==Q38_EXPERT_BF16&&m->native_bf16){ f.kind=VKT_SRC_BF16; ram_expert=(size_t)3*H*F*2; }
+ else if(kinds==Q38_EXPERT_BF16||kinds==Q38_EXPERT_F16||kinds==Q38_EXPERT_F32||kinds==Q38_EXPERT_FP8_EXPANDED){
+ f.kind=VKT_SRC_F32; ram_expert=(size_t)3*H*F*4;
+ }
+ }
+ if(f.kind==VKT_SRC_NONE){
+ fprintf(stderr,"[VK] tier qwen38: the routed experts mix formats; they stay on the CPU\n");
+ return;
+ }
+ VktConfig vc={.engine="qwen38",.layers=c->layers,.experts=c->experts,.hidden=H,.inter=F,.topk=c->topk,
+ .gate_up=f,.down=f,.act=VKT_ACT_SWIGLU,
+ .max_rows=q38_moe_prefill_rows(c,q38_prefill_batch_rows())*c->topk,
+ .ram_reserve=ram_expert*(size_t)cap*(size_t)c->layers,
+ .dense_bytes=q38_vk_dense_bytes(m)};
+ if(vc.max_rows<64)vc.max_rows=64;
+ uint32_t **heat=rt_counts_all();
+ if(!vkt_init(&vc,heat))return;
+ atexit(vkt_shutdown);
+ /* the warm start: the history's hottest experts, read in parallel */
+ int all=c->layers*c->experts,*pl=(int*)malloc((size_t)all*sizeof(int)),*pe=(int*)malloc((size_t)all*sizeof(int));
+ if(!pl||!pe){free(pl);free(pe);return;}
+ const char *warm=getenv("COLI_VK_TIER_WARM"); /* 0: no warm start, the tier fills as experts pass by */
+ int n=warm&&*warm=='0'?0:vkt_plan(pl,pe,all);
+ if(n>0){
+ double t0=now_s();
+ int64_t bufb=m->x4?m->x4->record_bytes:(int64_t)3*H*F;
+ int direct=m->x4||f.kind==VKT_SRC_FP8_BLOCK;
+ if(direct){
+ #pragma omp parallel
+ {
+ uint8_t *buf=(uint8_t*)malloc((size_t)bufb);
+ #pragma omp for schedule(dynamic,4)
+ for(int i=0;i<n;i++){
+ VktExpertSrc src;
+ if(buf&&q38_vk_read_direct(m,pl[i],pe[i],buf,bufb,&src))vkt_put(pl[i],pe[i],&src);
+ else vkt_put(pl[i],pe[i],NULL);
+ }
+ free(buf);
+ }
+ } else {
+ /* BF16/f32 experts (the small fixtures): through a temporary slot, serially */
+ for(int i=0;i<n;i++){
+ Slot tmp; memset(&tmp,0,sizeof tmp);
+ q38_load_expert(m,pl[i],pe[i],&tmp);
+ VktExpertSrc src=q38_vk_src(&tmp); vkt_put(pl[i],pe[i],&src);
+ q38_weight_free(&tmp.gate);q38_weight_free(&tmp.up);q38_weight_free(&tmp.down);
+ free(tmp.fp8_slab);free(tmp.int4_slab);
+ }
+ }
+ vkt_put_done();
+ fprintf(stderr,"[VK] tier qwen38: warm start, %d experts from the history in %.1fs\n",n,now_s()-t0);
+ }
+ free(pl);free(pe);
+}
+#endif
+
+
static int q38_segment_cache_capacity(uint64_t bytes_per_capacity,
uint64_t fixed_scale_bytes,int experts,
uint64_t memory_limit,int *capacity){
diff --git a/c/qwen38_core.h b/c/qwen38_core.h
index 5c9125aa..8e5297f2 100644
--- a/c/qwen38_core.h
+++ b/c/qwen38_core.h
@@ -1,4 +1,5 @@
#include "qwen36_tier.h" /* CUDA VRAM expert tier, shared with qwen36; CPU-only builds get the inline no-op stubs */
+#include "vk_tier.h" /* Vulkan routed-expert tier (COLI_VULKAN=1); builds without VK=1 get inline no-op stubs */
/* Native Qwen3.8-Flash-Next text core.
*
* This header is included once by qwen38.c after its tokenizer and protocol
@@ -16,6 +17,7 @@
#ifdef COLI_VULKAN
#include "backend_vulkan.h" /* COLI_VULKAN=1: the int8 trunk on a Vulkan device */
static int g_vk_ready = 0;
+static int g_vk_dense = 1; /* COLI_VK_DENSE=0: the trunk stays on the CPU, the expert tier alone uses the device */
#endif
#define Q38_MAX_LAYERS 512
@@ -499,7 +501,7 @@ static void q38_weight_matmul(float *y,const float *x,const Q38Weight *weight,
if(S>1&&g_q38_rowwise&&weight&&
(weight->gpu
#ifdef COLI_VULKAN
- ||(g_vk_ready&&q38_vk_eligible(weight))
+ ||(g_vk_ready&&g_vk_dense&&q38_vk_eligible(weight))
#endif
)){
for(int s=0;s<S;s++)q38_weight_matmul(y+(int64_t)s*O,x+(int64_t)s*I,weight,1,I,O);
@@ -511,7 +513,7 @@ static void q38_weight_matmul(float *y,const float *x,const Q38Weight *weight,
if(S==1&&weight&&weight->gpu&&weight->rows==O&&weight->cols==I&&
qt_dense_matmul(weight->gpu-1,y,x,I,O))return;
#ifdef COLI_VULKAN
- if(g_vk_ready&&weight&&weight->rows==O&&weight->cols==I&&q38_vk_eligible(weight)&&
+ if(g_vk_ready&&g_vk_dense&&weight&&weight->rows==O&&weight->cols==I&&q38_vk_eligible(weight)&&
q38_vk_matmul(y,x,weight,S,I,O))return;
#endif
if(weight&&weight->q8&&weight->rows==O&&weight->cols==I){
@@ -2572,10 +2574,31 @@ static void q38_tier_start(Model *m,int cap) {
}
}
+/* ---- Vulkan routed-expert tier (vk_tier.c) -------------------------------------
+ * The tier computes the resident experts of a step on the device while the CPU
+ * computes the rest; every expert's output then joins the row in rank order, the
+ * device's and the CPU's alike, so the sum's order is the CPU run's whatever was
+ * resident. The model's layers only: the MTP head's layer keeps its FP8 experts on
+ * the CPU (the sidecar's int4 covers the model's layers). A slot's bytes are what
+ * the tier reads when it promotes the expert: codes and scales of each matrix. */
+static VktExpertSrc q38_vk_src(const Slot *ex) {
+ VktExpertSrc s={ex->gate.data,ex->up.data,ex->down.data,ex->gate.scales,ex->up.scales,ex->down.scales};
+ return s;
+}
+static void q38_expert_row(Model *m,int layer,int eid,const float *xs,float *eg,float *eu,float *eh,float *eo) {
+ Cfg *c=&m->c;int H=c->hidden,I=c->inter;
+ Slot *ex=q38_expert_get(m,layer,eid);double started=now_s();
+ q38_weight_matmul(eg,xs,&ex->gate,1,H,I);q38_weight_matmul(eu,xs,&ex->up,1,H,I);
+ for(int j=0;j<I;j++)eh[j]=q38_silu(eg[j])*eu[j];q38_weight_matmul(eo,eh,&ex->down,1,I,H);
+ q38_tm_add(m,Q38_TM_ROUTED_EXPERT,started);
+}
+
static void q38_moe_decode(Model *m,Layer *l,int layer,const float *x,int S,float *out) {
Cfg *c=&m->c;int H=c->hidden,E=c->experts,K=c->topk,I=c->inter,SI=c->shared_inter;
float *logits=falloc(E),*sg=falloc(SI),*su=falloc(SI),*sh=falloc(SI),*shared=falloc(H);
float *eg=falloc(I),*eu=falloc(I),*eh=falloc(I),*eo=falloc(H);
+ int vk=vkt_ready()&&layer<c->layers; /* the Vulkan tier: outputs buffered per rank */
+ float *ebuf=vk?falloc((int64_t)K*H):NULL;
for(int s=0;s<S;s++){
const float *xs=x+(int64_t)s*H;float *ys=out+(int64_t)s*H;memset(ys,0,(size_t)H*sizeof(float));
q38_dense_matmul(m,logits,xs,&l->router,1,H,E);float mx=logits[0];for(int e=1;e<E;e++)if(logits[e]>mx)mx=logits[e];
@@ -2590,9 +2613,12 @@ static void q38_moe_decode(Model *m,Layer *l,int layer,const float *x,int S,floa
* CPU only loads and computes the rest, in route order. The tier
* knows the model's layers only: the MTP head's stays on the CPU. */
int tiered=layer<c->layers;
+ uint8_t taken[Q38_MAX_TOPK]={0};
+ int ndev=vk?vkt_issue(layer,xs,1,K,idx,taken):0;
+ for(int z=0;z<K;z++)if(taken[z])q38_ehit_mark(m,layer,idx[z]); /* routed this turn, wherever it ran */
uint32_t qmask=tiered?qt_issue(layer,idx,K,xs):0;
int cpu_idx[Q38_MAX_TOPK],cpu_rank[Q38_MAX_TOPK],cpu_n=0;
- for(int z=0;z<K;z++)if(!((qmask>>z)&1u)){cpu_idx[cpu_n]=idx[z];cpu_rank[cpu_n]=z;cpu_n++;}
+ for(int z=0;z<K;z++)if(!((qmask>>z)&1u)&&!taken[z]){cpu_idx[cpu_n]=idx[z];cpu_rank[cpu_n]=z;cpu_n++;}
q38_prefetch_experts(m,layer,cpu_idx,cpu_n);
double phase_started=now_s();
q38_weight_matmul(sg,xs,&l->sh_g,1,H,SI);q38_weight_matmul(su,xs,&l->sh_u,1,H,SI);
@@ -2606,10 +2632,27 @@ static void q38_moe_decode(Model *m,Layer *l,int layer,const float *x,int S,floa
Slot *ex=loaded_batch?selected[i]:q38_expert_get(m,layer,cpu_idx[i]);phase_started=now_s();
q38_weight_matmul(eg,xs,&ex->gate,1,H,I);q38_weight_matmul(eu,xs,&ex->up,1,H,I);
for(int j=0;j<I;j++)eh[j]=q38_silu(eg[j])*eu[j];q38_weight_matmul(eo,eh,&ex->down,1,I,H);
- for(int d=0;d<H;d++)ys[d]+=route_gates[z]*eo[d];
+ if(vk){
+ memcpy(ebuf+(int64_t)z*H,eo,(size_t)H*sizeof(float));
+ VktExpertSrc src=q38_vk_src(ex); vkt_note(layer,cpu_idx[i],&src);
+ }
+ else for(int d=0;d<H;d++)ys[d]+=route_gates[z]*eo[d];
q38_tm_add(m,Q38_TM_ROUTED_EXPERT,phase_started);
if(tiered)q38_tier_note(layer,cpu_idx[i],ex);
}
+ if(vk){
+ /* the device's experts, then every rank in order; a batch that failed is
+ * recomputed here (the tier has turned itself off) */
+ const float *dev[Q38_MAX_TOPK];
+ if(ndev&&!vkt_join(dev))
+ for(int z=0;z<K;z++)if(taken[z]){
+ q38_expert_row(m,layer,idx[z],xs,eg,eu,eh,ebuf+(int64_t)z*H);taken[z]=0;
+ }
+ for(int z=0;z<K;z++){
+ const float *e=taken[z]?dev[z]:ebuf+(int64_t)z*H;
+ for(int d=0;d<H;d++)ys[d]+=route_gates[z]*e[d];
+ }
+ }
/* GPU experts land after the CPU ones: same values, one more group in
* the float sum (that is the only ordering difference to a CPU run). */
if(tiered&&!qt_take(qmask,route_gates,K,ys)){
@@ -2619,7 +2662,7 @@ static void q38_moe_decode(Model *m,Layer *l,int layer,const float *x,int S,floa
for(int d=0;d<H;d++)ys[d]+=gate*shared[d];
}
rt_trace_end();
- free(logits);free(sg);free(su);free(sh);free(shared);free(eg);free(eu);free(eh);free(eo);
+ free(logits);free(sg);free(su);free(sh);free(shared);free(eg);free(eu);free(eh);free(eo);free(ebuf);
}
typedef struct {
@@ -2648,6 +2691,26 @@ static int q38_moe_prefill_rows(const Cfg *c,int requested) {
return q38_bounded_prefill_rows(requested,fixed,total_row);
}
+/* With the Vulkan tier: n of an expert's assignments (positions pos[] in the
+ * grouped order, assignment ids in assign[]) gathered, run as one batch and their
+ * outputs scattered to their places in routed_out, where the reduction reads them.
+ * Row for row the same arithmetic as the grouped loop below. */
+static void q38_prefill_expert_rows(Model *m,Slot *expert,const float *xc,int K,const int *assign,
+ const int *pos,int n,float *in,float *gate,float *up,float *routed_out) {
+ if(n<1)return;
+ Cfg *c=&m->c;int H=c->hidden,I=c->inter;
+ for(int a=0;a<n;a++)memcpy(in+(int64_t)a*H,xc+(int64_t)(assign[pos[a]]/K)*H,(size_t)H*sizeof(float));
+ double started=now_s();
+ q38_weight_matmul(gate,in,&expert->gate,n,H,I);
+ q38_weight_matmul(up,in,&expert->up,n,H,I);
+ for(int a=0;a<n;a++)for(int j=0;j<I;j++)
+ gate[(int64_t)a*I+j]=q38_silu(gate[(int64_t)a*I+j])*up[(int64_t)a*I+j];
+ float *o=in; /* the inputs are consumed: their rows hold the outputs, H wide each */
+ q38_weight_matmul(o,gate,&expert->down,n,I,H);
+ for(int a=0;a<n;a++)memcpy(routed_out+(int64_t)pos[a]*H,o+(int64_t)a*H,(size_t)H*sizeof(float));
+ q38_tm_add(m,Q38_TM_ROUTED_EXPERT,started);
+}
+
/* Prefill MoE: route a bounded row chunk first, then execute each distinct
* expert's assignments as one batched SwiGLU. Grouping is an I/O optimization
* only. Expert outputs are placed back in assignment order and the final
@@ -2674,6 +2737,15 @@ static void q38_moe_prefill(Model *m,Layer *l,int layer,const float *x,
fprintf(stderr,"OOM Qwen3.8 MoE prefill metadata\n");exit(1);
}
+ /* the Vulkan tier: per assignment, whether the device takes it and its output */
+ int vk=vkt_ready()&&layer<c->layers;
+ int *vk_idx=vk?(int*)malloc((size_t)max_assign*sizeof(int)):NULL;
+ uint8_t *vk_taken=vk?(uint8_t*)calloc((size_t)max_assign,1):NULL;
+ const float **vk_dev=vk?(const float**)malloc((size_t)max_assign*sizeof(*vk_dev)):NULL;
+ int *vk_cpu=vk?(int*)malloc((size_t)E*sizeof(int)):NULL;
+ int *vk_sub=vk?(int*)malloc((size_t)max_assign*sizeof(int)):NULL;
+ if(vk&&(!vk_idx||!vk_taken||!vk_dev||!vk_cpu||!vk_sub)){fprintf(stderr,"OOM Qwen3.8 MoE prefill metadata\n");exit(1);}
+
float *logits=falloc((int64_t)rows_capacity*E);
float *shared_gate=falloc(rows_capacity);
float *shared_g=falloc((int64_t)rows_capacity*SI);
@@ -2747,8 +2819,27 @@ static void q38_moe_prefill(Model *m,Layer *l,int layer,const float *x,
assignment_positions[assignment]=position;
}
+ /* The Vulkan tier takes the chunk's assignments of its resident experts now,
+ * and computes them while the shared expert and the other experts run here.
+ * cpu_unique: the experts with an assignment the device did not take (all of
+ * them without the tier). */
+ int ndev=0;
+ int *cpu_unique=unique,cpu_count=unique_count;
+ if(vk){
+ for(int a=0;a<assignment_count;a++)vk_idx[a]=routes[a].expert;
+ ndev=vkt_issue(layer,x+(int64_t)base*H,rows,K,vk_idx,vk_taken);
+ for(int a=0;a<assignment_count;a++)if(vk_taken[a])q38_ehit_mark(m,layer,vk_idx[a]);
+ cpu_count=0;
+ for(int u=0;u<unique_count;u++){
+ int e=unique[u],left=0;
+ for(int a=0;a<group_counts[e];a++)left+=!vk_taken[assignments[group_offsets[e]+a]];
+ if(left)vk_cpu[cpu_count++]=e;
+ }
+ cpu_unique=vk_cpu;
+ }
+
/* One advice range per distinct expert is enough for this chunk. */
- q38_prefetch_experts(m,layer,unique,unique_count);
+ q38_prefetch_experts(m,layer,cpu_unique,cpu_count);
/* Shared expert work is independent across rows and remains resident;
* batching it here also keeps its cost out of the routed groups. */
@@ -2781,17 +2872,27 @@ static void q38_moe_prefill(Model *m,Layer *l,int layer,const float *x,
* slots because the preceding outputs already live in routed_out. */
int load_limit=m->cache[layer].cap;
if(load_limit<1)load_limit=1;
- for(int unique_base=0;unique_base<unique_count;) {
- int load_count=unique_count-unique_base;
+ for(int unique_base=0;unique_base<cpu_count;) {
+ int load_count=cpu_count-unique_base;
if(load_count>load_limit)load_count=load_limit;
int loaded_batch=load_count>=2&&q38_expert_get_batch(
- m,layer,unique+unique_base,load_count,batch_slots);
+ m,layer,cpu_unique+unique_base,load_count,batch_slots);
for(int offset=0;offset<load_count;offset++) {
- int e=unique[unique_base+offset];
+ int e=cpu_unique[unique_base+offset];
int count=group_counts[e];
Slot *expert=loaded_batch?batch_slots[offset]:
q38_expert_get(m,layer,e);
int first=group_offsets[e];
+ if(vk){
+ /* the assignments the device left (all of them, unless its
+ * batch filled up mid-expert), computed and scattered */
+ int n=0;
+ for(int a=0;a<count;a++)if(!vk_taken[assignments[first+a]])vk_sub[n++]=first+a;
+ q38_prefill_expert_rows(m,expert,x+(int64_t)base*H,K,assignments,vk_sub,n,
+ expert_input,expert_gate,expert_up,routed_out);
+ VktExpertSrc src=q38_vk_src(expert); vkt_note(layer,e,&src);
+ continue;
+ }
for(int a=0;a<count;a++) {
int assignment=assignments[first+a];
int row=assignment/K;
@@ -2813,6 +2914,18 @@ static void q38_moe_prefill(Model *m,Layer *l,int layer,const float *x,
unique_base+=load_count;
}
+ if(ndev&&!vkt_join(vk_dev)){
+ /* the batch failed (the tier has turned itself off): its experts here */
+ for(int u=0;u<unique_count;u++){
+ int e=unique[u],first=group_offsets[e],n=0;
+ for(int a=0;a<group_counts[e];a++)if(vk_taken[assignments[first+a]])vk_sub[n++]=first+a;
+ if(!n)continue;
+ q38_prefill_expert_rows(m,q38_expert_get(m,layer,e),x+(int64_t)base*H,K,assignments,vk_sub,n,
+ expert_input,expert_gate,expert_up,routed_out);
+ for(int a=0;a<n;a++)vk_taken[assignments[vk_sub[a]]]=0;
+ }
+ }
+
/* The grouped execution above is intentionally not the reduction
* order. Replaying the original top-k sequence gives the same result
* as the single-row implementation for F32, BF16 and block-FP8. */
@@ -2820,7 +2933,7 @@ static void q38_moe_prefill(Model *m,Layer *l,int layer,const float *x,
float *ys=out+(int64_t)(base+s)*H;
for(int rank=0;rank<K;rank++) {
int assignment=s*K+rank;
- const float *expert_output=routed_out+
+ const float *expert_output=vk&&vk_taken[assignment]?vk_dev[assignment]:routed_out+
(int64_t)assignment_positions[assignment]*H;
float gate=routes[assignment].gate;
for(int d=0;d<H;d++)ys[d]+=gate*expert_output[d];
@@ -2836,6 +2949,7 @@ static void q38_moe_prefill(Model *m,Layer *l,int layer,const float *x,
free(expert_up);free(routed_out);free(routes);free(assignments);
free(assignment_positions);free(group_counts);free(group_offsets);
free(group_cursor);free(unique);free(batch_slots);
+ free(vk_idx);free(vk_taken);free(vk_dev);free(vk_cpu);free(vk_sub);
}
static void q38_moe(Model *m,Layer *l,int layer,const float *x,int S,float *out) {