From d12d012a763f44ebb367431497eaf727256b25a8 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Sun, 4 Oct 2026 14:08:18 -0500 Subject: [PATCH 1/2] [host PASS][sim PASS] attention: head-wiring mutants Q_ROW + OUT_ROW, robust mutant enumeration Two new test-only kernel mutants in OmegaGpuAttnMutant model head-wiring failures the battery did not cover: Q_ROW loads q from query-head row h^1 (kv head and output row stay h), OUT_ROW stores the correct result for head h in flattened output row h^1. The enum now ends in OMEGA_GPU_ATTN_MUTANT_COUNT and omega_gpu_attention_mutant_name replaces the test's private table; host-only, simulator, chip battery and the JSON receipt all enumerate 1..COUNT-1 (a mutant added without a battery entry fails the gate). Host-only also checks every mutant digest differs pairwise. New MHA case (16q/16kv, ctx 300) shows Q_ROW and OUT_ROW are observably distinct wrong kernels. Host: --host-only 85 checks PASS, --sim 91 checks PASS, all six mutants caught (NO_MAX 1/1, KV_HEAD 9/9, SLOT 7/7, NO_RESCALE 4/4, Q_ROW 6/6, OUT_ROW 6/6). Chip: not run in this commit (queued through the heavy lane). Co-authored-by: Drake Stapleton Co-Authored-By: Claude Fable 5.1 --- .crumb | 26 ++++++------ docs/.crumb | 14 +++--- docs/numeric/.crumb | 8 ++-- docs/numeric/FB1_CUT5_ATTENTION.md | 8 ++++ src/.crumb | 8 ++-- src/omega_gpu_attention_api.c | 26 ++++++++++-- src/omega_gpu_attention_api.h | 10 ++++- tests/.crumb | 8 ++-- tests/gpu_attention_test.c | 68 +++++++++++++++++++++++------- tools/.crumb | 10 ++--- tools/run_gpu_attention_chip.sh | 2 +- 11 files changed, 131 insertions(+), 57 deletions(-) diff --git a/.crumb b/.crumb index bdf2bded7..2603dca3e 100644 --- a/.crumb +++ b/.crumb @@ -51,8 +51,8 @@ "purpose": "Directory calibration. Purpose not yet described by a human." }, { - "digest": "28840d48abe117c5e29a9fc7f352475deee0c546157edfcb09ebe61a1d364da1", - "last_commit": "cff342b", + "digest": "e156df1e52268bb3346ded9bf7afcb426960e643a8889e1d76865a83a3a5a642", + "last_commit": "55d05d6", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, @@ -64,7 +64,7 @@ }, { "digest": "25dc918988768c936cdd41410a68c18df3c9acf7cd129812bc030c6a070cc67c", - "last_commit": "cff342b", + "last_commit": "55d05d6", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." }, @@ -87,33 +87,33 @@ "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "839e2dd4f0aa249d7b552e9d3eafdf3ae94f13a4a4c17c3bed1ce603a07659d8", - "last_commit": "cff342b", + "digest": "fab4c6952b54567c65c8bd843e46bcd238c688940ed1d1830af2b584b057781b", + "last_commit": "55d05d6", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "7f8951ba438340bc36223d5722c1055efd8fdd01efe3a43bcf8f2e61ce68dea7", - "last_commit": "cff342b", + "digest": "9eb57c43d48ae70bce44757cbf075c38a03a090e0da529260d88e197e602c767", + "last_commit": "55d05d6", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { - "digest": "49be2149359521a3a0f87c12aa31e565c6575309bd099aff8daa216b25052ed6", + "digest": "9e08561d488a03e636cb8622b6667636230ac21cce1711e0ff6a45cf7f5c5761", "last_commit": "00f54b6", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:f5cd48815a1e17f58c9a56231edbc7e6d4cb19045afc1f03cba9a5c92b20d6f4", - "compiled_at": "2026-10-04T18:54:29Z", + "children_root": "sha256:59067587092f050563a42fe041880c24f8c92e7c0c09bc01e5885b712222826b", + "compiled_at": "2026-10-04T19:57:19Z", "compiler": "crumb-compile/1", - "digest": "sha256:16e57dc9bcda740ae51c4709c0fd61944bf2690f670206423904038ad95a7bb7", + "digest": "sha256:55e53e69b40dc3fd8b30b97f8a493fda44b457f717f3048e3a37eb9c163589c4", "evidence_files": 1289, "evidence_root": "sha256:748eb7ce0bd377815585505582eb76663350e35af3d876a488897ceea120ba3f", "files": 3049, - "generated_at_commit": "cff342b400ec0bc6b2b68e1004212872d490bb07", - "source_tree": "sha256:63e49ae9974a0c96e66aef44ec14d08e19354be518c670f04e2b6d1dab924b6f" + "generated_at_commit": "55d05d6a53e55dddc06f5eb689bee10c8830f52d", + "source_tree": "sha256:a1f0fb11e0348083ced716d80bdce7f58050b2f26cb1c146206fb4244f1af0ed" }, "owner_lane": "", "provenance": { diff --git a/docs/.crumb b/docs/.crumb index 1b27715f9..a5012d5ce 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -70,7 +70,7 @@ }, { "digest": "e9baaaeec04be41a57c4a2d6a540b01a682c70d4db96cb4af4e1a9912a133aa9", - "last_commit": "00f54b6", + "last_commit": "55d05d6", "name": "dual", "purpose": "DUAL (ARCH-0031) documents in omega: applicability experiment method and findings (APPLICABILITY" }, @@ -81,7 +81,7 @@ "purpose": "Directory docs/estimation. Purpose not yet described by a human." }, { - "digest": "042face6780c6bd762a7e41b10d62c1b3d8f2828e20eaa391ecb1f691cda201a", + "digest": "4272674cbb644368ae8fef74a5775144b11dbd3aac3eb25f073bd898f0268329", "last_commit": "bceec7b", "name": "numeric", "purpose": "Directory docs/numeric. Purpose not yet described by a human." @@ -117,15 +117,15 @@ "purpose": "Directory docs/turing. Purpose not yet described by a human." } ], - "children_root": "sha256:7010ef7e3e982e3828bfeb2ff1a1fe5c8cfeceb6aef820c34c175fe1d0208734", - "compiled_at": "2026-10-04T18:54:28Z", + "children_root": "sha256:c86da32ec6d7b7bc1f8db4bdf024fadf35ae5d588cbadf733db2292beb32691f", + "compiled_at": "2026-10-04T19:57:19Z", "compiler": "crumb-compile/1", - "digest": "sha256:28840d48abe117c5e29a9fc7f352475deee0c546157edfcb09ebe61a1d364da1", + "digest": "sha256:e156df1e52268bb3346ded9bf7afcb426960e643a8889e1d76865a83a3a5a642", "evidence_files": 26, "evidence_root": "sha256:f58236184eadc5f8c8f95499127a9f43c2f4dc702ddca267853d8c3dee0690f1", "files": 100, - "generated_at_commit": "500a510f818ce4b973f4862c96e9361b9c450890", - "source_tree": "sha256:769aaad946a229b703b2def73085c781284fb13666223c34d2fdb16020a3f11b" + "generated_at_commit": "55d05d6a53e55dddc06f5eb689bee10c8830f52d", + "source_tree": "sha256:a6ae94eae6a9333187ad07cf8161cb7261242e9ad567e32d80602224c4556887" }, "owner_lane": "", "provenance": { diff --git a/docs/numeric/.crumb b/docs/numeric/.crumb index dd14119ed..211f49913 100644 --- a/docs/numeric/.crumb +++ b/docs/numeric/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-04T13:47:05Z", + "compiled_at": "2026-10-04T19:07:57Z", "compiler": "crumb-compile/1", - "digest": "sha256:042face6780c6bd762a7e41b10d62c1b3d8f2828e20eaa391ecb1f691cda201a", + "digest": "sha256:4272674cbb644368ae8fef74a5775144b11dbd3aac3eb25f073bd898f0268329", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 10, - "generated_at_commit": "834ee93e4314a42c1127a2fcda8b29cf3b511245", - "source_tree": "sha256:fb1c6be4cdf35159ea84a5e576a86dfb2121ad73df861aa4af5f80db1ef2b0d0" + "generated_at_commit": "3a3144d4a2cbc4fd55a60da85fe94bd7ed5a9a39", + "source_tree": "sha256:804ae75ce73ff6bc90b9a9a820a86ec37274225b1528c09617df150ad4d5ec6c" }, "owner_lane": "", "provenance": { diff --git a/docs/numeric/FB1_CUT5_ATTENTION.md b/docs/numeric/FB1_CUT5_ATTENTION.md index c535f727b..1bd32ea20 100644 --- a/docs/numeric/FB1_CUT5_ATTENTION.md +++ b/docs/numeric/FB1_CUT5_ATTENTION.md @@ -76,6 +76,14 @@ kernel itself walks the (renumbered) block table. | KV_HEAD | reads kv head `(h / ratio) ^ 1` | every context length including 1 | | SLOT | K read from slot `(t + 1) % block_size`, V from slot t | every context >= 2 (with one token the softmax weight is 1 whatever K says). Shifting both K and V would only permute matched pairs inside a full block, which softmax cannot see, so the mutant is K-only | | NO_RESCALE | accumulator not multiplied by alpha when the max moves | context > 64 (more than one chunk) | +| Q_ROW | q loaded from query-head row `h ^ 1` (kv head and output row stay `h`): a wrong query-head-row address | every context (heads h and h^1 come out swapped); needs num_q_heads even | +| OUT_ROW | the correct result for head `h` stored in flattened output row `h ^ 1`: a wrong output-head slot (merge order) | every context; needs num_q_heads even | + +The enum ends in `OMEGA_GPU_ATTN_MUTANT_COUNT`; every battery (host-only digests, simulator, +chip, JSON `mutants` summary) enumerates `1 .. COUNT-1` and fails if any mutant never ran or +was ever missed, so a mutant added to the enum without a battery entry fails the gate. Host-only +also checks that every mutant's kernel digest differs from the baseline and from every other +mutant's. Names come from `omega_gpu_attention_mutant_name` (table checked against COUNT). Negative control: the comparison must reject the oracle with kv heads swapped. Repeat run on a cache hit must be bit-identical. diff --git a/src/.crumb b/src/.crumb index 541f6f71a..ab1ce0cd5 100644 --- a/src/.crumb +++ b/src/.crumb @@ -170,14 +170,14 @@ } ], "children_root": "sha256:607467f8ea6e425bf33f7a0df65a411055a569e302b8127ab84822a0b4559109", - "compiled_at": "2026-10-04T18:54:28Z", + "compiled_at": "2026-10-04T19:57:19Z", "compiler": "crumb-compile/1", - "digest": "sha256:839e2dd4f0aa249d7b552e9d3eafdf3ae94f13a4a4c17c3bed1ce603a07659d8", + "digest": "sha256:fab4c6952b54567c65c8bd843e46bcd238c688940ed1d1830af2b584b057781b", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 403, - "generated_at_commit": "500a510f818ce4b973f4862c96e9361b9c450890", - "source_tree": "sha256:e98bb9c0ddc0aa8281597e44376154c18782109c91224cc49e83aa7a59cc1834" + "generated_at_commit": "55d05d6a53e55dddc06f5eb689bee10c8830f52d", + "source_tree": "sha256:bf53ea9b7e9ea01435d1b4889e9fae8f7dadddbbe66137845af1c377c0e6b667" }, "owner_lane": "", "provenance": { diff --git a/src/omega_gpu_attention_api.c b/src/omega_gpu_attention_api.c index 53884ae44..d680009ea 100644 --- a/src/omega_gpu_attention_api.c +++ b/src/omega_gpu_attention_api.c @@ -197,15 +197,26 @@ static void gen_attention(Em *e, const KSpec *k) { movi(e, c1, 1); movi(e, c4, 4); movi(e, c32, 32); movi(e, c64, 64); movi(e, c256, SH_S); movi(e, c512, SH_O); movi(e, c768, SH_R); movi(e, c896, SH_R + 128); movrz(e, zero); - /* q[tid] -> shared; out address kept for the end */ + /* q[tid] -> shared; out address kept for the end. Row = seq * num_q_heads + head for + * both q and out (flattened head-major, the oracle's layout). The head-wiring mutants + * swap the row of head h with h ^ 1 on one side only. */ int tid4 = V(e), qrow = V(e), qoff = V(e), qaddr = V64(e), qv = V(e), oaddr = V64(e); + int hq = h, ho = h; + if (k->mutant == OMEGA_GPU_ATTN_MUTANT_Q_ROW) { hq = V(e); lop3xor(e, hq, h, c1); } + if (k->mutant == OMEGA_GPU_ATTN_MUTANT_OUT_ROW) { ho = V(e); lop3xor(e, ho, h, c1); } imadi(e, tid4, tid, 4, NOREG); - imadr(e, qrow, seq, p_nqh, h); + imadr(e, qrow, seq, p_nqh, hq); imadi(e, qoff, qrow, HD * 4, tid4); wide(e, qaddr, qoff, 1, A); ldg(e, qv, qaddr, udesc); sts32(e, tid4, qv); /* SH_Q + tid*4 */ - wide(e, oaddr, qoff, 1, C); + if (ho == hq) wide(e, oaddr, qoff, 1, C); + else { + int orow = V(e), ooff = V(e); + imadr(e, orow, seq, p_nqh, ho); + imadi(e, ooff, orow, HD * 4, tid4); + wide(e, oaddr, ooff, 1, C); + } /* kv head offset, this sequence's table row and context length */ int kvh = V(e), kvoff = V(e), t1 = V(e), tab = V64(e), ca = V64(e), ctx = V(e), vb = V64(e); @@ -745,6 +756,15 @@ int omega_gpu_paged_attention_batch_bf16(const float *q, const void *pool, const return rc; } +static const char *const k_mutant_names[OMEGA_GPU_ATTN_MUTANT_COUNT] = { + "none", "NO_MAX", "KV_HEAD", "SLOT", "NO_RESCALE", "Q_ROW", "OUT_ROW" +}; +_Static_assert(sizeof k_mutant_names / sizeof k_mutant_names[0] == OMEGA_GPU_ATTN_MUTANT_COUNT, "mutant name table"); + +const char *omega_gpu_attention_mutant_name(OmegaGpuAttnMutant m) { + return (m >= 0 && m < OMEGA_GPU_ATTN_MUTANT_COUNT && k_mutant_names[m]) ? k_mutant_names[m] : "?"; +} + const char *omega_gpu_attention_rc_name(int rc) { switch (rc) { case OMEGA_GPU_ATTN_OK: return "OK"; diff --git a/src/omega_gpu_attention_api.h b/src/omega_gpu_attention_api.h index ab47354f4..d498eef98 100644 --- a/src/omega_gpu_attention_api.h +++ b/src/omega_gpu_attention_api.h @@ -136,9 +136,17 @@ typedef enum { OMEGA_GPU_ATTN_MUTANT_NO_MAX, /* softmax without the running-max subtraction (overflows for large scores) */ OMEGA_GPU_ATTN_MUTANT_KV_HEAD, /* reads kv head (h / ratio) ^ 1 */ OMEGA_GPU_ATTN_MUTANT_SLOT, /* K read from slot (t + 1) % block_size while V keeps slot t (K/V mismatch) */ - OMEGA_GPU_ATTN_MUTANT_NO_RESCALE /* the accumulator is not rescaled when the max moves between chunks */ + OMEGA_GPU_ATTN_MUTANT_NO_RESCALE, /* the accumulator is not rescaled when the max moves between chunks */ + /* Head-wiring mutants (attention hardening cut, 2026-10-04). Both swap the rows of + * query heads h and h ^ 1, so they need num_q_heads even (>= 2); never run them on a + * one-head shape (row h ^ 1 of the last sequence would be past the buffer). */ + OMEGA_GPU_ATTN_MUTANT_Q_ROW, /* q loaded from query-head row h ^ 1 (kv head and output row stay h) */ + OMEGA_GPU_ATTN_MUTANT_OUT_ROW, /* the correct result for head h stored in flattened output row h ^ 1 */ + OMEGA_GPU_ATTN_MUTANT_COUNT /* one past the last mutant: every battery enumerates 1 .. COUNT-1 */ } OmegaGpuAttnMutant; void omega_gpu_attention_test_set_mutant(OmegaGpuAttnMutant m); +/* "none", "NO_MAX", ... ; "?" past COUNT. The table is checked against COUNT at compile time. */ +const char *omega_gpu_attention_mutant_name(OmegaGpuAttnMutant m); /* Generate a kernel without launching (host tests, nvdisasm listings). * log2_block_size: shift for token -> block; log2_gqa: shift for query head -> kv head. */ diff --git a/tests/.crumb b/tests/.crumb index 5a2e5c5c1..0843844a9 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -284,14 +284,14 @@ } ], "children_root": "sha256:0a4e348cf355e1fa5924f9c76a460d1d49bc285687b134365ef0fb9dab4f0855", - "compiled_at": "2026-10-04T18:54:28Z", + "compiled_at": "2026-10-04T19:57:19Z", "compiler": "crumb-compile/1", - "digest": "sha256:7f8951ba438340bc36223d5722c1055efd8fdd01efe3a43bcf8f2e61ce68dea7", + "digest": "sha256:9eb57c43d48ae70bce44757cbf075c38a03a090e0da529260d88e197e602c767", "evidence_files": 11, "evidence_root": "sha256:d5c1bceb4d57ef0eb42797c58801b5926509711614c3e38bb7105b6efa3942f2", "files": 790, - "generated_at_commit": "500a510f818ce4b973f4862c96e9361b9c450890", - "source_tree": "sha256:9e8f9ed5339ccf473907a174516a6af9de8ed850a96934abd3c7474c4bf136a6" + "generated_at_commit": "55d05d6a53e55dddc06f5eb689bee10c8830f52d", + "source_tree": "sha256:c3ad53ad3ba707657f944d28dcf4aa052510d627d52b5318e31172e7f13cc243" }, "owner_lane": "", "provenance": { diff --git a/tests/gpu_attention_test.c b/tests/gpu_attention_test.c index 82d7de7e9..03a64a470 100644 --- a/tests/gpu_attention_test.c +++ b/tests/gpu_attention_test.c @@ -5,7 +5,8 @@ * ./gpu_attention_test --timing [--out r.json] cut 4b gate: 1 query x 32 heads x 2048 ctx, 100 calls * through the persistent session, median under 3 ms * ./gpu_attention_test --out receipt.json chip gate: every shape against the host oracle, the - * mutants (deliberately wrong kernels) which the same + * mutants (deliberately wrong kernels, all of + * 1 .. OMEGA_GPU_ATTN_MUTANT_COUNT-1) which the same * check must catch, a negative control, timings * * Oracles re-state aien-sovereign-core crates/aien-inference-abi/src/backend.rs (main 8b5caed) in C: @@ -189,18 +190,26 @@ static void host_only(void) { int rc = omega_gpu_attention_codegen(f32 != 0, f32 ? 12 : 4, 3, &k); CHECK(rc == OMEGA_GPU_ATTN_OK && k.code_size > 0 && k.gpr_count <= 64, "%s kernel generated (rc %s, %zu insns, %u gprs)", name, omega_gpu_attention_rc_name(rc), k.insn_count, k.gpr_count); printf("codegen %s: %zu insns, %u gprs, %u ugprs\n", name, k.insn_count, k.gpr_count, k.uniform_gpr_count); - uint8_t digest[32]; memcpy(digest, k.code_digest, 32); + /* every mutant (1 .. COUNT-1) is a distinct kernel: different from the baseline and from + * every other mutant, named, and inside the register budget */ + uint8_t digests[OMEGA_GPU_ATTN_MUTANT_COUNT][32]; memcpy(digests[0], k.code_digest, 32); free(k.code); - for (int m = 1; m <= 4; m++) { + for (int m = 1; m < OMEGA_GPU_ATTN_MUTANT_COUNT; m++) { omega_gpu_attention_test_set_mutant((OmegaGpuAttnMutant)m); memset(&k, 0, sizeof k); - CHECK(omega_gpu_attention_codegen(f32 != 0, f32 ? 12 : 4, 3, &k) == OMEGA_GPU_ATTN_OK && memcmp(digest, k.code_digest, 32) != 0, "%s mutant %d kernel differs", name, m); + int mrc = omega_gpu_attention_codegen(f32 != 0, f32 ? 12 : 4, 3, &k); + CHECK(mrc == OMEGA_GPU_ATTN_OK && k.gpr_count <= 64, "%s mutant %s generated (rc %s, %u gprs)", name, omega_gpu_attention_mutant_name((OmegaGpuAttnMutant)m), omega_gpu_attention_rc_name(mrc), k.gpr_count); + CHECK(strcmp(omega_gpu_attention_mutant_name((OmegaGpuAttnMutant)m), "?") != 0, "mutant %d has a name", m); + memcpy(digests[m], k.code_digest, 32); free(k.code); omega_gpu_attention_test_set_mutant(OMEGA_GPU_ATTN_MUTANT_NONE); + for (int o = 0; o < m; o++) + CHECK(memcmp(digests[o], digests[m], 32) != 0, "%s mutant %s kernel differs from %s", name, omega_gpu_attention_mutant_name((OmegaGpuAttnMutant)m), omega_gpu_attention_mutant_name((OmegaGpuAttnMutant)o)); } + CHECK(strcmp(omega_gpu_attention_mutant_name(OMEGA_GPU_ATTN_MUTANT_COUNT), "?") == 0, "COUNT has no name"); /* a different shape key is a different kernel */ memset(&k, 0, sizeof k); - CHECK(omega_gpu_attention_codegen(f32 != 0, f32 ? 12 : 4, 2, &k) == OMEGA_GPU_ATTN_OK && memcmp(digest, k.code_digest, 32) != 0, "%s log2gqa 2 kernel differs from log2gqa 3", name); + CHECK(omega_gpu_attention_codegen(f32 != 0, f32 ? 12 : 4, 2, &k) == OMEGA_GPU_ATTN_OK && memcmp(digests[0], k.code_digest, 32) != 0, "%s log2gqa 2 kernel differs from log2gqa 3", name); free(k.code); CHECK(nvdisasm_check(f32 != 0, name, f32 ? must_f32 : must_bf16, f32 ? 11 : 14) == 0, "%s nvdisasm listing decodes and shows the expected instructions", name); } @@ -208,9 +217,9 @@ static void host_only(void) { /* ------------------------------------------------------------- chip gate */ typedef struct { const char *name; size_t n; int rc; size_t bad; double worst; int pass; uint64_t chip_ns, call_ns; uint32_t calls, unwritten; - int mutants_run, mutants_caught; char mutant_note[160]; } Case; + int mutants_run, mutants_caught; char mutant_note[400]; } Case; static Case g_cases[32]; static int g_ncases; -static int g_mutant_caught[5], g_mutant_run[5]; +static int g_mutant_caught[OMEGA_GPU_ATTN_MUTANT_COUNT], g_mutant_run[OMEGA_GPU_ATTN_MUTANT_COUNT]; static void record(Case c, FILE *out) { g_cases[g_ncases++] = c; @@ -224,12 +233,12 @@ static void record(Case c, FILE *out) { c.mutants_run, c.mutants_caught, c.mutant_note); } -static const char *mutant_name(int m) { static const char *n[5] = { "none", "NO_MAX", "KV_HEAD", "SLOT", "NO_RESCALE" }; return n[m]; } +static const char *mutant_name(int m) { return omega_gpu_attention_mutant_name((OmegaGpuAttnMutant)m); } /* Runs the mutants listed in `ms` (terminated by 0) through `run` and counts the catches. */ typedef int (*RunFn)(void *ctx, float *got, OmegaGpuAttnInfo *info); static void run_mutants(Case *c, const int *ms, RunFn run, void *ctx, float *got, const float *want, size_t n, double rel, double abs_tol) { - size_t pos = 0; + size_t pos = strlen(c->mutant_note); /* appends: a case may call this more than once */ for (int i = 0; ms[i]; i++) { OmegaGpuAttnInfo info; double w; omega_gpu_attention_test_set_mutant((OmegaGpuAttnMutant)ms[i]); @@ -286,7 +295,7 @@ static int chip(const char *out_path) { size_t neg = compare(got, wrong, n, 2e-4, 2e-5, &w2); CHECK(neg > n / 2, "%s negative control: wrong expectation flagged (%zu of %zu)", name, neg, n); free(wrong); - static const int ms[3] = { OMEGA_GPU_ATTN_MUTANT_SLOT, OMEGA_GPU_ATTN_MUTANT_NO_RESCALE, 0 }; + static const int ms[5] = { OMEGA_GPU_ATTN_MUTANT_SLOT, OMEGA_GPU_ATTN_MUTANT_NO_RESCALE, OMEGA_GPU_ATTN_MUTANT_Q_ROW, OMEGA_GPU_ATTN_MUTANT_OUT_ROW, 0 }; run_mutants(&c, ms, run_gqa, &gc, got, want, n, 2e-4, 2e-5); } else if (ci == 1 && sh == 1) { static const int ms[2] = { OMEGA_GPU_ATTN_MUTANT_KV_HEAD, 0 }; @@ -310,6 +319,33 @@ static int chip(const char *out_path) { run_mutants(&c, ms, run_gqa, &gc, got, want, n, 1e-3, 1e-4); record(c, out); free(q); free(kc); free(vc); free(got); free(want); } + /* 2b. MHA (16q/16kv, ratio 1, ctx 300 = 4 full chunks + a partial one): the head-wiring + * mutants Q_ROW and OUT_ROW are observably different here (Q_ROW keeps kv head h, OUT_ROW + * lands on kv head h^1), unlike GQA where both swap the same pair of outputs. */ + { + uint32_t nqh = 16, nkv = 16, seq = 300; size_t n = (size_t)nqh * hd, nkvs = (size_t)seq * nkv * hd; + float *q = malloc(n * 4), *kc = malloc(nkvs * 4), *vc = malloc(nkvs * 4), *got = malloc(n * 4), *want = malloc(n * 4), *got_q = malloc(n * 4); + for (size_t i = 0; i < n; i++) q[i] = frand(&seed, -1.0f, 1.0f); + for (size_t i = 0; i < nkvs; i++) { kc[i] = frand(&seed, -1.0f, 1.0f); vc[i] = frand(&seed, -1.0f, 1.0f); } + oracle_gqa(q, kc, vc, seq, nqh, nkv, hd, want); + Case c = { .name = "gqa_f32_mha_16q16kv_ctx300", .n = n }; + OmegaGpuAttnInfo info; GqaCtx gc = { q, kc, vc, seq, nqh, nkv }; + c.rc = run_gqa(&gc, got, &info); + c.bad = compare(got, want, n, 2e-4, 2e-5, &c.worst); + c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.call_ns = info.call_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; + static const int ms_q[2] = { OMEGA_GPU_ATTN_MUTANT_Q_ROW, 0 }; + static const int ms_o[3] = { OMEGA_GPU_ATTN_MUTANT_OUT_ROW, OMEGA_GPU_ATTN_MUTANT_KV_HEAD, 0 }; + run_mutants(&c, ms_q, run_gqa, &gc, got, want, n, 2e-4, 2e-5); + memcpy(got_q, got, n * 4); /* Q_ROW output */ + run_mutants(&c, ms_o, run_gqa, &gc, got, want, n, 2e-4, 2e-5); + /* `got` now holds the KV_HEAD output; rerun OUT_ROW alone to compare it with Q_ROW */ + omega_gpu_attention_test_set_mutant(OMEGA_GPU_ATTN_MUTANT_OUT_ROW); + OmegaGpuAttnInfo i3; int rc3 = run_gqa(&gc, got, &i3); + omega_gpu_attention_test_set_mutant(OMEGA_GPU_ATTN_MUTANT_NONE); + double wq; size_t differ = compare(got, got_q, n, 2e-4, 2e-5, &wq); + CHECK(rc3 == OMEGA_GPU_ATTN_OK && differ > n / 2, "mha: Q_ROW and OUT_ROW outputs are distinct wrong answers (%zu of %zu differ)", differ, n); + record(c, out); free(q); free(kc); free(vc); free(got); free(want); free(got_q); + } /* 3. paged bf16 KV, block size 16, 2 layers (layer 1 under test), scattered block ids */ for (int sh = 0; sh < 2; sh++) for (int ci = 0; ci < 9; ci++) { uint32_t nqh = shapes[sh].nqh, nkv = shapes[sh].nkv, ctx = ctxs[ci], bs = 16, layers = 2, layer = 1; @@ -331,7 +367,7 @@ static int chip(const char *out_path) { c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.call_ns = info.call_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; if (ci == 3) { /* NO_MAX cannot overflow on unit-scale scores (softmax is shift invariant); it is caught by the large-score gqa case */ - static const int ms3[4] = { OMEGA_GPU_ATTN_MUTANT_KV_HEAD, OMEGA_GPU_ATTN_MUTANT_SLOT, OMEGA_GPU_ATTN_MUTANT_NO_RESCALE, 0 }; + static const int ms3[6] = { OMEGA_GPU_ATTN_MUTANT_KV_HEAD, OMEGA_GPU_ATTN_MUTANT_SLOT, OMEGA_GPU_ATTN_MUTANT_NO_RESCALE, OMEGA_GPU_ATTN_MUTANT_Q_ROW, OMEGA_GPU_ATTN_MUTANT_OUT_ROW, 0 }; run_mutants(&c, ms3, run_paged, &pc, got, want, n, 2e-4, 2e-5); } else if (ci == 1) { static const int ms[3] = { OMEGA_GPU_ATTN_MUTANT_KV_HEAD, OMEGA_GPU_ATTN_MUTANT_SLOT, 0 }; @@ -369,19 +405,21 @@ static int chip(const char *out_path) { c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.call_ns = info.call_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; int zeros = 1; for (size_t i = 0; i < (size_t)nqh * hd; i++) if (got[(size_t)nqh * hd + i] != 0.0f) zeros = 0; CHECK(zeros, "batch: the empty sequence is all zeros"); - static const int ms[3] = { OMEGA_GPU_ATTN_MUTANT_KV_HEAD, OMEGA_GPU_ATTN_MUTANT_SLOT, 0 }; + static const int ms[5] = { OMEGA_GPU_ATTN_MUTANT_KV_HEAD, OMEGA_GPU_ATTN_MUTANT_SLOT, OMEGA_GPU_ATTN_MUTANT_Q_ROW, OMEGA_GPU_ATTN_MUTANT_OUT_ROW, 0 }; run_mutants(&c, ms, run_batch, &bc, got, want, n, 2e-4, 2e-5); record(c, out); free(pool); free(tables); free(q); free(got); free(want); } - for (int m = 1; m <= 4; m++) { + /* every mutant the API knows (1 .. COUNT-1) ran at least once and was caught every time; + * a mutant added to the enum without a battery entry fails here */ + for (int m = 1; m < OMEGA_GPU_ATTN_MUTANT_COUNT; m++) { CHECK(g_mutant_run[m] > 0 && g_mutant_caught[m] == g_mutant_run[m], "mutant %s caught %d of %d runs", mutant_name(m), g_mutant_caught[m], g_mutant_run[m]); printf("mutant %s: caught %d of %d\n", mutant_name(m), g_mutant_caught[m], g_mutant_run[m]); } CHECK(omega_gpu_session_open_count() == (g_sim_mode ? 0u : 1u), "device opened %s for the battery (opens=%u)", g_sim_mode ? "never (simulator)" : "once", omega_gpu_session_open_count()); printf("device_opens=%u\n", omega_gpu_session_open_count()); if (out) { - fprintf(out, "],\"mutants\":{"); - for (int m = 1; m <= 4; m++) fprintf(out, "%s\"%s\":{\"run\":%d,\"caught\":%d}", m > 1 ? "," : "", mutant_name(m), g_mutant_run[m], g_mutant_caught[m]); + fprintf(out, "],\"mutant_count\":%d,\"mutants\":{", OMEGA_GPU_ATTN_MUTANT_COUNT - 1); + for (int m = 1; m < OMEGA_GPU_ATTN_MUTANT_COUNT; m++) fprintf(out, "%s\"%s\":{\"run\":%d,\"caught\":%d}", m > 1 ? "," : "", mutant_name(m), g_mutant_run[m], g_mutant_caught[m]); fprintf(out, "},\"device_opens\":%u,\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", omega_gpu_session_open_count(), g_checks, g_failed, g_failed == 0 ? "OMEGA_GPU_ATTENTION_PASS" : "OMEGA_GPU_ATTENTION_FAIL"); fclose(out); } diff --git a/tools/.crumb b/tools/.crumb index db7d883c2..49b41d808 100644 --- a/tools/.crumb +++ b/tools/.crumb @@ -38,7 +38,7 @@ }, { "digest": "64fe22fe57fab7e61e65cb7652e73dbde613c260cb8677b05d8b98a067231e83", - "last_commit": "", + "last_commit": "00f54b6", "name": "dual_experiment", "purpose": "Runner for the DUAL applicability experiment: runs the pre-registered campaign and writes result" }, @@ -56,14 +56,14 @@ } ], "children_root": "sha256:0920c282c31e3be9b4745fab718ceb60dbfe2ea90dda2108986f60fc2ac53e23", - "compiled_at": "2026-10-04T18:11:13Z", + "compiled_at": "2026-10-04T19:07:57Z", "compiler": "crumb-compile/1", - "digest": "sha256:49be2149359521a3a0f87c12aa31e565c6575309bd099aff8daa216b25052ed6", + "digest": "sha256:9e08561d488a03e636cb8622b6667636230ac21cce1711e0ff6a45cf7f5c5761", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 120, - "generated_at_commit": "cb7cc216147664549484fcca8e8ceadbd6f6cdbf", - "source_tree": "sha256:c4f08a23f53d0fee5dccc1efe157efdf0275735fbb68200875e19d63f0c5ada9" + "generated_at_commit": "3a3144d4a2cbc4fd55a60da85fe94bd7ed5a9a39", + "source_tree": "sha256:172b5a4666f700959782e204a6278eaf02307c5fe94fcf6f9441ae5bd8918412" }, "owner_lane": "", "provenance": { diff --git a/tools/run_gpu_attention_chip.sh b/tools/run_gpu_attention_chip.sh index 4e3753a98..ac2ba9bd0 100755 --- a/tools/run_gpu_attention_chip.sh +++ b/tools/run_gpu_attention_chip.sh @@ -3,7 +3,7 @@ # FB-1 cut 5 chip gate: gqa_attention (f32 KV) and paged_attention (+ batch, bf16 KV) # for the TinyLlama (32q/4kv) and Llama-3.2-1B (32q/8kv) head shapes at context # 1, 17, 256 and 2048, each against the host oracle (the ReferenceCpuBackend math), -# then against the mutant kernels (NO_MAX, KV_HEAD, SLOT, NO_RESCALE), with a +# then against every mutant kernel (NO_MAX, KV_HEAD, SLOT, NO_RESCALE, Q_ROW, OUT_ROW), with a # negative control and a cache-hit repeat. Sealed receipt folder # OUT_ROOT/FB1-CUT5-/ (default ~/workspace/evidence-out): # receipt.json machine-readable verdict per case run.log test output From f777036885f08dc3b01ed6d73988a0b758588990 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Sun, 4 Oct 2026 14:18:24 -0500 Subject: [PATCH 2/2] gpu attention: per-launch physical-block staging dedupe + head/kv-head sweep [host PASS][sim PASS] OM-2 of Drake's attention hardening cut (stacks on #273). - paged_core stages each physical KV block once per batched launch (slot keyed by block id); every block-table row referencing it points at the same staged slot. omega_gpu_attention_test_set_dedupe(false) restores per-reference staging as a negative control. - OmegaGpuAttnInfo appended (ABI order kept): kv_bytes_staged, kv_bytes_naive, kv_blocks_logical, kv_blocks_unique, q_bytes, tab_bytes, out_bytes, kv_source (OMEGA_GPU_ATTN_KV_STAGED only; no resident KV in this cut). - gpu_attention_test: shared-prefix cases (8q/2kv one launch: 19 unique of 49 logical, 155648 of 401408 bytes; 32q/8kv two launches: 29 of 49, 950272 of 1605632), legacy-vs-dedupe bit identity, counters in every receipt, and `--sweep --out ` (q heads 1..64 x every divisor x ctx 256/2048; non power-of-two ratios must return BAD_ARGS; schema OMEGA_GPU_ATTENTION_SWEEP_V1, no timing thresholds, no invented hardware counters). - make test-gpu-attention also runs the sweep under --sim; the chip gate script writes receipt.json, sweep.json and timing.json. Host battery: host-only 85 checks, sim 108 checks, sweep --sim 73 checks (72 points, 64 supported, 8 unsupported by the kernel envelope), all PASS. Co-authored-by: Drake Stapleton Co-Authored-By: Claude Fable 5.1 --- .crumb | 26 ++--- Makefile | 1 + docs/.crumb | 14 +-- docs/numeric/.crumb | 8 +- docs/numeric/FB1_CUT5_ATTENTION.md | 34 +++++- src/.crumb | 8 +- src/omega_gpu_attention_api.c | 69 +++++++++-- src/omega_gpu_attention_api.h | 22 +++- tests/.crumb | 8 +- tests/gpu_attention_test.c | 180 +++++++++++++++++++++++++++-- tools/.crumb | 8 +- tools/run_gpu_attention_chip.sh | 13 ++- 12 files changed, 327 insertions(+), 64 deletions(-) diff --git a/.crumb b/.crumb index 2603dca3e..719434c79 100644 --- a/.crumb +++ b/.crumb @@ -51,8 +51,8 @@ "purpose": "Directory calibration. Purpose not yet described by a human." }, { - "digest": "e156df1e52268bb3346ded9bf7afcb426960e643a8889e1d76865a83a3a5a642", - "last_commit": "55d05d6", + "digest": "1c478a7e59b6a030f35f00356f51098708307326e8bd7127d99573e98666b395", + "last_commit": "d12d012", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, @@ -87,33 +87,33 @@ "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "fab4c6952b54567c65c8bd843e46bcd238c688940ed1d1830af2b584b057781b", - "last_commit": "55d05d6", + "digest": "be4a15a5245076828c97b151dd4ed1cc891c7dc5f22aee9ffdd0d93fb430e720", + "last_commit": "d12d012", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "9eb57c43d48ae70bce44757cbf075c38a03a090e0da529260d88e197e602c767", - "last_commit": "55d05d6", + "digest": "58368f55fc201ea01917bd941190d00de8e40f4e5b161525d48d03eac877084e", + "last_commit": "d12d012", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { - "digest": "9e08561d488a03e636cb8622b6667636230ac21cce1711e0ff6a45cf7f5c5761", - "last_commit": "00f54b6", + "digest": "1a6281ce3f92b5e68d0709eab039f9852df5e7132493e2e12f90f7ff495560d1", + "last_commit": "d12d012", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:59067587092f050563a42fe041880c24f8c92e7c0c09bc01e5885b712222826b", - "compiled_at": "2026-10-04T19:57:19Z", + "children_root": "sha256:3dfdebd3b8a90fadcdc4cdf89c37d7939bfe585c8c3146736b41614ed8d4db47", + "compiled_at": "2026-10-04T19:57:41Z", "compiler": "crumb-compile/1", - "digest": "sha256:55e53e69b40dc3fd8b30b97f8a493fda44b457f717f3048e3a37eb9c163589c4", + "digest": "sha256:ef305ccd639ab0ae01d1bd7d341ec115dc0e3abf98e84293a73fb0482b17d5fa", "evidence_files": 1289, "evidence_root": "sha256:748eb7ce0bd377815585505582eb76663350e35af3d876a488897ceea120ba3f", "files": 3049, - "generated_at_commit": "55d05d6a53e55dddc06f5eb689bee10c8830f52d", - "source_tree": "sha256:a1f0fb11e0348083ced716d80bdce7f58050b2f26cb1c146206fb4244f1af0ed" + "generated_at_commit": "d12d012a763f44ebb367431497eaf727256b25a8", + "source_tree": "sha256:89fe46eed931e5dda00562ec210c6f07446d0bdd5444742f372a3d16f17730a4" }, "owner_lane": "", "provenance": { diff --git a/Makefile b/Makefile index 3106d0830..a79189330 100644 --- a/Makefile +++ b/Makefile @@ -2173,6 +2173,7 @@ $(GPU_ATTN_TEST): tests/gpu_attention_test.c src/omega_gpu_attention_api.h $(OUT test-gpu-attention: $(GPU_ATTN_TEST) ./$(GPU_ATTN_TEST) --host-only ./$(GPU_ATTN_TEST) --sim + ./$(GPU_ATTN_TEST) --sweep --sim --out $(OUT_DIR)/gpu_attention_sweep_sim.json # FB-1 cut 4b: fresh-process device-open probe (flake investigation, tools/probe_gpu_open.sh) $(OUT_DIR)/gpu_session_probe: tests/gpu_session_probe.c src/omega_gpu_session.h $(OUT_DIR)/libomega_gpu.a diff --git a/docs/.crumb b/docs/.crumb index a5012d5ce..3ce0ed302 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -81,8 +81,8 @@ "purpose": "Directory docs/estimation. Purpose not yet described by a human." }, { - "digest": "4272674cbb644368ae8fef74a5775144b11dbd3aac3eb25f073bd898f0268329", - "last_commit": "bceec7b", + "digest": "9d273061333f2d4c54d4450cf2ed8f5aad36e301fe2ba299399d388c47f86f8a", + "last_commit": "d12d012", "name": "numeric", "purpose": "Directory docs/numeric. Purpose not yet described by a human." }, @@ -117,15 +117,15 @@ "purpose": "Directory docs/turing. Purpose not yet described by a human." } ], - "children_root": "sha256:c86da32ec6d7b7bc1f8db4bdf024fadf35ae5d588cbadf733db2292beb32691f", - "compiled_at": "2026-10-04T19:57:19Z", + "children_root": "sha256:202ed4db636f4f835d49c1b95ba93276d042d33cab14e49d40266d0905e7d125", + "compiled_at": "2026-10-04T19:57:41Z", "compiler": "crumb-compile/1", - "digest": "sha256:e156df1e52268bb3346ded9bf7afcb426960e643a8889e1d76865a83a3a5a642", + "digest": "sha256:1c478a7e59b6a030f35f00356f51098708307326e8bd7127d99573e98666b395", "evidence_files": 26, "evidence_root": "sha256:f58236184eadc5f8c8f95499127a9f43c2f4dc702ddca267853d8c3dee0690f1", "files": 100, - "generated_at_commit": "55d05d6a53e55dddc06f5eb689bee10c8830f52d", - "source_tree": "sha256:a6ae94eae6a9333187ad07cf8161cb7261242e9ad567e32d80602224c4556887" + "generated_at_commit": "d12d012a763f44ebb367431497eaf727256b25a8", + "source_tree": "sha256:905f8c986ea594eb1dc8d8ed2ab228ff5f06078a5c3eb04b5a2f1b9d3e67c0ab" }, "owner_lane": "", "provenance": { diff --git a/docs/numeric/.crumb b/docs/numeric/.crumb index 211f49913..0061435f9 100644 --- a/docs/numeric/.crumb +++ b/docs/numeric/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-04T19:07:57Z", + "compiled_at": "2026-10-04T19:18:09Z", "compiler": "crumb-compile/1", - "digest": "sha256:4272674cbb644368ae8fef74a5775144b11dbd3aac3eb25f073bd898f0268329", + "digest": "sha256:9d273061333f2d4c54d4450cf2ed8f5aad36e301fe2ba299399d388c47f86f8a", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 10, - "generated_at_commit": "3a3144d4a2cbc4fd55a60da85fe94bd7ed5a9a39", - "source_tree": "sha256:804ae75ce73ff6bc90b9a9a820a86ec37274225b1528c09617df150ad4d5ec6c" + "generated_at_commit": "a274e0a2c8d39f3a9cb57cb9681a500b7d9f2252", + "source_tree": "sha256:afc08234f882c5d3af02da93f4e4443870ff429bc8c89c4a36647bbb5adaa66a" }, "owner_lane": "", "provenance": { diff --git a/docs/numeric/FB1_CUT5_ATTENTION.md b/docs/numeric/FB1_CUT5_ATTENTION.md index 1bd32ea20..87ea07774 100644 --- a/docs/numeric/FB1_CUT5_ATTENTION.md +++ b/docs/numeric/FB1_CUT5_ATTENTION.md @@ -88,6 +88,33 @@ mutant's. Names come from `omega_gpu_attention_mutant_name` (table checked again Negative control: the comparison must reject the oracle with kv heads swapped. Repeat run on a cache hit must be bit-identical. +## Staging dedupe per launch (OM-2) + +The paged path stages the referenced blocks' layer slice into a GPU-uncached staging buffer. +Within one batched launch the staged slot is now keyed by the physical block id: a block that +several sequences reference (a shared prompt prefix) is copied once and every row of the +renumbered block table points at the same slot. `OmegaGpuAttnInfo` reports the effect: +`kv_blocks_logical` (table entries walked), `kv_blocks_unique` (slices actually copied), +`kv_bytes_staged` (bytes copied) and `kv_bytes_naive` (what per-reference staging would have +copied); `kv_source` is `OMEGA_GPU_ATTN_KV_STAGED` (0) in this cut, there is no resident KV +(see "Limits"). `omega_gpu_attention_test_set_dedupe(false)` restores per-reference staging +as a negative control; the test proves the legacy counters equal the naive numbers and that the +deduped output is bit-identical to the legacy output. Simulator evidence, 4 sequences sharing +10 scattered prefix blocks with private tails {1,2,3,3}: 8q/2kv (one launch) 19 unique of 49 +logical, 155648 of 401408 bytes; 32q/8kv (two launches, dedupe is per launch) 29 of 49, +950272 of 1605632 bytes. + +## Head and kv-head scaling sweep (`gpu_attention_test --sweep --out `) + +q heads {1,2,4,8,16,24,32,64} x every divisor as kv heads x context {256, 2048}, bs 16, +scattered block ids with a shared prefix across 4 sequences. Non power-of-two ratios (the 24-head +rows) must be refused with `BAD_ARGS` and are recorded as `unsupported_by_kernel_envelope`. +Supported points: oracle parity on the first call, then N timed calls (20 on chip, 1 under +`--sim`), recording median/p90 call and kernel milliseconds, launches, CTAs, threads per CTA, +q/out widths and the staging counters above. Schema `OMEGA_GPU_ATTENTION_SWEEP_V1`; the file +says `host_simulator` and that hardware counters are unavailable (no invented counters). The +sweep asserts no timing thresholds; the 3 ms gate stays in `--timing`. + ## Host IR simulator (`gpu_attention_test --sim`) Executes the allocated IR program (physical registers, so the register allocator's loop @@ -103,8 +130,11 @@ of that CTA instruction by instruction. - head_dim 64 only (TinyLlama, Llama-3.2-1B). Larger heads need a bigger shared-memory declaration in the QMD (word 36) and a second thread-per-token tiling. -- Staging copy of q, the referenced KV blocks and the tables per call; the device is opened - per call (cut 1b's resident handle API is the path to remove both). +- Staging copy of q, the referenced KV blocks (once per physical block per launch, OM-2) and + the tables per call. No resident KV: the pool lives in an anonymous CPU mmap whose + "device address" is the CPU virtual address, not a GPU VA this channel can address, and + GPU-cached system memory is not coherent with CPU mappings (nvos.h), so a resident copy + needs an Omega-owned GPU-uncached mirror plus exact block generations from the pool. - Serialising instruction schedule (every instruction waits on everything pending), as in cut 4: correctness first, throughput later. - Context <= 4096 on the contiguous gqa path. diff --git a/src/.crumb b/src/.crumb index ab1ce0cd5..7610e4927 100644 --- a/src/.crumb +++ b/src/.crumb @@ -170,14 +170,14 @@ } ], "children_root": "sha256:607467f8ea6e425bf33f7a0df65a411055a569e302b8127ab84822a0b4559109", - "compiled_at": "2026-10-04T19:57:19Z", + "compiled_at": "2026-10-04T19:57:41Z", "compiler": "crumb-compile/1", - "digest": "sha256:fab4c6952b54567c65c8bd843e46bcd238c688940ed1d1830af2b584b057781b", + "digest": "sha256:be4a15a5245076828c97b151dd4ed1cc891c7dc5f22aee9ffdd0d93fb430e720", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 403, - "generated_at_commit": "55d05d6a53e55dddc06f5eb689bee10c8830f52d", - "source_tree": "sha256:bf53ea9b7e9ea01435d1b4889e9fae8f7dadddbbe66137845af1c377c0e6b667" + "generated_at_commit": "d12d012a763f44ebb367431497eaf727256b25a8", + "source_tree": "sha256:be5556c8348e8b02f96d241a4e0b4ade91f9cc94793b60f45c526218554ef8e2" }, "owner_lane": "", "provenance": { diff --git a/src/omega_gpu_attention_api.c b/src/omega_gpu_attention_api.c index d680009ea..aa83eab9e 100644 --- a/src/omega_gpu_attention_api.c +++ b/src/omega_gpu_attention_api.c @@ -422,6 +422,7 @@ static int build_kernel(const KSpec *k, OmegaBlackwellKernel *kernel, BlackwellI /* One kernel per KSpec; the device copy is uploaded once and stays resident for * the process (cut 4b). The IR program is kept for the host simulator. */ static int g_mutant; +static bool g_dedupe = true; /* shared-prefix staging dedupe; false = legacy per-sequence staging (test hook) */ typedef struct { int used; KSpec k; OmegaBlackwellKernel kernel; BlackwellIRProgram *prog; NvrmMem code; } Slot; static OmegaGpuAttnSim g_sim; static Slot g_cache[8]; @@ -431,6 +432,7 @@ static bool g_hooked; #define UNLOCK() omega_gpu_session_unlock() void omega_gpu_attention_test_set_simulator(OmegaGpuAttnSim sim) { LOCK(); g_sim = sim; UNLOCK(); } +void omega_gpu_attention_test_set_dedupe(bool on) { LOCK(); g_dedupe = on; UNLOCK(); } static void cache_clear_locked(void) { for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) @@ -568,8 +570,10 @@ static int run_launch(const Slot *ks, const Launch *L, OmegaGpuAttnInfo *info) { return unwritten ? OMEGA_GPU_ATTN_UNWRITTEN : OMEGA_GPU_ATTN_OK; } -/* Caller holds the lock. */ +/* Caller holds the lock. Accounts the launch's q / table / output traffic (the KV + * staging bytes are accounted where the slices are copied). */ static int launch(const Slot *ks, const Launch *L, OmegaGpuAttnInfo *info) { + if (info) { info->q_bytes += L->q_bytes; info->tab_bytes += L->tab_bytes; info->out_bytes += L->out_bytes; info->kv_source = OMEGA_GPU_ATTN_KV_STAGED; } if (g_sim) return g_sim(ks->prog, L, info); return run_launch(ks, L, info); } @@ -633,6 +637,7 @@ int omega_gpu_gqa_attention_f32(const float *q, const float *k_cache, const floa if (!pool) { UNLOCK(); return OMEGA_GPU_ATTN_CHIP_FAIL; } memcpy(pool, k_cache, plane); memcpy(pool + plane, v_cache, plane); + if (info) { info->kv_bytes_staged += 2 * plane; info->kv_bytes_naive += 2 * plane; info->kv_blocks_logical += 1; info->kv_blocks_unique += 1; } /* one contiguous pseudo-block */ uint32_t tab[2] = { 0, seq_len }; /* table row {0}, then the context length at byte 4 */ Launch L = { .q = q, .q_bytes = n_out * 4, .pool = pool, .pool_bytes = 2 * plane, .tab = tab, .tab_bytes = sizeof tab, @@ -681,27 +686,67 @@ static int paged_core(const float *q, const uint8_t *pool, const OmegaGpuKvLayou if (seqs_per_launch == 0) seqs_per_launch = 1; uint32_t maxb = 1; for (uint32_t s = 0; s < num_seqs; s++) if (n_blocks[s] > maxb) maxb = n_blocks[s]; + /* Dedupe map: physical block id -> staged slot of the current launch (-1 = not staged yet), + * plus the list of ids touched by the launch (in slot order) so the reset is O(unique). */ + int32_t *slot_of = NULL; uint32_t *touched = NULL; + if (g_dedupe) { + size_t nb = ly->num_blocks ? ly->num_blocks : 1; + slot_of = malloc(nb * sizeof *slot_of); + touched = malloc(nb * sizeof *touched); + if (!slot_of || !touched) { free(slot_of); free(touched); free(eff); UNLOCK(); return OMEGA_GPU_ATTN_CHIP_FAIL; } + memset(slot_of, 0xff, nb * sizeof *slot_of); /* every entry -1 */ + } for (uint32_t s0 = 0; s0 < num_seqs && rc == OMEGA_GPU_ATTN_OK; s0 += seqs_per_launch) { uint32_t ns = num_seqs - s0 < seqs_per_launch ? num_seqs - s0 : seqs_per_launch; - /* stage the referenced blocks' layer slice, renumbered in table order */ + /* logical references: every sequence's blocks up to its (truncated) context */ uint32_t total = 0; for (uint32_t s = 0; s < ns; s++) total += eff[s0 + s] ? (eff[s0 + s] + ly->block_size - 1) / ly->block_size : 0; - uint64_t staged_bytes = (uint64_t)(total ? total : 1) * ly->layer_stride_bytes; - if (staged_bytes > 0xffffffffull) { rc = OMEGA_GPU_ATTN_TOO_LARGE; break; } - void *host_tmp = NULL; - uint8_t *staged = stage_target((size_t)staged_bytes, &host_tmp); uint32_t *tab = calloc((size_t)ns * maxb + ns, sizeof *tab); - if (!staged || !tab) { free(host_tmp); free(tab); rc = OMEGA_GPU_ATTN_CHIP_FAIL; break; } + if (!tab) { rc = OMEGA_GPU_ATTN_CHIP_FAIL; break; } + /* pass 1: assign staged slots. Dedupe keys on the PHYSICAL block id (not the table + * position): a CoW prefix block shared by several branches gets one slot. Each + * sequence's compact table keeps its own block order and truncated context. */ uint32_t g = 0; for (uint32_t s = 0; s < ns; s++) { uint32_t need = eff[s0 + s] ? (eff[s0 + s] + ly->block_size - 1) / ly->block_size : 0; - for (uint32_t b = 0; b < need; b++, g++) { - const uint8_t *src = pool + (size_t)tables[s0 + s][b] * ly->block_stride_bytes + (size_t)layer_idx * ly->layer_stride_bytes; - memcpy(staged + (size_t)g * ly->layer_stride_bytes, src, (size_t)ly->layer_stride_bytes); - tab[(size_t)s * maxb + b] = g; + for (uint32_t b = 0; b < need; b++) { + uint32_t id = tables[s0 + s][b]; + if (g_dedupe) { + if (slot_of[id] < 0) { slot_of[id] = (int32_t)g; touched[g] = id; g++; } + tab[(size_t)s * maxb + b] = (uint32_t)slot_of[id]; + } else { + tab[(size_t)s * maxb + b] = g++; /* legacy: one slot per reference */ + } } tab[(size_t)ns * maxb + s] = eff[s0 + s]; } + uint64_t staged_bytes = (uint64_t)(g ? g : 1) * ly->layer_stride_bytes; + if (staged_bytes > 0xffffffffull) { free(tab); rc = OMEGA_GPU_ATTN_TOO_LARGE; break; } + void *host_tmp = NULL; + uint8_t *staged = stage_target((size_t)staged_bytes, &host_tmp); + if (!staged) { free(tab); rc = OMEGA_GPU_ATTN_CHIP_FAIL; break; } + /* pass 2: copy each staged slot's (physical block, layer) slice exactly once */ + if (g_dedupe) { + for (uint32_t u = 0; u < g; u++) { + const uint8_t *src = pool + (size_t)touched[u] * ly->block_stride_bytes + (size_t)layer_idx * ly->layer_stride_bytes; + memcpy(staged + (size_t)u * ly->layer_stride_bytes, src, (size_t)ly->layer_stride_bytes); + slot_of[touched[u]] = -1; /* reset for the next launch */ + } + } else { + uint32_t w = 0; + for (uint32_t s = 0; s < ns; s++) { + uint32_t need = eff[s0 + s] ? (eff[s0 + s] + ly->block_size - 1) / ly->block_size : 0; + for (uint32_t b = 0; b < need; b++, w++) { + const uint8_t *src = pool + (size_t)tables[s0 + s][b] * ly->block_stride_bytes + (size_t)layer_idx * ly->layer_stride_bytes; + memcpy(staged + (size_t)w * ly->layer_stride_bytes, src, (size_t)ly->layer_stride_bytes); + } + } + } + if (info) { + info->kv_blocks_logical += total; info->kv_blocks_unique += g; + info->kv_bytes_staged += (uint64_t)g * ly->layer_stride_bytes; + info->kv_bytes_naive += (uint64_t)total * ly->layer_stride_bytes; + } Launch L = { .q = q + (size_t)s0 * row, .q_bytes = (size_t)ns * row * 4, .pool = staged, .pool_bytes = (size_t)staged_bytes, .tab = tab, .tab_bytes = ((size_t)ns * maxb + ns) * 4, .out = out + (size_t)s0 * row, .out_bytes = (size_t)ns * row * 4, @@ -713,7 +758,7 @@ static int paged_core(const float *q, const uint8_t *pool, const OmegaGpuKvLayou free(host_tmp); free(tab); } UNLOCK(); - free(eff); + free(eff); free(slot_of); free(touched); if (info) info->call_ns = now_ns() - t0; return rc; } diff --git a/src/omega_gpu_attention_api.h b/src/omega_gpu_attention_api.h index d498eef98..01ad01179 100644 --- a/src/omega_gpu_attention_api.h +++ b/src/omega_gpu_attention_api.h @@ -45,7 +45,10 @@ * Staging: the host copies q, the referenced KV blocks' layer slice, the (compacted, * renumbered) block tables and the context lengths into GPU-uncached staging buffers * for each launch and copies the result back. The kernel itself walks the block - * table; a resident pool handle is a later cut. + * table; a resident pool handle is a later cut. Since the attention hardening cut + * (2026-10-04) a launch stages each physical block's layer slice once however many + * sequences of the batch reference it (shared CoW prefixes), and OmegaGpuAttnInfo + * reports the bytes staged against the per-sequence (naive) figure. * * Cut 4b (2026-10-04): the device, channel and launch scratch are the process-wide * session shared with the matmul and elementwise APIs (omega_gpu_session.h): one @@ -100,7 +103,16 @@ typedef struct { uint32_t ctas_last_launch, threads_per_cta; char target_chip[64]; uint32_t sm_architecture; + /* Appended (attention hardening cut, 2026-10-04): transfer accounting for the call. + * Existing field order above is ABI; sovereign-core mirrors this struct field for field. */ + uint64_t kv_bytes_staged; /* KV layer-slice bytes actually copied into staging (sum over launches) */ + uint64_t kv_bytes_naive; /* what per-sequence staging would have copied (every sequence its own blocks) */ + uint32_t kv_blocks_logical; /* block references summed over sequences, truncated to each context */ + uint32_t kv_blocks_unique; /* distinct (physical block, layer) slices staged, summed over launches */ + uint64_t q_bytes, tab_bytes, out_bytes; /* q, block-table/context and result traffic (sum over launches) */ + uint32_t kv_source; /* OMEGA_GPU_ATTN_KV_STAGED; a resident pool is a later cut */ } OmegaGpuAttnInfo; +#define OMEGA_GPU_ATTN_KV_STAGED 0u /* Contiguous f32 KV (TensorBackend::gqa_attention). q and out: [num_q_heads][head_dim]. */ int omega_gpu_gqa_attention_f32(const float *q, const float *k_cache, const float *v_cache, @@ -173,6 +185,14 @@ struct BlackwellIRProgramTag; typedef int (*OmegaGpuAttnSim)(const void *prog /* const BlackwellIRProgram * */, const OmegaGpuAttnLaunch *L, OmegaGpuAttnInfo *info); void omega_gpu_attention_test_set_simulator(OmegaGpuAttnSim sim); +/* Shared-prefix staging dedupe (attention hardening cut, 2026-10-04): within one launch + * every (physical block, layer) slice referenced by the batch is staged once, keyed by the + * physical block id, and every sequence's compact table points at that one staged slot. + * On by default. `false` selects the legacy per-sequence staging (every sequence copies + * its own blocks), kept as the reference fallback and as the negative control of the + * staging-byte tests. Test hook; never cleared in production. */ +void omega_gpu_attention_test_set_dedupe(bool on); + #ifdef __cplusplus } #endif diff --git a/tests/.crumb b/tests/.crumb index 0843844a9..ab6260b53 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -284,14 +284,14 @@ } ], "children_root": "sha256:0a4e348cf355e1fa5924f9c76a460d1d49bc285687b134365ef0fb9dab4f0855", - "compiled_at": "2026-10-04T19:57:19Z", + "compiled_at": "2026-10-04T19:57:41Z", "compiler": "crumb-compile/1", - "digest": "sha256:9eb57c43d48ae70bce44757cbf075c38a03a090e0da529260d88e197e602c767", + "digest": "sha256:58368f55fc201ea01917bd941190d00de8e40f4e5b161525d48d03eac877084e", "evidence_files": 11, "evidence_root": "sha256:d5c1bceb4d57ef0eb42797c58801b5926509711614c3e38bb7105b6efa3942f2", "files": 790, - "generated_at_commit": "55d05d6a53e55dddc06f5eb689bee10c8830f52d", - "source_tree": "sha256:c3ad53ad3ba707657f944d28dcf4aa052510d627d52b5318e31172e7f13cc243" + "generated_at_commit": "d12d012a763f44ebb367431497eaf727256b25a8", + "source_tree": "sha256:0b472eb701f9a089b91ad665fe93d050ae46cdca92672a0ed7d2a6a65c3d5bae" }, "owner_lane": "", "provenance": { diff --git a/tests/gpu_attention_test.c b/tests/gpu_attention_test.c index 03a64a470..af714069b 100644 --- a/tests/gpu_attention_test.c +++ b/tests/gpu_attention_test.c @@ -4,6 +4,9 @@ * mutant), register budget, nvdisasm listing check * ./gpu_attention_test --timing [--out r.json] cut 4b gate: 1 query x 32 heads x 2048 ctx, 100 calls * through the persistent session, median under 3 ms + * ./gpu_attention_test --sweep [--out s.json] head-scaling sweep: paged bf16 at head_dim 64 over q heads + * 1..64 x every kv-head divisor x ctx 256/2048, parity + timings + * (receipt OMEGA_GPU_ATTENTION_SWEEP_V1; --sim marks host timings) * ./gpu_attention_test --out receipt.json chip gate: every shape against the host oracle, the * mutants (deliberately wrong kernels, all of * 1 .. OMEGA_GPU_ATTN_MUTANT_COUNT-1) which the same @@ -217,7 +220,12 @@ static void host_only(void) { /* ------------------------------------------------------------- chip gate */ typedef struct { const char *name; size_t n; int rc; size_t bad; double worst; int pass; uint64_t chip_ns, call_ns; uint32_t calls, unwritten; - int mutants_run, mutants_caught; char mutant_note[400]; } Case; + int mutants_run, mutants_caught; char mutant_note[400]; + uint64_t kv_staged, kv_naive; uint32_t blk_logical, blk_unique; } Case; +static void case_info(Case *c, const OmegaGpuAttnInfo *i) { + c->chip_ns = i->elapsed_ns; c->call_ns = i->call_ns; c->calls = i->chip_calls; c->unwritten = i->unwritten_words; + c->kv_staged = i->kv_bytes_staged; c->kv_naive = i->kv_bytes_naive; c->blk_logical = i->kv_blocks_logical; c->blk_unique = i->kv_blocks_unique; +} static Case g_cases[32]; static int g_ncases; static int g_mutant_caught[OMEGA_GPU_ATTN_MUTANT_COUNT], g_mutant_run[OMEGA_GPU_ATTN_MUTANT_COUNT]; @@ -225,12 +233,13 @@ static void record(Case c, FILE *out) { g_cases[g_ncases++] = c; CHECK(c.pass, "%s rc=%s violations=%zu worst=%g", c.name, omega_gpu_attention_rc_name(c.rc), c.bad, c.worst); CHECK(c.mutants_caught == c.mutants_run, "%s mutants caught %d of %d (%s)", c.name, c.mutants_caught, c.mutants_run, c.mutant_note); - printf("%s %s n=%zu rc=%s violations=%zu worst_scaled_err=%g chip_ns=%" PRIu64 " call_ns=%" PRIu64 " calls=%u unwritten=%u | mutants %d/%d %s\n", + printf("%s %s n=%zu rc=%s violations=%zu worst_scaled_err=%g chip_ns=%" PRIu64 " call_ns=%" PRIu64 " calls=%u unwritten=%u kv_staged=%" PRIu64 "/%" PRIu64 " blocks=%u/%u | mutants %d/%d %s\n", c.pass && c.mutants_caught == c.mutants_run ? "PASS" : "FAIL", c.name, c.n, omega_gpu_attention_rc_name(c.rc), c.bad, c.worst, c.chip_ns, c.call_ns, c.calls, c.unwritten, - c.mutants_caught, c.mutants_run, c.mutant_note); - if (out) fprintf(out, "%s{\"case\":\"%s\",\"n\":%zu,\"rc\":\"%s\",\"violations\":%zu,\"worst_scaled_err\":%g,\"pass\":%s,\"chip_elapsed_ns\":%" PRIu64 ",\"call_ns\":%" PRIu64 ",\"chip_calls\":%u,\"unwritten_words\":%u,\"mutants_run\":%d,\"mutants_caught\":%d,\"mutant_note\":\"%s\"}", + c.kv_staged, c.kv_naive, c.blk_unique, c.blk_logical, c.mutants_caught, c.mutants_run, c.mutant_note); + if (out) fprintf(out, "%s{\"case\":\"%s\",\"n\":%zu,\"rc\":\"%s\",\"violations\":%zu,\"worst_scaled_err\":%g,\"pass\":%s,\"chip_elapsed_ns\":%" PRIu64 ",\"call_ns\":%" PRIu64 ",\"chip_calls\":%u,\"unwritten_words\":%u," + "\"kv_bytes_staged\":%" PRIu64 ",\"kv_bytes_naive\":%" PRIu64 ",\"kv_blocks_unique\":%u,\"kv_blocks_logical\":%u,\"mutants_run\":%d,\"mutants_caught\":%d,\"mutant_note\":\"%s\"}", g_ncases > 1 ? "," : "", c.name, c.n, omega_gpu_attention_rc_name(c.rc), c.bad, c.worst, c.pass ? "true" : "false", c.chip_ns, c.call_ns, c.calls, c.unwritten, - c.mutants_run, c.mutants_caught, c.mutant_note); + c.kv_staged, c.kv_naive, c.blk_unique, c.blk_logical, c.mutants_run, c.mutants_caught, c.mutant_note); } static const char *mutant_name(int m) { return omega_gpu_attention_mutant_name((OmegaGpuAttnMutant)m); } @@ -285,7 +294,7 @@ static int chip(const char *out_path) { GqaCtx gc = { q, kc, vc, seq, nqh, nkv }; c.rc = run_gqa(&gc, got, &info); c.bad = compare(got, want, n, 2e-4, 2e-5, &c.worst); - c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.call_ns = info.call_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; + c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; case_info(&c, &info); if (ci == 3) { OmegaGpuAttnInfo i2; int rc2 = run_gqa(&gc, again, &i2); CHECK(rc2 == OMEGA_GPU_ATTN_OK && i2.kernel_cache_hit && memcmp(got, again, n * 4) == 0, "%s repeat run (cache hit) is bit-identical", name); @@ -314,7 +323,7 @@ static int chip(const char *out_path) { OmegaGpuAttnInfo info; GqaCtx gc = { q, kc, vc, seq, nqh, nkv }; c.rc = run_gqa(&gc, got, &info); c.bad = compare(got, want, n, 1e-3, 1e-4, &c.worst); - c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.call_ns = info.call_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; + c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; case_info(&c, &info); static const int ms[2] = { OMEGA_GPU_ATTN_MUTANT_NO_MAX, 0 }; run_mutants(&c, ms, run_gqa, &gc, got, want, n, 1e-3, 1e-4); record(c, out); free(q); free(kc); free(vc); free(got); free(want); @@ -332,7 +341,7 @@ static int chip(const char *out_path) { OmegaGpuAttnInfo info; GqaCtx gc = { q, kc, vc, seq, nqh, nkv }; c.rc = run_gqa(&gc, got, &info); c.bad = compare(got, want, n, 2e-4, 2e-5, &c.worst); - c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.call_ns = info.call_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; + c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; case_info(&c, &info); static const int ms_q[2] = { OMEGA_GPU_ATTN_MUTANT_Q_ROW, 0 }; static const int ms_o[3] = { OMEGA_GPU_ATTN_MUTANT_OUT_ROW, OMEGA_GPU_ATTN_MUTANT_KV_HEAD, 0 }; run_mutants(&c, ms_q, run_gqa, &gc, got, want, n, 2e-4, 2e-5); @@ -364,7 +373,7 @@ static int chip(const char *out_path) { OmegaGpuAttnInfo info; PagedCtx pc = { q, pool, &ly, ids, nblk, ctx, layer, nqh, nkv }; c.rc = run_paged(&pc, got, &info); c.bad = compare(got, want, n, 2e-4, 2e-5, &c.worst); - c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.call_ns = info.call_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; + c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; case_info(&c, &info); if (ci == 3) { /* NO_MAX cannot overflow on unit-scale scores (softmax is shift invariant); it is caught by the large-score gqa case */ static const int ms3[6] = { OMEGA_GPU_ATTN_MUTANT_KV_HEAD, OMEGA_GPU_ATTN_MUTANT_SLOT, OMEGA_GPU_ATTN_MUTANT_NO_RESCALE, OMEGA_GPU_ATTN_MUTANT_Q_ROW, OMEGA_GPU_ATTN_MUTANT_OUT_ROW, 0 }; @@ -402,13 +411,65 @@ static int chip(const char *out_path) { OmegaGpuAttnInfo info; BatchCtx bc = { q, pool, &ly, tables, ctxs2, maxb, nseq, layer, nqh, nkv }; c.rc = run_batch(&bc, got, &info); c.bad = compare(got, want, n, 2e-4, 2e-5, &c.worst); - c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.call_ns = info.call_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; + c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; case_info(&c, &info); int zeros = 1; for (size_t i = 0; i < (size_t)nqh * hd; i++) if (got[(size_t)nqh * hd + i] != 0.0f) zeros = 0; CHECK(zeros, "batch: the empty sequence is all zeros"); static const int ms[5] = { OMEGA_GPU_ATTN_MUTANT_KV_HEAD, OMEGA_GPU_ATTN_MUTANT_SLOT, OMEGA_GPU_ATTN_MUTANT_Q_ROW, OMEGA_GPU_ATTN_MUTANT_OUT_ROW, 0 }; run_mutants(&c, ms, run_batch, &bc, got, want, n, 2e-4, 2e-5); + /* all 7 referenced blocks are distinct here, so dedupe stages exactly the naive figure */ + CHECK(c.blk_logical == 7 && c.blk_unique == 7 && c.kv_staged == c.kv_naive && c.kv_staged == 7ull * ly.layer_stride_bytes, "batch: 7 distinct blocks staged once each (%u/%u, %" PRIu64 " bytes)", c.blk_unique, c.blk_logical, c.kv_staged); record(c, out); free(pool); free(tables); free(q); free(got); free(want); } + /* 5. branch-heavy batch (attention hardening cut): four sequences share the same 10 physical + * prefix blocks (160 tokens, scattered ids) and own private tails of 1..3 blocks ending in a + * partial 64-token chunk. The shared prefix must be staged once per launch, keyed by the + * physical block id. Two head shapes: 8q/2kv puts all four sequences in one launch + * (unique = 10 + 9 tails), 32q/8kv splits them two per launch (unique = 2*10 + 9). + * Negative control: the legacy per-sequence staging (test hook) copies the prefix four + * times and the byte-reduction check must flag it. */ + for (int sh = 0; sh < 2; sh++) { + uint32_t nqh = sh ? 32 : 8, nkv = sh ? 8 : 2, bs = 16, layers = 2, layer = 1, nseq = 4, prefix_blocks = 10, pool_blocks = 64; + uint32_t expect_unique = sh ? 2 * prefix_blocks + 9 : prefix_blocks + 9; + uint32_t maxb = prefix_blocks + 3; + OmegaGpuKvLayout ly = layout_for(pool_blocks, bs, layers, nkv, hd); + uint8_t *pool = malloc(ly.pool_bytes); uint16_t *pw = (uint16_t *)pool; + for (size_t i = 0; i < ly.pool_bytes / 2; i++) pw[i] = f2bf(frand(&seed, -1.0f, 1.0f)); + int32_t *tables = malloc((size_t)nseq * maxb * 4); + for (size_t i = 0; i < (size_t)nseq * maxb; i++) tables[i] = -1; + const uint32_t tails[4] = { 1, 2, 3, 3 }; /* private blocks per sequence: 9 in all */ + const int32_t ctxs5[4] = { 165, 180, 193, 207 }; /* 160 + 5, 20, 33, 47: every last chunk partial */ + for (uint32_t s = 0; s < nseq; s++) { + for (uint32_t b = 0; b < prefix_blocks; b++) tables[s * maxb + b] = (int32_t)((b * 5 + 7) % pool_blocks); /* same scattered physical prefix */ + for (uint32_t b = 0; b < tails[s]; b++) tables[s * maxb + prefix_blocks + b] = (int32_t)(50 + s * 3 + b); /* private, distinct */ + } + size_t n = (size_t)nseq * nqh * hd; + float *q = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4), *legacy = malloc(n * 4); + for (size_t i = 0; i < n; i++) q[i] = frand(&seed, -1.0f, 1.0f); + oracle_batch(q, pool, &ly, tables, ctxs5, maxb, nseq, layer, nqh, nkv, hd, want); + char name[96]; snprintf(name, sizeof name, "paged_bf16_shared_prefix_4seq_%uq%ukv_10pre", nqh, nkv); + Case c = { .name = strdup(name), .n = n }; + OmegaGpuAttnInfo info; BatchCtx bc = { q, pool, &ly, tables, ctxs5, maxb, nseq, layer, nqh, nkv }; + c.rc = run_batch(&bc, got, &info); + c.bad = compare(got, want, n, 2e-4, 2e-5, &c.worst); + c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; case_info(&c, &info); + uint32_t logical = 4 * prefix_blocks + 9; + CHECK(c.blk_logical == logical && c.blk_unique == expect_unique, "%s: %u logical references, %u unique staged (expected %u/%u)", name, c.blk_logical, c.blk_unique, logical, expect_unique); + CHECK(c.kv_naive == (uint64_t)logical * ly.layer_stride_bytes && c.kv_staged == (uint64_t)expect_unique * ly.layer_stride_bytes && c.kv_staged < c.kv_naive, + "%s: staged %" PRIu64 " < naive %" PRIu64 " bytes (saved %" PRIu64 ")", name, c.kv_staged, c.kv_naive, c.kv_naive - c.kv_staged); + CHECK(info.kv_source == OMEGA_GPU_ATTN_KV_STAGED && info.q_bytes == n * 4 && info.out_bytes == n * 4 && info.tab_bytes > 0, "%s: q/out/table traffic accounted (q=%" PRIu64 " out=%" PRIu64 " tab=%" PRIu64 ")", name, info.q_bytes, info.out_bytes, info.tab_bytes); + /* negative control: legacy per-sequence staging duplicates the shared prefix; same answer, more bytes */ + omega_gpu_attention_test_set_dedupe(false); + OmegaGpuAttnInfo li; int lrc = run_batch(&bc, legacy, &li); + omega_gpu_attention_test_set_dedupe(true); + double lw; size_t lbad = compare(legacy, want, n, 2e-4, 2e-5, &lw); + CHECK(lrc == OMEGA_GPU_ATTN_OK && lbad == 0, "%s legacy staging: parity (rc=%s bad=%zu)", name, omega_gpu_attention_rc_name(lrc), lbad); + CHECK(li.kv_blocks_unique == logical && li.kv_bytes_staged == li.kv_bytes_naive && li.kv_bytes_staged > c.kv_staged, + "%s negative control: duplicated prefix staging is flagged (legacy staged %" PRIu64 " == naive %" PRIu64 ", dedupe %" PRIu64 ")", name, li.kv_bytes_staged, li.kv_bytes_naive, c.kv_staged); + CHECK(memcmp(got, legacy, n * 4) == 0, "%s: dedupe and legacy staging are bit-identical", name); + static const int ms5[3] = { OMEGA_GPU_ATTN_MUTANT_KV_HEAD, OMEGA_GPU_ATTN_MUTANT_SLOT, 0 }; + run_mutants(&c, ms5, run_batch, &bc, got, want, n, 2e-4, 2e-5); + record(c, out); free(pool); free(tables); free(q); free(got); free(want); free(legacy); + } /* every mutant the API knows (1 .. COUNT-1) ran at least once and was caught every time; * a mutant added to the enum without a battery entry fails here */ for (int m = 1; m < OMEGA_GPU_ATTN_MUTANT_COUNT; m++) { @@ -585,17 +646,114 @@ static int timing(const char *out_path) { return 0; } +/* ------------------------------------------------ head-scaling sweep (attention hardening cut) + * `--sweep [--out sweep.json]`: the production paged bf16 path at head_dim 64 over query-head + * counts 1, 2, 4, 8, 16, 24, 32, 64 and every divisor as the kv-head count, at context 256 + * (latency) and 2048 (bandwidth), block size 16, scattered block ids. Points whose GQA ratio + * is not a power of two are mathematically legal (the generic contract allows them) but + * outside the current kernel envelope (codegen selects the kv head with a shift): they are + * called anyway, must be refused with BAD_ARGS, and are recorded as + * "unsupported_by_kernel_envelope" (no chip time). Every supported point is checked against + * the host oracle on its first call, then warmed and timed over N calls (median / p90 of the + * whole call and of the chip kernel time). Gates: parity, no unwritten word, one device open, + * every call OK; the timings are evidence, not a threshold. Only quantities the stack measures + * are recorded; SM occupancy, bandwidth utilisation and cache hit rates are not observable + * through this path and are reported as unavailable. Under --sim the timings are host-simulator + * timings and the receipt says so. */ +static int sweep(const char *out_path) { + FILE *out = out_path ? fopen(out_path, "w") : NULL; + if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; } + const int N = g_sim_mode ? 1 : 20; + const uint32_t hd = 64, bs = 16, layers = 1, layer = 0; + static const uint32_t qheads[8] = { 1, 2, 4, 8, 16, 24, 32, 64 }; + static const uint32_t ctxs[2] = { 256, 2048 }; + uint32_t seed = 0xfb1c05eeu; + if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_ATTENTION_SWEEP_V1\",\"path\":\"paged_bf16\",\"head_dim\":%u,\"block_size\":%u,\"timed_calls\":%d,\"host_simulator\":%s," + "\"tolerances\":{\"rel\":2e-4,\"abs\":2e-5},\"hardware_counters\":\"unavailable (SM occupancy, bandwidth utilisation, cache hit rates are not observable through this path)\",\"points\":[", + hd, bs, N, g_sim_mode ? "true" : "false"); + int npoints = 0, supported = 0, unsupported = 0; + double *samples = malloc((size_t)N * sizeof *samples), *ksamples = malloc((size_t)N * sizeof *ksamples); + for (int qi = 0; qi < 8; qi++) for (uint32_t nkv = 1; nkv <= qheads[qi]; nkv++) { + uint32_t nqh = qheads[qi]; + if (nqh % nkv) continue; + uint32_t ratio = nqh / nkv; int pow2 = (ratio & (ratio - 1)) == 0; + for (int ci = 0; ci < 2; ci++) { + uint32_t ctx = ctxs[ci], nblk = (ctx + bs - 1) / bs, pool_blocks = nblk + 5; + OmegaGpuKvLayout ly = layout_for(pool_blocks, bs, layers, nkv, hd); + uint8_t *pool = malloc(ly.pool_bytes); uint16_t *pw = (uint16_t *)pool; + for (size_t i = 0; i < ly.pool_bytes / 2; i++) pw[i] = f2bf(frand(&seed, -1.0f, 1.0f)); + uint32_t *ids = malloc(nblk * 4); + for (uint32_t b = 0; b < nblk; b++) ids[b] = (b * 7 + 3) % pool_blocks; + size_t n = (size_t)nqh * hd; + float *q = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4); + for (size_t i = 0; i < n; i++) q[i] = frand(&seed, -1.0f, 1.0f); + OmegaGpuAttnInfo info; memset(&info, 0, sizeof info); + int rc = omega_gpu_paged_attention_bf16(q, pool, &ly, ids, nblk, ctx, layer, nqh, nkv, hd, got, &info); + const char *klass; int pass; size_t bad = 0; double worst = 0; int ok_calls = 0; uint32_t unwritten = 0, cache_hits = 0; double med = 0, p90 = 0, kmed = 0, kp90 = 0; + if (!pow2) { + klass = "unsupported_by_kernel_envelope"; + pass = rc == OMEGA_GPU_ATTN_BAD_ARGS; unsupported++; + CHECK(pass, "sweep %uq/%ukv ctx %u: ratio %u (not a power of two) refused with BAD_ARGS (got %s)", nqh, nkv, ctx, ratio, omega_gpu_attention_rc_name(rc)); + } else { + klass = "supported"; supported++; + oracle_paged(q, pool, &ly, ids, nblk, ctx, layer, nqh, nkv, hd, want); + bad = compare(got, want, n, 2e-4, 2e-5, &worst); + unwritten = info.unwritten_words; + uint64_t kv_staged = info.kv_bytes_staged, kv_naive = info.kv_bytes_naive, qb = info.q_bytes, tb = info.tab_bytes, ob = info.out_bytes; + uint32_t launches = info.chip_calls, ctas = info.ctas_last_launch, blk_l = info.kv_blocks_logical, blk_u = info.kv_blocks_unique, kv_src = info.kv_source; + for (int i = 0; i < N; i++) { + OmegaGpuAttnInfo ti; + int trc = omega_gpu_paged_attention_bf16(q, pool, &ly, ids, nblk, ctx, layer, nqh, nkv, hd, got, &ti); + samples[i] = ti.call_ns / 1e6; ksamples[i] = ti.elapsed_ns / 1e6; + double w; size_t b2 = compare(got, want, n, 2e-4, 2e-5, &w); + if (w > worst) worst = w; + bad += b2; unwritten += ti.unwritten_words; cache_hits += ti.kernel_cache_hit; + if (trc == OMEGA_GPU_ATTN_OK && b2 == 0) ok_calls++; + } + qsort(samples, (size_t)N, sizeof samples[0], cmp_d); qsort(ksamples, (size_t)N, sizeof ksamples[0], cmp_d); + med = samples[N / 2]; p90 = samples[N * 9 / 10]; kmed = ksamples[N / 2]; kp90 = ksamples[N * 9 / 10]; + pass = rc == OMEGA_GPU_ATTN_OK && bad == 0 && ok_calls == N && unwritten == 0 && cache_hits == (uint32_t)N; + CHECK(pass, "sweep %uq/%ukv ctx %u: rc=%s violations=%zu ok_calls=%d/%d unwritten=%u cache_hits=%u", nqh, nkv, ctx, omega_gpu_attention_rc_name(rc), bad, ok_calls, N, unwritten, cache_hits); + printf("%s sweep %2uq/%2ukv ratio %2u ctx %4u: launches=%u ctas=%u call_ms med=%.3f p90=%.3f kernel_ms med=%.3f p90=%.3f kv_staged=%" PRIu64 " (naive %" PRIu64 ") blocks=%u/%u q=%" PRIu64 " tab=%" PRIu64 " out=%" PRIu64 " worst=%g\n", + pass ? "PASS" : "FAIL", nqh, nkv, ratio, ctx, launches, ctas, med, p90, kmed, kp90, kv_staged, kv_naive, blk_u, blk_l, qb, tb, ob, worst); + if (out) fprintf(out, "%s{\"q_heads\":%u,\"kv_heads\":%u,\"gqa_ratio\":%u,\"head_dim\":%u,\"context\":%u,\"q_width\":%zu,\"out_width\":%zu,\"class\":\"%s\",\"rc\":\"%s\",\"pass\":%s," + "\"violations\":%zu,\"worst_scaled_err\":%g,\"unwritten_words\":%u,\"launches_per_call\":%u,\"ctas_last_launch\":%u,\"threads_per_cta\":%u," + "\"call_ms_median\":%.4f,\"call_ms_p90\":%.4f,\"kernel_ms_median\":%.4f,\"kernel_ms_p90\":%.4f,\"kernel_cache_hits\":%u,\"first_call_cache_hit\":%s," + "\"kv_bytes_staged\":%" PRIu64 ",\"kv_bytes_naive\":%" PRIu64 ",\"kv_blocks_logical\":%u,\"kv_blocks_unique\":%u,\"q_bytes\":%" PRIu64 ",\"tab_bytes\":%" PRIu64 ",\"out_bytes\":%" PRIu64 ",\"kv_source\":\"%s\"}", + npoints ? "," : "", nqh, nkv, ratio, hd, ctx, n, n, klass, omega_gpu_attention_rc_name(rc), pass ? "true" : "false", bad, worst, unwritten, launches, ctas, info.threads_per_cta, + med, p90, kmed, kp90, cache_hits, info.kernel_cache_hit ? "true" : "false", kv_staged, kv_naive, blk_l, blk_u, qb, tb, ob, kv_src == OMEGA_GPU_ATTN_KV_STAGED ? "staged" : "resident"); + npoints++; + } + if (!pow2) { + printf("%s sweep %2uq/%2ukv ratio %2u ctx %4u: %s (rc=%s)\n", pass ? "PASS" : "FAIL", nqh, nkv, ratio, ctx, klass, omega_gpu_attention_rc_name(rc)); + if (out) fprintf(out, "%s{\"q_heads\":%u,\"kv_heads\":%u,\"gqa_ratio\":%u,\"head_dim\":%u,\"context\":%u,\"q_width\":%zu,\"out_width\":%zu,\"class\":\"%s\",\"rc\":\"%s\",\"pass\":%s}", + npoints ? "," : "", nqh, nkv, ratio, hd, ctx, n, n, klass, omega_gpu_attention_rc_name(rc), pass ? "true" : "false"); + npoints++; + } + free(pool); free(ids); free(q); free(got); free(want); + } + } + free(samples); free(ksamples); + uint32_t opens = omega_gpu_session_open_count(); + CHECK(opens == (g_sim_mode ? 0u : 1u), "sweep: device opened %s (opens=%u)", g_sim_mode ? "never (simulator)" : "once", opens); + printf("sweep: %d points (%d supported, %d unsupported by the kernel envelope), device_opens=%u\n", npoints, supported, unsupported, opens); + if (out) { fprintf(out, "],\"points_total\":%d,\"points_supported\":%d,\"points_unsupported\":%d,\"device_opens\":%u,\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", npoints, supported, unsupported, opens, g_checks, g_failed, g_failed == 0 ? "OMEGA_GPU_ATTENTION_SWEEP_PASS" : "OMEGA_GPU_ATTENTION_SWEEP_FAIL"); fclose(out); } + return 0; +} + int main(int argc, char **argv) { - const char *out_path = NULL; int host = 0, sim = 0, timing_mode = 0; + const char *out_path = NULL; int host = 0, sim = 0, timing_mode = 0, sweep_mode = 0; for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "--host-only") == 0) host = 1; else if (strcmp(argv[i], "--sim") == 0) sim = 1; else if (strcmp(argv[i], "--timing") == 0) timing_mode = 1; + else if (strcmp(argv[i], "--sweep") == 0) sweep_mode = 1; else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i]; } if (sim) { g_sim_mode = 1; omega_gpu_attention_test_set_simulator(sim_launch); } if (host) host_only(); else if (timing_mode) { if (timing(out_path) != 0) return 2; } + else if (sweep_mode) { if (sweep(out_path) != 0) return 2; } else if (chip(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; diff --git a/tools/.crumb b/tools/.crumb index 49b41d808..2b8e1d430 100644 --- a/tools/.crumb +++ b/tools/.crumb @@ -56,14 +56,14 @@ } ], "children_root": "sha256:0920c282c31e3be9b4745fab718ceb60dbfe2ea90dda2108986f60fc2ac53e23", - "compiled_at": "2026-10-04T19:07:57Z", + "compiled_at": "2026-10-04T19:18:09Z", "compiler": "crumb-compile/1", - "digest": "sha256:9e08561d488a03e636cb8622b6667636230ac21cce1711e0ff6a45cf7f5c5761", + "digest": "sha256:1a6281ce3f92b5e68d0709eab039f9852df5e7132493e2e12f90f7ff495560d1", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 120, - "generated_at_commit": "3a3144d4a2cbc4fd55a60da85fe94bd7ed5a9a39", - "source_tree": "sha256:172b5a4666f700959782e204a6278eaf02307c5fe94fcf6f9441ae5bd8918412" + "generated_at_commit": "a274e0a2c8d39f3a9cb57cb9681a500b7d9f2252", + "source_tree": "sha256:cbcfb5f36ecaa585d40d30eed94d7992c3fdf9b069b1dc25a2d720970d184762" }, "owner_lane": "", "provenance": { diff --git a/tools/run_gpu_attention_chip.sh b/tools/run_gpu_attention_chip.sh index ac2ba9bd0..734b6a0b8 100755 --- a/tools/run_gpu_attention_chip.sh +++ b/tools/run_gpu_attention_chip.sh @@ -7,6 +7,8 @@ # negative control and a cache-hit repeat. Sealed receipt folder # OUT_ROOT/FB1-CUT5-/ (default ~/workspace/evidence-out): # receipt.json machine-readable verdict per case run.log test output +# sweep.json head-scaling sweep (q/kv heads x ctx) sweep.log its output +# timing.json cut 4b timing gate (median < 3 ms) timing.log its output # device.txt kernel, driver, omega and physics shas build.log compiler output # sim.log the same battery through the host IR simulator, run first # copies of the three source files the verdict depends on, SHA256SUMS over all @@ -35,8 +37,15 @@ if [ "$sim_rc" -ne 0 ]; then fi ./build/gpu_attention_test --out "$OUT/receipt.json" > "$OUT/run.log" 2>&1 rc=$? +# head-scaling sweep (attention hardening cut) and the cut 4b timing gate, same session rules +./build/gpu_attention_test --sweep --out "$OUT/sweep.json" > "$OUT/sweep.log" 2>&1 +sweep_rc=$? +./build/gpu_attention_test --timing --out "$OUT/timing.json" > "$OUT/timing.log" 2>&1 +timing_rc=$? +[ "$rc" -eq 0 ] && rc=$sweep_rc +[ "$rc" -eq 0 ] && rc=$timing_rc cp src/omega_gpu_attention_api.c src/omega_gpu_attention_api.h tests/gpu_attention_test.c "$OUT/" (cd "$OUT" && sha256sum -- * > SHA256SUMS) -tail -1 "$OUT/run.log" -echo "FB1-CUT5 chip gate rc=$rc receipt=$OUT/receipt.json" +tail -1 "$OUT/run.log"; tail -1 "$OUT/sweep.log"; tail -1 "$OUT/timing.log" +echo "FB1-CUT5 chip gate rc=$rc (sweep rc=$sweep_rc timing rc=$timing_rc) receipt=$OUT/receipt.json sweep=$OUT/sweep.json" exit $rc