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.

build: detect platform so plain `make` works on Apple Silicon

Douglas Mun1567c405703c
diff --git a/Makefile b/Makefile
index 94ab35e..029d86a 100644
--- a/Makefile
+++ b/Makefile
@@ -3,13 +3,17 @@
# make build the engine (bin/k3)
# make test run every test that needs no model weights
# make bench kernel microbenchmarks
-# make portable build without -march=native (for distribution)
+# make portable build without -march/-mcpu=native (for distribution)
# make debug -O0 -g with assertions
# make asan / ubsan sanitizer builds
# make format clang-format the tree
# make clean
#
# Nothing here requires a checkpoint. `make test` is the gate that must stay green.
+#
+# PLATFORMS. Linux/x86-64 is the reference. macOS/arm64 builds with plain `make` too,
+# but needs Homebrew's libomp for OpenMP (`brew install libomp`) because Apple Clang
+# ships no OpenMP runtime; the platform block below detects and wires it up.
# ---------------------------------------------------------------------------- config --
CC ?= cc
@@ -18,9 +22,48 @@ BUILD ?= build
BIN ?= bin
PREFIX ?= /usr/local
-# -march=native is a real win on the expert matmuls but produces a binary that will not
-# run on an older CPU. `make portable` drops it.
-ARCH ?= -march=native
+# ---------------------------------------------------------------------- platform --
+# Two things differ on macOS/arm64 and both are build failures, not warnings:
+#
+# -march=native is x86 syntax. Clang on arm64 rejects it outright; the equivalent
+# spelling is -mcpu=. `native` is used rather than a specific core so
+# the same line works on M1 through M5 and beyond.
+# -fopenmp Apple Clang ships no OpenMP runtime. Homebrew's libomp supplies it,
+# but the flag must be passed through the preprocessor
+# (-Xpreprocessor -fopenmp) and the library linked explicitly.
+#
+# UNAME_S/UNAME_M are the detection; everything below keys off them. Any of these can
+# still be overridden on the command line, which is how `make portable` and the
+# sanitizer targets work.
+UNAME_S := $(shell uname -s)
+UNAME_M := $(shell uname -m)
+
+ifeq ($(UNAME_S),Darwin)
+ ifeq ($(UNAME_M),arm64)
+ ARCH ?= -mcpu=native
+ else
+ ARCH ?= -march=native
+ endif
+ # Locate libomp without hardcoding a prefix: Homebrew is /opt/homebrew on Apple
+ # Silicon and /usr/local on Intel, and MacPorts is elsewhere again. Fall back to the
+ # Apple Silicon default if brew is not on PATH, so the error message names a real
+ # path rather than an empty one.
+ OMP_PREFIX ?= $(shell brew --prefix libomp 2>/dev/null || echo /opt/homebrew/opt/libomp)
+ OMP_CFLAGS ?= -Xpreprocessor -fopenmp -I$(OMP_PREFIX)/include
+ OMP_LDFLAGS ?= -L$(OMP_PREFIX)/lib -lomp
+ # Say what is wrong and how to fix it, rather than letting the compiler report a
+ # missing omp.h fifty lines into the build.
+ ifeq ($(wildcard $(OMP_PREFIX)/include/omp.h),)
+ $(warning libomp not found at $(OMP_PREFIX). Install it with `brew install libomp`,)
+ $(warning or point the build at another copy with `make OMP_PREFIX=/path/to/libomp`.)
+ endif
+else
+ # -march=native is a real win on the expert matmuls but produces a binary that will
+ # not run on an older CPU. `make portable` drops it.
+ ARCH ?= -march=native
+ OMP_CFLAGS ?= -fopenmp
+ OMP_LDFLAGS ?= -fopenmp
+endif
# -Wpointer-arith is not cosmetic: weight pointers are `const void *`, and arithmetic on
# a void pointer is a silent GNU extension that strides by ONE BYTE. Without this flag
@@ -30,8 +73,8 @@ ARCH ?= -march=native
# disabling automatic FMA contraction. The test-suite compares against a reference to a
# fixed tolerance; letting the compiler fuse changes results by more than that.
WARN := -Wall -Wextra -Wpointer-arith -Wshadow -Wvla -Wno-unused-parameter
-CFLAGS ?= -O3 -std=gnu99 $(WARN) $(ARCH) -fopenmp -ffp-contract=off
-LDFLAGS ?= -lm -fopenmp
+CFLAGS ?= -O3 -std=gnu99 $(WARN) $(ARCH) $(OMP_CFLAGS) -ffp-contract=off
+LDFLAGS ?= -lm $(OMP_LDFLAGS)
# Flat include search across the module dirs: sources use "k3.h", "k3_cache.h" etc
# rather than path-qualified includes, which keeps them relocatable.
@@ -153,14 +196,24 @@ cfg: $(BIN)/test_cfg
bench: $(BIN)/bench_kernels
./$(BIN)/bench_kernels
-## portable: no -march=native, runs on any x86-64
+## portable: drop the -march/-mcpu=native tuning, for a distributable binary
+# On x86-64 that means a generic AVX2 + FMA baseline. On arm64 there is no equivalent
+# sub-baseline worth naming -- the ISA is the baseline -- so tuning is simply omitted.
portable:
+ifeq ($(UNAME_S)/$(UNAME_M),Darwin/arm64)
+ $(MAKE) ARCH= all
+else
$(MAKE) ARCH="-mavx2 -mfma" all
+endif
## debug: -O0 -g, assertions on
debug:
- $(MAKE) CFLAGS="-O0 -g3 -std=gnu99 $(WARN) -fopenmp -ffp-contract=off" all
+ $(MAKE) CFLAGS="-O0 -g3 -std=gnu99 $(WARN) $(OMP_CFLAGS) -ffp-contract=off" all
+# The sanitizer builds drop OpenMP deliberately: ASan's interceptors and the OpenMP
+# runtime's thread pool produce false positives together, and a serial build is the
+# point of a sanitizer run. OMP_CFLAGS is still omitted rather than replaced, so the
+# #pragma omp lines compile to nothing on every platform alike.
asan:
$(MAKE) CFLAGS="-O1 -g -std=gnu99 $(WARN) -fsanitize=address,undefined -fno-omit-frame-pointer" \
LDFLAGS="-lm -fsanitize=address,undefined" ARCH= all