From 32a2a62a7b2d306884cd58743bfa51917dff3487 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Mon, 12 Oct 2026 04:36:46 -0500 Subject: [PATCH 1/2] gpu matmul: pack the activation rows straight into the chip buffer, row 0 only at m = 1 Every matmul call packed the activation rows into a malloc'd block, copied it into the uncached activation buffer and freed it. At m = 1 (one decode token, 145 calls per token on Qwen3-8B) that was 128 KB of memset, pack and copy per call to carry 8 KB of data: the 15 padding rows of every 16-row block are zero on every call. - matmul_core packs straight into the activation buffer (its CPU side is ordinary cached memory; the dsb before the launch orders it as before). - A one-row call whose buffer already holds a full one-row pack of the same width in the same allocation writes row 0 only (omega_matmul_frag_pack_a_row0 / _f32, every column, zeros past k). Any other pack clears that state; so does the session close. - Test hook omega_gpu_matmul_test_a_row0_hits counts the row-0 calls. ABI unchanged. Host battery 136/136 (row-0 pack equals the full one-row pack, bf16 and f32, k 4096 and 40). Chip battery 25/25: the default sweep plus the one-row sequence (full pack, row 0 with a new A, a 32-row call between, full pack again, row 0, another width packs in full), every result against the oracle. Receipts in the decode-speed investigation, cut-h. Co-Authored-By: Claude Fable 5.1 --- src/omega_blackwell_codegen.c | 21 ++++++++++++++ src/omega_blackwell_codegen.h | 4 +++ src/omega_gpu_matmul_api.c | 32 +++++++++++++++------ src/omega_gpu_matmul_api.h | 2 ++ tests/gpu_matmul_api_test.c | 53 +++++++++++++++++++++++++++++++++++ 5 files changed, 104 insertions(+), 8 deletions(-) diff --git a/src/omega_blackwell_codegen.c b/src/omega_blackwell_codegen.c index b0f657c11..62efccaa9 100644 --- a/src/omega_blackwell_codegen.c +++ b/src/omega_blackwell_codegen.c @@ -2165,6 +2165,27 @@ void omega_matmul_frag_pack_a_f32(const float *a, uint32_t m, uint32_t k, uint32 FRAG_PACK_A_BODY(frag_f32_to_bf16); } +/* Row 0 only, every column of every K block (zeros past k): for a one-row call whose buffer already + * holds zeros in rows 1..15 of every block from an earlier full pack of the same width. */ +#define FRAG_PACK_A_ROW0_BODY(LOAD) \ + do { \ + const uint32_t KS = kp / 16u; \ + frag_a_tab_init(); \ + const uint16_t *tab = g_frag_a_tab; \ + for (uint32_t ks = 0; ks < KS; ks++) { \ + uint16_t *blk = dst + (size_t)ks * 256u; \ + const uint32_t c0 = ks * 16u, c1 = k - c0 < 16u ? k - c0 : 16u; \ + for (uint32_t cc = 0; cc < c1; cc++) blk[tab[cc]] = LOAD(a[c0 + cc]); \ + for (uint32_t cc = c1; cc < 16u; cc++) blk[tab[cc]] = 0; \ + } \ + } while (0) +void omega_matmul_frag_pack_a_row0(const uint16_t *a, uint32_t k, uint32_t kp, uint16_t *dst) { + FRAG_PACK_A_ROW0_BODY(FRAG_ID); +} +void omega_matmul_frag_pack_a_row0_f32(const float *a, uint32_t k, uint32_t kp, uint16_t *dst) { + FRAG_PACK_A_ROW0_BODY(frag_f32_to_bf16); +} + void omega_matmul_frag_pack_b(const uint16_t *b, uint32_t k, uint32_t n, uint32_t kp, uint32_t nt0, uint32_t nt1, uint16_t *dst) { const uint32_t KS = kp / 16u; diff --git a/src/omega_blackwell_codegen.h b/src/omega_blackwell_codegen.h index 48658c178..b1baf21b1 100644 --- a/src/omega_blackwell_codegen.h +++ b/src/omega_blackwell_codegen.h @@ -252,6 +252,10 @@ uint32_t omega_matmul_frag_block_unroll(uint32_t kp, uint32_t rt, uint32_t ct); void omega_matmul_frag_pack_a(const uint16_t *a, uint32_t m, uint32_t k, uint32_t mp, uint32_t kp, uint16_t *dst); /* the same from f32 rows, converted to bf16 as omega_fp32_to_bf16 does (one pass, no staging copy) */ void omega_matmul_frag_pack_a_f32(const float *a, uint32_t m, uint32_t k, uint32_t mp, uint32_t kp, uint16_t *dst); +/* Row 0 of a one-row call into a buffer whose rows 1..15 already hold zeros for this kp (every column, + * zeros past k); the caller tracks that the zeros are there (omega_gpu_matmul_api.c, m = 1 path). */ +void omega_matmul_frag_pack_a_row0(const uint16_t *a, uint32_t k, uint32_t kp, uint16_t *dst); +void omega_matmul_frag_pack_a_row0_f32(const float *a, uint32_t k, uint32_t kp, uint16_t *dst); /* b: k x n row-major. dst: the column tiles [nt0, nt1), (nt1 - nt0) * kp * 8 elements, fully * written; it is the part of the packed tensor starting at element nt0 * kp * 8. */ void omega_matmul_frag_pack_b(const uint16_t *b, uint32_t k, uint32_t n, uint32_t kp, diff --git a/src/omega_gpu_matmul_api.c b/src/omega_gpu_matmul_api.c index c914ae525..907ccb19c 100644 --- a/src/omega_gpu_matmul_api.c +++ b/src/omega_gpu_matmul_api.c @@ -51,6 +51,11 @@ typedef struct { static struct { OmegaGpuScratch a_buf, c_buf; /* activation and result staging, grown to the high-water mark */ + /* a_buf holds a full one-row pack (rows 1..15 of every block zero) at width a_row0_kp in the + * allocation a_row0_cpu: the next one-row call of that width writes row 0 only. Cleared by any + * other pack and whenever the buffer is freed. */ + bool a_row0_ready; uint32_t a_row0_kp; const void *a_row0_cpu; + uint64_t a_row0_hits; /* one-row calls that wrote row 0 only (test hook) */ uint32_t cta_budget; int oracle, mutant; uint32_t unroll; /* test override of the fragment kernel unroll; 0 = omega_matmul_frag_unroll */ @@ -102,6 +107,7 @@ static void cache_clear_locked(void) { /* Called by the session (lock held, device still open) right before it closes. */ static void on_session_close(void) { cache_clear_locked(); + g.a_row0_ready = false; omega_gpu_session_scratch_free(&g.a_buf); omega_gpu_session_scratch_free(&g.c_buf); g.slots = CACHE_SLOTS; /* a serving reservation ends with the device */ @@ -210,6 +216,8 @@ void omega_gpu_matmul_test_set_mutant(int on) { omega_gpu_session_unlock(); } +uint64_t omega_gpu_matmul_test_a_row0_hits(void) { return g.a_row0_hits; } + void omega_gpu_matmul_test_set_unroll(uint32_t unroll) { omega_gpu_session_lock(); g.unroll = unroll; @@ -391,14 +399,22 @@ static int matmul_core(uint32_t m, const uint16_t *a16, const float *a32, const return src == OMEGA_GPU_SESSION_SCRATCH_RESERVED ? OMEGA_GPU_MATMUL_TOO_LARGE : OMEGA_GPU_MATMUL_CHIP_FAIL; } /* activation rows in, fragment order (omega_matmul_frag_pack_a / _f32: f32 rows are converted - * while packing, one pass), zero padded, packed on the host and copied in order into the - * uncached buffer; result rows poisoned */ - uint16_t *ah = malloc((size_t)mp * kp * sizeof *ah); - if (!ah) { omega_gpu_session_unlock(); return OMEGA_GPU_MATMUL_BAD_ARGS; } - if (a32) omega_matmul_frag_pack_a_f32(a32, m, k, mp, kp, ah); - else omega_matmul_frag_pack_a(a16, m, k, mp, kp, ah); - memcpy(g.a_buf.mem.cpu, ah, (size_t)mp * kp * sizeof *ah); - free(ah); + * while packing, one pass), zero padded, packed on the host straight into the uncached buffer + * (the CPU side of that memory is ordinary cached memory; dsb below orders it before the launch); + * result rows poisoned. A one-row call (decode) writes row 0 only when the buffer already holds + * a full one-row pack of this width (rows 1..15 of every block zero): the 15 zero rows are not + * rewritten per token. */ + uint16_t *ad = g.a_buf.mem.cpu; + const bool one_row = m == 1 && mp == 16u; + if (one_row && g.a_row0_ready && g.a_row0_kp == kp && g.a_row0_cpu == ad) { + if (a32) omega_matmul_frag_pack_a_row0_f32(a32, k, kp, ad); + else omega_matmul_frag_pack_a_row0(a16, k, kp, ad); + g.a_row0_hits++; + } else { + if (a32) omega_matmul_frag_pack_a_f32(a32, m, k, mp, kp, ad); + else omega_matmul_frag_pack_a(a16, m, k, mp, kp, ad); + g.a_row0_ready = one_row; g.a_row0_kp = kp; g.a_row0_cpu = ad; + } float *cd = g.c_buf.mem.cpu; for (size_t i = 0; i < (size_t)m * np; i++) cd[i] = POISON_F32; __asm__ volatile("dsb sy" ::: "memory"); diff --git a/src/omega_gpu_matmul_api.h b/src/omega_gpu_matmul_api.h index af4f5fb09..a8fd1a614 100644 --- a/src/omega_gpu_matmul_api.h +++ b/src/omega_gpu_matmul_api.h @@ -164,6 +164,8 @@ void omega_gpu_matmul_test_set_mutant(int on); /* Test only: force the fragment kernel unroll (1, 2, 4 or 8; it must divide kp / 16 or the call * fails CODEGEN_FAIL); 0 restores the automatic choice. Clears the kernel cache. */ void omega_gpu_matmul_test_set_unroll(uint32_t unroll); +/* Test hook: one-row calls so far that wrote only row 0 into a buffer already zero in rows 1..15. */ +uint64_t omega_gpu_matmul_test_a_row0_hits(void); #ifdef __cplusplus } diff --git a/tests/gpu_matmul_api_test.c b/tests/gpu_matmul_api_test.c index 97d34bd8a..c987a60c1 100644 --- a/tests/gpu_matmul_api_test.c +++ b/tests/gpu_matmul_api_test.c @@ -65,6 +65,27 @@ static double e2e_max_rel(uint32_t m, uint32_t k, uint32_t n, const float *a, co * 2t+8,2t+9; a6,a7 row g+8. B: b0,b1 rows 2t,2t+1 col g; b2,b3 rows 2t+8,2t+9; g = lane>>2, * t = lane&3), independently of the packers: pack then unpack must give the matrix back. */ static void frag_host_checks(void) { + /* row-0 pack (one-row decode call): a full one-row pack of A2 equals a full pack of A1 followed by the + * row-0 pack of A2, bf16 and f32 inputs, including a width with padding columns */ + { + const uint32_t ks[] = { 4096u, 40u }; + for (size_t i = 0; i < 2; i++) { + const uint32_t k = ks[i], kp = (k + 15u) / 16u * 16u; + float *a1 = malloc(k * sizeof *a1), *a2 = malloc(k * sizeof *a2); + uint16_t *b1 = malloc(k * 2), *b2 = malloc(k * 2), *full = malloc((size_t)16 * kp * 2), *inc = malloc((size_t)16 * kp * 2); + uint32_t seed = 77u + k; + fill(a1, k, &seed); fill(a2, k, &seed); + for (uint32_t j = 0; j < k; j++) { b1[j] = omega_fp32_to_bf16(a1[j]); b2[j] = omega_fp32_to_bf16(a2[j]); } + omega_matmul_frag_pack_a(b2, 1, k, 16, kp, full); + omega_matmul_frag_pack_a(b1, 1, k, 16, kp, inc); + omega_matmul_frag_pack_a_row0(b2, k, kp, inc); + CHECK(memcmp(full, inc, (size_t)16 * kp * 2) == 0, "row-0 pack bf16 k=%u equals the full one-row pack", k); + omega_matmul_frag_pack_a_f32(a1, 1, k, 16, kp, inc); + omega_matmul_frag_pack_a_row0_f32(a2, k, kp, inc); + CHECK(memcmp(full, inc, (size_t)16 * kp * 2) == 0, "row-0 pack f32 k=%u equals the full one-row pack", k); + free(a1); free(a2); free(b1); free(b2); free(full); free(inc); + } + } CHECK(omega_blackwell_verify_codegen_fixtures_fragmm() == 0, "fragment matmul golden words (rc %d)", omega_blackwell_verify_codegen_fixtures_fragmm()); struct { uint32_t m, k, n; } sh[] = { {1, 16, 8}, {1, 40, 13}, {17, 48, 24}, {33, 2560, 72}, {5, 160, 1000} }; for (size_t s = 0; s < sizeof sh / sizeof sh[0]; s++) { @@ -253,6 +274,37 @@ static void host_only(void) { omega_gpu_matmul_cache_clear(); } +/* One-row calls reuse the zero padding rows of the activation buffer: a second one-row call of the same + * width writes row 0 only (hook counts it) and still matches the oracle with a different A; a wider + * call in between, or another width, forces a full pack again. */ +static void one_row_repack_checks(void) { + const uint32_t k = 4096, n = 256; + float *a = malloc((size_t)32 * k * sizeof *a), *b = malloc((size_t)k * n * sizeof *b), *c = malloc((size_t)32 * n * sizeof *c); + uint32_t seed = 0x10c0u; + fill(a, (size_t)32 * k, &seed); fill(b, (size_t)k * n, &seed); + OmegaGpuMatmulInfo info; + const uint64_t h0 = omega_gpu_matmul_test_a_row0_hits(); + int rc1 = omega_gpu_matmul_f32(1, k, n, a, b, c, &info); + CHECK(rc1 == OMEGA_GPU_MATMUL_OK && info.parity_verified, "one-row call 1 (full pack) rc=%s parity=%d", omega_gpu_matmul_rc_name(rc1), info.parity_verified); + const uint64_t h1 = omega_gpu_matmul_test_a_row0_hits(); + int rc2 = omega_gpu_matmul_f32(1, k, n, a + k, b, c, &info); + const uint64_t h2 = omega_gpu_matmul_test_a_row0_hits(); + CHECK(rc2 == OMEGA_GPU_MATMUL_OK && info.parity_verified, "one-row call 2 (row 0 only, new A) rc=%s parity=%d", omega_gpu_matmul_rc_name(rc2), info.parity_verified); + CHECK(h1 == h0 && h2 == h1 + 1, "row-0 path taken on the second one-row call only (hits %" PRIu64 " %" PRIu64 " %" PRIu64 ")", h0, h1, h2); + int rc3 = omega_gpu_matmul_f32(32, k, n, a, b, c, &info); + CHECK(rc3 == OMEGA_GPU_MATMUL_OK && info.parity_verified, "32-row call between rc=%s parity=%d", omega_gpu_matmul_rc_name(rc3), info.parity_verified); + int rc4 = omega_gpu_matmul_f32(1, k, n, a + 2 * (size_t)k, b, c, &info); + const uint64_t h4 = omega_gpu_matmul_test_a_row0_hits(); + CHECK(rc4 == OMEGA_GPU_MATMUL_OK && info.parity_verified && h4 == h2, "one-row call after a wider call packs in full (hits %" PRIu64 ")", h4); + int rc5 = omega_gpu_matmul_f32(1, k, n, a + 3 * (size_t)k, b, c, &info); + const uint64_t h5 = omega_gpu_matmul_test_a_row0_hits(); + CHECK(rc5 == OMEGA_GPU_MATMUL_OK && info.parity_verified && h5 == h4 + 1, "one-row call after a one-row call writes row 0 only (hits %" PRIu64 ")", h5); + int rc6 = omega_gpu_matmul_f32(1, 64, n, a, b, c, &info); + const uint64_t h6 = omega_gpu_matmul_test_a_row0_hits(); + CHECK(rc6 == OMEGA_GPU_MATMUL_OK && info.parity_verified && h6 == h5, "one-row call of another width packs in full (hits %" PRIu64 ")", h6); + free(a); free(b); free(c); +} + static int chip_sweep(const char *shapes, const char *out_path, int mutant) { FILE *out = out_path ? fopen(out_path, "w") : NULL; if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; } @@ -412,6 +464,7 @@ int main(int argc, char **argv) { else if (do_timing) rc = timing(shapes ? shapes : TIMING_SHAPES, out_path); else if (mutant) { omega_gpu_matmul_test_set_mutant(1); rc = chip_sweep(shapes ? shapes : MUTANT_SHAPES, out_path, 1); omega_gpu_matmul_test_set_mutant(0); } else rc = chip_sweep(shapes ? shapes : DEFAULT_SHAPES, out_path, 0); + if (!host && !do_timing && !mutant && !omega_gpu_matmul_is_blocked()) one_row_repack_checks(); if (!omega_gpu_matmul_is_blocked()) omega_gpu_device_close(); if (rc) return 2; printf("%s: %d checks, %d failed%s\n", g_failed ? "FAIL" : "PASS", g_checks, g_failed, mutant ? (g_failed ? " (mutant SURVIVED)" : " (mutant killed)") : ""); From f551027b0796d51d8b68f2fea3ad66d367c30a3e Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Mon, 12 Oct 2026 04:36:48 -0500 Subject: [PATCH 2/2] crumb: compile for 32a2a62a Co-Authored-By: Claude Fable 5.1 --- .crumb | 18 +++++++++--------- src/.crumb | 8 ++++---- tests/.crumb | 8 ++++---- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/.crumb b/.crumb index ce1ff603a..f7901259d 100644 --- a/.crumb +++ b/.crumb @@ -87,14 +87,14 @@ "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "a0886818e3da6cdc8deeb7830808467b75798b7f3a538204557da7998b52e564", - "last_commit": "45eef1d1", + "digest": "7f1978d218d30126cc74ab22cfd18fe2283632fec4ba3518685dc3e5121c03e0", + "last_commit": "32a2a62a", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "57ed2a1bfe7a7a524224ed3b38f88a32b0bda81322a3f1267fdf9da2db161d45", - "last_commit": "89913b0b", + "digest": "ba0c62a71a9a3f069b7afd3ea71d0900f34321f2e5b0f55bad347c9487879fef", + "last_commit": "32a2a62a", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, @@ -105,15 +105,15 @@ "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:5f18309aa63bf3ef664c738098ca2d7b13b088fd79ee1ee2aebc66552213fb4a", - "compiled_at": "2026-10-12T09:00:14Z", + "children_root": "sha256:b68cc59a22835e3342cddb4d6607df0352f62a5d17dc5b2fc19177c306dc325f", + "compiled_at": "2026-10-12T09:36:47Z", "compiler": "crumb-compile/1", - "digest": "sha256:fb21f51a731200840f038c9f2da76a3f2cf8001f7d8d6c7d851968eacc05f2e6", + "digest": "sha256:6bfafa6e43ae0a9cf9fe7a5f69a3f62cd94203c9651ecbbbc7900c50d7e96317", "evidence_files": 7245, "evidence_root": "sha256:035beef5cdb99afaefd1743640f5e7eeaaed13c4a5ec6c8b4b6a4cb0c528407a", "files": 9734, - "generated_at_commit": "45eef1d18b4cd6cf5daf3f02a329a6e4d5b6fd23", - "source_tree": "sha256:a76f7787d26bcef60783f61ba0d87844a6d9872dcd2d2bb8bfa0010e746a510e" + "generated_at_commit": "32a2a62a7b2d306884cd58743bfa51917dff3487", + "source_tree": "sha256:8a5db9e1d0a438a9157e8dc683ed598af1f24528eca38df120281da4ab943ea4" }, "owner_lane": "", "provenance": { diff --git a/src/.crumb b/src/.crumb index 428e0d12a..35d6d1e0c 100644 --- a/src/.crumb +++ b/src/.crumb @@ -176,14 +176,14 @@ } ], "children_root": "sha256:2f64d32b2351272f4de10e1143efdb74dbc3cae56b1f0a3039ca9edcfebf6e8c", - "compiled_at": "2026-10-12T09:00:14Z", + "compiled_at": "2026-10-12T09:36:47Z", "compiler": "crumb-compile/1", - "digest": "sha256:a0886818e3da6cdc8deeb7830808467b75798b7f3a538204557da7998b52e564", + "digest": "sha256:7f1978d218d30126cc74ab22cfd18fe2283632fec4ba3518685dc3e5121c03e0", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 451, - "generated_at_commit": "45eef1d18b4cd6cf5daf3f02a329a6e4d5b6fd23", - "source_tree": "sha256:ce50af03cd4d5f414ee4deca3c874adf424d31a7c206620a1ed7f7fe4a0e7857" + "generated_at_commit": "32a2a62a7b2d306884cd58743bfa51917dff3487", + "source_tree": "sha256:dc5a78fedde575600c2669e18b3319b95a2a7df44e50ef34203aa3340a5e9216" }, "owner_lane": "", "provenance": { diff --git a/tests/.crumb b/tests/.crumb index 8778f120d..83acdba42 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -290,14 +290,14 @@ } ], "children_root": "sha256:9597c068d5bf4eb27f1d91842813a2b10269c2ccae421dceefb4308030ec0fc2", - "compiled_at": "2026-10-12T08:54:18Z", + "compiled_at": "2026-10-12T09:36:47Z", "compiler": "crumb-compile/1", - "digest": "sha256:57ed2a1bfe7a7a524224ed3b38f88a32b0bda81322a3f1267fdf9da2db161d45", + "digest": "sha256:ba0c62a71a9a3f069b7afd3ea71d0900f34321f2e5b0f55bad347c9487879fef", "evidence_files": 16, "evidence_root": "sha256:96d08097822ad95f4e562c19e0ba5df5a035774c48632f1832912c40a27bd27a", "files": 1024, - "generated_at_commit": "89913b0b89f40dd3934e18eecc79708e32508bb2", - "source_tree": "sha256:fe3786166e0e61bc59ecd143bb211b255c9755ee21fae75145bd253a6268de48" + "generated_at_commit": "32a2a62a7b2d306884cd58743bfa51917dff3487", + "source_tree": "sha256:411a0a7092a3167a5049fd52829df6d07bec1c0214c27b4e279757b709cfc726" }, "owner_lane": "", "provenance": {