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.

port: build and pass the test suite on Apple Silicon

Douglas Mund4e3f119b001
diff --git a/src/cli/k3_run.c b/src/cli/k3_run.c
index 4e3afb7..0f1ed38 100644
--- a/src/cli/k3_run.c
+++ b/src/cli/k3_run.c
@@ -38,6 +38,11 @@
* keep correct and the copy is the one that goes stale.
*/
#define _POSIX_C_SOURCE 200809L
+/* _POSIX_C_SOURCE alone hides the BSD rusage fields, ru_maxrss among them, from
+ * <sys/resource.h> on Darwin. peak_rss_bytes() below needs it. */
+#if defined(__APPLE__) && !defined(_DARWIN_C_SOURCE)
+#define _DARWIN_C_SOURCE
+#endif
#include <math.h>
#include <stdio.h>
@@ -207,7 +212,9 @@ static void k3_preset_list(FILE *f)
"Run scripts/k3-doctor.sh to see which one this machine fits.\n");
}
-/* PEAK resident set, in bytes. ru_maxrss is kilobytes on Linux.
+/* PEAK resident set, in bytes. ru_maxrss is kilobytes on Linux and BYTES on Darwin, so
+ * the scale factor differs by platform; applying the Linux one on macOS would overstate
+ * the peak by 1024x.
*
* This is the authoritative memory figure. The banner printed before allocation is a
* PLAN and understates: it omits the safetensors index (~78 MB at full scale), reports
@@ -217,7 +224,11 @@ static double peak_rss_bytes(void)
{
struct rusage ru;
if (getrusage(RUSAGE_SELF, &ru) != 0) return 0.0;
- return (double)ru.ru_maxrss * 1024.0;
+#if defined(__APPLE__)
+ return (double)ru.ru_maxrss; /* already bytes */
+#else
+ return (double)ru.ru_maxrss * 1024.0; /* kilobytes */
+#endif
}
/* MemAvailable, which is what the kernel thinks can actually be handed out, not
diff --git a/src/io/k3_portable_io.h b/src/io/k3_portable_io.h
new file mode 100644
index 0000000..a9da467
--- /dev/null
+++ b/src/io/k3_portable_io.h
@@ -0,0 +1,61 @@
+/* k3_portable_io.h - shims for the Linux-only I/O calls the readers use.
+ *
+ * The engine asks for two things Linux gives it and Darwin does not spell the same way:
+ *
+ * O_DIRECT bypass the page cache on the trunk and expert reads. Darwin's
+ * equivalent is not an open() flag but fcntl(F_NOCACHE) after the
+ * fact, so O_DIRECT is defined to 0 here (open() is unaffected) and
+ * k3_set_direct() applies the real thing to the returned descriptor.
+ *
+ * posix_fadvise a page-cache prefetch hint with no Darwin equivalent. Callers
+ * already treat it as advisory -- the one call site returns early on
+ * the direct path because the hint has nothing to populate there -- so
+ * the shim is a no-op that keeps the buffered path compiling.
+ *
+ * Both call sites fall back to buffered reads when the direct path is unavailable, so
+ * neither shim changes what the engine computes, only how fast it reads.
+ */
+#ifndef K3_PORTABLE_IO_H
+#define K3_PORTABLE_IO_H
+
+/* The readers define _POSIX_C_SOURCE, which hides Darwin's non-standard fcntl commands
+ * (F_NOCACHE among them) from <fcntl.h>. _DARWIN_C_SOURCE puts them back. It must be
+ * set before the first libc header is pulled in, so this header is included first. */
+#if defined(__APPLE__) && !defined(_DARWIN_C_SOURCE)
+#define _DARWIN_C_SOURCE
+#endif
+
+#include <fcntl.h>
+
+#if defined(__APPLE__)
+
+/* Not an open() flag on Darwin: defining it to 0 leaves open() semantics untouched. */
+#ifndef O_DIRECT
+#define O_DIRECT 0
+#endif
+
+#ifndef POSIX_FADV_WILLNEED
+#define POSIX_FADV_WILLNEED 3
+#endif
+
+static inline int posix_fadvise(int fd, off_t off, off_t len, int advice)
+{
+ (void)fd; (void)off; (void)len; (void)advice;
+ return 0; /* advisory only; the buffered path is correct without it */
+}
+
+/* Darwin's O_DIRECT equivalent, applied after open(). Failure is not fatal: the caller
+ * keeps the descriptor and reads through the page cache instead. */
+static inline int k3_set_direct(int fd)
+{
+ if (fd < 0) return -1;
+ return fcntl(fd, F_NOCACHE, 1);
+}
+
+#else /* Linux and friends: O_DIRECT on open() already did it */
+
+static inline int k3_set_direct(int fd) { (void)fd; return 0; }
+
+#endif
+
+#endif /* K3_PORTABLE_IO_H */
diff --git a/src/io/k3_st.c b/src/io/k3_st.c
index a674401..2be9165 100644
--- a/src/io/k3_st.c
+++ b/src/io/k3_st.c
@@ -17,6 +17,8 @@
#define _POSIX_C_SOURCE 200809L
#define _FILE_OFFSET_BITS 64
+#include "k3_portable_io.h" /* first: sets _DARWIN_C_SOURCE before any libc header */
+
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
@@ -341,7 +343,10 @@ static int scan_shard(K3St *s, Build *b, int shard, const char *path)
/* A second descriptor on the same file, for streamed expert reads that must not go
* through the page cache. Optional: if the filesystem refuses O_DIRECT the reader
* falls back to fd[]. */
- if (s->dfd) s->dfd[shard] = open(path, O_RDONLY | O_DIRECT);
+ if (s->dfd) {
+ s->dfd[shard] = open(path, O_RDONLY | O_DIRECT);
+ k3_set_direct(s->dfd[shard]); /* no-op off Darwin; advisory, failure is fine */
+ }
return ntensor;
bad:
free(json);
diff --git a/src/io/k3_trunk.c b/src/io/k3_trunk.c
index 3173639..d4f37f5 100644
--- a/src/io/k3_trunk.c
+++ b/src/io/k3_trunk.c
@@ -3,6 +3,8 @@
#define _POSIX_C_SOURCE 200809L
#define _FILE_OFFSET_BITS 64
+#include "k3_portable_io.h" /* first: sets _DARWIN_C_SOURCE before any libc header */
+
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
@@ -137,6 +139,8 @@ int k3_trunk_open(K3Trunk *tr, const char *dir, const K3Cfg *c, int64_t budget_b
* O_DIRECT, fall back rather than fail: correctness does not depend on it. */
tr->direct = 1;
tr->fd = open(p, O_RDONLY | O_DIRECT);
+ if (tr->fd >= 0 && k3_set_direct(tr->fd) != 0)
+ tr->direct = 0; /* Darwin refused F_NOCACHE: reads stay buffered but correct */
if (tr->fd < 0) {
tr->direct = 0;
tr->fd = open(p, O_RDONLY);
diff --git a/tests/unit/scale_test.c b/tests/unit/scale_test.c
index 49c2b01..d1e1ff2 100644
--- a/tests/unit/scale_test.c
+++ b/tests/unit/scale_test.c
@@ -16,8 +16,11 @@
* This does NOT load real weights. It allocates at real shapes with synthetic values
* to prove the plumbing survives the dimensions.
*/
-/* -std=c99 hides the POSIX clock API; request it before any include. */
-#define _POSIX_C_SOURCE 199309L
+/* -std=c99 hides the POSIX clock API; request it before any include.
+ * 200809L rather than 199309L: the older level predates C99 and, on Darwin's headers,
+ * gates out snprintf along with it. Both levels declare clock_gettime, which is the
+ * only reason this macro is here. */
+#define _POSIX_C_SOURCE 200809L
#include <math.h>
#include <stdio.h>