Skip to content

A 2.78-trillion-parameter Kimi K3 running inference on a single CPU in 8.24 GB of RAM. Portable C99: no BLAS, no framework, no GPU.

fix(trunk): charge a prefetched layer to the bind that reaches it

Fareed Khan7721a1b1661f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 02df6b1..cb10595 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,17 @@ versioning follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
+### Fixed
+
+- **The trunk report counted a prefetched layer as a hit** whenever its read finished
+ before the walk bound it. On a fully streamed run of the tiny checkpoint, every one of
+ the 156 layer binds was a real read ("reads 156 against 156 the walk owes"), and the
+ line above it still said `hits 144 (92.3%), reads 12`. A layer the reader fetched
+ ahead is now charged as a read to the bind that reaches it, whichever thread gets there
+ first, so the counts no longer depend on timing and the hit rate again tracks the
+ pinned fraction. Counting only: output is unchanged. This was also the cause of an
+ intermittent `test_trunk` failure on CI, which now tests both interleavings.
+
## [1.1.0] - 2026-10-02
Chat, native Windows builds alongside Linux and macOS, an 8 GB class mode for the
diff --git a/src/io/k3_trunk.c b/src/io/k3_trunk.c
index 394f802..4f50dda 100644
--- a/src/io/k3_trunk.c
+++ b/src/io/k3_trunk.c
@@ -62,6 +62,7 @@ typedef struct {
int qslot[K3_TRUNK_MAXRING];
int nq;
int pending[K3_TRUNK_MAXRING];/* [slot] layer being read, or -1 */
+ int fresh[K3_TRUNK_MAXRING]; /* [slot] read ahead, not yet bound */
int held; /* slot the caller is using, or -1 */
} K3TrunkIO;
@@ -686,6 +687,7 @@ static int claim_slot_locked(K3Trunk *tr, K3TrunkIO *io, int L)
tr->ring = (s + 1) % tr->nslot;
if (tr->layer_of[s] >= 0) tr->slot_of[tr->layer_of[s]] = -1;
tr->layer_of[s] = -1; /* EMPTY before the read, not after */
+ io->fresh[s] = 0;
io->pending[s] = L;
return s;
}
@@ -728,9 +730,12 @@ static void *trunk_io_main(void *arg)
* a bind that waits for a prefetch is one bind, not one miss plus one hit;
* k3_trunk_bind owns that classification so the two always sum to the bind
* count. Counting here as well inflated the hit rate by the prefetch rate,
- * which is precisely the number the rate is meant to expose. */
+ * which is precisely the number the rate is meant to expose. The slot is
+ * marked fresh instead, so that bind charges this read even when it lands
+ * before the walk arrives. */
tr->layer_of[slot] = L;
tr->slot_of[L] = slot;
+ io->fresh[slot] = 1;
}
pthread_cond_broadcast(&io->cv_done);
pthread_mutex_unlock(&io->mu);
@@ -772,14 +777,24 @@ int k3_trunk_fetch(K3Trunk *tr, int L, unsigned char **out)
pthread_mutex_lock(&io->mu);
io->held = -1; /* release the previous layer's slot before asking for one */
pthread_cond_broadcast(&io->cv_done);
- /* Classify this bind ONCE, on the first look. Resident already means no bytes
- * moved for it; anything else means bytes did, whether this thread read them or
- * it waited for the reader to finish. hits + misses therefore equals the bind
- * count, which is what makes the printed rate mean anything. */
+ /* Classify this bind ONCE, on the first look. Resident from an earlier bind
+ * means no bytes moved for it; anything else means bytes did, whether this
+ * thread read them or the reader did, before the walk arrived or after. hits +
+ * misses therefore equals the bind count, which is what makes the printed rate
+ * mean anything.
+ *
+ * `fresh` is what makes "before or after" hold. Without it a prefetch that had
+ * already landed counted as a hit and one still in flight as a miss, so the same
+ * run gave different counts depending on which thread won: test_trunk's
+ * two-slot case failed on CI whenever the reader finished first. */
int first = 1;
for (;;) {
slot = tr->slot_of[L];
- if (slot >= 0) { if (first) tr->hits++; break; }
+ if (slot >= 0) {
+ if (first) { if (io->fresh[slot]) tr->misses++; else tr->hits++; }
+ io->fresh[slot] = 0;
+ break;
+ }
if (first) { tr->misses++; first = 0; }
const int pend = pending_slot_locked(tr, io, L);
if (pend >= 0) { /* the reader is already fetching it */
diff --git a/tests/unit/test_trunk.c b/tests/unit/test_trunk.c
index 43ab7a4..40dd914 100644
--- a/tests/unit/test_trunk.c
+++ b/tests/unit/test_trunk.c
@@ -27,6 +27,9 @@
* 7 REFUSAL corrupt manifests (empty layers, negative geometry, a
* tensor escaping its run) are refused at open, with
* nothing left allocated behind them.
+ * 8 RACE a prefetch that lands before the bind looks is still that
+ * bind's read, so the counts do not depend on which thread
+ * wins. They did, and §2 failed on CI when the reader won.
*
* usage: test_trunk
* writes a synthetic 3-layer trunk fixture to a temp directory, then drives
@@ -42,6 +45,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
+#include <time.h> /* nanosleep */
#include <unistd.h> /* ftruncate */
#include "k3.h"
@@ -421,9 +425,9 @@ static int test_two_slot(const char *dir, const K3Cfg *c)
/* Prefetch L1 -- starts async read into slot 1 */
k3_trunk_prefetch(&tr, 1);
- /* Bind L1 -- blocks until reader finishes, publishes slot 1.
- * The miss was recorded inside trunk_io_wait when it published the slot;
- * k3_trunk_bind itself records a hit only on the synchronous lookup path. */
+ /* Bind L1 -- blocks until the reader finishes, if it has not already.
+ * Either way this bind is charged one miss for the read; test_landed_first
+ * forces the other interleaving and expects the same counts. */
K3LayerBind b1; memset(&b1, 0, sizeof b1);
if (k3_trunk_bind(&tr, c, 1, &b1) != 0) { k3_trunk_close(&tr); return 1; }
ck(tr.hits == 0 && tr.misses == 2, "two-slot: after bind L1, misses=2", "");
@@ -478,6 +482,52 @@ static int test_two_slot(const char *dir, const K3Cfg *c)
return 0;
}
+static void nap_ms(int ms)
+{
+#ifdef _WIN32
+ Sleep((DWORD)ms);
+#else
+ struct timespec t;
+ t.tv_sec = ms / 1000;
+ t.tv_nsec = (long)(ms % 1000) * 1000000L;
+ nanosleep(&t, NULL);
+#endif
+}
+
+/* The other interleaving of §2: give the reader time to land L1 before the bind
+ * looks. The bind must still be charged the read, exactly as when it has to wait.
+ * If the reader is slower than the nap, the bind waits instead and the expected
+ * counts are the same, so this cannot fail on a slow machine, only on wrong code. */
+static int test_landed_first(const char *dir, const K3Cfg *c)
+{
+ K3Trunk tr;
+ if (k3_trunk_open(&tr, dir, c, 24576, 0) != 0) {
+ fprintf(stderr, "TRUNK OPEN FAILED (landed-first)\n"); return 1;
+ }
+ ck(tr.nslot == 2, "landed-first: ring has 2 slots", "");
+
+ int dt_ti = 18;
+ K3LayerBind b0; memset(&b0, 0, sizeof b0);
+ if (k3_trunk_bind(&tr, c, 0, &b0) != 0) { k3_trunk_close(&tr); return 1; }
+
+ k3_trunk_prefetch(&tr, 1);
+ nap_ms(200);
+
+ K3LayerBind b1; memset(&b1, 0, sizeof b1);
+ if (k3_trunk_bind(&tr, c, 1, &b1) != 0) { k3_trunk_close(&tr); return 1; }
+ ck(tr.hits == 0 && tr.misses == 2,
+ "landed-first: bind after the read landed, misses=2", "");
+ int dt_ok_1 = check_marker((const unsigned char *)b1.kda.dt_bias, 1, dt_ti, 32);
+ ck(dt_ok_1, "landed-first: bind L1 content correct", "");
+
+ /* Binding the same resident layer again moved no bytes: that one is a hit. */
+ if (k3_trunk_bind(&tr, c, 1, &b1) != 0) { k3_trunk_close(&tr); return 1; }
+ ck(tr.hits == 1 && tr.misses == 2, "landed-first: rebind L1 is a hit", "");
+
+ k3_trunk_close(&tr);
+ return 0;
+}
+
/* Truncated read: a fresh open, then truncate trunk.bin at layer 2's file_off.
* Prefetch+bind of L2 must fail and leave no slot published. */
static int test_truncated(const char *dir, const K3Cfg *c)
@@ -658,6 +708,10 @@ int main(void)
if (test_two_slot(tmpdir, &c) != 0) g_fail++;
printf("\n");
+ /* §2b two-slot, the prefetch lands before the bind looks */
+ if (test_landed_first(tmpdir, &c) != 0) g_fail++;
+ printf("\n");
+
/* §3 truncated read (fresh open) */
/* Re-generate the fixture since test_two_slot closed and the file is still intact. */
if (gen_fixture(tmpdir) != 0) {