omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
GitHub aien-dev/omega-mv#241 (code only)
Apply it
git checkout -b patch-3f2c231058308857 400dbfb93c34eceb88e5234cbc6c93005ea98a63 curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/3f2c231058308857.patch | git am
Provenance
github-import 1
aien-dev/omega-mv
241
2255ea6d360a8c69de37fcea3b3c464806008914
{"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":241,"url":"https://github.com/aien-dev/omega-mv/pull/241","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:56Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/Makefile b/Makefile index effb07d7d..bc5633353 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ SRCS = src/sha256.c src/omega_canonical.c src/omega_validate.c src/omega_core.c src/omega_library.c src/omega_discovery.c src/omega_machine.c src/omega_realize_synth.c \ src/omega_matvec.c src/omega_accelerator.c src/omega_accelerator_world.c \ src/omega_vector.c src/omega_blackwell_encoder.c src/omega_blackwell_qmd.c \ - src/omega_blackwell_realize.c src/omega_blackwell_submit.c src/omega_blackwell_engine.c src/omega_gpu_engine.c src/omega_blackwell_gates.c src/omega_blackwell_matmul.c src/omega_blackwell_codegen.c src/omega_world_gates.c \ + src/omega_blackwell_realize.c src/omega_blackwell_submit.c src/omega_blackwell_engine.c src/omega_gpu_engine.c src/omega_blackwell_gates.c src/omega_blackwell_matmul.c src/omega_blackwell_codegen.c src/omega_gpu_matmul_api.c src/omega_world_gates.c \ src/omega_evidence.c \ $(PHYSICS_DIR)/m16/m16_native.c $(PHYSICS_DIR)/nvrm/nvrm.c \ tools/omegatool.c @@ -2128,3 +2128,18 @@ test-vc-bridge: tests/test_omega_vc_bridge.c src/omega_vc_bridge.c src/omega_vc_ vmut cap omega_vc_bridge refuse-bridge-record-in-build-domain ' ou32(&o, 0);'; \ vmut build-refuses-selfminted omega_resolve refuse-bridge-record-in-build-domain ' (void)lists_selfminted_cap;' @echo "test-vc-bridge: PASS (bridge verifies and recomputes the id, bridge records refused in build, ASan/UBSan clean, 4 mutants killed)" + +# FB-1 cut 1: native (no CUDA) matmul entry point for the inference stack. +# libomega_gpu.a = everything the tool links except its main; the Rust FFI crate +# (cut 2) links it. test-gpu-matmul-api runs the host-only refusals; the chip +# sweep is `./build/gpu_matmul_api_test --out receipt.json` through the heavy queue. +.PHONY: libomega_gpu test-gpu-matmul-api +GPU_API_OBJS = $(filter-out $(OUT_DIR)/omegatool.o,$(OBJS)) +GPU_API_TEST = $(OUT_DIR)/gpu_matmul_api_test +$(OUT_DIR)/libomega_gpu.a: check-physics-lock $(GPU_API_OBJS) + ar rcs $@ $(GPU_API_OBJS) +libomega_gpu: $(OUT_DIR)/libomega_gpu.a +$(GPU_API_TEST): tests/gpu_matmul_api_test.c src/omega_gpu_matmul_api.h $(OUT_DIR)/libomega_gpu.a + $(CC) $(CFLAGS) -o $@ tests/gpu_matmul_api_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm +test-gpu-matmul-api: $(GPU_API_TEST) + ./$(GPU_API_TEST) --host-only diff --git a/evidence/GPU_MATMUL_API/manifest-6cd2dc0-run4.txt b/evidence/GPU_MATMUL_API/manifest-6cd2dc0-run4.txt new file mode 100644 index 000000000..90f9b377e --- /dev/null +++ b/evidence/GPU_MATMUL_API/manifest-6cd2dc0-run4.txt @@ -0,0 +1,8 @@ +omega_sha=6cd2dc01663ba059f54c5946688eb6d7b7958aec +physics_sha=6d7cf0d4d8eb2cda7b512100ff6058e25dbb3ddf +binary_sha256=ed5ebf36c82b5d3bd354b31c4f030c3dc81a91d3a291906cffc088a59f13017e +tree_clean=0 +start=2026-10-03T22:08:32Z +rc=0 end=2026-10-03T22:09:47Z +6ae7abe1d63835e394fbd83a7858acd07724402cdadffadec67b53da05a14372 /home/drakestapleton/workspace/evidence-out/GPU-MATMUL-API-6cd2dc0/receipt-run4.json +094f7c1e1001b34da3812f08e7a8658227249f037333ee54bce551b9a4a46d2a /home/drakestapleton/workspace/evidence-out/GPU-MATMUL-API-6cd2dc0/sweep-run4.log diff --git a/evidence/GPU_MATMUL_API/receipt-6cd2dc0-run4.json b/evidence/GPU_MATMUL_API/receipt-6cd2dc0-run4.json new file mode 100644 index 000000000..f268a5a94 --- /dev/null +++ b/evidence/GPU_MATMUL_API/receipt-6cd2dc0-run4.json @@ -0,0 +1 @@ +{"schema":"OMEGA_GPU_MATMUL_API_SWEEP_V1","shapes":[{"m":16,"k":16,"n":16,"rc":"OK","pass":true,"chip_elapsed_ns":124305,"wall_ms":52.309,"max_abs_err":2.38419e-07,"max_rel_err":3.0714e-06,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":4.51833e-08,"k_slices":1,"chip_calls":1,"rows_per_call":16,"padded_m":16,"padded_n":16,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":32,"k":32,"n":32,"rc":"OK","pass":true,"chip_elapsed_ns":217138,"wall_ms":89.633,"max_abs_err":2.38419e-07,"max_rel_err":2.17111e-05,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":4.77202e-08,"k_slices":2,"chip_calls":2,"rows_per_call":32,"padded_m":32,"padded_n":32,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":64,"k":64,"n":64,"rc":"OK","pass":true,"chip_elapsed_ns":509635,"wall_ms":172.425,"max_abs_err":4.76837e-07,"max_rel_err":9.56023e-06,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":4.69721e-08,"k_slices":4,"chip_calls":4,"rows_per_call":64,"padded_m":64,"padded_n":64,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":128,"k":128,"n":128,"rc":"OK","pass":true,"chip_elapsed_ns":971939,"wall_ms":355.496,"max_abs_err":4.76837e-07,"max_rel_err":0.000153941,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":5.41535e-08,"k_slices":8,"chip_calls":8,"rows_per_call":128,"padded_m":128,"padded_n":128,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":256,"k":256,"n":256,"rc":"OK","pass":true,"chip_elapsed_ns":7443288,"wall_ms":2898.589,"max_abs_err":9.53674e-07,"max_rel_err":0.0178571,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":5.87267e-08,"k_slices":16,"chip_calls":64,"rows_per_call":64,"padded_m":256,"padded_n":256,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":512,"k":512,"n":512,"rc":"OK","pass":true,"chip_elapsed_ns":58674645,"wall_ms":22791.168,"max_abs_err":9.53674e-07,"max_rel_err":0.04375,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":6.86441e-08,"k_slices":32,"chip_calls":512,"rows_per_call":32,"padded_m":512,"padded_n":512,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":1,"k":1024,"n":1024,"rc":"OK","pass":true,"chip_elapsed_ns":7209244,"wall_ms":2923.342,"max_abs_err":4.76837e-07,"max_rel_err":6.47836e-05,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":4.95366e-08,"k_slices":64,"chip_calls":64,"rows_per_call":16,"padded_m":16,"padded_n":1024,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":1,"k":256,"n":1024,"rc":"OK","pass":true,"chip_elapsed_ns":1795639,"wall_ms":720.003,"max_abs_err":4.76837e-07,"max_rel_err":0.000146028,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":3.87896e-08,"k_slices":16,"chip_calls":16,"rows_per_call":16,"padded_m":16,"padded_n":1024,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":16,"k":1024,"n":256,"rc":"OK","pass":true,"chip_elapsed_ns":7398287,"wall_ms":2833.148,"max_abs_err":4.76837e-07,"max_rel_err":0.00181159,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":3.94155e-08,"k_slices":64,"chip_calls":64,"rows_per_call":16,"padded_m":16,"padded_n":256,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":8,"k":1024,"n":8,"rc":"OK","pass":true,"chip_elapsed_ns":7489873,"wall_ms":2831.004,"max_abs_err":4.76837e-07,"max_rel_err":8.33806e-06,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":2.04752e-08,"k_slices":64,"chip_calls":64,"rows_per_call":16,"padded_m":16,"padded_n":8,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":1024,"k":16,"n":128,"rc":"OK","pass":true,"chip_elapsed_ns":873043,"wall_ms":360.698,"max_abs_err":4.76837e-07,"max_rel_err":0.01,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":9.37062e-08,"k_slices":1,"chip_calls":8,"rows_per_call":128,"padded_m":1024,"padded_n":128,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":192,"k":16,"n":192,"rc":"OK","pass":true,"chip_elapsed_ns":323521,"wall_ms":134.352,"max_abs_err":4.76837e-07,"max_rel_err":4.77373e-05,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":1.0078e-07,"k_slices":1,"chip_calls":3,"rows_per_call":80,"padded_m":192,"padded_n":192,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":512,"k":16,"n":64,"rc":"OK","pass":true,"chip_elapsed_ns":300753,"wall_ms":93.684,"max_abs_err":4.76837e-07,"max_rel_err":0.00213068,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":7.64946e-08,"k_slices":1,"chip_calls":2,"rows_per_call":256,"padded_m":512,"padded_n":64,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":256,"k":16,"n":256,"rc":"OK","pass":true,"chip_elapsed_ns":435872,"wall_ms":180.818,"max_abs_err":4.76837e-07,"max_rel_err":2.54298e-05,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":9.74134e-08,"k_slices":1,"chip_calls":4,"rows_per_call":64,"padded_m":256,"padded_n":256,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":128,"k":16,"n":512,"rc":"OK","pass":true,"chip_elapsed_ns":441265,"wall_ms":186.548,"max_abs_err":4.76837e-07,"max_rel_err":5.449e-05,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":8.99603e-08,"k_slices":1,"chip_calls":4,"rows_per_call":32,"padded_m":128,"padded_n":512,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121}],"checks":15,"failed":0,"verdict":"OMEGA_GPU_MATMUL_API_PASS"} diff --git a/evidence/GPU_MATMUL_API/sweep-6cd2dc0-run4.log b/evidence/GPU_MATMUL_API/sweep-6cd2dc0-run4.log new file mode 100644 index 000000000..aab4c9530 --- /dev/null +++ b/evidence/GPU_MATMUL_API/sweep-6cd2dc0-run4.log @@ -0,0 +1,16 @@ +PASS 16x16x16 rc=OK chip_ns=124305 wall_ms=52.3 max_abs=2.38419e-07 max_rel=3.0714e-06 mismatches=0 e2e_rel=4.51833e-08 slices=1 calls=1 rows_per_call=16 cache_hit2=1 +PASS 32x32x32 rc=OK chip_ns=217138 wall_ms=89.6 max_abs=2.38419e-07 max_rel=2.17111e-05 mismatches=0 e2e_rel=4.77202e-08 slices=2 calls=2 rows_per_call=32 cache_hit2=1 +PASS 64x64x64 rc=OK chip_ns=509635 wall_ms=172.4 max_abs=4.76837e-07 max_rel=9.56023e-06 mismatches=0 e2e_rel=4.69721e-08 slices=4 calls=4 rows_per_call=64 cache_hit2=1 +PASS 128x128x128 rc=OK chip_ns=971939 wall_ms=355.5 max_abs=4.76837e-07 max_rel=0.000153941 mismatches=0 e2e_rel=5.41535e-08 slices=8 calls=8 rows_per_call=128 cache_hit2=1 +PASS 256x256x256 rc=OK chip_ns=7443288 wall_ms=2898.6 max_abs=9.53674e-07 max_rel=0.0178571 mismatches=0 e2e_rel=5.87267e-08 slices=16 calls=64 rows_per_call=64 cache_hit2=1 +PASS 512x512x512 rc=OK chip_ns=58674645 wall_ms=22791.2 max_abs=9.53674e-07 max_rel=0.04375 mismatches=0 e2e_rel=6.86441e-08 slices=32 calls=512 rows_per_call=32 cache_hit2=1 +PASS 1x1024x1024 rc=OK chip_ns=7209244 wall_ms=2923.3 max_abs=4.76837e-07 max_rel=6.47836e-05 mismatches=0 e2e_rel=4.95366e-08 slices=64 calls=64 rows_per_call=16 cache_hit2=1 +PASS 1x256x1024 rc=OK chip_ns=1795639 wall_ms=720.0 max_abs=4.76837e-07 max_rel=0.000146028 mismatches=0 e2e_rel=3.87896e-08 slices=16 calls=16 rows_per_call=16 cache_hit2=1 +PASS 16x1024x256 rc=OK chip_ns=7398287 wall_ms=2833.1 max_abs=4.76837e-07 max_rel=0.00181159 mismatches=0 e2e_rel=3.94155e-08 slices=64 calls=64 rows_per_call=16 cache_hit2=1 +PASS 8x1024x8 rc=OK chip_ns=7489873 wall_ms=2831.0 max_abs=4.76837e-07 max_rel=8.33806e-06 mismatches=0 e2e_rel=2.04752e-08 slices=64 calls=64 rows_per_call=16 cache_hit2=1 +PASS 1024x16x128 rc=OK chip_ns=873043 wall_ms=360.7 max_abs=4.76837e-07 max_rel=0.01 mismatches=0 e2e_rel=9.37062e-08 slices=1 calls=8 rows_per_call=128 cache_hit2=1 +PASS 192x16x192 rc=OK chip_ns=323521 wall_ms=134.4 max_abs=4.76837e-07 max_rel=4.77373e-05 mismatches=0 e2e_rel=1.0078e-07 slices=1 calls=3 rows_per_call=80 cache_hit2=1 +PASS 512x16x64 rc=OK chip_ns=300753 wall_ms=93.7 max_abs=4.76837e-07 max_rel=0.00213068 mismatches=0 e2e_rel=7.64946e-08 slices=1 calls=2 rows_per_call=256 cache_hit2=1 +PASS 256x16x256 rc=OK chip_ns=435872 wall_ms=180.8 max_abs=4.76837e-07 max_rel=2.54298e-05 mismatches=0 e2e_rel=9.74134e-08 slices=1 calls=4 rows_per_call=64 cache_hit2=1 +PASS 128x16x512 rc=OK chip_ns=441265 wall_ms=186.5 max_abs=4.76837e-07 max_rel=5.449e-05 mismatches=0 e2e_rel=8.99603e-08 slices=1 calls=4 rows_per_call=32 cache_hit2=1 +PASS: 15 checks, 0 failed diff --git a/src/omega_gpu_matmul_api.c b/src/omega_gpu_matmul_api.c new file mode 100644 index 000000000..61e41d332 --- /dev/null +++ b/src/omega_gpu_matmul_api.c @@ -0,0 +1,194 @@ +#include "omega_gpu_matmul_api.h" +#include "omega_blackwell_matmul.h" +#include "omega_blackwell_submit.h" +#include <pthread.h> +#include <stdlib.h> +#include <string.h> + +/* Small in-process kernel cache keyed by shape. Eight shapes cover a decode + * loop (gemv per projection) comfortably; least-recently-inserted is evicted. */ +#define CACHE_SLOTS 8 + +typedef struct { + bool valid; + uint32_t m, k, n; + OmegaMatMulSpec spec; + OmegaBlackwellKernel kernel; +} CacheSlot; + +static CacheSlot g_cache[CACHE_SLOTS]; +static unsigned g_cache_next; +static pthread_mutex_t g_cache_mu = PTHREAD_MUTEX_INITIALIZER; + +const char *omega_gpu_matmul_rc_name(int rc) { + switch (rc) { + case OMEGA_GPU_MATMUL_OK: return "OK"; + case OMEGA_GPU_MATMUL_BAD_ARGS: return "BAD_ARGS"; + case OMEGA_GPU_MATMUL_TOO_LARGE: return "TOO_LARGE"; + case OMEGA_GPU_MATMUL_CODEGEN_FAIL: return "CODEGEN_FAIL"; + case OMEGA_GPU_MATMUL_CHIP_FAIL: return "CHIP_FAIL"; + case OMEGA_GPU_MATMUL_PARITY_FAIL: return "PARITY_FAIL"; + default: return "UNKNOWN"; + } +} + +void omega_gpu_matmul_cache_clear(void) { + pthread_mutex_lock(&g_cache_mu); + for (unsigned i = 0; i < CACHE_SLOTS; i++) { + if (g_cache[i].valid) omega_blackwell_kernel_free(&g_cache[i].kernel); + memset(&g_cache[i], 0, sizeof g_cache[i]); + } + g_cache_next = 0; + pthread_mutex_unlock(&g_cache_mu); +} + +/* Returns a slot holding a generated kernel for the shape, or NULL with *rc set. + * The slot stays valid until cache_clear or eviction; callers copy spec+kernel + * pointers under the lock and release before the chip call (kernel code bytes + * are immutable once generated, eviction only happens under the same lock, and + * with 8 slots a caller's shape cannot be evicted by its own call). */ +static int kernel_for_shape(uint32_t m, uint32_t k, uint32_t n, + OmegaMatMulSpec *spec_out, OmegaBlackwellKernel *kernel_out, + bool *hit) { + pthread_mutex_lock(&g_cache_mu); + for (unsigned i = 0; i < CACHE_SLOTS; i++) { + CacheSlot *s = &g_cache[i]; + if (s->valid && s->m == m && s->k == k && s->n == n) { + *spec_out = s->spec; + *kernel_out = s->kernel; + *hit = true; + pthread_mutex_unlock(&g_cache_mu); + return OMEGA_GPU_MATMUL_OK; + } + } + OmegaMatMulSpec spec; + if (omega_matmul_spec_init(&spec, m, k, n, OMEGA_MATMUL_PRECISION_BF16) != 0) { + pthread_mutex_unlock(&g_cache_mu); + return OMEGA_GPU_MATMUL_TOO_LARGE; + } + OmegaBlackwellKernel kernel; + memset(&kernel, 0, sizeof kernel); + if (omega_blackwell_codegen_matmul(&spec, &kernel) != 0 || !kernel.code || kernel.code_size == 0) { + pthread_mutex_unlock(&g_cache_mu); + return OMEGA_GPU_MATMUL_CODEGEN_FAIL; + } + CacheSlot *s = &g_cache[g_cache_next]; + g_cache_next = (g_cache_next + 1) % CACHE_SLOTS; + if (s->valid) omega_blackwell_kernel_free(&s->kernel); + s->valid = true; + s->m = m; s->k = k; s->n = n; + s->spec = spec; + s->kernel = kernel; + *spec_out = spec; + *kernel_out = kernel; + *hit = false; + pthread_mutex_unlock(&g_cache_mu); + return OMEGA_GPU_MATMUL_OK; +} + +static uint32_t round_up(uint32_t v, uint32_t q) { return (v + q - 1) / q * q; } + +int omega_gpu_matmul_bf16(uint32_t m, uint32_t k, uint32_t n, + const uint16_t *a, const uint16_t *b, float *c, + OmegaGpuMatmulInfo *info) { + if (info) memset(info, 0, sizeof *info); + if (!a || !b || !c || m == 0 || k == 0 || n == 0) return OMEGA_GPU_MATMUL_BAD_ARGS; + if (m > OMEGA_BW_MATMUL_MAX_M || k > OMEGA_BW_MATMUL_MAX_K || n > OMEGA_BW_MATMUL_MAX_N) + return OMEGA_GPU_MATMUL_TOO_LARGE; + const uint32_t mp = round_up(m, OMEGA_GPU_MATMUL_TILE_M); + const uint32_t np = round_up(n, OMEGA_GPU_MATMUL_TILE_N); + const uint32_t tk = OMEGA_GPU_MATMUL_TILE_K; + const uint32_t slices = (k + tk - 1) / tk; + /* CTA envelope: the kernel rasters 16x16 tiles (grid_x = np/16 rounded up, + * grid_y = rows/16). Keep grid_x * grid_y <= OMEGA_GPU_MATMUL_MAX_CTAS by + * limiting the rows handed to one launch. */ + const uint32_t grid_x = (np + 15) / 16; + uint32_t rows_per_call = (OMEGA_GPU_MATMUL_MAX_CTAS / grid_x) * OMEGA_GPU_MATMUL_TILE_M; + if (rows_per_call < OMEGA_GPU_MATMUL_TILE_M) rows_per_call = OMEGA_GPU_MATMUL_TILE_M; /* grid_x alone exceeds: 1024 cols = 64 CTAs, still inside */ + if (rows_per_call > mp) rows_per_call = mp; + const uint32_t row_blocks = (mp + rows_per_call - 1) / rows_per_call; + if (info) { + info->m = m; info->k = k; info->n = n; info->padded_m = mp; info->padded_n = np; + info->k_slices = slices; info->rows_per_call = rows_per_call; + } + + uint16_t *ap = calloc((size_t)rows_per_call * tk, sizeof *ap); + uint16_t *bp = calloc((size_t)tk * np, sizeof *bp); + float *cp = malloc((size_t)rows_per_call * np * sizeof *cp); + if (!ap || !bp || !cp) { free(ap); free(bp); free(cp); return OMEGA_GPU_MATMUL_BAD_ARGS; } + for (size_t i = 0; i < (size_t)m * n; i++) c[i] = 0.0f; + + bool all_parity = true, any_miss = false; + int rc = OMEGA_GPU_MATMUL_OK; + for (uint32_t s = 0; s < slices && rc == OMEGA_GPU_MATMUL_OK; s++) { + const uint32_t k0 = s * tk; + const uint32_t kw = (k - k0 < tk) ? (k - k0) : tk; + /* B slice: rows k0..k0+kw of columns 0..n, zero elsewhere (row-major 16 x np) */ + memset(bp, 0, (size_t)tk * np * sizeof *bp); + for (uint32_t j = 0; j < kw; j++) + memcpy(&bp[(size_t)j * np], &b[(size_t)(k0 + j) * n], n * sizeof *bp); + for (uint32_t rb = 0; rb < row_blocks && rc == OMEGA_GPU_MATMUL_OK; rb++) { + const uint32_t r0 = rb * rows_per_call; + uint32_t rows = mp - r0 < rows_per_call ? mp - r0 : rows_per_call; + rows = round_up(rows, OMEGA_GPU_MATMUL_TILE_M); + const uint32_t rows_real = (m > r0) ? ((m - r0 < rows) ? (m - r0) : rows) : 0; + OmegaMatMulSpec spec; + OmegaBlackwellKernel kernel; + bool hit = false; + rc = kernel_for_shape(rows, tk, np, &spec, &kernel, &hit); + if (rc != OMEGA_GPU_MATMUL_OK) break; + if (info) { if (hit) info->kernel_cache_hit = true; else any_miss = true; } + /* A slice: rows r0..r0+rows_real of columns k0..k0+kw, zero elsewhere (row-major rows x 16) */ + memset(ap, 0, (size_t)rows * tk * sizeof *ap); + for (uint32_t i = 0; i < rows_real; i++) + memcpy(&ap[(size_t)i * tk], &a[(size_t)(r0 + i) * k + k0], kw * sizeof *ap); + + OmegaBlackwellMatMulExecution exec; + memset(&exec, 0, sizeof exec); + float max_abs = 0.0f, max_rel = 0.0f; + int r = omega_blackwell_execute_matmul_tensor(&spec, &kernel, ap, bp, cp, &exec, &max_abs, &max_rel); + if (info) { + info->chip_calls++; + info->elapsed_ns += exec.elapsed_ns; + if (max_abs > info->max_abs_err) info->max_abs_err = max_abs; + if (max_rel > info->max_rel_err) info->max_rel_err = max_rel; + info->mismatch_count += exec.mismatch_count; + info->completion_marker = exec.completion_marker; + info->sm_architecture = exec.sm_architecture; + memcpy(info->target_chip, exec.target_chip, sizeof info->target_chip); + } + if (r != 0 || exec.completion_marker != OMEGA_BW_MARKER_COMPLETION_PAYLOAD) { rc = OMEGA_GPU_MATMUL_CHIP_FAIL; break; } + if (!exec.parity_verified) all_parity = false; + for (uint32_t i = 0; i < rows_real; i++) + for (uint32_t j = 0; j < n; j++) + c[(size_t)(r0 + i) * n + j] += cp[(size_t)i * np + j]; + } + } + free(ap); free(bp); free(cp); + if (info) { + info->parity_verified = all_parity && rc == OMEGA_GPU_MATMUL_OK; + if (any_miss) info->kernel_cache_hit = false; /* hit only when every launch found its kernel */ + } + if (rc != OMEGA_GPU_MATMUL_OK) return rc; + if (!all_parity) return OMEGA_GPU_MATMUL_PARITY_FAIL; + return OMEGA_GPU_MATMUL_OK; +} + +int omega_gpu_matmul_f32(uint32_t m, uint32_t k, uint32_t n, + const float *a, const float *b, float *c, + OmegaGpuMatmulInfo *info) { + if (info) memset(info, 0, sizeof *info); + if (!a || !b || !c || m == 0 || k == 0 || n == 0) return OMEGA_GPU_MATMUL_BAD_ARGS; + if (m > OMEGA_BW_MATMUL_MAX_M || k > OMEGA_BW_MATMUL_MAX_K || n > OMEGA_BW_MATMUL_MAX_N) + return OMEGA_GPU_MATMUL_TOO_LARGE; + size_t na = (size_t)m * k, nb = (size_t)k * n; + uint16_t *ab = malloc(na * sizeof *ab); + uint16_t *bb = malloc(nb * sizeof *bb); + if (!ab || !bb) { free(ab); free(bb); return OMEGA_GPU_MATMUL_BAD_ARGS; } + for (size_t i = 0; i < na; i++) ab[i] = omega_fp32_to_bf16(a[i]); + for (size_t i = 0; i < nb; i++) bb[i] = omega_fp32_to_bf16(b[i]); + int rc = omega_gpu_matmul_bf16(m, k, n, ab, bb, c, info); + free(ab); + free(bb); + return rc; +} diff --git a/src/omega_gpu_matmul_api.h b/src/omega_gpu_matmul_api.h new file mode 100644 index 000000000..e051fdcda --- /dev/null +++ b/src/omega_gpu_matmul_api.h @@ -0,0 +1,85 @@ +#ifndef OMEGA_GPU_MATMUL_API_H +#define OMEGA_GPU_MATMUL_API_H +/* + * omega_gpu_matmul_api: the first native (no CUDA) matrix-multiply entry point + * for the AIEN inference stack (FB-1 cut 1, 2026-10-03). + * + * Wraps the chip-proven M16/M17 tensor matmul path (omega_blackwell_submit.c, + * all buffers GPU-uncached, C3 release tail) behind a plain C call that a + * Rust FFI crate can link. Inputs are row-major bf16 (A: m x k, B: k x n), + * output is row-major f32 (C: m x n). Every call also runs the host oracle and + * reports the error bound, so the first consumers get a parity receipt for free + * (cost: one CPU matmul per call; a later cut makes the oracle optional). + * + * Kernels are generated by our own codegen per (m, k, n) and cached in-process. + */ +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> + +#ifdef __cplusplus +extern "C" { +#endif + +enum { + OMEGA_GPU_MATMUL_OK = 0, + OMEGA_GPU_MATMUL_BAD_ARGS = -1, /* NULL pointer or zero dimension */ + OMEGA_GPU_MATMUL_TOO_LARGE = -2, /* a dimension exceeds OMEGA_BW_MATMUL_MAX_* */ + OMEGA_GPU_MATMUL_CODEGEN_FAIL = -3, /* our codegen refused the shape */ + OMEGA_GPU_MATMUL_CHIP_FAIL = -4, /* device open, submit, marker or release failed */ + OMEGA_GPU_MATMUL_PARITY_FAIL = -5 /* chip result disagrees with the host oracle */ +}; + +/* Kernel shape rule today (omega_blackwell_codegen_matmul_tensor): M multiple of + * 16, N multiple of 8, K exactly 16. The API hides it: A and B are zero-padded + * on the host to (Mp x 16) and (16 x Np) slices, one chip call per 16-wide K + * slice, partial products summed on the host in f32. Correct for any shape up + * to the limits; the per-slice device open/submit cost is the known price until + * the in-kernel K loop lands (FB-1 cut 1b). */ +#define OMEGA_GPU_MATMUL_TILE_M 16 +#define OMEGA_GPU_MATMUL_TILE_N 8 +#define OMEGA_GPU_MATMUL_TILE_K 16 +/* Chip-proven launch envelope (2026-10-03 sweep, evidence GPU-MATMUL-API-2ea8e67): + * grids of <= 100 CTAs return correct tiles, 128 and above lose whole tiles + * (investigation I42). The API never launches more than this many CTAs per + * call and splits rows on the host above it. */ +#define OMEGA_GPU_MATMUL_MAX_CTAS 64 + +typedef struct { + uint32_t m, k, n; + uint32_t padded_m, padded_n;/* shape handed to the kernel */ + uint32_t k_slices; /* 16-wide K slices: ceil(k / 16) */ + uint32_t chip_calls; /* launches made (slices x row blocks) */ + uint32_t rows_per_call; /* row block height chosen for the CTA envelope */ + uint64_t elapsed_ns; /* sum of chip launch-to-completion over the slices */ + float max_abs_err; /* vs host bf16 oracle */ + float max_rel_err; + size_t mismatch_count; + bool parity_verified; /* every slice agreed with the host oracle */ + uint32_t completion_marker; /* observed first marker word of the last slice */ + bool kernel_cache_hit; /* the padded shape's kernel was already generated */ + char target_chip[64]; + uint32_t sm_architecture; +} OmegaGpuMatmulInfo; + +/* C[m x n] = A[m x k] * B[k x n], bf16 inputs, f32 output. info may be NULL. */ +int omega_gpu_matmul_bf16(uint32_t m, uint32_t k, uint32_t n, + const uint16_t *a, const uint16_t *b, float *c, + OmegaGpuMatmulInfo *info); + +/* Same, taking f32 inputs: they are rounded to bf16 on the host first + * (round-to-nearest-even), so the result carries bf16 input precision. */ +int omega_gpu_matmul_f32(uint32_t m, uint32_t k, uint32_t n, + const float *a, const float *b, float *c, + OmegaGpuMatmulInfo *info); + +/* Name of a return code, for logs and receipts. */ +const char *omega_gpu_matmul_rc_name(int rc); + +/* Drop every cached kernel (tests; otherwise never needed). */ +void omega_gpu_matmul_cache_clear(void); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/tests/gpu_matmul_api_test.c b/tests/gpu_matmul_api_test.c new file mode 100644 index 000000000..beb366762 --- /dev/null +++ b/tests/gpu_matmul_api_test.c @@ -0,0 +1,137 @@ +/* gpu_matmul_api_test: FB-1 cut 1 gate for omega_gpu_matmul_api. + * + * ./gpu_matmul_api_test --host-only argument and limit refusals, codegen only, no chip + * ./gpu_matmul_api_test --out receipt.json chip sweep over shapes, JSON receipt + * + * Shapes come from OMEGA_GPU_MATMUL_SHAPES ("m,k,n;m,k,n;..."), default below. + * Pass rule per shape: rc OK, parity verified, end-to-end error < 1e-5 relative to the accumulation scale (bf16 inputs + * against the host bf16 oracle, f32 accumulate; only summation order differs). + */ +#include "omega_gpu_matmul_api.h" +#include "omega_blackwell_matmul.h" +#include <inttypes.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <math.h> + +/* round a float to bf16 and back, as the API does on the way in */ +static float bf16r(float f) { uint16_t h = omega_fp32_to_bf16(f); uint32_t u = (uint32_t)h << 16; float o; memcpy(&o, &u, 4); return o; } + +static int g_checks, g_failed; +#define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) + +static const char *DEFAULT_SHAPES = + "16,16,16;32,32,32;64,64,64;128,128,128;256,256,256;512,512,512;" + "1,1024,1024;1,256,1024;16,1024,256;8,1024,8;" + "1024,16,128;192,16,192;512,16,64;256,16,256;128,16,512"; + +static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } + +static void host_only(void) { + float a[16 * 16] = {0}, b[16 * 16] = {0}, c[16 * 16] = {0}; + OmegaGpuMatmulInfo info; + CHECK(omega_gpu_matmul_f32(0, 16, 16, a, b, c, &info) == OMEGA_GPU_MATMUL_BAD_ARGS, "m=0 refused"); + CHECK(omega_gpu_matmul_f32(16, 16, 16, NULL, b, c, &info) == OMEGA_GPU_MATMUL_BAD_ARGS, "NULL a refused"); + CHECK(omega_gpu_matmul_f32(OMEGA_BW_MATMUL_MAX_M + 1, 16, 16, a, b, c, &info) == OMEGA_GPU_MATMUL_TOO_LARGE, "m over limit refused"); + CHECK(omega_gpu_matmul_bf16(16, OMEGA_BW_MATMUL_MAX_K + 1, 16, (uint16_t *)a, (uint16_t *)b, c, NULL) == OMEGA_GPU_MATMUL_TOO_LARGE, "k over limit refused (NULL info ok)"); + CHECK(strcmp(omega_gpu_matmul_rc_name(OMEGA_GPU_MATMUL_PARITY_FAIL), "PARITY_FAIL") == 0, "rc name"); + /* codegen only: shapes must be accepted by our codegen without touching the chip */ + OmegaMatMulSpec spec; OmegaBlackwellKernel kernel; memset(&kernel, 0, sizeof kernel); + /* documents the kernel rule the API pads around: M%16, N%8, K==16 */ + CHECK(omega_matmul_spec_init(&spec, 16, 16, 8, OMEGA_MATMUL_PRECISION_BF16) == 0 && omega_blackwell_codegen_matmul(&spec, &kernel) == 0 && kernel.code_size > 0, "16x16x8 tile kernel generated"); + omega_blackwell_kernel_free(&kernel); memset(&kernel, 0, sizeof kernel); + CHECK(omega_matmul_spec_init(&spec, 1, 1024, 1024, OMEGA_MATMUL_PRECISION_BF16) == 0 && omega_blackwell_codegen_matmul(&spec, &kernel) != 0, "raw 1x1024x1024 refused by codegen (the API pads and slices it)"); + omega_gpu_matmul_cache_clear(); +} + +static int chip_sweep(const char *out_path) { + const char *shapes = getenv("OMEGA_GPU_MATMUL_SHAPES"); + if (!shapes || !*shapes) shapes = DEFAULT_SHAPES; + FILE *out = out_path ? fopen(out_path, "w") : NULL; + if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; } + if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_MATMUL_API_SWEEP_V1\",\"shapes\":["); + char *copy = strdup(shapes); + char *save = NULL; + int first = 1; + for (char *tok = strtok_r(copy, ";", &save); tok; tok = strtok_r(NULL, ";", &save)) { + unsigned m, k, n; + if (sscanf(tok, "%u,%u,%u", &m, &k, &n) != 3) { printf("bad shape %s\n", tok); continue; } + size_t na = (size_t)m * k, nb = (size_t)k * n, nc = (size_t)m * n; + float *a = malloc(na * sizeof *a), *b = malloc(nb * sizeof *b), *c = malloc(nc * sizeof *c); + uint32_t seed = 0x5eed0000u ^ (m << 20) ^ (k << 10) ^ n; + for (size_t i = 0; i < na; i++) a[i] = ((float)(lcg(&seed) % 2001) - 1000.0f) / 1000.0f; + for (size_t i = 0; i < nb; i++) b[i] = ((float)(lcg(&seed) % 2001) - 1000.0f) / 1000.0f; + for (size_t i = 0; i < nc; i++) c[i] = -12345.0f; + OmegaGpuMatmulInfo info; + struct timespec t0, t1; + clock_gettime(CLOCK_MONOTONIC, &t0); + int rc = omega_gpu_matmul_f32(m, k, n, a, b, c, &info); + clock_gettime(CLOCK_MONOTONIC, &t1); + /* second call on the same shape must hit the kernel cache and agree */ + float *c2 = malloc(nc * sizeof *c2); + OmegaGpuMatmulInfo info2; + int rc2 = omega_gpu_matmul_f32(m, k, n, a, b, c2, &info2); + size_t repeat_mismatch = 0; + for (size_t i = 0; i < nc; i++) if (c[i] != c2[i]) repeat_mismatch++; + /* end-to-end: full-K product on the host from the same bf16-rounded inputs, in double */ + double e2e_max_rel = 0.0; + if (rc == OMEGA_GPU_MATMUL_OK) { + for (size_t i = 0; i < m; i++) for (size_t j = 0; j < n; j++) { + double acc = 0.0, scale = 0.0; + for (size_t t = 0; t < k; t++) { + double p = (double)bf16r(a[i * k + t]) * (double)bf16r(b[t * n + j]); + acc += p; scale += fabs(p); + } + /* error relative to the accumulation scale (sum of |terms|), not to a + * result that may cancel to near zero: f32 accumulation over k terms */ + double d = fabs(acc - (double)c[i * n + j]); + double rel = d / (scale > 1e-6 ? scale : 1e-6); + if (rel > e2e_max_rel) e2e_max_rel = rel; + } + } + double wall_ms = (t1.tv_sec - t0.tv_sec) * 1e3 + (t1.tv_nsec - t0.tv_nsec) / 1e6; + int pass = rc == OMEGA_GPU_MATMUL_OK && info.parity_verified && e2e_max_rel < 1e-5 + && rc2 == OMEGA_GPU_MATMUL_OK && info2.kernel_cache_hit && repeat_mismatch == 0; + CHECK(pass, "shape %ux%ux%u rc=%s parity=%d max_rel=%g e2e_rel=%g rc2=%s hit=%d repeat_mismatch=%zu", + m, k, n, omega_gpu_matmul_rc_name(rc), info.parity_verified, info.max_rel_err, e2e_max_rel, + omega_gpu_matmul_rc_name(rc2), info2.kernel_cache_hit, repeat_mismatch); + printf("%s %ux%ux%u rc=%s chip_ns=%" PRIu64 " wall_ms=%.1f max_abs=%g max_rel=%g mismatches=%zu e2e_rel=%g slices=%u calls=%u rows_per_call=%u cache_hit2=%d\n", + pass ? "PASS" : "FAIL", m, k, n, omega_gpu_matmul_rc_name(rc), info.elapsed_ns, wall_ms, + info.max_abs_err, info.max_rel_err, info.mismatch_count, e2e_max_rel, info.k_slices, info.chip_calls, info.rows_per_call, info2.kernel_cache_hit); + if (out) { + fprintf(out, "%s{\"m\":%u,\"k\":%u,\"n\":%u,\"rc\":\"%s\",\"pass\":%s,\"chip_elapsed_ns\":%" PRIu64 + ",\"wall_ms\":%.3f,\"max_abs_err\":%g,\"max_rel_err\":%g,\"mismatch_count\":%zu," + "\"parity_verified\":%s,\"completion_marker\":%u,\"repeat_rc\":\"%s\",\"repeat_cache_hit\":%s," + "\"repeat_mismatch\":%zu,\"e2e_max_rel\":%g,\"k_slices\":%u,\"chip_calls\":%u,\"rows_per_call\":%u,\"padded_m\":%u,\"padded_n\":%u,\"target_chip\":\"%s\",\"sm_architecture\":%u}", + first ? "" : ",", m, k, n, omega_gpu_matmul_rc_name(rc), pass ? "true" : "false", + info.elapsed_ns, wall_ms, info.max_abs_err, info.max_rel_err, info.mismatch_count, + info.parity_verified ? "true" : "false", info.completion_marker, + omega_gpu_matmul_rc_name(rc2), info2.kernel_cache_hit ? "true" : "false", + repeat_mismatch, e2e_max_rel, info.k_slices, info.chip_calls, info.rows_per_call, info.padded_m, info.padded_n, info.target_chip, info.sm_architecture); + first = 0; + } + free(a); free(b); free(c); free(c2); + } + free(copy); + if (out) { + fprintf(out, "],\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", g_checks, g_failed, + g_failed == 0 ? "OMEGA_GPU_MATMUL_API_PASS" : "OMEGA_GPU_MATMUL_API_FAIL"); + fclose(out); + } + return 0; +} + +int main(int argc, char **argv) { + const char *out_path = NULL; + int host = 0; + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "--host-only") == 0) host = 1; + else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i]; + } + if (host) host_only(); + else if (chip_sweep(out_path) != 0) return 2; + printf("%s: %d checks, %d failed\n", g_failed ? "FAIL" : "PASS", g_checks, g_failed); + return g_failed ? 1 : 0; +}