Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
docs(deepseek-v41): what the blocking bought, and the two overlaps that did not
JustVugg53669e27f542
diff --git a/c/deepseek_v41.c b/c/deepseek_v41.cindex 23962156..c499572e 100644--- a/c/deepseek_v41.c+++ b/c/deepseek_v41.c@@ -1889,11 +1889,18 @@ static void moe_run_at(Model *m, Layer *l, LCache *cache, const char *kind, intfloat *gathered = xmalloc((size_t)rows * dim * sizeof(float), "expert inputs");float *down = xmalloc((size_t)rows * dim * sizeof(float), "expert outputs");int step = cache->cap < MOE_ROW_CHUNK ? cache->cap : MOE_ROW_CHUNK;- /* A readahead hint for the next chunk, issued here while this one is being- * multiplied, was tried and measured worse: posix_fadvise(WILLNEED) blocks- * against a device that is already saturated by the demand reads, and it- * cost four seconds of wall to save one of disk. The same shape was already- * measured on the V4 engine. Left as a note so it is not tried a third time. */+ /* Two ways of overlapping these reads with the matmuls were built and+ * measured here, and both were worse. A posix_fadvise(WILLNEED) hint for the+ * next chunk blocks against a device already saturated by the demand reads:+ * four seconds of wall to save one of disk, the same result the V4 engine+ * got. A pool of reader threads, so each expert is multiplied as soon as its+ * own six tensors land, does overlap -- the main thread's wait on disk falls+ * from 10.4 s to 1.0 s -- but the expert matmul rises from 7.6 s to 18.1 s+ * and the turn is 7% slower, 25% slower at four readers and 68% slower at two. A 5.9 MB pread is+ * not free CPU: it is a kernel-side copy competing for the same memory+ * bandwidth the matmuls are already bound by, and the idle disk wait it+ * removes costs more than it was worth. Both are measurements, not opinions;+ * they are written down so the third attempt starts from them. */for (int u0 = 0; u0 < n_uniq; u0 += step) {int ne = n_uniq - u0 < step ? n_uniq - u0 : step;Slot *slot[MOE_ROW_CHUNK];diff --git a/docs/deepseek-v41.md b/docs/deepseek-v41.mdindex 333d8cc6..63b2134e 100644--- a/docs/deepseek-v41.md+++ b/docs/deepseek-v41.md@@ -180,10 +180,62 @@ while the rate barely moves. The depth-8 row is two independent cold runs, to shmuch of the wall is run-to-run noise: the I/O figures repeat to within half a percent,the compute figures do not.-Prefill is where most of it goes: 54.6 GB of the 88.5, because each of the 26 prompt+Prefill is where most of it goes: 52.3 GB of the 87.4, because each of the 26 promptpositions routes independently and a 49-slot cache cannot hold what 26 positions askof one layer.+## Blocks of positions, not one position at a time++Everything above the experts used to run one position at a time, which meant a block+of positions made one full pass over every matrix per position. The matrices are the+things that do not fit in cache: wq_b is 42 MB on the released checkpoint, so is wo_b,+the shared expert is 35 MB of them and the engram projection is 157 MB. Prefill of a+26-token prompt was reading each of those twenty-six times per layer.++So the engine now drives a block of positions through one pass instead. `mv8_rows`+does it for the fp8 projections, holding the decoded weight tile across the block;+the output projection waits until every position has its heads, because it reads the+heads and writes the output and touches no cached state; the MoE routes the whole+block first, reduces the six-per-position draws to a list of distinct experts, and+applies each expert to every position that asked for it, which matmul_mxfp4 was+already able to do for a block of inputs.++None of it moves a value. Each output folds the same tiles in the same order, and+each expert's contribution is kept apart and summed into the output in rank order+afterwards, exactly as the position-major loop accumulated it. Blocks are capped at+32 positions so the scratch stays a few megabytes however long the prompt is.++Cold, same turn, same prompt, same seed, each step measured on the one before it:++| | wall | disk | expert matmul | attention | tok/s |+|---|---|---|---|---|---|+| serial reads, scalar kernels | 78.7 s | 36.9 s | 15.0 s | 19.1 s | 0.305 |+| batched expert reads | 42.4 s | 13.0 s | 8.4 s | 16.1 s | 0.566 |+| vector attention kernel | 41.1 s | 12.9 s | 7.8 s | 15.9 s | 0.584 |+| blocked attention matrices | 29.5 s | 12.7 s | 9.1 s | 3.4 s | 0.812 |+| expert-major MoE | 25.1 s | 10.4 s | 7.6 s | 3.4 s | 0.957 |++Of that last 25.1 s turn, prefill is 10.7 s (5.9 disk, 2.3 matmul) and decode is+13.8 s (4.5 disk, 4.7 matmul).++## Two ways of hiding the reads that did not work++Both are written down because they are the obvious next ideas and both were built,+measured cold, and removed.++A `posix_fadvise(WILLNEED)` hint for the next chunk of experts, issued while the+current one is being multiplied, blocks against a device that the demand reads have+already saturated. It cost four seconds of wall to save one of disk. The V4 engine+reached the same conclusion by a different route.++A pool of reader threads, so each expert is multiplied as soon as its own six tensors+land, does overlap: the main thread's wait on disk falls from 10.4 s to 1.0 s. But the+expert matmul rises from 7.6 s to 18.1 s and the turn is 7% slower with eight readers,+25% slower with four and 68% slower with two. A 5.9 MB pread is not free CPU -- it is a kernel-side copy+competing for the memory bandwidth the matmuls are already bound by -- so moving the+work off the disk wait and onto the cores that were doing the arithmetic buys nothing+and costs the contention. Anything that tries again has to start from that.+## Environment| variable | default | what it does |