Skip to content

tools: FP4 expert matmul microbench (SIMD vs scalar arm, ref #1696) - #1727

Closed
mfethe1 wants to merge 1 commit into
JustVugg:devfrom
mfethe1:bench/dsv4-fp4-kernel-microbench
Closed

mfethe1 wants to merge 1 commit into
JustVugg:devfrom
mfethe1:bench/dsv4-fp4-kernel-microbench

Conversation

@mfethe1

@mfethe1 mfethe1 commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

What

Adds c/tools/bench_fp4_matmul.c + bench_fp4_matmul.sh: a self-contained microbench for coli_fp4_matmul_batch_rows16_order that builds the COLI_V4_UNIT_NATIVE_QUANT unit twice β€” default flags (SIMD arm) vs EXTRA_CFLAGS=-mno-avx2 (scalar arm) β€” verifies the two arms agree bit-exactly on seeded data, then times both at the real V4-Flash expert shapes.

Why (#1696)

To size what a NEON arm is worth on arm64, I measured the A/B on x86-64 (Zen 2 Ryzen 7 3700X, gcc 15.2, repo flags). Numbers in the issue comment:

shape (SΓ—IΓ—O) AVX2 arm scalar arm ratio
128Γ—4096Γ—2048 (w1/w3) ~141 GFLOP/s ~1.9 GFLOP/s 75Γ—
128Γ—2048Γ—4096 (w2) ~149 GFLOP/s ~3.8 GFLOP/s 41Γ—
32Γ—4096Γ—2048 ~156 GFLOP/s ~6.8 GFLOP/s 23Γ—

Outputs bit-exact identical between arms (0/262144 floats differ), consistent with the design note at deepseek_v4.c:~16450 (per-row accumulation order preserved across arms).

The arm64 story: this kernel gates on __AVX2__ only, so Apple Silicon runs the scalar arm β€” which on an M-series (gcc 15.2, -march=armv8.2-a+dotprod) measures ~10 GFLOP/s vs ~2 on Zen 2 x86-64. The scalar arm is ~1.5–2.5% of SIMD throughput; a NEON arm does not need to match AVX2's absolute numbers to be a large win. The AVX2 arm's structure (pshufb nibble decode β†’ 8Γ—8 transpose β†’ column-batched multiply/add) maps closely onto NEON (vqtbl1q lookup decode, vtrn/vzip transpose, vfma/mul+add lanes).

Harness details

  • Mirrors the repo build: unit compiled with repo-equivalent flags (-O2 -fopenmp -march=…, COLI_V4_UNIT_NATIVE_QUANT), GPU-tier entry points stubbed (referenced but never taken on the CPU-only path).
  • Deterministic seeded data (xorshift); e8m0 scale bytes constrained to 120–127 so products can never hit inf/NaN β€” full-range random scale bytes poison bit-comparison (NaN β‰  NaN).
  • Bit-exact A/B: first build writes a float32 reference file, second build compares (MISMATCH β†’ nonzero exit).
  • Guards: rejects S > 128 (kernel's sums[128] batch cap; the caller chunks prefill to ≀128 rows) and non-multiple-of-32 I.
  • Portable: POSIX + Windows (access/_access), arm64 (builds scalar-baseline mode until a NEON arm exists, per [Performance]: DeepSeek V4 on Apple Silicon (CPU path): prefill runs at decode speed, 1.3–1.75 prompt tok/s β€” 577-token prompt = 6 min TTFTΒ #1696). Prefers real gcc over Apple clang (clang lacks -fopenmp).
  • Verified: 0-warning build (-Wall -Wextra), run end-to-end on arm64 macOS and x86-64 Windows at SΓ—IΓ—O = 128Γ—4096Γ—2048.

How to run

cd c && bash tools/bench_fp4_matmul.sh            # defaults: 128 4096 2048 7
bash tools/bench_fp4_matmul.sh 128 2048 4096 7    # w2 shape

No model files, torch, or GPU needed β€” CPU-only, ~30 s.

bench_fp4_matmul builds the COLI_V4_UNIT_NATIVE_QUANT unit twice β€” default
flags (SIMD arm active) and with EXTRA_CFLAGS=-mno-avx2 (scalar arm) β€”
verifies the two arms agree bit-exactly on seeded data, and times both at
the real DeepSeek-V4-Flash expert shapes (w1/w3 [4096β†’2048], w2 [2048β†’4096]).

Motivation (JustVugg#1696): on x86-64 (Zen 2, gcc 15, -march=x86-64-v3 vs -mno-avx2)
the AVX2 batch arm measures 40–75Γ— faster than the scalar arm at those
shapes (~141–159 vs ~1.9–3.8 GFLOP/s), with bit-exact identical outputs.
This harness reproduces that A/B anywhere and gives arm64 a baseline for
the (currently missing) NEON arm.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@JustVugg

Copy link
Copy Markdown
Owner

The bench landed with #1730, which carries the same tools/bench_fp4_matmul.{c,sh} plus the NEON comparison on arm64 that now runs in the ARM CI job. Closing as included; thank you, it is what made the NEON arm checkable.

@JustVugg JustVugg closed this Sep 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants