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

olmoe: measure available RAM on macOS and Windows too

JustVugg7ed608443154
diff --git a/c/Makefile b/c/Makefile
index ba136a48..881d9317 100644
--- a/c/Makefile
+++ b/c/Makefile
@@ -1236,6 +1236,9 @@ tests/test_st_mirror$(EXE): tests/test_st_mirror.c st.h json.h compat.h
tests/test_st_overlay$(EXE): tests/test_st_overlay.c st.h json.h compat.h
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
+tests/test_compat_mem$(EXE): tests/test_compat_mem.c compat.h
+ $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
+
tests/test_st_missing$(EXE): tests/test_st_missing.c st.h json.h compat.h
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
diff --git a/c/olmoe.c b/c/olmoe.c
index 15083f78..84aaac4b 100644
--- a/c/olmoe.c
+++ b/c/olmoe.c
@@ -211,20 +211,32 @@ static double rss_gb(void) { struct rusage r; getrusage(RUSAGE_SELF, &r); return
* budget automatico pari a cio' che il processo gia' tiene: la cache risulta
* minima invece che sbagliata, e --ram (o --cap) resta la via esplicita. */
static double mem_available_gb(void) {
- double avail = 0.0;
-#ifdef __linux__
- FILE *mi = fopen("/proc/meminfo", "r");
- if (mi) {
- char ln[256]; double v = 0;
- while (fgets(ln, sizeof(ln), mi))
- if (sscanf(ln, "MemAvailable: %lf", &v) == 1) { avail = v / 1e6; break; }
- fclose(mi);
- }
-#elif defined(_SC_AVPHYS_PAGES) && defined(_SC_PAGESIZE)
- long pages = sysconf(_SC_AVPHYS_PAGES), page = sysconf(_SC_PAGESIZE);
- if (pages > 0 && page > 0) avail = (double)pages * (double)page / 1e9;
+ /* compat.h's probe knows Linux (MemAvailable), macOS (host_statistics64)
+ * and Windows (GlobalMemoryStatusEx). The Linux-only version that lived
+ * here returned 0 on the other two, and 0 sized the expert cache to one
+ * slot per layer: 2.5x slower without --ram, on every Windows and macOS
+ * benchmark taken since (#1500). */
+ double avail = compat_mem_available_gb();
+ if (avail > 0.0) return avail;
+ /* Not measurable here: say so once and fall back to half the physical RAM
+ * where that is known, else to a small fixed budget, rather than to a
+ * cache that streams every expert from disk on every token. */
+ static int noted = 0;
+ double total = 0.0;
+#ifdef _WIN32
+ double a2 = 0.0; compat_meminfo(&total, &a2);
+#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
+ long pages = sysconf(_SC_PHYS_PAGES), page = sysconf(_SC_PAGESIZE);
+ if (pages > 0 && page > 0) total = (double)pages * (double)page / 1e9;
#endif
- return avail;
+ double fallback = total > 0.0 ? total * 0.5 : 8.0;
+ if (!noted) {
+ noted = 1;
+ fprintf(stderr, "[olmoe] could not measure available RAM on this platform; assuming %.1f GB "
+ "(%s). Pass --ram <GB> to set the budget explicitly.\n",
+ fallback, total > 0.0 ? "half the physical RAM" : "a fixed default");
+ }
+ return fallback;
}
static float *falloc(int64_t n) { float *p = malloc(n*sizeof(float)); if(!p){fprintf(stderr,"OOM %ld\n",(long)n);exit(1);} return p; }
@@ -1523,6 +1535,12 @@ static void serve_hwinfo(Model *m) {
if (sscanf(ln, "MemTotal: %lf", &v) == 1) rt = v/1e6;
if (sscanf(ln, "MemAvailable: %lf", &v) == 1) ra = v/1e6;
} fclose(mi); }
+ if (ra <= 0.0) ra = compat_mem_available_gb(); /* macOS, Windows: no /proc (#1500) */
+#ifdef _WIN32
+ if (rt <= 0.0) { double a2 = 0.0; compat_meminfo(&rt, &a2); }
+#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
+ if (rt <= 0.0) { long pg = sysconf(_SC_PHYS_PAGES), ps = sysconf(_SC_PAGESIZE); if (pg > 0 && ps > 0) rt = (double)pg * ps / 1e9; }
+#endif
printf("HWINFO %d %.1f %.1f 0 0.0 %s|\n", cores, rt, ra, cpu[0] ? cpu : "unknown");
fflush(stdout);
}
diff --git a/c/tests/test_compat_mem.c b/c/tests/test_compat_mem.c
new file mode 100644
index 00000000..d20f53cf
--- /dev/null
+++ b/c/tests/test_compat_mem.c
@@ -0,0 +1,14 @@
+/* #1500: compat_mem_available_gb() must measure something on every platform
+ * the release ships for. olmoe sized its expert cache from a Linux-only probe
+ * that returned 0 on Windows and macOS, and 0 meant one slot per layer. This
+ * runs on all three CI legs and pins the shared probe itself. */
+#include <stdio.h>
+#include "../compat.h"
+int main(void) {
+ double avail = compat_mem_available_gb();
+ printf("available RAM: %.2f GB\n", avail);
+ if (!(avail > 0.0)) { fprintf(stderr, "compat_mem_available_gb() returned %.3f on this platform\n", avail); return 1; }
+ if (avail > 100000.0) { fprintf(stderr, "implausible available RAM %.1f GB\n", avail); return 1; }
+ puts("compat mem probe: ok");
+ return 0;
+}