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.
C avx2c99cpu-inferencedeep-learningfrom-scratchinference-enginekimi-k3linear-attentionllmllm-inferencemachine-learningmemory-efficientmixture-of-expertsmoemxfp4quantizationsimdsystems-programmingtransformerzero-dependencies
merge: macOS / Apple Silicon build support
FareedKhan-deve7378a0ee39f
diff --cc .github/workflows/ci.ymlindex 7b641f1,527805e..7fd11b7--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@@ -34,30 -34,79 +34,72 @@@ jobssudo apt-get install -y --no-install-recommends libomp-dev${{ matrix.cc }} --version- # -march=native is wrong for CI: the runner's CPU is not the user's. `portable`- # targets a documented baseline (AVX2 + FMA) instead.+ # -march=native is wrong for CI: the runner's CPU is not the user's. ARCH pins the+ # documented baseline (AVX2 + FMA) instead. It is passed to `test`, not only to+ # `all`, because the test binaries are built from the same objects and a mixed+ # -march=native/-mavx2 tree is neither of the two configurations we ship.- name: Build (portable baseline)- run: make portable CC=${{ matrix.cc }} -j"$(nproc)"-- - name: Build tests- run: make CC=${{ matrix.cc }} bin/test_ops bin/test_cache bin/test_st bin/test_cfg bin/test_tok bin/k3_model -j"$(nproc)"-- - name: Op kernels- run: ./bin/test_ops tests/fixtures/ops-- - name: Streaming expert cache- run: ./bin/test_cache tests/fixtures/cache-- - name: Safetensors reader+ run: make CC=${{ matrix.cc }} ARCH="-mavx2 -mfma" -j"$(nproc)"++ # `make test` and not the individual binaries. This is the command the README's+ # Quick start tells every user to run, so it is the one that has to be green. The+ # per-binary list this replaced omitted scale_test entirely -- the only gate with a+ # real memory cost, and the only one that exercises the real 7168-wide dimensions.+ - name: make test+ run: make CC=${{ matrix.cc }} ARCH="-mavx2 -mfma" test -j"$(nproc)"++ # The tokenizer leg above reports NOT RUN without a vocabulary, which is correct but+ # must not be mistaken for coverage. Assert the rest of the suite really ran.+ - name: Assert the suite was not silently emptyrun: |- ./bin/test_st tests/fixtures/st /tmp/idx.json \- plain.f32.2d plain.bf16.1d tricky.f16.1d packed.u8.2d scalar.f32 second.shard.f32-- - name: Config reader (both layouts + refusal cases)- run: ./bin/test_cfg fixture tests/fixtures/ref_k3.json-- # The end-to-end gate: teacher forcing, greedy decode, and incremental decode must- # all match a reference implementation exactly on the tiny oracle model.- - name: Full-model oracle gates- run: ./bin/k3_model tests/fixtures+ make CC=${{ matrix.cc }} ARCH="-mavx2 -mfma" test 2>&1 | tee /tmp/t.log+ grep -q "22 passed, 0 failed, 0 skipped" /tmp/t.log+ grep -q "SCALE TEST PASSED" /tmp/t.log+ grep -q "VERDICT: ENGINE MATCHES THE REFERENCE EXACTLY" /tmp/t.log+ grep -q "ALL WEIGHTLESS TESTS PASSED" /tmp/t.log+ # A separate job rather than a matrix leg: the toolchain step is brew rather than apt,+ # nproc does not exist on macOS, and Apple Clang ships no OpenMP runtime, so the flags+ # the Makefile has to discover are exactly the ones this job exists to check.+ build-and-test-macos:+ name: build + test (macos-14, apple clang)+ runs-on: macos-14+ steps:+ - uses: actions/checkout@v7++ - name: Install toolchain+ run: |+ brew install libomp+ cc --version+ echo "libomp prefix: $(brew --prefix libomp)"++ # Deliberately no arguments. The defect this job guards against is `make` failing+ # on a Mac, so it must exercise the platform-detection path, not bypass it.+ - name: Build (default flags, platform detection)+ run: make -j"$(getconf _NPROCESSORS_ONLN)"++ # The other arm64 branch: `portable` must drop tuning rather than pass x86 flags.+ - name: Build (portable baseline)+ run: |+ make clean+ make portable -j"$(getconf _NPROCESSORS_ONLN)"+- - name: Build tests- run: make bin/test_ops bin/test_cache bin/test_st bin/test_cfg bin/test_tok bin/k3_model -j"$(getconf _NPROCESSORS_ONLN)"++ # `make test`, for the same reason as the Linux job. A hand-written binary list here++ # would omit scale_test, and scale_test is the ONLY file in this port whose change++ # is a Darwin-only compile fix: snprintf sits behind __DARWIN_C_LEVEL >= 200112L, so++ # its _POSIX_C_SOURCE bump is load-bearing on macOS and inert on Linux. Building the++ # six binaries by name would leave that hunk revertible with the job still green.++ - name: make test++ run: make test -j"$(getconf _NPROCESSORS_ONLN)"+- - name: Op kernels- run: ./bin/test_ops tests/fixtures/ops-- - name: Streaming expert cache- run: ./bin/test_cache tests/fixtures/cache-- - name: Safetensors reader++ - name: Assert the suite was not silently empty+ run: |- ./bin/test_st tests/fixtures/st "$RUNNER_TEMP/idx.json" \- plain.f32.2d plain.bf16.1d tricky.f16.1d packed.u8.2d scalar.f32 second.shard.f32-- - name: Config reader (both layouts + refusal cases)- run: ./bin/test_cfg fixture tests/fixtures/ref_k3.json-- - name: Full-model oracle gates- run: ./bin/k3_model tests/fixtures++ make test 2>&1 | tee "$RUNNER_TEMP/t.log"++ grep -q "22 passed, 0 failed, 0 skipped" "$RUNNER_TEMP/t.log"++ grep -q "SCALE TEST PASSED" "$RUNNER_TEMP/t.log"++ grep -q "VERDICT: ENGINE MATCHES THE REFERENCE EXACTLY" "$RUNNER_TEMP/t.log"++ grep -q "ALL WEIGHTLESS TESTS PASSED" "$RUNNER_TEMP/t.log"+# Warnings are defects here. The engine does arithmetic on `const void *` weight# pointers, where a missing -Wpointer-arith silently strides by one byte.strict-warnings:diff --cc Makefileindex 8b11382,029d86a..6c155fa--- a/Makefile+++ b/Makefile@@@ -18,9 -22,48 +22,55 @@@ BUILD ?= builBIN ?= binPREFIX ?= /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.++# -march=native is x86 spelling. -mcpu= is the arm64 one, and it is what sets++# tuning as well as architecture; -march= on aarch64 selects an++# architecture level and leaves tuning alone even where it is++# accepted, and it is rejected outright by Apple Clang before++# roughly Xcode 13. `native` rather than a named 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)++ # `:=`, not `?=`. CFLAGS and LDFLAGS are recursive, so a recursive OMP_PREFIX re-runs++ # `brew --prefix` once per expansion -- about twenty forks for `make test`, each a++ # process spawn. A command-line `make OMP_PREFIX=...` still wins either way, because++ # command-line assignments override makefile assignments regardless of flavour.++ 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