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

Merge dev into feat/kimi-cuda-situ-pipeline: keep both MXFP4 scratch sets, both cuda-test additions

JustVugg7edf6eb81886
diff --cc c/Makefile
index 18a1cd32,82c7b1d1..8cd95fae
--- a/c/Makefile
+++ b/c/Makefile
@@@ -956,7 -956,7 +956,7 @@@ rans: $(RANSLIB
$(RANSLIB): tools/rans_ctypes.c rans.h
$(CC) $(CFLAGS) -fPIC -shared $< -o $@ $(LDFLAGS)
- cuda-test: tests/test_mxfp4_expert_cuda.cu backend_cuda.cu backend_cuda.h backend_gpu_compat.h tests/test_backend_cuda.cu tests/test_ragged_attention.cu tests/test_absorb_determinism.cu tests/test_mxfp4_cuda.cu tests/mxfp4_ref.c tests/test_fp8_warp_cuda.cu tests/test_fp8_cuda.cu tests/test_weights_owned_cuda.cu tests/test_cuda_fmt_trap_cuda.cu tests/test_alloc_footprint_cuda.cu
-cuda-test: backend_cuda.cu backend_cuda.h backend_gpu_compat.h tests/test_backend_cuda.cu tests/test_ragged_attention.cu tests/test_absorb_determinism.cu tests/test_mxfp4_cuda.cu tests/mxfp4_ref.c tests/test_fp8_warp_cuda.cu tests/test_fp8_cuda.cu tests/test_weights_owned_cuda.cu tests/test_cuda_fmt_trap_cuda.cu tests/test_alloc_footprint_cuda.cu tests/test_cuda_init_failure.cu
++cuda-test: tests/test_mxfp4_expert_cuda.cu backend_cuda.cu backend_cuda.h backend_gpu_compat.h tests/test_backend_cuda.cu tests/test_ragged_attention.cu tests/test_absorb_determinism.cu tests/test_mxfp4_cuda.cu tests/mxfp4_ref.c tests/test_fp8_warp_cuda.cu tests/test_fp8_cuda.cu tests/test_weights_owned_cuda.cu tests/test_cuda_fmt_trap_cuda.cu tests/test_alloc_footprint_cuda.cu tests/test_cuda_init_failure.cu
@command -v "$(GPUCC)" >/dev/null 2>&1 || { echo "$(GPUCC_NAME) not found" >&2; exit 1; }
"$(GPUCC)" $(GPUFLAGS) backend_cuda.cu tests/test_backend_cuda.cu -o backend_cuda_test$(EXE) $(ANS_NVCC_LIBS) $(GPU_TEST_LIBS)
./backend_cuda_test$(EXE)
diff --cc c/backend_cuda.cu
index becb6472,3deaa9eb..a0c892a5
--- a/c/backend_cuda.cu
+++ b/c/backend_cuda.cu
@@@ -82,8 -87,9 +87,11 @@@ typedef struct
int compute_major,compute_minor;
float *x, *y, *gate, *up;
size_t x_cap, y_cap, gate_cap, up_cap;
+ /* Streaming MXFP4 weights are refreshed on every call; only storage is reused. */
+ void *mxfp4_weights, *mxfp4_scales;
+ size_t mxfp4_weights_cap, mxfp4_scales_cap;
+ void *mxfp4_expert_weights, *mxfp4_expert_scales;
+ size_t mxfp4_expert_weights_cap, mxfp4_expert_scales_cap;
/* Staging of the resident dense matvec (coli_cuda_matmul), apart from
* x/y: the expert group (coli_cuda_expert_group_issue) runs on ctx->stream
* asynchronously while the engine's thread keeps computing -- qwen38's
@@@ -1298,8 -1302,8 +1312,10 @@@ extern "C" void coli_cuda_shutdown(void
for (int i = 0; i < g_nctx; i++) {
DeviceContext *ctx = &g_ctx[i];
if (!select_ctx(ctx)) continue;
+ if (ctx->mxfp4_weights) cudaFree(ctx->mxfp4_weights);
+ if (ctx->mxfp4_scales) cudaFree(ctx->mxfp4_scales);
+ if (ctx->mxfp4_expert_weights) cudaFree(ctx->mxfp4_expert_weights);
+ if (ctx->mxfp4_expert_scales) cudaFree(ctx->mxfp4_expert_scales);
if (ctx->x) cudaFree(ctx->x);
if (ctx->y) cudaFree(ctx->y);
if (ctx->dx) cudaFree(ctx->dx);
@@@ -1324,8 -1328,8 +1340,10 @@@
ctx->ans_scratch=nullptr;ctx->ans_chunks=nullptr;ctx->ans_raw=nullptr;ctx->ans_raw_cap=0;
ctx->ans_host=nullptr;ctx->ans_host_cap=0;ctx->ans_copy_pending=0;
#endif
+ ctx->mxfp4_weights = ctx->mxfp4_scales = nullptr;
+ ctx->mxfp4_weights_cap = ctx->mxfp4_scales_cap = 0;
+ ctx->mxfp4_expert_weights = ctx->mxfp4_expert_scales = nullptr;
+ ctx->mxfp4_expert_weights_cap = ctx->mxfp4_expert_scales_cap = 0;
ctx->x = ctx->y = ctx->gate = ctx->up = nullptr;
ctx->dx = ctx->dy = nullptr; ctx->dx_cap = ctx->dy_cap = 0;
ctx->qx=nullptr; ctx->qscale=nullptr;