interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / mk / gemm-ex.mk

150 lines · 11638 bytes Raw Blame History
1# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2# The general matrix multiply omega_gpu_gemm_ex (src/omega_gpu_gemm_ex.h): any M, N, K >= 1, op N/T, leading dimensions,
3# alpha/beta, strided batches, f32/bf16/f16/e4m3/e5m2 inputs, fp8 scales, over the C3 kernels of src/omega_gpu_gemm.c.
4# The plan, routes, k parts, operand conversion, k-part sums, host recompute, epilogue and fp8 dequantization are seeded
5# Omega (om/gemmex/*.om -> src/cudapp_om_gemm_ex_*.c by om/units.sh, CUDApp#144); src/omega_gpu_gemm_ex.c is their memory
6# shell. MIGRATION-PENDING C, kept as the differential oracle: CUDAPP_MIGRATION_PENDING_GEMM_EX=1 builds the old C path
7# instead (default 0, the Omega path); CUDAPP_MIGRATION_PENDING_GEMM_EX_DIFF=1 runs both on every call and aborts on any
8# difference in return code, info or output bytes.
9# make test-gpu-gemm-ex / test-gpu-gemm-ex-sim the host and simulator tiers on the library (the Omega path)
10# make test-gpu-gemm-ex-c the same two tiers on the C oracle
11# make test-gpu-gemm-ex-diff both tiers with every call run both ways (DIFF), plus its red check:
12# one flipped bit of the C copy must abort
13# make test-gemm-ex-unfused no fused multiply-add in the C oracle, the shell or the units, plus its
14# red check: the oracle built without -ffp-contract=off has one (AArch64)
15# make test-gemm-ex-om-num / test-gemm-ex-om-mutants (not in `make test`: need OMEGA_MV and OMEGA_BRIDGE_DIR as in
16# mk/om-encoder.mk; without them exit 3, NOT_RUN, never PASS) the exhaustive number-format test of om/gemmex/num.om,
17# and mutant .om units, each compiled by the pinned omc, that must turn the host tier red.
18# Picked up by the `-include mk/*.mk` line of the Makefile, after CFLAGS and before HOST_TESTS.
19CUDAPP_MIGRATION_PENDING_GEMM_EX ?= 0
20CFLAGS += -DCUDAPP_MIGRATION_PENDING_GEMM_EX=$(CUDAPP_MIGRATION_PENDING_GEMM_EX)
21# Pure gather/packing policy C is an independent, default-OFF oracle; this does not enable GX_C.
22CUDAPP_MIGRATION_PENDING_C_GEMM_EX_HOST_POLICY ?= 0
23$(OUT_DIR)/omega_gpu_gemm_ex.o: CFLAGS += -DCUDAPP_MIGRATION_PENDING_C_GEMM_EX_HOST_POLICY=$(CUDAPP_MIGRATION_PENDING_C_GEMM_EX_HOST_POLICY)
24.PHONY: test-gemm-ex-host-policy
25test-gemm-ex-host-policy:
26 python3 tests/gemm_ex_policy_check.py
27# The C oracle sums in double; a fused multiply-add rounds once where the C rounds twice, so contraction stays off.
28$(OUT_DIR)/omega_gpu_gemm_ex.o: CFLAGS += -ffp-contract=off
29# The checked Omega-to-C units do every operation through small generic helpers (o_op, o_get, o_norm, ...); at -O2's
30# default inline limits they stay calls, at raised ones they fold to the operation itself (the growth limits matter for
31# the large unit entry functions, whose o_op calls stay calls under the per-call limit alone). With them, the host path
32# measured by `make bench-gemm-ex` (2026-11-06, core 14, 623 cases, per-case medians of 6 runs) sums 2028 ms against
33# 902 ms for the C oracle (2.25x), same output bytes (test-gpu-gemm-ex-diff). Only
34# when $(CC) takes the flags (gcc does; clang ignores some with a warning, which -Werror would make an error).
35GEMM_EX_INLINE_FLAGS := -finline-limit=1000 --param large-function-growth=1000 --param inline-unit-growth=1000 \
36 --param large-function-insns=100000
37GEMM_EX_INLINE := $(shell $(CC) -Werror $(GEMM_EX_INLINE_FLAGS) -x c -c -o /dev/null /dev/null 2>/dev/null && \
38 echo $(GEMM_EX_INLINE_FLAGS))
39$(patsubst src/%.c,$(OUT_DIR)/%.o,$(wildcard src/cudapp_om_gemm_ex_*.c)): CFLAGS += $(GEMM_EX_INLINE)
40# The op 7 scan of the row unit: inlined through to the unit entry, the C compiler sees the w window is 8192 words (the
41# entry refuses any other length) and that the scan stops at word 8192, and drops the per-word bounds checks.
42GEMM_EX_ROW_FLAGS := --param max-inline-insns-auto=1000 --param max-inline-insns-single=4000
43GEMM_EX_ROW_INLINE := $(if $(GEMM_EX_INLINE),$(shell $(CC) -Werror $(GEMM_EX_ROW_FLAGS) -x c -c -o /dev/null /dev/null \
44 2>/dev/null && echo $(GEMM_EX_ROW_FLAGS)))
45# The epi unit: under the same limits the byte-view load (gx_get) and the conversion helpers fold into the per-element
46# store helper; left calls, s111 measured 39.6 ms against 29.6 ms folded (core 13, 2026-11-06).
47$(OUT_DIR)/cudapp_om_gemm_ex_row.o $(OUT_DIR)/cudapp_om_gemm_ex_epi.o: CFLAGS += $(GEMM_EX_ROW_INLINE)
48
49GEMM_EX_DIR = $(OUT_DIR)/gemm-ex
50GEMM_EX_TEST_DEPS = tests/gpu_gemm_ex_test.c tests/bw_warp_sim_tc.h tests/bw_hmma_model.h tests/bw_qmma_model.h \
51 src/omega_gpu_gemm_ex.h src/omega_gpu_gemm.h
52GEMM_EX_CFLAGS = $(filter-out -DCUDAPP_MIGRATION_PENDING_GEMM_EX=%,$(CFLAGS)) -ffp-contract=off -Itests
53# a test binary over src/omega_gpu_gemm_ex.c built with the given defines; the archive supplies everything else
54GEMM_EX_LINK = $(CC) $(GEMM_EX_CFLAGS) $(2) -o $(1) tests/gpu_gemm_ex_test.c src/omega_gpu_gemm_ex.c $(3) \
55 $(OUT_DIR)/libomega_gpu.a -lpthread -lm
56
57GPU_GEMM_EX_TEST = $(OUT_DIR)/gpu_gemm_ex_test
58$(GPU_GEMM_EX_TEST): $(GEMM_EX_TEST_DEPS) $(OUT_DIR)/libomega_gpu.a
59 $(CC) $(CFLAGS) -ffp-contract=off -Itests -o $@ tests/gpu_gemm_ex_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm
60$(GEMM_EX_DIR)/test_c: $(GEMM_EX_TEST_DEPS) src/omega_gpu_gemm_ex.c $(OUT_DIR)/libomega_gpu.a
61 @mkdir -p $(GEMM_EX_DIR)
62 $(call GEMM_EX_LINK,$@,-DCUDAPP_MIGRATION_PENDING_GEMM_EX=1)
63$(GEMM_EX_DIR)/test_diff: $(GEMM_EX_TEST_DEPS) src/omega_gpu_gemm_ex.c $(OUT_DIR)/libomega_gpu.a
64 @mkdir -p $(GEMM_EX_DIR)
65 $(call GEMM_EX_LINK,$@,-DCUDAPP_MIGRATION_PENDING_GEMM_EX_DIFF=1)
66$(GEMM_EX_DIR)/test_diff_red: $(GEMM_EX_TEST_DEPS) src/omega_gpu_gemm_ex.c $(OUT_DIR)/libomega_gpu.a
67 @mkdir -p $(GEMM_EX_DIR)
68 $(call GEMM_EX_LINK,$@,-DCUDAPP_MIGRATION_PENDING_GEMM_EX_DIFF=1 -DCUDAPP_MIGRATION_PENDING_GEMM_EX_DIFF_SELFTEST)
69
70# Host tiers: no device opened. The chip tier is the same binary with no flag, under the GB10 lock
71# (flock -w 900 /tmp/aien-gb10.lock $(GPU_GEMM_EX_TEST) [--part I/N]); --list prints the predeclared sweep.
72.PHONY: test-gpu-gemm-ex test-gpu-gemm-ex-sim test-gpu-gemm-ex-c test-gpu-gemm-ex-diff test-gemm-ex-unfused \
73 test-gemm-ex-om-num test-gemm-ex-om-mutants
74test-gpu-gemm-ex: $(GPU_GEMM_EX_TEST)
75 ./$(GPU_GEMM_EX_TEST) --host-only
76test-gpu-gemm-ex-sim: $(GPU_GEMM_EX_TEST)
77 ./$(GPU_GEMM_EX_TEST) --sim
78test-gpu-gemm-ex-c: $(GEMM_EX_DIR)/test_c
79 $(GEMM_EX_DIR)/test_c --host-only
80 $(GEMM_EX_DIR)/test_c --sim
81test-gpu-gemm-ex-diff: $(GEMM_EX_DIR)/test_diff $(GEMM_EX_DIR)/test_diff_red
82 $(GEMM_EX_DIR)/test_diff --host-only
83 $(GEMM_EX_DIR)/test_diff --sim
84 @if $(GEMM_EX_DIR)/test_diff_red --host-only > $(GEMM_EX_DIR)/diff_red.log 2>&1; then \
85 echo "RED CHECK BROKEN: a flipped output bit passed the gemm_ex C-vs-Omega differential"; exit 1; fi; \
86 grep -q 'FAIL gemm_ex C-vs-Omega differential: output bytes differ' $(GEMM_EX_DIR)/diff_red.log || \
87 { echo "RED CHECK BROKEN: the flipped bit failed without the differential's message"; exit 1; }; \
88 echo "red check: one flipped output bit aborts the gemm_ex C-vs-Omega differential, as it must"
89
90# fmadd/fmsub/fnmadd/fnmsub (AArch64), vfmadd.../vfnmsub... (x86)
91GEMM_EX_FMA = '\bv?fn?m(add|sub)[0-9a-z]*\b'
92test-gemm-ex-unfused: $(GEMM_EX_DIR)/test_c $(OUT_DIR)/libomega_gpu.a
93 @mkdir -p $(GEMM_EX_DIR)
94 $(CC) $(GEMM_EX_CFLAGS) -DCUDAPP_MIGRATION_PENDING_GEMM_EX=1 -c -o $(GEMM_EX_DIR)/oracle.o src/omega_gpu_gemm_ex.c
95 @for o in $(GEMM_EX_DIR)/oracle.o $(OUT_DIR)/omega_gpu_gemm_ex.o $(patsubst src/%.c,$(OUT_DIR)/%.o,$(wildcard src/cudapp_om_gemm_ex_*.c)); do \
96 [ -f $$o ] || { echo "FAIL gemm_ex unfused: $$o missing"; exit 1; }; \
97 if objdump -d $$o | grep -Eq $(GEMM_EX_FMA); then echo "FAIL gemm_ex unfused: $$o holds a fused multiply-add"; \
98 objdump -d $$o | grep -Em3 $(GEMM_EX_FMA); exit 1; fi; done; \
99 echo "gemm_ex unfused: no fused multiply-add in the C oracle, the shell or the $(words $(wildcard src/cudapp_om_gemm_ex_*.c)) units"
100 @if [ "$$(uname -m)" != aarch64 ]; then echo "NOT_RUN red check: no fused multiply-add in this host's base ISA"; exit 0; fi; \
101 $(CC) $(filter-out -DCUDAPP_MIGRATION_PENDING_GEMM_EX=%,$(CFLAGS)) -DCUDAPP_MIGRATION_PENDING_GEMM_EX=1 -c \
102 -o $(GEMM_EX_DIR)/oracle_fused.o src/omega_gpu_gemm_ex.c; \
103 if objdump -d $(GEMM_EX_DIR)/oracle_fused.o | grep -Eq $(GEMM_EX_FMA); then \
104 echo "red check: the oracle built without -ffp-contract=off holds a fused multiply-add, as the check must see"; \
105 else echo "RED CHECK BROKEN: no fused multiply-add found even without -ffp-contract=off"; exit 1; fi
106
107CARRIED_HOST_TESTS += test-gpu-gemm-ex test-gpu-gemm-ex-sim test-gpu-gemm-ex-c test-gpu-gemm-ex-diff test-gemm-ex-unfused
108
109# Not in `make test` (timing, no verdict): the host-path cost of the Omega path against the C oracle on one core,
110# null core, 5 calls per case, medians (tools/gemm_ex_bench.sh). Output in $(GEMM_EX_DIR)/bench/.
111.PHONY: bench-gemm-ex
112bench-gemm-ex: $(GPU_GEMM_EX_TEST) $(GEMM_EX_DIR)/test_c
113 taskset -c 14 nice -n 10 sh tools/gemm_ex_bench.sh ./$(GPU_GEMM_EX_TEST) $(GEMM_EX_DIR)/test_c $(GEMM_EX_DIR)/bench
114
115GEMM_EX_OMC = $(OMEGA_MV)/seed/out/omc
116GEMM_EX_TOOLS = [ -x $(GEMM_EX_OMC) ] && [ -f $(OMEGA_BRIDGE_DIR)/src/native.om ] || \
117 { echo "NOT_RUN: set OMEGA_MV and OMEGA_BRIDGE_DIR"; exit 3; }
118test-gemm-ex-om-num:
119 @$(GEMM_EX_TOOLS)
120 @mkdir -p $(GEMM_EX_DIR)
121 cat om/gemmex/num.om om/gemmex/probe.om > $(GEMM_EX_DIR)/num_probe.om
122 OMEGA_SEEDED_OMC=$(GEMM_EX_OMC) sh $(OMEGA_BRIDGE_DIR)/tools/bridge-seeded c $(GEMM_EX_DIR)/num_probe.om > $(GEMM_EX_DIR)/num_probe.c
123 gcc -std=gnu11 -O2 -ffp-contract=off -o $(GEMM_EX_DIR)/num_test tests/gemm_ex_om_num_test.c $(GEMM_EX_DIR)/num_probe.c -lm
124 GX_STEP=13 $(GEMM_EX_DIR)/num_test
125
126# Each mutant: unit=sed-expression on that unit's last source (om/gemmex/<unit>.om); the unit is regenerated by the
127# pinned omc into its own directory and linked into the host tier in place of the committed one, which must go red.
128GEMM_EX_MUTANTS = \
129 'plan|s/^if or gt get x 18 2 eq get x 28 0$$/if or gt get x 18 3 eq get x 28 0/' \
130 'row|s/^ret shr b 16$$/ret shr b 15/' \
131 'row|s/^if ne and a gx_wh es 0$$/if ne and a 0 0/' \
132 'row|s/^gx_put edit dst i ce uint get tab ulong get src i$$/gx_put edit dst i ce uint get tab 0/' \
133 'x3|s/^p1 = gx_lane p1 shr s 16 kk$$/p1 = gx_lane p1 shr s 15 kk/' \
134 'acc|s/^f = f64 get s add ix 1$$/f = f64 get s add ix 2/' \
135 'dot|s/^s = add s mul x f$$/s = add s x/' \
136 'epi|s/^if ne bt bitcast f64 ulong 0$$/if eq bt bitcast f64 ulong 0/' \
137 'epi|s/^put c add k 1 ushort shr u 16$$/put c add k 1 ushort and shr u 15 65535/' \
138 'fp8|s/^ret ulong get s sub cb sb$$/ret ulong get s ulong 0/' \
139 'fp8|s/^cb = add cb 1$$/cb = add cb 0/'
140test-gemm-ex-om-mutants: $(OUT_DIR)/libomega_gpu.a
141 @$(GEMM_EX_TOOLS)
142 @n=0; for mu in $(GEMM_EX_MUTANTS); do n=$$((n+1)); u=$${mu%%|*}; m=$${mu#*|}; d=$$(pwd)/$(GEMM_EX_DIR)/mutant$$n; \
143 rm -rf $$d; mkdir -p $$d; sed "$$m" om/gemmex/$$u.om > $$d/$$u.om; \
144 if cmp -s $$d/$$u.om om/gemmex/$$u.om; then echo "MUTANT $$n BROKEN: $$u '$$m' changed nothing"; exit 1; fi; \
145 OMEGA_UNITS=gemm_ex_$$u=$$(pwd)/om/gemmex/num.om,$$d/$$u.om OMEGA_UNITS_DEST=$$d OMEGA_OMC=$(GEMM_EX_OMC) \
146 OMEGA_BRIDGE=$(OMEGA_BRIDGE_DIR) sh om/units.sh > $$d/units.log 2>&1 || { echo "MUTANT $$n BROKEN: $$u does not compile: $$(tail -1 $$d/units.log)"; exit 1; }; \
147 $(call GEMM_EX_LINK,$$d/test,,$$d/cudapp_om_gemm_ex_$$u.c) || exit 1; \
148 if $$d/test --host-only > $$d/test.log 2>&1; then echo "MUTANT $$n SURVIVED: $$u '$$m'"; exit 1; fi; \
149 echo "mutant $$n red ($$u '$$m'): $$(grep -m1 -e FAIL -e 'trap\|abort' $$d/test.log)"; \
150 done