Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
feat(glm53): add Metal routed MoE acceleration
trigger2k20a21482418aae
diff --git a/c/Makefile b/c/Makefileindex 8b5c8ab7..80e26e1d 100644--- a/c/Makefile+++ b/c/Makefile@@ -1122,11 +1122,10 @@ NOCUDA_CFLAGS = $(filter-out -DCOLI_CUDA -DCOLI_ANS,$(CFLAGS))NOCUDA_LDFLAGS = $(filter-out -lcudart -lstdc++ -lcuda -L$(CUDA_HOME)/lib64 \-Wl$(comma)-rpath$(comma)$(CUDA_HOME)/lib64,$(LDFLAGS))-# GLM-5.3-Flash: CPU puro, esperti in streaming dal contenitore int4 gs64.-# Nessun oggetto CUDA/Vulkan/Metal fra le dipendenze perche' il motore non ne-# ha ancora: quando li avra', questa riga somigliera' a quella di kimi_k3.-glm53$(EXE): glm53.c cli_args.h st.h json.h tok.h tok_unicode.h tok_unicode_o200k.h compat.h serve_poll.h quant.h hyper_connections.h delta_attention.h sparse_index.h vision_tower.h backend_vulkan.h edge_adapter_internal.h edge_adapters.h edge_runtime.h segment_adapter_internal.h segment_adapters.h segment_runtime.h $(VK_OBJ) $(VK_SPV)- $(CC) $(CFLAGS) glm53.c $(VK_OBJ) -o glm53$(EXE) $(LDFLAGS)+# GLM-5.3-Flash: routed experts stream from the int4-gs64 container.+# METAL=1 accelerates resident matrices and routed MoE; CPU remains fallback.+glm53$(EXE): glm53.c cli_args.h st.h json.h tok.h tok_unicode.h tok_unicode_o200k.h compat.h serve_poll.h quant.h hyper_connections.h delta_attention.h sparse_index.h vision_tower.h backend_metal.h backend_vulkan.h edge_adapter_internal.h edge_adapters.h edge_runtime.h segment_adapter_internal.h segment_adapters.h segment_runtime.h $(METAL_OBJ) $(VK_OBJ) $(VK_SPV)+ $(CC) $(CFLAGS) glm53.c $(METAL_OBJ) $(VK_OBJ) -o glm53$(EXE) $(LDFLAGS)kimi_k3$(EXE): kimi_k3.c cli_args.h st.h json.h tok.h tok_unicode.h tok_unicode_o200k.h compat.h quant.h omp_tune.h route_trace.h kv_prefix.h serve_codec.h backend_cuda.h backend_metal.h backend_vulkan.h edge_adapters.h edge_runtime.h edge_tok_internal.h hybrid_split.h segment_adapter_internal.h segment_adapters.h segment_runtime.h $(CUDA_OBJ) $(VK_OBJ) $(VK_SPV) $(METAL_OBJ)$(CC) $(CFLAGS) kimi_k3.c $(CUDA_OBJ) $(VK_OBJ) $(METAL_OBJ) -o kimi_k3$(EXE) $(LDFLAGS)diff --git a/c/backend_metal.h b/c/backend_metal.hindex a8a01be2..4fd5a334 100644--- a/c/backend_metal.h+++ b/c/backend_metal.h@@ -46,6 +46,8 @@ int coli_metal_add(float *y, const float *a, size_t n);* Returns 1 on success, 0 if Metal unavailable.*/int coli_metal_silu_mul(float *g, const float *u, size_t n);+/* Numerical-oracle hook for GLM-5.3 clamped SwiGLU. */+int coli_metal_silu_mul_clamped(float *g, const float *u, size_t n, float limit);/** y[S,O] = (x[S,I] @ W[O,I]^T) * scale[o]. fmt=4 (grouped int4) instead folds a@@ -187,6 +189,15 @@ int coli_metal_moe_block(int nb, int D, int Iinter, int fmt, int qgs,const int *rows, const float *rw,float *out, int S);+/* GLM-5.3 routed experts: same batched path, but with its clamped SwiGLU semantics.+ * gate is upper-clamped to +limit; up is clamped to [-limit,+limit]. */+int coli_metal_moe_block_clamped(int nb, int D, int Iinter, int fmt, int qgs,+ const void *const *g, const void *const *u, const void *const *d,+ const float *const *gs, const float *const *us, const float *const *ds,+ const float *xg, const int *xoff, const int *nr,+ const int *rows, const float *rw,+ float *out, int S, float swiglu_limit);+/** Async two-phase variant: begin encodes+commits the block (own scratch, no wait) and* returns a handle, so the CPU can load missed experts from disk WHILE the GPU computesdiff --git a/c/backend_metal.mm b/c/backend_metal.mmindex b7cdf70e..0d63a394 100644--- a/c/backend_metal.mm+++ b/c/backend_metal.mm@@ -263,6 +263,13 @@ kernel void moe_fwht(device float* v [[buffer(0)]], device const uchar* signs [[}kernel void moe_silu(device float* g [[buffer(0)]], device const float* u [[buffer(1)]],uint i [[thread_position_in_grid]]) { float v=g[i]; g[i]=(v/(1.0f+exp(-v)))*u[i]; }+kernel void moe_silu_clamped(device float* g [[buffer(0)]], device const float* u [[buffer(1)]],+ constant float& limit [[buffer(2)]],+ uint i [[thread_position_in_grid]]) {+ float v=min(g[i], limit);+ float uv=clamp(u[i], -limit, limit);+ g[i]=(v/(1.0f+exp(-v)))*uv;+}// ===== Fused decode attention (GLM-5.2 dims, S=1) =====constant int A_HID=6144, A_H=64, A_QLORA=2048, A_KVL=512, A_NOPE=192, A_ROPE=64, A_VH=256;@@ -590,7 +597,7 @@ struct ColiMetalTensor {static id<MTLDevice> g_dev;static id<MTLCommandQueue> g_queue;-static id<MTLComputePipelineState> g_gemv, g_moe_gemv, g_moe_silu, g_moe_fwht;+static id<MTLComputePipelineState> g_gemv, g_moe_gemv, g_moe_silu, g_moe_silu_clamped, g_moe_fwht;// fmt=6: sign-bit buffers for the GPU FWHT, one per tile size, cached forever (a// handful of sizes). The xorshift64* draw replicates quant.h e8_signs exactly —@@ -824,6 +831,7 @@ extern "C" int coli_metal_init(void) {g_moe_silu = [g_dev newComputePipelineStateWithFunction:[lib newFunctionWithName:@"moe_silu"] error:&err];g_moe_fwht = [g_dev newComputePipelineStateWithFunction:[lib newFunctionWithName:@"moe_fwht"] error:&err];auto P=[&](const char*n){ return [g_dev newComputePipelineStateWithFunction:[lib newFunctionWithName:@(n)] error:&err]; };+ g_moe_silu_clamped=P("moe_silu_clamped");g_a_rms=P("a_rmsnorm"); g_a_rope=P("a_rope"); g_a_copy=P("a_copy");g_a_qabs=P("a_qabs"); g_a_score=P("a_score"); g_a_smax=P("a_smax"); g_a_clat=P("a_clat"); g_a_ctx=P("a_ctx");g_a_add=P("a_add"); g_r_router=P("r_router"); g_r_top8=P("r_top8"); g_r_top8p=P("r_top8_par");@@ -846,7 +854,7 @@ extern "C" int coli_metal_init(void) {"r_top8 in use\n", (unsigned long)[g_r_top8p threadExecutionWidth]);g_rtop8_par = 0;}- if (!g_gemv || !g_moe_gemv || !g_moe_silu || !g_moe_fwht || !g_a_rms || !g_a_rope || !g_a_copy ||+ if (!g_gemv || !g_moe_gemv || !g_moe_silu || !g_moe_silu_clamped || !g_moe_fwht || !g_a_rms || !g_a_rope || !g_a_copy ||!g_a_qabs || !g_a_score || !g_a_smax || !g_a_clat || !g_a_ctx) {fprintf(stderr, "[metal] pipeline failed\n"); g_dev = nil; return 0; }// E5 experiment: COLI_METAL_RESSET=1 -- see g_resset_obj comment above.@@ -1399,7 +1407,8 @@ extern "C" size_t coli_metal_tensor_bytes(const ColiMetalTensor *t) { return t ?// if Metal is off or any expert pointer is not in a registered slab.// Encode + commit a MoE block (no wait). Writes hh[R,D] into hh_buf. Returns nil on// unresolved slab / bad fmt (caller falls back to CPU).-static id<MTLCommandBuffer> moe_submit(int nb, int D, int Iinter, int fmt, int qgs,+static id<MTLCommandBuffer> moe_submit_impl(int nb, int D, int Iinter, int fmt, int qgs,+ int clamped, float swiglu_limit,const void *const *g, const void *const *u, const void *const *d,const float *const *gs, const float *const *us, const float *const *ds,const float *xg, const int *xoff, const int *nr, int R,@@ -1474,8 +1483,9 @@ static id<MTLCommandBuffer> moe_submit(int nb, int D, int Iinter, int fmt, int qgemv(bag,bsg,xg_buf,gg_buf,Iinter,D,D); // gategemv(bau,bsu,xg_buf,uu_buf,Iinter,D,D); // up[e memoryBarrierWithScope:MTLBarrierScopeBuffers];- [e setComputePipelineState:g_moe_silu];+ [e setComputePipelineState:clamped ? g_moe_silu_clamped : g_moe_silu];[e setBuffer:gg_buf offset:0 atIndex:0];[e setBuffer:uu_buf offset:0 atIndex:1];+ if (clamped) [e setBytes:&swiglu_limit length:sizeof(float) atIndex:2];[e dispatchThreads:MTLSizeMake((size_t)R*Iinter,1,1) threadsPerThreadgroup:MTLSizeMake(256,1,1)];[e memoryBarrierWithScope:MTLBarrierScopeBuffers];if (fmt == 6) { /* rotate the down-projection input in place: same block-diagonal@@ -1534,7 +1544,29 @@ extern "C" int coli_metal_moe_block(int nb, int D, int Iinter, int fmt, int qgs,g_gg = ensure(g_gg,&g_gg_cap,(size_t)R*Iinter*4);g_uu = ensure(g_uu,&g_uu_cap,(size_t)R*Iinter*4);g_hh = ensure(g_hh,&g_hh_cap,(size_t)R*D*4);- id<MTLCommandBuffer> cb = moe_submit(nb,D,Iinter,fmt,qgs,g,u,d,gs,us,ds,xg,xoff,nr,R,g_xg,g_gg,g_uu,g_hh);+ id<MTLCommandBuffer> cb = moe_submit_impl(nb,D,Iinter,fmt,qgs,0,0.0f,g,u,d,gs,us,ds,xg,xoff,nr,R,g_xg,g_gg,g_uu,g_hh);+ if (!cb) return 0;+ return moe_finish(cb,g_hh,nb,R,D,rows,rw,out);+ }+}++extern "C" int coli_metal_moe_block_clamped(int nb, int D, int Iinter, int fmt, int qgs,+ const void *const *g, const void *const *u, const void *const *d,+ const float *const *gs, const float *const *us, const float *const *ds,+ const float *xg, const int *xoff, const int *nr,+ const int *rows, const float *rw, float *out, int S,+ float swiglu_limit) {+ (void)S;+ if (!(swiglu_limit > 0.0f)) return 0;+ @autoreleasepool {+ int R = 0; for (int e=0;e<nb;e++) R += nr[e];+ if (R == 0) return 1;+ g_xg = ensure(g_xg,&g_xg_cap,(size_t)R*D*4);+ g_gg = ensure(g_gg,&g_gg_cap,(size_t)R*Iinter*4);+ g_uu = ensure(g_uu,&g_uu_cap,(size_t)R*Iinter*4);+ g_hh = ensure(g_hh,&g_hh_cap,(size_t)R*D*4);+ id<MTLCommandBuffer> cb = moe_submit_impl(nb,D,Iinter,fmt,qgs,1,swiglu_limit,+ g,u,d,gs,us,ds,xg,xoff,nr,R,g_xg,g_gg,g_uu,g_hh);if (!cb) return 0;return moe_finish(cb,g_hh,nb,R,D,rows,rw,out);}@@ -1559,7 +1591,7 @@ extern "C" ColiMetalMoeHandle* coli_metal_moe_block_begin(int nb, int D, int Iinid<MTLBuffer> bgg=[g_dev newBufferWithLength:(size_t)R*Iinter*4 options:g_res_opts];id<MTLBuffer> buu=[g_dev newBufferWithLength:(size_t)R*Iinter*4 options:g_res_opts];id<MTLBuffer> bhh=[g_dev newBufferWithLength:(size_t)R*D*4 options:g_res_opts];- id<MTLCommandBuffer> cb = moe_submit(nb,D,Iinter,fmt,qgs,g,u,d,gs,us,ds,xg,xoff,nr,R,bxg,bgg,buu,bhh);+ id<MTLCommandBuffer> cb = moe_submit_impl(nb,D,Iinter,fmt,qgs,0,0.0f,g,u,d,gs,us,ds,xg,xoff,nr,R,bxg,bgg,buu,bhh);if (!cb) return nullptr;ColiMetalMoeHandle *h = new ColiMetalMoeHandle();h->cb=cb; h->hh=bhh; h->rows.assign(rows,rows+R); h->rwv.assign(rw,rw+R);@@ -1632,6 +1664,27 @@ extern "C" int coli_metal_add(float *y, const float *a, size_t n) {}}+extern "C" int coli_metal_silu_mul_clamped(float *g, const float *u, size_t n, float limit) {+ if (!g_dev || n == 0 || !(limit > 0.0f)) return n == 0 ? 1 : 0;+ @autoreleasepool {+ id<MTLCommandBuffer> cb = [g_queue commandBuffer];+ id<MTLBuffer> gb = cpubuf(g_dev, g, n*4);+ id<MTLBuffer> ub = cpubuf(g_dev, u, n*4);+ if (!gb || !ub) return 0;+ id<MTLComputeCommandEncoder> e = [cb computeCommandEncoder];+ [e setComputePipelineState:g_moe_silu_clamped];+ [e setBuffer:gb offset:0 atIndex:0]; [e setBuffer:ub offset:0 atIndex:1];+ [e setBytes:&limit length:sizeof(float) atIndex:2];+ [e dispatchThreads:MTLSizeMake((uint32_t)n,1,1) threadsPerThreadgroup:MTLSizeMake(256,1,1)];+ [e endEncoding];+ { id<MTLBlitCommandEncoder> bl = [cb blitCommandEncoder]; [bl synchronizeResource:gb]; [bl endEncoding]; }+ [cb commit]; [cb waitUntilCompleted];+ if (cb.status == MTLCommandBufferStatusError) return 0;+ memcpy(g, gb.contents, n*4);+ return 1;+ }+}+extern "C" int coli_metal_silu_mul(float *g, const float *u, size_t n) {if (!g_dev || n == 0) return n == 0 ? 1 : 0;@autoreleasepool {diff --git a/c/coli b/c/coliindex ed691be0..86786775 100755--- a/c/coli+++ b/c/coli@@ -1285,7 +1285,12 @@ def cmd_run(a):sys.exit(result)if arch=="glm53":e=env_for_engine(a,arch)- cmd=[engine,"--model",os.path.abspath(a.model),"--prompt",prompt,+ # GLM53 accepts cache/layer as a positional numeric argument. Keep the+ # same cap_for_launch contract used by the persistent gateway so an+ # explicit `coli run --cap N` reaches the engine instead of being+ # silently ignored by the one-shot path.+ cap = cap_for_launch(a.cap, e, 0)+ cmd=[engine,str(cap),"--model",os.path.abspath(a.model),"--prompt",prompt,"--greedy",str(ngen_for(a,interactive=True,family=family))]sys.exit(subprocess.call(cmd,env=e))if arch=="olmoe":diff --git a/c/glm53.c b/c/glm53.cindex 7ba1c7e6..433478e1 100644--- a/c/glm53.c+++ b/c/glm53.c@@ -71,6 +71,17 @@#include "st.h"#include "quant.h"#include "tok.h"+#ifdef COLI_METAL+#include "backend_metal.h"+static int g_metal_ready = 0;+/* Runtime proof for the routed-expert path. These counters intentionally count+ * cache-sized MoE blocks, not individual matmuls: one successful block means+ * gate/up/clamped-SwiGLU/down/scatter all completed on Metal. */+static uint64_t g_metal_moe_attempt = 0;+static uint64_t g_metal_moe_ok = 0;+static uint64_t g_metal_moe_fallback = 0;+static uint64_t g_metal_moe_rows = 0;+#endif#ifdef COLI_VULKAN#include "backend_vulkan.h"static int g_vk_ready = 0;@@ -606,6 +617,8 @@ typedef struct {const float *s;int rows, columns, gs;void *vk; /* ColiVkTensor*, caricata alla prima uso */+ int resident; /* eligible for persistent accelerator wrapping */+ void *metal; /* ColiMetalTensor*, created lazily */} Mat;typedef struct {@@ -747,7 +760,7 @@ static void quantize_i4_grouped(const float *w, uint8_t *q4, float *scale,* buffer: o lo tiene com'e' o lo libera dopo averlo quantizzato. */static Mat quantize_loaded(float *buffer, int rows, int columns) {Mat mat; memset(&mat, 0, sizeof(mat));- mat.rows = rows; mat.columns = columns;+ mat.rows = rows; mat.columns = columns; mat.resident = 1;const int bits = glm53_dense_bits();if (bits == 32) { mat.fmt = 0; mat.f = buffer; return mat; }if (bits == 4 && columns % 64 == 0) {@@ -873,7 +886,7 @@ static Mat load_mat(GModel *m, const char *fmt, ...) {if (!packed || !step) { fprintf(stderr, "OOM su %s\n", name); exit(1); }st_read_raw(&m->S, name, packed, 1);st_read_f32_cap(&m->S, scales, step, qs->numel, 1);- mat.fmt = 4; mat.q4 = packed; mat.s = step; mat.gs = 64;+ mat.fmt = 4; mat.q4 = packed; mat.s = step; mat.gs = 64; mat.resident = 1;return mat;}@@ -919,6 +932,15 @@ static void mv_rows(float *out, const Mat *w, const float *x, int row0, int rows* Il campo `vk` e' una cache dentro a una matrice che il resto del codice* tratta come sola lettura: da qui il cast, che riguarda solo lui. */static void mv(float *out, const Mat *w, const float *x) {+#ifdef COLI_METAL+ if (g_metal_ready && w->resident && (w->fmt == 1 || w->fmt == 4)) {+ Mat *mutable_w = (Mat *)w;+ if (coli_metal_matmul((ColiMetalTensor **)&mutable_w->metal, out, x,+ w->fmt == 4 ? (const void *)w->q4 : (const void *)w->q8,+ w->s, w->fmt, 1, w->columns, w->rows, w->gs))+ return;+ }+#endif#ifdef COLI_VULKANif (g_vk_ready && (w->fmt == 1 || w->fmt == 4)) {Mat *mutable_w = (Mat *)w;@@ -1155,7 +1177,14 @@ typedef struct ERef {/* I sei pezzi non sono adiacenti nel file, ma non devono esserlo nemmeno in* memoria: expert_mats ci costruisce sopra solo tre viste in sola lettura. */-typedef struct { int eid; uint8_t *piece[GLM53_EXPERT_PIECES]; uint8_t *own; uint64_t used; } Slot;+typedef struct {+ int eid;+ uint8_t *piece[GLM53_EXPERT_PIECES];+ uint8_t *own;+ size_t own_len;+ int metal_registered;+ uint64_t used;+} Slot;typedef struct LCache { Slot *s; int n, cap; } LCache;/* Lunghezze e posizioni dei sei pezzi dentro allo slot. Gate e up sono@@ -1283,23 +1312,49 @@ static Slot *slot_find(GModel *m, int layer, int eid) {static void expert_read(GModel *m, int layer, int eid, Slot *slot) {const ERef *ref = &m->eref[(size_t)layer * m->c.n_experts + eid];- int mapped_ok = 1;- for (int p = 0; p < GLM53_EXPERT_PIECES && mapped_ok; p++) {- const void *pr = st_map_shard_range(ref->fd[p], ref->off[p], m->e_len[p]);- if (!pr) { mapped_ok = 0; break; }- slot->piece[p] = (uint8_t *)pr;- }- if (mapped_ok) { slot->eid = eid; return; }- /* Fallback: si torna a scrivere in memoria NOSTRA, non in una mappatura- * di sola lettura che questo slot poteva star usando prima. */+ int metal_slot = 0;+#ifdef COLI_METAL+ metal_slot = g_metal_ready;+#endif+ /* The batched Metal MoE uses resolve() on expert pointers. st_map_shard_range+ * may return a pointer inside an mmap rather than its 16-KiB-aligned base, so+ * those views cannot be registered safely. Metal-active slots therefore own+ * one stable aligned slab; CPU-only runs retain the zero-copy mmap fast path. */+ if (!metal_slot) {+ int mapped_ok = 1;+ for (int p = 0; p < GLM53_EXPERT_PIECES && mapped_ok; p++) {+ const void *pr = st_map_shard_range(ref->fd[p], ref->off[p], m->e_len[p]);+ if (!pr) { mapped_ok = 0; break; }+ slot->piece[p] = (uint8_t *)pr;+ }+ if (mapped_ok) { slot->eid = eid; return; }+ }+ /* Fallback/Metal path: write into memory owned by the slot. Metal requires+ * page alignment and a page-multiple registration length. The base stays+ * stable across LRU reuse, so registration happens only on first allocation. */if (!slot->own) {- slot->own = malloc((size_t)m->e_slot);+ size_t need = ((size_t)m->e_slot + 16383u) & ~(size_t)16383u;+ void *p = NULL;+ if (metal_slot) {+ if (posix_memalign(&p, 16384, need) != 0) p = NULL;+ } else {+ p = malloc((size_t)m->e_slot);+ need = (size_t)m->e_slot;+ }+ slot->own = (uint8_t *)p;+ slot->own_len = need;if (!slot->own) {fprintf(stderr, "OOM su uno slot esperto (%.1f MB): la cache esperti non ci sta ""in memoria; riduci con --ram N o GLM53_EXPERT_GB=N (#1375)\n",m->e_slot / 1e6);exit(1);}+#ifdef COLI_METAL+ if (metal_slot) {+ coli_metal_register(slot->own, slot->own_len);+ slot->metal_registered = 1;+ }+#endif}for (int p = 0; p < GLM53_EXPERT_PIECES; p++) slot->piece[p] = slot->own + m->e_at[p];if (ref->contig) {@@ -1507,28 +1562,85 @@ static void ffn_layer(GModel *m, const GLayer *l, int index, const float *x,}m->t_disk += now_s() - t_batch0; /* fuori dalla regione omp: e' il muro del batch */- /* Un esperto per volta, e per ognuno tutti i token che lo hanno- * scelto. Nell'ordine opposto i suoi 12,6 MB di pesi verrebbero- * ripercorsi da capo per ogni token, e a questa taglia la banda di- * memoria e' quanto costa davvero il calcolo. */- for (int i = 0; i < here; i++) {- const int eid = union_ids[base + i];- Slot *slot = &cache->s[slot_of[i]];- slot->used = ++m->clock;- Mat gate, up, down;- expert_mats(m, slot, &gate, &up, &down);- for (int t = 0; t < tokens; t++) {- float scale = 0.0f;- for (int k = 0; k < topk; k++)- if (chosen[(size_t)t * topk + k] == eid) {- scale = weight[(size_t)t * topk + k];- break;+ /* Try all experts in this cache-sized block as one Metal command buffer.+ * xg is grouped by expert; rows/rw preserve the exact CPU scatter weights.+ * On any backend refusal/fault, run the original CPU loop unchanged. */+ int metal_done = 0;+#ifdef COLI_METAL+ if (g_metal_ready) {+ g_metal_moe_attempt++;+ const int max_rows = tokens * topk;+ const void **mg = malloc((size_t)here * sizeof(*mg));+ const void **mu = malloc((size_t)here * sizeof(*mu));+ const void **md = malloc((size_t)here * sizeof(*md));+ const float **mgs = malloc((size_t)here * sizeof(*mgs));+ const float **mus = malloc((size_t)here * sizeof(*mus));+ const float **mds = malloc((size_t)here * sizeof(*mds));+ int *xoff = malloc((size_t)here * sizeof(*xoff));+ int *nr = calloc((size_t)here, sizeof(*nr));+ int *rows = malloc((size_t)max_rows * sizeof(*rows));+ float *rw = malloc((size_t)max_rows * sizeof(*rw));+ float *xg = malloc((size_t)max_rows * c->hidden * sizeof(*xg));+ if (mg && mu && md && mgs && mus && mds && xoff && nr && rows && rw && xg) {+ int R = 0;+ for (int i = 0; i < here; i++) {+ const int eid = union_ids[base + i];+ Slot *slot = &cache->s[slot_of[i]];+ slot->used = ++m->clock;+ Mat gate, up, down;+ expert_mats(m, slot, &gate, &up, &down);+ mg[i] = gate.q4; mu[i] = up.q4; md[i] = down.q4;+ mgs[i] = gate.s; mus[i] = up.s; mds[i] = down.s;+ xoff[i] = R;+ for (int t = 0; t < tokens; t++) {+ float scale = 0.0f;+ for (int k = 0; k < topk; k++)+ if (chosen[(size_t)t * topk + k] == eid) {+ scale = weight[(size_t)t * topk + k];+ break;+ }+ if (scale == 0.0f) continue;+ memcpy(xg + (size_t)R * c->hidden,+ x + (size_t)t * c->hidden,+ (size_t)c->hidden * sizeof(float));+ rows[R] = t; rw[R] = scale; nr[i]++; R++;}- if (scale == 0.0f) continue; /* non lo ha scelto */- mlp3(tmp, x + (size_t)t * c->hidden, &gate, &up, &down,- c->swiglu_limit, sg, su);- float *dst = out + (size_t)t * c->hidden;- for (int d = 0; d < c->hidden; d++) dst[d] += scale * tmp[d];+ }+ metal_done = coli_metal_moe_block_clamped(+ here, c->hidden, c->moe_inter, 4, 64,+ mg, mu, md, mgs, mus, mds, xg, xoff, nr, rows, rw,+ out, tokens, c->swiglu_limit);+ if (metal_done) {+ g_metal_moe_ok++;+ g_metal_moe_rows += (uint64_t)R;+ }+ }+ if (!metal_done) g_metal_moe_fallback++;+ free(xg); free(rw); free(rows); free(nr); free(xoff);+ free(mds); free(mus); free(mgs); free(md); free(mu); free(mg);+ }+#endif+ if (!metal_done) {+ /* CPU fallback: one expert at a time, then every token that chose it. */+ for (int i = 0; i < here; i++) {+ const int eid = union_ids[base + i];+ Slot *slot = &cache->s[slot_of[i]];+ slot->used = ++m->clock;+ Mat gate, up, down;+ expert_mats(m, slot, &gate, &up, &down);+ for (int t = 0; t < tokens; t++) {+ float scale = 0.0f;+ for (int k = 0; k < topk; k++)+ if (chosen[(size_t)t * topk + k] == eid) {+ scale = weight[(size_t)t * topk + k];+ break;+ }+ if (scale == 0.0f) continue;+ mlp3(tmp, x + (size_t)t * c->hidden, &gate, &up, &down,+ c->swiglu_limit, sg, su);+ float *dst = out + (size_t)t * c->hidden;+ for (int d = 0; d < c->hidden; d++) dst[d] += scale * tmp[d];+ }}}}@@ -1688,6 +1800,17 @@ static void model_load_range(GModel *m, const char *dir, int layer_begin,}}vision_load(m);+#ifdef COLI_METAL+ /* Metal is runtime opt-in. Failure is non-fatal: the existing CPU path+ * remains authoritative and streamed routed experts are deliberately+ * excluded from this first integration step. */+ if (getenv("COLI_METAL") && atoi(getenv("COLI_METAL"))) {+ g_metal_ready = coli_metal_init() && coli_metal_available();+ fprintf(stderr, g_metal_ready+ ? "Metal: attivo sulle matrici residenti\n"+ : "Metal: nessun device utilizzabile, resto su CPU\n");+ }+#endif#ifdef COLI_VULKAN/* Il device si apre dopo i pesi: se non c'e', il motore continua sulla CPU* senza dire niente di piu' di una riga, perche' Vulkan qui e' un'opzione@@ -1922,6 +2045,9 @@ static float *run_layers(GModel *m, GSession *s, float *streams, float *next,* una perdita per richiesta. Ogni allocazione fatta dal caricamento ha qui il* suo rilascio, l'indice dei tensori compreso. */static void mat_release(Mat *mat) {+#ifdef COLI_METAL+ if (mat->metal) coli_metal_tensor_free((ColiMetalTensor *)mat->metal);+#endiffree((void *)mat->f); free((void *)mat->q8);free((void *)mat->q4); free((void *)mat->s);memset(mat, 0, sizeof(*mat));@@ -3010,6 +3136,14 @@ int main(int argc, char **argv) {if (model.streaming)printf("experts hits %ld miss %ld bytes %llu\n",model.hits, model.miss, (unsigned long long)model.ebytes);+#ifdef COLI_METAL+ if (g_metal_ready && getenv("GLM53_VERBOSE") && atoi(getenv("GLM53_VERBOSE")))+ printf("metal moe attempts %llu ok %llu fallback %llu rows %llu\n",+ (unsigned long long)g_metal_moe_attempt,+ (unsigned long long)g_metal_moe_ok,+ (unsigned long long)g_metal_moe_fallback,+ (unsigned long long)g_metal_moe_rows);+#endiffree(logits);session_close(&model, session);free(vision);diff --git a/c/tests/test_glm53_cap_launch_source.py b/c/tests/test_glm53_cap_launch_source.pynew file mode 100644index 00000000..13db5660--- /dev/null+++ b/c/tests/test_glm53_cap_launch_source.py@@ -0,0 +1,22 @@+#!/usr/bin/env python3+from pathlib import Path+import unittest++SRC = Path(__file__).resolve().parents[1] / "coli"++class GLM53CapLaunchSourceTests(unittest.TestCase):+ @classmethod+ def setUpClass(cls):+ cls.s = SRC.read_text()++ def test_glm53_one_shot_uses_cap_for_launch(self):+ self.assertIn('cap = cap_for_launch(a.cap, e, 0)', self.s)++ def test_cap_is_positional_before_glm53_flags(self):+ self.assertIn('cmd=[engine,str(cap),"--model",os.path.abspath(a.model),"--prompt",prompt,', self.s)++ def test_old_cap_dropping_form_is_gone(self):+ self.assertNotIn('cmd=[engine,"--model",os.path.abspath(a.model),"--prompt",prompt,', self.s)++if __name__ == "__main__":+ unittest.main()diff --git a/c/tests/test_glm53_clamped_metal.c b/c/tests/test_glm53_clamped_metal.cnew file mode 100644index 00000000..ac1d6099--- /dev/null+++ b/c/tests/test_glm53_clamped_metal.c@@ -0,0 +1,48 @@+#include <math.h>+#include <stdio.h>+#include <stdlib.h>+#include "../backend_metal.h"++static float ref(float g, float u, float limit) {+ float gv = fminf(g, limit);+ float uv = fmaxf(-limit, fminf(u, limit));+ return (gv / (1.0f + expf(-gv))) * uv;+}++int main(void) {+ const float limit = 10.0f;+ float g[] = {-20.0f,-10.0001f,-10.0f,-9.9999f,-1.0f,-0.0f,0.0f,1.0f,9.9999f,10.0f,10.0001f,20.0f,+ 3.25f,-7.75f,12.5f,0.125f};+ float u[] = {-20.0f,-10.0001f,-10.0f,-9.9999f,-1.0f,-0.0f,0.0f,1.0f,9.9999f,10.0f,10.0001f,20.0f,+ -12.5f,7.75f,3.25f,-0.125f};+ enum { N = sizeof(g)/sizeof(g[0]) };+ float expect[N];+ for (int i=0;i<N;i++) expect[i]=ref(g[i],u[i],limit);++ if (!coli_metal_init() || !coli_metal_available()) {+ fprintf(stderr,"SKIP: Metal unavailable\n");+ return 77;+ }+ if (!coli_metal_silu_mul_clamped(g,u,N,limit)) {+ fprintf(stderr,"FAIL: Metal clamped SwiGLU call failed\n");+ return 2;+ }++ float max_abs=0.0f, max_rel=0.0f;+ int bad=0;+ for (int i=0;i<N;i++) {+ float ae=fabsf(g[i]-expect[i]);+ float re=ae/fmaxf(1.0f,fabsf(expect[i]));+ if (ae>max_abs) max_abs=ae;+ if (re>max_rel) max_rel=re;+ if (!(ae <= 2e-5f || re <= 2e-5f)) {+ fprintf(stderr,"mismatch[%d]: metal=%.9g cpu=%.9g abs=%.3g rel=%.3g\n",i,g[i],expect[i],ae,re);+ bad++;+ }+ }+ printf("GLM53 clamped SwiGLU oracle: n=%d limit=%.3f max_abs=%.9g max_rel=%.9g\n",N,limit,max_abs,max_rel);+ coli_metal_shutdown();+ if (bad) { fprintf(stderr,"FAIL: %d mismatches\n",bad); return 1; }+ puts("GLM53 clamped SwiGLU oracle: OK");+ return 0;+}diff --git a/c/tests/test_glm53_metal_clamped_source.py b/c/tests/test_glm53_metal_clamped_source.pynew file mode 100644index 00000000..d8d39994--- /dev/null+++ b/c/tests/test_glm53_metal_clamped_source.py@@ -0,0 +1,37 @@+#!/usr/bin/env python3+"""Source contract for GLM-5.3 clamped-SwiGLU Metal support.++GLM-5.3 must not use the existing plain SwiGLU routed-expert kernel. This test+protects a separate clamped kernel/API while the engine wiring is added next.+"""+from pathlib import Path+import unittest++ROOT = Path(__file__).resolve().parents[1]+HDR = (ROOT / "backend_metal.h").read_text()+SRC = (ROOT / "backend_metal.mm").read_text()+++class Glm53MetalClampedSourceTests(unittest.TestCase):+ def test_separate_clamped_kernel_exists(self):+ self.assertIn("kernel void moe_silu_clamped", SRC)+ self.assertIn("float v=min(g[i], limit)", SRC)+ self.assertIn("float uv=clamp(u[i], -limit, limit)", SRC)+ self.assertIn("(v/(1.0f+exp(-v)))*uv", SRC)++ def test_separate_pipeline_is_compiled(self):+ self.assertIn("g_moe_silu_clamped", SRC)+ self.assertIn('P("moe_silu_clamped")', SRC)++ def test_public_clamped_moe_api_exists(self):+ self.assertIn("coli_metal_moe_block_clamped", HDR)+ self.assertIn("float swiglu_limit", HDR)+ self.assertIn('extern "C" int coli_metal_moe_block_clamped', SRC)++ def test_plain_moe_api_remains_unchanged(self):+ self.assertIn("int coli_metal_moe_block(int nb", HDR)+ self.assertIn("g_moe_silu];", SRC)+++if __name__ == "__main__":+ unittest.main()diff --git a/c/tests/test_glm53_metal_routed_source.py b/c/tests/test_glm53_metal_routed_source.pynew file mode 100644index 00000000..186da3a4--- /dev/null+++ b/c/tests/test_glm53_metal_routed_source.py@@ -0,0 +1,35 @@+#!/usr/bin/env python3+from pathlib import Path+import unittest++ROOT = Path(__file__).resolve().parents[1]+SRC = (ROOT / "glm53.c").read_text()+++class Glm53MetalRoutedSourceTests(unittest.TestCase):+ def test_metal_slots_are_page_aligned_and_registered(self):+ self.assertIn("posix_memalign(&p, 16384, need)", SRC)+ self.assertIn("coli_metal_register(slot->own, slot->own_len)", SRC)+ self.assertIn("metal_slot = g_metal_ready", SRC)++ def test_cpu_mmap_path_is_preserved(self):+ self.assertIn("if (!metal_slot)", SRC)+ self.assertIn("st_map_shard_range", SRC)+ self.assertIn("if (mapped_ok) { slot->eid = eid; return; }", SRC)++ def test_routed_experts_use_clamped_batched_api(self):+ self.assertIn("coli_metal_moe_block_clamped(", SRC)+ self.assertIn("here, c->hidden, c->moe_inter, 4, 64", SRC)+ self.assertIn("out, tokens, c->swiglu_limit", SRC)++ def test_metal_failure_keeps_cpu_fallback(self):+ self.assertIn("if (!metal_done)", SRC)+ self.assertIn("mlp3(tmp, x + (size_t)t * c->hidden", SRC)++ def test_packed_rows_keep_router_weights(self):+ self.assertIn("rows[R] = t; rw[R] = scale; nr[i]++; R++;", SRC)+ self.assertIn("xoff[i] = R", SRC)+++if __name__ == "__main__":+ unittest.main()diff --git a/c/tests/test_glm53_metal_source.py b/c/tests/test_glm53_metal_source.pynew file mode 100644index 00000000..f9ea6cea--- /dev/null+++ b/c/tests/test_glm53_metal_source.py@@ -0,0 +1,48 @@+#!/usr/bin/env python3+"""Source-level contract for the first GLM-5.3 Metal integration step.++This intentionally does not claim routed-expert acceleration yet. It protects+build wiring, opt-in initialization, resident fmt=1/fmt=4 dispatch, CPU fallback,+and Metal-handle cleanup while the MoE path is implemented separately.+"""+from pathlib import Path+import unittest++ROOT = Path(__file__).resolve().parents[1]+SRC = (ROOT / "glm53.c").read_text()+MAKE = (ROOT / "Makefile").read_text()+++class Glm53MetalSourceTests(unittest.TestCase):+ def test_backend_is_opt_in(self):+ self.assertIn('#ifdef COLI_METAL\n#include "backend_metal.h"', SRC)+ self.assertIn('getenv("COLI_METAL")', SRC)+ self.assertIn('coli_metal_init()', SRC)+ self.assertIn('coli_metal_available()', SRC)++ def test_resident_matmul_has_metal_dispatch_and_cpu_fallback(self):+ start = SRC.index("static void mv(float *out, const Mat *w, const float *x)")+ end = SRC.index("static void rms(", start)+ body = SRC[start:end]+ self.assertIn("g_metal_ready", body)+ self.assertIn("coli_metal_matmul", body)+ self.assertIn("w->fmt == 1 || w->fmt == 4", body)+ self.assertIn("matmul_i4_grouped", body)+ self.assertIn("matmul_q", body)++ def test_metal_handle_is_released(self):+ start = SRC.index("static void mat_release(Mat *mat)")+ end = SRC.index("static void model_release", start)+ body = SRC[start:end]+ self.assertIn("coli_metal_tensor_free", body)++ def test_glm53_links_metal_object(self):+ start = MAKE.index("glm53$(EXE):")+ end = MAKE.index("kimi_k3$(EXE):", start)+ rule = MAKE[start:end]+ self.assertIn("backend_metal.h", rule)+ self.assertIn("$(METAL_OBJ)", rule)+++if __name__ == "__main__":+ unittest.main()diff --git a/c/tests/test_glm53_metal_stats_source.py b/c/tests/test_glm53_metal_stats_source.pynew file mode 100644index 00000000..8d3285f1--- /dev/null+++ b/c/tests/test_glm53_metal_stats_source.py@@ -0,0 +1,40 @@+#!/usr/bin/env python3+import unittest+from pathlib import Path++ROOT = Path(__file__).resolve().parents[2]+SRC = ROOT / "c" / "glm53.c"+++class Glm53MetalStatsSourceTest(unittest.TestCase):+ @classmethod+ def setUpClass(cls):+ cls.src = SRC.read_text()++ def test_counters_declared(self):+ for needle in (+ "g_metal_moe_attempt",+ "g_metal_moe_ok",+ "g_metal_moe_fallback",+ "g_metal_moe_rows",+ ):+ self.assertIn(needle, self.src)++ def test_attempt_counted_at_routed_dispatch(self):+ self.assertIn("g_metal_moe_attempt++;", self.src)+ self.assertIn("metal_done = coli_metal_moe_block_clamped(", self.src)++ def test_success_and_fallback_counted(self):+ self.assertIn("g_metal_moe_ok++;", self.src)+ self.assertIn("g_metal_moe_rows += (uint64_t)R;", self.src)+ self.assertIn("if (!metal_done) g_metal_moe_fallback++;", self.src)++ def test_verbose_summary_emitted(self):+ self.assertIn(+ 'printf("metal moe attempts %llu ok %llu fallback %llu rows %llu\\n",',+ self.src,+ )+++if __name__ == "__main__":+ unittest.main()diff --git a/c/tests/test_glm53_moe_block_metal.c b/c/tests/test_glm53_moe_block_metal.cnew file mode 100644index 00000000..82e8fb12--- /dev/null+++ b/c/tests/test_glm53_moe_block_metal.c@@ -0,0 +1,148 @@+#define _POSIX_C_SOURCE 200112L+#include <math.h>+#include <stdint.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#include "../backend_metal.h"+#include "../quant.h"++enum { NB = 2, D = 64, II = 64, S = 3, GS = 64, SLAB = 16384 };++typedef struct {+ uint8_t *base;+ uint8_t *g, *u, *d;+ float *gs, *us, *ds;+} Expert;++static size_t align_up(size_t x, size_t a) { return (x + a - 1) & ~(a - 1); }++static void pack_matrix(uint8_t *q4, float *sc, int O, int I, int seed) {+ const int rb = (I + 1) / 2;+ const int ng = (I + GS - 1) / GS;+ memset(q4, 0, (size_t)O * rb);+ for (int o = 0; o < O; o++) {+ for (int g = 0; g < ng; g++)+ sc[(size_t)o * ng + g] = 0.0065f + 0.00017f * (float)((o + 3*g + seed) % 11);+ for (int i = 0; i < I; i++) {+ int q = ((o * 17 + i * 13 + seed * 7) % 16) - 8;+ uint8_t n = (uint8_t)(q + 8);+ uint8_t *b = &q4[(size_t)o * rb + (i >> 1)];+ if (i & 1) *b |= (uint8_t)(n << 4);+ else *b = (uint8_t)((*b & 0xf0u) | n);+ }+ }+}++static int make_expert(Expert *e, int seed) {+ void *p = NULL;+ if (posix_memalign(&p, 16384, SLAB) != 0 || !p) return 0;+ memset(p, 0, SLAB);+ e->base = (uint8_t *)p;++ const size_t q_g = (size_t)II * ((D + 1) / 2);+ const size_t s_g = (size_t)II * ((D + GS - 1) / GS) * sizeof(float);+ const size_t q_d = (size_t)D * ((II + 1) / 2);+ const size_t s_d = (size_t)D * ((II + GS - 1) / GS) * sizeof(float);++ size_t at = 0;+ e->g = e->base + at; at += q_g;+ at = align_up(at, 16); e->gs = (float *)(e->base + at); at += s_g;+ at = align_up(at, 16); e->u = e->base + at; at += q_g;+ at = align_up(at, 16); e->us = (float *)(e->base + at); at += s_g;+ at = align_up(at, 16); e->d = e->base + at; at += q_d;+ at = align_up(at, 16); e->ds = (float *)(e->base + at); at += s_d;+ if (at > SLAB) return 0;++ pack_matrix(e->g, e->gs, II, D, seed + 1);+ pack_matrix(e->u, e->us, II, D, seed + 2);+ pack_matrix(e->d, e->ds, D, II, seed + 3);+ return 1;+}++static void cpu_one(const Expert *e, const float *x, float *y, float limit) {+ float g[II], u[II], h[D];+ matmul_i4_grouped(g, x, e->g, e->gs, 1, D, II, GS);+ matmul_i4_grouped(u, x, e->u, e->us, 1, D, II, GS);+ for (int i = 0; i < II; i++) {+ float v = fminf(g[i], limit);+ float uv = fmaxf(-limit, fminf(u[i], limit));+ g[i] = (v / (1.0f + expf(-v))) * uv;+ }+ matmul_i4_grouped(h, g, e->d, e->ds, 1, II, D, GS);+ memcpy(y, h, sizeof(h));+}++int main(void) {+ const float limit = 10.0f;+ Expert ex[NB] = {0};+ if (!make_expert(&ex[0], 3) || !make_expert(&ex[1], 19)) {+ fprintf(stderr, "expert slab allocation failed\n");+ return 2;+ }+ if (!coli_metal_init() || !coli_metal_available()) {+ fprintf(stderr, "Metal unavailable\n");+ return 77;+ }+ for (int e = 0; e < NB; e++) coli_metal_register(ex[e].base, SLAB);++ const void *g[NB] = { ex[0].g, ex[1].g };+ const void *u[NB] = { ex[0].u, ex[1].u };+ const void *d[NB] = { ex[0].d, ex[1].d };+ const float *gs[NB] = { ex[0].gs, ex[1].gs };+ const float *us[NB] = { ex[0].us, ex[1].us };+ const float *ds[NB] = { ex[0].ds, ex[1].ds };++ /* Packed rows: expert 0 handles rows 0/2, expert 1 handles rows 1/2. */+ const int nr[NB] = { 2, 2 };+ const int xoff[NB] = { 0, 2 };+ const int rows[4] = { 0, 2, 1, 2 };+ const float rw[4] = { 0.75f, 0.20f, 0.55f, 0.35f };+ float xg[4 * D];+ for (int r = 0; r < 4; r++)+ for (int i = 0; i < D; i++)+ xg[(size_t)r * D + i] = ((float)(((r + 1) * 29 + i * 7) % 41) - 20.0f) / 9.0f;++ float ref[S * D]; memset(ref, 0, sizeof(ref));+ float got[S * D]; memset(got, 0, sizeof(got));+ int roff = 0;+ for (int e = 0; e < NB; e++) {+ for (int j = 0; j < nr[e]; j++) {+ float h[D];+ cpu_one(&ex[e], xg + (size_t)(xoff[e] + j) * D, h, limit);+ float *dst = ref + (size_t)rows[roff + j] * D;+ for (int k = 0; k < D; k++) dst[k] += rw[roff + j] * h[k];+ }+ roff += nr[e];+ }++ int ok = coli_metal_moe_block_clamped(NB, D, II, 4, GS,+ g, u, d, gs, us, ds, xg, xoff, nr, rows, rw, got, S, limit);+ if (!ok) {+ fprintf(stderr, "Metal routed MoE block returned fallback\n");+ return 3;+ }++ double max_abs = 0.0, max_rel = 0.0;+ int worst = -1;+ for (int i = 0; i < S * D; i++) {+ double ae = fabs((double)got[i] - (double)ref[i]);+ double re = ae / (1.0 + fabs((double)ref[i]));+ if (ae > max_abs) { max_abs = ae; worst = i; }+ if (re > max_rel) max_rel = re;+ }+ printf("GLM53 routed Metal MoE oracle: max_abs=%.9g max_rel=%.9g worst=%d\n",+ max_abs, max_rel, worst);++ const double abs_tol = 2e-4, rel_tol = 2e-4;+ int pass = (max_abs <= abs_tol || max_rel <= rel_tol);+ printf("GLM53 routed Metal MoE oracle: %s\n", pass ? "OK" : "FAIL");++ for (int e = 0; e < NB; e++) {+ coli_metal_unregister(ex[e].base);+ free(ex[e].base);+ }+ coli_metal_shutdown();+ return pass ? 0 : 4;+}