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

tools: add FP4 expert matmul microbench (SIMD vs scalar arm)

mfethe1fa866875edad
diff --git a/c/tools/bench_fp4_matmul.c b/c/tools/bench_fp4_matmul.c
new file mode 100644
index 00000000..69bfba77
--- /dev/null
+++ b/c/tools/bench_fp4_matmul.c
@@ -0,0 +1,141 @@
+/* bench_fp4_matmul.c β€” microbench for coli_fp4_matmul_batch_rows16_order.
+ *
+ * Measures the cost of the SIMD arm vs the scalar #else arm of the FP4 expert
+ * matmul used in DeepSeek V4 CPU prefill (see Makefile.deepseek-v4: the arm is
+ * selected by __AVX2__ / __ARM_NEON on the build). Build the same unit twice
+ * β€” default flags and with EXTRA_CFLAGS=-mno-avx2 (x86) β€” then compare.
+ *
+ * Usage: bench_fp4_matmul [S] [I] [O] [iters] [ref_file]
+ * S rows (token batch); the kernel contract caps S at 128
+ * I/O input/output dims. Real V4-Flash experts: (S,4096,2048) w1/w3,
+ * (S,2048,4096) w2. Tiny fixture: (S,128,128).
+ * ref optional path: first run writes a float32 reference output,
+ * second run (other arm) compares bit-exactly against it.
+ *
+ * Data notes:
+ * - seeded xorshift, deterministic across platforms/builds
+ * - e8m0 scale bytes limited to 120..127 (2^-8 .. 2^-1) so products can
+ * never overflow to inf/NaN β€” full-range random scale bytes poison any
+ * bit-comparison with NaN (NaN != NaN).
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <time.h>
+
+#if defined(_WIN32)
+# include <io.h>
+# define BK_ACCESS(p, m) _access(p, m)
+#else
+# include <unistd.h>
+# define BK_ACCESS(p, m) access(p, m)
+#endif
+
+/* Mirrors the kernel's own arm selection (deepseek_v4.c gates on __AVX2__
+ * only today): on arm64 the SIMD arm does not exist yet, so the build is the
+ * scalar one even though NEON is available. */
+#if defined(__AVX2__)
+# define BK_ARM_NAME "avx2"
+#else
+# define BK_ARM_NAME "scalar"
+#endif
+
+/* Link stubs: GPU-tier entry points referenced (never taken) by the CPU-only
+ * build of this unit. Signatures from deepseek_v4_internal.h. */
+int coli_v4_gpu_matvec_grouped(const void *w, float *output,
+ const float *input, int groups) { (void)w; (void)output; (void)input; (void)groups; return 1; }
+int coli_v4_gpu_fp8_matvec(const void *w, float *output,
+ const float *input) { (void)w; (void)output; (void)input; return 1; }
+int coli_v4_gpu_fp8_matmul_batch(const void *w, float *outputs,
+ const float *inputs, int batch) { (void)w; (void)outputs; (void)inputs; (void)batch; return 1; }
+
+void coli_fp4_matmul_batch_rows16_order(float *y, const uint8_t *q4,
+ const uint8_t *e8s, const float *x,
+ int S, int I, int O);
+
+static double now_s(void) {
+ struct timespec ts;
+#if defined(__APPLE__)
+ clock_gettime(CLOCK_MONOTONIC, &ts); /* works since macOS 10.12 */
+#else
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+#endif
+ return ts.tv_sec + ts.tv_nsec * 1e-9;
+}
+
+static uint32_t rng_state = 0x1234abcd;
+static uint32_t rng(void) {
+ rng_state ^= rng_state << 13; rng_state ^= rng_state >> 17;
+ rng_state ^= rng_state << 5; return rng_state;
+}
+
+int main(int argc, char **argv) {
+ int S = argc > 1 ? atoi(argv[1]) : 128;
+ int I = argc > 2 ? atoi(argv[2]) : 4096;
+ int O = argc > 3 ? atoi(argv[3]) : 2048;
+ int iters = argc > 4 ? atoi(argv[4]) : 7;
+ const char *ref_path = argc > 5 ? argv[5] : NULL;
+
+ if (S < 1 || I % 2 || I % 32 || O < 1) {
+ fprintf(stderr, "bad shape: need S>=1, I%%2==0, I%%32==0 (FP4 group "
+ "packing), O>=1\n");
+ return 2;
+ }
+ if (S > 128) {
+ fprintf(stderr, "S=%d exceeds the kernel's batch cap (sums[128]); the "
+ "sole caller chunks prefill to <=128 rows\n", S);
+ return 2;
+ }
+
+ size_t rb = (size_t)I / 2, ng = (size_t)I / 32;
+ uint8_t *q4 = malloc(rb * O);
+ uint8_t *e8s = malloc(ng * O);
+ float *x = malloc((size_t)S * I * sizeof(float));
+ float *y = malloc((size_t)S * O * sizeof(float));
+ if (!q4 || !e8s || !x || !y) { fprintf(stderr, "oom\n"); return 1; }
+ for (size_t i = 0; i < rb * O; i++) q4[i] = (uint8_t)rng();
+ for (size_t i = 0; i < ng * O; i++) e8s[i] = (uint8_t)(120 + (rng() & 7));
+ for (size_t i = 0; i < (size_t)S * I; i++) x[i] = (float)((double)(rng() >> 8) / 8388608.0 - 1.0);
+
+ memset(y, 0, (size_t)S * O * sizeof(float));
+ coli_fp4_matmul_batch_rows16_order(y, q4, e8s, x, S, I, O);
+
+ if (ref_path) {
+ FILE *f = fopen(ref_path, "rb");
+ if (f) {
+ size_t n = (size_t)S * O;
+ float *ref = malloc(n * sizeof(float));
+ if (fread(ref, sizeof(float), n, f) == n) {
+ size_t bad = 0;
+ for (size_t i = 0; i < n; i++) if (ref[i] != y[i]) bad++;
+ printf("BITEXACT vs %s : %s (%zu/%zu differ)\n", ref_path,
+ bad ? "MISMATCH" : "IDENTICAL", bad, n);
+ if (bad) return 3;
+ } else printf("BITEXACT: ref file wrong size\n");
+ fclose(f); free(ref);
+ } else printf("BITEXACT: no ref (this run writes it)\n");
+ }
+
+ /* timing: discard first timed call (page faults), median of the rest */
+ double best = 1e9, times[64]; int nt = 0;
+ if (iters > 63) iters = 63;
+ for (int it = 0; it < iters + 1; it++) {
+ double t0 = now_s();
+ coli_fp4_matmul_batch_rows16_order(y, q4, e8s, x, S, I, O);
+ double dt = now_s() - t0;
+ if (it > 0) { times[nt++] = dt; if (dt < best) best = dt; }
+ }
+ for (int i = 0; i < nt; i++) for (int j = i + 1; j < nt; j++)
+ if (times[j] < times[i]) { double t = times[i]; times[i] = times[j]; times[j] = t; }
+ double med = times[nt / 2];
+ double flops = 2.0 * S * I * O;
+ printf("KERNEL S=%d I=%d O=%d arm=%s iters=%d med=%.4fs best=%.4fs "
+ "gflops_med=%.2f\n",
+ S, I, O, BK_ARM_NAME, iters, med, best, flops / med / 1e9);
+ if (ref_path && BK_ACCESS(ref_path, 0) != 0) {
+ FILE *f = fopen(ref_path, "wb");
+ if (f) { fwrite(y, sizeof(float), (size_t)S * O, f); fclose(f); }
+ }
+ return 0;
+}
diff --git a/c/tools/bench_fp4_matmul.sh b/c/tools/bench_fp4_matmul.sh
new file mode 100755
index 00000000..37df8b79
--- /dev/null
+++ b/c/tools/bench_fp4_matmul.sh
@@ -0,0 +1,56 @@
+#!/usr/bin/env bash
+# bench_fp4_matmul.sh β€” build & A/B the FP4 expert matmul's SIMD vs scalar arm.
+#
+# Builds c/deepseek_v4.c twice (default flags = SIMD arm active; then with
+# EXTRA_CFLAGS=-mno-avx2 on x86 to force the scalar #else arm), links the
+# bench_fp4_matmul.c harness against each, verifies the two arms agree
+# bit-exactly, and prints timings at the real DeepSeek-V4-Flash expert shapes.
+#
+# Usage (from c/): bash tools/bench_fp4_matmul.sh [S] [I] [O] [iters]
+# Needs: gcc or clang, make. CPU-only; no model files required.
+set -euo pipefail
+cd "$(dirname "$0")/.."
+
+S="${1:-128}"; I="${2:-4096}"; O="${3:-2048}"; IT="${4:-7}"
+
+# Mirror the repo's own unit flags (Makefile.deepseek-v4 / .units).
+ARCH=armv8.2-a+dotprod
+SCALAR=""
+AB=1
+case "$(uname -m)" in
+ x86_64*|amd64) ARCH=x86-64-v3 ; SCALAR="-mno-avx2" ;;
+ arm64|aarch64) AB=0 ;; # no SIMD arm in this kernel on NEON yet (#1696):
+ # arm64 builds only the scalar arm; this is the
+ # baseline a future NEON arm must beat
+ *) echo "unsupported arch $(uname -m)"; exit 1 ;;
+esac
+
+UNIT="-DCOLI_V4_UNIT_NATIVE_QUANT"
+COMMON="-O2 -fopenmp -I."
+# Apple clang lacks -fopenmp; prefer a real gcc when present.
+CC=gcc
+$CC --version 2>/dev/null | grep -qi "gcc" || CC=gcc-15
+command -v $CC >/dev/null || { echo "no gcc with -fopenmp found"; exit 1; }
+echo "using $($CC --version | head -1)"
+
+echo "== building SIMD-arm unit =="
+$CC $COMMON -march=$ARCH $UNIT -c deepseek_v4.c -o /tmp/bk_unit_simd.o
+$CC $COMMON -march=$ARCH -c tools/bench_fp4_matmul.c -o /tmp/bk_main_simd.o
+$CC /tmp/bk_unit_simd.o /tmp/bk_main_simd.o -o /tmp/bk_simd -lm -fopenmp
+
+REF=/tmp/bk_ref.f32
+rm -f "$REF"
+if [ "$AB" = 1 ]; then
+ echo "== building scalar-arm unit (-mno-avx2) =="
+ $CC $COMMON -march=$ARCH $SCALAR $UNIT -c deepseek_v4.c -o /tmp/bk_unit_sca.o
+ $CC $COMMON -march=$ARCH $SCALAR -c tools/bench_fp4_matmul.c -o /tmp/bk_main_sca.o
+ $CC /tmp/bk_unit_sca.o /tmp/bk_main_sca.o -o /tmp/bk_sca -lm -fopenmp
+ echo "== SIMD arm (writes reference) =="
+ /tmp/bk_simd "$S" "$I" "$O" "$IT" "$REF"
+ echo "== scalar arm (compares bit-exactly + times) =="
+ /tmp/bk_sca "$S" "$I" "$O" "$IT" "$REF"
+ echo "Done. Repeat /tmp/bk_simd ... to re-verify bit-exactness."
+else
+ echo "== no SIMD arm on this arch/kernel yet (#1696); scalar baseline only =="
+ /tmp/bk_simd "$S" "$I" "$O" "$IT"
+fi