diff --git a/.crumb b/.crumb index e245c5a26..c7ae2b8ef 100644 --- a/.crumb +++ b/.crumb @@ -51,8 +51,8 @@ "purpose": "Directory calibration. Purpose not yet described by a human." }, { - "digest": "01f9bcaf892355eff231425a96bf8f625fc9d9be4c1480991db92b6fefd6cd49", - "last_commit": "f816473", + "digest": "bb89b5d99f5816b19958cdca790ea2f2066d87c00a0d78f4db6955f1540ed821", + "last_commit": "1f4a3c7", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, @@ -63,8 +63,8 @@ "purpose": "Directory doctrine. Purpose not yet described by a human." }, { - "digest": "243c8e51a2a001d63e898e6853e3ef522a65a92d6ad7344e74f5d54c8e575cb9", - "last_commit": "8190bad", + "digest": "ba7d7c6e399d6a92a34be467f1d5501a9620a63839d38c27bcf8fb90b7448a02", + "last_commit": "197566a", "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": "6d49877f41b071c4f28e00bb47293fd6c483791242e7a7fc54460a5e0f6ae3ca", - "last_commit": "7f36700", + "digest": "e96259ccad1aad77187347965341897722b089df3ee84a17145a32e8d308a4a6", + "last_commit": "197566a", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "c9825de4f77620c738f1cac290238b55040200959e0938ac03fa41a3c95c488d", - "last_commit": "f816473", + "digest": "03e780fc46936d8b534e4992f9d87e3ce95711a40fdfe0128a96328410e3af5b", + "last_commit": "197566a", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { - "digest": "ef22f063312bab17b11019f16b48d0e30d907c7bd1af458b6dbc6923907b517f", - "last_commit": "f816473", + "digest": "808edb958b46d236f503828399cbdb7f1cb62f8eebfd1604d4768b42734b459f", + "last_commit": "197566a", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:2c5c4c1dd77a1bf73e662f3cb0c5d707b294bbdc63f5821947f4bc62d8ebaa17", - "compiled_at": "2026-10-06T01:43:54Z", + "children_root": "sha256:a1b9d0223f485c3d79afd990b37b8a8575e6668d77f9a85ccbe77f136f7388d1", + "compiled_at": "2026-10-06T01:57:59Z", "compiler": "crumb-compile/1", - "digest": "sha256:77c5261e0f8d0b6a3cfee57862bced9d40b8c2002774fcbbc332821556e6f121", - "evidence_files": 3304, - "evidence_root": "sha256:9594e3e2a05603747e60ab08376dcf98a6857a89e43720f33126d5b68e41f4d1", - "files": 5127, - "generated_at_commit": "df9f40a0435a70d680f6b49297815e53e3497ea9", - "source_tree": "sha256:7a3871434c18631c02fc551dce23a09b61df1fdfc8852b4fff3b3154a9f80c38" + "digest": "sha256:be23f82039edc46a35eabd6a56e179f144df76e656d6fe46a6de3641d4db8bbc", + "evidence_files": 3415, + "evidence_root": "sha256:1b9ca1bba224bd584685de23bd348db4924906fcbbef61d70265a267c57c0ab1", + "files": 5243, + "generated_at_commit": "1f4a3c7821d769a99ce1fcc896ac8ee30a33ea69", + "source_tree": "sha256:eba61a1b9111b0ed16e83ca6a589174e89d646facd904ca708b3993116ae3d41" }, "owner_lane": "", "provenance": { diff --git a/Makefile b/Makefile index 0de1d65b8..39639f824 100644 --- a/Makefile +++ b/Makefile @@ -2246,6 +2246,17 @@ $(GPU_EW_TEST): tests/gpu_elementwise_test.c src/omega_gpu_elementwise_api.h $(O test-gpu-elementwise: $(GPU_EW_TEST) ./$(GPU_EW_TEST) --host-only +# omega #308: structured warp reconvergence (BSSY/BSYNC regions). test-gpu-reconv is +# host-only (encoder goldens, region bookkeeping, refusals, nvdisasm listing, every probe +# kernel through the SIMT warp simulator tests/bw_warp_sim.h against the oracle, mutation +# controls); the chip gate is tools/run_gpu_reconv_chip.sh. +.PHONY: test-gpu-reconv +GPU_RECONV_TEST = $(OUT_DIR)/gpu_reconv_test +$(GPU_RECONV_TEST): tests/gpu_reconv_test.c tests/bw_warp_sim.h src/omega_gpu_elementwise_api.h src/omega_bw_reconv.h $(OUT_DIR)/libomega_gpu.a + $(CC) $(CFLAGS) -Itests -o $@ tests/gpu_reconv_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm +test-gpu-reconv: $(GPU_RECONV_TEST) + ./$(GPU_RECONV_TEST) --host-only + # FB-1 cut 5: native gqa_attention (f32 KV) and paged_attention (bf16 KV, + batch). # test-gpu-attention runs host-only (refusals, codegen, nvdisasm listing) and then the # whole parity battery through the host IR simulator (--sim, no chip); the chip @@ -2253,8 +2264,8 @@ test-gpu-elementwise: $(GPU_EW_TEST) # (tools/run_gpu_attention_chip.sh). -ffp-contract=off keeps the oracle free of FMA. .PHONY: test-gpu-attention GPU_ATTN_TEST = $(OUT_DIR)/gpu_attention_test -$(GPU_ATTN_TEST): tests/gpu_attention_test.c src/omega_gpu_attention_api.h $(OUT_DIR)/libomega_gpu.a - $(CC) $(CFLAGS) -ffp-contract=off -o $@ tests/gpu_attention_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm +$(GPU_ATTN_TEST): tests/gpu_attention_test.c tests/bw_warp_sim.h src/omega_gpu_attention_api.h $(OUT_DIR)/libomega_gpu.a + $(CC) $(CFLAGS) -ffp-contract=off -Itests -o $@ tests/gpu_attention_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm test-gpu-attention: $(GPU_ATTN_TEST) ./$(GPU_ATTN_TEST) --host-only ./$(GPU_ATTN_TEST) --sim diff --git a/bench/prime_race/.crumb b/bench/prime_race/.crumb index 6673818e8..7f2f6bcbd 100644 --- a/bench/prime_race/.crumb +++ b/bench/prime_race/.crumb @@ -3,14 +3,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-05T21:43:28Z", + "compiled_at": "2026-10-05T23:21:14Z", "compiler": "crumb-compile/1", - "digest": "sha256:e48fd63d20aa45947aecd81b6481d5edfce2fdd5f88f5e1f70ae18026a19e679", + "digest": "sha256:2ff0bcbc1cfcfd4b5c4572225f063ff0f542447b9cc85b413a183eebc6d26cf0", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 10, - "generated_at_commit": "7b961a5590f1087971321490a2655757d72cd31e", - "source_tree": "sha256:819b99c3125bfc48608d2177983d34efa8c81aab38b7a3cc57c218fbdfe37c5a" + "generated_at_commit": "2f8b6f85971afc782d986abb888e55fa200ca3d0", + "source_tree": "sha256:eb185f001ff6c322c33228fe0021cf8bd22ebf3183d990ca54a48ad3cceba033" } }, "invariants": [ diff --git a/bench/prime_race/gb10_native.c b/bench/prime_race/gb10_native.c index b735e046d..5772600d9 100644 --- a/bench/prime_race/gb10_native.c +++ b/bench/prime_race/gb10_native.c @@ -22,6 +22,10 @@ * sleeping for up to N us before the session's usual 50 us sleep-poll; unset or 0 keeps the old * wait. Any other value fails setup. Reported in the detail JSON as spin_us. * + * Mark loop (omega #308): PR_GB10_MARK=uniform (default, campaign C5), divergent (the original + * lane-dependent mark loop inside a BSSY/BSYNC reconvergence region, regression R1) or divergent2 + * (the prime loop in a region as well, nested). Any other value fails setup. Reported as mark. + * * Labels (handoff 2026-10-05 DESIGN): algorithm=other, faithful=no, bits=1, * environment linux-hosted-gb10-native. Build with -DPR_GB10_MUTANT=1 only for the chip * test's negative control (the r == 0 fix is dropped; the oracle must catch it). @@ -77,11 +81,24 @@ static int gb_table_global(void) { return -1; } +/* PR_GB10_MARK (omega #308): "uniform" or unset = the C5 warp-uniform mark loop; "divergent" = + * the original lane-dependent mark loop inside a BSSY/BSYNC region (regression R1); "divergent2" = + * the prime loop in a region as well (nested). -1 on any other value. */ +static int gb_mark_mode(void) { + const char *v = getenv("PR_GB10_MARK"); + if (!v || strcmp(v, "uniform") == 0) return 0; + if (strcmp(v, "divergent") == 0) return 1; + if (strcmp(v, "divergent2") == 0) return 2; + return -1; +} +static const char *const GB_MARK_NAMES[3] = { "uniform", "divergent", "divergent2" }; + typedef struct { uint64_t limit, odd; uint32_t nwords, ctas, budget, stride, lastw, tailinv; uint32_t spin_us; /* marker wait spin window (campaign C4) */ int table_global; /* PR_GB10_TABLE=global: never stage the table (campaign C6) */ + int mark; /* PR_GB10_MARK: 0 uniform (C5), 1 regioned mark loop, 2 nested regions (omega #308) */ uint32_t root; /* isqrt(limit) */ uint32_t nprimes; /* odd primes in the table of the last pass (sentinel excluded) */ size_t table_cap; /* entries, sentinel included */ @@ -145,6 +162,8 @@ static int gb_setup(void **state, uint64_t limit) { if (s->spin_us == UINT32_MAX) { fprintf(stderr, "gb10-native: PR_GB10_SPIN_US must be 0..%u\n", OMEGA_GPU_SESSION_MAX_SPIN_US); return -1; } s->table_global = gb_table_global(); if (s->table_global < 0) { fprintf(stderr, "gb10-native: PR_GB10_TABLE must be auto or global\n"); return -1; } + s->mark = gb_mark_mode(); + if (s->mark < 0) { fprintf(stderr, "gb10-native: PR_GB10_MARK must be uniform, divergent or divergent2\n"); return -1; } uint32_t blocks = (s->nwords + GB_THREADS - 1) / GB_THREADS; s->ctas = blocks == 0 ? 1 : (blocks > s->budget ? s->budget : blocks); s->stride = s->ctas * GB_THREADS; @@ -161,6 +180,8 @@ static int gb_setup(void **state, uint64_t limit) { /* campaign C3: stage the table in shared memory whenever it fits the QMD bound */ if (!s->table_global && (uint64_t)s->entries * 12 <= OMEGA_BW_QMD_MAX_SHARED_BYTES) s->shared_bytes = s->entries * 12; OmegaGpuEwOp op = s->shared_bytes ? OMEGA_GPU_EW_PRIME_SIEVE_SHARED : OMEGA_GPU_EW_PRIME_SIEVE; + if (s->mark == 1) op = s->shared_bytes ? OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1 : OMEGA_GPU_EW_PRIME_SIEVE_DIV1; + if (s->mark == 2) op = s->shared_bytes ? OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2 : OMEGA_GPU_EW_PRIME_SIEVE_DIV2; if (omega_gpu_elementwise_codegen_ir(op, PR_GB10_MUTANT, NULL, &s->k) != OMEGA_GPU_EW_OK) { fprintf(stderr, "gb10-native: kernel codegen failed\n"); return -1; @@ -242,8 +263,8 @@ static uint64_t gb_threads(void *state) { gb_state *s = state; return s ? s->str static int gb_detail(void *state, FILE *f) { gb_state *s = state; if (!s) return -1; - fprintf(f, "{\"grid\": %u, \"cta_budget\": %u, \"block\": %u, \"words\": %u, \"nprimes\": %u, \"table\": \"%s\", \"shared_bytes\": %u, \"spin_us\": %u, \"kernel_code_sha256\": \"", - s->ctas, s->budget, GB_THREADS, s->nwords, s->nprimes, s->shared_bytes ? "shared" : "global", s->shared_bytes, s->spin_us); + fprintf(f, "{\"grid\": %u, \"cta_budget\": %u, \"block\": %u, \"words\": %u, \"nprimes\": %u, \"table\": \"%s\", \"shared_bytes\": %u, \"spin_us\": %u, \"mark\": \"%s\", \"kernel_code_sha256\": \"", + s->ctas, s->budget, GB_THREADS, s->nwords, s->nprimes, s->shared_bytes ? "shared" : "global", s->shared_bytes, s->spin_us, GB_MARK_NAMES[s->mark]); for (int i = 0; i < 32; i++) fprintf(f, "%02x", s->k.code_digest[i]); fprintf(f, "\", \"kernel_insns\": %zu, \"kernel_gpr\": %u, \"mutant\": %d, \"launches\": %" PRIu64 ", \"diag_kernel_ns_total\": %" PRIu64 diff --git a/bench/prime_race/tests/gb10_sieve_host_test.c b/bench/prime_race/tests/gb10_sieve_host_test.c index cf82a6007..28cc73768 100644 --- a/bench/prime_race/tests/gb10_sieve_host_test.c +++ b/bench/prime_race/tests/gb10_sieve_host_test.c @@ -17,6 +17,7 @@ #include "prime_race_impl.h" #include "omega_blackwell_codegen.h" #include "omega_gpu_elementwise_api.h" +#include "bw_warp_sim.h" /* SIMT warp simulator (omega #308): the regioned sieves run through it */ #include #include #include @@ -172,6 +173,24 @@ static void sim_cta(const BlackwellIRProgram *p, uint32_t cta) { } } +/* omega #308: when g_ws_order >= 0, sim_sieve runs the CTAs through the SIMT warp simulator + * (tests/bw_warp_sim.h: lane masks, divergence, BSSY/BSYNC) in that fragment order instead of + * the per-thread simulator above; a simulator error lands in g_sim_err as 200 + its code. */ +static int g_ws_order = -1; +static WsSim g_ws_last; +static uint8_t g_ws_smem[65536]; +static void ws_sieve_run(const BlackwellIRProgram *p, uint32_t ctas) { + WsSim s; memset(&s, 0, sizeof s); + s.p = p; s.threads = OMEGA_GPU_EW_THREADS; s.order = g_ws_order; + memcpy(s.cbank, g_cb, sizeof s.cbank); + s.smem = g_ws_smem; s.smem_bytes = g_smem_bytes > sizeof g_ws_smem ? (uint32_t)sizeof g_ws_smem : g_smem_bytes; + s.ranges[0] = (WsRange){ g_ranges[0].lo, g_ranges[0].hi }; s.ranges[1] = (WsRange){ g_ranges[1].lo, g_ranges[1].hi }; s.nranges = 2; + int err = ws_run(&s, ctas, 1); + g_ws_last = s; + g_steps += s.warp_issues; + if (err) g_sim_err = 200 + err; +} + /* Mirrors gb10_native.c's pass on the host: same table, same argument words; `budget` * is the CTA cap (PR_GB10_CTA_BUDGET, default OMEGA_GPU_EW_MAX_CTAS). */ static int sim_sieve(const BlackwellIRProgram *p, uint64_t limit, uint32_t budget, int staged, uint64_t *words) { @@ -205,8 +224,8 @@ static int sim_sieve(const BlackwellIRProgram *p, uint64_t limit, uint32_t budge g_ranges[0] = (Range){ (uintptr_t)tab, (uintptr_t)tab + cap * 12 }; g_ranges[1] = (Range){ (uintptr_t)out, (uintptr_t)out + (size_t)nwords * 4 }; g_sim_err = 0; - for (uint32_t cta = 0; cta < ctas && !g_sim_err; cta++) - sim_cta(p, cta); + if (g_ws_order >= 0) ws_sieve_run(p, ctas); + else for (uint32_t cta = 0; cta < ctas && !g_sim_err; cta++) sim_cta(p, cta); size_t nw64 = pr_words64(limit); memset(words, 0, nw64 * 8); memcpy(words, out, (size_t)nwords * 4); @@ -648,7 +667,115 @@ int main(int argc, char **argv) { free(sk.code); free(smk.code); free(sp); free(smp); free(nobar); } - /* 6. division model */ + /* 6. structured reconvergence (omega #308, regression R1): the sieve with its lane-dependent + * loops put back inside BSSY/BSYNC regions. DIV1 = the 888a011 mark loop in a region, DIV2 = + * the prime loop in an outer region too (nested). Each variant: codegen, hazard walks, + * listing, the oracle sweep through the warp simulator in both fragment orders, the math + * mutant caught, and the dropped-join / corrupted-target mutants rejected by the simulator. + * Then the warp-issue model of the C5 kernel against the regioned ones (a host model, not + * a measurement: the chip number comes from gb10_native PR_GB10_MARK=divergent). */ + { + static const struct { OmegaGpuEwOp op; int staged; const char *name; int regions; } dv[] = { + { OMEGA_GPU_EW_PRIME_SIEVE_DIV1, 0, "div1", 1 }, { OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1, 1, "shared_div1", 2 }, + { OMEGA_GPU_EW_PRIME_SIEVE_DIV2, 0, "div2", 2 }, { OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2, 1, "shared_div2", 3 }, + }; /* the staged variants also put the staging loop (lane-dependent trip count) in a region */ + static const char *const MUST_DIV[] = { "BSSY", "BSYNC", "IMAD.HI.U32", "@P0 BRA", "@!P0 BRA", "@P0 EXIT", "STG.E", "LDG.E" }; + /* positive control for the adapter: the production (C5) kernel through the warp simulator */ + { + uint64_t L = 1000000, *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8); + g_ws_order = 0; + int r = want && got ? sim_sieve(prog, L, OMEGA_GPU_EW_MAX_CTAS, 0, got) : -1; + g_ws_order = -1; + CHECK(r == 0 && want && memcmp(got, want, pr_words64(L) * 8) == 0, "C5 kernel through the warp simulator at 1e6 (rc %d err %d)", r, g_sim_err); + printf("warp-sim model C5 (global) 1e6: warp_issues=%" PRIu64 " lane_util=%.3f splits=%" PRIu64 " max_frags=%d\n", + g_ws_last.warp_issues, g_ws_last.warp_issues ? (double)g_ws_last.lane_issues / (32.0 * (double)g_ws_last.warp_issues) : 0.0, g_ws_last.splits, g_ws_last.max_frags); + /* the production staged kernel too: its staging loop splits the warp before BAR.SYNC + * (allowed on the chip since Volta; counted here, and the fragments stay split) */ + BlackwellIRProgram *cp = calloc(1, sizeof *cp); OmegaBlackwellKernel ck; memset(&ck, 0, sizeof ck); + if (cp && omega_gpu_elementwise_codegen_ir(OMEGA_GPU_EW_PRIME_SIEVE_SHARED, 0, cp, &ck) == OMEGA_GPU_EW_OK) { + want = oracle_bitmap(L); got = calloc(pr_words64(L) + 1, 8); + g_ws_order = 0; + r = want && got ? sim_sieve(cp, L, OMEGA_GPU_EW_MAX_CTAS, 1, got) : -1; + g_ws_order = -1; + CHECK(r == 0 && want && memcmp(got, want, pr_words64(L) * 8) == 0, "C5 staged kernel through the warp simulator at 1e6 (rc %d err %d)", r, g_sim_err); + printf("warp-sim model C5 (staged) 1e6: warp_issues=%" PRIu64 " lane_util=%.3f splits=%" PRIu64 " max_frags=%d bar_split_arrivals=%" PRIu64 "\n", + g_ws_last.warp_issues, g_ws_last.warp_issues ? (double)g_ws_last.lane_issues / (32.0 * (double)g_ws_last.warp_issues) : 0.0, g_ws_last.splits, g_ws_last.max_frags, g_ws_last.bar_split_arrivals); + free(ck.code); + } else CHECK(0, "C5 staged codegen for the warp-sim control"); + free(cp); + free(want); free(got); + } + for (size_t v = 0; v < sizeof dv / sizeof dv[0]; v++) { + BlackwellIRProgram *dp = calloc(1, sizeof *dp); + OmegaBlackwellKernel dk; memset(&dk, 0, sizeof dk); + int drc = dp ? omega_gpu_elementwise_codegen_ir(dv[v].op, 0, dp, &dk) : -1; + CHECK(drc == OMEGA_GPU_EW_OK && dk.gpr_count <= 64, "%s codegen rc %d gprs %u", dv[v].name, drc, dk.gpr_count); + if (drc != OMEGA_GPU_EW_OK) { free(dp); continue; } + int nbssy = 0, nbsync = 0; + for (size_t i = 0; i < dp->count; i++) { nbssy += dp->insns[i].op == BW_IR_BSSY; nbsync += dp->insns[i].op == BW_IR_BSYNC; } + CHECK(nbssy == dv[v].regions && nbsync == dv[v].regions, "%s has %d BSSY / %d BSYNC (want %d)", dv[v].name, nbssy, nbsync, dv[v].regions); + printf("%s kernel: %zu IR insns, %u GPRs, %d regions, sha256 ", dv[v].name, dp->count, dk.gpr_count, nbssy); + for (int i = 0; i < 32; i++) printf("%02x", dk.code_digest[i]); + printf("\n"); + CHECK(sb_hazards(dp, 1) == 0, "%s scoreboard hazards", dv[v].name); + CHECK(fixed_hazards(dp, &dk) == 0, "%s encoded fixed RAW hazards", dv[v].name); + if (!dv[v].staged) nvdisasm_check(&dk, dp, MUST_DIV, dv[v].name); + int dfail = 0, dn = 0; + for (int order = 0; order < 2; order++) for (size_t i = 0; i < nl; i++, dn++) { + uint64_t L = lims[i], *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8); + if (!want || !got) { CHECK(0, "alloc"); free(want); free(got); break; } + g_ws_order = order; + int r = sim_sieve(dp, L, OMEGA_GPU_EW_MAX_CTAS, dv[v].staged, got); + g_ws_order = -1; + if (r != 0 || memcmp(got, want, pr_words64(L) * 8) != 0) { + if (!dfail++) printf(" %s order %d limit %" PRIu64 ": sim rc %d err %d (%s), differs\n", dv[v].name, order, L, r, g_sim_err, g_sim_err >= 200 ? ws_err_name(g_sim_err - 200) : "-"); + } + if (L == 1000000 && order == 0) { + CHECK(count_primes(got, L) == 78498, "%s pi(1e6) = %" PRIu64, dv[v].name, count_primes(got, L)); + printf("warp-sim model %s 1e6: warp_issues=%" PRIu64 " lane_util=%.3f splits=%" PRIu64 " max_frags=%d peak_bars=%d bar_split_arrivals=%" PRIu64 "\n", dv[v].name, + g_ws_last.warp_issues, g_ws_last.warp_issues ? (double)g_ws_last.lane_issues / (32.0 * (double)g_ws_last.warp_issues) : 0.0, g_ws_last.splits, g_ws_last.max_frags, g_ws_last.peak_bars, g_ws_last.bar_split_arrivals); + CHECK(g_ws_last.bar_split_arrivals == 0, "%s reaches BAR.SYNC as a whole warp", dv[v].name); + } + free(want); free(got); + } + static const uint32_t budgets[] = { 1, 2, 128, 256 }; + for (size_t b = 0; b < sizeof budgets / sizeof budgets[0]; b++) { + uint64_t edge = 64ull * budgets[b] * OMEGA_GPU_EW_THREADS; + const uint64_t ls[] = { edge - 1, edge, edge + 1 }; + for (size_t i = 0; i < 3; i++, dn++) { + uint64_t L = ls[i], *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8); + if (!want || !got) { CHECK(0, "alloc"); free(want); free(got); break; } + g_ws_order = (int)(i & 1); + int r = sim_sieve(dp, L, budgets[b], dv[v].staged, got); + g_ws_order = -1; + if (r != 0 || memcmp(got, want, pr_words64(L) * 8) != 0) { if (!dfail++) printf(" %s budget %u limit %" PRIu64 ": sim rc %d err %d\n", dv[v].name, budgets[b], L, r, g_sim_err); } + free(want); free(got); + } + } + CHECK(dfail == 0, "%s differs from the oracle in %d of %d warp-sim cases", dv[v].name, dfail, dn); + printf("%s warp simulator: %d cases (both orders), %d differ\n", dv[v].name, dn, dfail); + /* mutants: 1 math (caught by the oracle), 2 join dropped and 3 target corrupted (rejected by the simulator) */ + for (int m = 1; m <= 3; m++) { + BlackwellIRProgram *mp2 = calloc(1, sizeof *mp2); OmegaBlackwellKernel mk2; memset(&mk2, 0, sizeof mk2); + int mrc = mp2 ? omega_gpu_elementwise_codegen_ir(dv[v].op, m, mp2, &mk2) : -1; + CHECK(mrc == OMEGA_GPU_EW_OK && memcmp(mk2.code_digest, dk.code_digest, 32) != 0, "%s mutant %d builds and differs (rc %d)", dv[v].name, m, mrc); + if (mrc == OMEGA_GPU_EW_OK) { + uint64_t L = 100000, *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8); + g_ws_order = 0; + int r = want && got ? sim_sieve(mp2, L, OMEGA_GPU_EW_MAX_CTAS, dv[v].staged, got) : -1; + g_ws_order = -1; + if (m == 1) CHECK(r == 0 && want && memcmp(got, want, pr_words64(L) * 8) != 0, "%s math mutant not caught at 1e5 (rc %d err %d)", dv[v].name, r, g_sim_err); + else CHECK(r != 0 && g_sim_err >= 200, "%s mutant %d (%s) not rejected by the warp simulator (rc %d err %d)", dv[v].name, m, m == 2 ? "BSYNC dropped" : "BSSY target corrupted", r, g_sim_err); + if (m > 1) printf("%s mutant %d: warp simulator says %s\n", dv[v].name, m, g_sim_err >= 200 ? ws_err_name(g_sim_err - 200) : "nothing"); + free(want); free(got); free(mk2.code); + } + free(mp2); + } + free(dk.code); free(dp); + } + } + + /* 7. division model */ magic_model(1000000); if (big) magic_model(10000000); diff --git a/docs/.crumb b/docs/.crumb index 8c941367a..fdf65c8f2 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -118,14 +118,14 @@ } ], "children_root": "sha256:ce53c9c7f75a7d122d6a926bb304b2df1bd0730693b54887b842ad3559bafe83", - "compiled_at": "2026-10-06T00:23:42Z", + "compiled_at": "2026-10-06T01:57:59Z", "compiler": "crumb-compile/1", - "digest": "sha256:01f9bcaf892355eff231425a96bf8f625fc9d9be4c1480991db92b6fefd6cd49", + "digest": "sha256:bb89b5d99f5816b19958cdca790ea2f2066d87c00a0d78f4db6955f1540ed821", "evidence_files": 34, "evidence_root": "sha256:589d8b3706e6d7a202ab20b278dac5fc5bafc973f1c2a867b2668aa7210b65cc", - "files": 113, - "generated_at_commit": "fa18e58a86a7402de785e07044f5f444a1dcca3c", - "source_tree": "sha256:0a688cde0c483735c8aee62f8ae2162bc7fbc30456961d7fc031b897c64e1587" + "files": 114, + "generated_at_commit": "1f4a3c7821d769a99ce1fcc896ac8ee30a33ea69", + "source_tree": "sha256:c1905c7445c800667d356e7bd34b303d6decbacc3630effc2ad13e48f5ae4fc7" }, "owner_lane": "", "provenance": { diff --git a/docs/gpu-reconvergence-308.md b/docs/gpu-reconvergence-308.md new file mode 100644 index 000000000..3081e06bd --- /dev/null +++ b/docs/gpu-reconvergence-308.md @@ -0,0 +1,154 @@ +# GPU structured warp reconvergence (omega #308) + +Branch feat/gpu-reconvergence-308, code at 0a1812f, receipts in evidence/gpu-reconv-308/. +Evidence classes used below: PROVEN BY TEST (a test in this repo passes, command and receipt +named), MEASURED (read off the real chip or the real nvcc), DERIVED FROM REFERENCE (taken from +Mesa NAK source, not checked by us on the chip), INFERRED (reasoned from documentation, not +checked), UNKNOWN. + +## 1. The failure mode + +On the GB10 (sm_121) a warp is 32 lanes. When lanes take different paths (a lane-dependent +branch or loop exit) and the code then runs a warp-wide step (SHFL, or a shared-memory exchange +guarded by BAR.SYNC), the lanes must first be brought back together. Without an explicit +reconvergence point the chip may run the later step with only some lanes active and reads the +inactive lanes' registers, which are undefined. + +Two places in AIEN hit this and were worked around by removing the divergence: +- The original divergent prime sieve (omega 888a011, lane-dependent mark loop) gave wrong + results; the production C5 sieve avoids it with a warp-uniform loop. +- Attention with a `j >= ctx` branch gave wrong results in the SHFL reduction after it + (2026-10-04); production attention is branch-free. + +nvcc brackets such code with BSSY (arm a barrier register, name the join point) and BSYNC (wait +for every armed lane, continue as one warp). Omega's Blackwell code generator had neither. #308 +adds them, plus a host simulator that rejects code which misuses them. +Chip observation (MEASURED, T1b): the diamond probe with its BSYNC dropped runs on the chip +without error and returns 466 of 1000 words wrong. + +## 2. Encodings (BSSY / BSYNC, sm_121) + +| Item | Value | Class | +|---|---|---| +| BSSY.RECONVERGENT | w0 = 0x945 \| pred<<12 \| bar<<16; w1 = (delta_insns - 1) * 16; w2 = 0x03800200; control 0x000fe200 (stall 1, yield) | MEASURED (nvcc 13.0.88 -arch=sm_121, evidence/gpu-reconv-308/oracle/) | +| BSYNC.RECONVERGENT | w0 = 0x941 \| pred<<12 \| bar<<16; w1 = 0; w2 = 0x03800200; control 0x000fea00 (stall 5, yield) | MEASURED (same oracle) | +| Opcodes 0x945 / 0x941, barrier id in bits 16..19 | Mesa NAK sm70_encode.rs OpBSSy / OpBSync | DERIVED FROM REFERENCE | +| RECONVERGENT bit (bit 73) | set by nvcc, omitted by NAK | MEASURED (nvcc) / DERIVED (NAK). We emit nvcc's form. | +| Branch target of BSSY | the instruction after the BSYNC; loop exits and breaks jump to the BSYNC itself (where nvcc sends them) | MEASURED (oracle k_break, k_loop, k_nested) | + +Golden fixtures for both words and refusals -101..-105 pass in `build/gpu_reconv_test` +(PROVEN BY TEST, 220 checks, T1a). nvdisasm decodes every emitted listing with zero +undecodable words (PROVEN BY TEST, same run). + +## 3. Region API and fail-closed rules + +Bookkeeping in src/omega_bw_reconv.h (`bw_regions_*`), wrapped by region_begin / region_exit / +region_join in src/omega_gpu_elementwise_api.c and src/omega_gpu_attention_api.c. + +- begin allocates barrier register Bn with n = nesting depth, then emits BSSY. 16 barrier + registers (B0..B15) are encodable; a 17th nested region is refused and the build fails. + Whether the chip really provides 16 is not documented to us; we ran 16 deep on the chip + (rc_depth16, MEASURED, T1a) and no deeper. +- exit records a forward branch that leaves the region; join patches it to the BSYNC and patches + the BSSY target to the instruction after the BSYNC. +- Refused at codegen: SHFL, BAR.SYNC or EXIT inside an open region; an unclosed region; a BSYNC + with no BSSY; on the launch path, SHFL or BAR.SYNC after a predicated EXIT (lanes already gone). + (PROVEN BY TEST: RC_REFUSE_* probes and RC_DEPTH16 in `build/gpu_reconv_test`.) +- This is why R6 (lanes exit early, then BAR.SYNC/SHFL on the rest) is refused at codegen rather + than proven correct on the chip. + +## 4. Host warp simulator (tests/bw_warp_sim.h) + +A warp is a set of fragments (pc, lane mask). A predicated branch whose lanes disagree splits the +fragment. Fragments run one at a time in a chosen order; tests run both lowest-pc-first and +highest-pc-first. Fragments merge only at a BSYNC. + +Errors (first wins, each names the pc): SHFL_SPLIT, SHFL_EXITED, BSYNC_UNARMED, BSSY_REARM, +JOIN_MISMATCH, NONMEMBER, STUCK, NEVER_JOINED, plus range and resource errors. + +BAR.SYNC reached by a split warp is allowed and counted (`bar_split_arrivals`). Basis: the CUDA +programming guide says the CTA barrier counts arriving threads since Volta. Class: INFERRED for +sm_121, not verified by us as a rule. Supporting observation (MEASURED earlier, prime-race +receipts): the production C5 staged sieve has 128 split arrivals at 1e6 in the simulator and is +chip-correct. Fragments stay split after the barrier (a barrier is not a reconvergence point), so a +later SHFL without a BSYNC still fails with SHFL_SPLIT. + +Limits: EX2 and RCP are exact in the simulator, approximate on the chip. LDCU64 is not modelled. +Warp issue counts are a model, not chip time (UNKNOWN how well they track the chip). + +## 5. Hazard / scheduling metadata + +From NAK sm120: BSSY, BSYNC and BClear are Decoupled (variable latency, tracked by the +scoreboard). DERIVED FROM REFERENCE. Omega does not enforce this: `schedule_fixed_pairs` ignores +control ops, and the control words above (stall 1 / stall 5, both yield) are copied from nvcc. +Chip correctness of the shapes we emit is MEASURED (T1, T2, T4); a different placement is not covered. + +## 6. Evidence table + +| # | Claim | Class | Command / receipt | +|---|---|---|---| +| 1 | BSSY/BSYNC words match nvcc; refusals -101..-105 | PROVEN BY TEST + MEASURED | `build/gpu_reconv_test` PASS 220; oracle/cf.{cu,sass} | +| 2 | Region API bounds and refusals (16 ok, 17 refused, SHFL/BAR/EXIT in region, unclosed) | PROVEN BY TEST | gpu_reconv_test PASS 220 | +| 3 | Simulator catches dropped BSYNC, corrupted target, exit past join, early exit then SHFL, split SHFL | PROVEN BY TEST | gpu_reconv_test; `gpu_attention_test --sim --divergent` PASS 124 | +| 4 | R3 diamond, R4 loop break, R5 nested, R6 exit+BAR+SHFL, 16-deep nest: 19 sizes each (incl. partial warps), bit-exact vs CPU oracle on the chip | PROVEN BY TEST (chip) | T1a/GPU-RECONV-0a1812f/receipt.json: OMEGA_GPU_RECONV_PASS, 220 checks, 95 chip cases, 0 failed. Repeat T1b PASS 220 | +| 5 | Chip catches math mutants; dropped BSYNC gives wrong words | MEASURED | T1 receipts: mutants diamond 755, loopbreak 377, nested 110 violations, exit_bar CODEGEN_FAIL; dropped BSYNC 466/1000 words wrong (T1b) | +| 6 | R2 attention j>=ctx branch inside a region, SHFL after the join, bit-correct incl. partial warps | PROVEN BY TEST (chip) | T2-attention: production run PASS 122, sweep PASS 73; `--divergent` run PASS 122, sweep PASS 73; timing PASS 2 | +| 7 | Existing gates unchanged | PROVEN BY TEST (chip) | T3: elementwise 31 checks PASS; cut4b (elementwise, attention, matmul 18 + timing 4, session probe 0/30 first-call failures) PASS; prime-race run_gb10_chip.sh VERDICT PASS (585 audit runs, 1172 passes, mutant caught) in T3b | +| 8 | R1 original divergent sieve (888a011 mark loop in a region) and nested variant bit-identical to oracle | PROVEN BY TEST (chip) | T4: PR_GB10_MARK=divergent and divergent2, 585 runs each (every DESIGN limit plus 1e7), 0 mismatches | +| 9 | BAR.SYNC on a split warp is legal on sm_121 | INFERRED | see section 4 | +| 10 | 16 barrier registers exist on the chip beyond depth 16 / refusal of 17 matches hardware | UNKNOWN | only depth 16 tested | +| 11 | R6 correctness after early exit | not proven: refused at codegen | section 3 | +| 12 | Simulator issue counts predict chip time | UNKNOWN | model only | + +## 7. Performance (R1, honest) + +Setup: `PR_GB10_CTA_BUDGET=64`, limit 1e6, 5 s per run, same session, three marks interleaved, +8 rounds (T4/perf*.json, perf64.log, perf64-repeat.log). Unit: passes per second. MEASURED. + +| mark | per-round passes/s | note | +|---|---|---| +| uniform (C5, production) | 7618 7530 5213 6354 6914 7485 7936 6960 | noisy, 5213..7936 | +| divergent (888a011 loop in a region) | 9155 9147 9134 9127 9158 9107 9113 9162 | 9107..9162 | +| divergent2 (plus prime loop, nested) | 9092 9247 9119 9263 9004 9081 9126 9183 | 9004..9263 | + +The brief's C5 baseline was 9139 passes/s. In this session the C5 uniform build measured lower +and much noisier than that figure, so the table does not support a claim that divergent beats C5. +What it does show: the regioned divergent kernels lose no throughput against the 9139 baseline and +sit flat near 9140. A flat figure that matches the old baseline to within 0.2 percent suggests a +fixed per-pass cost (launch, host base-prime sieve, marker wait) dominates at this size +(INFERRED, not investigated; out of scope). Why the uniform run is noisier is UNKNOWN. +Kernel size: 88 instructions for all three; registers 29 (uniform) vs 26 (divergent). +The warp-simulator model predicts divergent warp issues 2.53M (util .656) and divergent2 2.01M +(util .830) against C5 global 2.89M (util .774) at 1e6 (model only, not chip time). +Correctness was the objective; this section is reported for completeness. + +## 7a. Invariant for new GPU code (Drake, 2026-10-05) + +Any new lane-divergent code emitted for the Blackwell backend must either + +1. wrap the divergent stretch in `region_begin` -> `region_exit` -> `region_join` and pass the host warp simulator (`build/gpu_reconv_test` plus the suite that exercises the kernel), or +2. show in the kernel comment why every branch is provably warp-uniform (all 32 lanes take the same path), as the C5 sieve mark loop does. + +A SHFL or BAR.SYNC that depends on a lane-divergent branch without an enclosing region is a defect, not a style choice: on the chip the dropped-BSYNC mutant returned 466 of 1000 words wrong with no error (evidence/gpu-reconv-308/T1b-*). The emitter refuses the cases it can see (section 3); the simulator catches the rest. Reviewers should ask for one of the two proofs on every PR that adds a predicated branch. + +## 8. What was not changed + +Production C5 sieve, branch-free attention, test thresholds, omega main. Divergent kernels are +opt-in (`PR_GB10_MARK`, `--divergent`). + +## 9. Limitations + +- Only the shapes in the probes are chip-verified. BSSY/BSYNC placement other than "BSSY before + the divergence, every exit to the BSYNC, SHFL/BAR after the join" is not covered. +- R6 is refused, not proven. Barrier registers beyond 16 deep untested (and refused at 17). +- Control-op latency metadata is documented, not enforced. +- Chip runs were taken on one machine (GB10, driver and kernel in each receipt's device.txt). + +## 10. Reproduce + + make build/gpu_reconv_test PHYSICS_DIR=../physics-reconv-lock + sh tools/run_gpu_reconv_chip.sh [OUT] [-- --observe-dropped-join] + build/gpu_attention_test [--divergent] [--sweep] --out receipt.json + PR_GB10_MARK=divergent bench/prime_race/build/gb10_native --limit 1000000 --audit-passes 5 + +Chip runs need a quiet window and the GPU lock (/tmp/aien-gb10.lock). diff --git a/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/SHA256SUMS b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/SHA256SUMS new file mode 100644 index 000000000..25b5cd116 --- /dev/null +++ b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/SHA256SUMS @@ -0,0 +1,10 @@ +2455c607827def7a1cc5d96ce3759e8280bb254d6ed7e9409072faf9cc949a56 build.log +8ca14df583da96c66c4d7dc1a301c39e0a19747c1b0842fdee62a5e7e2cf1260 bw_warp_sim.h +6ecff2b05540a8a79777b86298a87ea06240f57cd5a6e0132fb05ee78774915d device.txt +0cfa2a3922fa89022a2a0b77734a09914ccb4aef9221b14a182bb436cbd6e744 gpu_reconv_test.c +ce6aa4077426ff76120276424103843f5e6641d312255df17b0ef91057ffefce omega_blackwell_codegen.c +0da10397dd4ee813633afa425192d126d056684fdd998585fea666995a7c50ea omega_bw_reconv.h +bd2346fb86eed5e3be35c49c970c25adf1621043d6e4e4ba02e990375e5859c7 omega_gpu_elementwise_api.c +4d9eb1b5fd55e168f0cd4a96e906957376d88bb5efbe1f08633fcdfa52ce8fba omega_gpu_elementwise_api.h +51cfb8bf97e6abd41b51dbdac32cb57c97eb7cff586d26d1d143a7e64aac0fd0 receipt.json +e39d8ee59c455f39b85b89a4d2b14af4542c4f2b9a9b66ea06b0740d5f6e3d21 run.log diff --git a/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/build.log b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/build.log new file mode 100644 index 000000000..8eab8a45c --- /dev/null +++ b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/build.log @@ -0,0 +1,2 @@ +ar rcs build/libomega_gpu.a build/sha256.o build/omega_canonical.o build/omega_validate.o build/omega_core.o build/omega_codec.o build/aarch64_encoder.o build/aarch64_decoder.o build/omega_realize.o build/omega_exec.o build/omega_self_host.o build/omega_verify.o build/omega_program.o build/omega_synthesis.o build/omega_library.o build/omega_discovery.o build/omega_machine.o build/omega_realize_synth.o build/omega_matvec.o build/omega_accelerator.o build/omega_accelerator_world.o build/omega_vector.o build/omega_blackwell_encoder.o build/omega_blackwell_qmd.o build/omega_blackwell_realize.o build/omega_blackwell_submit.o build/omega_blackwell_engine.o build/omega_gpu_engine.o build/omega_blackwell_matmul.o build/omega_blackwell_codegen.o build/omega_gpu_session.o build/omega_gpu_matmul_api.o build/omega_gpu_elementwise_api.o build/omega_gpu_attention_api.o build/omega_gpu_wait.o build/omega_evidence.o build/m16_native.o build/nvrm.o build/omega_program_ir.o build/omega_vc_bridge.o build/omega_resolve.o build/omega_receipt.o build/omega_blake3.o build/omega_vcstore.o +cc -std=gnu11 -Wall -Wextra -Werror -MMD -MP -D_GNU_SOURCE -O2 -Isrc -I../physics-reconv-lock/m16 -I../physics-reconv-lock/nvrm -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/src/common/sdk/nvidia/inc -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/kernel-open/common/inc -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/kernel-open/nvidia-uvm -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/src/nvidia/arch/nvalloc/unix/include -Itests -o build/gpu_reconv_test tests/gpu_reconv_test.c build/libomega_gpu.a -lpthread -lm diff --git a/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/bw_warp_sim.h b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/bw_warp_sim.h new file mode 100644 index 000000000..328094cb0 --- /dev/null +++ b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/bw_warp_sim.h @@ -0,0 +1,313 @@ +/* bw_warp_sim.h: host SIMT simulator for the Blackwell IR (omega #308). + * + * Models what the earlier per-thread simulators could not: a warp of 32 lanes with an + * active mask, divergence at predicated branches, structured reconvergence through the + * barrier registers (BSSY arms Bn for the lanes executing it and names the join; BSYNC + * parks arriving lanes until every armed lane that has not exited is there, then the + * warp continues as one), nested regions, and lane participation in SHFL and BAR.SYNC. + * + * A warp is a set of fragments (pc, lane mask). A predicated branch whose lanes disagree + * splits the fragment; the scheduler then runs one fragment at a time in a chosen order + * (lowest pc first or highest pc first; tests run both), which is the freedom the chip's + * scheduler also has. Fragments only merge at a BSYNC. Errors (all fatal, first one wins): + * WS_E_SHFL_SPLIT a SHFL reached by a fragment that is not the whole live warp + * (the chip reads inactive lanes: undefined; attention 2026-10-04) + * WS_E_SHFL_EXITED a SHFL on a warp some of whose lanes already exited (undefined too) + * WS_E_BAR_SPLIT (defined, never raised) a BAR.SYNC reached by a split warp is allowed: + * since Volta the CTA barrier counts arriving threads, not warps (CUDA + * programming guide, independent thread scheduling; INFERRED for sm_121, + * not chip-verified by us). The simulator parks each fragment, releases + * them together, counts the event in bar_split_arrivals, and leaves the + * fragments split: a barrier is not a reconvergence point, so a SHFL after + * it still fails with SHFL_SPLIT unless a BSYNC rejoined the warp. + * WS_E_BSYNC_UNARMED BSYNC on a barrier no BSSY armed + * WS_E_BSSY_REARM BSSY on a barrier still armed (lanes may still be heading to its join) + * WS_E_JOIN_MISMATCH BSYNC at an address other than the one the BSSY named minus one + * WS_E_NONMEMBER a lane reached BSYNC Bn without having executed its BSSY + * WS_E_STUCK lanes parked at a BSYNC that can never complete (a join was dropped, + * an exit jumped past it, or a target was corrupted) + * WS_E_NEVER_JOINED the warp finished with a barrier still armed + * WS_E_SELF_BRANCH, WS_E_PC, WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS + * Every error names the pc. Memory: global addresses are host pointers (optionally range + * checked); shared memory is a per-CTA buffer poisoned at CTA start so a read of a word + * nobody wrote shows up in the oracle comparison. + * + * Per-lane ALU semantics are those of the two earlier simulators (tests/gpu_attention_test.c, + * bench/prime_race/tests/gb10_sieve_host_test.c). EX2 and RCP are exact here; the chip's + * are approximate. LDCU64 (memory descriptor) is not modelled. */ +#ifndef BW_WARP_SIM_H +#define BW_WARP_SIM_H + +#include "omega_blackwell_codegen.h" +#include +#include +#include +#include +#include + +#define WS_WARP 32 +#define WS_MAX_THREADS 256 +#define WS_MAX_WARPS (WS_MAX_THREADS / WS_WARP) +#define WS_MAX_FRAGS 64 +#define WS_NBAR BW_RECONV_MAX_BAR +#define WS_MAX_RANGES 8 + +enum { + WS_OK = 0, WS_E_SHFL_SPLIT, WS_E_SHFL_EXITED, WS_E_BAR_SPLIT, WS_E_BSYNC_UNARMED, WS_E_BSSY_REARM, + WS_E_JOIN_MISMATCH, WS_E_NONMEMBER, WS_E_STUCK, WS_E_NEVER_JOINED, WS_E_SELF_BRANCH, WS_E_PC, + WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS, WS_E_BAR_TIMEOUT, WS_E_STEPS +}; +static const char *ws_err_name(int e) { + static const char *const n[] = { "OK", "SHFL_SPLIT", "SHFL_EXITED", "BAR_SPLIT", "BSYNC_UNARMED", "BSSY_REARM", + "JOIN_MISMATCH", "NONMEMBER", "STUCK", "NEVER_JOINED", "SELF_BRANCH", "PC", "OP", "SHIFT", "MEM", "SMEM", + "FRAGS", "BAR_TIMEOUT", "STEPS" }; + return (e >= 0 && e < (int)(sizeof n / sizeof n[0])) ? n[e] : "?"; +} + +typedef struct { uint32_t r[256]; uint8_t p0; } WsLane; +typedef struct { int pc; uint32_t mask; int parked; /* waiting at a BAR.SYNC */ } WsFrag; +typedef struct { int valid; int target; uint32_t members, arrived; } WsBar; +typedef struct { + WsLane lane[WS_WARP]; + WsFrag frag[WS_MAX_FRAGS]; + int nfrag; + WsBar bar[WS_NBAR]; + uint32_t exited; +} WsWarp; +typedef struct { uintptr_t lo, hi; } WsRange; + +typedef struct { + const BlackwellIRProgram *p; + uint32_t cbank[256]; /* constant bank 0 words (byte offset / 4) */ + uint32_t threads; /* per CTA, a multiple of 32, <= WS_MAX_THREADS */ + uint8_t *smem; /* per-CTA shared memory buffer (poisoned at CTA start) */ + uint32_t smem_bytes; + WsRange ranges[WS_MAX_RANGES]; int nranges; /* when > 0, every global access must fall in one */ + int order; /* 0 = lowest pc first, 1 = highest pc first */ + int trace; /* print every issue of warp 0 of CTA (0,0) */ + /* results */ + int err; int err_pc; char msg[200]; + uint64_t warp_issues; /* fragment-instruction issues (the chip's cost unit for divergence) */ + uint64_t lane_issues; /* lanes active over those issues */ + uint64_t splits; /* branches that split a fragment */ + uint64_t bar_split_arrivals; /* BAR.SYNC reached by a fragment that is not the whole live warp */ + int max_frags, peak_bars; +} WsSim; + +static void ws_fail(WsSim *s, int err, int pc, const char *what) { + if (s->err) return; + s->err = err; s->err_pc = pc; + snprintf(s->msg, sizeof s->msg, "%s at pc %d: %s", ws_err_name(err), pc, what ? what : ""); +} + +static uint32_t ws_rd(const WsLane *l, int v, const BlackwellIRProgram *p) { if (v < 0) return 0; int ph = p->regalloc.vreg_to_phys[v]; return ph < 0 ? 0 : l->r[ph & 0xff]; } +static uint64_t ws_rd64(const WsLane *l, int v, const BlackwellIRProgram *p) { if (v < 0) return 0; int ph = p->regalloc.vreg_to_phys[v] & 0xff; return (uint64_t)l->r[ph] | ((uint64_t)l->r[(ph + 1) & 0xff] << 32); } +static void ws_wr(WsLane *l, int v, uint32_t x, const BlackwellIRProgram *p) { if (v < 0) return; int ph = p->regalloc.vreg_to_phys[v]; if (ph >= 0) l->r[ph & 0xff] = x; } +static void ws_wr64(WsLane *l, int v, uint64_t x, const BlackwellIRProgram *p) { if (v < 0) return; int ph = p->regalloc.vreg_to_phys[v] & 0xff; l->r[ph] = (uint32_t)x; l->r[(ph + 1) & 0xff] = (uint32_t)(x >> 32); } +static float ws_u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } +static uint32_t ws_f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } +static uint32_t ws_lut3(uint32_t lut, uint32_t a, uint32_t b, uint32_t c) { + uint32_t r = 0; + for (int i = 0; i < 8; i++) if (lut >> i & 1) r |= ((i & 4) ? a : ~a) & ((i & 2) ? b : ~b) & ((i & 1) ? c : ~c); + return r; +} +static int ws_mem_ok(WsSim *s, uint64_t a, unsigned bytes, int pc) { + if (s->nranges == 0) return 1; + for (int i = 0; i < s->nranges; i++) if (a >= s->ranges[i].lo && a + bytes <= s->ranges[i].hi) return 1; + ws_fail(s, WS_E_MEM, pc, "global access outside the declared buffers"); return 0; +} +static uint32_t *ws_smem(WsSim *s, uint32_t a, int pc) { + if ((a & 3) || a + 4 > s->smem_bytes) { ws_fail(s, WS_E_SMEM, pc, "shared access past the declared size"); return NULL; } + return (uint32_t *)(s->smem + a); +} +static int ws_popc(uint32_t m) { return __builtin_popcount(m); } + +/* One lane, one non-control instruction. */ +static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc, uint32_t tid, uint32_t cx, uint32_t cy) { + const BlackwellIRProgram *p = s->p; + uint32_t a = ws_rd(l, in->src1_vreg, p), b = ws_rd(l, in->src2_vreg, p), c = ws_rd(l, in->src3_vreg, p); + switch (in->op) { + case BW_IR_NOP: break; + case BW_IR_S2R: ws_wr(l, in->dst_vreg, in->imm == BW_SR_TID_X ? tid : in->imm == BW_SR_CTAID_X ? cx : in->imm == BW_SR_CTAID_Y ? cy : 0, p); break; + case BW_IR_LDC: ws_wr(l, in->dst_vreg, s->cbank[(in->imm / 4) & 0xff], p); break; + case BW_IR_LDC64: ws_wr64(l, in->dst_vreg, (uint64_t)s->cbank[(in->imm / 4) & 0xff] | ((uint64_t)s->cbank[(in->imm / 4 + 1) & 0xff] << 32), p); break; + case BW_IR_LDCU64: case BW_IR_LDCU: break; /* memory descriptor: not modelled */ + case BW_IR_MOV_IMM: ws_wr(l, in->dst_vreg, in->imm, p); break; + case BW_IR_MOV_RZ: ws_wr(l, in->dst_vreg, 0, p); break; + case BW_IR_IMAD: ws_wr(l, in->dst_vreg, in->src2_vreg >= 0 ? a * b + c : a * in->imm + c, p); break; + case BW_IR_IMAD_WIDE: ws_wr64(l, in->dst_vreg, (uint64_t)a * in->imm + ws_rd64(l, in->src3_vreg, p), p); break; + case BW_IR_IMAD_HI_U32: ws_wr(l, in->dst_vreg, (uint32_t)(((uint64_t)a * b) >> 32) + c, p); break; + case BW_IR_IADD3: ws_wr(l, in->dst_vreg, a + b, p); break; /* the encoder's IADD3 has Rc = RZ */ + case BW_IR_SHF_R: ws_wr(l, in->dst_vreg, a >> (in->imm & 31), p); break; + case BW_IR_SHF_L_U32: if (b >= 32) { ws_fail(s, WS_E_SHIFT, pc, "SHF.L.U32 amount >= 32 (not chip-verified)"); return; } ws_wr(l, in->dst_vreg, a << b, p); break; + case BW_IR_LOP3_AND: ws_wr(l, in->dst_vreg, a & in->imm, p); break; + case BW_IR_LOP3_XOR: ws_wr(l, in->dst_vreg, a ^ b, p); break; + case BW_IR_LOP3_LUT: ws_wr(l, in->dst_vreg, ws_lut3(in->imm & 0xff, a, b, c), p); break; + case BW_IR_ISETP_GE_U32: l->p0 = a >= b; break; + case BW_IR_LDG_E: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; uint32_t x; memcpy(&x, (const void *)(uintptr_t)ad, 4); ws_wr(l, in->dst_vreg, x, p); break; } + case BW_IR_LDG_E_U16: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 2, pc)) return; uint16_t x; memcpy(&x, (const void *)(uintptr_t)ad, 2); ws_wr(l, in->dst_vreg, x, p); break; } + case BW_IR_STG_E: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; memcpy((void *)(uintptr_t)ad, &b, 4); break; } + case BW_IR_LDS32: { uint32_t *m = ws_smem(s, a, pc); if (!m) return; ws_wr(l, in->dst_vreg, *m, p); break; } + case BW_IR_STS32: { uint32_t *m = ws_smem(s, a, pc); if (!m) return; *m = b; break; } + case BW_IR_FADD: ws_wr(l, in->dst_vreg, ws_f2u(ws_u2f(a) + ws_u2f(b)), p); break; + case BW_IR_FSUB: ws_wr(l, in->dst_vreg, ws_f2u(ws_u2f(a) - ws_u2f(b)), p); break; + case BW_IR_FMUL: ws_wr(l, in->dst_vreg, ws_f2u(ws_u2f(a) * ws_u2f(b)), p); break; + case BW_IR_FFMA: ws_wr(l, in->dst_vreg, ws_f2u(fmaf(ws_u2f(a), ws_u2f(b), ws_u2f(c))), p); break; + case BW_IR_FMNMX_MAX: ws_wr(l, in->dst_vreg, ws_f2u(fmaxf(ws_u2f(a), ws_u2f(b))), p); break; + case BW_IR_FMNMX_MIN: ws_wr(l, in->dst_vreg, ws_f2u(fminf(ws_u2f(a), ws_u2f(b))), p); break; + case BW_IR_MUFU_EX2: ws_wr(l, in->dst_vreg, ws_f2u(exp2f(ws_u2f(a))), p); break; + case BW_IR_MUFU_RCP: ws_wr(l, in->dst_vreg, ws_f2u(1.0f / ws_u2f(a)), p); break; + case BW_IR_MUFU_RSQ: ws_wr(l, in->dst_vreg, ws_f2u(1.0f / sqrtf(ws_u2f(a))), p); break; + default: ws_fail(s, WS_E_OP, pc, "opcode not modelled"); return; + } +} + +static int ws_pick(const WsSim *s, const WsWarp *w) { + int best = -1; + for (int i = 0; i < w->nfrag; i++) { + if (w->frag[i].mask == 0 || w->frag[i].parked) continue; + if (best < 0) { best = i; continue; } + if (s->order == 0 ? w->frag[i].pc < w->frag[best].pc : w->frag[i].pc > w->frag[best].pc) best = i; + } + return best; +} +static void ws_drop_frag(WsWarp *w, int i) { w->frag[i] = w->frag[--w->nfrag]; } +static int ws_add_frag(WsSim *s, WsWarp *w, int pc, uint32_t mask) { + if (mask == 0) return 0; + if (w->nfrag >= WS_MAX_FRAGS) { ws_fail(s, WS_E_FRAGS, pc, "too many fragments"); return -1; } + w->frag[w->nfrag].pc = pc; w->frag[w->nfrag].mask = mask; w->frag[w->nfrag].parked = 0; w->nfrag++; + if (w->nfrag > s->max_frags) s->max_frags = w->nfrag; + return 0; +} + +/* Runs one warp until it has no runnable fragment (finished, parked at BAR.SYNC, or stuck). + * Returns 1 when parked at BAR.SYNC, 0 when finished, -1 on error. */ +static int ws_run_warp(WsSim *s, WsWarp *w, uint32_t warp, uint32_t cx, uint32_t cy, int trace) { + const BlackwellIRProgram *p = s->p; + const uint32_t live_all = 0xffffffffu; + for (uint64_t guard = 0;; guard++) { + if (s->err) return -1; + if (guard > 2000000000ull) { ws_fail(s, WS_E_STEPS, -1, "step budget"); return -1; } + int fi = ws_pick(s, w); + if (fi < 0) { + /* nothing runnable: parked at the CTA barrier, finished, or lanes waiting at a BSYNC + * that can never complete */ + for (int i = 0; i < w->nfrag; i++) if (w->frag[i].parked) return 1; + for (int b = 0; b < WS_NBAR; b++) if (w->bar[b].valid) { + if (w->bar[b].arrived) { ws_fail(s, WS_E_STUCK, w->bar[b].target - 1, "lanes parked at BSYNC, the rest never arrive"); return -1; } + ws_fail(s, WS_E_NEVER_JOINED, w->bar[b].target - 1, "warp finished with a barrier still armed"); return -1; + } + return 0; + } + WsFrag *f = &w->frag[fi]; + int pc = f->pc; + if (pc < 0 || (size_t)pc >= p->count) { ws_fail(s, WS_E_PC, pc, "pc outside the program"); return -1; } + const BlackwellIRInsn *in = &p->insns[pc]; + uint32_t mask = f->mask; + s->warp_issues++; s->lane_issues += (uint64_t)ws_popc(mask); + if (trace) printf("W%u pc=%d op=%d mask=%08x frags=%d\n", warp, pc, (int)in->op, mask, w->nfrag); + uint32_t live = live_all & ~w->exited; + uint32_t pmask = 0; + for (int L = 0; L < WS_WARP; L++) if ((mask >> L & 1) && w->lane[L].p0) pmask |= 1u << L; + uint32_t cond = in->predicate_p0 ? (in->predicate_not ? (mask & ~pmask) : (mask & pmask)) : mask; + switch (in->op) { + case BW_IR_EXIT: { + w->exited |= cond; + f->mask = mask & ~cond; + if (f->mask == 0) ws_drop_frag(w, fi); else f->pc = pc + 1; + break; + } + case BW_IR_BRA: { + int32_t delta = (int32_t)in->imm; + uint32_t taken = cond, fall = mask & ~cond; + if (taken && delta == 0) { ws_fail(s, WS_E_SELF_BRANCH, pc, "branch to itself"); return -1; } + if (taken && fall) s->splits++; + if (taken == 0) { f->pc = pc + 1; break; } + if (fall == 0) { f->pc = pc + delta; break; } + f->mask = fall; f->pc = pc + 1; + if (ws_add_frag(s, w, pc + delta, taken) < 0) return -1; + break; + } + case BW_IR_BSSY: { + WsBar *b = &w->bar[in->bar_reg & (WS_NBAR - 1)]; + if (in->bar_reg >= WS_NBAR) { ws_fail(s, WS_E_OP, pc, "barrier id"); return -1; } + if (b->valid) { ws_fail(s, WS_E_BSSY_REARM, pc, "BSSY on an armed barrier"); return -1; } + b->valid = 1; b->target = pc + (int32_t)in->imm; b->members = cond; b->arrived = 0; + int armed = 0; for (int k = 0; k < WS_NBAR; k++) armed += w->bar[k].valid; + if (armed > s->peak_bars) s->peak_bars = armed; + f->pc = pc + 1; + break; + } + case BW_IR_BSYNC: { + if (in->bar_reg >= WS_NBAR) { ws_fail(s, WS_E_OP, pc, "barrier id"); return -1; } + WsBar *b = &w->bar[in->bar_reg]; + if (!b->valid) { ws_fail(s, WS_E_BSYNC_UNARMED, pc, "BSYNC without a BSSY"); return -1; } + if (cond & ~b->members) { ws_fail(s, WS_E_NONMEMBER, pc, "a lane at BSYNC did not execute the BSSY"); return -1; } + b->arrived |= cond; + uint32_t rest = mask & ~cond; /* a predicated-off lane walks on */ + if (rest) { f->mask = rest; f->pc = pc + 1; } else ws_drop_frag(w, fi); + if (b->arrived == (b->members & ~w->exited)) { + if (b->target != pc + 1) { ws_fail(s, WS_E_JOIN_MISMATCH, pc, "BSSY named a different join point"); return -1; } + uint32_t joined = b->arrived; b->valid = 0; b->arrived = 0; b->members = 0; + if (ws_add_frag(s, w, pc + 1, joined) < 0) return -1; + } + break; + } + case BW_IR_SHFL_DOWN: { + if (w->exited) { ws_fail(s, WS_E_SHFL_EXITED, pc, "SHFL on a warp with exited lanes"); return -1; } + if (mask != live) { ws_fail(s, WS_E_SHFL_SPLIT, pc, "SHFL on a split warp"); return -1; } + uint32_t src[WS_WARP]; + for (int L = 0; L < WS_WARP; L++) src[L] = ws_rd(&w->lane[L], in->src1_vreg, p); + for (int L = 0; L < WS_WARP; L++) { int from = L + (int)(in->imm & 31); ws_wr(&w->lane[L], in->dst_vreg, from < WS_WARP ? src[from] : src[L], p); } + f->pc = pc + 1; + break; + } + case BW_IR_BAR_SYNC: { + if (mask != live) s->bar_split_arrivals++; + f->parked = 1; /* released by ws_run when every live lane of the CTA has arrived */ + break; + } + default: { + for (int L = 0; L < WS_WARP; L++) if (mask >> L & 1) { + ws_lane_exec(s, &w->lane[L], in, pc, warp * WS_WARP + (uint32_t)L, cx, cy); + if (s->err) return -1; + } + f->pc = pc + 1; + break; + } + } + } +} + +/* Runs every CTA of the grid. Returns s->err (0 = clean). */ +static int ws_run(WsSim *s, uint32_t grid_x, uint32_t grid_y) { + s->err = 0; s->msg[0] = 0; s->warp_issues = s->lane_issues = s->splits = s->bar_split_arrivals = 0; s->max_frags = 0; s->peak_bars = 0; + if (!s->p || s->threads == 0 || s->threads % WS_WARP || s->threads > WS_MAX_THREADS) { ws_fail(s, WS_E_OP, -1, "bad simulator configuration"); return s->err; } + static WsWarp warps[WS_MAX_WARPS]; + uint32_t nw = s->threads / WS_WARP; + for (uint32_t cy = 0; cy < grid_y && !s->err; cy++) for (uint32_t cx = 0; cx < grid_x && !s->err; cx++) { + memset(warps, 0, sizeof(WsWarp) * nw); + if (s->smem && s->smem_bytes) for (uint32_t i = 0; i + 4 <= s->smem_bytes; i += 4) { uint32_t poison = 0xfeedfaceu; memcpy(s->smem + i, &poison, 4); } + for (uint32_t w = 0; w < nw; w++) { warps[w].nfrag = 1; warps[w].frag[0].pc = 0; warps[w].frag[0].mask = 0xffffffffu; warps[w].frag[0].parked = 0; } + int trace = s->trace && cx == 0 && cy == 0; + /* Barrier phases: every warp runs until all of its fragments are parked at BAR.SYNC or it + * has finished (exited lanes do not hold the barrier up: INFERRED, see WS_E_BAR_SPLIT). + * Then every parked fragment is released and the next phase starts. */ + for (int phase = 0; ; phase++) { + int parked = 0; + for (uint32_t w = 0; w < nw && !s->err; w++) { + if (warps[w].nfrag == 0) continue; + int r = ws_run_warp(s, &warps[w], w, cx, cy, trace && w == 0); + if (r < 0) break; + if (r == 1) parked++; + } + if (s->err || parked == 0) break; /* error, or every warp finished */ + for (uint32_t w = 0; w < nw; w++) for (int i = 0; i < warps[w].nfrag; i++) + if (warps[w].frag[i].parked) { warps[w].frag[i].parked = 0; warps[w].frag[i].pc++; } + if (phase > 100000) { ws_fail(s, WS_E_BAR_TIMEOUT, -1, "barrier phases"); break; } + } + } + return s->err; +} + +#endif /* BW_WARP_SIM_H */ diff --git a/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/device.txt b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/device.txt new file mode 100644 index 000000000..fe6e247aa --- /dev/null +++ b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/device.txt @@ -0,0 +1,5 @@ +Linux spark-b87b 7.0.0-1019-nvidia #19~24.04.2-Ubuntu SMP PREEMPT_DYNAMIC Sun Sep 6 18:35:11 UTC 2 aarch64 aarch64 aarch64 GNU/Linux +NVIDIA GB10, 580.173.02 +omega 0a1812f44eaa3dcf8fe5a914d1dd65cfaccbbcd7 (1 uncommitted paths) +physics 6d7cf0d4d8eb2cda7b512100ff6058e25dbb3ddf +2026-10-05T23:14:40Z diff --git a/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/gpu_reconv_test.c b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/gpu_reconv_test.c new file mode 100644 index 000000000..227e3b658 --- /dev/null +++ b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/gpu_reconv_test.c @@ -0,0 +1,470 @@ +/* gpu_reconv_test: structured warp reconvergence (omega #308) on Omega's own Blackwell codegen. + * + * ./gpu_reconv_test --host-only encoder golden words, region bookkeeping, codegen refusals, + * nvdisasm listing (when /usr/local/cuda/bin/nvdisasm exists), + * every probe kernel through the SIMT warp simulator against + * the host oracle under both fragment orders, the mutation + * controls, scoreboard and control-edge walks. No device. + * ./gpu_reconv_test --out receipt.json chip gate: host part first (a kernel the simulator rejects + * never gets chip time), then every probe on the chip against + * the oracle, bit-exact, then each math mutant must be caught. + * --observe-dropped-join chip only, optional: also launch the dropped-BSYNC mutant of + * one probe and record what the chip did (an observation, not + * a gate; the simulator rejects that kernel first). + * + * Probes (src/omega_gpu_elementwise_api.c, gen_rc_*): R3 diamond, R4 loop break, R5 nested, + * R6 lanes past the end with STS/BAR/LDS + SHFL, and the 16-deep barrier-register bound. + * Oracle: rc_oracle below; every comparison is exact (u32). Launcher chunking (64 CTAs x 128 + * threads per launch, P0 = words in the chunk) is part of the oracle, so a clamp to the wrong + * chunk end shows up. */ +#include "omega_gpu_elementwise_api.h" +#include "omega_blackwell_codegen.h" +#include "omega_bw_reconv.h" +#include "omega_gpu_session.h" +#include "bw_warp_sim.h" +#include +#include +#include +#include +#include + +static int g_checks, g_failed; +#define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) + +static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } + +typedef struct { OmegaGpuEwOp op; const char *name; const char *reg; } Probe; +static const Probe PROBES[] = { + { OMEGA_GPU_EW_RC_DIAMOND, "rc_diamond", "R3" }, + { OMEGA_GPU_EW_RC_LOOPBREAK, "rc_loopbreak", "R4" }, + { OMEGA_GPU_EW_RC_NESTED, "rc_nested", "R5" }, + { OMEGA_GPU_EW_RC_EXIT_BAR, "rc_exit_bar", "R6" }, + { OMEGA_GPU_EW_RC_DEPTH16, "rc_depth16", "B0..B15" }, +}; +#define NPROBES (sizeof PROBES / sizeof PROBES[0]) +static const uint32_t NS[] = { 1, 5, 31, 32, 33, 63, 64, 65, 96, 127, 128, 129, 200, 1000, 4096, 8191, 8192, 8193, 20000 }; +#define NNS (sizeof NS / sizeof NS[0]) +#define CHUNK (OMEGA_GPU_EW_MAX_CTAS * OMEGA_GPU_EW_THREADS) + +/* ----------------------------------------------------------------- oracle */ +static uint32_t count_loop(uint32_t x, uint32_t y, int math, uint32_t *v_out) { + uint32_t v = x & 255u, st = (y & 15u) + 1u, cnt = 0, bound = math ? 255u : 256u; + while (v < bound) { v += st; cnt++; } + *v_out = v; return cnt; +} +static uint32_t rc_r(OmegaGpuEwOp op, int math, uint32_t x, uint32_t y, uint32_t partner_x) { + uint32_t v, cnt; + switch (op) { + case OMEGA_GPU_EW_RC_DIAMOND: + if (x >= y) { uint32_t t = 3u * x + y, u = t ^ x, r = 5u * u + 1u; r += r >> 3; return math ? r + 1u : r; } + return x + 1u; + case OMEGA_GPU_EW_RC_LOOPBREAK: cnt = count_loop(x, y, math, &v); return cnt * 256u + v; + case OMEGA_GPU_EW_RC_NESTED: if (x & 1u) return 7u; cnt = count_loop(x, y, math, &v); return cnt * 256u + v + 100u; + case OMEGA_GPU_EW_RC_EXIT_BAR: return 3u * x + (partner_x | 1u); + case OMEGA_GPU_EW_RC_DEPTH16: return x + (x < y ? (uint32_t)BW_RECONV_MAX_BAR : 0u); + default: return 0xdeadbeefu; + } +} +static void rc_oracle(OmegaGpuEwOp op, int math, uint32_t n, const uint32_t *a, const uint32_t *b, uint32_t *out) { + for (uint32_t i0 = 0; i0 < n; i0 += CHUNK) { + uint32_t ni = n - i0 < CHUNK ? n - i0 : CHUNK; + uint32_t threads = ((ni + OMEGA_GPU_EW_THREADS - 1) / OMEGA_GPU_EW_THREADS) * OMEGA_GPU_EW_THREADS; + uint32_t *r = malloc((size_t)threads * 4); + for (uint32_t t = 0; t < threads; t++) { + uint32_t idx = t < ni ? t : ni - 1, pt = t ^ 1u, pidx = pt < ni ? pt : ni - 1; + r[t] = rc_r(op, math, a[i0 + idx], b[i0 + idx], a[i0 + pidx]); + } + for (uint32_t i = 0; i < ni; i++) { uint32_t nb = (i & 31u) == 31u ? i : i + 1u; out[i0 + i] = r[i] + 4099u * r[nb]; } + free(r); + } +} +/* An LCG's low bits cycle quickly (bit 0 has period 2), and the probes key on low bits + * (x & 255, y & 15, x & 1), so each draw goes through a mixing step. */ +static uint32_t mix(uint32_t r) { r ^= r >> 16; r *= 0x45d9f3bu; r ^= r >> 16; r *= 0x45d9f3bu; r ^= r >> 16; return r; } +static void fill(uint32_t *a, uint32_t *b, uint32_t n, uint32_t seed) { + for (uint32_t i = 0; i < n; i++) { a[i] = mix(lcg(&seed)); b[i] = mix(lcg(&seed)); } +} +static size_t diff_count(const uint32_t *got, const uint32_t *want, uint32_t n, uint32_t *first) { + size_t bad = 0; *first = n; + for (uint32_t i = 0; i < n; i++) if (got[i] != want[i]) { if (!bad) *first = i; bad++; } + return bad; +} + +/* --------------------------------------------------------- simulator driver */ +static uint8_t g_smem[1024]; +/* Mirrors run_1d: 64 CTAs x 128 threads per launch, P0 = words in the chunk. Returns the + * simulator's error code (0 clean); the output words are written to out. */ +static int ws_probe(const BlackwellIRProgram *p, uint32_t n, const uint32_t *a, const uint32_t *b, uint32_t *out, int order, WsSim *stats) { + for (uint32_t i = 0; i < n; i++) out[i] = 0xffbadbadu; + uint64_t wi = 0, li = 0, sp = 0; int mf = 0, pb = 0; + for (uint32_t i0 = 0; i0 < n; i0 += CHUNK) { + uint32_t ni = n - i0 < CHUNK ? n - i0 : CHUNK; + WsSim s; memset(&s, 0, sizeof s); + s.p = p; s.threads = OMEGA_GPU_EW_THREADS; s.smem = g_smem; s.smem_bytes = sizeof g_smem; s.order = order; + uint64_t aa = (uintptr_t)(a + i0), ba = (uintptr_t)(b + i0), ca = (uintptr_t)(out + i0); + s.cbank[0x380 / 4] = (uint32_t)aa; s.cbank[0x380 / 4 + 1] = (uint32_t)(aa >> 32); + s.cbank[0x388 / 4] = (uint32_t)ba; s.cbank[0x388 / 4 + 1] = (uint32_t)(ba >> 32); + s.cbank[0x390 / 4] = (uint32_t)ca; s.cbank[0x390 / 4 + 1] = (uint32_t)(ca >> 32); + s.cbank[0x398 / 4] = ni; + s.ranges[0] = (WsRange){ (uintptr_t)aa, (uintptr_t)aa + (size_t)ni * 4 }; + s.ranges[1] = (WsRange){ (uintptr_t)ba, (uintptr_t)ba + (size_t)ni * 4 }; + s.ranges[2] = (WsRange){ (uintptr_t)ca, (uintptr_t)ca + (size_t)ni * 4 }; + s.nranges = 3; + int err = ws_run(&s, (ni + OMEGA_GPU_EW_THREADS - 1) / OMEGA_GPU_EW_THREADS, 1); + wi += s.warp_issues; li += s.lane_issues; sp += s.splits; + if (s.max_frags > mf) mf = s.max_frags; + if (s.peak_bars > pb) pb = s.peak_bars; + if (err) { if (stats) *stats = s; return err; } + } + if (stats) { memset(stats, 0, sizeof *stats); stats->warp_issues = wi; stats->lane_issues = li; stats->splits = sp; stats->max_frags = mf; stats->peak_bars = pb; } + return 0; +} +static int codegen(OmegaGpuEwOp op, int mutant, BlackwellIRProgram *p, OmegaBlackwellKernel *k) { + memset(k, 0, sizeof *k); + return omega_gpu_elementwise_codegen_ir(op, mutant, p, k); +} +static int is_reconv_err(int e) { + return e == WS_E_SHFL_SPLIT || e == WS_E_BAR_SPLIT || e == WS_E_STUCK || e == WS_E_NEVER_JOINED || e == WS_E_JOIN_MISMATCH || + e == WS_E_BSYNC_UNARMED || e == WS_E_BSSY_REARM || e == WS_E_NONMEMBER || e == WS_E_SHFL_EXITED; +} + +/* --------------------------------------------------------- static walks */ +/* Scoreboard walk (as bench/prime_race/tests/gb10_sieve_host_test.c sb_hazards): a destination + * written under write scoreboard 0 is in flight until a wait mask that includes it. */ +static int sb_hazards(const BlackwellIRProgram *p) { + int inflight[64], n = 0, bad = 0; + for (size_t i = 0; i < p->count; i++) { + const BlackwellIRInsn *in = &p->insns[i]; + uint32_t c = in->control; + if ((c >> 20) & 1u) n = 0; + const int src[3] = { in->src1_vreg, in->src2_vreg, in->src3_vreg }; + for (int s = 0; s < 3; s++) for (int k = 0; src[s] >= 0 && k < n; k++) if (inflight[k] == src[s]) bad++; + if (c != 0 && ((c >> 14) & 7u) == 0 && in->dst_vreg >= 0 && n < 64) inflight[n++] = in->dst_vreg; + } + return bad; +} +/* Control-edge walk: the straight-line scoreboard reasoning above holds only if nothing is in + * flight across a branch, barrier op or exit. Every BRA / EXIT / BSSY / BSYNC must wait on every + * scoreboard set since the last wait. Returns the number of control ops that do not. */ +static int edge_waits(const BlackwellIRProgram *p) { + uint32_t pending = 0; int bad = 0; + for (size_t i = 0; i < p->count; i++) { + const BlackwellIRInsn *in = &p->insns[i]; + uint32_t c = in->control, wait = (c >> 20) & 63u, wb = (c >> 14) & 7u, rb = (c >> 17) & 7u; + pending &= ~wait; + if (in->op == BW_IR_BRA || in->op == BW_IR_EXIT || in->op == BW_IR_BSSY || in->op == BW_IR_BSYNC) { if (pending) bad++; } + if (wb != 7u) pending |= 1u << wb; + if (rb != 7u) pending |= 1u << rb; + } + return bad; +} +static int count_op(const BlackwellIRProgram *p, BlackwellIROpcode op, int bar) { + int n = 0; + for (size_t i = 0; i < p->count; i++) if (p->insns[i].op == op && (bar < 0 || p->insns[i].bar_reg == bar)) n++; + return n; +} +static int find_op(const BlackwellIRProgram *p, BlackwellIROpcode op, int nth) { + for (size_t i = 0; i < p->count; i++) if (p->insns[i].op == op && nth-- == 0) return (int)i; + return -1; +} + +/* --------------------------------------------------------- nvdisasm listing */ +static int nvdisasm_bytes(const uint8_t *code, size_t bytes, const char *name, const char *const *must, int n_must) { + const char *nv = "/usr/local/cuda/bin/nvdisasm"; + if (access(nv, X_OK) != 0) { printf("nvdisasm: not present, listing check skipped for %s\n", name); return 0; } + char bin[96], lst[96], cmd[320]; + snprintf(bin, sizeof bin, "/tmp/omega_rc_%s_%d.bin", name, (int)getpid()); + snprintf(lst, sizeof lst, "/tmp/omega_rc_%s_%d.sass", name, (int)getpid()); + FILE *f = fopen(bin, "wb"); if (!f) return -1; + fwrite(code, 1, bytes, f); fclose(f); + snprintf(cmd, sizeof cmd, "%s -b SM121 %s > %s 2>&1", nv, bin, lst); + int rc = system(cmd); + FILE *l = fopen(lst, "r"); if (!l) return -1; + char line[512]; int seen[16] = {0}, bad = 0, insns = 0; + while (fgets(line, sizeof line, l)) { + if (!strstr(line, "/*0")) continue; + insns++; + if (strstr(line, "?") || strstr(line, "error")) bad++; + for (int i = 0; i < n_must && i < 16; i++) if (strstr(line, must[i])) seen[i] = 1; + } + fclose(l); + int miss = 0; + for (int i = 0; i < n_must && i < 16; i++) if (!seen[i]) { printf(" %s: nvdisasm listing lacks %s\n", name, must[i]); miss++; } + printf("nvdisasm %s: rc=%d insns=%d undecodable=%d missing=%d (%s)\n", name, rc, insns, bad, miss, lst); + unlink(bin); + return (rc == 0 && bad == 0 && miss == 0) ? 0 : -1; +} +static int nvdisasm_kernel(OmegaGpuEwOp op, int mutant, const char *name, const char *const *must, int n_must) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + if (!p || codegen(op, mutant, p, &k) != OMEGA_GPU_EW_OK) { free(p); return -1; } + int rc = nvdisasm_bytes(k.code, k.code_size, name, must, n_must); + free(k.code); free(p); + return rc; +} + +/* --------------------------------------------------------- host part */ +typedef struct { const char *name; uint64_t warp_issues, lane_issues, splits; int max_frags, peak_bars; } Model; +static Model g_model[NPROBES * 2]; static int g_nmodel; + +static int host_part(void) { + int before = g_failed; + /* 1. encoder golden words (nvcc 13.0.88 -arch=sm_121 oracle, docs/gpu-reconvergence-308.md) */ + int fx = omega_blackwell_verify_codegen_fixtures_reconv(); + CHECK(fx == 0, "encoder golden words BSSY / BSYNC and refusals (rc %d)", fx); + + /* 2. region bookkeeping on a hand-built program */ + { + BlackwellIRProgram *p = calloc(1, sizeof *p); BwRegions r; memset(&r, 0, sizeof r); + omega_bw_ir_init(p); + BlackwellIRInsn in; memset(&in, 0, sizeof in); in.dst_vreg = in.src1_vreg = in.src2_vreg = in.src3_vreg = -1; + in.op = BW_IR_BSSY; in.bar_reg = 0; in.imm = 2; omega_bw_ir_append(p, &in); /* 0 */ + CHECK(bw_regions_next_bar(&r) == 0 && bw_regions_begin(&r, 0) == 0, "region begin returns B0"); + in.op = BW_IR_BRA; in.imm = 1; in.predicate_p0 = true; omega_bw_ir_append(p, &in); /* 1 */ + CHECK(bw_regions_exit(&r, 1) == 0, "region exit recorded"); + in.op = BW_IR_NOP; in.predicate_p0 = false; omega_bw_ir_append(p, &in); /* 2 */ + in.op = BW_IR_BSYNC; in.bar_reg = 0; omega_bw_ir_append(p, &in); /* 3 */ + CHECK(bw_regions_join(&r, p, 3) == 0, "region join returns B0"); + CHECK(p->insns[0].imm == 4 && p->insns[1].imm == 2, "join patched BSSY to the insn after BSYNC (%u) and the exit to the BSYNC (%u)", p->insns[0].imm, p->insns[1].imm); + CHECK(bw_regions_join(&r, p, 3) == -1, "join with no open region refused"); + CHECK(bw_regions_exit(&r, 1) == -1, "exit with no open region refused"); + /* exits table full */ + memset(&r, 0, sizeof r); bw_regions_begin(&r, 0); + int ok = 1; for (int i = 0; i < BW_RECONV_MAX_EXITS; i++) ok &= bw_regions_exit(&r, 1) == 0; + CHECK(ok && bw_regions_exit(&r, 1) == -1, "exit table bound (%d) fails closed", BW_RECONV_MAX_EXITS); + /* join index inconsistent: BSYNC before BSSY, wrong barrier */ + memset(&r, 0, sizeof r); bw_regions_begin(&r, 3); + CHECK(bw_regions_join(&r, p, 0) == -1, "join with BSSY after BSYNC refused"); + memset(&r, 0, sizeof r); bw_regions_begin(&r, 0); p->insns[3].bar_reg = 1; + CHECK(bw_regions_join(&r, p, 3) == -1, "join with the wrong barrier register refused"); + p->insns[3].bar_reg = 0; + /* depth bound */ + memset(&r, 0, sizeof r); ok = 1; + for (int d = 0; d < BW_RECONV_MAX_DEPTH; d++) ok &= bw_regions_next_bar(&r) == d && bw_regions_begin(&r, d) == d; + CHECK(ok && bw_regions_next_bar(&r) == -1 && bw_regions_begin(&r, 99) == -1, "nesting bound %d: the next begin fails closed", BW_RECONV_MAX_DEPTH); + free(p); + } + + /* 3. codegen: production probes build, refusals refuse, mutants differ */ + static const struct { OmegaGpuEwOp op; const char *name; } refuse[] = { + { OMEGA_GPU_EW_RC_REFUSE_DEPTH17, "17 nested regions" }, { OMEGA_GPU_EW_RC_REFUSE_EXIT_SHFL, "EXIT then BAR/SHFL" }, + { OMEGA_GPU_EW_RC_REFUSE_EXIT_IN_REGION, "EXIT inside a region" }, { OMEGA_GPU_EW_RC_REFUSE_BAR_IN_REGION, "BAR.SYNC inside a region" }, + { OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION, "SHFL inside a region" }, { OMEGA_GPU_EW_RC_REFUSE_UNCLOSED, "region never joined" }, + { OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY, "BSYNC with no BSSY" }, + }; + for (size_t i = 0; i < sizeof refuse / sizeof refuse[0]; i++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + int rc = codegen(refuse[i].op, 0, p, &k); + CHECK(rc == OMEGA_GPU_EW_CODEGEN_FAIL, "%s refused at codegen (rc %s)", refuse[i].name, omega_gpu_elementwise_rc_name(rc)); + if (rc == OMEGA_GPU_EW_OK) free(k.code); + free(p); + } + for (size_t i = 0; i < NPROBES; i++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + int rc = codegen(PROBES[i].op, 0, p, &k); + CHECK(rc == OMEGA_GPU_EW_OK && k.gpr_count <= 64, "%s builds (rc %s, %zu insns, %u gprs)", PROBES[i].name, omega_gpu_elementwise_rc_name(rc), k.insn_count, k.gpr_count); + if (rc != OMEGA_GPU_EW_OK) { free(p); continue; } + int nb = count_op(p, BW_IR_BSSY, -1), ns = count_op(p, BW_IR_BSYNC, -1); + int want = PROBES[i].op == OMEGA_GPU_EW_RC_NESTED ? 2 : PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16 ? BW_RECONV_MAX_BAR : 1; + CHECK(nb == want && ns == want, "%s has %d BSSY and %d BSYNC (want %d)", PROBES[i].name, nb, ns, want); + if (PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16) { + int each = 1; for (int b = 0; b < BW_RECONV_MAX_BAR; b++) each &= count_op(p, BW_IR_BSSY, b) == 1 && count_op(p, BW_IR_BSYNC, b) == 1; + CHECK(each, "depth16 uses every barrier register B0..B15 exactly once"); + } + CHECK(sb_hazards(p) == 0, "%s scoreboard walk clean", PROBES[i].name); + CHECK(edge_waits(p) == 0, "%s every control op waits on everything in flight", PROBES[i].name); + printf("codegen %s: %zu insns, %u gprs, %d regions\n", PROBES[i].name, p->count, k.gpr_count, nb); + uint8_t d0[32]; memcpy(d0, k.code_digest, 32); free(k.code); + for (int m = 1; m <= 3; m++) { + BlackwellIRProgram *mp = calloc(1, sizeof *mp); OmegaBlackwellKernel mk; + int mrc = codegen(PROBES[i].op, m, mp, &mk); + int same_ok = m == 1 && PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16; /* depth16 has no math mutant */ + CHECK(mrc == OMEGA_GPU_EW_OK && ((memcmp(d0, mk.code_digest, 32) != 0) != same_ok), "%s mutant %d builds and %s (rc %s)", PROBES[i].name, m, same_ok ? "equals production" : "differs", omega_gpu_elementwise_rc_name(mrc)); + if (mrc == OMEGA_GPU_EW_OK) { + if (m == 2) CHECK(count_op(mp, BW_IR_BSYNC, -1) == 0 && count_op(mp, BW_IR_BSSY, -1) == nb, "%s mutant 2 has no BSYNC", PROBES[i].name); + if (m == 3) CHECK(count_op(mp, BW_IR_BSYNC, -1) == ns, "%s mutant 3 keeps its BSYNCs", PROBES[i].name); + free(mk.code); + } + free(mp); + } + free(p); + } + /* the four regioned sieves build too (their sweep runs in gb10_sieve_host_test) */ + for (int op = OMEGA_GPU_EW_PRIME_SIEVE_DIV1; op <= OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2; op++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + int rc = codegen((OmegaGpuEwOp)op, 0, p, &k); + int staged = op == OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1 || op == OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2; + int nb = rc == OMEGA_GPU_EW_OK ? count_op(p, BW_IR_BSSY, -1) : -1, want = (op >= OMEGA_GPU_EW_PRIME_SIEVE_DIV2 ? 2 : 1) + staged; + CHECK(rc == OMEGA_GPU_EW_OK && k.gpr_count <= 64 && nb == want && edge_waits(p) == 0, "sieve op %d builds with %d regions (rc %s, %u gprs)", op, nb, omega_gpu_elementwise_rc_name(rc), k.gpr_count); + if (rc == OMEGA_GPU_EW_OK) free(k.code); + free(p); + } + + /* 4. nvdisasm: the words decode as BSSY/BSYNC on the real barrier registers, and the predicated + * and B15 forms the encoder can produce decode too (built by hand: encode_one) */ + { + static const char *const m0[] = { "BSSY", "BSYNC", "B0", "SHFL.DOWN", "@P0 BRA" }; + CHECK(nvdisasm_kernel(OMEGA_GPU_EW_RC_DIAMOND, 0, "diamond", m0, 5) == 0, "diamond listing decodes with BSSY/BSYNC B0"); + static const char *const m1[] = { "BSSY", "BSYNC", "B15", "B7" }; + CHECK(nvdisasm_kernel(OMEGA_GPU_EW_RC_DEPTH16, 0, "depth16", m1, 4) == 0, "depth16 listing decodes with B15"); + static const char *const m2[] = { "BSSY", "B1", "BSYNC" }; + CHECK(nvdisasm_kernel(OMEGA_GPU_EW_PRIME_SIEVE_DIV2, 0, "sieve_div2", m2, 3) == 0, "nested sieve listing decodes with B1"); + uint8_t code[64]; uint32_t w[4]; BlackwellIRInsn in; memset(&in, 0, sizeof in); in.dst_vreg = in.src1_vreg = in.src2_vreg = in.src3_vreg = -1; + in.op = BW_IR_BSSY; in.bar_reg = 3; in.imm = 2; in.predicate_p0 = true; omega_blackwell_encode_one(&in, w); memcpy(code, w, 16); + in.op = BW_IR_BSYNC; in.bar_reg = 3; in.predicate_p0 = true; in.predicate_not = true; omega_blackwell_encode_one(&in, w); memcpy(code + 16, w, 16); + in.op = BW_IR_BSYNC; in.bar_reg = 15; in.predicate_p0 = false; in.predicate_not = false; omega_blackwell_encode_one(&in, w); memcpy(code + 32, w, 16); + in.op = BW_IR_EXIT; omega_blackwell_encode_one(&in, w); memcpy(code + 48, w, 16); + static const char *const m3[] = { "@P0 BSSY", "B3", "@!P0 BSYNC", "BSYNC.RECONVERGENT B15" }; + CHECK(nvdisasm_bytes(code, 64, "forms", m3, 4) == 0, "predicated BSSY/BSYNC and B15 decode"); + } + + /* 5. simulator: every probe, every n, both fragment orders, exact against the oracle */ + uint32_t *a = malloc(20000 * 4), *b = malloc(20000 * 4), *got = malloc(20000 * 4), *want = malloc(20000 * 4); + for (size_t i = 0; i < NPROBES; i++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + if (codegen(PROBES[i].op, 0, p, &k) != OMEGA_GPU_EW_OK) { free(p); continue; } + int fails = 0; uint64_t cases = 0; + for (size_t j = 0; j < NNS; j++) for (int order = 0; order < 2; order++) { + uint32_t n = NS[j]; fill(a, b, n, 0x308u + n * 7u + (uint32_t)order); + rc_oracle(PROBES[i].op, 0, n, a, b, want); + WsSim st; int err = ws_probe(p, n, a, b, got, order, &st); + uint32_t first; size_t bad = diff_count(got, want, n, &first); + cases++; + if (err || bad) { fails++; printf(" %s n=%u order=%d: sim %s, %zu words differ (first %u: got %08x want %08x)\n", PROBES[i].name, n, order, err ? st.msg : "clean", bad, first, first < n ? got[first] : 0, first < n ? want[first] : 0); } + if (n == 4096) { Model *m = &g_model[g_nmodel++]; m->name = PROBES[i].name; m->warp_issues = st.warp_issues; m->lane_issues = st.lane_issues; m->splits = st.splits; m->max_frags = st.max_frags; m->peak_bars = st.peak_bars; } + } + CHECK(fails == 0, "%s simulator vs oracle: %d of %" PRIu64 " cases differ", PROBES[i].name, fails, cases); + /* math mutant: simulator clean, result wrong (the oracle sees it) */ + BlackwellIRProgram *mp = calloc(1, sizeof *mp); OmegaBlackwellKernel mk; + if (codegen(PROBES[i].op, 1, mp, &mk) == OMEGA_GPU_EW_OK) { + uint32_t n = 1000; fill(a, b, n, 0x1001u); rc_oracle(PROBES[i].op, 0, n, a, b, want); + WsSim st; int err = ws_probe(mp, n, a, b, got, 0, &st); uint32_t first; size_t bad = diff_count(got, want, n, &first); + if (PROBES[i].op == OMEGA_GPU_EW_RC_EXIT_BAR) CHECK(err == WS_E_SHFL_EXITED || err == WS_E_BAR_SPLIT, "R6 early-exit kernel: simulator reports SHFL_EXITED (got %s)", st.msg); + else if (PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16) CHECK(err == 0 && bad == 0, "depth16 'mutant 1' is the production kernel: clean and exact (err %d bad %zu)", err, bad); + else CHECK(err == 0 && bad > 0, "%s math mutant: simulator clean, %zu words differ (err %d)", PROBES[i].name, bad, err); + free(mk.code); + } + free(mp); + /* dropped join and corrupted target: the simulator must reject both, in both orders */ + for (int m = 2; m <= 3; m++) for (int order = 0; order < 2; order++) { + BlackwellIRProgram *xp = calloc(1, sizeof *xp); OmegaBlackwellKernel xk; + if (codegen(PROBES[i].op, m, xp, &xk) == OMEGA_GPU_EW_OK) { + uint32_t n = 1000; fill(a, b, n, 0x2002u + (uint32_t)m); + WsSim st; int err = ws_probe(xp, n, a, b, got, order, &st); + CHECK(is_reconv_err(err), "%s mutant %d (%s) order %d rejected by the simulator: %s", PROBES[i].name, m, m == 2 ? "BSYNC dropped" : "BSSY target corrupted", order, err ? st.msg : "NOT CAUGHT"); + if (m == 3) CHECK(err == WS_E_JOIN_MISMATCH || err == WS_E_STUCK, "%s corrupted target shows as JOIN_MISMATCH or STUCK (%s)", PROBES[i].name, ws_err_name(err)); + free(xk.code); + } + free(xp); + } + free(k.code); free(p); + } + /* 6. hand mutations of the production diamond (controls on the simulator itself, independent of + * the emitter's mutant hooks): exit jumping past the BSYNC, BSYNC on the wrong barrier, a BSSY + * on an armed barrier. And the unmodified program as the positive control. */ + { + BlackwellIRProgram *p = calloc(1, sizeof *p), *q = calloc(1, sizeof *q); OmegaBlackwellKernel k; + if (codegen(OMEGA_GPU_EW_RC_DIAMOND, 0, p, &k) == OMEGA_GPU_EW_OK) { + uint32_t n = 500; fill(a, b, n, 0x3003u); WsSim st; + int bssy = find_op(p, BW_IR_BSSY, 0), bsync = find_op(p, BW_IR_BSYNC, 0), ex = -1; + for (int i = bssy + 1; i < bsync; i++) if (p->insns[i].op == BW_IR_BRA && !p->insns[i].predicate_p0) ex = i; + CHECK(bssy >= 0 && bsync > bssy && ex > bssy, "diamond shape: BSSY %d, exit %d, BSYNC %d", bssy, ex, bsync); + CHECK(ws_probe(p, n, a, b, got, 0, &st) == 0, "positive control: untouched diamond runs clean"); + *q = *p; q->insns[ex].imm += 1; /* exit lands after the BSYNC */ + int e1 = ws_probe(q, n, a, b, got, 0, &st); CHECK(is_reconv_err(e1), "exit past the BSYNC rejected: %s", st.msg); + *q = *p; q->insns[bsync].bar_reg = 1; /* BSYNC on a barrier nobody armed */ + int e2 = ws_probe(q, n, a, b, got, 0, &st); CHECK(e2 == WS_E_BSYNC_UNARMED, "BSYNC on an unarmed barrier rejected: %s", st.msg); + *q = *p; q->insns[bsync].op = BW_IR_NOP; /* join dropped by hand */ + int e3 = ws_probe(q, n, a, b, got, 0, &st); CHECK(is_reconv_err(e3), "join replaced by NOP rejected: %s", st.msg); + *q = *p; q->insns[bssy].imm += 1; /* names the wrong join */ + int e4 = ws_probe(q, n, a, b, got, 0, &st); CHECK(e4 == WS_E_JOIN_MISMATCH, "wrong join target rejected: %s", st.msg); + *q = *p; q->insns[bssy + 1] = p->insns[bssy]; /* second BSSY on the armed barrier (overwrites the ISETP) */ + int e5 = ws_probe(q, n, a, b, got, 0, &st); CHECK(e5 == WS_E_BSSY_REARM, "BSSY on an armed barrier rejected: %s", st.msg); + free(k.code); + } + free(p); free(q); + } + free(a); free(b); free(got); free(want); + for (int i = 0; i < g_nmodel; i++) printf("model %s n=4096: warp_issues=%" PRIu64 " lane_util=%.3f splits=%" PRIu64 " max_frags=%d peak_bars=%d\n", g_model[i].name, g_model[i].warp_issues, g_model[i].warp_issues ? (double)g_model[i].lane_issues / (32.0 * (double)g_model[i].warp_issues) : 0.0, g_model[i].splits, g_model[i].max_frags, g_model[i].peak_bars); + return g_failed == before ? 0 : -1; +} + +/* --------------------------------------------------------- chip part */ +static int chip(const char *out_path, int observe) { + FILE *out = out_path ? fopen(out_path, "w") : NULL; + if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; } + if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_RECONV_V1\",\"issue\":308,\"comparison\":\"exact u32\",\"cases\":["); + int first = 1; + uint32_t *a = malloc(20000 * 4), *b = malloc(20000 * 4), *got = malloc(20000 * 4), *want = malloc(20000 * 4); + for (size_t i = 0; i < NPROBES; i++) { + for (size_t j = 0; j < NNS; j++) { + uint32_t n = NS[j]; fill(a, b, n, 0x5050u + n); + rc_oracle(PROBES[i].op, 0, n, a, b, want); + OmegaGpuEwInfo info; memset(&info, 0, sizeof info); + int rc = omega_gpu_reconv_probe_u32(PROBES[i].op, n, a, b, got, &info); + uint32_t fi; size_t bad = rc == OMEGA_GPU_EW_OK ? diff_count(got, want, n, &fi) : n; + int pass = rc == OMEGA_GPU_EW_OK && bad == 0; + CHECK(pass, "%s n=%u chip rc=%s violations=%zu err=\"%s\"", PROBES[i].name, n, omega_gpu_elementwise_rc_name(rc), bad, omega_gpu_elementwise_last_error()); + if (out) fprintf(out, "%s{\"op\":\"%s\",\"regression\":\"%s\",\"n\":%u,\"rc\":\"%s\",\"violations\":%zu,\"pass\":%s,\"chip_elapsed_ns\":%" PRIu64 ",\"chip_calls\":%u,\"unwritten_words\":%u,\"cache_hit\":%s}", + first ? "" : ",", PROBES[i].name, PROBES[i].reg, n, omega_gpu_elementwise_rc_name(rc), bad, pass ? "true" : "false", info.elapsed_ns, info.chip_calls, info.unwritten_words, info.kernel_cache_hit ? "true" : "false"); + first = 0; + if (!pass) { printf("STOP: %s failed on the chip at n=%u; no further launches of this probe\n", PROBES[i].name, n); break; } + } + } + if (out) fprintf(out, "],\"mutants\":["); + first = 1; + for (size_t i = 0; i < NPROBES; i++) { + if (PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16) continue; /* no math mutant */ + uint32_t n = 1000; fill(a, b, n, 0x6060u); rc_oracle(PROBES[i].op, 0, n, a, b, want); + omega_gpu_elementwise_test_set_mutant((int)PROBES[i].op); + /* R6's math mutant is the early-exit kernel: the emitter refuses it on the launch path + * (the bypass exists only for codegen_ir), so the catch is CODEGEN_FAIL: no undefined + * SHFL ever reaches the chip. */ + OmegaGpuEwInfo info; memset(&info, 0, sizeof info); + int rc = omega_gpu_reconv_probe_u32(PROBES[i].op, n, a, b, got, &info); + uint32_t fi; size_t bad = rc == OMEGA_GPU_EW_OK ? diff_count(got, want, n, &fi) : n; + int caught = rc != OMEGA_GPU_EW_OK || bad > 0; + omega_gpu_elementwise_test_set_mutant(0); + CHECK(caught, "%s math mutant caught (rc %s, violations %zu)", PROBES[i].name, omega_gpu_elementwise_rc_name(rc), bad); + if (PROBES[i].op == OMEGA_GPU_EW_RC_EXIT_BAR) CHECK(rc == OMEGA_GPU_EW_CODEGEN_FAIL, "R6 early-exit form refused at codegen on the launch path (rc %s)", omega_gpu_elementwise_rc_name(rc)); + if (out) fprintf(out, "%s{\"op\":\"%s\",\"mutant\":1,\"rc\":\"%s\",\"violations\":%zu,\"caught\":%s}", first ? "" : ",", PROBES[i].name, omega_gpu_elementwise_rc_name(rc), bad, caught ? "true" : "false"); + first = 0; + } + if (out) fprintf(out, "],\"observations\":["); + if (observe) { + /* The dropped-join kernel (rejected by the simulator above) on the chip once, small n: + * recorded as an observation of what the hardware does without the BSYNC. Not a gate. */ + uint32_t n = 1000; fill(a, b, n, 0x7070u); rc_oracle(OMEGA_GPU_EW_RC_DIAMOND, 0, n, a, b, want); + omega_gpu_elementwise_test_set_mutant_level(OMEGA_GPU_EW_RC_DIAMOND, 2); + OmegaGpuEwInfo info; memset(&info, 0, sizeof info); + int rc = omega_gpu_reconv_probe_u32(OMEGA_GPU_EW_RC_DIAMOND, n, a, b, got, &info); + uint32_t fi; size_t bad = rc == OMEGA_GPU_EW_OK ? diff_count(got, want, n, &fi) : n; + omega_gpu_elementwise_test_set_mutant(0); + printf("observation: diamond with the BSYNC dropped, n=%u: rc=%s, %zu of %u words differ from the oracle (first %u)\n", n, omega_gpu_elementwise_rc_name(rc), bad, n, fi); + if (out) fprintf(out, "{\"op\":\"rc_diamond\",\"mutant\":2,\"what\":\"BSYNC dropped\",\"n\":%u,\"rc\":\"%s\",\"violations\":%zu,\"first_bad\":%u,\"gate\":false}", n, omega_gpu_elementwise_rc_name(rc), bad, fi); + } + CHECK(omega_gpu_session_open_count() == 1, "device opened once for the battery (opens=%u)", omega_gpu_session_open_count()); + if (out) { + fprintf(out, "],\"sim_model_n4096\":["); + for (int i = 0; i < g_nmodel; i++) fprintf(out, "%s{\"op\":\"%s\",\"order\":%d,\"warp_issues\":%" PRIu64 ",\"lane_issues\":%" PRIu64 ",\"splits\":%" PRIu64 ",\"max_frags\":%d,\"peak_bars\":%d}", i ? "," : "", g_model[i].name, i & 1, g_model[i].warp_issues, g_model[i].lane_issues, g_model[i].splits, g_model[i].max_frags, g_model[i].peak_bars); + 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_RECONV_PASS" : "OMEGA_GPU_RECONV_FAIL"); + fclose(out); + } + free(a); free(b); free(got); free(want); + return 0; +} + +int main(int argc, char **argv) { + const char *out_path = NULL; int host = 0, observe = 0; + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "--host-only") == 0) host = 1; + else if (strcmp(argv[i], "--observe-dropped-join") == 0) observe = 1; + else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i]; + } + int hrc = host_part(); + if (!host) { + if (hrc != 0) { printf("host part failed: no chip launches\nFAIL: %d checks, %d failed\n", g_checks, g_failed); return 1; } + if (chip(out_path, observe) != 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/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/omega_blackwell_codegen.c b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/omega_blackwell_codegen.c new file mode 100644 index 000000000..38f73412a --- /dev/null +++ b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/omega_blackwell_codegen.c @@ -0,0 +1,1945 @@ +#include "omega_blackwell_codegen.h" +#include "omega_blackwell_submit.h" +#include "sha256.h" +#include +#include +#include + +void omega_bw_ir_init(BlackwellIRProgram *prog) { + if (!prog) return; + memset(prog, 0, sizeof(*prog)); +} + +int omega_bw_ir_alloc_vreg(BlackwellIRProgram *prog) { + if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1; + int v = prog->regalloc.num_vregs++; + prog->regalloc.intervals[v].vreg = v; + prog->regalloc.intervals[v].first_def = -1; + prog->regalloc.intervals[v].last_use = -1; + prog->regalloc.intervals[v].phys_reg = -1; + prog->regalloc.intervals[v].bundle_size = 1; + prog->regalloc.intervals[v].is_pair = false; + prog->regalloc.intervals[v].is_quad = false; + prog->regalloc.intervals[v].active = false; + prog->regalloc.vreg_to_phys[v] = -1; + return v; +} + +int omega_bw_ir_alloc_vreg64(BlackwellIRProgram *prog) { + if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1; + int v = prog->regalloc.num_vregs++; + prog->regalloc.intervals[v].vreg = v; + prog->regalloc.intervals[v].first_def = -1; + prog->regalloc.intervals[v].last_use = -1; + prog->regalloc.intervals[v].phys_reg = -1; + prog->regalloc.intervals[v].bundle_size = 2; + prog->regalloc.intervals[v].is_pair = true; + prog->regalloc.intervals[v].is_quad = false; + prog->regalloc.intervals[v].active = false; + prog->regalloc.vreg_to_phys[v] = -1; + return v; +} + +int omega_bw_ir_alloc_vreg128(BlackwellIRProgram *prog) { + if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1; + int v = prog->regalloc.num_vregs++; + prog->regalloc.intervals[v].vreg = v; + prog->regalloc.intervals[v].first_def = -1; + prog->regalloc.intervals[v].last_use = -1; + prog->regalloc.intervals[v].phys_reg = -1; + prog->regalloc.intervals[v].bundle_size = 4; + prog->regalloc.intervals[v].is_pair = false; + prog->regalloc.intervals[v].is_quad = true; + prog->regalloc.intervals[v].active = false; + prog->regalloc.vreg_to_phys[v] = -1; + return v; +} + +int omega_bw_ir_alloc_uvreg(BlackwellIRProgram *prog) { + if (!prog || prog->regalloc.num_uvregs >= BW_MAX_UVREGS) return -1; + int uv = prog->regalloc.num_uvregs++; + prog->regalloc.uintervals[uv].vreg = uv; + prog->regalloc.uintervals[uv].first_def = -1; + prog->regalloc.uintervals[uv].last_use = -1; + prog->regalloc.uintervals[uv].phys_reg = -1; + prog->regalloc.uintervals[uv].is_pair = false; + prog->regalloc.uintervals[uv].active = false; + prog->regalloc.uvreg_to_phys[uv] = -1; + return uv; +} + +int omega_bw_ir_alloc_uvreg64(BlackwellIRProgram *prog) { + if (!prog || prog->regalloc.num_uvregs >= BW_MAX_UVREGS) return -1; + int uv = prog->regalloc.num_uvregs++; + prog->regalloc.uintervals[uv].vreg = uv; + prog->regalloc.uintervals[uv].first_def = -1; + prog->regalloc.uintervals[uv].last_use = -1; + prog->regalloc.uintervals[uv].phys_reg = -1; + prog->regalloc.uintervals[uv].is_pair = true; + prog->regalloc.uintervals[uv].active = false; + prog->regalloc.uvreg_to_phys[uv] = -1; + return uv; +} + +int omega_bw_ir_append(BlackwellIRProgram *prog, const BlackwellIRInsn *insn) { + if (!prog || !insn || prog->count >= BW_MAX_IR_INSNS) return -1; + int idx = (int)prog->count++; + prog->insns[idx] = *insn; + + /* Update live-interval def/use tracking */ + if (insn->dst_vreg >= 0 && insn->dst_vreg < prog->regalloc.num_vregs) { + if (!insn->is_uniform) { + OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->dst_vreg]; + if (iv->first_def < 0) iv->first_def = idx; + if (idx > iv->last_use) iv->last_use = idx; + } + } + if (insn->src1_vreg >= 0 && insn->src1_vreg < prog->regalloc.num_vregs) { + OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src1_vreg]; + if (iv->first_def < 0) iv->first_def = idx; + if (idx > iv->last_use) iv->last_use = idx; + } + if (insn->src2_vreg >= 0 && insn->src2_vreg < prog->regalloc.num_vregs) { + OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src2_vreg]; + if (iv->first_def < 0) iv->first_def = idx; + if (idx > iv->last_use) iv->last_use = idx; + } + if (insn->src3_vreg >= 0 && insn->src3_vreg < prog->regalloc.num_vregs) { + OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src3_vreg]; + if (iv->first_def < 0) iv->first_def = idx; + if (idx > iv->last_use) iv->last_use = idx; + } + + /* Uniform register def/use tracking */ + if (insn->is_uniform && insn->dst_vreg >= 0 && insn->dst_vreg < prog->regalloc.num_uvregs) { + OmegaLiveInterval *uiv = &prog->regalloc.uintervals[insn->dst_vreg]; + if (uiv->first_def < 0) uiv->first_def = idx; + if (idx > uiv->last_use) uiv->last_use = idx; + } + if (insn->ureg >= 0 && insn->ureg < prog->regalloc.num_uvregs) { + OmegaLiveInterval *uiv = &prog->regalloc.uintervals[insn->ureg]; + if (uiv->first_def < 0) uiv->first_def = idx; + if (idx > uiv->last_use) uiv->last_use = idx; + } + + return idx; +} + +int omega_bw_regalloc_solve(BlackwellIRProgram *prog) { + if (!prog) return -1; + OmegaRegAlloc *ra = &prog->regalloc; + + /* + * Bounded deterministic linear-scan register allocator. + * Computes genuine live intervals across virtual registers and assigns + * available physical GPRs (R2..R127) and UGPRs (UR4..UR31). + * Enforces even-alignment and dual-register reservation for 64-bit pairs. + */ + bool gpr_busy[BW_PHYS_GPR_MAX]; + memset(gpr_busy, 0, sizeof(gpr_busy)); + + int max_phys_used = BW_PHYS_GPR_START; + + /* Sort virtual register indices chronologically by first_def (Poletto & Sarkar Linear Scan) */ + int order[BW_MAX_VREGS]; + int count = 0; + for (int v = 0; v < ra->num_vregs; v++) { + if (ra->intervals[v].first_def >= 0) { + order[count++] = v; + } else { + ra->intervals[v].phys_reg = BW_PHYS_GPR_START; + ra->vreg_to_phys[v] = ra->intervals[v].phys_reg; + } + } + for (int i = 0; i < count - 1; i++) { + for (int j = i + 1; j < count; j++) { + if (ra->intervals[order[i]].first_def > ra->intervals[order[j]].first_def) { + int tmp = order[i]; + order[i] = order[j]; + order[j] = tmp; + } + } + } + + for (int idx = 0; idx < count; idx++) { + int v = order[idx]; + OmegaLiveInterval *iv = &ra->intervals[v]; + + /* Free physical registers whose virtual live intervals have ended */ + for (int p = 0; p < ra->num_vregs; p++) { + if (ra->intervals[p].active && ra->intervals[p].last_use < iv->first_def) { + int freed = ra->intervals[p].phys_reg; + if (freed >= 0 && freed < BW_PHYS_GPR_MAX) { + uint32_t bsize = ra->intervals[p].bundle_size ? ra->intervals[p].bundle_size : (ra->intervals[p].is_pair ? 2 : (ra->intervals[p].is_quad ? 4 : 1)); + for (uint32_t k = 0; k < bsize && (freed + (int)k) < BW_PHYS_GPR_MAX; k++) { + gpr_busy[freed + k] = false; + } + } + ra->intervals[p].active = false; + } + } + + /* Find lowest available physical register */ + int assigned = -1; + uint32_t bsize = iv->bundle_size ? iv->bundle_size : (iv->is_pair ? 2 : (iv->is_quad ? 4 : 1)); + if (bsize == 4) { + /* 128-bit quad requires 4-alignment and 4 contiguous free registers */ + int start = (BW_PHYS_GPR_START + 3) & ~3; + for (int p = start; p <= BW_PHYS_GPR_MAX - 4; p += 4) { + if (!gpr_busy[p] && !gpr_busy[p + 1] && !gpr_busy[p + 2] && !gpr_busy[p + 3]) { + assigned = p; + for (int k = 0; k < 4; k++) gpr_busy[p + k] = true; + break; + } + } + } else if (bsize == 2) { + /* 64-bit pair requires even alignment and contiguous free registers */ + int start = (BW_PHYS_GPR_START + 1) & ~1; + for (int p = start; p <= BW_PHYS_GPR_MAX - 2; p += 2) { + if (!gpr_busy[p] && !gpr_busy[p + 1]) { + assigned = p; + gpr_busy[p] = true; + gpr_busy[p + 1] = true; + break; + } + } + } else { + for (int p = BW_PHYS_GPR_START; p < BW_PHYS_GPR_MAX; p++) { + if (!gpr_busy[p]) { + assigned = p; + gpr_busy[p] = true; + break; + } + } + } + + if (assigned < 0) { + /* Hardware register capacity exceeded: fail closed loudly */ + return -1; + } + + iv->phys_reg = assigned; + iv->active = true; + ra->vreg_to_phys[v] = assigned; + + int top = assigned + (int)bsize - 1; + if (top > max_phys_used) { + max_phys_used = top; + } + } + ra->peak_gpr_usage = (uint32_t)(max_phys_used + 1); + + /* Allocate uniform registers */ + int cur_u = BW_PHYS_UGPR_START; + for (int uv = 0; uv < ra->num_uvregs; uv++) { + OmegaLiveInterval *uiv = &ra->uintervals[uv]; + if (uiv->is_pair) { + if (cur_u % 2 != 0) cur_u++; + if (cur_u + 1 >= BW_PHYS_UGPR_MAX) return -1; + uiv->phys_reg = cur_u; + ra->uvreg_to_phys[uv] = cur_u; + cur_u += 2; + } else { + if (cur_u >= BW_PHYS_UGPR_MAX) return -1; + uiv->phys_reg = cur_u; + ra->uvreg_to_phys[uv] = cur_u++; + } + } + ra->peak_ugpr_usage = (uint32_t)cur_u; + + return 0; +} + +static int encode_single_insn(const BlackwellIRInsn *insn, const OmegaRegAlloc *ra, uint32_t w[4]) { + memset(w, 0, 16); + + int dst = (insn->dst_vreg >= 0) ? (ra->vreg_to_phys[insn->dst_vreg] + insn->dst_subreg) : 0; + int src1 = (insn->src1_vreg >= 0) ? (ra->vreg_to_phys[insn->src1_vreg] + insn->src1_subreg) : 0; + int src2 = (insn->src2_vreg >= 0) ? (ra->vreg_to_phys[insn->src2_vreg] + insn->src2_subreg) : 0; + int src3 = (insn->src3_vreg >= 0) ? (ra->vreg_to_phys[insn->src3_vreg] + insn->src3_subreg) : 0xff; /* Default to RZ (0xff) */ + int ureg = (insn->ureg >= 0) ? ra->uvreg_to_phys[insn->ureg] : 0; + + if (insn->is_uniform && insn->dst_vreg >= 0) { + dst = ra->uvreg_to_phys[insn->dst_vreg]; + } + + switch (insn->op) { + case BW_IR_NOP: + w[0] = 0x00007918; + w[1] = 0x00000000; + w[2] = 0x00000000; + w[3] = 0x000fc000; + break; + + case BW_IR_MOV_RZ: + /* MOV Rd, RZ */ + w[0] = 0x7202 | ((uint32_t)(dst & 0xff) << 16); + w[1] = 0x000000ff; + w[2] = 0x00000f00; + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_MOV_IMM: + /* MOV Rd, imm32 */ + w[0] = 0x7802 | ((uint32_t)(dst & 0xff) << 16); + w[1] = insn->imm; + w[2] = 0x00000f00; + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_S2R: + /* S2R Rd, SR */ + w[0] = 0x7919 | ((uint32_t)(dst & 0xff) << 16); + w[1] = 0x0; + w[2] = (insn->imm & 0xff) << 8; + w[3] = insn->control ? insn->control : 0x000e2200; + break; + + case BW_IR_LDC: + /* LDC Rd, c[0x0][imm] */ + w[0] = 0xff007b82 | ((uint32_t)(dst & 0xff) << 16); + w[1] = (insn->imm << 6) & 0x00ffff00; + w[2] = 0x00000800; + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_LDC64: + /* LDC.64 Rd:Rd+1, c[0x0][imm] */ + w[0] = 0xff007b82 | ((uint32_t)(dst & 0xff) << 16); + w[1] = (insn->imm << 6) & 0x00ffff00; + w[2] = 0x00000a00; + w[3] = insn->control ? insn->control : 0x000e2200; + break; + + case BW_IR_LDCU: + /* LDCU URd, c[0x0][imm] */ + w[0] = 0xff0077ac | ((uint32_t)(dst & 0xff) << 16); + w[1] = (insn->imm << 5) & 0x00ffff00; + w[2] = 0x08000800; + w[3] = insn->control ? insn->control : 0x000e2200; + break; + + case BW_IR_LDCU64: + /* LDCU.64 URd:URd+1, c[0x0][imm] */ + w[0] = 0xff0077ac | ((uint32_t)(dst & 0xff) << 16); + w[1] = (insn->imm << 5) & 0x00ffff00; + w[2] = 0x08000a00; + w[3] = insn->control ? insn->control : 0x000e6e00; + break; + + case BW_IR_IMAD: + if (insn->src2_vreg >= 0) { + /* Register-register: IMAD Rd, Ra, Rb, Rc (0x7224) */ + w[0] = 0x7224 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x078e0200 | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + } else if (insn->is_uniform && insn->ureg >= 0) { + /* Uniform register multiplier: IMAD Rd, Ra, URb, Rc (0x7c24) */ + w[0] = 0x7c24 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0f8e0200 | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + } else { + /* Immediate multiplier: IMAD Rd, Ra, imm, Rc (0x7824) */ + w[0] = 0x7824 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = insn->imm; + w[2] = 0x078e0200 | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + } + break; + + case BW_IR_IMAD_WIDE: + /* IMAD.WIDE.U32 Rd:Rd+1, Ra, imm, Rc:Rc+1 */ + w[0] = 0x7825 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = insn->imm; + w[2] = 0x078e0000 | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fcc00; + break; + + case BW_IR_ISETP_GE: + if (insn->is_uniform && insn->ureg >= 0) { + w[0] = 0x7c0c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0bf06070; + w[3] = 0x002fda00; + } else { + w[0] = 0x780c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = insn->imm; + w[2] = 0x03f06270; + w[3] = 0x001fda00; + } + break; + + case BW_IR_LDG_E: + /* LDG.E Rd, desc[URd][Ra.64] */ + w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0c1e1900; + w[3] = insn->control ? insn->control : 0x000f2200; + break; + + case BW_IR_STG_E: + /* STG.E desc[URd][Ra.64], Rb */ + w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x0c101900 | (uint32_t)(ureg & 0xff); + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_STG_EF: + /* STG.E.MMIO.GPU. Uncached store, meant to be visible without a fence. */ + w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x0c111900 | (uint32_t)(ureg & 0xff); + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_IADD3: + /* IADD3 Rd, PT, PT, Ra, Rb, Rc */ + w[0] = 0x7210 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x07ffe0ff; + w[3] = 0x010fca00; + break; + + case BW_IR_HMMA_F16: + case BW_IR_HMMA_BF16: { + /* HMMA.16816.F32[.BF16] Rd, Ra, Rb, Rc */ + w[0] = 0x723c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + uint32_t prec_bit = (insn->op == BW_IR_HMMA_BF16) ? 0x00040000 : 0x00000000; + w[2] = (uint32_t)(src3 & 0xff) | (0x18 << 8) | prec_bit; + w[3] = insn->control ? insn->control : 0x000fe200; + break; + } + + case BW_IR_SHF_R: + /* SHF.R.U32.HI Rd, RZ, imm, Ra */ + w[0] = 0x7819U | ((uint32_t)(dst & 0xff) << 16) | (0xffU << 24); + w[1] = (uint32_t)insn->imm; + w[2] = 0x00011600 | (uint32_t)(src1 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + break; + + case BW_IR_LOP3_AND: + /* LOP3.LUT Rd, Ra, imm, RZ, 0xc0, !PT */ + w[0] = 0x7812U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)insn->imm; + w[2] = 0x078ec0ff; + w[3] = insn->control ? insn->control : 0x001fca00; + break; + + case BW_IR_LDG_E_U16: + /* LDG.E.U16 Rd, desc[URd][Ra.64] */ + w[0] = 0x7981U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0c1e1500; + w[3] = insn->control ? insn->control : 0x000f2200; + break; + + case BW_IR_LDG_STRONG_SYS: + /* LDG.E.STRONG.SYS. Same shape as LDG.E; w[2] carries the + * system-scope bits (0x0c1e1900 -> 0x0c1f5900). */ + w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0c1f5900; + w[3] = insn->control ? insn->control : 0x000f2200; + break; + + case BW_IR_LDG_MMIO: + /* LDG.E.EF. Drop the cached line, then load. */ + w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0c0e1900; + w[3] = insn->control ? insn->control : 0x000f2200; + break; + + case BW_IR_STG_STRONG_SYS: + w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); + if (insn->predicate_p0) { + uint32_t pred = insn->predicate_not ? 0x8u : 0x0u; + w[0] = (w[0] & ~0xF000u) | (pred << 12); + } + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x0c115900 | (uint32_t)(ureg & 0xff); + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_MEMBAR_ALL_SYS: + w[0] = 0x00007992; + w[1] = 0x00000000; + w[2] = 0x0000b000; + w[3] = insn->control ? insn->control : 0x000fec00; + break; + + case BW_IR_MEMBAR_SC_SYS: + w[0] = 0x00007992; + w[1] = 0x00000000; + w[2] = 0x00003000; + w[3] = insn->control ? insn->control : 0x000fec00; + break; + + case BW_IR_CCTL_IVALL: + w[0] = 0xff00798f; + w[1] = 0x00000000; + w[2] = 0x02000000; + w[3] = insn->control ? insn->control : 0x000fe800; + break; + + case BW_IR_ATOMG_ADD_STRONG_SYS: + w[0] = 0x79a8 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff) | 0x80000000u; + w[2] = 0x081f5100 | (uint32_t)(ureg & 0xff); + w[3] = insn->control ? insn->control : 0x00321e00; + break; + + case BW_IR_ATOMG_EXCH_STRONG_SYS: + w[0] = 0x79a8 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff) | 0x80000000u; + w[2] = 0x0c1f5100 | (uint32_t)(ureg & 0xff); + w[3] = insn->control ? insn->control : 0x00321e00; + break; + + case BW_IR_ISETP_GE_U32: + /* ISETP.GE.U32.AND P0, PT, Ra, Rb, PT. Checked with nvdisasm -b SM121. */ + w[0] = 0x720c | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x03f06070; + w[3] = insn->control ? insn->control : 0x001fda00; + break; + + case BW_IR_LOP3_XOR: + /* LOP3.LUT Rd, Ra, Rb, RZ, 0x3c, !PT. Checked with nvdisasm -b SM121. */ + w[0] = 0x7212 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x078e3cff; + w[3] = insn->control ? insn->control : 0x001fca00; + break; + + case BW_IR_FADD: + /* FADD Rd, Ra, Rb */ + w[0] = 0x7221U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x00000000; + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_FSUB: + /* FSUB Rd, Ra, Rb (FADD Rd, Ra, -Rb) */ + w[0] = 0x7221U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff) | 0x80000000U; + w[2] = 0x00000000; + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_FMUL: + /* FMUL Rd, Ra, Rb */ + w[0] = 0x7220U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x00400000; + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_FFMA: + /* FFMA Rd, Ra, Rb, Rc */ + w[0] = 0x7223U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_FSETP: + /* FSETP.cond.AND P0, PT, Ra, Rb, PT */ + w[0] = 0x720bU | ((uint32_t)(dst & 0x7) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = insn->imm ? insn->imm : 0x03f06000; /* Default GE condition */ + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_FSEL: + /* FSEL Rd, Ra, Rb, P0 */ + w[0] = 0x7208U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = (uint32_t)(src3 & 0x7); + w[3] = insn->control ? insn->control : 0x000fca00; + break; + + case BW_IR_FMNMX_MIN: + /* FMNMX Rd, Ra, Rb, PT (minimum) */ + w[0] = 0x7209U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x03800000; + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_FMNMX_MAX: + /* FMNMX Rd, Ra, Rb, !PT (maximum) */ + w[0] = 0x7209U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x07800000; + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_I2FP: + /* I2FP.F32.S32 Rd, Ra */ + w[0] = 0x7245U | ((uint32_t)(dst & 0xff) << 16); + w[1] = (uint32_t)(src1 & 0xff); + w[2] = 0x00201400; + w[3] = insn->control ? insn->control : 0x004fe200; + break; + + case BW_IR_F2I: + /* F2I.TRUNC.NTZ Rd, Ra */ + w[0] = 0x7305U | ((uint32_t)(dst & 0xff) << 16); + w[1] = (uint32_t)(src1 & 0xff); + w[2] = 0x0020f100; + w[3] = insn->control ? insn->control : 0x004e2200; + break; + + case BW_IR_MUFU_RCP: + /* MUFU.RCP Rd, Ra */ + w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); + w[1] = (uint32_t)(src1 & 0xff); + w[2] = 0x00001000; + w[3] = insn->control ? insn->control : 0x000e2400; + break; + + case BW_IR_MUFU_RSQ: + /* MUFU.RSQ Rd, Ra */ + w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); + w[1] = (uint32_t)(src1 & 0xff); + w[2] = 0x00001400; + w[3] = insn->control ? insn->control : 0x000e2400; + break; + + case BW_IR_SHFL_DOWN: + /* SHFL.DOWN PT, Rd, Ra, offset, 0x1f */ + w[0] = 0x7f89U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = 0x08001f00U | (((uint32_t)insn->imm & 0x1f) << 21); + w[2] = 0x000e0000; + w[3] = insn->control ? insn->control : 0x000e2400; + break; + + case BW_IR_LDS: + /* LDS Rd, [Ra] */ + w[0] = 0x7984U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = 0x00000000; + w[2] = (uint32_t)(insn->imm & 0xffff); + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_STS: + /* STS [Ra], Rb */ + w[0] = 0x7388U | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = (uint32_t)(insn->imm & 0xffff); + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_EXIT: + w[0] = insn->predicate_p0 ? 0x0000094d : 0x0000794d; + w[1] = 0x00000000; + w[2] = 0x03800000; + w[3] = 0x000fea00; + break; + + case BW_IR_BRA: { + /* sm_121 BRA. Distance is (target - this) - 1, split across the + * low six bits at instruction bits 18..23 and the rest at bit 34. + * Predicate bits 12..15: 7 = always, 0 = @P0, 8 = @!P0. + * A zero delta with no predicate keeps the self-branch word the + * existing kernels already emit after EXIT. */ + int32_t delta = (int32_t)insn->imm; + int32_t rel = delta - 1; + uint32_t pred_field = 0x7u; + if (insn->predicate_p0) + pred_field = insn->predicate_not ? 0x8u : 0x0u; + w[0] = 0x947u | (pred_field << 12) | (((uint32_t)rel & 0x3fu) << 18); + w[1] = (uint32_t)(rel >> 6) << 2; + w[2] = (rel < 0) ? 0x0383ffffu : 0x03800000u; + if (insn->control) + w[3] = insn->control; + else if (delta == 0 && !insn->predicate_p0) + w[3] = 0x000fc000u; + else + w[3] = 0x000fea00u; + break; + } + + case BW_IR_BSSY: { + /* BSSY.RECONVERGENT Bn, target (omega #308). nvcc 13.0.88 -arch=sm_121: + * [0070] BSSY.RECONVERGENT B0, 0x140 ; 0x000000c000007945 0x000fe20003800200 + * opcode 0x945 (NAK sm70_encode.rs:4165), barrier id at bits 16..19 (set_bar_dst + * 16..20), guard predicate at bits 12..15 like BRA, relative byte offset + * target - (this + 16) at bit 32 (NAK: field 34.. in 4-byte units), the cond + * predicate PT at bits 87..89 (0x0380 << 16 in w[2]) and bit 73 (0x200 in w[2]), + * which the disassembler prints as .RECONVERGENT; nvcc sets it on every BSSY/BSYNC + * for sm_121 and Mesa NAK does not. We follow nvcc. The target is the instruction + * after the matching BSYNC, so the delta is at least 2; smaller is refused. */ + int32_t delta = (int32_t)insn->imm; + if (delta < 2 || insn->bar_reg >= BW_RECONV_MAX_BAR) return -1; + uint32_t pred_field = 0x7u; + if (insn->predicate_p0) + pred_field = insn->predicate_not ? 0x8u : 0x0u; + w[0] = 0x945u | (pred_field << 12) | ((uint32_t)insn->bar_reg << 16); + w[1] = (uint32_t)(delta - 1) * 16u; + w[2] = 0x03800200u; + w[3] = insn->control ? insn->control : 0x000fe200u; + break; + } + + case BW_IR_BSYNC: { + /* BSYNC.RECONVERGENT Bn: [0130] BSYNC.RECONVERGENT B0 ; 0x0000000000007941 0x000fea0003800200 + * opcode 0x941 (NAK sm70_encode.rs:4179), barrier id bits 16..19, cond PT, bit 73. */ + if (insn->bar_reg >= BW_RECONV_MAX_BAR) return -1; + uint32_t pred_field = 0x7u; + if (insn->predicate_p0) + pred_field = insn->predicate_not ? 0x8u : 0x0u; + w[0] = 0x941u | (pred_field << 12) | ((uint32_t)insn->bar_reg << 16); + w[1] = 0; + w[2] = 0x03800200u; + w[3] = insn->control ? insn->control : 0x000fea00u; + break; + } + + case BW_IR_MUFU_EX2: + /* MUFU.EX2 Rd, Ra. nvdisasm 13.0 -b SM121: 0x7308 with sub-op 0x0800 decodes + * "MUFU.EX2 R2, R4" (RCP is 0x1000, RSQ 0x1400 above). */ + w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); + w[1] = (uint32_t)(src1 & 0xff); + w[2] = 0x00000800; + w[3] = insn->control ? insn->control : 0x000e2400; + break; + + case BW_IR_BAR_SYNC: + /* BAR.SYNC.DEFER_BLOCKING 0x0: the exact words src/omega_numeric.c:885 runs on + * the chip (nvdisasm: "BAR.SYNC.DEFER_BLOCKING 0x0"). No registers. */ + w[0] = 0x00007b1dU; + w[1] = 0x00000000; + w[2] = 0x00010000; + w[3] = insn->control ? insn->control : 0x000fec00; + break; + + case BW_IR_LDS32: + /* LDS Rd, [Ra+URZ] (32-bit), the src/omega_numeric.c:886 form. */ + w[0] = 0x7984U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = 0x000000ff; + w[2] = 0x08000800; + w[3] = insn->control ? insn->control : 0x000e2800; + break; + + case BW_IR_STS32: + /* STS [Ra+URZ], Rb (32-bit), the src/omega_numeric.c:884 form. */ + w[0] = 0x7988U | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x080008ffU; + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + /* Prime race cut (2026-10-05). An absent operand is RZ for these three ops. */ + case BW_IR_LOP3_LUT: { + /* LOP3.LUT Rd, Ra, Rb, Rc, imm8, !PT. LOP3_XOR above is this form with Rc = RZ, LUT 0x3c. */ + uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU; + uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU; + w[0] = 0x7212U | ((uint32_t)(dst & 0xff) << 16) | (a << 24); + w[1] = b; + w[2] = 0x078e0000U | ((insn->imm & 0xffU) << 8) | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + break; + } + + case BW_IR_SHF_L_U32: { + /* SHF.L.U32 Rd, Ra, Rb, Rc: register-amount form (0x7219) of SHF_R's 0x7819, with the + * .R (w[2] bit 12) and .HI (bit 16) flags clear. Rc is the funnel high word (RZ). */ + uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU; + uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU; + w[0] = 0x7219U | ((uint32_t)(dst & 0xff) << 16) | (a << 24); + w[1] = b; + w[2] = 0x00000600U | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + break; + } + + case BW_IR_IMAD_HI_U32: { + /* IMAD.HI.U32 Rd, Ra, Rb, Rc: opcode 0x227 with the IMAD.WIDE.U32 modifier word. */ + uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU; + uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU; + w[0] = 0x7227U | ((uint32_t)(dst & 0xff) << 16) | (a << 24); + w[1] = b; + w[2] = 0x078e0000U | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + break; + } + + default: + return -1; + } + return 0; +} + +/* Prime race cut (2026-10-05): golden words for LOP3_LUT, SHF_L_U32 and IMAD_HI_U32. + * Each expected (w0, w1, w2) is the low 96 bits of an instruction nvcc 13.0.88 emitted + * for sm_121 (nvcc -arch=sm_121 -cubin, cuobjdump -sass; source and dump in + * bench/prime_race/tests/encoding_oracle.md). Control words (w3) are scheduler output and + * not compared. nvcc is an offline oracle only; nothing here links CUDA. 0 = all good. */ +int omega_blackwell_verify_codegen_fixtures_intops(void) { + OmegaRegAlloc ra; + memset(&ra, 0, sizeof(ra)); + static const int phys[] = { 0, 2, 6, 7, 9, 11, 13 }; + for (int i = 0; i < (int)(sizeof phys / sizeof phys[0]); i++) ra.vreg_to_phys[i] = phys[i]; + enum { R0 = 0, R2, R6, R7, R9, R11, R13, RZ = -1 }; + static const struct { + BlackwellIROpcode op; int d, a, b, c; uint32_t imm; uint32_t w0, w1, w2; const char *text; + } fx[] = { + { BW_IR_LOP3_LUT, R9, R7, R0, RZ, 0xfc, 0x07097212U, 0x00000000U, 0x078efcffU, "LOP3.LUT R9, R7, R0, RZ, 0xfc, !PT" }, + { BW_IR_LOP3_LUT, R11, R6, R7, R0, 0x78, 0x060b7212U, 0x00000007U, 0x078e7800U, "LOP3.LUT R11, R6, R7, R0, 0x78, !PT" }, + { BW_IR_LOP3_LUT, R13, R6, R7, R0, 0x10, 0x060d7212U, 0x00000007U, 0x078e1000U, "LOP3.LUT R13, R6, R7, R0, 0x10, !PT" }, + { BW_IR_LOP3_LUT, R7, RZ, R9, RZ, 0x33, 0xff077212U, 0x00000009U, 0x078e33ffU, "LOP3.LUT R7, RZ, R9, RZ, 0x33, !PT" }, + { BW_IR_SHF_L_U32, R7, R9, R2, RZ, 0, 0x09077219U, 0x00000002U, 0x000006ffU, "SHF.L.U32 R7, R9, R2, RZ" }, + { BW_IR_SHF_L_U32, R7, R0, R7, RZ, 0, 0x00077219U, 0x00000007U, 0x000006ffU, "SHF.L.U32 R7, R0, R7, RZ" }, + { BW_IR_IMAD_HI_U32, R9, R9, R0, R6, 0, 0x09097227U, 0x00000000U, 0x078e0006U, "IMAD.HI.U32 R9, R9, R0, R6" }, + { BW_IR_IMAD_HI_U32, R7, R0, R7, RZ, 0, 0x00077227U, 0x00000007U, 0x078e00ffU, "IMAD.HI.U32 R7, R0, R7, RZ" }, + }; + for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) { + BlackwellIRInsn in; + memset(&in, 0, sizeof in); + in.op = fx[i].op; in.dst_vreg = fx[i].d; in.src1_vreg = fx[i].a; in.src2_vreg = fx[i].b; + in.src3_vreg = fx[i].c; in.ureg = -1; in.imm = fx[i].imm; + uint32_t w[4]; + if (encode_single_insn(&in, &ra, w) != 0) return -(2 * i + 1); + if (w[0] != fx[i].w0 || w[1] != fx[i].w1 || w[2] != fx[i].w2) return -(2 * i + 2); + } + return 0; +} + +int omega_blackwell_encode_one(const BlackwellIRInsn *insn, uint32_t w[4]) { + if (!insn || !w) return -1; + OmegaRegAlloc ra; + memset(&ra, 0, sizeof ra); + for (int i = 0; i < BW_MAX_VREGS; i++) ra.vreg_to_phys[i] = i; + for (int i = 0; i < BW_MAX_UVREGS; i++) ra.uvreg_to_phys[i] = i; + return encode_single_insn(insn, &ra, w); +} + +/* omega #308: BSSY / BSYNC golden words. Oracle: nvcc 13.0.88 -arch=sm_121 -cubin -O3 on the + * kernels in docs/gpu-reconvergence-308.md, cuobjdump -sass (low 64-bit word, then high; the + * top 23 bits of the high word are the control word and are not compared here): + * k_loop [0070] BSSY.RECONVERGENT B0, 0x140 ; 0x000000c000007945 0x000fe20003800200 (delta 13) + * k_nested [0070] BSSY.RECONVERGENT B0, 0x300 ; 0x0000028000007945 0x000fe20003800200 (delta 41) + * k_break [0070] BSSY.RECONVERGENT B0, 0x110 ; 0x0000009000007945 0x000fe20003800200 (delta 10) + * k_loop [0130] BSYNC.RECONVERGENT B0 ; 0x0000000000007941 0x000fea0003800200 + * Barrier ids other than B0 and the predicated forms are decoded by tools/reconv_nvdisasm_check.sh + * (nvdisasm -b SM121 text), not by this table. */ +int omega_blackwell_verify_codegen_fixtures_reconv(void) { + static const struct { BlackwellIROpcode op; int delta; int bar; uint32_t w0, w1, w2; } fx[] = { + { BW_IR_BSSY, 13, 0, 0x00007945U, 0x000000c0U, 0x03800200U }, + { BW_IR_BSSY, 41, 0, 0x00007945U, 0x00000280U, 0x03800200U }, + { BW_IR_BSSY, 10, 0, 0x00007945U, 0x00000090U, 0x03800200U }, + { BW_IR_BSYNC, 0, 0, 0x00007941U, 0x00000000U, 0x03800200U }, + }; + for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) { + BlackwellIRInsn in; + memset(&in, 0, sizeof in); + in.op = fx[i].op; in.dst_vreg = -1; in.src1_vreg = -1; in.src2_vreg = -1; in.src3_vreg = -1; in.ureg = -1; + in.imm = (uint32_t)fx[i].delta; in.bar_reg = (uint8_t)fx[i].bar; + uint32_t w[4]; + if (omega_blackwell_encode_one(&in, w) != 0) return -(2 * i + 1); + if (w[0] != fx[i].w0 || w[1] != fx[i].w1 || w[2] != fx[i].w2) return -(2 * i + 2); + /* control word defaults mirror nvcc's: BSSY 0x000fe200, BSYNC 0x000fea00 */ + if (w[3] != (fx[i].op == BW_IR_BSSY ? 0x000fe200U : 0x000fea00U)) return -(2 * i + 2); + } + /* refusals: a BSSY must name a later instruction (delta >= 2), barrier ids stop at B15 */ + BlackwellIRInsn bad; uint32_t w[4]; + memset(&bad, 0, sizeof bad); bad.op = BW_IR_BSSY; bad.dst_vreg = bad.src1_vreg = bad.src2_vreg = bad.src3_vreg = bad.ureg = -1; + bad.imm = 1; bad.bar_reg = 0; + if (omega_blackwell_encode_one(&bad, w) == 0) return -101; + bad.imm = (uint32_t)-3; + if (omega_blackwell_encode_one(&bad, w) == 0) return -102; + bad.imm = 5; bad.bar_reg = 16; + if (omega_blackwell_encode_one(&bad, w) == 0) return -103; + bad.op = BW_IR_BSYNC; bad.imm = 0; bad.bar_reg = 16; + if (omega_blackwell_encode_one(&bad, w) == 0) return -104; + bad.bar_reg = 15; + if (omega_blackwell_encode_one(&bad, w) != 0 || w[0] != (0x00007941U | (15U << 16))) return -105; + return 0; +} + +/* FB-1 cut 4: word-level fixtures for the four additive ops (checked against the + * nvdisasm decodes recorded in docs/numeric/FB1_CUT4_ELEMENTWISE.md). 0 = all good. */ +int omega_blackwell_verify_codegen_fixtures_fb1cut4(void) { + OmegaRegAlloc ra; + memset(&ra, 0, sizeof(ra)); + ra.vreg_to_phys[0] = 2; /* R2 */ + ra.vreg_to_phys[1] = 4; /* R4 */ + ra.vreg_to_phys[2] = 9; /* R9 */ + ra.vreg_to_phys[3] = 10; /* R10 */ + ra.vreg_to_phys[4] = 8; /* R8 */ + uint32_t w[4]; + + /* MUFU.EX2 R2, R4 */ + BlackwellIRInsn ex2 = { .op = BW_IR_MUFU_EX2, .dst_vreg = 0, .src1_vreg = 1 }; + if (encode_single_insn(&ex2, &ra, w) != 0) return -1; + if (w[0] != 0x00027308U || w[1] != 4 || w[2] != 0x00000800U) return -2; + /* BAR.SYNC.DEFER_BLOCKING 0x0 */ + BlackwellIRInsn bar = { .op = BW_IR_BAR_SYNC }; + if (encode_single_insn(&bar, &ra, w) != 0) return -3; + if (w[0] != 0x00007b1dU || w[1] != 0 || w[2] != 0x00010000U || w[3] != 0x000fec00U) return -4; + /* LDS R9, [R10+URZ] */ + BlackwellIRInsn lds = { .op = BW_IR_LDS32, .dst_vreg = 2, .src1_vreg = 3 }; + if (encode_single_insn(&lds, &ra, w) != 0) return -5; + if (w[0] != 0x0a097984U || w[1] != 0xff || w[2] != 0x08000800U) return -6; + /* STS [R8+URZ], R2 */ + BlackwellIRInsn sts = { .op = BW_IR_STS32, .src1_vreg = 4, .src2_vreg = 0 }; + if (encode_single_insn(&sts, &ra, w) != 0) return -7; + if (w[0] != 0x08007988U || w[1] != 2 || w[2] != 0x080008ffU) return -8; + return 0; +} + +int omega_bw_encode_program(const BlackwellIRProgram *prog, uint8_t *code_buf, size_t max_len, size_t *out_len) { + if (!prog || !code_buf || !out_len) return -1; + + size_t padded_insns = (prog->count + 7) & ~7ULL; + if (padded_insns < 32) padded_insns = 32; + + size_t req_bytes = padded_insns * 16; + if (max_len < req_bytes) return -1; + + uint8_t *p = code_buf; + for (size_t i = 0; i < padded_insns; i++) { + uint32_t w[4]; + if (i < prog->count) { + if (encode_single_insn(&prog->insns[i], &prog->regalloc, w) != 0) { + return -1; + } + } else { + /* Pad with NOP */ + w[0] = 0x00007918; + w[1] = 0x00000000; + w[2] = 0x00000000; + w[3] = 0x000fc000; + } + + p[0] = (uint8_t)(w[0] & 0xff); + p[1] = (uint8_t)((w[0] >> 8) & 0xff); + p[2] = (uint8_t)((w[0] >> 16) & 0xff); + p[3] = (uint8_t)((w[0] >> 24) & 0xff); + + p[4] = (uint8_t)(w[1] & 0xff); + p[5] = (uint8_t)((w[1] >> 8) & 0xff); + p[6] = (uint8_t)((w[1] >> 16) & 0xff); + p[7] = (uint8_t)((w[1] >> 24) & 0xff); + + p[8] = (uint8_t)(w[2] & 0xff); + p[9] = (uint8_t)((w[2] >> 8) & 0xff); + p[10] = (uint8_t)((w[2] >> 16) & 0xff); + p[11] = (uint8_t)((w[2] >> 24) & 0xff); + + p[12] = (uint8_t)(w[3] & 0xff); + p[13] = (uint8_t)((w[3] >> 8) & 0xff); + p[14] = (uint8_t)((w[3] >> 16) & 0xff); + p[15] = (uint8_t)((w[3] >> 24) & 0xff); + + p += 16; + } + + *out_len = req_bytes; + return 0; +} + +int omega_blackwell_codegen_matmul_i32(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel) { + if (!spec || !kernel) return -1; + if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; + + /* Fail closed loudly if K exceeds bounded unroll limit */ + if (spec->k > 32) { + fprintf(stderr, "ERROR: MatMul integer codegen exceeds bounded unroll limit (K=%u > 32)\n", spec->k); + return -1; + } + + BlackwellIRProgram prog; + omega_bw_ir_init(&prog); + + /* 1. Allocate virtual registers */ + /* 64-bit base pointers */ + int v_ptr_a = omega_bw_ir_alloc_vreg64(&prog); + int v_ptr_b = omega_bw_ir_alloc_vreg64(&prog); + int v_ptr_c = omega_bw_ir_alloc_vreg64(&prog); + int uv_desc = omega_bw_ir_alloc_uvreg64(&prog); + + /* 32-bit CTA / Thread indexing */ + int v_ctaid_x = omega_bw_ir_alloc_vreg(&prog); + int v_ctaid_y = omega_bw_ir_alloc_vreg(&prog); + int v_tid_x = omega_bw_ir_alloc_vreg(&prog); + int v_tid_y = omega_bw_ir_alloc_vreg(&prog); + + /* 2D Coordinates & loop invariants */ + int v_col = omega_bw_ir_alloc_vreg(&prog); + int v_row = omega_bw_ir_alloc_vreg(&prog); + int v_row_k = omega_bw_ir_alloc_vreg(&prog); + int v_const_1 = omega_bw_ir_alloc_vreg(&prog); + int v_acc = omega_bw_ir_alloc_vreg(&prog); + + /* Working registers for memory address and operands */ + int v_idx_a = omega_bw_ir_alloc_vreg(&prog); + int v_off_a = omega_bw_ir_alloc_vreg64(&prog); + int v_val_a = omega_bw_ir_alloc_vreg(&prog); + + int v_idx_b = omega_bw_ir_alloc_vreg(&prog); + int v_off_b = omega_bw_ir_alloc_vreg64(&prog); + int v_val_b = omega_bw_ir_alloc_vreg(&prog); + + int v_idx_c = omega_bw_ir_alloc_vreg(&prog); + int v_off_c = omega_bw_ir_alloc_vreg64(&prog); + + if (v_off_c < 0) return -1; + + /* 2. Load 64-bit global memory descriptor into uniform register pair */ + /* LDCU.64 uv_desc, c[0x0][0x358] */ + BlackwellIRInsn insn_desc = { .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true }; + omega_bw_ir_append(&prog, &insn_desc); + + /* 3. Load 64-bit base pointers from Constant Bank 0 */ + /* LDC.64 v_ptr_a, c[0x0][0x380] */ + BlackwellIRInsn insn_ptra = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380 }; + omega_bw_ir_append(&prog, &insn_ptra); + + /* LDC.64 v_ptr_b, c[0x0][0x388] */ + BlackwellIRInsn insn_ptrb = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388 }; + omega_bw_ir_append(&prog, &insn_ptrb); + + /* LDC.64 v_ptr_c, c[0x0][0x390] */ + BlackwellIRInsn insn_ptrc = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390 }; + omega_bw_ir_append(&prog, &insn_ptrc); + + /* 4. Query 2D CTA and Thread coordinates */ + /* S2R v_ctaid_x, SR_CTAID.X */ + BlackwellIRInsn insn_ctax = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X }; + omega_bw_ir_append(&prog, &insn_ctax); + + /* S2R v_ctaid_y, SR_CTAID.Y */ + BlackwellIRInsn insn_ctay = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y }; + omega_bw_ir_append(&prog, &insn_ctay); + + /* S2R v_tid_x, SR_TID.X */ + BlackwellIRInsn insn_tidx = { .op = BW_IR_S2R, .dst_vreg = v_tid_x, .imm = BW_SR_TID_X }; + omega_bw_ir_append(&prog, &insn_tidx); + + /* S2R v_tid_y, SR_TID.Y */ + BlackwellIRInsn insn_tidy = { .op = BW_IR_S2R, .dst_vreg = v_tid_y, .imm = BW_SR_TID_Y }; + omega_bw_ir_append(&prog, &insn_tidy); + + /* 5. Compute 2D (row, col) coordinates */ + /* Standard 16x16 tile threads: col = ctaid_x * 16 + tid_x */ + BlackwellIRInsn insn_col = { .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = 16, .src3_vreg = v_tid_x }; + omega_bw_ir_append(&prog, &insn_col); + + /* row = ctaid_y * 16 + tid_y */ + BlackwellIRInsn insn_row = { .op = BW_IR_IMAD, .dst_vreg = v_row, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = 16, .src3_vreg = v_tid_y }; + omega_bw_ir_append(&prog, &insn_row); + + /* 6. Precompute row base index: v_row_k = v_row * K + RZ */ + BlackwellIRInsn insn_row_k = { .op = BW_IR_IMAD, .dst_vreg = v_row_k, .src1_vreg = v_row, .src2_vreg = -1, .imm = spec->k, .src3_vreg = -1 }; + omega_bw_ir_append(&prog, &insn_row_k); + + /* 7. Initialize v_const_1 = 1 for immediate linear indexing */ + BlackwellIRInsn insn_c1 = { .op = BW_IR_MOV_IMM, .dst_vreg = v_const_1, .imm = 1 }; + omega_bw_ir_append(&prog, &insn_c1); + + /* 8. Initialize accumulator to zero: MOV v_acc, RZ */ + BlackwellIRInsn insn_init_acc = { .op = BW_IR_MOV_RZ, .dst_vreg = v_acc }; + omega_bw_ir_append(&prog, &insn_init_acc); + + /* 9. Synthesize inner accumulation loop across K iterations */ + for (uint32_t step = 0; step < spec->k; step++) { + /* A[row, step]: idx_a = v_row_k + step = v_const_1 * step + v_row_k */ + BlackwellIRInsn insn_idxa = { .op = BW_IR_IMAD, .dst_vreg = v_idx_a, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = step, .src3_vreg = v_row_k }; + omega_bw_ir_append(&prog, &insn_idxa); + + /* off_a = v_ptr_a + idx_a * 4 */ + BlackwellIRInsn insn_offa = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off_a, .src1_vreg = v_idx_a, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_a }; + omega_bw_ir_append(&prog, &insn_offa); + + /* Load A[row, step]: LDG.E v_val_a, desc[uv_desc][v_off_a.64] */ + BlackwellIRInsn insn_lda = { .op = BW_IR_LDG_E, .dst_vreg = v_val_a, .src1_vreg = v_off_a, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; + omega_bw_ir_append(&prog, &insn_lda); + + /* B[step, col]: idx_b = step * N + col = v_const_1 * (step * N) + v_col */ + uint32_t step_n = step * spec->n; + BlackwellIRInsn insn_idxb = { .op = BW_IR_IMAD, .dst_vreg = v_idx_b, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = step_n, .src3_vreg = v_col }; + omega_bw_ir_append(&prog, &insn_idxb); + + /* off_b = v_ptr_b + idx_b * 4 */ + BlackwellIRInsn insn_offb = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off_b, .src1_vreg = v_idx_b, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_b }; + omega_bw_ir_append(&prog, &insn_offb); + + /* Load B[step, col]: LDG.E v_val_b, desc[uv_desc][v_off_b.64] */ + BlackwellIRInsn insn_ldb = { .op = BW_IR_LDG_E, .dst_vreg = v_val_b, .src1_vreg = v_off_b, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; + omega_bw_ir_append(&prog, &insn_ldb); + + /* Multiply and accumulate: IMAD v_acc, v_val_a, v_val_b, v_acc */ + BlackwellIRInsn insn_acc = { .op = BW_IR_IMAD, .dst_vreg = v_acc, .src1_vreg = v_val_a, .src2_vreg = v_val_b, .src3_vreg = v_acc, .control = 0x004fca00 }; + omega_bw_ir_append(&prog, &insn_acc); + } + + /* 10. Compute output index: idx_c = row * N + col */ + BlackwellIRInsn insn_idxc = { .op = BW_IR_IMAD, .dst_vreg = v_idx_c, .src1_vreg = v_row, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col }; + omega_bw_ir_append(&prog, &insn_idxc); + + /* off_c = v_ptr_c + idx_c * 4 */ + BlackwellIRInsn insn_offc = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off_c, .src1_vreg = v_idx_c, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; + omega_bw_ir_append(&prog, &insn_offc); + + /* Store C[row, col]: STG.E desc[uv_desc][v_off_c.64], v_acc */ + BlackwellIRInsn insn_stc = { .op = BW_IR_STG_E, .src1_vreg = v_off_c, .src2_vreg = v_acc, .src3_vreg = -1, .ureg = uv_desc }; + omega_bw_ir_append(&prog, &insn_stc); + + /* 11. Kernel termination */ + BlackwellIRInsn insn_exit = { .op = BW_IR_EXIT, .predicate_p0 = false }; + omega_bw_ir_append(&prog, &insn_exit); + + BlackwellIRInsn insn_bra = { .op = BW_IR_BRA }; + omega_bw_ir_append(&prog, &insn_bra); + + /* 12. Solve bounded deterministic register allocation */ + if (omega_bw_regalloc_solve(&prog) != 0) { + return -1; + } + + /* 13. Allocate code buffer and encode instructions */ + size_t padded_insns = (prog.count + 7) & ~7ULL; + if (padded_insns < 32) padded_insns = 32; + size_t alloc_size = padded_insns * 16; + + kernel->code = (uint8_t *)malloc(alloc_size); + if (!kernel->code) return -1; + + size_t emitted_size = 0; + if (omega_bw_encode_program(&prog, kernel->code, alloc_size, &emitted_size) != 0) { + free(kernel->code); + kernel->code = NULL; + return -1; + } + + kernel->code_size = emitted_size; + kernel->insn_count = emitted_size / 16; + kernel->gpr_count = prog.regalloc.peak_gpr_usage; + kernel->uniform_gpr_count = prog.regalloc.peak_ugpr_usage; + + /* 14. Compute runtime SHA-256 digest of emitted machine code */ + sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); + return 0; +} + +int omega_blackwell_codegen_matmul_tensor_prog(const OmegaMatMulSpec *spec, BlackwellIRProgram *prog) { + if (!spec || !prog) return -1; + if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; + if (spec->precision != OMEGA_MATMUL_PRECISION_FP16 && spec->precision != OMEGA_MATMUL_PRECISION_BF16) return -1; + if (spec->m % 16 != 0 || spec->n % 8 != 0 || spec->k != 16) { + fprintf(stderr, "ERROR: MatMul tensor codegen requires M multiple of 16, N multiple of 8, K=16 (got %ux%ux%u)\n", + spec->m, spec->k, spec->n); + return -1; + } + + omega_bw_ir_init(prog); + + /* 1. Allocate virtual registers */ + /* 64-bit base pointers */ + int v_ptr_a = omega_bw_ir_alloc_vreg64(prog); + int v_ptr_b = omega_bw_ir_alloc_vreg64(prog); + int v_ptr_c = omega_bw_ir_alloc_vreg64(prog); + int uv_desc = omega_bw_ir_alloc_uvreg64(prog); + + /* 32-bit CTA / Thread indexing */ + int v_tid = omega_bw_ir_alloc_vreg(prog); + int v_ctaid_x = omega_bw_ir_alloc_vreg(prog); + int v_ctaid_y = omega_bw_ir_alloc_vreg(prog); + + /* Tile and thread coordinates */ + int v_group_id = omega_bw_ir_alloc_vreg(prog); + int v_tid_in_grp = omega_bw_ir_alloc_vreg(prog); + int v_row_base = omega_bw_ir_alloc_vreg(prog); + int v_col_base = omega_bw_ir_alloc_vreg(prog); + int v_r0 = omega_bw_ir_alloc_vreg(prog); + int v_r1 = omega_bw_ir_alloc_vreg(prog); + int v_col_b = omega_bw_ir_alloc_vreg(prog); + int v_tid_col2 = omega_bw_ir_alloc_vreg(prog); + int v_const_1 = omega_bw_ir_alloc_vreg(prog); + + /* Bundles for HMMA */ + int v_ra = omega_bw_ir_alloc_vreg128(prog); /* Quad: Ra[0]..Ra[3] */ + int v_rb = omega_bw_ir_alloc_vreg64(prog); /* Pair: Rb[0]..Rb[1] */ + int v_rd = omega_bw_ir_alloc_vreg128(prog); /* Quad: Rd[0]..Rd[3] */ + + /* Scratch registers for indexing and loads */ + int v_idx = omega_bw_ir_alloc_vreg(prog); + int v_off = omega_bw_ir_alloc_vreg64(prog); + int v_b_tmp0 = omega_bw_ir_alloc_vreg(prog); + int v_b_tmp1 = omega_bw_ir_alloc_vreg(prog); + + if (v_b_tmp1 < 0) return -1; + + /* 2. Load 64-bit global memory descriptor into uniform register pair */ + /* LDCU.64 uv_desc, c[0x0][0x358] */ + BlackwellIRInsn insn_desc = { .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true }; + omega_bw_ir_append(prog, &insn_desc); + + /* 3. Load 64-bit base pointers from Constant Bank 0 */ + /* LDC.64 v_ptr_a, c[0x0][0x380] */ + BlackwellIRInsn insn_ptra = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380 }; + omega_bw_ir_append(prog, &insn_ptra); + + /* LDC.64 v_ptr_b, c[0x0][0x388] */ + BlackwellIRInsn insn_ptrb = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388 }; + omega_bw_ir_append(prog, &insn_ptrb); + + /* LDC.64 v_ptr_c, c[0x0][0x390] */ + BlackwellIRInsn insn_ptrc = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390 }; + omega_bw_ir_append(prog, &insn_ptrc); + + /* 4. Query Special Registers */ + /* S2R v_tid, SR_TID.X */ + BlackwellIRInsn insn_tidx = { .op = BW_IR_S2R, .dst_vreg = v_tid, .imm = BW_SR_TID_X }; + omega_bw_ir_append(prog, &insn_tidx); + + /* S2R v_ctaid_x, SR_CTAID.X */ + BlackwellIRInsn insn_cta_x = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X }; + omega_bw_ir_append(prog, &insn_cta_x); + + /* S2R v_ctaid_y, SR_CTAID.Y */ + BlackwellIRInsn insn_cta_y = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y }; + omega_bw_ir_append(prog, &insn_cta_y); + + /* 5. Set up constant 1 */ + BlackwellIRInsn insn_c1 = { .op = BW_IR_MOV_IMM, .dst_vreg = v_const_1, .imm = 1 }; + omega_bw_ir_append(prog, &insn_c1); + + /* 6. Extract warp-level tile and thread coordinates */ + /* group_id = tid >> 2 */ + BlackwellIRInsn insn_grp = { .op = BW_IR_SHF_R, .dst_vreg = v_group_id, .src1_vreg = v_tid, .imm = 2 }; + omega_bw_ir_append(prog, &insn_grp); + + /* tid_in_grp = tid & 3 */ + BlackwellIRInsn insn_tig = { .op = BW_IR_LOP3_AND, .dst_vreg = v_tid_in_grp, .src1_vreg = v_tid, .imm = 3 }; + omega_bw_ir_append(prog, &insn_tig); + + /* row_base = ctaid_y * 16 */ + BlackwellIRInsn insn_rbase = { .op = BW_IR_IMAD, .dst_vreg = v_row_base, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = 16, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_rbase); + + /* col_base = ctaid_x * 8 */ + BlackwellIRInsn insn_cbase = { .op = BW_IR_IMAD, .dst_vreg = v_col_base, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = 8, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_cbase); + + /* r0 = row_base + group_id */ + BlackwellIRInsn insn_r0 = { .op = BW_IR_IADD3, .dst_vreg = v_r0, .src1_vreg = v_row_base, .src2_vreg = v_group_id, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_r0); + + /* r1 = r0 + 8 */ + BlackwellIRInsn insn_r1 = { .op = BW_IR_IMAD, .dst_vreg = v_r1, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 8, .src3_vreg = v_r0 }; + omega_bw_ir_append(prog, &insn_r1); + + /* col_b = col_base + group_id */ + BlackwellIRInsn insn_colb = { .op = BW_IR_IADD3, .dst_vreg = v_col_b, .src1_vreg = v_col_base, .src2_vreg = v_group_id, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_colb); + + /* tid_col2 = tid_in_grp * 2 */ + BlackwellIRInsn insn_tcol2 = { .op = BW_IR_IMAD, .dst_vreg = v_tid_col2, .src1_vreg = v_tid_in_grp, .src2_vreg = -1, .imm = 2, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_tcol2); + + /* 7. Load A fragments (16x16 row-major, 2 bytes/element, row stride 32 bytes) */ + /* Pair 0: a0, a1 at r0 * 32 + tid_col2 * 2 */ + BlackwellIRInsn insn_a0_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_a0_idx1); + BlackwellIRInsn insn_a0_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = 2, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_a0_idx2); + BlackwellIRInsn insn_a0_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 1, .src3_vreg = v_ptr_a }; + omega_bw_ir_append(prog, &insn_a0_off); + BlackwellIRInsn insn_lda0 = { .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 0, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; + omega_bw_ir_append(prog, &insn_lda0); + + /* Pair 1: a2, a3 at r1 * 32 + tid_col2 * 2 */ + BlackwellIRInsn insn_a1_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_a1_idx1); + BlackwellIRInsn insn_a1_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = 2, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_a1_idx2); + BlackwellIRInsn insn_a1_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 1, .src3_vreg = v_ptr_a }; + omega_bw_ir_append(prog, &insn_a1_off); + BlackwellIRInsn insn_lda1 = { .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 1, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; + omega_bw_ir_append(prog, &insn_lda1); + + /* Pair 2: a4, a5 at r0 * 32 + tid_col2 * 2 + 16 */ + BlackwellIRInsn insn_a2_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_a2_idx1); + BlackwellIRInsn insn_a2_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = 2, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_a2_idx2); + BlackwellIRInsn insn_a2_idx3 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 16, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_a2_idx3); + BlackwellIRInsn insn_a2_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 1, .src3_vreg = v_ptr_a }; + omega_bw_ir_append(prog, &insn_a2_off); + BlackwellIRInsn insn_lda2 = { .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 2, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; + omega_bw_ir_append(prog, &insn_lda2); + + /* Pair 3: a6, a7 at r1 * 32 + tid_col2 * 2 + 16 */ + BlackwellIRInsn insn_a3_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_a3_idx1); + BlackwellIRInsn insn_a3_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = 2, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_a3_idx2); + BlackwellIRInsn insn_a3_idx3 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 16, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_a3_idx3); + BlackwellIRInsn insn_a3_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 1, .src3_vreg = v_ptr_a }; + omega_bw_ir_append(prog, &insn_a3_off); + BlackwellIRInsn insn_lda3 = { .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 3, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; + omega_bw_ir_append(prog, &insn_lda3); + + /* 8. Load and pack B fragments (16xN row-major, 2 bytes/element) */ + /* b0 at tid_col2 * N + col_b */ + BlackwellIRInsn insn_b0_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col_b }; + omega_bw_ir_append(prog, &insn_b0_idx); + BlackwellIRInsn insn_b0_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b }; + omega_bw_ir_append(prog, &insn_b0_off); + BlackwellIRInsn insn_ldb0 = { .op = BW_IR_LDG_E_U16, .dst_vreg = v_b_tmp0, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; + omega_bw_ir_append(prog, &insn_ldb0); + + /* b1 at (tid_col2 + 1) * N + col_b = b0_idx + N */ + BlackwellIRInsn insn_b1_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_b1_idx); + BlackwellIRInsn insn_b1_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b }; + omega_bw_ir_append(prog, &insn_b1_off); + BlackwellIRInsn insn_ldb1 = { .op = BW_IR_LDG_E_U16, .dst_vreg = v_b_tmp1, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; + omega_bw_ir_append(prog, &insn_ldb1); + + /* Pack Rb[0] = (b1 << 16) | b0 (wait on barrier 2) */ + BlackwellIRInsn insn_pack_b0 = { .op = BW_IR_IMAD, .dst_vreg = v_rb, .dst_subreg = 0, .src1_vreg = v_b_tmp1, .src2_vreg = -1, .imm = 0x10000, .src3_vreg = v_b_tmp0, .control = 0x004fca00 }; + omega_bw_ir_append(prog, &insn_pack_b0); + + /* b2 at (tid_col2 + 8) * N + col_b */ + BlackwellIRInsn insn_b2_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col_b }; + omega_bw_ir_append(prog, &insn_b2_idx1); + BlackwellIRInsn insn_b2_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 8 * spec->n, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_b2_idx2); + BlackwellIRInsn insn_b2_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b }; + omega_bw_ir_append(prog, &insn_b2_off); + BlackwellIRInsn insn_ldb2 = { .op = BW_IR_LDG_E_U16, .dst_vreg = v_b_tmp0, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; + omega_bw_ir_append(prog, &insn_ldb2); + + /* b3 at (tid_col2 + 9) * N + col_b = b2_idx + N */ + BlackwellIRInsn insn_b3_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_b3_idx); + BlackwellIRInsn insn_b3_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b }; + omega_bw_ir_append(prog, &insn_b3_off); + BlackwellIRInsn insn_ldb3 = { .op = BW_IR_LDG_E_U16, .dst_vreg = v_b_tmp1, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; + omega_bw_ir_append(prog, &insn_ldb3); + + /* Pack Rb[1] = (b3 << 16) | b2 (wait on barrier 2) */ + BlackwellIRInsn insn_pack_b1 = { .op = BW_IR_IMAD, .dst_vreg = v_rb, .dst_subreg = 1, .src1_vreg = v_b_tmp1, .src2_vreg = -1, .imm = 0x10000, .src3_vreg = v_b_tmp0, .control = 0x004fca00 }; + omega_bw_ir_append(prog, &insn_pack_b1); + + /* 9. Execute HMMA Tensor Core instruction (wait on barrier 1 for A loads) */ + BlackwellIROpcode mma_op = (spec->precision == OMEGA_MATMUL_PRECISION_BF16) ? BW_IR_HMMA_BF16 : BW_IR_HMMA_F16; + BlackwellIRInsn insn_hmma = { + .op = mma_op, + .dst_vreg = v_rd, + .dst_subreg = 0, + .src1_vreg = v_ra, + .src1_subreg = 0, + .src2_vreg = v_rb, + .src2_subreg = 0, + .src3_vreg = -1, /* RZ accumulator input */ + .control = 0x002fca00 /* Wait on barrier 1 */ + }; + omega_bw_ir_append(prog, &insn_hmma); + + /* 10. Store C output fragments (16xN row-major, 4 bytes/float element) */ + /* d0: row r0, col col_base + tid_col2 + 0 */ + BlackwellIRInsn insn_d0_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col_base }; + omega_bw_ir_append(prog, &insn_d0_idx1); + BlackwellIRInsn insn_d0_idx2 = { .op = BW_IR_IADD3, .dst_vreg = v_idx, .src1_vreg = v_idx, .src2_vreg = v_tid_col2, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_d0_idx2); + BlackwellIRInsn insn_d0_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; + omega_bw_ir_append(prog, &insn_d0_off); + BlackwellIRInsn insn_std0 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 0, .ureg = uv_desc, .control = 0x000fe200 }; + omega_bw_ir_append(prog, &insn_std0); + + /* d1: row r0, col col_base + tid_col2 + 1 */ + BlackwellIRInsn insn_d1_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 1, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_d1_idx); + BlackwellIRInsn insn_d1_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; + omega_bw_ir_append(prog, &insn_d1_off); + BlackwellIRInsn insn_std1 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 1, .ureg = uv_desc, .control = 0x000fe200 }; + omega_bw_ir_append(prog, &insn_std1); + + /* d2: row r1, col col_base + tid_col2 + 0 */ + BlackwellIRInsn insn_d2_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col_base }; + omega_bw_ir_append(prog, &insn_d2_idx1); + BlackwellIRInsn insn_d2_idx2 = { .op = BW_IR_IADD3, .dst_vreg = v_idx, .src1_vreg = v_idx, .src2_vreg = v_tid_col2, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_d2_idx2); + BlackwellIRInsn insn_d2_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; + omega_bw_ir_append(prog, &insn_d2_off); + BlackwellIRInsn insn_std2 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 2, .ureg = uv_desc, .control = 0x000fe200 }; + omega_bw_ir_append(prog, &insn_std2); + + /* d3: row r1, col col_base + tid_col2 + 1 */ + BlackwellIRInsn insn_d3_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 1, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_d3_idx); + BlackwellIRInsn insn_d3_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; + omega_bw_ir_append(prog, &insn_d3_off); + BlackwellIRInsn insn_std3 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 3, .ureg = uv_desc, .control = 0x000fe200 }; + omega_bw_ir_append(prog, &insn_std3); + + /* 11. Program Exit */ + BlackwellIRInsn insn_exit = { .op = BW_IR_EXIT }; + omega_bw_ir_append(prog, &insn_exit); + BlackwellIRInsn insn_bra = { .op = BW_IR_BRA }; + omega_bw_ir_append(prog, &insn_bra); + + /* 12. Run bounded register allocation */ + if (omega_bw_regalloc_solve(prog) != 0) { + return -1; + } + return 0; +} + +int omega_blackwell_codegen_matmul_tensor(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel) { + if (!spec || !kernel) return -1; + BlackwellIRProgram prog; + if (omega_blackwell_codegen_matmul_tensor_prog(spec, &prog) != 0) { + return -1; + } + + size_t max_bytes = 4096; + kernel->code = malloc(max_bytes); + if (!kernel->code) return -1; + + size_t emitted_size = 0; + if (omega_bw_encode_program(&prog, kernel->code, max_bytes, &emitted_size) != 0) { + free(kernel->code); + kernel->code = NULL; + return -1; + } + + kernel->code_size = emitted_size; + kernel->insn_count = emitted_size / 16; + kernel->gpr_count = prog.regalloc.peak_gpr_usage; + kernel->uniform_gpr_count = prog.regalloc.peak_ugpr_usage; + + sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); + return 0; +} + +/* ---- FB-1 cut 1b: looped tensor matmul ------------------------------------------ + * + * Control words (bundle bits 105..125, decoded from the chip-proven kernel and from + * the vendor compiler's SASS for an equivalent HMMA loop on this chip, 2026-10-04): + * stall = bits 9..12 of w[3], yield = 13, write barrier = 14..16, read barrier = + * 17..19, wait mask = 20..25. 7 in a barrier field means "none". + * LDG (A) 0x001ea800 stall 4, yield, sets SB2 + * LDG.U16 (B) 0x002ea800 stall 4, yield, sets SB2 + * pack IMAD 0x004fca00 stall 5, waits SB2 (every outstanding load has landed) + * HMMA 0x002fca00 stall 5 (fixed latency: the vendor compiler sets no + * write barrier on HMMA either; consumers wait by stall count) + * STG 0x0007e200 stall 1, yield, read barrier SB3 (the store may read its + * data/address registers late; the next writer waits SB3) + * wait SB3 mask bit 23 (0x00800000) on the first instruction that overwrites a + * register a store reads: the C pointer advance. + * Loop-carried registers are kept live to the end of their loop (keep_alive) because + * the allocator is a straight-line linear scan and knows nothing about back edges. + */ +static void bw_keep_alive(BlackwellIRProgram *prog, int from_idx, int to_idx, int last_idx) { + /* every vreg first defined in [from_idx, to_idx) stays live until last_idx */ + for (int v = 0; v < prog->regalloc.num_vregs; v++) { + OmegaLiveInterval *iv = &prog->regalloc.intervals[v]; + if (iv->first_def >= from_idx && iv->first_def < to_idx && iv->last_use < last_idx) + iv->last_use = last_idx; + } +} + +#define BW_APPEND(prog, ...) do { BlackwellIRInsn _i = { __VA_ARGS__ }; if (omega_bw_ir_append((prog), &_i) < 0) return -1; } while (0) + +int omega_blackwell_codegen_matmul_tensor_loop_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, + int mutant, BlackwellIRProgram *prog) { + if (!spec || !prog) return -1; + if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; + if (spec->precision != OMEGA_MATMUL_PRECISION_FP16 && spec->precision != OMEGA_MATMUL_PRECISION_BF16) return -1; + if (spec->m % 16 != 0 || spec->n % 8 != 0 || spec->k % 16 != 0) { + fprintf(stderr, "ERROR: looped MatMul tensor codegen requires M%%16, N%%8, K%%16 (got %ux%ux%u)\n", + spec->m, spec->k, spec->n); + return -1; + } + const uint32_t K = spec->k, N = spec->n, NT = N / 8, KSTEPS = K / 16; + if (grid_x == 0 || grid_x > NT) return -1; + /* every immediate below must fit 32 bits: 32 * N (B row-block stride in bytes) is the largest */ + if ((uint64_t)N * 32u > 0xffffffffull || (uint64_t)grid_x * 32u > 0xffffffffull) return -1; + if (mutant && KSTEPS < 2) return -1; + + omega_bw_ir_init(prog); + + int v_ptr_a = omega_bw_ir_alloc_vreg64(prog), v_ptr_b = omega_bw_ir_alloc_vreg64(prog), v_ptr_c = omega_bw_ir_alloc_vreg64(prog); + int uv_desc = omega_bw_ir_alloc_uvreg64(prog); + int v_tid = omega_bw_ir_alloc_vreg(prog), v_ctaid_x = omega_bw_ir_alloc_vreg(prog), v_ctaid_y = omega_bw_ir_alloc_vreg(prog); + int v_group = omega_bw_ir_alloc_vreg(prog), v_tig = omega_bw_ir_alloc_vreg(prog), v_tcol2 = omega_bw_ir_alloc_vreg(prog); + int v_r0 = omega_bw_ir_alloc_vreg(prog), v_r1 = omega_bw_ir_alloc_vreg(prog), v_c1 = omega_bw_ir_alloc_vreg(prog); + int v_idx = omega_bw_ir_alloc_vreg(prog), v_col = omega_bw_ir_alloc_vreg(prog), v_cnt = omega_bw_ir_alloc_vreg(prog); + int v_pa0 = omega_bw_ir_alloc_vreg64(prog), v_pa1 = omega_bw_ir_alloc_vreg64(prog), v_pbcol = omega_bw_ir_alloc_vreg64(prog); + int v_pc0 = omega_bw_ir_alloc_vreg64(prog), v_pc1 = omega_bw_ir_alloc_vreg64(prog); + int v_pa0c = omega_bw_ir_alloc_vreg64(prog), v_pa1c = omega_bw_ir_alloc_vreg64(prog), v_pbc = omega_bw_ir_alloc_vreg64(prog); + int v_ta2 = omega_bw_ir_alloc_vreg64(prog), v_ta3 = omega_bw_ir_alloc_vreg64(prog); + int v_tb1 = omega_bw_ir_alloc_vreg64(prog), v_tb2 = omega_bw_ir_alloc_vreg64(prog), v_tb3 = omega_bw_ir_alloc_vreg64(prog); + int v_tc1 = omega_bw_ir_alloc_vreg64(prog), v_tc3 = omega_bw_ir_alloc_vreg64(prog); + int v_ra = omega_bw_ir_alloc_vreg128(prog), v_rb = omega_bw_ir_alloc_vreg64(prog), v_rd = omega_bw_ir_alloc_vreg128(prog); + int v_b0 = omega_bw_ir_alloc_vreg(prog), v_b1 = omega_bw_ir_alloc_vreg(prog); + if (v_b1 < 0 || uv_desc < 0) return -1; + + const uint32_t CTRL_LDA = 0x001ea800u, CTRL_LDB = 0x002ea800u, CTRL_PACK = 0x004fca00u; + const uint32_t CTRL_HMMA = 0x002fca00u, CTRL_STG_RDBAR3 = 0x0007e200u, CTRL_IMADW_WAIT3 = 0x009fcc00u; + const uint32_t CTRL_MOVRZ_WAIT3 = 0x008fe200u; + + /* prologue: descriptor, pointers, ids */ + BW_APPEND(prog, .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true); + BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380); + BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388); + BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390); + BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_tid, .imm = BW_SR_TID_X); + BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X); + BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y); + BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_c1, .imm = 1); + BW_APPEND(prog, .op = BW_IR_SHF_R, .dst_vreg = v_group, .src1_vreg = v_tid, .imm = 2); + BW_APPEND(prog, .op = BW_IR_LOP3_AND, .dst_vreg = v_tig, .src1_vreg = v_tid, .imm = 3); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_tcol2, .src1_vreg = v_tig, .src2_vreg = -1, .imm = 2, .src3_vreg = -1); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r0, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = 16, .src3_vreg = v_group); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 8, .src3_vreg = v_r0); + /* pa0 = A + (r0*K + tcol2)*2 ; pa1 likewise with r1 (A row-major, K bf16 per row) */ + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = K, .src3_vreg = v_tcol2); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa0, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_a); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = K, .src3_vreg = v_tcol2); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa1, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_a); + /* col = ctaid_x (first column tile of this CTA) */ + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = 1, .src3_vreg = -1); + /* pbcol = B + (tcol2*N + col*8 + group)*2 */ + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tcol2, .src2_vreg = -1, .imm = N, .src3_vreg = v_group); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbcol, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b); + /* pc0 = C + (r0*N + col*8 + tcol2)*4 ; pc1 with r1 */ + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc0, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc1, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c); + + /* ---- column-tile loop ---- */ + const int L_col = (int)prog->count; + /* accumulator = 0 (the first write waits SB3: the previous tile's stores may still read it) */ + BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 0, .control = CTRL_MOVRZ_WAIT3); + BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 1); + BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 2); + BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 3); + /* K cursors start at the row/column bases (IMAD.WIDE with imm 0 is a 64-bit copy) */ + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa0c, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pa0); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa1c, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pa1); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbc, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pbcol); + BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_cnt, .imm = mutant ? KSTEPS - 1 : KSTEPS); + + /* ---- K loop: one 16x16 A fragment and one 16x8 B fragment per step ---- */ + const int L_k = (int)prog->count; + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_ta2, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 16, .src3_vreg = v_pa0c); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_ta3, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 16, .src3_vreg = v_pa1c); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tb1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 2u * N, .src3_vreg = v_pbc); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tb2, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 16u * N, .src3_vreg = v_pbc); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tb3, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 18u * N, .src3_vreg = v_pbc); + /* A: a0,a1 at (r0, kk+tcol2) ; a2,a3 at (r1, ...) ; a4,a5 at (r0, +8) ; a6,a7 at (r1, +8) */ + BW_APPEND(prog, .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 0, .src1_vreg = v_pa0c, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDA); + BW_APPEND(prog, .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 1, .src1_vreg = v_pa1c, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDA); + BW_APPEND(prog, .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 2, .src1_vreg = v_ta2, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDA); + BW_APPEND(prog, .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 3, .src1_vreg = v_ta3, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDA); + /* B: rows kk+tcol2, +1, +8, +9 at column col*8+group */ + BW_APPEND(prog, .op = BW_IR_LDG_E_U16, .dst_vreg = v_b0, .src1_vreg = v_pbc, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDB); + BW_APPEND(prog, .op = BW_IR_LDG_E_U16, .dst_vreg = v_b1, .src1_vreg = v_tb1, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDB); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_rb, .dst_subreg = 0, .src1_vreg = v_b1, .src2_vreg = -1, .imm = 0x10000, .src3_vreg = v_b0, .control = CTRL_PACK); + BW_APPEND(prog, .op = BW_IR_LDG_E_U16, .dst_vreg = v_b0, .src1_vreg = v_tb2, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDB); + BW_APPEND(prog, .op = BW_IR_LDG_E_U16, .dst_vreg = v_b1, .src1_vreg = v_tb3, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDB); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_rb, .dst_subreg = 1, .src1_vreg = v_b1, .src2_vreg = -1, .imm = 0x10000, .src3_vreg = v_b0, .control = CTRL_PACK); + /* rd += ra * rb (accumulate in place) */ + BW_APPEND(prog, .op = (spec->precision == OMEGA_MATMUL_PRECISION_BF16) ? BW_IR_HMMA_BF16 : BW_IR_HMMA_F16, + .dst_vreg = v_rd, .dst_subreg = 0, .src1_vreg = v_ra, .src1_subreg = 0, .src2_vreg = v_rb, .src2_subreg = 0, + .src3_vreg = v_rd, .src3_subreg = 0, .control = CTRL_HMMA); + /* advance K: A by 16 bf16 (32 bytes), B by 16 rows (32 N bytes); every load above has landed (packs waited SB2) */ + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa0c, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 32, .src3_vreg = v_pa0c); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa1c, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 32, .src3_vreg = v_pa1c); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbc, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 32u * N, .src3_vreg = v_pbc); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_cnt, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0xffffffffu, .src3_vreg = v_cnt); + BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_cnt, .src2_vreg = -1, .imm = 1, .src3_vreg = -1); + { + int here = (int)prog->count; + BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_k - here), .predicate_p0 = true); + } + const int L_k_end = (int)prog->count - 1; + + /* ---- store the 16x8 tile: d0 (r0, c), d1 (r0, c+1), d2 (r1, c), d3 (r1, c+1) ---- */ + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tc1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 4, .src3_vreg = v_pc0); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tc3, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 4, .src3_vreg = v_pc1); + BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc0, .src2_vreg = v_rd, .src2_subreg = 0, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); + BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_tc1, .src2_vreg = v_rd, .src2_subreg = 1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); + BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc1, .src2_vreg = v_rd, .src2_subreg = 2, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); + BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_tc3, .src2_vreg = v_rd, .src2_subreg = 3, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); + /* next column tile of this CTA: col += grid_x ; B by grid_x*8 bf16 ; C by grid_x*8 f32 (waits SB3 first) */ + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x, .src3_vreg = v_col); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc0, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * 32u, .src3_vreg = v_pc0, .control = CTRL_IMADW_WAIT3); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * 32u, .src3_vreg = v_pc1); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbcol, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * 16u, .src3_vreg = v_pbcol); + BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_col, .src2_vreg = -1, .imm = NT, .src3_vreg = -1); + { + int here = (int)prog->count; + BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_col - here), .predicate_p0 = true, .predicate_not = true); + } + const int L_col_end = (int)prog->count - 1; + BW_APPEND(prog, .op = BW_IR_EXIT); + BW_APPEND(prog, .op = BW_IR_BRA); + + /* loop-carried values live to the end of their loop */ + bw_keep_alive(prog, 0, L_col, L_col_end); + bw_keep_alive(prog, L_col, L_k, L_k_end); + /* K-loop temporaries too: a load's address pair must never be recycled by a + * later instruction of the same iteration (the load may read it late). */ + bw_keep_alive(prog, L_k, L_k_end, L_k_end); + /* the accumulator is read by the stores after the K loop */ + if (prog->regalloc.intervals[v_rd].last_use < L_col_end) prog->regalloc.intervals[v_rd].last_use = L_col_end; + + return omega_bw_regalloc_solve(prog); +} + +int omega_blackwell_codegen_matmul_tensor_loop(const OmegaMatMulSpec *spec, uint32_t grid_x, + int mutant, OmegaBlackwellKernel *kernel) { + if (!spec || !kernel) return -1; + BlackwellIRProgram prog; + if (omega_blackwell_codegen_matmul_tensor_loop_prog(spec, grid_x, mutant, &prog) != 0) return -1; + size_t max_bytes = 4096; + kernel->code = malloc(max_bytes); + if (!kernel->code) return -1; + size_t emitted_size = 0; + if (omega_bw_encode_program(&prog, kernel->code, max_bytes, &emitted_size) != 0) { + free(kernel->code); + kernel->code = NULL; + return -1; + } + kernel->code_size = emitted_size; + kernel->insn_count = emitted_size / 16; + kernel->gpr_count = prog.regalloc.peak_gpr_usage; + kernel->uniform_gpr_count = prog.regalloc.peak_ugpr_usage; + sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); + return 0; +} + +int omega_blackwell_verify_codegen_fixtures(void) { + OmegaRegAlloc ra; + memset(&ra, 0, sizeof(ra)); + ra.vreg_to_phys[0] = 2; + ra.vreg_to_phys[1] = 4; + ra.vreg_to_phys[2] = 6; + ra.vreg_to_phys[3] = 7; + ra.uvreg_to_phys[0] = 4; + + uint32_t w[4]; + + /* 1. MOV R7, RZ */ + BlackwellIRInsn insn_mov_rz = { .op = BW_IR_MOV_RZ, .dst_vreg = 3 }; + if (encode_single_insn(&insn_mov_rz, &ra, w) != 0) return -1; + if (w[0] != (0x7202 | (7 << 16)) || w[1] != 0xff || w[2] != 0x00000f00) return -2; + + /* 2. MOV R2, 1 */ + BlackwellIRInsn insn_mov_imm = { .op = BW_IR_MOV_IMM, .dst_vreg = 0, .imm = 1 }; + if (encode_single_insn(&insn_mov_imm, &ra, w) != 0) return -3; + if (w[0] != (0x7802 | (2 << 16)) || w[1] != 1 || w[2] != 0x00000f00) return -4; + + /* 3. Register-Register IMAD: IMAD R7, R2, R6, R7 */ + BlackwellIRInsn insn_imad_rr = { .op = BW_IR_IMAD, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 2, .src3_vreg = 3 }; + if (encode_single_insn(&insn_imad_rr, &ra, w) != 0) return -5; + if (w[0] != (0x7224 | (7 << 16) | (2 << 24)) || w[1] != 6 || w[2] != (0x078e0200 | 7)) return -6; + + /* 4. Immediate IMAD: IMAD R6, R2, 16, RZ */ + BlackwellIRInsn insn_imad_imm = { .op = BW_IR_IMAD, .dst_vreg = 2, .src1_vreg = 0, .src2_vreg = -1, .imm = 16, .src3_vreg = -1 }; + if (encode_single_insn(&insn_imad_imm, &ra, w) != 0) return -7; + if (w[0] != (0x7824 | (6 << 16) | (2 << 24)) || w[1] != 16 || w[2] != 0x078e02ff) return -8; + + /* 5. IMAD.WIDE: IMAD.WIDE R4, R2, 4, R4 */ + BlackwellIRInsn insn_imad_wide = { .op = BW_IR_IMAD_WIDE, .dst_vreg = 1, .src1_vreg = 0, .imm = 4, .src3_vreg = 1 }; + if (encode_single_insn(&insn_imad_wide, &ra, w) != 0) return -9; + if (w[0] != (0x7825 | (4 << 16) | (2 << 24)) || w[1] != 4 || w[2] != (0x078e0000 | 4)) return -10; + + /* 6. LDC.64 R4, c[0x0][0x380] */ + BlackwellIRInsn insn_ldc64 = { .op = BW_IR_LDC64, .dst_vreg = 1, .imm = 0x380 }; + if (encode_single_insn(&insn_ldc64, &ra, w) != 0) return -11; + if (w[0] != (0xff007b82 | (4 << 16)) || w[1] != ((0x380 << 6) & 0x00ffff00) || w[2] != 0x00000a00) return -12; + + /* 7. LDCU.64 UR4, c[0x0][0x358] */ + BlackwellIRInsn insn_ldcu64 = { .op = BW_IR_LDCU64, .dst_vreg = 0, .imm = 0x358, .is_uniform = true }; + if (encode_single_insn(&insn_ldcu64, &ra, w) != 0) return -13; + if (w[0] != (0xff0077ac | (4 << 16)) || w[1] != ((0x358 << 5) & 0x00ffff00) || w[2] != 0x08000a00) return -14; + + /* 8. LDG.E R2, desc[UR4][R4.64] */ + BlackwellIRInsn insn_ldg = { .op = BW_IR_LDG_E, .dst_vreg = 0, .src1_vreg = 1, .ureg = 0 }; + if (encode_single_insn(&insn_ldg, &ra, w) != 0) return -15; + if (w[0] != (0x7981 | (2 << 16) | (4 << 24)) || w[1] != 4 || w[2] != 0x0c1e1900) return -16; + + /* 9. STG.E desc[UR4][R4.64], R7 */ + BlackwellIRInsn insn_stg = { .op = BW_IR_STG_E, .src1_vreg = 1, .src2_vreg = 3, .ureg = 0 }; + if (encode_single_insn(&insn_stg, &ra, w) != 0) return -17; + if (w[0] != (0x7986 | (4 << 24)) || w[1] != 7 || w[2] != (0x0c101900 | 4)) return -18; + + /* 10. S2R R2, SR_CTAID.X */ + BlackwellIRInsn insn_s2r = { .op = BW_IR_S2R, .dst_vreg = 0, .imm = BW_SR_CTAID_X }; + if (encode_single_insn(&insn_s2r, &ra, w) != 0) return -19; + if (w[0] != (0x7919 | (2 << 16)) || w[2] != (BW_SR_CTAID_X << 8)) return -20; + + /* 11. HMMA.16816.F32 R4, R4, R2, RZ */ + BlackwellIRInsn insn_hmma_f16 = { .op = BW_IR_HMMA_F16, .dst_vreg = 1, .src1_vreg = 1, .src2_vreg = 0, .src3_vreg = -1 }; + if (encode_single_insn(&insn_hmma_f16, &ra, w) != 0) return -21; + if (w[0] != 0x0404723c || w[1] != 0x00000002 || w[2] != 0x000018ff || w[3] != 0x000fe200) return -22; + + /* 12. HMMA.16816.F32.BF16 R4, R4, R2, RZ */ + BlackwellIRInsn insn_hmma_bf16 = { .op = BW_IR_HMMA_BF16, .dst_vreg = 1, .src1_vreg = 1, .src2_vreg = 0, .src3_vreg = -1 }; + uint32_t w_bf16[4]; + if (encode_single_insn(&insn_hmma_bf16, &ra, w_bf16) != 0) return -23; + if (w_bf16[0] != 0x0404723c || w_bf16[1] != 0x00000002 || w_bf16[2] != 0x000418ff || w_bf16[3] != 0x000fe200) return -24; + + /* 13. Differential Invariant: w[2] bit 18 toggles strictly between FP16 and BF16 */ + if ((w[2] ^ w_bf16[2]) != 0x00040000) return -25; + if (w[0] != w_bf16[0] || w[1] != w_bf16[1] || w[3] != w_bf16[3]) return -26; + + /* 14. SHF.R.U32.HI R4, RZ, 2, R2 */ + BlackwellIRInsn insn_shf = { .op = BW_IR_SHF_R, .dst_vreg = 1, .src1_vreg = 0, .imm = 2 }; + if (encode_single_insn(&insn_shf, &ra, w) != 0) return -27; + if (w[0] != 0xff047819 || w[1] != 2 || w[2] != 0x00011602 || w[3] != 0x001fca00) return -28; + + /* 15. LOP3.LUT R4, R2, 3, RZ, 0xc0, !PT */ + BlackwellIRInsn insn_lop3 = { .op = BW_IR_LOP3_AND, .dst_vreg = 1, .src1_vreg = 0, .imm = 3 }; + if (encode_single_insn(&insn_lop3, &ra, w) != 0) return -29; + if (w[0] != 0x02047812 || w[1] != 3 || w[2] != 0x078ec0ff || w[3] != 0x001fca00) return -30; + + /* 16. LDG.E.U16 R2, desc[UR4][R4.64] */ + BlackwellIRInsn insn_ldg16 = { .op = BW_IR_LDG_E_U16, .dst_vreg = 0, .src1_vreg = 1, .ureg = 0 }; + if (encode_single_insn(&insn_ldg16, &ra, w) != 0) return -31; + if (w[0] != 0x04027981 || w[1] != 4 || w[2] != 0x0c1e1500 || w[3] != 0x000f2200) return -32; + + /* A zero-delta branch stays the self-branch the matmul kernels already emit. */ + BlackwellIRInsn insn_bra0 = { .op = BW_IR_BRA }; + if (encode_single_insn(&insn_bra0, &ra, w) != 0) return -33; + if (w[0] != 0x00fc7947 || w[1] != 0xfffffffc || w[2] != 0x0383ffff || w[3] != 0x000fc000) return -34; + + BlackwellIRInsn insn_ge = { .op = BW_IR_ISETP_GE_U32, .src1_vreg = 1, .src2_vreg = 2 }; + if (encode_single_insn(&insn_ge, &ra, w) != 0) return -35; + if (w[0] != (0x720c | (4 << 24)) || w[1] != 6 || w[2] != 0x03f06070) return -36; + + BlackwellIRInsn insn_xor = { .op = BW_IR_LOP3_XOR, .dst_vreg = 1, .src1_vreg = 0, .src2_vreg = 2 }; + if (encode_single_insn(&insn_xor, &ra, w) != 0) return -37; + if (w[0] != (0x7212 | (4 << 16) | (2 << 24)) || w[1] != 6 || w[2] != 0x078e3cff) return -38; + + BlackwellIRInsn insn_ldg_ss = { .op = BW_IR_LDG_STRONG_SYS, .dst_vreg = 0, .src1_vreg = 1, .ureg = 0 }; + if (encode_single_insn(&insn_ldg_ss, &ra, w) != 0) return -39; + if (w[0] != (0x7981 | (2 << 16) | (4 << 24)) || w[1] != 4 || w[2] != 0x0c1f5900) return -40; + /* 17. FADD R7, R2, R4 */ + BlackwellIRInsn insn_fadd = { .op = BW_IR_FADD, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; + if (encode_single_insn(&insn_fadd, &ra, w) != 0) return -133; + if (w[0] != 0x02077221 || w[1] != 4 || w[2] != 0 || w[3] != 0x004fc400) return -134; + + /* 18. FSUB R7, R2, R4 (negated src2) */ + BlackwellIRInsn insn_fsub = { .op = BW_IR_FSUB, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; + if (encode_single_insn(&insn_fsub, &ra, w) != 0) return -135; + if (w[0] != 0x02077221 || w[1] != 0x80000004 || w[2] != 0 || w[3] != 0x004fc400) return -136; + + /* 19. FMUL R7, R2, R4 */ + BlackwellIRInsn insn_fmul = { .op = BW_IR_FMUL, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; + if (encode_single_insn(&insn_fmul, &ra, w) != 0) return -137; + if (w[0] != 0x02077220 || w[1] != 4 || w[2] != 0x00400000 || w[3] != 0x004fc400) return -138; + + /* 20. FFMA R7, R2, R4, R6 */ + BlackwellIRInsn insn_ffma = { .op = BW_IR_FFMA, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1, .src3_vreg = 2 }; + if (encode_single_insn(&insn_ffma, &ra, w) != 0) return -139; + if (w[0] != 0x02077223 || w[1] != 4 || w[2] != 6 || w[3] != 0x004fc400) return -140; + + /* 21. FMNMX_MIN R7, R2, R4 */ + BlackwellIRInsn insn_min = { .op = BW_IR_FMNMX_MIN, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; + if (encode_single_insn(&insn_min, &ra, w) != 0) return -141; + if (w[0] != 0x02077209 || w[1] != 4 || w[2] != 0x03800000 || w[3] != 0x004fc400) return -142; + + /* 22. FMNMX_MAX R7, R2, R4 */ + BlackwellIRInsn insn_max = { .op = BW_IR_FMNMX_MAX, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; + if (encode_single_insn(&insn_max, &ra, w) != 0) return -143; + if (w[0] != 0x02077209 || w[1] != 4 || w[2] != 0x07800000 || w[3] != 0x004fc400) return -144; + + /* 23. I2FP R7, R2 */ + BlackwellIRInsn insn_i2f = { .op = BW_IR_I2FP, .dst_vreg = 3, .src1_vreg = 0 }; + if (encode_single_insn(&insn_i2f, &ra, w) != 0) return -145; + if (w[0] != 0x00077245 || w[1] != 2 || w[2] != 0x00201400 || w[3] != 0x004fe200) return -146; + + /* 24. F2I R7, R2 */ + BlackwellIRInsn insn_f2i = { .op = BW_IR_F2I, .dst_vreg = 3, .src1_vreg = 0 }; + if (encode_single_insn(&insn_f2i, &ra, w) != 0) return -147; + if (w[0] != 0x00077305 || w[1] != 2 || w[2] != 0x0020f100 || w[3] != 0x004e2200) return -148; + + /* 25. MUFU_RCP R7, R2 */ + BlackwellIRInsn insn_mufu_rcp = { .op = BW_IR_MUFU_RCP, .dst_vreg = 3, .src1_vreg = 0 }; + if (encode_single_insn(&insn_mufu_rcp, &ra, w) != 0) return -149; + if (w[0] != 0x00077308 || w[1] != 2 || w[2] != 0x00001000 || w[3] != 0x000e2400) return -150; + + /* 26. MUFU_RSQ R7, R2 */ + BlackwellIRInsn insn_mufu_rsq = { .op = BW_IR_MUFU_RSQ, .dst_vreg = 3, .src1_vreg = 0 }; + if (encode_single_insn(&insn_mufu_rsq, &ra, w) != 0) return -151; + if (w[0] != 0x00077308 || w[1] != 2 || w[2] != 0x00001400 || w[3] != 0x000e2400) return -152; + + /* 27. SHFL_DOWN R7, R2, 16 */ + BlackwellIRInsn insn_shfl = { .op = BW_IR_SHFL_DOWN, .dst_vreg = 3, .src1_vreg = 0, .imm = 16 }; + if (encode_single_insn(&insn_shfl, &ra, w) != 0) return -153; + if (w[0] != 0x02077f89 || w[1] != 0x0a001f00 || w[2] != 0x000e0000 || w[3] != 0x000e2400) return -154; + + /* 28. LDS R7, [R2] */ + BlackwellIRInsn insn_lds = { .op = BW_IR_LDS, .dst_vreg = 3, .src1_vreg = 0, .imm = 0 }; + if (encode_single_insn(&insn_lds, &ra, w) != 0) return -155; + if (w[0] != 0x02077984 || w[1] != 0 || w[2] != 0 || w[3] != 0x000fe200) return -156; + + /* 29. STS [R2], R4 */ + BlackwellIRInsn insn_sts = { .op = BW_IR_STS, .src1_vreg = 0, .src2_vreg = 1, .imm = 0 }; + if (encode_single_insn(&insn_sts, &ra, w) != 0) return -157; + if (w[0] != 0x02007388 || w[1] != 4 || w[2] != 0 || w[3] != 0x000fe200) return -158; + + return 0; +} + +int omega_blackwell_test_regalloc_bounds(void) { + BlackwellIRProgram prog; + omega_bw_ir_init(&prog); + + int v0 = omega_bw_ir_alloc_vreg64(&prog); + int v1 = omega_bw_ir_alloc_vreg(&prog); + int v2 = omega_bw_ir_alloc_vreg64(&prog); + int v3 = omega_bw_ir_alloc_vreg128(&prog); /* Test 128-bit quad allocation */ + + BlackwellIRInsn i0 = { .op = BW_IR_LDC64, .dst_vreg = v0, .imm = 0x380 }; + omega_bw_ir_append(&prog, &i0); + BlackwellIRInsn i1 = { .op = BW_IR_MOV_IMM, .dst_vreg = v1, .imm = 10 }; + omega_bw_ir_append(&prog, &i1); + BlackwellIRInsn i2 = { .op = BW_IR_LDC64, .dst_vreg = v2, .imm = 0x388 }; + omega_bw_ir_append(&prog, &i2); + BlackwellIRInsn i3 = { .op = BW_IR_HMMA_F16, .dst_vreg = v3, .src1_vreg = v3, .src2_vreg = v0, .src3_vreg = -1 }; + omega_bw_ir_append(&prog, &i3); + /* i4 uses v1, v2, v3 simultaneously so all intervals are concurrently active */ + int v_sink = omega_bw_ir_alloc_vreg(&prog); + BlackwellIRInsn i4 = { .op = BW_IR_IADD3, .dst_vreg = v_sink, .src1_vreg = v1, .src2_vreg = v2, .src3_vreg = v3 }; + omega_bw_ir_append(&prog, &i4); + + if (omega_bw_regalloc_solve(&prog) != 0) return -1; + + int phys_v0 = prog.regalloc.vreg_to_phys[v0]; + int phys_v1 = prog.regalloc.vreg_to_phys[v1]; + int phys_v2 = prog.regalloc.vreg_to_phys[v2]; + int phys_v3 = prog.regalloc.vreg_to_phys[v3]; + + if (phys_v0 % 2 != 0) return -2; + if (phys_v2 % 2 != 0) return -3; + if (phys_v3 % 4 != 0) return -4; /* 4-alignment for 128-bit quad */ + if (phys_v1 == phys_v0 || phys_v1 == phys_v0 + 1) return -5; + if (phys_v2 == phys_v0 || phys_v2 == phys_v0 + 1) return -6; + for (int k = 0; k < 4; k++) { + if (phys_v3 + k == phys_v0 || phys_v3 + k == phys_v0 + 1) return -7; + if (phys_v3 + k == phys_v2 || phys_v3 + k == phys_v2 + 1) return -8; + if (phys_v3 + k == phys_v1) return -9; + } + + /* Test bounds refusal: intentionally attempt to allocate more registers than hardware capacity */ + BlackwellIRProgram overflow_prog; + omega_bw_ir_init(&overflow_prog); + for (int i = 0; i < BW_PHYS_GPR_MAX + 5; i++) { + int v = omega_bw_ir_alloc_vreg(&overflow_prog); + if (v < 0) break; + BlackwellIRInsn insn = { .op = BW_IR_MOV_IMM, .dst_vreg = v, .imm = (uint32_t)i }; + omega_bw_ir_append(&overflow_prog, &insn); + } + for (size_t i = 0; i < overflow_prog.count; i++) { + for (int v = 0; v < overflow_prog.regalloc.num_vregs; v++) { + overflow_prog.regalloc.intervals[v].last_use = (int)overflow_prog.count - 1; + } + } + if (omega_bw_regalloc_solve(&overflow_prog) == 0) { + return -6; + } + + return 0; +} + +int omega_blackwell_test_codegen_variation(void) { + OmegaMatMulSpec spec1, spec2; + if (omega_matmul_spec_init(&spec1, 16, 16, 16, OMEGA_MATMUL_PRECISION_INT32) != 0) { + printf("FAIL: spec1 init failed\n"); + return -1; + } + if (omega_matmul_spec_init(&spec2, 32, 16, 64, OMEGA_MATMUL_PRECISION_INT32) != 0) { + printf("FAIL: spec2 init failed\n"); + return -1; + } + + OmegaBlackwellKernel k1, k2; + memset(&k1, 0, sizeof(k1)); + memset(&k2, 0, sizeof(k2)); + + if (omega_blackwell_codegen_matmul(&spec1, &k1) != 0) { + printf("FAIL: k1 codegen failed\n"); + return -1; + } + if (omega_blackwell_codegen_matmul(&spec2, &k2) != 0) { + printf("FAIL: k2 codegen failed\n"); + omega_blackwell_kernel_free(&k1); + return -1; + } + + OmegaBlackwellRealizationIdentity id1, id2; + omega_blackwell_bind_matmul_realization(&spec1, &k1, &id1); + omega_blackwell_bind_matmul_realization(&spec2, &k2, &id2); + + printf("================================================================================\n"); + printf(" OMEGA BLACKWELL CODEGEN VARIATION DEMONSTRATION (GATE 7 STAGE-1)\n"); + printf("================================================================================\n"); + printf("Config 1: 16x16x16 INT32\n"); + printf(" Instructions: %zu (%zu bytes, %u GPRs, %u UGPRs)\n", k1.insn_count, k1.code_size, k1.gpr_count, k1.uniform_gpr_count); + printf(" Spec ID: "); + for (int i = 0; i < 16; i++) printf("%02x", spec1.spec_id[i]); + printf("...\n"); + printf(" Code Digest: "); + for (int i = 0; i < 16; i++) printf("%02x", k1.code_digest[i]); + printf("...\n"); + printf(" Realization: "); + for (int i = 0; i < 16; i++) printf("%02x", id1.realization_id[i]); + printf("...\n\n"); + + printf("Config 2: 32x16x64 INT32\n"); + printf(" Instructions: %zu (%zu bytes, %u GPRs, %u UGPRs)\n", k2.insn_count, k2.code_size, k2.gpr_count, k2.uniform_gpr_count); + printf(" Spec ID: "); + for (int i = 0; i < 16; i++) printf("%02x", spec2.spec_id[i]); + printf("...\n"); + printf(" Code Digest: "); + for (int i = 0; i < 16; i++) printf("%02x", k2.code_digest[i]); + printf("...\n"); + printf(" Realization: "); + for (int i = 0; i < 16; i++) printf("%02x", id2.realization_id[i]); + printf("...\n"); + printf("================================================================================\n"); + + /* Validation checks */ + if (memcmp(spec1.spec_id, spec2.spec_id, 32) == 0) { + printf("FAIL: spec IDs match (expected distinct)\n"); + return -2; + } + if (memcmp(k1.code_digest, k2.code_digest, 32) == 0) { + printf("FAIL: code digests match (expected distinct)\n"); + return -3; + } + if (memcmp(id1.realization_id, id2.realization_id, 32) == 0) { + printf("FAIL: realization IDs match (expected distinct)\n"); + return -4; + } + + printf("PASS: Dynamic codegen variation verified (distinct specifications, code digests, and realization IDs).\n"); + + omega_blackwell_kernel_free(&k1); + omega_blackwell_kernel_free(&k2); + return 0; +} diff --git a/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/omega_bw_reconv.h b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/omega_bw_reconv.h new file mode 100644 index 000000000..d432e5763 --- /dev/null +++ b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/omega_bw_reconv.h @@ -0,0 +1,83 @@ +#ifndef OMEGA_BW_RECONV_H +#define OMEGA_BW_RECONV_H +/* Structured warp reconvergence regions for the Blackwell IR emitters (omega #308). + * + * A region is the span between a BSSY (arm barrier Bn, name the join point) and the + * matching BSYNC (wait for every armed lane, continue as one warp). Inside a region the + * lanes of a warp may take different paths: a predicated forward branch, a loop whose + * trip count differs per lane, a break. Every path that leaves the region jumps to the + * BSYNC itself (that is where nvcc 13.0.88 sends loop exits on sm_121); the BSSY names the + * instruction after the BSYNC. Regions nest; the innermost open region owns Bn with + * n = depth, so nesting depth is bounded by the barrier register count and begin fails + * closed (returns -1) when that bound is reached. + * + * This header holds the bookkeeping only (indices, patching, bounds). Each emitter wraps + * it with its own control-word scheme: see region_begin / region_exit / region_join in + * src/omega_gpu_elementwise_api.c and src/omega_gpu_attention_api.c. + * + * Reference: Mesa NAK nak_nir_lower_cf.c (push_scope / pop_scope: bar_set_nv before a + * divergent if or loop, bar_sync_nv at its merge block, breaks jump to the sync) and the + * nvcc oracle listing in docs/gpu-reconvergence-308.md. */ + +#include "omega_blackwell_codegen.h" + +#define BW_RECONV_MAX_DEPTH BW_RECONV_MAX_BAR +#define BW_RECONV_MAX_EXITS 8 /* forward exits (breaks) recorded per region */ + +typedef struct { + int depth; /* open regions */ + int peak; /* deepest nesting seen (reported in receipts) */ + int opened; /* regions begun (for receipts) */ + int bssy_at[BW_RECONV_MAX_DEPTH]; + int exits[BW_RECONV_MAX_DEPTH][BW_RECONV_MAX_EXITS]; + int n_exits[BW_RECONV_MAX_DEPTH]; +} BwRegions; + +/* Called after the emitter appended the BSSY at index `bssy_index`. Returns the barrier + * register to use, or -1 when the nesting bound is reached (the caller must fail the + * build: no code with a missing barrier may be emitted). The caller allocates the + * barrier BEFORE emitting: use bw_regions_next_bar to pick it, then bw_regions_begin. */ +static inline int bw_regions_next_bar(const BwRegions *r) { + return r->depth < BW_RECONV_MAX_DEPTH ? r->depth : -1; +} +static inline int bw_regions_begin(BwRegions *r, int bssy_index) { + if (r->depth >= BW_RECONV_MAX_DEPTH || bssy_index < 0) return -1; + r->bssy_at[r->depth] = bssy_index; + r->n_exits[r->depth] = 0; + r->depth++; + r->opened++; + if (r->depth > r->peak) r->peak = r->depth; + return r->depth - 1; +} + +/* Records a forward branch (already appended at `bra_index`) that leaves the innermost + * open region; its target is patched to the BSYNC by bw_regions_join. -1 when no region + * is open or the per-region exit table is full. */ +static inline int bw_regions_exit(BwRegions *r, int bra_index) { + if (r->depth <= 0 || bra_index < 0) return -1; + int d = r->depth - 1; + if (r->n_exits[d] >= BW_RECONV_MAX_EXITS) return -1; + r->exits[d][r->n_exits[d]++] = bra_index; + return 0; +} + +/* Called after the emitter appended the BSYNC at index `bsync_index` for the innermost + * open region. Patches the BSSY to name the instruction after the BSYNC and every + * recorded exit to jump to the BSYNC. Returns the barrier register closed, or -1 when + * no region is open or an index is inconsistent (BSSY must precede the BSYNC). */ +static inline int bw_regions_join(BwRegions *r, BlackwellIRProgram *p, int bsync_index) { + if (r->depth <= 0 || !p || bsync_index < 0 || (size_t)bsync_index >= p->count) return -1; + int d = --r->depth; + int at = r->bssy_at[d]; + if (at < 0 || at >= bsync_index || p->insns[at].op != BW_IR_BSSY || p->insns[bsync_index].op != BW_IR_BSYNC) return -1; + if (p->insns[at].bar_reg != (uint8_t)d || p->insns[bsync_index].bar_reg != (uint8_t)d) return -1; + p->insns[at].imm = (uint32_t)(bsync_index + 1 - at); + for (int i = 0; i < r->n_exits[d]; i++) { + int b = r->exits[d][i]; + if (b <= at || b >= bsync_index || p->insns[b].op != BW_IR_BRA) return -1; + p->insns[b].imm = (uint32_t)(bsync_index - b); + } + return d; +} + +#endif /* OMEGA_BW_RECONV_H */ diff --git a/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/omega_gpu_elementwise_api.c b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/omega_gpu_elementwise_api.c new file mode 100644 index 000000000..b16c70205 --- /dev/null +++ b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/omega_gpu_elementwise_api.c @@ -0,0 +1,1139 @@ +/* omega_gpu_elementwise_api.c: FB-1 cut 4 (2026-10-04). See the header. + * + * Layout of this file: (1) kernel codegen on top of the BlackwellIR, with a + * small scheduler that writes every control word; (2) one launcher cloned from + * the chip-proven tensor matmul path (omega_blackwell_execute_matmul_tensor); + * (3) the public entry points, which split work into <= OMEGA_GPU_EW_MAX_CTAS + * launches and run the host-side bookkeeping. + * + * Scheduling rules (chip-learned, src/omega_numeric.c omega_numeric_check_patch): + * - a variable-latency result (LDG, LDS, LDC, LDCU, S2R, SHFL, MUFU) arrives through + * a scoreboard; the producer stalls >= 2 and the consumer waits on that SB; + * - a fixed-latency result (FADD FMUL FFMA IMAD IADD3 MOV LOP3 SHF) has no + * scoreboard: the producer stalls >= 5 before its consumer (we use 6); + * - a predicate producer (ISETP) stalls 14 before @P0 use (NAK SM120 padding); + * - stores (STG, STS) set a read barrier so the next instruction cannot overwrite + * the source register early. + * The emitter below serialises: every instruction waits on everything still + * pending. A final local pass tightens supported adjacent ALU pairs to 5 cycles. + */ +#include "omega_gpu_elementwise_api.h" +#include "omega_blackwell_codegen.h" +#include "omega_bw_reconv.h" +#include "omega_blackwell_qmd.h" +#include "omega_blackwell_submit.h" +#include "omega_gpu_session.h" +#include "sha256.h" +#include +#include +#include +#include + +/* ------------------------------------------------------------------ codegen */ + +#define CW(stall, wbar, rbar, wait) \ + (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) +#define CW_YIELD 0x2000u +#define SB_NONE 7u +#define SB_GPR 0u /* variable-latency GPR results */ +#define SB_RD 1u /* store source-register read barrier */ +#define SB_UGPR 2u /* uniform-register results (LDCU) */ + +enum { K_FIXED, K_PRED, K_VAR, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC }; + +#define EW_MAX_LOOPS 4 +typedef struct { + BlackwellIRProgram *p; + uint32_t pending; /* scoreboards set and not yet waited on */ + int err; + int loop_start[EW_MAX_LOOPS], loop_end[EW_MAX_LOOPS]; + int n_loops; + int open_start[EW_MAX_LOOPS]; /* starts of loops begun and not yet ended (nesting stack) */ + int depth; + BwRegions rg; /* structured reconvergence regions (omega #308) */ + int rg_mutant; /* test only: 1 = drop the BSYNC at join, 2 = corrupt the BSSY target (host sim must catch both) */ + int exited; /* a predicated EXIT was emitted: some lanes of a warp may be gone */ + int exit_sync_ok; /* test only: lets a kernel put SHFL/BAR after a predicated EXIT (the negative control R6) */ + int test_path; /* building for omega_gpu_elementwise_codegen_ir (host tests), never for a launch */ +} Em; + +static void emit(Em *e, BlackwellIRInsn in, int kind) { + uint32_t wait = e->pending; + switch (kind) { + case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; + case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break; + case K_VAR: in.control = CW(4, SB_GPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_GPR; break; + case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; + case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; + case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; + /* a predicated EXIT or BRA that falls through must not be the only waiter */ + case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; + case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; + /* BSSY / BSYNC read no registers; the stall counts (1 and 5, both with yield) are the ones + * nvcc 13.0.88 writes for sm_121 (docs/gpu-reconvergence-308.md). They wait on everything + * pending so that, as for every branch here, nothing is in flight across a control edge + * and the straight-line scoreboard walk in the tests stays valid. */ + case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; + case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; + default: e->err = -1; return; + } + if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; /* append returns the index */ +} + +static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; } +static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; } +static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; } +static int here(const Em *e) { return (int)e->p->count; } + +#define NOREG -1 +static BlackwellIRInsn I0(BlackwellIROpcode op) { + BlackwellIRInsn in; memset(&in, 0, sizeof in); + in.op = op; in.dst_vreg = NOREG; in.src1_vreg = NOREG; in.src2_vreg = NOREG; in.src3_vreg = NOREG; in.ureg = NOREG; + return in; +} +static void ldcu64(Em *e, int udst, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDCU64); i.dst_vreg = udst; i.imm = off; i.is_uniform = true; emit(e, i, K_UVAR); } +static void ldc64(Em *e, int dst64, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC64); i.dst_vreg = dst64; i.imm = off; emit(e, i, K_VAR); } +static void ldc32(Em *e, int dst, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC); i.dst_vreg = dst; i.imm = off; emit(e, i, K_VAR); } +static void s2r(Em *e, int dst, uint32_t sr) { BlackwellIRInsn i = I0(BW_IR_S2R); i.dst_vreg = dst; i.imm = sr; emit(e, i, K_VAR); } +static void movi(Em *e, int dst, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_MOV_IMM); i.dst_vreg = dst; i.imm = imm; emit(e, i, K_FIXED); } +static void movf(Em *e, int dst, float f) { uint32_t u; memcpy(&u, &f, 4); movi(e, dst, u); } +static void movrz(Em *e, int dst) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = dst; emit(e, i, K_FIXED); } +/* dst = a * imm + c (c may be NOREG for RZ) */ +static void imadi(Em *e, int dst, int a, uint32_t imm, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = dst; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c; emit(e, i, K_FIXED); } +/* dst = a * b + c (registers) */ +static void imadr(Em *e, int dst, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = dst; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } +static void iadd3(Em *e, int dst, int a, int b) { BlackwellIRInsn i = I0(BW_IR_IADD3); i.dst_vreg = dst; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void lop3xor(Em *e, int dst, int a, int b) { BlackwellIRInsn i = I0(BW_IR_LOP3_XOR); i.dst_vreg = dst; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +/* dst64 = idx * 4 + ptr64 (byte address of element idx) */ +static void addr4(Em *e, int dst64, int idx, int ptr64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = dst64; i.src1_vreg = idx; i.imm = 4; i.src3_vreg = ptr64; emit(e, i, K_FIXED); } +static void ldg(Em *e, int dst, int addr64, int udesc) { BlackwellIRInsn i = I0(BW_IR_LDG_E); i.dst_vreg = dst; i.src1_vreg = addr64; i.ureg = udesc; emit(e, i, K_VAR); } +/* Independent loads (LDG_E from global, or LDS32 from shared memory with ureg NOREG) issued + * back to back (campaign C2a, 2026-10-05). The first load + * waits on whatever is pending; the others wait on nothing, so all n are in flight together. + * Every load sets SB_GPR (a counting scoreboard), so the next instruction waits once for all + * of them instead of once per load. Fails the build if a load address is a destination of the + * group or a destination repeats (the later loads would read an operand still in flight). */ +static void ld_group(Em *e, BlackwellIROpcode op, int n, const int *dst, const int *addr, int ureg) { + for (int i = 0; i < n; i++) + for (int k = 0; k < n; k++) + if (addr[i] == dst[k] || (k != i && dst[i] == dst[k])) { e->err = -1; return; } + for (int i = 0; i < n; i++) { + if (op != BW_IR_LDG_E && op != BW_IR_LDS32) { e->err = -1; return; } + BlackwellIRInsn in = I0(op); in.dst_vreg = dst[i]; in.src1_vreg = addr[i]; in.ureg = ureg; + if (i == 0) { emit(e, in, K_VAR); continue; } + in.control = CW(4, SB_GPR, SB_NONE, 0) | CW_YIELD; /* nonzero, so the encoder keeps it */ + if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; + } +} +static void stg(Em *e, int addr64, int val, int udesc) { BlackwellIRInsn i = I0(BW_IR_STG_E); i.src1_vreg = addr64; i.src2_vreg = val; i.ureg = udesc; emit(e, i, K_STORE); } +static void fadd(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FADD); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void fsub(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FSUB); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void fmul(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FMUL); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void ffma(Em *e, int d, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_FFMA); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } +static void mufu(Em *e, BlackwellIROpcode op, int d, int a) { BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = a; emit(e, i, K_VAR); } +/* Cross-lane and warp-ending ops are refused inside an open reconvergence region (omega #308): + * a SHFL or BAR.SYNC reached by a split warp reads inactive lanes / counts a partial warp + * (undefined; the 2026-10-04 attention bug), and a lane that EXITs inside a region can no + * longer be joined. The build fails closed instead of emitting such code. */ +/* Cross-lane instructions (SHFL, BAR.SYNC) need the whole warp present. The emitter refuses + * them (fails the build) inside an open reconvergence region, where lanes may be on different + * paths, and after a predicated EXIT, where lanes may be gone for good: a SHFL then reads + * inactive lanes (undefined on the chip, the attention failure of 2026-10-04). A kernel that + * must drop lanes before a cross-lane step clamps them instead and exits them afterwards. */ +static int warp_split(Em *e) { if (e->rg.depth > 0 || (e->exited && !e->exit_sync_ok)) { e->err = -1; return 1; } return 0; } +static void shfl_down(Em *e, int d, int a, uint32_t off) { if (warp_split(e)) return; BlackwellIRInsn i = I0(BW_IR_SHFL_DOWN); i.dst_vreg = d; i.src1_vreg = a; i.imm = off; emit(e, i, K_VAR); } +static void sts32(Em *e, int addr, int val) { BlackwellIRInsn i = I0(BW_IR_STS32); i.src1_vreg = addr; i.src2_vreg = val; emit(e, i, K_STORE); } +static void lds32(Em *e, int d, int addr) { BlackwellIRInsn i = I0(BW_IR_LDS32); i.dst_vreg = d; i.src1_vreg = addr; emit(e, i, K_VAR); } +static void bar_sync(Em *e) { if (warp_split(e)) return; emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } +static void isetp_ge_u32(Em *e, int a, int b) { BlackwellIRInsn i = I0(BW_IR_ISETP_GE_U32); i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_PRED); } +/* An EXIT inside a region would leave the barrier waiting for a lane that never comes: refused. */ +static void exit_if_p0(Em *e) { if (e->rg.depth > 0) { e->err = -1; return; } e->exited = 1; BlackwellIRInsn i = I0(BW_IR_EXIT); i.predicate_p0 = true; emit(e, i, K_EXIT); } +static void bra_back_if_not_p0(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.predicate_not = true; i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } +static void tail(Em *e) { + emit(e, I0(BW_IR_EXIT), K_EXIT); + BlackwellIRInsn self = I0(BW_IR_BRA); /* the self-branch every kernel here ends with */ + self.control = 0x000fc000u; + if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1; +} +/* Loops may nest (prime race cut): begin pushes the start, end pops it, so the inner + * loop is closed (and its liveness fixed) first. For the single-level loops above this + * is exactly the old behaviour. */ +static void loop_begin(Em *e) { if (e->depth >= EW_MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); } +/* Closing a loop: every value defined before the loop and read inside it stays + * live until the back-branch, otherwise the linear-scan allocator (which knows + * nothing about back-edges) would hand its register to a body temporary whose + * first definition comes after the value's last textual use (e.g. the scale y + * in rmsnorm pass 2), and the next iteration would read garbage. */ +static void loop_end(Em *e) { + if (e->n_loops >= EW_MAX_LOOPS || e->depth <= 0) { e->err = -1; return; } + int start = e->open_start[--e->depth], end = here(e) - 1; + e->loop_start[e->n_loops] = start; + e->loop_end[e->n_loops] = end; + e->n_loops++; + OmegaRegAlloc *ra = &e->p->regalloc; + for (int v = 0; v < ra->num_vregs; v++) { + OmegaLiveInterval *iv = &ra->intervals[v]; + if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end; + } +} + +/* Kernel argument words (constant bank 0, same slots as the matmul path): + * 0x380 a, 0x388 b, 0x390 c (64-bit each), then four 32-bit parameters. */ +#define ARG_A 0x380u +#define ARG_B 0x388u +#define ARG_C 0x390u +#define ARG_P0 0x398u +#define ARG_P1 0x39cu +#define ARG_P2 0x3a0u +#define ARG_P3 0x3a4u +#define ARG_P4 0x3a8u +#define DESC_OFF 0x358u + +typedef struct { int udesc, pa, pb, pc, tid, cta; } Pro; +static Pro prologue(Em *e) { + Pro r; + r.udesc = UV64(e); r.pa = V64(e); r.pb = V64(e); r.pc = V64(e); r.tid = V(e); r.cta = V(e); + ldcu64(e, r.udesc, DESC_OFF); + ldc64(e, r.pa, ARG_A); ldc64(e, r.pb, ARG_B); ldc64(e, r.pc, ARG_C); + s2r(e, r.tid, BW_SR_TID_X); s2r(e, r.cta, BW_SR_CTAID_X); + return r; +} + +/* out[i] = 2^a[i] for i < n (n = P0). Threads 128, grid ceil(n/128). Mutant: 2^(a+1). */ +static void gen_ex2(Em *e, int mutant) { + Pro p = prologue(e); + int n = V(e), i = V(e), off = V64(e), x = V(e), y = V(e); + ldc32(e, n, ARG_P0); + imadi(e, i, p.cta, OMEGA_GPU_EW_THREADS, p.tid); + isetp_ge_u32(e, i, n); + exit_if_p0(e); + addr4(e, off, i, p.pa); + ldg(e, x, off, p.udesc); + if (mutant) { int one = V(e); movf(e, one, 1.0f); fadd(e, x, x, one); } + mufu(e, BW_IR_MUFU_EX2, y, x); + addr4(e, off, i, p.pc); + stg(e, off, y, p.udesc); + tail(e); +} + +/* out[i] = a[i ^ 127] within each CTA of 128 (n % 128 == 0). STS own word, BAR.SYNC, + * LDS the partner word. Mutant: LDS own word (no exchange). */ +static void gen_xchg(Em *e, int mutant) { + Pro p = prologue(e); + int i = V(e), off = V64(e), x = V(e), saddr = V(e), paddr = V(e), mask = V(e), y = V(e); + imadi(e, i, p.cta, OMEGA_GPU_EW_THREADS, p.tid); + addr4(e, off, i, p.pa); + ldg(e, x, off, p.udesc); + imadi(e, saddr, p.tid, 4, NOREG); + sts32(e, saddr, x); + bar_sync(e); + if (mutant) { + lds32(e, y, saddr); + } else { + movi(e, mask, OMEGA_GPU_EW_THREADS - 1); + lop3xor(e, paddr, p.tid, mask); + imadi(e, paddr, paddr, 4, NOREG); + lds32(e, y, paddr); + } + addr4(e, off, i, p.pc); + stg(e, off, y, p.udesc); + tail(e); +} + +/* One CTA of 128 threads per row. P0 = dim/128 (loop count), P1 = bits(1/dim), + * P2 = bits(eps), P3 = dim. Mutant: the warp tree drops the last SHFL step. */ +static void gen_rmsnorm(Em *e, int mutant) { + Pro p = prologue(e); + int nk = V(e), invd = V(e), eps = V(e), dim = V(e); + int rowbase = V(e), idx = V(e), col = V(e), k = V(e), acc = V(e), cT = V(e), c1 = V(e); + int off = V64(e), x = V(e), t = V(e); + ldc32(e, nk, ARG_P0); ldc32(e, invd, ARG_P1); ldc32(e, eps, ARG_P2); ldc32(e, dim, ARG_P3); + imadr(e, rowbase, p.cta, dim, NOREG); + iadd3(e, idx, rowbase, p.tid); + movrz(e, acc); movrz(e, k); + movi(e, cT, OMEGA_GPU_EW_THREADS); movi(e, c1, 1); + loop_begin(e); + int l1 = here(e); + addr4(e, off, idx, p.pa); + ldg(e, x, off, p.udesc); + ffma(e, acc, x, x, acc); + iadd3(e, idx, idx, cT); + iadd3(e, k, k, c1); + isetp_ge_u32(e, k, nk); + bra_back_if_not_p0(e, l1); + loop_end(e); + static const uint32_t steps[5] = { 16, 8, 4, 2, 1 }; + for (int s = 0; s < (mutant ? 4 : 5); s++) { shfl_down(e, t, acc, steps[s]); fadd(e, acc, acc, t); } + int saddr = V(e); + imadi(e, saddr, p.tid, 4, NOREG); + sts32(e, saddr, acc); + bar_sync(e); + int a0 = V(e), a1 = V(e), a2 = V(e), a3 = V(e), s0 = V(e), s1 = V(e), s2 = V(e), s3 = V(e), sum = V(e); + movrz(e, a0); movi(e, a1, 128); movi(e, a2, 256); movi(e, a3, 384); + lds32(e, s0, a0); lds32(e, s1, a1); lds32(e, s2, a2); lds32(e, s3, a3); + fadd(e, sum, s0, s1); fadd(e, sum, sum, s2); fadd(e, sum, sum, s3); + int v = V(e), y = V(e), u = V(e), cmh = V(e), c15 = V(e); + fmul(e, v, sum, invd); + fadd(e, v, v, eps); + mufu(e, BW_IR_MUFU_RSQ, y, v); + movf(e, cmh, -0.5f); movf(e, c15, 1.5f); + fmul(e, u, v, y); fmul(e, u, u, y); ffma(e, u, u, cmh, c15); fmul(e, y, y, u); + int offw = V64(e), offc = V64(e), w = V(e), o = V(e); + iadd3(e, idx, rowbase, p.tid); + imadi(e, col, p.tid, 1, NOREG); /* col = tid (the encoder maps a missing src2 to R0, not RZ, so no IADD3 here) */ + movrz(e, k); + loop_begin(e); + int l2 = here(e); + addr4(e, off, idx, p.pa); + ldg(e, x, off, p.udesc); + addr4(e, offw, col, p.pb); + ldg(e, w, offw, p.udesc); + fmul(e, o, x, y); + fmul(e, o, o, w); + addr4(e, offc, idx, p.pc); + stg(e, offc, o, p.udesc); + iadd3(e, idx, idx, cT); + iadd3(e, col, col, cT); + iadd3(e, k, k, c1); + isetp_ge_u32(e, k, nk); + bra_back_if_not_p0(e, l2); + loop_end(e); + tail(e); +} + +/* One CTA of head_dim/2 threads per head. P0 = head_dim, P1 = head_dim/2. + * a = vector, b = table (cos in the first half of each head, sin in the second). + * Mutant: out0 = q0*cos + q1*sin. */ +static void gen_rope(Em *e, int mutant) { + Pro p = prologue(e); + int hd = V(e), half = V(e), i0 = V(e), i1 = V(e), off0 = V64(e), off1 = V64(e); + int q0 = V(e), q1 = V(e), c = V(e), s = V(e), t0 = V(e), t1 = V(e), o0 = V(e), o1 = V(e); + ldc32(e, hd, ARG_P0); ldc32(e, half, ARG_P1); + imadr(e, i0, p.cta, hd, p.tid); + iadd3(e, i1, i0, half); + addr4(e, off0, i0, p.pa); ldg(e, q0, off0, p.udesc); + addr4(e, off1, i1, p.pa); ldg(e, q1, off1, p.udesc); + addr4(e, off0, i0, p.pb); ldg(e, c, off0, p.udesc); + addr4(e, off1, i1, p.pb); ldg(e, s, off1, p.udesc); + fmul(e, t0, q0, c); fmul(e, t1, q1, s); + if (mutant) fadd(e, o0, t0, t1); else fsub(e, o0, t0, t1); + fmul(e, t0, q0, s); fmul(e, t1, q1, c); + fadd(e, o1, t0, t1); + addr4(e, off0, i0, p.pc); stg(e, off0, o0, p.udesc); + addr4(e, off1, i1, p.pc); stg(e, off1, o1, p.udesc); + tail(e); +} + +/* out[i] = g/(1+2^(g*-log2e)) * up for i < n (P0). Mutant: 2^(g*+log2e). */ +static void gen_swiglu(Em *e, int mutant) { + Pro p = prologue(e); + int n = V(e), i = V(e), off = V64(e), g = V(e), u = V(e), k = V(e), t = V(e), ex = V(e), d = V(e), r = V(e), one = V(e), o = V(e); + ldc32(e, n, ARG_P0); + imadi(e, i, p.cta, OMEGA_GPU_EW_THREADS, p.tid); + isetp_ge_u32(e, i, n); + exit_if_p0(e); + addr4(e, off, i, p.pa); ldg(e, g, off, p.udesc); + addr4(e, off, i, p.pb); ldg(e, u, off, p.udesc); + movf(e, k, mutant ? 1.4426950408889634f : -1.4426950408889634f); + movf(e, one, 1.0f); + fmul(e, t, g, k); + mufu(e, BW_IR_MUFU_EX2, ex, t); + fadd(e, d, ex, one); + mufu(e, BW_IR_MUFU_RCP, r, d); + fmul(e, o, g, r); + fmul(e, o, o, u); + addr4(e, off, i, p.pc); + stg(e, off, o, p.udesc); + tail(e); +} + +/* ------------------------------------------- prime race sieve (2026-10-05) */ +static void lop3lut(Em *e, int d, int a, int b, int c, uint32_t lut) { BlackwellIRInsn i = I0(BW_IR_LOP3_LUT); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; i.imm = lut & 0xffu; emit(e, i, K_FIXED); } +static void shl_r(Em *e, int d, int a, int n) { BlackwellIRInsn i = I0(BW_IR_SHF_L_U32); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = n; emit(e, i, K_FIXED); } +static void shr_i(Em *e, int d, int a, uint32_t n) { BlackwellIRInsn i = I0(BW_IR_SHF_R); i.dst_vreg = d; i.src1_vreg = a; i.imm = n; emit(e, i, K_FIXED); } +static void imadhi(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_IMAD_HI_U32); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +/* dst64 = a * imm + c64 (IMAD.WIDE.U32 immediate form) */ +static void widei(Em *e, int dst64, int a, uint32_t imm, int c64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = dst64; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c64; emit(e, i, K_FIXED); } +/* @P0 BRA forward; returns the instruction index for bra_patch_here */ +static int bra_fwd_if_p0(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = 1; emit(e, i, K_BRA); return at; } +static void bra_patch_here(Em *e, int at) { if (e->err == 0 && at >= 0 && at < here(e)) e->p->insns[at].imm = (uint32_t)(here(e) - at); else e->err = -1; } +static void bra_always(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } + +/* ---- structured reconvergence regions (omega #308; bookkeeping in omega_bw_reconv.h) ---- + * region_begin: BSSY Bd (d = nesting depth), fails closed when no barrier register is left. + * region_exit: a forward branch out of the innermost region, patched to its BSYNC at join + * (RX_ALWAYS, RX_IF_P0, RX_IF_NOT_P0). + * region_join: BSYNC Bd; patches the BSSY to the instruction after it and every exit to it. + * Usage: divergent loop = region_begin; L: isetp; region_exit(RX_IF_P0); body; bra_always(L); region_join. + * if/else diamond = region_begin; isetp; t = bra_fwd_if_p0; then; region_exit(RX_ALWAYS); + * bra_patch_here(t); else; region_join. */ +enum { RX_ALWAYS, RX_IF_P0, RX_IF_NOT_P0 }; +static int region_begin(Em *e) { + int bar = bw_regions_next_bar(&e->rg); + if (bar < 0) { e->err = -1; return -1; } + int at = here(e); + BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = (uint8_t)bar; i.imm = 2; emit(e, i, K_BSSY); + if (bw_regions_begin(&e->rg, at) < 0) e->err = -1; + return bar; +} +static void region_exit(Em *e, int cond) { + int at = here(e); + BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = 1; + if (cond != RX_ALWAYS) { i.predicate_p0 = true; i.predicate_not = (cond == RX_IF_NOT_P0); } + emit(e, i, K_BRA); + if (bw_regions_exit(&e->rg, at) < 0) e->err = -1; +} +static void region_join(Em *e) { + if (e->rg.depth <= 0) { e->err = -1; return; } + int d = e->rg.depth - 1, bsync = here(e); + BlackwellIRInsn i = I0(e->rg_mutant == 1 ? BW_IR_NOP : BW_IR_BSYNC); i.bar_reg = (uint8_t)d; emit(e, i, K_BSYNC); + if (e->rg_mutant == 1) { + /* negative control (host sim only): the join is a NOP; patch as the real join would */ + BwRegions *r = &e->rg; r->depth--; + e->p->insns[r->bssy_at[d]].imm = (uint32_t)(bsync + 1 - r->bssy_at[d]); + for (int k = 0; k < r->n_exits[d]; k++) e->p->insns[r->exits[d][k]].imm = (uint32_t)(bsync - r->exits[d][k]); + return; + } + if (bw_regions_join(&e->rg, e->p, bsync) != d) e->err = -1; + if (e->rg_mutant == 2) e->p->insns[e->rg.bssy_at[d]].imm += 1; /* negative control: BSSY names the wrong join */ +} + +/* Word-parallel odd-only sieve (docs: handoff 2026-10-05 DESIGN "GPU kernel (native)"). + * Thread t owns 32-bit output words w = t, t + S, t + 2S, ... (S = P1 = threads launched; + * the I42 envelope caps a launch at OMEGA_GPU_EW_MAX_CTAS CTAs, so a grid-stride loop + * replaces one thread per word). Every word has exactly one writer; no atomics. + * Bit j of word w is the odd number 2(32w + j) + 1. + * Arguments: a (0x380) = prime table, entries {p, magic = ceil(2^32 / p), k0 = (p*p - 1)/2} + * (12 bytes, ascending p, odd primes <= isqrt(limit)) followed by ONE sentinel entry with + * k0 = 0xffffffff that ends every scan; c (0x390) = output words; + * P0 = nwords, P1 = S, P2 = last word index, P3 = ~tailmask (bits past the last odd). + * Range: limit < 2^32 (so base, k0 < 2^31 and the sign-bit tricks below hold). + * Per word: base = 32w; for each prime while k0 < base + 32: + * d = base - k0; neg = d >> 31 (k0 > base); dpos = neg ? 0 : d; jadd = neg ? k0 - base : 0 + * q = hi32(dpos * magic) (= floor(dpos/p) or one more: magic is rounded up) + * r = dpos - q*p (q one too big -> r = (dpos mod p) - p, wrapped) + * j = p - r; j -= p * ((r - 1) >> 31) (r == 0 -> 0; wrapped r -> p - (dpos mod p)) + * One fix covers both cases: with q one too big, dpos mod p >= p - dpos*e/2^32 > p/2 > 0 + * (e = magic*p - 2^32 < p, dpos < 2^31), so the true remainder is never 0 there and + * p - r - p = p - (dpos mod p) is the right offset. A separate "r += p if wrapped" step was + * removed after a negative control showed it changes no output (host sim, limit 1e7, where + * the overshoot occurs 268 times; bench/prime_race/tests magic_model). + * j += jadd; while j < 32 { acc |= 1 << j; j += p } + * then acc |= (w == 0) (the number 1), word = ~(acc | (w == last ? ~tailmask : 0)). + * Branch-free except the loop branches. Mutant: drops the j fix (j = p - r unreduced), so words where + * base - k0 is a multiple of p (or q overshot) mark the wrong bits. + * + * staged = 1 (OMEGA_GPU_EW_PRIME_SIEVE_SHARED, campaign C3): P4 = table words, sentinel included + * (3 per entry). The CTA first copies the table into shared memory at offset 0 (thread t copies + * words t, t + 128, ...), BAR_SYNC, then the prime loop reads shared memory (LDS) instead of + * uncached global memory. Every thread reaches the barrier: the w >= nwords exit comes after + * it. The launch must declare at least 4 * P4 bytes of shared memory. + * + * divergent (omega #308, regression R1): 0 = the warp-uniform mark loop above (campaign C5). + * 1 = the original lane-dependent mark loop (omega 888a011: `while j < 32 { acc |= 1 << j; j += p }`) + * wrapped in a reconvergence region. 2 = as 1, and the prime loop (whose break `k0 >= base + 32` + * is also lane-dependent: every lane sieves a different word) wrapped in an outer region, so the + * mark loop's region nests inside it (B0 outer, B1 inner). */ +static void gen_prime_sieve(Em *e, int mutant, int staged, int divergent) { + Pro pr = prologue(e); + int nwords = V(e), stride = V(e), lastw = V(e), tailinv = V(e); + int one = V(e), c32 = V(e), cm1 = V(e), w = V(e); + ldc32(e, nwords, ARG_P0); ldc32(e, stride, ARG_P1); ldc32(e, lastw, ARG_P2); ldc32(e, tailinv, ARG_P3); + movi(e, one, 1); movi(e, c32, 32); movi(e, cm1, 0xffffffffu); + int c4 = -1, c8 = -1, c12 = -1; + if (staged) { + int twords = V(e), i = V(e), ga = V64(e), val = V(e), sa = V(e); + c4 = V(e); c8 = V(e); c12 = V(e); + ldc32(e, twords, ARG_P4); + movi(e, c4, 4); movi(e, c8, 8); movi(e, c12, 12); + imadi(e, i, pr.tid, 1, NOREG); + /* The staging loop's trip count differs per thread (thread t copies words t, t+128, ...), + * so its exit splits the warp. The divergent variants rejoin the warp before BAR.SYNC + * (a barrier is not a reconvergence point: the fragments would stay split for the rest + * of the kernel). The production kernel keeps the 2026-10-05 C3 form unchanged. */ + if (divergent) region_begin(e); + loop_begin(e); /* staging loop */ + int ls = here(e); + isetp_ge_u32(e, i, twords); + int sdone = -1; + if (divergent) region_exit(e, RX_IF_P0); else sdone = bra_fwd_if_p0(e); + addr4(e, ga, i, pr.pa); + ldg(e, val, ga, pr.udesc); + imadi(e, sa, i, 4, NOREG); + sts32(e, sa, val); + imadi(e, i, one, OMEGA_GPU_EW_THREADS, i); + bra_always(e, ls); + loop_end(e); + if (divergent) region_join(e); else bra_patch_here(e, sdone); + bar_sync(e); + } + imadi(e, w, pr.cta, OMEGA_GPU_EW_THREADS, pr.tid); + isetp_ge_u32(e, w, nwords); + exit_if_p0(e); + + /* ptr, a64, a64m: global addresses, or (staged) 32-bit shared offsets */ + int base = V(e), basep32 = V(e), acc = V(e); + int ptr = staged ? V(e) : V64(e), a64 = staged ? V(e) : V64(e), a64m = staged ? V(e) : V64(e); + int p = V(e), magic = V(e), k0 = V(e), negp = V(e), d = V(e), neg = V(e), nd = V(e), dpos = V(e), jadd = V(e); + int q = V(e), r = V(e), j = V(e), t = V(e), z = V(e), bit = V(e); + int e1 = V(e), m = V(e), word = V(e), oaddr = V64(e); + int tt = V(e), jj = V(e), sh = V(e), c31 = V(e), cm32 = V(e); + movi(e, c31, 31); movi(e, cm32, 0xffffffe0u); + + loop_begin(e); /* word loop */ + int lw = here(e); + imadi(e, base, w, 32, NOREG); + iadd3(e, basep32, base, c32); + movrz(e, acc); + if (staged) movrz(e, ptr); else widei(e, ptr, one, 0, pr.pa); + + if (divergent >= 2) region_begin(e); /* outer region: the prime loop's break is lane-dependent */ + loop_begin(e); /* prime loop */ + int lp = here(e); + /* one memory round trip per prime: all three entry words load together (C2a). The + * sentinel entry is a whole entry, so loading p and magic before the k0 test is in bounds. */ + if (staged) { iadd3(e, a64, ptr, c8); iadd3(e, a64m, ptr, c4); } + else { widei(e, a64, one, 8, ptr); widei(e, a64m, one, 4, ptr); } + { + const int dsts[3] = { k0, p, magic }, adrs[3] = { a64, ptr, a64m }; + if (staged) ld_group(e, BW_IR_LDS32, 3, dsts, adrs, NOREG); + else ld_group(e, BW_IR_LDG_E, 3, dsts, adrs, pr.udesc); + } + isetp_ge_u32(e, k0, basep32); + int brk = -1; + if (divergent >= 2) region_exit(e, RX_IF_P0); else brk = bra_fwd_if_p0(e); + imadi(e, negp, p, 0xffffffffu, NOREG); /* -p */ + imadi(e, d, k0, 0xffffffffu, base); /* base - k0 */ + shr_i(e, neg, d, 31); + imadi(e, nd, d, 0xffffffffu, NOREG); /* -d */ + imadr(e, dpos, neg, nd, d); /* d - neg*d */ + imadr(e, jadd, neg, nd, NOREG); /* neg * (k0 - base) */ + imadhi(e, q, dpos, magic); + imadr(e, r, q, negp, dpos); /* dpos - q*p */ + imadi(e, j, r, 0xffffffffu, p); /* p - r */ + if (!mutant) { + iadd3(e, t, r, cm1); + shr_i(e, z, t, 31); /* r == 0, or r wrapped (q one too big) */ + imadr(e, j, z, negp, j); /* j - p */ + } + iadd3(e, j, j, jadd); + + /* mark loop (campaign C5): the trip count depends only on p, so every active lane runs the + * same ceil(32/p) steps and the warp never splits here (all active lanes are on the same + * prime). Bits past this word are masked out instead of branched around. The old loop ran + * "while j < 32", a lane-dependent branch with no reconvergence point. */ + if (divergent) { + /* R1: the 888a011 mark loop, lane-dependent trip count, inside a region. Each lane + * leaves at its own iteration (the exit jumps to the BSYNC) and the warp reunites there. */ + region_begin(e); + loop_begin(e); + int li = here(e); + isetp_ge_u32(e, j, c32); + region_exit(e, RX_IF_P0); + shl_r(e, bit, one, j); + lop3lut(e, acc, acc, bit, NOREG, 0xfc); /* acc | bit */ + iadd3(e, j, j, p); + bra_always(e, li); + loop_end(e); + region_join(e); + } else { + movrz(e, tt); + loop_begin(e); + int li = here(e); + isetp_ge_u32(e, tt, c32); + int done = bra_fwd_if_p0(e); + iadd3(e, jj, j, tt); + lop3lut(e, sh, jj, c31, NOREG, 0xc0); /* jj & 31 */ + shl_r(e, bit, one, sh); + iadd3(e, t, jj, cm32); + shr_i(e, z, t, 31); /* jj < 32 (j <= p + 31, far below 2^31) */ + imadi(e, m, z, 0xffffffffu, NOREG); /* all ones when jj < 32, else 0 */ + lop3lut(e, acc, acc, bit, m, 0xf8); /* acc | (bit & m) */ + iadd3(e, tt, tt, p); + bra_always(e, li); + loop_end(e); + bra_patch_here(e, done); + } + + if (staged) iadd3(e, ptr, ptr, c12); else widei(e, ptr, one, 12, ptr); + bra_always(e, lp); + loop_end(e); + if (divergent >= 2) region_join(e); else bra_patch_here(e, brk); + + iadd3(e, t, w, cm1); + shr_i(e, z, t, 31); /* w == 0: the number 1 is not prime */ + lop3lut(e, acc, acc, z, NOREG, 0xfc); + lop3xor(e, e1, w, lastw); + iadd3(e, t, e1, cm1); + shr_i(e, z, t, 31); /* w == last word */ + imadr(e, m, z, tailinv, NOREG); + lop3lut(e, word, acc, m, NOREG, 0x03); /* ~(acc | m) */ + addr4(e, oaddr, w, pr.pc); + stg(e, oaddr, word, pr.udesc); + /* IMAD, not IADD3: the IADD3 encoder ignores the scheduler control word (fixed + * 0x010fca00), so right after a store it would not wait on the read barrier */ + imadr(e, w, stride, one, w); + isetp_ge_u32(e, w, nwords); + bra_back_if_not_p0(e, lw); + loop_end(e); + tail(e); +} + +/* ---- reconvergence probes (omega #308, regressions R3..R6 and the resource bound) ---- + * All: a, b = u32 inputs, out = u32, P0 = n; 128 threads, grid ceil(n/128); launched by + * omega_gpu_reconv_probe_u32. A lane past n clamps its index to n-1 so that it loads valid + * data and takes part in the warp-wide SHFL; it exits only after the SHFL, before its store. + * Each probe computes a per-lane r on divergent paths, joins, then + * out[i] = r + 4099 * s, s = r of lane (lane+1) in the same warp (SHFL.DOWN 1; lane 31: own r) + * so a lane that joined late, never, or with the wrong value shows in the neighbour's word too. + * The host oracle is rc_oracle in tests/gpu_reconv_test.c. */ +typedef struct { Pro pr; int n, i, idx, x, y, r, s, one, cm1; } Rp; +static Rp rp_begin(Em *e) { + Rp k; memset(&k, 0, sizeof k); + k.pr = prologue(e); + k.n = V(e); k.i = V(e); k.idx = V(e); k.x = V(e); k.y = V(e); k.r = V(e); k.s = V(e); k.one = V(e); k.cm1 = V(e); + int nm1 = V(e), d = V(e), neg = V(e), off = V64(e); + ldc32(e, k.n, ARG_P0); + movi(e, k.one, 1); movi(e, k.cm1, 0xffffffffu); + imadi(e, k.i, k.pr.cta, OMEGA_GPU_EW_THREADS, k.pr.tid); + iadd3(e, nm1, k.n, k.cm1); /* n - 1 */ + imadi(e, d, k.i, 0xffffffffu, nm1); /* (n - 1) - i, top bit set when i > n - 1 */ + shr_i(e, neg, d, 31); + imadr(e, k.idx, neg, d, k.i); /* i, or n - 1 when past the end */ + addr4(e, off, k.idx, k.pr.pa); ldg(e, k.x, off, k.pr.udesc); + addr4(e, off, k.idx, k.pr.pb); ldg(e, k.y, off, k.pr.udesc); + return k; +} +static void rp_end(Em *e, const Rp *k) { + int out = V(e), off = V64(e); + shfl_down(e, k->s, k->r, 1); + imadi(e, out, k->s, 4099, k->r); + isetp_ge_u32(e, k->i, k->n); + exit_if_p0(e); + addr4(e, off, k->i, k->pr.pc); + stg(e, off, out, k->pr.udesc); + tail(e); +} +/* R3: if/else with sides of different length. x >= y: r = f(x, y) (5 steps); else r = x + 1. + * Math mutant: the long side is one too big. */ +static void gen_rc_diamond(Em *e, int mutant) { + Rp k = rp_begin(e); + int t = V(e), u = V(e); + region_begin(e); + isetp_ge_u32(e, k.x, k.y); + int els = bra_fwd_if_p0(e); + iadd3(e, k.r, k.x, k.one); /* short side */ + region_exit(e, RX_ALWAYS); + bra_patch_here(e, els); + imadi(e, t, k.x, 3, k.y); /* long side: t = 3x + y */ + lop3xor(e, u, t, k.x); /* u = t ^ x */ + imadi(e, k.r, u, 5, k.one); /* r = 5u + 1 */ + shr_i(e, t, k.r, 3); + iadd3(e, k.r, k.r, t); /* r += r >> 3 */ + if (mutant) iadd3(e, k.r, k.r, k.one); + region_join(e); + rp_end(e, &k); +} +/* The lane-dependent loop shared by R4 and R5: v = x & 255, step = (y & 15) + 1, + * while v < 256 { v += step; cnt += 1 }. Math mutant: the bound is 255. Emits the region. */ +static void rc_count_loop(Em *e, const Rp *k, int mutant, int v, int cnt) { + int st = V(e), bound = V(e), c255 = V(e), c15 = V(e); + movi(e, c255, 255); movi(e, c15, 15); movi(e, bound, mutant ? 255 : 256); + lop3lut(e, v, k->x, c255, NOREG, 0xc0); + lop3lut(e, st, k->y, c15, NOREG, 0xc0); + iadd3(e, st, st, k->one); + movrz(e, cnt); + region_begin(e); + loop_begin(e); + int L = here(e); + isetp_ge_u32(e, v, bound); + region_exit(e, RX_IF_P0); + iadd3(e, v, v, st); + iadd3(e, cnt, cnt, k->one); + bra_always(e, L); + loop_end(e); + region_join(e); +} +/* R4: lane-dependent loop exit (break). r = 256 * cnt + v. */ +static void gen_rc_loopbreak(Em *e, int mutant) { + Rp k = rp_begin(e); + int v = V(e), cnt = V(e); + rc_count_loop(e, &k, mutant, v, cnt); + imadi(e, k.r, cnt, 256, v); + rp_end(e, &k); +} +/* R5: nested divergence. Odd x: r = 7 (skips the inner region). Even x: the R4 loop inside the + * outer region, r = 256 * cnt + v + 100. */ +static void gen_rc_nested(Em *e, int mutant) { + Rp k = rp_begin(e); + int v = V(e), cnt = V(e), low = V(e), c100 = V(e); + movi(e, k.r, 7); movi(e, c100, 100); + lop3lut(e, low, k.x, k.one, NOREG, 0xc0); /* x & 1 */ + region_begin(e); + isetp_ge_u32(e, low, k.one); + region_exit(e, RX_IF_P0); + rc_count_loop(e, &k, mutant, v, cnt); + imadi(e, k.r, cnt, 256, v); + iadd3(e, k.r, k.r, c100); + region_join(e); + rp_end(e, &k); +} +/* R6: lanes past the end, a divergent region, then a CTA-wide exchange and the warp SHFL. + * xv = x | 1 computed on divergent paths (even lanes take the branch), every thread stores xv + * to its shared slot, BAR.SYNC, reads its partner's (tid ^ 1), r = 3x + partner_xv. + * Production form: lanes past n are clamped and exit after the SHFL (correct). early_exit + * (mutant 1): lanes past n EXIT first, as a naive kernel would; the emitter refuses that + * (BAR/SHFL after a predicated EXIT). Only the host-test build path (codegen_ir) may bypass the + * refusal, so the simulator can show what the kernel would do (SHFL_EXITED); a launch never can. */ +static void gen_rc_exit_bar(Em *e, int early_exit) { + Rp k = rp_begin(e); + int saddr = V(e), paddr = V(e), yv = V(e), xv = V(e), low = V(e); + if (early_exit) { + e->exit_sync_ok = early_exit == 1 && e->test_path; + isetp_ge_u32(e, k.i, k.n); + exit_if_p0(e); + } + imadi(e, xv, k.x, 1, NOREG); + lop3lut(e, low, k.x, k.one, NOREG, 0xc0); /* x & 1 */ + region_begin(e); + isetp_ge_u32(e, low, k.one); + region_exit(e, RX_IF_P0); /* odd lanes leave */ + iadd3(e, xv, xv, k.one); /* even lanes: xv = x + 1 */ + region_join(e); + imadi(e, saddr, k.pr.tid, 4, NOREG); + sts32(e, saddr, xv); + bar_sync(e); + lop3xor(e, paddr, k.pr.tid, k.one); + imadi(e, paddr, paddr, 4, NOREG); + lds32(e, yv, paddr); + imadi(e, k.r, k.x, 3, yv); + rp_end(e, &k); +} +/* Barrier-register bound: `levels` nested regions, each leaving lanes with x >= y out and adding 1 + * for the others: r = x + levels * (x < y). 16 levels must build (B0..B15); 17 must be refused. */ +static void gen_rc_depth(Em *e, int levels) { + Rp k = rp_begin(e); + imadi(e, k.r, k.x, 1, NOREG); + for (int d = 0; d < levels; d++) { + region_begin(e); + isetp_ge_u32(e, k.x, k.y); + region_exit(e, RX_IF_P0); + iadd3(e, k.r, k.r, k.one); + } + for (int d = 0; d < levels; d++) region_join(e); + rp_end(e, &k); +} +/* Codegen refusals (each must come back CODEGEN_FAIL; a kernel with a split warp at a + * cross-lane step must never reach the chip). */ +static void gen_rc_refuse(Em *e, OmegaGpuEwOp which) { + Rp k = rp_begin(e); + switch (which) { + case OMEGA_GPU_EW_RC_REFUSE_EXIT_IN_REGION: region_begin(e); isetp_ge_u32(e, k.i, k.n); exit_if_p0(e); region_join(e); break; + case OMEGA_GPU_EW_RC_REFUSE_BAR_IN_REGION: region_begin(e); bar_sync(e); region_join(e); break; + case OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION: region_begin(e); shfl_down(e, k.s, k.x, 1); region_join(e); break; + case OMEGA_GPU_EW_RC_REFUSE_UNCLOSED: region_begin(e); isetp_ge_u32(e, k.x, k.y); region_exit(e, RX_IF_P0); break; /* no join */ + case OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY: region_join(e); break; /* BSYNC without BSSY */ + default: e->err = -1; break; + } + imadi(e, k.r, k.x, 1, NOREG); + rp_end(e, &k); +} + +/* A value first defined inside a loop body may not be read after the loop + * before it is written again: the allocator could have let a later body + * temporary share its register, and the last iteration's value would be lost. */ +static int check_loop_invariant(const Em *e) { + const OmegaRegAlloc *ra = &e->p->regalloc; + for (int L = 0; L < e->n_loops; L++) + for (int v = 0; v < ra->num_vregs; v++) { + int fd = ra->intervals[v].first_def; + if (fd < e->loop_start[L] || fd > e->loop_end[L]) continue; + for (size_t i = (size_t)e->loop_end[L] + 1; i < e->p->count; i++) { + const BlackwellIRInsn *in = &e->p->insns[i]; + if (in->src1_vreg == v || in->src2_vreg == v || in->src3_vreg == v) return -1; + if (in->dst_vreg == v && !in->is_uniform) break; + } + } + return 0; +} + +#define EW_GPR_BUDGET 64u /* the QMD declares 64 registers, as the matmul launcher does */ + +/* Mesa NAK sm120_instr_latencies.rs + sm100/reg_raw.csv: ALU/dual-ALU pairs + * and FMA/FMA pairs need 4 + 1 cycles; mixed pairs need 5 + 1. CSV rows are + * READERS, columns WRITERS (see lat_rs_gen.py), not the reverse. IMAD.WIDE + * has operand-specific latencies and is deliberately excluded here. + * + * Retain at least 5 cycles after every fixed producer. Thus the next reader + * uses the pair-specific bound and later readers are >= 9 cycles away (the + * next instruction in a tightened pair is itself fixed). No instruction, + * branch target, wait, barrier assignment or register allocation is changed. + * IADD3's encoder still supplies its historical control word; do not pretend + * to schedule that producer here. Its existing 5-cycle delay is unchanged. + * + * Predicate-to-guard uses NAK paw_latency -> raw(write, None): reader + * RedirectedFp64, writer Dualalu = 13, plus SM120's 1 = 14, in emit above. + * NVIDIA's public ISA docs are silent about SASS control-word semantics. + */ +static int fixed_pair_class(BlackwellIROpcode op) { + switch (op) { + case BW_IR_MOV_RZ: case BW_IR_MOV_IMM: case BW_IR_IADD3: + case BW_IR_LOP3_XOR: case BW_IR_LOP3_LUT: + case BW_IR_SHF_R: case BW_IR_SHF_L_U32: return 1; + case BW_IR_IMAD: case BW_IR_IMAD_HI_U32: + case BW_IR_FADD: case BW_IR_FSUB: case BW_IR_FMUL: case BW_IR_FFMA: return 2; + default: return 0; + } +} + +static void schedule_fixed_pairs(BlackwellIRProgram *p) { + for (size_t i = 0; i + 1 < p->count; i++) { + BlackwellIRInsn *a = &p->insns[i]; + const BlackwellIRInsn *b = &p->insns[i + 1]; + int cls = fixed_pair_class(a->op); + if (!cls || cls != fixed_pair_class(b->op) || a->op == BW_IR_IADD3 || + a->predicate_p0 || b->predicate_p0 || ((a->control >> 9) & 15u) != 6) + continue; + a->control = (a->control & ~(15u << 9)) | (5u << 9); + } +} + +static int build_kernel(OmegaGpuEwOp op, int mutant, OmegaBlackwellKernel *kernel, BlackwellIRProgram *prog_out, int test_path) { + BlackwellIRProgram *prog = calloc(1, sizeof *prog); + if (!prog) return OMEGA_GPU_EW_CODEGEN_FAIL; + omega_bw_ir_init(prog); + Em e; memset(&e, 0, sizeof e); e.p = prog; e.test_path = test_path; + /* mutant 1 = the op's math mutant (as before); 2 = the join (BSYNC) dropped; 3 = the BSSY + * names the wrong join. 2 and 3 only change kernels that open a region. */ + e.rg_mutant = mutant == 2 ? 1 : mutant == 3 ? 2 : 0; + int math = mutant == 1; + switch (op) { + case OMEGA_GPU_EW_EX2: gen_ex2(&e, math); break; + case OMEGA_GPU_EW_XCHG: gen_xchg(&e, math); break; + case OMEGA_GPU_EW_RMSNORM: gen_rmsnorm(&e, math); break; + case OMEGA_GPU_EW_ROPE: gen_rope(&e, math); break; + case OMEGA_GPU_EW_SWIGLU: gen_swiglu(&e, math); break; + case OMEGA_GPU_EW_PRIME_SIEVE: gen_prime_sieve(&e, math, 0, 0); break; + case OMEGA_GPU_EW_PRIME_SIEVE_SHARED: gen_prime_sieve(&e, math, 1, 0); break; + case OMEGA_GPU_EW_PRIME_SIEVE_DIV1: gen_prime_sieve(&e, math, 0, 1); break; + case OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1: gen_prime_sieve(&e, math, 1, 1); break; + case OMEGA_GPU_EW_PRIME_SIEVE_DIV2: gen_prime_sieve(&e, math, 0, 2); break; + case OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2: gen_prime_sieve(&e, math, 1, 2); break; + case OMEGA_GPU_EW_RC_DIAMOND: gen_rc_diamond(&e, math); break; + case OMEGA_GPU_EW_RC_LOOPBREAK: gen_rc_loopbreak(&e, math); break; + case OMEGA_GPU_EW_RC_NESTED: gen_rc_nested(&e, math); break; + case OMEGA_GPU_EW_RC_EXIT_BAR: gen_rc_exit_bar(&e, math); break; + case OMEGA_GPU_EW_RC_REFUSE_EXIT_SHFL: gen_rc_exit_bar(&e, 2); break; + case OMEGA_GPU_EW_RC_DEPTH16: gen_rc_depth(&e, BW_RECONV_MAX_BAR); break; + case OMEGA_GPU_EW_RC_REFUSE_DEPTH17: gen_rc_depth(&e, BW_RECONV_MAX_BAR + 1); break; + case OMEGA_GPU_EW_RC_REFUSE_EXIT_IN_REGION: case OMEGA_GPU_EW_RC_REFUSE_BAR_IN_REGION: + case OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION: case OMEGA_GPU_EW_RC_REFUSE_UNCLOSED: + case OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY: gen_rc_refuse(&e, op); break; + default: e.err = -1; break; + } + if (e.rg.depth != 0) e.err = -1; /* a region left open has no join: refuse the build */ + schedule_fixed_pairs(prog); + int rc = OMEGA_GPU_EW_CODEGEN_FAIL; + int ra_rc = (e.err == 0) ? omega_bw_regalloc_solve(prog) : -1; + int loop_rc = (ra_rc == 0) ? check_loop_invariant(&e) : -1; +#ifdef OMEGA_EW_DEBUG + fprintf(stderr, "ew codegen op=%d mutant=%d emit_err=%d insns=%zu vregs=%d regalloc=%d loops=%d peak_gpr=%u\n", + (int)op, mutant, e.err, prog->count, prog->regalloc.num_vregs, ra_rc, loop_rc, prog->regalloc.peak_gpr_usage); +#endif + if (e.err == 0 && ra_rc == 0 && loop_rc == 0 && prog->regalloc.peak_gpr_usage <= EW_GPR_BUDGET) { + size_t max_bytes = (size_t)BW_MAX_IR_INSNS * 16; + memset(kernel, 0, sizeof *kernel); + kernel->code = malloc(max_bytes); + size_t emitted = 0; + if (kernel->code && omega_bw_encode_program(prog, kernel->code, max_bytes, &emitted) == 0) { + kernel->code_size = emitted; + kernel->insn_count = emitted / 16; + kernel->gpr_count = prog->regalloc.peak_gpr_usage; + kernel->uniform_gpr_count = prog->regalloc.peak_ugpr_usage; + sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); + if (prog_out) *prog_out = *prog; + rc = OMEGA_GPU_EW_OK; + } else { +#ifdef OMEGA_EW_DEBUG + fprintf(stderr, "ew codegen op=%d: encode failed (an instruction form the encoder refuses)\n", (int)op); +#endif + free(kernel->code); kernel->code = NULL; + } + } + free(prog); + return rc; +} + +/* --------------------------------------------------------- kernel cache */ + +/* One kernel per (op, mutant); the device copy is uploaded once and stays + * resident for the process (cut 4b: no per-call code page). */ +typedef struct { int used; int op; int mutant; OmegaBlackwellKernel kernel; NvrmMem code; } Slot; +static int g_mutant_op; /* 0 = none */ +static int g_mutant_level = 1; /* the mutant number built for g_mutant_op (1 math, 2 join dropped, 3 join corrupted) */ +static Slot g_cache[8]; +static OmegaGpuScratch g_a, g_b, g_c; /* staging, grown to the high-water mark, reused per call */ +static bool g_hooked; +#define LOCK() omega_gpu_session_lock() +#define UNLOCK() omega_gpu_session_unlock() + +static void cache_clear_locked(void) { + for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) + if (g_cache[i].used) { free(g_cache[i].kernel.code); omega_gpu_session_free(&g_cache[i].code); memset(&g_cache[i], 0, sizeof g_cache[i]); } +} + +void omega_gpu_elementwise_cache_clear(void) { + LOCK(); + cache_clear_locked(); + UNLOCK(); +} + +/* Called by the session (lock held, device still open) right before it closes. */ +static void on_session_close(void) { + cache_clear_locked(); + omega_gpu_session_scratch_free(&g_a); omega_gpu_session_scratch_free(&g_b); omega_gpu_session_scratch_free(&g_c); +} + +void omega_gpu_elementwise_test_set_mutant(int op) { + LOCK(); + g_mutant_op = op; g_mutant_level = 1; + cache_clear_locked(); + UNLOCK(); +} +void omega_gpu_elementwise_test_set_mutant_level(int op, int level) { + LOCK(); + g_mutant_op = op; g_mutant_level = level < 1 ? 1 : level > 3 ? 3 : level; + cache_clear_locked(); + UNLOCK(); +} + +/* Caller holds the lock. Builds the kernel if needed; uploads the device copy when a + * device is open (upload = false lets the host-only codegen path run without a chip). */ +static int kernel_for(OmegaGpuEwOp op, bool upload, Slot **out, bool *hit) { + int mutant = (g_mutant_op == (int)op) ? g_mutant_level : 0; + Slot *s = NULL; + for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) + if (g_cache[i].used && g_cache[i].op == (int)op && g_cache[i].mutant == mutant) { s = &g_cache[i]; *hit = true; break; } + if (!s) { + OmegaBlackwellKernel k; + int rc = build_kernel(op, mutant, &k, NULL, 0); + if (rc != OMEGA_GPU_EW_OK) return rc; + size_t slot = 0; + for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) if (!g_cache[i].used) { slot = i; break; } + s = &g_cache[slot]; + if (s->used) { free(s->kernel.code); omega_gpu_session_free(&s->code); } /* full: evict slot 0 */ + memset(s, 0, sizeof *s); + s->used = 1; s->op = (int)op; s->mutant = mutant; s->kernel = k; + *hit = false; + } + if (upload && !s->code.cpu) { + if (omega_gpu_session_alloc(s->kernel.code_size, &s->code) != 0) return OMEGA_GPU_EW_CHIP_FAIL; + memcpy(s->code.cpu, s->kernel.code, s->kernel.code_size); + __asm__ volatile("dsb sy" ::: "memory"); + } + *out = s; + return OMEGA_GPU_EW_OK; +} + +int omega_gpu_elementwise_codegen(OmegaGpuEwOp op, const uint32_t *dims, float eps, OmegaBlackwellKernel *kernel) { + (void)dims; (void)eps; /* kernels read their shape from the argument words */ + if (!kernel) return OMEGA_GPU_EW_BAD_ARGS; + LOCK(); + int mutant = (g_mutant_op == (int)op) ? g_mutant_level : 0; + UNLOCK(); + return build_kernel(op, mutant, kernel, NULL, 0); +} + +int omega_gpu_elementwise_codegen_ir(OmegaGpuEwOp op, int mutant, void *prog, OmegaBlackwellKernel *kernel) { + if (!kernel) return OMEGA_GPU_EW_BAD_ARGS; + if (mutant < 0 || mutant > 3) return OMEGA_GPU_EW_BAD_ARGS; + return build_kernel(op, mutant, kernel, (BlackwellIRProgram *)prog, 1); +} + +const char *omega_gpu_elementwise_last_error(void) { return omega_gpu_session_last_error(); } + +/* ------------------------------------------------------------ launcher */ + +#define EW_POISON 0xffbadbadu /* a NaN pattern no kernel here produces on purpose */ +#define EW_WAIT_MS 600000ull /* like main's load/store launchers; a 5 s wait failed under contention (omega#198) */ + +static uint64_t now_ns(void) { + struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); + return (uint64_t)t.tv_sec * 1000000000ULL + (uint64_t)t.tv_nsec; +} + +/* Caller holds the lock. Opens the shared device on first use. */ +static int dev_open_locked(void) { + if (!g_hooked) { (void)omega_gpu_session_on_close(on_session_close); g_hooked = true; } + return omega_gpu_session_open() ? OMEGA_GPU_EW_OK : OMEGA_GPU_EW_CHIP_FAIL; +} + +/* One launch: a and b staged in, c out (c_bytes/4 words, poison-filled first), through + * the shared session (omega_gpu_session_launch: shader-cache invalidate, constant bank, + * QMD0/QMD1, first marker, L2_FLUSH_DIRTY, second marker), then readback and the + * poison scan. b may be NULL when b_fill wrote the staging buffer already. */ +static int run_abc(const Slot *ks, const void *a, size_t a_bytes, const void *b, size_t b_bytes, + void *c, size_t c_bytes, uint32_t threads_x, uint32_t grid_x, + const uint32_t params[4], OmegaGpuEwInfo *info) { + if (omega_gpu_session_scratch(&g_a, a_bytes) || omega_gpu_session_scratch(&g_b, b_bytes) || omega_gpu_session_scratch(&g_c, c_bytes)) + return OMEGA_GPU_EW_CHIP_FAIL; + memcpy(g_a.mem.cpu, a, a_bytes); + if (b) memcpy(g_b.mem.cpu, b, b_bytes); + size_t c_words = c_bytes / 4; + uint32_t *c_dev = (uint32_t *)g_c.mem.cpu; + for (size_t i = 0; i < c_words; i++) c_dev[i] = EW_POISON; + __asm__ volatile("dsb sy" ::: "memory"); + + uint32_t args[OMEGA_BW_CBANK_MATMUL_ARGS_WORDS]; + args[0] = (uint32_t)g_a.mem.va; args[1] = (uint32_t)(g_a.mem.va >> 32); + args[2] = (uint32_t)g_b.mem.va; args[3] = (uint32_t)(g_b.mem.va >> 32); + args[4] = (uint32_t)g_c.mem.va; args[5] = (uint32_t)(g_c.mem.va >> 32); + args[6] = params[0]; args[7] = params[1]; args[8] = params[2]; args[9] = params[3]; + OmegaGpuLaunch L = { + .code_va = ks->code.va, .gpr_count = EW_GPR_BUDGET, .threads_x = threads_x, .threads_y = 1, + .grid_x = grid_x, .grid_y = 1, .num_elements = c_words, + .args = args, .n_args = OMEGA_BW_CBANK_MATMUL_ARGS_WORDS, .timeout_ms = EW_WAIT_MS, + }; + uint64_t elapsed = 0; uint32_t marker = 0; + if (omega_gpu_session_launch(&L, &elapsed, &marker) != 0) return OMEGA_GPU_EW_CHIP_FAIL; + + memcpy(c, g_c.mem.cpu, c_bytes); + uint32_t unwritten = 0; + for (size_t i = 0; i < c_words; i++) if (((uint32_t *)c)[i] == EW_POISON) unwritten++; + if (info) { + info->chip_calls++; + info->elapsed_ns += elapsed; + info->completion_marker = marker; + info->unwritten_words += unwritten; + info->threads_per_cta = threads_x; + info->ctas_last_launch = grid_x; + } + return unwritten ? OMEGA_GPU_EW_UNWRITTEN : OMEGA_GPU_EW_OK; +} + +/* ------------------------------------------------------- public entries */ + +static void info_begin(OmegaGpuEwInfo *info, const OmegaBlackwellKernel *k, bool hit) { + if (!info) return; + memset(info, 0, sizeof *info); + info->kernel_cache_hit = hit; + info->gpr_count = k->gpr_count; + info->insn_count = (uint32_t)k->insn_count; + snprintf(info->target_chip, sizeof info->target_chip, "NVIDIA DGX Spark (Grace Blackwell GB10)"); + info->sm_architecture = 121; +} +static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } +static void info_end(OmegaGpuEwInfo *info, uint64_t t0) { if (info) info->call_ns = now_ns() - t0; } + +/* Common prologue of every chip entry: lock, open, kernel (resident), info. */ +static int begin(OmegaGpuEwOp op, Slot **ks, OmegaGpuEwInfo *info) { + LOCK(); + int rc = dev_open_locked(); + bool hit = false; + if (rc == OMEGA_GPU_EW_OK) rc = kernel_for(op, true, ks, &hit); + if (rc != OMEGA_GPU_EW_OK) { UNLOCK(); return rc; } + info_begin(info, &(*ks)->kernel, hit); + return OMEGA_GPU_EW_OK; +} + +int omega_gpu_rmsnorm_f32(uint32_t rows, uint32_t dim, const float *x, const float *weight, + float eps, float *out, OmegaGpuEwInfo *info) { + uint64_t t0 = now_ns(); + if (!x || !weight || !out || rows == 0 || dim == 0 || dim % OMEGA_GPU_EW_THREADS != 0) return OMEGA_GPU_EW_BAD_ARGS; + if (dim > OMEGA_GPU_EW_MAX_DIM) return OMEGA_GPU_EW_TOO_LARGE; + Slot *k = NULL; + int rc = begin(OMEGA_GPU_EW_RMSNORM, &k, info); + if (rc != OMEGA_GPU_EW_OK) return rc; + uint32_t params[4] = { dim / OMEGA_GPU_EW_THREADS, f2u(1.0f / (float)dim), f2u(eps), dim }; + for (uint32_t r0 = 0; r0 < rows && rc == OMEGA_GPU_EW_OK; r0 += OMEGA_GPU_EW_MAX_CTAS) { + uint32_t nr = rows - r0 < OMEGA_GPU_EW_MAX_CTAS ? rows - r0 : OMEGA_GPU_EW_MAX_CTAS; + size_t bytes = (size_t)nr * dim * 4; + rc = run_abc(k, x + (size_t)r0 * dim, bytes, weight, (size_t)dim * 4, out + (size_t)r0 * dim, bytes, + OMEGA_GPU_EW_THREADS, nr, params, info); + } + UNLOCK(); + info_end(info, t0); + return rc; +} + +int omega_gpu_rope_f32(uint32_t heads, uint32_t head_dim, const float *v, + const float *cos_half, const float *sin_half, float *out, OmegaGpuEwInfo *info) { + uint64_t t0 = now_ns(); + if (!v || !cos_half || !sin_half || !out || heads == 0 || head_dim < 2 || (head_dim & 1)) return OMEGA_GPU_EW_BAD_ARGS; + if (head_dim > 2048) return OMEGA_GPU_EW_TOO_LARGE; + Slot *k = NULL; + int rc = begin(OMEGA_GPU_EW_ROPE, &k, info); + if (rc != OMEGA_GPU_EW_OK) return rc; + uint32_t half = head_dim / 2; + uint32_t params[4] = { head_dim, half, 0, 0 }; + for (uint32_t h0 = 0; h0 < heads && rc == OMEGA_GPU_EW_OK; h0 += OMEGA_GPU_EW_MAX_CTAS) { + uint32_t nh = heads - h0 < OMEGA_GPU_EW_MAX_CTAS ? heads - h0 : OMEGA_GPU_EW_MAX_CTAS; + size_t bytes = (size_t)nh * head_dim * 4; + /* table: cos in the first half of each head, sin in the second, written straight into + * the staging buffer (one copy per head of this launch) */ + if (omega_gpu_session_scratch(&g_b, bytes) != 0) { rc = OMEGA_GPU_EW_CHIP_FAIL; break; } + float *table = g_b.mem.cpu; + for (uint32_t h = 0; h < nh; h++) { + memcpy(table + (size_t)h * head_dim, cos_half, half * sizeof *table); + memcpy(table + (size_t)h * head_dim + half, sin_half, half * sizeof *table); + } + /* out may alias v: v is copied into the staging buffer before the result lands in out */ + rc = run_abc(k, v + (size_t)h0 * head_dim, bytes, NULL, bytes, out + (size_t)h0 * head_dim, bytes, half, nh, params, info); + } + UNLOCK(); + info_end(info, t0); + return rc; +} + +static int run_1d(OmegaGpuEwOp op, uint32_t n, const void *a, const void *b, void *c, OmegaGpuEwInfo *info) { + uint64_t t0 = now_ns(); + Slot *k = NULL; + int rc = begin(op, &k, info); + if (rc != OMEGA_GPU_EW_OK) return rc; + const uint32_t chunk = OMEGA_GPU_EW_MAX_CTAS * OMEGA_GPU_EW_THREADS; + for (uint32_t i0 = 0; i0 < n && rc == OMEGA_GPU_EW_OK; i0 += chunk) { + uint32_t ni = n - i0 < chunk ? n - i0 : chunk; + uint32_t grid = (ni + OMEGA_GPU_EW_THREADS - 1) / OMEGA_GPU_EW_THREADS; + uint32_t params[4] = { ni, 0, 0, 0 }; + size_t bytes = (size_t)ni * 4; + rc = run_abc(k, (const uint8_t *)a + (size_t)i0 * 4, bytes, b ? (const uint8_t *)b + (size_t)i0 * 4 : a, bytes, + (uint8_t *)c + (size_t)i0 * 4, bytes, OMEGA_GPU_EW_THREADS, grid, params, info); + } + UNLOCK(); + info_end(info, t0); + return rc; +} + +int omega_gpu_swiglu_f32(uint32_t n, const float *gate, const float *up, float *out, OmegaGpuEwInfo *info) { + if (!gate || !up || !out || n == 0) return OMEGA_GPU_EW_BAD_ARGS; + return run_1d(OMEGA_GPU_EW_SWIGLU, n, gate, up, out, info); +} +int omega_gpu_ex2_f32(uint32_t n, const float *x, float *out, OmegaGpuEwInfo *info) { + if (!x || !out || n == 0) return OMEGA_GPU_EW_BAD_ARGS; + return run_1d(OMEGA_GPU_EW_EX2, n, x, NULL, out, info); +} +int omega_gpu_shared_xchg_u32(uint32_t n, const uint32_t *x, uint32_t *out, OmegaGpuEwInfo *info) { + if (!x || !out || n == 0 || n % OMEGA_GPU_EW_THREADS != 0) return OMEGA_GPU_EW_BAD_ARGS; + return run_1d(OMEGA_GPU_EW_XCHG, n, x, NULL, out, info); +} +int omega_gpu_reconv_probe_u32(OmegaGpuEwOp op, uint32_t n, const uint32_t *a, const uint32_t *b, uint32_t *out, OmegaGpuEwInfo *info) { + if (!a || !b || !out || n == 0) return OMEGA_GPU_EW_BAD_ARGS; + switch (op) { + case OMEGA_GPU_EW_RC_DIAMOND: case OMEGA_GPU_EW_RC_LOOPBREAK: case OMEGA_GPU_EW_RC_NESTED: + case OMEGA_GPU_EW_RC_EXIT_BAR: case OMEGA_GPU_EW_RC_DEPTH16: break; + default: return OMEGA_GPU_EW_BAD_ARGS; + } + return run_1d(op, n, a, b, out, info); +} + +const char *omega_gpu_elementwise_rc_name(int rc) { + switch (rc) { + case OMEGA_GPU_EW_OK: return "OK"; + case OMEGA_GPU_EW_BAD_ARGS: return "BAD_ARGS"; + case OMEGA_GPU_EW_TOO_LARGE: return "TOO_LARGE"; + case OMEGA_GPU_EW_CODEGEN_FAIL: return "CODEGEN_FAIL"; + case OMEGA_GPU_EW_CHIP_FAIL: return "CHIP_FAIL"; + case OMEGA_GPU_EW_UNWRITTEN: return "UNWRITTEN"; + default: return "UNKNOWN"; + } +} diff --git a/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/omega_gpu_elementwise_api.h b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/omega_gpu_elementwise_api.h new file mode 100644 index 000000000..8b92c839d --- /dev/null +++ b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/omega_gpu_elementwise_api.h @@ -0,0 +1,177 @@ +#ifndef OMEGA_GPU_ELEMENTWISE_API_H +#define OMEGA_GPU_ELEMENTWISE_API_H +/* + * omega_gpu_elementwise_api: native (no CUDA) elementwise and row-reduction + * kernels for the AIEN inference stack (FB-1 cut 4, 2026-10-04). + * + * Three inference ops plus two probe ops, every kernel generated by our own + * codegen (src/omega_blackwell_codegen.c IR) and launched through the same + * chip-proven sequence as the tensor matmul path: all buffers GPU-uncached + * (nvos.h NVOS32_ATTR2_GPU_CACHEABLE: system memory cached by the GPU is not + * coherent with CPU mappings), first marker, L2_FLUSH_DIRTY, second marker + * (C3 tail), poison-filled output scanned for unwritten words after readback. + * + * The math follows aien-sovereign-core crates/aien-inference-abi/src/tensor.rs + * (ReferenceCpuBackend), read 2026-10-04 at main 5fdab70: + * rmsnorm: scale = 1/sqrt(mean(x^2) + eps) (oracle sums in f64; the chip + * sums in f32 and uses MUFU.RSQ refined by one Newton step), + * out[i] = (x[i] * scale) * weight[i] in that order. + * rope: rotate_half pairs (i, i + head_dim/2) per head, cos/sin tables + * supplied by the host as f32 (the oracle casts f64 cos/sin to f32 + * before the f32 math): out0 = q0*cos - q1*sin, out1 = q0*sin + q1*cos, + * emitted as FMUL, FMUL, FADD (no fused multiply-add), so the chip + * result is bit-identical to the f32 oracle. + * swiglu: out = silu(gate) * up, silu(g) = g / (1 + exp(-g)); the chip uses + * MUFU.EX2(g * -log2(e)) and MUFU.RCP (approximate units), the + * oracle computes in f64. Tolerance is stated per op in the test. + * + * Cut 4b (2026-10-04): the device, channel and launch scratch are the process-wide + * session shared with the matmul and attention APIs (omega_gpu_session.h): one open + * per process instead of one per call (~40 ms), kernels resident after first use, + * staging buffers reused at their high-water mark. Calls serialise on one lock. + * + * Launch envelope: at most OMEGA_GPU_EW_MAX_CTAS CTAs per launch (the matmul + * sweep of 2026-10-03 lost whole tiles at 128 CTAs, investigation I42); the + * API splits larger work on the host and makes several launches. + */ +#include +#include +#include +#include "omega_blackwell_matmul.h" /* OmegaBlackwellKernel */ + +#ifdef __cplusplus +extern "C" { +#endif + +enum { + OMEGA_GPU_EW_OK = 0, + OMEGA_GPU_EW_BAD_ARGS = -1, /* NULL pointer, zero dimension, or a shape rule below broken */ + OMEGA_GPU_EW_TOO_LARGE = -2, /* dimension outside the codegen limits */ + OMEGA_GPU_EW_CODEGEN_FAIL = -3, /* our codegen refused or exceeded the register budget */ + OMEGA_GPU_EW_CHIP_FAIL = -4, /* device open, submit, marker or release failed */ + OMEGA_GPU_EW_UNWRITTEN = -5 /* output words still equal to their poison after the run */ +}; + +/* Kernel families. The two probes exist to prove the new instructions on the + * chip with the smallest possible kernels (and are kept for regression). */ +typedef enum { + OMEGA_GPU_EW_EX2 = 1, /* out[i] = 2^a[i] (probe for MUFU.EX2) */ + OMEGA_GPU_EW_XCHG, /* out[i] = a[i ^ (T-1)] inside each CTA (probe for STS/BAR.SYNC/LDS) */ + OMEGA_GPU_EW_RMSNORM, + OMEGA_GPU_EW_ROPE, + OMEGA_GPU_EW_SWIGLU, + OMEGA_GPU_EW_PRIME_SIEVE, /* prime race (2026-10-05): word-parallel odd-only sieve, codegen only + * here; launched by bench/prime_race/gb10_native.c (argument words in + * gen_prime_sieve's comment) */ + OMEGA_GPU_EW_PRIME_SIEVE_SHARED, /* the same sieve with the table staged in shared memory (C3) */ + /* Structured warp reconvergence (omega #308). The four sieves below are the production sieve + * with its lane-dependent loops put back inside BSSY/BSYNC regions (regression R1): DIV1 wraps + * the original mark loop (omega 888a011), DIV2 also wraps the prime loop (nested regions). */ + OMEGA_GPU_EW_PRIME_SIEVE_DIV1, + OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1, + OMEGA_GPU_EW_PRIME_SIEVE_DIV2, + OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2, + /* Probes for omega_gpu_reconv_probe_u32 (u32 in a, b; u32 out; oracle in tests/gpu_reconv_test.c) */ + OMEGA_GPU_EW_RC_DIAMOND, /* R3: if/else, sides of different length, SHFL after the join */ + OMEGA_GPU_EW_RC_LOOPBREAK, /* R4: loop with a lane-dependent break */ + OMEGA_GPU_EW_RC_NESTED, /* R5: a lane-dependent loop region inside an if region */ + OMEGA_GPU_EW_RC_EXIT_BAR, /* R6: lanes past the end + STS/BAR.SYNC/LDS exchange + SHFL, lanes exit last */ + OMEGA_GPU_EW_RC_DEPTH16, /* 16 nested regions: every barrier register B0..B15 in use */ + /* Codegen refusals: omega_gpu_elementwise_codegen_ir must return CODEGEN_FAIL for each */ + OMEGA_GPU_EW_RC_REFUSE_DEPTH17, /* a 17th nested region: no barrier register left */ + OMEGA_GPU_EW_RC_REFUSE_EXIT_SHFL, /* lanes EXIT, then BAR.SYNC/SHFL on the rest */ + OMEGA_GPU_EW_RC_REFUSE_EXIT_IN_REGION, /* EXIT inside an open region */ + OMEGA_GPU_EW_RC_REFUSE_BAR_IN_REGION, /* BAR.SYNC inside an open region */ + OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION, /* SHFL inside an open region */ + OMEGA_GPU_EW_RC_REFUSE_UNCLOSED, /* region begun, never joined */ + OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY /* BSYNC with no BSSY */ +} OmegaGpuEwOp; + +/* Shape rules (checked, refused with BAD_ARGS / TOO_LARGE): + * rmsnorm: dim % OMEGA_GPU_EW_THREADS == 0, dim <= OMEGA_GPU_EW_MAX_DIM, rows >= 1 + * rope: head_dim even, 2 <= head_dim <= 2048 (head_dim/2 threads per CTA), heads >= 1 + * swiglu: n >= 1 (the kernel guards the tail with a predicated EXIT) + * ex2: n >= 1; xchg: n % OMEGA_GPU_EW_THREADS == 0 */ +#define OMEGA_GPU_EW_THREADS 128u +#define OMEGA_GPU_EW_MAX_DIM 16384u +#define OMEGA_GPU_EW_MAX_CTAS 64u + +typedef struct { + uint32_t chip_calls; /* launches made */ + uint64_t elapsed_ns; /* sum of launch-to-second-marker over the launches */ + uint32_t completion_marker; /* observed first marker word of the last launch */ + uint32_t unwritten_words; /* output words still equal to poison (0 on success) */ + bool kernel_cache_hit; /* the shape's kernel was already generated in this process */ + uint32_t gpr_count; /* registers the generated kernel uses */ + uint32_t insn_count; + uint32_t threads_per_cta; + uint32_t ctas_last_launch; + char target_chip[64]; + uint32_t sm_architecture; + uint64_t call_ns; /* cut 4b: wall time of the whole API call (staging, launches, copy-out) */ +} OmegaGpuEwInfo; + +/* out[r*dim + i] = x[r*dim + i] * scale_r * weight[i]; scale_r = 1/sqrt(mean_i(x^2) + eps). */ +int omega_gpu_rmsnorm_f32(uint32_t rows, uint32_t dim, const float *x, const float *weight, + float eps, float *out, OmegaGpuEwInfo *info); + +/* v holds `heads` heads of head_dim values back to back; cos_half and sin_half + * hold head_dim/2 values each (cos(pos*freq_i), sin(pos*freq_i)); out gets the + * rotated heads (may alias v: the API copies through its own buffers). */ +int omega_gpu_rope_f32(uint32_t heads, uint32_t head_dim, const float *v, + const float *cos_half, const float *sin_half, float *out, + OmegaGpuEwInfo *info); + +/* out[i] = silu(gate[i]) * up[i] for i < n. */ +int omega_gpu_swiglu_f32(uint32_t n, const float *gate, const float *up, float *out, + OmegaGpuEwInfo *info); + +/* Probes. ex2: out[i] = 2^x[i]. xchg: out[i] = x[i ^ (OMEGA_GPU_EW_THREADS-1)] within each CTA. */ +int omega_gpu_ex2_f32(uint32_t n, const float *x, float *out, OmegaGpuEwInfo *info); +int omega_gpu_shared_xchg_u32(uint32_t n, const uint32_t *x, uint32_t *out, OmegaGpuEwInfo *info); +/* Reconvergence probes (op = one of OMEGA_GPU_EW_RC_DIAMOND .. OMEGA_GPU_EW_RC_DEPTH16, else + * BAD_ARGS). n >= 1, any n: lanes past the end take part in the warp step and exit before + * their store. out[i] = r_i + 4099 * r_(i+1 in warp); r per op in src/omega_gpu_elementwise_api.c. */ +int omega_gpu_reconv_probe_u32(OmegaGpuEwOp op, uint32_t n, const uint32_t *a, const uint32_t *b, + uint32_t *out, OmegaGpuEwInfo *info); + +/* Generate (without launching) the kernel for one op. Kernels read their shape + * from the argument words, so one kernel per op serves every shape; dims and eps + * are accepted for forward compatibility and ignored today (may be NULL / 0). + * Lets tests disassemble the bytes offline (nvdisasm) and check the register + * budget. Caller frees kernel->code. */ +int omega_gpu_elementwise_codegen(OmegaGpuEwOp op, const uint32_t *dims, float eps, + OmegaBlackwellKernel *kernel); + +/* Same, with the mutant chosen by the caller (0 = production kernel, 1 = the op's math mutant, + * 2 = the reconvergence join (BSYNC) dropped, 3 = the BSSY names the wrong join; 2 and 3 change + * only kernels that open a region, the host simulator must catch both) and, when prog is + * not NULL, a copy of the allocated IR program (a BlackwellIRProgram, post register + * allocation) for a host IR simulator. Does not touch the device or the kernel cache. + * Caller frees kernel->code. */ +int omega_gpu_elementwise_codegen_ir(OmegaGpuEwOp op, int mutant, void *prog, + OmegaBlackwellKernel *kernel); + +/* TEST ONLY. Selects a deliberately wrong kernel for one op (0 clears). Each + * mutant changes the math by far more than the op's tolerance, so a parity + * check that still passes against it proves nothing. Clears the kernel cache. + * EX2: computes 2^(x+1). XCHG: reads its own shared word (no exchange). + * RMSNORM: the warp tree drops its last SHFL step (half the lanes go missing). + * ROPE: out0 = q0*cos + q1*sin. SWIGLU: exp(+g) instead of exp(-g). */ +void omega_gpu_elementwise_test_set_mutant(int op); +/* TEST ONLY. Like test_set_mutant, choosing the mutant number (1 math, 2 join dropped, 3 join + * corrupted) that the next launch of `op` builds; lets a chip test observe what a missing + * reconvergence does (the simulator must have flagged it first). */ +void omega_gpu_elementwise_test_set_mutant_level(int op, int level); + +const char *omega_gpu_elementwise_rc_name(int rc); +void omega_gpu_elementwise_cache_clear(void); + +/* Stage name of the most recent CHIP_FAIL ("" if none), shared with the other GPU APIs + * (omega_gpu_session_last_error); an open failure carries the driver text. */ +const char *omega_gpu_elementwise_last_error(void); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/receipt.json b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/receipt.json new file mode 100644 index 000000000..28f01d1fc --- /dev/null +++ b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/receipt.json @@ -0,0 +1 @@ +{"schema":"OMEGA_GPU_RECONV_V1","issue":308,"comparison":"exact u32","cases":[{"op":"rc_diamond","regression":"R3","n":1,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":112065,"chip_calls":1,"unwritten_words":0,"cache_hit":false},{"op":"rc_diamond","regression":"R3","n":5,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":104033,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":31,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103185,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":32,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103537,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":33,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103265,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":63,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102961,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":64,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103473,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":65,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103264,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":96,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103601,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":127,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103281,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":128,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103265,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":129,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103233,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":200,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102993,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":1000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103249,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":4096,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":104209,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":8191,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":104209,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":8192,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103537,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":8193,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":206594,"chip_calls":2,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":20000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":310067,"chip_calls":3,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":1,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103217,"chip_calls":1,"unwritten_words":0,"cache_hit":false},{"op":"rc_loopbreak","regression":"R4","n":5,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103041,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":31,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102961,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":32,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103297,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":33,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102977,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":63,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103777,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":64,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":104849,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":65,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103217,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":96,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103137,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":127,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102977,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":128,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103089,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":129,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103329,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":200,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103265,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":1000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103073,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":4096,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103024,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":8191,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103377,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":8192,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103009,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":8193,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":206866,"chip_calls":2,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":20000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":310819,"chip_calls":3,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":1,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103089,"chip_calls":1,"unwritten_words":0,"cache_hit":false},{"op":"rc_nested","regression":"R5","n":5,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102945,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":31,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102961,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":32,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102961,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":33,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103313,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":63,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103248,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":64,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103185,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":65,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103153,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":96,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102945,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":127,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103441,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":128,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103665,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":129,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102881,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":200,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102817,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":1000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103089,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":4096,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103152,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":8191,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103777,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":8192,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103025,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":8193,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":206258,"chip_calls":2,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":20000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":309651,"chip_calls":3,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":1,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103553,"chip_calls":1,"unwritten_words":0,"cache_hit":false},{"op":"rc_exit_bar","regression":"R6","n":5,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102785,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":31,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102801,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":32,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102801,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":33,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102801,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":63,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102849,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":64,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102929,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":65,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102993,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":96,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103505,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":127,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102865,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":128,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102768,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":129,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102753,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":200,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102785,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":1000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102801,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":4096,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102817,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":8191,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102848,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":8192,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103073,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":8193,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":206002,"chip_calls":2,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":20000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":310803,"chip_calls":3,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":1,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103265,"chip_calls":1,"unwritten_words":0,"cache_hit":false},{"op":"rc_depth16","regression":"B0..B15","n":5,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103009,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":31,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103697,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":32,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103105,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":33,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102929,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":63,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103088,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":64,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102801,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":65,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102865,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":96,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103521,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":127,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103217,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":128,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103169,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":129,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103233,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":200,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103233,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":1000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103713,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":4096,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103313,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":8191,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103313,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":8192,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103361,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":8193,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":206546,"chip_calls":2,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":20000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":310163,"chip_calls":3,"unwritten_words":0,"cache_hit":true}],"mutants":[{"op":"rc_diamond","mutant":1,"rc":"OK","violations":755,"caught":true},{"op":"rc_loopbreak","mutant":1,"rc":"OK","violations":377,"caught":true},{"op":"rc_nested","mutant":1,"rc":"OK","violations":110,"caught":true},{"op":"rc_exit_bar","mutant":1,"rc":"CODEGEN_FAIL","violations":1000,"caught":true}],"observations":[],"sim_model_n4096":[{"op":"rc_diamond","order":0,"warp_issues":4736,"lane_issues":133045,"splits":128,"max_frags":2,"peak_bars":1},{"op":"rc_diamond","order":1,"warp_issues":4736,"lane_issues":133096,"splits":128,"max_frags":2,"peak_bars":1},{"op":"rc_loopbreak","order":0,"warp_issues":115685,"lane_issues":713552,"splits":2889,"max_frags":28,"peak_bars":1},{"op":"rc_loopbreak","order":1,"warp_issues":116847,"lane_issues":728322,"splits":2876,"max_frags":2,"peak_bars":1},{"op":"rc_nested","order":0,"warp_issues":92819,"lane_issues":444474,"splits":1739,"max_frags":20,"peak_bars":2},{"op":"rc_nested","order":1,"warp_issues":91594,"lane_issues":433797,"splits":1694,"max_frags":2,"peak_bars":2},{"op":"rc_exit_bar","order":0,"warp_issues":5120,"lane_issues":157717,"splits":128,"max_frags":2,"peak_bars":1},{"op":"rc_exit_bar","order":1,"warp_issues":5120,"lane_issues":157638,"splits":128,"max_frags":2,"peak_bars":1},{"op":"rc_depth16","order":0,"warp_issues":13696,"lane_issues":280428,"splits":128,"max_frags":2,"peak_bars":16},{"op":"rc_depth16","order":1,"warp_issues":13696,"lane_issues":279136,"splits":128,"max_frags":2,"peak_bars":16}],"device_opens":1,"checks":220,"failed":0,"verdict":"OMEGA_GPU_RECONV_PASS"} diff --git a/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/run.log b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/run.log new file mode 100644 index 000000000..74fe665e8 --- /dev/null +++ b/evidence/gpu-reconv-308/T1a-GPU-RECONV-0a1812f/run.log @@ -0,0 +1,20 @@ +codegen rc_diamond: 37 insns, 14 gprs, 1 regions +codegen rc_loopbreak: 41 insns, 14 gprs, 1 regions +codegen rc_nested: 49 insns, 15 gprs, 2 regions +codegen rc_exit_bar: 40 insns, 18 gprs, 1 regions +codegen rc_depth16: 107 insns, 14 gprs, 16 regions +nvdisasm diamond: rc=0 insns=40 undecodable=0 missing=0 (/tmp/omega_rc_diamond_2695930.sass) +nvdisasm depth16: rc=0 insns=112 undecodable=0 missing=0 (/tmp/omega_rc_depth16_2695930.sass) +nvdisasm sieve_div2: rc=0 insns=72 undecodable=0 missing=0 (/tmp/omega_rc_sieve_div2_2695930.sass) +nvdisasm forms: rc=0 insns=4 undecodable=0 missing=0 (/tmp/omega_rc_forms_2695930.sass) +model rc_diamond n=4096: warp_issues=4736 lane_util=0.878 splits=128 max_frags=2 peak_bars=1 +model rc_diamond n=4096: warp_issues=4736 lane_util=0.878 splits=128 max_frags=2 peak_bars=1 +model rc_loopbreak n=4096: warp_issues=115685 lane_util=0.193 splits=2889 max_frags=28 peak_bars=1 +model rc_loopbreak n=4096: warp_issues=116847 lane_util=0.195 splits=2876 max_frags=2 peak_bars=1 +model rc_nested n=4096: warp_issues=92819 lane_util=0.150 splits=1739 max_frags=20 peak_bars=2 +model rc_nested n=4096: warp_issues=91594 lane_util=0.148 splits=1694 max_frags=2 peak_bars=2 +model rc_exit_bar n=4096: warp_issues=5120 lane_util=0.963 splits=128 max_frags=2 peak_bars=1 +model rc_exit_bar n=4096: warp_issues=5120 lane_util=0.962 splits=128 max_frags=2 peak_bars=1 +model rc_depth16 n=4096: warp_issues=13696 lane_util=0.640 splits=128 max_frags=2 peak_bars=16 +model rc_depth16 n=4096: warp_issues=13696 lane_util=0.637 splits=128 max_frags=2 peak_bars=16 +PASS: 220 checks, 0 failed diff --git a/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/SHA256SUMS b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/SHA256SUMS new file mode 100644 index 000000000..78b108a2b --- /dev/null +++ b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/SHA256SUMS @@ -0,0 +1,10 @@ +2455c607827def7a1cc5d96ce3759e8280bb254d6ed7e9409072faf9cc949a56 build.log +8ca14df583da96c66c4d7dc1a301c39e0a19747c1b0842fdee62a5e7e2cf1260 bw_warp_sim.h +cf1431b7c25bfa84e4cd760017ed8b47c989bc47111a3bee1eac939127f7c814 device.txt +0cfa2a3922fa89022a2a0b77734a09914ccb4aef9221b14a182bb436cbd6e744 gpu_reconv_test.c +ce6aa4077426ff76120276424103843f5e6641d312255df17b0ef91057ffefce omega_blackwell_codegen.c +0da10397dd4ee813633afa425192d126d056684fdd998585fea666995a7c50ea omega_bw_reconv.h +bd2346fb86eed5e3be35c49c970c25adf1621043d6e4e4ba02e990375e5859c7 omega_gpu_elementwise_api.c +4d9eb1b5fd55e168f0cd4a96e906957376d88bb5efbe1f08633fcdfa52ce8fba omega_gpu_elementwise_api.h +d5fd8db12085c3f0e498c4c0f0b93b5cda721d613579e6f5e06e883958461328 receipt.json +f904886390c48a1c546b423c9d2b278576e4a57191db24ea292c25c5b562595d run.log diff --git a/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/build.log b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/build.log new file mode 100644 index 000000000..8eab8a45c --- /dev/null +++ b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/build.log @@ -0,0 +1,2 @@ +ar rcs build/libomega_gpu.a build/sha256.o build/omega_canonical.o build/omega_validate.o build/omega_core.o build/omega_codec.o build/aarch64_encoder.o build/aarch64_decoder.o build/omega_realize.o build/omega_exec.o build/omega_self_host.o build/omega_verify.o build/omega_program.o build/omega_synthesis.o build/omega_library.o build/omega_discovery.o build/omega_machine.o build/omega_realize_synth.o build/omega_matvec.o build/omega_accelerator.o build/omega_accelerator_world.o build/omega_vector.o build/omega_blackwell_encoder.o build/omega_blackwell_qmd.o build/omega_blackwell_realize.o build/omega_blackwell_submit.o build/omega_blackwell_engine.o build/omega_gpu_engine.o build/omega_blackwell_matmul.o build/omega_blackwell_codegen.o build/omega_gpu_session.o build/omega_gpu_matmul_api.o build/omega_gpu_elementwise_api.o build/omega_gpu_attention_api.o build/omega_gpu_wait.o build/omega_evidence.o build/m16_native.o build/nvrm.o build/omega_program_ir.o build/omega_vc_bridge.o build/omega_resolve.o build/omega_receipt.o build/omega_blake3.o build/omega_vcstore.o +cc -std=gnu11 -Wall -Wextra -Werror -MMD -MP -D_GNU_SOURCE -O2 -Isrc -I../physics-reconv-lock/m16 -I../physics-reconv-lock/nvrm -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/src/common/sdk/nvidia/inc -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/kernel-open/common/inc -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/kernel-open/nvidia-uvm -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/src/nvidia/arch/nvalloc/unix/include -Itests -o build/gpu_reconv_test tests/gpu_reconv_test.c build/libomega_gpu.a -lpthread -lm diff --git a/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/bw_warp_sim.h b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/bw_warp_sim.h new file mode 100644 index 000000000..328094cb0 --- /dev/null +++ b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/bw_warp_sim.h @@ -0,0 +1,313 @@ +/* bw_warp_sim.h: host SIMT simulator for the Blackwell IR (omega #308). + * + * Models what the earlier per-thread simulators could not: a warp of 32 lanes with an + * active mask, divergence at predicated branches, structured reconvergence through the + * barrier registers (BSSY arms Bn for the lanes executing it and names the join; BSYNC + * parks arriving lanes until every armed lane that has not exited is there, then the + * warp continues as one), nested regions, and lane participation in SHFL and BAR.SYNC. + * + * A warp is a set of fragments (pc, lane mask). A predicated branch whose lanes disagree + * splits the fragment; the scheduler then runs one fragment at a time in a chosen order + * (lowest pc first or highest pc first; tests run both), which is the freedom the chip's + * scheduler also has. Fragments only merge at a BSYNC. Errors (all fatal, first one wins): + * WS_E_SHFL_SPLIT a SHFL reached by a fragment that is not the whole live warp + * (the chip reads inactive lanes: undefined; attention 2026-10-04) + * WS_E_SHFL_EXITED a SHFL on a warp some of whose lanes already exited (undefined too) + * WS_E_BAR_SPLIT (defined, never raised) a BAR.SYNC reached by a split warp is allowed: + * since Volta the CTA barrier counts arriving threads, not warps (CUDA + * programming guide, independent thread scheduling; INFERRED for sm_121, + * not chip-verified by us). The simulator parks each fragment, releases + * them together, counts the event in bar_split_arrivals, and leaves the + * fragments split: a barrier is not a reconvergence point, so a SHFL after + * it still fails with SHFL_SPLIT unless a BSYNC rejoined the warp. + * WS_E_BSYNC_UNARMED BSYNC on a barrier no BSSY armed + * WS_E_BSSY_REARM BSSY on a barrier still armed (lanes may still be heading to its join) + * WS_E_JOIN_MISMATCH BSYNC at an address other than the one the BSSY named minus one + * WS_E_NONMEMBER a lane reached BSYNC Bn without having executed its BSSY + * WS_E_STUCK lanes parked at a BSYNC that can never complete (a join was dropped, + * an exit jumped past it, or a target was corrupted) + * WS_E_NEVER_JOINED the warp finished with a barrier still armed + * WS_E_SELF_BRANCH, WS_E_PC, WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS + * Every error names the pc. Memory: global addresses are host pointers (optionally range + * checked); shared memory is a per-CTA buffer poisoned at CTA start so a read of a word + * nobody wrote shows up in the oracle comparison. + * + * Per-lane ALU semantics are those of the two earlier simulators (tests/gpu_attention_test.c, + * bench/prime_race/tests/gb10_sieve_host_test.c). EX2 and RCP are exact here; the chip's + * are approximate. LDCU64 (memory descriptor) is not modelled. */ +#ifndef BW_WARP_SIM_H +#define BW_WARP_SIM_H + +#include "omega_blackwell_codegen.h" +#include +#include +#include +#include +#include + +#define WS_WARP 32 +#define WS_MAX_THREADS 256 +#define WS_MAX_WARPS (WS_MAX_THREADS / WS_WARP) +#define WS_MAX_FRAGS 64 +#define WS_NBAR BW_RECONV_MAX_BAR +#define WS_MAX_RANGES 8 + +enum { + WS_OK = 0, WS_E_SHFL_SPLIT, WS_E_SHFL_EXITED, WS_E_BAR_SPLIT, WS_E_BSYNC_UNARMED, WS_E_BSSY_REARM, + WS_E_JOIN_MISMATCH, WS_E_NONMEMBER, WS_E_STUCK, WS_E_NEVER_JOINED, WS_E_SELF_BRANCH, WS_E_PC, + WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS, WS_E_BAR_TIMEOUT, WS_E_STEPS +}; +static const char *ws_err_name(int e) { + static const char *const n[] = { "OK", "SHFL_SPLIT", "SHFL_EXITED", "BAR_SPLIT", "BSYNC_UNARMED", "BSSY_REARM", + "JOIN_MISMATCH", "NONMEMBER", "STUCK", "NEVER_JOINED", "SELF_BRANCH", "PC", "OP", "SHIFT", "MEM", "SMEM", + "FRAGS", "BAR_TIMEOUT", "STEPS" }; + return (e >= 0 && e < (int)(sizeof n / sizeof n[0])) ? n[e] : "?"; +} + +typedef struct { uint32_t r[256]; uint8_t p0; } WsLane; +typedef struct { int pc; uint32_t mask; int parked; /* waiting at a BAR.SYNC */ } WsFrag; +typedef struct { int valid; int target; uint32_t members, arrived; } WsBar; +typedef struct { + WsLane lane[WS_WARP]; + WsFrag frag[WS_MAX_FRAGS]; + int nfrag; + WsBar bar[WS_NBAR]; + uint32_t exited; +} WsWarp; +typedef struct { uintptr_t lo, hi; } WsRange; + +typedef struct { + const BlackwellIRProgram *p; + uint32_t cbank[256]; /* constant bank 0 words (byte offset / 4) */ + uint32_t threads; /* per CTA, a multiple of 32, <= WS_MAX_THREADS */ + uint8_t *smem; /* per-CTA shared memory buffer (poisoned at CTA start) */ + uint32_t smem_bytes; + WsRange ranges[WS_MAX_RANGES]; int nranges; /* when > 0, every global access must fall in one */ + int order; /* 0 = lowest pc first, 1 = highest pc first */ + int trace; /* print every issue of warp 0 of CTA (0,0) */ + /* results */ + int err; int err_pc; char msg[200]; + uint64_t warp_issues; /* fragment-instruction issues (the chip's cost unit for divergence) */ + uint64_t lane_issues; /* lanes active over those issues */ + uint64_t splits; /* branches that split a fragment */ + uint64_t bar_split_arrivals; /* BAR.SYNC reached by a fragment that is not the whole live warp */ + int max_frags, peak_bars; +} WsSim; + +static void ws_fail(WsSim *s, int err, int pc, const char *what) { + if (s->err) return; + s->err = err; s->err_pc = pc; + snprintf(s->msg, sizeof s->msg, "%s at pc %d: %s", ws_err_name(err), pc, what ? what : ""); +} + +static uint32_t ws_rd(const WsLane *l, int v, const BlackwellIRProgram *p) { if (v < 0) return 0; int ph = p->regalloc.vreg_to_phys[v]; return ph < 0 ? 0 : l->r[ph & 0xff]; } +static uint64_t ws_rd64(const WsLane *l, int v, const BlackwellIRProgram *p) { if (v < 0) return 0; int ph = p->regalloc.vreg_to_phys[v] & 0xff; return (uint64_t)l->r[ph] | ((uint64_t)l->r[(ph + 1) & 0xff] << 32); } +static void ws_wr(WsLane *l, int v, uint32_t x, const BlackwellIRProgram *p) { if (v < 0) return; int ph = p->regalloc.vreg_to_phys[v]; if (ph >= 0) l->r[ph & 0xff] = x; } +static void ws_wr64(WsLane *l, int v, uint64_t x, const BlackwellIRProgram *p) { if (v < 0) return; int ph = p->regalloc.vreg_to_phys[v] & 0xff; l->r[ph] = (uint32_t)x; l->r[(ph + 1) & 0xff] = (uint32_t)(x >> 32); } +static float ws_u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } +static uint32_t ws_f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } +static uint32_t ws_lut3(uint32_t lut, uint32_t a, uint32_t b, uint32_t c) { + uint32_t r = 0; + for (int i = 0; i < 8; i++) if (lut >> i & 1) r |= ((i & 4) ? a : ~a) & ((i & 2) ? b : ~b) & ((i & 1) ? c : ~c); + return r; +} +static int ws_mem_ok(WsSim *s, uint64_t a, unsigned bytes, int pc) { + if (s->nranges == 0) return 1; + for (int i = 0; i < s->nranges; i++) if (a >= s->ranges[i].lo && a + bytes <= s->ranges[i].hi) return 1; + ws_fail(s, WS_E_MEM, pc, "global access outside the declared buffers"); return 0; +} +static uint32_t *ws_smem(WsSim *s, uint32_t a, int pc) { + if ((a & 3) || a + 4 > s->smem_bytes) { ws_fail(s, WS_E_SMEM, pc, "shared access past the declared size"); return NULL; } + return (uint32_t *)(s->smem + a); +} +static int ws_popc(uint32_t m) { return __builtin_popcount(m); } + +/* One lane, one non-control instruction. */ +static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc, uint32_t tid, uint32_t cx, uint32_t cy) { + const BlackwellIRProgram *p = s->p; + uint32_t a = ws_rd(l, in->src1_vreg, p), b = ws_rd(l, in->src2_vreg, p), c = ws_rd(l, in->src3_vreg, p); + switch (in->op) { + case BW_IR_NOP: break; + case BW_IR_S2R: ws_wr(l, in->dst_vreg, in->imm == BW_SR_TID_X ? tid : in->imm == BW_SR_CTAID_X ? cx : in->imm == BW_SR_CTAID_Y ? cy : 0, p); break; + case BW_IR_LDC: ws_wr(l, in->dst_vreg, s->cbank[(in->imm / 4) & 0xff], p); break; + case BW_IR_LDC64: ws_wr64(l, in->dst_vreg, (uint64_t)s->cbank[(in->imm / 4) & 0xff] | ((uint64_t)s->cbank[(in->imm / 4 + 1) & 0xff] << 32), p); break; + case BW_IR_LDCU64: case BW_IR_LDCU: break; /* memory descriptor: not modelled */ + case BW_IR_MOV_IMM: ws_wr(l, in->dst_vreg, in->imm, p); break; + case BW_IR_MOV_RZ: ws_wr(l, in->dst_vreg, 0, p); break; + case BW_IR_IMAD: ws_wr(l, in->dst_vreg, in->src2_vreg >= 0 ? a * b + c : a * in->imm + c, p); break; + case BW_IR_IMAD_WIDE: ws_wr64(l, in->dst_vreg, (uint64_t)a * in->imm + ws_rd64(l, in->src3_vreg, p), p); break; + case BW_IR_IMAD_HI_U32: ws_wr(l, in->dst_vreg, (uint32_t)(((uint64_t)a * b) >> 32) + c, p); break; + case BW_IR_IADD3: ws_wr(l, in->dst_vreg, a + b, p); break; /* the encoder's IADD3 has Rc = RZ */ + case BW_IR_SHF_R: ws_wr(l, in->dst_vreg, a >> (in->imm & 31), p); break; + case BW_IR_SHF_L_U32: if (b >= 32) { ws_fail(s, WS_E_SHIFT, pc, "SHF.L.U32 amount >= 32 (not chip-verified)"); return; } ws_wr(l, in->dst_vreg, a << b, p); break; + case BW_IR_LOP3_AND: ws_wr(l, in->dst_vreg, a & in->imm, p); break; + case BW_IR_LOP3_XOR: ws_wr(l, in->dst_vreg, a ^ b, p); break; + case BW_IR_LOP3_LUT: ws_wr(l, in->dst_vreg, ws_lut3(in->imm & 0xff, a, b, c), p); break; + case BW_IR_ISETP_GE_U32: l->p0 = a >= b; break; + case BW_IR_LDG_E: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; uint32_t x; memcpy(&x, (const void *)(uintptr_t)ad, 4); ws_wr(l, in->dst_vreg, x, p); break; } + case BW_IR_LDG_E_U16: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 2, pc)) return; uint16_t x; memcpy(&x, (const void *)(uintptr_t)ad, 2); ws_wr(l, in->dst_vreg, x, p); break; } + case BW_IR_STG_E: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; memcpy((void *)(uintptr_t)ad, &b, 4); break; } + case BW_IR_LDS32: { uint32_t *m = ws_smem(s, a, pc); if (!m) return; ws_wr(l, in->dst_vreg, *m, p); break; } + case BW_IR_STS32: { uint32_t *m = ws_smem(s, a, pc); if (!m) return; *m = b; break; } + case BW_IR_FADD: ws_wr(l, in->dst_vreg, ws_f2u(ws_u2f(a) + ws_u2f(b)), p); break; + case BW_IR_FSUB: ws_wr(l, in->dst_vreg, ws_f2u(ws_u2f(a) - ws_u2f(b)), p); break; + case BW_IR_FMUL: ws_wr(l, in->dst_vreg, ws_f2u(ws_u2f(a) * ws_u2f(b)), p); break; + case BW_IR_FFMA: ws_wr(l, in->dst_vreg, ws_f2u(fmaf(ws_u2f(a), ws_u2f(b), ws_u2f(c))), p); break; + case BW_IR_FMNMX_MAX: ws_wr(l, in->dst_vreg, ws_f2u(fmaxf(ws_u2f(a), ws_u2f(b))), p); break; + case BW_IR_FMNMX_MIN: ws_wr(l, in->dst_vreg, ws_f2u(fminf(ws_u2f(a), ws_u2f(b))), p); break; + case BW_IR_MUFU_EX2: ws_wr(l, in->dst_vreg, ws_f2u(exp2f(ws_u2f(a))), p); break; + case BW_IR_MUFU_RCP: ws_wr(l, in->dst_vreg, ws_f2u(1.0f / ws_u2f(a)), p); break; + case BW_IR_MUFU_RSQ: ws_wr(l, in->dst_vreg, ws_f2u(1.0f / sqrtf(ws_u2f(a))), p); break; + default: ws_fail(s, WS_E_OP, pc, "opcode not modelled"); return; + } +} + +static int ws_pick(const WsSim *s, const WsWarp *w) { + int best = -1; + for (int i = 0; i < w->nfrag; i++) { + if (w->frag[i].mask == 0 || w->frag[i].parked) continue; + if (best < 0) { best = i; continue; } + if (s->order == 0 ? w->frag[i].pc < w->frag[best].pc : w->frag[i].pc > w->frag[best].pc) best = i; + } + return best; +} +static void ws_drop_frag(WsWarp *w, int i) { w->frag[i] = w->frag[--w->nfrag]; } +static int ws_add_frag(WsSim *s, WsWarp *w, int pc, uint32_t mask) { + if (mask == 0) return 0; + if (w->nfrag >= WS_MAX_FRAGS) { ws_fail(s, WS_E_FRAGS, pc, "too many fragments"); return -1; } + w->frag[w->nfrag].pc = pc; w->frag[w->nfrag].mask = mask; w->frag[w->nfrag].parked = 0; w->nfrag++; + if (w->nfrag > s->max_frags) s->max_frags = w->nfrag; + return 0; +} + +/* Runs one warp until it has no runnable fragment (finished, parked at BAR.SYNC, or stuck). + * Returns 1 when parked at BAR.SYNC, 0 when finished, -1 on error. */ +static int ws_run_warp(WsSim *s, WsWarp *w, uint32_t warp, uint32_t cx, uint32_t cy, int trace) { + const BlackwellIRProgram *p = s->p; + const uint32_t live_all = 0xffffffffu; + for (uint64_t guard = 0;; guard++) { + if (s->err) return -1; + if (guard > 2000000000ull) { ws_fail(s, WS_E_STEPS, -1, "step budget"); return -1; } + int fi = ws_pick(s, w); + if (fi < 0) { + /* nothing runnable: parked at the CTA barrier, finished, or lanes waiting at a BSYNC + * that can never complete */ + for (int i = 0; i < w->nfrag; i++) if (w->frag[i].parked) return 1; + for (int b = 0; b < WS_NBAR; b++) if (w->bar[b].valid) { + if (w->bar[b].arrived) { ws_fail(s, WS_E_STUCK, w->bar[b].target - 1, "lanes parked at BSYNC, the rest never arrive"); return -1; } + ws_fail(s, WS_E_NEVER_JOINED, w->bar[b].target - 1, "warp finished with a barrier still armed"); return -1; + } + return 0; + } + WsFrag *f = &w->frag[fi]; + int pc = f->pc; + if (pc < 0 || (size_t)pc >= p->count) { ws_fail(s, WS_E_PC, pc, "pc outside the program"); return -1; } + const BlackwellIRInsn *in = &p->insns[pc]; + uint32_t mask = f->mask; + s->warp_issues++; s->lane_issues += (uint64_t)ws_popc(mask); + if (trace) printf("W%u pc=%d op=%d mask=%08x frags=%d\n", warp, pc, (int)in->op, mask, w->nfrag); + uint32_t live = live_all & ~w->exited; + uint32_t pmask = 0; + for (int L = 0; L < WS_WARP; L++) if ((mask >> L & 1) && w->lane[L].p0) pmask |= 1u << L; + uint32_t cond = in->predicate_p0 ? (in->predicate_not ? (mask & ~pmask) : (mask & pmask)) : mask; + switch (in->op) { + case BW_IR_EXIT: { + w->exited |= cond; + f->mask = mask & ~cond; + if (f->mask == 0) ws_drop_frag(w, fi); else f->pc = pc + 1; + break; + } + case BW_IR_BRA: { + int32_t delta = (int32_t)in->imm; + uint32_t taken = cond, fall = mask & ~cond; + if (taken && delta == 0) { ws_fail(s, WS_E_SELF_BRANCH, pc, "branch to itself"); return -1; } + if (taken && fall) s->splits++; + if (taken == 0) { f->pc = pc + 1; break; } + if (fall == 0) { f->pc = pc + delta; break; } + f->mask = fall; f->pc = pc + 1; + if (ws_add_frag(s, w, pc + delta, taken) < 0) return -1; + break; + } + case BW_IR_BSSY: { + WsBar *b = &w->bar[in->bar_reg & (WS_NBAR - 1)]; + if (in->bar_reg >= WS_NBAR) { ws_fail(s, WS_E_OP, pc, "barrier id"); return -1; } + if (b->valid) { ws_fail(s, WS_E_BSSY_REARM, pc, "BSSY on an armed barrier"); return -1; } + b->valid = 1; b->target = pc + (int32_t)in->imm; b->members = cond; b->arrived = 0; + int armed = 0; for (int k = 0; k < WS_NBAR; k++) armed += w->bar[k].valid; + if (armed > s->peak_bars) s->peak_bars = armed; + f->pc = pc + 1; + break; + } + case BW_IR_BSYNC: { + if (in->bar_reg >= WS_NBAR) { ws_fail(s, WS_E_OP, pc, "barrier id"); return -1; } + WsBar *b = &w->bar[in->bar_reg]; + if (!b->valid) { ws_fail(s, WS_E_BSYNC_UNARMED, pc, "BSYNC without a BSSY"); return -1; } + if (cond & ~b->members) { ws_fail(s, WS_E_NONMEMBER, pc, "a lane at BSYNC did not execute the BSSY"); return -1; } + b->arrived |= cond; + uint32_t rest = mask & ~cond; /* a predicated-off lane walks on */ + if (rest) { f->mask = rest; f->pc = pc + 1; } else ws_drop_frag(w, fi); + if (b->arrived == (b->members & ~w->exited)) { + if (b->target != pc + 1) { ws_fail(s, WS_E_JOIN_MISMATCH, pc, "BSSY named a different join point"); return -1; } + uint32_t joined = b->arrived; b->valid = 0; b->arrived = 0; b->members = 0; + if (ws_add_frag(s, w, pc + 1, joined) < 0) return -1; + } + break; + } + case BW_IR_SHFL_DOWN: { + if (w->exited) { ws_fail(s, WS_E_SHFL_EXITED, pc, "SHFL on a warp with exited lanes"); return -1; } + if (mask != live) { ws_fail(s, WS_E_SHFL_SPLIT, pc, "SHFL on a split warp"); return -1; } + uint32_t src[WS_WARP]; + for (int L = 0; L < WS_WARP; L++) src[L] = ws_rd(&w->lane[L], in->src1_vreg, p); + for (int L = 0; L < WS_WARP; L++) { int from = L + (int)(in->imm & 31); ws_wr(&w->lane[L], in->dst_vreg, from < WS_WARP ? src[from] : src[L], p); } + f->pc = pc + 1; + break; + } + case BW_IR_BAR_SYNC: { + if (mask != live) s->bar_split_arrivals++; + f->parked = 1; /* released by ws_run when every live lane of the CTA has arrived */ + break; + } + default: { + for (int L = 0; L < WS_WARP; L++) if (mask >> L & 1) { + ws_lane_exec(s, &w->lane[L], in, pc, warp * WS_WARP + (uint32_t)L, cx, cy); + if (s->err) return -1; + } + f->pc = pc + 1; + break; + } + } + } +} + +/* Runs every CTA of the grid. Returns s->err (0 = clean). */ +static int ws_run(WsSim *s, uint32_t grid_x, uint32_t grid_y) { + s->err = 0; s->msg[0] = 0; s->warp_issues = s->lane_issues = s->splits = s->bar_split_arrivals = 0; s->max_frags = 0; s->peak_bars = 0; + if (!s->p || s->threads == 0 || s->threads % WS_WARP || s->threads > WS_MAX_THREADS) { ws_fail(s, WS_E_OP, -1, "bad simulator configuration"); return s->err; } + static WsWarp warps[WS_MAX_WARPS]; + uint32_t nw = s->threads / WS_WARP; + for (uint32_t cy = 0; cy < grid_y && !s->err; cy++) for (uint32_t cx = 0; cx < grid_x && !s->err; cx++) { + memset(warps, 0, sizeof(WsWarp) * nw); + if (s->smem && s->smem_bytes) for (uint32_t i = 0; i + 4 <= s->smem_bytes; i += 4) { uint32_t poison = 0xfeedfaceu; memcpy(s->smem + i, &poison, 4); } + for (uint32_t w = 0; w < nw; w++) { warps[w].nfrag = 1; warps[w].frag[0].pc = 0; warps[w].frag[0].mask = 0xffffffffu; warps[w].frag[0].parked = 0; } + int trace = s->trace && cx == 0 && cy == 0; + /* Barrier phases: every warp runs until all of its fragments are parked at BAR.SYNC or it + * has finished (exited lanes do not hold the barrier up: INFERRED, see WS_E_BAR_SPLIT). + * Then every parked fragment is released and the next phase starts. */ + for (int phase = 0; ; phase++) { + int parked = 0; + for (uint32_t w = 0; w < nw && !s->err; w++) { + if (warps[w].nfrag == 0) continue; + int r = ws_run_warp(s, &warps[w], w, cx, cy, trace && w == 0); + if (r < 0) break; + if (r == 1) parked++; + } + if (s->err || parked == 0) break; /* error, or every warp finished */ + for (uint32_t w = 0; w < nw; w++) for (int i = 0; i < warps[w].nfrag; i++) + if (warps[w].frag[i].parked) { warps[w].frag[i].parked = 0; warps[w].frag[i].pc++; } + if (phase > 100000) { ws_fail(s, WS_E_BAR_TIMEOUT, -1, "barrier phases"); break; } + } + } + return s->err; +} + +#endif /* BW_WARP_SIM_H */ diff --git a/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/device.txt b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/device.txt new file mode 100644 index 000000000..135a7deb6 --- /dev/null +++ b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/device.txt @@ -0,0 +1,5 @@ +Linux spark-b87b 7.0.0-1019-nvidia #19~24.04.2-Ubuntu SMP PREEMPT_DYNAMIC Sun Sep 6 18:35:11 UTC 2 aarch64 aarch64 aarch64 GNU/Linux +NVIDIA GB10, 580.173.02 +omega 0a1812f44eaa3dcf8fe5a914d1dd65cfaccbbcd7 (1 uncommitted paths) +physics 6d7cf0d4d8eb2cda7b512100ff6058e25dbb3ddf +2026-10-05T23:14:41Z diff --git a/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/gpu_reconv_test.c b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/gpu_reconv_test.c new file mode 100644 index 000000000..227e3b658 --- /dev/null +++ b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/gpu_reconv_test.c @@ -0,0 +1,470 @@ +/* gpu_reconv_test: structured warp reconvergence (omega #308) on Omega's own Blackwell codegen. + * + * ./gpu_reconv_test --host-only encoder golden words, region bookkeeping, codegen refusals, + * nvdisasm listing (when /usr/local/cuda/bin/nvdisasm exists), + * every probe kernel through the SIMT warp simulator against + * the host oracle under both fragment orders, the mutation + * controls, scoreboard and control-edge walks. No device. + * ./gpu_reconv_test --out receipt.json chip gate: host part first (a kernel the simulator rejects + * never gets chip time), then every probe on the chip against + * the oracle, bit-exact, then each math mutant must be caught. + * --observe-dropped-join chip only, optional: also launch the dropped-BSYNC mutant of + * one probe and record what the chip did (an observation, not + * a gate; the simulator rejects that kernel first). + * + * Probes (src/omega_gpu_elementwise_api.c, gen_rc_*): R3 diamond, R4 loop break, R5 nested, + * R6 lanes past the end with STS/BAR/LDS + SHFL, and the 16-deep barrier-register bound. + * Oracle: rc_oracle below; every comparison is exact (u32). Launcher chunking (64 CTAs x 128 + * threads per launch, P0 = words in the chunk) is part of the oracle, so a clamp to the wrong + * chunk end shows up. */ +#include "omega_gpu_elementwise_api.h" +#include "omega_blackwell_codegen.h" +#include "omega_bw_reconv.h" +#include "omega_gpu_session.h" +#include "bw_warp_sim.h" +#include +#include +#include +#include +#include + +static int g_checks, g_failed; +#define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) + +static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } + +typedef struct { OmegaGpuEwOp op; const char *name; const char *reg; } Probe; +static const Probe PROBES[] = { + { OMEGA_GPU_EW_RC_DIAMOND, "rc_diamond", "R3" }, + { OMEGA_GPU_EW_RC_LOOPBREAK, "rc_loopbreak", "R4" }, + { OMEGA_GPU_EW_RC_NESTED, "rc_nested", "R5" }, + { OMEGA_GPU_EW_RC_EXIT_BAR, "rc_exit_bar", "R6" }, + { OMEGA_GPU_EW_RC_DEPTH16, "rc_depth16", "B0..B15" }, +}; +#define NPROBES (sizeof PROBES / sizeof PROBES[0]) +static const uint32_t NS[] = { 1, 5, 31, 32, 33, 63, 64, 65, 96, 127, 128, 129, 200, 1000, 4096, 8191, 8192, 8193, 20000 }; +#define NNS (sizeof NS / sizeof NS[0]) +#define CHUNK (OMEGA_GPU_EW_MAX_CTAS * OMEGA_GPU_EW_THREADS) + +/* ----------------------------------------------------------------- oracle */ +static uint32_t count_loop(uint32_t x, uint32_t y, int math, uint32_t *v_out) { + uint32_t v = x & 255u, st = (y & 15u) + 1u, cnt = 0, bound = math ? 255u : 256u; + while (v < bound) { v += st; cnt++; } + *v_out = v; return cnt; +} +static uint32_t rc_r(OmegaGpuEwOp op, int math, uint32_t x, uint32_t y, uint32_t partner_x) { + uint32_t v, cnt; + switch (op) { + case OMEGA_GPU_EW_RC_DIAMOND: + if (x >= y) { uint32_t t = 3u * x + y, u = t ^ x, r = 5u * u + 1u; r += r >> 3; return math ? r + 1u : r; } + return x + 1u; + case OMEGA_GPU_EW_RC_LOOPBREAK: cnt = count_loop(x, y, math, &v); return cnt * 256u + v; + case OMEGA_GPU_EW_RC_NESTED: if (x & 1u) return 7u; cnt = count_loop(x, y, math, &v); return cnt * 256u + v + 100u; + case OMEGA_GPU_EW_RC_EXIT_BAR: return 3u * x + (partner_x | 1u); + case OMEGA_GPU_EW_RC_DEPTH16: return x + (x < y ? (uint32_t)BW_RECONV_MAX_BAR : 0u); + default: return 0xdeadbeefu; + } +} +static void rc_oracle(OmegaGpuEwOp op, int math, uint32_t n, const uint32_t *a, const uint32_t *b, uint32_t *out) { + for (uint32_t i0 = 0; i0 < n; i0 += CHUNK) { + uint32_t ni = n - i0 < CHUNK ? n - i0 : CHUNK; + uint32_t threads = ((ni + OMEGA_GPU_EW_THREADS - 1) / OMEGA_GPU_EW_THREADS) * OMEGA_GPU_EW_THREADS; + uint32_t *r = malloc((size_t)threads * 4); + for (uint32_t t = 0; t < threads; t++) { + uint32_t idx = t < ni ? t : ni - 1, pt = t ^ 1u, pidx = pt < ni ? pt : ni - 1; + r[t] = rc_r(op, math, a[i0 + idx], b[i0 + idx], a[i0 + pidx]); + } + for (uint32_t i = 0; i < ni; i++) { uint32_t nb = (i & 31u) == 31u ? i : i + 1u; out[i0 + i] = r[i] + 4099u * r[nb]; } + free(r); + } +} +/* An LCG's low bits cycle quickly (bit 0 has period 2), and the probes key on low bits + * (x & 255, y & 15, x & 1), so each draw goes through a mixing step. */ +static uint32_t mix(uint32_t r) { r ^= r >> 16; r *= 0x45d9f3bu; r ^= r >> 16; r *= 0x45d9f3bu; r ^= r >> 16; return r; } +static void fill(uint32_t *a, uint32_t *b, uint32_t n, uint32_t seed) { + for (uint32_t i = 0; i < n; i++) { a[i] = mix(lcg(&seed)); b[i] = mix(lcg(&seed)); } +} +static size_t diff_count(const uint32_t *got, const uint32_t *want, uint32_t n, uint32_t *first) { + size_t bad = 0; *first = n; + for (uint32_t i = 0; i < n; i++) if (got[i] != want[i]) { if (!bad) *first = i; bad++; } + return bad; +} + +/* --------------------------------------------------------- simulator driver */ +static uint8_t g_smem[1024]; +/* Mirrors run_1d: 64 CTAs x 128 threads per launch, P0 = words in the chunk. Returns the + * simulator's error code (0 clean); the output words are written to out. */ +static int ws_probe(const BlackwellIRProgram *p, uint32_t n, const uint32_t *a, const uint32_t *b, uint32_t *out, int order, WsSim *stats) { + for (uint32_t i = 0; i < n; i++) out[i] = 0xffbadbadu; + uint64_t wi = 0, li = 0, sp = 0; int mf = 0, pb = 0; + for (uint32_t i0 = 0; i0 < n; i0 += CHUNK) { + uint32_t ni = n - i0 < CHUNK ? n - i0 : CHUNK; + WsSim s; memset(&s, 0, sizeof s); + s.p = p; s.threads = OMEGA_GPU_EW_THREADS; s.smem = g_smem; s.smem_bytes = sizeof g_smem; s.order = order; + uint64_t aa = (uintptr_t)(a + i0), ba = (uintptr_t)(b + i0), ca = (uintptr_t)(out + i0); + s.cbank[0x380 / 4] = (uint32_t)aa; s.cbank[0x380 / 4 + 1] = (uint32_t)(aa >> 32); + s.cbank[0x388 / 4] = (uint32_t)ba; s.cbank[0x388 / 4 + 1] = (uint32_t)(ba >> 32); + s.cbank[0x390 / 4] = (uint32_t)ca; s.cbank[0x390 / 4 + 1] = (uint32_t)(ca >> 32); + s.cbank[0x398 / 4] = ni; + s.ranges[0] = (WsRange){ (uintptr_t)aa, (uintptr_t)aa + (size_t)ni * 4 }; + s.ranges[1] = (WsRange){ (uintptr_t)ba, (uintptr_t)ba + (size_t)ni * 4 }; + s.ranges[2] = (WsRange){ (uintptr_t)ca, (uintptr_t)ca + (size_t)ni * 4 }; + s.nranges = 3; + int err = ws_run(&s, (ni + OMEGA_GPU_EW_THREADS - 1) / OMEGA_GPU_EW_THREADS, 1); + wi += s.warp_issues; li += s.lane_issues; sp += s.splits; + if (s.max_frags > mf) mf = s.max_frags; + if (s.peak_bars > pb) pb = s.peak_bars; + if (err) { if (stats) *stats = s; return err; } + } + if (stats) { memset(stats, 0, sizeof *stats); stats->warp_issues = wi; stats->lane_issues = li; stats->splits = sp; stats->max_frags = mf; stats->peak_bars = pb; } + return 0; +} +static int codegen(OmegaGpuEwOp op, int mutant, BlackwellIRProgram *p, OmegaBlackwellKernel *k) { + memset(k, 0, sizeof *k); + return omega_gpu_elementwise_codegen_ir(op, mutant, p, k); +} +static int is_reconv_err(int e) { + return e == WS_E_SHFL_SPLIT || e == WS_E_BAR_SPLIT || e == WS_E_STUCK || e == WS_E_NEVER_JOINED || e == WS_E_JOIN_MISMATCH || + e == WS_E_BSYNC_UNARMED || e == WS_E_BSSY_REARM || e == WS_E_NONMEMBER || e == WS_E_SHFL_EXITED; +} + +/* --------------------------------------------------------- static walks */ +/* Scoreboard walk (as bench/prime_race/tests/gb10_sieve_host_test.c sb_hazards): a destination + * written under write scoreboard 0 is in flight until a wait mask that includes it. */ +static int sb_hazards(const BlackwellIRProgram *p) { + int inflight[64], n = 0, bad = 0; + for (size_t i = 0; i < p->count; i++) { + const BlackwellIRInsn *in = &p->insns[i]; + uint32_t c = in->control; + if ((c >> 20) & 1u) n = 0; + const int src[3] = { in->src1_vreg, in->src2_vreg, in->src3_vreg }; + for (int s = 0; s < 3; s++) for (int k = 0; src[s] >= 0 && k < n; k++) if (inflight[k] == src[s]) bad++; + if (c != 0 && ((c >> 14) & 7u) == 0 && in->dst_vreg >= 0 && n < 64) inflight[n++] = in->dst_vreg; + } + return bad; +} +/* Control-edge walk: the straight-line scoreboard reasoning above holds only if nothing is in + * flight across a branch, barrier op or exit. Every BRA / EXIT / BSSY / BSYNC must wait on every + * scoreboard set since the last wait. Returns the number of control ops that do not. */ +static int edge_waits(const BlackwellIRProgram *p) { + uint32_t pending = 0; int bad = 0; + for (size_t i = 0; i < p->count; i++) { + const BlackwellIRInsn *in = &p->insns[i]; + uint32_t c = in->control, wait = (c >> 20) & 63u, wb = (c >> 14) & 7u, rb = (c >> 17) & 7u; + pending &= ~wait; + if (in->op == BW_IR_BRA || in->op == BW_IR_EXIT || in->op == BW_IR_BSSY || in->op == BW_IR_BSYNC) { if (pending) bad++; } + if (wb != 7u) pending |= 1u << wb; + if (rb != 7u) pending |= 1u << rb; + } + return bad; +} +static int count_op(const BlackwellIRProgram *p, BlackwellIROpcode op, int bar) { + int n = 0; + for (size_t i = 0; i < p->count; i++) if (p->insns[i].op == op && (bar < 0 || p->insns[i].bar_reg == bar)) n++; + return n; +} +static int find_op(const BlackwellIRProgram *p, BlackwellIROpcode op, int nth) { + for (size_t i = 0; i < p->count; i++) if (p->insns[i].op == op && nth-- == 0) return (int)i; + return -1; +} + +/* --------------------------------------------------------- nvdisasm listing */ +static int nvdisasm_bytes(const uint8_t *code, size_t bytes, const char *name, const char *const *must, int n_must) { + const char *nv = "/usr/local/cuda/bin/nvdisasm"; + if (access(nv, X_OK) != 0) { printf("nvdisasm: not present, listing check skipped for %s\n", name); return 0; } + char bin[96], lst[96], cmd[320]; + snprintf(bin, sizeof bin, "/tmp/omega_rc_%s_%d.bin", name, (int)getpid()); + snprintf(lst, sizeof lst, "/tmp/omega_rc_%s_%d.sass", name, (int)getpid()); + FILE *f = fopen(bin, "wb"); if (!f) return -1; + fwrite(code, 1, bytes, f); fclose(f); + snprintf(cmd, sizeof cmd, "%s -b SM121 %s > %s 2>&1", nv, bin, lst); + int rc = system(cmd); + FILE *l = fopen(lst, "r"); if (!l) return -1; + char line[512]; int seen[16] = {0}, bad = 0, insns = 0; + while (fgets(line, sizeof line, l)) { + if (!strstr(line, "/*0")) continue; + insns++; + if (strstr(line, "?") || strstr(line, "error")) bad++; + for (int i = 0; i < n_must && i < 16; i++) if (strstr(line, must[i])) seen[i] = 1; + } + fclose(l); + int miss = 0; + for (int i = 0; i < n_must && i < 16; i++) if (!seen[i]) { printf(" %s: nvdisasm listing lacks %s\n", name, must[i]); miss++; } + printf("nvdisasm %s: rc=%d insns=%d undecodable=%d missing=%d (%s)\n", name, rc, insns, bad, miss, lst); + unlink(bin); + return (rc == 0 && bad == 0 && miss == 0) ? 0 : -1; +} +static int nvdisasm_kernel(OmegaGpuEwOp op, int mutant, const char *name, const char *const *must, int n_must) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + if (!p || codegen(op, mutant, p, &k) != OMEGA_GPU_EW_OK) { free(p); return -1; } + int rc = nvdisasm_bytes(k.code, k.code_size, name, must, n_must); + free(k.code); free(p); + return rc; +} + +/* --------------------------------------------------------- host part */ +typedef struct { const char *name; uint64_t warp_issues, lane_issues, splits; int max_frags, peak_bars; } Model; +static Model g_model[NPROBES * 2]; static int g_nmodel; + +static int host_part(void) { + int before = g_failed; + /* 1. encoder golden words (nvcc 13.0.88 -arch=sm_121 oracle, docs/gpu-reconvergence-308.md) */ + int fx = omega_blackwell_verify_codegen_fixtures_reconv(); + CHECK(fx == 0, "encoder golden words BSSY / BSYNC and refusals (rc %d)", fx); + + /* 2. region bookkeeping on a hand-built program */ + { + BlackwellIRProgram *p = calloc(1, sizeof *p); BwRegions r; memset(&r, 0, sizeof r); + omega_bw_ir_init(p); + BlackwellIRInsn in; memset(&in, 0, sizeof in); in.dst_vreg = in.src1_vreg = in.src2_vreg = in.src3_vreg = -1; + in.op = BW_IR_BSSY; in.bar_reg = 0; in.imm = 2; omega_bw_ir_append(p, &in); /* 0 */ + CHECK(bw_regions_next_bar(&r) == 0 && bw_regions_begin(&r, 0) == 0, "region begin returns B0"); + in.op = BW_IR_BRA; in.imm = 1; in.predicate_p0 = true; omega_bw_ir_append(p, &in); /* 1 */ + CHECK(bw_regions_exit(&r, 1) == 0, "region exit recorded"); + in.op = BW_IR_NOP; in.predicate_p0 = false; omega_bw_ir_append(p, &in); /* 2 */ + in.op = BW_IR_BSYNC; in.bar_reg = 0; omega_bw_ir_append(p, &in); /* 3 */ + CHECK(bw_regions_join(&r, p, 3) == 0, "region join returns B0"); + CHECK(p->insns[0].imm == 4 && p->insns[1].imm == 2, "join patched BSSY to the insn after BSYNC (%u) and the exit to the BSYNC (%u)", p->insns[0].imm, p->insns[1].imm); + CHECK(bw_regions_join(&r, p, 3) == -1, "join with no open region refused"); + CHECK(bw_regions_exit(&r, 1) == -1, "exit with no open region refused"); + /* exits table full */ + memset(&r, 0, sizeof r); bw_regions_begin(&r, 0); + int ok = 1; for (int i = 0; i < BW_RECONV_MAX_EXITS; i++) ok &= bw_regions_exit(&r, 1) == 0; + CHECK(ok && bw_regions_exit(&r, 1) == -1, "exit table bound (%d) fails closed", BW_RECONV_MAX_EXITS); + /* join index inconsistent: BSYNC before BSSY, wrong barrier */ + memset(&r, 0, sizeof r); bw_regions_begin(&r, 3); + CHECK(bw_regions_join(&r, p, 0) == -1, "join with BSSY after BSYNC refused"); + memset(&r, 0, sizeof r); bw_regions_begin(&r, 0); p->insns[3].bar_reg = 1; + CHECK(bw_regions_join(&r, p, 3) == -1, "join with the wrong barrier register refused"); + p->insns[3].bar_reg = 0; + /* depth bound */ + memset(&r, 0, sizeof r); ok = 1; + for (int d = 0; d < BW_RECONV_MAX_DEPTH; d++) ok &= bw_regions_next_bar(&r) == d && bw_regions_begin(&r, d) == d; + CHECK(ok && bw_regions_next_bar(&r) == -1 && bw_regions_begin(&r, 99) == -1, "nesting bound %d: the next begin fails closed", BW_RECONV_MAX_DEPTH); + free(p); + } + + /* 3. codegen: production probes build, refusals refuse, mutants differ */ + static const struct { OmegaGpuEwOp op; const char *name; } refuse[] = { + { OMEGA_GPU_EW_RC_REFUSE_DEPTH17, "17 nested regions" }, { OMEGA_GPU_EW_RC_REFUSE_EXIT_SHFL, "EXIT then BAR/SHFL" }, + { OMEGA_GPU_EW_RC_REFUSE_EXIT_IN_REGION, "EXIT inside a region" }, { OMEGA_GPU_EW_RC_REFUSE_BAR_IN_REGION, "BAR.SYNC inside a region" }, + { OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION, "SHFL inside a region" }, { OMEGA_GPU_EW_RC_REFUSE_UNCLOSED, "region never joined" }, + { OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY, "BSYNC with no BSSY" }, + }; + for (size_t i = 0; i < sizeof refuse / sizeof refuse[0]; i++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + int rc = codegen(refuse[i].op, 0, p, &k); + CHECK(rc == OMEGA_GPU_EW_CODEGEN_FAIL, "%s refused at codegen (rc %s)", refuse[i].name, omega_gpu_elementwise_rc_name(rc)); + if (rc == OMEGA_GPU_EW_OK) free(k.code); + free(p); + } + for (size_t i = 0; i < NPROBES; i++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + int rc = codegen(PROBES[i].op, 0, p, &k); + CHECK(rc == OMEGA_GPU_EW_OK && k.gpr_count <= 64, "%s builds (rc %s, %zu insns, %u gprs)", PROBES[i].name, omega_gpu_elementwise_rc_name(rc), k.insn_count, k.gpr_count); + if (rc != OMEGA_GPU_EW_OK) { free(p); continue; } + int nb = count_op(p, BW_IR_BSSY, -1), ns = count_op(p, BW_IR_BSYNC, -1); + int want = PROBES[i].op == OMEGA_GPU_EW_RC_NESTED ? 2 : PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16 ? BW_RECONV_MAX_BAR : 1; + CHECK(nb == want && ns == want, "%s has %d BSSY and %d BSYNC (want %d)", PROBES[i].name, nb, ns, want); + if (PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16) { + int each = 1; for (int b = 0; b < BW_RECONV_MAX_BAR; b++) each &= count_op(p, BW_IR_BSSY, b) == 1 && count_op(p, BW_IR_BSYNC, b) == 1; + CHECK(each, "depth16 uses every barrier register B0..B15 exactly once"); + } + CHECK(sb_hazards(p) == 0, "%s scoreboard walk clean", PROBES[i].name); + CHECK(edge_waits(p) == 0, "%s every control op waits on everything in flight", PROBES[i].name); + printf("codegen %s: %zu insns, %u gprs, %d regions\n", PROBES[i].name, p->count, k.gpr_count, nb); + uint8_t d0[32]; memcpy(d0, k.code_digest, 32); free(k.code); + for (int m = 1; m <= 3; m++) { + BlackwellIRProgram *mp = calloc(1, sizeof *mp); OmegaBlackwellKernel mk; + int mrc = codegen(PROBES[i].op, m, mp, &mk); + int same_ok = m == 1 && PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16; /* depth16 has no math mutant */ + CHECK(mrc == OMEGA_GPU_EW_OK && ((memcmp(d0, mk.code_digest, 32) != 0) != same_ok), "%s mutant %d builds and %s (rc %s)", PROBES[i].name, m, same_ok ? "equals production" : "differs", omega_gpu_elementwise_rc_name(mrc)); + if (mrc == OMEGA_GPU_EW_OK) { + if (m == 2) CHECK(count_op(mp, BW_IR_BSYNC, -1) == 0 && count_op(mp, BW_IR_BSSY, -1) == nb, "%s mutant 2 has no BSYNC", PROBES[i].name); + if (m == 3) CHECK(count_op(mp, BW_IR_BSYNC, -1) == ns, "%s mutant 3 keeps its BSYNCs", PROBES[i].name); + free(mk.code); + } + free(mp); + } + free(p); + } + /* the four regioned sieves build too (their sweep runs in gb10_sieve_host_test) */ + for (int op = OMEGA_GPU_EW_PRIME_SIEVE_DIV1; op <= OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2; op++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + int rc = codegen((OmegaGpuEwOp)op, 0, p, &k); + int staged = op == OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1 || op == OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2; + int nb = rc == OMEGA_GPU_EW_OK ? count_op(p, BW_IR_BSSY, -1) : -1, want = (op >= OMEGA_GPU_EW_PRIME_SIEVE_DIV2 ? 2 : 1) + staged; + CHECK(rc == OMEGA_GPU_EW_OK && k.gpr_count <= 64 && nb == want && edge_waits(p) == 0, "sieve op %d builds with %d regions (rc %s, %u gprs)", op, nb, omega_gpu_elementwise_rc_name(rc), k.gpr_count); + if (rc == OMEGA_GPU_EW_OK) free(k.code); + free(p); + } + + /* 4. nvdisasm: the words decode as BSSY/BSYNC on the real barrier registers, and the predicated + * and B15 forms the encoder can produce decode too (built by hand: encode_one) */ + { + static const char *const m0[] = { "BSSY", "BSYNC", "B0", "SHFL.DOWN", "@P0 BRA" }; + CHECK(nvdisasm_kernel(OMEGA_GPU_EW_RC_DIAMOND, 0, "diamond", m0, 5) == 0, "diamond listing decodes with BSSY/BSYNC B0"); + static const char *const m1[] = { "BSSY", "BSYNC", "B15", "B7" }; + CHECK(nvdisasm_kernel(OMEGA_GPU_EW_RC_DEPTH16, 0, "depth16", m1, 4) == 0, "depth16 listing decodes with B15"); + static const char *const m2[] = { "BSSY", "B1", "BSYNC" }; + CHECK(nvdisasm_kernel(OMEGA_GPU_EW_PRIME_SIEVE_DIV2, 0, "sieve_div2", m2, 3) == 0, "nested sieve listing decodes with B1"); + uint8_t code[64]; uint32_t w[4]; BlackwellIRInsn in; memset(&in, 0, sizeof in); in.dst_vreg = in.src1_vreg = in.src2_vreg = in.src3_vreg = -1; + in.op = BW_IR_BSSY; in.bar_reg = 3; in.imm = 2; in.predicate_p0 = true; omega_blackwell_encode_one(&in, w); memcpy(code, w, 16); + in.op = BW_IR_BSYNC; in.bar_reg = 3; in.predicate_p0 = true; in.predicate_not = true; omega_blackwell_encode_one(&in, w); memcpy(code + 16, w, 16); + in.op = BW_IR_BSYNC; in.bar_reg = 15; in.predicate_p0 = false; in.predicate_not = false; omega_blackwell_encode_one(&in, w); memcpy(code + 32, w, 16); + in.op = BW_IR_EXIT; omega_blackwell_encode_one(&in, w); memcpy(code + 48, w, 16); + static const char *const m3[] = { "@P0 BSSY", "B3", "@!P0 BSYNC", "BSYNC.RECONVERGENT B15" }; + CHECK(nvdisasm_bytes(code, 64, "forms", m3, 4) == 0, "predicated BSSY/BSYNC and B15 decode"); + } + + /* 5. simulator: every probe, every n, both fragment orders, exact against the oracle */ + uint32_t *a = malloc(20000 * 4), *b = malloc(20000 * 4), *got = malloc(20000 * 4), *want = malloc(20000 * 4); + for (size_t i = 0; i < NPROBES; i++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + if (codegen(PROBES[i].op, 0, p, &k) != OMEGA_GPU_EW_OK) { free(p); continue; } + int fails = 0; uint64_t cases = 0; + for (size_t j = 0; j < NNS; j++) for (int order = 0; order < 2; order++) { + uint32_t n = NS[j]; fill(a, b, n, 0x308u + n * 7u + (uint32_t)order); + rc_oracle(PROBES[i].op, 0, n, a, b, want); + WsSim st; int err = ws_probe(p, n, a, b, got, order, &st); + uint32_t first; size_t bad = diff_count(got, want, n, &first); + cases++; + if (err || bad) { fails++; printf(" %s n=%u order=%d: sim %s, %zu words differ (first %u: got %08x want %08x)\n", PROBES[i].name, n, order, err ? st.msg : "clean", bad, first, first < n ? got[first] : 0, first < n ? want[first] : 0); } + if (n == 4096) { Model *m = &g_model[g_nmodel++]; m->name = PROBES[i].name; m->warp_issues = st.warp_issues; m->lane_issues = st.lane_issues; m->splits = st.splits; m->max_frags = st.max_frags; m->peak_bars = st.peak_bars; } + } + CHECK(fails == 0, "%s simulator vs oracle: %d of %" PRIu64 " cases differ", PROBES[i].name, fails, cases); + /* math mutant: simulator clean, result wrong (the oracle sees it) */ + BlackwellIRProgram *mp = calloc(1, sizeof *mp); OmegaBlackwellKernel mk; + if (codegen(PROBES[i].op, 1, mp, &mk) == OMEGA_GPU_EW_OK) { + uint32_t n = 1000; fill(a, b, n, 0x1001u); rc_oracle(PROBES[i].op, 0, n, a, b, want); + WsSim st; int err = ws_probe(mp, n, a, b, got, 0, &st); uint32_t first; size_t bad = diff_count(got, want, n, &first); + if (PROBES[i].op == OMEGA_GPU_EW_RC_EXIT_BAR) CHECK(err == WS_E_SHFL_EXITED || err == WS_E_BAR_SPLIT, "R6 early-exit kernel: simulator reports SHFL_EXITED (got %s)", st.msg); + else if (PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16) CHECK(err == 0 && bad == 0, "depth16 'mutant 1' is the production kernel: clean and exact (err %d bad %zu)", err, bad); + else CHECK(err == 0 && bad > 0, "%s math mutant: simulator clean, %zu words differ (err %d)", PROBES[i].name, bad, err); + free(mk.code); + } + free(mp); + /* dropped join and corrupted target: the simulator must reject both, in both orders */ + for (int m = 2; m <= 3; m++) for (int order = 0; order < 2; order++) { + BlackwellIRProgram *xp = calloc(1, sizeof *xp); OmegaBlackwellKernel xk; + if (codegen(PROBES[i].op, m, xp, &xk) == OMEGA_GPU_EW_OK) { + uint32_t n = 1000; fill(a, b, n, 0x2002u + (uint32_t)m); + WsSim st; int err = ws_probe(xp, n, a, b, got, order, &st); + CHECK(is_reconv_err(err), "%s mutant %d (%s) order %d rejected by the simulator: %s", PROBES[i].name, m, m == 2 ? "BSYNC dropped" : "BSSY target corrupted", order, err ? st.msg : "NOT CAUGHT"); + if (m == 3) CHECK(err == WS_E_JOIN_MISMATCH || err == WS_E_STUCK, "%s corrupted target shows as JOIN_MISMATCH or STUCK (%s)", PROBES[i].name, ws_err_name(err)); + free(xk.code); + } + free(xp); + } + free(k.code); free(p); + } + /* 6. hand mutations of the production diamond (controls on the simulator itself, independent of + * the emitter's mutant hooks): exit jumping past the BSYNC, BSYNC on the wrong barrier, a BSSY + * on an armed barrier. And the unmodified program as the positive control. */ + { + BlackwellIRProgram *p = calloc(1, sizeof *p), *q = calloc(1, sizeof *q); OmegaBlackwellKernel k; + if (codegen(OMEGA_GPU_EW_RC_DIAMOND, 0, p, &k) == OMEGA_GPU_EW_OK) { + uint32_t n = 500; fill(a, b, n, 0x3003u); WsSim st; + int bssy = find_op(p, BW_IR_BSSY, 0), bsync = find_op(p, BW_IR_BSYNC, 0), ex = -1; + for (int i = bssy + 1; i < bsync; i++) if (p->insns[i].op == BW_IR_BRA && !p->insns[i].predicate_p0) ex = i; + CHECK(bssy >= 0 && bsync > bssy && ex > bssy, "diamond shape: BSSY %d, exit %d, BSYNC %d", bssy, ex, bsync); + CHECK(ws_probe(p, n, a, b, got, 0, &st) == 0, "positive control: untouched diamond runs clean"); + *q = *p; q->insns[ex].imm += 1; /* exit lands after the BSYNC */ + int e1 = ws_probe(q, n, a, b, got, 0, &st); CHECK(is_reconv_err(e1), "exit past the BSYNC rejected: %s", st.msg); + *q = *p; q->insns[bsync].bar_reg = 1; /* BSYNC on a barrier nobody armed */ + int e2 = ws_probe(q, n, a, b, got, 0, &st); CHECK(e2 == WS_E_BSYNC_UNARMED, "BSYNC on an unarmed barrier rejected: %s", st.msg); + *q = *p; q->insns[bsync].op = BW_IR_NOP; /* join dropped by hand */ + int e3 = ws_probe(q, n, a, b, got, 0, &st); CHECK(is_reconv_err(e3), "join replaced by NOP rejected: %s", st.msg); + *q = *p; q->insns[bssy].imm += 1; /* names the wrong join */ + int e4 = ws_probe(q, n, a, b, got, 0, &st); CHECK(e4 == WS_E_JOIN_MISMATCH, "wrong join target rejected: %s", st.msg); + *q = *p; q->insns[bssy + 1] = p->insns[bssy]; /* second BSSY on the armed barrier (overwrites the ISETP) */ + int e5 = ws_probe(q, n, a, b, got, 0, &st); CHECK(e5 == WS_E_BSSY_REARM, "BSSY on an armed barrier rejected: %s", st.msg); + free(k.code); + } + free(p); free(q); + } + free(a); free(b); free(got); free(want); + for (int i = 0; i < g_nmodel; i++) printf("model %s n=4096: warp_issues=%" PRIu64 " lane_util=%.3f splits=%" PRIu64 " max_frags=%d peak_bars=%d\n", g_model[i].name, g_model[i].warp_issues, g_model[i].warp_issues ? (double)g_model[i].lane_issues / (32.0 * (double)g_model[i].warp_issues) : 0.0, g_model[i].splits, g_model[i].max_frags, g_model[i].peak_bars); + return g_failed == before ? 0 : -1; +} + +/* --------------------------------------------------------- chip part */ +static int chip(const char *out_path, int observe) { + FILE *out = out_path ? fopen(out_path, "w") : NULL; + if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; } + if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_RECONV_V1\",\"issue\":308,\"comparison\":\"exact u32\",\"cases\":["); + int first = 1; + uint32_t *a = malloc(20000 * 4), *b = malloc(20000 * 4), *got = malloc(20000 * 4), *want = malloc(20000 * 4); + for (size_t i = 0; i < NPROBES; i++) { + for (size_t j = 0; j < NNS; j++) { + uint32_t n = NS[j]; fill(a, b, n, 0x5050u + n); + rc_oracle(PROBES[i].op, 0, n, a, b, want); + OmegaGpuEwInfo info; memset(&info, 0, sizeof info); + int rc = omega_gpu_reconv_probe_u32(PROBES[i].op, n, a, b, got, &info); + uint32_t fi; size_t bad = rc == OMEGA_GPU_EW_OK ? diff_count(got, want, n, &fi) : n; + int pass = rc == OMEGA_GPU_EW_OK && bad == 0; + CHECK(pass, "%s n=%u chip rc=%s violations=%zu err=\"%s\"", PROBES[i].name, n, omega_gpu_elementwise_rc_name(rc), bad, omega_gpu_elementwise_last_error()); + if (out) fprintf(out, "%s{\"op\":\"%s\",\"regression\":\"%s\",\"n\":%u,\"rc\":\"%s\",\"violations\":%zu,\"pass\":%s,\"chip_elapsed_ns\":%" PRIu64 ",\"chip_calls\":%u,\"unwritten_words\":%u,\"cache_hit\":%s}", + first ? "" : ",", PROBES[i].name, PROBES[i].reg, n, omega_gpu_elementwise_rc_name(rc), bad, pass ? "true" : "false", info.elapsed_ns, info.chip_calls, info.unwritten_words, info.kernel_cache_hit ? "true" : "false"); + first = 0; + if (!pass) { printf("STOP: %s failed on the chip at n=%u; no further launches of this probe\n", PROBES[i].name, n); break; } + } + } + if (out) fprintf(out, "],\"mutants\":["); + first = 1; + for (size_t i = 0; i < NPROBES; i++) { + if (PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16) continue; /* no math mutant */ + uint32_t n = 1000; fill(a, b, n, 0x6060u); rc_oracle(PROBES[i].op, 0, n, a, b, want); + omega_gpu_elementwise_test_set_mutant((int)PROBES[i].op); + /* R6's math mutant is the early-exit kernel: the emitter refuses it on the launch path + * (the bypass exists only for codegen_ir), so the catch is CODEGEN_FAIL: no undefined + * SHFL ever reaches the chip. */ + OmegaGpuEwInfo info; memset(&info, 0, sizeof info); + int rc = omega_gpu_reconv_probe_u32(PROBES[i].op, n, a, b, got, &info); + uint32_t fi; size_t bad = rc == OMEGA_GPU_EW_OK ? diff_count(got, want, n, &fi) : n; + int caught = rc != OMEGA_GPU_EW_OK || bad > 0; + omega_gpu_elementwise_test_set_mutant(0); + CHECK(caught, "%s math mutant caught (rc %s, violations %zu)", PROBES[i].name, omega_gpu_elementwise_rc_name(rc), bad); + if (PROBES[i].op == OMEGA_GPU_EW_RC_EXIT_BAR) CHECK(rc == OMEGA_GPU_EW_CODEGEN_FAIL, "R6 early-exit form refused at codegen on the launch path (rc %s)", omega_gpu_elementwise_rc_name(rc)); + if (out) fprintf(out, "%s{\"op\":\"%s\",\"mutant\":1,\"rc\":\"%s\",\"violations\":%zu,\"caught\":%s}", first ? "" : ",", PROBES[i].name, omega_gpu_elementwise_rc_name(rc), bad, caught ? "true" : "false"); + first = 0; + } + if (out) fprintf(out, "],\"observations\":["); + if (observe) { + /* The dropped-join kernel (rejected by the simulator above) on the chip once, small n: + * recorded as an observation of what the hardware does without the BSYNC. Not a gate. */ + uint32_t n = 1000; fill(a, b, n, 0x7070u); rc_oracle(OMEGA_GPU_EW_RC_DIAMOND, 0, n, a, b, want); + omega_gpu_elementwise_test_set_mutant_level(OMEGA_GPU_EW_RC_DIAMOND, 2); + OmegaGpuEwInfo info; memset(&info, 0, sizeof info); + int rc = omega_gpu_reconv_probe_u32(OMEGA_GPU_EW_RC_DIAMOND, n, a, b, got, &info); + uint32_t fi; size_t bad = rc == OMEGA_GPU_EW_OK ? diff_count(got, want, n, &fi) : n; + omega_gpu_elementwise_test_set_mutant(0); + printf("observation: diamond with the BSYNC dropped, n=%u: rc=%s, %zu of %u words differ from the oracle (first %u)\n", n, omega_gpu_elementwise_rc_name(rc), bad, n, fi); + if (out) fprintf(out, "{\"op\":\"rc_diamond\",\"mutant\":2,\"what\":\"BSYNC dropped\",\"n\":%u,\"rc\":\"%s\",\"violations\":%zu,\"first_bad\":%u,\"gate\":false}", n, omega_gpu_elementwise_rc_name(rc), bad, fi); + } + CHECK(omega_gpu_session_open_count() == 1, "device opened once for the battery (opens=%u)", omega_gpu_session_open_count()); + if (out) { + fprintf(out, "],\"sim_model_n4096\":["); + for (int i = 0; i < g_nmodel; i++) fprintf(out, "%s{\"op\":\"%s\",\"order\":%d,\"warp_issues\":%" PRIu64 ",\"lane_issues\":%" PRIu64 ",\"splits\":%" PRIu64 ",\"max_frags\":%d,\"peak_bars\":%d}", i ? "," : "", g_model[i].name, i & 1, g_model[i].warp_issues, g_model[i].lane_issues, g_model[i].splits, g_model[i].max_frags, g_model[i].peak_bars); + 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_RECONV_PASS" : "OMEGA_GPU_RECONV_FAIL"); + fclose(out); + } + free(a); free(b); free(got); free(want); + return 0; +} + +int main(int argc, char **argv) { + const char *out_path = NULL; int host = 0, observe = 0; + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "--host-only") == 0) host = 1; + else if (strcmp(argv[i], "--observe-dropped-join") == 0) observe = 1; + else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i]; + } + int hrc = host_part(); + if (!host) { + if (hrc != 0) { printf("host part failed: no chip launches\nFAIL: %d checks, %d failed\n", g_checks, g_failed); return 1; } + if (chip(out_path, observe) != 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/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/omega_blackwell_codegen.c b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/omega_blackwell_codegen.c new file mode 100644 index 000000000..38f73412a --- /dev/null +++ b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/omega_blackwell_codegen.c @@ -0,0 +1,1945 @@ +#include "omega_blackwell_codegen.h" +#include "omega_blackwell_submit.h" +#include "sha256.h" +#include +#include +#include + +void omega_bw_ir_init(BlackwellIRProgram *prog) { + if (!prog) return; + memset(prog, 0, sizeof(*prog)); +} + +int omega_bw_ir_alloc_vreg(BlackwellIRProgram *prog) { + if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1; + int v = prog->regalloc.num_vregs++; + prog->regalloc.intervals[v].vreg = v; + prog->regalloc.intervals[v].first_def = -1; + prog->regalloc.intervals[v].last_use = -1; + prog->regalloc.intervals[v].phys_reg = -1; + prog->regalloc.intervals[v].bundle_size = 1; + prog->regalloc.intervals[v].is_pair = false; + prog->regalloc.intervals[v].is_quad = false; + prog->regalloc.intervals[v].active = false; + prog->regalloc.vreg_to_phys[v] = -1; + return v; +} + +int omega_bw_ir_alloc_vreg64(BlackwellIRProgram *prog) { + if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1; + int v = prog->regalloc.num_vregs++; + prog->regalloc.intervals[v].vreg = v; + prog->regalloc.intervals[v].first_def = -1; + prog->regalloc.intervals[v].last_use = -1; + prog->regalloc.intervals[v].phys_reg = -1; + prog->regalloc.intervals[v].bundle_size = 2; + prog->regalloc.intervals[v].is_pair = true; + prog->regalloc.intervals[v].is_quad = false; + prog->regalloc.intervals[v].active = false; + prog->regalloc.vreg_to_phys[v] = -1; + return v; +} + +int omega_bw_ir_alloc_vreg128(BlackwellIRProgram *prog) { + if (!prog || prog->regalloc.num_vregs >= BW_MAX_VREGS) return -1; + int v = prog->regalloc.num_vregs++; + prog->regalloc.intervals[v].vreg = v; + prog->regalloc.intervals[v].first_def = -1; + prog->regalloc.intervals[v].last_use = -1; + prog->regalloc.intervals[v].phys_reg = -1; + prog->regalloc.intervals[v].bundle_size = 4; + prog->regalloc.intervals[v].is_pair = false; + prog->regalloc.intervals[v].is_quad = true; + prog->regalloc.intervals[v].active = false; + prog->regalloc.vreg_to_phys[v] = -1; + return v; +} + +int omega_bw_ir_alloc_uvreg(BlackwellIRProgram *prog) { + if (!prog || prog->regalloc.num_uvregs >= BW_MAX_UVREGS) return -1; + int uv = prog->regalloc.num_uvregs++; + prog->regalloc.uintervals[uv].vreg = uv; + prog->regalloc.uintervals[uv].first_def = -1; + prog->regalloc.uintervals[uv].last_use = -1; + prog->regalloc.uintervals[uv].phys_reg = -1; + prog->regalloc.uintervals[uv].is_pair = false; + prog->regalloc.uintervals[uv].active = false; + prog->regalloc.uvreg_to_phys[uv] = -1; + return uv; +} + +int omega_bw_ir_alloc_uvreg64(BlackwellIRProgram *prog) { + if (!prog || prog->regalloc.num_uvregs >= BW_MAX_UVREGS) return -1; + int uv = prog->regalloc.num_uvregs++; + prog->regalloc.uintervals[uv].vreg = uv; + prog->regalloc.uintervals[uv].first_def = -1; + prog->regalloc.uintervals[uv].last_use = -1; + prog->regalloc.uintervals[uv].phys_reg = -1; + prog->regalloc.uintervals[uv].is_pair = true; + prog->regalloc.uintervals[uv].active = false; + prog->regalloc.uvreg_to_phys[uv] = -1; + return uv; +} + +int omega_bw_ir_append(BlackwellIRProgram *prog, const BlackwellIRInsn *insn) { + if (!prog || !insn || prog->count >= BW_MAX_IR_INSNS) return -1; + int idx = (int)prog->count++; + prog->insns[idx] = *insn; + + /* Update live-interval def/use tracking */ + if (insn->dst_vreg >= 0 && insn->dst_vreg < prog->regalloc.num_vregs) { + if (!insn->is_uniform) { + OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->dst_vreg]; + if (iv->first_def < 0) iv->first_def = idx; + if (idx > iv->last_use) iv->last_use = idx; + } + } + if (insn->src1_vreg >= 0 && insn->src1_vreg < prog->regalloc.num_vregs) { + OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src1_vreg]; + if (iv->first_def < 0) iv->first_def = idx; + if (idx > iv->last_use) iv->last_use = idx; + } + if (insn->src2_vreg >= 0 && insn->src2_vreg < prog->regalloc.num_vregs) { + OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src2_vreg]; + if (iv->first_def < 0) iv->first_def = idx; + if (idx > iv->last_use) iv->last_use = idx; + } + if (insn->src3_vreg >= 0 && insn->src3_vreg < prog->regalloc.num_vregs) { + OmegaLiveInterval *iv = &prog->regalloc.intervals[insn->src3_vreg]; + if (iv->first_def < 0) iv->first_def = idx; + if (idx > iv->last_use) iv->last_use = idx; + } + + /* Uniform register def/use tracking */ + if (insn->is_uniform && insn->dst_vreg >= 0 && insn->dst_vreg < prog->regalloc.num_uvregs) { + OmegaLiveInterval *uiv = &prog->regalloc.uintervals[insn->dst_vreg]; + if (uiv->first_def < 0) uiv->first_def = idx; + if (idx > uiv->last_use) uiv->last_use = idx; + } + if (insn->ureg >= 0 && insn->ureg < prog->regalloc.num_uvregs) { + OmegaLiveInterval *uiv = &prog->regalloc.uintervals[insn->ureg]; + if (uiv->first_def < 0) uiv->first_def = idx; + if (idx > uiv->last_use) uiv->last_use = idx; + } + + return idx; +} + +int omega_bw_regalloc_solve(BlackwellIRProgram *prog) { + if (!prog) return -1; + OmegaRegAlloc *ra = &prog->regalloc; + + /* + * Bounded deterministic linear-scan register allocator. + * Computes genuine live intervals across virtual registers and assigns + * available physical GPRs (R2..R127) and UGPRs (UR4..UR31). + * Enforces even-alignment and dual-register reservation for 64-bit pairs. + */ + bool gpr_busy[BW_PHYS_GPR_MAX]; + memset(gpr_busy, 0, sizeof(gpr_busy)); + + int max_phys_used = BW_PHYS_GPR_START; + + /* Sort virtual register indices chronologically by first_def (Poletto & Sarkar Linear Scan) */ + int order[BW_MAX_VREGS]; + int count = 0; + for (int v = 0; v < ra->num_vregs; v++) { + if (ra->intervals[v].first_def >= 0) { + order[count++] = v; + } else { + ra->intervals[v].phys_reg = BW_PHYS_GPR_START; + ra->vreg_to_phys[v] = ra->intervals[v].phys_reg; + } + } + for (int i = 0; i < count - 1; i++) { + for (int j = i + 1; j < count; j++) { + if (ra->intervals[order[i]].first_def > ra->intervals[order[j]].first_def) { + int tmp = order[i]; + order[i] = order[j]; + order[j] = tmp; + } + } + } + + for (int idx = 0; idx < count; idx++) { + int v = order[idx]; + OmegaLiveInterval *iv = &ra->intervals[v]; + + /* Free physical registers whose virtual live intervals have ended */ + for (int p = 0; p < ra->num_vregs; p++) { + if (ra->intervals[p].active && ra->intervals[p].last_use < iv->first_def) { + int freed = ra->intervals[p].phys_reg; + if (freed >= 0 && freed < BW_PHYS_GPR_MAX) { + uint32_t bsize = ra->intervals[p].bundle_size ? ra->intervals[p].bundle_size : (ra->intervals[p].is_pair ? 2 : (ra->intervals[p].is_quad ? 4 : 1)); + for (uint32_t k = 0; k < bsize && (freed + (int)k) < BW_PHYS_GPR_MAX; k++) { + gpr_busy[freed + k] = false; + } + } + ra->intervals[p].active = false; + } + } + + /* Find lowest available physical register */ + int assigned = -1; + uint32_t bsize = iv->bundle_size ? iv->bundle_size : (iv->is_pair ? 2 : (iv->is_quad ? 4 : 1)); + if (bsize == 4) { + /* 128-bit quad requires 4-alignment and 4 contiguous free registers */ + int start = (BW_PHYS_GPR_START + 3) & ~3; + for (int p = start; p <= BW_PHYS_GPR_MAX - 4; p += 4) { + if (!gpr_busy[p] && !gpr_busy[p + 1] && !gpr_busy[p + 2] && !gpr_busy[p + 3]) { + assigned = p; + for (int k = 0; k < 4; k++) gpr_busy[p + k] = true; + break; + } + } + } else if (bsize == 2) { + /* 64-bit pair requires even alignment and contiguous free registers */ + int start = (BW_PHYS_GPR_START + 1) & ~1; + for (int p = start; p <= BW_PHYS_GPR_MAX - 2; p += 2) { + if (!gpr_busy[p] && !gpr_busy[p + 1]) { + assigned = p; + gpr_busy[p] = true; + gpr_busy[p + 1] = true; + break; + } + } + } else { + for (int p = BW_PHYS_GPR_START; p < BW_PHYS_GPR_MAX; p++) { + if (!gpr_busy[p]) { + assigned = p; + gpr_busy[p] = true; + break; + } + } + } + + if (assigned < 0) { + /* Hardware register capacity exceeded: fail closed loudly */ + return -1; + } + + iv->phys_reg = assigned; + iv->active = true; + ra->vreg_to_phys[v] = assigned; + + int top = assigned + (int)bsize - 1; + if (top > max_phys_used) { + max_phys_used = top; + } + } + ra->peak_gpr_usage = (uint32_t)(max_phys_used + 1); + + /* Allocate uniform registers */ + int cur_u = BW_PHYS_UGPR_START; + for (int uv = 0; uv < ra->num_uvregs; uv++) { + OmegaLiveInterval *uiv = &ra->uintervals[uv]; + if (uiv->is_pair) { + if (cur_u % 2 != 0) cur_u++; + if (cur_u + 1 >= BW_PHYS_UGPR_MAX) return -1; + uiv->phys_reg = cur_u; + ra->uvreg_to_phys[uv] = cur_u; + cur_u += 2; + } else { + if (cur_u >= BW_PHYS_UGPR_MAX) return -1; + uiv->phys_reg = cur_u; + ra->uvreg_to_phys[uv] = cur_u++; + } + } + ra->peak_ugpr_usage = (uint32_t)cur_u; + + return 0; +} + +static int encode_single_insn(const BlackwellIRInsn *insn, const OmegaRegAlloc *ra, uint32_t w[4]) { + memset(w, 0, 16); + + int dst = (insn->dst_vreg >= 0) ? (ra->vreg_to_phys[insn->dst_vreg] + insn->dst_subreg) : 0; + int src1 = (insn->src1_vreg >= 0) ? (ra->vreg_to_phys[insn->src1_vreg] + insn->src1_subreg) : 0; + int src2 = (insn->src2_vreg >= 0) ? (ra->vreg_to_phys[insn->src2_vreg] + insn->src2_subreg) : 0; + int src3 = (insn->src3_vreg >= 0) ? (ra->vreg_to_phys[insn->src3_vreg] + insn->src3_subreg) : 0xff; /* Default to RZ (0xff) */ + int ureg = (insn->ureg >= 0) ? ra->uvreg_to_phys[insn->ureg] : 0; + + if (insn->is_uniform && insn->dst_vreg >= 0) { + dst = ra->uvreg_to_phys[insn->dst_vreg]; + } + + switch (insn->op) { + case BW_IR_NOP: + w[0] = 0x00007918; + w[1] = 0x00000000; + w[2] = 0x00000000; + w[3] = 0x000fc000; + break; + + case BW_IR_MOV_RZ: + /* MOV Rd, RZ */ + w[0] = 0x7202 | ((uint32_t)(dst & 0xff) << 16); + w[1] = 0x000000ff; + w[2] = 0x00000f00; + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_MOV_IMM: + /* MOV Rd, imm32 */ + w[0] = 0x7802 | ((uint32_t)(dst & 0xff) << 16); + w[1] = insn->imm; + w[2] = 0x00000f00; + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_S2R: + /* S2R Rd, SR */ + w[0] = 0x7919 | ((uint32_t)(dst & 0xff) << 16); + w[1] = 0x0; + w[2] = (insn->imm & 0xff) << 8; + w[3] = insn->control ? insn->control : 0x000e2200; + break; + + case BW_IR_LDC: + /* LDC Rd, c[0x0][imm] */ + w[0] = 0xff007b82 | ((uint32_t)(dst & 0xff) << 16); + w[1] = (insn->imm << 6) & 0x00ffff00; + w[2] = 0x00000800; + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_LDC64: + /* LDC.64 Rd:Rd+1, c[0x0][imm] */ + w[0] = 0xff007b82 | ((uint32_t)(dst & 0xff) << 16); + w[1] = (insn->imm << 6) & 0x00ffff00; + w[2] = 0x00000a00; + w[3] = insn->control ? insn->control : 0x000e2200; + break; + + case BW_IR_LDCU: + /* LDCU URd, c[0x0][imm] */ + w[0] = 0xff0077ac | ((uint32_t)(dst & 0xff) << 16); + w[1] = (insn->imm << 5) & 0x00ffff00; + w[2] = 0x08000800; + w[3] = insn->control ? insn->control : 0x000e2200; + break; + + case BW_IR_LDCU64: + /* LDCU.64 URd:URd+1, c[0x0][imm] */ + w[0] = 0xff0077ac | ((uint32_t)(dst & 0xff) << 16); + w[1] = (insn->imm << 5) & 0x00ffff00; + w[2] = 0x08000a00; + w[3] = insn->control ? insn->control : 0x000e6e00; + break; + + case BW_IR_IMAD: + if (insn->src2_vreg >= 0) { + /* Register-register: IMAD Rd, Ra, Rb, Rc (0x7224) */ + w[0] = 0x7224 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x078e0200 | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + } else if (insn->is_uniform && insn->ureg >= 0) { + /* Uniform register multiplier: IMAD Rd, Ra, URb, Rc (0x7c24) */ + w[0] = 0x7c24 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0f8e0200 | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + } else { + /* Immediate multiplier: IMAD Rd, Ra, imm, Rc (0x7824) */ + w[0] = 0x7824 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = insn->imm; + w[2] = 0x078e0200 | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + } + break; + + case BW_IR_IMAD_WIDE: + /* IMAD.WIDE.U32 Rd:Rd+1, Ra, imm, Rc:Rc+1 */ + w[0] = 0x7825 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = insn->imm; + w[2] = 0x078e0000 | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fcc00; + break; + + case BW_IR_ISETP_GE: + if (insn->is_uniform && insn->ureg >= 0) { + w[0] = 0x7c0c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0bf06070; + w[3] = 0x002fda00; + } else { + w[0] = 0x780c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = insn->imm; + w[2] = 0x03f06270; + w[3] = 0x001fda00; + } + break; + + case BW_IR_LDG_E: + /* LDG.E Rd, desc[URd][Ra.64] */ + w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0c1e1900; + w[3] = insn->control ? insn->control : 0x000f2200; + break; + + case BW_IR_STG_E: + /* STG.E desc[URd][Ra.64], Rb */ + w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x0c101900 | (uint32_t)(ureg & 0xff); + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_STG_EF: + /* STG.E.MMIO.GPU. Uncached store, meant to be visible without a fence. */ + w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x0c111900 | (uint32_t)(ureg & 0xff); + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_IADD3: + /* IADD3 Rd, PT, PT, Ra, Rb, Rc */ + w[0] = 0x7210 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x07ffe0ff; + w[3] = 0x010fca00; + break; + + case BW_IR_HMMA_F16: + case BW_IR_HMMA_BF16: { + /* HMMA.16816.F32[.BF16] Rd, Ra, Rb, Rc */ + w[0] = 0x723c | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + uint32_t prec_bit = (insn->op == BW_IR_HMMA_BF16) ? 0x00040000 : 0x00000000; + w[2] = (uint32_t)(src3 & 0xff) | (0x18 << 8) | prec_bit; + w[3] = insn->control ? insn->control : 0x000fe200; + break; + } + + case BW_IR_SHF_R: + /* SHF.R.U32.HI Rd, RZ, imm, Ra */ + w[0] = 0x7819U | ((uint32_t)(dst & 0xff) << 16) | (0xffU << 24); + w[1] = (uint32_t)insn->imm; + w[2] = 0x00011600 | (uint32_t)(src1 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + break; + + case BW_IR_LOP3_AND: + /* LOP3.LUT Rd, Ra, imm, RZ, 0xc0, !PT */ + w[0] = 0x7812U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)insn->imm; + w[2] = 0x078ec0ff; + w[3] = insn->control ? insn->control : 0x001fca00; + break; + + case BW_IR_LDG_E_U16: + /* LDG.E.U16 Rd, desc[URd][Ra.64] */ + w[0] = 0x7981U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0c1e1500; + w[3] = insn->control ? insn->control : 0x000f2200; + break; + + case BW_IR_LDG_STRONG_SYS: + /* LDG.E.STRONG.SYS. Same shape as LDG.E; w[2] carries the + * system-scope bits (0x0c1e1900 -> 0x0c1f5900). */ + w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0c1f5900; + w[3] = insn->control ? insn->control : 0x000f2200; + break; + + case BW_IR_LDG_MMIO: + /* LDG.E.EF. Drop the cached line, then load. */ + w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0c0e1900; + w[3] = insn->control ? insn->control : 0x000f2200; + break; + + case BW_IR_STG_STRONG_SYS: + w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); + if (insn->predicate_p0) { + uint32_t pred = insn->predicate_not ? 0x8u : 0x0u; + w[0] = (w[0] & ~0xF000u) | (pred << 12); + } + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x0c115900 | (uint32_t)(ureg & 0xff); + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_MEMBAR_ALL_SYS: + w[0] = 0x00007992; + w[1] = 0x00000000; + w[2] = 0x0000b000; + w[3] = insn->control ? insn->control : 0x000fec00; + break; + + case BW_IR_MEMBAR_SC_SYS: + w[0] = 0x00007992; + w[1] = 0x00000000; + w[2] = 0x00003000; + w[3] = insn->control ? insn->control : 0x000fec00; + break; + + case BW_IR_CCTL_IVALL: + w[0] = 0xff00798f; + w[1] = 0x00000000; + w[2] = 0x02000000; + w[3] = insn->control ? insn->control : 0x000fe800; + break; + + case BW_IR_ATOMG_ADD_STRONG_SYS: + w[0] = 0x79a8 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff) | 0x80000000u; + w[2] = 0x081f5100 | (uint32_t)(ureg & 0xff); + w[3] = insn->control ? insn->control : 0x00321e00; + break; + + case BW_IR_ATOMG_EXCH_STRONG_SYS: + w[0] = 0x79a8 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff) | 0x80000000u; + w[2] = 0x0c1f5100 | (uint32_t)(ureg & 0xff); + w[3] = insn->control ? insn->control : 0x00321e00; + break; + + case BW_IR_ISETP_GE_U32: + /* ISETP.GE.U32.AND P0, PT, Ra, Rb, PT. Checked with nvdisasm -b SM121. */ + w[0] = 0x720c | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x03f06070; + w[3] = insn->control ? insn->control : 0x001fda00; + break; + + case BW_IR_LOP3_XOR: + /* LOP3.LUT Rd, Ra, Rb, RZ, 0x3c, !PT. Checked with nvdisasm -b SM121. */ + w[0] = 0x7212 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x078e3cff; + w[3] = insn->control ? insn->control : 0x001fca00; + break; + + case BW_IR_FADD: + /* FADD Rd, Ra, Rb */ + w[0] = 0x7221U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x00000000; + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_FSUB: + /* FSUB Rd, Ra, Rb (FADD Rd, Ra, -Rb) */ + w[0] = 0x7221U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff) | 0x80000000U; + w[2] = 0x00000000; + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_FMUL: + /* FMUL Rd, Ra, Rb */ + w[0] = 0x7220U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x00400000; + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_FFMA: + /* FFMA Rd, Ra, Rb, Rc */ + w[0] = 0x7223U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_FSETP: + /* FSETP.cond.AND P0, PT, Ra, Rb, PT */ + w[0] = 0x720bU | ((uint32_t)(dst & 0x7) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = insn->imm ? insn->imm : 0x03f06000; /* Default GE condition */ + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_FSEL: + /* FSEL Rd, Ra, Rb, P0 */ + w[0] = 0x7208U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = (uint32_t)(src3 & 0x7); + w[3] = insn->control ? insn->control : 0x000fca00; + break; + + case BW_IR_FMNMX_MIN: + /* FMNMX Rd, Ra, Rb, PT (minimum) */ + w[0] = 0x7209U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x03800000; + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_FMNMX_MAX: + /* FMNMX Rd, Ra, Rb, !PT (maximum) */ + w[0] = 0x7209U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x07800000; + w[3] = insn->control ? insn->control : 0x004fc400; + break; + + case BW_IR_I2FP: + /* I2FP.F32.S32 Rd, Ra */ + w[0] = 0x7245U | ((uint32_t)(dst & 0xff) << 16); + w[1] = (uint32_t)(src1 & 0xff); + w[2] = 0x00201400; + w[3] = insn->control ? insn->control : 0x004fe200; + break; + + case BW_IR_F2I: + /* F2I.TRUNC.NTZ Rd, Ra */ + w[0] = 0x7305U | ((uint32_t)(dst & 0xff) << 16); + w[1] = (uint32_t)(src1 & 0xff); + w[2] = 0x0020f100; + w[3] = insn->control ? insn->control : 0x004e2200; + break; + + case BW_IR_MUFU_RCP: + /* MUFU.RCP Rd, Ra */ + w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); + w[1] = (uint32_t)(src1 & 0xff); + w[2] = 0x00001000; + w[3] = insn->control ? insn->control : 0x000e2400; + break; + + case BW_IR_MUFU_RSQ: + /* MUFU.RSQ Rd, Ra */ + w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); + w[1] = (uint32_t)(src1 & 0xff); + w[2] = 0x00001400; + w[3] = insn->control ? insn->control : 0x000e2400; + break; + + case BW_IR_SHFL_DOWN: + /* SHFL.DOWN PT, Rd, Ra, offset, 0x1f */ + w[0] = 0x7f89U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = 0x08001f00U | (((uint32_t)insn->imm & 0x1f) << 21); + w[2] = 0x000e0000; + w[3] = insn->control ? insn->control : 0x000e2400; + break; + + case BW_IR_LDS: + /* LDS Rd, [Ra] */ + w[0] = 0x7984U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = 0x00000000; + w[2] = (uint32_t)(insn->imm & 0xffff); + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_STS: + /* STS [Ra], Rb */ + w[0] = 0x7388U | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = (uint32_t)(insn->imm & 0xffff); + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + case BW_IR_EXIT: + w[0] = insn->predicate_p0 ? 0x0000094d : 0x0000794d; + w[1] = 0x00000000; + w[2] = 0x03800000; + w[3] = 0x000fea00; + break; + + case BW_IR_BRA: { + /* sm_121 BRA. Distance is (target - this) - 1, split across the + * low six bits at instruction bits 18..23 and the rest at bit 34. + * Predicate bits 12..15: 7 = always, 0 = @P0, 8 = @!P0. + * A zero delta with no predicate keeps the self-branch word the + * existing kernels already emit after EXIT. */ + int32_t delta = (int32_t)insn->imm; + int32_t rel = delta - 1; + uint32_t pred_field = 0x7u; + if (insn->predicate_p0) + pred_field = insn->predicate_not ? 0x8u : 0x0u; + w[0] = 0x947u | (pred_field << 12) | (((uint32_t)rel & 0x3fu) << 18); + w[1] = (uint32_t)(rel >> 6) << 2; + w[2] = (rel < 0) ? 0x0383ffffu : 0x03800000u; + if (insn->control) + w[3] = insn->control; + else if (delta == 0 && !insn->predicate_p0) + w[3] = 0x000fc000u; + else + w[3] = 0x000fea00u; + break; + } + + case BW_IR_BSSY: { + /* BSSY.RECONVERGENT Bn, target (omega #308). nvcc 13.0.88 -arch=sm_121: + * [0070] BSSY.RECONVERGENT B0, 0x140 ; 0x000000c000007945 0x000fe20003800200 + * opcode 0x945 (NAK sm70_encode.rs:4165), barrier id at bits 16..19 (set_bar_dst + * 16..20), guard predicate at bits 12..15 like BRA, relative byte offset + * target - (this + 16) at bit 32 (NAK: field 34.. in 4-byte units), the cond + * predicate PT at bits 87..89 (0x0380 << 16 in w[2]) and bit 73 (0x200 in w[2]), + * which the disassembler prints as .RECONVERGENT; nvcc sets it on every BSSY/BSYNC + * for sm_121 and Mesa NAK does not. We follow nvcc. The target is the instruction + * after the matching BSYNC, so the delta is at least 2; smaller is refused. */ + int32_t delta = (int32_t)insn->imm; + if (delta < 2 || insn->bar_reg >= BW_RECONV_MAX_BAR) return -1; + uint32_t pred_field = 0x7u; + if (insn->predicate_p0) + pred_field = insn->predicate_not ? 0x8u : 0x0u; + w[0] = 0x945u | (pred_field << 12) | ((uint32_t)insn->bar_reg << 16); + w[1] = (uint32_t)(delta - 1) * 16u; + w[2] = 0x03800200u; + w[3] = insn->control ? insn->control : 0x000fe200u; + break; + } + + case BW_IR_BSYNC: { + /* BSYNC.RECONVERGENT Bn: [0130] BSYNC.RECONVERGENT B0 ; 0x0000000000007941 0x000fea0003800200 + * opcode 0x941 (NAK sm70_encode.rs:4179), barrier id bits 16..19, cond PT, bit 73. */ + if (insn->bar_reg >= BW_RECONV_MAX_BAR) return -1; + uint32_t pred_field = 0x7u; + if (insn->predicate_p0) + pred_field = insn->predicate_not ? 0x8u : 0x0u; + w[0] = 0x941u | (pred_field << 12) | ((uint32_t)insn->bar_reg << 16); + w[1] = 0; + w[2] = 0x03800200u; + w[3] = insn->control ? insn->control : 0x000fea00u; + break; + } + + case BW_IR_MUFU_EX2: + /* MUFU.EX2 Rd, Ra. nvdisasm 13.0 -b SM121: 0x7308 with sub-op 0x0800 decodes + * "MUFU.EX2 R2, R4" (RCP is 0x1000, RSQ 0x1400 above). */ + w[0] = 0x7308U | ((uint32_t)(dst & 0xff) << 16); + w[1] = (uint32_t)(src1 & 0xff); + w[2] = 0x00000800; + w[3] = insn->control ? insn->control : 0x000e2400; + break; + + case BW_IR_BAR_SYNC: + /* BAR.SYNC.DEFER_BLOCKING 0x0: the exact words src/omega_numeric.c:885 runs on + * the chip (nvdisasm: "BAR.SYNC.DEFER_BLOCKING 0x0"). No registers. */ + w[0] = 0x00007b1dU; + w[1] = 0x00000000; + w[2] = 0x00010000; + w[3] = insn->control ? insn->control : 0x000fec00; + break; + + case BW_IR_LDS32: + /* LDS Rd, [Ra+URZ] (32-bit), the src/omega_numeric.c:886 form. */ + w[0] = 0x7984U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = 0x000000ff; + w[2] = 0x08000800; + w[3] = insn->control ? insn->control : 0x000e2800; + break; + + case BW_IR_STS32: + /* STS [Ra+URZ], Rb (32-bit), the src/omega_numeric.c:884 form. */ + w[0] = 0x7988U | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff); + w[2] = 0x080008ffU; + w[3] = insn->control ? insn->control : 0x000fe200; + break; + + /* Prime race cut (2026-10-05). An absent operand is RZ for these three ops. */ + case BW_IR_LOP3_LUT: { + /* LOP3.LUT Rd, Ra, Rb, Rc, imm8, !PT. LOP3_XOR above is this form with Rc = RZ, LUT 0x3c. */ + uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU; + uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU; + w[0] = 0x7212U | ((uint32_t)(dst & 0xff) << 16) | (a << 24); + w[1] = b; + w[2] = 0x078e0000U | ((insn->imm & 0xffU) << 8) | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + break; + } + + case BW_IR_SHF_L_U32: { + /* SHF.L.U32 Rd, Ra, Rb, Rc: register-amount form (0x7219) of SHF_R's 0x7819, with the + * .R (w[2] bit 12) and .HI (bit 16) flags clear. Rc is the funnel high word (RZ). */ + uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU; + uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU; + w[0] = 0x7219U | ((uint32_t)(dst & 0xff) << 16) | (a << 24); + w[1] = b; + w[2] = 0x00000600U | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + break; + } + + case BW_IR_IMAD_HI_U32: { + /* IMAD.HI.U32 Rd, Ra, Rb, Rc: opcode 0x227 with the IMAD.WIDE.U32 modifier word. */ + uint32_t a = (insn->src1_vreg >= 0) ? (uint32_t)(src1 & 0xff) : 0xffU; + uint32_t b = (insn->src2_vreg >= 0) ? (uint32_t)(src2 & 0xff) : 0xffU; + w[0] = 0x7227U | ((uint32_t)(dst & 0xff) << 16) | (a << 24); + w[1] = b; + w[2] = 0x078e0000U | (uint32_t)(src3 & 0xff); + w[3] = insn->control ? insn->control : 0x001fca00; + break; + } + + default: + return -1; + } + return 0; +} + +/* Prime race cut (2026-10-05): golden words for LOP3_LUT, SHF_L_U32 and IMAD_HI_U32. + * Each expected (w0, w1, w2) is the low 96 bits of an instruction nvcc 13.0.88 emitted + * for sm_121 (nvcc -arch=sm_121 -cubin, cuobjdump -sass; source and dump in + * bench/prime_race/tests/encoding_oracle.md). Control words (w3) are scheduler output and + * not compared. nvcc is an offline oracle only; nothing here links CUDA. 0 = all good. */ +int omega_blackwell_verify_codegen_fixtures_intops(void) { + OmegaRegAlloc ra; + memset(&ra, 0, sizeof(ra)); + static const int phys[] = { 0, 2, 6, 7, 9, 11, 13 }; + for (int i = 0; i < (int)(sizeof phys / sizeof phys[0]); i++) ra.vreg_to_phys[i] = phys[i]; + enum { R0 = 0, R2, R6, R7, R9, R11, R13, RZ = -1 }; + static const struct { + BlackwellIROpcode op; int d, a, b, c; uint32_t imm; uint32_t w0, w1, w2; const char *text; + } fx[] = { + { BW_IR_LOP3_LUT, R9, R7, R0, RZ, 0xfc, 0x07097212U, 0x00000000U, 0x078efcffU, "LOP3.LUT R9, R7, R0, RZ, 0xfc, !PT" }, + { BW_IR_LOP3_LUT, R11, R6, R7, R0, 0x78, 0x060b7212U, 0x00000007U, 0x078e7800U, "LOP3.LUT R11, R6, R7, R0, 0x78, !PT" }, + { BW_IR_LOP3_LUT, R13, R6, R7, R0, 0x10, 0x060d7212U, 0x00000007U, 0x078e1000U, "LOP3.LUT R13, R6, R7, R0, 0x10, !PT" }, + { BW_IR_LOP3_LUT, R7, RZ, R9, RZ, 0x33, 0xff077212U, 0x00000009U, 0x078e33ffU, "LOP3.LUT R7, RZ, R9, RZ, 0x33, !PT" }, + { BW_IR_SHF_L_U32, R7, R9, R2, RZ, 0, 0x09077219U, 0x00000002U, 0x000006ffU, "SHF.L.U32 R7, R9, R2, RZ" }, + { BW_IR_SHF_L_U32, R7, R0, R7, RZ, 0, 0x00077219U, 0x00000007U, 0x000006ffU, "SHF.L.U32 R7, R0, R7, RZ" }, + { BW_IR_IMAD_HI_U32, R9, R9, R0, R6, 0, 0x09097227U, 0x00000000U, 0x078e0006U, "IMAD.HI.U32 R9, R9, R0, R6" }, + { BW_IR_IMAD_HI_U32, R7, R0, R7, RZ, 0, 0x00077227U, 0x00000007U, 0x078e00ffU, "IMAD.HI.U32 R7, R0, R7, RZ" }, + }; + for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) { + BlackwellIRInsn in; + memset(&in, 0, sizeof in); + in.op = fx[i].op; in.dst_vreg = fx[i].d; in.src1_vreg = fx[i].a; in.src2_vreg = fx[i].b; + in.src3_vreg = fx[i].c; in.ureg = -1; in.imm = fx[i].imm; + uint32_t w[4]; + if (encode_single_insn(&in, &ra, w) != 0) return -(2 * i + 1); + if (w[0] != fx[i].w0 || w[1] != fx[i].w1 || w[2] != fx[i].w2) return -(2 * i + 2); + } + return 0; +} + +int omega_blackwell_encode_one(const BlackwellIRInsn *insn, uint32_t w[4]) { + if (!insn || !w) return -1; + OmegaRegAlloc ra; + memset(&ra, 0, sizeof ra); + for (int i = 0; i < BW_MAX_VREGS; i++) ra.vreg_to_phys[i] = i; + for (int i = 0; i < BW_MAX_UVREGS; i++) ra.uvreg_to_phys[i] = i; + return encode_single_insn(insn, &ra, w); +} + +/* omega #308: BSSY / BSYNC golden words. Oracle: nvcc 13.0.88 -arch=sm_121 -cubin -O3 on the + * kernels in docs/gpu-reconvergence-308.md, cuobjdump -sass (low 64-bit word, then high; the + * top 23 bits of the high word are the control word and are not compared here): + * k_loop [0070] BSSY.RECONVERGENT B0, 0x140 ; 0x000000c000007945 0x000fe20003800200 (delta 13) + * k_nested [0070] BSSY.RECONVERGENT B0, 0x300 ; 0x0000028000007945 0x000fe20003800200 (delta 41) + * k_break [0070] BSSY.RECONVERGENT B0, 0x110 ; 0x0000009000007945 0x000fe20003800200 (delta 10) + * k_loop [0130] BSYNC.RECONVERGENT B0 ; 0x0000000000007941 0x000fea0003800200 + * Barrier ids other than B0 and the predicated forms are decoded by tools/reconv_nvdisasm_check.sh + * (nvdisasm -b SM121 text), not by this table. */ +int omega_blackwell_verify_codegen_fixtures_reconv(void) { + static const struct { BlackwellIROpcode op; int delta; int bar; uint32_t w0, w1, w2; } fx[] = { + { BW_IR_BSSY, 13, 0, 0x00007945U, 0x000000c0U, 0x03800200U }, + { BW_IR_BSSY, 41, 0, 0x00007945U, 0x00000280U, 0x03800200U }, + { BW_IR_BSSY, 10, 0, 0x00007945U, 0x00000090U, 0x03800200U }, + { BW_IR_BSYNC, 0, 0, 0x00007941U, 0x00000000U, 0x03800200U }, + }; + for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) { + BlackwellIRInsn in; + memset(&in, 0, sizeof in); + in.op = fx[i].op; in.dst_vreg = -1; in.src1_vreg = -1; in.src2_vreg = -1; in.src3_vreg = -1; in.ureg = -1; + in.imm = (uint32_t)fx[i].delta; in.bar_reg = (uint8_t)fx[i].bar; + uint32_t w[4]; + if (omega_blackwell_encode_one(&in, w) != 0) return -(2 * i + 1); + if (w[0] != fx[i].w0 || w[1] != fx[i].w1 || w[2] != fx[i].w2) return -(2 * i + 2); + /* control word defaults mirror nvcc's: BSSY 0x000fe200, BSYNC 0x000fea00 */ + if (w[3] != (fx[i].op == BW_IR_BSSY ? 0x000fe200U : 0x000fea00U)) return -(2 * i + 2); + } + /* refusals: a BSSY must name a later instruction (delta >= 2), barrier ids stop at B15 */ + BlackwellIRInsn bad; uint32_t w[4]; + memset(&bad, 0, sizeof bad); bad.op = BW_IR_BSSY; bad.dst_vreg = bad.src1_vreg = bad.src2_vreg = bad.src3_vreg = bad.ureg = -1; + bad.imm = 1; bad.bar_reg = 0; + if (omega_blackwell_encode_one(&bad, w) == 0) return -101; + bad.imm = (uint32_t)-3; + if (omega_blackwell_encode_one(&bad, w) == 0) return -102; + bad.imm = 5; bad.bar_reg = 16; + if (omega_blackwell_encode_one(&bad, w) == 0) return -103; + bad.op = BW_IR_BSYNC; bad.imm = 0; bad.bar_reg = 16; + if (omega_blackwell_encode_one(&bad, w) == 0) return -104; + bad.bar_reg = 15; + if (omega_blackwell_encode_one(&bad, w) != 0 || w[0] != (0x00007941U | (15U << 16))) return -105; + return 0; +} + +/* FB-1 cut 4: word-level fixtures for the four additive ops (checked against the + * nvdisasm decodes recorded in docs/numeric/FB1_CUT4_ELEMENTWISE.md). 0 = all good. */ +int omega_blackwell_verify_codegen_fixtures_fb1cut4(void) { + OmegaRegAlloc ra; + memset(&ra, 0, sizeof(ra)); + ra.vreg_to_phys[0] = 2; /* R2 */ + ra.vreg_to_phys[1] = 4; /* R4 */ + ra.vreg_to_phys[2] = 9; /* R9 */ + ra.vreg_to_phys[3] = 10; /* R10 */ + ra.vreg_to_phys[4] = 8; /* R8 */ + uint32_t w[4]; + + /* MUFU.EX2 R2, R4 */ + BlackwellIRInsn ex2 = { .op = BW_IR_MUFU_EX2, .dst_vreg = 0, .src1_vreg = 1 }; + if (encode_single_insn(&ex2, &ra, w) != 0) return -1; + if (w[0] != 0x00027308U || w[1] != 4 || w[2] != 0x00000800U) return -2; + /* BAR.SYNC.DEFER_BLOCKING 0x0 */ + BlackwellIRInsn bar = { .op = BW_IR_BAR_SYNC }; + if (encode_single_insn(&bar, &ra, w) != 0) return -3; + if (w[0] != 0x00007b1dU || w[1] != 0 || w[2] != 0x00010000U || w[3] != 0x000fec00U) return -4; + /* LDS R9, [R10+URZ] */ + BlackwellIRInsn lds = { .op = BW_IR_LDS32, .dst_vreg = 2, .src1_vreg = 3 }; + if (encode_single_insn(&lds, &ra, w) != 0) return -5; + if (w[0] != 0x0a097984U || w[1] != 0xff || w[2] != 0x08000800U) return -6; + /* STS [R8+URZ], R2 */ + BlackwellIRInsn sts = { .op = BW_IR_STS32, .src1_vreg = 4, .src2_vreg = 0 }; + if (encode_single_insn(&sts, &ra, w) != 0) return -7; + if (w[0] != 0x08007988U || w[1] != 2 || w[2] != 0x080008ffU) return -8; + return 0; +} + +int omega_bw_encode_program(const BlackwellIRProgram *prog, uint8_t *code_buf, size_t max_len, size_t *out_len) { + if (!prog || !code_buf || !out_len) return -1; + + size_t padded_insns = (prog->count + 7) & ~7ULL; + if (padded_insns < 32) padded_insns = 32; + + size_t req_bytes = padded_insns * 16; + if (max_len < req_bytes) return -1; + + uint8_t *p = code_buf; + for (size_t i = 0; i < padded_insns; i++) { + uint32_t w[4]; + if (i < prog->count) { + if (encode_single_insn(&prog->insns[i], &prog->regalloc, w) != 0) { + return -1; + } + } else { + /* Pad with NOP */ + w[0] = 0x00007918; + w[1] = 0x00000000; + w[2] = 0x00000000; + w[3] = 0x000fc000; + } + + p[0] = (uint8_t)(w[0] & 0xff); + p[1] = (uint8_t)((w[0] >> 8) & 0xff); + p[2] = (uint8_t)((w[0] >> 16) & 0xff); + p[3] = (uint8_t)((w[0] >> 24) & 0xff); + + p[4] = (uint8_t)(w[1] & 0xff); + p[5] = (uint8_t)((w[1] >> 8) & 0xff); + p[6] = (uint8_t)((w[1] >> 16) & 0xff); + p[7] = (uint8_t)((w[1] >> 24) & 0xff); + + p[8] = (uint8_t)(w[2] & 0xff); + p[9] = (uint8_t)((w[2] >> 8) & 0xff); + p[10] = (uint8_t)((w[2] >> 16) & 0xff); + p[11] = (uint8_t)((w[2] >> 24) & 0xff); + + p[12] = (uint8_t)(w[3] & 0xff); + p[13] = (uint8_t)((w[3] >> 8) & 0xff); + p[14] = (uint8_t)((w[3] >> 16) & 0xff); + p[15] = (uint8_t)((w[3] >> 24) & 0xff); + + p += 16; + } + + *out_len = req_bytes; + return 0; +} + +int omega_blackwell_codegen_matmul_i32(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel) { + if (!spec || !kernel) return -1; + if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; + + /* Fail closed loudly if K exceeds bounded unroll limit */ + if (spec->k > 32) { + fprintf(stderr, "ERROR: MatMul integer codegen exceeds bounded unroll limit (K=%u > 32)\n", spec->k); + return -1; + } + + BlackwellIRProgram prog; + omega_bw_ir_init(&prog); + + /* 1. Allocate virtual registers */ + /* 64-bit base pointers */ + int v_ptr_a = omega_bw_ir_alloc_vreg64(&prog); + int v_ptr_b = omega_bw_ir_alloc_vreg64(&prog); + int v_ptr_c = omega_bw_ir_alloc_vreg64(&prog); + int uv_desc = omega_bw_ir_alloc_uvreg64(&prog); + + /* 32-bit CTA / Thread indexing */ + int v_ctaid_x = omega_bw_ir_alloc_vreg(&prog); + int v_ctaid_y = omega_bw_ir_alloc_vreg(&prog); + int v_tid_x = omega_bw_ir_alloc_vreg(&prog); + int v_tid_y = omega_bw_ir_alloc_vreg(&prog); + + /* 2D Coordinates & loop invariants */ + int v_col = omega_bw_ir_alloc_vreg(&prog); + int v_row = omega_bw_ir_alloc_vreg(&prog); + int v_row_k = omega_bw_ir_alloc_vreg(&prog); + int v_const_1 = omega_bw_ir_alloc_vreg(&prog); + int v_acc = omega_bw_ir_alloc_vreg(&prog); + + /* Working registers for memory address and operands */ + int v_idx_a = omega_bw_ir_alloc_vreg(&prog); + int v_off_a = omega_bw_ir_alloc_vreg64(&prog); + int v_val_a = omega_bw_ir_alloc_vreg(&prog); + + int v_idx_b = omega_bw_ir_alloc_vreg(&prog); + int v_off_b = omega_bw_ir_alloc_vreg64(&prog); + int v_val_b = omega_bw_ir_alloc_vreg(&prog); + + int v_idx_c = omega_bw_ir_alloc_vreg(&prog); + int v_off_c = omega_bw_ir_alloc_vreg64(&prog); + + if (v_off_c < 0) return -1; + + /* 2. Load 64-bit global memory descriptor into uniform register pair */ + /* LDCU.64 uv_desc, c[0x0][0x358] */ + BlackwellIRInsn insn_desc = { .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true }; + omega_bw_ir_append(&prog, &insn_desc); + + /* 3. Load 64-bit base pointers from Constant Bank 0 */ + /* LDC.64 v_ptr_a, c[0x0][0x380] */ + BlackwellIRInsn insn_ptra = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380 }; + omega_bw_ir_append(&prog, &insn_ptra); + + /* LDC.64 v_ptr_b, c[0x0][0x388] */ + BlackwellIRInsn insn_ptrb = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388 }; + omega_bw_ir_append(&prog, &insn_ptrb); + + /* LDC.64 v_ptr_c, c[0x0][0x390] */ + BlackwellIRInsn insn_ptrc = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390 }; + omega_bw_ir_append(&prog, &insn_ptrc); + + /* 4. Query 2D CTA and Thread coordinates */ + /* S2R v_ctaid_x, SR_CTAID.X */ + BlackwellIRInsn insn_ctax = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X }; + omega_bw_ir_append(&prog, &insn_ctax); + + /* S2R v_ctaid_y, SR_CTAID.Y */ + BlackwellIRInsn insn_ctay = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y }; + omega_bw_ir_append(&prog, &insn_ctay); + + /* S2R v_tid_x, SR_TID.X */ + BlackwellIRInsn insn_tidx = { .op = BW_IR_S2R, .dst_vreg = v_tid_x, .imm = BW_SR_TID_X }; + omega_bw_ir_append(&prog, &insn_tidx); + + /* S2R v_tid_y, SR_TID.Y */ + BlackwellIRInsn insn_tidy = { .op = BW_IR_S2R, .dst_vreg = v_tid_y, .imm = BW_SR_TID_Y }; + omega_bw_ir_append(&prog, &insn_tidy); + + /* 5. Compute 2D (row, col) coordinates */ + /* Standard 16x16 tile threads: col = ctaid_x * 16 + tid_x */ + BlackwellIRInsn insn_col = { .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = 16, .src3_vreg = v_tid_x }; + omega_bw_ir_append(&prog, &insn_col); + + /* row = ctaid_y * 16 + tid_y */ + BlackwellIRInsn insn_row = { .op = BW_IR_IMAD, .dst_vreg = v_row, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = 16, .src3_vreg = v_tid_y }; + omega_bw_ir_append(&prog, &insn_row); + + /* 6. Precompute row base index: v_row_k = v_row * K + RZ */ + BlackwellIRInsn insn_row_k = { .op = BW_IR_IMAD, .dst_vreg = v_row_k, .src1_vreg = v_row, .src2_vreg = -1, .imm = spec->k, .src3_vreg = -1 }; + omega_bw_ir_append(&prog, &insn_row_k); + + /* 7. Initialize v_const_1 = 1 for immediate linear indexing */ + BlackwellIRInsn insn_c1 = { .op = BW_IR_MOV_IMM, .dst_vreg = v_const_1, .imm = 1 }; + omega_bw_ir_append(&prog, &insn_c1); + + /* 8. Initialize accumulator to zero: MOV v_acc, RZ */ + BlackwellIRInsn insn_init_acc = { .op = BW_IR_MOV_RZ, .dst_vreg = v_acc }; + omega_bw_ir_append(&prog, &insn_init_acc); + + /* 9. Synthesize inner accumulation loop across K iterations */ + for (uint32_t step = 0; step < spec->k; step++) { + /* A[row, step]: idx_a = v_row_k + step = v_const_1 * step + v_row_k */ + BlackwellIRInsn insn_idxa = { .op = BW_IR_IMAD, .dst_vreg = v_idx_a, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = step, .src3_vreg = v_row_k }; + omega_bw_ir_append(&prog, &insn_idxa); + + /* off_a = v_ptr_a + idx_a * 4 */ + BlackwellIRInsn insn_offa = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off_a, .src1_vreg = v_idx_a, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_a }; + omega_bw_ir_append(&prog, &insn_offa); + + /* Load A[row, step]: LDG.E v_val_a, desc[uv_desc][v_off_a.64] */ + BlackwellIRInsn insn_lda = { .op = BW_IR_LDG_E, .dst_vreg = v_val_a, .src1_vreg = v_off_a, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; + omega_bw_ir_append(&prog, &insn_lda); + + /* B[step, col]: idx_b = step * N + col = v_const_1 * (step * N) + v_col */ + uint32_t step_n = step * spec->n; + BlackwellIRInsn insn_idxb = { .op = BW_IR_IMAD, .dst_vreg = v_idx_b, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = step_n, .src3_vreg = v_col }; + omega_bw_ir_append(&prog, &insn_idxb); + + /* off_b = v_ptr_b + idx_b * 4 */ + BlackwellIRInsn insn_offb = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off_b, .src1_vreg = v_idx_b, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_b }; + omega_bw_ir_append(&prog, &insn_offb); + + /* Load B[step, col]: LDG.E v_val_b, desc[uv_desc][v_off_b.64] */ + BlackwellIRInsn insn_ldb = { .op = BW_IR_LDG_E, .dst_vreg = v_val_b, .src1_vreg = v_off_b, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; + omega_bw_ir_append(&prog, &insn_ldb); + + /* Multiply and accumulate: IMAD v_acc, v_val_a, v_val_b, v_acc */ + BlackwellIRInsn insn_acc = { .op = BW_IR_IMAD, .dst_vreg = v_acc, .src1_vreg = v_val_a, .src2_vreg = v_val_b, .src3_vreg = v_acc, .control = 0x004fca00 }; + omega_bw_ir_append(&prog, &insn_acc); + } + + /* 10. Compute output index: idx_c = row * N + col */ + BlackwellIRInsn insn_idxc = { .op = BW_IR_IMAD, .dst_vreg = v_idx_c, .src1_vreg = v_row, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col }; + omega_bw_ir_append(&prog, &insn_idxc); + + /* off_c = v_ptr_c + idx_c * 4 */ + BlackwellIRInsn insn_offc = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off_c, .src1_vreg = v_idx_c, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; + omega_bw_ir_append(&prog, &insn_offc); + + /* Store C[row, col]: STG.E desc[uv_desc][v_off_c.64], v_acc */ + BlackwellIRInsn insn_stc = { .op = BW_IR_STG_E, .src1_vreg = v_off_c, .src2_vreg = v_acc, .src3_vreg = -1, .ureg = uv_desc }; + omega_bw_ir_append(&prog, &insn_stc); + + /* 11. Kernel termination */ + BlackwellIRInsn insn_exit = { .op = BW_IR_EXIT, .predicate_p0 = false }; + omega_bw_ir_append(&prog, &insn_exit); + + BlackwellIRInsn insn_bra = { .op = BW_IR_BRA }; + omega_bw_ir_append(&prog, &insn_bra); + + /* 12. Solve bounded deterministic register allocation */ + if (omega_bw_regalloc_solve(&prog) != 0) { + return -1; + } + + /* 13. Allocate code buffer and encode instructions */ + size_t padded_insns = (prog.count + 7) & ~7ULL; + if (padded_insns < 32) padded_insns = 32; + size_t alloc_size = padded_insns * 16; + + kernel->code = (uint8_t *)malloc(alloc_size); + if (!kernel->code) return -1; + + size_t emitted_size = 0; + if (omega_bw_encode_program(&prog, kernel->code, alloc_size, &emitted_size) != 0) { + free(kernel->code); + kernel->code = NULL; + return -1; + } + + kernel->code_size = emitted_size; + kernel->insn_count = emitted_size / 16; + kernel->gpr_count = prog.regalloc.peak_gpr_usage; + kernel->uniform_gpr_count = prog.regalloc.peak_ugpr_usage; + + /* 14. Compute runtime SHA-256 digest of emitted machine code */ + sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); + return 0; +} + +int omega_blackwell_codegen_matmul_tensor_prog(const OmegaMatMulSpec *spec, BlackwellIRProgram *prog) { + if (!spec || !prog) return -1; + if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; + if (spec->precision != OMEGA_MATMUL_PRECISION_FP16 && spec->precision != OMEGA_MATMUL_PRECISION_BF16) return -1; + if (spec->m % 16 != 0 || spec->n % 8 != 0 || spec->k != 16) { + fprintf(stderr, "ERROR: MatMul tensor codegen requires M multiple of 16, N multiple of 8, K=16 (got %ux%ux%u)\n", + spec->m, spec->k, spec->n); + return -1; + } + + omega_bw_ir_init(prog); + + /* 1. Allocate virtual registers */ + /* 64-bit base pointers */ + int v_ptr_a = omega_bw_ir_alloc_vreg64(prog); + int v_ptr_b = omega_bw_ir_alloc_vreg64(prog); + int v_ptr_c = omega_bw_ir_alloc_vreg64(prog); + int uv_desc = omega_bw_ir_alloc_uvreg64(prog); + + /* 32-bit CTA / Thread indexing */ + int v_tid = omega_bw_ir_alloc_vreg(prog); + int v_ctaid_x = omega_bw_ir_alloc_vreg(prog); + int v_ctaid_y = omega_bw_ir_alloc_vreg(prog); + + /* Tile and thread coordinates */ + int v_group_id = omega_bw_ir_alloc_vreg(prog); + int v_tid_in_grp = omega_bw_ir_alloc_vreg(prog); + int v_row_base = omega_bw_ir_alloc_vreg(prog); + int v_col_base = omega_bw_ir_alloc_vreg(prog); + int v_r0 = omega_bw_ir_alloc_vreg(prog); + int v_r1 = omega_bw_ir_alloc_vreg(prog); + int v_col_b = omega_bw_ir_alloc_vreg(prog); + int v_tid_col2 = omega_bw_ir_alloc_vreg(prog); + int v_const_1 = omega_bw_ir_alloc_vreg(prog); + + /* Bundles for HMMA */ + int v_ra = omega_bw_ir_alloc_vreg128(prog); /* Quad: Ra[0]..Ra[3] */ + int v_rb = omega_bw_ir_alloc_vreg64(prog); /* Pair: Rb[0]..Rb[1] */ + int v_rd = omega_bw_ir_alloc_vreg128(prog); /* Quad: Rd[0]..Rd[3] */ + + /* Scratch registers for indexing and loads */ + int v_idx = omega_bw_ir_alloc_vreg(prog); + int v_off = omega_bw_ir_alloc_vreg64(prog); + int v_b_tmp0 = omega_bw_ir_alloc_vreg(prog); + int v_b_tmp1 = omega_bw_ir_alloc_vreg(prog); + + if (v_b_tmp1 < 0) return -1; + + /* 2. Load 64-bit global memory descriptor into uniform register pair */ + /* LDCU.64 uv_desc, c[0x0][0x358] */ + BlackwellIRInsn insn_desc = { .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true }; + omega_bw_ir_append(prog, &insn_desc); + + /* 3. Load 64-bit base pointers from Constant Bank 0 */ + /* LDC.64 v_ptr_a, c[0x0][0x380] */ + BlackwellIRInsn insn_ptra = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380 }; + omega_bw_ir_append(prog, &insn_ptra); + + /* LDC.64 v_ptr_b, c[0x0][0x388] */ + BlackwellIRInsn insn_ptrb = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388 }; + omega_bw_ir_append(prog, &insn_ptrb); + + /* LDC.64 v_ptr_c, c[0x0][0x390] */ + BlackwellIRInsn insn_ptrc = { .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390 }; + omega_bw_ir_append(prog, &insn_ptrc); + + /* 4. Query Special Registers */ + /* S2R v_tid, SR_TID.X */ + BlackwellIRInsn insn_tidx = { .op = BW_IR_S2R, .dst_vreg = v_tid, .imm = BW_SR_TID_X }; + omega_bw_ir_append(prog, &insn_tidx); + + /* S2R v_ctaid_x, SR_CTAID.X */ + BlackwellIRInsn insn_cta_x = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X }; + omega_bw_ir_append(prog, &insn_cta_x); + + /* S2R v_ctaid_y, SR_CTAID.Y */ + BlackwellIRInsn insn_cta_y = { .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y }; + omega_bw_ir_append(prog, &insn_cta_y); + + /* 5. Set up constant 1 */ + BlackwellIRInsn insn_c1 = { .op = BW_IR_MOV_IMM, .dst_vreg = v_const_1, .imm = 1 }; + omega_bw_ir_append(prog, &insn_c1); + + /* 6. Extract warp-level tile and thread coordinates */ + /* group_id = tid >> 2 */ + BlackwellIRInsn insn_grp = { .op = BW_IR_SHF_R, .dst_vreg = v_group_id, .src1_vreg = v_tid, .imm = 2 }; + omega_bw_ir_append(prog, &insn_grp); + + /* tid_in_grp = tid & 3 */ + BlackwellIRInsn insn_tig = { .op = BW_IR_LOP3_AND, .dst_vreg = v_tid_in_grp, .src1_vreg = v_tid, .imm = 3 }; + omega_bw_ir_append(prog, &insn_tig); + + /* row_base = ctaid_y * 16 */ + BlackwellIRInsn insn_rbase = { .op = BW_IR_IMAD, .dst_vreg = v_row_base, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = 16, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_rbase); + + /* col_base = ctaid_x * 8 */ + BlackwellIRInsn insn_cbase = { .op = BW_IR_IMAD, .dst_vreg = v_col_base, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = 8, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_cbase); + + /* r0 = row_base + group_id */ + BlackwellIRInsn insn_r0 = { .op = BW_IR_IADD3, .dst_vreg = v_r0, .src1_vreg = v_row_base, .src2_vreg = v_group_id, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_r0); + + /* r1 = r0 + 8 */ + BlackwellIRInsn insn_r1 = { .op = BW_IR_IMAD, .dst_vreg = v_r1, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 8, .src3_vreg = v_r0 }; + omega_bw_ir_append(prog, &insn_r1); + + /* col_b = col_base + group_id */ + BlackwellIRInsn insn_colb = { .op = BW_IR_IADD3, .dst_vreg = v_col_b, .src1_vreg = v_col_base, .src2_vreg = v_group_id, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_colb); + + /* tid_col2 = tid_in_grp * 2 */ + BlackwellIRInsn insn_tcol2 = { .op = BW_IR_IMAD, .dst_vreg = v_tid_col2, .src1_vreg = v_tid_in_grp, .src2_vreg = -1, .imm = 2, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_tcol2); + + /* 7. Load A fragments (16x16 row-major, 2 bytes/element, row stride 32 bytes) */ + /* Pair 0: a0, a1 at r0 * 32 + tid_col2 * 2 */ + BlackwellIRInsn insn_a0_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_a0_idx1); + BlackwellIRInsn insn_a0_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = 2, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_a0_idx2); + BlackwellIRInsn insn_a0_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 1, .src3_vreg = v_ptr_a }; + omega_bw_ir_append(prog, &insn_a0_off); + BlackwellIRInsn insn_lda0 = { .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 0, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; + omega_bw_ir_append(prog, &insn_lda0); + + /* Pair 1: a2, a3 at r1 * 32 + tid_col2 * 2 */ + BlackwellIRInsn insn_a1_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_a1_idx1); + BlackwellIRInsn insn_a1_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = 2, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_a1_idx2); + BlackwellIRInsn insn_a1_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 1, .src3_vreg = v_ptr_a }; + omega_bw_ir_append(prog, &insn_a1_off); + BlackwellIRInsn insn_lda1 = { .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 1, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; + omega_bw_ir_append(prog, &insn_lda1); + + /* Pair 2: a4, a5 at r0 * 32 + tid_col2 * 2 + 16 */ + BlackwellIRInsn insn_a2_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_a2_idx1); + BlackwellIRInsn insn_a2_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = 2, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_a2_idx2); + BlackwellIRInsn insn_a2_idx3 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 16, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_a2_idx3); + BlackwellIRInsn insn_a2_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 1, .src3_vreg = v_ptr_a }; + omega_bw_ir_append(prog, &insn_a2_off); + BlackwellIRInsn insn_lda2 = { .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 2, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; + omega_bw_ir_append(prog, &insn_lda2); + + /* Pair 3: a6, a7 at r1 * 32 + tid_col2 * 2 + 16 */ + BlackwellIRInsn insn_a3_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = 32, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_a3_idx1); + BlackwellIRInsn insn_a3_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = 2, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_a3_idx2); + BlackwellIRInsn insn_a3_idx3 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 16, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_a3_idx3); + BlackwellIRInsn insn_a3_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 1, .src3_vreg = v_ptr_a }; + omega_bw_ir_append(prog, &insn_a3_off); + BlackwellIRInsn insn_lda3 = { .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 3, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x001ea800 }; + omega_bw_ir_append(prog, &insn_lda3); + + /* 8. Load and pack B fragments (16xN row-major, 2 bytes/element) */ + /* b0 at tid_col2 * N + col_b */ + BlackwellIRInsn insn_b0_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col_b }; + omega_bw_ir_append(prog, &insn_b0_idx); + BlackwellIRInsn insn_b0_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b }; + omega_bw_ir_append(prog, &insn_b0_off); + BlackwellIRInsn insn_ldb0 = { .op = BW_IR_LDG_E_U16, .dst_vreg = v_b_tmp0, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; + omega_bw_ir_append(prog, &insn_ldb0); + + /* b1 at (tid_col2 + 1) * N + col_b = b0_idx + N */ + BlackwellIRInsn insn_b1_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_b1_idx); + BlackwellIRInsn insn_b1_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b }; + omega_bw_ir_append(prog, &insn_b1_off); + BlackwellIRInsn insn_ldb1 = { .op = BW_IR_LDG_E_U16, .dst_vreg = v_b_tmp1, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; + omega_bw_ir_append(prog, &insn_ldb1); + + /* Pack Rb[0] = (b1 << 16) | b0 (wait on barrier 2) */ + BlackwellIRInsn insn_pack_b0 = { .op = BW_IR_IMAD, .dst_vreg = v_rb, .dst_subreg = 0, .src1_vreg = v_b_tmp1, .src2_vreg = -1, .imm = 0x10000, .src3_vreg = v_b_tmp0, .control = 0x004fca00 }; + omega_bw_ir_append(prog, &insn_pack_b0); + + /* b2 at (tid_col2 + 8) * N + col_b */ + BlackwellIRInsn insn_b2_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tid_col2, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col_b }; + omega_bw_ir_append(prog, &insn_b2_idx1); + BlackwellIRInsn insn_b2_idx2 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 8 * spec->n, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_b2_idx2); + BlackwellIRInsn insn_b2_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b }; + omega_bw_ir_append(prog, &insn_b2_off); + BlackwellIRInsn insn_ldb2 = { .op = BW_IR_LDG_E_U16, .dst_vreg = v_b_tmp0, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; + omega_bw_ir_append(prog, &insn_ldb2); + + /* b3 at (tid_col2 + 9) * N + col_b = b2_idx + N */ + BlackwellIRInsn insn_b3_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_b3_idx); + BlackwellIRInsn insn_b3_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b }; + omega_bw_ir_append(prog, &insn_b3_off); + BlackwellIRInsn insn_ldb3 = { .op = BW_IR_LDG_E_U16, .dst_vreg = v_b_tmp1, .src1_vreg = v_off, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = 0x002ea800 }; + omega_bw_ir_append(prog, &insn_ldb3); + + /* Pack Rb[1] = (b3 << 16) | b2 (wait on barrier 2) */ + BlackwellIRInsn insn_pack_b1 = { .op = BW_IR_IMAD, .dst_vreg = v_rb, .dst_subreg = 1, .src1_vreg = v_b_tmp1, .src2_vreg = -1, .imm = 0x10000, .src3_vreg = v_b_tmp0, .control = 0x004fca00 }; + omega_bw_ir_append(prog, &insn_pack_b1); + + /* 9. Execute HMMA Tensor Core instruction (wait on barrier 1 for A loads) */ + BlackwellIROpcode mma_op = (spec->precision == OMEGA_MATMUL_PRECISION_BF16) ? BW_IR_HMMA_BF16 : BW_IR_HMMA_F16; + BlackwellIRInsn insn_hmma = { + .op = mma_op, + .dst_vreg = v_rd, + .dst_subreg = 0, + .src1_vreg = v_ra, + .src1_subreg = 0, + .src2_vreg = v_rb, + .src2_subreg = 0, + .src3_vreg = -1, /* RZ accumulator input */ + .control = 0x002fca00 /* Wait on barrier 1 */ + }; + omega_bw_ir_append(prog, &insn_hmma); + + /* 10. Store C output fragments (16xN row-major, 4 bytes/float element) */ + /* d0: row r0, col col_base + tid_col2 + 0 */ + BlackwellIRInsn insn_d0_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col_base }; + omega_bw_ir_append(prog, &insn_d0_idx1); + BlackwellIRInsn insn_d0_idx2 = { .op = BW_IR_IADD3, .dst_vreg = v_idx, .src1_vreg = v_idx, .src2_vreg = v_tid_col2, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_d0_idx2); + BlackwellIRInsn insn_d0_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; + omega_bw_ir_append(prog, &insn_d0_off); + BlackwellIRInsn insn_std0 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 0, .ureg = uv_desc, .control = 0x000fe200 }; + omega_bw_ir_append(prog, &insn_std0); + + /* d1: row r0, col col_base + tid_col2 + 1 */ + BlackwellIRInsn insn_d1_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 1, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_d1_idx); + BlackwellIRInsn insn_d1_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; + omega_bw_ir_append(prog, &insn_d1_off); + BlackwellIRInsn insn_std1 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 1, .ureg = uv_desc, .control = 0x000fe200 }; + omega_bw_ir_append(prog, &insn_std1); + + /* d2: row r1, col col_base + tid_col2 + 0 */ + BlackwellIRInsn insn_d2_idx1 = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = spec->n, .src3_vreg = v_col_base }; + omega_bw_ir_append(prog, &insn_d2_idx1); + BlackwellIRInsn insn_d2_idx2 = { .op = BW_IR_IADD3, .dst_vreg = v_idx, .src1_vreg = v_idx, .src2_vreg = v_tid_col2, .src3_vreg = -1 }; + omega_bw_ir_append(prog, &insn_d2_idx2); + BlackwellIRInsn insn_d2_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; + omega_bw_ir_append(prog, &insn_d2_off); + BlackwellIRInsn insn_std2 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 2, .ureg = uv_desc, .control = 0x000fe200 }; + omega_bw_ir_append(prog, &insn_std2); + + /* d3: row r1, col col_base + tid_col2 + 1 */ + BlackwellIRInsn insn_d3_idx = { .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_const_1, .src2_vreg = -1, .imm = 1, .src3_vreg = v_idx }; + omega_bw_ir_append(prog, &insn_d3_idx); + BlackwellIRInsn insn_d3_off = { .op = BW_IR_IMAD_WIDE, .dst_vreg = v_off, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c }; + omega_bw_ir_append(prog, &insn_d3_off); + BlackwellIRInsn insn_std3 = { .op = BW_IR_STG_E, .src1_vreg = v_off, .src2_vreg = v_rd, .src2_subreg = 3, .ureg = uv_desc, .control = 0x000fe200 }; + omega_bw_ir_append(prog, &insn_std3); + + /* 11. Program Exit */ + BlackwellIRInsn insn_exit = { .op = BW_IR_EXIT }; + omega_bw_ir_append(prog, &insn_exit); + BlackwellIRInsn insn_bra = { .op = BW_IR_BRA }; + omega_bw_ir_append(prog, &insn_bra); + + /* 12. Run bounded register allocation */ + if (omega_bw_regalloc_solve(prog) != 0) { + return -1; + } + return 0; +} + +int omega_blackwell_codegen_matmul_tensor(const OmegaMatMulSpec *spec, OmegaBlackwellKernel *kernel) { + if (!spec || !kernel) return -1; + BlackwellIRProgram prog; + if (omega_blackwell_codegen_matmul_tensor_prog(spec, &prog) != 0) { + return -1; + } + + size_t max_bytes = 4096; + kernel->code = malloc(max_bytes); + if (!kernel->code) return -1; + + size_t emitted_size = 0; + if (omega_bw_encode_program(&prog, kernel->code, max_bytes, &emitted_size) != 0) { + free(kernel->code); + kernel->code = NULL; + return -1; + } + + kernel->code_size = emitted_size; + kernel->insn_count = emitted_size / 16; + kernel->gpr_count = prog.regalloc.peak_gpr_usage; + kernel->uniform_gpr_count = prog.regalloc.peak_ugpr_usage; + + sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); + return 0; +} + +/* ---- FB-1 cut 1b: looped tensor matmul ------------------------------------------ + * + * Control words (bundle bits 105..125, decoded from the chip-proven kernel and from + * the vendor compiler's SASS for an equivalent HMMA loop on this chip, 2026-10-04): + * stall = bits 9..12 of w[3], yield = 13, write barrier = 14..16, read barrier = + * 17..19, wait mask = 20..25. 7 in a barrier field means "none". + * LDG (A) 0x001ea800 stall 4, yield, sets SB2 + * LDG.U16 (B) 0x002ea800 stall 4, yield, sets SB2 + * pack IMAD 0x004fca00 stall 5, waits SB2 (every outstanding load has landed) + * HMMA 0x002fca00 stall 5 (fixed latency: the vendor compiler sets no + * write barrier on HMMA either; consumers wait by stall count) + * STG 0x0007e200 stall 1, yield, read barrier SB3 (the store may read its + * data/address registers late; the next writer waits SB3) + * wait SB3 mask bit 23 (0x00800000) on the first instruction that overwrites a + * register a store reads: the C pointer advance. + * Loop-carried registers are kept live to the end of their loop (keep_alive) because + * the allocator is a straight-line linear scan and knows nothing about back edges. + */ +static void bw_keep_alive(BlackwellIRProgram *prog, int from_idx, int to_idx, int last_idx) { + /* every vreg first defined in [from_idx, to_idx) stays live until last_idx */ + for (int v = 0; v < prog->regalloc.num_vregs; v++) { + OmegaLiveInterval *iv = &prog->regalloc.intervals[v]; + if (iv->first_def >= from_idx && iv->first_def < to_idx && iv->last_use < last_idx) + iv->last_use = last_idx; + } +} + +#define BW_APPEND(prog, ...) do { BlackwellIRInsn _i = { __VA_ARGS__ }; if (omega_bw_ir_append((prog), &_i) < 0) return -1; } while (0) + +int omega_blackwell_codegen_matmul_tensor_loop_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, + int mutant, BlackwellIRProgram *prog) { + if (!spec || !prog) return -1; + if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; + if (spec->precision != OMEGA_MATMUL_PRECISION_FP16 && spec->precision != OMEGA_MATMUL_PRECISION_BF16) return -1; + if (spec->m % 16 != 0 || spec->n % 8 != 0 || spec->k % 16 != 0) { + fprintf(stderr, "ERROR: looped MatMul tensor codegen requires M%%16, N%%8, K%%16 (got %ux%ux%u)\n", + spec->m, spec->k, spec->n); + return -1; + } + const uint32_t K = spec->k, N = spec->n, NT = N / 8, KSTEPS = K / 16; + if (grid_x == 0 || grid_x > NT) return -1; + /* every immediate below must fit 32 bits: 32 * N (B row-block stride in bytes) is the largest */ + if ((uint64_t)N * 32u > 0xffffffffull || (uint64_t)grid_x * 32u > 0xffffffffull) return -1; + if (mutant && KSTEPS < 2) return -1; + + omega_bw_ir_init(prog); + + int v_ptr_a = omega_bw_ir_alloc_vreg64(prog), v_ptr_b = omega_bw_ir_alloc_vreg64(prog), v_ptr_c = omega_bw_ir_alloc_vreg64(prog); + int uv_desc = omega_bw_ir_alloc_uvreg64(prog); + int v_tid = omega_bw_ir_alloc_vreg(prog), v_ctaid_x = omega_bw_ir_alloc_vreg(prog), v_ctaid_y = omega_bw_ir_alloc_vreg(prog); + int v_group = omega_bw_ir_alloc_vreg(prog), v_tig = omega_bw_ir_alloc_vreg(prog), v_tcol2 = omega_bw_ir_alloc_vreg(prog); + int v_r0 = omega_bw_ir_alloc_vreg(prog), v_r1 = omega_bw_ir_alloc_vreg(prog), v_c1 = omega_bw_ir_alloc_vreg(prog); + int v_idx = omega_bw_ir_alloc_vreg(prog), v_col = omega_bw_ir_alloc_vreg(prog), v_cnt = omega_bw_ir_alloc_vreg(prog); + int v_pa0 = omega_bw_ir_alloc_vreg64(prog), v_pa1 = omega_bw_ir_alloc_vreg64(prog), v_pbcol = omega_bw_ir_alloc_vreg64(prog); + int v_pc0 = omega_bw_ir_alloc_vreg64(prog), v_pc1 = omega_bw_ir_alloc_vreg64(prog); + int v_pa0c = omega_bw_ir_alloc_vreg64(prog), v_pa1c = omega_bw_ir_alloc_vreg64(prog), v_pbc = omega_bw_ir_alloc_vreg64(prog); + int v_ta2 = omega_bw_ir_alloc_vreg64(prog), v_ta3 = omega_bw_ir_alloc_vreg64(prog); + int v_tb1 = omega_bw_ir_alloc_vreg64(prog), v_tb2 = omega_bw_ir_alloc_vreg64(prog), v_tb3 = omega_bw_ir_alloc_vreg64(prog); + int v_tc1 = omega_bw_ir_alloc_vreg64(prog), v_tc3 = omega_bw_ir_alloc_vreg64(prog); + int v_ra = omega_bw_ir_alloc_vreg128(prog), v_rb = omega_bw_ir_alloc_vreg64(prog), v_rd = omega_bw_ir_alloc_vreg128(prog); + int v_b0 = omega_bw_ir_alloc_vreg(prog), v_b1 = omega_bw_ir_alloc_vreg(prog); + if (v_b1 < 0 || uv_desc < 0) return -1; + + const uint32_t CTRL_LDA = 0x001ea800u, CTRL_LDB = 0x002ea800u, CTRL_PACK = 0x004fca00u; + const uint32_t CTRL_HMMA = 0x002fca00u, CTRL_STG_RDBAR3 = 0x0007e200u, CTRL_IMADW_WAIT3 = 0x009fcc00u; + const uint32_t CTRL_MOVRZ_WAIT3 = 0x008fe200u; + + /* prologue: descriptor, pointers, ids */ + BW_APPEND(prog, .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true); + BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380); + BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388); + BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390); + BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_tid, .imm = BW_SR_TID_X); + BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X); + BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y); + BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_c1, .imm = 1); + BW_APPEND(prog, .op = BW_IR_SHF_R, .dst_vreg = v_group, .src1_vreg = v_tid, .imm = 2); + BW_APPEND(prog, .op = BW_IR_LOP3_AND, .dst_vreg = v_tig, .src1_vreg = v_tid, .imm = 3); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_tcol2, .src1_vreg = v_tig, .src2_vreg = -1, .imm = 2, .src3_vreg = -1); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r0, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = 16, .src3_vreg = v_group); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 8, .src3_vreg = v_r0); + /* pa0 = A + (r0*K + tcol2)*2 ; pa1 likewise with r1 (A row-major, K bf16 per row) */ + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = K, .src3_vreg = v_tcol2); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa0, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_a); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = K, .src3_vreg = v_tcol2); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa1, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_a); + /* col = ctaid_x (first column tile of this CTA) */ + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = 1, .src3_vreg = -1); + /* pbcol = B + (tcol2*N + col*8 + group)*2 */ + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_tcol2, .src2_vreg = -1, .imm = N, .src3_vreg = v_group); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbcol, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 2, .src3_vreg = v_ptr_b); + /* pc0 = C + (r0*N + col*8 + tcol2)*4 ; pc1 with r1 */ + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc0, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc1, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c); + + /* ---- column-tile loop ---- */ + const int L_col = (int)prog->count; + /* accumulator = 0 (the first write waits SB3: the previous tile's stores may still read it) */ + BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 0, .control = CTRL_MOVRZ_WAIT3); + BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 1); + BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 2); + BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 3); + /* K cursors start at the row/column bases (IMAD.WIDE with imm 0 is a 64-bit copy) */ + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa0c, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pa0); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa1c, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pa1); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbc, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pbcol); + BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_cnt, .imm = mutant ? KSTEPS - 1 : KSTEPS); + + /* ---- K loop: one 16x16 A fragment and one 16x8 B fragment per step ---- */ + const int L_k = (int)prog->count; + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_ta2, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 16, .src3_vreg = v_pa0c); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_ta3, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 16, .src3_vreg = v_pa1c); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tb1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 2u * N, .src3_vreg = v_pbc); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tb2, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 16u * N, .src3_vreg = v_pbc); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tb3, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 18u * N, .src3_vreg = v_pbc); + /* A: a0,a1 at (r0, kk+tcol2) ; a2,a3 at (r1, ...) ; a4,a5 at (r0, +8) ; a6,a7 at (r1, +8) */ + BW_APPEND(prog, .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 0, .src1_vreg = v_pa0c, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDA); + BW_APPEND(prog, .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 1, .src1_vreg = v_pa1c, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDA); + BW_APPEND(prog, .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 2, .src1_vreg = v_ta2, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDA); + BW_APPEND(prog, .op = BW_IR_LDG_E, .dst_vreg = v_ra, .dst_subreg = 3, .src1_vreg = v_ta3, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDA); + /* B: rows kk+tcol2, +1, +8, +9 at column col*8+group */ + BW_APPEND(prog, .op = BW_IR_LDG_E_U16, .dst_vreg = v_b0, .src1_vreg = v_pbc, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDB); + BW_APPEND(prog, .op = BW_IR_LDG_E_U16, .dst_vreg = v_b1, .src1_vreg = v_tb1, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDB); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_rb, .dst_subreg = 0, .src1_vreg = v_b1, .src2_vreg = -1, .imm = 0x10000, .src3_vreg = v_b0, .control = CTRL_PACK); + BW_APPEND(prog, .op = BW_IR_LDG_E_U16, .dst_vreg = v_b0, .src1_vreg = v_tb2, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDB); + BW_APPEND(prog, .op = BW_IR_LDG_E_U16, .dst_vreg = v_b1, .src1_vreg = v_tb3, .src2_vreg = -1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_LDB); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_rb, .dst_subreg = 1, .src1_vreg = v_b1, .src2_vreg = -1, .imm = 0x10000, .src3_vreg = v_b0, .control = CTRL_PACK); + /* rd += ra * rb (accumulate in place) */ + BW_APPEND(prog, .op = (spec->precision == OMEGA_MATMUL_PRECISION_BF16) ? BW_IR_HMMA_BF16 : BW_IR_HMMA_F16, + .dst_vreg = v_rd, .dst_subreg = 0, .src1_vreg = v_ra, .src1_subreg = 0, .src2_vreg = v_rb, .src2_subreg = 0, + .src3_vreg = v_rd, .src3_subreg = 0, .control = CTRL_HMMA); + /* advance K: A by 16 bf16 (32 bytes), B by 16 rows (32 N bytes); every load above has landed (packs waited SB2) */ + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa0c, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 32, .src3_vreg = v_pa0c); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa1c, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 32, .src3_vreg = v_pa1c); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbc, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 32u * N, .src3_vreg = v_pbc); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_cnt, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0xffffffffu, .src3_vreg = v_cnt); + BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_cnt, .src2_vreg = -1, .imm = 1, .src3_vreg = -1); + { + int here = (int)prog->count; + BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_k - here), .predicate_p0 = true); + } + const int L_k_end = (int)prog->count - 1; + + /* ---- store the 16x8 tile: d0 (r0, c), d1 (r0, c+1), d2 (r1, c), d3 (r1, c+1) ---- */ + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tc1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 4, .src3_vreg = v_pc0); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tc3, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 4, .src3_vreg = v_pc1); + BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc0, .src2_vreg = v_rd, .src2_subreg = 0, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); + BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_tc1, .src2_vreg = v_rd, .src2_subreg = 1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); + BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc1, .src2_vreg = v_rd, .src2_subreg = 2, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); + BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_tc3, .src2_vreg = v_rd, .src2_subreg = 3, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); + /* next column tile of this CTA: col += grid_x ; B by grid_x*8 bf16 ; C by grid_x*8 f32 (waits SB3 first) */ + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x, .src3_vreg = v_col); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc0, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * 32u, .src3_vreg = v_pc0, .control = CTRL_IMADW_WAIT3); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * 32u, .src3_vreg = v_pc1); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbcol, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * 16u, .src3_vreg = v_pbcol); + BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_col, .src2_vreg = -1, .imm = NT, .src3_vreg = -1); + { + int here = (int)prog->count; + BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_col - here), .predicate_p0 = true, .predicate_not = true); + } + const int L_col_end = (int)prog->count - 1; + BW_APPEND(prog, .op = BW_IR_EXIT); + BW_APPEND(prog, .op = BW_IR_BRA); + + /* loop-carried values live to the end of their loop */ + bw_keep_alive(prog, 0, L_col, L_col_end); + bw_keep_alive(prog, L_col, L_k, L_k_end); + /* K-loop temporaries too: a load's address pair must never be recycled by a + * later instruction of the same iteration (the load may read it late). */ + bw_keep_alive(prog, L_k, L_k_end, L_k_end); + /* the accumulator is read by the stores after the K loop */ + if (prog->regalloc.intervals[v_rd].last_use < L_col_end) prog->regalloc.intervals[v_rd].last_use = L_col_end; + + return omega_bw_regalloc_solve(prog); +} + +int omega_blackwell_codegen_matmul_tensor_loop(const OmegaMatMulSpec *spec, uint32_t grid_x, + int mutant, OmegaBlackwellKernel *kernel) { + if (!spec || !kernel) return -1; + BlackwellIRProgram prog; + if (omega_blackwell_codegen_matmul_tensor_loop_prog(spec, grid_x, mutant, &prog) != 0) return -1; + size_t max_bytes = 4096; + kernel->code = malloc(max_bytes); + if (!kernel->code) return -1; + size_t emitted_size = 0; + if (omega_bw_encode_program(&prog, kernel->code, max_bytes, &emitted_size) != 0) { + free(kernel->code); + kernel->code = NULL; + return -1; + } + kernel->code_size = emitted_size; + kernel->insn_count = emitted_size / 16; + kernel->gpr_count = prog.regalloc.peak_gpr_usage; + kernel->uniform_gpr_count = prog.regalloc.peak_ugpr_usage; + sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); + return 0; +} + +int omega_blackwell_verify_codegen_fixtures(void) { + OmegaRegAlloc ra; + memset(&ra, 0, sizeof(ra)); + ra.vreg_to_phys[0] = 2; + ra.vreg_to_phys[1] = 4; + ra.vreg_to_phys[2] = 6; + ra.vreg_to_phys[3] = 7; + ra.uvreg_to_phys[0] = 4; + + uint32_t w[4]; + + /* 1. MOV R7, RZ */ + BlackwellIRInsn insn_mov_rz = { .op = BW_IR_MOV_RZ, .dst_vreg = 3 }; + if (encode_single_insn(&insn_mov_rz, &ra, w) != 0) return -1; + if (w[0] != (0x7202 | (7 << 16)) || w[1] != 0xff || w[2] != 0x00000f00) return -2; + + /* 2. MOV R2, 1 */ + BlackwellIRInsn insn_mov_imm = { .op = BW_IR_MOV_IMM, .dst_vreg = 0, .imm = 1 }; + if (encode_single_insn(&insn_mov_imm, &ra, w) != 0) return -3; + if (w[0] != (0x7802 | (2 << 16)) || w[1] != 1 || w[2] != 0x00000f00) return -4; + + /* 3. Register-Register IMAD: IMAD R7, R2, R6, R7 */ + BlackwellIRInsn insn_imad_rr = { .op = BW_IR_IMAD, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 2, .src3_vreg = 3 }; + if (encode_single_insn(&insn_imad_rr, &ra, w) != 0) return -5; + if (w[0] != (0x7224 | (7 << 16) | (2 << 24)) || w[1] != 6 || w[2] != (0x078e0200 | 7)) return -6; + + /* 4. Immediate IMAD: IMAD R6, R2, 16, RZ */ + BlackwellIRInsn insn_imad_imm = { .op = BW_IR_IMAD, .dst_vreg = 2, .src1_vreg = 0, .src2_vreg = -1, .imm = 16, .src3_vreg = -1 }; + if (encode_single_insn(&insn_imad_imm, &ra, w) != 0) return -7; + if (w[0] != (0x7824 | (6 << 16) | (2 << 24)) || w[1] != 16 || w[2] != 0x078e02ff) return -8; + + /* 5. IMAD.WIDE: IMAD.WIDE R4, R2, 4, R4 */ + BlackwellIRInsn insn_imad_wide = { .op = BW_IR_IMAD_WIDE, .dst_vreg = 1, .src1_vreg = 0, .imm = 4, .src3_vreg = 1 }; + if (encode_single_insn(&insn_imad_wide, &ra, w) != 0) return -9; + if (w[0] != (0x7825 | (4 << 16) | (2 << 24)) || w[1] != 4 || w[2] != (0x078e0000 | 4)) return -10; + + /* 6. LDC.64 R4, c[0x0][0x380] */ + BlackwellIRInsn insn_ldc64 = { .op = BW_IR_LDC64, .dst_vreg = 1, .imm = 0x380 }; + if (encode_single_insn(&insn_ldc64, &ra, w) != 0) return -11; + if (w[0] != (0xff007b82 | (4 << 16)) || w[1] != ((0x380 << 6) & 0x00ffff00) || w[2] != 0x00000a00) return -12; + + /* 7. LDCU.64 UR4, c[0x0][0x358] */ + BlackwellIRInsn insn_ldcu64 = { .op = BW_IR_LDCU64, .dst_vreg = 0, .imm = 0x358, .is_uniform = true }; + if (encode_single_insn(&insn_ldcu64, &ra, w) != 0) return -13; + if (w[0] != (0xff0077ac | (4 << 16)) || w[1] != ((0x358 << 5) & 0x00ffff00) || w[2] != 0x08000a00) return -14; + + /* 8. LDG.E R2, desc[UR4][R4.64] */ + BlackwellIRInsn insn_ldg = { .op = BW_IR_LDG_E, .dst_vreg = 0, .src1_vreg = 1, .ureg = 0 }; + if (encode_single_insn(&insn_ldg, &ra, w) != 0) return -15; + if (w[0] != (0x7981 | (2 << 16) | (4 << 24)) || w[1] != 4 || w[2] != 0x0c1e1900) return -16; + + /* 9. STG.E desc[UR4][R4.64], R7 */ + BlackwellIRInsn insn_stg = { .op = BW_IR_STG_E, .src1_vreg = 1, .src2_vreg = 3, .ureg = 0 }; + if (encode_single_insn(&insn_stg, &ra, w) != 0) return -17; + if (w[0] != (0x7986 | (4 << 24)) || w[1] != 7 || w[2] != (0x0c101900 | 4)) return -18; + + /* 10. S2R R2, SR_CTAID.X */ + BlackwellIRInsn insn_s2r = { .op = BW_IR_S2R, .dst_vreg = 0, .imm = BW_SR_CTAID_X }; + if (encode_single_insn(&insn_s2r, &ra, w) != 0) return -19; + if (w[0] != (0x7919 | (2 << 16)) || w[2] != (BW_SR_CTAID_X << 8)) return -20; + + /* 11. HMMA.16816.F32 R4, R4, R2, RZ */ + BlackwellIRInsn insn_hmma_f16 = { .op = BW_IR_HMMA_F16, .dst_vreg = 1, .src1_vreg = 1, .src2_vreg = 0, .src3_vreg = -1 }; + if (encode_single_insn(&insn_hmma_f16, &ra, w) != 0) return -21; + if (w[0] != 0x0404723c || w[1] != 0x00000002 || w[2] != 0x000018ff || w[3] != 0x000fe200) return -22; + + /* 12. HMMA.16816.F32.BF16 R4, R4, R2, RZ */ + BlackwellIRInsn insn_hmma_bf16 = { .op = BW_IR_HMMA_BF16, .dst_vreg = 1, .src1_vreg = 1, .src2_vreg = 0, .src3_vreg = -1 }; + uint32_t w_bf16[4]; + if (encode_single_insn(&insn_hmma_bf16, &ra, w_bf16) != 0) return -23; + if (w_bf16[0] != 0x0404723c || w_bf16[1] != 0x00000002 || w_bf16[2] != 0x000418ff || w_bf16[3] != 0x000fe200) return -24; + + /* 13. Differential Invariant: w[2] bit 18 toggles strictly between FP16 and BF16 */ + if ((w[2] ^ w_bf16[2]) != 0x00040000) return -25; + if (w[0] != w_bf16[0] || w[1] != w_bf16[1] || w[3] != w_bf16[3]) return -26; + + /* 14. SHF.R.U32.HI R4, RZ, 2, R2 */ + BlackwellIRInsn insn_shf = { .op = BW_IR_SHF_R, .dst_vreg = 1, .src1_vreg = 0, .imm = 2 }; + if (encode_single_insn(&insn_shf, &ra, w) != 0) return -27; + if (w[0] != 0xff047819 || w[1] != 2 || w[2] != 0x00011602 || w[3] != 0x001fca00) return -28; + + /* 15. LOP3.LUT R4, R2, 3, RZ, 0xc0, !PT */ + BlackwellIRInsn insn_lop3 = { .op = BW_IR_LOP3_AND, .dst_vreg = 1, .src1_vreg = 0, .imm = 3 }; + if (encode_single_insn(&insn_lop3, &ra, w) != 0) return -29; + if (w[0] != 0x02047812 || w[1] != 3 || w[2] != 0x078ec0ff || w[3] != 0x001fca00) return -30; + + /* 16. LDG.E.U16 R2, desc[UR4][R4.64] */ + BlackwellIRInsn insn_ldg16 = { .op = BW_IR_LDG_E_U16, .dst_vreg = 0, .src1_vreg = 1, .ureg = 0 }; + if (encode_single_insn(&insn_ldg16, &ra, w) != 0) return -31; + if (w[0] != 0x04027981 || w[1] != 4 || w[2] != 0x0c1e1500 || w[3] != 0x000f2200) return -32; + + /* A zero-delta branch stays the self-branch the matmul kernels already emit. */ + BlackwellIRInsn insn_bra0 = { .op = BW_IR_BRA }; + if (encode_single_insn(&insn_bra0, &ra, w) != 0) return -33; + if (w[0] != 0x00fc7947 || w[1] != 0xfffffffc || w[2] != 0x0383ffff || w[3] != 0x000fc000) return -34; + + BlackwellIRInsn insn_ge = { .op = BW_IR_ISETP_GE_U32, .src1_vreg = 1, .src2_vreg = 2 }; + if (encode_single_insn(&insn_ge, &ra, w) != 0) return -35; + if (w[0] != (0x720c | (4 << 24)) || w[1] != 6 || w[2] != 0x03f06070) return -36; + + BlackwellIRInsn insn_xor = { .op = BW_IR_LOP3_XOR, .dst_vreg = 1, .src1_vreg = 0, .src2_vreg = 2 }; + if (encode_single_insn(&insn_xor, &ra, w) != 0) return -37; + if (w[0] != (0x7212 | (4 << 16) | (2 << 24)) || w[1] != 6 || w[2] != 0x078e3cff) return -38; + + BlackwellIRInsn insn_ldg_ss = { .op = BW_IR_LDG_STRONG_SYS, .dst_vreg = 0, .src1_vreg = 1, .ureg = 0 }; + if (encode_single_insn(&insn_ldg_ss, &ra, w) != 0) return -39; + if (w[0] != (0x7981 | (2 << 16) | (4 << 24)) || w[1] != 4 || w[2] != 0x0c1f5900) return -40; + /* 17. FADD R7, R2, R4 */ + BlackwellIRInsn insn_fadd = { .op = BW_IR_FADD, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; + if (encode_single_insn(&insn_fadd, &ra, w) != 0) return -133; + if (w[0] != 0x02077221 || w[1] != 4 || w[2] != 0 || w[3] != 0x004fc400) return -134; + + /* 18. FSUB R7, R2, R4 (negated src2) */ + BlackwellIRInsn insn_fsub = { .op = BW_IR_FSUB, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; + if (encode_single_insn(&insn_fsub, &ra, w) != 0) return -135; + if (w[0] != 0x02077221 || w[1] != 0x80000004 || w[2] != 0 || w[3] != 0x004fc400) return -136; + + /* 19. FMUL R7, R2, R4 */ + BlackwellIRInsn insn_fmul = { .op = BW_IR_FMUL, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; + if (encode_single_insn(&insn_fmul, &ra, w) != 0) return -137; + if (w[0] != 0x02077220 || w[1] != 4 || w[2] != 0x00400000 || w[3] != 0x004fc400) return -138; + + /* 20. FFMA R7, R2, R4, R6 */ + BlackwellIRInsn insn_ffma = { .op = BW_IR_FFMA, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1, .src3_vreg = 2 }; + if (encode_single_insn(&insn_ffma, &ra, w) != 0) return -139; + if (w[0] != 0x02077223 || w[1] != 4 || w[2] != 6 || w[3] != 0x004fc400) return -140; + + /* 21. FMNMX_MIN R7, R2, R4 */ + BlackwellIRInsn insn_min = { .op = BW_IR_FMNMX_MIN, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; + if (encode_single_insn(&insn_min, &ra, w) != 0) return -141; + if (w[0] != 0x02077209 || w[1] != 4 || w[2] != 0x03800000 || w[3] != 0x004fc400) return -142; + + /* 22. FMNMX_MAX R7, R2, R4 */ + BlackwellIRInsn insn_max = { .op = BW_IR_FMNMX_MAX, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1 }; + if (encode_single_insn(&insn_max, &ra, w) != 0) return -143; + if (w[0] != 0x02077209 || w[1] != 4 || w[2] != 0x07800000 || w[3] != 0x004fc400) return -144; + + /* 23. I2FP R7, R2 */ + BlackwellIRInsn insn_i2f = { .op = BW_IR_I2FP, .dst_vreg = 3, .src1_vreg = 0 }; + if (encode_single_insn(&insn_i2f, &ra, w) != 0) return -145; + if (w[0] != 0x00077245 || w[1] != 2 || w[2] != 0x00201400 || w[3] != 0x004fe200) return -146; + + /* 24. F2I R7, R2 */ + BlackwellIRInsn insn_f2i = { .op = BW_IR_F2I, .dst_vreg = 3, .src1_vreg = 0 }; + if (encode_single_insn(&insn_f2i, &ra, w) != 0) return -147; + if (w[0] != 0x00077305 || w[1] != 2 || w[2] != 0x0020f100 || w[3] != 0x004e2200) return -148; + + /* 25. MUFU_RCP R7, R2 */ + BlackwellIRInsn insn_mufu_rcp = { .op = BW_IR_MUFU_RCP, .dst_vreg = 3, .src1_vreg = 0 }; + if (encode_single_insn(&insn_mufu_rcp, &ra, w) != 0) return -149; + if (w[0] != 0x00077308 || w[1] != 2 || w[2] != 0x00001000 || w[3] != 0x000e2400) return -150; + + /* 26. MUFU_RSQ R7, R2 */ + BlackwellIRInsn insn_mufu_rsq = { .op = BW_IR_MUFU_RSQ, .dst_vreg = 3, .src1_vreg = 0 }; + if (encode_single_insn(&insn_mufu_rsq, &ra, w) != 0) return -151; + if (w[0] != 0x00077308 || w[1] != 2 || w[2] != 0x00001400 || w[3] != 0x000e2400) return -152; + + /* 27. SHFL_DOWN R7, R2, 16 */ + BlackwellIRInsn insn_shfl = { .op = BW_IR_SHFL_DOWN, .dst_vreg = 3, .src1_vreg = 0, .imm = 16 }; + if (encode_single_insn(&insn_shfl, &ra, w) != 0) return -153; + if (w[0] != 0x02077f89 || w[1] != 0x0a001f00 || w[2] != 0x000e0000 || w[3] != 0x000e2400) return -154; + + /* 28. LDS R7, [R2] */ + BlackwellIRInsn insn_lds = { .op = BW_IR_LDS, .dst_vreg = 3, .src1_vreg = 0, .imm = 0 }; + if (encode_single_insn(&insn_lds, &ra, w) != 0) return -155; + if (w[0] != 0x02077984 || w[1] != 0 || w[2] != 0 || w[3] != 0x000fe200) return -156; + + /* 29. STS [R2], R4 */ + BlackwellIRInsn insn_sts = { .op = BW_IR_STS, .src1_vreg = 0, .src2_vreg = 1, .imm = 0 }; + if (encode_single_insn(&insn_sts, &ra, w) != 0) return -157; + if (w[0] != 0x02007388 || w[1] != 4 || w[2] != 0 || w[3] != 0x000fe200) return -158; + + return 0; +} + +int omega_blackwell_test_regalloc_bounds(void) { + BlackwellIRProgram prog; + omega_bw_ir_init(&prog); + + int v0 = omega_bw_ir_alloc_vreg64(&prog); + int v1 = omega_bw_ir_alloc_vreg(&prog); + int v2 = omega_bw_ir_alloc_vreg64(&prog); + int v3 = omega_bw_ir_alloc_vreg128(&prog); /* Test 128-bit quad allocation */ + + BlackwellIRInsn i0 = { .op = BW_IR_LDC64, .dst_vreg = v0, .imm = 0x380 }; + omega_bw_ir_append(&prog, &i0); + BlackwellIRInsn i1 = { .op = BW_IR_MOV_IMM, .dst_vreg = v1, .imm = 10 }; + omega_bw_ir_append(&prog, &i1); + BlackwellIRInsn i2 = { .op = BW_IR_LDC64, .dst_vreg = v2, .imm = 0x388 }; + omega_bw_ir_append(&prog, &i2); + BlackwellIRInsn i3 = { .op = BW_IR_HMMA_F16, .dst_vreg = v3, .src1_vreg = v3, .src2_vreg = v0, .src3_vreg = -1 }; + omega_bw_ir_append(&prog, &i3); + /* i4 uses v1, v2, v3 simultaneously so all intervals are concurrently active */ + int v_sink = omega_bw_ir_alloc_vreg(&prog); + BlackwellIRInsn i4 = { .op = BW_IR_IADD3, .dst_vreg = v_sink, .src1_vreg = v1, .src2_vreg = v2, .src3_vreg = v3 }; + omega_bw_ir_append(&prog, &i4); + + if (omega_bw_regalloc_solve(&prog) != 0) return -1; + + int phys_v0 = prog.regalloc.vreg_to_phys[v0]; + int phys_v1 = prog.regalloc.vreg_to_phys[v1]; + int phys_v2 = prog.regalloc.vreg_to_phys[v2]; + int phys_v3 = prog.regalloc.vreg_to_phys[v3]; + + if (phys_v0 % 2 != 0) return -2; + if (phys_v2 % 2 != 0) return -3; + if (phys_v3 % 4 != 0) return -4; /* 4-alignment for 128-bit quad */ + if (phys_v1 == phys_v0 || phys_v1 == phys_v0 + 1) return -5; + if (phys_v2 == phys_v0 || phys_v2 == phys_v0 + 1) return -6; + for (int k = 0; k < 4; k++) { + if (phys_v3 + k == phys_v0 || phys_v3 + k == phys_v0 + 1) return -7; + if (phys_v3 + k == phys_v2 || phys_v3 + k == phys_v2 + 1) return -8; + if (phys_v3 + k == phys_v1) return -9; + } + + /* Test bounds refusal: intentionally attempt to allocate more registers than hardware capacity */ + BlackwellIRProgram overflow_prog; + omega_bw_ir_init(&overflow_prog); + for (int i = 0; i < BW_PHYS_GPR_MAX + 5; i++) { + int v = omega_bw_ir_alloc_vreg(&overflow_prog); + if (v < 0) break; + BlackwellIRInsn insn = { .op = BW_IR_MOV_IMM, .dst_vreg = v, .imm = (uint32_t)i }; + omega_bw_ir_append(&overflow_prog, &insn); + } + for (size_t i = 0; i < overflow_prog.count; i++) { + for (int v = 0; v < overflow_prog.regalloc.num_vregs; v++) { + overflow_prog.regalloc.intervals[v].last_use = (int)overflow_prog.count - 1; + } + } + if (omega_bw_regalloc_solve(&overflow_prog) == 0) { + return -6; + } + + return 0; +} + +int omega_blackwell_test_codegen_variation(void) { + OmegaMatMulSpec spec1, spec2; + if (omega_matmul_spec_init(&spec1, 16, 16, 16, OMEGA_MATMUL_PRECISION_INT32) != 0) { + printf("FAIL: spec1 init failed\n"); + return -1; + } + if (omega_matmul_spec_init(&spec2, 32, 16, 64, OMEGA_MATMUL_PRECISION_INT32) != 0) { + printf("FAIL: spec2 init failed\n"); + return -1; + } + + OmegaBlackwellKernel k1, k2; + memset(&k1, 0, sizeof(k1)); + memset(&k2, 0, sizeof(k2)); + + if (omega_blackwell_codegen_matmul(&spec1, &k1) != 0) { + printf("FAIL: k1 codegen failed\n"); + return -1; + } + if (omega_blackwell_codegen_matmul(&spec2, &k2) != 0) { + printf("FAIL: k2 codegen failed\n"); + omega_blackwell_kernel_free(&k1); + return -1; + } + + OmegaBlackwellRealizationIdentity id1, id2; + omega_blackwell_bind_matmul_realization(&spec1, &k1, &id1); + omega_blackwell_bind_matmul_realization(&spec2, &k2, &id2); + + printf("================================================================================\n"); + printf(" OMEGA BLACKWELL CODEGEN VARIATION DEMONSTRATION (GATE 7 STAGE-1)\n"); + printf("================================================================================\n"); + printf("Config 1: 16x16x16 INT32\n"); + printf(" Instructions: %zu (%zu bytes, %u GPRs, %u UGPRs)\n", k1.insn_count, k1.code_size, k1.gpr_count, k1.uniform_gpr_count); + printf(" Spec ID: "); + for (int i = 0; i < 16; i++) printf("%02x", spec1.spec_id[i]); + printf("...\n"); + printf(" Code Digest: "); + for (int i = 0; i < 16; i++) printf("%02x", k1.code_digest[i]); + printf("...\n"); + printf(" Realization: "); + for (int i = 0; i < 16; i++) printf("%02x", id1.realization_id[i]); + printf("...\n\n"); + + printf("Config 2: 32x16x64 INT32\n"); + printf(" Instructions: %zu (%zu bytes, %u GPRs, %u UGPRs)\n", k2.insn_count, k2.code_size, k2.gpr_count, k2.uniform_gpr_count); + printf(" Spec ID: "); + for (int i = 0; i < 16; i++) printf("%02x", spec2.spec_id[i]); + printf("...\n"); + printf(" Code Digest: "); + for (int i = 0; i < 16; i++) printf("%02x", k2.code_digest[i]); + printf("...\n"); + printf(" Realization: "); + for (int i = 0; i < 16; i++) printf("%02x", id2.realization_id[i]); + printf("...\n"); + printf("================================================================================\n"); + + /* Validation checks */ + if (memcmp(spec1.spec_id, spec2.spec_id, 32) == 0) { + printf("FAIL: spec IDs match (expected distinct)\n"); + return -2; + } + if (memcmp(k1.code_digest, k2.code_digest, 32) == 0) { + printf("FAIL: code digests match (expected distinct)\n"); + return -3; + } + if (memcmp(id1.realization_id, id2.realization_id, 32) == 0) { + printf("FAIL: realization IDs match (expected distinct)\n"); + return -4; + } + + printf("PASS: Dynamic codegen variation verified (distinct specifications, code digests, and realization IDs).\n"); + + omega_blackwell_kernel_free(&k1); + omega_blackwell_kernel_free(&k2); + return 0; +} diff --git a/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/omega_bw_reconv.h b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/omega_bw_reconv.h new file mode 100644 index 000000000..d432e5763 --- /dev/null +++ b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/omega_bw_reconv.h @@ -0,0 +1,83 @@ +#ifndef OMEGA_BW_RECONV_H +#define OMEGA_BW_RECONV_H +/* Structured warp reconvergence regions for the Blackwell IR emitters (omega #308). + * + * A region is the span between a BSSY (arm barrier Bn, name the join point) and the + * matching BSYNC (wait for every armed lane, continue as one warp). Inside a region the + * lanes of a warp may take different paths: a predicated forward branch, a loop whose + * trip count differs per lane, a break. Every path that leaves the region jumps to the + * BSYNC itself (that is where nvcc 13.0.88 sends loop exits on sm_121); the BSSY names the + * instruction after the BSYNC. Regions nest; the innermost open region owns Bn with + * n = depth, so nesting depth is bounded by the barrier register count and begin fails + * closed (returns -1) when that bound is reached. + * + * This header holds the bookkeeping only (indices, patching, bounds). Each emitter wraps + * it with its own control-word scheme: see region_begin / region_exit / region_join in + * src/omega_gpu_elementwise_api.c and src/omega_gpu_attention_api.c. + * + * Reference: Mesa NAK nak_nir_lower_cf.c (push_scope / pop_scope: bar_set_nv before a + * divergent if or loop, bar_sync_nv at its merge block, breaks jump to the sync) and the + * nvcc oracle listing in docs/gpu-reconvergence-308.md. */ + +#include "omega_blackwell_codegen.h" + +#define BW_RECONV_MAX_DEPTH BW_RECONV_MAX_BAR +#define BW_RECONV_MAX_EXITS 8 /* forward exits (breaks) recorded per region */ + +typedef struct { + int depth; /* open regions */ + int peak; /* deepest nesting seen (reported in receipts) */ + int opened; /* regions begun (for receipts) */ + int bssy_at[BW_RECONV_MAX_DEPTH]; + int exits[BW_RECONV_MAX_DEPTH][BW_RECONV_MAX_EXITS]; + int n_exits[BW_RECONV_MAX_DEPTH]; +} BwRegions; + +/* Called after the emitter appended the BSSY at index `bssy_index`. Returns the barrier + * register to use, or -1 when the nesting bound is reached (the caller must fail the + * build: no code with a missing barrier may be emitted). The caller allocates the + * barrier BEFORE emitting: use bw_regions_next_bar to pick it, then bw_regions_begin. */ +static inline int bw_regions_next_bar(const BwRegions *r) { + return r->depth < BW_RECONV_MAX_DEPTH ? r->depth : -1; +} +static inline int bw_regions_begin(BwRegions *r, int bssy_index) { + if (r->depth >= BW_RECONV_MAX_DEPTH || bssy_index < 0) return -1; + r->bssy_at[r->depth] = bssy_index; + r->n_exits[r->depth] = 0; + r->depth++; + r->opened++; + if (r->depth > r->peak) r->peak = r->depth; + return r->depth - 1; +} + +/* Records a forward branch (already appended at `bra_index`) that leaves the innermost + * open region; its target is patched to the BSYNC by bw_regions_join. -1 when no region + * is open or the per-region exit table is full. */ +static inline int bw_regions_exit(BwRegions *r, int bra_index) { + if (r->depth <= 0 || bra_index < 0) return -1; + int d = r->depth - 1; + if (r->n_exits[d] >= BW_RECONV_MAX_EXITS) return -1; + r->exits[d][r->n_exits[d]++] = bra_index; + return 0; +} + +/* Called after the emitter appended the BSYNC at index `bsync_index` for the innermost + * open region. Patches the BSSY to name the instruction after the BSYNC and every + * recorded exit to jump to the BSYNC. Returns the barrier register closed, or -1 when + * no region is open or an index is inconsistent (BSSY must precede the BSYNC). */ +static inline int bw_regions_join(BwRegions *r, BlackwellIRProgram *p, int bsync_index) { + if (r->depth <= 0 || !p || bsync_index < 0 || (size_t)bsync_index >= p->count) return -1; + int d = --r->depth; + int at = r->bssy_at[d]; + if (at < 0 || at >= bsync_index || p->insns[at].op != BW_IR_BSSY || p->insns[bsync_index].op != BW_IR_BSYNC) return -1; + if (p->insns[at].bar_reg != (uint8_t)d || p->insns[bsync_index].bar_reg != (uint8_t)d) return -1; + p->insns[at].imm = (uint32_t)(bsync_index + 1 - at); + for (int i = 0; i < r->n_exits[d]; i++) { + int b = r->exits[d][i]; + if (b <= at || b >= bsync_index || p->insns[b].op != BW_IR_BRA) return -1; + p->insns[b].imm = (uint32_t)(bsync_index - b); + } + return d; +} + +#endif /* OMEGA_BW_RECONV_H */ diff --git a/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/omega_gpu_elementwise_api.c b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/omega_gpu_elementwise_api.c new file mode 100644 index 000000000..b16c70205 --- /dev/null +++ b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/omega_gpu_elementwise_api.c @@ -0,0 +1,1139 @@ +/* omega_gpu_elementwise_api.c: FB-1 cut 4 (2026-10-04). See the header. + * + * Layout of this file: (1) kernel codegen on top of the BlackwellIR, with a + * small scheduler that writes every control word; (2) one launcher cloned from + * the chip-proven tensor matmul path (omega_blackwell_execute_matmul_tensor); + * (3) the public entry points, which split work into <= OMEGA_GPU_EW_MAX_CTAS + * launches and run the host-side bookkeeping. + * + * Scheduling rules (chip-learned, src/omega_numeric.c omega_numeric_check_patch): + * - a variable-latency result (LDG, LDS, LDC, LDCU, S2R, SHFL, MUFU) arrives through + * a scoreboard; the producer stalls >= 2 and the consumer waits on that SB; + * - a fixed-latency result (FADD FMUL FFMA IMAD IADD3 MOV LOP3 SHF) has no + * scoreboard: the producer stalls >= 5 before its consumer (we use 6); + * - a predicate producer (ISETP) stalls 14 before @P0 use (NAK SM120 padding); + * - stores (STG, STS) set a read barrier so the next instruction cannot overwrite + * the source register early. + * The emitter below serialises: every instruction waits on everything still + * pending. A final local pass tightens supported adjacent ALU pairs to 5 cycles. + */ +#include "omega_gpu_elementwise_api.h" +#include "omega_blackwell_codegen.h" +#include "omega_bw_reconv.h" +#include "omega_blackwell_qmd.h" +#include "omega_blackwell_submit.h" +#include "omega_gpu_session.h" +#include "sha256.h" +#include +#include +#include +#include + +/* ------------------------------------------------------------------ codegen */ + +#define CW(stall, wbar, rbar, wait) \ + (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) +#define CW_YIELD 0x2000u +#define SB_NONE 7u +#define SB_GPR 0u /* variable-latency GPR results */ +#define SB_RD 1u /* store source-register read barrier */ +#define SB_UGPR 2u /* uniform-register results (LDCU) */ + +enum { K_FIXED, K_PRED, K_VAR, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC }; + +#define EW_MAX_LOOPS 4 +typedef struct { + BlackwellIRProgram *p; + uint32_t pending; /* scoreboards set and not yet waited on */ + int err; + int loop_start[EW_MAX_LOOPS], loop_end[EW_MAX_LOOPS]; + int n_loops; + int open_start[EW_MAX_LOOPS]; /* starts of loops begun and not yet ended (nesting stack) */ + int depth; + BwRegions rg; /* structured reconvergence regions (omega #308) */ + int rg_mutant; /* test only: 1 = drop the BSYNC at join, 2 = corrupt the BSSY target (host sim must catch both) */ + int exited; /* a predicated EXIT was emitted: some lanes of a warp may be gone */ + int exit_sync_ok; /* test only: lets a kernel put SHFL/BAR after a predicated EXIT (the negative control R6) */ + int test_path; /* building for omega_gpu_elementwise_codegen_ir (host tests), never for a launch */ +} Em; + +static void emit(Em *e, BlackwellIRInsn in, int kind) { + uint32_t wait = e->pending; + switch (kind) { + case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; + case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break; + case K_VAR: in.control = CW(4, SB_GPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_GPR; break; + case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; + case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; + case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; + /* a predicated EXIT or BRA that falls through must not be the only waiter */ + case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; + case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; + /* BSSY / BSYNC read no registers; the stall counts (1 and 5, both with yield) are the ones + * nvcc 13.0.88 writes for sm_121 (docs/gpu-reconvergence-308.md). They wait on everything + * pending so that, as for every branch here, nothing is in flight across a control edge + * and the straight-line scoreboard walk in the tests stays valid. */ + case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; + case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; + default: e->err = -1; return; + } + if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; /* append returns the index */ +} + +static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; } +static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; } +static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; } +static int here(const Em *e) { return (int)e->p->count; } + +#define NOREG -1 +static BlackwellIRInsn I0(BlackwellIROpcode op) { + BlackwellIRInsn in; memset(&in, 0, sizeof in); + in.op = op; in.dst_vreg = NOREG; in.src1_vreg = NOREG; in.src2_vreg = NOREG; in.src3_vreg = NOREG; in.ureg = NOREG; + return in; +} +static void ldcu64(Em *e, int udst, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDCU64); i.dst_vreg = udst; i.imm = off; i.is_uniform = true; emit(e, i, K_UVAR); } +static void ldc64(Em *e, int dst64, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC64); i.dst_vreg = dst64; i.imm = off; emit(e, i, K_VAR); } +static void ldc32(Em *e, int dst, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC); i.dst_vreg = dst; i.imm = off; emit(e, i, K_VAR); } +static void s2r(Em *e, int dst, uint32_t sr) { BlackwellIRInsn i = I0(BW_IR_S2R); i.dst_vreg = dst; i.imm = sr; emit(e, i, K_VAR); } +static void movi(Em *e, int dst, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_MOV_IMM); i.dst_vreg = dst; i.imm = imm; emit(e, i, K_FIXED); } +static void movf(Em *e, int dst, float f) { uint32_t u; memcpy(&u, &f, 4); movi(e, dst, u); } +static void movrz(Em *e, int dst) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = dst; emit(e, i, K_FIXED); } +/* dst = a * imm + c (c may be NOREG for RZ) */ +static void imadi(Em *e, int dst, int a, uint32_t imm, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = dst; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c; emit(e, i, K_FIXED); } +/* dst = a * b + c (registers) */ +static void imadr(Em *e, int dst, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = dst; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } +static void iadd3(Em *e, int dst, int a, int b) { BlackwellIRInsn i = I0(BW_IR_IADD3); i.dst_vreg = dst; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void lop3xor(Em *e, int dst, int a, int b) { BlackwellIRInsn i = I0(BW_IR_LOP3_XOR); i.dst_vreg = dst; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +/* dst64 = idx * 4 + ptr64 (byte address of element idx) */ +static void addr4(Em *e, int dst64, int idx, int ptr64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = dst64; i.src1_vreg = idx; i.imm = 4; i.src3_vreg = ptr64; emit(e, i, K_FIXED); } +static void ldg(Em *e, int dst, int addr64, int udesc) { BlackwellIRInsn i = I0(BW_IR_LDG_E); i.dst_vreg = dst; i.src1_vreg = addr64; i.ureg = udesc; emit(e, i, K_VAR); } +/* Independent loads (LDG_E from global, or LDS32 from shared memory with ureg NOREG) issued + * back to back (campaign C2a, 2026-10-05). The first load + * waits on whatever is pending; the others wait on nothing, so all n are in flight together. + * Every load sets SB_GPR (a counting scoreboard), so the next instruction waits once for all + * of them instead of once per load. Fails the build if a load address is a destination of the + * group or a destination repeats (the later loads would read an operand still in flight). */ +static void ld_group(Em *e, BlackwellIROpcode op, int n, const int *dst, const int *addr, int ureg) { + for (int i = 0; i < n; i++) + for (int k = 0; k < n; k++) + if (addr[i] == dst[k] || (k != i && dst[i] == dst[k])) { e->err = -1; return; } + for (int i = 0; i < n; i++) { + if (op != BW_IR_LDG_E && op != BW_IR_LDS32) { e->err = -1; return; } + BlackwellIRInsn in = I0(op); in.dst_vreg = dst[i]; in.src1_vreg = addr[i]; in.ureg = ureg; + if (i == 0) { emit(e, in, K_VAR); continue; } + in.control = CW(4, SB_GPR, SB_NONE, 0) | CW_YIELD; /* nonzero, so the encoder keeps it */ + if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; + } +} +static void stg(Em *e, int addr64, int val, int udesc) { BlackwellIRInsn i = I0(BW_IR_STG_E); i.src1_vreg = addr64; i.src2_vreg = val; i.ureg = udesc; emit(e, i, K_STORE); } +static void fadd(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FADD); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void fsub(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FSUB); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void fmul(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FMUL); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void ffma(Em *e, int d, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_FFMA); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } +static void mufu(Em *e, BlackwellIROpcode op, int d, int a) { BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = a; emit(e, i, K_VAR); } +/* Cross-lane and warp-ending ops are refused inside an open reconvergence region (omega #308): + * a SHFL or BAR.SYNC reached by a split warp reads inactive lanes / counts a partial warp + * (undefined; the 2026-10-04 attention bug), and a lane that EXITs inside a region can no + * longer be joined. The build fails closed instead of emitting such code. */ +/* Cross-lane instructions (SHFL, BAR.SYNC) need the whole warp present. The emitter refuses + * them (fails the build) inside an open reconvergence region, where lanes may be on different + * paths, and after a predicated EXIT, where lanes may be gone for good: a SHFL then reads + * inactive lanes (undefined on the chip, the attention failure of 2026-10-04). A kernel that + * must drop lanes before a cross-lane step clamps them instead and exits them afterwards. */ +static int warp_split(Em *e) { if (e->rg.depth > 0 || (e->exited && !e->exit_sync_ok)) { e->err = -1; return 1; } return 0; } +static void shfl_down(Em *e, int d, int a, uint32_t off) { if (warp_split(e)) return; BlackwellIRInsn i = I0(BW_IR_SHFL_DOWN); i.dst_vreg = d; i.src1_vreg = a; i.imm = off; emit(e, i, K_VAR); } +static void sts32(Em *e, int addr, int val) { BlackwellIRInsn i = I0(BW_IR_STS32); i.src1_vreg = addr; i.src2_vreg = val; emit(e, i, K_STORE); } +static void lds32(Em *e, int d, int addr) { BlackwellIRInsn i = I0(BW_IR_LDS32); i.dst_vreg = d; i.src1_vreg = addr; emit(e, i, K_VAR); } +static void bar_sync(Em *e) { if (warp_split(e)) return; emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } +static void isetp_ge_u32(Em *e, int a, int b) { BlackwellIRInsn i = I0(BW_IR_ISETP_GE_U32); i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_PRED); } +/* An EXIT inside a region would leave the barrier waiting for a lane that never comes: refused. */ +static void exit_if_p0(Em *e) { if (e->rg.depth > 0) { e->err = -1; return; } e->exited = 1; BlackwellIRInsn i = I0(BW_IR_EXIT); i.predicate_p0 = true; emit(e, i, K_EXIT); } +static void bra_back_if_not_p0(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.predicate_not = true; i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } +static void tail(Em *e) { + emit(e, I0(BW_IR_EXIT), K_EXIT); + BlackwellIRInsn self = I0(BW_IR_BRA); /* the self-branch every kernel here ends with */ + self.control = 0x000fc000u; + if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1; +} +/* Loops may nest (prime race cut): begin pushes the start, end pops it, so the inner + * loop is closed (and its liveness fixed) first. For the single-level loops above this + * is exactly the old behaviour. */ +static void loop_begin(Em *e) { if (e->depth >= EW_MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); } +/* Closing a loop: every value defined before the loop and read inside it stays + * live until the back-branch, otherwise the linear-scan allocator (which knows + * nothing about back-edges) would hand its register to a body temporary whose + * first definition comes after the value's last textual use (e.g. the scale y + * in rmsnorm pass 2), and the next iteration would read garbage. */ +static void loop_end(Em *e) { + if (e->n_loops >= EW_MAX_LOOPS || e->depth <= 0) { e->err = -1; return; } + int start = e->open_start[--e->depth], end = here(e) - 1; + e->loop_start[e->n_loops] = start; + e->loop_end[e->n_loops] = end; + e->n_loops++; + OmegaRegAlloc *ra = &e->p->regalloc; + for (int v = 0; v < ra->num_vregs; v++) { + OmegaLiveInterval *iv = &ra->intervals[v]; + if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end; + } +} + +/* Kernel argument words (constant bank 0, same slots as the matmul path): + * 0x380 a, 0x388 b, 0x390 c (64-bit each), then four 32-bit parameters. */ +#define ARG_A 0x380u +#define ARG_B 0x388u +#define ARG_C 0x390u +#define ARG_P0 0x398u +#define ARG_P1 0x39cu +#define ARG_P2 0x3a0u +#define ARG_P3 0x3a4u +#define ARG_P4 0x3a8u +#define DESC_OFF 0x358u + +typedef struct { int udesc, pa, pb, pc, tid, cta; } Pro; +static Pro prologue(Em *e) { + Pro r; + r.udesc = UV64(e); r.pa = V64(e); r.pb = V64(e); r.pc = V64(e); r.tid = V(e); r.cta = V(e); + ldcu64(e, r.udesc, DESC_OFF); + ldc64(e, r.pa, ARG_A); ldc64(e, r.pb, ARG_B); ldc64(e, r.pc, ARG_C); + s2r(e, r.tid, BW_SR_TID_X); s2r(e, r.cta, BW_SR_CTAID_X); + return r; +} + +/* out[i] = 2^a[i] for i < n (n = P0). Threads 128, grid ceil(n/128). Mutant: 2^(a+1). */ +static void gen_ex2(Em *e, int mutant) { + Pro p = prologue(e); + int n = V(e), i = V(e), off = V64(e), x = V(e), y = V(e); + ldc32(e, n, ARG_P0); + imadi(e, i, p.cta, OMEGA_GPU_EW_THREADS, p.tid); + isetp_ge_u32(e, i, n); + exit_if_p0(e); + addr4(e, off, i, p.pa); + ldg(e, x, off, p.udesc); + if (mutant) { int one = V(e); movf(e, one, 1.0f); fadd(e, x, x, one); } + mufu(e, BW_IR_MUFU_EX2, y, x); + addr4(e, off, i, p.pc); + stg(e, off, y, p.udesc); + tail(e); +} + +/* out[i] = a[i ^ 127] within each CTA of 128 (n % 128 == 0). STS own word, BAR.SYNC, + * LDS the partner word. Mutant: LDS own word (no exchange). */ +static void gen_xchg(Em *e, int mutant) { + Pro p = prologue(e); + int i = V(e), off = V64(e), x = V(e), saddr = V(e), paddr = V(e), mask = V(e), y = V(e); + imadi(e, i, p.cta, OMEGA_GPU_EW_THREADS, p.tid); + addr4(e, off, i, p.pa); + ldg(e, x, off, p.udesc); + imadi(e, saddr, p.tid, 4, NOREG); + sts32(e, saddr, x); + bar_sync(e); + if (mutant) { + lds32(e, y, saddr); + } else { + movi(e, mask, OMEGA_GPU_EW_THREADS - 1); + lop3xor(e, paddr, p.tid, mask); + imadi(e, paddr, paddr, 4, NOREG); + lds32(e, y, paddr); + } + addr4(e, off, i, p.pc); + stg(e, off, y, p.udesc); + tail(e); +} + +/* One CTA of 128 threads per row. P0 = dim/128 (loop count), P1 = bits(1/dim), + * P2 = bits(eps), P3 = dim. Mutant: the warp tree drops the last SHFL step. */ +static void gen_rmsnorm(Em *e, int mutant) { + Pro p = prologue(e); + int nk = V(e), invd = V(e), eps = V(e), dim = V(e); + int rowbase = V(e), idx = V(e), col = V(e), k = V(e), acc = V(e), cT = V(e), c1 = V(e); + int off = V64(e), x = V(e), t = V(e); + ldc32(e, nk, ARG_P0); ldc32(e, invd, ARG_P1); ldc32(e, eps, ARG_P2); ldc32(e, dim, ARG_P3); + imadr(e, rowbase, p.cta, dim, NOREG); + iadd3(e, idx, rowbase, p.tid); + movrz(e, acc); movrz(e, k); + movi(e, cT, OMEGA_GPU_EW_THREADS); movi(e, c1, 1); + loop_begin(e); + int l1 = here(e); + addr4(e, off, idx, p.pa); + ldg(e, x, off, p.udesc); + ffma(e, acc, x, x, acc); + iadd3(e, idx, idx, cT); + iadd3(e, k, k, c1); + isetp_ge_u32(e, k, nk); + bra_back_if_not_p0(e, l1); + loop_end(e); + static const uint32_t steps[5] = { 16, 8, 4, 2, 1 }; + for (int s = 0; s < (mutant ? 4 : 5); s++) { shfl_down(e, t, acc, steps[s]); fadd(e, acc, acc, t); } + int saddr = V(e); + imadi(e, saddr, p.tid, 4, NOREG); + sts32(e, saddr, acc); + bar_sync(e); + int a0 = V(e), a1 = V(e), a2 = V(e), a3 = V(e), s0 = V(e), s1 = V(e), s2 = V(e), s3 = V(e), sum = V(e); + movrz(e, a0); movi(e, a1, 128); movi(e, a2, 256); movi(e, a3, 384); + lds32(e, s0, a0); lds32(e, s1, a1); lds32(e, s2, a2); lds32(e, s3, a3); + fadd(e, sum, s0, s1); fadd(e, sum, sum, s2); fadd(e, sum, sum, s3); + int v = V(e), y = V(e), u = V(e), cmh = V(e), c15 = V(e); + fmul(e, v, sum, invd); + fadd(e, v, v, eps); + mufu(e, BW_IR_MUFU_RSQ, y, v); + movf(e, cmh, -0.5f); movf(e, c15, 1.5f); + fmul(e, u, v, y); fmul(e, u, u, y); ffma(e, u, u, cmh, c15); fmul(e, y, y, u); + int offw = V64(e), offc = V64(e), w = V(e), o = V(e); + iadd3(e, idx, rowbase, p.tid); + imadi(e, col, p.tid, 1, NOREG); /* col = tid (the encoder maps a missing src2 to R0, not RZ, so no IADD3 here) */ + movrz(e, k); + loop_begin(e); + int l2 = here(e); + addr4(e, off, idx, p.pa); + ldg(e, x, off, p.udesc); + addr4(e, offw, col, p.pb); + ldg(e, w, offw, p.udesc); + fmul(e, o, x, y); + fmul(e, o, o, w); + addr4(e, offc, idx, p.pc); + stg(e, offc, o, p.udesc); + iadd3(e, idx, idx, cT); + iadd3(e, col, col, cT); + iadd3(e, k, k, c1); + isetp_ge_u32(e, k, nk); + bra_back_if_not_p0(e, l2); + loop_end(e); + tail(e); +} + +/* One CTA of head_dim/2 threads per head. P0 = head_dim, P1 = head_dim/2. + * a = vector, b = table (cos in the first half of each head, sin in the second). + * Mutant: out0 = q0*cos + q1*sin. */ +static void gen_rope(Em *e, int mutant) { + Pro p = prologue(e); + int hd = V(e), half = V(e), i0 = V(e), i1 = V(e), off0 = V64(e), off1 = V64(e); + int q0 = V(e), q1 = V(e), c = V(e), s = V(e), t0 = V(e), t1 = V(e), o0 = V(e), o1 = V(e); + ldc32(e, hd, ARG_P0); ldc32(e, half, ARG_P1); + imadr(e, i0, p.cta, hd, p.tid); + iadd3(e, i1, i0, half); + addr4(e, off0, i0, p.pa); ldg(e, q0, off0, p.udesc); + addr4(e, off1, i1, p.pa); ldg(e, q1, off1, p.udesc); + addr4(e, off0, i0, p.pb); ldg(e, c, off0, p.udesc); + addr4(e, off1, i1, p.pb); ldg(e, s, off1, p.udesc); + fmul(e, t0, q0, c); fmul(e, t1, q1, s); + if (mutant) fadd(e, o0, t0, t1); else fsub(e, o0, t0, t1); + fmul(e, t0, q0, s); fmul(e, t1, q1, c); + fadd(e, o1, t0, t1); + addr4(e, off0, i0, p.pc); stg(e, off0, o0, p.udesc); + addr4(e, off1, i1, p.pc); stg(e, off1, o1, p.udesc); + tail(e); +} + +/* out[i] = g/(1+2^(g*-log2e)) * up for i < n (P0). Mutant: 2^(g*+log2e). */ +static void gen_swiglu(Em *e, int mutant) { + Pro p = prologue(e); + int n = V(e), i = V(e), off = V64(e), g = V(e), u = V(e), k = V(e), t = V(e), ex = V(e), d = V(e), r = V(e), one = V(e), o = V(e); + ldc32(e, n, ARG_P0); + imadi(e, i, p.cta, OMEGA_GPU_EW_THREADS, p.tid); + isetp_ge_u32(e, i, n); + exit_if_p0(e); + addr4(e, off, i, p.pa); ldg(e, g, off, p.udesc); + addr4(e, off, i, p.pb); ldg(e, u, off, p.udesc); + movf(e, k, mutant ? 1.4426950408889634f : -1.4426950408889634f); + movf(e, one, 1.0f); + fmul(e, t, g, k); + mufu(e, BW_IR_MUFU_EX2, ex, t); + fadd(e, d, ex, one); + mufu(e, BW_IR_MUFU_RCP, r, d); + fmul(e, o, g, r); + fmul(e, o, o, u); + addr4(e, off, i, p.pc); + stg(e, off, o, p.udesc); + tail(e); +} + +/* ------------------------------------------- prime race sieve (2026-10-05) */ +static void lop3lut(Em *e, int d, int a, int b, int c, uint32_t lut) { BlackwellIRInsn i = I0(BW_IR_LOP3_LUT); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; i.imm = lut & 0xffu; emit(e, i, K_FIXED); } +static void shl_r(Em *e, int d, int a, int n) { BlackwellIRInsn i = I0(BW_IR_SHF_L_U32); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = n; emit(e, i, K_FIXED); } +static void shr_i(Em *e, int d, int a, uint32_t n) { BlackwellIRInsn i = I0(BW_IR_SHF_R); i.dst_vreg = d; i.src1_vreg = a; i.imm = n; emit(e, i, K_FIXED); } +static void imadhi(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_IMAD_HI_U32); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +/* dst64 = a * imm + c64 (IMAD.WIDE.U32 immediate form) */ +static void widei(Em *e, int dst64, int a, uint32_t imm, int c64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = dst64; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c64; emit(e, i, K_FIXED); } +/* @P0 BRA forward; returns the instruction index for bra_patch_here */ +static int bra_fwd_if_p0(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = 1; emit(e, i, K_BRA); return at; } +static void bra_patch_here(Em *e, int at) { if (e->err == 0 && at >= 0 && at < here(e)) e->p->insns[at].imm = (uint32_t)(here(e) - at); else e->err = -1; } +static void bra_always(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } + +/* ---- structured reconvergence regions (omega #308; bookkeeping in omega_bw_reconv.h) ---- + * region_begin: BSSY Bd (d = nesting depth), fails closed when no barrier register is left. + * region_exit: a forward branch out of the innermost region, patched to its BSYNC at join + * (RX_ALWAYS, RX_IF_P0, RX_IF_NOT_P0). + * region_join: BSYNC Bd; patches the BSSY to the instruction after it and every exit to it. + * Usage: divergent loop = region_begin; L: isetp; region_exit(RX_IF_P0); body; bra_always(L); region_join. + * if/else diamond = region_begin; isetp; t = bra_fwd_if_p0; then; region_exit(RX_ALWAYS); + * bra_patch_here(t); else; region_join. */ +enum { RX_ALWAYS, RX_IF_P0, RX_IF_NOT_P0 }; +static int region_begin(Em *e) { + int bar = bw_regions_next_bar(&e->rg); + if (bar < 0) { e->err = -1; return -1; } + int at = here(e); + BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = (uint8_t)bar; i.imm = 2; emit(e, i, K_BSSY); + if (bw_regions_begin(&e->rg, at) < 0) e->err = -1; + return bar; +} +static void region_exit(Em *e, int cond) { + int at = here(e); + BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = 1; + if (cond != RX_ALWAYS) { i.predicate_p0 = true; i.predicate_not = (cond == RX_IF_NOT_P0); } + emit(e, i, K_BRA); + if (bw_regions_exit(&e->rg, at) < 0) e->err = -1; +} +static void region_join(Em *e) { + if (e->rg.depth <= 0) { e->err = -1; return; } + int d = e->rg.depth - 1, bsync = here(e); + BlackwellIRInsn i = I0(e->rg_mutant == 1 ? BW_IR_NOP : BW_IR_BSYNC); i.bar_reg = (uint8_t)d; emit(e, i, K_BSYNC); + if (e->rg_mutant == 1) { + /* negative control (host sim only): the join is a NOP; patch as the real join would */ + BwRegions *r = &e->rg; r->depth--; + e->p->insns[r->bssy_at[d]].imm = (uint32_t)(bsync + 1 - r->bssy_at[d]); + for (int k = 0; k < r->n_exits[d]; k++) e->p->insns[r->exits[d][k]].imm = (uint32_t)(bsync - r->exits[d][k]); + return; + } + if (bw_regions_join(&e->rg, e->p, bsync) != d) e->err = -1; + if (e->rg_mutant == 2) e->p->insns[e->rg.bssy_at[d]].imm += 1; /* negative control: BSSY names the wrong join */ +} + +/* Word-parallel odd-only sieve (docs: handoff 2026-10-05 DESIGN "GPU kernel (native)"). + * Thread t owns 32-bit output words w = t, t + S, t + 2S, ... (S = P1 = threads launched; + * the I42 envelope caps a launch at OMEGA_GPU_EW_MAX_CTAS CTAs, so a grid-stride loop + * replaces one thread per word). Every word has exactly one writer; no atomics. + * Bit j of word w is the odd number 2(32w + j) + 1. + * Arguments: a (0x380) = prime table, entries {p, magic = ceil(2^32 / p), k0 = (p*p - 1)/2} + * (12 bytes, ascending p, odd primes <= isqrt(limit)) followed by ONE sentinel entry with + * k0 = 0xffffffff that ends every scan; c (0x390) = output words; + * P0 = nwords, P1 = S, P2 = last word index, P3 = ~tailmask (bits past the last odd). + * Range: limit < 2^32 (so base, k0 < 2^31 and the sign-bit tricks below hold). + * Per word: base = 32w; for each prime while k0 < base + 32: + * d = base - k0; neg = d >> 31 (k0 > base); dpos = neg ? 0 : d; jadd = neg ? k0 - base : 0 + * q = hi32(dpos * magic) (= floor(dpos/p) or one more: magic is rounded up) + * r = dpos - q*p (q one too big -> r = (dpos mod p) - p, wrapped) + * j = p - r; j -= p * ((r - 1) >> 31) (r == 0 -> 0; wrapped r -> p - (dpos mod p)) + * One fix covers both cases: with q one too big, dpos mod p >= p - dpos*e/2^32 > p/2 > 0 + * (e = magic*p - 2^32 < p, dpos < 2^31), so the true remainder is never 0 there and + * p - r - p = p - (dpos mod p) is the right offset. A separate "r += p if wrapped" step was + * removed after a negative control showed it changes no output (host sim, limit 1e7, where + * the overshoot occurs 268 times; bench/prime_race/tests magic_model). + * j += jadd; while j < 32 { acc |= 1 << j; j += p } + * then acc |= (w == 0) (the number 1), word = ~(acc | (w == last ? ~tailmask : 0)). + * Branch-free except the loop branches. Mutant: drops the j fix (j = p - r unreduced), so words where + * base - k0 is a multiple of p (or q overshot) mark the wrong bits. + * + * staged = 1 (OMEGA_GPU_EW_PRIME_SIEVE_SHARED, campaign C3): P4 = table words, sentinel included + * (3 per entry). The CTA first copies the table into shared memory at offset 0 (thread t copies + * words t, t + 128, ...), BAR_SYNC, then the prime loop reads shared memory (LDS) instead of + * uncached global memory. Every thread reaches the barrier: the w >= nwords exit comes after + * it. The launch must declare at least 4 * P4 bytes of shared memory. + * + * divergent (omega #308, regression R1): 0 = the warp-uniform mark loop above (campaign C5). + * 1 = the original lane-dependent mark loop (omega 888a011: `while j < 32 { acc |= 1 << j; j += p }`) + * wrapped in a reconvergence region. 2 = as 1, and the prime loop (whose break `k0 >= base + 32` + * is also lane-dependent: every lane sieves a different word) wrapped in an outer region, so the + * mark loop's region nests inside it (B0 outer, B1 inner). */ +static void gen_prime_sieve(Em *e, int mutant, int staged, int divergent) { + Pro pr = prologue(e); + int nwords = V(e), stride = V(e), lastw = V(e), tailinv = V(e); + int one = V(e), c32 = V(e), cm1 = V(e), w = V(e); + ldc32(e, nwords, ARG_P0); ldc32(e, stride, ARG_P1); ldc32(e, lastw, ARG_P2); ldc32(e, tailinv, ARG_P3); + movi(e, one, 1); movi(e, c32, 32); movi(e, cm1, 0xffffffffu); + int c4 = -1, c8 = -1, c12 = -1; + if (staged) { + int twords = V(e), i = V(e), ga = V64(e), val = V(e), sa = V(e); + c4 = V(e); c8 = V(e); c12 = V(e); + ldc32(e, twords, ARG_P4); + movi(e, c4, 4); movi(e, c8, 8); movi(e, c12, 12); + imadi(e, i, pr.tid, 1, NOREG); + /* The staging loop's trip count differs per thread (thread t copies words t, t+128, ...), + * so its exit splits the warp. The divergent variants rejoin the warp before BAR.SYNC + * (a barrier is not a reconvergence point: the fragments would stay split for the rest + * of the kernel). The production kernel keeps the 2026-10-05 C3 form unchanged. */ + if (divergent) region_begin(e); + loop_begin(e); /* staging loop */ + int ls = here(e); + isetp_ge_u32(e, i, twords); + int sdone = -1; + if (divergent) region_exit(e, RX_IF_P0); else sdone = bra_fwd_if_p0(e); + addr4(e, ga, i, pr.pa); + ldg(e, val, ga, pr.udesc); + imadi(e, sa, i, 4, NOREG); + sts32(e, sa, val); + imadi(e, i, one, OMEGA_GPU_EW_THREADS, i); + bra_always(e, ls); + loop_end(e); + if (divergent) region_join(e); else bra_patch_here(e, sdone); + bar_sync(e); + } + imadi(e, w, pr.cta, OMEGA_GPU_EW_THREADS, pr.tid); + isetp_ge_u32(e, w, nwords); + exit_if_p0(e); + + /* ptr, a64, a64m: global addresses, or (staged) 32-bit shared offsets */ + int base = V(e), basep32 = V(e), acc = V(e); + int ptr = staged ? V(e) : V64(e), a64 = staged ? V(e) : V64(e), a64m = staged ? V(e) : V64(e); + int p = V(e), magic = V(e), k0 = V(e), negp = V(e), d = V(e), neg = V(e), nd = V(e), dpos = V(e), jadd = V(e); + int q = V(e), r = V(e), j = V(e), t = V(e), z = V(e), bit = V(e); + int e1 = V(e), m = V(e), word = V(e), oaddr = V64(e); + int tt = V(e), jj = V(e), sh = V(e), c31 = V(e), cm32 = V(e); + movi(e, c31, 31); movi(e, cm32, 0xffffffe0u); + + loop_begin(e); /* word loop */ + int lw = here(e); + imadi(e, base, w, 32, NOREG); + iadd3(e, basep32, base, c32); + movrz(e, acc); + if (staged) movrz(e, ptr); else widei(e, ptr, one, 0, pr.pa); + + if (divergent >= 2) region_begin(e); /* outer region: the prime loop's break is lane-dependent */ + loop_begin(e); /* prime loop */ + int lp = here(e); + /* one memory round trip per prime: all three entry words load together (C2a). The + * sentinel entry is a whole entry, so loading p and magic before the k0 test is in bounds. */ + if (staged) { iadd3(e, a64, ptr, c8); iadd3(e, a64m, ptr, c4); } + else { widei(e, a64, one, 8, ptr); widei(e, a64m, one, 4, ptr); } + { + const int dsts[3] = { k0, p, magic }, adrs[3] = { a64, ptr, a64m }; + if (staged) ld_group(e, BW_IR_LDS32, 3, dsts, adrs, NOREG); + else ld_group(e, BW_IR_LDG_E, 3, dsts, adrs, pr.udesc); + } + isetp_ge_u32(e, k0, basep32); + int brk = -1; + if (divergent >= 2) region_exit(e, RX_IF_P0); else brk = bra_fwd_if_p0(e); + imadi(e, negp, p, 0xffffffffu, NOREG); /* -p */ + imadi(e, d, k0, 0xffffffffu, base); /* base - k0 */ + shr_i(e, neg, d, 31); + imadi(e, nd, d, 0xffffffffu, NOREG); /* -d */ + imadr(e, dpos, neg, nd, d); /* d - neg*d */ + imadr(e, jadd, neg, nd, NOREG); /* neg * (k0 - base) */ + imadhi(e, q, dpos, magic); + imadr(e, r, q, negp, dpos); /* dpos - q*p */ + imadi(e, j, r, 0xffffffffu, p); /* p - r */ + if (!mutant) { + iadd3(e, t, r, cm1); + shr_i(e, z, t, 31); /* r == 0, or r wrapped (q one too big) */ + imadr(e, j, z, negp, j); /* j - p */ + } + iadd3(e, j, j, jadd); + + /* mark loop (campaign C5): the trip count depends only on p, so every active lane runs the + * same ceil(32/p) steps and the warp never splits here (all active lanes are on the same + * prime). Bits past this word are masked out instead of branched around. The old loop ran + * "while j < 32", a lane-dependent branch with no reconvergence point. */ + if (divergent) { + /* R1: the 888a011 mark loop, lane-dependent trip count, inside a region. Each lane + * leaves at its own iteration (the exit jumps to the BSYNC) and the warp reunites there. */ + region_begin(e); + loop_begin(e); + int li = here(e); + isetp_ge_u32(e, j, c32); + region_exit(e, RX_IF_P0); + shl_r(e, bit, one, j); + lop3lut(e, acc, acc, bit, NOREG, 0xfc); /* acc | bit */ + iadd3(e, j, j, p); + bra_always(e, li); + loop_end(e); + region_join(e); + } else { + movrz(e, tt); + loop_begin(e); + int li = here(e); + isetp_ge_u32(e, tt, c32); + int done = bra_fwd_if_p0(e); + iadd3(e, jj, j, tt); + lop3lut(e, sh, jj, c31, NOREG, 0xc0); /* jj & 31 */ + shl_r(e, bit, one, sh); + iadd3(e, t, jj, cm32); + shr_i(e, z, t, 31); /* jj < 32 (j <= p + 31, far below 2^31) */ + imadi(e, m, z, 0xffffffffu, NOREG); /* all ones when jj < 32, else 0 */ + lop3lut(e, acc, acc, bit, m, 0xf8); /* acc | (bit & m) */ + iadd3(e, tt, tt, p); + bra_always(e, li); + loop_end(e); + bra_patch_here(e, done); + } + + if (staged) iadd3(e, ptr, ptr, c12); else widei(e, ptr, one, 12, ptr); + bra_always(e, lp); + loop_end(e); + if (divergent >= 2) region_join(e); else bra_patch_here(e, brk); + + iadd3(e, t, w, cm1); + shr_i(e, z, t, 31); /* w == 0: the number 1 is not prime */ + lop3lut(e, acc, acc, z, NOREG, 0xfc); + lop3xor(e, e1, w, lastw); + iadd3(e, t, e1, cm1); + shr_i(e, z, t, 31); /* w == last word */ + imadr(e, m, z, tailinv, NOREG); + lop3lut(e, word, acc, m, NOREG, 0x03); /* ~(acc | m) */ + addr4(e, oaddr, w, pr.pc); + stg(e, oaddr, word, pr.udesc); + /* IMAD, not IADD3: the IADD3 encoder ignores the scheduler control word (fixed + * 0x010fca00), so right after a store it would not wait on the read barrier */ + imadr(e, w, stride, one, w); + isetp_ge_u32(e, w, nwords); + bra_back_if_not_p0(e, lw); + loop_end(e); + tail(e); +} + +/* ---- reconvergence probes (omega #308, regressions R3..R6 and the resource bound) ---- + * All: a, b = u32 inputs, out = u32, P0 = n; 128 threads, grid ceil(n/128); launched by + * omega_gpu_reconv_probe_u32. A lane past n clamps its index to n-1 so that it loads valid + * data and takes part in the warp-wide SHFL; it exits only after the SHFL, before its store. + * Each probe computes a per-lane r on divergent paths, joins, then + * out[i] = r + 4099 * s, s = r of lane (lane+1) in the same warp (SHFL.DOWN 1; lane 31: own r) + * so a lane that joined late, never, or with the wrong value shows in the neighbour's word too. + * The host oracle is rc_oracle in tests/gpu_reconv_test.c. */ +typedef struct { Pro pr; int n, i, idx, x, y, r, s, one, cm1; } Rp; +static Rp rp_begin(Em *e) { + Rp k; memset(&k, 0, sizeof k); + k.pr = prologue(e); + k.n = V(e); k.i = V(e); k.idx = V(e); k.x = V(e); k.y = V(e); k.r = V(e); k.s = V(e); k.one = V(e); k.cm1 = V(e); + int nm1 = V(e), d = V(e), neg = V(e), off = V64(e); + ldc32(e, k.n, ARG_P0); + movi(e, k.one, 1); movi(e, k.cm1, 0xffffffffu); + imadi(e, k.i, k.pr.cta, OMEGA_GPU_EW_THREADS, k.pr.tid); + iadd3(e, nm1, k.n, k.cm1); /* n - 1 */ + imadi(e, d, k.i, 0xffffffffu, nm1); /* (n - 1) - i, top bit set when i > n - 1 */ + shr_i(e, neg, d, 31); + imadr(e, k.idx, neg, d, k.i); /* i, or n - 1 when past the end */ + addr4(e, off, k.idx, k.pr.pa); ldg(e, k.x, off, k.pr.udesc); + addr4(e, off, k.idx, k.pr.pb); ldg(e, k.y, off, k.pr.udesc); + return k; +} +static void rp_end(Em *e, const Rp *k) { + int out = V(e), off = V64(e); + shfl_down(e, k->s, k->r, 1); + imadi(e, out, k->s, 4099, k->r); + isetp_ge_u32(e, k->i, k->n); + exit_if_p0(e); + addr4(e, off, k->i, k->pr.pc); + stg(e, off, out, k->pr.udesc); + tail(e); +} +/* R3: if/else with sides of different length. x >= y: r = f(x, y) (5 steps); else r = x + 1. + * Math mutant: the long side is one too big. */ +static void gen_rc_diamond(Em *e, int mutant) { + Rp k = rp_begin(e); + int t = V(e), u = V(e); + region_begin(e); + isetp_ge_u32(e, k.x, k.y); + int els = bra_fwd_if_p0(e); + iadd3(e, k.r, k.x, k.one); /* short side */ + region_exit(e, RX_ALWAYS); + bra_patch_here(e, els); + imadi(e, t, k.x, 3, k.y); /* long side: t = 3x + y */ + lop3xor(e, u, t, k.x); /* u = t ^ x */ + imadi(e, k.r, u, 5, k.one); /* r = 5u + 1 */ + shr_i(e, t, k.r, 3); + iadd3(e, k.r, k.r, t); /* r += r >> 3 */ + if (mutant) iadd3(e, k.r, k.r, k.one); + region_join(e); + rp_end(e, &k); +} +/* The lane-dependent loop shared by R4 and R5: v = x & 255, step = (y & 15) + 1, + * while v < 256 { v += step; cnt += 1 }. Math mutant: the bound is 255. Emits the region. */ +static void rc_count_loop(Em *e, const Rp *k, int mutant, int v, int cnt) { + int st = V(e), bound = V(e), c255 = V(e), c15 = V(e); + movi(e, c255, 255); movi(e, c15, 15); movi(e, bound, mutant ? 255 : 256); + lop3lut(e, v, k->x, c255, NOREG, 0xc0); + lop3lut(e, st, k->y, c15, NOREG, 0xc0); + iadd3(e, st, st, k->one); + movrz(e, cnt); + region_begin(e); + loop_begin(e); + int L = here(e); + isetp_ge_u32(e, v, bound); + region_exit(e, RX_IF_P0); + iadd3(e, v, v, st); + iadd3(e, cnt, cnt, k->one); + bra_always(e, L); + loop_end(e); + region_join(e); +} +/* R4: lane-dependent loop exit (break). r = 256 * cnt + v. */ +static void gen_rc_loopbreak(Em *e, int mutant) { + Rp k = rp_begin(e); + int v = V(e), cnt = V(e); + rc_count_loop(e, &k, mutant, v, cnt); + imadi(e, k.r, cnt, 256, v); + rp_end(e, &k); +} +/* R5: nested divergence. Odd x: r = 7 (skips the inner region). Even x: the R4 loop inside the + * outer region, r = 256 * cnt + v + 100. */ +static void gen_rc_nested(Em *e, int mutant) { + Rp k = rp_begin(e); + int v = V(e), cnt = V(e), low = V(e), c100 = V(e); + movi(e, k.r, 7); movi(e, c100, 100); + lop3lut(e, low, k.x, k.one, NOREG, 0xc0); /* x & 1 */ + region_begin(e); + isetp_ge_u32(e, low, k.one); + region_exit(e, RX_IF_P0); + rc_count_loop(e, &k, mutant, v, cnt); + imadi(e, k.r, cnt, 256, v); + iadd3(e, k.r, k.r, c100); + region_join(e); + rp_end(e, &k); +} +/* R6: lanes past the end, a divergent region, then a CTA-wide exchange and the warp SHFL. + * xv = x | 1 computed on divergent paths (even lanes take the branch), every thread stores xv + * to its shared slot, BAR.SYNC, reads its partner's (tid ^ 1), r = 3x + partner_xv. + * Production form: lanes past n are clamped and exit after the SHFL (correct). early_exit + * (mutant 1): lanes past n EXIT first, as a naive kernel would; the emitter refuses that + * (BAR/SHFL after a predicated EXIT). Only the host-test build path (codegen_ir) may bypass the + * refusal, so the simulator can show what the kernel would do (SHFL_EXITED); a launch never can. */ +static void gen_rc_exit_bar(Em *e, int early_exit) { + Rp k = rp_begin(e); + int saddr = V(e), paddr = V(e), yv = V(e), xv = V(e), low = V(e); + if (early_exit) { + e->exit_sync_ok = early_exit == 1 && e->test_path; + isetp_ge_u32(e, k.i, k.n); + exit_if_p0(e); + } + imadi(e, xv, k.x, 1, NOREG); + lop3lut(e, low, k.x, k.one, NOREG, 0xc0); /* x & 1 */ + region_begin(e); + isetp_ge_u32(e, low, k.one); + region_exit(e, RX_IF_P0); /* odd lanes leave */ + iadd3(e, xv, xv, k.one); /* even lanes: xv = x + 1 */ + region_join(e); + imadi(e, saddr, k.pr.tid, 4, NOREG); + sts32(e, saddr, xv); + bar_sync(e); + lop3xor(e, paddr, k.pr.tid, k.one); + imadi(e, paddr, paddr, 4, NOREG); + lds32(e, yv, paddr); + imadi(e, k.r, k.x, 3, yv); + rp_end(e, &k); +} +/* Barrier-register bound: `levels` nested regions, each leaving lanes with x >= y out and adding 1 + * for the others: r = x + levels * (x < y). 16 levels must build (B0..B15); 17 must be refused. */ +static void gen_rc_depth(Em *e, int levels) { + Rp k = rp_begin(e); + imadi(e, k.r, k.x, 1, NOREG); + for (int d = 0; d < levels; d++) { + region_begin(e); + isetp_ge_u32(e, k.x, k.y); + region_exit(e, RX_IF_P0); + iadd3(e, k.r, k.r, k.one); + } + for (int d = 0; d < levels; d++) region_join(e); + rp_end(e, &k); +} +/* Codegen refusals (each must come back CODEGEN_FAIL; a kernel with a split warp at a + * cross-lane step must never reach the chip). */ +static void gen_rc_refuse(Em *e, OmegaGpuEwOp which) { + Rp k = rp_begin(e); + switch (which) { + case OMEGA_GPU_EW_RC_REFUSE_EXIT_IN_REGION: region_begin(e); isetp_ge_u32(e, k.i, k.n); exit_if_p0(e); region_join(e); break; + case OMEGA_GPU_EW_RC_REFUSE_BAR_IN_REGION: region_begin(e); bar_sync(e); region_join(e); break; + case OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION: region_begin(e); shfl_down(e, k.s, k.x, 1); region_join(e); break; + case OMEGA_GPU_EW_RC_REFUSE_UNCLOSED: region_begin(e); isetp_ge_u32(e, k.x, k.y); region_exit(e, RX_IF_P0); break; /* no join */ + case OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY: region_join(e); break; /* BSYNC without BSSY */ + default: e->err = -1; break; + } + imadi(e, k.r, k.x, 1, NOREG); + rp_end(e, &k); +} + +/* A value first defined inside a loop body may not be read after the loop + * before it is written again: the allocator could have let a later body + * temporary share its register, and the last iteration's value would be lost. */ +static int check_loop_invariant(const Em *e) { + const OmegaRegAlloc *ra = &e->p->regalloc; + for (int L = 0; L < e->n_loops; L++) + for (int v = 0; v < ra->num_vregs; v++) { + int fd = ra->intervals[v].first_def; + if (fd < e->loop_start[L] || fd > e->loop_end[L]) continue; + for (size_t i = (size_t)e->loop_end[L] + 1; i < e->p->count; i++) { + const BlackwellIRInsn *in = &e->p->insns[i]; + if (in->src1_vreg == v || in->src2_vreg == v || in->src3_vreg == v) return -1; + if (in->dst_vreg == v && !in->is_uniform) break; + } + } + return 0; +} + +#define EW_GPR_BUDGET 64u /* the QMD declares 64 registers, as the matmul launcher does */ + +/* Mesa NAK sm120_instr_latencies.rs + sm100/reg_raw.csv: ALU/dual-ALU pairs + * and FMA/FMA pairs need 4 + 1 cycles; mixed pairs need 5 + 1. CSV rows are + * READERS, columns WRITERS (see lat_rs_gen.py), not the reverse. IMAD.WIDE + * has operand-specific latencies and is deliberately excluded here. + * + * Retain at least 5 cycles after every fixed producer. Thus the next reader + * uses the pair-specific bound and later readers are >= 9 cycles away (the + * next instruction in a tightened pair is itself fixed). No instruction, + * branch target, wait, barrier assignment or register allocation is changed. + * IADD3's encoder still supplies its historical control word; do not pretend + * to schedule that producer here. Its existing 5-cycle delay is unchanged. + * + * Predicate-to-guard uses NAK paw_latency -> raw(write, None): reader + * RedirectedFp64, writer Dualalu = 13, plus SM120's 1 = 14, in emit above. + * NVIDIA's public ISA docs are silent about SASS control-word semantics. + */ +static int fixed_pair_class(BlackwellIROpcode op) { + switch (op) { + case BW_IR_MOV_RZ: case BW_IR_MOV_IMM: case BW_IR_IADD3: + case BW_IR_LOP3_XOR: case BW_IR_LOP3_LUT: + case BW_IR_SHF_R: case BW_IR_SHF_L_U32: return 1; + case BW_IR_IMAD: case BW_IR_IMAD_HI_U32: + case BW_IR_FADD: case BW_IR_FSUB: case BW_IR_FMUL: case BW_IR_FFMA: return 2; + default: return 0; + } +} + +static void schedule_fixed_pairs(BlackwellIRProgram *p) { + for (size_t i = 0; i + 1 < p->count; i++) { + BlackwellIRInsn *a = &p->insns[i]; + const BlackwellIRInsn *b = &p->insns[i + 1]; + int cls = fixed_pair_class(a->op); + if (!cls || cls != fixed_pair_class(b->op) || a->op == BW_IR_IADD3 || + a->predicate_p0 || b->predicate_p0 || ((a->control >> 9) & 15u) != 6) + continue; + a->control = (a->control & ~(15u << 9)) | (5u << 9); + } +} + +static int build_kernel(OmegaGpuEwOp op, int mutant, OmegaBlackwellKernel *kernel, BlackwellIRProgram *prog_out, int test_path) { + BlackwellIRProgram *prog = calloc(1, sizeof *prog); + if (!prog) return OMEGA_GPU_EW_CODEGEN_FAIL; + omega_bw_ir_init(prog); + Em e; memset(&e, 0, sizeof e); e.p = prog; e.test_path = test_path; + /* mutant 1 = the op's math mutant (as before); 2 = the join (BSYNC) dropped; 3 = the BSSY + * names the wrong join. 2 and 3 only change kernels that open a region. */ + e.rg_mutant = mutant == 2 ? 1 : mutant == 3 ? 2 : 0; + int math = mutant == 1; + switch (op) { + case OMEGA_GPU_EW_EX2: gen_ex2(&e, math); break; + case OMEGA_GPU_EW_XCHG: gen_xchg(&e, math); break; + case OMEGA_GPU_EW_RMSNORM: gen_rmsnorm(&e, math); break; + case OMEGA_GPU_EW_ROPE: gen_rope(&e, math); break; + case OMEGA_GPU_EW_SWIGLU: gen_swiglu(&e, math); break; + case OMEGA_GPU_EW_PRIME_SIEVE: gen_prime_sieve(&e, math, 0, 0); break; + case OMEGA_GPU_EW_PRIME_SIEVE_SHARED: gen_prime_sieve(&e, math, 1, 0); break; + case OMEGA_GPU_EW_PRIME_SIEVE_DIV1: gen_prime_sieve(&e, math, 0, 1); break; + case OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1: gen_prime_sieve(&e, math, 1, 1); break; + case OMEGA_GPU_EW_PRIME_SIEVE_DIV2: gen_prime_sieve(&e, math, 0, 2); break; + case OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2: gen_prime_sieve(&e, math, 1, 2); break; + case OMEGA_GPU_EW_RC_DIAMOND: gen_rc_diamond(&e, math); break; + case OMEGA_GPU_EW_RC_LOOPBREAK: gen_rc_loopbreak(&e, math); break; + case OMEGA_GPU_EW_RC_NESTED: gen_rc_nested(&e, math); break; + case OMEGA_GPU_EW_RC_EXIT_BAR: gen_rc_exit_bar(&e, math); break; + case OMEGA_GPU_EW_RC_REFUSE_EXIT_SHFL: gen_rc_exit_bar(&e, 2); break; + case OMEGA_GPU_EW_RC_DEPTH16: gen_rc_depth(&e, BW_RECONV_MAX_BAR); break; + case OMEGA_GPU_EW_RC_REFUSE_DEPTH17: gen_rc_depth(&e, BW_RECONV_MAX_BAR + 1); break; + case OMEGA_GPU_EW_RC_REFUSE_EXIT_IN_REGION: case OMEGA_GPU_EW_RC_REFUSE_BAR_IN_REGION: + case OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION: case OMEGA_GPU_EW_RC_REFUSE_UNCLOSED: + case OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY: gen_rc_refuse(&e, op); break; + default: e.err = -1; break; + } + if (e.rg.depth != 0) e.err = -1; /* a region left open has no join: refuse the build */ + schedule_fixed_pairs(prog); + int rc = OMEGA_GPU_EW_CODEGEN_FAIL; + int ra_rc = (e.err == 0) ? omega_bw_regalloc_solve(prog) : -1; + int loop_rc = (ra_rc == 0) ? check_loop_invariant(&e) : -1; +#ifdef OMEGA_EW_DEBUG + fprintf(stderr, "ew codegen op=%d mutant=%d emit_err=%d insns=%zu vregs=%d regalloc=%d loops=%d peak_gpr=%u\n", + (int)op, mutant, e.err, prog->count, prog->regalloc.num_vregs, ra_rc, loop_rc, prog->regalloc.peak_gpr_usage); +#endif + if (e.err == 0 && ra_rc == 0 && loop_rc == 0 && prog->regalloc.peak_gpr_usage <= EW_GPR_BUDGET) { + size_t max_bytes = (size_t)BW_MAX_IR_INSNS * 16; + memset(kernel, 0, sizeof *kernel); + kernel->code = malloc(max_bytes); + size_t emitted = 0; + if (kernel->code && omega_bw_encode_program(prog, kernel->code, max_bytes, &emitted) == 0) { + kernel->code_size = emitted; + kernel->insn_count = emitted / 16; + kernel->gpr_count = prog->regalloc.peak_gpr_usage; + kernel->uniform_gpr_count = prog->regalloc.peak_ugpr_usage; + sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); + if (prog_out) *prog_out = *prog; + rc = OMEGA_GPU_EW_OK; + } else { +#ifdef OMEGA_EW_DEBUG + fprintf(stderr, "ew codegen op=%d: encode failed (an instruction form the encoder refuses)\n", (int)op); +#endif + free(kernel->code); kernel->code = NULL; + } + } + free(prog); + return rc; +} + +/* --------------------------------------------------------- kernel cache */ + +/* One kernel per (op, mutant); the device copy is uploaded once and stays + * resident for the process (cut 4b: no per-call code page). */ +typedef struct { int used; int op; int mutant; OmegaBlackwellKernel kernel; NvrmMem code; } Slot; +static int g_mutant_op; /* 0 = none */ +static int g_mutant_level = 1; /* the mutant number built for g_mutant_op (1 math, 2 join dropped, 3 join corrupted) */ +static Slot g_cache[8]; +static OmegaGpuScratch g_a, g_b, g_c; /* staging, grown to the high-water mark, reused per call */ +static bool g_hooked; +#define LOCK() omega_gpu_session_lock() +#define UNLOCK() omega_gpu_session_unlock() + +static void cache_clear_locked(void) { + for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) + if (g_cache[i].used) { free(g_cache[i].kernel.code); omega_gpu_session_free(&g_cache[i].code); memset(&g_cache[i], 0, sizeof g_cache[i]); } +} + +void omega_gpu_elementwise_cache_clear(void) { + LOCK(); + cache_clear_locked(); + UNLOCK(); +} + +/* Called by the session (lock held, device still open) right before it closes. */ +static void on_session_close(void) { + cache_clear_locked(); + omega_gpu_session_scratch_free(&g_a); omega_gpu_session_scratch_free(&g_b); omega_gpu_session_scratch_free(&g_c); +} + +void omega_gpu_elementwise_test_set_mutant(int op) { + LOCK(); + g_mutant_op = op; g_mutant_level = 1; + cache_clear_locked(); + UNLOCK(); +} +void omega_gpu_elementwise_test_set_mutant_level(int op, int level) { + LOCK(); + g_mutant_op = op; g_mutant_level = level < 1 ? 1 : level > 3 ? 3 : level; + cache_clear_locked(); + UNLOCK(); +} + +/* Caller holds the lock. Builds the kernel if needed; uploads the device copy when a + * device is open (upload = false lets the host-only codegen path run without a chip). */ +static int kernel_for(OmegaGpuEwOp op, bool upload, Slot **out, bool *hit) { + int mutant = (g_mutant_op == (int)op) ? g_mutant_level : 0; + Slot *s = NULL; + for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) + if (g_cache[i].used && g_cache[i].op == (int)op && g_cache[i].mutant == mutant) { s = &g_cache[i]; *hit = true; break; } + if (!s) { + OmegaBlackwellKernel k; + int rc = build_kernel(op, mutant, &k, NULL, 0); + if (rc != OMEGA_GPU_EW_OK) return rc; + size_t slot = 0; + for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) if (!g_cache[i].used) { slot = i; break; } + s = &g_cache[slot]; + if (s->used) { free(s->kernel.code); omega_gpu_session_free(&s->code); } /* full: evict slot 0 */ + memset(s, 0, sizeof *s); + s->used = 1; s->op = (int)op; s->mutant = mutant; s->kernel = k; + *hit = false; + } + if (upload && !s->code.cpu) { + if (omega_gpu_session_alloc(s->kernel.code_size, &s->code) != 0) return OMEGA_GPU_EW_CHIP_FAIL; + memcpy(s->code.cpu, s->kernel.code, s->kernel.code_size); + __asm__ volatile("dsb sy" ::: "memory"); + } + *out = s; + return OMEGA_GPU_EW_OK; +} + +int omega_gpu_elementwise_codegen(OmegaGpuEwOp op, const uint32_t *dims, float eps, OmegaBlackwellKernel *kernel) { + (void)dims; (void)eps; /* kernels read their shape from the argument words */ + if (!kernel) return OMEGA_GPU_EW_BAD_ARGS; + LOCK(); + int mutant = (g_mutant_op == (int)op) ? g_mutant_level : 0; + UNLOCK(); + return build_kernel(op, mutant, kernel, NULL, 0); +} + +int omega_gpu_elementwise_codegen_ir(OmegaGpuEwOp op, int mutant, void *prog, OmegaBlackwellKernel *kernel) { + if (!kernel) return OMEGA_GPU_EW_BAD_ARGS; + if (mutant < 0 || mutant > 3) return OMEGA_GPU_EW_BAD_ARGS; + return build_kernel(op, mutant, kernel, (BlackwellIRProgram *)prog, 1); +} + +const char *omega_gpu_elementwise_last_error(void) { return omega_gpu_session_last_error(); } + +/* ------------------------------------------------------------ launcher */ + +#define EW_POISON 0xffbadbadu /* a NaN pattern no kernel here produces on purpose */ +#define EW_WAIT_MS 600000ull /* like main's load/store launchers; a 5 s wait failed under contention (omega#198) */ + +static uint64_t now_ns(void) { + struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); + return (uint64_t)t.tv_sec * 1000000000ULL + (uint64_t)t.tv_nsec; +} + +/* Caller holds the lock. Opens the shared device on first use. */ +static int dev_open_locked(void) { + if (!g_hooked) { (void)omega_gpu_session_on_close(on_session_close); g_hooked = true; } + return omega_gpu_session_open() ? OMEGA_GPU_EW_OK : OMEGA_GPU_EW_CHIP_FAIL; +} + +/* One launch: a and b staged in, c out (c_bytes/4 words, poison-filled first), through + * the shared session (omega_gpu_session_launch: shader-cache invalidate, constant bank, + * QMD0/QMD1, first marker, L2_FLUSH_DIRTY, second marker), then readback and the + * poison scan. b may be NULL when b_fill wrote the staging buffer already. */ +static int run_abc(const Slot *ks, const void *a, size_t a_bytes, const void *b, size_t b_bytes, + void *c, size_t c_bytes, uint32_t threads_x, uint32_t grid_x, + const uint32_t params[4], OmegaGpuEwInfo *info) { + if (omega_gpu_session_scratch(&g_a, a_bytes) || omega_gpu_session_scratch(&g_b, b_bytes) || omega_gpu_session_scratch(&g_c, c_bytes)) + return OMEGA_GPU_EW_CHIP_FAIL; + memcpy(g_a.mem.cpu, a, a_bytes); + if (b) memcpy(g_b.mem.cpu, b, b_bytes); + size_t c_words = c_bytes / 4; + uint32_t *c_dev = (uint32_t *)g_c.mem.cpu; + for (size_t i = 0; i < c_words; i++) c_dev[i] = EW_POISON; + __asm__ volatile("dsb sy" ::: "memory"); + + uint32_t args[OMEGA_BW_CBANK_MATMUL_ARGS_WORDS]; + args[0] = (uint32_t)g_a.mem.va; args[1] = (uint32_t)(g_a.mem.va >> 32); + args[2] = (uint32_t)g_b.mem.va; args[3] = (uint32_t)(g_b.mem.va >> 32); + args[4] = (uint32_t)g_c.mem.va; args[5] = (uint32_t)(g_c.mem.va >> 32); + args[6] = params[0]; args[7] = params[1]; args[8] = params[2]; args[9] = params[3]; + OmegaGpuLaunch L = { + .code_va = ks->code.va, .gpr_count = EW_GPR_BUDGET, .threads_x = threads_x, .threads_y = 1, + .grid_x = grid_x, .grid_y = 1, .num_elements = c_words, + .args = args, .n_args = OMEGA_BW_CBANK_MATMUL_ARGS_WORDS, .timeout_ms = EW_WAIT_MS, + }; + uint64_t elapsed = 0; uint32_t marker = 0; + if (omega_gpu_session_launch(&L, &elapsed, &marker) != 0) return OMEGA_GPU_EW_CHIP_FAIL; + + memcpy(c, g_c.mem.cpu, c_bytes); + uint32_t unwritten = 0; + for (size_t i = 0; i < c_words; i++) if (((uint32_t *)c)[i] == EW_POISON) unwritten++; + if (info) { + info->chip_calls++; + info->elapsed_ns += elapsed; + info->completion_marker = marker; + info->unwritten_words += unwritten; + info->threads_per_cta = threads_x; + info->ctas_last_launch = grid_x; + } + return unwritten ? OMEGA_GPU_EW_UNWRITTEN : OMEGA_GPU_EW_OK; +} + +/* ------------------------------------------------------- public entries */ + +static void info_begin(OmegaGpuEwInfo *info, const OmegaBlackwellKernel *k, bool hit) { + if (!info) return; + memset(info, 0, sizeof *info); + info->kernel_cache_hit = hit; + info->gpr_count = k->gpr_count; + info->insn_count = (uint32_t)k->insn_count; + snprintf(info->target_chip, sizeof info->target_chip, "NVIDIA DGX Spark (Grace Blackwell GB10)"); + info->sm_architecture = 121; +} +static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } +static void info_end(OmegaGpuEwInfo *info, uint64_t t0) { if (info) info->call_ns = now_ns() - t0; } + +/* Common prologue of every chip entry: lock, open, kernel (resident), info. */ +static int begin(OmegaGpuEwOp op, Slot **ks, OmegaGpuEwInfo *info) { + LOCK(); + int rc = dev_open_locked(); + bool hit = false; + if (rc == OMEGA_GPU_EW_OK) rc = kernel_for(op, true, ks, &hit); + if (rc != OMEGA_GPU_EW_OK) { UNLOCK(); return rc; } + info_begin(info, &(*ks)->kernel, hit); + return OMEGA_GPU_EW_OK; +} + +int omega_gpu_rmsnorm_f32(uint32_t rows, uint32_t dim, const float *x, const float *weight, + float eps, float *out, OmegaGpuEwInfo *info) { + uint64_t t0 = now_ns(); + if (!x || !weight || !out || rows == 0 || dim == 0 || dim % OMEGA_GPU_EW_THREADS != 0) return OMEGA_GPU_EW_BAD_ARGS; + if (dim > OMEGA_GPU_EW_MAX_DIM) return OMEGA_GPU_EW_TOO_LARGE; + Slot *k = NULL; + int rc = begin(OMEGA_GPU_EW_RMSNORM, &k, info); + if (rc != OMEGA_GPU_EW_OK) return rc; + uint32_t params[4] = { dim / OMEGA_GPU_EW_THREADS, f2u(1.0f / (float)dim), f2u(eps), dim }; + for (uint32_t r0 = 0; r0 < rows && rc == OMEGA_GPU_EW_OK; r0 += OMEGA_GPU_EW_MAX_CTAS) { + uint32_t nr = rows - r0 < OMEGA_GPU_EW_MAX_CTAS ? rows - r0 : OMEGA_GPU_EW_MAX_CTAS; + size_t bytes = (size_t)nr * dim * 4; + rc = run_abc(k, x + (size_t)r0 * dim, bytes, weight, (size_t)dim * 4, out + (size_t)r0 * dim, bytes, + OMEGA_GPU_EW_THREADS, nr, params, info); + } + UNLOCK(); + info_end(info, t0); + return rc; +} + +int omega_gpu_rope_f32(uint32_t heads, uint32_t head_dim, const float *v, + const float *cos_half, const float *sin_half, float *out, OmegaGpuEwInfo *info) { + uint64_t t0 = now_ns(); + if (!v || !cos_half || !sin_half || !out || heads == 0 || head_dim < 2 || (head_dim & 1)) return OMEGA_GPU_EW_BAD_ARGS; + if (head_dim > 2048) return OMEGA_GPU_EW_TOO_LARGE; + Slot *k = NULL; + int rc = begin(OMEGA_GPU_EW_ROPE, &k, info); + if (rc != OMEGA_GPU_EW_OK) return rc; + uint32_t half = head_dim / 2; + uint32_t params[4] = { head_dim, half, 0, 0 }; + for (uint32_t h0 = 0; h0 < heads && rc == OMEGA_GPU_EW_OK; h0 += OMEGA_GPU_EW_MAX_CTAS) { + uint32_t nh = heads - h0 < OMEGA_GPU_EW_MAX_CTAS ? heads - h0 : OMEGA_GPU_EW_MAX_CTAS; + size_t bytes = (size_t)nh * head_dim * 4; + /* table: cos in the first half of each head, sin in the second, written straight into + * the staging buffer (one copy per head of this launch) */ + if (omega_gpu_session_scratch(&g_b, bytes) != 0) { rc = OMEGA_GPU_EW_CHIP_FAIL; break; } + float *table = g_b.mem.cpu; + for (uint32_t h = 0; h < nh; h++) { + memcpy(table + (size_t)h * head_dim, cos_half, half * sizeof *table); + memcpy(table + (size_t)h * head_dim + half, sin_half, half * sizeof *table); + } + /* out may alias v: v is copied into the staging buffer before the result lands in out */ + rc = run_abc(k, v + (size_t)h0 * head_dim, bytes, NULL, bytes, out + (size_t)h0 * head_dim, bytes, half, nh, params, info); + } + UNLOCK(); + info_end(info, t0); + return rc; +} + +static int run_1d(OmegaGpuEwOp op, uint32_t n, const void *a, const void *b, void *c, OmegaGpuEwInfo *info) { + uint64_t t0 = now_ns(); + Slot *k = NULL; + int rc = begin(op, &k, info); + if (rc != OMEGA_GPU_EW_OK) return rc; + const uint32_t chunk = OMEGA_GPU_EW_MAX_CTAS * OMEGA_GPU_EW_THREADS; + for (uint32_t i0 = 0; i0 < n && rc == OMEGA_GPU_EW_OK; i0 += chunk) { + uint32_t ni = n - i0 < chunk ? n - i0 : chunk; + uint32_t grid = (ni + OMEGA_GPU_EW_THREADS - 1) / OMEGA_GPU_EW_THREADS; + uint32_t params[4] = { ni, 0, 0, 0 }; + size_t bytes = (size_t)ni * 4; + rc = run_abc(k, (const uint8_t *)a + (size_t)i0 * 4, bytes, b ? (const uint8_t *)b + (size_t)i0 * 4 : a, bytes, + (uint8_t *)c + (size_t)i0 * 4, bytes, OMEGA_GPU_EW_THREADS, grid, params, info); + } + UNLOCK(); + info_end(info, t0); + return rc; +} + +int omega_gpu_swiglu_f32(uint32_t n, const float *gate, const float *up, float *out, OmegaGpuEwInfo *info) { + if (!gate || !up || !out || n == 0) return OMEGA_GPU_EW_BAD_ARGS; + return run_1d(OMEGA_GPU_EW_SWIGLU, n, gate, up, out, info); +} +int omega_gpu_ex2_f32(uint32_t n, const float *x, float *out, OmegaGpuEwInfo *info) { + if (!x || !out || n == 0) return OMEGA_GPU_EW_BAD_ARGS; + return run_1d(OMEGA_GPU_EW_EX2, n, x, NULL, out, info); +} +int omega_gpu_shared_xchg_u32(uint32_t n, const uint32_t *x, uint32_t *out, OmegaGpuEwInfo *info) { + if (!x || !out || n == 0 || n % OMEGA_GPU_EW_THREADS != 0) return OMEGA_GPU_EW_BAD_ARGS; + return run_1d(OMEGA_GPU_EW_XCHG, n, x, NULL, out, info); +} +int omega_gpu_reconv_probe_u32(OmegaGpuEwOp op, uint32_t n, const uint32_t *a, const uint32_t *b, uint32_t *out, OmegaGpuEwInfo *info) { + if (!a || !b || !out || n == 0) return OMEGA_GPU_EW_BAD_ARGS; + switch (op) { + case OMEGA_GPU_EW_RC_DIAMOND: case OMEGA_GPU_EW_RC_LOOPBREAK: case OMEGA_GPU_EW_RC_NESTED: + case OMEGA_GPU_EW_RC_EXIT_BAR: case OMEGA_GPU_EW_RC_DEPTH16: break; + default: return OMEGA_GPU_EW_BAD_ARGS; + } + return run_1d(op, n, a, b, out, info); +} + +const char *omega_gpu_elementwise_rc_name(int rc) { + switch (rc) { + case OMEGA_GPU_EW_OK: return "OK"; + case OMEGA_GPU_EW_BAD_ARGS: return "BAD_ARGS"; + case OMEGA_GPU_EW_TOO_LARGE: return "TOO_LARGE"; + case OMEGA_GPU_EW_CODEGEN_FAIL: return "CODEGEN_FAIL"; + case OMEGA_GPU_EW_CHIP_FAIL: return "CHIP_FAIL"; + case OMEGA_GPU_EW_UNWRITTEN: return "UNWRITTEN"; + default: return "UNKNOWN"; + } +} diff --git a/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/omega_gpu_elementwise_api.h b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/omega_gpu_elementwise_api.h new file mode 100644 index 000000000..8b92c839d --- /dev/null +++ b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/omega_gpu_elementwise_api.h @@ -0,0 +1,177 @@ +#ifndef OMEGA_GPU_ELEMENTWISE_API_H +#define OMEGA_GPU_ELEMENTWISE_API_H +/* + * omega_gpu_elementwise_api: native (no CUDA) elementwise and row-reduction + * kernels for the AIEN inference stack (FB-1 cut 4, 2026-10-04). + * + * Three inference ops plus two probe ops, every kernel generated by our own + * codegen (src/omega_blackwell_codegen.c IR) and launched through the same + * chip-proven sequence as the tensor matmul path: all buffers GPU-uncached + * (nvos.h NVOS32_ATTR2_GPU_CACHEABLE: system memory cached by the GPU is not + * coherent with CPU mappings), first marker, L2_FLUSH_DIRTY, second marker + * (C3 tail), poison-filled output scanned for unwritten words after readback. + * + * The math follows aien-sovereign-core crates/aien-inference-abi/src/tensor.rs + * (ReferenceCpuBackend), read 2026-10-04 at main 5fdab70: + * rmsnorm: scale = 1/sqrt(mean(x^2) + eps) (oracle sums in f64; the chip + * sums in f32 and uses MUFU.RSQ refined by one Newton step), + * out[i] = (x[i] * scale) * weight[i] in that order. + * rope: rotate_half pairs (i, i + head_dim/2) per head, cos/sin tables + * supplied by the host as f32 (the oracle casts f64 cos/sin to f32 + * before the f32 math): out0 = q0*cos - q1*sin, out1 = q0*sin + q1*cos, + * emitted as FMUL, FMUL, FADD (no fused multiply-add), so the chip + * result is bit-identical to the f32 oracle. + * swiglu: out = silu(gate) * up, silu(g) = g / (1 + exp(-g)); the chip uses + * MUFU.EX2(g * -log2(e)) and MUFU.RCP (approximate units), the + * oracle computes in f64. Tolerance is stated per op in the test. + * + * Cut 4b (2026-10-04): the device, channel and launch scratch are the process-wide + * session shared with the matmul and attention APIs (omega_gpu_session.h): one open + * per process instead of one per call (~40 ms), kernels resident after first use, + * staging buffers reused at their high-water mark. Calls serialise on one lock. + * + * Launch envelope: at most OMEGA_GPU_EW_MAX_CTAS CTAs per launch (the matmul + * sweep of 2026-10-03 lost whole tiles at 128 CTAs, investigation I42); the + * API splits larger work on the host and makes several launches. + */ +#include +#include +#include +#include "omega_blackwell_matmul.h" /* OmegaBlackwellKernel */ + +#ifdef __cplusplus +extern "C" { +#endif + +enum { + OMEGA_GPU_EW_OK = 0, + OMEGA_GPU_EW_BAD_ARGS = -1, /* NULL pointer, zero dimension, or a shape rule below broken */ + OMEGA_GPU_EW_TOO_LARGE = -2, /* dimension outside the codegen limits */ + OMEGA_GPU_EW_CODEGEN_FAIL = -3, /* our codegen refused or exceeded the register budget */ + OMEGA_GPU_EW_CHIP_FAIL = -4, /* device open, submit, marker or release failed */ + OMEGA_GPU_EW_UNWRITTEN = -5 /* output words still equal to their poison after the run */ +}; + +/* Kernel families. The two probes exist to prove the new instructions on the + * chip with the smallest possible kernels (and are kept for regression). */ +typedef enum { + OMEGA_GPU_EW_EX2 = 1, /* out[i] = 2^a[i] (probe for MUFU.EX2) */ + OMEGA_GPU_EW_XCHG, /* out[i] = a[i ^ (T-1)] inside each CTA (probe for STS/BAR.SYNC/LDS) */ + OMEGA_GPU_EW_RMSNORM, + OMEGA_GPU_EW_ROPE, + OMEGA_GPU_EW_SWIGLU, + OMEGA_GPU_EW_PRIME_SIEVE, /* prime race (2026-10-05): word-parallel odd-only sieve, codegen only + * here; launched by bench/prime_race/gb10_native.c (argument words in + * gen_prime_sieve's comment) */ + OMEGA_GPU_EW_PRIME_SIEVE_SHARED, /* the same sieve with the table staged in shared memory (C3) */ + /* Structured warp reconvergence (omega #308). The four sieves below are the production sieve + * with its lane-dependent loops put back inside BSSY/BSYNC regions (regression R1): DIV1 wraps + * the original mark loop (omega 888a011), DIV2 also wraps the prime loop (nested regions). */ + OMEGA_GPU_EW_PRIME_SIEVE_DIV1, + OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1, + OMEGA_GPU_EW_PRIME_SIEVE_DIV2, + OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2, + /* Probes for omega_gpu_reconv_probe_u32 (u32 in a, b; u32 out; oracle in tests/gpu_reconv_test.c) */ + OMEGA_GPU_EW_RC_DIAMOND, /* R3: if/else, sides of different length, SHFL after the join */ + OMEGA_GPU_EW_RC_LOOPBREAK, /* R4: loop with a lane-dependent break */ + OMEGA_GPU_EW_RC_NESTED, /* R5: a lane-dependent loop region inside an if region */ + OMEGA_GPU_EW_RC_EXIT_BAR, /* R6: lanes past the end + STS/BAR.SYNC/LDS exchange + SHFL, lanes exit last */ + OMEGA_GPU_EW_RC_DEPTH16, /* 16 nested regions: every barrier register B0..B15 in use */ + /* Codegen refusals: omega_gpu_elementwise_codegen_ir must return CODEGEN_FAIL for each */ + OMEGA_GPU_EW_RC_REFUSE_DEPTH17, /* a 17th nested region: no barrier register left */ + OMEGA_GPU_EW_RC_REFUSE_EXIT_SHFL, /* lanes EXIT, then BAR.SYNC/SHFL on the rest */ + OMEGA_GPU_EW_RC_REFUSE_EXIT_IN_REGION, /* EXIT inside an open region */ + OMEGA_GPU_EW_RC_REFUSE_BAR_IN_REGION, /* BAR.SYNC inside an open region */ + OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION, /* SHFL inside an open region */ + OMEGA_GPU_EW_RC_REFUSE_UNCLOSED, /* region begun, never joined */ + OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY /* BSYNC with no BSSY */ +} OmegaGpuEwOp; + +/* Shape rules (checked, refused with BAD_ARGS / TOO_LARGE): + * rmsnorm: dim % OMEGA_GPU_EW_THREADS == 0, dim <= OMEGA_GPU_EW_MAX_DIM, rows >= 1 + * rope: head_dim even, 2 <= head_dim <= 2048 (head_dim/2 threads per CTA), heads >= 1 + * swiglu: n >= 1 (the kernel guards the tail with a predicated EXIT) + * ex2: n >= 1; xchg: n % OMEGA_GPU_EW_THREADS == 0 */ +#define OMEGA_GPU_EW_THREADS 128u +#define OMEGA_GPU_EW_MAX_DIM 16384u +#define OMEGA_GPU_EW_MAX_CTAS 64u + +typedef struct { + uint32_t chip_calls; /* launches made */ + uint64_t elapsed_ns; /* sum of launch-to-second-marker over the launches */ + uint32_t completion_marker; /* observed first marker word of the last launch */ + uint32_t unwritten_words; /* output words still equal to poison (0 on success) */ + bool kernel_cache_hit; /* the shape's kernel was already generated in this process */ + uint32_t gpr_count; /* registers the generated kernel uses */ + uint32_t insn_count; + uint32_t threads_per_cta; + uint32_t ctas_last_launch; + char target_chip[64]; + uint32_t sm_architecture; + uint64_t call_ns; /* cut 4b: wall time of the whole API call (staging, launches, copy-out) */ +} OmegaGpuEwInfo; + +/* out[r*dim + i] = x[r*dim + i] * scale_r * weight[i]; scale_r = 1/sqrt(mean_i(x^2) + eps). */ +int omega_gpu_rmsnorm_f32(uint32_t rows, uint32_t dim, const float *x, const float *weight, + float eps, float *out, OmegaGpuEwInfo *info); + +/* v holds `heads` heads of head_dim values back to back; cos_half and sin_half + * hold head_dim/2 values each (cos(pos*freq_i), sin(pos*freq_i)); out gets the + * rotated heads (may alias v: the API copies through its own buffers). */ +int omega_gpu_rope_f32(uint32_t heads, uint32_t head_dim, const float *v, + const float *cos_half, const float *sin_half, float *out, + OmegaGpuEwInfo *info); + +/* out[i] = silu(gate[i]) * up[i] for i < n. */ +int omega_gpu_swiglu_f32(uint32_t n, const float *gate, const float *up, float *out, + OmegaGpuEwInfo *info); + +/* Probes. ex2: out[i] = 2^x[i]. xchg: out[i] = x[i ^ (OMEGA_GPU_EW_THREADS-1)] within each CTA. */ +int omega_gpu_ex2_f32(uint32_t n, const float *x, float *out, OmegaGpuEwInfo *info); +int omega_gpu_shared_xchg_u32(uint32_t n, const uint32_t *x, uint32_t *out, OmegaGpuEwInfo *info); +/* Reconvergence probes (op = one of OMEGA_GPU_EW_RC_DIAMOND .. OMEGA_GPU_EW_RC_DEPTH16, else + * BAD_ARGS). n >= 1, any n: lanes past the end take part in the warp step and exit before + * their store. out[i] = r_i + 4099 * r_(i+1 in warp); r per op in src/omega_gpu_elementwise_api.c. */ +int omega_gpu_reconv_probe_u32(OmegaGpuEwOp op, uint32_t n, const uint32_t *a, const uint32_t *b, + uint32_t *out, OmegaGpuEwInfo *info); + +/* Generate (without launching) the kernel for one op. Kernels read their shape + * from the argument words, so one kernel per op serves every shape; dims and eps + * are accepted for forward compatibility and ignored today (may be NULL / 0). + * Lets tests disassemble the bytes offline (nvdisasm) and check the register + * budget. Caller frees kernel->code. */ +int omega_gpu_elementwise_codegen(OmegaGpuEwOp op, const uint32_t *dims, float eps, + OmegaBlackwellKernel *kernel); + +/* Same, with the mutant chosen by the caller (0 = production kernel, 1 = the op's math mutant, + * 2 = the reconvergence join (BSYNC) dropped, 3 = the BSSY names the wrong join; 2 and 3 change + * only kernels that open a region, the host simulator must catch both) and, when prog is + * not NULL, a copy of the allocated IR program (a BlackwellIRProgram, post register + * allocation) for a host IR simulator. Does not touch the device or the kernel cache. + * Caller frees kernel->code. */ +int omega_gpu_elementwise_codegen_ir(OmegaGpuEwOp op, int mutant, void *prog, + OmegaBlackwellKernel *kernel); + +/* TEST ONLY. Selects a deliberately wrong kernel for one op (0 clears). Each + * mutant changes the math by far more than the op's tolerance, so a parity + * check that still passes against it proves nothing. Clears the kernel cache. + * EX2: computes 2^(x+1). XCHG: reads its own shared word (no exchange). + * RMSNORM: the warp tree drops its last SHFL step (half the lanes go missing). + * ROPE: out0 = q0*cos + q1*sin. SWIGLU: exp(+g) instead of exp(-g). */ +void omega_gpu_elementwise_test_set_mutant(int op); +/* TEST ONLY. Like test_set_mutant, choosing the mutant number (1 math, 2 join dropped, 3 join + * corrupted) that the next launch of `op` builds; lets a chip test observe what a missing + * reconvergence does (the simulator must have flagged it first). */ +void omega_gpu_elementwise_test_set_mutant_level(int op, int level); + +const char *omega_gpu_elementwise_rc_name(int rc); +void omega_gpu_elementwise_cache_clear(void); + +/* Stage name of the most recent CHIP_FAIL ("" if none), shared with the other GPU APIs + * (omega_gpu_session_last_error); an open failure carries the driver text. */ +const char *omega_gpu_elementwise_last_error(void); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/receipt.json b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/receipt.json new file mode 100644 index 000000000..3246b6dbd --- /dev/null +++ b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/receipt.json @@ -0,0 +1 @@ +{"schema":"OMEGA_GPU_RECONV_V1","issue":308,"comparison":"exact u32","cases":[{"op":"rc_diamond","regression":"R3","n":1,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":109249,"chip_calls":1,"unwritten_words":0,"cache_hit":false},{"op":"rc_diamond","regression":"R3","n":5,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103633,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":31,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103393,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":32,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103361,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":33,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102833,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":63,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":96241,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":64,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102865,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":65,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102592,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":96,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102849,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":127,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102673,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":128,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102945,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":129,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102929,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":200,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102929,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":1000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102865,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":4096,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103041,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":8191,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103681,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":8192,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102785,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":8193,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":205698,"chip_calls":2,"unwritten_words":0,"cache_hit":true},{"op":"rc_diamond","regression":"R3","n":20000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":308867,"chip_calls":3,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":1,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103233,"chip_calls":1,"unwritten_words":0,"cache_hit":false},{"op":"rc_loopbreak","regression":"R4","n":5,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102769,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":31,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102577,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":32,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102657,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":33,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102769,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":63,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102945,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":64,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102529,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":65,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102945,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":96,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102721,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":127,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103297,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":128,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102609,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":129,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102801,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":200,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103089,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":1000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103041,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":4096,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102961,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":8191,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102689,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":8192,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103457,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":8193,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":205793,"chip_calls":2,"unwritten_words":0,"cache_hit":true},{"op":"rc_loopbreak","regression":"R4","n":20000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":309139,"chip_calls":3,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":1,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102881,"chip_calls":1,"unwritten_words":0,"cache_hit":false},{"op":"rc_nested","regression":"R5","n":5,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102832,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":31,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102657,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":32,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102881,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":33,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102897,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":63,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102865,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":64,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102913,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":65,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102897,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":96,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102897,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":127,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103057,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":128,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102897,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":129,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":70176,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":200,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103041,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":1000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102960,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":4096,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102753,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":8191,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103345,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":8192,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102977,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":8193,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":206690,"chip_calls":2,"unwritten_words":0,"cache_hit":true},{"op":"rc_nested","regression":"R5","n":20000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":310787,"chip_calls":3,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":1,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103329,"chip_calls":1,"unwritten_words":0,"cache_hit":false},{"op":"rc_exit_bar","regression":"R6","n":5,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103968,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":31,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":104017,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":32,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102945,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":33,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102913,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":63,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102609,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":64,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102865,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":65,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102913,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":96,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102881,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":127,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102929,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":128,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":102913,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":129,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103665,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":200,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103361,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":1000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103281,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":4096,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103393,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":8191,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103345,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":8192,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103121,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":8193,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":206690,"chip_calls":2,"unwritten_words":0,"cache_hit":true},{"op":"rc_exit_bar","regression":"R6","n":20000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":310643,"chip_calls":3,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":1,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103137,"chip_calls":1,"unwritten_words":0,"cache_hit":false},{"op":"rc_depth16","regression":"B0..B15","n":5,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103169,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":31,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103025,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":32,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103553,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":33,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103281,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":63,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103217,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":64,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103393,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":65,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103681,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":96,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103761,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":127,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103360,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":128,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103217,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":129,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103233,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":200,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103217,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":1000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103297,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":4096,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103361,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":8191,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103328,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":8192,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":103105,"chip_calls":1,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":8193,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":205794,"chip_calls":2,"unwritten_words":0,"cache_hit":true},{"op":"rc_depth16","regression":"B0..B15","n":20000,"rc":"OK","violations":0,"pass":true,"chip_elapsed_ns":308835,"chip_calls":3,"unwritten_words":0,"cache_hit":true}],"mutants":[{"op":"rc_diamond","mutant":1,"rc":"OK","violations":755,"caught":true},{"op":"rc_loopbreak","mutant":1,"rc":"OK","violations":377,"caught":true},{"op":"rc_nested","mutant":1,"rc":"OK","violations":110,"caught":true},{"op":"rc_exit_bar","mutant":1,"rc":"CODEGEN_FAIL","violations":1000,"caught":true}],"observations":[{"op":"rc_diamond","mutant":2,"what":"BSYNC dropped","n":1000,"rc":"OK","violations":466,"first_bad":0,"gate":false}],"sim_model_n4096":[{"op":"rc_diamond","order":0,"warp_issues":4736,"lane_issues":133045,"splits":128,"max_frags":2,"peak_bars":1},{"op":"rc_diamond","order":1,"warp_issues":4736,"lane_issues":133096,"splits":128,"max_frags":2,"peak_bars":1},{"op":"rc_loopbreak","order":0,"warp_issues":115685,"lane_issues":713552,"splits":2889,"max_frags":28,"peak_bars":1},{"op":"rc_loopbreak","order":1,"warp_issues":116847,"lane_issues":728322,"splits":2876,"max_frags":2,"peak_bars":1},{"op":"rc_nested","order":0,"warp_issues":92819,"lane_issues":444474,"splits":1739,"max_frags":20,"peak_bars":2},{"op":"rc_nested","order":1,"warp_issues":91594,"lane_issues":433797,"splits":1694,"max_frags":2,"peak_bars":2},{"op":"rc_exit_bar","order":0,"warp_issues":5120,"lane_issues":157717,"splits":128,"max_frags":2,"peak_bars":1},{"op":"rc_exit_bar","order":1,"warp_issues":5120,"lane_issues":157638,"splits":128,"max_frags":2,"peak_bars":1},{"op":"rc_depth16","order":0,"warp_issues":13696,"lane_issues":280428,"splits":128,"max_frags":2,"peak_bars":16},{"op":"rc_depth16","order":1,"warp_issues":13696,"lane_issues":279136,"splits":128,"max_frags":2,"peak_bars":16}],"device_opens":1,"checks":220,"failed":0,"verdict":"OMEGA_GPU_RECONV_PASS"} diff --git a/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/run.log b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/run.log new file mode 100644 index 000000000..050f41cb5 --- /dev/null +++ b/evidence/gpu-reconv-308/T1b-GPU-RECONV-0a1812f-observe-dropped-join/run.log @@ -0,0 +1,21 @@ +codegen rc_diamond: 37 insns, 14 gprs, 1 regions +codegen rc_loopbreak: 41 insns, 14 gprs, 1 regions +codegen rc_nested: 49 insns, 15 gprs, 2 regions +codegen rc_exit_bar: 40 insns, 18 gprs, 1 regions +codegen rc_depth16: 107 insns, 14 gprs, 16 regions +nvdisasm diamond: rc=0 insns=40 undecodable=0 missing=0 (/tmp/omega_rc_diamond_2696367.sass) +nvdisasm depth16: rc=0 insns=112 undecodable=0 missing=0 (/tmp/omega_rc_depth16_2696367.sass) +nvdisasm sieve_div2: rc=0 insns=72 undecodable=0 missing=0 (/tmp/omega_rc_sieve_div2_2696367.sass) +nvdisasm forms: rc=0 insns=4 undecodable=0 missing=0 (/tmp/omega_rc_forms_2696367.sass) +model rc_diamond n=4096: warp_issues=4736 lane_util=0.878 splits=128 max_frags=2 peak_bars=1 +model rc_diamond n=4096: warp_issues=4736 lane_util=0.878 splits=128 max_frags=2 peak_bars=1 +model rc_loopbreak n=4096: warp_issues=115685 lane_util=0.193 splits=2889 max_frags=28 peak_bars=1 +model rc_loopbreak n=4096: warp_issues=116847 lane_util=0.195 splits=2876 max_frags=2 peak_bars=1 +model rc_nested n=4096: warp_issues=92819 lane_util=0.150 splits=1739 max_frags=20 peak_bars=2 +model rc_nested n=4096: warp_issues=91594 lane_util=0.148 splits=1694 max_frags=2 peak_bars=2 +model rc_exit_bar n=4096: warp_issues=5120 lane_util=0.963 splits=128 max_frags=2 peak_bars=1 +model rc_exit_bar n=4096: warp_issues=5120 lane_util=0.962 splits=128 max_frags=2 peak_bars=1 +model rc_depth16 n=4096: warp_issues=13696 lane_util=0.640 splits=128 max_frags=2 peak_bars=16 +model rc_depth16 n=4096: warp_issues=13696 lane_util=0.637 splits=128 max_frags=2 peak_bars=16 +observation: diamond with the BSYNC dropped, n=1000: rc=OK, 466 of 1000 words differ from the oracle (first 0) +PASS: 220 checks, 0 failed diff --git a/evidence/gpu-reconv-308/T2-attention/device.txt b/evidence/gpu-reconv-308/T2-attention/device.txt new file mode 100644 index 000000000..70e90ab12 --- /dev/null +++ b/evidence/gpu-reconv-308/T2-attention/device.txt @@ -0,0 +1,4 @@ +Linux spark-b87b 7.0.0-1019-nvidia #19~24.04.2-Ubuntu SMP PREEMPT_DYNAMIC Sun Sep 6 18:35:11 UTC 2 aarch64 aarch64 aarch64 GNU/Linux +NVIDIA GB10, 580.173.02 +0a1812f44eaa3dcf8fe5a914d1dd65cfaccbbcd7 +Mon Oct 5 11:14:59 PM UTC 2026 diff --git a/evidence/gpu-reconv-308/T2-attention/div-receipt.json b/evidence/gpu-reconv-308/T2-attention/div-receipt.json new file mode 100644 index 000000000..95384dfb6 --- /dev/null +++ b/evidence/gpu-reconv-308/T2-attention/div-receipt.json @@ -0,0 +1 @@ +{"schema":"OMEGA_GPU_ATTENTION_V1","tolerances":{"rel":2e-4,"abs":2e-5,"large_scores_rel":1e-3,"large_scores_abs":1e-4},"cases":[{"case":"gqa_f32_tinyllama_32q4kv_ctx1","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":110289,"call_ns":29712500,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":2048,"kv_bytes_naive":2048,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_tinyllama_32q4kv_ctx17","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00189431,"pass":true,"chip_elapsed_ns":103937,"call_ns":246802,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":34816,"kv_bytes_naive":34816,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_tinyllama_32q4kv_ctx256","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00189409,"pass":true,"chip_elapsed_ns":207634,"call_ns":356307,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":524288,"kv_bytes_naive":524288,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_tinyllama_32q4kv_ctx2048","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.0028582,"pass":true,"chip_elapsed_ns":1126026,"call_ns":1650830,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":4194304,"kv_bytes_naive":4194304,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":4,"mutants_caught":4,"mutant_note":"SLOT:CAUGHT(rc=OK,bad=2041) NO_RESCALE:CAUGHT(rc=OK,bad=1959) Q_ROW:CAUGHT(rc=OK,bad=2048) OUT_ROW:CAUGHT(rc=OK,bad=2048)"},{"case":"gqa_f32_tinyllama_32q4kv_ctx65","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00174821,"pass":true,"chip_elapsed_ns":104048,"call_ns":191361,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":133120,"kv_bytes_naive":133120,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_tinyllama_32q4kv_ctx128","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00197914,"pass":true,"chip_elapsed_ns":103585,"call_ns":108049,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":262144,"kv_bytes_naive":262144,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_tinyllama_32q4kv_ctx129","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00177573,"pass":true,"chip_elapsed_ns":102785,"call_ns":106881,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":264192,"kv_bytes_naive":264192,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_tinyllama_32q4kv_ctx300","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00259576,"pass":true,"chip_elapsed_ns":205330,"call_ns":213426,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":614400,"kv_bytes_naive":614400,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_tinyllama_32q4kv_ctx1000","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00247545,"pass":true,"chip_elapsed_ns":513781,"call_ns":546405,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":2048000,"kv_bytes_naive":2048000,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_llama32_1b_32q8kv_ctx1","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":103185,"call_ns":217570,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":4096,"kv_bytes_naive":4096,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_llama32_1b_32q8kv_ctx17","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00174276,"pass":true,"chip_elapsed_ns":72369,"call_ns":75073,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":69632,"kv_bytes_naive":69632,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":1,"mutants_caught":1,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=2048)"},{"case":"gqa_f32_llama32_1b_32q8kv_ctx256","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00161378,"pass":true,"chip_elapsed_ns":206786,"call_ns":307939,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":1048576,"kv_bytes_naive":1048576,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_llama32_1b_32q8kv_ctx2048","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.0029454,"pass":true,"chip_elapsed_ns":1124842,"call_ns":2116274,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":8388608,"kv_bytes_naive":8388608,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":4,"mutants_caught":4,"mutant_note":"SLOT:CAUGHT(rc=OK,bad=2045) NO_RESCALE:CAUGHT(rc=OK,bad=1904) Q_ROW:CAUGHT(rc=OK,bad=2042) OUT_ROW:CAUGHT(rc=OK,bad=2042)"},{"case":"gqa_f32_llama32_1b_32q8kv_ctx65","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.0014332,"pass":true,"chip_elapsed_ns":103553,"call_ns":187473,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":266240,"kv_bytes_naive":266240,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_llama32_1b_32q8kv_ctx128","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00186415,"pass":true,"chip_elapsed_ns":103425,"call_ns":110897,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":524288,"kv_bytes_naive":524288,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_llama32_1b_32q8kv_ctx129","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00185519,"pass":true,"chip_elapsed_ns":102737,"call_ns":109729,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":528384,"kv_bytes_naive":528384,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_llama32_1b_32q8kv_ctx300","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00162705,"pass":true,"chip_elapsed_ns":204818,"call_ns":221106,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":1228800,"kv_bytes_naive":1228800,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_llama32_1b_32q8kv_ctx1000","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00330327,"pass":true,"chip_elapsed_ns":614261,"call_ns":696198,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":4096000,"kv_bytes_naive":4096000,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_large_scores_32q4kv_ctx256","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.105395,"pass":true,"chip_elapsed_ns":206130,"call_ns":311075,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":524288,"kv_bytes_naive":524288,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":1,"mutants_caught":1,"mutant_note":"NO_MAX:CAUGHT(rc=OK,bad=2048)"},{"case":"gqa_f32_mha_16q16kv_ctx300","n":1024,"rc":"OK","violations":0,"worst_scaled_err":0.00197746,"pass":true,"chip_elapsed_ns":206433,"call_ns":336195,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":2457600,"kv_bytes_naive":2457600,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":3,"mutants_caught":3,"mutant_note":"Q_ROW:CAUGHT(rc=OK,bad=1022) OUT_ROW:CAUGHT(rc=OK,bad=1024) KV_HEAD:CAUGHT(rc=OK,bad=1024)"},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx1","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":104177,"call_ns":182785,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":16384,"kv_bytes_naive":16384,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":1,"mutants_caught":1,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=2048)"},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx17","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.002096,"pass":true,"chip_elapsed_ns":103105,"call_ns":177137,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":32768,"kv_bytes_naive":32768,"kv_blocks_unique":2,"kv_blocks_logical":2,"mutants_run":2,"mutants_caught":2,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=2046) SLOT:CAUGHT(rc=OK,bad=2048)"},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx256","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00199949,"pass":true,"chip_elapsed_ns":204865,"call_ns":283218,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":262144,"kv_bytes_naive":262144,"kv_blocks_unique":16,"kv_blocks_logical":16,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx2048","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00336531,"pass":true,"chip_elapsed_ns":1019193,"call_ns":1065241,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":2097152,"kv_bytes_naive":2097152,"kv_blocks_unique":128,"kv_blocks_logical":128,"mutants_run":5,"mutants_caught":5,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=2045) SLOT:CAUGHT(rc=OK,bad=2042) NO_RESCALE:CAUGHT(rc=OK,bad=2023) Q_ROW:CAUGHT(rc=OK,bad=2042) OUT_ROW:CAUGHT(rc=OK,bad=2042)"},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx65","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00182741,"pass":true,"chip_elapsed_ns":103489,"call_ns":184978,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":81920,"kv_bytes_naive":81920,"kv_blocks_unique":5,"kv_blocks_logical":5,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx128","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00191997,"pass":true,"chip_elapsed_ns":102881,"call_ns":106353,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":131072,"kv_bytes_naive":131072,"kv_blocks_unique":8,"kv_blocks_logical":8,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx129","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00192425,"pass":true,"chip_elapsed_ns":101265,"call_ns":104833,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":147456,"kv_bytes_naive":147456,"kv_blocks_unique":9,"kv_blocks_logical":9,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx300","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00243899,"pass":true,"chip_elapsed_ns":204306,"call_ns":209794,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":311296,"kv_bytes_naive":311296,"kv_blocks_unique":19,"kv_blocks_logical":19,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx1000","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00547531,"pass":true,"chip_elapsed_ns":205234,"call_ns":209090,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":163840,"kv_bytes_naive":1032192,"kv_blocks_unique":10,"kv_blocks_logical":63,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx1","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":103169,"call_ns":184738,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":32768,"kv_bytes_naive":32768,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":1,"mutants_caught":1,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=2048)"},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx17","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00223287,"pass":true,"chip_elapsed_ns":103361,"call_ns":177489,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":65536,"kv_bytes_naive":65536,"kv_blocks_unique":2,"kv_blocks_logical":2,"mutants_run":2,"mutants_caught":2,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=2048) SLOT:CAUGHT(rc=OK,bad=2048)"},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx256","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00167964,"pass":true,"chip_elapsed_ns":204785,"call_ns":287826,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":524288,"kv_bytes_naive":524288,"kv_blocks_unique":16,"kv_blocks_logical":16,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx2048","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00358689,"pass":true,"chip_elapsed_ns":1025209,"call_ns":1119097,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":4194304,"kv_bytes_naive":4194304,"kv_blocks_unique":128,"kv_blocks_logical":128,"mutants_run":5,"mutants_caught":5,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=2048) SLOT:CAUGHT(rc=OK,bad=2045) NO_RESCALE:CAUGHT(rc=OK,bad=1999) Q_ROW:CAUGHT(rc=OK,bad=2044) OUT_ROW:CAUGHT(rc=OK,bad=2044)"},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx65","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00228809,"pass":true,"chip_elapsed_ns":104257,"call_ns":186881,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":163840,"kv_bytes_naive":163840,"kv_blocks_unique":5,"kv_blocks_logical":5,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx128","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00160376,"pass":true,"chip_elapsed_ns":103857,"call_ns":108913,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":262144,"kv_bytes_naive":262144,"kv_blocks_unique":8,"kv_blocks_logical":8,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx129","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00173996,"pass":true,"chip_elapsed_ns":103425,"call_ns":108769,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":294912,"kv_bytes_naive":294912,"kv_blocks_unique":9,"kv_blocks_logical":9,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx300","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00303696,"pass":true,"chip_elapsed_ns":205458,"call_ns":214498,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":622592,"kv_bytes_naive":622592,"kv_blocks_unique":19,"kv_blocks_logical":19,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx1000","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00420311,"pass":true,"chip_elapsed_ns":205377,"call_ns":211729,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":327680,"kv_bytes_naive":2064384,"kv_blocks_unique":10,"kv_blocks_logical":63,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_batch_3seq_32q8kv_ctx40_0_100","n":6144,"rc":"OK","violations":0,"worst_scaled_err":0.00181424,"pass":true,"chip_elapsed_ns":207090,"call_ns":432708,"chip_calls":2,"unwritten_words":0,"kv_bytes_staged":229376,"kv_bytes_naive":229376,"kv_blocks_unique":7,"kv_blocks_logical":7,"mutants_run":4,"mutants_caught":4,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=4095) SLOT:CAUGHT(rc=OK,bad=4090) Q_ROW:CAUGHT(rc=OK,bad=4096) OUT_ROW:CAUGHT(rc=OK,bad=4096)"},{"case":"paged_bf16_shared_prefix_4seq_8q2kv_10pre","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00188498,"pass":true,"chip_elapsed_ns":205346,"call_ns":283250,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":155648,"kv_bytes_naive":401408,"kv_blocks_unique":19,"kv_blocks_logical":49,"mutants_run":2,"mutants_caught":2,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=2048) SLOT:CAUGHT(rc=OK,bad=2046)"},{"case":"paged_bf16_shared_prefix_4seq_32q8kv_10pre","n":8192,"rc":"OK","violations":0,"worst_scaled_err":0.0024233,"pass":true,"chip_elapsed_ns":409427,"call_ns":507812,"chip_calls":2,"unwritten_words":0,"kv_bytes_staged":950272,"kv_bytes_naive":1605632,"kv_blocks_unique":29,"kv_blocks_logical":49,"mutants_run":2,"mutants_caught":2,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=8192) SLOT:CAUGHT(rc=OK,bad=8181)"}],"mutant_count":6,"mutants":{"NO_MAX":{"run":1,"caught":1},"KV_HEAD":{"run":11,"caught":11},"SLOT":{"run":9,"caught":9},"NO_RESCALE":{"run":4,"caught":4},"Q_ROW":{"run":6,"caught":6},"OUT_ROW":{"run":6,"caught":6}},"device_opens":1,"checks":122,"failed":0,"verdict":"OMEGA_GPU_ATTENTION_PASS"} diff --git a/evidence/gpu-reconv-308/T2-attention/div-run.log b/evidence/gpu-reconv-308/T2-attention/div-run.log new file mode 100644 index 000000000..8774f3221 --- /dev/null +++ b/evidence/gpu-reconv-308/T2-attention/div-run.log @@ -0,0 +1,49 @@ +PASS gqa_f32_tinyllama_32q4kv_ctx1 n=2048 rc=OK violations=0 worst_scaled_err=0 chip_ns=110289 call_ns=29712500 calls=1 unwritten=0 kv_staged=2048/2048 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx17 n=2048 rc=OK violations=0 worst_scaled_err=0.00189431 chip_ns=103937 call_ns=246802 calls=1 unwritten=0 kv_staged=34816/34816 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx256 n=2048 rc=OK violations=0 worst_scaled_err=0.00189409 chip_ns=207634 call_ns=356307 calls=1 unwritten=0 kv_staged=524288/524288 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx2048 n=2048 rc=OK violations=0 worst_scaled_err=0.0028582 chip_ns=1126026 call_ns=1650830 calls=1 unwritten=0 kv_staged=4194304/4194304 blocks=1/1 | mutants 4/4 SLOT:CAUGHT(rc=OK,bad=2041) NO_RESCALE:CAUGHT(rc=OK,bad=1959) Q_ROW:CAUGHT(rc=OK,bad=2048) OUT_ROW:CAUGHT(rc=OK,bad=2048) +PASS gqa_f32_tinyllama_32q4kv_ctx65 n=2048 rc=OK violations=0 worst_scaled_err=0.00174821 chip_ns=104048 call_ns=191361 calls=1 unwritten=0 kv_staged=133120/133120 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx128 n=2048 rc=OK violations=0 worst_scaled_err=0.00197914 chip_ns=103585 call_ns=108049 calls=1 unwritten=0 kv_staged=262144/262144 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx129 n=2048 rc=OK violations=0 worst_scaled_err=0.00177573 chip_ns=102785 call_ns=106881 calls=1 unwritten=0 kv_staged=264192/264192 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx300 n=2048 rc=OK violations=0 worst_scaled_err=0.00259576 chip_ns=205330 call_ns=213426 calls=1 unwritten=0 kv_staged=614400/614400 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx1000 n=2048 rc=OK violations=0 worst_scaled_err=0.00247545 chip_ns=513781 call_ns=546405 calls=1 unwritten=0 kv_staged=2048000/2048000 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx1 n=2048 rc=OK violations=0 worst_scaled_err=0 chip_ns=103185 call_ns=217570 calls=1 unwritten=0 kv_staged=4096/4096 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx17 n=2048 rc=OK violations=0 worst_scaled_err=0.00174276 chip_ns=72369 call_ns=75073 calls=1 unwritten=0 kv_staged=69632/69632 blocks=1/1 | mutants 1/1 KV_HEAD:CAUGHT(rc=OK,bad=2048) +PASS gqa_f32_llama32_1b_32q8kv_ctx256 n=2048 rc=OK violations=0 worst_scaled_err=0.00161378 chip_ns=206786 call_ns=307939 calls=1 unwritten=0 kv_staged=1048576/1048576 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx2048 n=2048 rc=OK violations=0 worst_scaled_err=0.0029454 chip_ns=1124842 call_ns=2116274 calls=1 unwritten=0 kv_staged=8388608/8388608 blocks=1/1 | mutants 4/4 SLOT:CAUGHT(rc=OK,bad=2045) NO_RESCALE:CAUGHT(rc=OK,bad=1904) Q_ROW:CAUGHT(rc=OK,bad=2042) OUT_ROW:CAUGHT(rc=OK,bad=2042) +PASS gqa_f32_llama32_1b_32q8kv_ctx65 n=2048 rc=OK violations=0 worst_scaled_err=0.0014332 chip_ns=103553 call_ns=187473 calls=1 unwritten=0 kv_staged=266240/266240 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx128 n=2048 rc=OK violations=0 worst_scaled_err=0.00186415 chip_ns=103425 call_ns=110897 calls=1 unwritten=0 kv_staged=524288/524288 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx129 n=2048 rc=OK violations=0 worst_scaled_err=0.00185519 chip_ns=102737 call_ns=109729 calls=1 unwritten=0 kv_staged=528384/528384 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx300 n=2048 rc=OK violations=0 worst_scaled_err=0.00162705 chip_ns=204818 call_ns=221106 calls=1 unwritten=0 kv_staged=1228800/1228800 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx1000 n=2048 rc=OK violations=0 worst_scaled_err=0.00330327 chip_ns=614261 call_ns=696198 calls=1 unwritten=0 kv_staged=4096000/4096000 blocks=1/1 | mutants 0/0 +PASS gqa_f32_large_scores_32q4kv_ctx256 n=2048 rc=OK violations=0 worst_scaled_err=0.105395 chip_ns=206130 call_ns=311075 calls=1 unwritten=0 kv_staged=524288/524288 blocks=1/1 | mutants 1/1 NO_MAX:CAUGHT(rc=OK,bad=2048) +PASS gqa_f32_mha_16q16kv_ctx300 n=1024 rc=OK violations=0 worst_scaled_err=0.00197746 chip_ns=206433 call_ns=336195 calls=1 unwritten=0 kv_staged=2457600/2457600 blocks=1/1 | mutants 3/3 Q_ROW:CAUGHT(rc=OK,bad=1022) OUT_ROW:CAUGHT(rc=OK,bad=1024) KV_HEAD:CAUGHT(rc=OK,bad=1024) +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx1 n=2048 rc=OK violations=0 worst_scaled_err=0 chip_ns=104177 call_ns=182785 calls=1 unwritten=0 kv_staged=16384/16384 blocks=1/1 | mutants 1/1 KV_HEAD:CAUGHT(rc=OK,bad=2048) +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx17 n=2048 rc=OK violations=0 worst_scaled_err=0.002096 chip_ns=103105 call_ns=177137 calls=1 unwritten=0 kv_staged=32768/32768 blocks=2/2 | mutants 2/2 KV_HEAD:CAUGHT(rc=OK,bad=2046) SLOT:CAUGHT(rc=OK,bad=2048) +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx256 n=2048 rc=OK violations=0 worst_scaled_err=0.00199949 chip_ns=204865 call_ns=283218 calls=1 unwritten=0 kv_staged=262144/262144 blocks=16/16 | mutants 0/0 +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx2048 n=2048 rc=OK violations=0 worst_scaled_err=0.00336531 chip_ns=1019193 call_ns=1065241 calls=1 unwritten=0 kv_staged=2097152/2097152 blocks=128/128 | mutants 5/5 KV_HEAD:CAUGHT(rc=OK,bad=2045) SLOT:CAUGHT(rc=OK,bad=2042) NO_RESCALE:CAUGHT(rc=OK,bad=2023) Q_ROW:CAUGHT(rc=OK,bad=2042) OUT_ROW:CAUGHT(rc=OK,bad=2042) +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx65 n=2048 rc=OK violations=0 worst_scaled_err=0.00182741 chip_ns=103489 call_ns=184978 calls=1 unwritten=0 kv_staged=81920/81920 blocks=5/5 | mutants 0/0 +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx128 n=2048 rc=OK violations=0 worst_scaled_err=0.00191997 chip_ns=102881 call_ns=106353 calls=1 unwritten=0 kv_staged=131072/131072 blocks=8/8 | mutants 0/0 +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx129 n=2048 rc=OK violations=0 worst_scaled_err=0.00192425 chip_ns=101265 call_ns=104833 calls=1 unwritten=0 kv_staged=147456/147456 blocks=9/9 | mutants 0/0 +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx300 n=2048 rc=OK violations=0 worst_scaled_err=0.00243899 chip_ns=204306 call_ns=209794 calls=1 unwritten=0 kv_staged=311296/311296 blocks=19/19 | mutants 0/0 +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx1000 n=2048 rc=OK violations=0 worst_scaled_err=0.00547531 chip_ns=205234 call_ns=209090 calls=1 unwritten=0 kv_staged=163840/1032192 blocks=10/63 | mutants 0/0 +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx1 n=2048 rc=OK violations=0 worst_scaled_err=0 chip_ns=103169 call_ns=184738 calls=1 unwritten=0 kv_staged=32768/32768 blocks=1/1 | mutants 1/1 KV_HEAD:CAUGHT(rc=OK,bad=2048) +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx17 n=2048 rc=OK violations=0 worst_scaled_err=0.00223287 chip_ns=103361 call_ns=177489 calls=1 unwritten=0 kv_staged=65536/65536 blocks=2/2 | mutants 2/2 KV_HEAD:CAUGHT(rc=OK,bad=2048) SLOT:CAUGHT(rc=OK,bad=2048) +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx256 n=2048 rc=OK violations=0 worst_scaled_err=0.00167964 chip_ns=204785 call_ns=287826 calls=1 unwritten=0 kv_staged=524288/524288 blocks=16/16 | mutants 0/0 +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx2048 n=2048 rc=OK violations=0 worst_scaled_err=0.00358689 chip_ns=1025209 call_ns=1119097 calls=1 unwritten=0 kv_staged=4194304/4194304 blocks=128/128 | mutants 5/5 KV_HEAD:CAUGHT(rc=OK,bad=2048) SLOT:CAUGHT(rc=OK,bad=2045) NO_RESCALE:CAUGHT(rc=OK,bad=1999) Q_ROW:CAUGHT(rc=OK,bad=2044) OUT_ROW:CAUGHT(rc=OK,bad=2044) +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx65 n=2048 rc=OK violations=0 worst_scaled_err=0.00228809 chip_ns=104257 call_ns=186881 calls=1 unwritten=0 kv_staged=163840/163840 blocks=5/5 | mutants 0/0 +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx128 n=2048 rc=OK violations=0 worst_scaled_err=0.00160376 chip_ns=103857 call_ns=108913 calls=1 unwritten=0 kv_staged=262144/262144 blocks=8/8 | mutants 0/0 +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx129 n=2048 rc=OK violations=0 worst_scaled_err=0.00173996 chip_ns=103425 call_ns=108769 calls=1 unwritten=0 kv_staged=294912/294912 blocks=9/9 | mutants 0/0 +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx300 n=2048 rc=OK violations=0 worst_scaled_err=0.00303696 chip_ns=205458 call_ns=214498 calls=1 unwritten=0 kv_staged=622592/622592 blocks=19/19 | mutants 0/0 +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx1000 n=2048 rc=OK violations=0 worst_scaled_err=0.00420311 chip_ns=205377 call_ns=211729 calls=1 unwritten=0 kv_staged=327680/2064384 blocks=10/63 | mutants 0/0 +PASS paged_bf16_batch_3seq_32q8kv_ctx40_0_100 n=6144 rc=OK violations=0 worst_scaled_err=0.00181424 chip_ns=207090 call_ns=432708 calls=2 unwritten=0 kv_staged=229376/229376 blocks=7/7 | mutants 4/4 KV_HEAD:CAUGHT(rc=OK,bad=4095) SLOT:CAUGHT(rc=OK,bad=4090) Q_ROW:CAUGHT(rc=OK,bad=4096) OUT_ROW:CAUGHT(rc=OK,bad=4096) +PASS paged_bf16_shared_prefix_4seq_8q2kv_10pre n=2048 rc=OK violations=0 worst_scaled_err=0.00188498 chip_ns=205346 call_ns=283250 calls=1 unwritten=0 kv_staged=155648/401408 blocks=19/49 | mutants 2/2 KV_HEAD:CAUGHT(rc=OK,bad=2048) SLOT:CAUGHT(rc=OK,bad=2046) +PASS paged_bf16_shared_prefix_4seq_32q8kv_10pre n=8192 rc=OK violations=0 worst_scaled_err=0.0024233 chip_ns=409427 call_ns=507812 calls=2 unwritten=0 kv_staged=950272/1605632 blocks=29/49 | mutants 2/2 KV_HEAD:CAUGHT(rc=OK,bad=8192) SLOT:CAUGHT(rc=OK,bad=8181) +mutant NO_MAX: caught 1 of 1 +mutant KV_HEAD: caught 11 of 11 +mutant SLOT: caught 9 of 9 +mutant NO_RESCALE: caught 4 of 4 +mutant Q_ROW: caught 6 of 6 +mutant OUT_ROW: caught 6 of 6 +device_opens=1 +PASS: 122 checks, 0 failed diff --git a/evidence/gpu-reconv-308/T2-attention/div-sweep.json b/evidence/gpu-reconv-308/T2-attention/div-sweep.json new file mode 100644 index 000000000..f2a0b0132 --- /dev/null +++ b/evidence/gpu-reconv-308/T2-attention/div-sweep.json @@ -0,0 +1 @@ +{"schema":"OMEGA_GPU_ATTENTION_SWEEP_V1","path":"paged_bf16","head_dim":64,"block_size":16,"timed_calls":20,"host_simulator":false,"tolerances":{"rel":2e-4,"abs":2e-5},"hardware_counters":"unavailable (SM occupancy, bandwidth utilisation, cache hit rates are not observable through this path)","points":[{"q_heads":1,"kv_heads":1,"gqa_ratio":1,"head_dim":64,"context":256,"q_width":64,"out_width":64,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00162825,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":1,"threads_per_cta":64,"call_ms_median":0.1033,"call_ms_p90":0.1038,"kernel_ms_median":0.1028,"kernel_ms_p90":0.1028,"kernel_cache_hits":20,"first_call_cache_hit":false,"kv_bytes_staged":12288,"kv_bytes_naive":65536,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":256,"tab_bytes":68,"out_bytes":256,"kv_source":"staged"},{"q_heads":1,"kv_heads":1,"gqa_ratio":1,"head_dim":64,"context":2048,"q_width":64,"out_width":64,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00286577,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":1,"threads_per_cta":64,"call_ms_median":0.4083,"call_ms_p90":0.4099,"kernel_ms_median":0.4067,"kernel_ms_p90":0.4084,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":77824,"kv_bytes_naive":524288,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":256,"tab_bytes":516,"out_bytes":256,"kv_source":"staged"},{"q_heads":2,"kv_heads":1,"gqa_ratio":2,"head_dim":64,"context":256,"q_width":128,"out_width":128,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00274735,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":2,"threads_per_cta":64,"call_ms_median":0.1037,"call_ms_p90":0.1038,"kernel_ms_median":0.1031,"kernel_ms_p90":0.1032,"kernel_cache_hits":20,"first_call_cache_hit":false,"kv_bytes_staged":12288,"kv_bytes_naive":65536,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":512,"tab_bytes":68,"out_bytes":512,"kv_source":"staged"},{"q_heads":2,"kv_heads":1,"gqa_ratio":2,"head_dim":64,"context":2048,"q_width":128,"out_width":128,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00293219,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":2,"threads_per_cta":64,"call_ms_median":0.4104,"call_ms_p90":0.4115,"kernel_ms_median":0.4089,"kernel_ms_p90":0.4099,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":77824,"kv_bytes_naive":524288,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":512,"tab_bytes":516,"out_bytes":512,"kv_source":"staged"},{"q_heads":2,"kv_heads":2,"gqa_ratio":1,"head_dim":64,"context":256,"q_width":128,"out_width":128,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00285429,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":2,"threads_per_cta":64,"call_ms_median":0.1038,"call_ms_p90":0.1043,"kernel_ms_median":0.1031,"kernel_ms_p90":0.1035,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":24576,"kv_bytes_naive":131072,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":512,"tab_bytes":68,"out_bytes":512,"kv_source":"staged"},{"q_heads":2,"kv_heads":2,"gqa_ratio":1,"head_dim":64,"context":2048,"q_width":128,"out_width":128,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00344759,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":2,"threads_per_cta":64,"call_ms_median":0.4089,"call_ms_p90":0.4100,"kernel_ms_median":0.4067,"kernel_ms_p90":0.4074,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":155648,"kv_bytes_naive":1048576,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":512,"tab_bytes":516,"out_bytes":512,"kv_source":"staged"},{"q_heads":4,"kv_heads":1,"gqa_ratio":4,"head_dim":64,"context":256,"q_width":256,"out_width":256,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00195521,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":4,"threads_per_cta":64,"call_ms_median":0.1034,"call_ms_p90":0.1040,"kernel_ms_median":0.1028,"kernel_ms_p90":0.1033,"kernel_cache_hits":20,"first_call_cache_hit":false,"kv_bytes_staged":12288,"kv_bytes_naive":65536,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":1024,"tab_bytes":68,"out_bytes":1024,"kv_source":"staged"},{"q_heads":4,"kv_heads":1,"gqa_ratio":4,"head_dim":64,"context":2048,"q_width":256,"out_width":256,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00455218,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":4,"threads_per_cta":64,"call_ms_median":0.4098,"call_ms_p90":0.4110,"kernel_ms_median":0.4082,"kernel_ms_p90":0.4094,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":77824,"kv_bytes_naive":524288,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":1024,"tab_bytes":516,"out_bytes":1024,"kv_source":"staged"},{"q_heads":4,"kv_heads":2,"gqa_ratio":2,"head_dim":64,"context":256,"q_width":256,"out_width":256,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00195668,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":4,"threads_per_cta":64,"call_ms_median":0.1039,"call_ms_p90":0.1048,"kernel_ms_median":0.1031,"kernel_ms_p90":0.1040,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":24576,"kv_bytes_naive":131072,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":1024,"tab_bytes":68,"out_bytes":1024,"kv_source":"staged"},{"q_heads":4,"kv_heads":2,"gqa_ratio":2,"head_dim":64,"context":2048,"q_width":256,"out_width":256,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00429287,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":4,"threads_per_cta":64,"call_ms_median":0.4105,"call_ms_p90":0.4118,"kernel_ms_median":0.4083,"kernel_ms_p90":0.4096,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":155648,"kv_bytes_naive":1048576,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":1024,"tab_bytes":516,"out_bytes":1024,"kv_source":"staged"},{"q_heads":4,"kv_heads":4,"gqa_ratio":1,"head_dim":64,"context":256,"q_width":256,"out_width":256,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00208557,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":4,"threads_per_cta":64,"call_ms_median":0.1041,"call_ms_p90":0.1043,"kernel_ms_median":0.1032,"kernel_ms_p90":0.1033,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":49152,"kv_bytes_naive":262144,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":1024,"tab_bytes":68,"out_bytes":1024,"kv_source":"staged"},{"q_heads":4,"kv_heads":4,"gqa_ratio":1,"head_dim":64,"context":2048,"q_width":256,"out_width":256,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00447204,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":4,"threads_per_cta":64,"call_ms_median":0.4123,"call_ms_p90":0.4137,"kernel_ms_median":0.4084,"kernel_ms_p90":0.4099,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":311296,"kv_bytes_naive":2097152,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":1024,"tab_bytes":516,"out_bytes":1024,"kv_source":"staged"},{"q_heads":8,"kv_heads":1,"gqa_ratio":8,"head_dim":64,"context":256,"q_width":512,"out_width":512,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00244755,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":8,"threads_per_cta":64,"call_ms_median":0.1035,"call_ms_p90":0.1037,"kernel_ms_median":0.1026,"kernel_ms_p90":0.1029,"kernel_cache_hits":20,"first_call_cache_hit":false,"kv_bytes_staged":12288,"kv_bytes_naive":65536,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":2048,"tab_bytes":68,"out_bytes":2048,"kv_source":"staged"},{"q_heads":8,"kv_heads":1,"gqa_ratio":8,"head_dim":64,"context":2048,"q_width":512,"out_width":512,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00561397,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":8,"threads_per_cta":64,"call_ms_median":0.4095,"call_ms_p90":0.4104,"kernel_ms_median":0.4078,"kernel_ms_p90":0.4087,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":77824,"kv_bytes_naive":524288,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":2048,"tab_bytes":516,"out_bytes":2048,"kv_source":"staged"},{"q_heads":8,"kv_heads":2,"gqa_ratio":4,"head_dim":64,"context":256,"q_width":512,"out_width":512,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00277137,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":8,"threads_per_cta":64,"call_ms_median":0.1040,"call_ms_p90":0.1043,"kernel_ms_median":0.1030,"kernel_ms_p90":0.1032,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":24576,"kv_bytes_naive":131072,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":2048,"tab_bytes":68,"out_bytes":2048,"kv_source":"staged"},{"q_heads":8,"kv_heads":2,"gqa_ratio":4,"head_dim":64,"context":2048,"q_width":512,"out_width":512,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00478059,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":8,"threads_per_cta":64,"call_ms_median":0.4106,"call_ms_p90":0.4121,"kernel_ms_median":0.4079,"kernel_ms_p90":0.4096,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":155648,"kv_bytes_naive":1048576,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":2048,"tab_bytes":516,"out_bytes":2048,"kv_source":"staged"},{"q_heads":8,"kv_heads":4,"gqa_ratio":2,"head_dim":64,"context":256,"q_width":512,"out_width":512,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00220051,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":8,"threads_per_cta":64,"call_ms_median":0.1043,"call_ms_p90":0.1044,"kernel_ms_median":0.1030,"kernel_ms_p90":0.1032,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":49152,"kv_bytes_naive":262144,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":2048,"tab_bytes":68,"out_bytes":2048,"kv_source":"staged"},{"q_heads":8,"kv_heads":4,"gqa_ratio":2,"head_dim":64,"context":2048,"q_width":512,"out_width":512,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.0051977,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":8,"threads_per_cta":64,"call_ms_median":0.4119,"call_ms_p90":0.4129,"kernel_ms_median":0.4078,"kernel_ms_p90":0.4088,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":311296,"kv_bytes_naive":2097152,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":2048,"tab_bytes":516,"out_bytes":2048,"kv_source":"staged"},{"q_heads":8,"kv_heads":8,"gqa_ratio":1,"head_dim":64,"context":256,"q_width":512,"out_width":512,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00240979,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":8,"threads_per_cta":64,"call_ms_median":0.1047,"call_ms_p90":0.1054,"kernel_ms_median":0.1030,"kernel_ms_p90":0.1034,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":98304,"kv_bytes_naive":524288,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":2048,"tab_bytes":68,"out_bytes":2048,"kv_source":"staged"},{"q_heads":8,"kv_heads":8,"gqa_ratio":1,"head_dim":64,"context":2048,"q_width":512,"out_width":512,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00466806,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":8,"threads_per_cta":64,"call_ms_median":0.4149,"call_ms_p90":0.4162,"kernel_ms_median":0.4080,"kernel_ms_p90":0.4090,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":622592,"kv_bytes_naive":4194304,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":2048,"tab_bytes":516,"out_bytes":2048,"kv_source":"staged"},{"q_heads":16,"kv_heads":1,"gqa_ratio":16,"head_dim":64,"context":256,"q_width":1024,"out_width":1024,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00249826,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":16,"threads_per_cta":64,"call_ms_median":0.1040,"call_ms_p90":0.1043,"kernel_ms_median":0.1028,"kernel_ms_p90":0.1029,"kernel_cache_hits":20,"first_call_cache_hit":false,"kv_bytes_staged":12288,"kv_bytes_naive":65536,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":4096,"tab_bytes":68,"out_bytes":4096,"kv_source":"staged"},{"q_heads":16,"kv_heads":1,"gqa_ratio":16,"head_dim":64,"context":2048,"q_width":1024,"out_width":1024,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00536905,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":16,"threads_per_cta":64,"call_ms_median":0.4102,"call_ms_p90":0.4120,"kernel_ms_median":0.4079,"kernel_ms_p90":0.4098,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":77824,"kv_bytes_naive":524288,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":4096,"tab_bytes":516,"out_bytes":4096,"kv_source":"staged"},{"q_heads":16,"kv_heads":2,"gqa_ratio":8,"head_dim":64,"context":256,"q_width":1024,"out_width":1024,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00239844,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":16,"threads_per_cta":64,"call_ms_median":0.1045,"call_ms_p90":0.1047,"kernel_ms_median":0.1032,"kernel_ms_p90":0.1033,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":24576,"kv_bytes_naive":131072,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":4096,"tab_bytes":68,"out_bytes":4096,"kv_source":"staged"},{"q_heads":16,"kv_heads":2,"gqa_ratio":8,"head_dim":64,"context":2048,"q_width":1024,"out_width":1024,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00570392,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":16,"threads_per_cta":64,"call_ms_median":0.4113,"call_ms_p90":0.4127,"kernel_ms_median":0.4082,"kernel_ms_p90":0.4097,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":155648,"kv_bytes_naive":1048576,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":4096,"tab_bytes":516,"out_bytes":4096,"kv_source":"staged"},{"q_heads":16,"kv_heads":4,"gqa_ratio":4,"head_dim":64,"context":256,"q_width":1024,"out_width":1024,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00278498,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":16,"threads_per_cta":64,"call_ms_median":0.1047,"call_ms_p90":0.1057,"kernel_ms_median":0.1032,"kernel_ms_p90":0.1041,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":49152,"kv_bytes_naive":262144,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":4096,"tab_bytes":68,"out_bytes":4096,"kv_source":"staged"},{"q_heads":16,"kv_heads":4,"gqa_ratio":4,"head_dim":64,"context":2048,"q_width":1024,"out_width":1024,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00518169,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":16,"threads_per_cta":64,"call_ms_median":0.4127,"call_ms_p90":0.4135,"kernel_ms_median":0.4083,"kernel_ms_p90":0.4090,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":311296,"kv_bytes_naive":2097152,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":4096,"tab_bytes":516,"out_bytes":4096,"kv_source":"staged"},{"q_heads":16,"kv_heads":8,"gqa_ratio":2,"head_dim":64,"context":256,"q_width":1024,"out_width":1024,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00262688,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":16,"threads_per_cta":64,"call_ms_median":0.1051,"call_ms_p90":0.1055,"kernel_ms_median":0.1030,"kernel_ms_p90":0.1034,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":98304,"kv_bytes_naive":524288,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":4096,"tab_bytes":68,"out_bytes":4096,"kv_source":"staged"},{"q_heads":16,"kv_heads":8,"gqa_ratio":2,"head_dim":64,"context":2048,"q_width":1024,"out_width":1024,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.0059316,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":16,"threads_per_cta":64,"call_ms_median":0.4161,"call_ms_p90":0.4171,"kernel_ms_median":0.4084,"kernel_ms_p90":0.4090,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":622592,"kv_bytes_naive":4194304,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":4096,"tab_bytes":516,"out_bytes":4096,"kv_source":"staged"},{"q_heads":16,"kv_heads":16,"gqa_ratio":1,"head_dim":64,"context":256,"q_width":1024,"out_width":1024,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00239914,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":16,"threads_per_cta":64,"call_ms_median":0.1060,"call_ms_p90":0.1068,"kernel_ms_median":0.1032,"kernel_ms_p90":0.1040,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":196608,"kv_bytes_naive":1048576,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":4096,"tab_bytes":68,"out_bytes":4096,"kv_source":"staged"},{"q_heads":16,"kv_heads":16,"gqa_ratio":1,"head_dim":64,"context":2048,"q_width":1024,"out_width":1024,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00553838,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":16,"threads_per_cta":64,"call_ms_median":0.4237,"call_ms_p90":0.4263,"kernel_ms_median":0.4084,"kernel_ms_p90":0.4106,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":1245184,"kv_bytes_naive":8388608,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":4096,"tab_bytes":516,"out_bytes":4096,"kv_source":"staged"},{"q_heads":24,"kv_heads":1,"gqa_ratio":24,"head_dim":64,"context":256,"q_width":1536,"out_width":1536,"class":"unsupported_by_kernel_envelope","rc":"BAD_ARGS","pass":true},{"q_heads":24,"kv_heads":1,"gqa_ratio":24,"head_dim":64,"context":2048,"q_width":1536,"out_width":1536,"class":"unsupported_by_kernel_envelope","rc":"BAD_ARGS","pass":true},{"q_heads":24,"kv_heads":2,"gqa_ratio":12,"head_dim":64,"context":256,"q_width":1536,"out_width":1536,"class":"unsupported_by_kernel_envelope","rc":"BAD_ARGS","pass":true},{"q_heads":24,"kv_heads":2,"gqa_ratio":12,"head_dim":64,"context":2048,"q_width":1536,"out_width":1536,"class":"unsupported_by_kernel_envelope","rc":"BAD_ARGS","pass":true},{"q_heads":24,"kv_heads":3,"gqa_ratio":8,"head_dim":64,"context":256,"q_width":1536,"out_width":1536,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00306755,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":24,"threads_per_cta":64,"call_ms_median":0.1048,"call_ms_p90":0.1055,"kernel_ms_median":0.1028,"kernel_ms_p90":0.1030,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":36864,"kv_bytes_naive":196608,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":6144,"tab_bytes":68,"out_bytes":6144,"kv_source":"staged"},{"q_heads":24,"kv_heads":3,"gqa_ratio":8,"head_dim":64,"context":2048,"q_width":1536,"out_width":1536,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00539717,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":24,"threads_per_cta":64,"call_ms_median":0.6157,"call_ms_p90":0.6175,"kernel_ms_median":0.6116,"kernel_ms_p90":0.6135,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":233472,"kv_bytes_naive":1572864,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":6144,"tab_bytes":516,"out_bytes":6144,"kv_source":"staged"},{"q_heads":24,"kv_heads":4,"gqa_ratio":6,"head_dim":64,"context":256,"q_width":1536,"out_width":1536,"class":"unsupported_by_kernel_envelope","rc":"BAD_ARGS","pass":true},{"q_heads":24,"kv_heads":4,"gqa_ratio":6,"head_dim":64,"context":2048,"q_width":1536,"out_width":1536,"class":"unsupported_by_kernel_envelope","rc":"BAD_ARGS","pass":true},{"q_heads":24,"kv_heads":6,"gqa_ratio":4,"head_dim":64,"context":256,"q_width":1536,"out_width":1536,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00326846,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":24,"threads_per_cta":64,"call_ms_median":0.1054,"call_ms_p90":0.1059,"kernel_ms_median":0.1031,"kernel_ms_p90":0.1037,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":73728,"kv_bytes_naive":393216,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":6144,"tab_bytes":68,"out_bytes":6144,"kv_source":"staged"},{"q_heads":24,"kv_heads":6,"gqa_ratio":4,"head_dim":64,"context":2048,"q_width":1536,"out_width":1536,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00527248,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":24,"threads_per_cta":64,"call_ms_median":0.6184,"call_ms_p90":0.6467,"kernel_ms_median":0.6121,"kernel_ms_p90":0.6388,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":466944,"kv_bytes_naive":3145728,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":6144,"tab_bytes":516,"out_bytes":6144,"kv_source":"staged"},{"q_heads":24,"kv_heads":8,"gqa_ratio":3,"head_dim":64,"context":256,"q_width":1536,"out_width":1536,"class":"unsupported_by_kernel_envelope","rc":"BAD_ARGS","pass":true},{"q_heads":24,"kv_heads":8,"gqa_ratio":3,"head_dim":64,"context":2048,"q_width":1536,"out_width":1536,"class":"unsupported_by_kernel_envelope","rc":"BAD_ARGS","pass":true},{"q_heads":24,"kv_heads":12,"gqa_ratio":2,"head_dim":64,"context":256,"q_width":1536,"out_width":1536,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00338069,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":24,"threads_per_cta":64,"call_ms_median":0.1059,"call_ms_p90":0.1062,"kernel_ms_median":0.1030,"kernel_ms_p90":0.1033,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":147456,"kv_bytes_naive":786432,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":6144,"tab_bytes":68,"out_bytes":6144,"kv_source":"staged"},{"q_heads":24,"kv_heads":12,"gqa_ratio":2,"head_dim":64,"context":2048,"q_width":1536,"out_width":1536,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00538782,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":24,"threads_per_cta":64,"call_ms_median":0.6241,"call_ms_p90":0.6251,"kernel_ms_median":0.6122,"kernel_ms_p90":0.6133,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":933888,"kv_bytes_naive":6291456,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":6144,"tab_bytes":516,"out_bytes":6144,"kv_source":"staged"},{"q_heads":24,"kv_heads":24,"gqa_ratio":1,"head_dim":64,"context":256,"q_width":1536,"out_width":1536,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00259323,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":24,"threads_per_cta":64,"call_ms_median":0.1074,"call_ms_p90":0.1078,"kernel_ms_median":0.1032,"kernel_ms_p90":0.1036,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":294912,"kv_bytes_naive":1572864,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":6144,"tab_bytes":68,"out_bytes":6144,"kv_source":"staged"},{"q_heads":24,"kv_heads":24,"gqa_ratio":1,"head_dim":64,"context":2048,"q_width":1536,"out_width":1536,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00737519,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":24,"threads_per_cta":64,"call_ms_median":0.5334,"call_ms_p90":0.5355,"kernel_ms_median":0.5085,"kernel_ms_p90":0.5112,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":1867776,"kv_bytes_naive":12582912,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":6144,"tab_bytes":516,"out_bytes":6144,"kv_source":"staged"},{"q_heads":32,"kv_heads":1,"gqa_ratio":32,"head_dim":64,"context":256,"q_width":2048,"out_width":2048,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00241453,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":32,"threads_per_cta":64,"call_ms_median":0.1048,"call_ms_p90":0.1050,"kernel_ms_median":0.1028,"kernel_ms_p90":0.1029,"kernel_cache_hits":20,"first_call_cache_hit":false,"kv_bytes_staged":12288,"kv_bytes_naive":65536,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":8192,"tab_bytes":68,"out_bytes":8192,"kv_source":"staged"},{"q_heads":32,"kv_heads":1,"gqa_ratio":32,"head_dim":64,"context":2048,"q_width":2048,"out_width":2048,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00525453,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":32,"threads_per_cta":64,"call_ms_median":0.4109,"call_ms_p90":0.4122,"kernel_ms_median":0.4080,"kernel_ms_p90":0.4092,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":77824,"kv_bytes_naive":524288,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":8192,"tab_bytes":516,"out_bytes":8192,"kv_source":"staged"},{"q_heads":32,"kv_heads":2,"gqa_ratio":16,"head_dim":64,"context":256,"q_width":2048,"out_width":2048,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.0031327,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":32,"threads_per_cta":64,"call_ms_median":0.1051,"call_ms_p90":0.1053,"kernel_ms_median":0.1030,"kernel_ms_p90":0.1032,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":24576,"kv_bytes_naive":131072,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":8192,"tab_bytes":68,"out_bytes":8192,"kv_source":"staged"},{"q_heads":32,"kv_heads":2,"gqa_ratio":16,"head_dim":64,"context":2048,"q_width":2048,"out_width":2048,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00648224,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":32,"threads_per_cta":64,"call_ms_median":0.4118,"call_ms_p90":0.4121,"kernel_ms_median":0.4082,"kernel_ms_p90":0.4086,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":155648,"kv_bytes_naive":1048576,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":8192,"tab_bytes":516,"out_bytes":8192,"kv_source":"staged"},{"q_heads":32,"kv_heads":4,"gqa_ratio":8,"head_dim":64,"context":256,"q_width":2048,"out_width":2048,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00316999,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":32,"threads_per_cta":64,"call_ms_median":0.1054,"call_ms_p90":0.1057,"kernel_ms_median":0.1031,"kernel_ms_p90":0.1032,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":49152,"kv_bytes_naive":262144,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":8192,"tab_bytes":68,"out_bytes":8192,"kv_source":"staged"},{"q_heads":32,"kv_heads":4,"gqa_ratio":8,"head_dim":64,"context":2048,"q_width":2048,"out_width":2048,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00599161,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":32,"threads_per_cta":64,"call_ms_median":0.4133,"call_ms_p90":0.4137,"kernel_ms_median":0.4082,"kernel_ms_p90":0.4086,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":311296,"kv_bytes_naive":2097152,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":8192,"tab_bytes":516,"out_bytes":8192,"kv_source":"staged"},{"q_heads":32,"kv_heads":8,"gqa_ratio":4,"head_dim":64,"context":256,"q_width":2048,"out_width":2048,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00278167,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":32,"threads_per_cta":64,"call_ms_median":0.1057,"call_ms_p90":0.1061,"kernel_ms_median":0.1031,"kernel_ms_p90":0.1035,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":98304,"kv_bytes_naive":524288,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":8192,"tab_bytes":68,"out_bytes":8192,"kv_source":"staged"},{"q_heads":32,"kv_heads":8,"gqa_ratio":4,"head_dim":64,"context":2048,"q_width":2048,"out_width":2048,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.005897,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":32,"threads_per_cta":64,"call_ms_median":0.4166,"call_ms_p90":0.4177,"kernel_ms_median":0.4082,"kernel_ms_p90":0.4094,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":622592,"kv_bytes_naive":4194304,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":8192,"tab_bytes":516,"out_bytes":8192,"kv_source":"staged"},{"q_heads":32,"kv_heads":16,"gqa_ratio":2,"head_dim":64,"context":256,"q_width":2048,"out_width":2048,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00268895,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":32,"threads_per_cta":64,"call_ms_median":0.1067,"call_ms_p90":0.1151,"kernel_ms_median":0.1030,"kernel_ms_p90":0.1058,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":196608,"kv_bytes_naive":1048576,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":8192,"tab_bytes":68,"out_bytes":8192,"kv_source":"staged"},{"q_heads":32,"kv_heads":16,"gqa_ratio":2,"head_dim":64,"context":2048,"q_width":2048,"out_width":2048,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00571167,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":32,"threads_per_cta":64,"call_ms_median":0.4256,"call_ms_p90":0.4294,"kernel_ms_median":0.4086,"kernel_ms_p90":0.4102,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":1245184,"kv_bytes_naive":8388608,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":8192,"tab_bytes":516,"out_bytes":8192,"kv_source":"staged"},{"q_heads":32,"kv_heads":32,"gqa_ratio":1,"head_dim":64,"context":256,"q_width":2048,"out_width":2048,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00256581,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":32,"threads_per_cta":64,"call_ms_median":0.1085,"call_ms_p90":0.1096,"kernel_ms_median":0.1030,"kernel_ms_p90":0.1035,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":393216,"kv_bytes_naive":2097152,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":8192,"tab_bytes":68,"out_bytes":8192,"kv_source":"staged"},{"q_heads":32,"kv_heads":32,"gqa_ratio":1,"head_dim":64,"context":2048,"q_width":2048,"out_width":2048,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00647965,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":32,"threads_per_cta":64,"call_ms_median":0.4445,"call_ms_p90":0.4463,"kernel_ms_median":0.4085,"kernel_ms_p90":0.4097,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":2490368,"kv_bytes_naive":16777216,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":8192,"tab_bytes":516,"out_bytes":8192,"kv_source":"staged"},{"q_heads":64,"kv_heads":1,"gqa_ratio":64,"head_dim":64,"context":256,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00332208,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":0.1061,"call_ms_p90":0.1067,"kernel_ms_median":0.1027,"kernel_ms_p90":0.1030,"kernel_cache_hits":20,"first_call_cache_hit":false,"kv_bytes_staged":12288,"kv_bytes_naive":65536,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":16384,"tab_bytes":68,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":1,"gqa_ratio":64,"head_dim":64,"context":2048,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00559141,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":0.4128,"call_ms_p90":0.4140,"kernel_ms_median":0.4083,"kernel_ms_p90":0.4094,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":77824,"kv_bytes_naive":524288,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":16384,"tab_bytes":516,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":2,"gqa_ratio":32,"head_dim":64,"context":256,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00286709,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":0.1069,"call_ms_p90":0.1073,"kernel_ms_median":0.1032,"kernel_ms_p90":0.1036,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":24576,"kv_bytes_naive":131072,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":16384,"tab_bytes":68,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":2,"gqa_ratio":32,"head_dim":64,"context":2048,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00639622,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":1.0238,"call_ms_p90":1.1008,"kernel_ms_median":1.0178,"kernel_ms_p90":1.0930,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":155648,"kv_bytes_naive":1048576,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":16384,"tab_bytes":516,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":4,"gqa_ratio":16,"head_dim":64,"context":256,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00399345,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":0.1128,"call_ms_p90":0.1175,"kernel_ms_median":0.1048,"kernel_ms_p90":0.1090,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":49152,"kv_bytes_naive":262144,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":16384,"tab_bytes":68,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":4,"gqa_ratio":16,"head_dim":64,"context":2048,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00618912,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":1.0228,"call_ms_p90":1.1182,"kernel_ms_median":1.0045,"kernel_ms_p90":1.1051,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":311296,"kv_bytes_naive":2097152,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":16384,"tab_bytes":516,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":8,"gqa_ratio":8,"head_dim":64,"context":256,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.0028507,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":0.1073,"call_ms_p90":0.1150,"kernel_ms_median":0.1028,"kernel_ms_p90":0.1088,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":98304,"kv_bytes_naive":524288,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":16384,"tab_bytes":68,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":8,"gqa_ratio":8,"head_dim":64,"context":2048,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00529008,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":1.0390,"call_ms_p90":1.1225,"kernel_ms_median":1.0170,"kernel_ms_p90":1.1062,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":622592,"kv_bytes_naive":4194304,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":16384,"tab_bytes":516,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":16,"gqa_ratio":4,"head_dim":64,"context":256,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00272721,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":0.1079,"call_ms_p90":0.1139,"kernel_ms_median":0.1027,"kernel_ms_p90":0.1056,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":196608,"kv_bytes_naive":1048576,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":16384,"tab_bytes":68,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":16,"gqa_ratio":4,"head_dim":64,"context":2048,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00565958,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":1.0448,"call_ms_p90":1.0619,"kernel_ms_median":1.0153,"kernel_ms_p90":1.0265,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":1245184,"kv_bytes_naive":8388608,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":16384,"tab_bytes":516,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":32,"gqa_ratio":2,"head_dim":64,"context":256,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00282842,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":0.1239,"call_ms_p90":0.1337,"kernel_ms_median":0.1073,"kernel_ms_p90":0.1163,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":393216,"kv_bytes_naive":2097152,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":16384,"tab_bytes":68,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":32,"gqa_ratio":2,"head_dim":64,"context":2048,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00670759,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":0.9888,"call_ms_p90":0.9917,"kernel_ms_median":0.9294,"kernel_ms_p90":0.9308,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":2490368,"kv_bytes_naive":16777216,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":16384,"tab_bytes":516,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":64,"gqa_ratio":1,"head_dim":64,"context":256,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00284045,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":0.1348,"call_ms_p90":0.1359,"kernel_ms_median":0.1086,"kernel_ms_p90":0.1101,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":786432,"kv_bytes_naive":4194304,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":16384,"tab_bytes":68,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":64,"gqa_ratio":1,"head_dim":64,"context":2048,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00870193,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":1.1009,"call_ms_p90":1.1038,"kernel_ms_median":1.0195,"kernel_ms_p90":1.0217,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":4980736,"kv_bytes_naive":33554432,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":16384,"tab_bytes":516,"out_bytes":16384,"kv_source":"staged"}],"points_total":72,"points_supported":64,"points_unsupported":8,"device_opens":1,"checks":73,"failed":0,"verdict":"OMEGA_GPU_ATTENTION_SWEEP_PASS"} diff --git a/evidence/gpu-reconv-308/T2-attention/div-sweep.log b/evidence/gpu-reconv-308/T2-attention/div-sweep.log new file mode 100644 index 000000000..89ab739fd --- /dev/null +++ b/evidence/gpu-reconv-308/T2-attention/div-sweep.log @@ -0,0 +1,74 @@ +PASS sweep 1q/ 1kv ratio 1 ctx 256: launches=1 ctas=1 call_ms med=0.103 p90=0.104 kernel_ms med=0.103 p90=0.103 kv_staged=12288 (naive 65536) blocks=3/16 q=256 tab=68 out=256 worst=0.00162825 +PASS sweep 1q/ 1kv ratio 1 ctx 2048: launches=1 ctas=1 call_ms med=0.408 p90=0.410 kernel_ms med=0.407 p90=0.408 kv_staged=77824 (naive 524288) blocks=19/128 q=256 tab=516 out=256 worst=0.00286577 +PASS sweep 2q/ 1kv ratio 2 ctx 256: launches=1 ctas=2 call_ms med=0.104 p90=0.104 kernel_ms med=0.103 p90=0.103 kv_staged=12288 (naive 65536) blocks=3/16 q=512 tab=68 out=512 worst=0.00274735 +PASS sweep 2q/ 1kv ratio 2 ctx 2048: launches=1 ctas=2 call_ms med=0.410 p90=0.411 kernel_ms med=0.409 p90=0.410 kv_staged=77824 (naive 524288) blocks=19/128 q=512 tab=516 out=512 worst=0.00293219 +PASS sweep 2q/ 2kv ratio 1 ctx 256: launches=1 ctas=2 call_ms med=0.104 p90=0.104 kernel_ms med=0.103 p90=0.103 kv_staged=24576 (naive 131072) blocks=3/16 q=512 tab=68 out=512 worst=0.00285429 +PASS sweep 2q/ 2kv ratio 1 ctx 2048: launches=1 ctas=2 call_ms med=0.409 p90=0.410 kernel_ms med=0.407 p90=0.407 kv_staged=155648 (naive 1048576) blocks=19/128 q=512 tab=516 out=512 worst=0.00344759 +PASS sweep 4q/ 1kv ratio 4 ctx 256: launches=1 ctas=4 call_ms med=0.103 p90=0.104 kernel_ms med=0.103 p90=0.103 kv_staged=12288 (naive 65536) blocks=3/16 q=1024 tab=68 out=1024 worst=0.00195521 +PASS sweep 4q/ 1kv ratio 4 ctx 2048: launches=1 ctas=4 call_ms med=0.410 p90=0.411 kernel_ms med=0.408 p90=0.409 kv_staged=77824 (naive 524288) blocks=19/128 q=1024 tab=516 out=1024 worst=0.00455218 +PASS sweep 4q/ 2kv ratio 2 ctx 256: launches=1 ctas=4 call_ms med=0.104 p90=0.105 kernel_ms med=0.103 p90=0.104 kv_staged=24576 (naive 131072) blocks=3/16 q=1024 tab=68 out=1024 worst=0.00195668 +PASS sweep 4q/ 2kv ratio 2 ctx 2048: launches=1 ctas=4 call_ms med=0.410 p90=0.412 kernel_ms med=0.408 p90=0.410 kv_staged=155648 (naive 1048576) blocks=19/128 q=1024 tab=516 out=1024 worst=0.00429287 +PASS sweep 4q/ 4kv ratio 1 ctx 256: launches=1 ctas=4 call_ms med=0.104 p90=0.104 kernel_ms med=0.103 p90=0.103 kv_staged=49152 (naive 262144) blocks=3/16 q=1024 tab=68 out=1024 worst=0.00208557 +PASS sweep 4q/ 4kv ratio 1 ctx 2048: launches=1 ctas=4 call_ms med=0.412 p90=0.414 kernel_ms med=0.408 p90=0.410 kv_staged=311296 (naive 2097152) blocks=19/128 q=1024 tab=516 out=1024 worst=0.00447204 +PASS sweep 8q/ 1kv ratio 8 ctx 256: launches=1 ctas=8 call_ms med=0.104 p90=0.104 kernel_ms med=0.103 p90=0.103 kv_staged=12288 (naive 65536) blocks=3/16 q=2048 tab=68 out=2048 worst=0.00244755 +PASS sweep 8q/ 1kv ratio 8 ctx 2048: launches=1 ctas=8 call_ms med=0.409 p90=0.410 kernel_ms med=0.408 p90=0.409 kv_staged=77824 (naive 524288) blocks=19/128 q=2048 tab=516 out=2048 worst=0.00561397 +PASS sweep 8q/ 2kv ratio 4 ctx 256: launches=1 ctas=8 call_ms med=0.104 p90=0.104 kernel_ms med=0.103 p90=0.103 kv_staged=24576 (naive 131072) blocks=3/16 q=2048 tab=68 out=2048 worst=0.00277137 +PASS sweep 8q/ 2kv ratio 4 ctx 2048: launches=1 ctas=8 call_ms med=0.411 p90=0.412 kernel_ms med=0.408 p90=0.410 kv_staged=155648 (naive 1048576) blocks=19/128 q=2048 tab=516 out=2048 worst=0.00478059 +PASS sweep 8q/ 4kv ratio 2 ctx 256: launches=1 ctas=8 call_ms med=0.104 p90=0.104 kernel_ms med=0.103 p90=0.103 kv_staged=49152 (naive 262144) blocks=3/16 q=2048 tab=68 out=2048 worst=0.00220051 +PASS sweep 8q/ 4kv ratio 2 ctx 2048: launches=1 ctas=8 call_ms med=0.412 p90=0.413 kernel_ms med=0.408 p90=0.409 kv_staged=311296 (naive 2097152) blocks=19/128 q=2048 tab=516 out=2048 worst=0.0051977 +PASS sweep 8q/ 8kv ratio 1 ctx 256: launches=1 ctas=8 call_ms med=0.105 p90=0.105 kernel_ms med=0.103 p90=0.103 kv_staged=98304 (naive 524288) blocks=3/16 q=2048 tab=68 out=2048 worst=0.00240979 +PASS sweep 8q/ 8kv ratio 1 ctx 2048: launches=1 ctas=8 call_ms med=0.415 p90=0.416 kernel_ms med=0.408 p90=0.409 kv_staged=622592 (naive 4194304) blocks=19/128 q=2048 tab=516 out=2048 worst=0.00466806 +PASS sweep 16q/ 1kv ratio 16 ctx 256: launches=1 ctas=16 call_ms med=0.104 p90=0.104 kernel_ms med=0.103 p90=0.103 kv_staged=12288 (naive 65536) blocks=3/16 q=4096 tab=68 out=4096 worst=0.00249826 +PASS sweep 16q/ 1kv ratio 16 ctx 2048: launches=1 ctas=16 call_ms med=0.410 p90=0.412 kernel_ms med=0.408 p90=0.410 kv_staged=77824 (naive 524288) blocks=19/128 q=4096 tab=516 out=4096 worst=0.00536905 +PASS sweep 16q/ 2kv ratio 8 ctx 256: launches=1 ctas=16 call_ms med=0.104 p90=0.105 kernel_ms med=0.103 p90=0.103 kv_staged=24576 (naive 131072) blocks=3/16 q=4096 tab=68 out=4096 worst=0.00239844 +PASS sweep 16q/ 2kv ratio 8 ctx 2048: launches=1 ctas=16 call_ms med=0.411 p90=0.413 kernel_ms med=0.408 p90=0.410 kv_staged=155648 (naive 1048576) blocks=19/128 q=4096 tab=516 out=4096 worst=0.00570392 +PASS sweep 16q/ 4kv ratio 4 ctx 256: launches=1 ctas=16 call_ms med=0.105 p90=0.106 kernel_ms med=0.103 p90=0.104 kv_staged=49152 (naive 262144) blocks=3/16 q=4096 tab=68 out=4096 worst=0.00278498 +PASS sweep 16q/ 4kv ratio 4 ctx 2048: launches=1 ctas=16 call_ms med=0.413 p90=0.414 kernel_ms med=0.408 p90=0.409 kv_staged=311296 (naive 2097152) blocks=19/128 q=4096 tab=516 out=4096 worst=0.00518169 +PASS sweep 16q/ 8kv ratio 2 ctx 256: launches=1 ctas=16 call_ms med=0.105 p90=0.106 kernel_ms med=0.103 p90=0.103 kv_staged=98304 (naive 524288) blocks=3/16 q=4096 tab=68 out=4096 worst=0.00262688 +PASS sweep 16q/ 8kv ratio 2 ctx 2048: launches=1 ctas=16 call_ms med=0.416 p90=0.417 kernel_ms med=0.408 p90=0.409 kv_staged=622592 (naive 4194304) blocks=19/128 q=4096 tab=516 out=4096 worst=0.0059316 +PASS sweep 16q/16kv ratio 1 ctx 256: launches=1 ctas=16 call_ms med=0.106 p90=0.107 kernel_ms med=0.103 p90=0.104 kv_staged=196608 (naive 1048576) blocks=3/16 q=4096 tab=68 out=4096 worst=0.00239914 +PASS sweep 16q/16kv ratio 1 ctx 2048: launches=1 ctas=16 call_ms med=0.424 p90=0.426 kernel_ms med=0.408 p90=0.411 kv_staged=1245184 (naive 8388608) blocks=19/128 q=4096 tab=516 out=4096 worst=0.00553838 +PASS sweep 24q/ 1kv ratio 24 ctx 256: unsupported_by_kernel_envelope (rc=BAD_ARGS) +PASS sweep 24q/ 1kv ratio 24 ctx 2048: unsupported_by_kernel_envelope (rc=BAD_ARGS) +PASS sweep 24q/ 2kv ratio 12 ctx 256: unsupported_by_kernel_envelope (rc=BAD_ARGS) +PASS sweep 24q/ 2kv ratio 12 ctx 2048: unsupported_by_kernel_envelope (rc=BAD_ARGS) +PASS sweep 24q/ 3kv ratio 8 ctx 256: launches=1 ctas=24 call_ms med=0.105 p90=0.105 kernel_ms med=0.103 p90=0.103 kv_staged=36864 (naive 196608) blocks=3/16 q=6144 tab=68 out=6144 worst=0.00306755 +PASS sweep 24q/ 3kv ratio 8 ctx 2048: launches=1 ctas=24 call_ms med=0.616 p90=0.618 kernel_ms med=0.612 p90=0.614 kv_staged=233472 (naive 1572864) blocks=19/128 q=6144 tab=516 out=6144 worst=0.00539717 +PASS sweep 24q/ 4kv ratio 6 ctx 256: unsupported_by_kernel_envelope (rc=BAD_ARGS) +PASS sweep 24q/ 4kv ratio 6 ctx 2048: unsupported_by_kernel_envelope (rc=BAD_ARGS) +PASS sweep 24q/ 6kv ratio 4 ctx 256: launches=1 ctas=24 call_ms med=0.105 p90=0.106 kernel_ms med=0.103 p90=0.104 kv_staged=73728 (naive 393216) blocks=3/16 q=6144 tab=68 out=6144 worst=0.00326846 +PASS sweep 24q/ 6kv ratio 4 ctx 2048: launches=1 ctas=24 call_ms med=0.618 p90=0.647 kernel_ms med=0.612 p90=0.639 kv_staged=466944 (naive 3145728) blocks=19/128 q=6144 tab=516 out=6144 worst=0.00527248 +PASS sweep 24q/ 8kv ratio 3 ctx 256: unsupported_by_kernel_envelope (rc=BAD_ARGS) +PASS sweep 24q/ 8kv ratio 3 ctx 2048: unsupported_by_kernel_envelope (rc=BAD_ARGS) +PASS sweep 24q/12kv ratio 2 ctx 256: launches=1 ctas=24 call_ms med=0.106 p90=0.106 kernel_ms med=0.103 p90=0.103 kv_staged=147456 (naive 786432) blocks=3/16 q=6144 tab=68 out=6144 worst=0.00338069 +PASS sweep 24q/12kv ratio 2 ctx 2048: launches=1 ctas=24 call_ms med=0.624 p90=0.625 kernel_ms med=0.612 p90=0.613 kv_staged=933888 (naive 6291456) blocks=19/128 q=6144 tab=516 out=6144 worst=0.00538782 +PASS sweep 24q/24kv ratio 1 ctx 256: launches=1 ctas=24 call_ms med=0.107 p90=0.108 kernel_ms med=0.103 p90=0.104 kv_staged=294912 (naive 1572864) blocks=3/16 q=6144 tab=68 out=6144 worst=0.00259323 +PASS sweep 24q/24kv ratio 1 ctx 2048: launches=1 ctas=24 call_ms med=0.533 p90=0.536 kernel_ms med=0.509 p90=0.511 kv_staged=1867776 (naive 12582912) blocks=19/128 q=6144 tab=516 out=6144 worst=0.00737519 +PASS sweep 32q/ 1kv ratio 32 ctx 256: launches=1 ctas=32 call_ms med=0.105 p90=0.105 kernel_ms med=0.103 p90=0.103 kv_staged=12288 (naive 65536) blocks=3/16 q=8192 tab=68 out=8192 worst=0.00241453 +PASS sweep 32q/ 1kv ratio 32 ctx 2048: launches=1 ctas=32 call_ms med=0.411 p90=0.412 kernel_ms med=0.408 p90=0.409 kv_staged=77824 (naive 524288) blocks=19/128 q=8192 tab=516 out=8192 worst=0.00525453 +PASS sweep 32q/ 2kv ratio 16 ctx 256: launches=1 ctas=32 call_ms med=0.105 p90=0.105 kernel_ms med=0.103 p90=0.103 kv_staged=24576 (naive 131072) blocks=3/16 q=8192 tab=68 out=8192 worst=0.0031327 +PASS sweep 32q/ 2kv ratio 16 ctx 2048: launches=1 ctas=32 call_ms med=0.412 p90=0.412 kernel_ms med=0.408 p90=0.409 kv_staged=155648 (naive 1048576) blocks=19/128 q=8192 tab=516 out=8192 worst=0.00648224 +PASS sweep 32q/ 4kv ratio 8 ctx 256: launches=1 ctas=32 call_ms med=0.105 p90=0.106 kernel_ms med=0.103 p90=0.103 kv_staged=49152 (naive 262144) blocks=3/16 q=8192 tab=68 out=8192 worst=0.00316999 +PASS sweep 32q/ 4kv ratio 8 ctx 2048: launches=1 ctas=32 call_ms med=0.413 p90=0.414 kernel_ms med=0.408 p90=0.409 kv_staged=311296 (naive 2097152) blocks=19/128 q=8192 tab=516 out=8192 worst=0.00599161 +PASS sweep 32q/ 8kv ratio 4 ctx 256: launches=1 ctas=32 call_ms med=0.106 p90=0.106 kernel_ms med=0.103 p90=0.103 kv_staged=98304 (naive 524288) blocks=3/16 q=8192 tab=68 out=8192 worst=0.00278167 +PASS sweep 32q/ 8kv ratio 4 ctx 2048: launches=1 ctas=32 call_ms med=0.417 p90=0.418 kernel_ms med=0.408 p90=0.409 kv_staged=622592 (naive 4194304) blocks=19/128 q=8192 tab=516 out=8192 worst=0.005897 +PASS sweep 32q/16kv ratio 2 ctx 256: launches=1 ctas=32 call_ms med=0.107 p90=0.115 kernel_ms med=0.103 p90=0.106 kv_staged=196608 (naive 1048576) blocks=3/16 q=8192 tab=68 out=8192 worst=0.00268895 +PASS sweep 32q/16kv ratio 2 ctx 2048: launches=1 ctas=32 call_ms med=0.426 p90=0.429 kernel_ms med=0.409 p90=0.410 kv_staged=1245184 (naive 8388608) blocks=19/128 q=8192 tab=516 out=8192 worst=0.00571167 +PASS sweep 32q/32kv ratio 1 ctx 256: launches=1 ctas=32 call_ms med=0.108 p90=0.110 kernel_ms med=0.103 p90=0.103 kv_staged=393216 (naive 2097152) blocks=3/16 q=8192 tab=68 out=8192 worst=0.00256581 +PASS sweep 32q/32kv ratio 1 ctx 2048: launches=1 ctas=32 call_ms med=0.444 p90=0.446 kernel_ms med=0.409 p90=0.410 kv_staged=2490368 (naive 16777216) blocks=19/128 q=8192 tab=516 out=8192 worst=0.00647965 +PASS sweep 64q/ 1kv ratio 64 ctx 256: launches=1 ctas=64 call_ms med=0.106 p90=0.107 kernel_ms med=0.103 p90=0.103 kv_staged=12288 (naive 65536) blocks=3/16 q=16384 tab=68 out=16384 worst=0.00332208 +PASS sweep 64q/ 1kv ratio 64 ctx 2048: launches=1 ctas=64 call_ms med=0.413 p90=0.414 kernel_ms med=0.408 p90=0.409 kv_staged=77824 (naive 524288) blocks=19/128 q=16384 tab=516 out=16384 worst=0.00559141 +PASS sweep 64q/ 2kv ratio 32 ctx 256: launches=1 ctas=64 call_ms med=0.107 p90=0.107 kernel_ms med=0.103 p90=0.104 kv_staged=24576 (naive 131072) blocks=3/16 q=16384 tab=68 out=16384 worst=0.00286709 +PASS sweep 64q/ 2kv ratio 32 ctx 2048: launches=1 ctas=64 call_ms med=1.024 p90=1.101 kernel_ms med=1.018 p90=1.093 kv_staged=155648 (naive 1048576) blocks=19/128 q=16384 tab=516 out=16384 worst=0.00639622 +PASS sweep 64q/ 4kv ratio 16 ctx 256: launches=1 ctas=64 call_ms med=0.113 p90=0.118 kernel_ms med=0.105 p90=0.109 kv_staged=49152 (naive 262144) blocks=3/16 q=16384 tab=68 out=16384 worst=0.00399345 +PASS sweep 64q/ 4kv ratio 16 ctx 2048: launches=1 ctas=64 call_ms med=1.023 p90=1.118 kernel_ms med=1.004 p90=1.105 kv_staged=311296 (naive 2097152) blocks=19/128 q=16384 tab=516 out=16384 worst=0.00618912 +PASS sweep 64q/ 8kv ratio 8 ctx 256: launches=1 ctas=64 call_ms med=0.107 p90=0.115 kernel_ms med=0.103 p90=0.109 kv_staged=98304 (naive 524288) blocks=3/16 q=16384 tab=68 out=16384 worst=0.0028507 +PASS sweep 64q/ 8kv ratio 8 ctx 2048: launches=1 ctas=64 call_ms med=1.039 p90=1.122 kernel_ms med=1.017 p90=1.106 kv_staged=622592 (naive 4194304) blocks=19/128 q=16384 tab=516 out=16384 worst=0.00529008 +PASS sweep 64q/16kv ratio 4 ctx 256: launches=1 ctas=64 call_ms med=0.108 p90=0.114 kernel_ms med=0.103 p90=0.106 kv_staged=196608 (naive 1048576) blocks=3/16 q=16384 tab=68 out=16384 worst=0.00272721 +PASS sweep 64q/16kv ratio 4 ctx 2048: launches=1 ctas=64 call_ms med=1.045 p90=1.062 kernel_ms med=1.015 p90=1.026 kv_staged=1245184 (naive 8388608) blocks=19/128 q=16384 tab=516 out=16384 worst=0.00565958 +PASS sweep 64q/32kv ratio 2 ctx 256: launches=1 ctas=64 call_ms med=0.124 p90=0.134 kernel_ms med=0.107 p90=0.116 kv_staged=393216 (naive 2097152) blocks=3/16 q=16384 tab=68 out=16384 worst=0.00282842 +PASS sweep 64q/32kv ratio 2 ctx 2048: launches=1 ctas=64 call_ms med=0.989 p90=0.992 kernel_ms med=0.929 p90=0.931 kv_staged=2490368 (naive 16777216) blocks=19/128 q=16384 tab=516 out=16384 worst=0.00670759 +PASS sweep 64q/64kv ratio 1 ctx 256: launches=1 ctas=64 call_ms med=0.135 p90=0.136 kernel_ms med=0.109 p90=0.110 kv_staged=786432 (naive 4194304) blocks=3/16 q=16384 tab=68 out=16384 worst=0.00284045 +PASS sweep 64q/64kv ratio 1 ctx 2048: launches=1 ctas=64 call_ms med=1.101 p90=1.104 kernel_ms med=1.019 p90=1.022 kv_staged=4980736 (naive 33554432) blocks=19/128 q=16384 tab=516 out=16384 worst=0.00870193 +sweep: 72 points (64 supported, 8 unsupported by the kernel envelope), device_opens=1 +PASS: 73 checks, 0 failed diff --git a/evidence/gpu-reconv-308/T2-attention/prod-receipt.json b/evidence/gpu-reconv-308/T2-attention/prod-receipt.json new file mode 100644 index 000000000..04ca8c0c2 --- /dev/null +++ b/evidence/gpu-reconv-308/T2-attention/prod-receipt.json @@ -0,0 +1 @@ +{"schema":"OMEGA_GPU_ATTENTION_V1","tolerances":{"rel":2e-4,"abs":2e-5,"large_scores_rel":1e-3,"large_scores_abs":1e-4},"cases":[{"case":"gqa_f32_tinyllama_32q4kv_ctx1","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":113297,"call_ns":29448818,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":2048,"kv_bytes_naive":2048,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_tinyllama_32q4kv_ctx17","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00189431,"pass":true,"chip_elapsed_ns":104545,"call_ns":227202,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":34816,"kv_bytes_naive":34816,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_tinyllama_32q4kv_ctx256","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00189409,"pass":true,"chip_elapsed_ns":208018,"call_ns":360595,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":524288,"kv_bytes_naive":524288,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_tinyllama_32q4kv_ctx2048","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.0028582,"pass":true,"chip_elapsed_ns":1123066,"call_ns":1654590,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":4194304,"kv_bytes_naive":4194304,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":4,"mutants_caught":4,"mutant_note":"SLOT:CAUGHT(rc=OK,bad=2041) NO_RESCALE:CAUGHT(rc=OK,bad=1959) Q_ROW:CAUGHT(rc=OK,bad=2048) OUT_ROW:CAUGHT(rc=OK,bad=2048)"},{"case":"gqa_f32_tinyllama_32q4kv_ctx65","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00174821,"pass":true,"chip_elapsed_ns":102849,"call_ns":185714,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":133120,"kv_bytes_naive":133120,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_tinyllama_32q4kv_ctx128","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00197914,"pass":true,"chip_elapsed_ns":102865,"call_ns":107217,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":262144,"kv_bytes_naive":262144,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_tinyllama_32q4kv_ctx129","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00177573,"pass":true,"chip_elapsed_ns":102896,"call_ns":107088,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":264192,"kv_bytes_naive":264192,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_tinyllama_32q4kv_ctx300","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00259576,"pass":true,"chip_elapsed_ns":204434,"call_ns":212434,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":614400,"kv_bytes_naive":614400,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_tinyllama_32q4kv_ctx1000","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00247545,"pass":true,"chip_elapsed_ns":510565,"call_ns":542597,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":2048000,"kv_bytes_naive":2048000,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_llama32_1b_32q8kv_ctx1","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":103105,"call_ns":195698,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":4096,"kv_bytes_naive":4096,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_llama32_1b_32q8kv_ctx17","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00174276,"pass":true,"chip_elapsed_ns":103153,"call_ns":105745,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":69632,"kv_bytes_naive":69632,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":1,"mutants_caught":1,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=2048)"},{"case":"gqa_f32_llama32_1b_32q8kv_ctx256","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00161378,"pass":true,"chip_elapsed_ns":207041,"call_ns":299490,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":1048576,"kv_bytes_naive":1048576,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_llama32_1b_32q8kv_ctx2048","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.0029454,"pass":true,"chip_elapsed_ns":1122857,"call_ns":2112386,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":8388608,"kv_bytes_naive":8388608,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":4,"mutants_caught":4,"mutant_note":"SLOT:CAUGHT(rc=OK,bad=2045) NO_RESCALE:CAUGHT(rc=OK,bad=1904) Q_ROW:CAUGHT(rc=OK,bad=2042) OUT_ROW:CAUGHT(rc=OK,bad=2042)"},{"case":"gqa_f32_llama32_1b_32q8kv_ctx65","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.0014332,"pass":true,"chip_elapsed_ns":103169,"call_ns":186146,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":266240,"kv_bytes_naive":266240,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_llama32_1b_32q8kv_ctx128","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00186415,"pass":true,"chip_elapsed_ns":103201,"call_ns":110337,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":524288,"kv_bytes_naive":524288,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_llama32_1b_32q8kv_ctx129","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00185519,"pass":true,"chip_elapsed_ns":102753,"call_ns":109985,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":528384,"kv_bytes_naive":528384,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_llama32_1b_32q8kv_ctx300","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00162705,"pass":true,"chip_elapsed_ns":204546,"call_ns":220482,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":1228800,"kv_bytes_naive":1228800,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_llama32_1b_32q8kv_ctx1000","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00330327,"pass":true,"chip_elapsed_ns":612806,"call_ns":696054,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":4096000,"kv_bytes_naive":4096000,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_large_scores_32q4kv_ctx256","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.105395,"pass":true,"chip_elapsed_ns":204705,"call_ns":309938,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":524288,"kv_bytes_naive":524288,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":1,"mutants_caught":1,"mutant_note":"NO_MAX:CAUGHT(rc=OK,bad=2048)"},{"case":"gqa_f32_mha_16q16kv_ctx300","n":1024,"rc":"OK","violations":0,"worst_scaled_err":0.00197746,"pass":true,"chip_elapsed_ns":206626,"call_ns":336611,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":2457600,"kv_bytes_naive":2457600,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":3,"mutants_caught":3,"mutant_note":"Q_ROW:CAUGHT(rc=OK,bad=1022) OUT_ROW:CAUGHT(rc=OK,bad=1024) KV_HEAD:CAUGHT(rc=OK,bad=1024)"},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx1","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":103185,"call_ns":182818,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":16384,"kv_bytes_naive":16384,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":1,"mutants_caught":1,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=2048)"},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx17","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.002096,"pass":true,"chip_elapsed_ns":103281,"call_ns":178513,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":32768,"kv_bytes_naive":32768,"kv_blocks_unique":2,"kv_blocks_logical":2,"mutants_run":2,"mutants_caught":2,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=2046) SLOT:CAUGHT(rc=OK,bad=2048)"},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx256","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00199949,"pass":true,"chip_elapsed_ns":204850,"call_ns":284770,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":262144,"kv_bytes_naive":262144,"kv_blocks_unique":16,"kv_blocks_logical":16,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx2048","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00336531,"pass":true,"chip_elapsed_ns":920008,"call_ns":964473,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":2097152,"kv_bytes_naive":2097152,"kv_blocks_unique":128,"kv_blocks_logical":128,"mutants_run":5,"mutants_caught":5,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=2045) SLOT:CAUGHT(rc=OK,bad=2042) NO_RESCALE:CAUGHT(rc=OK,bad=2023) Q_ROW:CAUGHT(rc=OK,bad=2042) OUT_ROW:CAUGHT(rc=OK,bad=2042)"},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx65","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00182741,"pass":true,"chip_elapsed_ns":103089,"call_ns":184946,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":81920,"kv_bytes_naive":81920,"kv_blocks_unique":5,"kv_blocks_logical":5,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx128","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00191997,"pass":true,"chip_elapsed_ns":102625,"call_ns":106177,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":131072,"kv_bytes_naive":131072,"kv_blocks_unique":8,"kv_blocks_logical":8,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx129","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00192425,"pass":true,"chip_elapsed_ns":102897,"call_ns":106465,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":147456,"kv_bytes_naive":147456,"kv_blocks_unique":9,"kv_blocks_logical":9,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx300","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00243899,"pass":true,"chip_elapsed_ns":203986,"call_ns":209490,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":311296,"kv_bytes_naive":311296,"kv_blocks_unique":19,"kv_blocks_logical":19,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx1000","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00547531,"pass":true,"chip_elapsed_ns":204290,"call_ns":208018,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":163840,"kv_bytes_naive":1032192,"kv_blocks_unique":10,"kv_blocks_logical":63,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx1","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":103665,"call_ns":183217,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":32768,"kv_bytes_naive":32768,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":1,"mutants_caught":1,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=2048)"},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx17","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00223287,"pass":true,"chip_elapsed_ns":103281,"call_ns":179442,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":65536,"kv_bytes_naive":65536,"kv_blocks_unique":2,"kv_blocks_logical":2,"mutants_run":2,"mutants_caught":2,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=2048) SLOT:CAUGHT(rc=OK,bad=2048)"},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx256","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00167964,"pass":true,"chip_elapsed_ns":206514,"call_ns":292387,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":524288,"kv_bytes_naive":524288,"kv_blocks_unique":16,"kv_blocks_logical":16,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx2048","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00358689,"pass":true,"chip_elapsed_ns":1022569,"call_ns":1116922,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":4194304,"kv_bytes_naive":4194304,"kv_blocks_unique":128,"kv_blocks_logical":128,"mutants_run":5,"mutants_caught":5,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=2048) SLOT:CAUGHT(rc=OK,bad=2045) NO_RESCALE:CAUGHT(rc=OK,bad=1999) Q_ROW:CAUGHT(rc=OK,bad=2044) OUT_ROW:CAUGHT(rc=OK,bad=2044)"},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx65","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00228809,"pass":true,"chip_elapsed_ns":103169,"call_ns":187393,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":163840,"kv_bytes_naive":163840,"kv_blocks_unique":5,"kv_blocks_logical":5,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx128","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00160376,"pass":true,"chip_elapsed_ns":102977,"call_ns":107841,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":262144,"kv_bytes_naive":262144,"kv_blocks_unique":8,"kv_blocks_logical":8,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx129","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00173996,"pass":true,"chip_elapsed_ns":103249,"call_ns":108161,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":294912,"kv_bytes_naive":294912,"kv_blocks_unique":9,"kv_blocks_logical":9,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx300","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00303696,"pass":true,"chip_elapsed_ns":204850,"call_ns":213442,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":622592,"kv_bytes_naive":622592,"kv_blocks_unique":19,"kv_blocks_logical":19,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx1000","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00420311,"pass":true,"chip_elapsed_ns":205506,"call_ns":211618,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":327680,"kv_bytes_naive":2064384,"kv_blocks_unique":10,"kv_blocks_logical":63,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_batch_3seq_32q8kv_ctx40_0_100","n":6144,"rc":"OK","violations":0,"worst_scaled_err":0.00181424,"pass":true,"chip_elapsed_ns":205808,"call_ns":432691,"chip_calls":2,"unwritten_words":0,"kv_bytes_staged":229376,"kv_bytes_naive":229376,"kv_blocks_unique":7,"kv_blocks_logical":7,"mutants_run":4,"mutants_caught":4,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=4095) SLOT:CAUGHT(rc=OK,bad=4090) Q_ROW:CAUGHT(rc=OK,bad=4096) OUT_ROW:CAUGHT(rc=OK,bad=4096)"},{"case":"paged_bf16_shared_prefix_4seq_8q2kv_10pre","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00188498,"pass":true,"chip_elapsed_ns":205474,"call_ns":288131,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":155648,"kv_bytes_naive":401408,"kv_blocks_unique":19,"kv_blocks_logical":49,"mutants_run":2,"mutants_caught":2,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=2048) SLOT:CAUGHT(rc=OK,bad=2046)"},{"case":"paged_bf16_shared_prefix_4seq_32q8kv_10pre","n":8192,"rc":"OK","violations":0,"worst_scaled_err":0.0024233,"pass":true,"chip_elapsed_ns":411812,"call_ns":517076,"chip_calls":2,"unwritten_words":0,"kv_bytes_staged":950272,"kv_bytes_naive":1605632,"kv_blocks_unique":29,"kv_blocks_logical":49,"mutants_run":2,"mutants_caught":2,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=8192) SLOT:CAUGHT(rc=OK,bad=8181)"}],"mutant_count":6,"mutants":{"NO_MAX":{"run":1,"caught":1},"KV_HEAD":{"run":11,"caught":11},"SLOT":{"run":9,"caught":9},"NO_RESCALE":{"run":4,"caught":4},"Q_ROW":{"run":6,"caught":6},"OUT_ROW":{"run":6,"caught":6}},"device_opens":1,"checks":122,"failed":0,"verdict":"OMEGA_GPU_ATTENTION_PASS"} diff --git a/evidence/gpu-reconv-308/T2-attention/prod-run.log b/evidence/gpu-reconv-308/T2-attention/prod-run.log new file mode 100644 index 000000000..c64e83671 --- /dev/null +++ b/evidence/gpu-reconv-308/T2-attention/prod-run.log @@ -0,0 +1,49 @@ +PASS gqa_f32_tinyllama_32q4kv_ctx1 n=2048 rc=OK violations=0 worst_scaled_err=0 chip_ns=113297 call_ns=29448818 calls=1 unwritten=0 kv_staged=2048/2048 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx17 n=2048 rc=OK violations=0 worst_scaled_err=0.00189431 chip_ns=104545 call_ns=227202 calls=1 unwritten=0 kv_staged=34816/34816 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx256 n=2048 rc=OK violations=0 worst_scaled_err=0.00189409 chip_ns=208018 call_ns=360595 calls=1 unwritten=0 kv_staged=524288/524288 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx2048 n=2048 rc=OK violations=0 worst_scaled_err=0.0028582 chip_ns=1123066 call_ns=1654590 calls=1 unwritten=0 kv_staged=4194304/4194304 blocks=1/1 | mutants 4/4 SLOT:CAUGHT(rc=OK,bad=2041) NO_RESCALE:CAUGHT(rc=OK,bad=1959) Q_ROW:CAUGHT(rc=OK,bad=2048) OUT_ROW:CAUGHT(rc=OK,bad=2048) +PASS gqa_f32_tinyllama_32q4kv_ctx65 n=2048 rc=OK violations=0 worst_scaled_err=0.00174821 chip_ns=102849 call_ns=185714 calls=1 unwritten=0 kv_staged=133120/133120 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx128 n=2048 rc=OK violations=0 worst_scaled_err=0.00197914 chip_ns=102865 call_ns=107217 calls=1 unwritten=0 kv_staged=262144/262144 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx129 n=2048 rc=OK violations=0 worst_scaled_err=0.00177573 chip_ns=102896 call_ns=107088 calls=1 unwritten=0 kv_staged=264192/264192 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx300 n=2048 rc=OK violations=0 worst_scaled_err=0.00259576 chip_ns=204434 call_ns=212434 calls=1 unwritten=0 kv_staged=614400/614400 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx1000 n=2048 rc=OK violations=0 worst_scaled_err=0.00247545 chip_ns=510565 call_ns=542597 calls=1 unwritten=0 kv_staged=2048000/2048000 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx1 n=2048 rc=OK violations=0 worst_scaled_err=0 chip_ns=103105 call_ns=195698 calls=1 unwritten=0 kv_staged=4096/4096 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx17 n=2048 rc=OK violations=0 worst_scaled_err=0.00174276 chip_ns=103153 call_ns=105745 calls=1 unwritten=0 kv_staged=69632/69632 blocks=1/1 | mutants 1/1 KV_HEAD:CAUGHT(rc=OK,bad=2048) +PASS gqa_f32_llama32_1b_32q8kv_ctx256 n=2048 rc=OK violations=0 worst_scaled_err=0.00161378 chip_ns=207041 call_ns=299490 calls=1 unwritten=0 kv_staged=1048576/1048576 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx2048 n=2048 rc=OK violations=0 worst_scaled_err=0.0029454 chip_ns=1122857 call_ns=2112386 calls=1 unwritten=0 kv_staged=8388608/8388608 blocks=1/1 | mutants 4/4 SLOT:CAUGHT(rc=OK,bad=2045) NO_RESCALE:CAUGHT(rc=OK,bad=1904) Q_ROW:CAUGHT(rc=OK,bad=2042) OUT_ROW:CAUGHT(rc=OK,bad=2042) +PASS gqa_f32_llama32_1b_32q8kv_ctx65 n=2048 rc=OK violations=0 worst_scaled_err=0.0014332 chip_ns=103169 call_ns=186146 calls=1 unwritten=0 kv_staged=266240/266240 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx128 n=2048 rc=OK violations=0 worst_scaled_err=0.00186415 chip_ns=103201 call_ns=110337 calls=1 unwritten=0 kv_staged=524288/524288 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx129 n=2048 rc=OK violations=0 worst_scaled_err=0.00185519 chip_ns=102753 call_ns=109985 calls=1 unwritten=0 kv_staged=528384/528384 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx300 n=2048 rc=OK violations=0 worst_scaled_err=0.00162705 chip_ns=204546 call_ns=220482 calls=1 unwritten=0 kv_staged=1228800/1228800 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx1000 n=2048 rc=OK violations=0 worst_scaled_err=0.00330327 chip_ns=612806 call_ns=696054 calls=1 unwritten=0 kv_staged=4096000/4096000 blocks=1/1 | mutants 0/0 +PASS gqa_f32_large_scores_32q4kv_ctx256 n=2048 rc=OK violations=0 worst_scaled_err=0.105395 chip_ns=204705 call_ns=309938 calls=1 unwritten=0 kv_staged=524288/524288 blocks=1/1 | mutants 1/1 NO_MAX:CAUGHT(rc=OK,bad=2048) +PASS gqa_f32_mha_16q16kv_ctx300 n=1024 rc=OK violations=0 worst_scaled_err=0.00197746 chip_ns=206626 call_ns=336611 calls=1 unwritten=0 kv_staged=2457600/2457600 blocks=1/1 | mutants 3/3 Q_ROW:CAUGHT(rc=OK,bad=1022) OUT_ROW:CAUGHT(rc=OK,bad=1024) KV_HEAD:CAUGHT(rc=OK,bad=1024) +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx1 n=2048 rc=OK violations=0 worst_scaled_err=0 chip_ns=103185 call_ns=182818 calls=1 unwritten=0 kv_staged=16384/16384 blocks=1/1 | mutants 1/1 KV_HEAD:CAUGHT(rc=OK,bad=2048) +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx17 n=2048 rc=OK violations=0 worst_scaled_err=0.002096 chip_ns=103281 call_ns=178513 calls=1 unwritten=0 kv_staged=32768/32768 blocks=2/2 | mutants 2/2 KV_HEAD:CAUGHT(rc=OK,bad=2046) SLOT:CAUGHT(rc=OK,bad=2048) +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx256 n=2048 rc=OK violations=0 worst_scaled_err=0.00199949 chip_ns=204850 call_ns=284770 calls=1 unwritten=0 kv_staged=262144/262144 blocks=16/16 | mutants 0/0 +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx2048 n=2048 rc=OK violations=0 worst_scaled_err=0.00336531 chip_ns=920008 call_ns=964473 calls=1 unwritten=0 kv_staged=2097152/2097152 blocks=128/128 | mutants 5/5 KV_HEAD:CAUGHT(rc=OK,bad=2045) SLOT:CAUGHT(rc=OK,bad=2042) NO_RESCALE:CAUGHT(rc=OK,bad=2023) Q_ROW:CAUGHT(rc=OK,bad=2042) OUT_ROW:CAUGHT(rc=OK,bad=2042) +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx65 n=2048 rc=OK violations=0 worst_scaled_err=0.00182741 chip_ns=103089 call_ns=184946 calls=1 unwritten=0 kv_staged=81920/81920 blocks=5/5 | mutants 0/0 +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx128 n=2048 rc=OK violations=0 worst_scaled_err=0.00191997 chip_ns=102625 call_ns=106177 calls=1 unwritten=0 kv_staged=131072/131072 blocks=8/8 | mutants 0/0 +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx129 n=2048 rc=OK violations=0 worst_scaled_err=0.00192425 chip_ns=102897 call_ns=106465 calls=1 unwritten=0 kv_staged=147456/147456 blocks=9/9 | mutants 0/0 +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx300 n=2048 rc=OK violations=0 worst_scaled_err=0.00243899 chip_ns=203986 call_ns=209490 calls=1 unwritten=0 kv_staged=311296/311296 blocks=19/19 | mutants 0/0 +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx1000 n=2048 rc=OK violations=0 worst_scaled_err=0.00547531 chip_ns=204290 call_ns=208018 calls=1 unwritten=0 kv_staged=163840/1032192 blocks=10/63 | mutants 0/0 +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx1 n=2048 rc=OK violations=0 worst_scaled_err=0 chip_ns=103665 call_ns=183217 calls=1 unwritten=0 kv_staged=32768/32768 blocks=1/1 | mutants 1/1 KV_HEAD:CAUGHT(rc=OK,bad=2048) +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx17 n=2048 rc=OK violations=0 worst_scaled_err=0.00223287 chip_ns=103281 call_ns=179442 calls=1 unwritten=0 kv_staged=65536/65536 blocks=2/2 | mutants 2/2 KV_HEAD:CAUGHT(rc=OK,bad=2048) SLOT:CAUGHT(rc=OK,bad=2048) +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx256 n=2048 rc=OK violations=0 worst_scaled_err=0.00167964 chip_ns=206514 call_ns=292387 calls=1 unwritten=0 kv_staged=524288/524288 blocks=16/16 | mutants 0/0 +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx2048 n=2048 rc=OK violations=0 worst_scaled_err=0.00358689 chip_ns=1022569 call_ns=1116922 calls=1 unwritten=0 kv_staged=4194304/4194304 blocks=128/128 | mutants 5/5 KV_HEAD:CAUGHT(rc=OK,bad=2048) SLOT:CAUGHT(rc=OK,bad=2045) NO_RESCALE:CAUGHT(rc=OK,bad=1999) Q_ROW:CAUGHT(rc=OK,bad=2044) OUT_ROW:CAUGHT(rc=OK,bad=2044) +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx65 n=2048 rc=OK violations=0 worst_scaled_err=0.00228809 chip_ns=103169 call_ns=187393 calls=1 unwritten=0 kv_staged=163840/163840 blocks=5/5 | mutants 0/0 +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx128 n=2048 rc=OK violations=0 worst_scaled_err=0.00160376 chip_ns=102977 call_ns=107841 calls=1 unwritten=0 kv_staged=262144/262144 blocks=8/8 | mutants 0/0 +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx129 n=2048 rc=OK violations=0 worst_scaled_err=0.00173996 chip_ns=103249 call_ns=108161 calls=1 unwritten=0 kv_staged=294912/294912 blocks=9/9 | mutants 0/0 +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx300 n=2048 rc=OK violations=0 worst_scaled_err=0.00303696 chip_ns=204850 call_ns=213442 calls=1 unwritten=0 kv_staged=622592/622592 blocks=19/19 | mutants 0/0 +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx1000 n=2048 rc=OK violations=0 worst_scaled_err=0.00420311 chip_ns=205506 call_ns=211618 calls=1 unwritten=0 kv_staged=327680/2064384 blocks=10/63 | mutants 0/0 +PASS paged_bf16_batch_3seq_32q8kv_ctx40_0_100 n=6144 rc=OK violations=0 worst_scaled_err=0.00181424 chip_ns=205808 call_ns=432691 calls=2 unwritten=0 kv_staged=229376/229376 blocks=7/7 | mutants 4/4 KV_HEAD:CAUGHT(rc=OK,bad=4095) SLOT:CAUGHT(rc=OK,bad=4090) Q_ROW:CAUGHT(rc=OK,bad=4096) OUT_ROW:CAUGHT(rc=OK,bad=4096) +PASS paged_bf16_shared_prefix_4seq_8q2kv_10pre n=2048 rc=OK violations=0 worst_scaled_err=0.00188498 chip_ns=205474 call_ns=288131 calls=1 unwritten=0 kv_staged=155648/401408 blocks=19/49 | mutants 2/2 KV_HEAD:CAUGHT(rc=OK,bad=2048) SLOT:CAUGHT(rc=OK,bad=2046) +PASS paged_bf16_shared_prefix_4seq_32q8kv_10pre n=8192 rc=OK violations=0 worst_scaled_err=0.0024233 chip_ns=411812 call_ns=517076 calls=2 unwritten=0 kv_staged=950272/1605632 blocks=29/49 | mutants 2/2 KV_HEAD:CAUGHT(rc=OK,bad=8192) SLOT:CAUGHT(rc=OK,bad=8181) +mutant NO_MAX: caught 1 of 1 +mutant KV_HEAD: caught 11 of 11 +mutant SLOT: caught 9 of 9 +mutant NO_RESCALE: caught 4 of 4 +mutant Q_ROW: caught 6 of 6 +mutant OUT_ROW: caught 6 of 6 +device_opens=1 +PASS: 122 checks, 0 failed diff --git a/evidence/gpu-reconv-308/T2-attention/prod-sweep.json b/evidence/gpu-reconv-308/T2-attention/prod-sweep.json new file mode 100644 index 000000000..a343f5f29 --- /dev/null +++ b/evidence/gpu-reconv-308/T2-attention/prod-sweep.json @@ -0,0 +1 @@ +{"schema":"OMEGA_GPU_ATTENTION_SWEEP_V1","path":"paged_bf16","head_dim":64,"block_size":16,"timed_calls":20,"host_simulator":false,"tolerances":{"rel":2e-4,"abs":2e-5},"hardware_counters":"unavailable (SM occupancy, bandwidth utilisation, cache hit rates are not observable through this path)","points":[{"q_heads":1,"kv_heads":1,"gqa_ratio":1,"head_dim":64,"context":256,"q_width":64,"out_width":64,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00162825,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":1,"threads_per_cta":64,"call_ms_median":0.1033,"call_ms_p90":0.1043,"kernel_ms_median":0.1026,"kernel_ms_p90":0.1028,"kernel_cache_hits":20,"first_call_cache_hit":false,"kv_bytes_staged":12288,"kv_bytes_naive":65536,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":256,"tab_bytes":68,"out_bytes":256,"kv_source":"staged"},{"q_heads":1,"kv_heads":1,"gqa_ratio":1,"head_dim":64,"context":2048,"q_width":64,"out_width":64,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00286577,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":1,"threads_per_cta":64,"call_ms_median":0.4093,"call_ms_p90":0.4100,"kernel_ms_median":0.4078,"kernel_ms_p90":0.4084,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":77824,"kv_bytes_naive":524288,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":256,"tab_bytes":516,"out_bytes":256,"kv_source":"staged"},{"q_heads":2,"kv_heads":1,"gqa_ratio":2,"head_dim":64,"context":256,"q_width":128,"out_width":128,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00274735,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":2,"threads_per_cta":64,"call_ms_median":0.1036,"call_ms_p90":0.1067,"kernel_ms_median":0.1029,"kernel_ms_p90":0.1044,"kernel_cache_hits":20,"first_call_cache_hit":false,"kv_bytes_staged":12288,"kv_bytes_naive":65536,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":512,"tab_bytes":68,"out_bytes":512,"kv_source":"staged"},{"q_heads":2,"kv_heads":1,"gqa_ratio":2,"head_dim":64,"context":2048,"q_width":128,"out_width":128,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00293219,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":2,"threads_per_cta":64,"call_ms_median":0.4084,"call_ms_p90":0.4097,"kernel_ms_median":0.4069,"kernel_ms_p90":0.4080,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":77824,"kv_bytes_naive":524288,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":512,"tab_bytes":516,"out_bytes":512,"kv_source":"staged"},{"q_heads":2,"kv_heads":2,"gqa_ratio":1,"head_dim":64,"context":256,"q_width":128,"out_width":128,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00285429,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":2,"threads_per_cta":64,"call_ms_median":0.1036,"call_ms_p90":0.1041,"kernel_ms_median":0.1030,"kernel_ms_p90":0.1033,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":24576,"kv_bytes_naive":131072,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":512,"tab_bytes":68,"out_bytes":512,"kv_source":"staged"},{"q_heads":2,"kv_heads":2,"gqa_ratio":1,"head_dim":64,"context":2048,"q_width":128,"out_width":128,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00344759,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":2,"threads_per_cta":64,"call_ms_median":0.4090,"call_ms_p90":0.4096,"kernel_ms_median":0.4066,"kernel_ms_p90":0.4073,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":155648,"kv_bytes_naive":1048576,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":512,"tab_bytes":516,"out_bytes":512,"kv_source":"staged"},{"q_heads":4,"kv_heads":1,"gqa_ratio":4,"head_dim":64,"context":256,"q_width":256,"out_width":256,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00195521,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":4,"threads_per_cta":64,"call_ms_median":0.1035,"call_ms_p90":0.1038,"kernel_ms_median":0.1028,"kernel_ms_p90":0.1028,"kernel_cache_hits":20,"first_call_cache_hit":false,"kv_bytes_staged":12288,"kv_bytes_naive":65536,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":1024,"tab_bytes":68,"out_bytes":1024,"kv_source":"staged"},{"q_heads":4,"kv_heads":1,"gqa_ratio":4,"head_dim":64,"context":2048,"q_width":256,"out_width":256,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00455218,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":4,"threads_per_cta":64,"call_ms_median":0.4082,"call_ms_p90":0.4089,"kernel_ms_median":0.4067,"kernel_ms_p90":0.4071,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":77824,"kv_bytes_naive":524288,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":1024,"tab_bytes":516,"out_bytes":1024,"kv_source":"staged"},{"q_heads":4,"kv_heads":2,"gqa_ratio":2,"head_dim":64,"context":256,"q_width":256,"out_width":256,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00195668,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":4,"threads_per_cta":64,"call_ms_median":0.1039,"call_ms_p90":0.1040,"kernel_ms_median":0.1031,"kernel_ms_p90":0.1031,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":24576,"kv_bytes_naive":131072,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":1024,"tab_bytes":68,"out_bytes":1024,"kv_source":"staged"},{"q_heads":4,"kv_heads":2,"gqa_ratio":2,"head_dim":64,"context":2048,"q_width":256,"out_width":256,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00429287,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":4,"threads_per_cta":64,"call_ms_median":0.4106,"call_ms_p90":0.4115,"kernel_ms_median":0.4084,"kernel_ms_p90":0.4092,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":155648,"kv_bytes_naive":1048576,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":1024,"tab_bytes":516,"out_bytes":1024,"kv_source":"staged"},{"q_heads":4,"kv_heads":4,"gqa_ratio":1,"head_dim":64,"context":256,"q_width":256,"out_width":256,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00208557,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":4,"threads_per_cta":64,"call_ms_median":0.1042,"call_ms_p90":0.1043,"kernel_ms_median":0.1031,"kernel_ms_p90":0.1032,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":49152,"kv_bytes_naive":262144,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":1024,"tab_bytes":68,"out_bytes":1024,"kv_source":"staged"},{"q_heads":4,"kv_heads":4,"gqa_ratio":1,"head_dim":64,"context":2048,"q_width":256,"out_width":256,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00447204,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":4,"threads_per_cta":64,"call_ms_median":0.4121,"call_ms_p90":0.4129,"kernel_ms_median":0.4082,"kernel_ms_p90":0.4089,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":311296,"kv_bytes_naive":2097152,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":1024,"tab_bytes":516,"out_bytes":1024,"kv_source":"staged"},{"q_heads":8,"kv_heads":1,"gqa_ratio":8,"head_dim":64,"context":256,"q_width":512,"out_width":512,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00244755,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":8,"threads_per_cta":64,"call_ms_median":0.1036,"call_ms_p90":0.1040,"kernel_ms_median":0.1028,"kernel_ms_p90":0.1032,"kernel_cache_hits":20,"first_call_cache_hit":false,"kv_bytes_staged":12288,"kv_bytes_naive":65536,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":2048,"tab_bytes":68,"out_bytes":2048,"kv_source":"staged"},{"q_heads":8,"kv_heads":1,"gqa_ratio":8,"head_dim":64,"context":2048,"q_width":512,"out_width":512,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00561397,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":8,"threads_per_cta":64,"call_ms_median":0.4087,"call_ms_p90":0.4107,"kernel_ms_median":0.4068,"kernel_ms_p90":0.4089,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":77824,"kv_bytes_naive":524288,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":2048,"tab_bytes":516,"out_bytes":2048,"kv_source":"staged"},{"q_heads":8,"kv_heads":2,"gqa_ratio":4,"head_dim":64,"context":256,"q_width":512,"out_width":512,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00277137,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":8,"threads_per_cta":64,"call_ms_median":0.1041,"call_ms_p90":0.1048,"kernel_ms_median":0.1030,"kernel_ms_p90":0.1038,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":24576,"kv_bytes_naive":131072,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":2048,"tab_bytes":68,"out_bytes":2048,"kv_source":"staged"},{"q_heads":8,"kv_heads":2,"gqa_ratio":4,"head_dim":64,"context":2048,"q_width":512,"out_width":512,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00478059,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":8,"threads_per_cta":64,"call_ms_median":0.4111,"call_ms_p90":0.4122,"kernel_ms_median":0.4084,"kernel_ms_p90":0.4096,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":155648,"kv_bytes_naive":1048576,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":2048,"tab_bytes":516,"out_bytes":2048,"kv_source":"staged"},{"q_heads":8,"kv_heads":4,"gqa_ratio":2,"head_dim":64,"context":256,"q_width":512,"out_width":512,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00220051,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":8,"threads_per_cta":64,"call_ms_median":0.1044,"call_ms_p90":0.1048,"kernel_ms_median":0.1032,"kernel_ms_p90":0.1035,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":49152,"kv_bytes_naive":262144,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":2048,"tab_bytes":68,"out_bytes":2048,"kv_source":"staged"},{"q_heads":8,"kv_heads":4,"gqa_ratio":2,"head_dim":64,"context":2048,"q_width":512,"out_width":512,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.0051977,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":8,"threads_per_cta":64,"call_ms_median":0.4124,"call_ms_p90":0.4136,"kernel_ms_median":0.4083,"kernel_ms_p90":0.4095,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":311296,"kv_bytes_naive":2097152,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":2048,"tab_bytes":516,"out_bytes":2048,"kv_source":"staged"},{"q_heads":8,"kv_heads":8,"gqa_ratio":1,"head_dim":64,"context":256,"q_width":512,"out_width":512,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00240979,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":8,"threads_per_cta":64,"call_ms_median":0.1048,"call_ms_p90":0.1053,"kernel_ms_median":0.1032,"kernel_ms_p90":0.1036,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":98304,"kv_bytes_naive":524288,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":2048,"tab_bytes":68,"out_bytes":2048,"kv_source":"staged"},{"q_heads":8,"kv_heads":8,"gqa_ratio":1,"head_dim":64,"context":2048,"q_width":512,"out_width":512,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00466806,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":8,"threads_per_cta":64,"call_ms_median":0.4153,"call_ms_p90":0.4171,"kernel_ms_median":0.4082,"kernel_ms_p90":0.4098,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":622592,"kv_bytes_naive":4194304,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":2048,"tab_bytes":516,"out_bytes":2048,"kv_source":"staged"},{"q_heads":16,"kv_heads":1,"gqa_ratio":16,"head_dim":64,"context":256,"q_width":1024,"out_width":1024,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00249826,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":16,"threads_per_cta":64,"call_ms_median":0.1041,"call_ms_p90":0.1043,"kernel_ms_median":0.1028,"kernel_ms_p90":0.1029,"kernel_cache_hits":20,"first_call_cache_hit":false,"kv_bytes_staged":12288,"kv_bytes_naive":65536,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":4096,"tab_bytes":68,"out_bytes":4096,"kv_source":"staged"},{"q_heads":16,"kv_heads":1,"gqa_ratio":16,"head_dim":64,"context":2048,"q_width":1024,"out_width":1024,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00536905,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":16,"threads_per_cta":64,"call_ms_median":0.4099,"call_ms_p90":0.4112,"kernel_ms_median":0.4070,"kernel_ms_p90":0.4087,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":77824,"kv_bytes_naive":524288,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":4096,"tab_bytes":516,"out_bytes":4096,"kv_source":"staged"},{"q_heads":16,"kv_heads":2,"gqa_ratio":8,"head_dim":64,"context":256,"q_width":1024,"out_width":1024,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00239844,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":16,"threads_per_cta":64,"call_ms_median":0.1045,"call_ms_p90":0.1046,"kernel_ms_median":0.1031,"kernel_ms_p90":0.1032,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":24576,"kv_bytes_naive":131072,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":4096,"tab_bytes":68,"out_bytes":4096,"kv_source":"staged"},{"q_heads":16,"kv_heads":2,"gqa_ratio":8,"head_dim":64,"context":2048,"q_width":1024,"out_width":1024,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00570392,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":16,"threads_per_cta":64,"call_ms_median":0.4112,"call_ms_p90":0.4126,"kernel_ms_median":0.4081,"kernel_ms_p90":0.4097,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":155648,"kv_bytes_naive":1048576,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":4096,"tab_bytes":516,"out_bytes":4096,"kv_source":"staged"},{"q_heads":16,"kv_heads":4,"gqa_ratio":4,"head_dim":64,"context":256,"q_width":1024,"out_width":1024,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00278498,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":16,"threads_per_cta":64,"call_ms_median":0.1048,"call_ms_p90":0.1055,"kernel_ms_median":0.1031,"kernel_ms_p90":0.1036,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":49152,"kv_bytes_naive":262144,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":4096,"tab_bytes":68,"out_bytes":4096,"kv_source":"staged"},{"q_heads":16,"kv_heads":4,"gqa_ratio":4,"head_dim":64,"context":2048,"q_width":1024,"out_width":1024,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00518169,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":16,"threads_per_cta":64,"call_ms_median":0.4130,"call_ms_p90":0.4255,"kernel_ms_median":0.4085,"kernel_ms_p90":0.4123,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":311296,"kv_bytes_naive":2097152,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":4096,"tab_bytes":516,"out_bytes":4096,"kv_source":"staged"},{"q_heads":16,"kv_heads":8,"gqa_ratio":2,"head_dim":64,"context":256,"q_width":1024,"out_width":1024,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00262688,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":16,"threads_per_cta":64,"call_ms_median":0.1054,"call_ms_p90":0.1058,"kernel_ms_median":0.1031,"kernel_ms_p90":0.1035,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":98304,"kv_bytes_naive":524288,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":4096,"tab_bytes":68,"out_bytes":4096,"kv_source":"staged"},{"q_heads":16,"kv_heads":8,"gqa_ratio":2,"head_dim":64,"context":2048,"q_width":1024,"out_width":1024,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.0059316,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":16,"threads_per_cta":64,"call_ms_median":0.4161,"call_ms_p90":0.4174,"kernel_ms_median":0.4084,"kernel_ms_p90":0.4098,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":622592,"kv_bytes_naive":4194304,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":4096,"tab_bytes":516,"out_bytes":4096,"kv_source":"staged"},{"q_heads":16,"kv_heads":16,"gqa_ratio":1,"head_dim":64,"context":256,"q_width":1024,"out_width":1024,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00239914,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":16,"threads_per_cta":64,"call_ms_median":0.1062,"call_ms_p90":0.1066,"kernel_ms_median":0.1032,"kernel_ms_p90":0.1034,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":196608,"kv_bytes_naive":1048576,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":4096,"tab_bytes":68,"out_bytes":4096,"kv_source":"staged"},{"q_heads":16,"kv_heads":16,"gqa_ratio":1,"head_dim":64,"context":2048,"q_width":1024,"out_width":1024,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00553838,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":16,"threads_per_cta":64,"call_ms_median":0.4248,"call_ms_p90":0.4497,"kernel_ms_median":0.4083,"kernel_ms_p90":0.4143,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":1245184,"kv_bytes_naive":8388608,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":4096,"tab_bytes":516,"out_bytes":4096,"kv_source":"staged"},{"q_heads":24,"kv_heads":1,"gqa_ratio":24,"head_dim":64,"context":256,"q_width":1536,"out_width":1536,"class":"unsupported_by_kernel_envelope","rc":"BAD_ARGS","pass":true},{"q_heads":24,"kv_heads":1,"gqa_ratio":24,"head_dim":64,"context":2048,"q_width":1536,"out_width":1536,"class":"unsupported_by_kernel_envelope","rc":"BAD_ARGS","pass":true},{"q_heads":24,"kv_heads":2,"gqa_ratio":12,"head_dim":64,"context":256,"q_width":1536,"out_width":1536,"class":"unsupported_by_kernel_envelope","rc":"BAD_ARGS","pass":true},{"q_heads":24,"kv_heads":2,"gqa_ratio":12,"head_dim":64,"context":2048,"q_width":1536,"out_width":1536,"class":"unsupported_by_kernel_envelope","rc":"BAD_ARGS","pass":true},{"q_heads":24,"kv_heads":3,"gqa_ratio":8,"head_dim":64,"context":256,"q_width":1536,"out_width":1536,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00306755,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":24,"threads_per_cta":64,"call_ms_median":0.1048,"call_ms_p90":0.1056,"kernel_ms_median":0.1028,"kernel_ms_p90":0.1031,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":36864,"kv_bytes_naive":196608,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":6144,"tab_bytes":68,"out_bytes":6144,"kv_source":"staged"},{"q_heads":24,"kv_heads":3,"gqa_ratio":8,"head_dim":64,"context":2048,"q_width":1536,"out_width":1536,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00539717,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":24,"threads_per_cta":64,"call_ms_median":0.6149,"call_ms_p90":0.6158,"kernel_ms_median":0.6109,"kernel_ms_p90":0.6117,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":233472,"kv_bytes_naive":1572864,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":6144,"tab_bytes":516,"out_bytes":6144,"kv_source":"staged"},{"q_heads":24,"kv_heads":4,"gqa_ratio":6,"head_dim":64,"context":256,"q_width":1536,"out_width":1536,"class":"unsupported_by_kernel_envelope","rc":"BAD_ARGS","pass":true},{"q_heads":24,"kv_heads":4,"gqa_ratio":6,"head_dim":64,"context":2048,"q_width":1536,"out_width":1536,"class":"unsupported_by_kernel_envelope","rc":"BAD_ARGS","pass":true},{"q_heads":24,"kv_heads":6,"gqa_ratio":4,"head_dim":64,"context":256,"q_width":1536,"out_width":1536,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00326846,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":24,"threads_per_cta":64,"call_ms_median":0.1054,"call_ms_p90":0.1063,"kernel_ms_median":0.1031,"kernel_ms_p90":0.1040,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":73728,"kv_bytes_naive":393216,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":6144,"tab_bytes":68,"out_bytes":6144,"kv_source":"staged"},{"q_heads":24,"kv_heads":6,"gqa_ratio":4,"head_dim":64,"context":2048,"q_width":1536,"out_width":1536,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00527248,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":24,"threads_per_cta":64,"call_ms_median":0.6181,"call_ms_p90":0.6197,"kernel_ms_median":0.6116,"kernel_ms_p90":0.6127,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":466944,"kv_bytes_naive":3145728,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":6144,"tab_bytes":516,"out_bytes":6144,"kv_source":"staged"},{"q_heads":24,"kv_heads":8,"gqa_ratio":3,"head_dim":64,"context":256,"q_width":1536,"out_width":1536,"class":"unsupported_by_kernel_envelope","rc":"BAD_ARGS","pass":true},{"q_heads":24,"kv_heads":8,"gqa_ratio":3,"head_dim":64,"context":2048,"q_width":1536,"out_width":1536,"class":"unsupported_by_kernel_envelope","rc":"BAD_ARGS","pass":true},{"q_heads":24,"kv_heads":12,"gqa_ratio":2,"head_dim":64,"context":256,"q_width":1536,"out_width":1536,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00338069,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":24,"threads_per_cta":64,"call_ms_median":0.1062,"call_ms_p90":0.1064,"kernel_ms_median":0.1032,"kernel_ms_p90":0.1034,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":147456,"kv_bytes_naive":786432,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":6144,"tab_bytes":68,"out_bytes":6144,"kv_source":"staged"},{"q_heads":24,"kv_heads":12,"gqa_ratio":2,"head_dim":64,"context":2048,"q_width":1536,"out_width":1536,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00538782,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":24,"threads_per_cta":64,"call_ms_median":0.6243,"call_ms_p90":0.6271,"kernel_ms_median":0.6122,"kernel_ms_p90":0.6141,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":933888,"kv_bytes_naive":6291456,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":6144,"tab_bytes":516,"out_bytes":6144,"kv_source":"staged"},{"q_heads":24,"kv_heads":24,"gqa_ratio":1,"head_dim":64,"context":256,"q_width":1536,"out_width":1536,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00259323,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":24,"threads_per_cta":64,"call_ms_median":0.1071,"call_ms_p90":0.1080,"kernel_ms_median":0.1030,"kernel_ms_p90":0.1035,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":294912,"kv_bytes_naive":1572864,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":6144,"tab_bytes":68,"out_bytes":6144,"kv_source":"staged"},{"q_heads":24,"kv_heads":24,"gqa_ratio":1,"head_dim":64,"context":2048,"q_width":1536,"out_width":1536,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00737519,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":24,"threads_per_cta":64,"call_ms_median":0.5352,"call_ms_p90":0.5948,"kernel_ms_median":0.5099,"kernel_ms_p90":0.5703,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":1867776,"kv_bytes_naive":12582912,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":6144,"tab_bytes":516,"out_bytes":6144,"kv_source":"staged"},{"q_heads":32,"kv_heads":1,"gqa_ratio":32,"head_dim":64,"context":256,"q_width":2048,"out_width":2048,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00241453,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":32,"threads_per_cta":64,"call_ms_median":0.1049,"call_ms_p90":0.1052,"kernel_ms_median":0.1028,"kernel_ms_p90":0.1028,"kernel_cache_hits":20,"first_call_cache_hit":false,"kv_bytes_staged":12288,"kv_bytes_naive":65536,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":8192,"tab_bytes":68,"out_bytes":8192,"kv_source":"staged"},{"q_heads":32,"kv_heads":1,"gqa_ratio":32,"head_dim":64,"context":2048,"q_width":2048,"out_width":2048,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00525453,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":32,"threads_per_cta":64,"call_ms_median":0.4122,"call_ms_p90":0.4226,"kernel_ms_median":0.4079,"kernel_ms_p90":0.4195,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":77824,"kv_bytes_naive":524288,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":8192,"tab_bytes":516,"out_bytes":8192,"kv_source":"staged"},{"q_heads":32,"kv_heads":2,"gqa_ratio":16,"head_dim":64,"context":256,"q_width":2048,"out_width":2048,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.0031327,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":32,"threads_per_cta":64,"call_ms_median":0.1047,"call_ms_p90":0.1051,"kernel_ms_median":0.1025,"kernel_ms_p90":0.1030,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":24576,"kv_bytes_naive":131072,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":8192,"tab_bytes":68,"out_bytes":8192,"kv_source":"staged"},{"q_heads":32,"kv_heads":2,"gqa_ratio":16,"head_dim":64,"context":2048,"q_width":2048,"out_width":2048,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00648224,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":32,"threads_per_cta":64,"call_ms_median":0.4113,"call_ms_p90":0.4121,"kernel_ms_median":0.4076,"kernel_ms_p90":0.4082,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":155648,"kv_bytes_naive":1048576,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":8192,"tab_bytes":516,"out_bytes":8192,"kv_source":"staged"},{"q_heads":32,"kv_heads":4,"gqa_ratio":8,"head_dim":64,"context":256,"q_width":2048,"out_width":2048,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00316999,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":32,"threads_per_cta":64,"call_ms_median":0.1052,"call_ms_p90":0.1058,"kernel_ms_median":0.1029,"kernel_ms_p90":0.1032,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":49152,"kv_bytes_naive":262144,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":8192,"tab_bytes":68,"out_bytes":8192,"kv_source":"staged"},{"q_heads":32,"kv_heads":4,"gqa_ratio":8,"head_dim":64,"context":2048,"q_width":2048,"out_width":2048,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00599161,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":32,"threads_per_cta":64,"call_ms_median":0.4130,"call_ms_p90":0.4140,"kernel_ms_median":0.4078,"kernel_ms_p90":0.4085,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":311296,"kv_bytes_naive":2097152,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":8192,"tab_bytes":516,"out_bytes":8192,"kv_source":"staged"},{"q_heads":32,"kv_heads":8,"gqa_ratio":4,"head_dim":64,"context":256,"q_width":2048,"out_width":2048,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00278167,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":32,"threads_per_cta":64,"call_ms_median":0.1057,"call_ms_p90":0.1060,"kernel_ms_median":0.1028,"kernel_ms_p90":0.1031,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":98304,"kv_bytes_naive":524288,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":8192,"tab_bytes":68,"out_bytes":8192,"kv_source":"staged"},{"q_heads":32,"kv_heads":8,"gqa_ratio":4,"head_dim":64,"context":2048,"q_width":2048,"out_width":2048,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.005897,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":32,"threads_per_cta":64,"call_ms_median":0.4166,"call_ms_p90":0.4230,"kernel_ms_median":0.4080,"kernel_ms_p90":0.4105,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":622592,"kv_bytes_naive":4194304,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":8192,"tab_bytes":516,"out_bytes":8192,"kv_source":"staged"},{"q_heads":32,"kv_heads":16,"gqa_ratio":2,"head_dim":64,"context":256,"q_width":2048,"out_width":2048,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00268895,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":32,"threads_per_cta":64,"call_ms_median":0.1067,"call_ms_p90":0.1073,"kernel_ms_median":0.1029,"kernel_ms_p90":0.1034,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":196608,"kv_bytes_naive":1048576,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":8192,"tab_bytes":68,"out_bytes":8192,"kv_source":"staged"},{"q_heads":32,"kv_heads":16,"gqa_ratio":2,"head_dim":64,"context":2048,"q_width":2048,"out_width":2048,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00571167,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":32,"threads_per_cta":64,"call_ms_median":0.4265,"call_ms_p90":0.4296,"kernel_ms_median":0.4091,"kernel_ms_p90":0.4103,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":1245184,"kv_bytes_naive":8388608,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":8192,"tab_bytes":516,"out_bytes":8192,"kv_source":"staged"},{"q_heads":32,"kv_heads":32,"gqa_ratio":1,"head_dim":64,"context":256,"q_width":2048,"out_width":2048,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00256581,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":32,"threads_per_cta":64,"call_ms_median":0.1086,"call_ms_p90":0.1093,"kernel_ms_median":0.1030,"kernel_ms_p90":0.1036,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":393216,"kv_bytes_naive":2097152,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":8192,"tab_bytes":68,"out_bytes":8192,"kv_source":"staged"},{"q_heads":32,"kv_heads":32,"gqa_ratio":1,"head_dim":64,"context":2048,"q_width":2048,"out_width":2048,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00647965,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":32,"threads_per_cta":64,"call_ms_median":0.4433,"call_ms_p90":0.4483,"kernel_ms_median":0.4075,"kernel_ms_p90":0.4083,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":2490368,"kv_bytes_naive":16777216,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":8192,"tab_bytes":516,"out_bytes":8192,"kv_source":"staged"},{"q_heads":64,"kv_heads":1,"gqa_ratio":64,"head_dim":64,"context":256,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00332208,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":0.1062,"call_ms_p90":0.1070,"kernel_ms_median":0.1028,"kernel_ms_p90":0.1035,"kernel_cache_hits":20,"first_call_cache_hit":false,"kv_bytes_staged":12288,"kv_bytes_naive":65536,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":16384,"tab_bytes":68,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":1,"gqa_ratio":64,"head_dim":64,"context":2048,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00559141,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":0.4114,"call_ms_p90":0.4671,"kernel_ms_median":0.4068,"kernel_ms_p90":0.4628,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":77824,"kv_bytes_naive":524288,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":16384,"tab_bytes":516,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":2,"gqa_ratio":32,"head_dim":64,"context":256,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00286709,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":0.1068,"call_ms_p90":0.1085,"kernel_ms_median":0.1030,"kernel_ms_p90":0.1033,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":24576,"kv_bytes_naive":131072,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":16384,"tab_bytes":68,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":2,"gqa_ratio":32,"head_dim":64,"context":2048,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00639622,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":1.0236,"call_ms_p90":1.0268,"kernel_ms_median":1.0182,"kernel_ms_p90":1.0215,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":155648,"kv_bytes_naive":1048576,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":16384,"tab_bytes":516,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":4,"gqa_ratio":16,"head_dim":64,"context":256,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00399345,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":0.1068,"call_ms_p90":0.1070,"kernel_ms_median":0.1028,"kernel_ms_p90":0.1031,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":49152,"kv_bytes_naive":262144,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":16384,"tab_bytes":68,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":4,"gqa_ratio":16,"head_dim":64,"context":2048,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00618912,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":1.0265,"call_ms_p90":1.3299,"kernel_ms_median":1.0184,"kernel_ms_p90":1.3235,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":311296,"kv_bytes_naive":2097152,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":16384,"tab_bytes":516,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":8,"gqa_ratio":8,"head_dim":64,"context":256,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.0028507,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":0.1145,"call_ms_p90":0.1224,"kernel_ms_median":0.1053,"kernel_ms_p90":0.1102,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":98304,"kv_bytes_naive":524288,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":16384,"tab_bytes":68,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":8,"gqa_ratio":8,"head_dim":64,"context":2048,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00529008,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":0.9881,"call_ms_p90":1.0507,"kernel_ms_median":0.9593,"kernel_ms_p90":1.0271,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":622592,"kv_bytes_naive":4194304,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":16384,"tab_bytes":516,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":16,"gqa_ratio":4,"head_dim":64,"context":256,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00272721,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":0.1182,"call_ms_p90":0.1268,"kernel_ms_median":0.1061,"kernel_ms_p90":0.1116,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":196608,"kv_bytes_naive":1048576,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":16384,"tab_bytes":68,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":16,"gqa_ratio":4,"head_dim":64,"context":2048,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00565958,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":1.0446,"call_ms_p90":1.0796,"kernel_ms_median":1.0070,"kernel_ms_p90":1.0364,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":1245184,"kv_bytes_naive":8388608,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":16384,"tab_bytes":516,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":32,"gqa_ratio":2,"head_dim":64,"context":256,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00282842,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":0.1260,"call_ms_p90":0.1301,"kernel_ms_median":0.1087,"kernel_ms_p90":0.1102,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":393216,"kv_bytes_naive":2097152,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":16384,"tab_bytes":68,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":32,"gqa_ratio":2,"head_dim":64,"context":2048,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00670759,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":1.0741,"call_ms_p90":1.0949,"kernel_ms_median":1.0047,"kernel_ms_p90":1.0292,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":2490368,"kv_bytes_naive":16777216,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":16384,"tab_bytes":516,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":64,"gqa_ratio":1,"head_dim":64,"context":256,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00284045,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":0.1350,"call_ms_p90":0.1375,"kernel_ms_median":0.1081,"kernel_ms_p90":0.1104,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":786432,"kv_bytes_naive":4194304,"kv_blocks_logical":16,"kv_blocks_unique":3,"q_bytes":16384,"tab_bytes":68,"out_bytes":16384,"kv_source":"staged"},{"q_heads":64,"kv_heads":64,"gqa_ratio":1,"head_dim":64,"context":2048,"q_width":4096,"out_width":4096,"class":"supported","rc":"OK","pass":true,"violations":0,"worst_scaled_err":0.00870193,"unwritten_words":0,"launches_per_call":1,"ctas_last_launch":64,"threads_per_cta":64,"call_ms_median":1.1805,"call_ms_p90":1.2581,"kernel_ms_median":1.0378,"kernel_ms_p90":1.0966,"kernel_cache_hits":20,"first_call_cache_hit":true,"kv_bytes_staged":4980736,"kv_bytes_naive":33554432,"kv_blocks_logical":128,"kv_blocks_unique":19,"q_bytes":16384,"tab_bytes":516,"out_bytes":16384,"kv_source":"staged"}],"points_total":72,"points_supported":64,"points_unsupported":8,"device_opens":1,"checks":73,"failed":0,"verdict":"OMEGA_GPU_ATTENTION_SWEEP_PASS"} diff --git a/evidence/gpu-reconv-308/T2-attention/prod-sweep.log b/evidence/gpu-reconv-308/T2-attention/prod-sweep.log new file mode 100644 index 000000000..a9103665a --- /dev/null +++ b/evidence/gpu-reconv-308/T2-attention/prod-sweep.log @@ -0,0 +1,74 @@ +PASS sweep 1q/ 1kv ratio 1 ctx 256: launches=1 ctas=1 call_ms med=0.103 p90=0.104 kernel_ms med=0.103 p90=0.103 kv_staged=12288 (naive 65536) blocks=3/16 q=256 tab=68 out=256 worst=0.00162825 +PASS sweep 1q/ 1kv ratio 1 ctx 2048: launches=1 ctas=1 call_ms med=0.409 p90=0.410 kernel_ms med=0.408 p90=0.408 kv_staged=77824 (naive 524288) blocks=19/128 q=256 tab=516 out=256 worst=0.00286577 +PASS sweep 2q/ 1kv ratio 2 ctx 256: launches=1 ctas=2 call_ms med=0.104 p90=0.107 kernel_ms med=0.103 p90=0.104 kv_staged=12288 (naive 65536) blocks=3/16 q=512 tab=68 out=512 worst=0.00274735 +PASS sweep 2q/ 1kv ratio 2 ctx 2048: launches=1 ctas=2 call_ms med=0.408 p90=0.410 kernel_ms med=0.407 p90=0.408 kv_staged=77824 (naive 524288) blocks=19/128 q=512 tab=516 out=512 worst=0.00293219 +PASS sweep 2q/ 2kv ratio 1 ctx 256: launches=1 ctas=2 call_ms med=0.104 p90=0.104 kernel_ms med=0.103 p90=0.103 kv_staged=24576 (naive 131072) blocks=3/16 q=512 tab=68 out=512 worst=0.00285429 +PASS sweep 2q/ 2kv ratio 1 ctx 2048: launches=1 ctas=2 call_ms med=0.409 p90=0.410 kernel_ms med=0.407 p90=0.407 kv_staged=155648 (naive 1048576) blocks=19/128 q=512 tab=516 out=512 worst=0.00344759 +PASS sweep 4q/ 1kv ratio 4 ctx 256: launches=1 ctas=4 call_ms med=0.104 p90=0.104 kernel_ms med=0.103 p90=0.103 kv_staged=12288 (naive 65536) blocks=3/16 q=1024 tab=68 out=1024 worst=0.00195521 +PASS sweep 4q/ 1kv ratio 4 ctx 2048: launches=1 ctas=4 call_ms med=0.408 p90=0.409 kernel_ms med=0.407 p90=0.407 kv_staged=77824 (naive 524288) blocks=19/128 q=1024 tab=516 out=1024 worst=0.00455218 +PASS sweep 4q/ 2kv ratio 2 ctx 256: launches=1 ctas=4 call_ms med=0.104 p90=0.104 kernel_ms med=0.103 p90=0.103 kv_staged=24576 (naive 131072) blocks=3/16 q=1024 tab=68 out=1024 worst=0.00195668 +PASS sweep 4q/ 2kv ratio 2 ctx 2048: launches=1 ctas=4 call_ms med=0.411 p90=0.411 kernel_ms med=0.408 p90=0.409 kv_staged=155648 (naive 1048576) blocks=19/128 q=1024 tab=516 out=1024 worst=0.00429287 +PASS sweep 4q/ 4kv ratio 1 ctx 256: launches=1 ctas=4 call_ms med=0.104 p90=0.104 kernel_ms med=0.103 p90=0.103 kv_staged=49152 (naive 262144) blocks=3/16 q=1024 tab=68 out=1024 worst=0.00208557 +PASS sweep 4q/ 4kv ratio 1 ctx 2048: launches=1 ctas=4 call_ms med=0.412 p90=0.413 kernel_ms med=0.408 p90=0.409 kv_staged=311296 (naive 2097152) blocks=19/128 q=1024 tab=516 out=1024 worst=0.00447204 +PASS sweep 8q/ 1kv ratio 8 ctx 256: launches=1 ctas=8 call_ms med=0.104 p90=0.104 kernel_ms med=0.103 p90=0.103 kv_staged=12288 (naive 65536) blocks=3/16 q=2048 tab=68 out=2048 worst=0.00244755 +PASS sweep 8q/ 1kv ratio 8 ctx 2048: launches=1 ctas=8 call_ms med=0.409 p90=0.411 kernel_ms med=0.407 p90=0.409 kv_staged=77824 (naive 524288) blocks=19/128 q=2048 tab=516 out=2048 worst=0.00561397 +PASS sweep 8q/ 2kv ratio 4 ctx 256: launches=1 ctas=8 call_ms med=0.104 p90=0.105 kernel_ms med=0.103 p90=0.104 kv_staged=24576 (naive 131072) blocks=3/16 q=2048 tab=68 out=2048 worst=0.00277137 +PASS sweep 8q/ 2kv ratio 4 ctx 2048: launches=1 ctas=8 call_ms med=0.411 p90=0.412 kernel_ms med=0.408 p90=0.410 kv_staged=155648 (naive 1048576) blocks=19/128 q=2048 tab=516 out=2048 worst=0.00478059 +PASS sweep 8q/ 4kv ratio 2 ctx 256: launches=1 ctas=8 call_ms med=0.104 p90=0.105 kernel_ms med=0.103 p90=0.104 kv_staged=49152 (naive 262144) blocks=3/16 q=2048 tab=68 out=2048 worst=0.00220051 +PASS sweep 8q/ 4kv ratio 2 ctx 2048: launches=1 ctas=8 call_ms med=0.412 p90=0.414 kernel_ms med=0.408 p90=0.409 kv_staged=311296 (naive 2097152) blocks=19/128 q=2048 tab=516 out=2048 worst=0.0051977 +PASS sweep 8q/ 8kv ratio 1 ctx 256: launches=1 ctas=8 call_ms med=0.105 p90=0.105 kernel_ms med=0.103 p90=0.104 kv_staged=98304 (naive 524288) blocks=3/16 q=2048 tab=68 out=2048 worst=0.00240979 +PASS sweep 8q/ 8kv ratio 1 ctx 2048: launches=1 ctas=8 call_ms med=0.415 p90=0.417 kernel_ms med=0.408 p90=0.410 kv_staged=622592 (naive 4194304) blocks=19/128 q=2048 tab=516 out=2048 worst=0.00466806 +PASS sweep 16q/ 1kv ratio 16 ctx 256: launches=1 ctas=16 call_ms med=0.104 p90=0.104 kernel_ms med=0.103 p90=0.103 kv_staged=12288 (naive 65536) blocks=3/16 q=4096 tab=68 out=4096 worst=0.00249826 +PASS sweep 16q/ 1kv ratio 16 ctx 2048: launches=1 ctas=16 call_ms med=0.410 p90=0.411 kernel_ms med=0.407 p90=0.409 kv_staged=77824 (naive 524288) blocks=19/128 q=4096 tab=516 out=4096 worst=0.00536905 +PASS sweep 16q/ 2kv ratio 8 ctx 256: launches=1 ctas=16 call_ms med=0.105 p90=0.105 kernel_ms med=0.103 p90=0.103 kv_staged=24576 (naive 131072) blocks=3/16 q=4096 tab=68 out=4096 worst=0.00239844 +PASS sweep 16q/ 2kv ratio 8 ctx 2048: launches=1 ctas=16 call_ms med=0.411 p90=0.413 kernel_ms med=0.408 p90=0.410 kv_staged=155648 (naive 1048576) blocks=19/128 q=4096 tab=516 out=4096 worst=0.00570392 +PASS sweep 16q/ 4kv ratio 4 ctx 256: launches=1 ctas=16 call_ms med=0.105 p90=0.105 kernel_ms med=0.103 p90=0.104 kv_staged=49152 (naive 262144) blocks=3/16 q=4096 tab=68 out=4096 worst=0.00278498 +PASS sweep 16q/ 4kv ratio 4 ctx 2048: launches=1 ctas=16 call_ms med=0.413 p90=0.426 kernel_ms med=0.408 p90=0.412 kv_staged=311296 (naive 2097152) blocks=19/128 q=4096 tab=516 out=4096 worst=0.00518169 +PASS sweep 16q/ 8kv ratio 2 ctx 256: launches=1 ctas=16 call_ms med=0.105 p90=0.106 kernel_ms med=0.103 p90=0.103 kv_staged=98304 (naive 524288) blocks=3/16 q=4096 tab=68 out=4096 worst=0.00262688 +PASS sweep 16q/ 8kv ratio 2 ctx 2048: launches=1 ctas=16 call_ms med=0.416 p90=0.417 kernel_ms med=0.408 p90=0.410 kv_staged=622592 (naive 4194304) blocks=19/128 q=4096 tab=516 out=4096 worst=0.0059316 +PASS sweep 16q/16kv ratio 1 ctx 256: launches=1 ctas=16 call_ms med=0.106 p90=0.107 kernel_ms med=0.103 p90=0.103 kv_staged=196608 (naive 1048576) blocks=3/16 q=4096 tab=68 out=4096 worst=0.00239914 +PASS sweep 16q/16kv ratio 1 ctx 2048: launches=1 ctas=16 call_ms med=0.425 p90=0.450 kernel_ms med=0.408 p90=0.414 kv_staged=1245184 (naive 8388608) blocks=19/128 q=4096 tab=516 out=4096 worst=0.00553838 +PASS sweep 24q/ 1kv ratio 24 ctx 256: unsupported_by_kernel_envelope (rc=BAD_ARGS) +PASS sweep 24q/ 1kv ratio 24 ctx 2048: unsupported_by_kernel_envelope (rc=BAD_ARGS) +PASS sweep 24q/ 2kv ratio 12 ctx 256: unsupported_by_kernel_envelope (rc=BAD_ARGS) +PASS sweep 24q/ 2kv ratio 12 ctx 2048: unsupported_by_kernel_envelope (rc=BAD_ARGS) +PASS sweep 24q/ 3kv ratio 8 ctx 256: launches=1 ctas=24 call_ms med=0.105 p90=0.106 kernel_ms med=0.103 p90=0.103 kv_staged=36864 (naive 196608) blocks=3/16 q=6144 tab=68 out=6144 worst=0.00306755 +PASS sweep 24q/ 3kv ratio 8 ctx 2048: launches=1 ctas=24 call_ms med=0.615 p90=0.616 kernel_ms med=0.611 p90=0.612 kv_staged=233472 (naive 1572864) blocks=19/128 q=6144 tab=516 out=6144 worst=0.00539717 +PASS sweep 24q/ 4kv ratio 6 ctx 256: unsupported_by_kernel_envelope (rc=BAD_ARGS) +PASS sweep 24q/ 4kv ratio 6 ctx 2048: unsupported_by_kernel_envelope (rc=BAD_ARGS) +PASS sweep 24q/ 6kv ratio 4 ctx 256: launches=1 ctas=24 call_ms med=0.105 p90=0.106 kernel_ms med=0.103 p90=0.104 kv_staged=73728 (naive 393216) blocks=3/16 q=6144 tab=68 out=6144 worst=0.00326846 +PASS sweep 24q/ 6kv ratio 4 ctx 2048: launches=1 ctas=24 call_ms med=0.618 p90=0.620 kernel_ms med=0.612 p90=0.613 kv_staged=466944 (naive 3145728) blocks=19/128 q=6144 tab=516 out=6144 worst=0.00527248 +PASS sweep 24q/ 8kv ratio 3 ctx 256: unsupported_by_kernel_envelope (rc=BAD_ARGS) +PASS sweep 24q/ 8kv ratio 3 ctx 2048: unsupported_by_kernel_envelope (rc=BAD_ARGS) +PASS sweep 24q/12kv ratio 2 ctx 256: launches=1 ctas=24 call_ms med=0.106 p90=0.106 kernel_ms med=0.103 p90=0.103 kv_staged=147456 (naive 786432) blocks=3/16 q=6144 tab=68 out=6144 worst=0.00338069 +PASS sweep 24q/12kv ratio 2 ctx 2048: launches=1 ctas=24 call_ms med=0.624 p90=0.627 kernel_ms med=0.612 p90=0.614 kv_staged=933888 (naive 6291456) blocks=19/128 q=6144 tab=516 out=6144 worst=0.00538782 +PASS sweep 24q/24kv ratio 1 ctx 256: launches=1 ctas=24 call_ms med=0.107 p90=0.108 kernel_ms med=0.103 p90=0.104 kv_staged=294912 (naive 1572864) blocks=3/16 q=6144 tab=68 out=6144 worst=0.00259323 +PASS sweep 24q/24kv ratio 1 ctx 2048: launches=1 ctas=24 call_ms med=0.535 p90=0.595 kernel_ms med=0.510 p90=0.570 kv_staged=1867776 (naive 12582912) blocks=19/128 q=6144 tab=516 out=6144 worst=0.00737519 +PASS sweep 32q/ 1kv ratio 32 ctx 256: launches=1 ctas=32 call_ms med=0.105 p90=0.105 kernel_ms med=0.103 p90=0.103 kv_staged=12288 (naive 65536) blocks=3/16 q=8192 tab=68 out=8192 worst=0.00241453 +PASS sweep 32q/ 1kv ratio 32 ctx 2048: launches=1 ctas=32 call_ms med=0.412 p90=0.423 kernel_ms med=0.408 p90=0.420 kv_staged=77824 (naive 524288) blocks=19/128 q=8192 tab=516 out=8192 worst=0.00525453 +PASS sweep 32q/ 2kv ratio 16 ctx 256: launches=1 ctas=32 call_ms med=0.105 p90=0.105 kernel_ms med=0.103 p90=0.103 kv_staged=24576 (naive 131072) blocks=3/16 q=8192 tab=68 out=8192 worst=0.0031327 +PASS sweep 32q/ 2kv ratio 16 ctx 2048: launches=1 ctas=32 call_ms med=0.411 p90=0.412 kernel_ms med=0.408 p90=0.408 kv_staged=155648 (naive 1048576) blocks=19/128 q=8192 tab=516 out=8192 worst=0.00648224 +PASS sweep 32q/ 4kv ratio 8 ctx 256: launches=1 ctas=32 call_ms med=0.105 p90=0.106 kernel_ms med=0.103 p90=0.103 kv_staged=49152 (naive 262144) blocks=3/16 q=8192 tab=68 out=8192 worst=0.00316999 +PASS sweep 32q/ 4kv ratio 8 ctx 2048: launches=1 ctas=32 call_ms med=0.413 p90=0.414 kernel_ms med=0.408 p90=0.409 kv_staged=311296 (naive 2097152) blocks=19/128 q=8192 tab=516 out=8192 worst=0.00599161 +PASS sweep 32q/ 8kv ratio 4 ctx 256: launches=1 ctas=32 call_ms med=0.106 p90=0.106 kernel_ms med=0.103 p90=0.103 kv_staged=98304 (naive 524288) blocks=3/16 q=8192 tab=68 out=8192 worst=0.00278167 +PASS sweep 32q/ 8kv ratio 4 ctx 2048: launches=1 ctas=32 call_ms med=0.417 p90=0.423 kernel_ms med=0.408 p90=0.410 kv_staged=622592 (naive 4194304) blocks=19/128 q=8192 tab=516 out=8192 worst=0.005897 +PASS sweep 32q/16kv ratio 2 ctx 256: launches=1 ctas=32 call_ms med=0.107 p90=0.107 kernel_ms med=0.103 p90=0.103 kv_staged=196608 (naive 1048576) blocks=3/16 q=8192 tab=68 out=8192 worst=0.00268895 +PASS sweep 32q/16kv ratio 2 ctx 2048: launches=1 ctas=32 call_ms med=0.427 p90=0.430 kernel_ms med=0.409 p90=0.410 kv_staged=1245184 (naive 8388608) blocks=19/128 q=8192 tab=516 out=8192 worst=0.00571167 +PASS sweep 32q/32kv ratio 1 ctx 256: launches=1 ctas=32 call_ms med=0.109 p90=0.109 kernel_ms med=0.103 p90=0.104 kv_staged=393216 (naive 2097152) blocks=3/16 q=8192 tab=68 out=8192 worst=0.00256581 +PASS sweep 32q/32kv ratio 1 ctx 2048: launches=1 ctas=32 call_ms med=0.443 p90=0.448 kernel_ms med=0.407 p90=0.408 kv_staged=2490368 (naive 16777216) blocks=19/128 q=8192 tab=516 out=8192 worst=0.00647965 +PASS sweep 64q/ 1kv ratio 64 ctx 256: launches=1 ctas=64 call_ms med=0.106 p90=0.107 kernel_ms med=0.103 p90=0.103 kv_staged=12288 (naive 65536) blocks=3/16 q=16384 tab=68 out=16384 worst=0.00332208 +PASS sweep 64q/ 1kv ratio 64 ctx 2048: launches=1 ctas=64 call_ms med=0.411 p90=0.467 kernel_ms med=0.407 p90=0.463 kv_staged=77824 (naive 524288) blocks=19/128 q=16384 tab=516 out=16384 worst=0.00559141 +PASS sweep 64q/ 2kv ratio 32 ctx 256: launches=1 ctas=64 call_ms med=0.107 p90=0.109 kernel_ms med=0.103 p90=0.103 kv_staged=24576 (naive 131072) blocks=3/16 q=16384 tab=68 out=16384 worst=0.00286709 +PASS sweep 64q/ 2kv ratio 32 ctx 2048: launches=1 ctas=64 call_ms med=1.024 p90=1.027 kernel_ms med=1.018 p90=1.021 kv_staged=155648 (naive 1048576) blocks=19/128 q=16384 tab=516 out=16384 worst=0.00639622 +PASS sweep 64q/ 4kv ratio 16 ctx 256: launches=1 ctas=64 call_ms med=0.107 p90=0.107 kernel_ms med=0.103 p90=0.103 kv_staged=49152 (naive 262144) blocks=3/16 q=16384 tab=68 out=16384 worst=0.00399345 +PASS sweep 64q/ 4kv ratio 16 ctx 2048: launches=1 ctas=64 call_ms med=1.027 p90=1.330 kernel_ms med=1.018 p90=1.323 kv_staged=311296 (naive 2097152) blocks=19/128 q=16384 tab=516 out=16384 worst=0.00618912 +PASS sweep 64q/ 8kv ratio 8 ctx 256: launches=1 ctas=64 call_ms med=0.115 p90=0.122 kernel_ms med=0.105 p90=0.110 kv_staged=98304 (naive 524288) blocks=3/16 q=16384 tab=68 out=16384 worst=0.0028507 +PASS sweep 64q/ 8kv ratio 8 ctx 2048: launches=1 ctas=64 call_ms med=0.988 p90=1.051 kernel_ms med=0.959 p90=1.027 kv_staged=622592 (naive 4194304) blocks=19/128 q=16384 tab=516 out=16384 worst=0.00529008 +PASS sweep 64q/16kv ratio 4 ctx 256: launches=1 ctas=64 call_ms med=0.118 p90=0.127 kernel_ms med=0.106 p90=0.112 kv_staged=196608 (naive 1048576) blocks=3/16 q=16384 tab=68 out=16384 worst=0.00272721 +PASS sweep 64q/16kv ratio 4 ctx 2048: launches=1 ctas=64 call_ms med=1.045 p90=1.080 kernel_ms med=1.007 p90=1.036 kv_staged=1245184 (naive 8388608) blocks=19/128 q=16384 tab=516 out=16384 worst=0.00565958 +PASS sweep 64q/32kv ratio 2 ctx 256: launches=1 ctas=64 call_ms med=0.126 p90=0.130 kernel_ms med=0.109 p90=0.110 kv_staged=393216 (naive 2097152) blocks=3/16 q=16384 tab=68 out=16384 worst=0.00282842 +PASS sweep 64q/32kv ratio 2 ctx 2048: launches=1 ctas=64 call_ms med=1.074 p90=1.095 kernel_ms med=1.005 p90=1.029 kv_staged=2490368 (naive 16777216) blocks=19/128 q=16384 tab=516 out=16384 worst=0.00670759 +PASS sweep 64q/64kv ratio 1 ctx 256: launches=1 ctas=64 call_ms med=0.135 p90=0.138 kernel_ms med=0.108 p90=0.110 kv_staged=786432 (naive 4194304) blocks=3/16 q=16384 tab=68 out=16384 worst=0.00284045 +PASS sweep 64q/64kv ratio 1 ctx 2048: launches=1 ctas=64 call_ms med=1.181 p90=1.258 kernel_ms med=1.038 p90=1.097 kv_staged=4980736 (naive 33554432) blocks=19/128 q=16384 tab=516 out=16384 worst=0.00870193 +sweep: 72 points (64 supported, 8 unsupported by the kernel envelope), device_opens=1 +PASS: 73 checks, 0 failed diff --git a/evidence/gpu-reconv-308/T2-attention/prod-timing.json b/evidence/gpu-reconv-308/T2-attention/prod-timing.json new file mode 100644 index 000000000..43bc05bb1 --- /dev/null +++ b/evidence/gpu-reconv-308/T2-attention/prod-timing.json @@ -0,0 +1 @@ +{"schema":"OMEGA_GPU_ATTENTION_TIMING_V1","gate_median_ms":3.0,"calls":100,"cases":[{"case":"gqa_f32_tinyllama_32q4kv_ctx2048","first_call_ms":28.687,"ok_calls":100,"violations":0,"worst_scaled_err":0.00323017,"median_ms":1.0905,"p90_ms":1.1905,"min_ms":1.0819,"max_ms":1.2171,"chip_us_mean":1048.4,"device_opens":1,"pass":true}],"checks":2,"failed":0,"verdict":"OMEGA_GPU_ATTENTION_TIMING_PASS"} diff --git a/evidence/gpu-reconv-308/T2-attention/prod-timing.log b/evidence/gpu-reconv-308/T2-attention/prod-timing.log new file mode 100644 index 000000000..5e530595a --- /dev/null +++ b/evidence/gpu-reconv-308/T2-attention/prod-timing.log @@ -0,0 +1,2 @@ +PASS timing gqa_f32_tinyllama_32q4kv_ctx2048 first_call_ms=28.687 ok_calls=100/100 violations=0 worst_scaled_err=0.00323017 median_ms=1.091 p90_ms=1.190 min_ms=1.082 max_ms=1.217 chip_us_mean=1048.4 device_opens=1 +PASS: 2 checks, 0 failed diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4-0a1812f/SHA256SUMS b/evidence/gpu-reconv-308/T3/FB1-CUT4-0a1812f/SHA256SUMS new file mode 100644 index 000000000..29f5d3f47 --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4-0a1812f/SHA256SUMS @@ -0,0 +1,7 @@ +e288f7035527683470d68d24c3e1a9f36f586a6bd48cab2111b28970698e9b62 build.log +ed637a6cf8650ffa11b7a1f5d99788b3633e2bb6e3602f18d795f5048ab7db8a device.txt +4cb4b438f6c6a95280820884c30b86eb7f7021123e0dc97fcd14b6dfd5eef9f6 gpu_elementwise_test.c +bd2346fb86eed5e3be35c49c970c25adf1621043d6e4e4ba02e990375e5859c7 omega_gpu_elementwise_api.c +4d9eb1b5fd55e168f0cd4a96e906957376d88bb5efbe1f08633fcdfa52ce8fba omega_gpu_elementwise_api.h +c53232524a5c9848abb1ba937fdacafa178a615375d7d06604fadd2089e60387 receipt.json +9f014b78e0c75558bd3cb5e427eabcccf71c3852e5d38d8e86a530bc2013715d run.log diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4-0a1812f/build.log b/evidence/gpu-reconv-308/T3/FB1-CUT4-0a1812f/build.log new file mode 100644 index 000000000..7704c8048 --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4-0a1812f/build.log @@ -0,0 +1,2 @@ +ar rcs build/libomega_gpu.a build/sha256.o build/omega_canonical.o build/omega_validate.o build/omega_core.o build/omega_codec.o build/aarch64_encoder.o build/aarch64_decoder.o build/omega_realize.o build/omega_exec.o build/omega_self_host.o build/omega_verify.o build/omega_program.o build/omega_synthesis.o build/omega_library.o build/omega_discovery.o build/omega_machine.o build/omega_realize_synth.o build/omega_matvec.o build/omega_accelerator.o build/omega_accelerator_world.o build/omega_vector.o build/omega_blackwell_encoder.o build/omega_blackwell_qmd.o build/omega_blackwell_realize.o build/omega_blackwell_submit.o build/omega_blackwell_engine.o build/omega_gpu_engine.o build/omega_blackwell_matmul.o build/omega_blackwell_codegen.o build/omega_gpu_session.o build/omega_gpu_matmul_api.o build/omega_gpu_elementwise_api.o build/omega_gpu_attention_api.o build/omega_gpu_wait.o build/omega_evidence.o build/m16_native.o build/nvrm.o build/omega_program_ir.o build/omega_vc_bridge.o build/omega_resolve.o build/omega_receipt.o build/omega_blake3.o build/omega_vcstore.o +cc -std=gnu11 -Wall -Wextra -Werror -MMD -MP -D_GNU_SOURCE -O2 -Isrc -I../physics-reconv-lock/m16 -I../physics-reconv-lock/nvrm -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/src/common/sdk/nvidia/inc -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/kernel-open/common/inc -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/kernel-open/nvidia-uvm -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/src/nvidia/arch/nvalloc/unix/include -ffp-contract=off -o build/gpu_elementwise_test tests/gpu_elementwise_test.c build/libomega_gpu.a -lpthread -lm diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4-0a1812f/device.txt b/evidence/gpu-reconv-308/T3/FB1-CUT4-0a1812f/device.txt new file mode 100644 index 000000000..e426f6f47 --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4-0a1812f/device.txt @@ -0,0 +1,5 @@ +Linux spark-b87b 7.0.0-1019-nvidia #19~24.04.2-Ubuntu SMP PREEMPT_DYNAMIC Sun Sep 6 18:35:11 UTC 2 aarch64 aarch64 aarch64 GNU/Linux +NVIDIA GB10, 580.173.02 +omega 0a1812f44eaa3dcf8fe5a914d1dd65cfaccbbcd7 (1 uncommitted paths) +physics 6d7cf0d4d8eb2cda7b512100ff6058e25dbb3ddf +2026-10-05T23:15:12Z diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4-0a1812f/gpu_elementwise_test.c b/evidence/gpu-reconv-308/T3/FB1-CUT4-0a1812f/gpu_elementwise_test.c new file mode 100644 index 000000000..c274a063e --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4-0a1812f/gpu_elementwise_test.c @@ -0,0 +1,386 @@ +/* gpu_elementwise_test: FB-1 cut 4 gate for omega_gpu_elementwise_api. + * + * ./gpu_elementwise_test --host-only refusals, codegen, word fixtures, register budget, + * nvdisasm listing check when /usr/local/cuda/bin/nvdisasm exists + * ./gpu_elementwise_test --out receipt.json chip gate: every op against the host oracle, then its mutant + * (a deliberately wrong kernel) which the same check must catch + * ./gpu_elementwise_test --timing --out t.json cut 4b gate: persistent device, 100 calls each of rmsnorm 1x2048 + * and swiglu 5632 under 0.5 ms median, one device open per process + * + * Oracles re-state aien-sovereign-core crates/aien-inference-abi/src/tensor.rs (main 5fdab70) in C: + * rmsnorm sums in f64, rope computes cos/sin in f64 and casts to f32 before f32 math, swiglu is f64. + * This file is compiled with -ffp-contract=off so the f32 oracle math has no hidden fused multiply-add. + * + * Tolerances (why): EX2 1e-6 rel (PTX ex2.approx.f32: 2^-22.5 rel, we allow 2x). XCHG exact. + * RMSNORM 1e-5 rel + 1e-6 abs (f32 sum of up to 16384 squares against an f64 sum, plus RSQ seed with one + * Newton step; the mutant moves the scale by about 40 percent). ROPE exact (same f32 ops, same order). + * SWIGLU 1e-5 rel + 1e-6 abs (EX2 and RCP approximations, each about 2^-22 rel; the mutant flips the sign + * of the exponent, which is wrong by a factor of e^(2g)). + */ +#include "omega_gpu_elementwise_api.h" +#include "omega_blackwell_codegen.h" +#include "omega_gpu_session.h" +#include +#include +#include +#include +#include +#include +#include + +static int g_checks, g_failed; +#define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) + +static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } +static float frand(uint32_t *s, float lo, float hi) { return lo + (hi - lo) * ((float)(lcg(s) >> 8) / 16777216.0f); } + +/* ----------------------------------------------------------------- oracles */ +static void oracle_rmsnorm(const float *x, const float *w, float eps, float *out, size_t dim) { + double ss = 0.0; + for (size_t i = 0; i < dim; i++) ss += (double)x[i] * (double)x[i]; + double mean = ss / (double)dim; + float scale = (float)(1.0 / sqrt(mean + (double)eps)); + for (size_t i = 0; i < dim; i++) { float t = x[i] * scale; out[i] = t * w[i]; } +} +static void oracle_rope_tables(size_t head_dim, size_t pos, float theta, float *cos_half, float *sin_half) { + size_t half = head_dim / 2; + for (size_t i = 0; i < half; i++) { + double exponent = (double)(2 * i) / (double)head_dim; + double freq = 1.0 / pow((double)theta, exponent); + double rot = (double)pos * freq; + sin_half[i] = (float)sin(rot); + cos_half[i] = (float)cos(rot); + } +} +static void oracle_rope(const float *v, size_t heads, size_t head_dim, const float *cos_half, const float *sin_half, float *out) { + size_t half = head_dim / 2; + for (size_t h = 0; h < heads; h++) + for (size_t i = 0; i < half; i++) { + size_t i0 = h * head_dim + i, i1 = i0 + half; + float q0 = v[i0], q1 = v[i1], c = cos_half[i], s = sin_half[i]; + float a = q0 * c, b = q1 * s; out[i0] = a - b; + float d = q0 * s, e = q1 * c; out[i1] = d + e; + } +} +static void oracle_swiglu(const float *g, const float *u, float *out, size_t n) { + for (size_t i = 0; i < n; i++) { double gg = g[i]; double silu = gg / (1.0 + exp(-gg)); out[i] = (float)(silu * (double)u[i]); } +} + +/* |got - want| <= rel*|want| + abs; returns the number of violations and the worst scaled error */ +static size_t compare(const float *got, const float *want, size_t n, double rel, double abs_tol, double *worst) { + size_t bad = 0; *worst = 0.0; + for (size_t i = 0; i < n; i++) { + double d = fabs((double)got[i] - (double)want[i]); + double lim = rel * fabs((double)want[i]) + abs_tol; + double scaled = lim > 0 ? d / lim : (d > 0 ? INFINITY : 0.0); + if (scaled > *worst) *worst = scaled; + if (!(d <= lim)) bad++; /* NaN counts as bad */ + } + return bad; +} + +/* ------------------------------------------------------------- host only */ +static int nvdisasm_check(OmegaGpuEwOp op, const char *name, const char *const *must, int n_must) { + const char *nv = "/usr/local/cuda/bin/nvdisasm"; + if (access(nv, X_OK) != 0) { printf("nvdisasm: not present, listing check skipped for %s\n", name); return 0; } + OmegaBlackwellKernel k; memset(&k, 0, sizeof k); + if (omega_gpu_elementwise_codegen(op, NULL, 0.0f, &k) != OMEGA_GPU_EW_OK) return -1; + char bin[64], lst[64], cmd[256]; + snprintf(bin, sizeof bin, "/tmp/omega_ew_%s_%d.bin", name, (int)getpid()); + snprintf(lst, sizeof lst, "/tmp/omega_ew_%s_%d.sass", name, (int)getpid()); + FILE *f = fopen(bin, "wb"); if (!f) { free(k.code); return -1; } + fwrite(k.code, 1, k.code_size, f); fclose(f); + snprintf(cmd, sizeof cmd, "%s -b SM121 %s > %s 2>&1", nv, bin, lst); + int rc = system(cmd); + free(k.code); + FILE *l = fopen(lst, "r"); if (!l) return -1; + char line[512]; int seen[16] = {0}; int bad = 0, insns = 0; + while (fgets(line, sizeof line, l)) { + if (!strstr(line, "/*0")) continue; + insns++; + if (strstr(line, "?") || strstr(line, "error")) bad++; + for (int i = 0; i < n_must; i++) if (strstr(line, must[i])) seen[i] = 1; + } + fclose(l); + int miss = 0; + for (int i = 0; i < n_must; i++) if (!seen[i]) { printf(" %s: nvdisasm listing lacks %s\n", name, must[i]); miss++; } + printf("nvdisasm %s: rc=%d insns=%d undecodable=%d missing=%d (%s)\n", name, rc, insns, bad, miss, lst); + unlink(bin); + return (rc == 0 && bad == 0 && miss == 0) ? 0 : -1; +} + +static void host_only(void) { + CHECK(omega_blackwell_verify_codegen_fixtures_fb1cut4() == 0, "word fixtures MUFU_EX2 / BAR_SYNC / LDS32 / STS32 (rc %d)", omega_blackwell_verify_codegen_fixtures_fb1cut4()); + float a[256] = {0}, b[256] = {0}, c[256] = {0}; + CHECK(omega_gpu_rmsnorm_f32(0, 128, a, b, 1e-5f, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "rows=0 refused"); + CHECK(omega_gpu_rmsnorm_f32(1, 100, a, b, 1e-5f, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "dim not a multiple of 128 refused"); + CHECK(omega_gpu_rmsnorm_f32(1, OMEGA_GPU_EW_MAX_DIM + 128, a, b, 1e-5f, c, NULL) == OMEGA_GPU_EW_TOO_LARGE, "dim over limit refused"); + CHECK(omega_gpu_rope_f32(1, 63, a, b, b, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "odd head_dim refused"); + CHECK(omega_gpu_rope_f32(1, 64, NULL, b, b, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "NULL vector refused"); + CHECK(omega_gpu_swiglu_f32(0, a, b, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "n=0 refused"); + CHECK(omega_gpu_shared_xchg_u32(100, (uint32_t *)a, (uint32_t *)c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "xchg n not a multiple of 128 refused"); + CHECK(strcmp(omega_gpu_elementwise_rc_name(OMEGA_GPU_EW_UNWRITTEN), "UNWRITTEN") == 0, "rc name"); + + struct { OmegaGpuEwOp op; const char *name; const char *must[6]; int n; } ops[] = { + { OMEGA_GPU_EW_EX2, "ex2", { "MUFU.EX2", "@P0 EXIT", "STG.E" }, 3 }, + { OMEGA_GPU_EW_XCHG, "xchg", { "STS [R", "BAR.SYNC", "LDS R" }, 3 }, + { OMEGA_GPU_EW_RMSNORM, "rmsnorm", { "SHFL.DOWN", "BAR.SYNC", "MUFU.RSQ", "@!P0 BRA", "STS [R", "LDS R" }, 6 }, + { OMEGA_GPU_EW_ROPE, "rope", { "FADD", "FMUL", "STG.E" }, 3 }, + { OMEGA_GPU_EW_SWIGLU, "swiglu", { "MUFU.EX2", "MUFU.RCP", "@P0 EXIT" }, 3 }, + }; + for (size_t i = 0; i < sizeof ops / sizeof ops[0]; i++) { + OmegaBlackwellKernel k; memset(&k, 0, sizeof k); + int rc = omega_gpu_elementwise_codegen(ops[i].op, NULL, 0.0f, &k); + CHECK(rc == OMEGA_GPU_EW_OK && k.code_size > 0 && k.gpr_count <= 64, "%s kernel generated (rc %s, %zu insns, %u gprs)", ops[i].name, omega_gpu_elementwise_rc_name(rc), k.insn_count, k.gpr_count); + printf("codegen %s: %zu insns, %u gprs, %u ugprs\n", ops[i].name, k.insn_count, k.gpr_count, k.uniform_gpr_count); + uint8_t digest[32]; memcpy(digest, k.code_digest, 32); + free(k.code); + /* the mutant must be a different kernel */ + omega_gpu_elementwise_test_set_mutant((int)ops[i].op); + memset(&k, 0, sizeof k); + CHECK(omega_gpu_elementwise_codegen(ops[i].op, NULL, 0.0f, &k) == OMEGA_GPU_EW_OK && memcmp(digest, k.code_digest, 32) != 0, "%s mutant kernel differs", ops[i].name); + free(k.code); + omega_gpu_elementwise_test_set_mutant(0); + CHECK(nvdisasm_check(ops[i].op, ops[i].name, ops[i].must, ops[i].n) == 0, "%s nvdisasm listing decodes and shows the expected instructions", ops[i].name); + } +} + +/* ------------------------------------------------------------- chip gate */ +typedef struct { const char *name; size_t n; int rc; size_t bad; double worst; int pass; int mutant_rc; size_t mutant_bad; int mutant_caught; uint64_t chip_ns; uint32_t calls; uint32_t unwritten; } Case; +static Case g_cases[16]; static int g_ncases; + +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_elementwise_rc_name(c.rc), c.bad, c.worst); + CHECK(c.mutant_caught, "%s mutant caught (rc=%s violations=%zu)", c.name, omega_gpu_elementwise_rc_name(c.mutant_rc), c.mutant_bad); + printf("%s %s n=%zu rc=%s violations=%zu worst_scaled_err=%g chip_ns=%" PRIu64 " calls=%u unwritten=%u | mutant rc=%s violations=%zu %s\n", + c.pass && c.mutant_caught ? "PASS" : "FAIL", c.name, c.n, omega_gpu_elementwise_rc_name(c.rc), c.bad, c.worst, c.chip_ns, c.calls, c.unwritten, + omega_gpu_elementwise_rc_name(c.mutant_rc), c.mutant_bad, c.mutant_caught ? "CAUGHT" : "NOT CAUGHT"); + if (out) fprintf(out, "%s{\"case\":\"%s\",\"n\":%zu,\"rc\":\"%s\",\"violations\":%zu,\"worst_scaled_err\":%g,\"pass\":%s,\"chip_elapsed_ns\":%" PRIu64 ",\"chip_calls\":%u,\"unwritten_words\":%u,\"mutant_rc\":\"%s\",\"mutant_violations\":%zu,\"mutant_caught\":%s}", + g_ncases > 1 ? "," : "", c.name, c.n, omega_gpu_elementwise_rc_name(c.rc), c.bad, c.worst, c.pass ? "true" : "false", c.chip_ns, c.calls, c.unwritten, + omega_gpu_elementwise_rc_name(c.mutant_rc), c.mutant_bad, c.mutant_caught ? "true" : "false"); +} + +static int chip(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; } + if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_ELEMENTWISE_V1\",\"tolerances\":{\"ex2_rel\":1e-6,\"xchg\":\"exact\",\"rmsnorm_rel\":1e-5,\"rmsnorm_abs\":1e-6,\"rope\":\"exact\",\"swiglu_rel\":1e-5,\"swiglu_abs\":1e-6},\"cases\":["); + uint32_t seed = 0xfb1c0004u; + OmegaGpuEwInfo info; + + /* 1. EX2 probe, with a negative control on the comparison itself */ + { + size_t n = 4096; float *x = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4), *wrong = malloc(n * 4); + for (size_t i = 0; i < n; i++) { x[i] = frand(&seed, -20.0f, 20.0f); want[i] = (float)exp2((double)x[i]); wrong[i] = (float)exp2((double)x[i] + 1.0); } + Case c = { .name = "ex2_probe", .n = n }; + c.rc = omega_gpu_ex2_f32((uint32_t)n, x, got, &info); + c.bad = compare(got, want, n, 1e-6, 0.0, &c.worst); + double w2; size_t neg = compare(got, wrong, n, 1e-6, 0.0, &w2); + CHECK(neg > n / 2, "ex2 negative control: the check rejects a wrong expectation (%zu of %zu flagged)", neg, n); + c.pass = c.rc == OMEGA_GPU_EW_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; + omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_EX2); + c.mutant_rc = omega_gpu_ex2_f32((uint32_t)n, x, got, &info); + c.mutant_bad = compare(got, want, n, 1e-6, 0.0, &w2); + c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0; + omega_gpu_elementwise_test_set_mutant(0); + record(c, out); free(x); free(got); free(want); free(wrong); + } + /* 2. shared-memory exchange probe (STS, BAR.SYNC, LDS) */ + { + size_t n = 4096; uint32_t *x = malloc(n * 4), *got = malloc(n * 4); + for (size_t i = 0; i < n; i++) x[i] = lcg(&seed); + Case c = { .name = "shared_xchg_probe", .n = n }; + c.rc = omega_gpu_shared_xchg_u32((uint32_t)n, x, got, &info); + for (size_t i = 0; i < n; i++) if (got[i] != x[i ^ (OMEGA_GPU_EW_THREADS - 1)]) c.bad++; + c.pass = c.rc == OMEGA_GPU_EW_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; + omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_XCHG); + c.mutant_rc = omega_gpu_shared_xchg_u32((uint32_t)n, x, got, &info); + for (size_t i = 0; i < n; i++) if (got[i] != x[i ^ (OMEGA_GPU_EW_THREADS - 1)]) c.mutant_bad++; + c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0; + omega_gpu_elementwise_test_set_mutant(0); + record(c, out); free(x); free(got); + } + /* 3. rmsnorm at the model hidden sizes (TinyLlama and Llama-3.2-1B: 2048), plus small and large */ + { size_t dims[3] = { 2048, 256, 8192 }; size_t rowss[3] = { 4, 1, 2 }; + for (int t = 0; t < 3; t++) { + size_t dim = dims[t], rows = rowss[t], n = rows * dim; float eps = 1e-5f; + float *x = malloc(n * 4), *w = malloc(dim * 4), *got = malloc(n * 4), *want = malloc(n * 4), *again = malloc(n * 4); + for (size_t i = 0; i < n; i++) x[i] = frand(&seed, -3.0f, 3.0f); + for (size_t i = 0; i < dim; i++) w[i] = frand(&seed, 0.5f, 1.5f); + for (size_t r = 0; r < rows; r++) oracle_rmsnorm(x + r * dim, w, eps, want + r * dim, dim); + char name[64]; snprintf(name, sizeof name, "rmsnorm_%zux%zu", rows, dim); + Case c = { .name = strdup(name), .n = n }; + c.rc = omega_gpu_rmsnorm_f32((uint32_t)rows, (uint32_t)dim, x, w, eps, got, &info); + c.bad = compare(got, want, n, 1e-5, 1e-6, &c.worst); + c.pass = c.rc == OMEGA_GPU_EW_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; + OmegaGpuEwInfo i2; int rc2 = omega_gpu_rmsnorm_f32((uint32_t)rows, (uint32_t)dim, x, w, eps, again, &i2); + CHECK(rc2 == OMEGA_GPU_EW_OK && i2.kernel_cache_hit && memcmp(got, again, n * 4) == 0, "%s repeat run (cache hit) is bit-identical", name); + omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_RMSNORM); + double w2; c.mutant_rc = omega_gpu_rmsnorm_f32((uint32_t)rows, (uint32_t)dim, x, w, eps, got, &info); + c.mutant_bad = compare(got, want, n, 1e-5, 1e-6, &w2); + c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0; + omega_gpu_elementwise_test_set_mutant(0); + record(c, out); free(x); free(w); free(got); free(want); free(again); + } } + /* 4. rope: TinyLlama (32 q heads, 4 kv heads, head_dim 64, theta 10000) and Llama-3.2-1B (32/8, 64, 500000) */ + { struct { size_t heads, hd, pos; float theta; } rs[4] = { { 32, 64, 17, 10000.0f }, { 4, 64, 17, 10000.0f }, { 32, 64, 1023, 500000.0f }, { 8, 64, 1023, 500000.0f } }; + for (int t = 0; t < 4; t++) { + size_t heads = rs[t].heads, hd = rs[t].hd, n = heads * hd, half = hd / 2; + float *v = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4), *ch = malloc(half * 4), *sh = malloc(half * 4); + for (size_t i = 0; i < n; i++) v[i] = frand(&seed, -4.0f, 4.0f); + oracle_rope_tables(hd, rs[t].pos, rs[t].theta, ch, sh); + oracle_rope(v, heads, hd, ch, sh, want); + char name[64]; snprintf(name, sizeof name, "rope_%zuh_%zud_pos%zu_theta%g", heads, hd, rs[t].pos, rs[t].theta); + Case c = { .name = strdup(name), .n = n }; + c.rc = omega_gpu_rope_f32((uint32_t)heads, (uint32_t)hd, v, ch, sh, got, &info); + c.bad = compare(got, want, n, 0.0, 0.0, &c.worst); + c.pass = c.rc == OMEGA_GPU_EW_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; + omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_ROPE); + double w2; c.mutant_rc = omega_gpu_rope_f32((uint32_t)heads, (uint32_t)hd, v, ch, sh, got, &info); + c.mutant_bad = compare(got, want, n, 0.0, 0.0, &w2); + c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0; + omega_gpu_elementwise_test_set_mutant(0); + record(c, out); free(v); free(got); free(want); free(ch); free(sh); + } } + /* 5. swiglu at the model ffn sizes (TinyLlama 5632, Llama-3.2-1B 8192) and an odd tail */ + { size_t ns[3] = { 5632, 8192, 1000 }; + for (int t = 0; t < 3; t++) { + size_t n = ns[t]; + float *g = malloc(n * 4), *u = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4); + for (size_t i = 0; i < n; i++) { g[i] = frand(&seed, -12.0f, 12.0f); u[i] = frand(&seed, -2.0f, 2.0f); } + oracle_swiglu(g, u, want, n); + char name[64]; snprintf(name, sizeof name, "swiglu_%zu", n); + Case c = { .name = strdup(name), .n = n }; + c.rc = omega_gpu_swiglu_f32((uint32_t)n, g, u, got, &info); + c.bad = compare(got, want, n, 1e-5, 1e-6, &c.worst); + c.pass = c.rc == OMEGA_GPU_EW_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; + omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_SWIGLU); + double w2; c.mutant_rc = omega_gpu_swiglu_f32((uint32_t)n, g, u, got, &info); + c.mutant_bad = compare(got, want, n, 1e-5, 1e-6, &w2); + c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0; + omega_gpu_elementwise_test_set_mutant(0); + record(c, out); free(g); free(u); free(got); free(want); + } } + /* 6. stale-SM-instruction-cache guard (cut 1b finding, omega #256): 10 distinct kernels + * (5 ops + 5 mutants) in rotation, two rounds. Every launch uploads code into a freshly + * allocated page, so a kernel routinely lands on the code address its predecessor used, + * and 10 keys evict the 8-slot kernel cache. Each clean launch must match the oracle and + * each mutant must still be caught; a stale icache would make a mutant pass or a clean + * kernel fail. Red was observed by cut 1b on matmul before its invalidate (receipt + * FB1-CUT1B-f629798); this build carries the invalidate, so the case is a guard. */ + { + enum { RN = 256, RH = 4, RD = 64 }; + float *x = malloc(RN * 4), *w = malloc(RN * 4), *u = malloc(RN * 4), *got = malloc(RN * 4), *want = malloc(RN * 4); + float *ch = malloc(RD / 2 * 4), *sh = malloc(RD / 2 * 4); + uint32_t *xu = malloc(RN * 4), *gu = malloc(RN * 4); + for (size_t i = 0; i < RN; i++) { x[i] = frand(&seed, -3.0f, 3.0f); w[i] = frand(&seed, 0.5f, 1.5f); u[i] = frand(&seed, -2.0f, 2.0f); xu[i] = lcg(&seed); } + oracle_rope_tables(RD, 17, 10000.0f, ch, sh); + Case c = { .name = "icache_reuse_rotation", .n = 20 }; + c.rc = OMEGA_GPU_EW_OK; c.mutant_rc = OMEGA_GPU_EW_OK; c.mutant_caught = 1; + const int order[10] = { OMEGA_GPU_EW_EX2, -OMEGA_GPU_EW_SWIGLU, OMEGA_GPU_EW_XCHG, -OMEGA_GPU_EW_EX2, OMEGA_GPU_EW_RMSNORM, + -OMEGA_GPU_EW_XCHG, OMEGA_GPU_EW_SWIGLU, -OMEGA_GPU_EW_ROPE, OMEGA_GPU_EW_ROPE, -OMEGA_GPU_EW_RMSNORM }; + for (int step = 0; step < 20; step++) { + int round = step / 10, k = step % 10; + int mut = order[k] < 0, op = mut ? -order[k] : order[k]; + omega_gpu_elementwise_test_set_mutant(mut ? op : 0); + int rc; size_t bad = 0; double wst; + switch (op) { + case OMEGA_GPU_EW_EX2: + for (size_t i = 0; i < RN; i++) want[i] = (float)exp2((double)x[i]); + rc = omega_gpu_ex2_f32(RN, x, got, &info); bad = compare(got, want, RN, 1e-6, 0.0, &wst); break; + case OMEGA_GPU_EW_XCHG: + rc = omega_gpu_shared_xchg_u32(RN, xu, gu, &info); + for (size_t i = 0; i < RN; i++) { if (gu[i] != xu[i ^ (OMEGA_GPU_EW_THREADS - 1)]) bad++; } + break; + case OMEGA_GPU_EW_RMSNORM: + oracle_rmsnorm(x, w, 1e-5f, want, RN); + rc = omega_gpu_rmsnorm_f32(1, RN, x, w, 1e-5f, got, &info); bad = compare(got, want, RN, 1e-5, 1e-6, &wst); break; + case OMEGA_GPU_EW_ROPE: + oracle_rope(x, RH, RD, ch, sh, want); + rc = omega_gpu_rope_f32(RH, RD, x, ch, sh, got, &info); bad = compare(got, want, RH * RD, 0.0, 0.0, &wst); break; + default: + oracle_swiglu(x, u, want, RN); + rc = omega_gpu_swiglu_f32(RN, x, u, got, &info); bad = compare(got, want, RN, 1e-5, 1e-6, &wst); break; + } + omega_gpu_elementwise_test_set_mutant(0); + int ok = rc == OMEGA_GPU_EW_OK && bad == 0; + if (!mut && !ok) { c.bad++; if (rc != OMEGA_GPU_EW_OK) c.rc = rc; printf(" rotation %d/%d: clean op %d FAILED rc=%s violations=%zu\n", round, k, op, omega_gpu_elementwise_rc_name(rc), bad); } + if (mut && ok) { c.mutant_bad++; c.mutant_caught = 0; printf(" rotation %d/%d: mutant op %d NOT caught\n", round, k, op); } + c.chip_ns += info.elapsed_ns; c.calls += info.chip_calls; c.unwritten += info.unwritten_words; + } + c.pass = c.bad == 0 && c.rc == OMEGA_GPU_EW_OK; + record(c, out); + free(x); free(w); free(u); free(got); free(want); free(ch); free(sh); free(xu); free(gu); + } + /* cut 4b: the whole battery (clean and mutant kernels, cache clears) ran on ONE device open */ + CHECK(omega_gpu_session_open_count() == 1, "device opened once for the battery (opens=%u)", omega_gpu_session_open_count()); + printf("device_opens=%u\n", omega_gpu_session_open_count()); + if (out) { + 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_ELEMENTWISE_PASS" : "OMEGA_GPU_ELEMENTWISE_FAIL"); + fclose(out); + } + return 0; +} + +/* ------------------------------------------------------------ timing gate (cut 4b) */ +/* The persistent device: 100 calls each of rmsnorm 1x2048 and swiglu 5632 (the TinyLlama + * decode shapes), every call checked against the oracle, median wall time per call under + * 0.5 ms (cut 4 opened and closed the device per call: ~40 ms). The device must have been + * opened exactly once in this process. */ +static int cmp_d(const void *a, const void *b) { double x = *(const double *)a, y = *(const double *)b; return x < y ? -1 : x > y; } +static double now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; } +static int timing(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; } + if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_ELEMENTWISE_TIMING_V1\",\"gate_median_ms\":0.5,\"calls\":100,\"cases\":["); + uint32_t seed = 0xfb1c004bu; + enum { N = 100 }; + for (int t = 0; t < 2; t++) { + const char *name = t == 0 ? "rmsnorm_1x2048" : "swiglu_5632"; + size_t n = t == 0 ? 2048 : 5632; + float *a = malloc(n * 4), *b = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4); + for (size_t i = 0; i < n; i++) { a[i] = frand(&seed, -3.0f, 3.0f); b[i] = t == 0 ? frand(&seed, 0.5f, 1.5f) : frand(&seed, -2.0f, 2.0f); } + if (t == 0) oracle_rmsnorm(a, b, 1e-5f, want, n); else oracle_swiglu(a, b, want, n); + double samples[N]; int ok_calls = 0; size_t bad_total = 0; double worst = 0; uint64_t chip_sum = 0; int rc_last = 0; + OmegaGpuEwInfo info; + /* warm-up: first call opens the device and generates the kernel (not timed) */ + int rc0 = t == 0 ? omega_gpu_rmsnorm_f32(1, (uint32_t)n, a, b, 1e-5f, got, &info) : omega_gpu_swiglu_f32((uint32_t)n, a, b, got, &info); + double first_ms = info.call_ns / 1e6; + CHECK(rc0 == OMEGA_GPU_EW_OK, "%s first call rc=%s err=\"%s\"", name, omega_gpu_elementwise_rc_name(rc0), omega_gpu_elementwise_last_error()); + for (int i = 0; i < N; i++) { + double t0 = now_ms(); + int rc = t == 0 ? omega_gpu_rmsnorm_f32(1, (uint32_t)n, a, b, 1e-5f, got, &info) : omega_gpu_swiglu_f32((uint32_t)n, a, b, got, &info); + samples[i] = now_ms() - t0; + double w; size_t bad = compare(got, want, n, 1e-5, 1e-6, &w); + if (w > worst) worst = w; + bad_total += bad; chip_sum += info.elapsed_ns; rc_last = rc; + if (rc == OMEGA_GPU_EW_OK && bad == 0) ok_calls++; + } + qsort(samples, N, sizeof samples[0], cmp_d); + double median = samples[N / 2], p90 = samples[N * 9 / 10], mn = samples[0], mx = samples[N - 1]; + uint32_t opens = omega_gpu_session_open_count(); + int pass = ok_calls == N && bad_total == 0 && median < 0.5 && opens == 1; + CHECK(pass, "timing %s ok_calls=%d/%d violations=%zu median_ms=%.3f opens=%u last_rc=%s err=\"%s\"", name, ok_calls, N, bad_total, median, opens, omega_gpu_elementwise_rc_name(rc_last), omega_gpu_elementwise_last_error()); + printf("%s timing %s first_call_ms=%.3f ok_calls=%d/%d violations=%zu worst_scaled_err=%g median_ms=%.3f p90_ms=%.3f min_ms=%.3f max_ms=%.3f chip_us_mean=%.1f device_opens=%u\n", + pass ? "PASS" : "FAIL", name, first_ms, ok_calls, N, bad_total, worst, median, p90, mn, mx, chip_sum / 1e3 / N, opens); + if (out) fprintf(out, "%s{\"case\":\"%s\",\"first_call_ms\":%.3f,\"ok_calls\":%d,\"violations\":%zu,\"worst_scaled_err\":%g,\"median_ms\":%.4f,\"p90_ms\":%.4f,\"min_ms\":%.4f,\"max_ms\":%.4f,\"chip_us_mean\":%.1f,\"device_opens\":%u,\"pass\":%s}", + t ? "," : "", name, first_ms, ok_calls, bad_total, worst, median, p90, mn, mx, chip_sum / 1e3 / N, opens, pass ? "true" : "false"); + free(a); free(b); free(got); free(want); + } + if (out) { fprintf(out, "],\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", g_checks, g_failed, g_failed == 0 ? "OMEGA_GPU_ELEMENTWISE_TIMING_PASS" : "OMEGA_GPU_ELEMENTWISE_TIMING_FAIL"); fclose(out); } + return 0; +} + +int main(int argc, char **argv) { + const char *out_path = NULL; int host = 0, timing_mode = 0; + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "--host-only") == 0) host = 1; + else if (strcmp(argv[i], "--timing") == 0) timing_mode = 1; + else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i]; + } + if (host) host_only(); + else if (timing_mode) { if (timing(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/evidence/gpu-reconv-308/T3/FB1-CUT4-0a1812f/omega_gpu_elementwise_api.c b/evidence/gpu-reconv-308/T3/FB1-CUT4-0a1812f/omega_gpu_elementwise_api.c new file mode 100644 index 000000000..b16c70205 --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4-0a1812f/omega_gpu_elementwise_api.c @@ -0,0 +1,1139 @@ +/* omega_gpu_elementwise_api.c: FB-1 cut 4 (2026-10-04). See the header. + * + * Layout of this file: (1) kernel codegen on top of the BlackwellIR, with a + * small scheduler that writes every control word; (2) one launcher cloned from + * the chip-proven tensor matmul path (omega_blackwell_execute_matmul_tensor); + * (3) the public entry points, which split work into <= OMEGA_GPU_EW_MAX_CTAS + * launches and run the host-side bookkeeping. + * + * Scheduling rules (chip-learned, src/omega_numeric.c omega_numeric_check_patch): + * - a variable-latency result (LDG, LDS, LDC, LDCU, S2R, SHFL, MUFU) arrives through + * a scoreboard; the producer stalls >= 2 and the consumer waits on that SB; + * - a fixed-latency result (FADD FMUL FFMA IMAD IADD3 MOV LOP3 SHF) has no + * scoreboard: the producer stalls >= 5 before its consumer (we use 6); + * - a predicate producer (ISETP) stalls 14 before @P0 use (NAK SM120 padding); + * - stores (STG, STS) set a read barrier so the next instruction cannot overwrite + * the source register early. + * The emitter below serialises: every instruction waits on everything still + * pending. A final local pass tightens supported adjacent ALU pairs to 5 cycles. + */ +#include "omega_gpu_elementwise_api.h" +#include "omega_blackwell_codegen.h" +#include "omega_bw_reconv.h" +#include "omega_blackwell_qmd.h" +#include "omega_blackwell_submit.h" +#include "omega_gpu_session.h" +#include "sha256.h" +#include +#include +#include +#include + +/* ------------------------------------------------------------------ codegen */ + +#define CW(stall, wbar, rbar, wait) \ + (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) +#define CW_YIELD 0x2000u +#define SB_NONE 7u +#define SB_GPR 0u /* variable-latency GPR results */ +#define SB_RD 1u /* store source-register read barrier */ +#define SB_UGPR 2u /* uniform-register results (LDCU) */ + +enum { K_FIXED, K_PRED, K_VAR, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC }; + +#define EW_MAX_LOOPS 4 +typedef struct { + BlackwellIRProgram *p; + uint32_t pending; /* scoreboards set and not yet waited on */ + int err; + int loop_start[EW_MAX_LOOPS], loop_end[EW_MAX_LOOPS]; + int n_loops; + int open_start[EW_MAX_LOOPS]; /* starts of loops begun and not yet ended (nesting stack) */ + int depth; + BwRegions rg; /* structured reconvergence regions (omega #308) */ + int rg_mutant; /* test only: 1 = drop the BSYNC at join, 2 = corrupt the BSSY target (host sim must catch both) */ + int exited; /* a predicated EXIT was emitted: some lanes of a warp may be gone */ + int exit_sync_ok; /* test only: lets a kernel put SHFL/BAR after a predicated EXIT (the negative control R6) */ + int test_path; /* building for omega_gpu_elementwise_codegen_ir (host tests), never for a launch */ +} Em; + +static void emit(Em *e, BlackwellIRInsn in, int kind) { + uint32_t wait = e->pending; + switch (kind) { + case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; + case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break; + case K_VAR: in.control = CW(4, SB_GPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_GPR; break; + case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; + case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; + case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; + /* a predicated EXIT or BRA that falls through must not be the only waiter */ + case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; + case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; + /* BSSY / BSYNC read no registers; the stall counts (1 and 5, both with yield) are the ones + * nvcc 13.0.88 writes for sm_121 (docs/gpu-reconvergence-308.md). They wait on everything + * pending so that, as for every branch here, nothing is in flight across a control edge + * and the straight-line scoreboard walk in the tests stays valid. */ + case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; + case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; + default: e->err = -1; return; + } + if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; /* append returns the index */ +} + +static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; } +static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; } +static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; } +static int here(const Em *e) { return (int)e->p->count; } + +#define NOREG -1 +static BlackwellIRInsn I0(BlackwellIROpcode op) { + BlackwellIRInsn in; memset(&in, 0, sizeof in); + in.op = op; in.dst_vreg = NOREG; in.src1_vreg = NOREG; in.src2_vreg = NOREG; in.src3_vreg = NOREG; in.ureg = NOREG; + return in; +} +static void ldcu64(Em *e, int udst, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDCU64); i.dst_vreg = udst; i.imm = off; i.is_uniform = true; emit(e, i, K_UVAR); } +static void ldc64(Em *e, int dst64, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC64); i.dst_vreg = dst64; i.imm = off; emit(e, i, K_VAR); } +static void ldc32(Em *e, int dst, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC); i.dst_vreg = dst; i.imm = off; emit(e, i, K_VAR); } +static void s2r(Em *e, int dst, uint32_t sr) { BlackwellIRInsn i = I0(BW_IR_S2R); i.dst_vreg = dst; i.imm = sr; emit(e, i, K_VAR); } +static void movi(Em *e, int dst, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_MOV_IMM); i.dst_vreg = dst; i.imm = imm; emit(e, i, K_FIXED); } +static void movf(Em *e, int dst, float f) { uint32_t u; memcpy(&u, &f, 4); movi(e, dst, u); } +static void movrz(Em *e, int dst) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = dst; emit(e, i, K_FIXED); } +/* dst = a * imm + c (c may be NOREG for RZ) */ +static void imadi(Em *e, int dst, int a, uint32_t imm, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = dst; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c; emit(e, i, K_FIXED); } +/* dst = a * b + c (registers) */ +static void imadr(Em *e, int dst, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = dst; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } +static void iadd3(Em *e, int dst, int a, int b) { BlackwellIRInsn i = I0(BW_IR_IADD3); i.dst_vreg = dst; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void lop3xor(Em *e, int dst, int a, int b) { BlackwellIRInsn i = I0(BW_IR_LOP3_XOR); i.dst_vreg = dst; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +/* dst64 = idx * 4 + ptr64 (byte address of element idx) */ +static void addr4(Em *e, int dst64, int idx, int ptr64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = dst64; i.src1_vreg = idx; i.imm = 4; i.src3_vreg = ptr64; emit(e, i, K_FIXED); } +static void ldg(Em *e, int dst, int addr64, int udesc) { BlackwellIRInsn i = I0(BW_IR_LDG_E); i.dst_vreg = dst; i.src1_vreg = addr64; i.ureg = udesc; emit(e, i, K_VAR); } +/* Independent loads (LDG_E from global, or LDS32 from shared memory with ureg NOREG) issued + * back to back (campaign C2a, 2026-10-05). The first load + * waits on whatever is pending; the others wait on nothing, so all n are in flight together. + * Every load sets SB_GPR (a counting scoreboard), so the next instruction waits once for all + * of them instead of once per load. Fails the build if a load address is a destination of the + * group or a destination repeats (the later loads would read an operand still in flight). */ +static void ld_group(Em *e, BlackwellIROpcode op, int n, const int *dst, const int *addr, int ureg) { + for (int i = 0; i < n; i++) + for (int k = 0; k < n; k++) + if (addr[i] == dst[k] || (k != i && dst[i] == dst[k])) { e->err = -1; return; } + for (int i = 0; i < n; i++) { + if (op != BW_IR_LDG_E && op != BW_IR_LDS32) { e->err = -1; return; } + BlackwellIRInsn in = I0(op); in.dst_vreg = dst[i]; in.src1_vreg = addr[i]; in.ureg = ureg; + if (i == 0) { emit(e, in, K_VAR); continue; } + in.control = CW(4, SB_GPR, SB_NONE, 0) | CW_YIELD; /* nonzero, so the encoder keeps it */ + if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; + } +} +static void stg(Em *e, int addr64, int val, int udesc) { BlackwellIRInsn i = I0(BW_IR_STG_E); i.src1_vreg = addr64; i.src2_vreg = val; i.ureg = udesc; emit(e, i, K_STORE); } +static void fadd(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FADD); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void fsub(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FSUB); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void fmul(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FMUL); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void ffma(Em *e, int d, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_FFMA); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } +static void mufu(Em *e, BlackwellIROpcode op, int d, int a) { BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = a; emit(e, i, K_VAR); } +/* Cross-lane and warp-ending ops are refused inside an open reconvergence region (omega #308): + * a SHFL or BAR.SYNC reached by a split warp reads inactive lanes / counts a partial warp + * (undefined; the 2026-10-04 attention bug), and a lane that EXITs inside a region can no + * longer be joined. The build fails closed instead of emitting such code. */ +/* Cross-lane instructions (SHFL, BAR.SYNC) need the whole warp present. The emitter refuses + * them (fails the build) inside an open reconvergence region, where lanes may be on different + * paths, and after a predicated EXIT, where lanes may be gone for good: a SHFL then reads + * inactive lanes (undefined on the chip, the attention failure of 2026-10-04). A kernel that + * must drop lanes before a cross-lane step clamps them instead and exits them afterwards. */ +static int warp_split(Em *e) { if (e->rg.depth > 0 || (e->exited && !e->exit_sync_ok)) { e->err = -1; return 1; } return 0; } +static void shfl_down(Em *e, int d, int a, uint32_t off) { if (warp_split(e)) return; BlackwellIRInsn i = I0(BW_IR_SHFL_DOWN); i.dst_vreg = d; i.src1_vreg = a; i.imm = off; emit(e, i, K_VAR); } +static void sts32(Em *e, int addr, int val) { BlackwellIRInsn i = I0(BW_IR_STS32); i.src1_vreg = addr; i.src2_vreg = val; emit(e, i, K_STORE); } +static void lds32(Em *e, int d, int addr) { BlackwellIRInsn i = I0(BW_IR_LDS32); i.dst_vreg = d; i.src1_vreg = addr; emit(e, i, K_VAR); } +static void bar_sync(Em *e) { if (warp_split(e)) return; emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } +static void isetp_ge_u32(Em *e, int a, int b) { BlackwellIRInsn i = I0(BW_IR_ISETP_GE_U32); i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_PRED); } +/* An EXIT inside a region would leave the barrier waiting for a lane that never comes: refused. */ +static void exit_if_p0(Em *e) { if (e->rg.depth > 0) { e->err = -1; return; } e->exited = 1; BlackwellIRInsn i = I0(BW_IR_EXIT); i.predicate_p0 = true; emit(e, i, K_EXIT); } +static void bra_back_if_not_p0(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.predicate_not = true; i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } +static void tail(Em *e) { + emit(e, I0(BW_IR_EXIT), K_EXIT); + BlackwellIRInsn self = I0(BW_IR_BRA); /* the self-branch every kernel here ends with */ + self.control = 0x000fc000u; + if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1; +} +/* Loops may nest (prime race cut): begin pushes the start, end pops it, so the inner + * loop is closed (and its liveness fixed) first. For the single-level loops above this + * is exactly the old behaviour. */ +static void loop_begin(Em *e) { if (e->depth >= EW_MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); } +/* Closing a loop: every value defined before the loop and read inside it stays + * live until the back-branch, otherwise the linear-scan allocator (which knows + * nothing about back-edges) would hand its register to a body temporary whose + * first definition comes after the value's last textual use (e.g. the scale y + * in rmsnorm pass 2), and the next iteration would read garbage. */ +static void loop_end(Em *e) { + if (e->n_loops >= EW_MAX_LOOPS || e->depth <= 0) { e->err = -1; return; } + int start = e->open_start[--e->depth], end = here(e) - 1; + e->loop_start[e->n_loops] = start; + e->loop_end[e->n_loops] = end; + e->n_loops++; + OmegaRegAlloc *ra = &e->p->regalloc; + for (int v = 0; v < ra->num_vregs; v++) { + OmegaLiveInterval *iv = &ra->intervals[v]; + if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end; + } +} + +/* Kernel argument words (constant bank 0, same slots as the matmul path): + * 0x380 a, 0x388 b, 0x390 c (64-bit each), then four 32-bit parameters. */ +#define ARG_A 0x380u +#define ARG_B 0x388u +#define ARG_C 0x390u +#define ARG_P0 0x398u +#define ARG_P1 0x39cu +#define ARG_P2 0x3a0u +#define ARG_P3 0x3a4u +#define ARG_P4 0x3a8u +#define DESC_OFF 0x358u + +typedef struct { int udesc, pa, pb, pc, tid, cta; } Pro; +static Pro prologue(Em *e) { + Pro r; + r.udesc = UV64(e); r.pa = V64(e); r.pb = V64(e); r.pc = V64(e); r.tid = V(e); r.cta = V(e); + ldcu64(e, r.udesc, DESC_OFF); + ldc64(e, r.pa, ARG_A); ldc64(e, r.pb, ARG_B); ldc64(e, r.pc, ARG_C); + s2r(e, r.tid, BW_SR_TID_X); s2r(e, r.cta, BW_SR_CTAID_X); + return r; +} + +/* out[i] = 2^a[i] for i < n (n = P0). Threads 128, grid ceil(n/128). Mutant: 2^(a+1). */ +static void gen_ex2(Em *e, int mutant) { + Pro p = prologue(e); + int n = V(e), i = V(e), off = V64(e), x = V(e), y = V(e); + ldc32(e, n, ARG_P0); + imadi(e, i, p.cta, OMEGA_GPU_EW_THREADS, p.tid); + isetp_ge_u32(e, i, n); + exit_if_p0(e); + addr4(e, off, i, p.pa); + ldg(e, x, off, p.udesc); + if (mutant) { int one = V(e); movf(e, one, 1.0f); fadd(e, x, x, one); } + mufu(e, BW_IR_MUFU_EX2, y, x); + addr4(e, off, i, p.pc); + stg(e, off, y, p.udesc); + tail(e); +} + +/* out[i] = a[i ^ 127] within each CTA of 128 (n % 128 == 0). STS own word, BAR.SYNC, + * LDS the partner word. Mutant: LDS own word (no exchange). */ +static void gen_xchg(Em *e, int mutant) { + Pro p = prologue(e); + int i = V(e), off = V64(e), x = V(e), saddr = V(e), paddr = V(e), mask = V(e), y = V(e); + imadi(e, i, p.cta, OMEGA_GPU_EW_THREADS, p.tid); + addr4(e, off, i, p.pa); + ldg(e, x, off, p.udesc); + imadi(e, saddr, p.tid, 4, NOREG); + sts32(e, saddr, x); + bar_sync(e); + if (mutant) { + lds32(e, y, saddr); + } else { + movi(e, mask, OMEGA_GPU_EW_THREADS - 1); + lop3xor(e, paddr, p.tid, mask); + imadi(e, paddr, paddr, 4, NOREG); + lds32(e, y, paddr); + } + addr4(e, off, i, p.pc); + stg(e, off, y, p.udesc); + tail(e); +} + +/* One CTA of 128 threads per row. P0 = dim/128 (loop count), P1 = bits(1/dim), + * P2 = bits(eps), P3 = dim. Mutant: the warp tree drops the last SHFL step. */ +static void gen_rmsnorm(Em *e, int mutant) { + Pro p = prologue(e); + int nk = V(e), invd = V(e), eps = V(e), dim = V(e); + int rowbase = V(e), idx = V(e), col = V(e), k = V(e), acc = V(e), cT = V(e), c1 = V(e); + int off = V64(e), x = V(e), t = V(e); + ldc32(e, nk, ARG_P0); ldc32(e, invd, ARG_P1); ldc32(e, eps, ARG_P2); ldc32(e, dim, ARG_P3); + imadr(e, rowbase, p.cta, dim, NOREG); + iadd3(e, idx, rowbase, p.tid); + movrz(e, acc); movrz(e, k); + movi(e, cT, OMEGA_GPU_EW_THREADS); movi(e, c1, 1); + loop_begin(e); + int l1 = here(e); + addr4(e, off, idx, p.pa); + ldg(e, x, off, p.udesc); + ffma(e, acc, x, x, acc); + iadd3(e, idx, idx, cT); + iadd3(e, k, k, c1); + isetp_ge_u32(e, k, nk); + bra_back_if_not_p0(e, l1); + loop_end(e); + static const uint32_t steps[5] = { 16, 8, 4, 2, 1 }; + for (int s = 0; s < (mutant ? 4 : 5); s++) { shfl_down(e, t, acc, steps[s]); fadd(e, acc, acc, t); } + int saddr = V(e); + imadi(e, saddr, p.tid, 4, NOREG); + sts32(e, saddr, acc); + bar_sync(e); + int a0 = V(e), a1 = V(e), a2 = V(e), a3 = V(e), s0 = V(e), s1 = V(e), s2 = V(e), s3 = V(e), sum = V(e); + movrz(e, a0); movi(e, a1, 128); movi(e, a2, 256); movi(e, a3, 384); + lds32(e, s0, a0); lds32(e, s1, a1); lds32(e, s2, a2); lds32(e, s3, a3); + fadd(e, sum, s0, s1); fadd(e, sum, sum, s2); fadd(e, sum, sum, s3); + int v = V(e), y = V(e), u = V(e), cmh = V(e), c15 = V(e); + fmul(e, v, sum, invd); + fadd(e, v, v, eps); + mufu(e, BW_IR_MUFU_RSQ, y, v); + movf(e, cmh, -0.5f); movf(e, c15, 1.5f); + fmul(e, u, v, y); fmul(e, u, u, y); ffma(e, u, u, cmh, c15); fmul(e, y, y, u); + int offw = V64(e), offc = V64(e), w = V(e), o = V(e); + iadd3(e, idx, rowbase, p.tid); + imadi(e, col, p.tid, 1, NOREG); /* col = tid (the encoder maps a missing src2 to R0, not RZ, so no IADD3 here) */ + movrz(e, k); + loop_begin(e); + int l2 = here(e); + addr4(e, off, idx, p.pa); + ldg(e, x, off, p.udesc); + addr4(e, offw, col, p.pb); + ldg(e, w, offw, p.udesc); + fmul(e, o, x, y); + fmul(e, o, o, w); + addr4(e, offc, idx, p.pc); + stg(e, offc, o, p.udesc); + iadd3(e, idx, idx, cT); + iadd3(e, col, col, cT); + iadd3(e, k, k, c1); + isetp_ge_u32(e, k, nk); + bra_back_if_not_p0(e, l2); + loop_end(e); + tail(e); +} + +/* One CTA of head_dim/2 threads per head. P0 = head_dim, P1 = head_dim/2. + * a = vector, b = table (cos in the first half of each head, sin in the second). + * Mutant: out0 = q0*cos + q1*sin. */ +static void gen_rope(Em *e, int mutant) { + Pro p = prologue(e); + int hd = V(e), half = V(e), i0 = V(e), i1 = V(e), off0 = V64(e), off1 = V64(e); + int q0 = V(e), q1 = V(e), c = V(e), s = V(e), t0 = V(e), t1 = V(e), o0 = V(e), o1 = V(e); + ldc32(e, hd, ARG_P0); ldc32(e, half, ARG_P1); + imadr(e, i0, p.cta, hd, p.tid); + iadd3(e, i1, i0, half); + addr4(e, off0, i0, p.pa); ldg(e, q0, off0, p.udesc); + addr4(e, off1, i1, p.pa); ldg(e, q1, off1, p.udesc); + addr4(e, off0, i0, p.pb); ldg(e, c, off0, p.udesc); + addr4(e, off1, i1, p.pb); ldg(e, s, off1, p.udesc); + fmul(e, t0, q0, c); fmul(e, t1, q1, s); + if (mutant) fadd(e, o0, t0, t1); else fsub(e, o0, t0, t1); + fmul(e, t0, q0, s); fmul(e, t1, q1, c); + fadd(e, o1, t0, t1); + addr4(e, off0, i0, p.pc); stg(e, off0, o0, p.udesc); + addr4(e, off1, i1, p.pc); stg(e, off1, o1, p.udesc); + tail(e); +} + +/* out[i] = g/(1+2^(g*-log2e)) * up for i < n (P0). Mutant: 2^(g*+log2e). */ +static void gen_swiglu(Em *e, int mutant) { + Pro p = prologue(e); + int n = V(e), i = V(e), off = V64(e), g = V(e), u = V(e), k = V(e), t = V(e), ex = V(e), d = V(e), r = V(e), one = V(e), o = V(e); + ldc32(e, n, ARG_P0); + imadi(e, i, p.cta, OMEGA_GPU_EW_THREADS, p.tid); + isetp_ge_u32(e, i, n); + exit_if_p0(e); + addr4(e, off, i, p.pa); ldg(e, g, off, p.udesc); + addr4(e, off, i, p.pb); ldg(e, u, off, p.udesc); + movf(e, k, mutant ? 1.4426950408889634f : -1.4426950408889634f); + movf(e, one, 1.0f); + fmul(e, t, g, k); + mufu(e, BW_IR_MUFU_EX2, ex, t); + fadd(e, d, ex, one); + mufu(e, BW_IR_MUFU_RCP, r, d); + fmul(e, o, g, r); + fmul(e, o, o, u); + addr4(e, off, i, p.pc); + stg(e, off, o, p.udesc); + tail(e); +} + +/* ------------------------------------------- prime race sieve (2026-10-05) */ +static void lop3lut(Em *e, int d, int a, int b, int c, uint32_t lut) { BlackwellIRInsn i = I0(BW_IR_LOP3_LUT); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; i.imm = lut & 0xffu; emit(e, i, K_FIXED); } +static void shl_r(Em *e, int d, int a, int n) { BlackwellIRInsn i = I0(BW_IR_SHF_L_U32); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = n; emit(e, i, K_FIXED); } +static void shr_i(Em *e, int d, int a, uint32_t n) { BlackwellIRInsn i = I0(BW_IR_SHF_R); i.dst_vreg = d; i.src1_vreg = a; i.imm = n; emit(e, i, K_FIXED); } +static void imadhi(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_IMAD_HI_U32); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +/* dst64 = a * imm + c64 (IMAD.WIDE.U32 immediate form) */ +static void widei(Em *e, int dst64, int a, uint32_t imm, int c64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = dst64; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c64; emit(e, i, K_FIXED); } +/* @P0 BRA forward; returns the instruction index for bra_patch_here */ +static int bra_fwd_if_p0(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = 1; emit(e, i, K_BRA); return at; } +static void bra_patch_here(Em *e, int at) { if (e->err == 0 && at >= 0 && at < here(e)) e->p->insns[at].imm = (uint32_t)(here(e) - at); else e->err = -1; } +static void bra_always(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } + +/* ---- structured reconvergence regions (omega #308; bookkeeping in omega_bw_reconv.h) ---- + * region_begin: BSSY Bd (d = nesting depth), fails closed when no barrier register is left. + * region_exit: a forward branch out of the innermost region, patched to its BSYNC at join + * (RX_ALWAYS, RX_IF_P0, RX_IF_NOT_P0). + * region_join: BSYNC Bd; patches the BSSY to the instruction after it and every exit to it. + * Usage: divergent loop = region_begin; L: isetp; region_exit(RX_IF_P0); body; bra_always(L); region_join. + * if/else diamond = region_begin; isetp; t = bra_fwd_if_p0; then; region_exit(RX_ALWAYS); + * bra_patch_here(t); else; region_join. */ +enum { RX_ALWAYS, RX_IF_P0, RX_IF_NOT_P0 }; +static int region_begin(Em *e) { + int bar = bw_regions_next_bar(&e->rg); + if (bar < 0) { e->err = -1; return -1; } + int at = here(e); + BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = (uint8_t)bar; i.imm = 2; emit(e, i, K_BSSY); + if (bw_regions_begin(&e->rg, at) < 0) e->err = -1; + return bar; +} +static void region_exit(Em *e, int cond) { + int at = here(e); + BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = 1; + if (cond != RX_ALWAYS) { i.predicate_p0 = true; i.predicate_not = (cond == RX_IF_NOT_P0); } + emit(e, i, K_BRA); + if (bw_regions_exit(&e->rg, at) < 0) e->err = -1; +} +static void region_join(Em *e) { + if (e->rg.depth <= 0) { e->err = -1; return; } + int d = e->rg.depth - 1, bsync = here(e); + BlackwellIRInsn i = I0(e->rg_mutant == 1 ? BW_IR_NOP : BW_IR_BSYNC); i.bar_reg = (uint8_t)d; emit(e, i, K_BSYNC); + if (e->rg_mutant == 1) { + /* negative control (host sim only): the join is a NOP; patch as the real join would */ + BwRegions *r = &e->rg; r->depth--; + e->p->insns[r->bssy_at[d]].imm = (uint32_t)(bsync + 1 - r->bssy_at[d]); + for (int k = 0; k < r->n_exits[d]; k++) e->p->insns[r->exits[d][k]].imm = (uint32_t)(bsync - r->exits[d][k]); + return; + } + if (bw_regions_join(&e->rg, e->p, bsync) != d) e->err = -1; + if (e->rg_mutant == 2) e->p->insns[e->rg.bssy_at[d]].imm += 1; /* negative control: BSSY names the wrong join */ +} + +/* Word-parallel odd-only sieve (docs: handoff 2026-10-05 DESIGN "GPU kernel (native)"). + * Thread t owns 32-bit output words w = t, t + S, t + 2S, ... (S = P1 = threads launched; + * the I42 envelope caps a launch at OMEGA_GPU_EW_MAX_CTAS CTAs, so a grid-stride loop + * replaces one thread per word). Every word has exactly one writer; no atomics. + * Bit j of word w is the odd number 2(32w + j) + 1. + * Arguments: a (0x380) = prime table, entries {p, magic = ceil(2^32 / p), k0 = (p*p - 1)/2} + * (12 bytes, ascending p, odd primes <= isqrt(limit)) followed by ONE sentinel entry with + * k0 = 0xffffffff that ends every scan; c (0x390) = output words; + * P0 = nwords, P1 = S, P2 = last word index, P3 = ~tailmask (bits past the last odd). + * Range: limit < 2^32 (so base, k0 < 2^31 and the sign-bit tricks below hold). + * Per word: base = 32w; for each prime while k0 < base + 32: + * d = base - k0; neg = d >> 31 (k0 > base); dpos = neg ? 0 : d; jadd = neg ? k0 - base : 0 + * q = hi32(dpos * magic) (= floor(dpos/p) or one more: magic is rounded up) + * r = dpos - q*p (q one too big -> r = (dpos mod p) - p, wrapped) + * j = p - r; j -= p * ((r - 1) >> 31) (r == 0 -> 0; wrapped r -> p - (dpos mod p)) + * One fix covers both cases: with q one too big, dpos mod p >= p - dpos*e/2^32 > p/2 > 0 + * (e = magic*p - 2^32 < p, dpos < 2^31), so the true remainder is never 0 there and + * p - r - p = p - (dpos mod p) is the right offset. A separate "r += p if wrapped" step was + * removed after a negative control showed it changes no output (host sim, limit 1e7, where + * the overshoot occurs 268 times; bench/prime_race/tests magic_model). + * j += jadd; while j < 32 { acc |= 1 << j; j += p } + * then acc |= (w == 0) (the number 1), word = ~(acc | (w == last ? ~tailmask : 0)). + * Branch-free except the loop branches. Mutant: drops the j fix (j = p - r unreduced), so words where + * base - k0 is a multiple of p (or q overshot) mark the wrong bits. + * + * staged = 1 (OMEGA_GPU_EW_PRIME_SIEVE_SHARED, campaign C3): P4 = table words, sentinel included + * (3 per entry). The CTA first copies the table into shared memory at offset 0 (thread t copies + * words t, t + 128, ...), BAR_SYNC, then the prime loop reads shared memory (LDS) instead of + * uncached global memory. Every thread reaches the barrier: the w >= nwords exit comes after + * it. The launch must declare at least 4 * P4 bytes of shared memory. + * + * divergent (omega #308, regression R1): 0 = the warp-uniform mark loop above (campaign C5). + * 1 = the original lane-dependent mark loop (omega 888a011: `while j < 32 { acc |= 1 << j; j += p }`) + * wrapped in a reconvergence region. 2 = as 1, and the prime loop (whose break `k0 >= base + 32` + * is also lane-dependent: every lane sieves a different word) wrapped in an outer region, so the + * mark loop's region nests inside it (B0 outer, B1 inner). */ +static void gen_prime_sieve(Em *e, int mutant, int staged, int divergent) { + Pro pr = prologue(e); + int nwords = V(e), stride = V(e), lastw = V(e), tailinv = V(e); + int one = V(e), c32 = V(e), cm1 = V(e), w = V(e); + ldc32(e, nwords, ARG_P0); ldc32(e, stride, ARG_P1); ldc32(e, lastw, ARG_P2); ldc32(e, tailinv, ARG_P3); + movi(e, one, 1); movi(e, c32, 32); movi(e, cm1, 0xffffffffu); + int c4 = -1, c8 = -1, c12 = -1; + if (staged) { + int twords = V(e), i = V(e), ga = V64(e), val = V(e), sa = V(e); + c4 = V(e); c8 = V(e); c12 = V(e); + ldc32(e, twords, ARG_P4); + movi(e, c4, 4); movi(e, c8, 8); movi(e, c12, 12); + imadi(e, i, pr.tid, 1, NOREG); + /* The staging loop's trip count differs per thread (thread t copies words t, t+128, ...), + * so its exit splits the warp. The divergent variants rejoin the warp before BAR.SYNC + * (a barrier is not a reconvergence point: the fragments would stay split for the rest + * of the kernel). The production kernel keeps the 2026-10-05 C3 form unchanged. */ + if (divergent) region_begin(e); + loop_begin(e); /* staging loop */ + int ls = here(e); + isetp_ge_u32(e, i, twords); + int sdone = -1; + if (divergent) region_exit(e, RX_IF_P0); else sdone = bra_fwd_if_p0(e); + addr4(e, ga, i, pr.pa); + ldg(e, val, ga, pr.udesc); + imadi(e, sa, i, 4, NOREG); + sts32(e, sa, val); + imadi(e, i, one, OMEGA_GPU_EW_THREADS, i); + bra_always(e, ls); + loop_end(e); + if (divergent) region_join(e); else bra_patch_here(e, sdone); + bar_sync(e); + } + imadi(e, w, pr.cta, OMEGA_GPU_EW_THREADS, pr.tid); + isetp_ge_u32(e, w, nwords); + exit_if_p0(e); + + /* ptr, a64, a64m: global addresses, or (staged) 32-bit shared offsets */ + int base = V(e), basep32 = V(e), acc = V(e); + int ptr = staged ? V(e) : V64(e), a64 = staged ? V(e) : V64(e), a64m = staged ? V(e) : V64(e); + int p = V(e), magic = V(e), k0 = V(e), negp = V(e), d = V(e), neg = V(e), nd = V(e), dpos = V(e), jadd = V(e); + int q = V(e), r = V(e), j = V(e), t = V(e), z = V(e), bit = V(e); + int e1 = V(e), m = V(e), word = V(e), oaddr = V64(e); + int tt = V(e), jj = V(e), sh = V(e), c31 = V(e), cm32 = V(e); + movi(e, c31, 31); movi(e, cm32, 0xffffffe0u); + + loop_begin(e); /* word loop */ + int lw = here(e); + imadi(e, base, w, 32, NOREG); + iadd3(e, basep32, base, c32); + movrz(e, acc); + if (staged) movrz(e, ptr); else widei(e, ptr, one, 0, pr.pa); + + if (divergent >= 2) region_begin(e); /* outer region: the prime loop's break is lane-dependent */ + loop_begin(e); /* prime loop */ + int lp = here(e); + /* one memory round trip per prime: all three entry words load together (C2a). The + * sentinel entry is a whole entry, so loading p and magic before the k0 test is in bounds. */ + if (staged) { iadd3(e, a64, ptr, c8); iadd3(e, a64m, ptr, c4); } + else { widei(e, a64, one, 8, ptr); widei(e, a64m, one, 4, ptr); } + { + const int dsts[3] = { k0, p, magic }, adrs[3] = { a64, ptr, a64m }; + if (staged) ld_group(e, BW_IR_LDS32, 3, dsts, adrs, NOREG); + else ld_group(e, BW_IR_LDG_E, 3, dsts, adrs, pr.udesc); + } + isetp_ge_u32(e, k0, basep32); + int brk = -1; + if (divergent >= 2) region_exit(e, RX_IF_P0); else brk = bra_fwd_if_p0(e); + imadi(e, negp, p, 0xffffffffu, NOREG); /* -p */ + imadi(e, d, k0, 0xffffffffu, base); /* base - k0 */ + shr_i(e, neg, d, 31); + imadi(e, nd, d, 0xffffffffu, NOREG); /* -d */ + imadr(e, dpos, neg, nd, d); /* d - neg*d */ + imadr(e, jadd, neg, nd, NOREG); /* neg * (k0 - base) */ + imadhi(e, q, dpos, magic); + imadr(e, r, q, negp, dpos); /* dpos - q*p */ + imadi(e, j, r, 0xffffffffu, p); /* p - r */ + if (!mutant) { + iadd3(e, t, r, cm1); + shr_i(e, z, t, 31); /* r == 0, or r wrapped (q one too big) */ + imadr(e, j, z, negp, j); /* j - p */ + } + iadd3(e, j, j, jadd); + + /* mark loop (campaign C5): the trip count depends only on p, so every active lane runs the + * same ceil(32/p) steps and the warp never splits here (all active lanes are on the same + * prime). Bits past this word are masked out instead of branched around. The old loop ran + * "while j < 32", a lane-dependent branch with no reconvergence point. */ + if (divergent) { + /* R1: the 888a011 mark loop, lane-dependent trip count, inside a region. Each lane + * leaves at its own iteration (the exit jumps to the BSYNC) and the warp reunites there. */ + region_begin(e); + loop_begin(e); + int li = here(e); + isetp_ge_u32(e, j, c32); + region_exit(e, RX_IF_P0); + shl_r(e, bit, one, j); + lop3lut(e, acc, acc, bit, NOREG, 0xfc); /* acc | bit */ + iadd3(e, j, j, p); + bra_always(e, li); + loop_end(e); + region_join(e); + } else { + movrz(e, tt); + loop_begin(e); + int li = here(e); + isetp_ge_u32(e, tt, c32); + int done = bra_fwd_if_p0(e); + iadd3(e, jj, j, tt); + lop3lut(e, sh, jj, c31, NOREG, 0xc0); /* jj & 31 */ + shl_r(e, bit, one, sh); + iadd3(e, t, jj, cm32); + shr_i(e, z, t, 31); /* jj < 32 (j <= p + 31, far below 2^31) */ + imadi(e, m, z, 0xffffffffu, NOREG); /* all ones when jj < 32, else 0 */ + lop3lut(e, acc, acc, bit, m, 0xf8); /* acc | (bit & m) */ + iadd3(e, tt, tt, p); + bra_always(e, li); + loop_end(e); + bra_patch_here(e, done); + } + + if (staged) iadd3(e, ptr, ptr, c12); else widei(e, ptr, one, 12, ptr); + bra_always(e, lp); + loop_end(e); + if (divergent >= 2) region_join(e); else bra_patch_here(e, brk); + + iadd3(e, t, w, cm1); + shr_i(e, z, t, 31); /* w == 0: the number 1 is not prime */ + lop3lut(e, acc, acc, z, NOREG, 0xfc); + lop3xor(e, e1, w, lastw); + iadd3(e, t, e1, cm1); + shr_i(e, z, t, 31); /* w == last word */ + imadr(e, m, z, tailinv, NOREG); + lop3lut(e, word, acc, m, NOREG, 0x03); /* ~(acc | m) */ + addr4(e, oaddr, w, pr.pc); + stg(e, oaddr, word, pr.udesc); + /* IMAD, not IADD3: the IADD3 encoder ignores the scheduler control word (fixed + * 0x010fca00), so right after a store it would not wait on the read barrier */ + imadr(e, w, stride, one, w); + isetp_ge_u32(e, w, nwords); + bra_back_if_not_p0(e, lw); + loop_end(e); + tail(e); +} + +/* ---- reconvergence probes (omega #308, regressions R3..R6 and the resource bound) ---- + * All: a, b = u32 inputs, out = u32, P0 = n; 128 threads, grid ceil(n/128); launched by + * omega_gpu_reconv_probe_u32. A lane past n clamps its index to n-1 so that it loads valid + * data and takes part in the warp-wide SHFL; it exits only after the SHFL, before its store. + * Each probe computes a per-lane r on divergent paths, joins, then + * out[i] = r + 4099 * s, s = r of lane (lane+1) in the same warp (SHFL.DOWN 1; lane 31: own r) + * so a lane that joined late, never, or with the wrong value shows in the neighbour's word too. + * The host oracle is rc_oracle in tests/gpu_reconv_test.c. */ +typedef struct { Pro pr; int n, i, idx, x, y, r, s, one, cm1; } Rp; +static Rp rp_begin(Em *e) { + Rp k; memset(&k, 0, sizeof k); + k.pr = prologue(e); + k.n = V(e); k.i = V(e); k.idx = V(e); k.x = V(e); k.y = V(e); k.r = V(e); k.s = V(e); k.one = V(e); k.cm1 = V(e); + int nm1 = V(e), d = V(e), neg = V(e), off = V64(e); + ldc32(e, k.n, ARG_P0); + movi(e, k.one, 1); movi(e, k.cm1, 0xffffffffu); + imadi(e, k.i, k.pr.cta, OMEGA_GPU_EW_THREADS, k.pr.tid); + iadd3(e, nm1, k.n, k.cm1); /* n - 1 */ + imadi(e, d, k.i, 0xffffffffu, nm1); /* (n - 1) - i, top bit set when i > n - 1 */ + shr_i(e, neg, d, 31); + imadr(e, k.idx, neg, d, k.i); /* i, or n - 1 when past the end */ + addr4(e, off, k.idx, k.pr.pa); ldg(e, k.x, off, k.pr.udesc); + addr4(e, off, k.idx, k.pr.pb); ldg(e, k.y, off, k.pr.udesc); + return k; +} +static void rp_end(Em *e, const Rp *k) { + int out = V(e), off = V64(e); + shfl_down(e, k->s, k->r, 1); + imadi(e, out, k->s, 4099, k->r); + isetp_ge_u32(e, k->i, k->n); + exit_if_p0(e); + addr4(e, off, k->i, k->pr.pc); + stg(e, off, out, k->pr.udesc); + tail(e); +} +/* R3: if/else with sides of different length. x >= y: r = f(x, y) (5 steps); else r = x + 1. + * Math mutant: the long side is one too big. */ +static void gen_rc_diamond(Em *e, int mutant) { + Rp k = rp_begin(e); + int t = V(e), u = V(e); + region_begin(e); + isetp_ge_u32(e, k.x, k.y); + int els = bra_fwd_if_p0(e); + iadd3(e, k.r, k.x, k.one); /* short side */ + region_exit(e, RX_ALWAYS); + bra_patch_here(e, els); + imadi(e, t, k.x, 3, k.y); /* long side: t = 3x + y */ + lop3xor(e, u, t, k.x); /* u = t ^ x */ + imadi(e, k.r, u, 5, k.one); /* r = 5u + 1 */ + shr_i(e, t, k.r, 3); + iadd3(e, k.r, k.r, t); /* r += r >> 3 */ + if (mutant) iadd3(e, k.r, k.r, k.one); + region_join(e); + rp_end(e, &k); +} +/* The lane-dependent loop shared by R4 and R5: v = x & 255, step = (y & 15) + 1, + * while v < 256 { v += step; cnt += 1 }. Math mutant: the bound is 255. Emits the region. */ +static void rc_count_loop(Em *e, const Rp *k, int mutant, int v, int cnt) { + int st = V(e), bound = V(e), c255 = V(e), c15 = V(e); + movi(e, c255, 255); movi(e, c15, 15); movi(e, bound, mutant ? 255 : 256); + lop3lut(e, v, k->x, c255, NOREG, 0xc0); + lop3lut(e, st, k->y, c15, NOREG, 0xc0); + iadd3(e, st, st, k->one); + movrz(e, cnt); + region_begin(e); + loop_begin(e); + int L = here(e); + isetp_ge_u32(e, v, bound); + region_exit(e, RX_IF_P0); + iadd3(e, v, v, st); + iadd3(e, cnt, cnt, k->one); + bra_always(e, L); + loop_end(e); + region_join(e); +} +/* R4: lane-dependent loop exit (break). r = 256 * cnt + v. */ +static void gen_rc_loopbreak(Em *e, int mutant) { + Rp k = rp_begin(e); + int v = V(e), cnt = V(e); + rc_count_loop(e, &k, mutant, v, cnt); + imadi(e, k.r, cnt, 256, v); + rp_end(e, &k); +} +/* R5: nested divergence. Odd x: r = 7 (skips the inner region). Even x: the R4 loop inside the + * outer region, r = 256 * cnt + v + 100. */ +static void gen_rc_nested(Em *e, int mutant) { + Rp k = rp_begin(e); + int v = V(e), cnt = V(e), low = V(e), c100 = V(e); + movi(e, k.r, 7); movi(e, c100, 100); + lop3lut(e, low, k.x, k.one, NOREG, 0xc0); /* x & 1 */ + region_begin(e); + isetp_ge_u32(e, low, k.one); + region_exit(e, RX_IF_P0); + rc_count_loop(e, &k, mutant, v, cnt); + imadi(e, k.r, cnt, 256, v); + iadd3(e, k.r, k.r, c100); + region_join(e); + rp_end(e, &k); +} +/* R6: lanes past the end, a divergent region, then a CTA-wide exchange and the warp SHFL. + * xv = x | 1 computed on divergent paths (even lanes take the branch), every thread stores xv + * to its shared slot, BAR.SYNC, reads its partner's (tid ^ 1), r = 3x + partner_xv. + * Production form: lanes past n are clamped and exit after the SHFL (correct). early_exit + * (mutant 1): lanes past n EXIT first, as a naive kernel would; the emitter refuses that + * (BAR/SHFL after a predicated EXIT). Only the host-test build path (codegen_ir) may bypass the + * refusal, so the simulator can show what the kernel would do (SHFL_EXITED); a launch never can. */ +static void gen_rc_exit_bar(Em *e, int early_exit) { + Rp k = rp_begin(e); + int saddr = V(e), paddr = V(e), yv = V(e), xv = V(e), low = V(e); + if (early_exit) { + e->exit_sync_ok = early_exit == 1 && e->test_path; + isetp_ge_u32(e, k.i, k.n); + exit_if_p0(e); + } + imadi(e, xv, k.x, 1, NOREG); + lop3lut(e, low, k.x, k.one, NOREG, 0xc0); /* x & 1 */ + region_begin(e); + isetp_ge_u32(e, low, k.one); + region_exit(e, RX_IF_P0); /* odd lanes leave */ + iadd3(e, xv, xv, k.one); /* even lanes: xv = x + 1 */ + region_join(e); + imadi(e, saddr, k.pr.tid, 4, NOREG); + sts32(e, saddr, xv); + bar_sync(e); + lop3xor(e, paddr, k.pr.tid, k.one); + imadi(e, paddr, paddr, 4, NOREG); + lds32(e, yv, paddr); + imadi(e, k.r, k.x, 3, yv); + rp_end(e, &k); +} +/* Barrier-register bound: `levels` nested regions, each leaving lanes with x >= y out and adding 1 + * for the others: r = x + levels * (x < y). 16 levels must build (B0..B15); 17 must be refused. */ +static void gen_rc_depth(Em *e, int levels) { + Rp k = rp_begin(e); + imadi(e, k.r, k.x, 1, NOREG); + for (int d = 0; d < levels; d++) { + region_begin(e); + isetp_ge_u32(e, k.x, k.y); + region_exit(e, RX_IF_P0); + iadd3(e, k.r, k.r, k.one); + } + for (int d = 0; d < levels; d++) region_join(e); + rp_end(e, &k); +} +/* Codegen refusals (each must come back CODEGEN_FAIL; a kernel with a split warp at a + * cross-lane step must never reach the chip). */ +static void gen_rc_refuse(Em *e, OmegaGpuEwOp which) { + Rp k = rp_begin(e); + switch (which) { + case OMEGA_GPU_EW_RC_REFUSE_EXIT_IN_REGION: region_begin(e); isetp_ge_u32(e, k.i, k.n); exit_if_p0(e); region_join(e); break; + case OMEGA_GPU_EW_RC_REFUSE_BAR_IN_REGION: region_begin(e); bar_sync(e); region_join(e); break; + case OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION: region_begin(e); shfl_down(e, k.s, k.x, 1); region_join(e); break; + case OMEGA_GPU_EW_RC_REFUSE_UNCLOSED: region_begin(e); isetp_ge_u32(e, k.x, k.y); region_exit(e, RX_IF_P0); break; /* no join */ + case OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY: region_join(e); break; /* BSYNC without BSSY */ + default: e->err = -1; break; + } + imadi(e, k.r, k.x, 1, NOREG); + rp_end(e, &k); +} + +/* A value first defined inside a loop body may not be read after the loop + * before it is written again: the allocator could have let a later body + * temporary share its register, and the last iteration's value would be lost. */ +static int check_loop_invariant(const Em *e) { + const OmegaRegAlloc *ra = &e->p->regalloc; + for (int L = 0; L < e->n_loops; L++) + for (int v = 0; v < ra->num_vregs; v++) { + int fd = ra->intervals[v].first_def; + if (fd < e->loop_start[L] || fd > e->loop_end[L]) continue; + for (size_t i = (size_t)e->loop_end[L] + 1; i < e->p->count; i++) { + const BlackwellIRInsn *in = &e->p->insns[i]; + if (in->src1_vreg == v || in->src2_vreg == v || in->src3_vreg == v) return -1; + if (in->dst_vreg == v && !in->is_uniform) break; + } + } + return 0; +} + +#define EW_GPR_BUDGET 64u /* the QMD declares 64 registers, as the matmul launcher does */ + +/* Mesa NAK sm120_instr_latencies.rs + sm100/reg_raw.csv: ALU/dual-ALU pairs + * and FMA/FMA pairs need 4 + 1 cycles; mixed pairs need 5 + 1. CSV rows are + * READERS, columns WRITERS (see lat_rs_gen.py), not the reverse. IMAD.WIDE + * has operand-specific latencies and is deliberately excluded here. + * + * Retain at least 5 cycles after every fixed producer. Thus the next reader + * uses the pair-specific bound and later readers are >= 9 cycles away (the + * next instruction in a tightened pair is itself fixed). No instruction, + * branch target, wait, barrier assignment or register allocation is changed. + * IADD3's encoder still supplies its historical control word; do not pretend + * to schedule that producer here. Its existing 5-cycle delay is unchanged. + * + * Predicate-to-guard uses NAK paw_latency -> raw(write, None): reader + * RedirectedFp64, writer Dualalu = 13, plus SM120's 1 = 14, in emit above. + * NVIDIA's public ISA docs are silent about SASS control-word semantics. + */ +static int fixed_pair_class(BlackwellIROpcode op) { + switch (op) { + case BW_IR_MOV_RZ: case BW_IR_MOV_IMM: case BW_IR_IADD3: + case BW_IR_LOP3_XOR: case BW_IR_LOP3_LUT: + case BW_IR_SHF_R: case BW_IR_SHF_L_U32: return 1; + case BW_IR_IMAD: case BW_IR_IMAD_HI_U32: + case BW_IR_FADD: case BW_IR_FSUB: case BW_IR_FMUL: case BW_IR_FFMA: return 2; + default: return 0; + } +} + +static void schedule_fixed_pairs(BlackwellIRProgram *p) { + for (size_t i = 0; i + 1 < p->count; i++) { + BlackwellIRInsn *a = &p->insns[i]; + const BlackwellIRInsn *b = &p->insns[i + 1]; + int cls = fixed_pair_class(a->op); + if (!cls || cls != fixed_pair_class(b->op) || a->op == BW_IR_IADD3 || + a->predicate_p0 || b->predicate_p0 || ((a->control >> 9) & 15u) != 6) + continue; + a->control = (a->control & ~(15u << 9)) | (5u << 9); + } +} + +static int build_kernel(OmegaGpuEwOp op, int mutant, OmegaBlackwellKernel *kernel, BlackwellIRProgram *prog_out, int test_path) { + BlackwellIRProgram *prog = calloc(1, sizeof *prog); + if (!prog) return OMEGA_GPU_EW_CODEGEN_FAIL; + omega_bw_ir_init(prog); + Em e; memset(&e, 0, sizeof e); e.p = prog; e.test_path = test_path; + /* mutant 1 = the op's math mutant (as before); 2 = the join (BSYNC) dropped; 3 = the BSSY + * names the wrong join. 2 and 3 only change kernels that open a region. */ + e.rg_mutant = mutant == 2 ? 1 : mutant == 3 ? 2 : 0; + int math = mutant == 1; + switch (op) { + case OMEGA_GPU_EW_EX2: gen_ex2(&e, math); break; + case OMEGA_GPU_EW_XCHG: gen_xchg(&e, math); break; + case OMEGA_GPU_EW_RMSNORM: gen_rmsnorm(&e, math); break; + case OMEGA_GPU_EW_ROPE: gen_rope(&e, math); break; + case OMEGA_GPU_EW_SWIGLU: gen_swiglu(&e, math); break; + case OMEGA_GPU_EW_PRIME_SIEVE: gen_prime_sieve(&e, math, 0, 0); break; + case OMEGA_GPU_EW_PRIME_SIEVE_SHARED: gen_prime_sieve(&e, math, 1, 0); break; + case OMEGA_GPU_EW_PRIME_SIEVE_DIV1: gen_prime_sieve(&e, math, 0, 1); break; + case OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1: gen_prime_sieve(&e, math, 1, 1); break; + case OMEGA_GPU_EW_PRIME_SIEVE_DIV2: gen_prime_sieve(&e, math, 0, 2); break; + case OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2: gen_prime_sieve(&e, math, 1, 2); break; + case OMEGA_GPU_EW_RC_DIAMOND: gen_rc_diamond(&e, math); break; + case OMEGA_GPU_EW_RC_LOOPBREAK: gen_rc_loopbreak(&e, math); break; + case OMEGA_GPU_EW_RC_NESTED: gen_rc_nested(&e, math); break; + case OMEGA_GPU_EW_RC_EXIT_BAR: gen_rc_exit_bar(&e, math); break; + case OMEGA_GPU_EW_RC_REFUSE_EXIT_SHFL: gen_rc_exit_bar(&e, 2); break; + case OMEGA_GPU_EW_RC_DEPTH16: gen_rc_depth(&e, BW_RECONV_MAX_BAR); break; + case OMEGA_GPU_EW_RC_REFUSE_DEPTH17: gen_rc_depth(&e, BW_RECONV_MAX_BAR + 1); break; + case OMEGA_GPU_EW_RC_REFUSE_EXIT_IN_REGION: case OMEGA_GPU_EW_RC_REFUSE_BAR_IN_REGION: + case OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION: case OMEGA_GPU_EW_RC_REFUSE_UNCLOSED: + case OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY: gen_rc_refuse(&e, op); break; + default: e.err = -1; break; + } + if (e.rg.depth != 0) e.err = -1; /* a region left open has no join: refuse the build */ + schedule_fixed_pairs(prog); + int rc = OMEGA_GPU_EW_CODEGEN_FAIL; + int ra_rc = (e.err == 0) ? omega_bw_regalloc_solve(prog) : -1; + int loop_rc = (ra_rc == 0) ? check_loop_invariant(&e) : -1; +#ifdef OMEGA_EW_DEBUG + fprintf(stderr, "ew codegen op=%d mutant=%d emit_err=%d insns=%zu vregs=%d regalloc=%d loops=%d peak_gpr=%u\n", + (int)op, mutant, e.err, prog->count, prog->regalloc.num_vregs, ra_rc, loop_rc, prog->regalloc.peak_gpr_usage); +#endif + if (e.err == 0 && ra_rc == 0 && loop_rc == 0 && prog->regalloc.peak_gpr_usage <= EW_GPR_BUDGET) { + size_t max_bytes = (size_t)BW_MAX_IR_INSNS * 16; + memset(kernel, 0, sizeof *kernel); + kernel->code = malloc(max_bytes); + size_t emitted = 0; + if (kernel->code && omega_bw_encode_program(prog, kernel->code, max_bytes, &emitted) == 0) { + kernel->code_size = emitted; + kernel->insn_count = emitted / 16; + kernel->gpr_count = prog->regalloc.peak_gpr_usage; + kernel->uniform_gpr_count = prog->regalloc.peak_ugpr_usage; + sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); + if (prog_out) *prog_out = *prog; + rc = OMEGA_GPU_EW_OK; + } else { +#ifdef OMEGA_EW_DEBUG + fprintf(stderr, "ew codegen op=%d: encode failed (an instruction form the encoder refuses)\n", (int)op); +#endif + free(kernel->code); kernel->code = NULL; + } + } + free(prog); + return rc; +} + +/* --------------------------------------------------------- kernel cache */ + +/* One kernel per (op, mutant); the device copy is uploaded once and stays + * resident for the process (cut 4b: no per-call code page). */ +typedef struct { int used; int op; int mutant; OmegaBlackwellKernel kernel; NvrmMem code; } Slot; +static int g_mutant_op; /* 0 = none */ +static int g_mutant_level = 1; /* the mutant number built for g_mutant_op (1 math, 2 join dropped, 3 join corrupted) */ +static Slot g_cache[8]; +static OmegaGpuScratch g_a, g_b, g_c; /* staging, grown to the high-water mark, reused per call */ +static bool g_hooked; +#define LOCK() omega_gpu_session_lock() +#define UNLOCK() omega_gpu_session_unlock() + +static void cache_clear_locked(void) { + for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) + if (g_cache[i].used) { free(g_cache[i].kernel.code); omega_gpu_session_free(&g_cache[i].code); memset(&g_cache[i], 0, sizeof g_cache[i]); } +} + +void omega_gpu_elementwise_cache_clear(void) { + LOCK(); + cache_clear_locked(); + UNLOCK(); +} + +/* Called by the session (lock held, device still open) right before it closes. */ +static void on_session_close(void) { + cache_clear_locked(); + omega_gpu_session_scratch_free(&g_a); omega_gpu_session_scratch_free(&g_b); omega_gpu_session_scratch_free(&g_c); +} + +void omega_gpu_elementwise_test_set_mutant(int op) { + LOCK(); + g_mutant_op = op; g_mutant_level = 1; + cache_clear_locked(); + UNLOCK(); +} +void omega_gpu_elementwise_test_set_mutant_level(int op, int level) { + LOCK(); + g_mutant_op = op; g_mutant_level = level < 1 ? 1 : level > 3 ? 3 : level; + cache_clear_locked(); + UNLOCK(); +} + +/* Caller holds the lock. Builds the kernel if needed; uploads the device copy when a + * device is open (upload = false lets the host-only codegen path run without a chip). */ +static int kernel_for(OmegaGpuEwOp op, bool upload, Slot **out, bool *hit) { + int mutant = (g_mutant_op == (int)op) ? g_mutant_level : 0; + Slot *s = NULL; + for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) + if (g_cache[i].used && g_cache[i].op == (int)op && g_cache[i].mutant == mutant) { s = &g_cache[i]; *hit = true; break; } + if (!s) { + OmegaBlackwellKernel k; + int rc = build_kernel(op, mutant, &k, NULL, 0); + if (rc != OMEGA_GPU_EW_OK) return rc; + size_t slot = 0; + for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) if (!g_cache[i].used) { slot = i; break; } + s = &g_cache[slot]; + if (s->used) { free(s->kernel.code); omega_gpu_session_free(&s->code); } /* full: evict slot 0 */ + memset(s, 0, sizeof *s); + s->used = 1; s->op = (int)op; s->mutant = mutant; s->kernel = k; + *hit = false; + } + if (upload && !s->code.cpu) { + if (omega_gpu_session_alloc(s->kernel.code_size, &s->code) != 0) return OMEGA_GPU_EW_CHIP_FAIL; + memcpy(s->code.cpu, s->kernel.code, s->kernel.code_size); + __asm__ volatile("dsb sy" ::: "memory"); + } + *out = s; + return OMEGA_GPU_EW_OK; +} + +int omega_gpu_elementwise_codegen(OmegaGpuEwOp op, const uint32_t *dims, float eps, OmegaBlackwellKernel *kernel) { + (void)dims; (void)eps; /* kernels read their shape from the argument words */ + if (!kernel) return OMEGA_GPU_EW_BAD_ARGS; + LOCK(); + int mutant = (g_mutant_op == (int)op) ? g_mutant_level : 0; + UNLOCK(); + return build_kernel(op, mutant, kernel, NULL, 0); +} + +int omega_gpu_elementwise_codegen_ir(OmegaGpuEwOp op, int mutant, void *prog, OmegaBlackwellKernel *kernel) { + if (!kernel) return OMEGA_GPU_EW_BAD_ARGS; + if (mutant < 0 || mutant > 3) return OMEGA_GPU_EW_BAD_ARGS; + return build_kernel(op, mutant, kernel, (BlackwellIRProgram *)prog, 1); +} + +const char *omega_gpu_elementwise_last_error(void) { return omega_gpu_session_last_error(); } + +/* ------------------------------------------------------------ launcher */ + +#define EW_POISON 0xffbadbadu /* a NaN pattern no kernel here produces on purpose */ +#define EW_WAIT_MS 600000ull /* like main's load/store launchers; a 5 s wait failed under contention (omega#198) */ + +static uint64_t now_ns(void) { + struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); + return (uint64_t)t.tv_sec * 1000000000ULL + (uint64_t)t.tv_nsec; +} + +/* Caller holds the lock. Opens the shared device on first use. */ +static int dev_open_locked(void) { + if (!g_hooked) { (void)omega_gpu_session_on_close(on_session_close); g_hooked = true; } + return omega_gpu_session_open() ? OMEGA_GPU_EW_OK : OMEGA_GPU_EW_CHIP_FAIL; +} + +/* One launch: a and b staged in, c out (c_bytes/4 words, poison-filled first), through + * the shared session (omega_gpu_session_launch: shader-cache invalidate, constant bank, + * QMD0/QMD1, first marker, L2_FLUSH_DIRTY, second marker), then readback and the + * poison scan. b may be NULL when b_fill wrote the staging buffer already. */ +static int run_abc(const Slot *ks, const void *a, size_t a_bytes, const void *b, size_t b_bytes, + void *c, size_t c_bytes, uint32_t threads_x, uint32_t grid_x, + const uint32_t params[4], OmegaGpuEwInfo *info) { + if (omega_gpu_session_scratch(&g_a, a_bytes) || omega_gpu_session_scratch(&g_b, b_bytes) || omega_gpu_session_scratch(&g_c, c_bytes)) + return OMEGA_GPU_EW_CHIP_FAIL; + memcpy(g_a.mem.cpu, a, a_bytes); + if (b) memcpy(g_b.mem.cpu, b, b_bytes); + size_t c_words = c_bytes / 4; + uint32_t *c_dev = (uint32_t *)g_c.mem.cpu; + for (size_t i = 0; i < c_words; i++) c_dev[i] = EW_POISON; + __asm__ volatile("dsb sy" ::: "memory"); + + uint32_t args[OMEGA_BW_CBANK_MATMUL_ARGS_WORDS]; + args[0] = (uint32_t)g_a.mem.va; args[1] = (uint32_t)(g_a.mem.va >> 32); + args[2] = (uint32_t)g_b.mem.va; args[3] = (uint32_t)(g_b.mem.va >> 32); + args[4] = (uint32_t)g_c.mem.va; args[5] = (uint32_t)(g_c.mem.va >> 32); + args[6] = params[0]; args[7] = params[1]; args[8] = params[2]; args[9] = params[3]; + OmegaGpuLaunch L = { + .code_va = ks->code.va, .gpr_count = EW_GPR_BUDGET, .threads_x = threads_x, .threads_y = 1, + .grid_x = grid_x, .grid_y = 1, .num_elements = c_words, + .args = args, .n_args = OMEGA_BW_CBANK_MATMUL_ARGS_WORDS, .timeout_ms = EW_WAIT_MS, + }; + uint64_t elapsed = 0; uint32_t marker = 0; + if (omega_gpu_session_launch(&L, &elapsed, &marker) != 0) return OMEGA_GPU_EW_CHIP_FAIL; + + memcpy(c, g_c.mem.cpu, c_bytes); + uint32_t unwritten = 0; + for (size_t i = 0; i < c_words; i++) if (((uint32_t *)c)[i] == EW_POISON) unwritten++; + if (info) { + info->chip_calls++; + info->elapsed_ns += elapsed; + info->completion_marker = marker; + info->unwritten_words += unwritten; + info->threads_per_cta = threads_x; + info->ctas_last_launch = grid_x; + } + return unwritten ? OMEGA_GPU_EW_UNWRITTEN : OMEGA_GPU_EW_OK; +} + +/* ------------------------------------------------------- public entries */ + +static void info_begin(OmegaGpuEwInfo *info, const OmegaBlackwellKernel *k, bool hit) { + if (!info) return; + memset(info, 0, sizeof *info); + info->kernel_cache_hit = hit; + info->gpr_count = k->gpr_count; + info->insn_count = (uint32_t)k->insn_count; + snprintf(info->target_chip, sizeof info->target_chip, "NVIDIA DGX Spark (Grace Blackwell GB10)"); + info->sm_architecture = 121; +} +static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } +static void info_end(OmegaGpuEwInfo *info, uint64_t t0) { if (info) info->call_ns = now_ns() - t0; } + +/* Common prologue of every chip entry: lock, open, kernel (resident), info. */ +static int begin(OmegaGpuEwOp op, Slot **ks, OmegaGpuEwInfo *info) { + LOCK(); + int rc = dev_open_locked(); + bool hit = false; + if (rc == OMEGA_GPU_EW_OK) rc = kernel_for(op, true, ks, &hit); + if (rc != OMEGA_GPU_EW_OK) { UNLOCK(); return rc; } + info_begin(info, &(*ks)->kernel, hit); + return OMEGA_GPU_EW_OK; +} + +int omega_gpu_rmsnorm_f32(uint32_t rows, uint32_t dim, const float *x, const float *weight, + float eps, float *out, OmegaGpuEwInfo *info) { + uint64_t t0 = now_ns(); + if (!x || !weight || !out || rows == 0 || dim == 0 || dim % OMEGA_GPU_EW_THREADS != 0) return OMEGA_GPU_EW_BAD_ARGS; + if (dim > OMEGA_GPU_EW_MAX_DIM) return OMEGA_GPU_EW_TOO_LARGE; + Slot *k = NULL; + int rc = begin(OMEGA_GPU_EW_RMSNORM, &k, info); + if (rc != OMEGA_GPU_EW_OK) return rc; + uint32_t params[4] = { dim / OMEGA_GPU_EW_THREADS, f2u(1.0f / (float)dim), f2u(eps), dim }; + for (uint32_t r0 = 0; r0 < rows && rc == OMEGA_GPU_EW_OK; r0 += OMEGA_GPU_EW_MAX_CTAS) { + uint32_t nr = rows - r0 < OMEGA_GPU_EW_MAX_CTAS ? rows - r0 : OMEGA_GPU_EW_MAX_CTAS; + size_t bytes = (size_t)nr * dim * 4; + rc = run_abc(k, x + (size_t)r0 * dim, bytes, weight, (size_t)dim * 4, out + (size_t)r0 * dim, bytes, + OMEGA_GPU_EW_THREADS, nr, params, info); + } + UNLOCK(); + info_end(info, t0); + return rc; +} + +int omega_gpu_rope_f32(uint32_t heads, uint32_t head_dim, const float *v, + const float *cos_half, const float *sin_half, float *out, OmegaGpuEwInfo *info) { + uint64_t t0 = now_ns(); + if (!v || !cos_half || !sin_half || !out || heads == 0 || head_dim < 2 || (head_dim & 1)) return OMEGA_GPU_EW_BAD_ARGS; + if (head_dim > 2048) return OMEGA_GPU_EW_TOO_LARGE; + Slot *k = NULL; + int rc = begin(OMEGA_GPU_EW_ROPE, &k, info); + if (rc != OMEGA_GPU_EW_OK) return rc; + uint32_t half = head_dim / 2; + uint32_t params[4] = { head_dim, half, 0, 0 }; + for (uint32_t h0 = 0; h0 < heads && rc == OMEGA_GPU_EW_OK; h0 += OMEGA_GPU_EW_MAX_CTAS) { + uint32_t nh = heads - h0 < OMEGA_GPU_EW_MAX_CTAS ? heads - h0 : OMEGA_GPU_EW_MAX_CTAS; + size_t bytes = (size_t)nh * head_dim * 4; + /* table: cos in the first half of each head, sin in the second, written straight into + * the staging buffer (one copy per head of this launch) */ + if (omega_gpu_session_scratch(&g_b, bytes) != 0) { rc = OMEGA_GPU_EW_CHIP_FAIL; break; } + float *table = g_b.mem.cpu; + for (uint32_t h = 0; h < nh; h++) { + memcpy(table + (size_t)h * head_dim, cos_half, half * sizeof *table); + memcpy(table + (size_t)h * head_dim + half, sin_half, half * sizeof *table); + } + /* out may alias v: v is copied into the staging buffer before the result lands in out */ + rc = run_abc(k, v + (size_t)h0 * head_dim, bytes, NULL, bytes, out + (size_t)h0 * head_dim, bytes, half, nh, params, info); + } + UNLOCK(); + info_end(info, t0); + return rc; +} + +static int run_1d(OmegaGpuEwOp op, uint32_t n, const void *a, const void *b, void *c, OmegaGpuEwInfo *info) { + uint64_t t0 = now_ns(); + Slot *k = NULL; + int rc = begin(op, &k, info); + if (rc != OMEGA_GPU_EW_OK) return rc; + const uint32_t chunk = OMEGA_GPU_EW_MAX_CTAS * OMEGA_GPU_EW_THREADS; + for (uint32_t i0 = 0; i0 < n && rc == OMEGA_GPU_EW_OK; i0 += chunk) { + uint32_t ni = n - i0 < chunk ? n - i0 : chunk; + uint32_t grid = (ni + OMEGA_GPU_EW_THREADS - 1) / OMEGA_GPU_EW_THREADS; + uint32_t params[4] = { ni, 0, 0, 0 }; + size_t bytes = (size_t)ni * 4; + rc = run_abc(k, (const uint8_t *)a + (size_t)i0 * 4, bytes, b ? (const uint8_t *)b + (size_t)i0 * 4 : a, bytes, + (uint8_t *)c + (size_t)i0 * 4, bytes, OMEGA_GPU_EW_THREADS, grid, params, info); + } + UNLOCK(); + info_end(info, t0); + return rc; +} + +int omega_gpu_swiglu_f32(uint32_t n, const float *gate, const float *up, float *out, OmegaGpuEwInfo *info) { + if (!gate || !up || !out || n == 0) return OMEGA_GPU_EW_BAD_ARGS; + return run_1d(OMEGA_GPU_EW_SWIGLU, n, gate, up, out, info); +} +int omega_gpu_ex2_f32(uint32_t n, const float *x, float *out, OmegaGpuEwInfo *info) { + if (!x || !out || n == 0) return OMEGA_GPU_EW_BAD_ARGS; + return run_1d(OMEGA_GPU_EW_EX2, n, x, NULL, out, info); +} +int omega_gpu_shared_xchg_u32(uint32_t n, const uint32_t *x, uint32_t *out, OmegaGpuEwInfo *info) { + if (!x || !out || n == 0 || n % OMEGA_GPU_EW_THREADS != 0) return OMEGA_GPU_EW_BAD_ARGS; + return run_1d(OMEGA_GPU_EW_XCHG, n, x, NULL, out, info); +} +int omega_gpu_reconv_probe_u32(OmegaGpuEwOp op, uint32_t n, const uint32_t *a, const uint32_t *b, uint32_t *out, OmegaGpuEwInfo *info) { + if (!a || !b || !out || n == 0) return OMEGA_GPU_EW_BAD_ARGS; + switch (op) { + case OMEGA_GPU_EW_RC_DIAMOND: case OMEGA_GPU_EW_RC_LOOPBREAK: case OMEGA_GPU_EW_RC_NESTED: + case OMEGA_GPU_EW_RC_EXIT_BAR: case OMEGA_GPU_EW_RC_DEPTH16: break; + default: return OMEGA_GPU_EW_BAD_ARGS; + } + return run_1d(op, n, a, b, out, info); +} + +const char *omega_gpu_elementwise_rc_name(int rc) { + switch (rc) { + case OMEGA_GPU_EW_OK: return "OK"; + case OMEGA_GPU_EW_BAD_ARGS: return "BAD_ARGS"; + case OMEGA_GPU_EW_TOO_LARGE: return "TOO_LARGE"; + case OMEGA_GPU_EW_CODEGEN_FAIL: return "CODEGEN_FAIL"; + case OMEGA_GPU_EW_CHIP_FAIL: return "CHIP_FAIL"; + case OMEGA_GPU_EW_UNWRITTEN: return "UNWRITTEN"; + default: return "UNKNOWN"; + } +} diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4-0a1812f/omega_gpu_elementwise_api.h b/evidence/gpu-reconv-308/T3/FB1-CUT4-0a1812f/omega_gpu_elementwise_api.h new file mode 100644 index 000000000..8b92c839d --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4-0a1812f/omega_gpu_elementwise_api.h @@ -0,0 +1,177 @@ +#ifndef OMEGA_GPU_ELEMENTWISE_API_H +#define OMEGA_GPU_ELEMENTWISE_API_H +/* + * omega_gpu_elementwise_api: native (no CUDA) elementwise and row-reduction + * kernels for the AIEN inference stack (FB-1 cut 4, 2026-10-04). + * + * Three inference ops plus two probe ops, every kernel generated by our own + * codegen (src/omega_blackwell_codegen.c IR) and launched through the same + * chip-proven sequence as the tensor matmul path: all buffers GPU-uncached + * (nvos.h NVOS32_ATTR2_GPU_CACHEABLE: system memory cached by the GPU is not + * coherent with CPU mappings), first marker, L2_FLUSH_DIRTY, second marker + * (C3 tail), poison-filled output scanned for unwritten words after readback. + * + * The math follows aien-sovereign-core crates/aien-inference-abi/src/tensor.rs + * (ReferenceCpuBackend), read 2026-10-04 at main 5fdab70: + * rmsnorm: scale = 1/sqrt(mean(x^2) + eps) (oracle sums in f64; the chip + * sums in f32 and uses MUFU.RSQ refined by one Newton step), + * out[i] = (x[i] * scale) * weight[i] in that order. + * rope: rotate_half pairs (i, i + head_dim/2) per head, cos/sin tables + * supplied by the host as f32 (the oracle casts f64 cos/sin to f32 + * before the f32 math): out0 = q0*cos - q1*sin, out1 = q0*sin + q1*cos, + * emitted as FMUL, FMUL, FADD (no fused multiply-add), so the chip + * result is bit-identical to the f32 oracle. + * swiglu: out = silu(gate) * up, silu(g) = g / (1 + exp(-g)); the chip uses + * MUFU.EX2(g * -log2(e)) and MUFU.RCP (approximate units), the + * oracle computes in f64. Tolerance is stated per op in the test. + * + * Cut 4b (2026-10-04): the device, channel and launch scratch are the process-wide + * session shared with the matmul and attention APIs (omega_gpu_session.h): one open + * per process instead of one per call (~40 ms), kernels resident after first use, + * staging buffers reused at their high-water mark. Calls serialise on one lock. + * + * Launch envelope: at most OMEGA_GPU_EW_MAX_CTAS CTAs per launch (the matmul + * sweep of 2026-10-03 lost whole tiles at 128 CTAs, investigation I42); the + * API splits larger work on the host and makes several launches. + */ +#include +#include +#include +#include "omega_blackwell_matmul.h" /* OmegaBlackwellKernel */ + +#ifdef __cplusplus +extern "C" { +#endif + +enum { + OMEGA_GPU_EW_OK = 0, + OMEGA_GPU_EW_BAD_ARGS = -1, /* NULL pointer, zero dimension, or a shape rule below broken */ + OMEGA_GPU_EW_TOO_LARGE = -2, /* dimension outside the codegen limits */ + OMEGA_GPU_EW_CODEGEN_FAIL = -3, /* our codegen refused or exceeded the register budget */ + OMEGA_GPU_EW_CHIP_FAIL = -4, /* device open, submit, marker or release failed */ + OMEGA_GPU_EW_UNWRITTEN = -5 /* output words still equal to their poison after the run */ +}; + +/* Kernel families. The two probes exist to prove the new instructions on the + * chip with the smallest possible kernels (and are kept for regression). */ +typedef enum { + OMEGA_GPU_EW_EX2 = 1, /* out[i] = 2^a[i] (probe for MUFU.EX2) */ + OMEGA_GPU_EW_XCHG, /* out[i] = a[i ^ (T-1)] inside each CTA (probe for STS/BAR.SYNC/LDS) */ + OMEGA_GPU_EW_RMSNORM, + OMEGA_GPU_EW_ROPE, + OMEGA_GPU_EW_SWIGLU, + OMEGA_GPU_EW_PRIME_SIEVE, /* prime race (2026-10-05): word-parallel odd-only sieve, codegen only + * here; launched by bench/prime_race/gb10_native.c (argument words in + * gen_prime_sieve's comment) */ + OMEGA_GPU_EW_PRIME_SIEVE_SHARED, /* the same sieve with the table staged in shared memory (C3) */ + /* Structured warp reconvergence (omega #308). The four sieves below are the production sieve + * with its lane-dependent loops put back inside BSSY/BSYNC regions (regression R1): DIV1 wraps + * the original mark loop (omega 888a011), DIV2 also wraps the prime loop (nested regions). */ + OMEGA_GPU_EW_PRIME_SIEVE_DIV1, + OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1, + OMEGA_GPU_EW_PRIME_SIEVE_DIV2, + OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2, + /* Probes for omega_gpu_reconv_probe_u32 (u32 in a, b; u32 out; oracle in tests/gpu_reconv_test.c) */ + OMEGA_GPU_EW_RC_DIAMOND, /* R3: if/else, sides of different length, SHFL after the join */ + OMEGA_GPU_EW_RC_LOOPBREAK, /* R4: loop with a lane-dependent break */ + OMEGA_GPU_EW_RC_NESTED, /* R5: a lane-dependent loop region inside an if region */ + OMEGA_GPU_EW_RC_EXIT_BAR, /* R6: lanes past the end + STS/BAR.SYNC/LDS exchange + SHFL, lanes exit last */ + OMEGA_GPU_EW_RC_DEPTH16, /* 16 nested regions: every barrier register B0..B15 in use */ + /* Codegen refusals: omega_gpu_elementwise_codegen_ir must return CODEGEN_FAIL for each */ + OMEGA_GPU_EW_RC_REFUSE_DEPTH17, /* a 17th nested region: no barrier register left */ + OMEGA_GPU_EW_RC_REFUSE_EXIT_SHFL, /* lanes EXIT, then BAR.SYNC/SHFL on the rest */ + OMEGA_GPU_EW_RC_REFUSE_EXIT_IN_REGION, /* EXIT inside an open region */ + OMEGA_GPU_EW_RC_REFUSE_BAR_IN_REGION, /* BAR.SYNC inside an open region */ + OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION, /* SHFL inside an open region */ + OMEGA_GPU_EW_RC_REFUSE_UNCLOSED, /* region begun, never joined */ + OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY /* BSYNC with no BSSY */ +} OmegaGpuEwOp; + +/* Shape rules (checked, refused with BAD_ARGS / TOO_LARGE): + * rmsnorm: dim % OMEGA_GPU_EW_THREADS == 0, dim <= OMEGA_GPU_EW_MAX_DIM, rows >= 1 + * rope: head_dim even, 2 <= head_dim <= 2048 (head_dim/2 threads per CTA), heads >= 1 + * swiglu: n >= 1 (the kernel guards the tail with a predicated EXIT) + * ex2: n >= 1; xchg: n % OMEGA_GPU_EW_THREADS == 0 */ +#define OMEGA_GPU_EW_THREADS 128u +#define OMEGA_GPU_EW_MAX_DIM 16384u +#define OMEGA_GPU_EW_MAX_CTAS 64u + +typedef struct { + uint32_t chip_calls; /* launches made */ + uint64_t elapsed_ns; /* sum of launch-to-second-marker over the launches */ + uint32_t completion_marker; /* observed first marker word of the last launch */ + uint32_t unwritten_words; /* output words still equal to poison (0 on success) */ + bool kernel_cache_hit; /* the shape's kernel was already generated in this process */ + uint32_t gpr_count; /* registers the generated kernel uses */ + uint32_t insn_count; + uint32_t threads_per_cta; + uint32_t ctas_last_launch; + char target_chip[64]; + uint32_t sm_architecture; + uint64_t call_ns; /* cut 4b: wall time of the whole API call (staging, launches, copy-out) */ +} OmegaGpuEwInfo; + +/* out[r*dim + i] = x[r*dim + i] * scale_r * weight[i]; scale_r = 1/sqrt(mean_i(x^2) + eps). */ +int omega_gpu_rmsnorm_f32(uint32_t rows, uint32_t dim, const float *x, const float *weight, + float eps, float *out, OmegaGpuEwInfo *info); + +/* v holds `heads` heads of head_dim values back to back; cos_half and sin_half + * hold head_dim/2 values each (cos(pos*freq_i), sin(pos*freq_i)); out gets the + * rotated heads (may alias v: the API copies through its own buffers). */ +int omega_gpu_rope_f32(uint32_t heads, uint32_t head_dim, const float *v, + const float *cos_half, const float *sin_half, float *out, + OmegaGpuEwInfo *info); + +/* out[i] = silu(gate[i]) * up[i] for i < n. */ +int omega_gpu_swiglu_f32(uint32_t n, const float *gate, const float *up, float *out, + OmegaGpuEwInfo *info); + +/* Probes. ex2: out[i] = 2^x[i]. xchg: out[i] = x[i ^ (OMEGA_GPU_EW_THREADS-1)] within each CTA. */ +int omega_gpu_ex2_f32(uint32_t n, const float *x, float *out, OmegaGpuEwInfo *info); +int omega_gpu_shared_xchg_u32(uint32_t n, const uint32_t *x, uint32_t *out, OmegaGpuEwInfo *info); +/* Reconvergence probes (op = one of OMEGA_GPU_EW_RC_DIAMOND .. OMEGA_GPU_EW_RC_DEPTH16, else + * BAD_ARGS). n >= 1, any n: lanes past the end take part in the warp step and exit before + * their store. out[i] = r_i + 4099 * r_(i+1 in warp); r per op in src/omega_gpu_elementwise_api.c. */ +int omega_gpu_reconv_probe_u32(OmegaGpuEwOp op, uint32_t n, const uint32_t *a, const uint32_t *b, + uint32_t *out, OmegaGpuEwInfo *info); + +/* Generate (without launching) the kernel for one op. Kernels read their shape + * from the argument words, so one kernel per op serves every shape; dims and eps + * are accepted for forward compatibility and ignored today (may be NULL / 0). + * Lets tests disassemble the bytes offline (nvdisasm) and check the register + * budget. Caller frees kernel->code. */ +int omega_gpu_elementwise_codegen(OmegaGpuEwOp op, const uint32_t *dims, float eps, + OmegaBlackwellKernel *kernel); + +/* Same, with the mutant chosen by the caller (0 = production kernel, 1 = the op's math mutant, + * 2 = the reconvergence join (BSYNC) dropped, 3 = the BSSY names the wrong join; 2 and 3 change + * only kernels that open a region, the host simulator must catch both) and, when prog is + * not NULL, a copy of the allocated IR program (a BlackwellIRProgram, post register + * allocation) for a host IR simulator. Does not touch the device or the kernel cache. + * Caller frees kernel->code. */ +int omega_gpu_elementwise_codegen_ir(OmegaGpuEwOp op, int mutant, void *prog, + OmegaBlackwellKernel *kernel); + +/* TEST ONLY. Selects a deliberately wrong kernel for one op (0 clears). Each + * mutant changes the math by far more than the op's tolerance, so a parity + * check that still passes against it proves nothing. Clears the kernel cache. + * EX2: computes 2^(x+1). XCHG: reads its own shared word (no exchange). + * RMSNORM: the warp tree drops its last SHFL step (half the lanes go missing). + * ROPE: out0 = q0*cos + q1*sin. SWIGLU: exp(+g) instead of exp(-g). */ +void omega_gpu_elementwise_test_set_mutant(int op); +/* TEST ONLY. Like test_set_mutant, choosing the mutant number (1 math, 2 join dropped, 3 join + * corrupted) that the next launch of `op` builds; lets a chip test observe what a missing + * reconvergence does (the simulator must have flagged it first). */ +void omega_gpu_elementwise_test_set_mutant_level(int op, int level); + +const char *omega_gpu_elementwise_rc_name(int rc); +void omega_gpu_elementwise_cache_clear(void); + +/* Stage name of the most recent CHIP_FAIL ("" if none), shared with the other GPU APIs + * (omega_gpu_session_last_error); an open failure carries the driver text. */ +const char *omega_gpu_elementwise_last_error(void); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4-0a1812f/receipt.json b/evidence/gpu-reconv-308/T3/FB1-CUT4-0a1812f/receipt.json new file mode 100644 index 000000000..7c187a1ec --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4-0a1812f/receipt.json @@ -0,0 +1 @@ +{"schema":"OMEGA_GPU_ELEMENTWISE_V1","tolerances":{"ex2_rel":1e-6,"xchg":"exact","rmsnorm_rel":1e-5,"rmsnorm_abs":1e-6,"rope":"exact","swiglu_rel":1e-5,"swiglu_abs":1e-6},"cases":[{"case":"ex2_probe","n":4096,"rc":"OK","violations":0,"worst_scaled_err":0.152538,"pass":true,"chip_elapsed_ns":112769,"chip_calls":1,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":4096,"mutant_caught":true},{"case":"shared_xchg_probe","n":4096,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":103345,"chip_calls":1,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":4096,"mutant_caught":true},{"case":"rmsnorm_4x2048","n":8192,"rc":"OK","violations":0,"worst_scaled_err":0.0244808,"pass":true,"chip_elapsed_ns":103857,"chip_calls":1,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":8192,"mutant_caught":true},{"case":"rmsnorm_1x256","n":256,"rc":"OK","violations":0,"worst_scaled_err":0.0206888,"pass":true,"chip_elapsed_ns":103121,"chip_calls":1,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":256,"mutant_caught":true},{"case":"rmsnorm_2x8192","n":16384,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":104001,"chip_calls":1,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":16384,"mutant_caught":true},{"case":"rope_32h_64d_pos17_theta10000","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":103473,"chip_calls":1,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":1024,"mutant_caught":true},{"case":"rope_4h_64d_pos17_theta10000","n":256,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":102737,"chip_calls":1,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":128,"mutant_caught":true},{"case":"rope_32h_64d_pos1023_theta500000","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":102673,"chip_calls":1,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":1024,"mutant_caught":true},{"case":"rope_8h_64d_pos1023_theta500000","n":512,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":102721,"chip_calls":1,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":256,"mutant_caught":true},{"case":"swiglu_5632","n":5632,"rc":"OK","violations":0,"worst_scaled_err":0.0196297,"pass":true,"chip_elapsed_ns":103505,"chip_calls":1,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":5630,"mutant_caught":true},{"case":"swiglu_8192","n":8192,"rc":"OK","violations":0,"worst_scaled_err":0.0177689,"pass":true,"chip_elapsed_ns":102801,"chip_calls":1,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":8192,"mutant_caught":true},{"case":"swiglu_1000","n":1000,"rc":"OK","violations":0,"worst_scaled_err":0.017264,"pass":true,"chip_elapsed_ns":102641,"chip_calls":1,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":1000,"mutant_caught":true},{"case":"icache_reuse_rotation","n":20,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":2062370,"chip_calls":20,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":0,"mutant_caught":true}],"device_opens":1,"checks":31,"failed":0,"verdict":"OMEGA_GPU_ELEMENTWISE_PASS"} diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4-0a1812f/run.log b/evidence/gpu-reconv-308/T3/FB1-CUT4-0a1812f/run.log new file mode 100644 index 000000000..a6356574f --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4-0a1812f/run.log @@ -0,0 +1,15 @@ +PASS ex2_probe n=4096 rc=OK violations=0 worst_scaled_err=0.152538 chip_ns=112769 calls=1 unwritten=0 | mutant rc=OK violations=4096 CAUGHT +PASS shared_xchg_probe n=4096 rc=OK violations=0 worst_scaled_err=0 chip_ns=103345 calls=1 unwritten=0 | mutant rc=OK violations=4096 CAUGHT +PASS rmsnorm_4x2048 n=8192 rc=OK violations=0 worst_scaled_err=0.0244808 chip_ns=103857 calls=1 unwritten=0 | mutant rc=OK violations=8192 CAUGHT +PASS rmsnorm_1x256 n=256 rc=OK violations=0 worst_scaled_err=0.0206888 chip_ns=103121 calls=1 unwritten=0 | mutant rc=OK violations=256 CAUGHT +PASS rmsnorm_2x8192 n=16384 rc=OK violations=0 worst_scaled_err=0 chip_ns=104001 calls=1 unwritten=0 | mutant rc=OK violations=16384 CAUGHT +PASS rope_32h_64d_pos17_theta10000 n=2048 rc=OK violations=0 worst_scaled_err=0 chip_ns=103473 calls=1 unwritten=0 | mutant rc=OK violations=1024 CAUGHT +PASS rope_4h_64d_pos17_theta10000 n=256 rc=OK violations=0 worst_scaled_err=0 chip_ns=102737 calls=1 unwritten=0 | mutant rc=OK violations=128 CAUGHT +PASS rope_32h_64d_pos1023_theta500000 n=2048 rc=OK violations=0 worst_scaled_err=0 chip_ns=102673 calls=1 unwritten=0 | mutant rc=OK violations=1024 CAUGHT +PASS rope_8h_64d_pos1023_theta500000 n=512 rc=OK violations=0 worst_scaled_err=0 chip_ns=102721 calls=1 unwritten=0 | mutant rc=OK violations=256 CAUGHT +PASS swiglu_5632 n=5632 rc=OK violations=0 worst_scaled_err=0.0196297 chip_ns=103505 calls=1 unwritten=0 | mutant rc=OK violations=5630 CAUGHT +PASS swiglu_8192 n=8192 rc=OK violations=0 worst_scaled_err=0.0177689 chip_ns=102801 calls=1 unwritten=0 | mutant rc=OK violations=8192 CAUGHT +PASS swiglu_1000 n=1000 rc=OK violations=0 worst_scaled_err=0.017264 chip_ns=102641 calls=1 unwritten=0 | mutant rc=OK violations=1000 CAUGHT +PASS icache_reuse_rotation n=20 rc=OK violations=0 worst_scaled_err=0 chip_ns=2062370 calls=20 unwritten=0 | mutant rc=OK violations=0 CAUGHT +device_opens=1 +PASS: 31 checks, 0 failed diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/SHA256SUMS b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/SHA256SUMS new file mode 100644 index 000000000..6c53f3c00 --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/SHA256SUMS @@ -0,0 +1,35 @@ +766ef6a72b8c8376b036a2ada0a4ddb83fef1e405b3642cefb7edd281aa50919 attention.json +b6cda883af78e533d75e4ba90a5d3ec1e4971b850114865e14544a388f6f9be3 attention.log +3c248082506713673231fcdde5591bf9fb3bc1d19cf86a343b398f8cf6203678 attention_sim.log +ef16aedf5d2ee5f7eba9c03965ad07a34b6e5d566381c762cacc67babb4a8d28 attention_timing.json +4d8363252dbbe7db09cc637906623516d1de62c495f9325bfd7292d945d498bb attention_timing.log +37f9747742c642bb5384334f9ed11cfa4cf4ebb153fbeb00938ecbde38fb0c25 build.log +297f2c577c3e7bf87f5dd76915f9c9f0e1070241bc796bc0d3eb8d4a8d96ec50 device.txt +d9752f6d36a402f1b2afe5f5eb83a8c29eb97c7d21216efc8b61b44e4591a36d elementwise.json +2a18e8ea110253c0c9dc6109be882a9c7f8cd4645a853ffe0ddd0ec5f2525b11 elementwise.log +2f1508ee7abf7e280d6c5ee968cab7eb2d09e5aad4cd96bd139687a924109dd2 elementwise_timing.json +6fe8df4574855cb295c6ce46467b676b5539f5a552cf25892614ffa35e2a429e elementwise_timing.log +8d2c2416ed5afb7d373f50603e1a2f3cd6989083cc04b97439d4dfe648d2f3ce gpu_attention_test.c +4cb4b438f6c6a95280820884c30b86eb7f7021123e0dc97fcd14b6dfd5eef9f6 gpu_elementwise_test.c +c5009757fbcae237d51037644b11bcac5dddd99b136d9e6b8eb35d715198d210 gpu_matmul_api_test.c +5cfee6110c917944618e5fe621b8289afc6f4c4a54a6fd5e50e3f9ebb906fbc9 matmul.json +924b9a303efd672cc33d85dc52132bf3f559db47c922e15f5de647c566728ec0 matmul.log +903bd038c9b29d32209c2bcd0ace99675dcf79081c8e7d147155942488235eca matmul_timing.json +a2a5184d8989be8b815b27e3ba3b044adf9308b7934d87b85c9ef121c11c1267 matmul_timing.log +d5c7d6bb8106cf0c161089d37ada76c2b98ab3a94d659d615d6463c786c7d259 omega_gpu_attention_api.c +c72f7082f59fd061c1a8287d8a18aac2d7ff612959539fe616d99416a3043246 omega_gpu_attention_api.h +bd2346fb86eed5e3be35c49c970c25adf1621043d6e4e4ba02e990375e5859c7 omega_gpu_elementwise_api.c +4d9eb1b5fd55e168f0cd4a96e906957376d88bb5efbe1f08633fcdfa52ce8fba omega_gpu_elementwise_api.h +94ca4af1995091a20b6a7740e579dd785eefc961e3ec3dfcdca20ebb4c6d2218 omega_gpu_matmul_api.c +f0b9caa347b2052c6d869506a041e5c94ff8f710bb84adddf884af1220a81116 omega_gpu_session.c +b4a0f92db1e7aa1c630cc11dfb8cc231b9dc51c6e4b2c64549bb6364ba13f8e9 omega_gpu_session.h +b5eff20771e2ba1cc23532b7d66b65d736ad506d5244807b133624435c6eb361 probe.log +95ae6028890f85dd718fb2f7bd2624f67dd637fe34f2ad450e0d32c02b2857da probe/SHA256SUMS +9e1be089bf7bd93044a938c9ad72a3de42599c8d53317c3ecc5b18dc0dce2728 probe/build.log +6f4e9e5ba150b22a10b1caf37aa4d6f903131963de7256448032b475e52b8879 probe/device.txt +e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 probe/failures.txt +2cf1630f99767bd7a9ab1e1f0940516dd3b534dd71906ef0a6c7d8e7ae0c63c8 probe/gpu_session_probe.c +673650f936cb3b0a2f93ce09d81be10748b1b203c19e8176b4eefc1964a0cf3a probe/ok_count.txt +f0b9caa347b2052c6d869506a041e5c94ff8f710bb84adddf884af1220a81116 probe/omega_gpu_session.c +98f7a2638602c63259579bedd946ad78058fcbae8f3f72f2af072ce1d8173a1f probe/probe.log +9068ccc6a7f0b9dd56048750f312739bc60fc56d2b6df4cfc81876aba7b05e49 probe/probe_gpu_open.sh diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/attention.json b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/attention.json new file mode 100644 index 000000000..ceb5269bc --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/attention.json @@ -0,0 +1 @@ +{"schema":"OMEGA_GPU_ATTENTION_V1","tolerances":{"rel":2e-4,"abs":2e-5,"large_scores_rel":1e-3,"large_scores_abs":1e-4},"cases":[{"case":"gqa_f32_tinyllama_32q4kv_ctx1","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":111425,"call_ns":28621046,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":2048,"kv_bytes_naive":2048,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_tinyllama_32q4kv_ctx17","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00189431,"pass":true,"chip_elapsed_ns":103537,"call_ns":224306,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":34816,"kv_bytes_naive":34816,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_tinyllama_32q4kv_ctx256","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00189409,"pass":true,"chip_elapsed_ns":268754,"call_ns":423092,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":524288,"kv_bytes_naive":524288,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_tinyllama_32q4kv_ctx2048","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.0028582,"pass":true,"chip_elapsed_ns":1120154,"call_ns":1742335,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":4194304,"kv_bytes_naive":4194304,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":4,"mutants_caught":4,"mutant_note":"SLOT:CAUGHT(rc=OK,bad=2041) NO_RESCALE:CAUGHT(rc=OK,bad=1959) Q_ROW:CAUGHT(rc=OK,bad=2048) OUT_ROW:CAUGHT(rc=OK,bad=2048)"},{"case":"gqa_f32_tinyllama_32q4kv_ctx65","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00174821,"pass":true,"chip_elapsed_ns":103057,"call_ns":193570,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":133120,"kv_bytes_naive":133120,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_tinyllama_32q4kv_ctx128","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00197914,"pass":true,"chip_elapsed_ns":102753,"call_ns":107233,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":262144,"kv_bytes_naive":262144,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_tinyllama_32q4kv_ctx129","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00177573,"pass":true,"chip_elapsed_ns":102865,"call_ns":107329,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":264192,"kv_bytes_naive":264192,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_tinyllama_32q4kv_ctx300","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00259576,"pass":true,"chip_elapsed_ns":205122,"call_ns":214418,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":614400,"kv_bytes_naive":614400,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_tinyllama_32q4kv_ctx1000","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00247545,"pass":true,"chip_elapsed_ns":590053,"call_ns":631749,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":2048000,"kv_bytes_naive":2048000,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_llama32_1b_32q8kv_ctx1","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":104017,"call_ns":215458,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":4096,"kv_bytes_naive":4096,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_llama32_1b_32q8kv_ctx17","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00174276,"pass":true,"chip_elapsed_ns":102657,"call_ns":105473,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":69632,"kv_bytes_naive":69632,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":1,"mutants_caught":1,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=2048)"},{"case":"gqa_f32_llama32_1b_32q8kv_ctx256","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00161378,"pass":true,"chip_elapsed_ns":208018,"call_ns":309298,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":1048576,"kv_bytes_naive":1048576,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_llama32_1b_32q8kv_ctx2048","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.0029454,"pass":true,"chip_elapsed_ns":1118074,"call_ns":2342084,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":8388608,"kv_bytes_naive":8388608,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":4,"mutants_caught":4,"mutant_note":"SLOT:CAUGHT(rc=OK,bad=2045) NO_RESCALE:CAUGHT(rc=OK,bad=1904) Q_ROW:CAUGHT(rc=OK,bad=2042) OUT_ROW:CAUGHT(rc=OK,bad=2042)"},{"case":"gqa_f32_llama32_1b_32q8kv_ctx65","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.0014332,"pass":true,"chip_elapsed_ns":105249,"call_ns":193650,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":266240,"kv_bytes_naive":266240,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_llama32_1b_32q8kv_ctx128","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00186415,"pass":true,"chip_elapsed_ns":103249,"call_ns":111201,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":524288,"kv_bytes_naive":524288,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_llama32_1b_32q8kv_ctx129","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00185519,"pass":true,"chip_elapsed_ns":103393,"call_ns":111281,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":528384,"kv_bytes_naive":528384,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_llama32_1b_32q8kv_ctx300","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00162705,"pass":true,"chip_elapsed_ns":206242,"call_ns":225298,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":1228800,"kv_bytes_naive":1228800,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_llama32_1b_32q8kv_ctx1000","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00330327,"pass":true,"chip_elapsed_ns":612149,"call_ns":722534,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":4096000,"kv_bytes_naive":4096000,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"gqa_f32_large_scores_32q4kv_ctx256","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.105395,"pass":true,"chip_elapsed_ns":205138,"call_ns":333523,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":524288,"kv_bytes_naive":524288,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":1,"mutants_caught":1,"mutant_note":"NO_MAX:CAUGHT(rc=OK,bad=2048)"},{"case":"gqa_f32_mha_16q16kv_ctx300","n":1024,"rc":"OK","violations":0,"worst_scaled_err":0.00197746,"pass":true,"chip_elapsed_ns":207058,"call_ns":358803,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":2457600,"kv_bytes_naive":2457600,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":3,"mutants_caught":3,"mutant_note":"Q_ROW:CAUGHT(rc=OK,bad=1022) OUT_ROW:CAUGHT(rc=OK,bad=1024) KV_HEAD:CAUGHT(rc=OK,bad=1024)"},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx1","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":104353,"call_ns":186690,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":16384,"kv_bytes_naive":16384,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":1,"mutants_caught":1,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=2048)"},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx17","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.002096,"pass":true,"chip_elapsed_ns":103249,"call_ns":179617,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":32768,"kv_bytes_naive":32768,"kv_blocks_unique":2,"kv_blocks_logical":2,"mutants_run":2,"mutants_caught":2,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=2046) SLOT:CAUGHT(rc=OK,bad=2048)"},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx256","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00199949,"pass":true,"chip_elapsed_ns":205346,"call_ns":286306,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":262144,"kv_bytes_naive":262144,"kv_blocks_unique":16,"kv_blocks_logical":16,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx2048","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00336531,"pass":true,"chip_elapsed_ns":1023833,"call_ns":1081706,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":2097152,"kv_bytes_naive":2097152,"kv_blocks_unique":128,"kv_blocks_logical":128,"mutants_run":5,"mutants_caught":5,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=2045) SLOT:CAUGHT(rc=OK,bad=2042) NO_RESCALE:CAUGHT(rc=OK,bad=2023) Q_ROW:CAUGHT(rc=OK,bad=2042) OUT_ROW:CAUGHT(rc=OK,bad=2042)"},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx65","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00182741,"pass":true,"chip_elapsed_ns":103489,"call_ns":187489,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":81920,"kv_bytes_naive":81920,"kv_blocks_unique":5,"kv_blocks_logical":5,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx128","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00191997,"pass":true,"chip_elapsed_ns":173761,"call_ns":177361,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":131072,"kv_bytes_naive":131072,"kv_blocks_unique":8,"kv_blocks_logical":8,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx129","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00192425,"pass":true,"chip_elapsed_ns":102945,"call_ns":106625,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":147456,"kv_bytes_naive":147456,"kv_blocks_unique":9,"kv_blocks_logical":9,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx300","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00243899,"pass":true,"chip_elapsed_ns":204289,"call_ns":210097,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":311296,"kv_bytes_naive":311296,"kv_blocks_unique":19,"kv_blocks_logical":19,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_tinyllama_32q4kv_bs16_ctx1000","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00547531,"pass":true,"chip_elapsed_ns":204690,"call_ns":208818,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":163840,"kv_bytes_naive":1032192,"kv_blocks_unique":10,"kv_blocks_logical":63,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx1","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":102880,"call_ns":187697,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":32768,"kv_bytes_naive":32768,"kv_blocks_unique":1,"kv_blocks_logical":1,"mutants_run":1,"mutants_caught":1,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=2048)"},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx17","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00223287,"pass":true,"chip_elapsed_ns":103217,"call_ns":180145,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":65536,"kv_bytes_naive":65536,"kv_blocks_unique":2,"kv_blocks_logical":2,"mutants_run":2,"mutants_caught":2,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=2048) SLOT:CAUGHT(rc=OK,bad=2048)"},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx256","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00167964,"pass":true,"chip_elapsed_ns":205218,"call_ns":292675,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":524288,"kv_bytes_naive":524288,"kv_blocks_unique":16,"kv_blocks_logical":16,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx2048","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00358689,"pass":true,"chip_elapsed_ns":1025577,"call_ns":1147898,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":4194304,"kv_bytes_naive":4194304,"kv_blocks_unique":128,"kv_blocks_logical":128,"mutants_run":5,"mutants_caught":5,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=2048) SLOT:CAUGHT(rc=OK,bad=2045) NO_RESCALE:CAUGHT(rc=OK,bad=1999) Q_ROW:CAUGHT(rc=OK,bad=2044) OUT_ROW:CAUGHT(rc=OK,bad=2044)"},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx65","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00228809,"pass":true,"chip_elapsed_ns":105216,"call_ns":195297,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":163840,"kv_bytes_naive":163840,"kv_blocks_unique":5,"kv_blocks_logical":5,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx128","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00160376,"pass":true,"chip_elapsed_ns":103009,"call_ns":107873,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":262144,"kv_bytes_naive":262144,"kv_blocks_unique":8,"kv_blocks_logical":8,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx129","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00173996,"pass":true,"chip_elapsed_ns":102961,"call_ns":108753,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":294912,"kv_bytes_naive":294912,"kv_blocks_unique":9,"kv_blocks_logical":9,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx300","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00303696,"pass":true,"chip_elapsed_ns":206705,"call_ns":217409,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":622592,"kv_bytes_naive":622592,"kv_blocks_unique":19,"kv_blocks_logical":19,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_llama32_1b_32q8kv_bs16_ctx1000","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00420311,"pass":true,"chip_elapsed_ns":206434,"call_ns":213026,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":327680,"kv_bytes_naive":2064384,"kv_blocks_unique":10,"kv_blocks_logical":63,"mutants_run":0,"mutants_caught":0,"mutant_note":""},{"case":"paged_bf16_batch_3seq_32q8kv_ctx40_0_100","n":6144,"rc":"OK","violations":0,"worst_scaled_err":0.00181424,"pass":true,"chip_elapsed_ns":207602,"call_ns":460084,"chip_calls":2,"unwritten_words":0,"kv_bytes_staged":229376,"kv_bytes_naive":229376,"kv_blocks_unique":7,"kv_blocks_logical":7,"mutants_run":4,"mutants_caught":4,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=4095) SLOT:CAUGHT(rc=OK,bad=4090) Q_ROW:CAUGHT(rc=OK,bad=4096) OUT_ROW:CAUGHT(rc=OK,bad=4096)"},{"case":"paged_bf16_shared_prefix_4seq_8q2kv_10pre","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0.00188498,"pass":true,"chip_elapsed_ns":205218,"call_ns":287395,"chip_calls":1,"unwritten_words":0,"kv_bytes_staged":155648,"kv_bytes_naive":401408,"kv_blocks_unique":19,"kv_blocks_logical":49,"mutants_run":2,"mutants_caught":2,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=2048) SLOT:CAUGHT(rc=OK,bad=2046)"},{"case":"paged_bf16_shared_prefix_4seq_32q8kv_10pre","n":8192,"rc":"OK","violations":0,"worst_scaled_err":0.0024233,"pass":true,"chip_elapsed_ns":412676,"call_ns":519172,"chip_calls":2,"unwritten_words":0,"kv_bytes_staged":950272,"kv_bytes_naive":1605632,"kv_blocks_unique":29,"kv_blocks_logical":49,"mutants_run":2,"mutants_caught":2,"mutant_note":"KV_HEAD:CAUGHT(rc=OK,bad=8192) SLOT:CAUGHT(rc=OK,bad=8181)"}],"mutant_count":6,"mutants":{"NO_MAX":{"run":1,"caught":1},"KV_HEAD":{"run":11,"caught":11},"SLOT":{"run":9,"caught":9},"NO_RESCALE":{"run":4,"caught":4},"Q_ROW":{"run":6,"caught":6},"OUT_ROW":{"run":6,"caught":6}},"device_opens":1,"checks":122,"failed":0,"verdict":"OMEGA_GPU_ATTENTION_PASS"} diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/attention.log b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/attention.log new file mode 100644 index 000000000..d80fd7d88 --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/attention.log @@ -0,0 +1,49 @@ +PASS gqa_f32_tinyllama_32q4kv_ctx1 n=2048 rc=OK violations=0 worst_scaled_err=0 chip_ns=111425 call_ns=28621046 calls=1 unwritten=0 kv_staged=2048/2048 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx17 n=2048 rc=OK violations=0 worst_scaled_err=0.00189431 chip_ns=103537 call_ns=224306 calls=1 unwritten=0 kv_staged=34816/34816 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx256 n=2048 rc=OK violations=0 worst_scaled_err=0.00189409 chip_ns=268754 call_ns=423092 calls=1 unwritten=0 kv_staged=524288/524288 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx2048 n=2048 rc=OK violations=0 worst_scaled_err=0.0028582 chip_ns=1120154 call_ns=1742335 calls=1 unwritten=0 kv_staged=4194304/4194304 blocks=1/1 | mutants 4/4 SLOT:CAUGHT(rc=OK,bad=2041) NO_RESCALE:CAUGHT(rc=OK,bad=1959) Q_ROW:CAUGHT(rc=OK,bad=2048) OUT_ROW:CAUGHT(rc=OK,bad=2048) +PASS gqa_f32_tinyllama_32q4kv_ctx65 n=2048 rc=OK violations=0 worst_scaled_err=0.00174821 chip_ns=103057 call_ns=193570 calls=1 unwritten=0 kv_staged=133120/133120 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx128 n=2048 rc=OK violations=0 worst_scaled_err=0.00197914 chip_ns=102753 call_ns=107233 calls=1 unwritten=0 kv_staged=262144/262144 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx129 n=2048 rc=OK violations=0 worst_scaled_err=0.00177573 chip_ns=102865 call_ns=107329 calls=1 unwritten=0 kv_staged=264192/264192 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx300 n=2048 rc=OK violations=0 worst_scaled_err=0.00259576 chip_ns=205122 call_ns=214418 calls=1 unwritten=0 kv_staged=614400/614400 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx1000 n=2048 rc=OK violations=0 worst_scaled_err=0.00247545 chip_ns=590053 call_ns=631749 calls=1 unwritten=0 kv_staged=2048000/2048000 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx1 n=2048 rc=OK violations=0 worst_scaled_err=0 chip_ns=104017 call_ns=215458 calls=1 unwritten=0 kv_staged=4096/4096 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx17 n=2048 rc=OK violations=0 worst_scaled_err=0.00174276 chip_ns=102657 call_ns=105473 calls=1 unwritten=0 kv_staged=69632/69632 blocks=1/1 | mutants 1/1 KV_HEAD:CAUGHT(rc=OK,bad=2048) +PASS gqa_f32_llama32_1b_32q8kv_ctx256 n=2048 rc=OK violations=0 worst_scaled_err=0.00161378 chip_ns=208018 call_ns=309298 calls=1 unwritten=0 kv_staged=1048576/1048576 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx2048 n=2048 rc=OK violations=0 worst_scaled_err=0.0029454 chip_ns=1118074 call_ns=2342084 calls=1 unwritten=0 kv_staged=8388608/8388608 blocks=1/1 | mutants 4/4 SLOT:CAUGHT(rc=OK,bad=2045) NO_RESCALE:CAUGHT(rc=OK,bad=1904) Q_ROW:CAUGHT(rc=OK,bad=2042) OUT_ROW:CAUGHT(rc=OK,bad=2042) +PASS gqa_f32_llama32_1b_32q8kv_ctx65 n=2048 rc=OK violations=0 worst_scaled_err=0.0014332 chip_ns=105249 call_ns=193650 calls=1 unwritten=0 kv_staged=266240/266240 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx128 n=2048 rc=OK violations=0 worst_scaled_err=0.00186415 chip_ns=103249 call_ns=111201 calls=1 unwritten=0 kv_staged=524288/524288 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx129 n=2048 rc=OK violations=0 worst_scaled_err=0.00185519 chip_ns=103393 call_ns=111281 calls=1 unwritten=0 kv_staged=528384/528384 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx300 n=2048 rc=OK violations=0 worst_scaled_err=0.00162705 chip_ns=206242 call_ns=225298 calls=1 unwritten=0 kv_staged=1228800/1228800 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx1000 n=2048 rc=OK violations=0 worst_scaled_err=0.00330327 chip_ns=612149 call_ns=722534 calls=1 unwritten=0 kv_staged=4096000/4096000 blocks=1/1 | mutants 0/0 +PASS gqa_f32_large_scores_32q4kv_ctx256 n=2048 rc=OK violations=0 worst_scaled_err=0.105395 chip_ns=205138 call_ns=333523 calls=1 unwritten=0 kv_staged=524288/524288 blocks=1/1 | mutants 1/1 NO_MAX:CAUGHT(rc=OK,bad=2048) +PASS gqa_f32_mha_16q16kv_ctx300 n=1024 rc=OK violations=0 worst_scaled_err=0.00197746 chip_ns=207058 call_ns=358803 calls=1 unwritten=0 kv_staged=2457600/2457600 blocks=1/1 | mutants 3/3 Q_ROW:CAUGHT(rc=OK,bad=1022) OUT_ROW:CAUGHT(rc=OK,bad=1024) KV_HEAD:CAUGHT(rc=OK,bad=1024) +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx1 n=2048 rc=OK violations=0 worst_scaled_err=0 chip_ns=104353 call_ns=186690 calls=1 unwritten=0 kv_staged=16384/16384 blocks=1/1 | mutants 1/1 KV_HEAD:CAUGHT(rc=OK,bad=2048) +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx17 n=2048 rc=OK violations=0 worst_scaled_err=0.002096 chip_ns=103249 call_ns=179617 calls=1 unwritten=0 kv_staged=32768/32768 blocks=2/2 | mutants 2/2 KV_HEAD:CAUGHT(rc=OK,bad=2046) SLOT:CAUGHT(rc=OK,bad=2048) +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx256 n=2048 rc=OK violations=0 worst_scaled_err=0.00199949 chip_ns=205346 call_ns=286306 calls=1 unwritten=0 kv_staged=262144/262144 blocks=16/16 | mutants 0/0 +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx2048 n=2048 rc=OK violations=0 worst_scaled_err=0.00336531 chip_ns=1023833 call_ns=1081706 calls=1 unwritten=0 kv_staged=2097152/2097152 blocks=128/128 | mutants 5/5 KV_HEAD:CAUGHT(rc=OK,bad=2045) SLOT:CAUGHT(rc=OK,bad=2042) NO_RESCALE:CAUGHT(rc=OK,bad=2023) Q_ROW:CAUGHT(rc=OK,bad=2042) OUT_ROW:CAUGHT(rc=OK,bad=2042) +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx65 n=2048 rc=OK violations=0 worst_scaled_err=0.00182741 chip_ns=103489 call_ns=187489 calls=1 unwritten=0 kv_staged=81920/81920 blocks=5/5 | mutants 0/0 +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx128 n=2048 rc=OK violations=0 worst_scaled_err=0.00191997 chip_ns=173761 call_ns=177361 calls=1 unwritten=0 kv_staged=131072/131072 blocks=8/8 | mutants 0/0 +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx129 n=2048 rc=OK violations=0 worst_scaled_err=0.00192425 chip_ns=102945 call_ns=106625 calls=1 unwritten=0 kv_staged=147456/147456 blocks=9/9 | mutants 0/0 +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx300 n=2048 rc=OK violations=0 worst_scaled_err=0.00243899 chip_ns=204289 call_ns=210097 calls=1 unwritten=0 kv_staged=311296/311296 blocks=19/19 | mutants 0/0 +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx1000 n=2048 rc=OK violations=0 worst_scaled_err=0.00547531 chip_ns=204690 call_ns=208818 calls=1 unwritten=0 kv_staged=163840/1032192 blocks=10/63 | mutants 0/0 +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx1 n=2048 rc=OK violations=0 worst_scaled_err=0 chip_ns=102880 call_ns=187697 calls=1 unwritten=0 kv_staged=32768/32768 blocks=1/1 | mutants 1/1 KV_HEAD:CAUGHT(rc=OK,bad=2048) +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx17 n=2048 rc=OK violations=0 worst_scaled_err=0.00223287 chip_ns=103217 call_ns=180145 calls=1 unwritten=0 kv_staged=65536/65536 blocks=2/2 | mutants 2/2 KV_HEAD:CAUGHT(rc=OK,bad=2048) SLOT:CAUGHT(rc=OK,bad=2048) +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx256 n=2048 rc=OK violations=0 worst_scaled_err=0.00167964 chip_ns=205218 call_ns=292675 calls=1 unwritten=0 kv_staged=524288/524288 blocks=16/16 | mutants 0/0 +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx2048 n=2048 rc=OK violations=0 worst_scaled_err=0.00358689 chip_ns=1025577 call_ns=1147898 calls=1 unwritten=0 kv_staged=4194304/4194304 blocks=128/128 | mutants 5/5 KV_HEAD:CAUGHT(rc=OK,bad=2048) SLOT:CAUGHT(rc=OK,bad=2045) NO_RESCALE:CAUGHT(rc=OK,bad=1999) Q_ROW:CAUGHT(rc=OK,bad=2044) OUT_ROW:CAUGHT(rc=OK,bad=2044) +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx65 n=2048 rc=OK violations=0 worst_scaled_err=0.00228809 chip_ns=105216 call_ns=195297 calls=1 unwritten=0 kv_staged=163840/163840 blocks=5/5 | mutants 0/0 +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx128 n=2048 rc=OK violations=0 worst_scaled_err=0.00160376 chip_ns=103009 call_ns=107873 calls=1 unwritten=0 kv_staged=262144/262144 blocks=8/8 | mutants 0/0 +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx129 n=2048 rc=OK violations=0 worst_scaled_err=0.00173996 chip_ns=102961 call_ns=108753 calls=1 unwritten=0 kv_staged=294912/294912 blocks=9/9 | mutants 0/0 +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx300 n=2048 rc=OK violations=0 worst_scaled_err=0.00303696 chip_ns=206705 call_ns=217409 calls=1 unwritten=0 kv_staged=622592/622592 blocks=19/19 | mutants 0/0 +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx1000 n=2048 rc=OK violations=0 worst_scaled_err=0.00420311 chip_ns=206434 call_ns=213026 calls=1 unwritten=0 kv_staged=327680/2064384 blocks=10/63 | mutants 0/0 +PASS paged_bf16_batch_3seq_32q8kv_ctx40_0_100 n=6144 rc=OK violations=0 worst_scaled_err=0.00181424 chip_ns=207602 call_ns=460084 calls=2 unwritten=0 kv_staged=229376/229376 blocks=7/7 | mutants 4/4 KV_HEAD:CAUGHT(rc=OK,bad=4095) SLOT:CAUGHT(rc=OK,bad=4090) Q_ROW:CAUGHT(rc=OK,bad=4096) OUT_ROW:CAUGHT(rc=OK,bad=4096) +PASS paged_bf16_shared_prefix_4seq_8q2kv_10pre n=2048 rc=OK violations=0 worst_scaled_err=0.00188498 chip_ns=205218 call_ns=287395 calls=1 unwritten=0 kv_staged=155648/401408 blocks=19/49 | mutants 2/2 KV_HEAD:CAUGHT(rc=OK,bad=2048) SLOT:CAUGHT(rc=OK,bad=2046) +PASS paged_bf16_shared_prefix_4seq_32q8kv_10pre n=8192 rc=OK violations=0 worst_scaled_err=0.0024233 chip_ns=412676 call_ns=519172 calls=2 unwritten=0 kv_staged=950272/1605632 blocks=29/49 | mutants 2/2 KV_HEAD:CAUGHT(rc=OK,bad=8192) SLOT:CAUGHT(rc=OK,bad=8181) +mutant NO_MAX: caught 1 of 1 +mutant KV_HEAD: caught 11 of 11 +mutant SLOT: caught 9 of 9 +mutant NO_RESCALE: caught 4 of 4 +mutant Q_ROW: caught 6 of 6 +mutant OUT_ROW: caught 6 of 6 +device_opens=1 +PASS: 122 checks, 0 failed diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/attention_sim.log b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/attention_sim.log new file mode 100644 index 000000000..20e36bacb --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/attention_sim.log @@ -0,0 +1,49 @@ +PASS gqa_f32_tinyllama_32q4kv_ctx1 n=2048 rc=OK violations=0 worst_scaled_err=0 chip_ns=0 call_ns=7163086 calls=1 unwritten=0 kv_staged=2048/2048 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx17 n=2048 rc=OK violations=0 worst_scaled_err=0.00189313 chip_ns=0 call_ns=9563715 calls=1 unwritten=0 kv_staged=34816/34816 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx256 n=2048 rc=OK violations=0 worst_scaled_err=0.00183169 chip_ns=0 call_ns=35185617 calls=1 unwritten=0 kv_staged=524288/524288 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx2048 n=2048 rc=OK violations=0 worst_scaled_err=0.00274869 chip_ns=0 call_ns=243163400 calls=1 unwritten=0 kv_staged=4194304/4194304 blocks=1/1 | mutants 4/4 SLOT:CAUGHT(rc=OK,bad=2041) NO_RESCALE:CAUGHT(rc=OK,bad=1959) Q_ROW:CAUGHT(rc=OK,bad=2048) OUT_ROW:CAUGHT(rc=OK,bad=2048) +PASS gqa_f32_tinyllama_32q4kv_ctx65 n=2048 rc=OK violations=0 worst_scaled_err=0.00145684 chip_ns=0 call_ns=10842670 calls=1 unwritten=0 kv_staged=133120/133120 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx128 n=2048 rc=OK violations=0 worst_scaled_err=0.00206007 chip_ns=0 call_ns=15382597 calls=1 unwritten=0 kv_staged=262144/262144 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx129 n=2048 rc=OK violations=0 worst_scaled_err=0.00165735 chip_ns=0 call_ns=18337007 calls=1 unwritten=0 kv_staged=264192/264192 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx300 n=2048 rc=OK violations=0 worst_scaled_err=0.00201893 chip_ns=0 call_ns=36586220 calls=1 unwritten=0 kv_staged=614400/614400 blocks=1/1 | mutants 0/0 +PASS gqa_f32_tinyllama_32q4kv_ctx1000 n=2048 rc=OK violations=0 worst_scaled_err=0.00297055 chip_ns=0 call_ns=119301559 calls=1 unwritten=0 kv_staged=2048000/2048000 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx1 n=2048 rc=OK violations=0 worst_scaled_err=0 chip_ns=0 call_ns=3296445 calls=1 unwritten=0 kv_staged=4096/4096 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx17 n=2048 rc=OK violations=0 worst_scaled_err=0.00187227 chip_ns=0 call_ns=4440039 calls=1 unwritten=0 kv_staged=69632/69632 blocks=1/1 | mutants 1/1 KV_HEAD:CAUGHT(rc=OK,bad=2048) +PASS gqa_f32_llama32_1b_32q8kv_ctx256 n=2048 rc=OK violations=0 worst_scaled_err=0.00201723 chip_ns=0 call_ns=30516088 calls=1 unwritten=0 kv_staged=1048576/1048576 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx2048 n=2048 rc=OK violations=0 worst_scaled_err=0.0029454 chip_ns=0 call_ns=244637460 calls=1 unwritten=0 kv_staged=8388608/8388608 blocks=1/1 | mutants 4/4 SLOT:CAUGHT(rc=OK,bad=2045) NO_RESCALE:CAUGHT(rc=OK,bad=1904) Q_ROW:CAUGHT(rc=OK,bad=2042) OUT_ROW:CAUGHT(rc=OK,bad=2042) +PASS gqa_f32_llama32_1b_32q8kv_ctx65 n=2048 rc=OK violations=0 worst_scaled_err=0.00195964 chip_ns=0 call_ns=10914334 calls=1 unwritten=0 kv_staged=266240/266240 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx128 n=2048 rc=OK violations=0 worst_scaled_err=0.00170583 chip_ns=0 call_ns=15496630 calls=1 unwritten=0 kv_staged=524288/524288 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx129 n=2048 rc=OK violations=0 worst_scaled_err=0.0017034 chip_ns=0 call_ns=18426463 calls=1 unwritten=0 kv_staged=528384/528384 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx300 n=2048 rc=OK violations=0 worst_scaled_err=0.00187268 chip_ns=0 call_ns=36872686 calls=1 unwritten=0 kv_staged=1228800/1228800 blocks=1/1 | mutants 0/0 +PASS gqa_f32_llama32_1b_32q8kv_ctx1000 n=2048 rc=OK violations=0 worst_scaled_err=0.00303901 chip_ns=0 call_ns=120279887 calls=1 unwritten=0 kv_staged=4096000/4096000 blocks=1/1 | mutants 0/0 +PASS gqa_f32_large_scores_32q4kv_ctx256 n=2048 rc=OK violations=0 worst_scaled_err=0.105626 chip_ns=0 call_ns=30539495 calls=1 unwritten=0 kv_staged=524288/524288 blocks=1/1 | mutants 1/1 NO_MAX:CAUGHT(rc=OK,bad=2048) +PASS gqa_f32_mha_16q16kv_ctx300 n=1024 rc=OK violations=0 worst_scaled_err=0.00197746 chip_ns=0 call_ns=18392799 calls=1 unwritten=0 kv_staged=2457600/2457600 blocks=1/1 | mutants 3/3 Q_ROW:CAUGHT(rc=OK,bad=1022) OUT_ROW:CAUGHT(rc=OK,bad=1024) KV_HEAD:CAUGHT(rc=OK,bad=1024) +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx1 n=2048 rc=OK violations=0 worst_scaled_err=0 chip_ns=0 call_ns=3201659 calls=1 unwritten=0 kv_staged=16384/16384 blocks=1/1 | mutants 1/1 KV_HEAD:CAUGHT(rc=OK,bad=2048) +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx17 n=2048 rc=OK violations=0 worst_scaled_err=0.0020981 chip_ns=0 call_ns=4452359 calls=1 unwritten=0 kv_staged=32768/32768 blocks=2/2 | mutants 2/2 KV_HEAD:CAUGHT(rc=OK,bad=2046) SLOT:CAUGHT(rc=OK,bad=2048) +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx256 n=2048 rc=OK violations=0 worst_scaled_err=0.00182155 chip_ns=0 call_ns=31331182 calls=1 unwritten=0 kv_staged=262144/262144 blocks=16/16 | mutants 0/0 +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx2048 n=2048 rc=OK violations=0 worst_scaled_err=0.00365795 chip_ns=0 call_ns=248209312 calls=1 unwritten=0 kv_staged=2097152/2097152 blocks=128/128 | mutants 5/5 KV_HEAD:CAUGHT(rc=OK,bad=2045) SLOT:CAUGHT(rc=OK,bad=2042) NO_RESCALE:CAUGHT(rc=OK,bad=2023) Q_ROW:CAUGHT(rc=OK,bad=2042) OUT_ROW:CAUGHT(rc=OK,bad=2042) +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx65 n=2048 rc=OK violations=0 worst_scaled_err=0.00173051 chip_ns=0 call_ns=10939870 calls=1 unwritten=0 kv_staged=81920/81920 blocks=5/5 | mutants 0/0 +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx128 n=2048 rc=OK violations=0 worst_scaled_err=0.00189837 chip_ns=0 call_ns=15811400 calls=1 unwritten=0 kv_staged=131072/131072 blocks=8/8 | mutants 0/0 +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx129 n=2048 rc=OK violations=0 worst_scaled_err=0.00172729 chip_ns=0 call_ns=18633313 calls=1 unwritten=0 kv_staged=147456/147456 blocks=9/9 | mutants 0/0 +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx300 n=2048 rc=OK violations=0 worst_scaled_err=0.00210268 chip_ns=0 call_ns=37453155 calls=1 unwritten=0 kv_staged=311296/311296 blocks=19/19 | mutants 0/0 +PASS paged_bf16_tinyllama_32q4kv_bs16_ctx1000 n=2048 rc=OK violations=0 worst_scaled_err=0.00547531 chip_ns=0 call_ns=122241727 calls=1 unwritten=0 kv_staged=163840/1032192 blocks=10/63 | mutants 0/0 +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx1 n=2048 rc=OK violations=0 worst_scaled_err=0 chip_ns=0 call_ns=3205563 calls=1 unwritten=0 kv_staged=32768/32768 blocks=1/1 | mutants 1/1 KV_HEAD:CAUGHT(rc=OK,bad=2048) +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx17 n=2048 rc=OK violations=0 worst_scaled_err=0.00215313 chip_ns=0 call_ns=4452214 calls=1 unwritten=0 kv_staged=65536/65536 blocks=2/2 | mutants 2/2 KV_HEAD:CAUGHT(rc=OK,bad=2048) SLOT:CAUGHT(rc=OK,bad=2048) +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx256 n=2048 rc=OK violations=0 worst_scaled_err=0.0021938 chip_ns=0 call_ns=31377647 calls=1 unwritten=0 kv_staged=524288/524288 blocks=16/16 | mutants 0/0 +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx2048 n=2048 rc=OK violations=0 worst_scaled_err=0.00358689 chip_ns=0 call_ns=248633249 calls=1 unwritten=0 kv_staged=4194304/4194304 blocks=128/128 | mutants 5/5 KV_HEAD:CAUGHT(rc=OK,bad=2048) SLOT:CAUGHT(rc=OK,bad=2045) NO_RESCALE:CAUGHT(rc=OK,bad=1999) Q_ROW:CAUGHT(rc=OK,bad=2044) OUT_ROW:CAUGHT(rc=OK,bad=2044) +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx65 n=2048 rc=OK violations=0 worst_scaled_err=0.00163443 chip_ns=0 call_ns=10943630 calls=1 unwritten=0 kv_staged=163840/163840 blocks=5/5 | mutants 0/0 +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx128 n=2048 rc=OK violations=0 worst_scaled_err=0.00186221 chip_ns=0 call_ns=15813688 calls=1 unwritten=0 kv_staged=262144/262144 blocks=8/8 | mutants 0/0 +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx129 n=2048 rc=OK violations=0 worst_scaled_err=0.00163299 chip_ns=0 call_ns=18681233 calls=1 unwritten=0 kv_staged=294912/294912 blocks=9/9 | mutants 0/0 +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx300 n=2048 rc=OK violations=0 worst_scaled_err=0.00264235 chip_ns=0 call_ns=37511059 calls=1 unwritten=0 kv_staged=622592/622592 blocks=19/19 | mutants 0/0 +PASS paged_bf16_llama32_1b_32q8kv_bs16_ctx1000 n=2048 rc=OK violations=0 worst_scaled_err=0.00399855 chip_ns=0 call_ns=122440415 calls=1 unwritten=0 kv_staged=327680/2064384 blocks=10/63 | mutants 0/0 +PASS paged_bf16_batch_3seq_32q8kv_ctx40_0_100 n=6144 rc=OK violations=0 worst_scaled_err=0.00179162 chip_ns=0 call_ns=14575086 calls=2 unwritten=0 kv_staged=229376/229376 blocks=7/7 | mutants 4/4 KV_HEAD:CAUGHT(rc=OK,bad=4095) SLOT:CAUGHT(rc=OK,bad=4090) Q_ROW:CAUGHT(rc=OK,bad=4096) OUT_ROW:CAUGHT(rc=OK,bad=4096) +PASS paged_bf16_shared_prefix_4seq_8q2kv_10pre n=2048 rc=OK violations=0 worst_scaled_err=0.00208743 chip_ns=0 call_ns=24454562 calls=1 unwritten=0 kv_staged=155648/401408 blocks=19/49 | mutants 2/2 KV_HEAD:CAUGHT(rc=OK,bad=2048) SLOT:CAUGHT(rc=OK,bad=2046) +PASS paged_bf16_shared_prefix_4seq_32q8kv_10pre n=8192 rc=OK violations=0 worst_scaled_err=0.0024233 chip_ns=0 call_ns=98042973 calls=2 unwritten=0 kv_staged=950272/1605632 blocks=29/49 | mutants 2/2 KV_HEAD:CAUGHT(rc=OK,bad=8192) SLOT:CAUGHT(rc=OK,bad=8181) +mutant NO_MAX: caught 1 of 1 +mutant KV_HEAD: caught 11 of 11 +mutant SLOT: caught 9 of 9 +mutant NO_RESCALE: caught 4 of 4 +mutant Q_ROW: caught 6 of 6 +mutant OUT_ROW: caught 6 of 6 +device_opens=0 +PASS: 122 checks, 0 failed diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/attention_timing.json b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/attention_timing.json new file mode 100644 index 000000000..312e4e302 --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/attention_timing.json @@ -0,0 +1 @@ +{"schema":"OMEGA_GPU_ATTENTION_TIMING_V1","gate_median_ms":3.0,"calls":100,"cases":[{"case":"gqa_f32_tinyllama_32q4kv_ctx2048","first_call_ms":28.933,"ok_calls":100,"violations":0,"worst_scaled_err":0.00323017,"median_ms":1.1884,"p90_ms":1.1951,"min_ms":1.0922,"max_ms":1.6011,"chip_us_mean":1129.3,"device_opens":1,"pass":true}],"checks":2,"failed":0,"verdict":"OMEGA_GPU_ATTENTION_TIMING_PASS"} diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/attention_timing.log b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/attention_timing.log new file mode 100644 index 000000000..27b8460f6 --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/attention_timing.log @@ -0,0 +1,2 @@ +PASS timing gqa_f32_tinyllama_32q4kv_ctx2048 first_call_ms=28.933 ok_calls=100/100 violations=0 worst_scaled_err=0.00323017 median_ms=1.188 p90_ms=1.195 min_ms=1.092 max_ms=1.601 chip_us_mean=1129.3 device_opens=1 +PASS: 2 checks, 0 failed diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/build.log b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/build.log new file mode 100644 index 000000000..c662b1bea --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/build.log @@ -0,0 +1,5 @@ +ar rcs build/libomega_gpu.a build/sha256.o build/omega_canonical.o build/omega_validate.o build/omega_core.o build/omega_codec.o build/aarch64_encoder.o build/aarch64_decoder.o build/omega_realize.o build/omega_exec.o build/omega_self_host.o build/omega_verify.o build/omega_program.o build/omega_synthesis.o build/omega_library.o build/omega_discovery.o build/omega_machine.o build/omega_realize_synth.o build/omega_matvec.o build/omega_accelerator.o build/omega_accelerator_world.o build/omega_vector.o build/omega_blackwell_encoder.o build/omega_blackwell_qmd.o build/omega_blackwell_realize.o build/omega_blackwell_submit.o build/omega_blackwell_engine.o build/omega_gpu_engine.o build/omega_blackwell_matmul.o build/omega_blackwell_codegen.o build/omega_gpu_session.o build/omega_gpu_matmul_api.o build/omega_gpu_elementwise_api.o build/omega_gpu_attention_api.o build/omega_gpu_wait.o build/omega_evidence.o build/m16_native.o build/nvrm.o build/omega_program_ir.o build/omega_vc_bridge.o build/omega_resolve.o build/omega_receipt.o build/omega_blake3.o build/omega_vcstore.o +cc -std=gnu11 -Wall -Wextra -Werror -MMD -MP -D_GNU_SOURCE -O2 -Isrc -I../physics-reconv-lock/m16 -I../physics-reconv-lock/nvrm -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/src/common/sdk/nvidia/inc -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/kernel-open/common/inc -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/kernel-open/nvidia-uvm -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/src/nvidia/arch/nvalloc/unix/include -ffp-contract=off -o build/gpu_elementwise_test tests/gpu_elementwise_test.c build/libomega_gpu.a -lpthread -lm +cc -std=gnu11 -Wall -Wextra -Werror -MMD -MP -D_GNU_SOURCE -O2 -Isrc -I../physics-reconv-lock/m16 -I../physics-reconv-lock/nvrm -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/src/common/sdk/nvidia/inc -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/kernel-open/common/inc -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/kernel-open/nvidia-uvm -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/src/nvidia/arch/nvalloc/unix/include -ffp-contract=off -Itests -o build/gpu_attention_test tests/gpu_attention_test.c build/libomega_gpu.a -lpthread -lm +cc -std=gnu11 -Wall -Wextra -Werror -MMD -MP -D_GNU_SOURCE -O2 -Isrc -I../physics-reconv-lock/m16 -I../physics-reconv-lock/nvrm -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/src/common/sdk/nvidia/inc -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/kernel-open/common/inc -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/kernel-open/nvidia-uvm -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/src/nvidia/arch/nvalloc/unix/include -o build/gpu_matmul_api_test tests/gpu_matmul_api_test.c build/libomega_gpu.a -lpthread -lm +cc -std=gnu11 -Wall -Wextra -Werror -MMD -MP -D_GNU_SOURCE -O2 -Isrc -I../physics-reconv-lock/m16 -I../physics-reconv-lock/nvrm -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/src/common/sdk/nvidia/inc -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/kernel-open/common/inc -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/kernel-open/nvidia-uvm -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/src/nvidia/arch/nvalloc/unix/include -o build/gpu_session_probe tests/gpu_session_probe.c build/libomega_gpu.a -lpthread -lm diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/device.txt b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/device.txt new file mode 100644 index 000000000..ee5714236 --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/device.txt @@ -0,0 +1,6 @@ +Linux spark-b87b 7.0.0-1019-nvidia #19~24.04.2-Ubuntu SMP PREEMPT_DYNAMIC Sun Sep 6 18:35:11 UTC 2 aarch64 aarch64 aarch64 GNU/Linux +NVIDIA GB10, 580.173.02 +omega 0a1812f44eaa3dcf8fe5a914d1dd65cfaccbbcd7 (1 uncommitted paths) +physics 6d7cf0d4d8eb2cda7b512100ff6058e25dbb3ddf +2026-10-05T23:15:13Z +end 2026-10-05T23:15:40Z diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/elementwise.json b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/elementwise.json new file mode 100644 index 000000000..26db5249e --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/elementwise.json @@ -0,0 +1 @@ +{"schema":"OMEGA_GPU_ELEMENTWISE_V1","tolerances":{"ex2_rel":1e-6,"xchg":"exact","rmsnorm_rel":1e-5,"rmsnorm_abs":1e-6,"rope":"exact","swiglu_rel":1e-5,"swiglu_abs":1e-6},"cases":[{"case":"ex2_probe","n":4096,"rc":"OK","violations":0,"worst_scaled_err":0.152538,"pass":true,"chip_elapsed_ns":113889,"chip_calls":1,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":4096,"mutant_caught":true},{"case":"shared_xchg_probe","n":4096,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":102961,"chip_calls":1,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":4096,"mutant_caught":true},{"case":"rmsnorm_4x2048","n":8192,"rc":"OK","violations":0,"worst_scaled_err":0.0244808,"pass":true,"chip_elapsed_ns":103585,"chip_calls":1,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":8192,"mutant_caught":true},{"case":"rmsnorm_1x256","n":256,"rc":"OK","violations":0,"worst_scaled_err":0.0206888,"pass":true,"chip_elapsed_ns":102753,"chip_calls":1,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":256,"mutant_caught":true},{"case":"rmsnorm_2x8192","n":16384,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":104097,"chip_calls":1,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":16384,"mutant_caught":true},{"case":"rope_32h_64d_pos17_theta10000","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":103393,"chip_calls":1,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":1024,"mutant_caught":true},{"case":"rope_4h_64d_pos17_theta10000","n":256,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":102561,"chip_calls":1,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":128,"mutant_caught":true},{"case":"rope_32h_64d_pos1023_theta500000","n":2048,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":102640,"chip_calls":1,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":1024,"mutant_caught":true},{"case":"rope_8h_64d_pos1023_theta500000","n":512,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":103729,"chip_calls":1,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":256,"mutant_caught":true},{"case":"swiglu_5632","n":5632,"rc":"OK","violations":0,"worst_scaled_err":0.0196297,"pass":true,"chip_elapsed_ns":102817,"chip_calls":1,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":5630,"mutant_caught":true},{"case":"swiglu_8192","n":8192,"rc":"OK","violations":0,"worst_scaled_err":0.0177689,"pass":true,"chip_elapsed_ns":102977,"chip_calls":1,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":8192,"mutant_caught":true},{"case":"swiglu_1000","n":1000,"rc":"OK","violations":0,"worst_scaled_err":0.017264,"pass":true,"chip_elapsed_ns":102929,"chip_calls":1,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":1000,"mutant_caught":true},{"case":"icache_reuse_rotation","n":20,"rc":"OK","violations":0,"worst_scaled_err":0,"pass":true,"chip_elapsed_ns":1995472,"chip_calls":20,"unwritten_words":0,"mutant_rc":"OK","mutant_violations":0,"mutant_caught":true}],"device_opens":1,"checks":31,"failed":0,"verdict":"OMEGA_GPU_ELEMENTWISE_PASS"} diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/elementwise.log b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/elementwise.log new file mode 100644 index 000000000..048d192d6 --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/elementwise.log @@ -0,0 +1,15 @@ +PASS ex2_probe n=4096 rc=OK violations=0 worst_scaled_err=0.152538 chip_ns=113889 calls=1 unwritten=0 | mutant rc=OK violations=4096 CAUGHT +PASS shared_xchg_probe n=4096 rc=OK violations=0 worst_scaled_err=0 chip_ns=102961 calls=1 unwritten=0 | mutant rc=OK violations=4096 CAUGHT +PASS rmsnorm_4x2048 n=8192 rc=OK violations=0 worst_scaled_err=0.0244808 chip_ns=103585 calls=1 unwritten=0 | mutant rc=OK violations=8192 CAUGHT +PASS rmsnorm_1x256 n=256 rc=OK violations=0 worst_scaled_err=0.0206888 chip_ns=102753 calls=1 unwritten=0 | mutant rc=OK violations=256 CAUGHT +PASS rmsnorm_2x8192 n=16384 rc=OK violations=0 worst_scaled_err=0 chip_ns=104097 calls=1 unwritten=0 | mutant rc=OK violations=16384 CAUGHT +PASS rope_32h_64d_pos17_theta10000 n=2048 rc=OK violations=0 worst_scaled_err=0 chip_ns=103393 calls=1 unwritten=0 | mutant rc=OK violations=1024 CAUGHT +PASS rope_4h_64d_pos17_theta10000 n=256 rc=OK violations=0 worst_scaled_err=0 chip_ns=102561 calls=1 unwritten=0 | mutant rc=OK violations=128 CAUGHT +PASS rope_32h_64d_pos1023_theta500000 n=2048 rc=OK violations=0 worst_scaled_err=0 chip_ns=102640 calls=1 unwritten=0 | mutant rc=OK violations=1024 CAUGHT +PASS rope_8h_64d_pos1023_theta500000 n=512 rc=OK violations=0 worst_scaled_err=0 chip_ns=103729 calls=1 unwritten=0 | mutant rc=OK violations=256 CAUGHT +PASS swiglu_5632 n=5632 rc=OK violations=0 worst_scaled_err=0.0196297 chip_ns=102817 calls=1 unwritten=0 | mutant rc=OK violations=5630 CAUGHT +PASS swiglu_8192 n=8192 rc=OK violations=0 worst_scaled_err=0.0177689 chip_ns=102977 calls=1 unwritten=0 | mutant rc=OK violations=8192 CAUGHT +PASS swiglu_1000 n=1000 rc=OK violations=0 worst_scaled_err=0.017264 chip_ns=102929 calls=1 unwritten=0 | mutant rc=OK violations=1000 CAUGHT +PASS icache_reuse_rotation n=20 rc=OK violations=0 worst_scaled_err=0 chip_ns=1995472 calls=20 unwritten=0 | mutant rc=OK violations=0 CAUGHT +device_opens=1 +PASS: 31 checks, 0 failed diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/elementwise_timing.json b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/elementwise_timing.json new file mode 100644 index 000000000..963eb49b0 --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/elementwise_timing.json @@ -0,0 +1 @@ +{"schema":"OMEGA_GPU_ELEMENTWISE_TIMING_V1","gate_median_ms":0.5,"calls":100,"cases":[{"case":"rmsnorm_1x2048","first_call_ms":28.008,"ok_calls":100,"violations":0,"worst_scaled_err":0,"median_ms":0.1053,"p90_ms":0.1057,"min_ms":0.1048,"max_ms":0.1072,"chip_us_mean":103.0,"device_opens":1,"pass":true},{"case":"swiglu_5632","first_call_ms":0.494,"ok_calls":100,"violations":0,"worst_scaled_err":0.0208791,"median_ms":0.1082,"p90_ms":0.1086,"min_ms":0.0975,"max_ms":0.1101,"chip_us_mean":103.1,"device_opens":1,"pass":true}],"checks":4,"failed":0,"verdict":"OMEGA_GPU_ELEMENTWISE_TIMING_PASS"} diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/elementwise_timing.log b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/elementwise_timing.log new file mode 100644 index 000000000..ed933bf1b --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/elementwise_timing.log @@ -0,0 +1,3 @@ +PASS timing rmsnorm_1x2048 first_call_ms=28.008 ok_calls=100/100 violations=0 worst_scaled_err=0 median_ms=0.105 p90_ms=0.106 min_ms=0.105 max_ms=0.107 chip_us_mean=103.0 device_opens=1 +PASS timing swiglu_5632 first_call_ms=0.494 ok_calls=100/100 violations=0 worst_scaled_err=0.0208791 median_ms=0.108 p90_ms=0.109 min_ms=0.098 max_ms=0.110 chip_us_mean=103.1 device_opens=1 +PASS: 4 checks, 0 failed diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/gpu_attention_test.c b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/gpu_attention_test.c new file mode 100644 index 000000000..1f0019a50 --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/gpu_attention_test.c @@ -0,0 +1,849 @@ +/* gpu_attention_test: FB-1 cut 5 gate for omega_gpu_attention_api. + * + * ./gpu_attention_test --host-only refusals, codegen (both kernel families and every + * 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 + * check must catch, a negative control, timings + * + * Oracles re-state aien-sovereign-core crates/aien-inference-abi/src/backend.rs (main 8b5caed) in C: + * ReferenceCpuBackend::gqa_attention (f64 scores, max, exp, sum, 1/max(sum,1e-12), f64 weighted sum), + * TensorBackend::paged_attention (f64 online softmax token by token over the paged bf16 pool, tokens + * past the block table dropped, zeros for empty context) and paged_attention_batch (ctx <= 0 -> 0, + * table rows past the array -> empty, negative entries removed). Compiled with -ffp-contract=off. + * + * Tolerance (why): the chip accumulates the 64-term dot products, the per-chunk sums and the + * rescaled running sums in f32 (worst case about n * 2^-24 relative over n = 2048 terms, 1.2e-4), + * uses MUFU.EX2 (PTX ex2.approx: 2^-22.5 relative) for every weight and for the 32 chunk rescale + * factors, and MUFU.RCP for the final normalisation. Every output is a convex combination of V + * values of unit scale, so |error| <= 2e-4 * |want| + 2e-5 covers these with margin; the mutants + * move results by 1e-2 or more (wrong head, wrong token) or produce NaN (overflow without the max). + * The large-score case (scores up to ~300) carries the f32 rounding of the score itself into the + * exponent (3e-5 absolute per score), so it uses 1e-3 relative + 1e-4 absolute. + */ +#include "omega_gpu_attention_api.h" +#include "omega_gpu_session.h" +#include "omega_blackwell_codegen.h" +#include "bw_warp_sim.h" /* omega #308: SIMT warp simulator for --divergent */ +#include +#include +#include +#include +#include +#include +#include + +static int g_checks, g_failed; +static int g_sim_mode; /* --sim: the host simulator never opens the device */ +static int g_divergent; /* --divergent (omega #308 R2): the j >= ctx branch inside a BSSY/BSYNC region */ +#define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) + +static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } +static float frand(uint32_t *s, float lo, float hi) { return lo + (hi - lo) * ((float)(lcg(s) >> 8) / 16777216.0f); } +static uint16_t f2bf(float f) { uint32_t u; memcpy(&u, &f, 4); uint32_t r = ((u >> 16) & 1u) + 0x7fffu; return (uint16_t)((u + r) >> 16); } +static float bf2f(uint16_t b) { uint32_t u = (uint32_t)b << 16; float f; memcpy(&f, &u, 4); return f; } + +/* ----------------------------------------------------------------- oracles */ +static void oracle_gqa(const float *q, const float *kc, const float *vc, size_t seq_len, size_t nqh, size_t nkv, size_t hd, float *out) { + if (seq_len == 0) { memset(out, 0, nqh * hd * 4); return; } + size_t ratio = nqh / nkv; double inv_sqrt_d = 1.0 / sqrt((double)hd); + double *scores = malloc(seq_len * sizeof *scores); + for (size_t h = 0; h < nqh; h++) { + size_t kvh = h / ratio; const float *qh = q + h * hd; + for (size_t t = 0; t < seq_len; t++) { + const float *kt = kc + (t * nkv + kvh) * hd; double dot = 0.0; + for (size_t d = 0; d < hd; d++) dot += (double)qh[d] * (double)kt[d]; + scores[t] = dot * inv_sqrt_d; + } + double mx = -INFINITY; for (size_t t = 0; t < seq_len; t++) if (scores[t] > mx) mx = scores[t]; + double sum = 0.0; for (size_t t = 0; t < seq_len; t++) { scores[t] = exp(scores[t] - mx); sum += scores[t]; } + double inv = 1.0 / (sum > 1e-12 ? sum : 1e-12); + for (size_t t = 0; t < seq_len; t++) scores[t] *= inv; + for (size_t d = 0; d < hd; d++) { + double s = 0.0; + for (size_t t = 0; t < seq_len; t++) s += scores[t] * (double)vc[(t * nkv + kvh) * hd + d]; + out[h * hd + d] = (float)s; + } + } + free(scores); +} +/* bf16 pool in the canonical layout; the oracle's read_kv_head: element_offset(block, layer, is_value, token) + kv_head*head_dim*2 */ +static const uint16_t *kv_head(const uint8_t *pool, const OmegaGpuKvLayout *ly, size_t blk, size_t layer, int is_v, size_t tok, size_t kvh, size_t hd) { + size_t off = blk * ly->block_stride_bytes + layer * ly->layer_stride_bytes + (is_v ? ly->kv_plane_stride_bytes : 0) + tok * ly->token_stride_bytes + kvh * hd * 2; + return (const uint16_t *)(pool + off); +} +static void oracle_paged(const float *q, const uint8_t *pool, const OmegaGpuKvLayout *ly, const uint32_t *ids, size_t nids, size_t ctx, size_t layer, + size_t nqh, size_t nkv, size_t hd, float *out) { + if (ctx == 0 || nids == 0) { memset(out, 0, nqh * hd * 4); return; } + size_t ratio = nqh / nkv; double inv_sqrt_d = 1.0 / sqrt((double)hd); size_t bs = ly->block_size; + double *acc = malloc(hd * sizeof *acc); + for (size_t h = 0; h < nqh; h++) { + size_t kvh = h / ratio; const float *qh = q + h * hd; + double m = -INFINITY, l = 0.0; for (size_t d = 0; d < hd; d++) acc[d] = 0.0; + for (size_t t = 0; t < ctx; t++) { + size_t bi = t / bs; if (bi >= nids) break; + const uint16_t *kh = kv_head(pool, ly, ids[bi], layer, 0, t % bs, kvh, hd); + const uint16_t *vh = kv_head(pool, ly, ids[bi], layer, 1, t % bs, kvh, hd); + double dot = 0.0; for (size_t d = 0; d < hd; d++) dot += (double)qh[d] * (double)bf2f(kh[d]); + double score = dot * inv_sqrt_d, mn = m > score ? m : score, alpha = exp(m - mn), beta = exp(score - mn); + l = l * alpha + beta; + for (size_t d = 0; d < hd; d++) acc[d] = acc[d] * alpha + beta * (double)bf2f(vh[d]); + m = mn; + } + double inv = l > 0.0 ? 1.0 / l : 0.0; + for (size_t d = 0; d < hd; d++) out[h * hd + d] = (float)(acc[d] * inv); + } + free(acc); +} +static void oracle_batch(const float *q, const uint8_t *pool, const OmegaGpuKvLayout *ly, const int32_t *tables, const int32_t *ctxs, size_t maxb, size_t nseq, + size_t layer, size_t nqh, size_t nkv, size_t hd, float *out) { + size_t stride = nqh * hd; + uint32_t *ids = malloc((maxb ? maxb : 1) * sizeof *ids); + for (size_t s = 0; s < nseq; s++) { + size_t ctx = ctxs[s] > 0 ? (size_t)ctxs[s] : 0, n = 0; + size_t total = nseq * maxb; /* the table array length as the trait sees it */ + if ((s + 1) * maxb <= total) for (size_t b = 0; b < maxb; b++) if (tables[s * maxb + b] >= 0) ids[n++] = (uint32_t)tables[s * maxb + b]; + oracle_paged(q + s * stride, pool, ly, ids, n, ctx, layer, nqh, nkv, hd, out + s * stride); + } + free(ids); +} + +static size_t compare(const float *got, const float *want, size_t n, double rel, double abs_tol, double *worst) { + size_t bad = 0; *worst = 0.0; + for (size_t i = 0; i < n; i++) { + double d = fabs((double)got[i] - (double)want[i]); + double lim = rel * fabs((double)want[i]) + abs_tol; + double scaled = lim > 0 ? d / lim : (d > 0 ? INFINITY : 0.0); + if (scaled > *worst) *worst = scaled; + if (!(d <= lim)) bad++; /* NaN counts as bad */ + } + return bad; +} + +/* ------------------------------------------------------------- host only */ +static int nvdisasm_check(bool f32, const char *name, const char *const *must, int n_must) { + const char *nv = "/usr/local/cuda/bin/nvdisasm"; + if (access(nv, X_OK) != 0) { printf("nvdisasm: not present, listing check skipped for %s\n", name); return 0; } + OmegaBlackwellKernel k; memset(&k, 0, sizeof k); + if (omega_gpu_attention_codegen(f32, f32 ? 12 : 4, 3, &k) != OMEGA_GPU_ATTN_OK) return -1; + char bin[64], lst[64], cmd[256]; + snprintf(bin, sizeof bin, "/tmp/omega_attn_%s_%d.bin", name, (int)getpid()); + snprintf(lst, sizeof lst, "/tmp/omega_attn_%s_%d.sass", name, (int)getpid()); + FILE *f = fopen(bin, "wb"); if (!f) { free(k.code); return -1; } + fwrite(k.code, 1, k.code_size, f); fclose(f); + snprintf(cmd, sizeof cmd, "%s -b SM121 %s > %s 2>&1", nv, bin, lst); + int rc = system(cmd); + free(k.code); + FILE *l = fopen(lst, "r"); if (!l) return -1; + char line[512]; int seen[16] = {0}; int bad = 0, insns = 0; + while (fgets(line, sizeof line, l)) { + if (!strstr(line, "/*0")) continue; + insns++; + if (strstr(line, "?") || strstr(line, "error")) bad++; + for (int i = 0; i < n_must; i++) if (strstr(line, must[i])) seen[i] = 1; + } + fclose(l); + int miss = 0; + for (int i = 0; i < n_must; i++) if (!seen[i]) { printf(" %s: nvdisasm listing lacks %s\n", name, must[i]); miss++; } + printf("nvdisasm %s: rc=%d insns=%d undecodable=%d missing=%d (%s)\n", name, rc, insns, bad, miss, lst); + unlink(bin); + return (rc == 0 && bad == 0 && miss == 0) ? 0 : -1; +} + +static OmegaGpuKvLayout layout_for(uint32_t num_blocks, uint32_t bs, uint32_t layers, uint32_t nkv, uint32_t hd) { + OmegaGpuKvLayout ly; + ly.head_stride_bytes = (uint64_t)hd * 2; ly.token_stride_bytes = ly.head_stride_bytes * nkv; + ly.kv_plane_stride_bytes = ly.token_stride_bytes * bs; ly.layer_stride_bytes = 2 * ly.kv_plane_stride_bytes; + ly.block_stride_bytes = ly.layer_stride_bytes * layers; ly.pool_bytes = ly.block_stride_bytes * num_blocks; + ly.num_blocks = num_blocks; ly.num_layers = layers; ly.block_size = bs; + return ly; +} + +static void host_only(void) { + float q[64 * 64], kv[64 * 64], out[64 * 64]; memset(q, 0, sizeof q); memset(kv, 0, sizeof kv); + CHECK(omega_gpu_gqa_attention_f32(NULL, kv, kv, 1, 32, 4, 64, out, NULL) == OMEGA_GPU_ATTN_BAD_ARGS, "NULL q refused"); + CHECK(omega_gpu_gqa_attention_f32(q, kv, kv, 1, 32, 4, 128, out, NULL) == OMEGA_GPU_ATTN_TOO_LARGE, "head_dim 128 refused (TOO_LARGE)"); + CHECK(omega_gpu_gqa_attention_f32(q, kv, kv, 1, 6, 2, 64, out, NULL) == OMEGA_GPU_ATTN_BAD_ARGS, "gqa ratio 3 refused"); + CHECK(omega_gpu_gqa_attention_f32(q, kv, kv, 1, 32, 5, 64, out, NULL) == OMEGA_GPU_ATTN_BAD_ARGS, "32 q heads / 5 kv heads refused"); + CHECK(omega_gpu_gqa_attention_f32(q, kv, kv, OMEGA_GPU_ATTN_MAX_GQA_CTX + 1, 32, 4, 64, out, NULL) == OMEGA_GPU_ATTN_TOO_LARGE, "context past the envelope refused"); + for (int i = 0; i < 64; i++) out[i] = 1.0f; + OmegaGpuAttnInfo info; + CHECK(omega_gpu_gqa_attention_f32(q, kv, kv, 0, 1, 1, 64, out, &info) == OMEGA_GPU_ATTN_OK && out[0] == 0.0f && out[63] == 0.0f && info.chip_calls == 0, "seq_len 0 writes zeros without a launch"); + OmegaGpuKvLayout ly = layout_for(8, 16, 2, 4, 64); + uint8_t *pool = calloc(ly.pool_bytes, 1); + uint32_t ids[2] = { 1, 9 }; + CHECK(omega_gpu_paged_attention_bf16(q, pool, &ly, ids, 2, 20, 0, 32, 4, 64, out, NULL) == OMEGA_GPU_ATTN_BAD_ARGS, "block id past the pool refused"); + CHECK(omega_gpu_paged_attention_bf16(q, pool, &ly, ids, 1, 20, 2, 32, 4, 64, out, NULL) == OMEGA_GPU_ATTN_BAD_ARGS, "layer past the pool refused"); + OmegaGpuKvLayout bad = ly; bad.block_size = 12; + CHECK(omega_gpu_paged_attention_bf16(q, pool, &bad, ids, 1, 10, 0, 32, 4, 64, out, NULL) == OMEGA_GPU_ATTN_BAD_ARGS, "block size 12 (not a power of two) refused"); + bad = ly; bad.head_stride_bytes = 256; + CHECK(omega_gpu_paged_attention_bf16(q, pool, &bad, ids, 1, 10, 0, 32, 4, 64, out, NULL) == OMEGA_GPU_ATTN_BAD_ARGS, "layout whose head stride is not head_dim*2 refused"); + free(pool); + CHECK(strcmp(omega_gpu_attention_rc_name(OMEGA_GPU_ATTN_UNWRITTEN), "UNWRITTEN") == 0, "rc name"); + + /* with --divergent the listing must also show the region ops (last two entries) */ + static const char *const must_bf16[] = { "MUFU.EX2", "MUFU.RCP", "FMNMX", "BAR.SYNC", "SHFL.DOWN", "@P0 BRA", "@!P0 BRA", "LDG.E.U16", "LDG.E ", "LDS R", "STS [R", "STG.E", "LOP3.LUT", "SHF.R.U32.HI", "BSSY", "BSYNC" }; + static const char *const must_f32[] = { "MUFU.EX2", "MUFU.RCP", "FMNMX", "BAR.SYNC", "SHFL.DOWN", "@P0 BRA", "@!P0 BRA", "LDG.E ", "LDS R", "STS [R", "STG.E", "BSSY", "BSYNC" }; + for (int f32 = 0; f32 < 2; f32++) { + const char *name = f32 ? "gqa_f32" : "paged_bf16"; + OmegaBlackwellKernel k; memset(&k, 0, sizeof k); + 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); + /* 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 < OMEGA_GPU_ATTN_MUTANT_COUNT; m++) { + omega_gpu_attention_test_set_mutant((OmegaGpuAttnMutant)m); + memset(&k, 0, sizeof k); + 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(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) + (g_divergent ? 2 : 0)) == 0, "%s nvdisasm listing decodes and shows the expected instructions", name); + } +} + +/* ------------------------------------------------------------- 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]; + 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]; + +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 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.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.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); } + +/* 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 = 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]); + int rc = run(ctx, got, &info); + size_t bad = compare(got, want, n, rel, abs_tol, &w); + int caught = rc != OMEGA_GPU_ATTN_OK || bad > 0; + omega_gpu_attention_test_set_mutant(OMEGA_GPU_ATTN_MUTANT_NONE); + c->mutants_run++; c->mutants_caught += caught; g_mutant_run[ms[i]]++; g_mutant_caught[ms[i]] += caught; + pos += (size_t)snprintf(c->mutant_note + pos, sizeof c->mutant_note - pos, "%s%s:%s(rc=%s,bad=%zu)", pos ? " " : "", mutant_name(ms[i]), caught ? "CAUGHT" : "MISSED", omega_gpu_attention_rc_name(rc), bad); + if (pos >= sizeof c->mutant_note) break; + } +} + +typedef struct { const float *q, *kc, *vc; uint32_t seq, nqh, nkv; } GqaCtx; +static int run_gqa(void *p, float *got, OmegaGpuAttnInfo *info) { GqaCtx *c = p; return omega_gpu_gqa_attention_f32(c->q, c->kc, c->vc, c->seq, c->nqh, c->nkv, 64, got, info); } +typedef struct { const float *q; const uint8_t *pool; const OmegaGpuKvLayout *ly; const uint32_t *ids; uint32_t nids, ctx, layer, nqh, nkv; } PagedCtx; +static int run_paged(void *p, float *got, OmegaGpuAttnInfo *info) { PagedCtx *c = p; return omega_gpu_paged_attention_bf16(c->q, c->pool, c->ly, c->ids, c->nids, c->ctx, c->layer, c->nqh, c->nkv, 64, got, info); } +typedef struct { const float *q; const uint8_t *pool; const OmegaGpuKvLayout *ly; const int32_t *tables, *ctxs; uint32_t maxb, nseq, layer, nqh, nkv; } BatchCtx; +static int run_batch(void *p, float *got, OmegaGpuAttnInfo *info) { BatchCtx *c = p; return omega_gpu_paged_attention_batch_bf16(c->q, c->pool, c->ly, c->tables, c->ctxs, c->maxb, c->nseq, c->layer, c->nqh, c->nkv, 64, got, info); } +/* single vs batched parity and repeat (L6-KV, CAND-0 coverage): each sequence of a batch, run + * alone through the single-sequence entry point with the batch's own rules applied by hand + * (negative table entries removed, ctx <= 0 -> 0), must equal its row of the batched output bit + * for bit (same kernel, same per-CTA inputs; the batch only shares staging). Then the batch is + * called again with the same inputs: kernel cache hit, bit-identical output, identical staging + * counters. `got` must hold the batched output of `bc`. */ +static void batch_single_repeat_checks(const char *name, const BatchCtx *bc, const float *got, const Case *c) { + size_t row = (size_t)bc->nqh * 64, n = (size_t)bc->nseq * row; + float *one = malloc(row * 4), *again = malloc(n * 4); + uint32_t *ids = malloc((bc->maxb ? bc->maxb : 1) * 4); + for (uint32_t s = 0; s < bc->nseq; s++) { + uint32_t nb = 0; + for (uint32_t b = 0; b < bc->maxb; b++) { int32_t v = bc->tables[(size_t)s * bc->maxb + b]; if (v >= 0) ids[nb++] = (uint32_t)v; } + uint32_t ctx = bc->ctxs[s] > 0 ? (uint32_t)bc->ctxs[s] : 0; + for (size_t i = 0; i < row; i++) one[i] = -7.0f; /* poison: an unwritten row cannot match */ + OmegaGpuAttnInfo si; + int src = omega_gpu_paged_attention_bf16(bc->q + (size_t)s * row, bc->pool, bc->ly, ids, nb, ctx, bc->layer, bc->nqh, bc->nkv, 64, one, &si); + CHECK(src == OMEGA_GPU_ATTN_OK && memcmp(one, got + (size_t)s * row, row * 4) == 0, + "%s: sequence %u alone (single entry point, %u blocks, ctx %u) is bit-identical to its batched row (rc=%s)", name, s, nb, ctx, omega_gpu_attention_rc_name(src)); + } + OmegaGpuAttnInfo ri; + int rrc = omega_gpu_paged_attention_batch_bf16(bc->q, bc->pool, bc->ly, bc->tables, bc->ctxs, bc->maxb, bc->nseq, bc->layer, bc->nqh, bc->nkv, 64, again, &ri); + CHECK(rrc == OMEGA_GPU_ATTN_OK && ri.kernel_cache_hit && memcmp(again, got, n * 4) == 0 && ri.kv_bytes_staged == c->kv_staged && ri.kv_bytes_naive == c->kv_naive + && ri.kv_blocks_unique == c->blk_unique && ri.kv_blocks_logical == c->blk_logical, + "%s: repeated batch call is a cache hit, bit-identical, same staging counters (rc=%s hit=%d staged %" PRIu64 "/%" PRIu64 ")", + name, omega_gpu_attention_rc_name(rrc), (int)ri.kernel_cache_hit, ri.kv_bytes_staged, c->kv_staged); + free(one); free(again); free(ids); +} + + +static int chip(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; } + if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_ATTENTION_V1\",\"tolerances\":{\"rel\":2e-4,\"abs\":2e-5,\"large_scores_rel\":1e-3,\"large_scores_abs\":1e-4},\"cases\":["); + uint32_t seed = 0xfb1c0005u; + const uint32_t hd = 64; + const struct { uint32_t nqh, nkv; const char *model; } shapes[2] = { { 32, 4, "tinyllama" }, { 32, 8, "llama32_1b" } }; + /* 65, 129, 300 and 1000 end in a partial 64-token chunk after whole ones: the case that + * caught the diverged-warp shuffle on the chip (2026-10-04); 1, 17, 128, 256, 2048 did not. */ + const uint32_t ctxs[9] = { 1, 17, 256, 2048, 65, 128, 129, 300, 1000 }; + const int no_mutants[1] = { 0 }; + + /* 1. contiguous f32 KV (gqa_attention) */ + for (int sh = 0; sh < 2; sh++) for (int ci = 0; ci < 9; ci++) { + uint32_t nqh = shapes[sh].nqh, nkv = shapes[sh].nkv, seq = ctxs[ci]; + 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), *again = 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); + char name[96]; snprintf(name, sizeof name, "gqa_f32_%s_%uq%ukv_ctx%u", shapes[sh].model, nqh, nkv, seq); + Case c = { .name = strdup(name), .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; 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); + /* negative control: the comparison must reject a wrong expectation (kv heads swapped) */ + float *wrong = malloc(n * 4); double w2; + for (uint32_t h = 0; h < nqh; h++) memcpy(wrong + (size_t)h * hd, want + (size_t)((h + nqh / nkv) % nqh) * hd, hd * 4); + 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[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 }; + run_mutants(&c, ms, run_gqa, &gc, got, want, n, 2e-4, 2e-5); + } else run_mutants(&c, no_mutants, run_gqa, &gc, got, want, n, 2e-4, 2e-5); + record(c, out); free(q); free(kc); free(vc); free(got); free(want); free(again); + } + /* 2. large scores (q scaled by 200): the softmax without the running max overflows */ + { + uint32_t nqh = 32, nkv = 4, seq = 256; 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); + for (size_t i = 0; i < n; i++) q[i] = frand(&seed, -200.0f, 200.0f); /* scores ~ +-200 natural: EX2 without the max subtraction overflows f32 (2^128) */ + 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_large_scores_32q4kv_ctx256", .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, 1e-3, 1e-4, &c.worst); + 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); + } + /* 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; 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); + 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; + uint32_t nblk = (ctx + bs - 1) / bs, pool_blocks = nblk + 7; + 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); /* a scattered permutation of the pool blocks */ + 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); + oracle_paged(q, pool, &ly, ids, nblk, ctx, layer, nqh, nkv, hd, want); + char name[96]; snprintf(name, sizeof name, "paged_bf16_%s_%uq%ukv_bs%u_ctx%u", shapes[sh].model, nqh, nkv, bs, ctx); + Case c = { .name = strdup(name), .n = n }; + 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; 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 }; + 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 }; + run_mutants(&c, ms, run_paged, &pc, got, want, n, 2e-4, 2e-5); + } else if (ci == 0) { + /* one token: its softmax weight is 1 whatever K says, so a K-only slot shift is invisible; KV_HEAD still shows */ + static const int ms[2] = { OMEGA_GPU_ATTN_MUTANT_KV_HEAD, 0 }; + run_mutants(&c, ms, run_paged, &pc, got, want, n, 2e-4, 2e-5); + } else run_mutants(&c, no_mutants, run_paged, &pc, got, want, n, 2e-4, 2e-5); + record(c, out); free(pool); free(ids); free(q); free(got); free(want); + } + /* 4. batch: three sequences (short, empty, truncated by its table), a negative table entry, table rows of 16 */ + { + uint32_t nqh = 32, nkv = 8, bs = 16, layers = 3, layer = 2, maxb = 16, nseq = 3, pool_blocks = 40; + 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; + /* seq 0: ctx 40 over blocks 5, 17 (-1 in the middle closes up), 29 */ + tables[0] = 5; tables[1] = -1; tables[2] = 17; tables[3] = 29; + /* seq 1: ctx 0 with a table */ + tables[maxb + 0] = 1; + /* seq 2: ctx 100 but only 4 blocks (64 tokens used) */ + tables[2 * maxb + 0] = 30; tables[2 * maxb + 1] = 2; tables[2 * maxb + 2] = 33; tables[2 * maxb + 3] = 11; + int32_t ctxs2[3] = { 40, 0, 100 }; + size_t n = (size_t)nseq * 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); + oracle_batch(q, pool, &ly, tables, ctxs2, maxb, nseq, layer, nqh, nkv, hd, want); + Case c = { .name = "paged_bf16_batch_3seq_32q8kv_ctx40_0_100", .n = n }; + 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; 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"); + batch_single_repeat_checks(c.name, &bc, got, &c); + 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); + batch_single_repeat_checks(name, &bc, got, &c); + 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++) { + 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, "],\"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); + } + return 0; +} + +/* ------------------------------------------------------------ host simulator + * Executes the allocated IR program (physical registers, so the register allocator's + * loop handling is under test too) for every CTA of a launch: 64 threads, 1 KB shared, + * host pointers as addresses, SHFL and BAR as lockstep points (all threads of the CTA + * must sit at the same instruction). EX2 and RCP are exact here; the chip's are approximate. */ +/* path: hash of every branch decision so far. The IR has no reconvergence instruction + * (no BSSY/BSYNC/WARPSYNC), so once a warp splits it may stay split on the chip, and a + * SHFL reading an inactive lane is undefined (CUDA guide, warp shuffle functions). Every + * SHFL therefore requires one path across the warp, or the simulator stops with error 7. */ +typedef struct { uint32_t r[256]; int pc; int p0; int done; uint64_t path; } SimThr; +static uint32_t sim_cbank[256]; +static uint8_t sim_shared[1024]; +#define SIM_T 64 +static int g_sim_err; +static int g_trace, g_trace_launch; static uint32_t g_trace_cx; +static uint32_t rd(const SimThr *t, int v, const BlackwellIRProgram *p) { if (v < 0) return 0; int ph = p->regalloc.vreg_to_phys[v]; return ph < 0 ? 0 : t->r[ph & 0xff]; } +static uint64_t rd64(const SimThr *t, int v, const BlackwellIRProgram *p) { if (v < 0) return 0; int ph = p->regalloc.vreg_to_phys[v] & 0xff; return (uint64_t)t->r[ph] | ((uint64_t)t->r[ph + 1] << 32); } +static void wr(SimThr *t, int v, uint32_t x, const BlackwellIRProgram *p) { if (v < 0) return; int ph = p->regalloc.vreg_to_phys[v]; if (ph >= 0) t->r[ph & 0xff] = x; } +static void wr64(SimThr *t, int v, uint64_t x, const BlackwellIRProgram *p) { if (v < 0) return; int ph = p->regalloc.vreg_to_phys[v] & 0xff; t->r[ph] = (uint32_t)x; t->r[ph + 1] = (uint32_t)(x >> 32); } +static float u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } +static uint32_t f2u_(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } +/* run one thread until it reaches a SHFL or BAR (not executed) or EXIT; returns 1 if parked at a sync op */ +static int sim_step(SimThr *t, const BlackwellIRProgram *p, uint32_t tid, uint32_t cx, uint32_t cy) { + for (int guard = 0; guard < 50000000; guard++) { + if (t->done) return 0; + if (t->pc < 0 || (size_t)t->pc >= p->count) { g_sim_err = 1; t->done = 1; return 0; } + const BlackwellIRInsn *in = &p->insns[t->pc]; + uint32_t a = rd(t, in->src1_vreg, p), b = rd(t, in->src2_vreg, p), c = rd(t, in->src3_vreg, p); + switch (in->op) { + case BW_IR_SHFL_DOWN: case BW_IR_BAR_SYNC: return 1; + case BW_IR_EXIT: if (!in->predicate_p0 || t->p0) { t->done = 1; return 0; } break; + case BW_IR_BRA: { + int take = !in->predicate_p0 || (in->predicate_not ? !t->p0 : t->p0); + t->path = t->path * 1000003u + (((uint64_t)t->pc << 1) | (uint64_t)take); + if (take) { if ((int32_t)in->imm == 0) { g_sim_err = 2; t->done = 1; return 0; } t->pc += (int32_t)in->imm; continue; } + break; } + case BW_IR_MOV_IMM: wr(t, in->dst_vreg, in->imm, p); break; + case BW_IR_MOV_RZ: wr(t, in->dst_vreg, 0, p); break; + case BW_IR_S2R: wr(t, in->dst_vreg, in->imm == BW_SR_TID_X ? tid : in->imm == BW_SR_CTAID_X ? cx : in->imm == BW_SR_CTAID_Y ? cy : 0, p); break; + case BW_IR_LDC: wr(t, in->dst_vreg, sim_cbank[in->imm / 4], p); break; + case BW_IR_LDC64: wr64(t, in->dst_vreg, (uint64_t)sim_cbank[in->imm / 4] | ((uint64_t)sim_cbank[in->imm / 4 + 1] << 32), p); break; + case BW_IR_LDCU64: break; /* memory descriptor: not modelled */ + case BW_IR_IMAD: wr(t, in->dst_vreg, in->src2_vreg >= 0 ? a * b + c : a * in->imm + c, p); break; + case BW_IR_IMAD_WIDE: wr64(t, in->dst_vreg, (uint64_t)a * in->imm + rd64(t, in->src3_vreg, p), p); break; + case BW_IR_IADD3: wr(t, in->dst_vreg, a + b, p); break; + case BW_IR_SHF_R: wr(t, in->dst_vreg, a >> (in->imm & 31), p); break; + case BW_IR_LOP3_AND: wr(t, in->dst_vreg, a & in->imm, p); break; + case BW_IR_LOP3_XOR: wr(t, in->dst_vreg, a ^ b, p); break; + case BW_IR_ISETP_GE_U32: t->p0 = a >= b; break; + case BW_IR_LDG_E: { uint32_t x; memcpy(&x, (const void *)(uintptr_t)rd64(t, in->src1_vreg, p), 4); wr(t, in->dst_vreg, x, p); break; } + case BW_IR_LDG_E_U16: { uint16_t x; memcpy(&x, (const void *)(uintptr_t)rd64(t, in->src1_vreg, p), 2); wr(t, in->dst_vreg, x, p); break; } + case BW_IR_STG_E: memcpy((void *)(uintptr_t)rd64(t, in->src1_vreg, p), &b, 4); break; + case BW_IR_LDS32: { uint32_t x; if (a + 4 > sizeof sim_shared) { g_sim_err = 3; t->done = 1; return 0; } memcpy(&x, sim_shared + a, 4); wr(t, in->dst_vreg, x, p); break; } + case BW_IR_STS32: if (a + 4 > sizeof sim_shared) { g_sim_err = 3; t->done = 1; return 0; } memcpy(sim_shared + a, &b, 4); break; + case BW_IR_FADD: wr(t, in->dst_vreg, f2u_(u2f(a) + u2f(b)), p); break; + case BW_IR_FSUB: wr(t, in->dst_vreg, f2u_(u2f(a) - u2f(b)), p); break; + case BW_IR_FMUL: wr(t, in->dst_vreg, f2u_(u2f(a) * u2f(b)), p); break; + case BW_IR_FFMA: wr(t, in->dst_vreg, f2u_(fmaf(u2f(a), u2f(b), u2f(c))), p); break; + case BW_IR_FMNMX_MAX: wr(t, in->dst_vreg, f2u_(fmaxf(u2f(a), u2f(b))), p); break; + case BW_IR_MUFU_EX2: wr(t, in->dst_vreg, f2u_(exp2f(u2f(a))), p); break; + case BW_IR_MUFU_RCP: wr(t, in->dst_vreg, f2u_(1.0f / u2f(a)), p); break; + default: g_sim_err = 100 + (int)in->op; t->done = 1; return 0; + } + if (g_trace && tid == 0 && cx == g_trace_cx && cy == 0 && g_trace_launch == 1) { uint32_t dv = rd(t, in->dst_vreg, p); printf("T pc=%d op=%d dst=v%d(r%d)=%08x f=%g p0=%d a=%08x b=%08x c=%08x\n", t->pc, (int)in->op, in->dst_vreg, in->dst_vreg >= 0 ? p->regalloc.vreg_to_phys[in->dst_vreg] : -1, dv, u2f(dv), t->p0, a, b, c); } + t->pc++; + } + g_sim_err = 4; t->done = 1; return 0; +} +static int sim_launch(const void *progv, const OmegaGpuAttnLaunch *L, OmegaGpuAttnInfo *info) { + const BlackwellIRProgram *p = progv; + memset(sim_cbank, 0, sizeof sim_cbank); + uint64_t qa = (uintptr_t)L->q, pa = (uintptr_t)L->pool, oa = (uintptr_t)L->out, ta = (uintptr_t)L->tab; + sim_cbank[0x380 / 4] = (uint32_t)qa; sim_cbank[0x380 / 4 + 1] = (uint32_t)(qa >> 32); + sim_cbank[0x388 / 4] = (uint32_t)pa; sim_cbank[0x388 / 4 + 1] = (uint32_t)(pa >> 32); + sim_cbank[0x390 / 4] = (uint32_t)oa; sim_cbank[0x390 / 4 + 1] = (uint32_t)(oa >> 32); + sim_cbank[0x398 / 4] = (uint32_t)ta; sim_cbank[0x398 / 4 + 1] = (uint32_t)(ta >> 32); + memcpy(&sim_cbank[0x3a0 / 4], L->params, sizeof L->params); + uint32_t *outw = L->out; for (size_t i = 0; i < L->out_bytes / 4; i++) outw[i] = 0xffbadbadu; + g_sim_err = 0; + static SimThr th[SIM_T]; + g_trace_launch++; if (getenv("ATTN_SIM_TRACE")) { g_trace = 1; g_trace_cx = (uint32_t)atoi(getenv("ATTN_SIM_TRACE")); } + for (uint32_t cy = 0; cy < L->grid_y && !g_sim_err; cy++) for (uint32_t cx = 0; cx < L->grid_x && !g_sim_err; cx++) { + memset(th, 0, sizeof th); memset(sim_shared, 0, sizeof sim_shared); + for (;;) { + int parked = 0, alive = 0; + for (int i = 0; i < SIM_T; i++) { if (th[i].done) continue; alive++; parked += sim_step(&th[i], p, (uint32_t)i, cx, cy); } + if (g_sim_err) break; + if (alive == 0) break; + if (parked == 0) continue; /* every live thread exited this round */ + if (parked != alive) { g_sim_err = 5; break; } /* a thread exited while others wait at a barrier */ + int pc = -1; + for (int i = 0; i < SIM_T; i++) if (!th[i].done) { if (pc < 0) pc = th[i].pc; else if (th[i].pc != pc) g_sim_err = 6; } + if (g_sim_err) break; + const BlackwellIRInsn *in = &p->insns[pc]; + if (in->op == BW_IR_SHFL_DOWN) { + for (int i = 0; i < SIM_T; i++) if (!th[i].done && !th[i & ~31].done && th[i].path != th[i & ~31].path) g_sim_err = 7; + if (g_sim_err) break; + uint32_t src[SIM_T]; + for (int i = 0; i < SIM_T; i++) src[i] = rd(&th[i], in->src1_vreg, p); + for (int i = 0; i < SIM_T; i++) { int lane = i & 31, from = lane + (int)(in->imm & 31); wr(&th[i], in->dst_vreg, from < 32 ? src[(i & ~31) + from] : src[i], p); } + } + for (int i = 0; i < SIM_T; i++) th[i].pc++; + } + } + uint32_t unwritten = 0; for (size_t i = 0; i < L->out_bytes / 4; i++) if (outw[i] == 0xffbadbadu) unwritten++; + if (info) { info->chip_calls++; info->unwritten_words += unwritten; info->ctas_last_launch = L->grid_x * L->grid_y; info->threads_per_cta = SIM_T; } + if (g_sim_err) { printf("simulator error %d\n", g_sim_err); return OMEGA_GPU_ATTN_CHIP_FAIL; } + return unwritten ? OMEGA_GPU_ATTN_UNWRITTEN : OMEGA_GPU_ATTN_OK; +} + + +/* ---------------------------------------------------- timing (cut 4b) */ +/* One query step of TinyLlama attention at context 2048 (32 q heads, 4 kv heads, + * f32 KV) 100 times through the persistent session, every call checked against the + * oracle; gate: median wall time per call under 3 ms, one device open per process. */ +static double now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; } +static int cmp_d(const void *a, const void *b) { double x = *(const double *)a, y = *(const double *)b; return x < y ? -1 : x > y; } +static int timing(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; } + enum { N = 100 }; + const uint32_t hd = 64, nqh = 32, nkv = 4, seq = 2048; + const char *name = "gqa_f32_tinyllama_32q4kv_ctx2048"; + if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_ATTENTION_TIMING_V1\",\"gate_median_ms\":3.0,\"calls\":%d,\"cases\":[", N); + uint32_t seed = 0xfb1c004bu; + 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); + 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); + double samples[N]; int ok_calls = 0; size_t bad_total = 0; double worst = 0; uint64_t chip_sum = 0; int rc_last = 0; + OmegaGpuAttnInfo info; + /* warm-up: the first call opens the device and generates the kernel (not timed) */ + int rc0 = omega_gpu_gqa_attention_f32(q, kc, vc, seq, nqh, nkv, hd, got, &info); + double first_ms = info.call_ns / 1e6; + CHECK(rc0 == OMEGA_GPU_ATTN_OK, "%s first call rc=%s err=\"%s\"", name, omega_gpu_attention_rc_name(rc0), omega_gpu_attention_last_error()); + for (int i = 0; i < N; i++) { + double t0 = now_ms(); + int rc = omega_gpu_gqa_attention_f32(q, kc, vc, seq, nqh, nkv, hd, got, &info); + samples[i] = now_ms() - t0; + double w; size_t bad = compare(got, want, n, 2e-4, 2e-5, &w); + if (w > worst) worst = w; + bad_total += bad; chip_sum += info.elapsed_ns; rc_last = rc; + if (rc == OMEGA_GPU_ATTN_OK && bad == 0) ok_calls++; + } + qsort(samples, N, sizeof samples[0], cmp_d); + double median = samples[N / 2], p90 = samples[N * 9 / 10], mn = samples[0], mx = samples[N - 1]; + uint32_t opens = omega_gpu_session_open_count(); + int pass = ok_calls == N && bad_total == 0 && median < 3.0 && opens == 1; + CHECK(pass, "timing %s ok_calls=%d/%d violations=%zu median_ms=%.3f opens=%u last_rc=%s err=\"%s\"", name, ok_calls, N, bad_total, median, opens, omega_gpu_attention_rc_name(rc_last), omega_gpu_attention_last_error()); + printf("%s timing %s first_call_ms=%.3f ok_calls=%d/%d violations=%zu worst_scaled_err=%g median_ms=%.3f p90_ms=%.3f min_ms=%.3f max_ms=%.3f chip_us_mean=%.1f device_opens=%u\n", + pass ? "PASS" : "FAIL", name, first_ms, ok_calls, N, bad_total, worst, median, p90, mn, mx, chip_sum / 1e3 / N, opens); + if (out) fprintf(out, "{\"case\":\"%s\",\"first_call_ms\":%.3f,\"ok_calls\":%d,\"violations\":%zu,\"worst_scaled_err\":%g,\"median_ms\":%.4f,\"p90_ms\":%.4f,\"min_ms\":%.4f,\"max_ms\":%.4f,\"chip_us_mean\":%.1f,\"device_opens\":%u,\"pass\":%s}", + name, first_ms, ok_calls, bad_total, worst, median, p90, mn, mx, chip_sum / 1e3 / N, opens, pass ? "true" : "false"); + free(q); free(kc); free(vc); free(got); free(want); + if (out) { fprintf(out, "],\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", g_checks, g_failed, g_failed == 0 ? "OMEGA_GPU_ATTENTION_TIMING_PASS" : "OMEGA_GPU_ATTENTION_TIMING_FAIL"); fclose(out); } + 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; +} + +/* ------------------------------------------------------------ warp simulator (omega #308) + * The per-thread simulator above has no model of a warp (its SHFL rule is "one path across the + * warp or error 7"). With --divergent the kernel carries a real lane-dependent branch inside a + * BSSY/BSYNC region, and every launch runs through tests/bw_warp_sim.h instead: 32-lane + * fragments, barrier registers, SHFL/BAR participation. Each launch runs in both fragment + * orders and the two outputs must be bit-identical. */ +static uint64_t g_ws_issues, g_ws_launches; +static int ws_attn_launch(const void *progv, const OmegaGpuAttnLaunch *L, OmegaGpuAttnInfo *info) { + const BlackwellIRProgram *p = progv; + uint32_t *outw = L->out; size_t nw = L->out_bytes / 4; + uint32_t *copy = malloc(L->out_bytes); + int err = 0; char msg[240] = ""; + for (int order = 0; order < 2 && !err && copy; order++) { + WsSim s; memset(&s, 0, sizeof s); + s.p = p; s.threads = SIM_T; s.smem = sim_shared; s.smem_bytes = sizeof sim_shared; s.order = order; + uint64_t qa = (uintptr_t)L->q, pa = (uintptr_t)L->pool, oa = (uintptr_t)L->out, ta = (uintptr_t)L->tab; + s.cbank[0x380 / 4] = (uint32_t)qa; s.cbank[0x380 / 4 + 1] = (uint32_t)(qa >> 32); + s.cbank[0x388 / 4] = (uint32_t)pa; s.cbank[0x388 / 4 + 1] = (uint32_t)(pa >> 32); + s.cbank[0x390 / 4] = (uint32_t)oa; s.cbank[0x390 / 4 + 1] = (uint32_t)(oa >> 32); + s.cbank[0x398 / 4] = (uint32_t)ta; s.cbank[0x398 / 4 + 1] = (uint32_t)(ta >> 32); + memcpy(&s.cbank[0x3a0 / 4], L->params, sizeof L->params); + for (size_t i = 0; i < nw; i++) outw[i] = 0xffbadbadu; + err = ws_run(&s, L->grid_x, L->grid_y); + if (err) { snprintf(msg, sizeof msg, "order %d: %s", order, s.msg); break; } + g_ws_issues += s.warp_issues; + if (order == 0) memcpy(copy, outw, L->out_bytes); + else if (memcmp(copy, outw, L->out_bytes) != 0) { err = -1; snprintf(msg, sizeof msg, "the fragment order changed the result"); } + } + free(copy); + g_ws_launches++; + uint32_t unwritten = 0; for (size_t i = 0; i < nw; i++) if (outw[i] == 0xffbadbadu) unwritten++; + if (info) { info->chip_calls++; info->unwritten_words += unwritten; info->ctas_last_launch = L->grid_x * L->grid_y; info->threads_per_cta = SIM_T; } + if (err) { printf("warp simulator error: %s\n", msg); return OMEGA_GPU_ATTN_CHIP_FAIL; } + return unwritten ? OMEGA_GPU_ATTN_UNWRITTEN : OMEGA_GPU_ATTN_OK; +} +/* Negative control for --divergent --sim: mode 2 builds the same kernel with the BSYNC dropped; + * the warp simulator must reject it (the whole point of the simulator: the old one could not). */ +static void divergent_negative_control(void) { + enum { SEQ = 65, NQH = 8, NKV = 2, HD = 64 }; + float *q = malloc(NQH * HD * 4), *kv = malloc((size_t)SEQ * NKV * HD * 4), *out = malloc(NQH * HD * 4); + uint32_t seed = 0x308u; + for (int i = 0; i < NQH * HD; i++) q[i] = frand(&seed, -1.0f, 1.0f); + for (size_t i = 0; i < (size_t)SEQ * NKV * HD; i++) kv[i] = frand(&seed, -1.0f, 1.0f); + omega_gpu_attention_test_set_divergent(2); + int rc = omega_gpu_gqa_attention_f32(q, kv, kv, SEQ, NQH, NKV, HD, out, NULL); + omega_gpu_attention_test_set_divergent(1); + CHECK(rc == OMEGA_GPU_ATTN_CHIP_FAIL, "divergent mode 2 (BSYNC dropped) rejected by the warp simulator (rc %s)", omega_gpu_attention_rc_name(rc)); + int rc1 = omega_gpu_gqa_attention_f32(q, kv, kv, SEQ, NQH, NKV, HD, out, NULL); + CHECK(rc1 == OMEGA_GPU_ATTN_OK, "divergent mode 1 (regioned branch) runs clean on the same input (rc %s)", omega_gpu_attention_rc_name(rc1)); + free(q); free(kv); free(out); +} + +int main(int argc, char **argv) { + 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], "--divergent") == 0) g_divergent = 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 (g_divergent) omega_gpu_attention_test_set_divergent(1); + if (sim) { g_sim_mode = 1; omega_gpu_attention_test_set_simulator(g_divergent ? ws_attn_launch : sim_launch); } + if (sim && g_divergent && !host) divergent_negative_control(); + 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/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/gpu_elementwise_test.c b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/gpu_elementwise_test.c new file mode 100644 index 000000000..c274a063e --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/gpu_elementwise_test.c @@ -0,0 +1,386 @@ +/* gpu_elementwise_test: FB-1 cut 4 gate for omega_gpu_elementwise_api. + * + * ./gpu_elementwise_test --host-only refusals, codegen, word fixtures, register budget, + * nvdisasm listing check when /usr/local/cuda/bin/nvdisasm exists + * ./gpu_elementwise_test --out receipt.json chip gate: every op against the host oracle, then its mutant + * (a deliberately wrong kernel) which the same check must catch + * ./gpu_elementwise_test --timing --out t.json cut 4b gate: persistent device, 100 calls each of rmsnorm 1x2048 + * and swiglu 5632 under 0.5 ms median, one device open per process + * + * Oracles re-state aien-sovereign-core crates/aien-inference-abi/src/tensor.rs (main 5fdab70) in C: + * rmsnorm sums in f64, rope computes cos/sin in f64 and casts to f32 before f32 math, swiglu is f64. + * This file is compiled with -ffp-contract=off so the f32 oracle math has no hidden fused multiply-add. + * + * Tolerances (why): EX2 1e-6 rel (PTX ex2.approx.f32: 2^-22.5 rel, we allow 2x). XCHG exact. + * RMSNORM 1e-5 rel + 1e-6 abs (f32 sum of up to 16384 squares against an f64 sum, plus RSQ seed with one + * Newton step; the mutant moves the scale by about 40 percent). ROPE exact (same f32 ops, same order). + * SWIGLU 1e-5 rel + 1e-6 abs (EX2 and RCP approximations, each about 2^-22 rel; the mutant flips the sign + * of the exponent, which is wrong by a factor of e^(2g)). + */ +#include "omega_gpu_elementwise_api.h" +#include "omega_blackwell_codegen.h" +#include "omega_gpu_session.h" +#include +#include +#include +#include +#include +#include +#include + +static int g_checks, g_failed; +#define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) + +static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } +static float frand(uint32_t *s, float lo, float hi) { return lo + (hi - lo) * ((float)(lcg(s) >> 8) / 16777216.0f); } + +/* ----------------------------------------------------------------- oracles */ +static void oracle_rmsnorm(const float *x, const float *w, float eps, float *out, size_t dim) { + double ss = 0.0; + for (size_t i = 0; i < dim; i++) ss += (double)x[i] * (double)x[i]; + double mean = ss / (double)dim; + float scale = (float)(1.0 / sqrt(mean + (double)eps)); + for (size_t i = 0; i < dim; i++) { float t = x[i] * scale; out[i] = t * w[i]; } +} +static void oracle_rope_tables(size_t head_dim, size_t pos, float theta, float *cos_half, float *sin_half) { + size_t half = head_dim / 2; + for (size_t i = 0; i < half; i++) { + double exponent = (double)(2 * i) / (double)head_dim; + double freq = 1.0 / pow((double)theta, exponent); + double rot = (double)pos * freq; + sin_half[i] = (float)sin(rot); + cos_half[i] = (float)cos(rot); + } +} +static void oracle_rope(const float *v, size_t heads, size_t head_dim, const float *cos_half, const float *sin_half, float *out) { + size_t half = head_dim / 2; + for (size_t h = 0; h < heads; h++) + for (size_t i = 0; i < half; i++) { + size_t i0 = h * head_dim + i, i1 = i0 + half; + float q0 = v[i0], q1 = v[i1], c = cos_half[i], s = sin_half[i]; + float a = q0 * c, b = q1 * s; out[i0] = a - b; + float d = q0 * s, e = q1 * c; out[i1] = d + e; + } +} +static void oracle_swiglu(const float *g, const float *u, float *out, size_t n) { + for (size_t i = 0; i < n; i++) { double gg = g[i]; double silu = gg / (1.0 + exp(-gg)); out[i] = (float)(silu * (double)u[i]); } +} + +/* |got - want| <= rel*|want| + abs; returns the number of violations and the worst scaled error */ +static size_t compare(const float *got, const float *want, size_t n, double rel, double abs_tol, double *worst) { + size_t bad = 0; *worst = 0.0; + for (size_t i = 0; i < n; i++) { + double d = fabs((double)got[i] - (double)want[i]); + double lim = rel * fabs((double)want[i]) + abs_tol; + double scaled = lim > 0 ? d / lim : (d > 0 ? INFINITY : 0.0); + if (scaled > *worst) *worst = scaled; + if (!(d <= lim)) bad++; /* NaN counts as bad */ + } + return bad; +} + +/* ------------------------------------------------------------- host only */ +static int nvdisasm_check(OmegaGpuEwOp op, const char *name, const char *const *must, int n_must) { + const char *nv = "/usr/local/cuda/bin/nvdisasm"; + if (access(nv, X_OK) != 0) { printf("nvdisasm: not present, listing check skipped for %s\n", name); return 0; } + OmegaBlackwellKernel k; memset(&k, 0, sizeof k); + if (omega_gpu_elementwise_codegen(op, NULL, 0.0f, &k) != OMEGA_GPU_EW_OK) return -1; + char bin[64], lst[64], cmd[256]; + snprintf(bin, sizeof bin, "/tmp/omega_ew_%s_%d.bin", name, (int)getpid()); + snprintf(lst, sizeof lst, "/tmp/omega_ew_%s_%d.sass", name, (int)getpid()); + FILE *f = fopen(bin, "wb"); if (!f) { free(k.code); return -1; } + fwrite(k.code, 1, k.code_size, f); fclose(f); + snprintf(cmd, sizeof cmd, "%s -b SM121 %s > %s 2>&1", nv, bin, lst); + int rc = system(cmd); + free(k.code); + FILE *l = fopen(lst, "r"); if (!l) return -1; + char line[512]; int seen[16] = {0}; int bad = 0, insns = 0; + while (fgets(line, sizeof line, l)) { + if (!strstr(line, "/*0")) continue; + insns++; + if (strstr(line, "?") || strstr(line, "error")) bad++; + for (int i = 0; i < n_must; i++) if (strstr(line, must[i])) seen[i] = 1; + } + fclose(l); + int miss = 0; + for (int i = 0; i < n_must; i++) if (!seen[i]) { printf(" %s: nvdisasm listing lacks %s\n", name, must[i]); miss++; } + printf("nvdisasm %s: rc=%d insns=%d undecodable=%d missing=%d (%s)\n", name, rc, insns, bad, miss, lst); + unlink(bin); + return (rc == 0 && bad == 0 && miss == 0) ? 0 : -1; +} + +static void host_only(void) { + CHECK(omega_blackwell_verify_codegen_fixtures_fb1cut4() == 0, "word fixtures MUFU_EX2 / BAR_SYNC / LDS32 / STS32 (rc %d)", omega_blackwell_verify_codegen_fixtures_fb1cut4()); + float a[256] = {0}, b[256] = {0}, c[256] = {0}; + CHECK(omega_gpu_rmsnorm_f32(0, 128, a, b, 1e-5f, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "rows=0 refused"); + CHECK(omega_gpu_rmsnorm_f32(1, 100, a, b, 1e-5f, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "dim not a multiple of 128 refused"); + CHECK(omega_gpu_rmsnorm_f32(1, OMEGA_GPU_EW_MAX_DIM + 128, a, b, 1e-5f, c, NULL) == OMEGA_GPU_EW_TOO_LARGE, "dim over limit refused"); + CHECK(omega_gpu_rope_f32(1, 63, a, b, b, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "odd head_dim refused"); + CHECK(omega_gpu_rope_f32(1, 64, NULL, b, b, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "NULL vector refused"); + CHECK(omega_gpu_swiglu_f32(0, a, b, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "n=0 refused"); + CHECK(omega_gpu_shared_xchg_u32(100, (uint32_t *)a, (uint32_t *)c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "xchg n not a multiple of 128 refused"); + CHECK(strcmp(omega_gpu_elementwise_rc_name(OMEGA_GPU_EW_UNWRITTEN), "UNWRITTEN") == 0, "rc name"); + + struct { OmegaGpuEwOp op; const char *name; const char *must[6]; int n; } ops[] = { + { OMEGA_GPU_EW_EX2, "ex2", { "MUFU.EX2", "@P0 EXIT", "STG.E" }, 3 }, + { OMEGA_GPU_EW_XCHG, "xchg", { "STS [R", "BAR.SYNC", "LDS R" }, 3 }, + { OMEGA_GPU_EW_RMSNORM, "rmsnorm", { "SHFL.DOWN", "BAR.SYNC", "MUFU.RSQ", "@!P0 BRA", "STS [R", "LDS R" }, 6 }, + { OMEGA_GPU_EW_ROPE, "rope", { "FADD", "FMUL", "STG.E" }, 3 }, + { OMEGA_GPU_EW_SWIGLU, "swiglu", { "MUFU.EX2", "MUFU.RCP", "@P0 EXIT" }, 3 }, + }; + for (size_t i = 0; i < sizeof ops / sizeof ops[0]; i++) { + OmegaBlackwellKernel k; memset(&k, 0, sizeof k); + int rc = omega_gpu_elementwise_codegen(ops[i].op, NULL, 0.0f, &k); + CHECK(rc == OMEGA_GPU_EW_OK && k.code_size > 0 && k.gpr_count <= 64, "%s kernel generated (rc %s, %zu insns, %u gprs)", ops[i].name, omega_gpu_elementwise_rc_name(rc), k.insn_count, k.gpr_count); + printf("codegen %s: %zu insns, %u gprs, %u ugprs\n", ops[i].name, k.insn_count, k.gpr_count, k.uniform_gpr_count); + uint8_t digest[32]; memcpy(digest, k.code_digest, 32); + free(k.code); + /* the mutant must be a different kernel */ + omega_gpu_elementwise_test_set_mutant((int)ops[i].op); + memset(&k, 0, sizeof k); + CHECK(omega_gpu_elementwise_codegen(ops[i].op, NULL, 0.0f, &k) == OMEGA_GPU_EW_OK && memcmp(digest, k.code_digest, 32) != 0, "%s mutant kernel differs", ops[i].name); + free(k.code); + omega_gpu_elementwise_test_set_mutant(0); + CHECK(nvdisasm_check(ops[i].op, ops[i].name, ops[i].must, ops[i].n) == 0, "%s nvdisasm listing decodes and shows the expected instructions", ops[i].name); + } +} + +/* ------------------------------------------------------------- chip gate */ +typedef struct { const char *name; size_t n; int rc; size_t bad; double worst; int pass; int mutant_rc; size_t mutant_bad; int mutant_caught; uint64_t chip_ns; uint32_t calls; uint32_t unwritten; } Case; +static Case g_cases[16]; static int g_ncases; + +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_elementwise_rc_name(c.rc), c.bad, c.worst); + CHECK(c.mutant_caught, "%s mutant caught (rc=%s violations=%zu)", c.name, omega_gpu_elementwise_rc_name(c.mutant_rc), c.mutant_bad); + printf("%s %s n=%zu rc=%s violations=%zu worst_scaled_err=%g chip_ns=%" PRIu64 " calls=%u unwritten=%u | mutant rc=%s violations=%zu %s\n", + c.pass && c.mutant_caught ? "PASS" : "FAIL", c.name, c.n, omega_gpu_elementwise_rc_name(c.rc), c.bad, c.worst, c.chip_ns, c.calls, c.unwritten, + omega_gpu_elementwise_rc_name(c.mutant_rc), c.mutant_bad, c.mutant_caught ? "CAUGHT" : "NOT CAUGHT"); + if (out) fprintf(out, "%s{\"case\":\"%s\",\"n\":%zu,\"rc\":\"%s\",\"violations\":%zu,\"worst_scaled_err\":%g,\"pass\":%s,\"chip_elapsed_ns\":%" PRIu64 ",\"chip_calls\":%u,\"unwritten_words\":%u,\"mutant_rc\":\"%s\",\"mutant_violations\":%zu,\"mutant_caught\":%s}", + g_ncases > 1 ? "," : "", c.name, c.n, omega_gpu_elementwise_rc_name(c.rc), c.bad, c.worst, c.pass ? "true" : "false", c.chip_ns, c.calls, c.unwritten, + omega_gpu_elementwise_rc_name(c.mutant_rc), c.mutant_bad, c.mutant_caught ? "true" : "false"); +} + +static int chip(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; } + if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_ELEMENTWISE_V1\",\"tolerances\":{\"ex2_rel\":1e-6,\"xchg\":\"exact\",\"rmsnorm_rel\":1e-5,\"rmsnorm_abs\":1e-6,\"rope\":\"exact\",\"swiglu_rel\":1e-5,\"swiglu_abs\":1e-6},\"cases\":["); + uint32_t seed = 0xfb1c0004u; + OmegaGpuEwInfo info; + + /* 1. EX2 probe, with a negative control on the comparison itself */ + { + size_t n = 4096; float *x = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4), *wrong = malloc(n * 4); + for (size_t i = 0; i < n; i++) { x[i] = frand(&seed, -20.0f, 20.0f); want[i] = (float)exp2((double)x[i]); wrong[i] = (float)exp2((double)x[i] + 1.0); } + Case c = { .name = "ex2_probe", .n = n }; + c.rc = omega_gpu_ex2_f32((uint32_t)n, x, got, &info); + c.bad = compare(got, want, n, 1e-6, 0.0, &c.worst); + double w2; size_t neg = compare(got, wrong, n, 1e-6, 0.0, &w2); + CHECK(neg > n / 2, "ex2 negative control: the check rejects a wrong expectation (%zu of %zu flagged)", neg, n); + c.pass = c.rc == OMEGA_GPU_EW_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; + omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_EX2); + c.mutant_rc = omega_gpu_ex2_f32((uint32_t)n, x, got, &info); + c.mutant_bad = compare(got, want, n, 1e-6, 0.0, &w2); + c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0; + omega_gpu_elementwise_test_set_mutant(0); + record(c, out); free(x); free(got); free(want); free(wrong); + } + /* 2. shared-memory exchange probe (STS, BAR.SYNC, LDS) */ + { + size_t n = 4096; uint32_t *x = malloc(n * 4), *got = malloc(n * 4); + for (size_t i = 0; i < n; i++) x[i] = lcg(&seed); + Case c = { .name = "shared_xchg_probe", .n = n }; + c.rc = omega_gpu_shared_xchg_u32((uint32_t)n, x, got, &info); + for (size_t i = 0; i < n; i++) if (got[i] != x[i ^ (OMEGA_GPU_EW_THREADS - 1)]) c.bad++; + c.pass = c.rc == OMEGA_GPU_EW_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; + omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_XCHG); + c.mutant_rc = omega_gpu_shared_xchg_u32((uint32_t)n, x, got, &info); + for (size_t i = 0; i < n; i++) if (got[i] != x[i ^ (OMEGA_GPU_EW_THREADS - 1)]) c.mutant_bad++; + c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0; + omega_gpu_elementwise_test_set_mutant(0); + record(c, out); free(x); free(got); + } + /* 3. rmsnorm at the model hidden sizes (TinyLlama and Llama-3.2-1B: 2048), plus small and large */ + { size_t dims[3] = { 2048, 256, 8192 }; size_t rowss[3] = { 4, 1, 2 }; + for (int t = 0; t < 3; t++) { + size_t dim = dims[t], rows = rowss[t], n = rows * dim; float eps = 1e-5f; + float *x = malloc(n * 4), *w = malloc(dim * 4), *got = malloc(n * 4), *want = malloc(n * 4), *again = malloc(n * 4); + for (size_t i = 0; i < n; i++) x[i] = frand(&seed, -3.0f, 3.0f); + for (size_t i = 0; i < dim; i++) w[i] = frand(&seed, 0.5f, 1.5f); + for (size_t r = 0; r < rows; r++) oracle_rmsnorm(x + r * dim, w, eps, want + r * dim, dim); + char name[64]; snprintf(name, sizeof name, "rmsnorm_%zux%zu", rows, dim); + Case c = { .name = strdup(name), .n = n }; + c.rc = omega_gpu_rmsnorm_f32((uint32_t)rows, (uint32_t)dim, x, w, eps, got, &info); + c.bad = compare(got, want, n, 1e-5, 1e-6, &c.worst); + c.pass = c.rc == OMEGA_GPU_EW_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; + OmegaGpuEwInfo i2; int rc2 = omega_gpu_rmsnorm_f32((uint32_t)rows, (uint32_t)dim, x, w, eps, again, &i2); + CHECK(rc2 == OMEGA_GPU_EW_OK && i2.kernel_cache_hit && memcmp(got, again, n * 4) == 0, "%s repeat run (cache hit) is bit-identical", name); + omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_RMSNORM); + double w2; c.mutant_rc = omega_gpu_rmsnorm_f32((uint32_t)rows, (uint32_t)dim, x, w, eps, got, &info); + c.mutant_bad = compare(got, want, n, 1e-5, 1e-6, &w2); + c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0; + omega_gpu_elementwise_test_set_mutant(0); + record(c, out); free(x); free(w); free(got); free(want); free(again); + } } + /* 4. rope: TinyLlama (32 q heads, 4 kv heads, head_dim 64, theta 10000) and Llama-3.2-1B (32/8, 64, 500000) */ + { struct { size_t heads, hd, pos; float theta; } rs[4] = { { 32, 64, 17, 10000.0f }, { 4, 64, 17, 10000.0f }, { 32, 64, 1023, 500000.0f }, { 8, 64, 1023, 500000.0f } }; + for (int t = 0; t < 4; t++) { + size_t heads = rs[t].heads, hd = rs[t].hd, n = heads * hd, half = hd / 2; + float *v = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4), *ch = malloc(half * 4), *sh = malloc(half * 4); + for (size_t i = 0; i < n; i++) v[i] = frand(&seed, -4.0f, 4.0f); + oracle_rope_tables(hd, rs[t].pos, rs[t].theta, ch, sh); + oracle_rope(v, heads, hd, ch, sh, want); + char name[64]; snprintf(name, sizeof name, "rope_%zuh_%zud_pos%zu_theta%g", heads, hd, rs[t].pos, rs[t].theta); + Case c = { .name = strdup(name), .n = n }; + c.rc = omega_gpu_rope_f32((uint32_t)heads, (uint32_t)hd, v, ch, sh, got, &info); + c.bad = compare(got, want, n, 0.0, 0.0, &c.worst); + c.pass = c.rc == OMEGA_GPU_EW_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; + omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_ROPE); + double w2; c.mutant_rc = omega_gpu_rope_f32((uint32_t)heads, (uint32_t)hd, v, ch, sh, got, &info); + c.mutant_bad = compare(got, want, n, 0.0, 0.0, &w2); + c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0; + omega_gpu_elementwise_test_set_mutant(0); + record(c, out); free(v); free(got); free(want); free(ch); free(sh); + } } + /* 5. swiglu at the model ffn sizes (TinyLlama 5632, Llama-3.2-1B 8192) and an odd tail */ + { size_t ns[3] = { 5632, 8192, 1000 }; + for (int t = 0; t < 3; t++) { + size_t n = ns[t]; + float *g = malloc(n * 4), *u = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4); + for (size_t i = 0; i < n; i++) { g[i] = frand(&seed, -12.0f, 12.0f); u[i] = frand(&seed, -2.0f, 2.0f); } + oracle_swiglu(g, u, want, n); + char name[64]; snprintf(name, sizeof name, "swiglu_%zu", n); + Case c = { .name = strdup(name), .n = n }; + c.rc = omega_gpu_swiglu_f32((uint32_t)n, g, u, got, &info); + c.bad = compare(got, want, n, 1e-5, 1e-6, &c.worst); + c.pass = c.rc == OMEGA_GPU_EW_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; + omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_SWIGLU); + double w2; c.mutant_rc = omega_gpu_swiglu_f32((uint32_t)n, g, u, got, &info); + c.mutant_bad = compare(got, want, n, 1e-5, 1e-6, &w2); + c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0; + omega_gpu_elementwise_test_set_mutant(0); + record(c, out); free(g); free(u); free(got); free(want); + } } + /* 6. stale-SM-instruction-cache guard (cut 1b finding, omega #256): 10 distinct kernels + * (5 ops + 5 mutants) in rotation, two rounds. Every launch uploads code into a freshly + * allocated page, so a kernel routinely lands on the code address its predecessor used, + * and 10 keys evict the 8-slot kernel cache. Each clean launch must match the oracle and + * each mutant must still be caught; a stale icache would make a mutant pass or a clean + * kernel fail. Red was observed by cut 1b on matmul before its invalidate (receipt + * FB1-CUT1B-f629798); this build carries the invalidate, so the case is a guard. */ + { + enum { RN = 256, RH = 4, RD = 64 }; + float *x = malloc(RN * 4), *w = malloc(RN * 4), *u = malloc(RN * 4), *got = malloc(RN * 4), *want = malloc(RN * 4); + float *ch = malloc(RD / 2 * 4), *sh = malloc(RD / 2 * 4); + uint32_t *xu = malloc(RN * 4), *gu = malloc(RN * 4); + for (size_t i = 0; i < RN; i++) { x[i] = frand(&seed, -3.0f, 3.0f); w[i] = frand(&seed, 0.5f, 1.5f); u[i] = frand(&seed, -2.0f, 2.0f); xu[i] = lcg(&seed); } + oracle_rope_tables(RD, 17, 10000.0f, ch, sh); + Case c = { .name = "icache_reuse_rotation", .n = 20 }; + c.rc = OMEGA_GPU_EW_OK; c.mutant_rc = OMEGA_GPU_EW_OK; c.mutant_caught = 1; + const int order[10] = { OMEGA_GPU_EW_EX2, -OMEGA_GPU_EW_SWIGLU, OMEGA_GPU_EW_XCHG, -OMEGA_GPU_EW_EX2, OMEGA_GPU_EW_RMSNORM, + -OMEGA_GPU_EW_XCHG, OMEGA_GPU_EW_SWIGLU, -OMEGA_GPU_EW_ROPE, OMEGA_GPU_EW_ROPE, -OMEGA_GPU_EW_RMSNORM }; + for (int step = 0; step < 20; step++) { + int round = step / 10, k = step % 10; + int mut = order[k] < 0, op = mut ? -order[k] : order[k]; + omega_gpu_elementwise_test_set_mutant(mut ? op : 0); + int rc; size_t bad = 0; double wst; + switch (op) { + case OMEGA_GPU_EW_EX2: + for (size_t i = 0; i < RN; i++) want[i] = (float)exp2((double)x[i]); + rc = omega_gpu_ex2_f32(RN, x, got, &info); bad = compare(got, want, RN, 1e-6, 0.0, &wst); break; + case OMEGA_GPU_EW_XCHG: + rc = omega_gpu_shared_xchg_u32(RN, xu, gu, &info); + for (size_t i = 0; i < RN; i++) { if (gu[i] != xu[i ^ (OMEGA_GPU_EW_THREADS - 1)]) bad++; } + break; + case OMEGA_GPU_EW_RMSNORM: + oracle_rmsnorm(x, w, 1e-5f, want, RN); + rc = omega_gpu_rmsnorm_f32(1, RN, x, w, 1e-5f, got, &info); bad = compare(got, want, RN, 1e-5, 1e-6, &wst); break; + case OMEGA_GPU_EW_ROPE: + oracle_rope(x, RH, RD, ch, sh, want); + rc = omega_gpu_rope_f32(RH, RD, x, ch, sh, got, &info); bad = compare(got, want, RH * RD, 0.0, 0.0, &wst); break; + default: + oracle_swiglu(x, u, want, RN); + rc = omega_gpu_swiglu_f32(RN, x, u, got, &info); bad = compare(got, want, RN, 1e-5, 1e-6, &wst); break; + } + omega_gpu_elementwise_test_set_mutant(0); + int ok = rc == OMEGA_GPU_EW_OK && bad == 0; + if (!mut && !ok) { c.bad++; if (rc != OMEGA_GPU_EW_OK) c.rc = rc; printf(" rotation %d/%d: clean op %d FAILED rc=%s violations=%zu\n", round, k, op, omega_gpu_elementwise_rc_name(rc), bad); } + if (mut && ok) { c.mutant_bad++; c.mutant_caught = 0; printf(" rotation %d/%d: mutant op %d NOT caught\n", round, k, op); } + c.chip_ns += info.elapsed_ns; c.calls += info.chip_calls; c.unwritten += info.unwritten_words; + } + c.pass = c.bad == 0 && c.rc == OMEGA_GPU_EW_OK; + record(c, out); + free(x); free(w); free(u); free(got); free(want); free(ch); free(sh); free(xu); free(gu); + } + /* cut 4b: the whole battery (clean and mutant kernels, cache clears) ran on ONE device open */ + CHECK(omega_gpu_session_open_count() == 1, "device opened once for the battery (opens=%u)", omega_gpu_session_open_count()); + printf("device_opens=%u\n", omega_gpu_session_open_count()); + if (out) { + 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_ELEMENTWISE_PASS" : "OMEGA_GPU_ELEMENTWISE_FAIL"); + fclose(out); + } + return 0; +} + +/* ------------------------------------------------------------ timing gate (cut 4b) */ +/* The persistent device: 100 calls each of rmsnorm 1x2048 and swiglu 5632 (the TinyLlama + * decode shapes), every call checked against the oracle, median wall time per call under + * 0.5 ms (cut 4 opened and closed the device per call: ~40 ms). The device must have been + * opened exactly once in this process. */ +static int cmp_d(const void *a, const void *b) { double x = *(const double *)a, y = *(const double *)b; return x < y ? -1 : x > y; } +static double now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; } +static int timing(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; } + if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_ELEMENTWISE_TIMING_V1\",\"gate_median_ms\":0.5,\"calls\":100,\"cases\":["); + uint32_t seed = 0xfb1c004bu; + enum { N = 100 }; + for (int t = 0; t < 2; t++) { + const char *name = t == 0 ? "rmsnorm_1x2048" : "swiglu_5632"; + size_t n = t == 0 ? 2048 : 5632; + float *a = malloc(n * 4), *b = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4); + for (size_t i = 0; i < n; i++) { a[i] = frand(&seed, -3.0f, 3.0f); b[i] = t == 0 ? frand(&seed, 0.5f, 1.5f) : frand(&seed, -2.0f, 2.0f); } + if (t == 0) oracle_rmsnorm(a, b, 1e-5f, want, n); else oracle_swiglu(a, b, want, n); + double samples[N]; int ok_calls = 0; size_t bad_total = 0; double worst = 0; uint64_t chip_sum = 0; int rc_last = 0; + OmegaGpuEwInfo info; + /* warm-up: first call opens the device and generates the kernel (not timed) */ + int rc0 = t == 0 ? omega_gpu_rmsnorm_f32(1, (uint32_t)n, a, b, 1e-5f, got, &info) : omega_gpu_swiglu_f32((uint32_t)n, a, b, got, &info); + double first_ms = info.call_ns / 1e6; + CHECK(rc0 == OMEGA_GPU_EW_OK, "%s first call rc=%s err=\"%s\"", name, omega_gpu_elementwise_rc_name(rc0), omega_gpu_elementwise_last_error()); + for (int i = 0; i < N; i++) { + double t0 = now_ms(); + int rc = t == 0 ? omega_gpu_rmsnorm_f32(1, (uint32_t)n, a, b, 1e-5f, got, &info) : omega_gpu_swiglu_f32((uint32_t)n, a, b, got, &info); + samples[i] = now_ms() - t0; + double w; size_t bad = compare(got, want, n, 1e-5, 1e-6, &w); + if (w > worst) worst = w; + bad_total += bad; chip_sum += info.elapsed_ns; rc_last = rc; + if (rc == OMEGA_GPU_EW_OK && bad == 0) ok_calls++; + } + qsort(samples, N, sizeof samples[0], cmp_d); + double median = samples[N / 2], p90 = samples[N * 9 / 10], mn = samples[0], mx = samples[N - 1]; + uint32_t opens = omega_gpu_session_open_count(); + int pass = ok_calls == N && bad_total == 0 && median < 0.5 && opens == 1; + CHECK(pass, "timing %s ok_calls=%d/%d violations=%zu median_ms=%.3f opens=%u last_rc=%s err=\"%s\"", name, ok_calls, N, bad_total, median, opens, omega_gpu_elementwise_rc_name(rc_last), omega_gpu_elementwise_last_error()); + printf("%s timing %s first_call_ms=%.3f ok_calls=%d/%d violations=%zu worst_scaled_err=%g median_ms=%.3f p90_ms=%.3f min_ms=%.3f max_ms=%.3f chip_us_mean=%.1f device_opens=%u\n", + pass ? "PASS" : "FAIL", name, first_ms, ok_calls, N, bad_total, worst, median, p90, mn, mx, chip_sum / 1e3 / N, opens); + if (out) fprintf(out, "%s{\"case\":\"%s\",\"first_call_ms\":%.3f,\"ok_calls\":%d,\"violations\":%zu,\"worst_scaled_err\":%g,\"median_ms\":%.4f,\"p90_ms\":%.4f,\"min_ms\":%.4f,\"max_ms\":%.4f,\"chip_us_mean\":%.1f,\"device_opens\":%u,\"pass\":%s}", + t ? "," : "", name, first_ms, ok_calls, bad_total, worst, median, p90, mn, mx, chip_sum / 1e3 / N, opens, pass ? "true" : "false"); + free(a); free(b); free(got); free(want); + } + if (out) { fprintf(out, "],\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", g_checks, g_failed, g_failed == 0 ? "OMEGA_GPU_ELEMENTWISE_TIMING_PASS" : "OMEGA_GPU_ELEMENTWISE_TIMING_FAIL"); fclose(out); } + return 0; +} + +int main(int argc, char **argv) { + const char *out_path = NULL; int host = 0, timing_mode = 0; + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "--host-only") == 0) host = 1; + else if (strcmp(argv[i], "--timing") == 0) timing_mode = 1; + else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i]; + } + if (host) host_only(); + else if (timing_mode) { if (timing(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/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/gpu_matmul_api_test.c b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/gpu_matmul_api_test.c new file mode 100644 index 000000000..17297dac3 --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/gpu_matmul_api_test.c @@ -0,0 +1,258 @@ +/* gpu_matmul_api_test: FB-1 cut 1 / 1b gate for omega_gpu_matmul_api. + * + * ./gpu_matmul_api_test --host-only argument and limit refusals, codegen only, no chip + * ./gpu_matmul_api_test --out receipt.json chip sweep over shapes, JSON receipt + * ./gpu_matmul_api_test --timing --out r.json resident GEMV timing (median of 100 calls) + parity + * ./gpu_matmul_api_test --mutant --out r.json wrong kernel (last K step dropped) must FAIL parity; + * exit 0 when every shape caught it ("mutant killed") + * ./gpu_matmul_api_test --cta-budget N ... run with a different CTA budget (I42 experiment) + * + * Shapes come from OMEGA_GPU_MATMUL_SHAPES ("m,k,n;m,k,n;..."), default below. + * Pass rule per shape: rc OK, parity verified by the API oracle, end-to-end error < 1e-5 relative to + * the accumulation scale (bf16 inputs against a double reference; only accumulation order and the + * chip's f32 accumulator differ), second call hits the kernel cache and agrees bit for bit. + */ +#include "omega_gpu_matmul_api.h" +#include "omega_blackwell_codegen.h" +#include "omega_blackwell_matmul.h" +#include +#include +#include +#include +#include +#include + +/* round a float to bf16 and back, as the API does on the way in */ +static float bf16r(float f) { uint16_t h = omega_fp32_to_bf16(f); uint32_t u = (uint32_t)h << 16; float o; memcpy(&o, &u, 4); return o; } + +static int g_checks, g_failed; +#define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) + +/* cut-1 list unchanged, then the cut-1b Llama shapes (hidden 2048, ffn 8192, vocab 128256) */ +static const char *DEFAULT_SHAPES = + "16,16,16;32,32,32;64,64,64;128,128,128;256,256,256;512,512,512;" + "1,1024,1024;1,256,1024;16,1024,256;8,1024,8;" + "1024,16,128;192,16,192;512,16,64;256,16,256;128,16,512;" + "1,2048,8192;1,8192,2048;1,2048,128256"; +static const char *MUTANT_SHAPES = "16,32,16;1,2048,8192;64,64,64;1,1024,1024"; +static const char *TIMING_SHAPES = "1,2048,8192;1,8192,2048"; + +static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } +static double now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; } + +static void fill(float *a, size_t n, uint32_t *seed) { for (size_t i = 0; i < n; i++) a[i] = ((float)(lcg(seed) % 2001) - 1000.0f) / 1000.0f; } + +/* end-to-end: full-K product on the host from the same bf16-rounded inputs, in double; error relative + * to the accumulation scale (sum of |terms|), not to a result that may cancel to near zero */ +static double e2e_max_rel(uint32_t m, uint32_t k, uint32_t n, const float *a, const float *b, const float *c, size_t *bad) { + double worst = 0.0; size_t nbad = 0; + for (size_t i = 0; i < m; i++) for (size_t j = 0; j < n; j++) { + double acc = 0.0, scale = 0.0; + for (size_t t = 0; t < k; t++) { double p = (double)bf16r(a[i * k + t]) * (double)bf16r(b[t * n + j]); acc += p; scale += fabs(p); } + double rel = fabs(acc - (double)c[i * n + j]) / (scale > 1e-6 ? scale : 1e-6); + if (rel > worst) worst = rel; + if (rel >= 1e-5) nbad++; + } + if (bad) *bad = nbad; + return worst; +} + +static void host_only(void) { + float a[16 * 16] = {0}, b[16 * 16] = {0}, c[16 * 16] = {0}; + OmegaGpuMatmulInfo info; + CHECK(omega_gpu_matmul_f32(0, 16, 16, a, b, c, &info) == OMEGA_GPU_MATMUL_BAD_ARGS, "m=0 refused"); + CHECK(omega_gpu_matmul_f32(16, 16, 16, NULL, b, c, &info) == OMEGA_GPU_MATMUL_BAD_ARGS, "NULL a refused"); + CHECK(omega_gpu_matmul_f32(OMEGA_BW_MATMUL_MAX_M + 1, 16, 16, a, b, c, &info) == OMEGA_GPU_MATMUL_TOO_LARGE, "m over limit refused"); + CHECK(omega_gpu_matmul_bf16(16, OMEGA_BW_MATMUL_MAX_K + 1, 16, (uint16_t *)a, (uint16_t *)b, c, NULL) == OMEGA_GPU_MATMUL_TOO_LARGE, "k over limit refused (NULL info ok)"); + CHECK(omega_gpu_matmul_bf16(16, 16, OMEGA_BW_MATMUL_MAX_N + 1, (uint16_t *)a, (uint16_t *)b, c, NULL) == OMEGA_GPU_MATMUL_TOO_LARGE, "n over limit refused"); + CHECK(OMEGA_BW_MATMUL_MAX_N >= 128256 && OMEGA_BW_MATMUL_MAX_K >= 8192 && OMEGA_BW_MATMUL_MAX_M >= 2048, "limits cover Llama shapes (vocab 128256, ffn 8192)"); + OmegaGpuTensor *t = NULL; + CHECK(omega_gpu_tensor_upload_f32(0, 16, a, &t) == OMEGA_GPU_MATMUL_BAD_ARGS && t == NULL, "tensor k=0 refused"); + CHECK(omega_gpu_tensor_upload_f32(16, 16, NULL, &t) == OMEGA_GPU_MATMUL_BAD_ARGS, "tensor NULL refused"); + CHECK(omega_gpu_matmul_resident_f32(16, a, NULL, c, &info) == OMEGA_GPU_MATMUL_BAD_ARGS, "resident NULL tensor refused"); + CHECK(strcmp(omega_gpu_matmul_rc_name(OMEGA_GPU_MATMUL_PARITY_FAIL), "PARITY_FAIL") == 0, "rc name"); + CHECK(omega_gpu_matmul_cta_budget() == OMEGA_GPU_MATMUL_MAX_CTAS, "default CTA budget"); + CHECK(omega_gpu_matmul_is_blocked() == 0, "not blocked at start"); + + /* codegen only: the looped kernel must accept every padded Llama shape without touching the chip */ + struct { uint32_t k, n, gx; } ok[] = { {16, 8, 1}, {2048, 8192, 64}, {8192, 2048, 64}, {2048, 128256, 64}, {1024, 1024, 1}, {16384, 262144, 64} }; + for (size_t i = 0; i < sizeof ok / sizeof ok[0]; i++) { + OmegaMatMulSpec spec; OmegaBlackwellKernel kernel; memset(&kernel, 0, sizeof kernel); + int rc = omega_matmul_spec_init(&spec, 16, ok[i].k, ok[i].n, OMEGA_MATMUL_PRECISION_BF16) == 0 && + omega_blackwell_codegen_matmul_tensor_loop(&spec, ok[i].gx, 0, &kernel) == 0 && kernel.code_size > 0; + CHECK(rc, "loop kernel K=%u N=%u gx=%u generated", ok[i].k, ok[i].n, ok[i].gx); + CHECK(kernel.gpr_count <= 64, "loop kernel K=%u N=%u fits 64 registers (got %u)", ok[i].k, ok[i].n, kernel.gpr_count); + /* the mutant kernel is a different program */ + OmegaBlackwellKernel mut; memset(&mut, 0, sizeof mut); + if (omega_blackwell_codegen_matmul_tensor_loop(&spec, ok[i].gx, 1, &mut) == 0) { + CHECK(memcmp(mut.code_digest, kernel.code_digest, 32) != 0, "mutant kernel differs K=%u N=%u", ok[i].k, ok[i].n); + omega_blackwell_kernel_free(&mut); + } else CHECK(ok[i].k == 16, "mutant refused only for K=16 (K=%u)", ok[i].k); + omega_blackwell_kernel_free(&kernel); + } + OmegaMatMulSpec spec; OmegaBlackwellKernel kernel; memset(&kernel, 0, sizeof kernel); + CHECK(omega_matmul_spec_init(&spec, 16, 24, 16, OMEGA_MATMUL_PRECISION_BF16) == 0 && omega_blackwell_codegen_matmul_tensor_loop(&spec, 1, 0, &kernel) != 0, "K not multiple of 16 refused by codegen (the API pads)"); + CHECK(omega_matmul_spec_init(&spec, 16, 16, 16, OMEGA_MATMUL_PRECISION_BF16) == 0 && omega_blackwell_codegen_matmul_tensor_loop(&spec, 3, 0, &kernel) != 0, "grid_x beyond the column tiles refused"); + /* the single-tile kernel of cut 1 still generates (other gates use it) */ + memset(&kernel, 0, sizeof kernel); + CHECK(omega_matmul_spec_init(&spec, 16, 16, 8, OMEGA_MATMUL_PRECISION_BF16) == 0 && omega_blackwell_codegen_matmul(&spec, &kernel) == 0 && kernel.code_size > 0, "16x16x8 single-tile kernel generated"); + omega_blackwell_kernel_free(&kernel); + omega_gpu_matmul_cache_clear(); +} + +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; } + if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_MATMUL_API_SWEEP_V2\",\"mutant\":%s,\"cta_budget\":%u,\"shapes\":[", mutant ? "true" : "false", omega_gpu_matmul_cta_budget()); + char *copy = strdup(shapes); + char *save = NULL; + int first = 1; + for (char *tok = strtok_r(copy, ";", &save); tok; tok = strtok_r(NULL, ";", &save)) { + unsigned m, k, n; + if (sscanf(tok, "%u,%u,%u", &m, &k, &n) != 3) { printf("bad shape %s\n", tok); continue; } + size_t na = (size_t)m * k, nb = (size_t)k * n, nc = (size_t)m * n; + float *a = malloc(na * sizeof *a), *b = malloc(nb * sizeof *b), *c = malloc(nc * sizeof *c), *c2 = malloc(nc * sizeof *c2); + if (!a || !b || !c || !c2) { printf("out of host memory for %s\n", tok); free(a); free(b); free(c); free(c2); continue; } + uint32_t seed = 0x5eed0000u ^ (m << 20) ^ (k << 10) ^ n; + fill(a, na, &seed); fill(b, nb, &seed); + for (size_t i = 0; i < nc; i++) c[i] = -12345.0f; + OmegaGpuMatmulInfo info, info2; + double t0 = now_ms(); + int rc = omega_gpu_matmul_f32(m, k, n, a, b, c, &info); + double wall_ms = now_ms() - t0; + /* second call on the same shape must hit the kernel cache and agree */ + int rc2 = omega_gpu_matmul_f32(m, k, n, a, b, c2, &info2); + size_t repeat_mismatch = 0; + for (size_t i = 0; i < nc; i++) if (c[i] != c2[i]) repeat_mismatch++; + size_t e2e_bad = 0; + double e2e = (rc == OMEGA_GPU_MATMUL_OK || rc == OMEGA_GPU_MATMUL_PARITY_FAIL) ? e2e_max_rel(m, k, n, a, b, c, &e2e_bad) : 0.0; + int pass = rc == OMEGA_GPU_MATMUL_OK && info.parity_verified && e2e < 1e-5 + && rc2 == OMEGA_GPU_MATMUL_OK && info2.kernel_cache_hit && repeat_mismatch == 0; + if (mutant) { + /* the wrong kernel must be caught: PARITY_FAIL from the API oracle and a visible e2e error */ + int caught = rc == OMEGA_GPU_MATMUL_PARITY_FAIL && !info.parity_verified && e2e >= 1e-5; + CHECK(caught, "mutant shape %ux%ux%u NOT caught: rc=%s parity=%d e2e_rel=%g", m, k, n, omega_gpu_matmul_rc_name(rc), info.parity_verified, e2e); + pass = caught; + } else { + CHECK(pass, "shape %ux%ux%u rc=%s parity=%d max_rel=%g e2e_rel=%g rc2=%s hit=%d repeat_mismatch=%zu", + m, k, n, omega_gpu_matmul_rc_name(rc), info.parity_verified, info.max_rel_err, e2e, + omega_gpu_matmul_rc_name(rc2), info2.kernel_cache_hit, repeat_mismatch); + } + printf("%s %ux%ux%u rc=%s chip_ns=%" PRIu64 " call_ms=%.3f wall_ms=%.1f max_abs=%g max_rel=%g mismatches=%zu e2e_rel=%g e2e_bad=%zu grid=%ux%u calls=%u rows_per_call=%u cache_hit2=%d err=\"%s\"\n", + pass ? (mutant ? "KILLED" : "PASS") : (mutant ? "SURVIVED" : "FAIL"), m, k, n, omega_gpu_matmul_rc_name(rc), info.elapsed_ns, info.call_ns / 1e6, wall_ms, + info.max_abs_err, info.max_rel_err, info.mismatch_count, e2e, e2e_bad, info.grid_x, info.grid_y, info.chip_calls, info.rows_per_call, info2.kernel_cache_hit, rc == OMEGA_GPU_MATMUL_CHIP_FAIL ? omega_gpu_matmul_last_error() : ""); + if (out) { + fprintf(out, "%s{\"m\":%u,\"k\":%u,\"n\":%u,\"rc\":\"%s\",\"pass\":%s,\"chip_elapsed_ns\":%" PRIu64 + ",\"call_ms\":%.3f,\"wall_ms\":%.3f,\"max_abs_err\":%g,\"max_rel_err\":%g,\"mismatch_count\":%zu," + "\"parity_verified\":%s,\"completion_marker\":%u,\"repeat_rc\":\"%s\",\"repeat_cache_hit\":%s," + "\"repeat_mismatch\":%zu,\"e2e_max_rel\":%g,\"e2e_bad\":%zu,\"grid_x\":%u,\"grid_y\":%u,\"chip_calls\":%u,\"rows_per_call\":%u,\"padded_m\":%u,\"padded_k\":%u,\"padded_n\":%u,\"target_chip\":\"%s\",\"sm_architecture\":%u}", + first ? "" : ",", m, k, n, omega_gpu_matmul_rc_name(rc), pass ? "true" : "false", + info.elapsed_ns, info.call_ns / 1e6, wall_ms, info.max_abs_err, info.max_rel_err, info.mismatch_count, + info.parity_verified ? "true" : "false", info.completion_marker, + omega_gpu_matmul_rc_name(rc2), info2.kernel_cache_hit ? "true" : "false", + repeat_mismatch, e2e, e2e_bad, info.grid_x, info.grid_y, info.chip_calls, info.rows_per_call, info.padded_m, info.padded_k, info.padded_n, info.target_chip, info.sm_architecture); + first = 0; + } + free(a); free(b); free(c); free(c2); + if (omega_gpu_matmul_is_blocked()) { printf("device latched after an uncertain completion; stopping the sweep\n"); break; } + } + free(copy); + if (out) { + fprintf(out, "],\"blocked\":%s,\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", omega_gpu_matmul_is_blocked() ? "true" : "false", g_checks, g_failed, + g_failed == 0 ? (mutant ? "OMEGA_GPU_MATMUL_MUTANT_KILLED" : "OMEGA_GPU_MATMUL_API_PASS") : (mutant ? "OMEGA_GPU_MATMUL_MUTANT_SURVIVED" : "OMEGA_GPU_MATMUL_API_FAIL")); + fclose(out); + } + return 0; +} + +static int cmp_double(const void *x, const void *y) { double a = *(const double *)x, b = *(const double *)y; return a < b ? -1 : a > b; } + +/* Resident path: upload B once, then 100 calls; median wall time per call must be under 5 ms. + * Parity: first and last result against the double reference (0 elements at or beyond 1e-5). */ +static int timing(const char *shapes, 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; } + if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_MATMUL_API_TIMING_V1\",\"cta_budget\":%u,\"calls\":100,\"shapes\":[", omega_gpu_matmul_cta_budget()); + char *copy = strdup(shapes); char *save = NULL; int first = 1; + for (char *tok = strtok_r(copy, ";", &save); tok; tok = strtok_r(NULL, ";", &save)) { + unsigned m, k, n; + if (sscanf(tok, "%u,%u,%u", &m, &k, &n) != 3) { printf("bad shape %s\n", tok); continue; } + size_t na = (size_t)m * k, nb = (size_t)k * n, nc = (size_t)m * n; + float *a = malloc(na * sizeof *a), *b = malloc(nb * sizeof *b), *c = malloc(nc * sizeof *c), *c_first = malloc(nc * sizeof *c_first); + uint16_t *ah = malloc(na * sizeof *ah); + if (!a || !b || !c || !c_first || !ah) { printf("out of host memory\n"); return 2; } + uint32_t seed = 0x7111e000u ^ (m << 20) ^ (k << 10) ^ n; + fill(a, na, &seed); fill(b, nb, &seed); + for (size_t i = 0; i < na; i++) ah[i] = omega_fp32_to_bf16(a[i]); + OmegaGpuTensor *t = NULL; + double tu0 = now_ms(); + int urc = omega_gpu_tensor_upload_f32(k, n, b, &t); + double upload_ms = now_ms() - tu0; + CHECK(urc == OMEGA_GPU_MATMUL_OK, "upload %ux%u rc=%s", k, n, omega_gpu_matmul_rc_name(urc)); + double samples[100]; int ok_calls = 0; OmegaGpuMatmulInfo info; memset(&info, 0, sizeof info); + uint64_t chip_ns_sum = 0; size_t repeat_mismatch = 0; + if (urc == OMEGA_GPU_MATMUL_OK) { + for (int i = 0; i < 100; i++) { + for (size_t j = 0; j < nc; j++) c[j] = -12345.0f; + double t0 = now_ms(); + int rc = omega_gpu_matmul_resident_bf16(m, ah, t, c, &info); + samples[i] = now_ms() - t0; + if (rc == OMEGA_GPU_MATMUL_OK) ok_calls++; + chip_ns_sum += info.elapsed_ns; + if (i == 0) memcpy(c_first, c, nc * sizeof *c); + else for (size_t j = 0; j < nc; j++) if (c[j] != c_first[j]) repeat_mismatch++; + if (omega_gpu_matmul_is_blocked()) break; + } + } + qsort(samples, 100, sizeof samples[0], cmp_double); + double median = samples[50], p90 = samples[90], mn = samples[0], mx = samples[99]; + size_t bad_first = 0, bad_last = 0; + double e2e_first = e2e_max_rel(m, k, n, a, b, c_first, &bad_first); + double e2e_last = e2e_max_rel(m, k, n, a, b, c, &bad_last); + int pass = ok_calls == 100 && bad_first == 0 && bad_last == 0 && repeat_mismatch == 0 && median < 5.0; + CHECK(pass, "timing %ux%ux%u ok_calls=%d median_ms=%.3f e2e_first=%g e2e_last=%g bad=%zu/%zu repeat_mismatch=%zu", + m, k, n, ok_calls, median, e2e_first, e2e_last, bad_first, bad_last, repeat_mismatch); + printf("%s timing %ux%ux%u upload_ms=%.1f ok_calls=%d median_ms=%.3f p90_ms=%.3f min_ms=%.3f max_ms=%.3f chip_ns_mean=%" PRIu64 " grid=%ux%u e2e_first=%g e2e_last=%g repeat_mismatch=%zu\n", + pass ? "PASS" : "FAIL", m, k, n, upload_ms, ok_calls, median, p90, mn, mx, ok_calls ? chip_ns_sum / (uint64_t)ok_calls : 0, info.grid_x, info.grid_y, e2e_first, e2e_last, repeat_mismatch); + if (out) { + fprintf(out, "%s{\"m\":%u,\"k\":%u,\"n\":%u,\"pass\":%s,\"upload_ms\":%.3f,\"ok_calls\":%d,\"median_ms\":%.4f,\"p90_ms\":%.4f,\"min_ms\":%.4f,\"max_ms\":%.4f," + "\"chip_ns_mean\":%" PRIu64 ",\"grid_x\":%u,\"grid_y\":%u,\"e2e_first\":%g,\"e2e_last\":%g,\"e2e_bad_first\":%zu,\"e2e_bad_last\":%zu,\"repeat_mismatch\":%zu}", + first ? "" : ",", m, k, n, pass ? "true" : "false", upload_ms, ok_calls, median, p90, mn, mx, + ok_calls ? chip_ns_sum / (uint64_t)ok_calls : 0, info.grid_x, info.grid_y, e2e_first, e2e_last, bad_first, bad_last, repeat_mismatch); + first = 0; + } + omega_gpu_tensor_free(t); + free(a); free(b); free(c); free(c_first); free(ah); + if (omega_gpu_matmul_is_blocked()) break; + } + free(copy); + if (out) { + fprintf(out, "],\"blocked\":%s,\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", omega_gpu_matmul_is_blocked() ? "true" : "false", g_checks, g_failed, + g_failed == 0 ? "OMEGA_GPU_MATMUL_TIMING_PASS" : "OMEGA_GPU_MATMUL_TIMING_FAIL"); + fclose(out); + } + return 0; +} + +int main(int argc, char **argv) { + const char *out_path = NULL; + int host = 0, mutant = 0, do_timing = 0; + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "--host-only") == 0) host = 1; + else if (strcmp(argv[i], "--mutant") == 0) mutant = 1; + else if (strcmp(argv[i], "--timing") == 0) do_timing = 1; + else if (strcmp(argv[i], "--cta-budget") == 0 && i + 1 < argc) omega_gpu_matmul_set_cta_budget((uint32_t)strtoul(argv[++i], NULL, 10)); + else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i]; + } + const char *shapes = getenv("OMEGA_GPU_MATMUL_SHAPES"); + if (shapes && !*shapes) shapes = NULL; + int rc = 0; + if (host) host_only(); + 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 (!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)") : ""); + return g_failed ? 1 : 0; +} diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/matmul.json b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/matmul.json new file mode 100644 index 000000000..32594855e --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/matmul.json @@ -0,0 +1 @@ +{"schema":"OMEGA_GPU_MATMUL_API_SWEEP_V2","mutant":false,"cta_budget":64,"shapes":[{"m":16,"k":16,"n":16,"rc":"OK","pass":true,"chip_elapsed_ns":112049,"call_ms":27.567,"wall_ms":27.571,"max_abs_err":2.08616e-07,"max_rel_err":4.51833e-08,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":4.51833e-08,"e2e_bad":0,"grid_x":2,"grid_y":1,"chip_calls":1,"rows_per_call":16,"padded_m":16,"padded_k":16,"padded_n":16,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":32,"k":32,"n":32,"rc":"OK","pass":true,"chip_elapsed_ns":103377,"call_ms":0.280,"wall_ms":0.291,"max_abs_err":4.47035e-07,"max_rel_err":5.60462e-08,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":5.60462e-08,"e2e_bad":0,"grid_x":4,"grid_y":2,"chip_calls":1,"rows_per_call":32,"padded_m":32,"padded_k":32,"padded_n":32,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":64,"k":64,"n":64,"rc":"OK","pass":true,"chip_elapsed_ns":103873,"call_ms":0.675,"wall_ms":0.720,"max_abs_err":1.00583e-06,"max_rel_err":6.11591e-08,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":6.11591e-08,"e2e_bad":0,"grid_x":8,"grid_y":4,"chip_calls":1,"rows_per_call":64,"padded_m":64,"padded_k":64,"padded_n":64,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":128,"k":128,"n":128,"rc":"OK","pass":true,"chip_elapsed_ns":104353,"call_ms":2.383,"wall_ms":2.564,"max_abs_err":2.89083e-06,"max_rel_err":9.41787e-08,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":9.41787e-08,"e2e_bad":0,"grid_x":8,"grid_y":8,"chip_calls":1,"rows_per_call":128,"padded_m":128,"padded_k":128,"padded_n":128,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":256,"k":256,"n":256,"rc":"OK","pass":true,"chip_elapsed_ns":209250,"call_ms":16.064,"wall_ms":16.763,"max_abs_err":8.43406e-06,"max_rel_err":1.30669e-07,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":1.30669e-07,"e2e_bad":0,"grid_x":4,"grid_y":16,"chip_calls":1,"rows_per_call":256,"padded_m":256,"padded_k":256,"padded_n":256,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":512,"k":512,"n":512,"rc":"OK","pass":true,"chip_elapsed_ns":1402300,"call_ms":125.696,"wall_ms":128.631,"max_abs_err":2.39611e-05,"max_rel_err":1.82928e-07,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":1.82928e-07,"e2e_bad":0,"grid_x":2,"grid_y":32,"chip_calls":1,"rows_per_call":512,"padded_m":512,"padded_k":512,"padded_n":512,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":1,"k":1024,"n":1024,"rc":"OK","pass":true,"chip_elapsed_ns":210402,"call_ms":1.823,"wall_ms":7.601,"max_abs_err":3.67463e-05,"max_rel_err":1.48922e-07,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":1.48922e-07,"e2e_bad":0,"grid_x":64,"grid_y":1,"chip_calls":1,"rows_per_call":16,"padded_m":16,"padded_k":1024,"padded_n":1024,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":1,"k":256,"n":1024,"rc":"OK","pass":true,"chip_elapsed_ns":107777,"call_ms":0.629,"wall_ms":1.946,"max_abs_err":4.94719e-06,"max_rel_err":7.52664e-08,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":7.52664e-08,"e2e_bad":0,"grid_x":64,"grid_y":1,"chip_calls":1,"rows_per_call":16,"padded_m":16,"padded_k":256,"padded_n":1024,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":16,"k":1024,"n":256,"rc":"OK","pass":true,"chip_elapsed_ns":105249,"call_ms":4.205,"wall_ms":5.594,"max_abs_err":3.72902e-05,"max_rel_err":1.45806e-07,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":1.45806e-07,"e2e_bad":0,"grid_x":32,"grid_y":1,"chip_calls":1,"rows_per_call":16,"padded_m":16,"padded_k":1024,"padded_n":256,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":8,"k":1024,"n":8,"rc":"OK","pass":true,"chip_elapsed_ns":107361,"call_ms":0.419,"wall_ms":0.500,"max_abs_err":3.00165e-05,"max_rel_err":1.18908e-07,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":1.18908e-07,"e2e_bad":0,"grid_x":1,"grid_y":1,"chip_calls":1,"rows_per_call":16,"padded_m":16,"padded_k":1024,"padded_n":8,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":1024,"k":16,"n":128,"rc":"OK","pass":true,"chip_elapsed_ns":104049,"call_ms":2.628,"wall_ms":2.720,"max_abs_err":4.39584e-07,"max_rel_err":9.37062e-08,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":9.37062e-08,"e2e_bad":0,"grid_x":1,"grid_y":64,"chip_calls":1,"rows_per_call":1024,"padded_m":1024,"padded_k":16,"padded_n":128,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":192,"k":16,"n":192,"rc":"OK","pass":true,"chip_elapsed_ns":106737,"call_ms":0.970,"wall_ms":1.001,"max_abs_err":4.61936e-07,"max_rel_err":1.0078e-07,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":1.0078e-07,"e2e_bad":0,"grid_x":5,"grid_y":12,"chip_calls":1,"rows_per_call":192,"padded_m":192,"padded_k":16,"padded_n":192,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":512,"k":16,"n":64,"rc":"OK","pass":true,"chip_elapsed_ns":104721,"call_ms":0.868,"wall_ms":0.914,"max_abs_err":4.32134e-07,"max_rel_err":7.64946e-08,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":7.64946e-08,"e2e_bad":0,"grid_x":2,"grid_y":32,"chip_calls":1,"rows_per_call":512,"padded_m":512,"padded_k":16,"padded_n":64,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":256,"k":16,"n":256,"rc":"OK","pass":true,"chip_elapsed_ns":104593,"call_ms":1.451,"wall_ms":1.494,"max_abs_err":4.47035e-07,"max_rel_err":9.74134e-08,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":9.74134e-08,"e2e_bad":0,"grid_x":4,"grid_y":16,"chip_calls":1,"rows_per_call":256,"padded_m":256,"padded_k":16,"padded_n":256,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":128,"k":16,"n":512,"rc":"OK","pass":true,"chip_elapsed_ns":104705,"call_ms":1.454,"wall_ms":1.505,"max_abs_err":4.47035e-07,"max_rel_err":8.99603e-08,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":8.99603e-08,"e2e_bad":0,"grid_x":8,"grid_y":8,"chip_calls":1,"rows_per_call":128,"padded_m":128,"padded_k":16,"padded_n":512,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":1,"k":2048,"n":8192,"rc":"OK","pass":true,"chip_elapsed_ns":2246563,"call_ms":40.801,"wall_ms":132.726,"max_abs_err":0.000129104,"max_rel_err":2.62102e-07,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":2.62102e-07,"e2e_bad":0,"grid_x":64,"grid_y":1,"chip_calls":1,"rows_per_call":16,"padded_m":16,"padded_k":2048,"padded_n":8192,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":1,"k":8192,"n":2048,"rc":"OK","pass":true,"chip_elapsed_ns":2452357,"call_ms":70.110,"wall_ms":161.878,"max_abs_err":0.00123078,"max_rel_err":5.96705e-07,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":5.96705e-07,"e2e_bad":0,"grid_x":64,"grid_y":1,"chip_calls":1,"rows_per_call":16,"padded_m":16,"padded_k":8192,"padded_n":2048,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121},{"m":1,"k":2048,"n":128256,"rc":"OK","pass":true,"chip_elapsed_ns":37023550,"call_ms":434.017,"wall_ms":1869.956,"max_abs_err":0.000163585,"max_rel_err":3.1728e-07,"mismatch_count":0,"parity_verified":true,"completion_marker":1145324612,"repeat_rc":"OK","repeat_cache_hit":true,"repeat_mismatch":0,"e2e_max_rel":3.1728e-07,"e2e_bad":0,"grid_x":64,"grid_y":1,"chip_calls":1,"rows_per_call":16,"padded_m":16,"padded_k":2048,"padded_n":128256,"target_chip":"NVIDIA DGX Spark (Grace Blackwell GB10)","sm_architecture":121}],"blocked":false,"checks":18,"failed":0,"verdict":"OMEGA_GPU_MATMUL_API_PASS"} diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/matmul.log b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/matmul.log new file mode 100644 index 000000000..b2169f866 --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/matmul.log @@ -0,0 +1,19 @@ +PASS 16x16x16 rc=OK chip_ns=112049 call_ms=27.567 wall_ms=27.6 max_abs=2.08616e-07 max_rel=4.51833e-08 mismatches=0 e2e_rel=4.51833e-08 e2e_bad=0 grid=2x1 calls=1 rows_per_call=16 cache_hit2=1 err="" +PASS 32x32x32 rc=OK chip_ns=103377 call_ms=0.280 wall_ms=0.3 max_abs=4.47035e-07 max_rel=5.60462e-08 mismatches=0 e2e_rel=5.60462e-08 e2e_bad=0 grid=4x2 calls=1 rows_per_call=32 cache_hit2=1 err="" +PASS 64x64x64 rc=OK chip_ns=103873 call_ms=0.675 wall_ms=0.7 max_abs=1.00583e-06 max_rel=6.11591e-08 mismatches=0 e2e_rel=6.11591e-08 e2e_bad=0 grid=8x4 calls=1 rows_per_call=64 cache_hit2=1 err="" +PASS 128x128x128 rc=OK chip_ns=104353 call_ms=2.383 wall_ms=2.6 max_abs=2.89083e-06 max_rel=9.41787e-08 mismatches=0 e2e_rel=9.41787e-08 e2e_bad=0 grid=8x8 calls=1 rows_per_call=128 cache_hit2=1 err="" +PASS 256x256x256 rc=OK chip_ns=209250 call_ms=16.064 wall_ms=16.8 max_abs=8.43406e-06 max_rel=1.30669e-07 mismatches=0 e2e_rel=1.30669e-07 e2e_bad=0 grid=4x16 calls=1 rows_per_call=256 cache_hit2=1 err="" +PASS 512x512x512 rc=OK chip_ns=1402300 call_ms=125.696 wall_ms=128.6 max_abs=2.39611e-05 max_rel=1.82928e-07 mismatches=0 e2e_rel=1.82928e-07 e2e_bad=0 grid=2x32 calls=1 rows_per_call=512 cache_hit2=1 err="" +PASS 1x1024x1024 rc=OK chip_ns=210402 call_ms=1.823 wall_ms=7.6 max_abs=3.67463e-05 max_rel=1.48922e-07 mismatches=0 e2e_rel=1.48922e-07 e2e_bad=0 grid=64x1 calls=1 rows_per_call=16 cache_hit2=1 err="" +PASS 1x256x1024 rc=OK chip_ns=107777 call_ms=0.629 wall_ms=1.9 max_abs=4.94719e-06 max_rel=7.52664e-08 mismatches=0 e2e_rel=7.52664e-08 e2e_bad=0 grid=64x1 calls=1 rows_per_call=16 cache_hit2=1 err="" +PASS 16x1024x256 rc=OK chip_ns=105249 call_ms=4.205 wall_ms=5.6 max_abs=3.72902e-05 max_rel=1.45806e-07 mismatches=0 e2e_rel=1.45806e-07 e2e_bad=0 grid=32x1 calls=1 rows_per_call=16 cache_hit2=1 err="" +PASS 8x1024x8 rc=OK chip_ns=107361 call_ms=0.419 wall_ms=0.5 max_abs=3.00165e-05 max_rel=1.18908e-07 mismatches=0 e2e_rel=1.18908e-07 e2e_bad=0 grid=1x1 calls=1 rows_per_call=16 cache_hit2=1 err="" +PASS 1024x16x128 rc=OK chip_ns=104049 call_ms=2.628 wall_ms=2.7 max_abs=4.39584e-07 max_rel=9.37062e-08 mismatches=0 e2e_rel=9.37062e-08 e2e_bad=0 grid=1x64 calls=1 rows_per_call=1024 cache_hit2=1 err="" +PASS 192x16x192 rc=OK chip_ns=106737 call_ms=0.970 wall_ms=1.0 max_abs=4.61936e-07 max_rel=1.0078e-07 mismatches=0 e2e_rel=1.0078e-07 e2e_bad=0 grid=5x12 calls=1 rows_per_call=192 cache_hit2=1 err="" +PASS 512x16x64 rc=OK chip_ns=104721 call_ms=0.868 wall_ms=0.9 max_abs=4.32134e-07 max_rel=7.64946e-08 mismatches=0 e2e_rel=7.64946e-08 e2e_bad=0 grid=2x32 calls=1 rows_per_call=512 cache_hit2=1 err="" +PASS 256x16x256 rc=OK chip_ns=104593 call_ms=1.451 wall_ms=1.5 max_abs=4.47035e-07 max_rel=9.74134e-08 mismatches=0 e2e_rel=9.74134e-08 e2e_bad=0 grid=4x16 calls=1 rows_per_call=256 cache_hit2=1 err="" +PASS 128x16x512 rc=OK chip_ns=104705 call_ms=1.454 wall_ms=1.5 max_abs=4.47035e-07 max_rel=8.99603e-08 mismatches=0 e2e_rel=8.99603e-08 e2e_bad=0 grid=8x8 calls=1 rows_per_call=128 cache_hit2=1 err="" +PASS 1x2048x8192 rc=OK chip_ns=2246563 call_ms=40.801 wall_ms=132.7 max_abs=0.000129104 max_rel=2.62102e-07 mismatches=0 e2e_rel=2.62102e-07 e2e_bad=0 grid=64x1 calls=1 rows_per_call=16 cache_hit2=1 err="" +PASS 1x8192x2048 rc=OK chip_ns=2452357 call_ms=70.110 wall_ms=161.9 max_abs=0.00123078 max_rel=5.96705e-07 mismatches=0 e2e_rel=5.96705e-07 e2e_bad=0 grid=64x1 calls=1 rows_per_call=16 cache_hit2=1 err="" +PASS 1x2048x128256 rc=OK chip_ns=37023550 call_ms=434.017 wall_ms=1870.0 max_abs=0.000163585 max_rel=3.1728e-07 mismatches=0 e2e_rel=3.1728e-07 e2e_bad=0 grid=64x1 calls=1 rows_per_call=16 cache_hit2=1 err="" +PASS: 18 checks, 0 failed diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/matmul_timing.json b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/matmul_timing.json new file mode 100644 index 000000000..ddd95b70f --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/matmul_timing.json @@ -0,0 +1 @@ +{"schema":"OMEGA_GPU_MATMUL_API_TIMING_V1","cta_budget":64,"calls":100,"shapes":[{"m":1,"k":2048,"n":8192,"pass":true,"upload_ms":123.781,"ok_calls":100,"median_ms":2.0403,"p90_ms":2.1985,"min_ms":1.8164,"max_ms":2.9089,"chip_ns_mean":2062386,"grid_x":64,"grid_y":1,"e2e_first":2.36603e-07,"e2e_last":2.36603e-07,"e2e_bad_first":0,"e2e_bad_last":0,"repeat_mismatch":0},{"m":1,"k":8192,"n":2048,"pass":true,"upload_ms":96.367,"ok_calls":100,"median_ms":1.9350,"p90_ms":1.9398,"min_ms":1.7912,"max_ms":2.8510,"chip_ns_mean":1912867,"grid_x":64,"grid_y":1,"e2e_first":5.21488e-07,"e2e_last":5.21488e-07,"e2e_bad_first":0,"e2e_bad_last":0,"repeat_mismatch":0}],"blocked":false,"checks":4,"failed":0,"verdict":"OMEGA_GPU_MATMUL_TIMING_PASS"} diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/matmul_timing.log b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/matmul_timing.log new file mode 100644 index 000000000..c2df446d7 --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/matmul_timing.log @@ -0,0 +1,3 @@ +PASS timing 1x2048x8192 upload_ms=123.8 ok_calls=100 median_ms=2.040 p90_ms=2.198 min_ms=1.816 max_ms=2.909 chip_ns_mean=2062386 grid=64x1 e2e_first=2.36603e-07 e2e_last=2.36603e-07 repeat_mismatch=0 +PASS timing 1x8192x2048 upload_ms=96.4 ok_calls=100 median_ms=1.935 p90_ms=1.940 min_ms=1.791 max_ms=2.851 chip_ns_mean=1912867 grid=64x1 e2e_first=5.21488e-07 e2e_last=5.21488e-07 repeat_mismatch=0 +PASS: 4 checks, 0 failed diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/omega_gpu_attention_api.c b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/omega_gpu_attention_api.c new file mode 100644 index 000000000..64249473c --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/omega_gpu_attention_api.c @@ -0,0 +1,892 @@ +/* omega_gpu_attention_api.c: FB-1 cut 5 (2026-10-04). See the header. + * + * Layout: (1) kernel codegen on the BlackwellIR with the serialising scheduler + * of cut 4 (every instruction waits on everything still pending; slow and safe), + * extended with forward branches and more loops; (2) the launcher, cloned from + * cut 4's run_abc with a fourth buffer (block tables and context lengths), 16 + * argument words, and cut 1b's shader-cache invalidate; (3) the public entry + * points, which stage the referenced KV blocks per launch and apply the oracle's + * argument rules (dropped tokens past the table, negative table entries, zero + * context) on the host. + * + * One CTA per (query head, sequence), 64 threads (= head_dim). The context is + * walked in chunks of 64 tokens: + * phase A thread t owns token base+t: block lookup, K dot q (f32 FFMA), s = dot * scale + * (scale folds 1/sqrt(d) and log2 e), s and the token's byte offset go to shared; + * reduce chunk max (SHFL.DOWN tree per warp, lane partials to shared, BAR.SYNC, 2 LDS), + * m_new = max(m, chunk max), alpha = EX2(m - m_new), p = EX2(s - m_new) to shared, + * chunk sum the same way, l = l * alpha + chunk sum; + * phase B thread d owns dimension d: acc = acc * alpha + sum over the chunk of p_t * V[t][d]. + * Then out[d] = acc * RCP(l). The first chunk sees m = -inf, so alpha = EX2(-inf) = 0 and + * the initial l and acc drop out (PTX ISA ex2.approx: ex2(-Inf) = +0). + * + * Shared memory (1024 bytes, the QMD's declared size): [0,256) q, [256,512) s then p, + * [512,768) token byte offsets, [768,1024) warp partials for the reductions. + */ +#include "omega_gpu_attention_api.h" +#include "omega_blackwell_codegen.h" +#include "omega_bw_reconv.h" +#include "omega_blackwell_qmd.h" +#include "omega_blackwell_submit.h" +#include "omega_gpu_session.h" +#include "sha256.h" +#include +#include +#include +#include +#include + +/* ------------------------------------------------------------------ codegen */ + +#define CW(stall, wbar, rbar, wait) \ + (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) +#define CW_YIELD 0x2000u +#define SB_NONE 7u +#define SB_GPR 0u +#define SB_RD 1u +#define SB_UGPR 2u + +enum { K_FIXED, K_PRED, K_VAR, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC }; + +#define AT_MAX_LOOPS 8 +typedef struct { + BlackwellIRProgram *p; + uint32_t pending; + int err; + int loop_start[AT_MAX_LOOPS], loop_end[AT_MAX_LOOPS]; + int n_loops, depth, stack[AT_MAX_LOOPS]; + BwRegions rg; /* structured reconvergence regions (omega #308) */ + int rg_mutant; /* test only: 1 = drop the BSYNC at join (the 2026-10-04 bug shape; host sim must catch it) */ +} Em; + +static void emit(Em *e, BlackwellIRInsn in, int kind) { + uint32_t wait = e->pending; + switch (kind) { + case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; + case K_PRED: in.control = CW(13, SB_NONE, SB_NONE, wait); e->pending = 0; break; + case K_VAR: in.control = CW(4, SB_GPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_GPR; break; + case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; + case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; + case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; + case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; + case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; + /* stall counts as nvcc 13.0.88 writes them for sm_121 (see src/omega_gpu_elementwise_api.c) */ + case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; + case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; + default: e->err = -1; return; + } + if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; +} + +static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; } +static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; } +static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; } +static int here(const Em *e) { return (int)e->p->count; } + +#define NOREG -1 +static BlackwellIRInsn I0(BlackwellIROpcode op) { + BlackwellIRInsn in; memset(&in, 0, sizeof in); + in.op = op; in.dst_vreg = NOREG; in.src1_vreg = NOREG; in.src2_vreg = NOREG; in.src3_vreg = NOREG; in.ureg = NOREG; + return in; +} +static void ldcu64(Em *e, int udst, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDCU64); i.dst_vreg = udst; i.imm = off; i.is_uniform = true; emit(e, i, K_UVAR); } +static void ldc64(Em *e, int dst64, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC64); i.dst_vreg = dst64; i.imm = off; emit(e, i, K_VAR); } +static void ldc32(Em *e, int dst, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC); i.dst_vreg = dst; i.imm = off; emit(e, i, K_VAR); } +static void s2r(Em *e, int dst, uint32_t sr) { BlackwellIRInsn i = I0(BW_IR_S2R); i.dst_vreg = dst; i.imm = sr; emit(e, i, K_VAR); } +static void movi(Em *e, int dst, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_MOV_IMM); i.dst_vreg = dst; i.imm = imm; emit(e, i, K_FIXED); } +static void movf(Em *e, int dst, float f) { uint32_t u; memcpy(&u, &f, 4); movi(e, dst, u); } +static void movrz(Em *e, int dst) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = dst; emit(e, i, K_FIXED); } +/* dst = a * imm + c (c may be NOREG for RZ) */ +static void imadi(Em *e, int dst, int a, uint32_t imm, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = dst; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c; emit(e, i, K_FIXED); } +/* dst = a * b + c (registers; c may be NOREG) */ +static void imadr(Em *e, int dst, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = dst; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } +static void iadd3(Em *e, int dst, int a, int b) { BlackwellIRInsn i = I0(BW_IR_IADD3); i.dst_vreg = dst; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void lop3xor(Em *e, int dst, int a, int b) { BlackwellIRInsn i = I0(BW_IR_LOP3_XOR); i.dst_vreg = dst; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void lop3andi(Em *e, int dst, int a, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_LOP3_AND); i.dst_vreg = dst; i.src1_vreg = a; i.imm = imm; emit(e, i, K_FIXED); } +static void shri(Em *e, int dst, int a, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_SHF_R); i.dst_vreg = dst; i.src1_vreg = a; i.imm = imm; emit(e, i, K_FIXED); } +/* dst64 = idx * imm + base64 (byte address) */ +static void wide(Em *e, int dst64, int idx, uint32_t imm, int base64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = dst64; i.src1_vreg = idx; i.imm = imm; i.src3_vreg = base64; emit(e, i, K_FIXED); } +static void ldg(Em *e, int dst, int addr64, int udesc) { BlackwellIRInsn i = I0(BW_IR_LDG_E); i.dst_vreg = dst; i.src1_vreg = addr64; i.ureg = udesc; emit(e, i, K_VAR); } +static void ldg16(Em *e, int dst, int addr64, int udesc) { BlackwellIRInsn i = I0(BW_IR_LDG_E_U16); i.dst_vreg = dst; i.src1_vreg = addr64; i.ureg = udesc; emit(e, i, K_VAR); } +static void stg(Em *e, int addr64, int val, int udesc) { BlackwellIRInsn i = I0(BW_IR_STG_E); i.src1_vreg = addr64; i.src2_vreg = val; i.ureg = udesc; emit(e, i, K_STORE); } +static void e_fadd(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FADD); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void e_fsub(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FSUB); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void e_fmul(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FMUL); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void e_ffma(Em *e, int d, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_FFMA); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } +static void e_fmax(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FMNMX_MAX); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void mufu(Em *e, BlackwellIROpcode op, int d, int a) { BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = a; emit(e, i, K_VAR); } +/* SHFL and BAR.SYNC are refused inside an open reconvergence region (omega #308): on a split + * warp they read inactive lanes / count a partial warp, which is what produced wrong dims on + * the chip on 2026-10-04. The build fails closed instead. */ +static int in_region(Em *e) { if (e->rg.depth > 0) { e->err = -1; return 1; } return 0; } +static void shfl_down(Em *e, int d, int a, uint32_t off) { if (in_region(e)) return; BlackwellIRInsn i = I0(BW_IR_SHFL_DOWN); i.dst_vreg = d; i.src1_vreg = a; i.imm = off; emit(e, i, K_VAR); } +static void sts32(Em *e, int addr, int val) { BlackwellIRInsn i = I0(BW_IR_STS32); i.src1_vreg = addr; i.src2_vreg = val; emit(e, i, K_STORE); } +static void lds32(Em *e, int d, int addr) { BlackwellIRInsn i = I0(BW_IR_LDS32); i.dst_vreg = d; i.src1_vreg = addr; emit(e, i, K_VAR); } +static void bar_sync(Em *e) { if (in_region(e)) return; emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } +static void isetp_ge_u32(Em *e, int a, int b) { BlackwellIRInsn i = I0(BW_IR_ISETP_GE_U32); i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_PRED); } +static void bra_back_if_not_p0(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.predicate_not = true; i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } +/* @P0 BRA forward; the target is patched in when known */ +static int bra_fwd_if_p0(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = 0; emit(e, i, K_BRA); return at; } +static void patch_fwd(Em *e, int at) { if (at >= 0 && (size_t)at < e->p->count) e->p->insns[at].imm = (uint32_t)(here(e) - at); else e->err = -1; } + +/* ---- structured reconvergence regions (omega #308); same API as the elementwise emitter ---- */ +enum { RX_ALWAYS, RX_IF_P0, RX_IF_NOT_P0 }; +static int region_begin(Em *e) { + int bar = bw_regions_next_bar(&e->rg); + if (bar < 0) { e->err = -1; return -1; } + int at = here(e); + BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = (uint8_t)bar; i.imm = 2; emit(e, i, K_BSSY); + if (bw_regions_begin(&e->rg, at) < 0) e->err = -1; + return bar; +} +static void region_exit(Em *e, int cond) { + int at = here(e); + BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = 1; + if (cond != RX_ALWAYS) { i.predicate_p0 = true; i.predicate_not = (cond == RX_IF_NOT_P0); } + emit(e, i, K_BRA); + if (bw_regions_exit(&e->rg, at) < 0) e->err = -1; +} +static void region_join(Em *e) { + if (e->rg.depth <= 0) { e->err = -1; return; } + int d = e->rg.depth - 1, bsync = here(e); + BlackwellIRInsn i = I0(e->rg_mutant == 1 ? BW_IR_NOP : BW_IR_BSYNC); i.bar_reg = (uint8_t)d; emit(e, i, K_BSYNC); + if (e->rg_mutant == 1) { + BwRegions *r = &e->rg; r->depth--; + e->p->insns[r->bssy_at[d]].imm = (uint32_t)(bsync + 1 - r->bssy_at[d]); + for (int k = 0; k < r->n_exits[d]; k++) e->p->insns[r->exits[d][k]].imm = (uint32_t)(bsync - r->exits[d][k]); + return; + } + if (bw_regions_join(&e->rg, e->p, bsync) != d) e->err = -1; +} +static void tail(Em *e) { + emit(e, I0(BW_IR_EXIT), K_EXIT); + BlackwellIRInsn self = I0(BW_IR_BRA); + self.control = 0x000fc000u; + if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1; +} +/* Loops nest; loop_end() extends every value defined before the loop and read inside + * it to the back-branch (the linear-scan allocator knows nothing about back-edges). */ +static void loop_begin(Em *e) { + if (e->depth >= AT_MAX_LOOPS || e->n_loops >= AT_MAX_LOOPS) { e->err = -1; return; } + e->stack[e->depth++] = here(e); +} +static void loop_end(Em *e) { + if (e->depth == 0 || e->n_loops >= AT_MAX_LOOPS) { e->err = -1; return; } + int start = e->stack[--e->depth], end = here(e) - 1; + e->loop_start[e->n_loops] = start; e->loop_end[e->n_loops] = end; e->n_loops++; + OmegaRegAlloc *ra = &e->p->regalloc; + for (int v = 0; v < ra->num_vregs; v++) { + OmegaLiveInterval *iv = &ra->intervals[v]; + if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end; + } +} +/* A value first defined inside a loop may not be read after it before being redefined. */ +static int check_loop_invariant(const Em *e) { + const OmegaRegAlloc *ra = &e->p->regalloc; + for (int L = 0; L < e->n_loops; L++) + for (int v = 0; v < ra->num_vregs; v++) { + int fd = ra->intervals[v].first_def; + if (fd < e->loop_start[L] || fd > e->loop_end[L]) continue; + for (size_t i = (size_t)e->loop_end[L] + 1; i < e->p->count; i++) { + const BlackwellIRInsn *in = &e->p->insns[i]; + if (in->src1_vreg == v || in->src2_vreg == v || in->src3_vreg == v) return -1; + if (in->dst_vreg == v && !in->is_uniform) break; + } + } + return 0; +} + +/* Kernel argument words (constant bank 0 at 0x380; the bank is 1024 bytes, so 32 words fit). */ +#define ARG_Q 0x380u +#define ARG_POOL 0x388u +#define ARG_OUT 0x390u +#define ARG_TAB 0x398u +#define ARG_MAXB 0x3a0u /* words per block-table row */ +#define ARG_SCALE 0x3a4u /* bits of log2(e) / sqrt(head_dim) */ +#define ARG_BSTRIDE 0x3a8u /* bytes between staged blocks */ +#define ARG_TSTRIDE 0x3acu /* bytes between tokens */ +#define ARG_KVP 0x3b0u /* bytes from the K plane to the V plane */ +#define ARG_HSTRIDE 0x3b4u /* bytes between kv heads */ +#define ARG_NQH 0x3b8u /* query heads (q and out row count per sequence) */ +#define ARG_CTXOFF 0x3bcu /* byte offset of the context-length array inside the table buffer */ +#define ARG_WORDS 16u +#define DESC_OFF 0x358u + +#define HD OMEGA_GPU_ATTN_HEAD_DIM +#define SH_Q 0u +#define SH_S 256u +#define SH_O 512u +#define SH_R 768u +#define F32_NEG_INF 0xff800000u + +/* divergent (omega #308 regression R2): 0 = production branch-free chunk loop; 1 = the + * lane-dependent "j >= ctx" branch restored inside a reconvergence region (BSSY/BSYNC) so + * the SHFL reductions run on a joined warp; 2 = the same with the BSYNC dropped (the + * 2026-10-04 bug shape, a negative control the host simulator must catch). */ +typedef struct { bool f32; uint32_t log2bs, log2gqa; int mutant; int divergent; } KSpec; + +static void gen_attention(Em *e, const KSpec *k) { + const uint32_t elem = k->f32 ? 4u : 2u; + const uint32_t kstep = k->f32 ? 8u : 4u; /* bytes per inner iteration: two dims */ + int udesc = UV64(e), A = V64(e), B = V64(e), C = V64(e), D = V64(e); + int tid = V(e), h = V(e), seq = V(e); + int p_maxb = V(e), scale = V(e), p_bstride = V(e), p_tstride = V(e), p_kvp = V(e), p_hstride = V(e), p_nqh = V(e), p_ctxoff = V(e); + ldcu64(e, udesc, DESC_OFF); + ldc64(e, A, ARG_Q); ldc64(e, B, ARG_POOL); ldc64(e, C, ARG_OUT); ldc64(e, D, ARG_TAB); + ldc32(e, p_maxb, ARG_MAXB); ldc32(e, scale, ARG_SCALE); ldc32(e, p_bstride, ARG_BSTRIDE); ldc32(e, p_tstride, ARG_TSTRIDE); + ldc32(e, p_kvp, ARG_KVP); ldc32(e, p_hstride, ARG_HSTRIDE); ldc32(e, p_nqh, ARG_NQH); ldc32(e, p_ctxoff, ARG_CTXOFF); + s2r(e, tid, BW_SR_TID_X); s2r(e, h, BW_SR_CTAID_X); s2r(e, seq, BW_SR_CTAID_Y); + + int c1 = V(e), c4 = V(e), c32 = V(e), c64 = V(e), c256 = V(e), c512 = V(e), c768 = V(e), c896 = V(e), zero = V(e); + 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. 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, 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 */ + 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); + shri(e, kvh, h, k->log2gqa); + if (k->mutant == OMEGA_GPU_ATTN_MUTANT_KV_HEAD) lop3xor(e, kvh, kvh, c1); + imadr(e, kvoff, kvh, p_hstride, NOREG); + imadr(e, t1, seq, p_maxb, NOREG); + imadi(e, t1, t1, 4, NOREG); + wide(e, tab, t1, 1, D); + imadi(e, t1, seq, 4, p_ctxoff); + wide(e, ca, t1, 1, D); + ldg(e, ctx, ca, udesc); + /* V column base for dimension tid: pool + kv plane + tid * elem. The kv head offset is + * already inside the per-token offset phase A leaves in shared memory (found by the host + * simulator: adding it here too read V from the wrong head for every kv head but 0). */ + wide(e, vb, tid, elem, B); + wide(e, vb, p_kvp, 1, vb); + + int M = V(e), L = V(e), acc = V(e), base = V(e), sOff = V(e), oOff = V(e), rOff = V(e); + movi(e, M, F32_NEG_INF); + if (k->mutant == OMEGA_GPU_ATTN_MUTANT_NO_MAX) movrz(e, L); else movf(e, L, 1.0f); /* alpha = 0 on the first chunk clears it */ + movrz(e, acc); movrz(e, base); + iadd3(e, sOff, tid4, c256); iadd3(e, oOff, tid4, c512); iadd3(e, rOff, tid4, c768); + bar_sync(e); /* q visible to every thread */ + isetp_ge_u32(e, zero, ctx); /* ctx == 0: out = 0 */ + int bZ = bra_fwd_if_p0(e); + + /* ---------------- chunk loop ---------------- */ + int j = V(e), s = V(e), koff = V(e); + int bi = V(e), sl = V(e), ta = V64(e), blk = V(e), ka = V64(e), dot = V(e), d = V(e); + int x = V(e), lo = V(e), hi = V(e), qa = V(e), qb = V(e), q0 = V(e), q1 = V(e), ka2 = V64(e), j1 = V(e); + int dneg = V(e), over = V(e), ot = V(e), ctxm1 = V(e), cm1 = V(e), cninf = V(e), nb = V(e), ns = V(e); + movi(e, cm1, 0xFFFFFFFFu); iadd3(e, ctxm1, ctx, cm1); movi(e, cninf, F32_NEG_INF); + loop_begin(e); + int Lout = here(e); + /* No branch here: a warp split by "j >= ctx" stays split (the IR has no reconvergence + * instruction) and the SHFL reductions below then read inactive lanes, which is undefined + * (wrong dims tid >= tail on the chip, 2026-10-04). over = (j >= ctx) as 0/1 from the sign of + * ctx-1-j; out-of-range lanes read token `base` (always < ctx here) and their score + * is forced to -inf after the dot product with s = -max(-s, nb), nb = -inf or +inf. */ + iadd3(e, j, base, tid); + movi(e, s, F32_NEG_INF); + movrz(e, koff); + if (k->divergent) { + /* omega #308 R2: the lane-dependent branch, inside a reconvergence region. Lanes with + * j >= ctx skip phase A (s stays -inf, koff 0); the BSYNC at region_join rejoins them + * before the SHFL reductions below. divergent == 2 drops that BSYNC (negative control). */ + region_begin(e); + isetp_ge_u32(e, j, ctx); + region_exit(e, RX_IF_P0); + } else { + imadi(e, dneg, j, 0xFFFFFFFFu, ctxm1); /* ctx - 1 - j */ + shri(e, over, dneg, 31); + imadr(e, ot, over, tid, NOREG); + imadi(e, j, ot, 0xFFFFFFFFu, j); /* j - over*tid */ + } + /* phase A: token j */ + shri(e, bi, j, k->log2bs); + lop3andi(e, sl, j, (1u << k->log2bs) - 1u); + wide(e, ta, bi, 4, tab); + ldg(e, blk, ta, udesc); + imadr(e, koff, blk, p_bstride, NOREG); + if (k->mutant == OMEGA_GPU_ATTN_MUTANT_SLOT) { + /* K read from slot (t + 1) % block_size, V from the right slot: a K/V mismatch. + * (Shifting both only permutes matched pairs inside a full block, which softmax cannot see.) */ + int sl1 = V(e); + iadd3(e, j1, j, c1); lop3andi(e, sl1, j1, (1u << k->log2bs) - 1u); + imadr(e, sl1, sl1, p_tstride, koff); + iadd3(e, sl1, sl1, kvoff); + wide(e, ka, sl1, 1, B); + imadr(e, koff, sl, p_tstride, koff); + iadd3(e, koff, koff, kvoff); + } else { + imadr(e, koff, sl, p_tstride, koff); + iadd3(e, koff, koff, kvoff); + wide(e, ka, koff, 1, B); + } + movrz(e, dot); movrz(e, d); + loop_begin(e); + int Lin = here(e); + if (k->f32) { + ldg(e, lo, ka, udesc); + wide(e, ka2, c1, 4, ka); + ldg(e, hi, ka2, udesc); + } else { + ldg(e, x, ka, udesc); + imadi(e, lo, x, 0x10000u, NOREG); /* low bf16 -> f32: bits << 16 */ + lop3andi(e, hi, x, 0xffff0000u); /* high bf16 -> f32: keep the top half */ + } + imadi(e, qa, d, 8, NOREG); + lds32(e, q0, qa); + iadd3(e, qb, qa, c4); + lds32(e, q1, qb); + e_ffma(e, dot, q0, lo, dot); + e_ffma(e, dot, q1, hi, dot); + wide(e, ka, c1, kstep, ka); + iadd3(e, d, d, c1); + isetp_ge_u32(e, d, c32); + bra_back_if_not_p0(e, Lin); + loop_end(e); + e_fmul(e, s, dot, scale); + if (k->divergent) { + region_join(e); /* every lane back before the cross-lane reductions */ + } else { + imadi(e, nb, over, 0x80000000u, cninf); /* over ? +inf : -inf (bits) */ + e_fsub(e, ns, zero, s); e_fmax(e, ns, ns, nb); e_fsub(e, s, zero, ns); + } + sts32(e, sOff, s); + sts32(e, oOff, koff); + + /* chunk max */ + int t = V(e), mw = V(e), ra = V(e), rb = V(e), mc = V(e), Mn = V(e), diff = V(e), alpha = V(e), parg = V(e), p = V(e), sw = V(e), lc = V(e); + static const uint32_t steps[5] = { 16, 8, 4, 2, 1 }; + shfl_down(e, t, s, steps[0]); e_fmax(e, mw, s, t); + for (int i = 1; i < 5; i++) { shfl_down(e, t, mw, steps[i]); e_fmax(e, mw, mw, t); } + sts32(e, rOff, mw); + bar_sync(e); + lds32(e, ra, c768); lds32(e, rb, c896); + e_fmax(e, mc, ra, rb); + e_fmax(e, Mn, M, mc); + if (k->mutant == OMEGA_GPU_ATTN_MUTANT_NO_MAX) { + movf(e, alpha, 1.0f); + mufu(e, BW_IR_MUFU_EX2, p, s); + e_fmax(e, M, Mn, mc); + } else { + e_fsub(e, diff, M, Mn); + mufu(e, BW_IR_MUFU_EX2, alpha, diff); + e_fmax(e, M, Mn, mc); /* M = Mn */ + e_fsub(e, parg, s, Mn); + mufu(e, BW_IR_MUFU_EX2, p, parg); + } + bar_sync(e); /* everyone has read the max partials before they are overwritten */ + sts32(e, sOff, p); + shfl_down(e, t, p, steps[0]); e_fadd(e, sw, p, t); + for (int i = 1; i < 5; i++) { shfl_down(e, t, sw, steps[i]); e_fadd(e, sw, sw, t); } + sts32(e, rOff, sw); + bar_sync(e); + lds32(e, ra, c768); lds32(e, rb, c896); + e_fadd(e, lc, ra, rb); + e_ffma(e, L, L, alpha, lc); + if (k->mutant != OMEGA_GPU_ATTN_MUTANT_NO_RESCALE) e_fmul(e, acc, acc, alpha); + + /* phase B: dimension tid over the chunk's tokens */ + int kk = V(e), tt = V(e), oa = V(e), ko = V(e), pa = V(e), pv = V(e), va = V64(e), v = V(e); + movrz(e, kk); + loop_begin(e); + int LB = here(e); + iadd3(e, tt, base, kk); + isetp_ge_u32(e, tt, ctx); + int bB = bra_fwd_if_p0(e); + imadi(e, oa, kk, 4, c512); + lds32(e, ko, oa); + imadi(e, pa, kk, 4, c256); + lds32(e, pv, pa); + wide(e, va, ko, 1, vb); + if (k->f32) ldg(e, v, va, udesc); + else { ldg16(e, v, va, udesc); imadi(e, v, v, 0x10000u, NOREG); } + e_ffma(e, acc, pv, v, acc); + iadd3(e, kk, kk, c1); + isetp_ge_u32(e, kk, c64); + bra_back_if_not_p0(e, LB); + loop_end(e); + patch_fwd(e, bB); + bar_sync(e); /* phase B reads of s/p and offsets finish before the next chunk writes them */ + iadd3(e, base, base, c64); + isetp_ge_u32(e, base, ctx); + bra_back_if_not_p0(e, Lout); + loop_end(e); + + patch_fwd(e, bZ); + int inv = V(e), o = V(e); + mufu(e, BW_IR_MUFU_RCP, inv, L); + e_fmul(e, o, acc, inv); + stg(e, oaddr, o, udesc); + tail(e); +} + +#define AT_GPR_BUDGET 64u + +static int build_kernel(const KSpec *k, OmegaBlackwellKernel *kernel, BlackwellIRProgram **keep) { + BlackwellIRProgram *prog = calloc(1, sizeof *prog); + if (!prog) return OMEGA_GPU_ATTN_CODEGEN_FAIL; + omega_bw_ir_init(prog); + Em e; memset(&e, 0, sizeof e); e.p = prog; + e.rg_mutant = (k->divergent == 2) ? 1 : 0; + gen_attention(&e, k); + if (e.depth != 0) e.err = -1; + if (e.rg.depth != 0) e.err = -1; /* a region left open has no join: refuse the build */ + int rc = OMEGA_GPU_ATTN_CODEGEN_FAIL; + int ra_rc = (e.err == 0) ? omega_bw_regalloc_solve(prog) : -1; + int loop_rc = (ra_rc == 0) ? check_loop_invariant(&e) : -1; +#ifdef OMEGA_ATTN_DEBUG + fprintf(stderr, "attn codegen f32=%d log2bs=%u log2gqa=%u mutant=%d emit_err=%d insns=%zu vregs=%d regalloc=%d loops=%d peak_gpr=%u\n", + (int)k->f32, k->log2bs, k->log2gqa, k->mutant, e.err, prog->count, prog->regalloc.num_vregs, ra_rc, loop_rc, prog->regalloc.peak_gpr_usage); +#endif + if (e.err == 0 && ra_rc == 0 && loop_rc == 0 && prog->regalloc.peak_gpr_usage <= AT_GPR_BUDGET) { + size_t max_bytes = (size_t)BW_MAX_IR_INSNS * 16; + memset(kernel, 0, sizeof *kernel); + kernel->code = malloc(max_bytes); + size_t emitted = 0; + if (kernel->code && omega_bw_encode_program(prog, kernel->code, max_bytes, &emitted) == 0) { + kernel->code_size = emitted; + kernel->insn_count = emitted / 16; + kernel->gpr_count = prog->regalloc.peak_gpr_usage; + kernel->uniform_gpr_count = prog->regalloc.peak_ugpr_usage; + sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); + rc = OMEGA_GPU_ATTN_OK; + } else { + free(kernel->code); kernel->code = NULL; + } + } + if (keep && rc == OMEGA_GPU_ATTN_OK) *keep = prog; else free(prog); + return rc; +} + +/* --------------------------------------------------------- kernel cache */ + +/* 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 int g_divergent; /* omega #308 R2 test hook: see KSpec.divergent */ +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]; +static OmegaGpuScratch g_q, g_pool, g_tab, g_out; /* staging, grown to the high-water mark, reused per call */ +static bool g_hooked; +#define LOCK() omega_gpu_session_lock() +#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++) + if (g_cache[i].used) { free(g_cache[i].kernel.code); free(g_cache[i].prog); omega_gpu_session_free(&g_cache[i].code); memset(&g_cache[i], 0, sizeof g_cache[i]); } +} + +void omega_gpu_attention_cache_clear(void) { + LOCK(); + cache_clear_locked(); + UNLOCK(); +} + +/* Called by the session (lock held, device still open) right before it closes. */ +static void on_session_close(void) { + cache_clear_locked(); + omega_gpu_session_scratch_free(&g_q); omega_gpu_session_scratch_free(&g_pool); + omega_gpu_session_scratch_free(&g_tab); omega_gpu_session_scratch_free(&g_out); +} + +void omega_gpu_attention_test_set_mutant(OmegaGpuAttnMutant m) { + LOCK(); + g_mutant = (int)m; + cache_clear_locked(); + UNLOCK(); +} + +/* Caller holds the lock. Builds the kernel if needed; uploads the device copy when + * `upload` (the chip path; the simulator never needs one). */ +static int kernel_for(bool f32, uint32_t log2bs, uint32_t log2gqa, bool upload, Slot **out, bool *hit) { + KSpec k = { .f32 = f32, .log2bs = log2bs, .log2gqa = log2gqa, .mutant = g_mutant, .divergent = g_divergent }; + Slot *s = NULL; + for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) + if (g_cache[i].used && g_cache[i].k.f32 == k.f32 && g_cache[i].k.log2bs == k.log2bs && g_cache[i].k.log2gqa == k.log2gqa && g_cache[i].k.mutant == k.mutant && g_cache[i].k.divergent == k.divergent) { /* field compare: memcmp would read struct padding */ + s = &g_cache[i]; *hit = true; break; + } + if (!s) { + OmegaBlackwellKernel kern; BlackwellIRProgram *kp = NULL; + int rc = build_kernel(&k, &kern, &kp); + if (rc != OMEGA_GPU_ATTN_OK) return rc; + size_t slot = 0; + for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) if (!g_cache[i].used) { slot = i; break; } + s = &g_cache[slot]; + if (s->used) { free(s->kernel.code); free(s->prog); omega_gpu_session_free(&s->code); } /* full: evict slot 0 */ + memset(s, 0, sizeof *s); + s->used = 1; s->k = k; s->kernel = kern; s->prog = kp; + *hit = false; + } + if (upload && !s->code.cpu) { + if (omega_gpu_session_alloc(s->kernel.code_size, &s->code) != 0) return OMEGA_GPU_ATTN_CHIP_FAIL; + memcpy(s->code.cpu, s->kernel.code, s->kernel.code_size); + __asm__ volatile("dsb sy" ::: "memory"); + } + *out = s; + return OMEGA_GPU_ATTN_OK; +} + +int omega_gpu_attention_codegen(bool kv_f32, uint32_t log2_block_size, uint32_t log2_gqa, OmegaBlackwellKernel *kernel) { + if (!kernel || log2_block_size > 16 || log2_gqa > 6) return OMEGA_GPU_ATTN_BAD_ARGS; + LOCK(); + KSpec k = { .f32 = kv_f32, .log2bs = log2_block_size, .log2gqa = log2_gqa, .mutant = g_mutant, .divergent = g_divergent }; + UNLOCK(); + return build_kernel(&k, kernel, NULL); +} + +const char *omega_gpu_attention_last_error(void) { return omega_gpu_session_last_error(); } + +/* ------------------------------------------------------------ launcher */ + +#define AT_POISON 0xffbadbadu +#define AT_WAIT_MS 600000ull + +static uint64_t now_ns(void) { + struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); + return (uint64_t)t.tv_sec * 1000000000ULL + (uint64_t)t.tv_nsec; +} + +typedef OmegaGpuAttnLaunch Launch; + +/* Caller holds the lock. Chip path: open the shared device on first use. */ +static int dev_open_locked(void) { + if (!g_hooked) { (void)omega_gpu_session_on_close(on_session_close); g_hooked = true; } + return omega_gpu_session_open() ? OMEGA_GPU_ATTN_OK : OMEGA_GPU_ATTN_CHIP_FAIL; +} + +/* Where a call stages the KV slice it is about to hand the kernel: straight into the + * device staging buffer on the chip path (one copy), a host buffer under the simulator. + * Caller holds the lock; the result is valid until the next staging call. */ +static void *stage_target(size_t bytes, void **host_tmp) { + *host_tmp = NULL; + if (g_sim) { *host_tmp = malloc(bytes ? bytes : 1); return *host_tmp; } + if (omega_gpu_session_scratch(&g_pool, bytes) != 0) return NULL; + return g_pool.mem.cpu; +} + +/* One launch through the shared session (omega_gpu_session_launch: shader-cache + * invalidate, constant bank, QMD0/QMD1, first marker, L2_FLUSH_DIRTY, second marker), + * then readback and the poison scan. L->pool may already be the pool staging buffer. */ +static int run_launch(const Slot *ks, const Launch *L, OmegaGpuAttnInfo *info) { + if (omega_gpu_session_scratch(&g_q, L->q_bytes) || omega_gpu_session_scratch(&g_tab, L->tab_bytes) || + omega_gpu_session_scratch(&g_out, L->out_bytes) || (L->pool != g_pool.mem.cpu && omega_gpu_session_scratch(&g_pool, L->pool_bytes))) + return OMEGA_GPU_ATTN_CHIP_FAIL; + memcpy(g_q.mem.cpu, L->q, L->q_bytes); + if (L->pool != g_pool.mem.cpu) memcpy(g_pool.mem.cpu, L->pool, L->pool_bytes); + memcpy(g_tab.mem.cpu, L->tab, L->tab_bytes); + size_t out_words = L->out_bytes / 4; + uint32_t *out_dev = (uint32_t *)g_out.mem.cpu; + for (size_t i = 0; i < out_words; i++) out_dev[i] = AT_POISON; + __asm__ volatile("dsb sy" ::: "memory"); + + uint32_t args[ARG_WORDS]; + args[0] = (uint32_t)g_q.mem.va; args[1] = (uint32_t)(g_q.mem.va >> 32); + args[2] = (uint32_t)g_pool.mem.va; args[3] = (uint32_t)(g_pool.mem.va >> 32); + args[4] = (uint32_t)g_out.mem.va; args[5] = (uint32_t)(g_out.mem.va >> 32); + args[6] = (uint32_t)g_tab.mem.va; args[7] = (uint32_t)(g_tab.mem.va >> 32); + memcpy(&args[8], L->params, sizeof L->params); + OmegaGpuLaunch GL = { + .code_va = ks->code.va, .gpr_count = AT_GPR_BUDGET, .threads_x = HD, .threads_y = 1, + .grid_x = L->grid_x, .grid_y = L->grid_y, .num_elements = out_words, + .args = args, .n_args = ARG_WORDS, .timeout_ms = AT_WAIT_MS, + }; + uint64_t elapsed = 0; uint32_t marker = 0; + if (omega_gpu_session_launch(&GL, &elapsed, &marker) != 0) return OMEGA_GPU_ATTN_CHIP_FAIL; + + memcpy(L->out, g_out.mem.cpu, L->out_bytes); + uint32_t unwritten = 0; + for (size_t i = 0; i < out_words; i++) if (((uint32_t *)L->out)[i] == AT_POISON) unwritten++; + if (info) { + info->chip_calls++; + info->elapsed_ns += elapsed; + info->completion_marker = marker; + info->unwritten_words += unwritten; + info->threads_per_cta = HD; + info->ctas_last_launch = L->grid_x * L->grid_y; + } + return unwritten ? OMEGA_GPU_ATTN_UNWRITTEN : OMEGA_GPU_ATTN_OK; +} + +/* 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); +} + +/* ------------------------------------------------------- public entries */ + +static void info_begin(OmegaGpuAttnInfo *info, const OmegaBlackwellKernel *k, bool hit) { + if (!info) return; + memset(info, 0, sizeof *info); + info->kernel_cache_hit = hit; + info->gpr_count = k->gpr_count; + info->insn_count = (uint32_t)k->insn_count; + snprintf(info->target_chip, sizeof info->target_chip, "NVIDIA DGX Spark (Grace Blackwell GB10)"); + info->sm_architecture = 121; +} +static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } +static int log2_exact(uint32_t v, uint32_t *out) { + if (v == 0 || (v & (v - 1))) return -1; + uint32_t l = 0; while ((1u << l) != v) l++; + *out = l; return 0; +} +static float score_scale(void) { return (float)(1.4426950408889634 / sqrt((double)HD)); } + +/* Common head-shape checks; returns log2 of the gqa ratio. */ +static int check_heads(uint32_t num_q_heads, uint32_t num_kv_heads, uint32_t head_dim, uint32_t *log2gqa) { + if (num_q_heads == 0 || num_kv_heads == 0 || head_dim == 0) return OMEGA_GPU_ATTN_BAD_ARGS; + if (head_dim != HD || num_q_heads > OMEGA_GPU_ATTN_MAX_Q_HEADS) return OMEGA_GPU_ATTN_TOO_LARGE; + if (num_q_heads % num_kv_heads != 0 || log2_exact(num_q_heads / num_kv_heads, log2gqa) != 0) return OMEGA_GPU_ATTN_BAD_ARGS; + return OMEGA_GPU_ATTN_OK; +} + +/* Common prologue of every chip entry (lock held on success): device, kernel, info. */ +static int begin(bool f32, uint32_t log2bs, uint32_t log2gqa, Slot **ks, OmegaGpuAttnInfo *info) { + LOCK(); + int rc = g_sim ? OMEGA_GPU_ATTN_OK : dev_open_locked(); + bool hit = false; + if (rc == OMEGA_GPU_ATTN_OK) rc = kernel_for(f32, log2bs, log2gqa, !g_sim, ks, &hit); + if (rc != OMEGA_GPU_ATTN_OK) { UNLOCK(); return rc; } + info_begin(info, &(*ks)->kernel, hit); + return OMEGA_GPU_ATTN_OK; +} + +int omega_gpu_gqa_attention_f32(const float *q, const float *k_cache, const float *v_cache, + uint32_t seq_len, uint32_t num_q_heads, uint32_t num_kv_heads, + uint32_t head_dim, float *out, OmegaGpuAttnInfo *info) { + uint64_t t0 = now_ns(); + if (!q || !k_cache || !v_cache || !out) return OMEGA_GPU_ATTN_BAD_ARGS; + uint32_t log2gqa; + int rc = check_heads(num_q_heads, num_kv_heads, head_dim, &log2gqa); + if (rc != OMEGA_GPU_ATTN_OK) return rc; + if (seq_len > OMEGA_GPU_ATTN_MAX_GQA_CTX) return OMEGA_GPU_ATTN_TOO_LARGE; + size_t n_out = (size_t)num_q_heads * HD; + if (seq_len == 0) { memset(out, 0, n_out * 4); if (info) { memset(info, 0, sizeof *info); info->call_ns = now_ns() - t0; } return OMEGA_GPU_ATTN_OK; } + Slot *ks = NULL; + const uint32_t log2bs = 12; /* one block of 4096 slots */ + rc = begin(true, log2bs, log2gqa, &ks, info); + if (rc != OMEGA_GPU_ATTN_OK) return rc; + size_t plane = (size_t)seq_len * num_kv_heads * HD * 4; + void *host_tmp = NULL; + uint8_t *pool = stage_target(2 * plane, &host_tmp); + 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, + .out = out, .out_bytes = n_out * 4, .grid_x = num_q_heads, .grid_y = 1, + .params = { 1, f2u(score_scale()), (uint32_t)(2 * plane), num_kv_heads * HD * 4, (uint32_t)plane, HD * 4, num_q_heads, 4 } + }; + rc = launch(ks, &L, info); + free(host_tmp); + UNLOCK(); + if (info) info->call_ns = now_ns() - t0; + return rc; +} + +/* Paged path shared by the single and batched entries. tables[s] has n_blocks[s] entries + * (already filtered of negatives), ctx[s] the context length. */ +static int paged_core(const float *q, const uint8_t *pool, const OmegaGpuKvLayout *ly, + const uint32_t *const *tables, const uint32_t *n_blocks, const uint32_t *ctx, + uint32_t num_seqs, uint32_t layer_idx, uint32_t num_q_heads, uint32_t num_kv_heads, + uint32_t head_dim, float *out, OmegaGpuAttnInfo *info) { + uint64_t t0 = now_ns(); + uint32_t log2gqa, log2bs; + int rc = check_heads(num_q_heads, num_kv_heads, head_dim, &log2gqa); + if (rc != OMEGA_GPU_ATTN_OK) return rc; + if (!ly || layer_idx >= ly->num_layers || log2_exact(ly->block_size, &log2bs) != 0) return OMEGA_GPU_ATTN_BAD_ARGS; + /* the oracle addresses heads as kv_head * head_dim * 2 inside a token: the layout must agree */ + if (ly->head_stride_bytes != HD * 2 || ly->token_stride_bytes != (uint64_t)num_kv_heads * HD * 2 || + ly->kv_plane_stride_bytes != (uint64_t)ly->block_size * ly->token_stride_bytes || + ly->layer_stride_bytes != 2 * ly->kv_plane_stride_bytes || ly->block_stride_bytes < (uint64_t)ly->num_layers * ly->layer_stride_bytes) + return OMEGA_GPU_ATTN_BAD_ARGS; + size_t row = (size_t)num_q_heads * HD; + /* effective context per sequence and block id range check */ + uint32_t *eff = calloc(num_seqs ? num_seqs : 1, sizeof *eff); + if (!eff) return OMEGA_GPU_ATTN_CHIP_FAIL; + for (uint32_t s = 0; s < num_seqs; s++) { + uint64_t cap = (uint64_t)n_blocks[s] * ly->block_size; + eff[s] = (uint32_t)(ctx[s] < cap ? ctx[s] : cap); + for (uint32_t b = 0; b < n_blocks[s]; b++) { + uint64_t id = tables[s][b]; + if (id >= ly->num_blocks || id * ly->block_stride_bytes + (uint64_t)(layer_idx + 1) * ly->layer_stride_bytes > ly->pool_bytes) { free(eff); return OMEGA_GPU_ATTN_BAD_ARGS; } + } + } + Slot *ks = NULL; + rc = begin(false, log2bs, log2gqa, &ks, info); + if (rc != OMEGA_GPU_ATTN_OK) { free(eff); return rc; } + uint32_t seqs_per_launch = OMEGA_GPU_ATTN_MAX_CTAS / num_q_heads; + 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; + /* 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; + uint32_t *tab = calloc((size_t)ns * maxb + ns, sizeof *tab); + 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++) { + 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, + .grid_x = num_q_heads, .grid_y = ns, + .params = { maxb, f2u(score_scale()), (uint32_t)ly->layer_stride_bytes, (uint32_t)ly->token_stride_bytes, + (uint32_t)ly->kv_plane_stride_bytes, (uint32_t)ly->head_stride_bytes, num_q_heads, ns * maxb * 4 } + }; + rc = launch(ks, &L, info); + free(host_tmp); free(tab); + } + UNLOCK(); + free(eff); free(slot_of); free(touched); + if (info) info->call_ns = now_ns() - t0; + return rc; +} + +int omega_gpu_paged_attention_bf16(const float *q, const void *pool, const OmegaGpuKvLayout *layout, + const uint32_t *block_ids, uint32_t num_block_ids, + uint32_t context_len, uint32_t layer_idx, + uint32_t num_q_heads, uint32_t num_kv_heads, uint32_t head_dim, + float *out, OmegaGpuAttnInfo *info) { + if (!q || !pool || !layout || !out || (num_block_ids && !block_ids)) return OMEGA_GPU_ATTN_BAD_ARGS; + const uint32_t *tables[1] = { block_ids }; + uint32_t nb[1] = { num_block_ids }, cx[1] = { context_len }; + return paged_core(q, pool, layout, tables, nb, cx, 1, layer_idx, num_q_heads, num_kv_heads, head_dim, out, info); +} + +int omega_gpu_paged_attention_batch_bf16(const float *q, const void *pool, const OmegaGpuKvLayout *layout, + const int32_t *block_tables, const int32_t *context_lens, + uint32_t max_blocks_per_seq, uint32_t num_seqs, uint32_t layer_idx, + uint32_t num_q_heads, uint32_t num_kv_heads, uint32_t head_dim, + float *out, OmegaGpuAttnInfo *info) { + if (!q || !pool || !layout || !out || !block_tables || !context_lens || num_seqs == 0) return OMEGA_GPU_ATTN_BAD_ARGS; + /* the oracle's rules: ctx <= 0 -> 0; a table row past the array -> empty; negatives removed */ + uint32_t **tables = calloc(num_seqs, sizeof *tables); + uint32_t *nb = calloc(num_seqs, sizeof *nb), *cx = calloc(num_seqs, sizeof *cx); + if (!tables || !nb || !cx) { free(tables); free(nb); free(cx); return OMEGA_GPU_ATTN_CHIP_FAIL; } + int rc = OMEGA_GPU_ATTN_OK; + for (uint32_t s = 0; s < num_seqs; s++) { + cx[s] = context_lens[s] > 0 ? (uint32_t)context_lens[s] : 0; + tables[s] = calloc(max_blocks_per_seq ? max_blocks_per_seq : 1, sizeof **tables); + if (!tables[s]) { rc = OMEGA_GPU_ATTN_CHIP_FAIL; break; } + for (uint32_t b = 0; b < max_blocks_per_seq; b++) { + int32_t v = block_tables[(size_t)s * max_blocks_per_seq + b]; + if (v >= 0) tables[s][nb[s]++] = (uint32_t)v; + } + } + if (rc == OMEGA_GPU_ATTN_OK) + rc = paged_core(q, pool, layout, (const uint32_t *const *)tables, nb, cx, num_seqs, layer_idx, num_q_heads, num_kv_heads, head_dim, out, info); + for (uint32_t s = 0; s < num_seqs; s++) free(tables[s]); + free(tables); free(nb); free(cx); + 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"; + case OMEGA_GPU_ATTN_BAD_ARGS: return "BAD_ARGS"; + case OMEGA_GPU_ATTN_TOO_LARGE: return "TOO_LARGE"; + case OMEGA_GPU_ATTN_CODEGEN_FAIL: return "CODEGEN_FAIL"; + case OMEGA_GPU_ATTN_CHIP_FAIL: return "CHIP_FAIL"; + case OMEGA_GPU_ATTN_UNWRITTEN: return "UNWRITTEN"; + default: return "UNKNOWN"; + } +} + +/* omega #308 R2 test hook: 0 production, 1 lane-dependent branch inside a reconvergence + * region, 2 the same with the BSYNC dropped (negative control). Clears the kernel cache. */ +void omega_gpu_attention_test_set_divergent(int mode) { + LOCK(); + g_divergent = (mode < 0 || mode > 2) ? 0 : mode; + cache_clear_locked(); + UNLOCK(); +} diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/omega_gpu_attention_api.h b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/omega_gpu_attention_api.h new file mode 100644 index 000000000..f4931c2a6 --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/omega_gpu_attention_api.h @@ -0,0 +1,205 @@ +#ifndef OMEGA_GPU_ATTENTION_API_H +#define OMEGA_GPU_ATTENTION_API_H +/* + * omega_gpu_attention_api: native (no CUDA) decode attention for the AIEN + * inference stack (FB-1 cut 5, 2026-10-04). + * + * Two kernel families, one query per (sequence, query head), generated by our + * own codegen (src/omega_blackwell_codegen.c IR) and launched through the same + * chip-proven sequence as cuts 1b and 4: every buffer GPU-uncached (nvos.h + * NVOS32_ATTR2_GPU_CACHEABLE: system memory cached by the GPU is not coherent + * with CPU mappings), shader caches invalidated before the launch (clcec0.h + * NVCEC0_INVALIDATE_SHADER_CACHES, cut 1b finding), first marker, + * L2_FLUSH_DIRTY, second marker (C3 tail), poison-filled output scanned for + * unwritten words after readback. + * + * The math follows aien-sovereign-core crates/aien-inference-abi/src/backend.rs + * (ReferenceCpuBackend / TensorBackend defaults), read 2026-10-04 at main 8b5caed: + * gqa_attention(out, q, k_cache, v_cache, seq_len, num_q_heads, num_kv_heads, head_dim) + * k_cache / v_cache are f32 [seq_len][num_kv_heads][head_dim]; query head h reads + * kv head h / (num_q_heads / num_kv_heads); score = (q . k) / sqrt(head_dim); + * softmax over all seq_len tokens (no mask: one query attends to the whole history); + * out[h] = sum_t softmax_t v_t. seq_len == 0 writes zeros. + * paged_attention(out, q, pool, block_ids, context_len, layer_idx, ...) + * the KV pool is bf16 in aien-kv-cache's canonical layout (KvLayout strides in + * bytes: block, layer, kv plane (0 = K, 1 = V), token, head); token t lives in + * block_ids[t / block_size] at slot t % block_size; tokens whose block index is + * past the end of block_ids are dropped; context_len == 0 or no blocks writes zeros. + * paged_attention_batch: per sequence s, context_lens[s] <= 0 means 0, the block + * table row is block_tables[s*max_blocks .. (s+1)*max_blocks] (empty if that row + * runs past the array), negative entries are removed (the rest close up). + * + * Chip math: scores in f32 (FFMA over the head), online softmax per chunk of + * head_dim tokens: running max (FMNMX), p = EX2((s - m) * log2 e) folded into the + * score scale, rescale factor alpha = EX2(m_old - m_new), f32 sums, final MUFU.RCP. + * bf16 K is read as 32-bit pairs (LDG.E) and widened by a 16-bit shift (bf16 is + * the top half of an f32); bf16 V by LDG.E.U16 and the same shift. f32 K/V are + * read directly. Tolerances are stated and justified in tests/gpu_attention_test.c. + * + * Launch envelope: at most OMEGA_GPU_ATTN_MAX_CTAS CTAs per launch (investigation + * I42, same as cuts 1b and 4): grid = (num_q_heads, sequences per launch). + * Shared memory: the QMD declares 1024 bytes (clcec0qmd.h SHARED_MEMORY_SIZE_SHIFTED7, + * src/omega_blackwell_qmd.c word 36); the kernel uses exactly 4 x 256 bytes for + * head_dim 64, so head_dim is fixed at 64 in this cut (TinyLlama and Llama-3.2-1B). + * + * 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. 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 + * open per process instead of one per call (~40 ms), kernels resident after first + * use, staging buffers reused at their high-water mark (the KV slice is written + * straight into the device staging buffer). Calls serialise on one lock. + */ +#include +#include +#include +#include "omega_blackwell_matmul.h" /* OmegaBlackwellKernel */ +#ifdef __cplusplus +extern "C" { +#endif + +enum { + OMEGA_GPU_ATTN_OK = 0, + OMEGA_GPU_ATTN_BAD_ARGS = -1, /* NULL pointer, zero dimension, head mapping or block id out of range */ + OMEGA_GPU_ATTN_TOO_LARGE = -2, /* head_dim != 64, num_q_heads > 64, context past the envelope */ + OMEGA_GPU_ATTN_CODEGEN_FAIL = -3, /* our codegen refused or exceeded the register budget */ + OMEGA_GPU_ATTN_CHIP_FAIL = -4, /* device open, alloc, submit, marker or release failed */ + OMEGA_GPU_ATTN_UNWRITTEN = -5 /* output words still equal to their poison after the run */ +}; + +#define OMEGA_GPU_ATTN_HEAD_DIM 64u /* threads per CTA = head_dim (one thread per dimension / per token of a chunk) */ +#define OMEGA_GPU_ATTN_MAX_CTAS 64u +#define OMEGA_GPU_ATTN_MAX_Q_HEADS 64u +#define OMEGA_GPU_ATTN_MAX_GQA_CTX 4096u /* contiguous path: one block of 2^12 slots */ + +/* Mirror of aien-kv-cache KvLayoutDesc (crates/aien-kv-cache/src/lib.rs, main 8b5caed), + * field for field, strides in bytes. */ +typedef struct { + uint64_t block_stride_bytes; + uint64_t layer_stride_bytes; + uint64_t kv_plane_stride_bytes; + uint64_t token_stride_bytes; + uint64_t head_stride_bytes; + uint64_t pool_bytes; + uint32_t num_blocks; + uint32_t num_layers; + uint32_t block_size; /* must be a power of two (the kernel shifts and masks) */ +} OmegaGpuKvLayout; + +typedef struct { + uint32_t chip_calls; /* launches made */ + uint64_t elapsed_ns; /* sum of submit-to-second-marker over the launches */ + uint64_t call_ns; /* wall time of the whole API call (staging, launches, copy-out) */ + uint32_t completion_marker; /* first marker word of the last launch */ + uint32_t unwritten_words; /* poison words left in the outputs (0 on OK) */ + uint32_t gpr_count, insn_count; + bool kernel_cache_hit; /* the kernel was already generated */ + 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, + uint32_t seq_len, uint32_t num_q_heads, uint32_t num_kv_heads, + uint32_t head_dim, float *out, OmegaGpuAttnInfo *info); + +/* Paged bf16 KV (TensorBackend::paged_attention). pool points at the pool base. */ +int omega_gpu_paged_attention_bf16(const float *q, const void *pool, const OmegaGpuKvLayout *layout, + const uint32_t *block_ids, uint32_t num_block_ids, + uint32_t context_len, uint32_t layer_idx, + uint32_t num_q_heads, uint32_t num_kv_heads, uint32_t head_dim, + float *out, OmegaGpuAttnInfo *info); + +/* Batched (TensorBackend::paged_attention_batch). block_tables: [num_seqs][max_blocks_per_seq] i32 + * (negative = none), context_lens: [num_seqs] i32. q and out: [num_seqs][num_q_heads][head_dim]. */ +int omega_gpu_paged_attention_batch_bf16(const float *q, const void *pool, const OmegaGpuKvLayout *layout, + const int32_t *block_tables, const int32_t *context_lens, + uint32_t max_blocks_per_seq, uint32_t num_seqs, uint32_t layer_idx, + uint32_t num_q_heads, uint32_t num_kv_heads, uint32_t head_dim, + float *out, OmegaGpuAttnInfo *info); + +const char *omega_gpu_attention_rc_name(int rc); +void omega_gpu_attention_cache_clear(void); + +/* Stage name of the most recent CHIP_FAIL ("" if none), shared with the other GPU APIs + * (omega_gpu_session_last_error); an open failure carries the driver text. */ +const char *omega_gpu_attention_last_error(void); + +/* Test hooks. Mutants are deliberately wrong kernels the parity test must catch; + * never set in production. */ +typedef enum { + OMEGA_GPU_ATTN_MUTANT_NONE = 0, + 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 */ + /* 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); + +/* TEST ONLY (omega #308 regression R2). 0 = production branch-free chunk loop; 1 = the + * lane-dependent j >= ctx branch restored inside a BSSY/BSYNC reconvergence region; + * 2 = that kernel with the BSYNC dropped (negative control: the host simulator must refuse + * it). Clears the kernel cache. */ +void omega_gpu_attention_test_set_divergent(int mode); +/* "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. */ +int omega_gpu_attention_codegen(bool kv_f32, uint32_t log2_block_size, uint32_t log2_gqa, + OmegaBlackwellKernel *kernel); + +/* One launch as the launcher sees it (test hook below). Pointers are host buffers; + * params are the eight argument words after the four buffer addresses: + * max_blocks, scale bits, block stride, token stride, kv plane stride, head stride, + * num_q_heads, context-array byte offset. */ +typedef struct { + const void *q; size_t q_bytes; + const void *pool; size_t pool_bytes; + const void *tab; size_t tab_bytes; + void *out; size_t out_bytes; + uint32_t grid_x, grid_y; + uint32_t params[8]; +} OmegaGpuAttnLaunch; + +/* Host simulator hook (tests): when set, every launch runs the allocated IR program + * through `sim` instead of the chip. Lets the whole battery run on the host first + * (review before chip time). The program is post register allocation. */ +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 +#endif /* OMEGA_GPU_ATTENTION_API_H */ diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/omega_gpu_elementwise_api.c b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/omega_gpu_elementwise_api.c new file mode 100644 index 000000000..b16c70205 --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/omega_gpu_elementwise_api.c @@ -0,0 +1,1139 @@ +/* omega_gpu_elementwise_api.c: FB-1 cut 4 (2026-10-04). See the header. + * + * Layout of this file: (1) kernel codegen on top of the BlackwellIR, with a + * small scheduler that writes every control word; (2) one launcher cloned from + * the chip-proven tensor matmul path (omega_blackwell_execute_matmul_tensor); + * (3) the public entry points, which split work into <= OMEGA_GPU_EW_MAX_CTAS + * launches and run the host-side bookkeeping. + * + * Scheduling rules (chip-learned, src/omega_numeric.c omega_numeric_check_patch): + * - a variable-latency result (LDG, LDS, LDC, LDCU, S2R, SHFL, MUFU) arrives through + * a scoreboard; the producer stalls >= 2 and the consumer waits on that SB; + * - a fixed-latency result (FADD FMUL FFMA IMAD IADD3 MOV LOP3 SHF) has no + * scoreboard: the producer stalls >= 5 before its consumer (we use 6); + * - a predicate producer (ISETP) stalls 14 before @P0 use (NAK SM120 padding); + * - stores (STG, STS) set a read barrier so the next instruction cannot overwrite + * the source register early. + * The emitter below serialises: every instruction waits on everything still + * pending. A final local pass tightens supported adjacent ALU pairs to 5 cycles. + */ +#include "omega_gpu_elementwise_api.h" +#include "omega_blackwell_codegen.h" +#include "omega_bw_reconv.h" +#include "omega_blackwell_qmd.h" +#include "omega_blackwell_submit.h" +#include "omega_gpu_session.h" +#include "sha256.h" +#include +#include +#include +#include + +/* ------------------------------------------------------------------ codegen */ + +#define CW(stall, wbar, rbar, wait) \ + (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) +#define CW_YIELD 0x2000u +#define SB_NONE 7u +#define SB_GPR 0u /* variable-latency GPR results */ +#define SB_RD 1u /* store source-register read barrier */ +#define SB_UGPR 2u /* uniform-register results (LDCU) */ + +enum { K_FIXED, K_PRED, K_VAR, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC }; + +#define EW_MAX_LOOPS 4 +typedef struct { + BlackwellIRProgram *p; + uint32_t pending; /* scoreboards set and not yet waited on */ + int err; + int loop_start[EW_MAX_LOOPS], loop_end[EW_MAX_LOOPS]; + int n_loops; + int open_start[EW_MAX_LOOPS]; /* starts of loops begun and not yet ended (nesting stack) */ + int depth; + BwRegions rg; /* structured reconvergence regions (omega #308) */ + int rg_mutant; /* test only: 1 = drop the BSYNC at join, 2 = corrupt the BSSY target (host sim must catch both) */ + int exited; /* a predicated EXIT was emitted: some lanes of a warp may be gone */ + int exit_sync_ok; /* test only: lets a kernel put SHFL/BAR after a predicated EXIT (the negative control R6) */ + int test_path; /* building for omega_gpu_elementwise_codegen_ir (host tests), never for a launch */ +} Em; + +static void emit(Em *e, BlackwellIRInsn in, int kind) { + uint32_t wait = e->pending; + switch (kind) { + case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; + case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break; + case K_VAR: in.control = CW(4, SB_GPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_GPR; break; + case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; + case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; + case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; + /* a predicated EXIT or BRA that falls through must not be the only waiter */ + case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; + case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; + /* BSSY / BSYNC read no registers; the stall counts (1 and 5, both with yield) are the ones + * nvcc 13.0.88 writes for sm_121 (docs/gpu-reconvergence-308.md). They wait on everything + * pending so that, as for every branch here, nothing is in flight across a control edge + * and the straight-line scoreboard walk in the tests stays valid. */ + case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; + case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; + default: e->err = -1; return; + } + if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; /* append returns the index */ +} + +static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; } +static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; } +static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; } +static int here(const Em *e) { return (int)e->p->count; } + +#define NOREG -1 +static BlackwellIRInsn I0(BlackwellIROpcode op) { + BlackwellIRInsn in; memset(&in, 0, sizeof in); + in.op = op; in.dst_vreg = NOREG; in.src1_vreg = NOREG; in.src2_vreg = NOREG; in.src3_vreg = NOREG; in.ureg = NOREG; + return in; +} +static void ldcu64(Em *e, int udst, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDCU64); i.dst_vreg = udst; i.imm = off; i.is_uniform = true; emit(e, i, K_UVAR); } +static void ldc64(Em *e, int dst64, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC64); i.dst_vreg = dst64; i.imm = off; emit(e, i, K_VAR); } +static void ldc32(Em *e, int dst, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC); i.dst_vreg = dst; i.imm = off; emit(e, i, K_VAR); } +static void s2r(Em *e, int dst, uint32_t sr) { BlackwellIRInsn i = I0(BW_IR_S2R); i.dst_vreg = dst; i.imm = sr; emit(e, i, K_VAR); } +static void movi(Em *e, int dst, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_MOV_IMM); i.dst_vreg = dst; i.imm = imm; emit(e, i, K_FIXED); } +static void movf(Em *e, int dst, float f) { uint32_t u; memcpy(&u, &f, 4); movi(e, dst, u); } +static void movrz(Em *e, int dst) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = dst; emit(e, i, K_FIXED); } +/* dst = a * imm + c (c may be NOREG for RZ) */ +static void imadi(Em *e, int dst, int a, uint32_t imm, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = dst; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c; emit(e, i, K_FIXED); } +/* dst = a * b + c (registers) */ +static void imadr(Em *e, int dst, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = dst; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } +static void iadd3(Em *e, int dst, int a, int b) { BlackwellIRInsn i = I0(BW_IR_IADD3); i.dst_vreg = dst; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void lop3xor(Em *e, int dst, int a, int b) { BlackwellIRInsn i = I0(BW_IR_LOP3_XOR); i.dst_vreg = dst; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +/* dst64 = idx * 4 + ptr64 (byte address of element idx) */ +static void addr4(Em *e, int dst64, int idx, int ptr64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = dst64; i.src1_vreg = idx; i.imm = 4; i.src3_vreg = ptr64; emit(e, i, K_FIXED); } +static void ldg(Em *e, int dst, int addr64, int udesc) { BlackwellIRInsn i = I0(BW_IR_LDG_E); i.dst_vreg = dst; i.src1_vreg = addr64; i.ureg = udesc; emit(e, i, K_VAR); } +/* Independent loads (LDG_E from global, or LDS32 from shared memory with ureg NOREG) issued + * back to back (campaign C2a, 2026-10-05). The first load + * waits on whatever is pending; the others wait on nothing, so all n are in flight together. + * Every load sets SB_GPR (a counting scoreboard), so the next instruction waits once for all + * of them instead of once per load. Fails the build if a load address is a destination of the + * group or a destination repeats (the later loads would read an operand still in flight). */ +static void ld_group(Em *e, BlackwellIROpcode op, int n, const int *dst, const int *addr, int ureg) { + for (int i = 0; i < n; i++) + for (int k = 0; k < n; k++) + if (addr[i] == dst[k] || (k != i && dst[i] == dst[k])) { e->err = -1; return; } + for (int i = 0; i < n; i++) { + if (op != BW_IR_LDG_E && op != BW_IR_LDS32) { e->err = -1; return; } + BlackwellIRInsn in = I0(op); in.dst_vreg = dst[i]; in.src1_vreg = addr[i]; in.ureg = ureg; + if (i == 0) { emit(e, in, K_VAR); continue; } + in.control = CW(4, SB_GPR, SB_NONE, 0) | CW_YIELD; /* nonzero, so the encoder keeps it */ + if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; + } +} +static void stg(Em *e, int addr64, int val, int udesc) { BlackwellIRInsn i = I0(BW_IR_STG_E); i.src1_vreg = addr64; i.src2_vreg = val; i.ureg = udesc; emit(e, i, K_STORE); } +static void fadd(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FADD); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void fsub(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FSUB); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void fmul(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FMUL); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +static void ffma(Em *e, int d, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_FFMA); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } +static void mufu(Em *e, BlackwellIROpcode op, int d, int a) { BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = a; emit(e, i, K_VAR); } +/* Cross-lane and warp-ending ops are refused inside an open reconvergence region (omega #308): + * a SHFL or BAR.SYNC reached by a split warp reads inactive lanes / counts a partial warp + * (undefined; the 2026-10-04 attention bug), and a lane that EXITs inside a region can no + * longer be joined. The build fails closed instead of emitting such code. */ +/* Cross-lane instructions (SHFL, BAR.SYNC) need the whole warp present. The emitter refuses + * them (fails the build) inside an open reconvergence region, where lanes may be on different + * paths, and after a predicated EXIT, where lanes may be gone for good: a SHFL then reads + * inactive lanes (undefined on the chip, the attention failure of 2026-10-04). A kernel that + * must drop lanes before a cross-lane step clamps them instead and exits them afterwards. */ +static int warp_split(Em *e) { if (e->rg.depth > 0 || (e->exited && !e->exit_sync_ok)) { e->err = -1; return 1; } return 0; } +static void shfl_down(Em *e, int d, int a, uint32_t off) { if (warp_split(e)) return; BlackwellIRInsn i = I0(BW_IR_SHFL_DOWN); i.dst_vreg = d; i.src1_vreg = a; i.imm = off; emit(e, i, K_VAR); } +static void sts32(Em *e, int addr, int val) { BlackwellIRInsn i = I0(BW_IR_STS32); i.src1_vreg = addr; i.src2_vreg = val; emit(e, i, K_STORE); } +static void lds32(Em *e, int d, int addr) { BlackwellIRInsn i = I0(BW_IR_LDS32); i.dst_vreg = d; i.src1_vreg = addr; emit(e, i, K_VAR); } +static void bar_sync(Em *e) { if (warp_split(e)) return; emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } +static void isetp_ge_u32(Em *e, int a, int b) { BlackwellIRInsn i = I0(BW_IR_ISETP_GE_U32); i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_PRED); } +/* An EXIT inside a region would leave the barrier waiting for a lane that never comes: refused. */ +static void exit_if_p0(Em *e) { if (e->rg.depth > 0) { e->err = -1; return; } e->exited = 1; BlackwellIRInsn i = I0(BW_IR_EXIT); i.predicate_p0 = true; emit(e, i, K_EXIT); } +static void bra_back_if_not_p0(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.predicate_not = true; i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } +static void tail(Em *e) { + emit(e, I0(BW_IR_EXIT), K_EXIT); + BlackwellIRInsn self = I0(BW_IR_BRA); /* the self-branch every kernel here ends with */ + self.control = 0x000fc000u; + if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1; +} +/* Loops may nest (prime race cut): begin pushes the start, end pops it, so the inner + * loop is closed (and its liveness fixed) first. For the single-level loops above this + * is exactly the old behaviour. */ +static void loop_begin(Em *e) { if (e->depth >= EW_MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); } +/* Closing a loop: every value defined before the loop and read inside it stays + * live until the back-branch, otherwise the linear-scan allocator (which knows + * nothing about back-edges) would hand its register to a body temporary whose + * first definition comes after the value's last textual use (e.g. the scale y + * in rmsnorm pass 2), and the next iteration would read garbage. */ +static void loop_end(Em *e) { + if (e->n_loops >= EW_MAX_LOOPS || e->depth <= 0) { e->err = -1; return; } + int start = e->open_start[--e->depth], end = here(e) - 1; + e->loop_start[e->n_loops] = start; + e->loop_end[e->n_loops] = end; + e->n_loops++; + OmegaRegAlloc *ra = &e->p->regalloc; + for (int v = 0; v < ra->num_vregs; v++) { + OmegaLiveInterval *iv = &ra->intervals[v]; + if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end; + } +} + +/* Kernel argument words (constant bank 0, same slots as the matmul path): + * 0x380 a, 0x388 b, 0x390 c (64-bit each), then four 32-bit parameters. */ +#define ARG_A 0x380u +#define ARG_B 0x388u +#define ARG_C 0x390u +#define ARG_P0 0x398u +#define ARG_P1 0x39cu +#define ARG_P2 0x3a0u +#define ARG_P3 0x3a4u +#define ARG_P4 0x3a8u +#define DESC_OFF 0x358u + +typedef struct { int udesc, pa, pb, pc, tid, cta; } Pro; +static Pro prologue(Em *e) { + Pro r; + r.udesc = UV64(e); r.pa = V64(e); r.pb = V64(e); r.pc = V64(e); r.tid = V(e); r.cta = V(e); + ldcu64(e, r.udesc, DESC_OFF); + ldc64(e, r.pa, ARG_A); ldc64(e, r.pb, ARG_B); ldc64(e, r.pc, ARG_C); + s2r(e, r.tid, BW_SR_TID_X); s2r(e, r.cta, BW_SR_CTAID_X); + return r; +} + +/* out[i] = 2^a[i] for i < n (n = P0). Threads 128, grid ceil(n/128). Mutant: 2^(a+1). */ +static void gen_ex2(Em *e, int mutant) { + Pro p = prologue(e); + int n = V(e), i = V(e), off = V64(e), x = V(e), y = V(e); + ldc32(e, n, ARG_P0); + imadi(e, i, p.cta, OMEGA_GPU_EW_THREADS, p.tid); + isetp_ge_u32(e, i, n); + exit_if_p0(e); + addr4(e, off, i, p.pa); + ldg(e, x, off, p.udesc); + if (mutant) { int one = V(e); movf(e, one, 1.0f); fadd(e, x, x, one); } + mufu(e, BW_IR_MUFU_EX2, y, x); + addr4(e, off, i, p.pc); + stg(e, off, y, p.udesc); + tail(e); +} + +/* out[i] = a[i ^ 127] within each CTA of 128 (n % 128 == 0). STS own word, BAR.SYNC, + * LDS the partner word. Mutant: LDS own word (no exchange). */ +static void gen_xchg(Em *e, int mutant) { + Pro p = prologue(e); + int i = V(e), off = V64(e), x = V(e), saddr = V(e), paddr = V(e), mask = V(e), y = V(e); + imadi(e, i, p.cta, OMEGA_GPU_EW_THREADS, p.tid); + addr4(e, off, i, p.pa); + ldg(e, x, off, p.udesc); + imadi(e, saddr, p.tid, 4, NOREG); + sts32(e, saddr, x); + bar_sync(e); + if (mutant) { + lds32(e, y, saddr); + } else { + movi(e, mask, OMEGA_GPU_EW_THREADS - 1); + lop3xor(e, paddr, p.tid, mask); + imadi(e, paddr, paddr, 4, NOREG); + lds32(e, y, paddr); + } + addr4(e, off, i, p.pc); + stg(e, off, y, p.udesc); + tail(e); +} + +/* One CTA of 128 threads per row. P0 = dim/128 (loop count), P1 = bits(1/dim), + * P2 = bits(eps), P3 = dim. Mutant: the warp tree drops the last SHFL step. */ +static void gen_rmsnorm(Em *e, int mutant) { + Pro p = prologue(e); + int nk = V(e), invd = V(e), eps = V(e), dim = V(e); + int rowbase = V(e), idx = V(e), col = V(e), k = V(e), acc = V(e), cT = V(e), c1 = V(e); + int off = V64(e), x = V(e), t = V(e); + ldc32(e, nk, ARG_P0); ldc32(e, invd, ARG_P1); ldc32(e, eps, ARG_P2); ldc32(e, dim, ARG_P3); + imadr(e, rowbase, p.cta, dim, NOREG); + iadd3(e, idx, rowbase, p.tid); + movrz(e, acc); movrz(e, k); + movi(e, cT, OMEGA_GPU_EW_THREADS); movi(e, c1, 1); + loop_begin(e); + int l1 = here(e); + addr4(e, off, idx, p.pa); + ldg(e, x, off, p.udesc); + ffma(e, acc, x, x, acc); + iadd3(e, idx, idx, cT); + iadd3(e, k, k, c1); + isetp_ge_u32(e, k, nk); + bra_back_if_not_p0(e, l1); + loop_end(e); + static const uint32_t steps[5] = { 16, 8, 4, 2, 1 }; + for (int s = 0; s < (mutant ? 4 : 5); s++) { shfl_down(e, t, acc, steps[s]); fadd(e, acc, acc, t); } + int saddr = V(e); + imadi(e, saddr, p.tid, 4, NOREG); + sts32(e, saddr, acc); + bar_sync(e); + int a0 = V(e), a1 = V(e), a2 = V(e), a3 = V(e), s0 = V(e), s1 = V(e), s2 = V(e), s3 = V(e), sum = V(e); + movrz(e, a0); movi(e, a1, 128); movi(e, a2, 256); movi(e, a3, 384); + lds32(e, s0, a0); lds32(e, s1, a1); lds32(e, s2, a2); lds32(e, s3, a3); + fadd(e, sum, s0, s1); fadd(e, sum, sum, s2); fadd(e, sum, sum, s3); + int v = V(e), y = V(e), u = V(e), cmh = V(e), c15 = V(e); + fmul(e, v, sum, invd); + fadd(e, v, v, eps); + mufu(e, BW_IR_MUFU_RSQ, y, v); + movf(e, cmh, -0.5f); movf(e, c15, 1.5f); + fmul(e, u, v, y); fmul(e, u, u, y); ffma(e, u, u, cmh, c15); fmul(e, y, y, u); + int offw = V64(e), offc = V64(e), w = V(e), o = V(e); + iadd3(e, idx, rowbase, p.tid); + imadi(e, col, p.tid, 1, NOREG); /* col = tid (the encoder maps a missing src2 to R0, not RZ, so no IADD3 here) */ + movrz(e, k); + loop_begin(e); + int l2 = here(e); + addr4(e, off, idx, p.pa); + ldg(e, x, off, p.udesc); + addr4(e, offw, col, p.pb); + ldg(e, w, offw, p.udesc); + fmul(e, o, x, y); + fmul(e, o, o, w); + addr4(e, offc, idx, p.pc); + stg(e, offc, o, p.udesc); + iadd3(e, idx, idx, cT); + iadd3(e, col, col, cT); + iadd3(e, k, k, c1); + isetp_ge_u32(e, k, nk); + bra_back_if_not_p0(e, l2); + loop_end(e); + tail(e); +} + +/* One CTA of head_dim/2 threads per head. P0 = head_dim, P1 = head_dim/2. + * a = vector, b = table (cos in the first half of each head, sin in the second). + * Mutant: out0 = q0*cos + q1*sin. */ +static void gen_rope(Em *e, int mutant) { + Pro p = prologue(e); + int hd = V(e), half = V(e), i0 = V(e), i1 = V(e), off0 = V64(e), off1 = V64(e); + int q0 = V(e), q1 = V(e), c = V(e), s = V(e), t0 = V(e), t1 = V(e), o0 = V(e), o1 = V(e); + ldc32(e, hd, ARG_P0); ldc32(e, half, ARG_P1); + imadr(e, i0, p.cta, hd, p.tid); + iadd3(e, i1, i0, half); + addr4(e, off0, i0, p.pa); ldg(e, q0, off0, p.udesc); + addr4(e, off1, i1, p.pa); ldg(e, q1, off1, p.udesc); + addr4(e, off0, i0, p.pb); ldg(e, c, off0, p.udesc); + addr4(e, off1, i1, p.pb); ldg(e, s, off1, p.udesc); + fmul(e, t0, q0, c); fmul(e, t1, q1, s); + if (mutant) fadd(e, o0, t0, t1); else fsub(e, o0, t0, t1); + fmul(e, t0, q0, s); fmul(e, t1, q1, c); + fadd(e, o1, t0, t1); + addr4(e, off0, i0, p.pc); stg(e, off0, o0, p.udesc); + addr4(e, off1, i1, p.pc); stg(e, off1, o1, p.udesc); + tail(e); +} + +/* out[i] = g/(1+2^(g*-log2e)) * up for i < n (P0). Mutant: 2^(g*+log2e). */ +static void gen_swiglu(Em *e, int mutant) { + Pro p = prologue(e); + int n = V(e), i = V(e), off = V64(e), g = V(e), u = V(e), k = V(e), t = V(e), ex = V(e), d = V(e), r = V(e), one = V(e), o = V(e); + ldc32(e, n, ARG_P0); + imadi(e, i, p.cta, OMEGA_GPU_EW_THREADS, p.tid); + isetp_ge_u32(e, i, n); + exit_if_p0(e); + addr4(e, off, i, p.pa); ldg(e, g, off, p.udesc); + addr4(e, off, i, p.pb); ldg(e, u, off, p.udesc); + movf(e, k, mutant ? 1.4426950408889634f : -1.4426950408889634f); + movf(e, one, 1.0f); + fmul(e, t, g, k); + mufu(e, BW_IR_MUFU_EX2, ex, t); + fadd(e, d, ex, one); + mufu(e, BW_IR_MUFU_RCP, r, d); + fmul(e, o, g, r); + fmul(e, o, o, u); + addr4(e, off, i, p.pc); + stg(e, off, o, p.udesc); + tail(e); +} + +/* ------------------------------------------- prime race sieve (2026-10-05) */ +static void lop3lut(Em *e, int d, int a, int b, int c, uint32_t lut) { BlackwellIRInsn i = I0(BW_IR_LOP3_LUT); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; i.imm = lut & 0xffu; emit(e, i, K_FIXED); } +static void shl_r(Em *e, int d, int a, int n) { BlackwellIRInsn i = I0(BW_IR_SHF_L_U32); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = n; emit(e, i, K_FIXED); } +static void shr_i(Em *e, int d, int a, uint32_t n) { BlackwellIRInsn i = I0(BW_IR_SHF_R); i.dst_vreg = d; i.src1_vreg = a; i.imm = n; emit(e, i, K_FIXED); } +static void imadhi(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_IMAD_HI_U32); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } +/* dst64 = a * imm + c64 (IMAD.WIDE.U32 immediate form) */ +static void widei(Em *e, int dst64, int a, uint32_t imm, int c64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = dst64; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c64; emit(e, i, K_FIXED); } +/* @P0 BRA forward; returns the instruction index for bra_patch_here */ +static int bra_fwd_if_p0(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = 1; emit(e, i, K_BRA); return at; } +static void bra_patch_here(Em *e, int at) { if (e->err == 0 && at >= 0 && at < here(e)) e->p->insns[at].imm = (uint32_t)(here(e) - at); else e->err = -1; } +static void bra_always(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } + +/* ---- structured reconvergence regions (omega #308; bookkeeping in omega_bw_reconv.h) ---- + * region_begin: BSSY Bd (d = nesting depth), fails closed when no barrier register is left. + * region_exit: a forward branch out of the innermost region, patched to its BSYNC at join + * (RX_ALWAYS, RX_IF_P0, RX_IF_NOT_P0). + * region_join: BSYNC Bd; patches the BSSY to the instruction after it and every exit to it. + * Usage: divergent loop = region_begin; L: isetp; region_exit(RX_IF_P0); body; bra_always(L); region_join. + * if/else diamond = region_begin; isetp; t = bra_fwd_if_p0; then; region_exit(RX_ALWAYS); + * bra_patch_here(t); else; region_join. */ +enum { RX_ALWAYS, RX_IF_P0, RX_IF_NOT_P0 }; +static int region_begin(Em *e) { + int bar = bw_regions_next_bar(&e->rg); + if (bar < 0) { e->err = -1; return -1; } + int at = here(e); + BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = (uint8_t)bar; i.imm = 2; emit(e, i, K_BSSY); + if (bw_regions_begin(&e->rg, at) < 0) e->err = -1; + return bar; +} +static void region_exit(Em *e, int cond) { + int at = here(e); + BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = 1; + if (cond != RX_ALWAYS) { i.predicate_p0 = true; i.predicate_not = (cond == RX_IF_NOT_P0); } + emit(e, i, K_BRA); + if (bw_regions_exit(&e->rg, at) < 0) e->err = -1; +} +static void region_join(Em *e) { + if (e->rg.depth <= 0) { e->err = -1; return; } + int d = e->rg.depth - 1, bsync = here(e); + BlackwellIRInsn i = I0(e->rg_mutant == 1 ? BW_IR_NOP : BW_IR_BSYNC); i.bar_reg = (uint8_t)d; emit(e, i, K_BSYNC); + if (e->rg_mutant == 1) { + /* negative control (host sim only): the join is a NOP; patch as the real join would */ + BwRegions *r = &e->rg; r->depth--; + e->p->insns[r->bssy_at[d]].imm = (uint32_t)(bsync + 1 - r->bssy_at[d]); + for (int k = 0; k < r->n_exits[d]; k++) e->p->insns[r->exits[d][k]].imm = (uint32_t)(bsync - r->exits[d][k]); + return; + } + if (bw_regions_join(&e->rg, e->p, bsync) != d) e->err = -1; + if (e->rg_mutant == 2) e->p->insns[e->rg.bssy_at[d]].imm += 1; /* negative control: BSSY names the wrong join */ +} + +/* Word-parallel odd-only sieve (docs: handoff 2026-10-05 DESIGN "GPU kernel (native)"). + * Thread t owns 32-bit output words w = t, t + S, t + 2S, ... (S = P1 = threads launched; + * the I42 envelope caps a launch at OMEGA_GPU_EW_MAX_CTAS CTAs, so a grid-stride loop + * replaces one thread per word). Every word has exactly one writer; no atomics. + * Bit j of word w is the odd number 2(32w + j) + 1. + * Arguments: a (0x380) = prime table, entries {p, magic = ceil(2^32 / p), k0 = (p*p - 1)/2} + * (12 bytes, ascending p, odd primes <= isqrt(limit)) followed by ONE sentinel entry with + * k0 = 0xffffffff that ends every scan; c (0x390) = output words; + * P0 = nwords, P1 = S, P2 = last word index, P3 = ~tailmask (bits past the last odd). + * Range: limit < 2^32 (so base, k0 < 2^31 and the sign-bit tricks below hold). + * Per word: base = 32w; for each prime while k0 < base + 32: + * d = base - k0; neg = d >> 31 (k0 > base); dpos = neg ? 0 : d; jadd = neg ? k0 - base : 0 + * q = hi32(dpos * magic) (= floor(dpos/p) or one more: magic is rounded up) + * r = dpos - q*p (q one too big -> r = (dpos mod p) - p, wrapped) + * j = p - r; j -= p * ((r - 1) >> 31) (r == 0 -> 0; wrapped r -> p - (dpos mod p)) + * One fix covers both cases: with q one too big, dpos mod p >= p - dpos*e/2^32 > p/2 > 0 + * (e = magic*p - 2^32 < p, dpos < 2^31), so the true remainder is never 0 there and + * p - r - p = p - (dpos mod p) is the right offset. A separate "r += p if wrapped" step was + * removed after a negative control showed it changes no output (host sim, limit 1e7, where + * the overshoot occurs 268 times; bench/prime_race/tests magic_model). + * j += jadd; while j < 32 { acc |= 1 << j; j += p } + * then acc |= (w == 0) (the number 1), word = ~(acc | (w == last ? ~tailmask : 0)). + * Branch-free except the loop branches. Mutant: drops the j fix (j = p - r unreduced), so words where + * base - k0 is a multiple of p (or q overshot) mark the wrong bits. + * + * staged = 1 (OMEGA_GPU_EW_PRIME_SIEVE_SHARED, campaign C3): P4 = table words, sentinel included + * (3 per entry). The CTA first copies the table into shared memory at offset 0 (thread t copies + * words t, t + 128, ...), BAR_SYNC, then the prime loop reads shared memory (LDS) instead of + * uncached global memory. Every thread reaches the barrier: the w >= nwords exit comes after + * it. The launch must declare at least 4 * P4 bytes of shared memory. + * + * divergent (omega #308, regression R1): 0 = the warp-uniform mark loop above (campaign C5). + * 1 = the original lane-dependent mark loop (omega 888a011: `while j < 32 { acc |= 1 << j; j += p }`) + * wrapped in a reconvergence region. 2 = as 1, and the prime loop (whose break `k0 >= base + 32` + * is also lane-dependent: every lane sieves a different word) wrapped in an outer region, so the + * mark loop's region nests inside it (B0 outer, B1 inner). */ +static void gen_prime_sieve(Em *e, int mutant, int staged, int divergent) { + Pro pr = prologue(e); + int nwords = V(e), stride = V(e), lastw = V(e), tailinv = V(e); + int one = V(e), c32 = V(e), cm1 = V(e), w = V(e); + ldc32(e, nwords, ARG_P0); ldc32(e, stride, ARG_P1); ldc32(e, lastw, ARG_P2); ldc32(e, tailinv, ARG_P3); + movi(e, one, 1); movi(e, c32, 32); movi(e, cm1, 0xffffffffu); + int c4 = -1, c8 = -1, c12 = -1; + if (staged) { + int twords = V(e), i = V(e), ga = V64(e), val = V(e), sa = V(e); + c4 = V(e); c8 = V(e); c12 = V(e); + ldc32(e, twords, ARG_P4); + movi(e, c4, 4); movi(e, c8, 8); movi(e, c12, 12); + imadi(e, i, pr.tid, 1, NOREG); + /* The staging loop's trip count differs per thread (thread t copies words t, t+128, ...), + * so its exit splits the warp. The divergent variants rejoin the warp before BAR.SYNC + * (a barrier is not a reconvergence point: the fragments would stay split for the rest + * of the kernel). The production kernel keeps the 2026-10-05 C3 form unchanged. */ + if (divergent) region_begin(e); + loop_begin(e); /* staging loop */ + int ls = here(e); + isetp_ge_u32(e, i, twords); + int sdone = -1; + if (divergent) region_exit(e, RX_IF_P0); else sdone = bra_fwd_if_p0(e); + addr4(e, ga, i, pr.pa); + ldg(e, val, ga, pr.udesc); + imadi(e, sa, i, 4, NOREG); + sts32(e, sa, val); + imadi(e, i, one, OMEGA_GPU_EW_THREADS, i); + bra_always(e, ls); + loop_end(e); + if (divergent) region_join(e); else bra_patch_here(e, sdone); + bar_sync(e); + } + imadi(e, w, pr.cta, OMEGA_GPU_EW_THREADS, pr.tid); + isetp_ge_u32(e, w, nwords); + exit_if_p0(e); + + /* ptr, a64, a64m: global addresses, or (staged) 32-bit shared offsets */ + int base = V(e), basep32 = V(e), acc = V(e); + int ptr = staged ? V(e) : V64(e), a64 = staged ? V(e) : V64(e), a64m = staged ? V(e) : V64(e); + int p = V(e), magic = V(e), k0 = V(e), negp = V(e), d = V(e), neg = V(e), nd = V(e), dpos = V(e), jadd = V(e); + int q = V(e), r = V(e), j = V(e), t = V(e), z = V(e), bit = V(e); + int e1 = V(e), m = V(e), word = V(e), oaddr = V64(e); + int tt = V(e), jj = V(e), sh = V(e), c31 = V(e), cm32 = V(e); + movi(e, c31, 31); movi(e, cm32, 0xffffffe0u); + + loop_begin(e); /* word loop */ + int lw = here(e); + imadi(e, base, w, 32, NOREG); + iadd3(e, basep32, base, c32); + movrz(e, acc); + if (staged) movrz(e, ptr); else widei(e, ptr, one, 0, pr.pa); + + if (divergent >= 2) region_begin(e); /* outer region: the prime loop's break is lane-dependent */ + loop_begin(e); /* prime loop */ + int lp = here(e); + /* one memory round trip per prime: all three entry words load together (C2a). The + * sentinel entry is a whole entry, so loading p and magic before the k0 test is in bounds. */ + if (staged) { iadd3(e, a64, ptr, c8); iadd3(e, a64m, ptr, c4); } + else { widei(e, a64, one, 8, ptr); widei(e, a64m, one, 4, ptr); } + { + const int dsts[3] = { k0, p, magic }, adrs[3] = { a64, ptr, a64m }; + if (staged) ld_group(e, BW_IR_LDS32, 3, dsts, adrs, NOREG); + else ld_group(e, BW_IR_LDG_E, 3, dsts, adrs, pr.udesc); + } + isetp_ge_u32(e, k0, basep32); + int brk = -1; + if (divergent >= 2) region_exit(e, RX_IF_P0); else brk = bra_fwd_if_p0(e); + imadi(e, negp, p, 0xffffffffu, NOREG); /* -p */ + imadi(e, d, k0, 0xffffffffu, base); /* base - k0 */ + shr_i(e, neg, d, 31); + imadi(e, nd, d, 0xffffffffu, NOREG); /* -d */ + imadr(e, dpos, neg, nd, d); /* d - neg*d */ + imadr(e, jadd, neg, nd, NOREG); /* neg * (k0 - base) */ + imadhi(e, q, dpos, magic); + imadr(e, r, q, negp, dpos); /* dpos - q*p */ + imadi(e, j, r, 0xffffffffu, p); /* p - r */ + if (!mutant) { + iadd3(e, t, r, cm1); + shr_i(e, z, t, 31); /* r == 0, or r wrapped (q one too big) */ + imadr(e, j, z, negp, j); /* j - p */ + } + iadd3(e, j, j, jadd); + + /* mark loop (campaign C5): the trip count depends only on p, so every active lane runs the + * same ceil(32/p) steps and the warp never splits here (all active lanes are on the same + * prime). Bits past this word are masked out instead of branched around. The old loop ran + * "while j < 32", a lane-dependent branch with no reconvergence point. */ + if (divergent) { + /* R1: the 888a011 mark loop, lane-dependent trip count, inside a region. Each lane + * leaves at its own iteration (the exit jumps to the BSYNC) and the warp reunites there. */ + region_begin(e); + loop_begin(e); + int li = here(e); + isetp_ge_u32(e, j, c32); + region_exit(e, RX_IF_P0); + shl_r(e, bit, one, j); + lop3lut(e, acc, acc, bit, NOREG, 0xfc); /* acc | bit */ + iadd3(e, j, j, p); + bra_always(e, li); + loop_end(e); + region_join(e); + } else { + movrz(e, tt); + loop_begin(e); + int li = here(e); + isetp_ge_u32(e, tt, c32); + int done = bra_fwd_if_p0(e); + iadd3(e, jj, j, tt); + lop3lut(e, sh, jj, c31, NOREG, 0xc0); /* jj & 31 */ + shl_r(e, bit, one, sh); + iadd3(e, t, jj, cm32); + shr_i(e, z, t, 31); /* jj < 32 (j <= p + 31, far below 2^31) */ + imadi(e, m, z, 0xffffffffu, NOREG); /* all ones when jj < 32, else 0 */ + lop3lut(e, acc, acc, bit, m, 0xf8); /* acc | (bit & m) */ + iadd3(e, tt, tt, p); + bra_always(e, li); + loop_end(e); + bra_patch_here(e, done); + } + + if (staged) iadd3(e, ptr, ptr, c12); else widei(e, ptr, one, 12, ptr); + bra_always(e, lp); + loop_end(e); + if (divergent >= 2) region_join(e); else bra_patch_here(e, brk); + + iadd3(e, t, w, cm1); + shr_i(e, z, t, 31); /* w == 0: the number 1 is not prime */ + lop3lut(e, acc, acc, z, NOREG, 0xfc); + lop3xor(e, e1, w, lastw); + iadd3(e, t, e1, cm1); + shr_i(e, z, t, 31); /* w == last word */ + imadr(e, m, z, tailinv, NOREG); + lop3lut(e, word, acc, m, NOREG, 0x03); /* ~(acc | m) */ + addr4(e, oaddr, w, pr.pc); + stg(e, oaddr, word, pr.udesc); + /* IMAD, not IADD3: the IADD3 encoder ignores the scheduler control word (fixed + * 0x010fca00), so right after a store it would not wait on the read barrier */ + imadr(e, w, stride, one, w); + isetp_ge_u32(e, w, nwords); + bra_back_if_not_p0(e, lw); + loop_end(e); + tail(e); +} + +/* ---- reconvergence probes (omega #308, regressions R3..R6 and the resource bound) ---- + * All: a, b = u32 inputs, out = u32, P0 = n; 128 threads, grid ceil(n/128); launched by + * omega_gpu_reconv_probe_u32. A lane past n clamps its index to n-1 so that it loads valid + * data and takes part in the warp-wide SHFL; it exits only after the SHFL, before its store. + * Each probe computes a per-lane r on divergent paths, joins, then + * out[i] = r + 4099 * s, s = r of lane (lane+1) in the same warp (SHFL.DOWN 1; lane 31: own r) + * so a lane that joined late, never, or with the wrong value shows in the neighbour's word too. + * The host oracle is rc_oracle in tests/gpu_reconv_test.c. */ +typedef struct { Pro pr; int n, i, idx, x, y, r, s, one, cm1; } Rp; +static Rp rp_begin(Em *e) { + Rp k; memset(&k, 0, sizeof k); + k.pr = prologue(e); + k.n = V(e); k.i = V(e); k.idx = V(e); k.x = V(e); k.y = V(e); k.r = V(e); k.s = V(e); k.one = V(e); k.cm1 = V(e); + int nm1 = V(e), d = V(e), neg = V(e), off = V64(e); + ldc32(e, k.n, ARG_P0); + movi(e, k.one, 1); movi(e, k.cm1, 0xffffffffu); + imadi(e, k.i, k.pr.cta, OMEGA_GPU_EW_THREADS, k.pr.tid); + iadd3(e, nm1, k.n, k.cm1); /* n - 1 */ + imadi(e, d, k.i, 0xffffffffu, nm1); /* (n - 1) - i, top bit set when i > n - 1 */ + shr_i(e, neg, d, 31); + imadr(e, k.idx, neg, d, k.i); /* i, or n - 1 when past the end */ + addr4(e, off, k.idx, k.pr.pa); ldg(e, k.x, off, k.pr.udesc); + addr4(e, off, k.idx, k.pr.pb); ldg(e, k.y, off, k.pr.udesc); + return k; +} +static void rp_end(Em *e, const Rp *k) { + int out = V(e), off = V64(e); + shfl_down(e, k->s, k->r, 1); + imadi(e, out, k->s, 4099, k->r); + isetp_ge_u32(e, k->i, k->n); + exit_if_p0(e); + addr4(e, off, k->i, k->pr.pc); + stg(e, off, out, k->pr.udesc); + tail(e); +} +/* R3: if/else with sides of different length. x >= y: r = f(x, y) (5 steps); else r = x + 1. + * Math mutant: the long side is one too big. */ +static void gen_rc_diamond(Em *e, int mutant) { + Rp k = rp_begin(e); + int t = V(e), u = V(e); + region_begin(e); + isetp_ge_u32(e, k.x, k.y); + int els = bra_fwd_if_p0(e); + iadd3(e, k.r, k.x, k.one); /* short side */ + region_exit(e, RX_ALWAYS); + bra_patch_here(e, els); + imadi(e, t, k.x, 3, k.y); /* long side: t = 3x + y */ + lop3xor(e, u, t, k.x); /* u = t ^ x */ + imadi(e, k.r, u, 5, k.one); /* r = 5u + 1 */ + shr_i(e, t, k.r, 3); + iadd3(e, k.r, k.r, t); /* r += r >> 3 */ + if (mutant) iadd3(e, k.r, k.r, k.one); + region_join(e); + rp_end(e, &k); +} +/* The lane-dependent loop shared by R4 and R5: v = x & 255, step = (y & 15) + 1, + * while v < 256 { v += step; cnt += 1 }. Math mutant: the bound is 255. Emits the region. */ +static void rc_count_loop(Em *e, const Rp *k, int mutant, int v, int cnt) { + int st = V(e), bound = V(e), c255 = V(e), c15 = V(e); + movi(e, c255, 255); movi(e, c15, 15); movi(e, bound, mutant ? 255 : 256); + lop3lut(e, v, k->x, c255, NOREG, 0xc0); + lop3lut(e, st, k->y, c15, NOREG, 0xc0); + iadd3(e, st, st, k->one); + movrz(e, cnt); + region_begin(e); + loop_begin(e); + int L = here(e); + isetp_ge_u32(e, v, bound); + region_exit(e, RX_IF_P0); + iadd3(e, v, v, st); + iadd3(e, cnt, cnt, k->one); + bra_always(e, L); + loop_end(e); + region_join(e); +} +/* R4: lane-dependent loop exit (break). r = 256 * cnt + v. */ +static void gen_rc_loopbreak(Em *e, int mutant) { + Rp k = rp_begin(e); + int v = V(e), cnt = V(e); + rc_count_loop(e, &k, mutant, v, cnt); + imadi(e, k.r, cnt, 256, v); + rp_end(e, &k); +} +/* R5: nested divergence. Odd x: r = 7 (skips the inner region). Even x: the R4 loop inside the + * outer region, r = 256 * cnt + v + 100. */ +static void gen_rc_nested(Em *e, int mutant) { + Rp k = rp_begin(e); + int v = V(e), cnt = V(e), low = V(e), c100 = V(e); + movi(e, k.r, 7); movi(e, c100, 100); + lop3lut(e, low, k.x, k.one, NOREG, 0xc0); /* x & 1 */ + region_begin(e); + isetp_ge_u32(e, low, k.one); + region_exit(e, RX_IF_P0); + rc_count_loop(e, &k, mutant, v, cnt); + imadi(e, k.r, cnt, 256, v); + iadd3(e, k.r, k.r, c100); + region_join(e); + rp_end(e, &k); +} +/* R6: lanes past the end, a divergent region, then a CTA-wide exchange and the warp SHFL. + * xv = x | 1 computed on divergent paths (even lanes take the branch), every thread stores xv + * to its shared slot, BAR.SYNC, reads its partner's (tid ^ 1), r = 3x + partner_xv. + * Production form: lanes past n are clamped and exit after the SHFL (correct). early_exit + * (mutant 1): lanes past n EXIT first, as a naive kernel would; the emitter refuses that + * (BAR/SHFL after a predicated EXIT). Only the host-test build path (codegen_ir) may bypass the + * refusal, so the simulator can show what the kernel would do (SHFL_EXITED); a launch never can. */ +static void gen_rc_exit_bar(Em *e, int early_exit) { + Rp k = rp_begin(e); + int saddr = V(e), paddr = V(e), yv = V(e), xv = V(e), low = V(e); + if (early_exit) { + e->exit_sync_ok = early_exit == 1 && e->test_path; + isetp_ge_u32(e, k.i, k.n); + exit_if_p0(e); + } + imadi(e, xv, k.x, 1, NOREG); + lop3lut(e, low, k.x, k.one, NOREG, 0xc0); /* x & 1 */ + region_begin(e); + isetp_ge_u32(e, low, k.one); + region_exit(e, RX_IF_P0); /* odd lanes leave */ + iadd3(e, xv, xv, k.one); /* even lanes: xv = x + 1 */ + region_join(e); + imadi(e, saddr, k.pr.tid, 4, NOREG); + sts32(e, saddr, xv); + bar_sync(e); + lop3xor(e, paddr, k.pr.tid, k.one); + imadi(e, paddr, paddr, 4, NOREG); + lds32(e, yv, paddr); + imadi(e, k.r, k.x, 3, yv); + rp_end(e, &k); +} +/* Barrier-register bound: `levels` nested regions, each leaving lanes with x >= y out and adding 1 + * for the others: r = x + levels * (x < y). 16 levels must build (B0..B15); 17 must be refused. */ +static void gen_rc_depth(Em *e, int levels) { + Rp k = rp_begin(e); + imadi(e, k.r, k.x, 1, NOREG); + for (int d = 0; d < levels; d++) { + region_begin(e); + isetp_ge_u32(e, k.x, k.y); + region_exit(e, RX_IF_P0); + iadd3(e, k.r, k.r, k.one); + } + for (int d = 0; d < levels; d++) region_join(e); + rp_end(e, &k); +} +/* Codegen refusals (each must come back CODEGEN_FAIL; a kernel with a split warp at a + * cross-lane step must never reach the chip). */ +static void gen_rc_refuse(Em *e, OmegaGpuEwOp which) { + Rp k = rp_begin(e); + switch (which) { + case OMEGA_GPU_EW_RC_REFUSE_EXIT_IN_REGION: region_begin(e); isetp_ge_u32(e, k.i, k.n); exit_if_p0(e); region_join(e); break; + case OMEGA_GPU_EW_RC_REFUSE_BAR_IN_REGION: region_begin(e); bar_sync(e); region_join(e); break; + case OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION: region_begin(e); shfl_down(e, k.s, k.x, 1); region_join(e); break; + case OMEGA_GPU_EW_RC_REFUSE_UNCLOSED: region_begin(e); isetp_ge_u32(e, k.x, k.y); region_exit(e, RX_IF_P0); break; /* no join */ + case OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY: region_join(e); break; /* BSYNC without BSSY */ + default: e->err = -1; break; + } + imadi(e, k.r, k.x, 1, NOREG); + rp_end(e, &k); +} + +/* A value first defined inside a loop body may not be read after the loop + * before it is written again: the allocator could have let a later body + * temporary share its register, and the last iteration's value would be lost. */ +static int check_loop_invariant(const Em *e) { + const OmegaRegAlloc *ra = &e->p->regalloc; + for (int L = 0; L < e->n_loops; L++) + for (int v = 0; v < ra->num_vregs; v++) { + int fd = ra->intervals[v].first_def; + if (fd < e->loop_start[L] || fd > e->loop_end[L]) continue; + for (size_t i = (size_t)e->loop_end[L] + 1; i < e->p->count; i++) { + const BlackwellIRInsn *in = &e->p->insns[i]; + if (in->src1_vreg == v || in->src2_vreg == v || in->src3_vreg == v) return -1; + if (in->dst_vreg == v && !in->is_uniform) break; + } + } + return 0; +} + +#define EW_GPR_BUDGET 64u /* the QMD declares 64 registers, as the matmul launcher does */ + +/* Mesa NAK sm120_instr_latencies.rs + sm100/reg_raw.csv: ALU/dual-ALU pairs + * and FMA/FMA pairs need 4 + 1 cycles; mixed pairs need 5 + 1. CSV rows are + * READERS, columns WRITERS (see lat_rs_gen.py), not the reverse. IMAD.WIDE + * has operand-specific latencies and is deliberately excluded here. + * + * Retain at least 5 cycles after every fixed producer. Thus the next reader + * uses the pair-specific bound and later readers are >= 9 cycles away (the + * next instruction in a tightened pair is itself fixed). No instruction, + * branch target, wait, barrier assignment or register allocation is changed. + * IADD3's encoder still supplies its historical control word; do not pretend + * to schedule that producer here. Its existing 5-cycle delay is unchanged. + * + * Predicate-to-guard uses NAK paw_latency -> raw(write, None): reader + * RedirectedFp64, writer Dualalu = 13, plus SM120's 1 = 14, in emit above. + * NVIDIA's public ISA docs are silent about SASS control-word semantics. + */ +static int fixed_pair_class(BlackwellIROpcode op) { + switch (op) { + case BW_IR_MOV_RZ: case BW_IR_MOV_IMM: case BW_IR_IADD3: + case BW_IR_LOP3_XOR: case BW_IR_LOP3_LUT: + case BW_IR_SHF_R: case BW_IR_SHF_L_U32: return 1; + case BW_IR_IMAD: case BW_IR_IMAD_HI_U32: + case BW_IR_FADD: case BW_IR_FSUB: case BW_IR_FMUL: case BW_IR_FFMA: return 2; + default: return 0; + } +} + +static void schedule_fixed_pairs(BlackwellIRProgram *p) { + for (size_t i = 0; i + 1 < p->count; i++) { + BlackwellIRInsn *a = &p->insns[i]; + const BlackwellIRInsn *b = &p->insns[i + 1]; + int cls = fixed_pair_class(a->op); + if (!cls || cls != fixed_pair_class(b->op) || a->op == BW_IR_IADD3 || + a->predicate_p0 || b->predicate_p0 || ((a->control >> 9) & 15u) != 6) + continue; + a->control = (a->control & ~(15u << 9)) | (5u << 9); + } +} + +static int build_kernel(OmegaGpuEwOp op, int mutant, OmegaBlackwellKernel *kernel, BlackwellIRProgram *prog_out, int test_path) { + BlackwellIRProgram *prog = calloc(1, sizeof *prog); + if (!prog) return OMEGA_GPU_EW_CODEGEN_FAIL; + omega_bw_ir_init(prog); + Em e; memset(&e, 0, sizeof e); e.p = prog; e.test_path = test_path; + /* mutant 1 = the op's math mutant (as before); 2 = the join (BSYNC) dropped; 3 = the BSSY + * names the wrong join. 2 and 3 only change kernels that open a region. */ + e.rg_mutant = mutant == 2 ? 1 : mutant == 3 ? 2 : 0; + int math = mutant == 1; + switch (op) { + case OMEGA_GPU_EW_EX2: gen_ex2(&e, math); break; + case OMEGA_GPU_EW_XCHG: gen_xchg(&e, math); break; + case OMEGA_GPU_EW_RMSNORM: gen_rmsnorm(&e, math); break; + case OMEGA_GPU_EW_ROPE: gen_rope(&e, math); break; + case OMEGA_GPU_EW_SWIGLU: gen_swiglu(&e, math); break; + case OMEGA_GPU_EW_PRIME_SIEVE: gen_prime_sieve(&e, math, 0, 0); break; + case OMEGA_GPU_EW_PRIME_SIEVE_SHARED: gen_prime_sieve(&e, math, 1, 0); break; + case OMEGA_GPU_EW_PRIME_SIEVE_DIV1: gen_prime_sieve(&e, math, 0, 1); break; + case OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1: gen_prime_sieve(&e, math, 1, 1); break; + case OMEGA_GPU_EW_PRIME_SIEVE_DIV2: gen_prime_sieve(&e, math, 0, 2); break; + case OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2: gen_prime_sieve(&e, math, 1, 2); break; + case OMEGA_GPU_EW_RC_DIAMOND: gen_rc_diamond(&e, math); break; + case OMEGA_GPU_EW_RC_LOOPBREAK: gen_rc_loopbreak(&e, math); break; + case OMEGA_GPU_EW_RC_NESTED: gen_rc_nested(&e, math); break; + case OMEGA_GPU_EW_RC_EXIT_BAR: gen_rc_exit_bar(&e, math); break; + case OMEGA_GPU_EW_RC_REFUSE_EXIT_SHFL: gen_rc_exit_bar(&e, 2); break; + case OMEGA_GPU_EW_RC_DEPTH16: gen_rc_depth(&e, BW_RECONV_MAX_BAR); break; + case OMEGA_GPU_EW_RC_REFUSE_DEPTH17: gen_rc_depth(&e, BW_RECONV_MAX_BAR + 1); break; + case OMEGA_GPU_EW_RC_REFUSE_EXIT_IN_REGION: case OMEGA_GPU_EW_RC_REFUSE_BAR_IN_REGION: + case OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION: case OMEGA_GPU_EW_RC_REFUSE_UNCLOSED: + case OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY: gen_rc_refuse(&e, op); break; + default: e.err = -1; break; + } + if (e.rg.depth != 0) e.err = -1; /* a region left open has no join: refuse the build */ + schedule_fixed_pairs(prog); + int rc = OMEGA_GPU_EW_CODEGEN_FAIL; + int ra_rc = (e.err == 0) ? omega_bw_regalloc_solve(prog) : -1; + int loop_rc = (ra_rc == 0) ? check_loop_invariant(&e) : -1; +#ifdef OMEGA_EW_DEBUG + fprintf(stderr, "ew codegen op=%d mutant=%d emit_err=%d insns=%zu vregs=%d regalloc=%d loops=%d peak_gpr=%u\n", + (int)op, mutant, e.err, prog->count, prog->regalloc.num_vregs, ra_rc, loop_rc, prog->regalloc.peak_gpr_usage); +#endif + if (e.err == 0 && ra_rc == 0 && loop_rc == 0 && prog->regalloc.peak_gpr_usage <= EW_GPR_BUDGET) { + size_t max_bytes = (size_t)BW_MAX_IR_INSNS * 16; + memset(kernel, 0, sizeof *kernel); + kernel->code = malloc(max_bytes); + size_t emitted = 0; + if (kernel->code && omega_bw_encode_program(prog, kernel->code, max_bytes, &emitted) == 0) { + kernel->code_size = emitted; + kernel->insn_count = emitted / 16; + kernel->gpr_count = prog->regalloc.peak_gpr_usage; + kernel->uniform_gpr_count = prog->regalloc.peak_ugpr_usage; + sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); + if (prog_out) *prog_out = *prog; + rc = OMEGA_GPU_EW_OK; + } else { +#ifdef OMEGA_EW_DEBUG + fprintf(stderr, "ew codegen op=%d: encode failed (an instruction form the encoder refuses)\n", (int)op); +#endif + free(kernel->code); kernel->code = NULL; + } + } + free(prog); + return rc; +} + +/* --------------------------------------------------------- kernel cache */ + +/* One kernel per (op, mutant); the device copy is uploaded once and stays + * resident for the process (cut 4b: no per-call code page). */ +typedef struct { int used; int op; int mutant; OmegaBlackwellKernel kernel; NvrmMem code; } Slot; +static int g_mutant_op; /* 0 = none */ +static int g_mutant_level = 1; /* the mutant number built for g_mutant_op (1 math, 2 join dropped, 3 join corrupted) */ +static Slot g_cache[8]; +static OmegaGpuScratch g_a, g_b, g_c; /* staging, grown to the high-water mark, reused per call */ +static bool g_hooked; +#define LOCK() omega_gpu_session_lock() +#define UNLOCK() omega_gpu_session_unlock() + +static void cache_clear_locked(void) { + for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) + if (g_cache[i].used) { free(g_cache[i].kernel.code); omega_gpu_session_free(&g_cache[i].code); memset(&g_cache[i], 0, sizeof g_cache[i]); } +} + +void omega_gpu_elementwise_cache_clear(void) { + LOCK(); + cache_clear_locked(); + UNLOCK(); +} + +/* Called by the session (lock held, device still open) right before it closes. */ +static void on_session_close(void) { + cache_clear_locked(); + omega_gpu_session_scratch_free(&g_a); omega_gpu_session_scratch_free(&g_b); omega_gpu_session_scratch_free(&g_c); +} + +void omega_gpu_elementwise_test_set_mutant(int op) { + LOCK(); + g_mutant_op = op; g_mutant_level = 1; + cache_clear_locked(); + UNLOCK(); +} +void omega_gpu_elementwise_test_set_mutant_level(int op, int level) { + LOCK(); + g_mutant_op = op; g_mutant_level = level < 1 ? 1 : level > 3 ? 3 : level; + cache_clear_locked(); + UNLOCK(); +} + +/* Caller holds the lock. Builds the kernel if needed; uploads the device copy when a + * device is open (upload = false lets the host-only codegen path run without a chip). */ +static int kernel_for(OmegaGpuEwOp op, bool upload, Slot **out, bool *hit) { + int mutant = (g_mutant_op == (int)op) ? g_mutant_level : 0; + Slot *s = NULL; + for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) + if (g_cache[i].used && g_cache[i].op == (int)op && g_cache[i].mutant == mutant) { s = &g_cache[i]; *hit = true; break; } + if (!s) { + OmegaBlackwellKernel k; + int rc = build_kernel(op, mutant, &k, NULL, 0); + if (rc != OMEGA_GPU_EW_OK) return rc; + size_t slot = 0; + for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) if (!g_cache[i].used) { slot = i; break; } + s = &g_cache[slot]; + if (s->used) { free(s->kernel.code); omega_gpu_session_free(&s->code); } /* full: evict slot 0 */ + memset(s, 0, sizeof *s); + s->used = 1; s->op = (int)op; s->mutant = mutant; s->kernel = k; + *hit = false; + } + if (upload && !s->code.cpu) { + if (omega_gpu_session_alloc(s->kernel.code_size, &s->code) != 0) return OMEGA_GPU_EW_CHIP_FAIL; + memcpy(s->code.cpu, s->kernel.code, s->kernel.code_size); + __asm__ volatile("dsb sy" ::: "memory"); + } + *out = s; + return OMEGA_GPU_EW_OK; +} + +int omega_gpu_elementwise_codegen(OmegaGpuEwOp op, const uint32_t *dims, float eps, OmegaBlackwellKernel *kernel) { + (void)dims; (void)eps; /* kernels read their shape from the argument words */ + if (!kernel) return OMEGA_GPU_EW_BAD_ARGS; + LOCK(); + int mutant = (g_mutant_op == (int)op) ? g_mutant_level : 0; + UNLOCK(); + return build_kernel(op, mutant, kernel, NULL, 0); +} + +int omega_gpu_elementwise_codegen_ir(OmegaGpuEwOp op, int mutant, void *prog, OmegaBlackwellKernel *kernel) { + if (!kernel) return OMEGA_GPU_EW_BAD_ARGS; + if (mutant < 0 || mutant > 3) return OMEGA_GPU_EW_BAD_ARGS; + return build_kernel(op, mutant, kernel, (BlackwellIRProgram *)prog, 1); +} + +const char *omega_gpu_elementwise_last_error(void) { return omega_gpu_session_last_error(); } + +/* ------------------------------------------------------------ launcher */ + +#define EW_POISON 0xffbadbadu /* a NaN pattern no kernel here produces on purpose */ +#define EW_WAIT_MS 600000ull /* like main's load/store launchers; a 5 s wait failed under contention (omega#198) */ + +static uint64_t now_ns(void) { + struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); + return (uint64_t)t.tv_sec * 1000000000ULL + (uint64_t)t.tv_nsec; +} + +/* Caller holds the lock. Opens the shared device on first use. */ +static int dev_open_locked(void) { + if (!g_hooked) { (void)omega_gpu_session_on_close(on_session_close); g_hooked = true; } + return omega_gpu_session_open() ? OMEGA_GPU_EW_OK : OMEGA_GPU_EW_CHIP_FAIL; +} + +/* One launch: a and b staged in, c out (c_bytes/4 words, poison-filled first), through + * the shared session (omega_gpu_session_launch: shader-cache invalidate, constant bank, + * QMD0/QMD1, first marker, L2_FLUSH_DIRTY, second marker), then readback and the + * poison scan. b may be NULL when b_fill wrote the staging buffer already. */ +static int run_abc(const Slot *ks, const void *a, size_t a_bytes, const void *b, size_t b_bytes, + void *c, size_t c_bytes, uint32_t threads_x, uint32_t grid_x, + const uint32_t params[4], OmegaGpuEwInfo *info) { + if (omega_gpu_session_scratch(&g_a, a_bytes) || omega_gpu_session_scratch(&g_b, b_bytes) || omega_gpu_session_scratch(&g_c, c_bytes)) + return OMEGA_GPU_EW_CHIP_FAIL; + memcpy(g_a.mem.cpu, a, a_bytes); + if (b) memcpy(g_b.mem.cpu, b, b_bytes); + size_t c_words = c_bytes / 4; + uint32_t *c_dev = (uint32_t *)g_c.mem.cpu; + for (size_t i = 0; i < c_words; i++) c_dev[i] = EW_POISON; + __asm__ volatile("dsb sy" ::: "memory"); + + uint32_t args[OMEGA_BW_CBANK_MATMUL_ARGS_WORDS]; + args[0] = (uint32_t)g_a.mem.va; args[1] = (uint32_t)(g_a.mem.va >> 32); + args[2] = (uint32_t)g_b.mem.va; args[3] = (uint32_t)(g_b.mem.va >> 32); + args[4] = (uint32_t)g_c.mem.va; args[5] = (uint32_t)(g_c.mem.va >> 32); + args[6] = params[0]; args[7] = params[1]; args[8] = params[2]; args[9] = params[3]; + OmegaGpuLaunch L = { + .code_va = ks->code.va, .gpr_count = EW_GPR_BUDGET, .threads_x = threads_x, .threads_y = 1, + .grid_x = grid_x, .grid_y = 1, .num_elements = c_words, + .args = args, .n_args = OMEGA_BW_CBANK_MATMUL_ARGS_WORDS, .timeout_ms = EW_WAIT_MS, + }; + uint64_t elapsed = 0; uint32_t marker = 0; + if (omega_gpu_session_launch(&L, &elapsed, &marker) != 0) return OMEGA_GPU_EW_CHIP_FAIL; + + memcpy(c, g_c.mem.cpu, c_bytes); + uint32_t unwritten = 0; + for (size_t i = 0; i < c_words; i++) if (((uint32_t *)c)[i] == EW_POISON) unwritten++; + if (info) { + info->chip_calls++; + info->elapsed_ns += elapsed; + info->completion_marker = marker; + info->unwritten_words += unwritten; + info->threads_per_cta = threads_x; + info->ctas_last_launch = grid_x; + } + return unwritten ? OMEGA_GPU_EW_UNWRITTEN : OMEGA_GPU_EW_OK; +} + +/* ------------------------------------------------------- public entries */ + +static void info_begin(OmegaGpuEwInfo *info, const OmegaBlackwellKernel *k, bool hit) { + if (!info) return; + memset(info, 0, sizeof *info); + info->kernel_cache_hit = hit; + info->gpr_count = k->gpr_count; + info->insn_count = (uint32_t)k->insn_count; + snprintf(info->target_chip, sizeof info->target_chip, "NVIDIA DGX Spark (Grace Blackwell GB10)"); + info->sm_architecture = 121; +} +static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } +static void info_end(OmegaGpuEwInfo *info, uint64_t t0) { if (info) info->call_ns = now_ns() - t0; } + +/* Common prologue of every chip entry: lock, open, kernel (resident), info. */ +static int begin(OmegaGpuEwOp op, Slot **ks, OmegaGpuEwInfo *info) { + LOCK(); + int rc = dev_open_locked(); + bool hit = false; + if (rc == OMEGA_GPU_EW_OK) rc = kernel_for(op, true, ks, &hit); + if (rc != OMEGA_GPU_EW_OK) { UNLOCK(); return rc; } + info_begin(info, &(*ks)->kernel, hit); + return OMEGA_GPU_EW_OK; +} + +int omega_gpu_rmsnorm_f32(uint32_t rows, uint32_t dim, const float *x, const float *weight, + float eps, float *out, OmegaGpuEwInfo *info) { + uint64_t t0 = now_ns(); + if (!x || !weight || !out || rows == 0 || dim == 0 || dim % OMEGA_GPU_EW_THREADS != 0) return OMEGA_GPU_EW_BAD_ARGS; + if (dim > OMEGA_GPU_EW_MAX_DIM) return OMEGA_GPU_EW_TOO_LARGE; + Slot *k = NULL; + int rc = begin(OMEGA_GPU_EW_RMSNORM, &k, info); + if (rc != OMEGA_GPU_EW_OK) return rc; + uint32_t params[4] = { dim / OMEGA_GPU_EW_THREADS, f2u(1.0f / (float)dim), f2u(eps), dim }; + for (uint32_t r0 = 0; r0 < rows && rc == OMEGA_GPU_EW_OK; r0 += OMEGA_GPU_EW_MAX_CTAS) { + uint32_t nr = rows - r0 < OMEGA_GPU_EW_MAX_CTAS ? rows - r0 : OMEGA_GPU_EW_MAX_CTAS; + size_t bytes = (size_t)nr * dim * 4; + rc = run_abc(k, x + (size_t)r0 * dim, bytes, weight, (size_t)dim * 4, out + (size_t)r0 * dim, bytes, + OMEGA_GPU_EW_THREADS, nr, params, info); + } + UNLOCK(); + info_end(info, t0); + return rc; +} + +int omega_gpu_rope_f32(uint32_t heads, uint32_t head_dim, const float *v, + const float *cos_half, const float *sin_half, float *out, OmegaGpuEwInfo *info) { + uint64_t t0 = now_ns(); + if (!v || !cos_half || !sin_half || !out || heads == 0 || head_dim < 2 || (head_dim & 1)) return OMEGA_GPU_EW_BAD_ARGS; + if (head_dim > 2048) return OMEGA_GPU_EW_TOO_LARGE; + Slot *k = NULL; + int rc = begin(OMEGA_GPU_EW_ROPE, &k, info); + if (rc != OMEGA_GPU_EW_OK) return rc; + uint32_t half = head_dim / 2; + uint32_t params[4] = { head_dim, half, 0, 0 }; + for (uint32_t h0 = 0; h0 < heads && rc == OMEGA_GPU_EW_OK; h0 += OMEGA_GPU_EW_MAX_CTAS) { + uint32_t nh = heads - h0 < OMEGA_GPU_EW_MAX_CTAS ? heads - h0 : OMEGA_GPU_EW_MAX_CTAS; + size_t bytes = (size_t)nh * head_dim * 4; + /* table: cos in the first half of each head, sin in the second, written straight into + * the staging buffer (one copy per head of this launch) */ + if (omega_gpu_session_scratch(&g_b, bytes) != 0) { rc = OMEGA_GPU_EW_CHIP_FAIL; break; } + float *table = g_b.mem.cpu; + for (uint32_t h = 0; h < nh; h++) { + memcpy(table + (size_t)h * head_dim, cos_half, half * sizeof *table); + memcpy(table + (size_t)h * head_dim + half, sin_half, half * sizeof *table); + } + /* out may alias v: v is copied into the staging buffer before the result lands in out */ + rc = run_abc(k, v + (size_t)h0 * head_dim, bytes, NULL, bytes, out + (size_t)h0 * head_dim, bytes, half, nh, params, info); + } + UNLOCK(); + info_end(info, t0); + return rc; +} + +static int run_1d(OmegaGpuEwOp op, uint32_t n, const void *a, const void *b, void *c, OmegaGpuEwInfo *info) { + uint64_t t0 = now_ns(); + Slot *k = NULL; + int rc = begin(op, &k, info); + if (rc != OMEGA_GPU_EW_OK) return rc; + const uint32_t chunk = OMEGA_GPU_EW_MAX_CTAS * OMEGA_GPU_EW_THREADS; + for (uint32_t i0 = 0; i0 < n && rc == OMEGA_GPU_EW_OK; i0 += chunk) { + uint32_t ni = n - i0 < chunk ? n - i0 : chunk; + uint32_t grid = (ni + OMEGA_GPU_EW_THREADS - 1) / OMEGA_GPU_EW_THREADS; + uint32_t params[4] = { ni, 0, 0, 0 }; + size_t bytes = (size_t)ni * 4; + rc = run_abc(k, (const uint8_t *)a + (size_t)i0 * 4, bytes, b ? (const uint8_t *)b + (size_t)i0 * 4 : a, bytes, + (uint8_t *)c + (size_t)i0 * 4, bytes, OMEGA_GPU_EW_THREADS, grid, params, info); + } + UNLOCK(); + info_end(info, t0); + return rc; +} + +int omega_gpu_swiglu_f32(uint32_t n, const float *gate, const float *up, float *out, OmegaGpuEwInfo *info) { + if (!gate || !up || !out || n == 0) return OMEGA_GPU_EW_BAD_ARGS; + return run_1d(OMEGA_GPU_EW_SWIGLU, n, gate, up, out, info); +} +int omega_gpu_ex2_f32(uint32_t n, const float *x, float *out, OmegaGpuEwInfo *info) { + if (!x || !out || n == 0) return OMEGA_GPU_EW_BAD_ARGS; + return run_1d(OMEGA_GPU_EW_EX2, n, x, NULL, out, info); +} +int omega_gpu_shared_xchg_u32(uint32_t n, const uint32_t *x, uint32_t *out, OmegaGpuEwInfo *info) { + if (!x || !out || n == 0 || n % OMEGA_GPU_EW_THREADS != 0) return OMEGA_GPU_EW_BAD_ARGS; + return run_1d(OMEGA_GPU_EW_XCHG, n, x, NULL, out, info); +} +int omega_gpu_reconv_probe_u32(OmegaGpuEwOp op, uint32_t n, const uint32_t *a, const uint32_t *b, uint32_t *out, OmegaGpuEwInfo *info) { + if (!a || !b || !out || n == 0) return OMEGA_GPU_EW_BAD_ARGS; + switch (op) { + case OMEGA_GPU_EW_RC_DIAMOND: case OMEGA_GPU_EW_RC_LOOPBREAK: case OMEGA_GPU_EW_RC_NESTED: + case OMEGA_GPU_EW_RC_EXIT_BAR: case OMEGA_GPU_EW_RC_DEPTH16: break; + default: return OMEGA_GPU_EW_BAD_ARGS; + } + return run_1d(op, n, a, b, out, info); +} + +const char *omega_gpu_elementwise_rc_name(int rc) { + switch (rc) { + case OMEGA_GPU_EW_OK: return "OK"; + case OMEGA_GPU_EW_BAD_ARGS: return "BAD_ARGS"; + case OMEGA_GPU_EW_TOO_LARGE: return "TOO_LARGE"; + case OMEGA_GPU_EW_CODEGEN_FAIL: return "CODEGEN_FAIL"; + case OMEGA_GPU_EW_CHIP_FAIL: return "CHIP_FAIL"; + case OMEGA_GPU_EW_UNWRITTEN: return "UNWRITTEN"; + default: return "UNKNOWN"; + } +} diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/omega_gpu_elementwise_api.h b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/omega_gpu_elementwise_api.h new file mode 100644 index 000000000..8b92c839d --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/omega_gpu_elementwise_api.h @@ -0,0 +1,177 @@ +#ifndef OMEGA_GPU_ELEMENTWISE_API_H +#define OMEGA_GPU_ELEMENTWISE_API_H +/* + * omega_gpu_elementwise_api: native (no CUDA) elementwise and row-reduction + * kernels for the AIEN inference stack (FB-1 cut 4, 2026-10-04). + * + * Three inference ops plus two probe ops, every kernel generated by our own + * codegen (src/omega_blackwell_codegen.c IR) and launched through the same + * chip-proven sequence as the tensor matmul path: all buffers GPU-uncached + * (nvos.h NVOS32_ATTR2_GPU_CACHEABLE: system memory cached by the GPU is not + * coherent with CPU mappings), first marker, L2_FLUSH_DIRTY, second marker + * (C3 tail), poison-filled output scanned for unwritten words after readback. + * + * The math follows aien-sovereign-core crates/aien-inference-abi/src/tensor.rs + * (ReferenceCpuBackend), read 2026-10-04 at main 5fdab70: + * rmsnorm: scale = 1/sqrt(mean(x^2) + eps) (oracle sums in f64; the chip + * sums in f32 and uses MUFU.RSQ refined by one Newton step), + * out[i] = (x[i] * scale) * weight[i] in that order. + * rope: rotate_half pairs (i, i + head_dim/2) per head, cos/sin tables + * supplied by the host as f32 (the oracle casts f64 cos/sin to f32 + * before the f32 math): out0 = q0*cos - q1*sin, out1 = q0*sin + q1*cos, + * emitted as FMUL, FMUL, FADD (no fused multiply-add), so the chip + * result is bit-identical to the f32 oracle. + * swiglu: out = silu(gate) * up, silu(g) = g / (1 + exp(-g)); the chip uses + * MUFU.EX2(g * -log2(e)) and MUFU.RCP (approximate units), the + * oracle computes in f64. Tolerance is stated per op in the test. + * + * Cut 4b (2026-10-04): the device, channel and launch scratch are the process-wide + * session shared with the matmul and attention APIs (omega_gpu_session.h): one open + * per process instead of one per call (~40 ms), kernels resident after first use, + * staging buffers reused at their high-water mark. Calls serialise on one lock. + * + * Launch envelope: at most OMEGA_GPU_EW_MAX_CTAS CTAs per launch (the matmul + * sweep of 2026-10-03 lost whole tiles at 128 CTAs, investigation I42); the + * API splits larger work on the host and makes several launches. + */ +#include +#include +#include +#include "omega_blackwell_matmul.h" /* OmegaBlackwellKernel */ + +#ifdef __cplusplus +extern "C" { +#endif + +enum { + OMEGA_GPU_EW_OK = 0, + OMEGA_GPU_EW_BAD_ARGS = -1, /* NULL pointer, zero dimension, or a shape rule below broken */ + OMEGA_GPU_EW_TOO_LARGE = -2, /* dimension outside the codegen limits */ + OMEGA_GPU_EW_CODEGEN_FAIL = -3, /* our codegen refused or exceeded the register budget */ + OMEGA_GPU_EW_CHIP_FAIL = -4, /* device open, submit, marker or release failed */ + OMEGA_GPU_EW_UNWRITTEN = -5 /* output words still equal to their poison after the run */ +}; + +/* Kernel families. The two probes exist to prove the new instructions on the + * chip with the smallest possible kernels (and are kept for regression). */ +typedef enum { + OMEGA_GPU_EW_EX2 = 1, /* out[i] = 2^a[i] (probe for MUFU.EX2) */ + OMEGA_GPU_EW_XCHG, /* out[i] = a[i ^ (T-1)] inside each CTA (probe for STS/BAR.SYNC/LDS) */ + OMEGA_GPU_EW_RMSNORM, + OMEGA_GPU_EW_ROPE, + OMEGA_GPU_EW_SWIGLU, + OMEGA_GPU_EW_PRIME_SIEVE, /* prime race (2026-10-05): word-parallel odd-only sieve, codegen only + * here; launched by bench/prime_race/gb10_native.c (argument words in + * gen_prime_sieve's comment) */ + OMEGA_GPU_EW_PRIME_SIEVE_SHARED, /* the same sieve with the table staged in shared memory (C3) */ + /* Structured warp reconvergence (omega #308). The four sieves below are the production sieve + * with its lane-dependent loops put back inside BSSY/BSYNC regions (regression R1): DIV1 wraps + * the original mark loop (omega 888a011), DIV2 also wraps the prime loop (nested regions). */ + OMEGA_GPU_EW_PRIME_SIEVE_DIV1, + OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1, + OMEGA_GPU_EW_PRIME_SIEVE_DIV2, + OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2, + /* Probes for omega_gpu_reconv_probe_u32 (u32 in a, b; u32 out; oracle in tests/gpu_reconv_test.c) */ + OMEGA_GPU_EW_RC_DIAMOND, /* R3: if/else, sides of different length, SHFL after the join */ + OMEGA_GPU_EW_RC_LOOPBREAK, /* R4: loop with a lane-dependent break */ + OMEGA_GPU_EW_RC_NESTED, /* R5: a lane-dependent loop region inside an if region */ + OMEGA_GPU_EW_RC_EXIT_BAR, /* R6: lanes past the end + STS/BAR.SYNC/LDS exchange + SHFL, lanes exit last */ + OMEGA_GPU_EW_RC_DEPTH16, /* 16 nested regions: every barrier register B0..B15 in use */ + /* Codegen refusals: omega_gpu_elementwise_codegen_ir must return CODEGEN_FAIL for each */ + OMEGA_GPU_EW_RC_REFUSE_DEPTH17, /* a 17th nested region: no barrier register left */ + OMEGA_GPU_EW_RC_REFUSE_EXIT_SHFL, /* lanes EXIT, then BAR.SYNC/SHFL on the rest */ + OMEGA_GPU_EW_RC_REFUSE_EXIT_IN_REGION, /* EXIT inside an open region */ + OMEGA_GPU_EW_RC_REFUSE_BAR_IN_REGION, /* BAR.SYNC inside an open region */ + OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION, /* SHFL inside an open region */ + OMEGA_GPU_EW_RC_REFUSE_UNCLOSED, /* region begun, never joined */ + OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY /* BSYNC with no BSSY */ +} OmegaGpuEwOp; + +/* Shape rules (checked, refused with BAD_ARGS / TOO_LARGE): + * rmsnorm: dim % OMEGA_GPU_EW_THREADS == 0, dim <= OMEGA_GPU_EW_MAX_DIM, rows >= 1 + * rope: head_dim even, 2 <= head_dim <= 2048 (head_dim/2 threads per CTA), heads >= 1 + * swiglu: n >= 1 (the kernel guards the tail with a predicated EXIT) + * ex2: n >= 1; xchg: n % OMEGA_GPU_EW_THREADS == 0 */ +#define OMEGA_GPU_EW_THREADS 128u +#define OMEGA_GPU_EW_MAX_DIM 16384u +#define OMEGA_GPU_EW_MAX_CTAS 64u + +typedef struct { + uint32_t chip_calls; /* launches made */ + uint64_t elapsed_ns; /* sum of launch-to-second-marker over the launches */ + uint32_t completion_marker; /* observed first marker word of the last launch */ + uint32_t unwritten_words; /* output words still equal to poison (0 on success) */ + bool kernel_cache_hit; /* the shape's kernel was already generated in this process */ + uint32_t gpr_count; /* registers the generated kernel uses */ + uint32_t insn_count; + uint32_t threads_per_cta; + uint32_t ctas_last_launch; + char target_chip[64]; + uint32_t sm_architecture; + uint64_t call_ns; /* cut 4b: wall time of the whole API call (staging, launches, copy-out) */ +} OmegaGpuEwInfo; + +/* out[r*dim + i] = x[r*dim + i] * scale_r * weight[i]; scale_r = 1/sqrt(mean_i(x^2) + eps). */ +int omega_gpu_rmsnorm_f32(uint32_t rows, uint32_t dim, const float *x, const float *weight, + float eps, float *out, OmegaGpuEwInfo *info); + +/* v holds `heads` heads of head_dim values back to back; cos_half and sin_half + * hold head_dim/2 values each (cos(pos*freq_i), sin(pos*freq_i)); out gets the + * rotated heads (may alias v: the API copies through its own buffers). */ +int omega_gpu_rope_f32(uint32_t heads, uint32_t head_dim, const float *v, + const float *cos_half, const float *sin_half, float *out, + OmegaGpuEwInfo *info); + +/* out[i] = silu(gate[i]) * up[i] for i < n. */ +int omega_gpu_swiglu_f32(uint32_t n, const float *gate, const float *up, float *out, + OmegaGpuEwInfo *info); + +/* Probes. ex2: out[i] = 2^x[i]. xchg: out[i] = x[i ^ (OMEGA_GPU_EW_THREADS-1)] within each CTA. */ +int omega_gpu_ex2_f32(uint32_t n, const float *x, float *out, OmegaGpuEwInfo *info); +int omega_gpu_shared_xchg_u32(uint32_t n, const uint32_t *x, uint32_t *out, OmegaGpuEwInfo *info); +/* Reconvergence probes (op = one of OMEGA_GPU_EW_RC_DIAMOND .. OMEGA_GPU_EW_RC_DEPTH16, else + * BAD_ARGS). n >= 1, any n: lanes past the end take part in the warp step and exit before + * their store. out[i] = r_i + 4099 * r_(i+1 in warp); r per op in src/omega_gpu_elementwise_api.c. */ +int omega_gpu_reconv_probe_u32(OmegaGpuEwOp op, uint32_t n, const uint32_t *a, const uint32_t *b, + uint32_t *out, OmegaGpuEwInfo *info); + +/* Generate (without launching) the kernel for one op. Kernels read their shape + * from the argument words, so one kernel per op serves every shape; dims and eps + * are accepted for forward compatibility and ignored today (may be NULL / 0). + * Lets tests disassemble the bytes offline (nvdisasm) and check the register + * budget. Caller frees kernel->code. */ +int omega_gpu_elementwise_codegen(OmegaGpuEwOp op, const uint32_t *dims, float eps, + OmegaBlackwellKernel *kernel); + +/* Same, with the mutant chosen by the caller (0 = production kernel, 1 = the op's math mutant, + * 2 = the reconvergence join (BSYNC) dropped, 3 = the BSSY names the wrong join; 2 and 3 change + * only kernels that open a region, the host simulator must catch both) and, when prog is + * not NULL, a copy of the allocated IR program (a BlackwellIRProgram, post register + * allocation) for a host IR simulator. Does not touch the device or the kernel cache. + * Caller frees kernel->code. */ +int omega_gpu_elementwise_codegen_ir(OmegaGpuEwOp op, int mutant, void *prog, + OmegaBlackwellKernel *kernel); + +/* TEST ONLY. Selects a deliberately wrong kernel for one op (0 clears). Each + * mutant changes the math by far more than the op's tolerance, so a parity + * check that still passes against it proves nothing. Clears the kernel cache. + * EX2: computes 2^(x+1). XCHG: reads its own shared word (no exchange). + * RMSNORM: the warp tree drops its last SHFL step (half the lanes go missing). + * ROPE: out0 = q0*cos + q1*sin. SWIGLU: exp(+g) instead of exp(-g). */ +void omega_gpu_elementwise_test_set_mutant(int op); +/* TEST ONLY. Like test_set_mutant, choosing the mutant number (1 math, 2 join dropped, 3 join + * corrupted) that the next launch of `op` builds; lets a chip test observe what a missing + * reconvergence does (the simulator must have flagged it first). */ +void omega_gpu_elementwise_test_set_mutant_level(int op, int level); + +const char *omega_gpu_elementwise_rc_name(int rc); +void omega_gpu_elementwise_cache_clear(void); + +/* Stage name of the most recent CHIP_FAIL ("" if none), shared with the other GPU APIs + * (omega_gpu_session_last_error); an open failure carries the driver text. */ +const char *omega_gpu_elementwise_last_error(void); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/omega_gpu_matmul_api.c b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/omega_gpu_matmul_api.c new file mode 100644 index 000000000..bb797d4c9 --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/omega_gpu_matmul_api.c @@ -0,0 +1,375 @@ +/* + * omega_gpu_matmul_api.c: FB-1 cut 1b, on the shared session since cut 4b. + * See the header for the contract. + * + * Submission path: omega_gpu_session_launch (src/omega_gpu_session.c), the same + * words this file emitted in cut 1b (chip PASS FB1-CUT1B-4345406): SETUP_WORDS, + * shader-cache invalidate, constant bank upload, QMD0/QMD1 launch, first marker + * release, then the C3 tail (L2_FLUSH_DIRTY and a second release on uncached + * memory the host waits for before reading). Every buffer both sides touch is + * GPU-uncached (nvos.h NVOS32_ATTR2_GPU_CACHEABLE: "For system memory this will + * not be coherent with direct CPU mappings"). The device, channel, pushbuffer, + * constant bank, marker page and QMD page now belong to the session and are + * shared with the elementwise and attention APIs; this file keeps the resident + * tensors, the kernel cache (host and device copies) and its staging buffers. + */ +#include "omega_gpu_matmul_api.h" +#include "omega_gpu_session.h" +#include "omega_blackwell_codegen.h" +#include "omega_blackwell_matmul.h" +#include "omega_blackwell_qmd.h" +#include "omega_blackwell_submit.h" +#include "nvrm.h" +#include +#include +#include +#include + +#define CACHE_SLOTS 8 +#define MARKER_TIMEOUT_MS 20000ull /* a 1x2048x128256 call is ~0.1 s; 20 s is a stall, not a slow run */ +#define POISON_F32 (-999.0f) +#define ORACLE_TOL 1e-5 + +struct OmegaGpuTensor { + uint32_t k, n, kp, np; + NvrmMem mem; /* kp x np bf16 */ +}; + +typedef struct { + bool valid; + uint32_t kp, np, grid_x; + int mutant; + OmegaMatMulSpec spec; + OmegaBlackwellKernel kernel; + NvrmMem code; /* resident copy of kernel.code */ +} CacheSlot; + +static struct { + OmegaGpuScratch a_buf, c_buf; /* activation and result staging, grown to the high-water mark */ + uint32_t cta_budget; + int oracle, mutant; + bool hooked; + CacheSlot cache[CACHE_SLOTS]; + unsigned cache_next; +} g = { .cta_budget = OMEGA_GPU_MATMUL_MAX_CTAS, .oracle = 1 }; + +#define FAILAT(s) (omega_gpu_session_set_error(s), 1) +const char *omega_gpu_matmul_last_error(void) { return omega_gpu_session_last_error(); } + +static uint64_t now_ns(void) { + struct timespec t; + clock_gettime(CLOCK_MONOTONIC, &t); + return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec; +} + +static uint32_t round_up(uint32_t v, uint32_t q) { return (v + q - 1) / q * q; } + +const char *omega_gpu_matmul_rc_name(int rc) { + switch (rc) { + case OMEGA_GPU_MATMUL_OK: return "OK"; + case OMEGA_GPU_MATMUL_BAD_ARGS: return "BAD_ARGS"; + case OMEGA_GPU_MATMUL_TOO_LARGE: return "TOO_LARGE"; + case OMEGA_GPU_MATMUL_CODEGEN_FAIL: return "CODEGEN_FAIL"; + case OMEGA_GPU_MATMUL_CHIP_FAIL: return "CHIP_FAIL"; + case OMEGA_GPU_MATMUL_PARITY_FAIL: return "PARITY_FAIL"; + default: return "UNKNOWN"; + } +} + +int omega_gpu_matmul_is_blocked(void) { return omega_gpu_session_is_blocked(); } + +/* ---- device-side state (caller holds the session lock) ---- */ + +static void cache_clear_locked(void) { + for (unsigned i = 0; i < CACHE_SLOTS; i++) { + CacheSlot *s = &g.cache[i]; + if (!s->valid) continue; + omega_blackwell_kernel_free(&s->kernel); + omega_gpu_session_free(&s->code); + memset(s, 0, sizeof *s); + } + g.cache_next = 0; +} + +/* Called by the session (lock held, device still open) right before it closes. */ +static void on_session_close(void) { + cache_clear_locked(); + omega_gpu_session_scratch_free(&g.a_buf); + omega_gpu_session_scratch_free(&g.c_buf); +} + +static OmegaGpuSession *dev_open_locked(void) { + if (!g.hooked) { (void)omega_gpu_session_on_close(on_session_close); g.hooked = true; } + return omega_gpu_session_open(); +} + +void omega_gpu_device_close(void) { omega_gpu_session_close(); } + +void omega_gpu_matmul_cache_clear(void) { + omega_gpu_session_lock(); + cache_clear_locked(); + omega_gpu_session_unlock(); +} + +void omega_gpu_matmul_set_cta_budget(uint32_t ctas) { + omega_gpu_session_lock(); + g.cta_budget = ctas ? ctas : OMEGA_GPU_MATMUL_MAX_CTAS; + cache_clear_locked(); + omega_gpu_session_unlock(); +} + +uint32_t omega_gpu_matmul_cta_budget(void) { return g.cta_budget; } + +void omega_gpu_matmul_set_oracle(int on) { g.oracle = on ? 1 : 0; } + +void omega_gpu_matmul_test_set_mutant(int on) { + omega_gpu_session_lock(); + g.mutant = on ? 1 : 0; + cache_clear_locked(); + omega_gpu_session_unlock(); +} + +/* ---- kernel cache (caller holds the lock, device open) ---- */ + +static int kernel_for(uint32_t kp, uint32_t np, uint32_t grid_x, CacheSlot **out, bool *hit) { + for (unsigned i = 0; i < CACHE_SLOTS; i++) { + CacheSlot *s = &g.cache[i]; + if (s->valid && s->kp == kp && s->np == np && s->grid_x == grid_x && s->mutant == g.mutant) { + *out = s; *hit = true; + return OMEGA_GPU_MATMUL_OK; + } + } + OmegaMatMulSpec spec; + /* the kernel ignores M; 16 keeps the spec inside the tile rule */ + if (omega_matmul_spec_init(&spec, 16, kp, np, OMEGA_MATMUL_PRECISION_BF16) != 0) return OMEGA_GPU_MATMUL_TOO_LARGE; + OmegaBlackwellKernel kernel; + memset(&kernel, 0, sizeof kernel); + if (omega_blackwell_codegen_matmul_tensor_loop(&spec, grid_x, g.mutant, &kernel) != 0 || !kernel.code || kernel.code_size == 0) + return OMEGA_GPU_MATMUL_CODEGEN_FAIL; + NvrmMem code; + if (omega_gpu_session_alloc(kernel.code_size, &code) != 0) { omega_blackwell_kernel_free(&kernel); return OMEGA_GPU_MATMUL_CHIP_FAIL; } + memcpy(code.cpu, kernel.code, kernel.code_size); + CacheSlot *s = &g.cache[g.cache_next]; + g.cache_next = (g.cache_next + 1) % CACHE_SLOTS; + if (s->valid) { omega_blackwell_kernel_free(&s->kernel); omega_gpu_session_free(&s->code); } + s->valid = true; s->kp = kp; s->np = np; s->grid_x = grid_x; s->mutant = g.mutant; + s->spec = spec; s->kernel = kernel; s->code = code; + *out = s; *hit = false; + return OMEGA_GPU_MATMUL_OK; +} + +/* ---- one launch (caller holds the lock, device open, not blocked) ---- */ + +static int launch(const CacheSlot *ks, uint64_t a_va, uint64_t b_va, uint64_t c_va, + uint32_t grid_x, uint32_t grid_y, uint64_t *elapsed_ns, uint32_t *marker_out) { + uint32_t cbank_args[OMEGA_BW_CBANK_MATMUL_ARGS_WORDS]; + omega_blackwell_build_cbank_args_matmul(cbank_args, a_va, b_va, c_va, grid_y * 16u, ks->kp, ks->np); + OmegaGpuLaunch L = { + .code_va = ks->code.va, .gpr_count = ks->kernel.gpr_count, + .threads_x = 32, .threads_y = 1, .grid_x = grid_x, .grid_y = grid_y, + .num_elements = (size_t)grid_x * grid_y * 128u, + .args = cbank_args, .n_args = OMEGA_BW_CBANK_MATMUL_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS, + }; + return omega_gpu_session_launch(&L, elapsed_ns, marker_out) == 0 ? OMEGA_GPU_MATMUL_OK : OMEGA_GPU_MATMUL_CHIP_FAIL; +} + +/* ---- tensors ---- */ + +void omega_gpu_tensor_shape(const OmegaGpuTensor *t, uint32_t *k, uint32_t *n) { + if (k) *k = t ? t->k : 0; + if (n) *n = t ? t->n : 0; +} + +int omega_gpu_tensor_upload_bf16(uint32_t k, uint32_t n, const uint16_t *b, OmegaGpuTensor **out) { + if (out) *out = NULL; + if (!b || !out || k == 0 || n == 0) return OMEGA_GPU_MATMUL_BAD_ARGS; + if (k > OMEGA_BW_MATMUL_MAX_K || n > OMEGA_BW_MATMUL_MAX_N) return OMEGA_GPU_MATMUL_TOO_LARGE; + OmegaGpuTensor *t = calloc(1, sizeof *t); + if (!t) return OMEGA_GPU_MATMUL_BAD_ARGS; + t->k = k; t->n = n; t->kp = round_up(k, OMEGA_GPU_MATMUL_TILE_K); t->np = round_up(n, OMEGA_GPU_MATMUL_TILE_N); + int rc = OMEGA_GPU_MATMUL_OK; + omega_gpu_session_lock(); + if (!dev_open_locked() || omega_gpu_session_alloc((size_t)t->kp * t->np * 2u, &t->mem) != 0) rc = OMEGA_GPU_MATMUL_CHIP_FAIL; + omega_gpu_session_unlock(); + if (rc != OMEGA_GPU_MATMUL_OK) { free(t); return rc; } + uint16_t *d = t->mem.cpu; + if (t->np != n || t->kp != k) memset(d, 0, (size_t)t->kp * t->np * 2u); + for (uint32_t r = 0; r < k; r++) memcpy(&d[(size_t)r * t->np], &b[(size_t)r * n], (size_t)n * 2u); + __asm__ volatile("dsb sy" ::: "memory"); + *out = t; + return OMEGA_GPU_MATMUL_OK; +} + +int omega_gpu_tensor_upload_f32(uint32_t k, uint32_t n, const float *b, OmegaGpuTensor **out) { + if (out) *out = NULL; + if (!b || !out || k == 0 || n == 0) return OMEGA_GPU_MATMUL_BAD_ARGS; + if (k > OMEGA_BW_MATMUL_MAX_K || n > OMEGA_BW_MATMUL_MAX_N) return OMEGA_GPU_MATMUL_TOO_LARGE; + size_t cnt = (size_t)k * n; + uint16_t *h = malloc(cnt * sizeof *h); + if (!h) return OMEGA_GPU_MATMUL_BAD_ARGS; + for (size_t i = 0; i < cnt; i++) h[i] = omega_fp32_to_bf16(b[i]); + int rc = omega_gpu_tensor_upload_bf16(k, n, h, out); + free(h); + return rc; +} + +void omega_gpu_tensor_free(OmegaGpuTensor *t) { + if (!t) return; + omega_gpu_session_lock(); + omega_gpu_session_free(&t->mem); + omega_gpu_session_unlock(); + free(t); +} + +/* ---- the matmul ---- */ + +static int matmul_core(uint32_t m, const uint16_t *a, const OmegaGpuTensor *b, float *c, OmegaGpuMatmulInfo *info) { + const uint32_t k = b->k, n = b->n, kp = b->kp, np = b->np; + const uint32_t mp = round_up(m, OMEGA_GPU_MATMUL_TILE_M), mt = mp / 16, nt = np / 8; + omega_gpu_session_lock(); + int rc = OMEGA_GPU_MATMUL_OK; + if (!dev_open_locked()) { omega_gpu_session_unlock(); return OMEGA_GPU_MATMUL_CHIP_FAIL; } + const uint32_t budget = g.cta_budget ? g.cta_budget : OMEGA_GPU_MATMUL_MAX_CTAS; + /* rows per launch: all of them unless the row tiles alone exceed the budget */ + uint32_t rows_per_launch = mt <= budget ? mp : budget * 16u; + uint32_t grid_y_full = rows_per_launch / 16u; + uint32_t grid_x = budget / grid_y_full; if (grid_x == 0) grid_x = 1; if (grid_x > nt) grid_x = nt; + if (info) { + info->m = m; info->k = k; info->n = n; info->padded_m = mp; info->padded_n = np; info->padded_k = kp; + info->k_slices = 1; info->rows_per_call = rows_per_launch; info->grid_x = grid_x; info->grid_y = grid_y_full; + info->resident = true; info->kernel_cache_hit = true; + strncpy(info->target_chip, "NVIDIA DGX Spark (Grace Blackwell GB10)", sizeof info->target_chip - 1); + info->sm_architecture = 121; + } + if (omega_gpu_session_scratch(&g.a_buf, (size_t)mp * kp * 2u) != 0 || omega_gpu_session_scratch(&g.c_buf, (size_t)mp * np * 4u) != 0) { + omega_gpu_session_unlock(); return OMEGA_GPU_MATMUL_CHIP_FAIL; + } + /* activation rows in, zero padded; result rows poisoned */ + uint16_t *ad = g.a_buf.mem.cpu; + if (mp != m || kp != k) memset(ad, 0, (size_t)mp * kp * 2u); + for (uint32_t r = 0; r < m; r++) memcpy(&ad[(size_t)r * kp], &a[(size_t)r * k], (size_t)k * 2u); + 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"); + + for (uint32_t r0 = 0; r0 < mp && rc == OMEGA_GPU_MATMUL_OK; r0 += rows_per_launch) { + uint32_t rows = mp - r0 < rows_per_launch ? mp - r0 : rows_per_launch; + uint32_t grid_y = rows / 16u; + CacheSlot *ks = NULL; bool hit = false; + rc = kernel_for(kp, np, grid_x, &ks, &hit); + if (rc != OMEGA_GPU_MATMUL_OK) break; + if (info && !hit) info->kernel_cache_hit = false; + rc = launch(ks, g.a_buf.mem.va + (uint64_t)r0 * kp * 2u, b->mem.va, g.c_buf.mem.va + (uint64_t)r0 * np * 4u, + grid_x, grid_y, info ? &info->elapsed_ns : NULL, info ? &info->completion_marker : NULL); + if (info) info->chip_calls++; + } + if (rc == OMEGA_GPU_MATMUL_OK) { + /* the chip must have written every real output word (poison check, as the engine does) */ + size_t unchanged = 0; + for (uint32_t r = 0; r < m; r++) { + const float *row = &cd[(size_t)r * np]; + for (uint32_t j = 0; j < n; j++) if (row[j] == POISON_F32) unchanged++; + memcpy(&c[(size_t)r * n], row, (size_t)n * sizeof *c); + } + if (unchanged && FAILAT("output poison survived")) rc = OMEGA_GPU_MATMUL_CHIP_FAIL; + } + omega_gpu_session_unlock(); + return rc; +} + +static int resident_entry(uint32_t m, const uint16_t *a, const OmegaGpuTensor *b, float *c, OmegaGpuMatmulInfo *info) { + uint64_t t0 = now_ns(); + if (info) memset(info, 0, sizeof *info); + if (!a || !b || !c || m == 0 || !b->mem.cpu) return OMEGA_GPU_MATMUL_BAD_ARGS; + if (m > OMEGA_BW_MATMUL_MAX_M) return OMEGA_GPU_MATMUL_TOO_LARGE; + int rc = matmul_core(m, a, b, c, info); + if (info) info->call_ns = now_ns() - t0; + return rc; +} + +int omega_gpu_matmul_resident_bf16(uint32_t m, const uint16_t *a, const OmegaGpuTensor *b, float *c, + OmegaGpuMatmulInfo *info) { + return resident_entry(m, a, b, c, info); +} + +int omega_gpu_matmul_resident_f32(uint32_t m, const float *a, const OmegaGpuTensor *b, float *c, + OmegaGpuMatmulInfo *info) { + uint64_t t0 = now_ns(); + if (info) memset(info, 0, sizeof *info); + if (!a || !b || !c || m == 0 || !b->mem.cpu) return OMEGA_GPU_MATMUL_BAD_ARGS; + if (m > OMEGA_BW_MATMUL_MAX_M) return OMEGA_GPU_MATMUL_TOO_LARGE; + size_t cnt = (size_t)m * b->k; + uint16_t *h = malloc(cnt * sizeof *h); + if (!h) return OMEGA_GPU_MATMUL_BAD_ARGS; + for (size_t i = 0; i < cnt; i++) h[i] = omega_fp32_to_bf16(a[i]); + int rc = matmul_core(m, h, b, c, info); + free(h); + if (info) info->call_ns = now_ns() - t0; + return rc; +} + +/* Host oracle: double accumulation of the bf16 inputs; an element mismatches when + * its error exceeds ORACLE_TOL of the accumulation scale (sum of |terms|). */ +static void oracle(uint32_t m, uint32_t k, uint32_t n, const uint16_t *a, const uint16_t *b, const float *c, OmegaGpuMatmulInfo *info) { + float max_abs = 0.0f, max_rel = 0.0f; + size_t mism = 0; + for (uint32_t i = 0; i < m; i++) { + for (uint32_t j = 0; j < n; j++) { + double acc = 0.0, scale = 0.0; + for (uint32_t t = 0; t < k; t++) { + double p = (double)omega_bf16_to_fp32(a[(size_t)i * k + t]) * (double)omega_bf16_to_fp32(b[(size_t)t * n + j]); + acc += p; scale += fabs(p); + } + double d = fabs(acc - (double)c[(size_t)i * n + j]); + double rel = d / (scale > 1e-6 ? scale : 1e-6); + if ((float)d > max_abs) max_abs = (float)d; + if ((float)rel > max_rel) max_rel = (float)rel; + if (rel > ORACLE_TOL) mism++; + } + } + info->max_abs_err = max_abs; info->max_rel_err = max_rel; info->mismatch_count = mism; + info->oracle_ran = true; info->parity_verified = (mism == 0); +} + +int omega_gpu_matmul_bf16(uint32_t m, uint32_t k, uint32_t n, + const uint16_t *a, const uint16_t *b, float *c, + OmegaGpuMatmulInfo *info) { + uint64_t t0 = now_ns(); + OmegaGpuMatmulInfo local; + if (!info) info = &local; + memset(info, 0, sizeof *info); + if (!a || !b || !c || m == 0 || k == 0 || n == 0) return OMEGA_GPU_MATMUL_BAD_ARGS; + if (m > OMEGA_BW_MATMUL_MAX_M || k > OMEGA_BW_MATMUL_MAX_K || n > OMEGA_BW_MATMUL_MAX_N) + return OMEGA_GPU_MATMUL_TOO_LARGE; + OmegaGpuTensor *t = NULL; + int rc = omega_gpu_tensor_upload_bf16(k, n, b, &t); + if (rc != OMEGA_GPU_MATMUL_OK) return rc; + rc = matmul_core(m, a, t, c, info); + omega_gpu_tensor_free(t); + info->resident = false; + if (rc == OMEGA_GPU_MATMUL_OK && g.oracle) { + oracle(m, k, n, a, b, c, info); + if (!info->parity_verified) rc = OMEGA_GPU_MATMUL_PARITY_FAIL; + } + info->call_ns = now_ns() - t0; + return rc; +} + +int omega_gpu_matmul_f32(uint32_t m, uint32_t k, uint32_t n, + const float *a, const float *b, float *c, + OmegaGpuMatmulInfo *info) { + if (info) memset(info, 0, sizeof *info); + if (!a || !b || !c || m == 0 || k == 0 || n == 0) return OMEGA_GPU_MATMUL_BAD_ARGS; + if (m > OMEGA_BW_MATMUL_MAX_M || k > OMEGA_BW_MATMUL_MAX_K || n > OMEGA_BW_MATMUL_MAX_N) + return OMEGA_GPU_MATMUL_TOO_LARGE; + size_t na = (size_t)m * k, nb = (size_t)k * n; + uint16_t *ab = malloc(na * sizeof *ab); + uint16_t *bb = malloc(nb * sizeof *bb); + if (!ab || !bb) { free(ab); free(bb); return OMEGA_GPU_MATMUL_BAD_ARGS; } + for (size_t i = 0; i < na; i++) ab[i] = omega_fp32_to_bf16(a[i]); + for (size_t i = 0; i < nb; i++) bb[i] = omega_fp32_to_bf16(b[i]); + int rc = omega_gpu_matmul_bf16(m, k, n, ab, bb, c, info); + free(ab); + free(bb); + return rc; +} diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/omega_gpu_session.c b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/omega_gpu_session.c new file mode 100644 index 000000000..29207d021 --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/omega_gpu_session.c @@ -0,0 +1,241 @@ +/* + * omega_gpu_session.c: FB-1 cut 4b. See the header for the contract. + * + * The launch words are the ones omega_gpu_matmul_api.c (cut 1b, chip PASS + * FB1-CUT1B-4345406) and omega_gpu_elementwise_api.c (cut 4, FB1-CUT4-0d0241a) + * both emitted; the two were identical except for the argument word count, which + * is now a parameter. Nothing about the sequence changed in this cut. + */ +#include "omega_gpu_session.h" +#include "omega_blackwell_qmd.h" +#include "omega_blackwell_submit.h" +#include "nvrm.h" +#include +#include +#include +#include + +#define PAGE 0x1000ull + +/* Same words as src/omega_blackwell_submit.c SETUP_WORDS (compute class setup). */ +static const uint32_t SETUP_WORDS[18] = { + 0x20012061, 0x0000cec0, 0x20012092, 0x00000001, 0x200120a8, 0x0000000f, 0x2001255d, 0x00000003, + 0x2001255e, 0x20000000, 0x2001255f, 0x000fffff, 0x20012557, 0x00000003, 0x20012558, 0x22000000, + 0x20012559, 0x00000000, +}; + +static pthread_mutex_t g_mu = PTHREAD_MUTEX_INITIALIZER; +static OmegaGpuSession g_s; +static bool g_open, g_blocked; +static char g_err[320] = ""; +static void (*g_hooks[8])(void); +static unsigned g_nhooks; + +static uint64_t now_ns(void) { + struct timespec t; + clock_gettime(CLOCK_MONOTONIC, &t); + return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec; +} + +/* Marker wait. With spin_us, the host first re-reads the uncached marker word back to back (with + * the ARM yield hint) so a launch that finishes inside the window is seen within about one read, + * instead of after a 50 us sleep that Linux timer slack stretches further. Past the window it is + * the same m16 sleep-poll as before, with the full timeout, so a stuck launch still latches. */ +static int session_wait(volatile uint32_t *m, uint32_t want, uint64_t timeout_ms, uint32_t spin_us) { + if (spin_us) { + const uint64_t end = now_ns() + (uint64_t)spin_us * 1000u; + do { + if ((int32_t)(*m - want) >= 0) return 0; + __asm__ volatile("yield" ::: "memory"); + } while (now_ns() < end); + } + return m16_native_wait_marker(m, want, timeout_ms); +} +static size_t page_up(size_t b) { b = (b + PAGE - 1) & ~(PAGE - 1); return b < PAGE ? PAGE : b; } + +const char *omega_gpu_session_last_error(void) { return g_err; } +void omega_gpu_session_set_error(const char *stage) { + snprintf(g_err, sizeof g_err, "%s", stage ? stage : ""); +} +static void fail_rm(const char *stage, const Nvrm *rm) { + /* the driver text (RM class / ioctl errno / RM status, nvrm.c fail()) travels with the stage */ + snprintf(g_err, sizeof g_err, "%s: %s", stage, rm && rm->err[0] ? rm->err : "(no driver text)"); +} + +void omega_gpu_session_lock(void) { pthread_mutex_lock(&g_mu); } +void omega_gpu_session_unlock(void) { pthread_mutex_unlock(&g_mu); } +bool omega_gpu_session_is_open(void) { return g_open; } +int omega_gpu_session_is_blocked(void) { return g_blocked ? 1 : 0; } + +int omega_gpu_session_alloc(size_t bytes, NvrmMem *out) { + if (!g_open || g_blocked) { omega_gpu_session_set_error("alloc without open device"); return -1; } + if (nvrm_alloc_gpu_uncached(&g_s.ctx.rm, page_up(bytes), out) != 0) { fail_rm("nvrm_alloc_gpu_uncached", &g_s.ctx.rm); memset(out, 0, sizeof *out); return -1; } + return 0; +} + +void omega_gpu_session_free(NvrmMem *m) { + if (m->cpu && g_open && !g_blocked) (void)nvrm_free(&g_s.ctx.rm, m); + memset(m, 0, sizeof *m); +} + +int omega_gpu_session_scratch(OmegaGpuScratch *s, size_t bytes) { + if (bytes > s->high_water) s->high_water = bytes; + if (s->cap >= bytes && s->mem.cpu) return 0; + omega_gpu_session_free(&s->mem); + s->cap = 0; + if (omega_gpu_session_alloc(s->high_water, &s->mem) != 0) return -1; + s->cap = (size_t)s->mem.size; + return 0; +} + +void omega_gpu_session_scratch_free(OmegaGpuScratch *s) { + omega_gpu_session_free(&s->mem); + s->cap = 0; +} + +static void free_session_buffers(void) { + omega_gpu_session_free(&g_s.qmd); omega_gpu_session_free(&g_s.marker); + omega_gpu_session_free(&g_s.cbank); omega_gpu_session_free(&g_s.pb); +} + +OmegaGpuSession *omega_gpu_session_open(void) { + if (g_blocked) { omega_gpu_session_set_error("process latched"); return NULL; } + if (g_open) return &g_s; + uint64_t t0 = now_ns(); + memset(&g_s.ctx, 0, sizeof g_s.ctx); + /* nvrm_open directly (m16_native_open is memset + nvrm_open + nvrm_close on + * failure) so the driver's own failure text survives into the stage name. */ + if (nvrm_open(&g_s.ctx.rm) != 0) { fail_rm("nvrm_open", &g_s.ctx.rm); (void)nvrm_close(&g_s.ctx.rm); return NULL; } + if (m16_native_create_channel(&g_s.ctx) != 0) { fail_rm("m16_native_create_channel", &g_s.ctx.rm); (void)m16_native_close(&g_s.ctx); return NULL; } + g_open = true; /* so alloc / free work during the rest of the open */ + if (omega_gpu_session_alloc(0x10000, &g_s.pb) || omega_gpu_session_alloc(PAGE, &g_s.cbank) || + omega_gpu_session_alloc(PAGE, &g_s.marker) || omega_gpu_session_alloc(0x10000, &g_s.qmd)) { + char keep[sizeof g_err]; memcpy(keep, g_err, sizeof keep); + free_session_buffers(); + g_open = false; + (void)m16_native_close(&g_s.ctx); + snprintf(g_err, sizeof g_err, "session scratch alloc (%s)", keep); + return NULL; + } + g_s.ctx.pb_mem = g_s.pb; /* m16_native_enqueue_methods copies the method stream here */ + g_s.opens++; + g_s.last_open_ns = now_ns() - t0; + return &g_s; +} + +int omega_gpu_session_on_close(void (*hook)(void)) { /* caller holds the lock (the APIs register from their open path) */ + if (!hook) return -1; + int rc = 0; + bool seen = false; + for (unsigned i = 0; i < g_nhooks; i++) if (g_hooks[i] == hook) seen = true; + if (!seen) { if (g_nhooks < sizeof g_hooks / sizeof g_hooks[0]) g_hooks[g_nhooks++] = hook; else rc = -1; } + return rc; +} + +void omega_gpu_session_close(void) { + pthread_mutex_lock(&g_mu); + if (g_open && !g_blocked) { + for (unsigned i = 0; i < g_nhooks; i++) g_hooks[i](); + free_session_buffers(); + (void)m16_native_close(&g_s.ctx); + g_open = false; + } + pthread_mutex_unlock(&g_mu); +} + +static uint32_t round_up(uint32_t v, uint32_t q) { return (v + q - 1) / q * q; } + +int omega_gpu_session_launch(const OmegaGpuLaunch *L, uint64_t *elapsed_ns, uint32_t *marker_out) { + if (!g_open || g_blocked) { omega_gpu_session_set_error(g_blocked ? "process latched" : "launch without open device"); return -1; } + if (!L || !L->args || L->n_args == 0 || L->n_args > OMEGA_GPU_SESSION_MAX_ARGS || L->threads_x == 0 || L->grid_x == 0 || L->grid_y == 0 || + L->spin_us > OMEGA_GPU_SESSION_MAX_SPIN_US) { + omega_gpu_session_set_error("launch arguments"); return -1; + } + const uint32_t threads_y = L->threads_y ? L->threads_y : 1; + uint32_t cbank_data[OMEGA_BW_CBANK_DRIVER_WORDS]; + omega_blackwell_build_cbank_driver_2d(cbank_data, g_s.cbank.va, L->threads_x, threads_y, L->grid_x, L->grid_y); + memcpy(g_s.cbank.cpu, cbank_data, sizeof cbank_data); + memcpy((uint8_t *)g_s.cbank.cpu + 0x380, L->args, (size_t)L->n_args * 4u); + + const uint64_t qmd0_va = g_s.qmd.va, qmd1_va = g_s.qmd.va + 0x1000, sem_va = g_s.qmd.va + 0x2000, scratch_va = g_s.qmd.va + 0x4000; + uint32_t gpr = L->gpr_count > 64u ? round_up(L->gpr_count, 8u) : 64u; + OmegaBlackwellQmdConfig cfg = { + .code_va = L->code_va, .cbank_va = g_s.cbank.va, .scratch_va = scratch_va, .sem_va = sem_va, + .qmd0_va = qmd0_va, .qmd1_va = qmd1_va, .num_elements = L->num_elements, + .threads_per_block = L->threads_x, .grid_width = L->grid_x * L->grid_y, .threads_x = L->threads_x, .threads_y = threads_y, + .grid_x = L->grid_x, .grid_y = L->grid_y, .gpr_count = gpr, + .shared_bytes = L->shared_bytes, + }; + uint32_t qmd0_words[OMEGA_BW_QMD_WORDS], qmd1_words[OMEGA_BW_QMD_WORDS]; + omega_blackwell_build_qmd0(qmd0_words, qmd0_va, qmd1_va); + if (omega_blackwell_build_qmd1(qmd1_words, &cfg) != 0) { omega_gpu_session_set_error("qmd build (shared_bytes)"); return -1; } + if (omega_blackwell_verify_qmd_invariants(qmd1_words) != 0) { omega_gpu_session_set_error("qmd invariants"); return -1; } + memcpy(g_s.qmd.cpu, qmd0_words, sizeof qmd0_words); + memcpy((uint8_t *)g_s.qmd.cpu + 0x1000, qmd1_words, sizeof qmd1_words); + + volatile uint32_t *hsem = (volatile uint32_t *)((uint8_t *)g_s.qmd.cpu + 0x2000); + volatile uint32_t *hmarker = (volatile uint32_t *)g_s.marker.cpu; + volatile uint32_t *hmarker2 = (volatile uint32_t *)((uint8_t *)g_s.marker.cpu + 0x10); + *hsem = 0; *hmarker = 0; *hmarker2 = 0; + __asm__ volatile("dsb sy" ::: "memory"); + + uint32_t pb[1024]; + size_t n = 0; + memcpy(&pb[n], SETUP_WORDS, sizeof SETUP_WORDS); n += sizeof SETUP_WORDS / 4; + /* Invalidate the SM instruction, constant and data caches before this launch + * (cut 1b finding: a reused code address served stale instructions; clcec0.h + * NVCEC0_INVALIDATE_SHADER_CACHES 0x021c, bits INSTRUCTION 0, DATA 4, CONSTANT 12). */ + pb[n++] = nvrm_mthd(1, OMEGA_BW_MTHD_INVALIDATE_SHADER_CACHES, 1); pb[n++] = OMEGA_BW_INVALIDATE_SHADER_CACHES_ALL; + /* constant bank 0: driver words, then our arguments at 0x380 (inline upload) */ + pb[n++] = nvrm_mthd(1, 0x0188, 2); pb[n++] = (uint32_t)(g_s.cbank.va >> 32); pb[n++] = (uint32_t)g_s.cbank.va; + pb[n++] = nvrm_mthd(1, 0x0180, 2); pb[n++] = 0x00000380; pb[n++] = 0x00000001; + pb[n++] = nvrm_mthd(1, 0x01b0, 1); pb[n++] = 0x00000041; + pb[n++] = (224u << 16) | (1u << 13) | (0x01b4 >> 2) | (6u << 28); + memcpy(&pb[n], cbank_data, 224 * 4); n += 224; + pb[n++] = nvrm_mthd(1, 0x0188, 2); pb[n++] = (uint32_t)((g_s.cbank.va + 0x380) >> 32); pb[n++] = (uint32_t)(g_s.cbank.va + 0x380); + pb[n++] = nvrm_mthd(1, 0x0180, 2); pb[n++] = L->n_args * 4u; pb[n++] = 0x00000001; + pb[n++] = nvrm_mthd(1, 0x01b0, 1); pb[n++] = 0x00000041; + pb[n++] = (L->n_args << 16) | (1u << 13) | (0x01b4 >> 2) | (6u << 28); + memcpy(&pb[n], L->args, (size_t)L->n_args * 4u); n += L->n_args; + /* QMD0 (null grid, dependence 2) */ + pb[n++] = (98u << 16) | (1u << 13) | (0x0318 >> 2) | (2u << 28); + pb[n++] = (1u << 30) | (uint32_t)((qmd0_va >> 40) & 0x1ff); pb[n++] = (uint32_t)(qmd0_va >> 8); + memcpy(&pb[n], qmd0_words, 96 * 4); n += 96; + /* intermediate semaphore init */ + pb[n++] = nvrm_mthd(1, 0x0188, 2); pb[n++] = (uint32_t)(sem_va >> 32); pb[n++] = (uint32_t)sem_va; + pb[n++] = nvrm_mthd(1, 0x0180, 2); pb[n++] = 0x00000004; pb[n++] = 0x00000001; + pb[n++] = nvrm_mthd(1, 0x01b0, 1); pb[n++] = 0x00000041; + pb[n++] = (1u << 16) | (1u << 13) | (0x01b4 >> 2) | (6u << 28); + pb[n++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT; + /* QMD1 (the kernel) */ + pb[n++] = (98u << 16) | (1u << 13) | (0x0318 >> 2) | (2u << 28); + pb[n++] = (1u << 30) | (uint32_t)((qmd1_va >> 40) & 0x1ff); pb[n++] = (uint32_t)(qmd1_va >> 8); + memcpy(&pb[n], qmd1_words, 96 * 4); n += 96; + /* first marker */ + pb[n++] = nvrm_mthd(0, 0x005c, 5); + pb[n++] = (uint32_t)g_s.marker.va; pb[n++] = (uint32_t)(g_s.marker.va >> 32); + pb[n++] = OMEGA_BW_MARKER_COMPLETION_PAYLOAD; pb[n++] = 0; pb[n++] = 0x1 | (1u << 20); + /* C3 tail: L2_FLUSH_DIRTY, then the second release on the uncached marker page */ + pb[n++] = nvrm_mthd(0, 0x0028, 4); pb[n++] = 0; pb[n++] = 0; pb[n++] = 0; pb[n++] = (0x10u << 27); + pb[n++] = nvrm_mthd(0, 0x005c, 5); + pb[n++] = (uint32_t)(g_s.marker.va + 0x10); pb[n++] = (uint32_t)((g_s.marker.va + 0x10) >> 32); + pb[n++] = OMEGA_BW_MARKER2_PAYLOAD; pb[n++] = 0; pb[n++] = 0x1 | (1u << 20); + + uint64_t t0 = now_ns(); + if (m16_native_submit_methods(&g_s.ctx, pb, n) != 0) { fail_rm("submit_methods", &g_s.ctx.rm); return -1; } + if (session_wait(hmarker, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, L->timeout_ms, L->spin_us) != 0 || + session_wait(hmarker2, OMEGA_BW_MARKER2_PAYLOAD, L->timeout_ms, L->spin_us) != 0) { + g_blocked = true; /* uncertain completion: keep everything, refuse every later call */ + omega_gpu_session_set_error("marker wait timed out (latched)"); + return -1; + } + __asm__ volatile("dsb sy" ::: "memory"); + uint64_t t1 = now_ns(); + nvrm_retire(&g_s.ctx.rm, g_s.ctx.rm.put); /* both releases landed: the GPFIFO entry is consumed */ + if (*hsem != OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE) { omega_gpu_session_set_error("intermediate semaphore not DONE"); return -1; } + if (elapsed_ns) *elapsed_ns += t1 - t0; + if (marker_out) *marker_out = *hmarker; + return 0; +} + +uint32_t omega_gpu_session_open_count(void) { return g_s.opens; } diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/omega_gpu_session.h b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/omega_gpu_session.h new file mode 100644 index 000000000..b48faf52f --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/omega_gpu_session.h @@ -0,0 +1,129 @@ +#ifndef OMEGA_GPU_SESSION_H +#define OMEGA_GPU_SESSION_H +/* + * omega_gpu_session: the ONE persistent device for the native GPU APIs + * (FB-1 cut 4b, 2026-10-04). Shared by omega_gpu_matmul_api, + * omega_gpu_elementwise_api and omega_gpu_attention_api. + * + * Before this cut the matmul API kept its own device open for the process while + * the elementwise and attention APIs opened a client, device, VA space and + * channel for EVERY call and closed them again (~40 ms per call against ~0.1 ms + * of chip time; TinyLlama decode makes ~3500 elementwise calls per 32 tokens, + * sovereign-core cut 3c: 0.207 tok/s). Now: + * - one RM client / device / channel per process, opened on the first launch of + * any API and kept until omega_gpu_session_close(); + * - one pushbuffer, constant bank, marker page and QMD page, reused per launch; + * - staging buffers owned by the callers grow to a high-water mark and are reused + * (omega_gpu_session_scratch); + * - every buffer is GPU-uncached (nvos.h NVOS32_ATTR2_GPU_CACHEABLE, read + * 2026-10-04: "For system memory this will not be coherent with direct CPU + * mappings"; the default for system memory is uncached), because the host + * writes inputs the chip then reads, and polls the markers the chip writes; + * - every launch keeps the chip-proven sequence: shader-cache invalidate + * (clcec0.h NVCEC0_INVALIDATE_SHADER_CACHES, cut 1b finding), constant bank + * upload, QMD0 / QMD1, first marker, L2_FLUSH_DIRTY, second marker on the + * uncached page (C3 tail, omega#225/#230), GPFIFO retire after completion. + * - an uncertain completion (marker wait timed out) latches the process: nothing + * is freed or closed (the chip may still be running) and every later launch + * of every API is refused, as the engine does (omega_gpu_engine.h). + * + * Threading: one process-wide mutex. A caller takes omega_gpu_session_lock(), + * does its whole call (kernel lookup, staging, launches, readback) and unlocks. + * Calls from several threads therefore serialise on the device; that is the + * documented model (one channel, one GPFIFO). + * + * Diagnostics (cut 4b flake investigation): every failure names its stage + * (omega_gpu_session_last_error) and an open failure carries the driver text + * (Nvrm.err: RM class, ioctl errno, RM status) so a first-call failure is never + * reported as a bare CHIP_FAIL again. + */ +#include +#include +#include +#include "m16_native.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + M16NativeContext ctx; + NvrmMem pb, cbank, marker, qmd; /* per-launch scratch, owned by the session */ + uint32_t opens; /* device opens in this process */ + uint64_t last_open_ns; /* wall time of the last successful open */ +} OmegaGpuSession; + +/* A caller-owned staging buffer that grows to its high-water mark. */ +typedef struct { + NvrmMem mem; + size_t cap; /* usable bytes */ + size_t high_water; /* largest request so far */ +} OmegaGpuScratch; + +/* One launch: a kernel already resident at code_va, the argument words for + * constant bank 0 at 0x380 (buffer addresses then parameters), the grid. */ +typedef struct { + uint64_t code_va; + uint32_t gpr_count; /* registers the QMD declares (multiple of 8, >= 64 in practice) */ + uint32_t threads_x, threads_y; + uint32_t grid_x, grid_y; + size_t num_elements; /* QMD config num_elements (kept per API for identical words) */ + const uint32_t *args; /* n_args words written at cbank + 0x380 */ + uint32_t n_args; /* <= OMEGA_GPU_SESSION_MAX_ARGS */ + uint64_t timeout_ms; /* marker wait; past it the process latches */ + uint32_t shared_bytes; /* shared memory per CTA; 0 = the QMD default (1024) */ + uint32_t spin_us; /* poll the marker without sleeping for up to this long, then the usual + 50 us sleep-poll until timeout_ms; 0 = sleep-poll only (old behavior) */ +} OmegaGpuLaunch; +#define OMEGA_GPU_SESSION_MAX_SPIN_US 1000000u +#define OMEGA_GPU_SESSION_MAX_ARGS 32u + +/* Take the session lock. Never fails; does not touch the device. */ +void omega_gpu_session_lock(void); +void omega_gpu_session_unlock(void); + +/* Caller holds the lock. Opens the device on first use. NULL when the process is + * latched or the open failed (stage in omega_gpu_session_last_error). */ +OmegaGpuSession *omega_gpu_session_open(void); +bool omega_gpu_session_is_open(void); + +/* 1 once an uncertain completion has latched this process. */ +int omega_gpu_session_is_blocked(void); + +/* Caller holds the lock, device open. GPU-uncached allocation, page rounded. */ +int omega_gpu_session_alloc(size_t bytes, NvrmMem *out); +void omega_gpu_session_free(NvrmMem *m); /* no-op when latched or closed; zeroes *m */ + +/* Caller holds the lock, device open. Makes s->mem at least `bytes` long + * (reallocated when it grows; contents are not kept). */ +int omega_gpu_session_scratch(OmegaGpuScratch *s, size_t bytes); +void omega_gpu_session_scratch_free(OmegaGpuScratch *s); + +/* Caller holds the lock, device open, not latched. Submits one launch and waits + * for both markers. 0 on success; -1 with the stage named on failure. A marker + * timeout latches the process. elapsed_ns (submit to second marker) and the + * observed first marker word are added / stored when the pointers are given. */ +int omega_gpu_session_launch(const OmegaGpuLaunch *L, uint64_t *elapsed_ns, uint32_t *marker_out); + +/* Stage of the most recent failure ("" if none). Shared by every API. */ +const char *omega_gpu_session_last_error(void); +void omega_gpu_session_set_error(const char *stage); + +/* Caller holds the lock. Register a function the session calls (lock held, device + * still open) right before it closes, so an API can drop its resident kernels and + * scratch. Up to 8; the same function is registered once. (Taking the lock inside + * deadlocked the first call of every API on 2026-10-04: the APIs register from + * their open path, which already holds it.) */ +int omega_gpu_session_on_close(void (*hook)(void)); + +/* Close the device and free every session buffer. Takes the lock itself. + * Refused (no-op) after an uncertain completion. The next open reopens. */ +void omega_gpu_session_close(void); + +/* Device opens so far in this process (diagnostics; a persistent session shows 1). */ +uint32_t omega_gpu_session_open_count(void); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe.log b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe.log new file mode 100644 index 000000000..39dfff72d --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe.log @@ -0,0 +1,10 @@ +## negative_control (fd limit 4: the loader runs, /dev/nvidiactl cannot be opened) +PROBE label=negative_control pid=2700688 rc=CHIP_FAIL stage="nvrm_open: open control nodes errno 24" first_call_ms=0.1 chip_us=0.0 calls_ok=0/1 opens=0 +negative_control exit=1 (must be non-zero with a named stage) +## baseline N=10 +baseline: failures 0 / 10 +## back_to_back N=10 +back_to_back: failures 0 / 10 +## overlap N=10 +overlap: failures 0 / 10 +PROBE_GPU_OPEN: negative_control_exit=1 first_call_failures=0/30 (details /home/drakestapleton/workspace/omega-reconv/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/failures.txt) diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/SHA256SUMS b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/SHA256SUMS new file mode 100644 index 000000000..780283f74 --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/SHA256SUMS @@ -0,0 +1,8 @@ +9e1be089bf7bd93044a938c9ad72a3de42599c8d53317c3ecc5b18dc0dce2728 build.log +6f4e9e5ba150b22a10b1caf37aa4d6f903131963de7256448032b475e52b8879 device.txt +e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 failures.txt +2cf1630f99767bd7a9ab1e1f0940516dd3b534dd71906ef0a6c7d8e7ae0c63c8 gpu_session_probe.c +673650f936cb3b0a2f93ce09d81be10748b1b203c19e8176b4eefc1964a0cf3a ok_count.txt +f0b9caa347b2052c6d869506a041e5c94ff8f710bb84adddf884af1220a81116 omega_gpu_session.c +9068ccc6a7f0b9dd56048750f312739bc60fc56d2b6df4cfc81876aba7b05e49 probe_gpu_open.sh +98f7a2638602c63259579bedd946ad78058fcbae8f3f72f2af072ce1d8173a1f probe.log diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/build.log b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/build.log new file mode 100644 index 000000000..4021f52e3 --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/build.log @@ -0,0 +1,2 @@ +ar rcs build/libomega_gpu.a build/sha256.o build/omega_canonical.o build/omega_validate.o build/omega_core.o build/omega_codec.o build/aarch64_encoder.o build/aarch64_decoder.o build/omega_realize.o build/omega_exec.o build/omega_self_host.o build/omega_verify.o build/omega_program.o build/omega_synthesis.o build/omega_library.o build/omega_discovery.o build/omega_machine.o build/omega_realize_synth.o build/omega_matvec.o build/omega_accelerator.o build/omega_accelerator_world.o build/omega_vector.o build/omega_blackwell_encoder.o build/omega_blackwell_qmd.o build/omega_blackwell_realize.o build/omega_blackwell_submit.o build/omega_blackwell_engine.o build/omega_gpu_engine.o build/omega_blackwell_matmul.o build/omega_blackwell_codegen.o build/omega_gpu_session.o build/omega_gpu_matmul_api.o build/omega_gpu_elementwise_api.o build/omega_gpu_attention_api.o build/omega_gpu_wait.o build/omega_evidence.o build/m16_native.o build/nvrm.o build/omega_program_ir.o build/omega_vc_bridge.o build/omega_resolve.o build/omega_receipt.o build/omega_blake3.o build/omega_vcstore.o +cc -std=gnu11 -Wall -Wextra -Werror -MMD -MP -D_GNU_SOURCE -O2 -Isrc -I../physics-reconv-lock/m16 -I../physics-reconv-lock/nvrm -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/src/common/sdk/nvidia/inc -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/kernel-open/common/inc -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/kernel-open/nvidia-uvm -I../physics-reconv-lock/third_party/nvidia-open-580.173.02/src/nvidia/arch/nvalloc/unix/include -o build/gpu_session_probe tests/gpu_session_probe.c build/libomega_gpu.a -lpthread -lm diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/device.txt b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/device.txt new file mode 100644 index 000000000..5bbe5254a --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/device.txt @@ -0,0 +1,5 @@ +Linux spark-b87b 7.0.0-1019-nvidia #19~24.04.2-Ubuntu SMP PREEMPT_DYNAMIC Sun Sep 6 18:35:11 UTC 2 aarch64 aarch64 aarch64 GNU/Linux +NVIDIA GB10, 580.173.02 +omega 0a1812f44eaa3dcf8fe5a914d1dd65cfaccbbcd7 physics 6d7cf0d4d8eb2cda7b512100ff6058e25dbb3ddf +2026-10-05T23:15:34Z +end 2026-10-05T23:15:40Z diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/failures.txt b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/failures.txt new file mode 100644 index 000000000..e69de29bb diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/gpu_session_probe.c b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/gpu_session_probe.c new file mode 100644 index 000000000..dee34588b --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/gpu_session_probe.c @@ -0,0 +1,56 @@ +/* gpu_session_probe: FB-1 cut 4b flake investigation (first-call device-open failure). + * + * One fresh process = one sample: open the shared device through the API's own + * path (omega_gpu_ex2_f32 on 128 values is the smallest launch there is), report + * the stage that failed (with the driver's own text) or the open time, exit 0/1. + * + * ./gpu_session_probe [--label L] [--calls N] [--hold-ms M] + * --calls N launches after the first (default 1); keeps the device busy for an overlap test + * --hold-ms M sleep M ms with the device open before exit (overlap window for a second probe) + * + * Output (one line, Astra's SNAP shape; every unknown says so): + * PROBE label=L pid=P rc=OK|CHIP_FAIL|... stage="..." first_call_ms=F open_ms=O calls_ok=K/N opens=1 + * Never run outside the heavy queue (tools/probe_gpu_open.sh). + */ +#include "omega_gpu_elementwise_api.h" +#include "omega_gpu_session.h" +#include +#include +#include +#include +#include + +static double now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; } + +int main(int argc, char **argv) { + const char *label = "probe"; int calls = 1, hold_ms = 0; + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "--label") == 0 && i + 1 < argc) label = argv[++i]; + else if (strcmp(argv[i], "--calls") == 0 && i + 1 < argc) calls = atoi(argv[++i]); + else if (strcmp(argv[i], "--hold-ms") == 0 && i + 1 < argc) hold_ms = atoi(argv[++i]); + else { fprintf(stderr, "unknown flag %s\n", argv[i]); return 2; } + } + float x[128], y[128]; + for (int i = 0; i < 128; i++) { x[i] = (float)i / 16.0f - 4.0f; y[i] = 0.0f; } + OmegaGpuEwInfo info; memset(&info, 0, sizeof info); + double t0 = now_ms(); + int rc = omega_gpu_ex2_f32(128, x, y, &info); + double first_ms = now_ms() - t0; + int ok = 0; + if (rc == OMEGA_GPU_EW_OK) { + /* negative control on the result itself: 2^0 must be 1 (x[64] = 0) */ + if (y[64] < 0.999f || y[64] > 1.001f) { rc = OMEGA_GPU_EW_UNWRITTEN; omega_gpu_session_set_error("probe: 2^0 != 1"); } + else ok = 1; + } + int more_ok = 0; + for (int i = 1; i < calls && ok; i++) if (omega_gpu_ex2_f32(128, x, y, &info) == OMEGA_GPU_EW_OK) more_ok++; + if (hold_ms > 0) usleep((useconds_t)hold_ms * 1000u); + const char *stage = omega_gpu_elementwise_last_error(); + printf("PROBE label=%s pid=%d rc=%s stage=\"%s\" first_call_ms=%.1f chip_us=%.1f calls_ok=%d/%d opens=%u%s\n", + label, (int)getpid(), omega_gpu_elementwise_rc_name(rc), stage[0] ? stage : "-", first_ms, + info.elapsed_ns / 1e3, ok + more_ok, calls, omega_gpu_session_open_count(), + rc == OMEGA_GPU_EW_OK && !stage[0] ? "" : (rc == OMEGA_GPU_EW_OK ? " note=stage_text_from_earlier_failure" : "")); + fflush(stdout); + omega_gpu_session_close(); /* the explicit close is part of what the next process sees */ + return ok ? 0 : 1; +} diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/ok_count.txt b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/ok_count.txt new file mode 100644 index 000000000..425151f3a --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/ok_count.txt @@ -0,0 +1 @@ +40 diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/omega_gpu_session.c b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/omega_gpu_session.c new file mode 100644 index 000000000..29207d021 --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/omega_gpu_session.c @@ -0,0 +1,241 @@ +/* + * omega_gpu_session.c: FB-1 cut 4b. See the header for the contract. + * + * The launch words are the ones omega_gpu_matmul_api.c (cut 1b, chip PASS + * FB1-CUT1B-4345406) and omega_gpu_elementwise_api.c (cut 4, FB1-CUT4-0d0241a) + * both emitted; the two were identical except for the argument word count, which + * is now a parameter. Nothing about the sequence changed in this cut. + */ +#include "omega_gpu_session.h" +#include "omega_blackwell_qmd.h" +#include "omega_blackwell_submit.h" +#include "nvrm.h" +#include +#include +#include +#include + +#define PAGE 0x1000ull + +/* Same words as src/omega_blackwell_submit.c SETUP_WORDS (compute class setup). */ +static const uint32_t SETUP_WORDS[18] = { + 0x20012061, 0x0000cec0, 0x20012092, 0x00000001, 0x200120a8, 0x0000000f, 0x2001255d, 0x00000003, + 0x2001255e, 0x20000000, 0x2001255f, 0x000fffff, 0x20012557, 0x00000003, 0x20012558, 0x22000000, + 0x20012559, 0x00000000, +}; + +static pthread_mutex_t g_mu = PTHREAD_MUTEX_INITIALIZER; +static OmegaGpuSession g_s; +static bool g_open, g_blocked; +static char g_err[320] = ""; +static void (*g_hooks[8])(void); +static unsigned g_nhooks; + +static uint64_t now_ns(void) { + struct timespec t; + clock_gettime(CLOCK_MONOTONIC, &t); + return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec; +} + +/* Marker wait. With spin_us, the host first re-reads the uncached marker word back to back (with + * the ARM yield hint) so a launch that finishes inside the window is seen within about one read, + * instead of after a 50 us sleep that Linux timer slack stretches further. Past the window it is + * the same m16 sleep-poll as before, with the full timeout, so a stuck launch still latches. */ +static int session_wait(volatile uint32_t *m, uint32_t want, uint64_t timeout_ms, uint32_t spin_us) { + if (spin_us) { + const uint64_t end = now_ns() + (uint64_t)spin_us * 1000u; + do { + if ((int32_t)(*m - want) >= 0) return 0; + __asm__ volatile("yield" ::: "memory"); + } while (now_ns() < end); + } + return m16_native_wait_marker(m, want, timeout_ms); +} +static size_t page_up(size_t b) { b = (b + PAGE - 1) & ~(PAGE - 1); return b < PAGE ? PAGE : b; } + +const char *omega_gpu_session_last_error(void) { return g_err; } +void omega_gpu_session_set_error(const char *stage) { + snprintf(g_err, sizeof g_err, "%s", stage ? stage : ""); +} +static void fail_rm(const char *stage, const Nvrm *rm) { + /* the driver text (RM class / ioctl errno / RM status, nvrm.c fail()) travels with the stage */ + snprintf(g_err, sizeof g_err, "%s: %s", stage, rm && rm->err[0] ? rm->err : "(no driver text)"); +} + +void omega_gpu_session_lock(void) { pthread_mutex_lock(&g_mu); } +void omega_gpu_session_unlock(void) { pthread_mutex_unlock(&g_mu); } +bool omega_gpu_session_is_open(void) { return g_open; } +int omega_gpu_session_is_blocked(void) { return g_blocked ? 1 : 0; } + +int omega_gpu_session_alloc(size_t bytes, NvrmMem *out) { + if (!g_open || g_blocked) { omega_gpu_session_set_error("alloc without open device"); return -1; } + if (nvrm_alloc_gpu_uncached(&g_s.ctx.rm, page_up(bytes), out) != 0) { fail_rm("nvrm_alloc_gpu_uncached", &g_s.ctx.rm); memset(out, 0, sizeof *out); return -1; } + return 0; +} + +void omega_gpu_session_free(NvrmMem *m) { + if (m->cpu && g_open && !g_blocked) (void)nvrm_free(&g_s.ctx.rm, m); + memset(m, 0, sizeof *m); +} + +int omega_gpu_session_scratch(OmegaGpuScratch *s, size_t bytes) { + if (bytes > s->high_water) s->high_water = bytes; + if (s->cap >= bytes && s->mem.cpu) return 0; + omega_gpu_session_free(&s->mem); + s->cap = 0; + if (omega_gpu_session_alloc(s->high_water, &s->mem) != 0) return -1; + s->cap = (size_t)s->mem.size; + return 0; +} + +void omega_gpu_session_scratch_free(OmegaGpuScratch *s) { + omega_gpu_session_free(&s->mem); + s->cap = 0; +} + +static void free_session_buffers(void) { + omega_gpu_session_free(&g_s.qmd); omega_gpu_session_free(&g_s.marker); + omega_gpu_session_free(&g_s.cbank); omega_gpu_session_free(&g_s.pb); +} + +OmegaGpuSession *omega_gpu_session_open(void) { + if (g_blocked) { omega_gpu_session_set_error("process latched"); return NULL; } + if (g_open) return &g_s; + uint64_t t0 = now_ns(); + memset(&g_s.ctx, 0, sizeof g_s.ctx); + /* nvrm_open directly (m16_native_open is memset + nvrm_open + nvrm_close on + * failure) so the driver's own failure text survives into the stage name. */ + if (nvrm_open(&g_s.ctx.rm) != 0) { fail_rm("nvrm_open", &g_s.ctx.rm); (void)nvrm_close(&g_s.ctx.rm); return NULL; } + if (m16_native_create_channel(&g_s.ctx) != 0) { fail_rm("m16_native_create_channel", &g_s.ctx.rm); (void)m16_native_close(&g_s.ctx); return NULL; } + g_open = true; /* so alloc / free work during the rest of the open */ + if (omega_gpu_session_alloc(0x10000, &g_s.pb) || omega_gpu_session_alloc(PAGE, &g_s.cbank) || + omega_gpu_session_alloc(PAGE, &g_s.marker) || omega_gpu_session_alloc(0x10000, &g_s.qmd)) { + char keep[sizeof g_err]; memcpy(keep, g_err, sizeof keep); + free_session_buffers(); + g_open = false; + (void)m16_native_close(&g_s.ctx); + snprintf(g_err, sizeof g_err, "session scratch alloc (%s)", keep); + return NULL; + } + g_s.ctx.pb_mem = g_s.pb; /* m16_native_enqueue_methods copies the method stream here */ + g_s.opens++; + g_s.last_open_ns = now_ns() - t0; + return &g_s; +} + +int omega_gpu_session_on_close(void (*hook)(void)) { /* caller holds the lock (the APIs register from their open path) */ + if (!hook) return -1; + int rc = 0; + bool seen = false; + for (unsigned i = 0; i < g_nhooks; i++) if (g_hooks[i] == hook) seen = true; + if (!seen) { if (g_nhooks < sizeof g_hooks / sizeof g_hooks[0]) g_hooks[g_nhooks++] = hook; else rc = -1; } + return rc; +} + +void omega_gpu_session_close(void) { + pthread_mutex_lock(&g_mu); + if (g_open && !g_blocked) { + for (unsigned i = 0; i < g_nhooks; i++) g_hooks[i](); + free_session_buffers(); + (void)m16_native_close(&g_s.ctx); + g_open = false; + } + pthread_mutex_unlock(&g_mu); +} + +static uint32_t round_up(uint32_t v, uint32_t q) { return (v + q - 1) / q * q; } + +int omega_gpu_session_launch(const OmegaGpuLaunch *L, uint64_t *elapsed_ns, uint32_t *marker_out) { + if (!g_open || g_blocked) { omega_gpu_session_set_error(g_blocked ? "process latched" : "launch without open device"); return -1; } + if (!L || !L->args || L->n_args == 0 || L->n_args > OMEGA_GPU_SESSION_MAX_ARGS || L->threads_x == 0 || L->grid_x == 0 || L->grid_y == 0 || + L->spin_us > OMEGA_GPU_SESSION_MAX_SPIN_US) { + omega_gpu_session_set_error("launch arguments"); return -1; + } + const uint32_t threads_y = L->threads_y ? L->threads_y : 1; + uint32_t cbank_data[OMEGA_BW_CBANK_DRIVER_WORDS]; + omega_blackwell_build_cbank_driver_2d(cbank_data, g_s.cbank.va, L->threads_x, threads_y, L->grid_x, L->grid_y); + memcpy(g_s.cbank.cpu, cbank_data, sizeof cbank_data); + memcpy((uint8_t *)g_s.cbank.cpu + 0x380, L->args, (size_t)L->n_args * 4u); + + const uint64_t qmd0_va = g_s.qmd.va, qmd1_va = g_s.qmd.va + 0x1000, sem_va = g_s.qmd.va + 0x2000, scratch_va = g_s.qmd.va + 0x4000; + uint32_t gpr = L->gpr_count > 64u ? round_up(L->gpr_count, 8u) : 64u; + OmegaBlackwellQmdConfig cfg = { + .code_va = L->code_va, .cbank_va = g_s.cbank.va, .scratch_va = scratch_va, .sem_va = sem_va, + .qmd0_va = qmd0_va, .qmd1_va = qmd1_va, .num_elements = L->num_elements, + .threads_per_block = L->threads_x, .grid_width = L->grid_x * L->grid_y, .threads_x = L->threads_x, .threads_y = threads_y, + .grid_x = L->grid_x, .grid_y = L->grid_y, .gpr_count = gpr, + .shared_bytes = L->shared_bytes, + }; + uint32_t qmd0_words[OMEGA_BW_QMD_WORDS], qmd1_words[OMEGA_BW_QMD_WORDS]; + omega_blackwell_build_qmd0(qmd0_words, qmd0_va, qmd1_va); + if (omega_blackwell_build_qmd1(qmd1_words, &cfg) != 0) { omega_gpu_session_set_error("qmd build (shared_bytes)"); return -1; } + if (omega_blackwell_verify_qmd_invariants(qmd1_words) != 0) { omega_gpu_session_set_error("qmd invariants"); return -1; } + memcpy(g_s.qmd.cpu, qmd0_words, sizeof qmd0_words); + memcpy((uint8_t *)g_s.qmd.cpu + 0x1000, qmd1_words, sizeof qmd1_words); + + volatile uint32_t *hsem = (volatile uint32_t *)((uint8_t *)g_s.qmd.cpu + 0x2000); + volatile uint32_t *hmarker = (volatile uint32_t *)g_s.marker.cpu; + volatile uint32_t *hmarker2 = (volatile uint32_t *)((uint8_t *)g_s.marker.cpu + 0x10); + *hsem = 0; *hmarker = 0; *hmarker2 = 0; + __asm__ volatile("dsb sy" ::: "memory"); + + uint32_t pb[1024]; + size_t n = 0; + memcpy(&pb[n], SETUP_WORDS, sizeof SETUP_WORDS); n += sizeof SETUP_WORDS / 4; + /* Invalidate the SM instruction, constant and data caches before this launch + * (cut 1b finding: a reused code address served stale instructions; clcec0.h + * NVCEC0_INVALIDATE_SHADER_CACHES 0x021c, bits INSTRUCTION 0, DATA 4, CONSTANT 12). */ + pb[n++] = nvrm_mthd(1, OMEGA_BW_MTHD_INVALIDATE_SHADER_CACHES, 1); pb[n++] = OMEGA_BW_INVALIDATE_SHADER_CACHES_ALL; + /* constant bank 0: driver words, then our arguments at 0x380 (inline upload) */ + pb[n++] = nvrm_mthd(1, 0x0188, 2); pb[n++] = (uint32_t)(g_s.cbank.va >> 32); pb[n++] = (uint32_t)g_s.cbank.va; + pb[n++] = nvrm_mthd(1, 0x0180, 2); pb[n++] = 0x00000380; pb[n++] = 0x00000001; + pb[n++] = nvrm_mthd(1, 0x01b0, 1); pb[n++] = 0x00000041; + pb[n++] = (224u << 16) | (1u << 13) | (0x01b4 >> 2) | (6u << 28); + memcpy(&pb[n], cbank_data, 224 * 4); n += 224; + pb[n++] = nvrm_mthd(1, 0x0188, 2); pb[n++] = (uint32_t)((g_s.cbank.va + 0x380) >> 32); pb[n++] = (uint32_t)(g_s.cbank.va + 0x380); + pb[n++] = nvrm_mthd(1, 0x0180, 2); pb[n++] = L->n_args * 4u; pb[n++] = 0x00000001; + pb[n++] = nvrm_mthd(1, 0x01b0, 1); pb[n++] = 0x00000041; + pb[n++] = (L->n_args << 16) | (1u << 13) | (0x01b4 >> 2) | (6u << 28); + memcpy(&pb[n], L->args, (size_t)L->n_args * 4u); n += L->n_args; + /* QMD0 (null grid, dependence 2) */ + pb[n++] = (98u << 16) | (1u << 13) | (0x0318 >> 2) | (2u << 28); + pb[n++] = (1u << 30) | (uint32_t)((qmd0_va >> 40) & 0x1ff); pb[n++] = (uint32_t)(qmd0_va >> 8); + memcpy(&pb[n], qmd0_words, 96 * 4); n += 96; + /* intermediate semaphore init */ + pb[n++] = nvrm_mthd(1, 0x0188, 2); pb[n++] = (uint32_t)(sem_va >> 32); pb[n++] = (uint32_t)sem_va; + pb[n++] = nvrm_mthd(1, 0x0180, 2); pb[n++] = 0x00000004; pb[n++] = 0x00000001; + pb[n++] = nvrm_mthd(1, 0x01b0, 1); pb[n++] = 0x00000041; + pb[n++] = (1u << 16) | (1u << 13) | (0x01b4 >> 2) | (6u << 28); + pb[n++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT; + /* QMD1 (the kernel) */ + pb[n++] = (98u << 16) | (1u << 13) | (0x0318 >> 2) | (2u << 28); + pb[n++] = (1u << 30) | (uint32_t)((qmd1_va >> 40) & 0x1ff); pb[n++] = (uint32_t)(qmd1_va >> 8); + memcpy(&pb[n], qmd1_words, 96 * 4); n += 96; + /* first marker */ + pb[n++] = nvrm_mthd(0, 0x005c, 5); + pb[n++] = (uint32_t)g_s.marker.va; pb[n++] = (uint32_t)(g_s.marker.va >> 32); + pb[n++] = OMEGA_BW_MARKER_COMPLETION_PAYLOAD; pb[n++] = 0; pb[n++] = 0x1 | (1u << 20); + /* C3 tail: L2_FLUSH_DIRTY, then the second release on the uncached marker page */ + pb[n++] = nvrm_mthd(0, 0x0028, 4); pb[n++] = 0; pb[n++] = 0; pb[n++] = 0; pb[n++] = (0x10u << 27); + pb[n++] = nvrm_mthd(0, 0x005c, 5); + pb[n++] = (uint32_t)(g_s.marker.va + 0x10); pb[n++] = (uint32_t)((g_s.marker.va + 0x10) >> 32); + pb[n++] = OMEGA_BW_MARKER2_PAYLOAD; pb[n++] = 0; pb[n++] = 0x1 | (1u << 20); + + uint64_t t0 = now_ns(); + if (m16_native_submit_methods(&g_s.ctx, pb, n) != 0) { fail_rm("submit_methods", &g_s.ctx.rm); return -1; } + if (session_wait(hmarker, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, L->timeout_ms, L->spin_us) != 0 || + session_wait(hmarker2, OMEGA_BW_MARKER2_PAYLOAD, L->timeout_ms, L->spin_us) != 0) { + g_blocked = true; /* uncertain completion: keep everything, refuse every later call */ + omega_gpu_session_set_error("marker wait timed out (latched)"); + return -1; + } + __asm__ volatile("dsb sy" ::: "memory"); + uint64_t t1 = now_ns(); + nvrm_retire(&g_s.ctx.rm, g_s.ctx.rm.put); /* both releases landed: the GPFIFO entry is consumed */ + if (*hsem != OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE) { omega_gpu_session_set_error("intermediate semaphore not DONE"); return -1; } + if (elapsed_ns) *elapsed_ns += t1 - t0; + if (marker_out) *marker_out = *hmarker; + return 0; +} + +uint32_t omega_gpu_session_open_count(void) { return g_s.opens; } diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/probe.log b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/probe.log new file mode 100644 index 000000000..05aacb2df --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/probe.log @@ -0,0 +1,49 @@ +## negative_control (fd limit 4: the loader runs, /dev/nvidiactl cannot be opened) +PROBE label=negative_control pid=2700688 rc=CHIP_FAIL stage="nvrm_open: open control nodes errno 24" first_call_ms=0.1 chip_us=0.0 calls_ok=0/1 opens=0 +negative_control exit=1 (must be non-zero with a named stage) +## baseline N=10 +PROBE label=baseline#1 pid=2700695 rc=OK stage="-" first_call_ms=27.5 chip_us=112.5 calls_ok=1/1 opens=1 +PROBE label=baseline#2 pid=2700699 rc=OK stage="-" first_call_ms=27.6 chip_us=109.8 calls_ok=1/1 opens=1 +PROBE label=baseline#3 pid=2700704 rc=OK stage="-" first_call_ms=29.3 chip_us=110.2 calls_ok=1/1 opens=1 +PROBE label=baseline#4 pid=2700717 rc=OK stage="-" first_call_ms=29.5 chip_us=219.8 calls_ok=1/1 opens=1 +PROBE label=baseline#5 pid=2700723 rc=OK stage="-" first_call_ms=28.4 chip_us=110.7 calls_ok=1/1 opens=1 +PROBE label=baseline#6 pid=2700727 rc=OK stage="-" first_call_ms=27.4 chip_us=112.2 calls_ok=1/1 opens=1 +PROBE label=baseline#7 pid=2700731 rc=OK stage="-" first_call_ms=27.6 chip_us=109.3 calls_ok=1/1 opens=1 +PROBE label=baseline#8 pid=2700735 rc=OK stage="-" first_call_ms=28.1 chip_us=112.3 calls_ok=1/1 opens=1 +PROBE label=baseline#9 pid=2700741 rc=OK stage="-" first_call_ms=28.3 chip_us=113.4 calls_ok=1/1 opens=1 +PROBE label=baseline#10 pid=2700757 rc=OK stage="-" first_call_ms=27.3 chip_us=109.1 calls_ok=1/1 opens=1 +baseline: failures 0 / 10 +## back_to_back N=10 +PROBE label=back_to_back#1 pid=2700766 rc=OK stage="-" first_call_ms=29.6 chip_us=212.7 calls_ok=1/1 opens=1 +PROBE label=back_to_back#2 pid=2700776 rc=OK stage="-" first_call_ms=27.2 chip_us=113.6 calls_ok=1/1 opens=1 +PROBE label=back_to_back#3 pid=2700781 rc=OK stage="-" first_call_ms=27.5 chip_us=111.6 calls_ok=1/1 opens=1 +PROBE label=back_to_back#4 pid=2700784 rc=OK stage="-" first_call_ms=27.3 chip_us=112.9 calls_ok=1/1 opens=1 +PROBE label=back_to_back#5 pid=2700787 rc=OK stage="-" first_call_ms=28.9 chip_us=113.0 calls_ok=1/1 opens=1 +PROBE label=back_to_back#6 pid=2700790 rc=OK stage="-" first_call_ms=27.2 chip_us=113.6 calls_ok=1/1 opens=1 +PROBE label=back_to_back#7 pid=2700793 rc=OK stage="-" first_call_ms=27.7 chip_us=113.8 calls_ok=1/1 opens=1 +PROBE label=back_to_back#8 pid=2700796 rc=OK stage="-" first_call_ms=29.3 chip_us=114.6 calls_ok=1/1 opens=1 +PROBE label=back_to_back#9 pid=2700799 rc=OK stage="-" first_call_ms=26.6 chip_us=111.7 calls_ok=1/1 opens=1 +PROBE label=back_to_back#10 pid=2700802 rc=OK stage="-" first_call_ms=28.4 chip_us=109.6 calls_ok=1/1 opens=1 +back_to_back: failures 0 / 10 +## overlap N=10 +PROBE label=overlap#1 pid=2700814 rc=OK stage="-" first_call_ms=27.8 chip_us=213.1 calls_ok=1/1 opens=1 +PROBE label=holder pid=2700810 rc=OK stage="-" first_call_ms=29.3 chip_us=211.4 calls_ok=1/1 opens=1 +PROBE label=overlap#2 pid=2700819 rc=OK stage="-" first_call_ms=27.4 chip_us=213.2 calls_ok=1/1 opens=1 +PROBE label=holder pid=2700817 rc=OK stage="-" first_call_ms=27.7 chip_us=110.2 calls_ok=1/1 opens=1 +PROBE label=overlap#3 pid=2700826 rc=OK stage="-" first_call_ms=29.6 chip_us=220.7 calls_ok=1/1 opens=1 +PROBE label=holder pid=2700824 rc=OK stage="-" first_call_ms=29.2 chip_us=108.4 calls_ok=1/1 opens=1 +PROBE label=overlap#4 pid=2700834 rc=OK stage="-" first_call_ms=29.5 chip_us=212.2 calls_ok=1/1 opens=1 +PROBE label=holder pid=2700832 rc=OK stage="-" first_call_ms=28.9 chip_us=109.5 calls_ok=1/1 opens=1 +PROBE label=overlap#5 pid=2700839 rc=OK stage="-" first_call_ms=29.8 chip_us=210.7 calls_ok=1/1 opens=1 +PROBE label=holder pid=2700837 rc=OK stage="-" first_call_ms=27.1 chip_us=108.4 calls_ok=1/1 opens=1 +PROBE label=overlap#6 pid=2700844 rc=OK stage="-" first_call_ms=28.3 chip_us=212.8 calls_ok=1/1 opens=1 +PROBE label=holder pid=2700842 rc=OK stage="-" first_call_ms=27.0 chip_us=107.9 calls_ok=1/1 opens=1 +PROBE label=overlap#7 pid=2700851 rc=OK stage="-" first_call_ms=28.2 chip_us=211.1 calls_ok=1/1 opens=1 +PROBE label=holder pid=2700849 rc=OK stage="-" first_call_ms=27.1 chip_us=108.5 calls_ok=1/1 opens=1 +PROBE label=overlap#8 pid=2700865 rc=OK stage="-" first_call_ms=29.8 chip_us=222.2 calls_ok=1/1 opens=1 +PROBE label=holder pid=2700854 rc=OK stage="-" first_call_ms=27.1 chip_us=110.1 calls_ok=1/1 opens=1 +PROBE label=overlap#9 pid=2700870 rc=OK stage="-" first_call_ms=29.4 chip_us=209.6 calls_ok=1/1 opens=1 +PROBE label=holder pid=2700868 rc=OK stage="-" first_call_ms=27.1 chip_us=108.8 calls_ok=1/1 opens=1 +PROBE label=overlap#10 pid=2700887 rc=OK stage="-" first_call_ms=29.5 chip_us=214.8 calls_ok=1/1 opens=1 +PROBE label=holder pid=2700885 rc=OK stage="-" first_call_ms=27.2 chip_us=110.1 calls_ok=1/1 opens=1 +overlap: failures 0 / 10 diff --git a/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/probe_gpu_open.sh b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/probe_gpu_open.sh new file mode 100755 index 000000000..30d061389 --- /dev/null +++ b/evidence/gpu-reconv-308/T3/FB1-CUT4B-0a1812f/probe/probe_gpu_open.sh @@ -0,0 +1,49 @@ +#!/bin/bash +# tools/probe_gpu_open.sh OUT_DIR [N] +# FB-1 cut 4b flake investigation: does the FIRST chip call of a fresh process fail +# in the device-open path (seen twice: cut 1b run 2, cut 3c attempt 1)? Each sample +# is a fresh process (build/gpu_session_probe). Variants, N samples each, in order: +# baseline one probe at a time, idle gap 200 ms between processes +# back_to_back the next probe starts the instant the previous one exits (the sighting: +# "another chip job had just used the chip") +# overlap a holder keeps a device open (hold 300 ms) while the probe opens its own +# after_latch NOT RUN (would need a deliberate stall; never leave the chip uncertain) +# Negative control first: the probe must report a failure when it cannot open the +# control nodes (file-descriptor limit 4 -> open() fails; limit 3 stops the dynamic loader itself, exit 127, which proves nothing), with the stage named. +# Run through the heavy queue only (lanes.sh queue --heavy); never kill it. +set -u +OUT=${1:?out dir}; N=${2:-30} +cd "$(dirname "$0")/.." || exit 2 +P=${PHYSICS_DIR:-../physics} +mkdir -p "$OUT" +make PHYSICS_DIR="$P" build/gpu_session_probe > "$OUT/build.log" 2>&1 || { echo "PROBE BUILD FAIL"; exit 1; } +PR=./build/gpu_session_probe +LOG="$OUT/probe.log"; : > "$LOG" +{ uname -a; nvidia-smi --query-gpu=name,driver_version --format=csv,noheader 2>/dev/null; echo "omega $(git rev-parse HEAD) physics $(git -C "$P" rev-parse HEAD)"; date -u +%FT%TZ; } > "$OUT/device.txt" +echo "## negative_control (fd limit 4: the loader runs, /dev/nvidiactl cannot be opened)" | tee -a "$LOG" +( ulimit -n 4; $PR --label negative_control ) 2>&1 | tee -a "$LOG" +neg_rc=${PIPESTATUS[0]} +echo "negative_control exit=$neg_rc (must be non-zero with a named stage)" | tee -a "$LOG" +fails=0; total=0 +run_variant() { # name + local name=$1 f=0 + echo "## $name N=$N" | tee -a "$LOG" + for i in $(seq 1 "$N"); do + if [ "$name" = overlap ]; then $PR --label holder --hold-ms 300 >> "$LOG" 2>&1 & sleep 0.05; fi + $PR --label "$name#$i" 2>&1 | tee -a "$LOG" | grep -q 'rc=OK' || f=$((f+1)) + [ "$name" = overlap ] && wait + [ "$name" = baseline ] && sleep 0.2 + done + echo "$name: failures $f / $N" | tee -a "$LOG" + fails=$((fails+f)); total=$((total+N)) +} +run_variant baseline +run_variant back_to_back +run_variant overlap +grep -c 'rc=OK' "$LOG" > "$OUT/ok_count.txt" +grep 'rc=' "$LOG" | grep -v 'rc=OK' | grep -v negative_control > "$OUT/failures.txt" +echo "end $(date -u +%FT%TZ)" >> "$OUT/device.txt" +cp tests/gpu_session_probe.c src/omega_gpu_session.c tools/probe_gpu_open.sh "$OUT/" +(cd "$OUT" && sha256sum -- * > SHA256SUMS) +echo "PROBE_GPU_OPEN: negative_control_exit=$neg_rc first_call_failures=$fails/$total (details $OUT/failures.txt)" +[ "$neg_rc" != 0 ] || { echo "PROBE_GPU_OPEN: INSTRUMENT FAIL (negative control passed)"; exit 3; } diff --git a/evidence/gpu-reconv-308/T3b-prime-race-gb10-chip/0754f68c1d47a29e5655a6524ef56d94737dfa54afde608db4bebd3926da3412.json b/evidence/gpu-reconv-308/T3b-prime-race-gb10-chip/0754f68c1d47a29e5655a6524ef56d94737dfa54afde608db4bebd3926da3412.json new file mode 100644 index 000000000..d32099250 --- /dev/null +++ b/evidence/gpu-reconv-308/T3b-prime-race-gb10-chip/0754f68c1d47a29e5655a6524ef56d94737dfa54afde608db4bebd3926da3412.json @@ -0,0 +1,23 @@ +{ + "schema": "aien-prime-race/gb10-chip-test/v1", + "verdict": "PASS", + "reason": "", + "omega_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", + "physics_commit": "6d7cf0d4d8eb2cda7b512100ff6058e25dbb3ddf", + "started": "2026-10-05T23:15:52Z", + "finished": "2026-10-05T23:16:52Z", + "binary_sha256": "ed32d37f17026bdbba3cfc03587878a2569609bbd0d4e1b3a3f26c4997592b78", + "mutant_binary_sha256": "b58275943481bcd1cd9253c76acc9f93f7d02766192996e97343e3ef31d75ad3", + "kernel_code_sha256": "8ff80476574a13272c81df93c0bd60f2b000cdab9811f60183c86c2965cb3b50", + "host_test_pass": true, + "audit_runs": 586, + "audit_passes_checked": 1172, + "timed_smoke_pass": true, + "mutant_caught": true, + "gpu": "NVIDIA GB10, GPU-fb8e53f0-3207-e89b-d02f-acdfe2695b59, 580.173.02", + "kernel": "7.0.0-1019-nvidia", + "gpu_lock": "/tmp/aien-gb10.lock", + "quiet_flag": "not touched (operator window)", + "reference": "independent Eratosthenes oracle (gb10_sieve_host_test --oracle), byte-for-byte per pass", + "log_blob": "2e03cad03b572392dea383df556d755fd25ab89524228cfd441c0c962d025d5f" +} diff --git a/evidence/gpu-reconv-308/T3b-prime-race-gb10-chip/blobs/2e03cad03b572392dea383df556d755fd25ab89524228cfd441c0c962d025d5f.log b/evidence/gpu-reconv-308/T3b-prime-race-gb10-chip/blobs/2e03cad03b572392dea383df556d755fd25ab89524228cfd441c0c962d025d5f.log new file mode 100644 index 000000000..25f621a07 --- /dev/null +++ b/evidence/gpu-reconv-308/T3b-prime-race-gb10-chip/blobs/2e03cad03b572392dea383df556d755fd25ab89524228cfd441c0c962d025d5f.log @@ -0,0 +1,44 @@ +kernel: 71 IR insns, 72 encoded words, 29 GPRs, sha256 813279aa5a034f0a902f055d7a81ad19da002d499e2e01dcfa4ad765cb4fac43 +nvdisasm (global table): 72 words, 0 undecodable, 6 branch targets checked (/tmp/pr_gb10_sieve_2702252.sass) +fixed RAW: 0 hazards; negative controls ALU=4: 1, predicate=13: 1 +scoreboard: 0 hazards; negative control (one wait dropped) found 6 +simulator: 155 limits, 0 differ, 930830759 IR steps +budget sweep: 16 cases (budgets 1, 2, 128, 256), 0 differ +staged kernel: 85 IR insns, 29 GPRs, sha256 8ff80476574a13272c81df93c0bd60f2b000cdab9811f60183c86c2965cb3b50 +nvdisasm (staged): 88 words, 0 undecodable, 8 branch targets checked (/tmp/pr_gb10_sieve_2702252.sass) +fixed RAW: 0 hazards; negative controls ALU=4: 1, predicate=13: 1 +staged simulator: 167 cases, 0 differ, races seen 0 +negative control, barrier removed: rc -1 err 6 (6 = race) +negative control, shared memory one entry short: rc -1 err 5 (5 = out of bounds) +warp-sim model C5 (global) 1e6: warp_issues=2892925 lane_util=0.774 splits=163 max_frags=11 +warp-sim model C5 (staged) 1e6: warp_issues=3452511 lane_util=0.652 splits=224 max_frags=11 bar_split_arrivals=128 +div1 kernel: 67 IR insns, 26 GPRs, 1 regions, sha256 be5aa2e4673a9f31acca298ed0f64c286a8663e9cb444bf17b132c07ed92010b +nvdisasm (div1): 72 words, 0 undecodable, 6 branch targets checked (/tmp/pr_gb10_sieve_2702252.sass) +warp-sim model div1 1e6: warp_issues=2533736 lane_util=0.656 splits=67346 max_frags=12 peak_bars=1 bar_split_arrivals=0 +div1 warp simulator: 322 cases (both orders), 0 differ +div1 mutant 2: warp simulator says BSSY_REARM +div1 mutant 3: warp simulator says JOIN_MISMATCH +shared_div1 kernel: 83 IR insns, 26 GPRs, 2 regions, sha256 ad14a9c538b8d1b9e3c07af78a0a855c0c98a7fb5147e3253ab8889f9e1a24b9 +warp-sim model shared_div1 1e6: warp_issues=2544552 lane_util=0.657 splits=67410 max_frags=12 peak_bars=1 bar_split_arrivals=0 +shared_div1 warp simulator: 322 cases (both orders), 0 differ +shared_div1 mutant 2: warp simulator says BSSY_REARM +shared_div1 mutant 3: warp simulator says JOIN_MISMATCH +div2 kernel: 69 IR insns, 26 GPRs, 2 regions, sha256 822e7fa3c5c18bfa1e0d5af968f3e03606299fd7af447c223c041d9a7d2684c1 +nvdisasm (div2): 72 words, 0 undecodable, 6 branch targets checked (/tmp/pr_gb10_sieve_2702252.sass) +warp-sim model div2 1e6: warp_issues=2005051 lane_util=0.830 splits=57765 max_frags=12 peak_bars=2 bar_split_arrivals=0 +div2 warp simulator: 322 cases (both orders), 0 differ +div2 mutant 2: warp simulator says BSSY_REARM +div2 mutant 3: warp simulator says JOIN_MISMATCH +shared_div2 kernel: 85 IR insns, 26 GPRs, 3 regions, sha256 070d829ddf8d75613af5f85d6c6b7ded5019f026d669bc07d877452349f5634b +warp-sim model shared_div2 1e6: warp_issues=2015867 lane_util=0.830 splits=57829 max_frags=12 peak_bars=2 bar_split_arrivals=0 +shared_div2 warp simulator: 322 cases (both orders), 0 differ +shared_div2 mutant 2: warp simulator says BSSY_REARM +shared_div2 mutant 3: warp simulator says JOIN_MISMATCH +magic model limit 1000000: 1838283 word/prime pairs, 0 needed the correction, 0 wrong +checks=172 failed=0 +VERDICT PASS +GPU lock held from 2026-10-05T23:16:25Z +audit sweep: 585 runs, 1172 passes checked +timed smoke: aien-gb10-native;9725;2.000007;8192;algorithm=other,faithful=no,bits=1 +limit 1000000: bitmap differs from oracle: /tmp/tmp.VJsNCsBkWT/got.bin /tmp/tmp.VJsNCsBkWT/want.bin differ: byte 1, line 1 +mutant caught at 1e6 diff --git a/evidence/gpu-reconv-308/T4/divergent-sweep.log b/evidence/gpu-reconv-308/T4/divergent-sweep.log new file mode 100644 index 000000000..26601bfae --- /dev/null +++ b/evidence/gpu-reconv-308/T4/divergent-sweep.log @@ -0,0 +1,2 @@ +{"mark":"divergent","kernel":"ad14a9c538b8d1b9e3c07af78a0a855c0c98a7fb5147e3253ab8889f9e1a24b9","insns":null} +MARK=divergent runs=585 mismatches_or_failures=0 PASS diff --git a/evidence/gpu-reconv-308/T4/divergent/rep-1e6.json b/evidence/gpu-reconv-308/T4/divergent/rep-1e6.json new file mode 100644 index 000000000..54b62a029 --- /dev/null +++ b/evidence/gpu-reconv-308/T4/divergent/rep-1e6.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 0.000000, + "mode": "audit", + "passes": 5, + "elapsed_s": 0.000000, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "divergent", "kernel_code_sha256": "ad14a9c538b8d1b9e3c07af78a0a855c0c98a7fb5147e3253ab8889f9e1a24b9", "kernel_insns": 88, "kernel_gpr": 26, "mutant": 0, "launches": 5, "diag_kernel_ns_total": 641333, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/divergent2-sweep.log b/evidence/gpu-reconv-308/T4/divergent2-sweep.log new file mode 100644 index 000000000..8767c2b09 --- /dev/null +++ b/evidence/gpu-reconv-308/T4/divergent2-sweep.log @@ -0,0 +1,2 @@ +{"mark":"divergent2","kernel":"070d829ddf8d75613af5f85d6c6b7ded5019f026d669bc07d877452349f5634b","insns":null} +MARK=divergent2 runs=585 mismatches_or_failures=0 PASS diff --git a/evidence/gpu-reconv-308/T4/divergent2/rep-1e6.json b/evidence/gpu-reconv-308/T4/divergent2/rep-1e6.json new file mode 100644 index 000000000..b0385ae22 --- /dev/null +++ b/evidence/gpu-reconv-308/T4/divergent2/rep-1e6.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 0.000000, + "mode": "audit", + "passes": 5, + "elapsed_s": 0.000000, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "divergent2", "kernel_code_sha256": "070d829ddf8d75613af5f85d6c6b7ded5019f026d669bc07d877452349f5634b", "kernel_insns": 88, "kernel_gpr": 26, "mutant": 0, "launches": 5, "diag_kernel_ns_total": 657171, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf-divergent-1.json b/evidence/gpu-reconv-308/T4/perf-divergent-1.json new file mode 100644 index 000000000..6770a1454 --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf-divergent-1.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 5.000000, + "mode": "timed", + "passes": 45775, + "elapsed_s": 5.000109, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "divergent", "kernel_code_sha256": "ad14a9c538b8d1b9e3c07af78a0a855c0c98a7fb5147e3253ab8889f9e1a24b9", "kernel_insns": 88, "kernel_gpr": 26, "mutant": 0, "launches": 45775, "diag_kernel_ns_total": 4737851083, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf-divergent-2.json b/evidence/gpu-reconv-308/T4/perf-divergent-2.json new file mode 100644 index 000000000..ea55fbda6 --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf-divergent-2.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 5.000000, + "mode": "timed", + "passes": 45735, + "elapsed_s": 5.000087, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "divergent", "kernel_code_sha256": "ad14a9c538b8d1b9e3c07af78a0a855c0c98a7fb5147e3253ab8889f9e1a24b9", "kernel_insns": 88, "kernel_gpr": 26, "mutant": 0, "launches": 45735, "diag_kernel_ns_total": 4738901885, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf-divergent-3.json b/evidence/gpu-reconv-308/T4/perf-divergent-3.json new file mode 100644 index 000000000..4ac018888 --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf-divergent-3.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 5.000000, + "mode": "timed", + "passes": 45673, + "elapsed_s": 5.000078, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "divergent", "kernel_code_sha256": "ad14a9c538b8d1b9e3c07af78a0a855c0c98a7fb5147e3253ab8889f9e1a24b9", "kernel_insns": 88, "kernel_gpr": 26, "mutant": 0, "launches": 45673, "diag_kernel_ns_total": 4736214726, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf-divergent-4.json b/evidence/gpu-reconv-308/T4/perf-divergent-4.json new file mode 100644 index 000000000..f53f4988c --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf-divergent-4.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 5.000000, + "mode": "timed", + "passes": 45633, + "elapsed_s": 5.000031, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "divergent", "kernel_code_sha256": "ad14a9c538b8d1b9e3c07af78a0a855c0c98a7fb5147e3253ab8889f9e1a24b9", "kernel_insns": 88, "kernel_gpr": 26, "mutant": 0, "launches": 45633, "diag_kernel_ns_total": 4731279080, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf-divergent-5.json b/evidence/gpu-reconv-308/T4/perf-divergent-5.json new file mode 100644 index 000000000..12c377403 --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf-divergent-5.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 5.000000, + "mode": "timed", + "passes": 45790, + "elapsed_s": 5.000100, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "divergent", "kernel_code_sha256": "ad14a9c538b8d1b9e3c07af78a0a855c0c98a7fb5147e3253ab8889f9e1a24b9", "kernel_insns": 88, "kernel_gpr": 26, "mutant": 0, "launches": 45790, "diag_kernel_ns_total": 4741623009, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf-divergent-6.json b/evidence/gpu-reconv-308/T4/perf-divergent-6.json new file mode 100644 index 000000000..bf8060881 --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf-divergent-6.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 5.000000, + "mode": "timed", + "passes": 45537, + "elapsed_s": 5.000076, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "divergent", "kernel_code_sha256": "ad14a9c538b8d1b9e3c07af78a0a855c0c98a7fb5147e3253ab8889f9e1a24b9", "kernel_insns": 88, "kernel_gpr": 26, "mutant": 0, "launches": 45537, "diag_kernel_ns_total": 4726255927, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf-divergent-7.json b/evidence/gpu-reconv-308/T4/perf-divergent-7.json new file mode 100644 index 000000000..8b1970874 --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf-divergent-7.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 5.000000, + "mode": "timed", + "passes": 45564, + "elapsed_s": 5.000073, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "divergent", "kernel_code_sha256": "ad14a9c538b8d1b9e3c07af78a0a855c0c98a7fb5147e3253ab8889f9e1a24b9", "kernel_insns": 88, "kernel_gpr": 26, "mutant": 0, "launches": 45564, "diag_kernel_ns_total": 4727113310, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf-divergent-8.json b/evidence/gpu-reconv-308/T4/perf-divergent-8.json new file mode 100644 index 000000000..3507dce01 --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf-divergent-8.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 5.000000, + "mode": "timed", + "passes": 45808, + "elapsed_s": 5.000046, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "divergent", "kernel_code_sha256": "ad14a9c538b8d1b9e3c07af78a0a855c0c98a7fb5147e3253ab8889f9e1a24b9", "kernel_insns": 88, "kernel_gpr": 26, "mutant": 0, "launches": 45808, "diag_kernel_ns_total": 4723789151, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf-divergent2-1.json b/evidence/gpu-reconv-308/T4/perf-divergent2-1.json new file mode 100644 index 000000000..d4020d90b --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf-divergent2-1.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 5.000000, + "mode": "timed", + "passes": 45462, + "elapsed_s": 5.000021, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "divergent2", "kernel_code_sha256": "070d829ddf8d75613af5f85d6c6b7ded5019f026d669bc07d877452349f5634b", "kernel_insns": 88, "kernel_gpr": 26, "mutant": 0, "launches": 45462, "diag_kernel_ns_total": 4721406522, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf-divergent2-2.json b/evidence/gpu-reconv-308/T4/perf-divergent2-2.json new file mode 100644 index 000000000..73b8c6b6d --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf-divergent2-2.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 5.000000, + "mode": "timed", + "passes": 46237, + "elapsed_s": 5.000020, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "divergent2", "kernel_code_sha256": "070d829ddf8d75613af5f85d6c6b7ded5019f026d669bc07d877452349f5634b", "kernel_insns": 88, "kernel_gpr": 26, "mutant": 0, "launches": 46237, "diag_kernel_ns_total": 4771014339, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf-divergent2-3.json b/evidence/gpu-reconv-308/T4/perf-divergent2-3.json new file mode 100644 index 000000000..7415b131c --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf-divergent2-3.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 5.000000, + "mode": "timed", + "passes": 45594, + "elapsed_s": 5.000067, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "divergent2", "kernel_code_sha256": "070d829ddf8d75613af5f85d6c6b7ded5019f026d669bc07d877452349f5634b", "kernel_insns": 88, "kernel_gpr": 26, "mutant": 0, "launches": 45594, "diag_kernel_ns_total": 4729664922, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf-divergent2-4.json b/evidence/gpu-reconv-308/T4/perf-divergent2-4.json new file mode 100644 index 000000000..050145a83 --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf-divergent2-4.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 5.000000, + "mode": "timed", + "passes": 46313, + "elapsed_s": 5.000040, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "divergent2", "kernel_code_sha256": "070d829ddf8d75613af5f85d6c6b7ded5019f026d669bc07d877452349f5634b", "kernel_insns": 88, "kernel_gpr": 26, "mutant": 0, "launches": 46313, "diag_kernel_ns_total": 4765656158, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf-divergent2-5.json b/evidence/gpu-reconv-308/T4/perf-divergent2-5.json new file mode 100644 index 000000000..935d025f1 --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf-divergent2-5.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 5.000000, + "mode": "timed", + "passes": 45021, + "elapsed_s": 5.000062, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "divergent2", "kernel_code_sha256": "070d829ddf8d75613af5f85d6c6b7ded5019f026d669bc07d877452349f5634b", "kernel_insns": 88, "kernel_gpr": 26, "mutant": 0, "launches": 45021, "diag_kernel_ns_total": 4693258941, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf-divergent2-6.json b/evidence/gpu-reconv-308/T4/perf-divergent2-6.json new file mode 100644 index 000000000..cd75d9a95 --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf-divergent2-6.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 5.000000, + "mode": "timed", + "passes": 45407, + "elapsed_s": 5.000013, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "divergent2", "kernel_code_sha256": "070d829ddf8d75613af5f85d6c6b7ded5019f026d669bc07d877452349f5634b", "kernel_insns": 88, "kernel_gpr": 26, "mutant": 0, "launches": 45407, "diag_kernel_ns_total": 4716242964, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf-divergent2-7.json b/evidence/gpu-reconv-308/T4/perf-divergent2-7.json new file mode 100644 index 000000000..27d2c610c --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf-divergent2-7.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 5.000000, + "mode": "timed", + "passes": 45628, + "elapsed_s": 5.000022, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "divergent2", "kernel_code_sha256": "070d829ddf8d75613af5f85d6c6b7ded5019f026d669bc07d877452349f5634b", "kernel_insns": 88, "kernel_gpr": 26, "mutant": 0, "launches": 45628, "diag_kernel_ns_total": 4729255671, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf-divergent2-8.json b/evidence/gpu-reconv-308/T4/perf-divergent2-8.json new file mode 100644 index 000000000..25c2b40ae --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf-divergent2-8.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 5.000000, + "mode": "timed", + "passes": 45913, + "elapsed_s": 5.000037, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "divergent2", "kernel_code_sha256": "070d829ddf8d75613af5f85d6c6b7ded5019f026d669bc07d877452349f5634b", "kernel_insns": 88, "kernel_gpr": 26, "mutant": 0, "launches": 45913, "diag_kernel_ns_total": 4727085651, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf-uniform-1.json b/evidence/gpu-reconv-308/T4/perf-uniform-1.json new file mode 100644 index 000000000..7404405f0 --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf-uniform-1.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 5.000000, + "mode": "timed", + "passes": 38093, + "elapsed_s": 5.000080, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "uniform", "kernel_code_sha256": "8ff80476574a13272c81df93c0bd60f2b000cdab9811f60183c86c2965cb3b50", "kernel_insns": 88, "kernel_gpr": 29, "mutant": 0, "launches": 38093, "diag_kernel_ns_total": 4768214060, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf-uniform-2.json b/evidence/gpu-reconv-308/T4/perf-uniform-2.json new file mode 100644 index 000000000..5d7ecbb9a --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf-uniform-2.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 5.000000, + "mode": "timed", + "passes": 37653, + "elapsed_s": 5.000119, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "uniform", "kernel_code_sha256": "8ff80476574a13272c81df93c0bd60f2b000cdab9811f60183c86c2965cb3b50", "kernel_insns": 88, "kernel_gpr": 29, "mutant": 0, "launches": 37653, "diag_kernel_ns_total": 4797881369, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf-uniform-3.json b/evidence/gpu-reconv-308/T4/perf-uniform-3.json new file mode 100644 index 000000000..84f58346c --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf-uniform-3.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 5.000000, + "mode": "timed", + "passes": 26063, + "elapsed_s": 5.000009, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "uniform", "kernel_code_sha256": "8ff80476574a13272c81df93c0bd60f2b000cdab9811f60183c86c2965cb3b50", "kernel_insns": 88, "kernel_gpr": 29, "mutant": 0, "launches": 26063, "diag_kernel_ns_total": 4854336896, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf-uniform-4.json b/evidence/gpu-reconv-308/T4/perf-uniform-4.json new file mode 100644 index 000000000..b873312f1 --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf-uniform-4.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 5.000000, + "mode": "timed", + "passes": 31768, + "elapsed_s": 5.000056, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "uniform", "kernel_code_sha256": "8ff80476574a13272c81df93c0bd60f2b000cdab9811f60183c86c2965cb3b50", "kernel_insns": 88, "kernel_gpr": 29, "mutant": 0, "launches": 31768, "diag_kernel_ns_total": 4804156930, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf-uniform-5.json b/evidence/gpu-reconv-308/T4/perf-uniform-5.json new file mode 100644 index 000000000..4c7f37645 --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf-uniform-5.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 5.000000, + "mode": "timed", + "passes": 34572, + "elapsed_s": 5.000099, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "uniform", "kernel_code_sha256": "8ff80476574a13272c81df93c0bd60f2b000cdab9811f60183c86c2965cb3b50", "kernel_insns": 88, "kernel_gpr": 29, "mutant": 0, "launches": 34572, "diag_kernel_ns_total": 4765281473, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf-uniform-6.json b/evidence/gpu-reconv-308/T4/perf-uniform-6.json new file mode 100644 index 000000000..df9b13aa0 --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf-uniform-6.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 5.000000, + "mode": "timed", + "passes": 37426, + "elapsed_s": 5.000090, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "uniform", "kernel_code_sha256": "8ff80476574a13272c81df93c0bd60f2b000cdab9811f60183c86c2965cb3b50", "kernel_insns": 88, "kernel_gpr": 29, "mutant": 0, "launches": 37426, "diag_kernel_ns_total": 4815020063, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf-uniform-7.json b/evidence/gpu-reconv-308/T4/perf-uniform-7.json new file mode 100644 index 000000000..9694ae085 --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf-uniform-7.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 5.000000, + "mode": "timed", + "passes": 39680, + "elapsed_s": 5.000104, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "uniform", "kernel_code_sha256": "8ff80476574a13272c81df93c0bd60f2b000cdab9811f60183c86c2965cb3b50", "kernel_insns": 88, "kernel_gpr": 29, "mutant": 0, "launches": 39680, "diag_kernel_ns_total": 4761801708, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf-uniform-8.json b/evidence/gpu-reconv-308/T4/perf-uniform-8.json new file mode 100644 index 000000000..d5d43ad77 --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf-uniform-8.json @@ -0,0 +1,19 @@ +{ + "schema": "aien-prime-race/impl-report/v1", + "impl": "gb10-native", + "label": "aien-gb10-native", + "algorithm": "other", + "faithful": "no", + "bits": 1, + "threads": 8192, + "environment": "linux-hosted-gb10-native", + "limit": 1000000, + "min_seconds": 5.000000, + "mode": "timed", + "passes": 34800, + "elapsed_s": 5.000028, + "status": "COMPLETE", + "error": null, + "build": {"source_commit": "7b4ced852ef89aecffe7a69401ff3efab9a713f8", "cc": "cc", "cflags": "-O3 -mcpu=native -Ibench/prime_race + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"}, + "detail": {"grid": 64, "cta_budget": 64, "block": 128, "words": 15625, "nprimes": 167, "table": "shared", "shared_bytes": 2016, "spin_us": 0, "mark": "uniform", "kernel_code_sha256": "8ff80476574a13272c81df93c0bd60f2b000cdab9811f60183c86c2965cb3b50", "kernel_insns": 88, "kernel_gpr": 29, "mutant": 0, "launches": 34800, "diag_kernel_ns_total": 4788964699, "diag_note": "sum of session elapsed_ns (submit to second marker), diagnostic only"} +} diff --git a/evidence/gpu-reconv-308/T4/perf64-repeat.log b/evidence/gpu-reconv-308/T4/perf64-repeat.log new file mode 100644 index 000000000..aabc62c1a --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf64-repeat.log @@ -0,0 +1,15 @@ +r4 uniform aien-gb10-native;31768;5.000056;8192;algorithm=other,faithful=no,bits=1 +r4 divergent aien-gb10-native;45633;5.000031;8192;algorithm=other,faithful=no,bits=1 +r4 divergent2 aien-gb10-native;46313;5.000040;8192;algorithm=other,faithful=no,bits=1 +r5 uniform aien-gb10-native;34572;5.000099;8192;algorithm=other,faithful=no,bits=1 +r5 divergent aien-gb10-native;45790;5.000100;8192;algorithm=other,faithful=no,bits=1 +r5 divergent2 aien-gb10-native;45021;5.000062;8192;algorithm=other,faithful=no,bits=1 +r6 uniform aien-gb10-native;37426;5.000090;8192;algorithm=other,faithful=no,bits=1 +r6 divergent aien-gb10-native;45537;5.000076;8192;algorithm=other,faithful=no,bits=1 +r6 divergent2 aien-gb10-native;45407;5.000013;8192;algorithm=other,faithful=no,bits=1 +r7 uniform aien-gb10-native;39680;5.000104;8192;algorithm=other,faithful=no,bits=1 +r7 divergent aien-gb10-native;45564;5.000073;8192;algorithm=other,faithful=no,bits=1 +r7 divergent2 aien-gb10-native;45628;5.000022;8192;algorithm=other,faithful=no,bits=1 +r8 uniform aien-gb10-native;34800;5.000028;8192;algorithm=other,faithful=no,bits=1 +r8 divergent aien-gb10-native;45808;5.000046;8192;algorithm=other,faithful=no,bits=1 +r8 divergent2 aien-gb10-native;45913;5.000037;8192;algorithm=other,faithful=no,bits=1 diff --git a/evidence/gpu-reconv-308/T4/perf64.log b/evidence/gpu-reconv-308/T4/perf64.log new file mode 100644 index 000000000..c404dda1e --- /dev/null +++ b/evidence/gpu-reconv-308/T4/perf64.log @@ -0,0 +1,18 @@ +== run 1 mark=uniform +aien-gb10-native;38093;5.000080;8192;algorithm=other,faithful=no,bits=1 +== run 1 mark=divergent +aien-gb10-native;45775;5.000109;8192;algorithm=other,faithful=no,bits=1 +== run 1 mark=divergent2 +aien-gb10-native;45462;5.000021;8192;algorithm=other,faithful=no,bits=1 +== run 2 mark=uniform +aien-gb10-native;37653;5.000119;8192;algorithm=other,faithful=no,bits=1 +== run 2 mark=divergent +aien-gb10-native;45735;5.000087;8192;algorithm=other,faithful=no,bits=1 +== run 2 mark=divergent2 +aien-gb10-native;46237;5.000020;8192;algorithm=other,faithful=no,bits=1 +== run 3 mark=uniform +aien-gb10-native;26063;5.000009;8192;algorithm=other,faithful=no,bits=1 +== run 3 mark=divergent +aien-gb10-native;45673;5.000078;8192;algorithm=other,faithful=no,bits=1 +== run 3 mark=divergent2 +aien-gb10-native;45594;5.000067;8192;algorithm=other,faithful=no,bits=1 diff --git a/evidence/gpu-reconv-308/oracle/cf.cu b/evidence/gpu-reconv-308/oracle/cf.cu new file mode 100644 index 000000000..9957be546 --- /dev/null +++ b/evidence/gpu-reconv-308/oracle/cf.cu @@ -0,0 +1,25 @@ +// Offline encoding/placement oracle for omega #308 (BSSY/BSYNC). Never linked into AIEN. +extern "C" __global__ void k_diamond(unsigned *o, const unsigned *a) { + unsigned x = a[threadIdx.x], y; + if (x & 1) y = x * 3u + 7u; else { y = x >> 2; y = y * y + 1u; y ^= 0x5a5a; } + y += __shfl_down_sync(0xffffffffu, y, 1); + o[threadIdx.x] = y; +} +extern "C" __global__ void k_loop(unsigned *o, const unsigned *a) { + unsigned j = a[threadIdx.x], p = a[threadIdx.x + 64], acc = 0; + while (j < 32) { acc |= 1u << j; j += p; } + acc += __shfl_down_sync(0xffffffffu, acc, 1); + o[threadIdx.x] = acc; +} +extern "C" __global__ void k_nested(unsigned *o, const unsigned *a) { + unsigned x = a[threadIdx.x], acc = 0; + if (x & 1) { unsigned j = x >> 1; while (j < 32) { acc += j; j += (x & 7) + 1; } } + acc += __shfl_down_sync(0xffffffffu, acc, 1); + __syncthreads(); + o[threadIdx.x] = acc; +} +extern "C" __global__ void k_break(unsigned *o, const unsigned *a) { + unsigned x = a[threadIdx.x], acc = 0; + for (unsigned i = 0; i < 64; i++) { acc += a[i]; if (acc > x) break; } + o[threadIdx.x] = acc + __shfl_down_sync(0xffffffffu, acc, 1); +} diff --git a/evidence/gpu-reconv-308/oracle/cf.sass b/evidence/gpu-reconv-308/oracle/cf.sass new file mode 100644 index 000000000..946471405 --- /dev/null +++ b/evidence/gpu-reconv-308/oracle/cf.sass @@ -0,0 +1,360 @@ + + code for sm_121 + .target sm_121 + + Function : k_break + .headerflags @"EF_CUDA_SM121 EF_CUDA_VIRTUAL_SM(EF_CUDA_SM121)" + /*0000*/ LDC R1, c[0x0][0x37c] ; /* 0x0000df00ff017b82 */ + /* 0x000fe20000000800 */ + /*0010*/ S2R R11, SR_TID.X ; /* 0x00000000000b7919 */ + /* 0x000e2e0000002100 */ + /*0020*/ LDC.64 R4, c[0x0][0x388] ; /* 0x0000e200ff047b82 */ + /* 0x000e220000000a00 */ + /*0030*/ LDCU.64 UR4, c[0x0][0x358] ; /* 0x00006b00ff0477ac */ + /* 0x000e620008000a00 */ + /*0040*/ IMAD.WIDE.U32 R2, R11, 0x4, R4 ; /* 0x000000040b027825 */ + /* 0x001fca00078e0004 */ + /*0050*/ LDG.E R0, desc[UR4][R2.64] ; /* 0x0000000402007981 */ + /* 0x002162000c1e1900 */ + /*0060*/ HFMA2 R7, -RZ, RZ, 0, 0 ; /* 0x00000000ff077431 */ + /* 0x000fe200000001ff */ + /*0070*/ BSSY.RECONVERGENT B0, 0x110 ; /* 0x0000009000007945 */ + /* 0x000fe20003800200 */ + /*0080*/ MOV R9, RZ ; /* 0x000000ff00097202 */ + /* 0x000fce0000000f00 */ + /*0090*/ IMAD.WIDE.U32 R2, R7, 0x4, R4 ; /* 0x0000000407027825 */ + /* 0x001fcc00078e0004 */ + /*00a0*/ LDG.E R2, desc[UR4][R2.64] ; /* 0x0000000402027981 */ + /* 0x000ea2000c1e1900 */ + /*00b0*/ ISETP.GE.U32.AND P0, PT, R7.reuse, 0x3f, PT ; /* 0x0000003f0700780c */ + /* 0x040fe20003f06070 */ + /*00c0*/ IADD3 R7, PT, PT, R7, 0x1, RZ ; /* 0x0000000107077810 */ + /* 0x000fe40007ffe0ff */ + /*00d0*/ IADD3 R9, PT, PT, R2, R9, RZ ; /* 0x0000000902097210 */ + /* 0x004fca0007ffe0ff */ + /*00e0*/ ISETP.LE.U32.AND P1, PT, R9, R0, PT ; /* 0x000000000900720c */ + /* 0x020fda0003f23070 */ + /*00f0*/ @!P0 BRA P1, 0x90 ; /* 0xfffffffc00e48947 */ + /* 0x000fea000083ffff */ + /*0100*/ BSYNC.RECONVERGENT B0 ; /* 0x0000000000007941 */ + /* 0x000fea0003800200 */ + /*0110*/ SHFL.DOWN PT, R0, R9, 0x1, 0x1f ; /* 0x08201f0009007f89 */ + /* 0x000e2200000e0000 */ + /*0120*/ LDC.64 R2, c[0x0][0x380] ; /* 0x0000e000ff027b82 */ + /* 0x000e620000000a00 */ + /*0130*/ IADD3 R5, PT, PT, R9, R0, RZ ; /* 0x0000000009057210 */ + /* 0x001fe20007ffe0ff */ + /*0140*/ IMAD.WIDE.U32 R2, R11, 0x4, R2 ; /* 0x000000040b027825 */ + /* 0x002fca00078e0002 */ + /*0150*/ STG.E desc[UR4][R2.64], R5 ; /* 0x0000000502007986 */ + /* 0x000fe2000c101904 */ + /*0160*/ EXIT ; /* 0x000000000000794d */ + /* 0x000fea0003800000 */ + /*0170*/ BRA 0x170; /* 0xfffffffc00fc7947 */ + /* 0x000fc0000383ffff */ + /*0180*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*0190*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*01a0*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*01b0*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*01c0*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*01d0*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*01e0*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*01f0*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + .......... + + + Function : k_nested + .headerflags @"EF_CUDA_SM121 EF_CUDA_VIRTUAL_SM(EF_CUDA_SM121)" + /*0000*/ LDC R1, c[0x0][0x37c] ; /* 0x0000df00ff017b82 */ + /* 0x000fe20000000800 */ + /*0010*/ S2R R5, SR_TID.X ; /* 0x0000000000057919 */ + /* 0x000e2e0000002100 */ + /*0020*/ LDC.64 R6, c[0x0][0x388] ; /* 0x0000e200ff067b82 */ + /* 0x000e220000000a00 */ + /*0030*/ LDCU.64 UR4, c[0x0][0x358] ; /* 0x00006b00ff0477ac */ + /* 0x000e6e0008000a00 */ + /*0040*/ LDC.64 R2, c[0x0][0x380] ; /* 0x0000e000ff027b82 */ + /* 0x000ea20000000a00 */ + /*0050*/ IMAD.WIDE.U32 R6, R5, 0x4, R6 ; /* 0x0000000405067825 */ + /* 0x001fcc00078e0006 */ + /*0060*/ LDG.E R6, desc[UR4][R6.64] ; /* 0x0000000406067981 */ + /* 0x002ee2000c1e1900 */ + /*0070*/ BSSY.RECONVERGENT B0, 0x300 ; /* 0x0000028000007945 */ + /* 0x000fe20003800200 */ + /*0080*/ LOP3.LUT R0, R6, 0xffffffc1, RZ, 0xc0, !PT ; /* 0xffffffc106007812 */ + /* 0x008fca00078ec0ff */ + /*0090*/ ISETP.NE.AND P0, PT, R0, 0x1, PT ; /* 0x000000010000780c */ + /* 0x000fe20003f05270 */ + /*00a0*/ HFMA2 R0, -RZ, RZ, 0, 0 ; /* 0x00000000ff007431 */ + /* 0x000fd800000001ff */ + /*00b0*/ @P0 BRA 0x2f0 ; /* 0x00000000008c0947 */ + /* 0x004fea0003800000 */ + /*00c0*/ LOP3.LUT R9, R6, 0x7, RZ, 0xc0, !PT ; /* 0x0000000706097812 */ + /* 0x000fe400078ec0ff */ + /*00d0*/ SHF.R.U32.HI R0, RZ, 0x1, R6 ; /* 0x00000001ff007819 */ + /* 0x000fe40000011606 */ + /*00e0*/ IADD3 R11, PT, PT, R9, 0x1, RZ ; /* 0x00000001090b7810 */ + /* 0x000fe40007ffe0ff */ + /*00f0*/ IADD3 R4, PT, PT, R0.reuse, R9, RZ ; /* 0x0000000900047210 */ + /* 0x040fe40007ffe0ff */ + /*0100*/ I2F.U32.RP R8, R11 ; /* 0x0000000b00087306 */ + /* 0x000e220000209000 */ + /*0110*/ IADD3 R13, PT, PT, R0, R11.reuse, RZ ; /* 0x0000000b000d7210 */ + /* 0x080fe40007ffe0ff */ + /*0120*/ IADD3 R17, PT, PT, RZ, -R11, RZ ; /* 0x8000000bff117210 */ + /* 0x000fe20007ffe0ff */ + /*0130*/ ISETP.NE.U32.AND P2, PT, R11, RZ, PT ; /* 0x000000ff0b00720c */ + /* 0x000fc40003f45070 */ + /*0140*/ VIMNMX.U32 R6, R13, 0x20, !PT ; /* 0x000000200d067848 */ + /* 0x000fca0007fe0000 */ + /*0150*/ IADD3 R15, PT, PT, R6, -0x1, RZ ; /* 0xffffffff060f7810 */ + /* 0x000fe20007ffe0ff */ + /*0160*/ MOV R6, RZ ; /* 0x000000ff00067202 */ + /* 0x000fe20000000f00 */ + /*0170*/ MUFU.RCP R8, R8 ; /* 0x0000000800087308 */ + /* 0x001e260000001000 */ + /*0180*/ ISETP.NE.AND P0, PT, R15, R4, PT ; /* 0x000000040f00720c */ + /* 0x000fe20003f05270 */ + /*0190*/ IADD3 R7, PT, PT, R8, 0xffffffe, RZ ; /* 0x0ffffffe08077810 */ + /* 0x001fc80007ffe0ff */ + /*01a0*/ SEL R8, RZ, 0x1, !P0 ; /* 0x00000001ff087807 */ + /* 0x000fe40004000000 */ + /*01b0*/ F2I.FTZ.U32.TRUNC.NTZ R7, R7 ; /* 0x0000000700077305 */ + /* 0x000e26000021f000 */ + /*01c0*/ IADD3 R4, PT, PT, R15, -R4, -R8 ; /* 0x800000040f047210 */ + /* 0x000fe20007ffe808 */ + /*01d0*/ IMAD R17, R17, R7, RZ ; /* 0x0000000711117224 */ + /* 0x001fc800078e02ff */ + /*01e0*/ IMAD.HI.U32 R17, R7, R17, R6 ; /* 0x0000001107117227 */ + /* 0x000fcc00078e0006 */ + /*01f0*/ IMAD.HI.U32 R17, R17, R4, RZ ; /* 0x0000000411117227 */ + /* 0x000fca00078e00ff */ + /*0200*/ IADD3 R6, PT, PT, -R17, RZ, RZ ; /* 0x000000ff11067210 */ + /* 0x000fca0007ffe1ff */ + /*0210*/ IMAD R4, R11, R6, R4 ; /* 0x000000060b047224 */ + /* 0x000fca00078e0204 */ + /*0220*/ ISETP.GE.U32.AND P0, PT, R4, R11, PT ; /* 0x0000000b0400720c */ + /* 0x000fda0003f06070 */ + /*0230*/ @P0 IADD3 R4, PT, PT, -R11, R4, RZ ; /* 0x000000040b040210 */ + /* 0x000fe40007ffe1ff */ + /*0240*/ @P0 IADD3 R17, PT, PT, R17, 0x1, RZ ; /* 0x0000000111110810 */ + /* 0x000fc60007ffe0ff */ + /*0250*/ ISETP.GE.U32.AND P1, PT, R4, R11, PT ; /* 0x0000000b0400720c */ + /* 0x000fda0003f26070 */ + /*0260*/ @P1 IADD3 R17, PT, PT, R17, 0x1, RZ ; /* 0x0000000111111810 */ + /* 0x000fe40007ffe0ff */ + /*0270*/ @!P2 LOP3.LUT R17, RZ, R11, RZ, 0x33, !PT ; /* 0x0000000bff11a212 */ + /* 0x000fc800078e33ff */ + /*0280*/ IADD3 R8, PT, PT, R8, R17, RZ ; /* 0x0000001108087210 */ + /* 0x000fc80007ffe0ff */ + /*0290*/ IADD3 R7, PT, PT, R8, -0x1, RZ ; /* 0xffffffff08077810 */ + /* 0x000fe20007ffe0ff */ + /*02a0*/ IMAD R0, R13, R8, R0 ; /* 0x000000080d007224 */ + /* 0x000fc800078e0200 */ + /*02b0*/ IMAD.WIDE.U32 R6, R7, R8, RZ ; /* 0x0000000807067225 */ + /* 0x000fca00078e00ff */ + /*02c0*/ SHF.R.U64 R6, R6, 0x1, R7 ; /* 0x0000000106067819 */ + /* 0x000fca0000001207 */ + /*02d0*/ IMAD R9, R9, R6, R6 ; /* 0x0000000609097224 */ + /* 0x000fca00078e0206 */ + /*02e0*/ IADD3 R0, PT, PT, R0, R9, RZ ; /* 0x0000000900007210 */ + /* 0x000fce0007ffe0ff */ + /*02f0*/ BSYNC.RECONVERGENT B0 ; /* 0x0000000000007941 */ + /* 0x000fea0003800200 */ + /*0300*/ SHFL.DOWN PT, R7, R0, 0x1, 0x1f ; /* 0x08201f0000077f89 */ + /* 0x000e2200000e0000 */ + /*0310*/ IMAD.WIDE.U32 R2, R5, 0x4, R2 ; /* 0x0000000405027825 */ + /* 0x000fe200078e0002 */ + /*0320*/ BAR.SYNC.DEFER_BLOCKING 0x0 ; /* 0x0000000000007b1d */ + /* 0x000fe20000010000 */ + /*0330*/ IADD3 R7, PT, PT, R7, R0, RZ ; /* 0x0000000007077210 */ + /* 0x001fca0007ffe0ff */ + /*0340*/ STG.E desc[UR4][R2.64], R7 ; /* 0x0000000702007986 */ + /* 0x000fe2000c101904 */ + /*0350*/ EXIT ; /* 0x000000000000794d */ + /* 0x000fea0003800000 */ + /*0360*/ BRA 0x360; /* 0xfffffffc00fc7947 */ + /* 0x000fc0000383ffff */ + /*0370*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*0380*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*0390*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*03a0*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*03b0*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*03c0*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*03d0*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*03e0*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*03f0*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + .......... + + + Function : k_loop + .headerflags @"EF_CUDA_SM121 EF_CUDA_VIRTUAL_SM(EF_CUDA_SM121)" + /*0000*/ LDC R1, c[0x0][0x37c] ; /* 0x0000df00ff017b82 */ + /* 0x000fe20000000800 */ + /*0010*/ S2R R9, SR_TID.X ; /* 0x0000000000097919 */ + /* 0x000e2e0000002100 */ + /*0020*/ LDC.64 R2, c[0x0][0x388] ; /* 0x0000e200ff027b82 */ + /* 0x000e220000000a00 */ + /*0030*/ LDCU.64 UR4, c[0x0][0x358] ; /* 0x00006b00ff0477ac */ + /* 0x000e6e0008000a00 */ + /*0040*/ LDC.64 R4, c[0x0][0x380] ; /* 0x0000e000ff047b82 */ + /* 0x000ea20000000a00 */ + /*0050*/ IMAD.WIDE.U32 R2, R9, 0x4, R2 ; /* 0x0000000409027825 */ + /* 0x001fca00078e0002 */ + /*0060*/ LDG.E R0, desc[UR4][R2.64] ; /* 0x0000000402007981 */ + /* 0x002ee2000c1e1900 */ + /*0070*/ BSSY.RECONVERGENT B0, 0x140 ; /* 0x000000c000007945 */ + /* 0x000fe20003800200 */ + /*0080*/ HFMA2 R6, -RZ, RZ, 0, 0 ; /* 0x00000000ff067431 */ + /* 0x000fe200000001ff */ + /*0090*/ ISETP.GT.U32.AND P0, PT, R0, 0x1f, PT ; /* 0x0000001f0000780c */ + /* 0x008fda0003f04070 */ + /*00a0*/ @P0 BRA 0x130 ; /* 0x0000000000200947 */ + /* 0x004fea0003800000 */ + /*00b0*/ LDG.E R2, desc[UR4][R2.64+0x100] ; /* 0x0001000402027981 */ + /* 0x000162000c1e1900 */ + /*00c0*/ MOV R6, RZ ; /* 0x000000ff00067202 */ + /* 0x000fe20000000f00 */ + /*00d0*/ MOV R7, 0x1 ; /* 0x0000000100077802 */ + /* 0x000fce0000000f00 */ + /*00e0*/ SHF.L.U32 R3, R7, R0.reuse, RZ ; /* 0x0000000007037219 */ + /* 0x081fe400000006ff */ + /*00f0*/ IADD3 R0, PT, PT, R2, R0, RZ ; /* 0x0000000002007210 */ + /* 0x020fe40007ffe0ff */ + /*0100*/ LOP3.LUT R6, R6, R3, RZ, 0xfc, !PT ; /* 0x0000000306067212 */ + /* 0x000fc600078efcff */ + /*0110*/ ISETP.GE.U32.AND P0, PT, R0, 0x20, PT ; /* 0x000000200000780c */ + /* 0x000fda0003f06070 */ + /*0120*/ @!P0 BRA 0xe0 ; /* 0xfffffffc00ec8947 */ + /* 0x000fea000383ffff */ + /*0130*/ BSYNC.RECONVERGENT B0 ; /* 0x0000000000007941 */ + /* 0x000fea0003800200 */ + /*0140*/ SHFL.DOWN PT, R7, R6, 0x1, 0x1f ; /* 0x08201f0006077f89 */ + /* 0x000e2200000e0000 */ + /*0150*/ IMAD.WIDE.U32 R2, R9, 0x4, R4 ; /* 0x0000000409027825 */ + /* 0x000fe200078e0004 */ + /*0160*/ IADD3 R7, PT, PT, R7, R6, RZ ; /* 0x0000000607077210 */ + /* 0x001fca0007ffe0ff */ + /*0170*/ STG.E desc[UR4][R2.64], R7 ; /* 0x0000000702007986 */ + /* 0x000fe2000c101904 */ + /*0180*/ EXIT ; /* 0x000000000000794d */ + /* 0x000fea0003800000 */ + /*0190*/ BRA 0x190; /* 0xfffffffc00fc7947 */ + /* 0x000fc0000383ffff */ + /*01a0*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*01b0*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*01c0*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*01d0*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*01e0*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*01f0*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*0200*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*0210*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*0220*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*0230*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*0240*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*0250*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*0260*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*0270*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + .......... + + + Function : k_diamond + .headerflags @"EF_CUDA_SM121 EF_CUDA_VIRTUAL_SM(EF_CUDA_SM121)" + /*0000*/ LDC R1, c[0x0][0x37c] ; /* 0x0000df00ff017b82 */ + /* 0x000fe20000000800 */ + /*0010*/ S2R R9, SR_TID.X ; /* 0x0000000000097919 */ + /* 0x000e2e0000002100 */ + /*0020*/ LDC.64 R2, c[0x0][0x388] ; /* 0x0000e200ff027b82 */ + /* 0x000e220000000a00 */ + /*0030*/ LDCU.64 UR4, c[0x0][0x358] ; /* 0x00006b00ff0477ac */ + /* 0x000e620008000a00 */ + /*0040*/ IMAD.WIDE.U32 R2, R9, 0x4, R2 ; /* 0x0000000409027825 */ + /* 0x001fcc00078e0002 */ + /*0050*/ LDG.E R2, desc[UR4][R2.64] ; /* 0x0000000402027981 */ + /* 0x002ea4000c1e1900 */ + /*0060*/ LOP3.LUT R0, R2, 0x1, RZ, 0xc0, !PT ; /* 0x0000000102007812 */ + /* 0x004fca00078ec0ff */ + /*0070*/ ISETP.NE.U32.AND P0, PT, R0, 0x1, PT ; /* 0x000000010000780c */ + /* 0x000fda0003f05070 */ + /*0080*/ @P0 SHF.R.U32.HI R4, RZ, 0x2, R2 ; /* 0x00000002ff040819 */ + /* 0x000fe20000011602 */ + /*0090*/ @!P0 MOV R5, 0x3 ; /* 0x0000000300058802 */ + /* 0x000fc80000000f00 */ + /*00a0*/ @P0 IMAD R4, R4, R4, 0x1 ; /* 0x0000000104040424 */ + /* 0x000fe400078e0204 */ + /*00b0*/ @!P0 IMAD R0, R2, R5, 0x7 ; /* 0x0000000702008424 */ + /* 0x000fc600078e0205 */ + /*00c0*/ @P0 LOP3.LUT R0, R4, 0x5a5a, RZ, 0x3c, !PT ; /* 0x00005a5a04000812 */ + /* 0x000fe400078e3cff */ + /*00d0*/ LDC.64 R4, c[0x0][0x380] ; /* 0x0000e000ff047b82 */ + /* 0x000e260000000a00 */ + /*00e0*/ SHFL.DOWN PT, R7, R0, 0x1, 0x1f ; /* 0x08201f0000077f89 */ + /* 0x000e6200000e0000 */ + /*00f0*/ IMAD.WIDE.U32 R4, R9, 0x4, R4 ; /* 0x0000000409047825 */ + /* 0x001fe200078e0004 */ + /*0100*/ IADD3 R7, PT, PT, R7, R0, RZ ; /* 0x0000000007077210 */ + /* 0x002fca0007ffe0ff */ + /*0110*/ STG.E desc[UR4][R4.64], R7 ; /* 0x0000000704007986 */ + /* 0x000fe2000c101904 */ + /*0120*/ EXIT ; /* 0x000000000000794d */ + /* 0x000fea0003800000 */ + /*0130*/ BRA 0x130; /* 0xfffffffc00fc7947 */ + /* 0x000fc0000383ffff */ + /*0140*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*0150*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*0160*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*0170*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*0180*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*0190*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*01a0*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*01b0*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*01c0*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*01d0*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*01e0*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + /*01f0*/ NOP; /* 0x0000000000007918 */ + /* 0x000fc00000000000 */ + .......... + + diff --git a/mk/.crumb b/mk/.crumb index 4543a8b24..a6b7d4514 100644 --- a/mk/.crumb +++ b/mk/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-05T20:11:58Z", + "compiled_at": "2026-10-05T23:21:14Z", "compiler": "crumb-compile/1", - "digest": "sha256:243c8e51a2a001d63e898e6853e3ef522a65a92d6ad7344e74f5d54c8e575cb9", + "digest": "sha256:ba7d7c6e399d6a92a34be467f1d5501a9620a63839d38c27bcf8fb90b7448a02", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 58, - "generated_at_commit": "c884b376ee4d9fa56af4078b3ed47c5b5a9af959", - "source_tree": "sha256:e78d5187aea843e7d5c695959e2e8aa7bed183b7b2d2b36c50bbb12b4c31b987" + "generated_at_commit": "2f8b6f85971afc782d986abb888e55fa200ca3d0", + "source_tree": "sha256:779bf559b726b707d9d7b6bb029cf16b4e9b5ee0f5692f0d22931fa7934e367b" }, "owner_lane": "", "provenance": { diff --git a/mk/prime_race_gb10.mk b/mk/prime_race_gb10.mk index fe94c8c65..67634702a 100644 --- a/mk/prime_race_gb10.mk +++ b/mk/prime_race_gb10.mk @@ -39,9 +39,9 @@ $(GB_LABEL_STAMP): PRIME_RACE_FORCE @mkdir -p $(GB_BUILD) @echo '$(GB_SRC_COMMIT)' > $@.tmp; cmp -s $@.tmp $@ && rm $@.tmp || mv $@.tmp $@ -$(GB_BUILD)/gb10_sieve_host_test: $(GB_DIR)/tests/gb10_sieve_host_test.c $(GB_DIR)/prime_race_impl.h src/omega_blackwell_codegen.h src/omega_gpu_elementwise_api.h $(OUT_DIR)/libomega_gpu.a +$(GB_BUILD)/gb10_sieve_host_test: $(GB_DIR)/tests/gb10_sieve_host_test.c $(GB_DIR)/prime_race_impl.h src/omega_blackwell_codegen.h src/omega_gpu_elementwise_api.h tests/bw_warp_sim.h $(OUT_DIR)/libomega_gpu.a @mkdir -p $(GB_BUILD) - $(CC) $(CFLAGS) -I$(GB_DIR) -o $@ $(GB_DIR)/tests/gb10_sieve_host_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm + $(CC) $(CFLAGS) -I$(GB_DIR) -Itests -o $@ $(GB_DIR)/tests/gb10_sieve_host_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm prime-race-gb10-host-test: $(GB_BUILD)/gb10_sieve_host_test ./$(GB_BUILD)/gb10_sieve_host_test $(GB_BUILD)/gb10_launch_cost: $(GB_DIR)/tests/gb10_launch_cost.c src/omega_blackwell_codegen.h src/omega_gpu_elementwise_api.h src/omega_gpu_session.h $(OUT_DIR)/libomega_gpu.a diff --git a/src/.crumb b/src/.crumb index ab6aec041..99d1a96e1 100644 --- a/src/.crumb +++ b/src/.crumb @@ -170,14 +170,14 @@ } ], "children_root": "sha256:920218d42490379078986bd65f6d50e8ef9064df20da67bfc0ca50ea0de4b4c0", - "compiled_at": "2026-10-06T01:43:54Z", + "compiled_at": "2026-10-06T01:57:59Z", "compiler": "crumb-compile/1", - "digest": "sha256:6d49877f41b071c4f28e00bb47293fd6c483791242e7a7fc54460a5e0f6ae3ca", + "digest": "sha256:e96259ccad1aad77187347965341897722b089df3ee84a17145a32e8d308a4a6", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 408, - "generated_at_commit": "df9f40a0435a70d680f6b49297815e53e3497ea9", - "source_tree": "sha256:f65cd26a7304076fc024ceb08e6fdc1f909b2b730bd432b85183879bd747eaae" + "files": 409, + "generated_at_commit": "1f4a3c7821d769a99ce1fcc896ac8ee30a33ea69", + "source_tree": "sha256:6bf5b4894dc9c29cfa891e8995d4f01de9c2cf5c2e411c1a813fa75828285bc7" }, "owner_lane": "", "provenance": { diff --git a/src/omega_blackwell_codegen.c b/src/omega_blackwell_codegen.c index 3873cfd7d..38f73412a 100644 --- a/src/omega_blackwell_codegen.c +++ b/src/omega_blackwell_codegen.c @@ -667,6 +667,42 @@ static int encode_single_insn(const BlackwellIRInsn *insn, const OmegaRegAlloc * break; } + case BW_IR_BSSY: { + /* BSSY.RECONVERGENT Bn, target (omega #308). nvcc 13.0.88 -arch=sm_121: + * [0070] BSSY.RECONVERGENT B0, 0x140 ; 0x000000c000007945 0x000fe20003800200 + * opcode 0x945 (NAK sm70_encode.rs:4165), barrier id at bits 16..19 (set_bar_dst + * 16..20), guard predicate at bits 12..15 like BRA, relative byte offset + * target - (this + 16) at bit 32 (NAK: field 34.. in 4-byte units), the cond + * predicate PT at bits 87..89 (0x0380 << 16 in w[2]) and bit 73 (0x200 in w[2]), + * which the disassembler prints as .RECONVERGENT; nvcc sets it on every BSSY/BSYNC + * for sm_121 and Mesa NAK does not. We follow nvcc. The target is the instruction + * after the matching BSYNC, so the delta is at least 2; smaller is refused. */ + int32_t delta = (int32_t)insn->imm; + if (delta < 2 || insn->bar_reg >= BW_RECONV_MAX_BAR) return -1; + uint32_t pred_field = 0x7u; + if (insn->predicate_p0) + pred_field = insn->predicate_not ? 0x8u : 0x0u; + w[0] = 0x945u | (pred_field << 12) | ((uint32_t)insn->bar_reg << 16); + w[1] = (uint32_t)(delta - 1) * 16u; + w[2] = 0x03800200u; + w[3] = insn->control ? insn->control : 0x000fe200u; + break; + } + + case BW_IR_BSYNC: { + /* BSYNC.RECONVERGENT Bn: [0130] BSYNC.RECONVERGENT B0 ; 0x0000000000007941 0x000fea0003800200 + * opcode 0x941 (NAK sm70_encode.rs:4179), barrier id bits 16..19, cond PT, bit 73. */ + if (insn->bar_reg >= BW_RECONV_MAX_BAR) return -1; + uint32_t pred_field = 0x7u; + if (insn->predicate_p0) + pred_field = insn->predicate_not ? 0x8u : 0x0u; + w[0] = 0x941u | (pred_field << 12) | ((uint32_t)insn->bar_reg << 16); + w[1] = 0; + w[2] = 0x03800200u; + w[3] = insn->control ? insn->control : 0x000fea00u; + break; + } + case BW_IR_MUFU_EX2: /* MUFU.EX2 Rd, Ra. nvdisasm 13.0 -b SM121: 0x7308 with sub-op 0x0800 decodes * "MUFU.EX2 R2, R4" (RCP is 0x1000, RSQ 0x1400 above). */ @@ -777,6 +813,58 @@ int omega_blackwell_verify_codegen_fixtures_intops(void) { return 0; } +int omega_blackwell_encode_one(const BlackwellIRInsn *insn, uint32_t w[4]) { + if (!insn || !w) return -1; + OmegaRegAlloc ra; + memset(&ra, 0, sizeof ra); + for (int i = 0; i < BW_MAX_VREGS; i++) ra.vreg_to_phys[i] = i; + for (int i = 0; i < BW_MAX_UVREGS; i++) ra.uvreg_to_phys[i] = i; + return encode_single_insn(insn, &ra, w); +} + +/* omega #308: BSSY / BSYNC golden words. Oracle: nvcc 13.0.88 -arch=sm_121 -cubin -O3 on the + * kernels in docs/gpu-reconvergence-308.md, cuobjdump -sass (low 64-bit word, then high; the + * top 23 bits of the high word are the control word and are not compared here): + * k_loop [0070] BSSY.RECONVERGENT B0, 0x140 ; 0x000000c000007945 0x000fe20003800200 (delta 13) + * k_nested [0070] BSSY.RECONVERGENT B0, 0x300 ; 0x0000028000007945 0x000fe20003800200 (delta 41) + * k_break [0070] BSSY.RECONVERGENT B0, 0x110 ; 0x0000009000007945 0x000fe20003800200 (delta 10) + * k_loop [0130] BSYNC.RECONVERGENT B0 ; 0x0000000000007941 0x000fea0003800200 + * Barrier ids other than B0 and the predicated forms are decoded by tools/reconv_nvdisasm_check.sh + * (nvdisasm -b SM121 text), not by this table. */ +int omega_blackwell_verify_codegen_fixtures_reconv(void) { + static const struct { BlackwellIROpcode op; int delta; int bar; uint32_t w0, w1, w2; } fx[] = { + { BW_IR_BSSY, 13, 0, 0x00007945U, 0x000000c0U, 0x03800200U }, + { BW_IR_BSSY, 41, 0, 0x00007945U, 0x00000280U, 0x03800200U }, + { BW_IR_BSSY, 10, 0, 0x00007945U, 0x00000090U, 0x03800200U }, + { BW_IR_BSYNC, 0, 0, 0x00007941U, 0x00000000U, 0x03800200U }, + }; + for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) { + BlackwellIRInsn in; + memset(&in, 0, sizeof in); + in.op = fx[i].op; in.dst_vreg = -1; in.src1_vreg = -1; in.src2_vreg = -1; in.src3_vreg = -1; in.ureg = -1; + in.imm = (uint32_t)fx[i].delta; in.bar_reg = (uint8_t)fx[i].bar; + uint32_t w[4]; + if (omega_blackwell_encode_one(&in, w) != 0) return -(2 * i + 1); + if (w[0] != fx[i].w0 || w[1] != fx[i].w1 || w[2] != fx[i].w2) return -(2 * i + 2); + /* control word defaults mirror nvcc's: BSSY 0x000fe200, BSYNC 0x000fea00 */ + if (w[3] != (fx[i].op == BW_IR_BSSY ? 0x000fe200U : 0x000fea00U)) return -(2 * i + 2); + } + /* refusals: a BSSY must name a later instruction (delta >= 2), barrier ids stop at B15 */ + BlackwellIRInsn bad; uint32_t w[4]; + memset(&bad, 0, sizeof bad); bad.op = BW_IR_BSSY; bad.dst_vreg = bad.src1_vreg = bad.src2_vreg = bad.src3_vreg = bad.ureg = -1; + bad.imm = 1; bad.bar_reg = 0; + if (omega_blackwell_encode_one(&bad, w) == 0) return -101; + bad.imm = (uint32_t)-3; + if (omega_blackwell_encode_one(&bad, w) == 0) return -102; + bad.imm = 5; bad.bar_reg = 16; + if (omega_blackwell_encode_one(&bad, w) == 0) return -103; + bad.op = BW_IR_BSYNC; bad.imm = 0; bad.bar_reg = 16; + if (omega_blackwell_encode_one(&bad, w) == 0) return -104; + bad.bar_reg = 15; + if (omega_blackwell_encode_one(&bad, w) != 0 || w[0] != (0x00007941U | (15U << 16))) return -105; + return 0; +} + /* FB-1 cut 4: word-level fixtures for the four additive ops (checked against the * nvdisasm decodes recorded in docs/numeric/FB1_CUT4_ELEMENTWISE.md). 0 = all good. */ int omega_blackwell_verify_codegen_fixtures_fb1cut4(void) { diff --git a/src/omega_blackwell_codegen.h b/src/omega_blackwell_codegen.h index 8cbfedf73..2358ea760 100644 --- a/src/omega_blackwell_codegen.h +++ b/src/omega_blackwell_codegen.h @@ -81,10 +81,25 @@ typedef enum { * truth table over Ra = 0xf0, Rb = 0xcc, Rc = 0xaa (a|b = 0xfc, ~(a|b) = 0x03) */ BW_IR_SHF_L_U32, /* SHF.L.U32 Rd, Ra, Rb, RZ: Rd = Ra << Rb, register amount (the form nvcc emits * for PTX shl.b32). Amounts >= 32 are NOT chip-verified: callers keep Rb < 32 */ - BW_IR_IMAD_HI_U32 /* IMAD.HI.U32 Rd, Ra, Rb, Rc: Rd = hi32(Ra * Rb) + Rc mod 2^32 (nvcc emits this + BW_IR_IMAD_HI_U32, /* IMAD.HI.U32 Rd, Ra, Rb, Rc: Rd = hi32(Ra * Rb) + Rc mod 2^32 (nvcc emits this * word for __umulhi(a, b) + c) */ + /* Structured warp reconvergence (omega #308, 2026-10-05), additive. Words matched against + * nvcc 13.0.88 -arch=sm_121 output (BSSY.RECONVERGENT / BSYNC.RECONVERGENT; the oracle + * kernels are listed in docs/gpu-reconvergence-308.md) and the Mesa NAK encoder + * (sm70_encode.rs OpBSSy 0x945 / OpBSync 0x941, barrier register at bits 16..19). + * bar_reg selects B0..B15. BSSY arms the barrier for the lanes executing it and names + * the join point; BSYNC waits for every armed lane that has not exited, then the warp + * continues as one. imm of BSSY = signed instruction delta to the join point (the + * instruction after the BSYNC), like BW_IR_BRA; the encoder refuses deltas < 1. */ + BW_IR_BSSY, + BW_IR_BSYNC } BlackwellIROpcode; +/* Barrier (reconvergence) registers: the sm_121 BSSY/BSYNC words carry a 4-bit barrier id + * (NAK sm70_encode.rs set_bar_dst(16..20)), so B0..B15 are encodable. How many the chip + * provides is not documented to us; chip tests nest to BW_RECONV_CHIP_TESTED_DEPTH. */ +#define BW_RECONV_MAX_BAR 16 + /* Special Register Identifiers */ typedef enum { BW_SR_TID_X = 0x21, @@ -112,6 +127,7 @@ typedef struct { uint8_t src1_subreg; uint8_t src2_subreg; uint8_t src3_subreg; + uint8_t bar_reg; /* BW_IR_BSSY / BW_IR_BSYNC: barrier register B0..B15 */ } BlackwellIRInsn; /* Live Interval for Bounded Linear Register Allocation */ @@ -203,6 +219,14 @@ int omega_blackwell_verify_codegen_fixtures_fb1cut4(void); /* Prime race cut: golden words for LOP3_LUT, SHF_L_U32, IMAD_HI_U32 (0 = pass) */ int omega_blackwell_verify_codegen_fixtures_intops(void); +/* omega #308: golden words for BSSY / BSYNC (nvcc 13.0.88 sm_121 oracle), plus the + * encoder's refusals (negative delta, barrier id > 15). 0 = pass. */ +int omega_blackwell_verify_codegen_fixtures_reconv(void); + +/* omega #308: encodes one instruction with a given physical register map of + * identity (vreg n -> Rn) so offline tools can disassemble single words. 0 = ok. */ +int omega_blackwell_encode_one(const BlackwellIRInsn *insn, uint32_t w[4]); + /* Unit test for bounded register allocation live intervals and bounds enforcement (Gate 4) */ int omega_blackwell_test_regalloc_bounds(void); diff --git a/src/omega_bw_reconv.h b/src/omega_bw_reconv.h new file mode 100644 index 000000000..d432e5763 --- /dev/null +++ b/src/omega_bw_reconv.h @@ -0,0 +1,83 @@ +#ifndef OMEGA_BW_RECONV_H +#define OMEGA_BW_RECONV_H +/* Structured warp reconvergence regions for the Blackwell IR emitters (omega #308). + * + * A region is the span between a BSSY (arm barrier Bn, name the join point) and the + * matching BSYNC (wait for every armed lane, continue as one warp). Inside a region the + * lanes of a warp may take different paths: a predicated forward branch, a loop whose + * trip count differs per lane, a break. Every path that leaves the region jumps to the + * BSYNC itself (that is where nvcc 13.0.88 sends loop exits on sm_121); the BSSY names the + * instruction after the BSYNC. Regions nest; the innermost open region owns Bn with + * n = depth, so nesting depth is bounded by the barrier register count and begin fails + * closed (returns -1) when that bound is reached. + * + * This header holds the bookkeeping only (indices, patching, bounds). Each emitter wraps + * it with its own control-word scheme: see region_begin / region_exit / region_join in + * src/omega_gpu_elementwise_api.c and src/omega_gpu_attention_api.c. + * + * Reference: Mesa NAK nak_nir_lower_cf.c (push_scope / pop_scope: bar_set_nv before a + * divergent if or loop, bar_sync_nv at its merge block, breaks jump to the sync) and the + * nvcc oracle listing in docs/gpu-reconvergence-308.md. */ + +#include "omega_blackwell_codegen.h" + +#define BW_RECONV_MAX_DEPTH BW_RECONV_MAX_BAR +#define BW_RECONV_MAX_EXITS 8 /* forward exits (breaks) recorded per region */ + +typedef struct { + int depth; /* open regions */ + int peak; /* deepest nesting seen (reported in receipts) */ + int opened; /* regions begun (for receipts) */ + int bssy_at[BW_RECONV_MAX_DEPTH]; + int exits[BW_RECONV_MAX_DEPTH][BW_RECONV_MAX_EXITS]; + int n_exits[BW_RECONV_MAX_DEPTH]; +} BwRegions; + +/* Called after the emitter appended the BSSY at index `bssy_index`. Returns the barrier + * register to use, or -1 when the nesting bound is reached (the caller must fail the + * build: no code with a missing barrier may be emitted). The caller allocates the + * barrier BEFORE emitting: use bw_regions_next_bar to pick it, then bw_regions_begin. */ +static inline int bw_regions_next_bar(const BwRegions *r) { + return r->depth < BW_RECONV_MAX_DEPTH ? r->depth : -1; +} +static inline int bw_regions_begin(BwRegions *r, int bssy_index) { + if (r->depth >= BW_RECONV_MAX_DEPTH || bssy_index < 0) return -1; + r->bssy_at[r->depth] = bssy_index; + r->n_exits[r->depth] = 0; + r->depth++; + r->opened++; + if (r->depth > r->peak) r->peak = r->depth; + return r->depth - 1; +} + +/* Records a forward branch (already appended at `bra_index`) that leaves the innermost + * open region; its target is patched to the BSYNC by bw_regions_join. -1 when no region + * is open or the per-region exit table is full. */ +static inline int bw_regions_exit(BwRegions *r, int bra_index) { + if (r->depth <= 0 || bra_index < 0) return -1; + int d = r->depth - 1; + if (r->n_exits[d] >= BW_RECONV_MAX_EXITS) return -1; + r->exits[d][r->n_exits[d]++] = bra_index; + return 0; +} + +/* Called after the emitter appended the BSYNC at index `bsync_index` for the innermost + * open region. Patches the BSSY to name the instruction after the BSYNC and every + * recorded exit to jump to the BSYNC. Returns the barrier register closed, or -1 when + * no region is open or an index is inconsistent (BSSY must precede the BSYNC). */ +static inline int bw_regions_join(BwRegions *r, BlackwellIRProgram *p, int bsync_index) { + if (r->depth <= 0 || !p || bsync_index < 0 || (size_t)bsync_index >= p->count) return -1; + int d = --r->depth; + int at = r->bssy_at[d]; + if (at < 0 || at >= bsync_index || p->insns[at].op != BW_IR_BSSY || p->insns[bsync_index].op != BW_IR_BSYNC) return -1; + if (p->insns[at].bar_reg != (uint8_t)d || p->insns[bsync_index].bar_reg != (uint8_t)d) return -1; + p->insns[at].imm = (uint32_t)(bsync_index + 1 - at); + for (int i = 0; i < r->n_exits[d]; i++) { + int b = r->exits[d][i]; + if (b <= at || b >= bsync_index || p->insns[b].op != BW_IR_BRA) return -1; + p->insns[b].imm = (uint32_t)(bsync_index - b); + } + return d; +} + +#endif /* OMEGA_BW_RECONV_H */ diff --git a/src/omega_gpu_attention_api.c b/src/omega_gpu_attention_api.c index aa83eab9e..64249473c 100644 --- a/src/omega_gpu_attention_api.c +++ b/src/omega_gpu_attention_api.c @@ -25,6 +25,7 @@ */ #include "omega_gpu_attention_api.h" #include "omega_blackwell_codegen.h" +#include "omega_bw_reconv.h" #include "omega_blackwell_qmd.h" #include "omega_blackwell_submit.h" #include "omega_gpu_session.h" @@ -45,7 +46,7 @@ #define SB_RD 1u #define SB_UGPR 2u -enum { K_FIXED, K_PRED, K_VAR, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA }; +enum { K_FIXED, K_PRED, K_VAR, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC }; #define AT_MAX_LOOPS 8 typedef struct { @@ -54,6 +55,8 @@ typedef struct { int err; int loop_start[AT_MAX_LOOPS], loop_end[AT_MAX_LOOPS]; int n_loops, depth, stack[AT_MAX_LOOPS]; + BwRegions rg; /* structured reconvergence regions (omega #308) */ + int rg_mutant; /* test only: 1 = drop the BSYNC at join (the 2026-10-04 bug shape; host sim must catch it) */ } Em; static void emit(Em *e, BlackwellIRInsn in, int kind) { @@ -67,6 +70,9 @@ static void emit(Em *e, BlackwellIRInsn in, int kind) { case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; + /* stall counts as nvcc 13.0.88 writes them for sm_121 (see src/omega_gpu_elementwise_api.c) */ + case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; + case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; default: e->err = -1; return; } if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; @@ -109,15 +115,49 @@ static void e_fmul(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FM static void e_ffma(Em *e, int d, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_FFMA); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } static void e_fmax(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FMNMX_MAX); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } static void mufu(Em *e, BlackwellIROpcode op, int d, int a) { BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = a; emit(e, i, K_VAR); } -static void shfl_down(Em *e, int d, int a, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_SHFL_DOWN); i.dst_vreg = d; i.src1_vreg = a; i.imm = off; emit(e, i, K_VAR); } +/* SHFL and BAR.SYNC are refused inside an open reconvergence region (omega #308): on a split + * warp they read inactive lanes / count a partial warp, which is what produced wrong dims on + * the chip on 2026-10-04. The build fails closed instead. */ +static int in_region(Em *e) { if (e->rg.depth > 0) { e->err = -1; return 1; } return 0; } +static void shfl_down(Em *e, int d, int a, uint32_t off) { if (in_region(e)) return; BlackwellIRInsn i = I0(BW_IR_SHFL_DOWN); i.dst_vreg = d; i.src1_vreg = a; i.imm = off; emit(e, i, K_VAR); } static void sts32(Em *e, int addr, int val) { BlackwellIRInsn i = I0(BW_IR_STS32); i.src1_vreg = addr; i.src2_vreg = val; emit(e, i, K_STORE); } static void lds32(Em *e, int d, int addr) { BlackwellIRInsn i = I0(BW_IR_LDS32); i.dst_vreg = d; i.src1_vreg = addr; emit(e, i, K_VAR); } -static void bar_sync(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } +static void bar_sync(Em *e) { if (in_region(e)) return; emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } static void isetp_ge_u32(Em *e, int a, int b) { BlackwellIRInsn i = I0(BW_IR_ISETP_GE_U32); i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_PRED); } static void bra_back_if_not_p0(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.predicate_not = true; i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } /* @P0 BRA forward; the target is patched in when known */ static int bra_fwd_if_p0(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = 0; emit(e, i, K_BRA); return at; } static void patch_fwd(Em *e, int at) { if (at >= 0 && (size_t)at < e->p->count) e->p->insns[at].imm = (uint32_t)(here(e) - at); else e->err = -1; } + +/* ---- structured reconvergence regions (omega #308); same API as the elementwise emitter ---- */ +enum { RX_ALWAYS, RX_IF_P0, RX_IF_NOT_P0 }; +static int region_begin(Em *e) { + int bar = bw_regions_next_bar(&e->rg); + if (bar < 0) { e->err = -1; return -1; } + int at = here(e); + BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = (uint8_t)bar; i.imm = 2; emit(e, i, K_BSSY); + if (bw_regions_begin(&e->rg, at) < 0) e->err = -1; + return bar; +} +static void region_exit(Em *e, int cond) { + int at = here(e); + BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = 1; + if (cond != RX_ALWAYS) { i.predicate_p0 = true; i.predicate_not = (cond == RX_IF_NOT_P0); } + emit(e, i, K_BRA); + if (bw_regions_exit(&e->rg, at) < 0) e->err = -1; +} +static void region_join(Em *e) { + if (e->rg.depth <= 0) { e->err = -1; return; } + int d = e->rg.depth - 1, bsync = here(e); + BlackwellIRInsn i = I0(e->rg_mutant == 1 ? BW_IR_NOP : BW_IR_BSYNC); i.bar_reg = (uint8_t)d; emit(e, i, K_BSYNC); + if (e->rg_mutant == 1) { + BwRegions *r = &e->rg; r->depth--; + e->p->insns[r->bssy_at[d]].imm = (uint32_t)(bsync + 1 - r->bssy_at[d]); + for (int k = 0; k < r->n_exits[d]; k++) e->p->insns[r->exits[d][k]].imm = (uint32_t)(bsync - r->exits[d][k]); + return; + } + if (bw_regions_join(&e->rg, e->p, bsync) != d) e->err = -1; +} static void tail(Em *e) { emit(e, I0(BW_IR_EXIT), K_EXIT); BlackwellIRInsn self = I0(BW_IR_BRA); @@ -179,7 +219,11 @@ static int check_loop_invariant(const Em *e) { #define SH_R 768u #define F32_NEG_INF 0xff800000u -typedef struct { bool f32; uint32_t log2bs, log2gqa; int mutant; } KSpec; +/* divergent (omega #308 regression R2): 0 = production branch-free chunk loop; 1 = the + * lane-dependent "j >= ctx" branch restored inside a reconvergence region (BSSY/BSYNC) so + * the SHFL reductions run on a joined warp; 2 = the same with the BSYNC dropped (the + * 2026-10-04 bug shape, a negative control the host simulator must catch). */ +typedef struct { bool f32; uint32_t log2bs, log2gqa; int mutant; int divergent; } KSpec; static void gen_attention(Em *e, const KSpec *k) { const uint32_t elem = k->f32 ? 4u : 2u; @@ -260,10 +304,19 @@ static void gen_attention(Em *e, const KSpec *k) { iadd3(e, j, base, tid); movi(e, s, F32_NEG_INF); movrz(e, koff); - imadi(e, dneg, j, 0xFFFFFFFFu, ctxm1); /* ctx - 1 - j */ - shri(e, over, dneg, 31); - imadr(e, ot, over, tid, NOREG); - imadi(e, j, ot, 0xFFFFFFFFu, j); /* j - over*tid */ + if (k->divergent) { + /* omega #308 R2: the lane-dependent branch, inside a reconvergence region. Lanes with + * j >= ctx skip phase A (s stays -inf, koff 0); the BSYNC at region_join rejoins them + * before the SHFL reductions below. divergent == 2 drops that BSYNC (negative control). */ + region_begin(e); + isetp_ge_u32(e, j, ctx); + region_exit(e, RX_IF_P0); + } else { + imadi(e, dneg, j, 0xFFFFFFFFu, ctxm1); /* ctx - 1 - j */ + shri(e, over, dneg, 31); + imadr(e, ot, over, tid, NOREG); + imadi(e, j, ot, 0xFFFFFFFFu, j); /* j - over*tid */ + } /* phase A: token j */ shri(e, bi, j, k->log2bs); lop3andi(e, sl, j, (1u << k->log2bs) - 1u); @@ -309,8 +362,12 @@ static void gen_attention(Em *e, const KSpec *k) { bra_back_if_not_p0(e, Lin); loop_end(e); e_fmul(e, s, dot, scale); - imadi(e, nb, over, 0x80000000u, cninf); /* over ? +inf : -inf (bits) */ - e_fsub(e, ns, zero, s); e_fmax(e, ns, ns, nb); e_fsub(e, s, zero, ns); + if (k->divergent) { + region_join(e); /* every lane back before the cross-lane reductions */ + } else { + imadi(e, nb, over, 0x80000000u, cninf); /* over ? +inf : -inf (bits) */ + e_fsub(e, ns, zero, s); e_fmax(e, ns, ns, nb); e_fsub(e, s, zero, ns); + } sts32(e, sOff, s); sts32(e, oOff, koff); @@ -388,8 +445,10 @@ static int build_kernel(const KSpec *k, OmegaBlackwellKernel *kernel, BlackwellI if (!prog) return OMEGA_GPU_ATTN_CODEGEN_FAIL; omega_bw_ir_init(prog); Em e; memset(&e, 0, sizeof e); e.p = prog; + e.rg_mutant = (k->divergent == 2) ? 1 : 0; gen_attention(&e, k); if (e.depth != 0) e.err = -1; + if (e.rg.depth != 0) e.err = -1; /* a region left open has no join: refuse the build */ int rc = OMEGA_GPU_ATTN_CODEGEN_FAIL; int ra_rc = (e.err == 0) ? omega_bw_regalloc_solve(prog) : -1; int loop_rc = (ra_rc == 0) ? check_loop_invariant(&e) : -1; @@ -422,6 +481,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 int g_divergent; /* omega #308 R2 test hook: see KSpec.divergent */ 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; @@ -462,10 +522,10 @@ void omega_gpu_attention_test_set_mutant(OmegaGpuAttnMutant m) { /* Caller holds the lock. Builds the kernel if needed; uploads the device copy when * `upload` (the chip path; the simulator never needs one). */ static int kernel_for(bool f32, uint32_t log2bs, uint32_t log2gqa, bool upload, Slot **out, bool *hit) { - KSpec k = { .f32 = f32, .log2bs = log2bs, .log2gqa = log2gqa, .mutant = g_mutant }; + KSpec k = { .f32 = f32, .log2bs = log2bs, .log2gqa = log2gqa, .mutant = g_mutant, .divergent = g_divergent }; Slot *s = NULL; for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) - if (g_cache[i].used && g_cache[i].k.f32 == k.f32 && g_cache[i].k.log2bs == k.log2bs && g_cache[i].k.log2gqa == k.log2gqa && g_cache[i].k.mutant == k.mutant) { /* field compare: memcmp would read struct padding */ + if (g_cache[i].used && g_cache[i].k.f32 == k.f32 && g_cache[i].k.log2bs == k.log2bs && g_cache[i].k.log2gqa == k.log2gqa && g_cache[i].k.mutant == k.mutant && g_cache[i].k.divergent == k.divergent) { /* field compare: memcmp would read struct padding */ s = &g_cache[i]; *hit = true; break; } if (!s) { @@ -492,7 +552,7 @@ static int kernel_for(bool f32, uint32_t log2bs, uint32_t log2gqa, bool upload, int omega_gpu_attention_codegen(bool kv_f32, uint32_t log2_block_size, uint32_t log2_gqa, OmegaBlackwellKernel *kernel) { if (!kernel || log2_block_size > 16 || log2_gqa > 6) return OMEGA_GPU_ATTN_BAD_ARGS; LOCK(); - KSpec k = { .f32 = kv_f32, .log2bs = log2_block_size, .log2gqa = log2_gqa, .mutant = g_mutant }; + KSpec k = { .f32 = kv_f32, .log2bs = log2_block_size, .log2gqa = log2_gqa, .mutant = g_mutant, .divergent = g_divergent }; UNLOCK(); return build_kernel(&k, kernel, NULL); } @@ -821,3 +881,12 @@ const char *omega_gpu_attention_rc_name(int rc) { default: return "UNKNOWN"; } } + +/* omega #308 R2 test hook: 0 production, 1 lane-dependent branch inside a reconvergence + * region, 2 the same with the BSYNC dropped (negative control). Clears the kernel cache. */ +void omega_gpu_attention_test_set_divergent(int mode) { + LOCK(); + g_divergent = (mode < 0 || mode > 2) ? 0 : mode; + cache_clear_locked(); + UNLOCK(); +} diff --git a/src/omega_gpu_attention_api.h b/src/omega_gpu_attention_api.h index 01ad01179..f4931c2a6 100644 --- a/src/omega_gpu_attention_api.h +++ b/src/omega_gpu_attention_api.h @@ -157,6 +157,12 @@ typedef enum { 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); + +/* TEST ONLY (omega #308 regression R2). 0 = production branch-free chunk loop; 1 = the + * lane-dependent j >= ctx branch restored inside a BSSY/BSYNC reconvergence region; + * 2 = that kernel with the BSYNC dropped (negative control: the host simulator must refuse + * it). Clears the kernel cache. */ +void omega_gpu_attention_test_set_divergent(int mode); /* "none", "NO_MAX", ... ; "?" past COUNT. The table is checked against COUNT at compile time. */ const char *omega_gpu_attention_mutant_name(OmegaGpuAttnMutant m); diff --git a/src/omega_gpu_elementwise_api.c b/src/omega_gpu_elementwise_api.c index 83e746f0c..b16c70205 100644 --- a/src/omega_gpu_elementwise_api.c +++ b/src/omega_gpu_elementwise_api.c @@ -19,6 +19,7 @@ */ #include "omega_gpu_elementwise_api.h" #include "omega_blackwell_codegen.h" +#include "omega_bw_reconv.h" #include "omega_blackwell_qmd.h" #include "omega_blackwell_submit.h" #include "omega_gpu_session.h" @@ -38,7 +39,7 @@ #define SB_RD 1u /* store source-register read barrier */ #define SB_UGPR 2u /* uniform-register results (LDCU) */ -enum { K_FIXED, K_PRED, K_VAR, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA }; +enum { K_FIXED, K_PRED, K_VAR, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC }; #define EW_MAX_LOOPS 4 typedef struct { @@ -49,6 +50,11 @@ typedef struct { int n_loops; int open_start[EW_MAX_LOOPS]; /* starts of loops begun and not yet ended (nesting stack) */ int depth; + BwRegions rg; /* structured reconvergence regions (omega #308) */ + int rg_mutant; /* test only: 1 = drop the BSYNC at join, 2 = corrupt the BSSY target (host sim must catch both) */ + int exited; /* a predicated EXIT was emitted: some lanes of a warp may be gone */ + int exit_sync_ok; /* test only: lets a kernel put SHFL/BAR after a predicated EXIT (the negative control R6) */ + int test_path; /* building for omega_gpu_elementwise_codegen_ir (host tests), never for a launch */ } Em; static void emit(Em *e, BlackwellIRInsn in, int kind) { @@ -63,6 +69,12 @@ static void emit(Em *e, BlackwellIRInsn in, int kind) { /* a predicated EXIT or BRA that falls through must not be the only waiter */ case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; + /* BSSY / BSYNC read no registers; the stall counts (1 and 5, both with yield) are the ones + * nvcc 13.0.88 writes for sm_121 (docs/gpu-reconvergence-308.md). They wait on everything + * pending so that, as for every branch here, nothing is in flight across a control edge + * and the straight-line scoreboard walk in the tests stays valid. */ + case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; + case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; default: e->err = -1; return; } if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; /* append returns the index */ @@ -119,12 +131,23 @@ static void fsub(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FSUB static void fmul(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FMUL); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } static void ffma(Em *e, int d, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_FFMA); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } static void mufu(Em *e, BlackwellIROpcode op, int d, int a) { BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = a; emit(e, i, K_VAR); } -static void shfl_down(Em *e, int d, int a, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_SHFL_DOWN); i.dst_vreg = d; i.src1_vreg = a; i.imm = off; emit(e, i, K_VAR); } +/* Cross-lane and warp-ending ops are refused inside an open reconvergence region (omega #308): + * a SHFL or BAR.SYNC reached by a split warp reads inactive lanes / counts a partial warp + * (undefined; the 2026-10-04 attention bug), and a lane that EXITs inside a region can no + * longer be joined. The build fails closed instead of emitting such code. */ +/* Cross-lane instructions (SHFL, BAR.SYNC) need the whole warp present. The emitter refuses + * them (fails the build) inside an open reconvergence region, where lanes may be on different + * paths, and after a predicated EXIT, where lanes may be gone for good: a SHFL then reads + * inactive lanes (undefined on the chip, the attention failure of 2026-10-04). A kernel that + * must drop lanes before a cross-lane step clamps them instead and exits them afterwards. */ +static int warp_split(Em *e) { if (e->rg.depth > 0 || (e->exited && !e->exit_sync_ok)) { e->err = -1; return 1; } return 0; } +static void shfl_down(Em *e, int d, int a, uint32_t off) { if (warp_split(e)) return; BlackwellIRInsn i = I0(BW_IR_SHFL_DOWN); i.dst_vreg = d; i.src1_vreg = a; i.imm = off; emit(e, i, K_VAR); } static void sts32(Em *e, int addr, int val) { BlackwellIRInsn i = I0(BW_IR_STS32); i.src1_vreg = addr; i.src2_vreg = val; emit(e, i, K_STORE); } static void lds32(Em *e, int d, int addr) { BlackwellIRInsn i = I0(BW_IR_LDS32); i.dst_vreg = d; i.src1_vreg = addr; emit(e, i, K_VAR); } -static void bar_sync(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } +static void bar_sync(Em *e) { if (warp_split(e)) return; emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } static void isetp_ge_u32(Em *e, int a, int b) { BlackwellIRInsn i = I0(BW_IR_ISETP_GE_U32); i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_PRED); } -static void exit_if_p0(Em *e) { BlackwellIRInsn i = I0(BW_IR_EXIT); i.predicate_p0 = true; emit(e, i, K_EXIT); } +/* An EXIT inside a region would leave the barrier waiting for a lane that never comes: refused. */ +static void exit_if_p0(Em *e) { if (e->rg.depth > 0) { e->err = -1; return; } e->exited = 1; BlackwellIRInsn i = I0(BW_IR_EXIT); i.predicate_p0 = true; emit(e, i, K_EXIT); } static void bra_back_if_not_p0(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.predicate_not = true; i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } static void tail(Em *e) { emit(e, I0(BW_IR_EXIT), K_EXIT); @@ -336,6 +359,45 @@ static int bra_fwd_if_p0(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR static void bra_patch_here(Em *e, int at) { if (e->err == 0 && at >= 0 && at < here(e)) e->p->insns[at].imm = (uint32_t)(here(e) - at); else e->err = -1; } static void bra_always(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } +/* ---- structured reconvergence regions (omega #308; bookkeeping in omega_bw_reconv.h) ---- + * region_begin: BSSY Bd (d = nesting depth), fails closed when no barrier register is left. + * region_exit: a forward branch out of the innermost region, patched to its BSYNC at join + * (RX_ALWAYS, RX_IF_P0, RX_IF_NOT_P0). + * region_join: BSYNC Bd; patches the BSSY to the instruction after it and every exit to it. + * Usage: divergent loop = region_begin; L: isetp; region_exit(RX_IF_P0); body; bra_always(L); region_join. + * if/else diamond = region_begin; isetp; t = bra_fwd_if_p0; then; region_exit(RX_ALWAYS); + * bra_patch_here(t); else; region_join. */ +enum { RX_ALWAYS, RX_IF_P0, RX_IF_NOT_P0 }; +static int region_begin(Em *e) { + int bar = bw_regions_next_bar(&e->rg); + if (bar < 0) { e->err = -1; return -1; } + int at = here(e); + BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = (uint8_t)bar; i.imm = 2; emit(e, i, K_BSSY); + if (bw_regions_begin(&e->rg, at) < 0) e->err = -1; + return bar; +} +static void region_exit(Em *e, int cond) { + int at = here(e); + BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = 1; + if (cond != RX_ALWAYS) { i.predicate_p0 = true; i.predicate_not = (cond == RX_IF_NOT_P0); } + emit(e, i, K_BRA); + if (bw_regions_exit(&e->rg, at) < 0) e->err = -1; +} +static void region_join(Em *e) { + if (e->rg.depth <= 0) { e->err = -1; return; } + int d = e->rg.depth - 1, bsync = here(e); + BlackwellIRInsn i = I0(e->rg_mutant == 1 ? BW_IR_NOP : BW_IR_BSYNC); i.bar_reg = (uint8_t)d; emit(e, i, K_BSYNC); + if (e->rg_mutant == 1) { + /* negative control (host sim only): the join is a NOP; patch as the real join would */ + BwRegions *r = &e->rg; r->depth--; + e->p->insns[r->bssy_at[d]].imm = (uint32_t)(bsync + 1 - r->bssy_at[d]); + for (int k = 0; k < r->n_exits[d]; k++) e->p->insns[r->exits[d][k]].imm = (uint32_t)(bsync - r->exits[d][k]); + return; + } + if (bw_regions_join(&e->rg, e->p, bsync) != d) e->err = -1; + if (e->rg_mutant == 2) e->p->insns[e->rg.bssy_at[d]].imm += 1; /* negative control: BSSY names the wrong join */ +} + /* Word-parallel odd-only sieve (docs: handoff 2026-10-05 DESIGN "GPU kernel (native)"). * Thread t owns 32-bit output words w = t, t + S, t + 2S, ... (S = P1 = threads launched; * the I42 envelope caps a launch at OMEGA_GPU_EW_MAX_CTAS CTAs, so a grid-stride loop @@ -365,8 +427,14 @@ static void bra_always(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i * (3 per entry). The CTA first copies the table into shared memory at offset 0 (thread t copies * words t, t + 128, ...), BAR_SYNC, then the prime loop reads shared memory (LDS) instead of * uncached global memory. Every thread reaches the barrier: the w >= nwords exit comes after - * it. The launch must declare at least 4 * P4 bytes of shared memory. */ -static void gen_prime_sieve(Em *e, int mutant, int staged) { + * it. The launch must declare at least 4 * P4 bytes of shared memory. + * + * divergent (omega #308, regression R1): 0 = the warp-uniform mark loop above (campaign C5). + * 1 = the original lane-dependent mark loop (omega 888a011: `while j < 32 { acc |= 1 << j; j += p }`) + * wrapped in a reconvergence region. 2 = as 1, and the prime loop (whose break `k0 >= base + 32` + * is also lane-dependent: every lane sieves a different word) wrapped in an outer region, so the + * mark loop's region nests inside it (B0 outer, B1 inner). */ +static void gen_prime_sieve(Em *e, int mutant, int staged, int divergent) { Pro pr = prologue(e); int nwords = V(e), stride = V(e), lastw = V(e), tailinv = V(e); int one = V(e), c32 = V(e), cm1 = V(e), w = V(e); @@ -379,10 +447,16 @@ static void gen_prime_sieve(Em *e, int mutant, int staged) { ldc32(e, twords, ARG_P4); movi(e, c4, 4); movi(e, c8, 8); movi(e, c12, 12); imadi(e, i, pr.tid, 1, NOREG); + /* The staging loop's trip count differs per thread (thread t copies words t, t+128, ...), + * so its exit splits the warp. The divergent variants rejoin the warp before BAR.SYNC + * (a barrier is not a reconvergence point: the fragments would stay split for the rest + * of the kernel). The production kernel keeps the 2026-10-05 C3 form unchanged. */ + if (divergent) region_begin(e); loop_begin(e); /* staging loop */ int ls = here(e); isetp_ge_u32(e, i, twords); - int sdone = bra_fwd_if_p0(e); + int sdone = -1; + if (divergent) region_exit(e, RX_IF_P0); else sdone = bra_fwd_if_p0(e); addr4(e, ga, i, pr.pa); ldg(e, val, ga, pr.udesc); imadi(e, sa, i, 4, NOREG); @@ -390,7 +464,7 @@ static void gen_prime_sieve(Em *e, int mutant, int staged) { imadi(e, i, one, OMEGA_GPU_EW_THREADS, i); bra_always(e, ls); loop_end(e); - bra_patch_here(e, sdone); + if (divergent) region_join(e); else bra_patch_here(e, sdone); bar_sync(e); } imadi(e, w, pr.cta, OMEGA_GPU_EW_THREADS, pr.tid); @@ -413,6 +487,7 @@ static void gen_prime_sieve(Em *e, int mutant, int staged) { movrz(e, acc); if (staged) movrz(e, ptr); else widei(e, ptr, one, 0, pr.pa); + if (divergent >= 2) region_begin(e); /* outer region: the prime loop's break is lane-dependent */ loop_begin(e); /* prime loop */ int lp = here(e); /* one memory round trip per prime: all three entry words load together (C2a). The @@ -425,7 +500,8 @@ static void gen_prime_sieve(Em *e, int mutant, int staged) { else ld_group(e, BW_IR_LDG_E, 3, dsts, adrs, pr.udesc); } isetp_ge_u32(e, k0, basep32); - int brk = bra_fwd_if_p0(e); + int brk = -1; + if (divergent >= 2) region_exit(e, RX_IF_P0); else brk = bra_fwd_if_p0(e); imadi(e, negp, p, 0xffffffffu, NOREG); /* -p */ imadi(e, d, k0, 0xffffffffu, base); /* base - k0 */ shr_i(e, neg, d, 31); @@ -446,27 +522,43 @@ static void gen_prime_sieve(Em *e, int mutant, int staged) { * same ceil(32/p) steps and the warp never splits here (all active lanes are on the same * prime). Bits past this word are masked out instead of branched around. The old loop ran * "while j < 32", a lane-dependent branch with no reconvergence point. */ - movrz(e, tt); - loop_begin(e); - int li = here(e); - isetp_ge_u32(e, tt, c32); - int done = bra_fwd_if_p0(e); - iadd3(e, jj, j, tt); - lop3lut(e, sh, jj, c31, NOREG, 0xc0); /* jj & 31 */ - shl_r(e, bit, one, sh); - iadd3(e, t, jj, cm32); - shr_i(e, z, t, 31); /* jj < 32 (j <= p + 31, far below 2^31) */ - imadi(e, m, z, 0xffffffffu, NOREG); /* all ones when jj < 32, else 0 */ - lop3lut(e, acc, acc, bit, m, 0xf8); /* acc | (bit & m) */ - iadd3(e, tt, tt, p); - bra_always(e, li); - loop_end(e); - bra_patch_here(e, done); + if (divergent) { + /* R1: the 888a011 mark loop, lane-dependent trip count, inside a region. Each lane + * leaves at its own iteration (the exit jumps to the BSYNC) and the warp reunites there. */ + region_begin(e); + loop_begin(e); + int li = here(e); + isetp_ge_u32(e, j, c32); + region_exit(e, RX_IF_P0); + shl_r(e, bit, one, j); + lop3lut(e, acc, acc, bit, NOREG, 0xfc); /* acc | bit */ + iadd3(e, j, j, p); + bra_always(e, li); + loop_end(e); + region_join(e); + } else { + movrz(e, tt); + loop_begin(e); + int li = here(e); + isetp_ge_u32(e, tt, c32); + int done = bra_fwd_if_p0(e); + iadd3(e, jj, j, tt); + lop3lut(e, sh, jj, c31, NOREG, 0xc0); /* jj & 31 */ + shl_r(e, bit, one, sh); + iadd3(e, t, jj, cm32); + shr_i(e, z, t, 31); /* jj < 32 (j <= p + 31, far below 2^31) */ + imadi(e, m, z, 0xffffffffu, NOREG); /* all ones when jj < 32, else 0 */ + lop3lut(e, acc, acc, bit, m, 0xf8); /* acc | (bit & m) */ + iadd3(e, tt, tt, p); + bra_always(e, li); + loop_end(e); + bra_patch_here(e, done); + } if (staged) iadd3(e, ptr, ptr, c12); else widei(e, ptr, one, 12, ptr); bra_always(e, lp); loop_end(e); - bra_patch_here(e, brk); + if (divergent >= 2) region_join(e); else bra_patch_here(e, brk); iadd3(e, t, w, cm1); shr_i(e, z, t, 31); /* w == 0: the number 1 is not prime */ @@ -487,6 +579,166 @@ static void gen_prime_sieve(Em *e, int mutant, int staged) { tail(e); } +/* ---- reconvergence probes (omega #308, regressions R3..R6 and the resource bound) ---- + * All: a, b = u32 inputs, out = u32, P0 = n; 128 threads, grid ceil(n/128); launched by + * omega_gpu_reconv_probe_u32. A lane past n clamps its index to n-1 so that it loads valid + * data and takes part in the warp-wide SHFL; it exits only after the SHFL, before its store. + * Each probe computes a per-lane r on divergent paths, joins, then + * out[i] = r + 4099 * s, s = r of lane (lane+1) in the same warp (SHFL.DOWN 1; lane 31: own r) + * so a lane that joined late, never, or with the wrong value shows in the neighbour's word too. + * The host oracle is rc_oracle in tests/gpu_reconv_test.c. */ +typedef struct { Pro pr; int n, i, idx, x, y, r, s, one, cm1; } Rp; +static Rp rp_begin(Em *e) { + Rp k; memset(&k, 0, sizeof k); + k.pr = prologue(e); + k.n = V(e); k.i = V(e); k.idx = V(e); k.x = V(e); k.y = V(e); k.r = V(e); k.s = V(e); k.one = V(e); k.cm1 = V(e); + int nm1 = V(e), d = V(e), neg = V(e), off = V64(e); + ldc32(e, k.n, ARG_P0); + movi(e, k.one, 1); movi(e, k.cm1, 0xffffffffu); + imadi(e, k.i, k.pr.cta, OMEGA_GPU_EW_THREADS, k.pr.tid); + iadd3(e, nm1, k.n, k.cm1); /* n - 1 */ + imadi(e, d, k.i, 0xffffffffu, nm1); /* (n - 1) - i, top bit set when i > n - 1 */ + shr_i(e, neg, d, 31); + imadr(e, k.idx, neg, d, k.i); /* i, or n - 1 when past the end */ + addr4(e, off, k.idx, k.pr.pa); ldg(e, k.x, off, k.pr.udesc); + addr4(e, off, k.idx, k.pr.pb); ldg(e, k.y, off, k.pr.udesc); + return k; +} +static void rp_end(Em *e, const Rp *k) { + int out = V(e), off = V64(e); + shfl_down(e, k->s, k->r, 1); + imadi(e, out, k->s, 4099, k->r); + isetp_ge_u32(e, k->i, k->n); + exit_if_p0(e); + addr4(e, off, k->i, k->pr.pc); + stg(e, off, out, k->pr.udesc); + tail(e); +} +/* R3: if/else with sides of different length. x >= y: r = f(x, y) (5 steps); else r = x + 1. + * Math mutant: the long side is one too big. */ +static void gen_rc_diamond(Em *e, int mutant) { + Rp k = rp_begin(e); + int t = V(e), u = V(e); + region_begin(e); + isetp_ge_u32(e, k.x, k.y); + int els = bra_fwd_if_p0(e); + iadd3(e, k.r, k.x, k.one); /* short side */ + region_exit(e, RX_ALWAYS); + bra_patch_here(e, els); + imadi(e, t, k.x, 3, k.y); /* long side: t = 3x + y */ + lop3xor(e, u, t, k.x); /* u = t ^ x */ + imadi(e, k.r, u, 5, k.one); /* r = 5u + 1 */ + shr_i(e, t, k.r, 3); + iadd3(e, k.r, k.r, t); /* r += r >> 3 */ + if (mutant) iadd3(e, k.r, k.r, k.one); + region_join(e); + rp_end(e, &k); +} +/* The lane-dependent loop shared by R4 and R5: v = x & 255, step = (y & 15) + 1, + * while v < 256 { v += step; cnt += 1 }. Math mutant: the bound is 255. Emits the region. */ +static void rc_count_loop(Em *e, const Rp *k, int mutant, int v, int cnt) { + int st = V(e), bound = V(e), c255 = V(e), c15 = V(e); + movi(e, c255, 255); movi(e, c15, 15); movi(e, bound, mutant ? 255 : 256); + lop3lut(e, v, k->x, c255, NOREG, 0xc0); + lop3lut(e, st, k->y, c15, NOREG, 0xc0); + iadd3(e, st, st, k->one); + movrz(e, cnt); + region_begin(e); + loop_begin(e); + int L = here(e); + isetp_ge_u32(e, v, bound); + region_exit(e, RX_IF_P0); + iadd3(e, v, v, st); + iadd3(e, cnt, cnt, k->one); + bra_always(e, L); + loop_end(e); + region_join(e); +} +/* R4: lane-dependent loop exit (break). r = 256 * cnt + v. */ +static void gen_rc_loopbreak(Em *e, int mutant) { + Rp k = rp_begin(e); + int v = V(e), cnt = V(e); + rc_count_loop(e, &k, mutant, v, cnt); + imadi(e, k.r, cnt, 256, v); + rp_end(e, &k); +} +/* R5: nested divergence. Odd x: r = 7 (skips the inner region). Even x: the R4 loop inside the + * outer region, r = 256 * cnt + v + 100. */ +static void gen_rc_nested(Em *e, int mutant) { + Rp k = rp_begin(e); + int v = V(e), cnt = V(e), low = V(e), c100 = V(e); + movi(e, k.r, 7); movi(e, c100, 100); + lop3lut(e, low, k.x, k.one, NOREG, 0xc0); /* x & 1 */ + region_begin(e); + isetp_ge_u32(e, low, k.one); + region_exit(e, RX_IF_P0); + rc_count_loop(e, &k, mutant, v, cnt); + imadi(e, k.r, cnt, 256, v); + iadd3(e, k.r, k.r, c100); + region_join(e); + rp_end(e, &k); +} +/* R6: lanes past the end, a divergent region, then a CTA-wide exchange and the warp SHFL. + * xv = x | 1 computed on divergent paths (even lanes take the branch), every thread stores xv + * to its shared slot, BAR.SYNC, reads its partner's (tid ^ 1), r = 3x + partner_xv. + * Production form: lanes past n are clamped and exit after the SHFL (correct). early_exit + * (mutant 1): lanes past n EXIT first, as a naive kernel would; the emitter refuses that + * (BAR/SHFL after a predicated EXIT). Only the host-test build path (codegen_ir) may bypass the + * refusal, so the simulator can show what the kernel would do (SHFL_EXITED); a launch never can. */ +static void gen_rc_exit_bar(Em *e, int early_exit) { + Rp k = rp_begin(e); + int saddr = V(e), paddr = V(e), yv = V(e), xv = V(e), low = V(e); + if (early_exit) { + e->exit_sync_ok = early_exit == 1 && e->test_path; + isetp_ge_u32(e, k.i, k.n); + exit_if_p0(e); + } + imadi(e, xv, k.x, 1, NOREG); + lop3lut(e, low, k.x, k.one, NOREG, 0xc0); /* x & 1 */ + region_begin(e); + isetp_ge_u32(e, low, k.one); + region_exit(e, RX_IF_P0); /* odd lanes leave */ + iadd3(e, xv, xv, k.one); /* even lanes: xv = x + 1 */ + region_join(e); + imadi(e, saddr, k.pr.tid, 4, NOREG); + sts32(e, saddr, xv); + bar_sync(e); + lop3xor(e, paddr, k.pr.tid, k.one); + imadi(e, paddr, paddr, 4, NOREG); + lds32(e, yv, paddr); + imadi(e, k.r, k.x, 3, yv); + rp_end(e, &k); +} +/* Barrier-register bound: `levels` nested regions, each leaving lanes with x >= y out and adding 1 + * for the others: r = x + levels * (x < y). 16 levels must build (B0..B15); 17 must be refused. */ +static void gen_rc_depth(Em *e, int levels) { + Rp k = rp_begin(e); + imadi(e, k.r, k.x, 1, NOREG); + for (int d = 0; d < levels; d++) { + region_begin(e); + isetp_ge_u32(e, k.x, k.y); + region_exit(e, RX_IF_P0); + iadd3(e, k.r, k.r, k.one); + } + for (int d = 0; d < levels; d++) region_join(e); + rp_end(e, &k); +} +/* Codegen refusals (each must come back CODEGEN_FAIL; a kernel with a split warp at a + * cross-lane step must never reach the chip). */ +static void gen_rc_refuse(Em *e, OmegaGpuEwOp which) { + Rp k = rp_begin(e); + switch (which) { + case OMEGA_GPU_EW_RC_REFUSE_EXIT_IN_REGION: region_begin(e); isetp_ge_u32(e, k.i, k.n); exit_if_p0(e); region_join(e); break; + case OMEGA_GPU_EW_RC_REFUSE_BAR_IN_REGION: region_begin(e); bar_sync(e); region_join(e); break; + case OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION: region_begin(e); shfl_down(e, k.s, k.x, 1); region_join(e); break; + case OMEGA_GPU_EW_RC_REFUSE_UNCLOSED: region_begin(e); isetp_ge_u32(e, k.x, k.y); region_exit(e, RX_IF_P0); break; /* no join */ + case OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY: region_join(e); break; /* BSYNC without BSSY */ + default: e->err = -1; break; + } + imadi(e, k.r, k.x, 1, NOREG); + rp_end(e, &k); +} + /* A value first defined inside a loop body may not be read after the loop * before it is written again: the allocator could have let a later body * temporary share its register, and the last iteration's value would be lost. */ @@ -546,21 +798,40 @@ static void schedule_fixed_pairs(BlackwellIRProgram *p) { } } -static int build_kernel(OmegaGpuEwOp op, int mutant, OmegaBlackwellKernel *kernel, BlackwellIRProgram *prog_out) { +static int build_kernel(OmegaGpuEwOp op, int mutant, OmegaBlackwellKernel *kernel, BlackwellIRProgram *prog_out, int test_path) { BlackwellIRProgram *prog = calloc(1, sizeof *prog); if (!prog) return OMEGA_GPU_EW_CODEGEN_FAIL; omega_bw_ir_init(prog); - Em e; memset(&e, 0, sizeof e); e.p = prog; + Em e; memset(&e, 0, sizeof e); e.p = prog; e.test_path = test_path; + /* mutant 1 = the op's math mutant (as before); 2 = the join (BSYNC) dropped; 3 = the BSSY + * names the wrong join. 2 and 3 only change kernels that open a region. */ + e.rg_mutant = mutant == 2 ? 1 : mutant == 3 ? 2 : 0; + int math = mutant == 1; switch (op) { - case OMEGA_GPU_EW_EX2: gen_ex2(&e, mutant); break; - case OMEGA_GPU_EW_XCHG: gen_xchg(&e, mutant); break; - case OMEGA_GPU_EW_RMSNORM: gen_rmsnorm(&e, mutant); break; - case OMEGA_GPU_EW_ROPE: gen_rope(&e, mutant); break; - case OMEGA_GPU_EW_SWIGLU: gen_swiglu(&e, mutant); break; - case OMEGA_GPU_EW_PRIME_SIEVE: gen_prime_sieve(&e, mutant, 0); break; - case OMEGA_GPU_EW_PRIME_SIEVE_SHARED: gen_prime_sieve(&e, mutant, 1); break; + case OMEGA_GPU_EW_EX2: gen_ex2(&e, math); break; + case OMEGA_GPU_EW_XCHG: gen_xchg(&e, math); break; + case OMEGA_GPU_EW_RMSNORM: gen_rmsnorm(&e, math); break; + case OMEGA_GPU_EW_ROPE: gen_rope(&e, math); break; + case OMEGA_GPU_EW_SWIGLU: gen_swiglu(&e, math); break; + case OMEGA_GPU_EW_PRIME_SIEVE: gen_prime_sieve(&e, math, 0, 0); break; + case OMEGA_GPU_EW_PRIME_SIEVE_SHARED: gen_prime_sieve(&e, math, 1, 0); break; + case OMEGA_GPU_EW_PRIME_SIEVE_DIV1: gen_prime_sieve(&e, math, 0, 1); break; + case OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1: gen_prime_sieve(&e, math, 1, 1); break; + case OMEGA_GPU_EW_PRIME_SIEVE_DIV2: gen_prime_sieve(&e, math, 0, 2); break; + case OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2: gen_prime_sieve(&e, math, 1, 2); break; + case OMEGA_GPU_EW_RC_DIAMOND: gen_rc_diamond(&e, math); break; + case OMEGA_GPU_EW_RC_LOOPBREAK: gen_rc_loopbreak(&e, math); break; + case OMEGA_GPU_EW_RC_NESTED: gen_rc_nested(&e, math); break; + case OMEGA_GPU_EW_RC_EXIT_BAR: gen_rc_exit_bar(&e, math); break; + case OMEGA_GPU_EW_RC_REFUSE_EXIT_SHFL: gen_rc_exit_bar(&e, 2); break; + case OMEGA_GPU_EW_RC_DEPTH16: gen_rc_depth(&e, BW_RECONV_MAX_BAR); break; + case OMEGA_GPU_EW_RC_REFUSE_DEPTH17: gen_rc_depth(&e, BW_RECONV_MAX_BAR + 1); break; + case OMEGA_GPU_EW_RC_REFUSE_EXIT_IN_REGION: case OMEGA_GPU_EW_RC_REFUSE_BAR_IN_REGION: + case OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION: case OMEGA_GPU_EW_RC_REFUSE_UNCLOSED: + case OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY: gen_rc_refuse(&e, op); break; default: e.err = -1; break; } + if (e.rg.depth != 0) e.err = -1; /* a region left open has no join: refuse the build */ schedule_fixed_pairs(prog); int rc = OMEGA_GPU_EW_CODEGEN_FAIL; int ra_rc = (e.err == 0) ? omega_bw_regalloc_solve(prog) : -1; @@ -599,6 +870,7 @@ static int build_kernel(OmegaGpuEwOp op, int mutant, OmegaBlackwellKernel *kerne * resident for the process (cut 4b: no per-call code page). */ typedef struct { int used; int op; int mutant; OmegaBlackwellKernel kernel; NvrmMem code; } Slot; static int g_mutant_op; /* 0 = none */ +static int g_mutant_level = 1; /* the mutant number built for g_mutant_op (1 math, 2 join dropped, 3 join corrupted) */ static Slot g_cache[8]; static OmegaGpuScratch g_a, g_b, g_c; /* staging, grown to the high-water mark, reused per call */ static bool g_hooked; @@ -624,7 +896,13 @@ static void on_session_close(void) { void omega_gpu_elementwise_test_set_mutant(int op) { LOCK(); - g_mutant_op = op; + g_mutant_op = op; g_mutant_level = 1; + cache_clear_locked(); + UNLOCK(); +} +void omega_gpu_elementwise_test_set_mutant_level(int op, int level) { + LOCK(); + g_mutant_op = op; g_mutant_level = level < 1 ? 1 : level > 3 ? 3 : level; cache_clear_locked(); UNLOCK(); } @@ -632,13 +910,13 @@ void omega_gpu_elementwise_test_set_mutant(int op) { /* Caller holds the lock. Builds the kernel if needed; uploads the device copy when a * device is open (upload = false lets the host-only codegen path run without a chip). */ static int kernel_for(OmegaGpuEwOp op, bool upload, Slot **out, bool *hit) { - int mutant = (g_mutant_op == (int)op); + int mutant = (g_mutant_op == (int)op) ? g_mutant_level : 0; Slot *s = NULL; for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) if (g_cache[i].used && g_cache[i].op == (int)op && g_cache[i].mutant == mutant) { s = &g_cache[i]; *hit = true; break; } if (!s) { OmegaBlackwellKernel k; - int rc = build_kernel(op, mutant, &k, NULL); + int rc = build_kernel(op, mutant, &k, NULL, 0); if (rc != OMEGA_GPU_EW_OK) return rc; size_t slot = 0; for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) if (!g_cache[i].used) { slot = i; break; } @@ -661,14 +939,15 @@ int omega_gpu_elementwise_codegen(OmegaGpuEwOp op, const uint32_t *dims, float e (void)dims; (void)eps; /* kernels read their shape from the argument words */ if (!kernel) return OMEGA_GPU_EW_BAD_ARGS; LOCK(); - int mutant = (g_mutant_op == (int)op); + int mutant = (g_mutant_op == (int)op) ? g_mutant_level : 0; UNLOCK(); - return build_kernel(op, mutant, kernel, NULL); + return build_kernel(op, mutant, kernel, NULL, 0); } int omega_gpu_elementwise_codegen_ir(OmegaGpuEwOp op, int mutant, void *prog, OmegaBlackwellKernel *kernel) { if (!kernel) return OMEGA_GPU_EW_BAD_ARGS; - return build_kernel(op, mutant ? 1 : 0, kernel, (BlackwellIRProgram *)prog); + if (mutant < 0 || mutant > 3) return OMEGA_GPU_EW_BAD_ARGS; + return build_kernel(op, mutant, kernel, (BlackwellIRProgram *)prog, 1); } const char *omega_gpu_elementwise_last_error(void) { return omega_gpu_session_last_error(); } @@ -837,6 +1116,15 @@ int omega_gpu_shared_xchg_u32(uint32_t n, const uint32_t *x, uint32_t *out, Omeg if (!x || !out || n == 0 || n % OMEGA_GPU_EW_THREADS != 0) return OMEGA_GPU_EW_BAD_ARGS; return run_1d(OMEGA_GPU_EW_XCHG, n, x, NULL, out, info); } +int omega_gpu_reconv_probe_u32(OmegaGpuEwOp op, uint32_t n, const uint32_t *a, const uint32_t *b, uint32_t *out, OmegaGpuEwInfo *info) { + if (!a || !b || !out || n == 0) return OMEGA_GPU_EW_BAD_ARGS; + switch (op) { + case OMEGA_GPU_EW_RC_DIAMOND: case OMEGA_GPU_EW_RC_LOOPBREAK: case OMEGA_GPU_EW_RC_NESTED: + case OMEGA_GPU_EW_RC_EXIT_BAR: case OMEGA_GPU_EW_RC_DEPTH16: break; + default: return OMEGA_GPU_EW_BAD_ARGS; + } + return run_1d(op, n, a, b, out, info); +} const char *omega_gpu_elementwise_rc_name(int rc) { switch (rc) { diff --git a/src/omega_gpu_elementwise_api.h b/src/omega_gpu_elementwise_api.h index 3d168fbd2..8b92c839d 100644 --- a/src/omega_gpu_elementwise_api.h +++ b/src/omega_gpu_elementwise_api.h @@ -63,7 +63,28 @@ typedef enum { OMEGA_GPU_EW_PRIME_SIEVE, /* prime race (2026-10-05): word-parallel odd-only sieve, codegen only * here; launched by bench/prime_race/gb10_native.c (argument words in * gen_prime_sieve's comment) */ - OMEGA_GPU_EW_PRIME_SIEVE_SHARED /* the same sieve with the table staged in shared memory (C3) */ + OMEGA_GPU_EW_PRIME_SIEVE_SHARED, /* the same sieve with the table staged in shared memory (C3) */ + /* Structured warp reconvergence (omega #308). The four sieves below are the production sieve + * with its lane-dependent loops put back inside BSSY/BSYNC regions (regression R1): DIV1 wraps + * the original mark loop (omega 888a011), DIV2 also wraps the prime loop (nested regions). */ + OMEGA_GPU_EW_PRIME_SIEVE_DIV1, + OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1, + OMEGA_GPU_EW_PRIME_SIEVE_DIV2, + OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2, + /* Probes for omega_gpu_reconv_probe_u32 (u32 in a, b; u32 out; oracle in tests/gpu_reconv_test.c) */ + OMEGA_GPU_EW_RC_DIAMOND, /* R3: if/else, sides of different length, SHFL after the join */ + OMEGA_GPU_EW_RC_LOOPBREAK, /* R4: loop with a lane-dependent break */ + OMEGA_GPU_EW_RC_NESTED, /* R5: a lane-dependent loop region inside an if region */ + OMEGA_GPU_EW_RC_EXIT_BAR, /* R6: lanes past the end + STS/BAR.SYNC/LDS exchange + SHFL, lanes exit last */ + OMEGA_GPU_EW_RC_DEPTH16, /* 16 nested regions: every barrier register B0..B15 in use */ + /* Codegen refusals: omega_gpu_elementwise_codegen_ir must return CODEGEN_FAIL for each */ + OMEGA_GPU_EW_RC_REFUSE_DEPTH17, /* a 17th nested region: no barrier register left */ + OMEGA_GPU_EW_RC_REFUSE_EXIT_SHFL, /* lanes EXIT, then BAR.SYNC/SHFL on the rest */ + OMEGA_GPU_EW_RC_REFUSE_EXIT_IN_REGION, /* EXIT inside an open region */ + OMEGA_GPU_EW_RC_REFUSE_BAR_IN_REGION, /* BAR.SYNC inside an open region */ + OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION, /* SHFL inside an open region */ + OMEGA_GPU_EW_RC_REFUSE_UNCLOSED, /* region begun, never joined */ + OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY /* BSYNC with no BSSY */ } OmegaGpuEwOp; /* Shape rules (checked, refused with BAD_ARGS / TOO_LARGE): @@ -108,6 +129,11 @@ int omega_gpu_swiglu_f32(uint32_t n, const float *gate, const float *up, float * /* Probes. ex2: out[i] = 2^x[i]. xchg: out[i] = x[i ^ (OMEGA_GPU_EW_THREADS-1)] within each CTA. */ int omega_gpu_ex2_f32(uint32_t n, const float *x, float *out, OmegaGpuEwInfo *info); int omega_gpu_shared_xchg_u32(uint32_t n, const uint32_t *x, uint32_t *out, OmegaGpuEwInfo *info); +/* Reconvergence probes (op = one of OMEGA_GPU_EW_RC_DIAMOND .. OMEGA_GPU_EW_RC_DEPTH16, else + * BAD_ARGS). n >= 1, any n: lanes past the end take part in the warp step and exit before + * their store. out[i] = r_i + 4099 * r_(i+1 in warp); r per op in src/omega_gpu_elementwise_api.c. */ +int omega_gpu_reconv_probe_u32(OmegaGpuEwOp op, uint32_t n, const uint32_t *a, const uint32_t *b, + uint32_t *out, OmegaGpuEwInfo *info); /* Generate (without launching) the kernel for one op. Kernels read their shape * from the argument words, so one kernel per op serves every shape; dims and eps @@ -117,7 +143,9 @@ int omega_gpu_shared_xchg_u32(uint32_t n, const uint32_t *x, uint32_t *out, Omeg int omega_gpu_elementwise_codegen(OmegaGpuEwOp op, const uint32_t *dims, float eps, OmegaBlackwellKernel *kernel); -/* Same, with the mutant chosen by the caller (0 = production kernel) and, when prog is +/* Same, with the mutant chosen by the caller (0 = production kernel, 1 = the op's math mutant, + * 2 = the reconvergence join (BSYNC) dropped, 3 = the BSSY names the wrong join; 2 and 3 change + * only kernels that open a region, the host simulator must catch both) and, when prog is * not NULL, a copy of the allocated IR program (a BlackwellIRProgram, post register * allocation) for a host IR simulator. Does not touch the device or the kernel cache. * Caller frees kernel->code. */ @@ -131,6 +159,10 @@ int omega_gpu_elementwise_codegen_ir(OmegaGpuEwOp op, int mutant, void *prog, * RMSNORM: the warp tree drops its last SHFL step (half the lanes go missing). * ROPE: out0 = q0*cos + q1*sin. SWIGLU: exp(+g) instead of exp(-g). */ void omega_gpu_elementwise_test_set_mutant(int op); +/* TEST ONLY. Like test_set_mutant, choosing the mutant number (1 math, 2 join dropped, 3 join + * corrupted) that the next launch of `op` builds; lets a chip test observe what a missing + * reconvergence does (the simulator must have flagged it first). */ +void omega_gpu_elementwise_test_set_mutant_level(int op, int level); const char *omega_gpu_elementwise_rc_name(int rc); void omega_gpu_elementwise_cache_clear(void); diff --git a/tests/.crumb b/tests/.crumb index 0484543a0..508d059b7 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -254,7 +254,7 @@ }, { "digest": "810c8881a76b9617e39a887e006c69f51301e96ebcdda35478a7060fc8788b75", - "last_commit": "96748a2", + "last_commit": "f816473", "name": "runtime", "purpose": "Directory tests/runtime. Purpose not yet described by a human." }, @@ -284,14 +284,14 @@ } ], "children_root": "sha256:cc7551dc7594896ade97e5a5f78205cd93aad4878acd6e114b6b188e02ff10e6", - "compiled_at": "2026-10-06T00:26:33Z", + "compiled_at": "2026-10-06T01:57:59Z", "compiler": "crumb-compile/1", - "digest": "sha256:c9825de4f77620c738f1cac290238b55040200959e0938ac03fa41a3c95c488d", + "digest": "sha256:03e780fc46936d8b534e4992f9d87e3ce95711a40fdfe0128a96328410e3af5b", "evidence_files": 11, "evidence_root": "sha256:d5c1bceb4d57ef0eb42797c58801b5926509711614c3e38bb7105b6efa3942f2", - "files": 810, - "generated_at_commit": "96748a2078862997c4ee423797ed4d1a99dcf300", - "source_tree": "sha256:7cb4aeb7d1d40a33eb72f6873af832428d8c6c91bcf40b01eb8d231560ad6977" + "files": 812, + "generated_at_commit": "1f4a3c7821d769a99ce1fcc896ac8ee30a33ea69", + "source_tree": "sha256:c05002c05a6afef20cc155f921d19c6041975d629317e083ea3a91a2f568ec7d" }, "owner_lane": "", "provenance": { diff --git a/tests/bw_warp_sim.h b/tests/bw_warp_sim.h new file mode 100644 index 000000000..328094cb0 --- /dev/null +++ b/tests/bw_warp_sim.h @@ -0,0 +1,313 @@ +/* bw_warp_sim.h: host SIMT simulator for the Blackwell IR (omega #308). + * + * Models what the earlier per-thread simulators could not: a warp of 32 lanes with an + * active mask, divergence at predicated branches, structured reconvergence through the + * barrier registers (BSSY arms Bn for the lanes executing it and names the join; BSYNC + * parks arriving lanes until every armed lane that has not exited is there, then the + * warp continues as one), nested regions, and lane participation in SHFL and BAR.SYNC. + * + * A warp is a set of fragments (pc, lane mask). A predicated branch whose lanes disagree + * splits the fragment; the scheduler then runs one fragment at a time in a chosen order + * (lowest pc first or highest pc first; tests run both), which is the freedom the chip's + * scheduler also has. Fragments only merge at a BSYNC. Errors (all fatal, first one wins): + * WS_E_SHFL_SPLIT a SHFL reached by a fragment that is not the whole live warp + * (the chip reads inactive lanes: undefined; attention 2026-10-04) + * WS_E_SHFL_EXITED a SHFL on a warp some of whose lanes already exited (undefined too) + * WS_E_BAR_SPLIT (defined, never raised) a BAR.SYNC reached by a split warp is allowed: + * since Volta the CTA barrier counts arriving threads, not warps (CUDA + * programming guide, independent thread scheduling; INFERRED for sm_121, + * not chip-verified by us). The simulator parks each fragment, releases + * them together, counts the event in bar_split_arrivals, and leaves the + * fragments split: a barrier is not a reconvergence point, so a SHFL after + * it still fails with SHFL_SPLIT unless a BSYNC rejoined the warp. + * WS_E_BSYNC_UNARMED BSYNC on a barrier no BSSY armed + * WS_E_BSSY_REARM BSSY on a barrier still armed (lanes may still be heading to its join) + * WS_E_JOIN_MISMATCH BSYNC at an address other than the one the BSSY named minus one + * WS_E_NONMEMBER a lane reached BSYNC Bn without having executed its BSSY + * WS_E_STUCK lanes parked at a BSYNC that can never complete (a join was dropped, + * an exit jumped past it, or a target was corrupted) + * WS_E_NEVER_JOINED the warp finished with a barrier still armed + * WS_E_SELF_BRANCH, WS_E_PC, WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS + * Every error names the pc. Memory: global addresses are host pointers (optionally range + * checked); shared memory is a per-CTA buffer poisoned at CTA start so a read of a word + * nobody wrote shows up in the oracle comparison. + * + * Per-lane ALU semantics are those of the two earlier simulators (tests/gpu_attention_test.c, + * bench/prime_race/tests/gb10_sieve_host_test.c). EX2 and RCP are exact here; the chip's + * are approximate. LDCU64 (memory descriptor) is not modelled. */ +#ifndef BW_WARP_SIM_H +#define BW_WARP_SIM_H + +#include "omega_blackwell_codegen.h" +#include +#include +#include +#include +#include + +#define WS_WARP 32 +#define WS_MAX_THREADS 256 +#define WS_MAX_WARPS (WS_MAX_THREADS / WS_WARP) +#define WS_MAX_FRAGS 64 +#define WS_NBAR BW_RECONV_MAX_BAR +#define WS_MAX_RANGES 8 + +enum { + WS_OK = 0, WS_E_SHFL_SPLIT, WS_E_SHFL_EXITED, WS_E_BAR_SPLIT, WS_E_BSYNC_UNARMED, WS_E_BSSY_REARM, + WS_E_JOIN_MISMATCH, WS_E_NONMEMBER, WS_E_STUCK, WS_E_NEVER_JOINED, WS_E_SELF_BRANCH, WS_E_PC, + WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS, WS_E_BAR_TIMEOUT, WS_E_STEPS +}; +static const char *ws_err_name(int e) { + static const char *const n[] = { "OK", "SHFL_SPLIT", "SHFL_EXITED", "BAR_SPLIT", "BSYNC_UNARMED", "BSSY_REARM", + "JOIN_MISMATCH", "NONMEMBER", "STUCK", "NEVER_JOINED", "SELF_BRANCH", "PC", "OP", "SHIFT", "MEM", "SMEM", + "FRAGS", "BAR_TIMEOUT", "STEPS" }; + return (e >= 0 && e < (int)(sizeof n / sizeof n[0])) ? n[e] : "?"; +} + +typedef struct { uint32_t r[256]; uint8_t p0; } WsLane; +typedef struct { int pc; uint32_t mask; int parked; /* waiting at a BAR.SYNC */ } WsFrag; +typedef struct { int valid; int target; uint32_t members, arrived; } WsBar; +typedef struct { + WsLane lane[WS_WARP]; + WsFrag frag[WS_MAX_FRAGS]; + int nfrag; + WsBar bar[WS_NBAR]; + uint32_t exited; +} WsWarp; +typedef struct { uintptr_t lo, hi; } WsRange; + +typedef struct { + const BlackwellIRProgram *p; + uint32_t cbank[256]; /* constant bank 0 words (byte offset / 4) */ + uint32_t threads; /* per CTA, a multiple of 32, <= WS_MAX_THREADS */ + uint8_t *smem; /* per-CTA shared memory buffer (poisoned at CTA start) */ + uint32_t smem_bytes; + WsRange ranges[WS_MAX_RANGES]; int nranges; /* when > 0, every global access must fall in one */ + int order; /* 0 = lowest pc first, 1 = highest pc first */ + int trace; /* print every issue of warp 0 of CTA (0,0) */ + /* results */ + int err; int err_pc; char msg[200]; + uint64_t warp_issues; /* fragment-instruction issues (the chip's cost unit for divergence) */ + uint64_t lane_issues; /* lanes active over those issues */ + uint64_t splits; /* branches that split a fragment */ + uint64_t bar_split_arrivals; /* BAR.SYNC reached by a fragment that is not the whole live warp */ + int max_frags, peak_bars; +} WsSim; + +static void ws_fail(WsSim *s, int err, int pc, const char *what) { + if (s->err) return; + s->err = err; s->err_pc = pc; + snprintf(s->msg, sizeof s->msg, "%s at pc %d: %s", ws_err_name(err), pc, what ? what : ""); +} + +static uint32_t ws_rd(const WsLane *l, int v, const BlackwellIRProgram *p) { if (v < 0) return 0; int ph = p->regalloc.vreg_to_phys[v]; return ph < 0 ? 0 : l->r[ph & 0xff]; } +static uint64_t ws_rd64(const WsLane *l, int v, const BlackwellIRProgram *p) { if (v < 0) return 0; int ph = p->regalloc.vreg_to_phys[v] & 0xff; return (uint64_t)l->r[ph] | ((uint64_t)l->r[(ph + 1) & 0xff] << 32); } +static void ws_wr(WsLane *l, int v, uint32_t x, const BlackwellIRProgram *p) { if (v < 0) return; int ph = p->regalloc.vreg_to_phys[v]; if (ph >= 0) l->r[ph & 0xff] = x; } +static void ws_wr64(WsLane *l, int v, uint64_t x, const BlackwellIRProgram *p) { if (v < 0) return; int ph = p->regalloc.vreg_to_phys[v] & 0xff; l->r[ph] = (uint32_t)x; l->r[(ph + 1) & 0xff] = (uint32_t)(x >> 32); } +static float ws_u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } +static uint32_t ws_f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } +static uint32_t ws_lut3(uint32_t lut, uint32_t a, uint32_t b, uint32_t c) { + uint32_t r = 0; + for (int i = 0; i < 8; i++) if (lut >> i & 1) r |= ((i & 4) ? a : ~a) & ((i & 2) ? b : ~b) & ((i & 1) ? c : ~c); + return r; +} +static int ws_mem_ok(WsSim *s, uint64_t a, unsigned bytes, int pc) { + if (s->nranges == 0) return 1; + for (int i = 0; i < s->nranges; i++) if (a >= s->ranges[i].lo && a + bytes <= s->ranges[i].hi) return 1; + ws_fail(s, WS_E_MEM, pc, "global access outside the declared buffers"); return 0; +} +static uint32_t *ws_smem(WsSim *s, uint32_t a, int pc) { + if ((a & 3) || a + 4 > s->smem_bytes) { ws_fail(s, WS_E_SMEM, pc, "shared access past the declared size"); return NULL; } + return (uint32_t *)(s->smem + a); +} +static int ws_popc(uint32_t m) { return __builtin_popcount(m); } + +/* One lane, one non-control instruction. */ +static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc, uint32_t tid, uint32_t cx, uint32_t cy) { + const BlackwellIRProgram *p = s->p; + uint32_t a = ws_rd(l, in->src1_vreg, p), b = ws_rd(l, in->src2_vreg, p), c = ws_rd(l, in->src3_vreg, p); + switch (in->op) { + case BW_IR_NOP: break; + case BW_IR_S2R: ws_wr(l, in->dst_vreg, in->imm == BW_SR_TID_X ? tid : in->imm == BW_SR_CTAID_X ? cx : in->imm == BW_SR_CTAID_Y ? cy : 0, p); break; + case BW_IR_LDC: ws_wr(l, in->dst_vreg, s->cbank[(in->imm / 4) & 0xff], p); break; + case BW_IR_LDC64: ws_wr64(l, in->dst_vreg, (uint64_t)s->cbank[(in->imm / 4) & 0xff] | ((uint64_t)s->cbank[(in->imm / 4 + 1) & 0xff] << 32), p); break; + case BW_IR_LDCU64: case BW_IR_LDCU: break; /* memory descriptor: not modelled */ + case BW_IR_MOV_IMM: ws_wr(l, in->dst_vreg, in->imm, p); break; + case BW_IR_MOV_RZ: ws_wr(l, in->dst_vreg, 0, p); break; + case BW_IR_IMAD: ws_wr(l, in->dst_vreg, in->src2_vreg >= 0 ? a * b + c : a * in->imm + c, p); break; + case BW_IR_IMAD_WIDE: ws_wr64(l, in->dst_vreg, (uint64_t)a * in->imm + ws_rd64(l, in->src3_vreg, p), p); break; + case BW_IR_IMAD_HI_U32: ws_wr(l, in->dst_vreg, (uint32_t)(((uint64_t)a * b) >> 32) + c, p); break; + case BW_IR_IADD3: ws_wr(l, in->dst_vreg, a + b, p); break; /* the encoder's IADD3 has Rc = RZ */ + case BW_IR_SHF_R: ws_wr(l, in->dst_vreg, a >> (in->imm & 31), p); break; + case BW_IR_SHF_L_U32: if (b >= 32) { ws_fail(s, WS_E_SHIFT, pc, "SHF.L.U32 amount >= 32 (not chip-verified)"); return; } ws_wr(l, in->dst_vreg, a << b, p); break; + case BW_IR_LOP3_AND: ws_wr(l, in->dst_vreg, a & in->imm, p); break; + case BW_IR_LOP3_XOR: ws_wr(l, in->dst_vreg, a ^ b, p); break; + case BW_IR_LOP3_LUT: ws_wr(l, in->dst_vreg, ws_lut3(in->imm & 0xff, a, b, c), p); break; + case BW_IR_ISETP_GE_U32: l->p0 = a >= b; break; + case BW_IR_LDG_E: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; uint32_t x; memcpy(&x, (const void *)(uintptr_t)ad, 4); ws_wr(l, in->dst_vreg, x, p); break; } + case BW_IR_LDG_E_U16: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 2, pc)) return; uint16_t x; memcpy(&x, (const void *)(uintptr_t)ad, 2); ws_wr(l, in->dst_vreg, x, p); break; } + case BW_IR_STG_E: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; memcpy((void *)(uintptr_t)ad, &b, 4); break; } + case BW_IR_LDS32: { uint32_t *m = ws_smem(s, a, pc); if (!m) return; ws_wr(l, in->dst_vreg, *m, p); break; } + case BW_IR_STS32: { uint32_t *m = ws_smem(s, a, pc); if (!m) return; *m = b; break; } + case BW_IR_FADD: ws_wr(l, in->dst_vreg, ws_f2u(ws_u2f(a) + ws_u2f(b)), p); break; + case BW_IR_FSUB: ws_wr(l, in->dst_vreg, ws_f2u(ws_u2f(a) - ws_u2f(b)), p); break; + case BW_IR_FMUL: ws_wr(l, in->dst_vreg, ws_f2u(ws_u2f(a) * ws_u2f(b)), p); break; + case BW_IR_FFMA: ws_wr(l, in->dst_vreg, ws_f2u(fmaf(ws_u2f(a), ws_u2f(b), ws_u2f(c))), p); break; + case BW_IR_FMNMX_MAX: ws_wr(l, in->dst_vreg, ws_f2u(fmaxf(ws_u2f(a), ws_u2f(b))), p); break; + case BW_IR_FMNMX_MIN: ws_wr(l, in->dst_vreg, ws_f2u(fminf(ws_u2f(a), ws_u2f(b))), p); break; + case BW_IR_MUFU_EX2: ws_wr(l, in->dst_vreg, ws_f2u(exp2f(ws_u2f(a))), p); break; + case BW_IR_MUFU_RCP: ws_wr(l, in->dst_vreg, ws_f2u(1.0f / ws_u2f(a)), p); break; + case BW_IR_MUFU_RSQ: ws_wr(l, in->dst_vreg, ws_f2u(1.0f / sqrtf(ws_u2f(a))), p); break; + default: ws_fail(s, WS_E_OP, pc, "opcode not modelled"); return; + } +} + +static int ws_pick(const WsSim *s, const WsWarp *w) { + int best = -1; + for (int i = 0; i < w->nfrag; i++) { + if (w->frag[i].mask == 0 || w->frag[i].parked) continue; + if (best < 0) { best = i; continue; } + if (s->order == 0 ? w->frag[i].pc < w->frag[best].pc : w->frag[i].pc > w->frag[best].pc) best = i; + } + return best; +} +static void ws_drop_frag(WsWarp *w, int i) { w->frag[i] = w->frag[--w->nfrag]; } +static int ws_add_frag(WsSim *s, WsWarp *w, int pc, uint32_t mask) { + if (mask == 0) return 0; + if (w->nfrag >= WS_MAX_FRAGS) { ws_fail(s, WS_E_FRAGS, pc, "too many fragments"); return -1; } + w->frag[w->nfrag].pc = pc; w->frag[w->nfrag].mask = mask; w->frag[w->nfrag].parked = 0; w->nfrag++; + if (w->nfrag > s->max_frags) s->max_frags = w->nfrag; + return 0; +} + +/* Runs one warp until it has no runnable fragment (finished, parked at BAR.SYNC, or stuck). + * Returns 1 when parked at BAR.SYNC, 0 when finished, -1 on error. */ +static int ws_run_warp(WsSim *s, WsWarp *w, uint32_t warp, uint32_t cx, uint32_t cy, int trace) { + const BlackwellIRProgram *p = s->p; + const uint32_t live_all = 0xffffffffu; + for (uint64_t guard = 0;; guard++) { + if (s->err) return -1; + if (guard > 2000000000ull) { ws_fail(s, WS_E_STEPS, -1, "step budget"); return -1; } + int fi = ws_pick(s, w); + if (fi < 0) { + /* nothing runnable: parked at the CTA barrier, finished, or lanes waiting at a BSYNC + * that can never complete */ + for (int i = 0; i < w->nfrag; i++) if (w->frag[i].parked) return 1; + for (int b = 0; b < WS_NBAR; b++) if (w->bar[b].valid) { + if (w->bar[b].arrived) { ws_fail(s, WS_E_STUCK, w->bar[b].target - 1, "lanes parked at BSYNC, the rest never arrive"); return -1; } + ws_fail(s, WS_E_NEVER_JOINED, w->bar[b].target - 1, "warp finished with a barrier still armed"); return -1; + } + return 0; + } + WsFrag *f = &w->frag[fi]; + int pc = f->pc; + if (pc < 0 || (size_t)pc >= p->count) { ws_fail(s, WS_E_PC, pc, "pc outside the program"); return -1; } + const BlackwellIRInsn *in = &p->insns[pc]; + uint32_t mask = f->mask; + s->warp_issues++; s->lane_issues += (uint64_t)ws_popc(mask); + if (trace) printf("W%u pc=%d op=%d mask=%08x frags=%d\n", warp, pc, (int)in->op, mask, w->nfrag); + uint32_t live = live_all & ~w->exited; + uint32_t pmask = 0; + for (int L = 0; L < WS_WARP; L++) if ((mask >> L & 1) && w->lane[L].p0) pmask |= 1u << L; + uint32_t cond = in->predicate_p0 ? (in->predicate_not ? (mask & ~pmask) : (mask & pmask)) : mask; + switch (in->op) { + case BW_IR_EXIT: { + w->exited |= cond; + f->mask = mask & ~cond; + if (f->mask == 0) ws_drop_frag(w, fi); else f->pc = pc + 1; + break; + } + case BW_IR_BRA: { + int32_t delta = (int32_t)in->imm; + uint32_t taken = cond, fall = mask & ~cond; + if (taken && delta == 0) { ws_fail(s, WS_E_SELF_BRANCH, pc, "branch to itself"); return -1; } + if (taken && fall) s->splits++; + if (taken == 0) { f->pc = pc + 1; break; } + if (fall == 0) { f->pc = pc + delta; break; } + f->mask = fall; f->pc = pc + 1; + if (ws_add_frag(s, w, pc + delta, taken) < 0) return -1; + break; + } + case BW_IR_BSSY: { + WsBar *b = &w->bar[in->bar_reg & (WS_NBAR - 1)]; + if (in->bar_reg >= WS_NBAR) { ws_fail(s, WS_E_OP, pc, "barrier id"); return -1; } + if (b->valid) { ws_fail(s, WS_E_BSSY_REARM, pc, "BSSY on an armed barrier"); return -1; } + b->valid = 1; b->target = pc + (int32_t)in->imm; b->members = cond; b->arrived = 0; + int armed = 0; for (int k = 0; k < WS_NBAR; k++) armed += w->bar[k].valid; + if (armed > s->peak_bars) s->peak_bars = armed; + f->pc = pc + 1; + break; + } + case BW_IR_BSYNC: { + if (in->bar_reg >= WS_NBAR) { ws_fail(s, WS_E_OP, pc, "barrier id"); return -1; } + WsBar *b = &w->bar[in->bar_reg]; + if (!b->valid) { ws_fail(s, WS_E_BSYNC_UNARMED, pc, "BSYNC without a BSSY"); return -1; } + if (cond & ~b->members) { ws_fail(s, WS_E_NONMEMBER, pc, "a lane at BSYNC did not execute the BSSY"); return -1; } + b->arrived |= cond; + uint32_t rest = mask & ~cond; /* a predicated-off lane walks on */ + if (rest) { f->mask = rest; f->pc = pc + 1; } else ws_drop_frag(w, fi); + if (b->arrived == (b->members & ~w->exited)) { + if (b->target != pc + 1) { ws_fail(s, WS_E_JOIN_MISMATCH, pc, "BSSY named a different join point"); return -1; } + uint32_t joined = b->arrived; b->valid = 0; b->arrived = 0; b->members = 0; + if (ws_add_frag(s, w, pc + 1, joined) < 0) return -1; + } + break; + } + case BW_IR_SHFL_DOWN: { + if (w->exited) { ws_fail(s, WS_E_SHFL_EXITED, pc, "SHFL on a warp with exited lanes"); return -1; } + if (mask != live) { ws_fail(s, WS_E_SHFL_SPLIT, pc, "SHFL on a split warp"); return -1; } + uint32_t src[WS_WARP]; + for (int L = 0; L < WS_WARP; L++) src[L] = ws_rd(&w->lane[L], in->src1_vreg, p); + for (int L = 0; L < WS_WARP; L++) { int from = L + (int)(in->imm & 31); ws_wr(&w->lane[L], in->dst_vreg, from < WS_WARP ? src[from] : src[L], p); } + f->pc = pc + 1; + break; + } + case BW_IR_BAR_SYNC: { + if (mask != live) s->bar_split_arrivals++; + f->parked = 1; /* released by ws_run when every live lane of the CTA has arrived */ + break; + } + default: { + for (int L = 0; L < WS_WARP; L++) if (mask >> L & 1) { + ws_lane_exec(s, &w->lane[L], in, pc, warp * WS_WARP + (uint32_t)L, cx, cy); + if (s->err) return -1; + } + f->pc = pc + 1; + break; + } + } + } +} + +/* Runs every CTA of the grid. Returns s->err (0 = clean). */ +static int ws_run(WsSim *s, uint32_t grid_x, uint32_t grid_y) { + s->err = 0; s->msg[0] = 0; s->warp_issues = s->lane_issues = s->splits = s->bar_split_arrivals = 0; s->max_frags = 0; s->peak_bars = 0; + if (!s->p || s->threads == 0 || s->threads % WS_WARP || s->threads > WS_MAX_THREADS) { ws_fail(s, WS_E_OP, -1, "bad simulator configuration"); return s->err; } + static WsWarp warps[WS_MAX_WARPS]; + uint32_t nw = s->threads / WS_WARP; + for (uint32_t cy = 0; cy < grid_y && !s->err; cy++) for (uint32_t cx = 0; cx < grid_x && !s->err; cx++) { + memset(warps, 0, sizeof(WsWarp) * nw); + if (s->smem && s->smem_bytes) for (uint32_t i = 0; i + 4 <= s->smem_bytes; i += 4) { uint32_t poison = 0xfeedfaceu; memcpy(s->smem + i, &poison, 4); } + for (uint32_t w = 0; w < nw; w++) { warps[w].nfrag = 1; warps[w].frag[0].pc = 0; warps[w].frag[0].mask = 0xffffffffu; warps[w].frag[0].parked = 0; } + int trace = s->trace && cx == 0 && cy == 0; + /* Barrier phases: every warp runs until all of its fragments are parked at BAR.SYNC or it + * has finished (exited lanes do not hold the barrier up: INFERRED, see WS_E_BAR_SPLIT). + * Then every parked fragment is released and the next phase starts. */ + for (int phase = 0; ; phase++) { + int parked = 0; + for (uint32_t w = 0; w < nw && !s->err; w++) { + if (warps[w].nfrag == 0) continue; + int r = ws_run_warp(s, &warps[w], w, cx, cy, trace && w == 0); + if (r < 0) break; + if (r == 1) parked++; + } + if (s->err || parked == 0) break; /* error, or every warp finished */ + for (uint32_t w = 0; w < nw; w++) for (int i = 0; i < warps[w].nfrag; i++) + if (warps[w].frag[i].parked) { warps[w].frag[i].parked = 0; warps[w].frag[i].pc++; } + if (phase > 100000) { ws_fail(s, WS_E_BAR_TIMEOUT, -1, "barrier phases"); break; } + } + } + return s->err; +} + +#endif /* BW_WARP_SIM_H */ diff --git a/tests/gpu_attention_test.c b/tests/gpu_attention_test.c index 9fb8abbbe..1f0019a50 100644 --- a/tests/gpu_attention_test.c +++ b/tests/gpu_attention_test.c @@ -30,6 +30,7 @@ #include "omega_gpu_attention_api.h" #include "omega_gpu_session.h" #include "omega_blackwell_codegen.h" +#include "bw_warp_sim.h" /* omega #308: SIMT warp simulator for --divergent */ #include #include #include @@ -40,6 +41,7 @@ static int g_checks, g_failed; static int g_sim_mode; /* --sim: the host simulator never opens the device */ +static int g_divergent; /* --divergent (omega #308 R2): the j >= ctx branch inside a BSSY/BSYNC region */ #define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } @@ -185,8 +187,9 @@ static void host_only(void) { free(pool); CHECK(strcmp(omega_gpu_attention_rc_name(OMEGA_GPU_ATTN_UNWRITTEN), "UNWRITTEN") == 0, "rc name"); - static const char *const must_bf16[] = { "MUFU.EX2", "MUFU.RCP", "FMNMX", "BAR.SYNC", "SHFL.DOWN", "@P0 BRA", "@!P0 BRA", "LDG.E.U16", "LDG.E ", "LDS R", "STS [R", "STG.E", "LOP3.LUT", "SHF.R.U32.HI" }; - static const char *const must_f32[] = { "MUFU.EX2", "MUFU.RCP", "FMNMX", "BAR.SYNC", "SHFL.DOWN", "@P0 BRA", "@!P0 BRA", "LDG.E ", "LDS R", "STS [R", "STG.E" }; + /* with --divergent the listing must also show the region ops (last two entries) */ + static const char *const must_bf16[] = { "MUFU.EX2", "MUFU.RCP", "FMNMX", "BAR.SYNC", "SHFL.DOWN", "@P0 BRA", "@!P0 BRA", "LDG.E.U16", "LDG.E ", "LDS R", "STS [R", "STG.E", "LOP3.LUT", "SHF.R.U32.HI", "BSSY", "BSYNC" }; + static const char *const must_f32[] = { "MUFU.EX2", "MUFU.RCP", "FMNMX", "BAR.SYNC", "SHFL.DOWN", "@P0 BRA", "@!P0 BRA", "LDG.E ", "LDS R", "STS [R", "STG.E", "BSSY", "BSYNC" }; for (int f32 = 0; f32 < 2; f32++) { const char *name = f32 ? "gqa_f32" : "paged_bf16"; OmegaBlackwellKernel k; memset(&k, 0, sizeof k); @@ -214,7 +217,7 @@ static void host_only(void) { memset(&k, 0, sizeof k); 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); + CHECK(nvdisasm_check(f32 != 0, name, f32 ? must_f32 : must_bf16, (f32 ? 11 : 14) + (g_divergent ? 2 : 0)) == 0, "%s nvdisasm listing decodes and shows the expected instructions", name); } } @@ -772,16 +775,71 @@ static int sweep(const char *out_path) { return 0; } +/* ------------------------------------------------------------ warp simulator (omega #308) + * The per-thread simulator above has no model of a warp (its SHFL rule is "one path across the + * warp or error 7"). With --divergent the kernel carries a real lane-dependent branch inside a + * BSSY/BSYNC region, and every launch runs through tests/bw_warp_sim.h instead: 32-lane + * fragments, barrier registers, SHFL/BAR participation. Each launch runs in both fragment + * orders and the two outputs must be bit-identical. */ +static uint64_t g_ws_issues, g_ws_launches; +static int ws_attn_launch(const void *progv, const OmegaGpuAttnLaunch *L, OmegaGpuAttnInfo *info) { + const BlackwellIRProgram *p = progv; + uint32_t *outw = L->out; size_t nw = L->out_bytes / 4; + uint32_t *copy = malloc(L->out_bytes); + int err = 0; char msg[240] = ""; + for (int order = 0; order < 2 && !err && copy; order++) { + WsSim s; memset(&s, 0, sizeof s); + s.p = p; s.threads = SIM_T; s.smem = sim_shared; s.smem_bytes = sizeof sim_shared; s.order = order; + uint64_t qa = (uintptr_t)L->q, pa = (uintptr_t)L->pool, oa = (uintptr_t)L->out, ta = (uintptr_t)L->tab; + s.cbank[0x380 / 4] = (uint32_t)qa; s.cbank[0x380 / 4 + 1] = (uint32_t)(qa >> 32); + s.cbank[0x388 / 4] = (uint32_t)pa; s.cbank[0x388 / 4 + 1] = (uint32_t)(pa >> 32); + s.cbank[0x390 / 4] = (uint32_t)oa; s.cbank[0x390 / 4 + 1] = (uint32_t)(oa >> 32); + s.cbank[0x398 / 4] = (uint32_t)ta; s.cbank[0x398 / 4 + 1] = (uint32_t)(ta >> 32); + memcpy(&s.cbank[0x3a0 / 4], L->params, sizeof L->params); + for (size_t i = 0; i < nw; i++) outw[i] = 0xffbadbadu; + err = ws_run(&s, L->grid_x, L->grid_y); + if (err) { snprintf(msg, sizeof msg, "order %d: %s", order, s.msg); break; } + g_ws_issues += s.warp_issues; + if (order == 0) memcpy(copy, outw, L->out_bytes); + else if (memcmp(copy, outw, L->out_bytes) != 0) { err = -1; snprintf(msg, sizeof msg, "the fragment order changed the result"); } + } + free(copy); + g_ws_launches++; + uint32_t unwritten = 0; for (size_t i = 0; i < nw; i++) if (outw[i] == 0xffbadbadu) unwritten++; + if (info) { info->chip_calls++; info->unwritten_words += unwritten; info->ctas_last_launch = L->grid_x * L->grid_y; info->threads_per_cta = SIM_T; } + if (err) { printf("warp simulator error: %s\n", msg); return OMEGA_GPU_ATTN_CHIP_FAIL; } + return unwritten ? OMEGA_GPU_ATTN_UNWRITTEN : OMEGA_GPU_ATTN_OK; +} +/* Negative control for --divergent --sim: mode 2 builds the same kernel with the BSYNC dropped; + * the warp simulator must reject it (the whole point of the simulator: the old one could not). */ +static void divergent_negative_control(void) { + enum { SEQ = 65, NQH = 8, NKV = 2, HD = 64 }; + float *q = malloc(NQH * HD * 4), *kv = malloc((size_t)SEQ * NKV * HD * 4), *out = malloc(NQH * HD * 4); + uint32_t seed = 0x308u; + for (int i = 0; i < NQH * HD; i++) q[i] = frand(&seed, -1.0f, 1.0f); + for (size_t i = 0; i < (size_t)SEQ * NKV * HD; i++) kv[i] = frand(&seed, -1.0f, 1.0f); + omega_gpu_attention_test_set_divergent(2); + int rc = omega_gpu_gqa_attention_f32(q, kv, kv, SEQ, NQH, NKV, HD, out, NULL); + omega_gpu_attention_test_set_divergent(1); + CHECK(rc == OMEGA_GPU_ATTN_CHIP_FAIL, "divergent mode 2 (BSYNC dropped) rejected by the warp simulator (rc %s)", omega_gpu_attention_rc_name(rc)); + int rc1 = omega_gpu_gqa_attention_f32(q, kv, kv, SEQ, NQH, NKV, HD, out, NULL); + CHECK(rc1 == OMEGA_GPU_ATTN_OK, "divergent mode 1 (regioned branch) runs clean on the same input (rc %s)", omega_gpu_attention_rc_name(rc1)); + free(q); free(kv); free(out); +} + int main(int argc, char **argv) { 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], "--divergent") == 0) g_divergent = 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 (g_divergent) omega_gpu_attention_test_set_divergent(1); + if (sim) { g_sim_mode = 1; omega_gpu_attention_test_set_simulator(g_divergent ? ws_attn_launch : sim_launch); } + if (sim && g_divergent && !host) divergent_negative_control(); 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; } diff --git a/tests/gpu_reconv_test.c b/tests/gpu_reconv_test.c new file mode 100644 index 000000000..227e3b658 --- /dev/null +++ b/tests/gpu_reconv_test.c @@ -0,0 +1,470 @@ +/* gpu_reconv_test: structured warp reconvergence (omega #308) on Omega's own Blackwell codegen. + * + * ./gpu_reconv_test --host-only encoder golden words, region bookkeeping, codegen refusals, + * nvdisasm listing (when /usr/local/cuda/bin/nvdisasm exists), + * every probe kernel through the SIMT warp simulator against + * the host oracle under both fragment orders, the mutation + * controls, scoreboard and control-edge walks. No device. + * ./gpu_reconv_test --out receipt.json chip gate: host part first (a kernel the simulator rejects + * never gets chip time), then every probe on the chip against + * the oracle, bit-exact, then each math mutant must be caught. + * --observe-dropped-join chip only, optional: also launch the dropped-BSYNC mutant of + * one probe and record what the chip did (an observation, not + * a gate; the simulator rejects that kernel first). + * + * Probes (src/omega_gpu_elementwise_api.c, gen_rc_*): R3 diamond, R4 loop break, R5 nested, + * R6 lanes past the end with STS/BAR/LDS + SHFL, and the 16-deep barrier-register bound. + * Oracle: rc_oracle below; every comparison is exact (u32). Launcher chunking (64 CTAs x 128 + * threads per launch, P0 = words in the chunk) is part of the oracle, so a clamp to the wrong + * chunk end shows up. */ +#include "omega_gpu_elementwise_api.h" +#include "omega_blackwell_codegen.h" +#include "omega_bw_reconv.h" +#include "omega_gpu_session.h" +#include "bw_warp_sim.h" +#include +#include +#include +#include +#include + +static int g_checks, g_failed; +#define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) + +static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } + +typedef struct { OmegaGpuEwOp op; const char *name; const char *reg; } Probe; +static const Probe PROBES[] = { + { OMEGA_GPU_EW_RC_DIAMOND, "rc_diamond", "R3" }, + { OMEGA_GPU_EW_RC_LOOPBREAK, "rc_loopbreak", "R4" }, + { OMEGA_GPU_EW_RC_NESTED, "rc_nested", "R5" }, + { OMEGA_GPU_EW_RC_EXIT_BAR, "rc_exit_bar", "R6" }, + { OMEGA_GPU_EW_RC_DEPTH16, "rc_depth16", "B0..B15" }, +}; +#define NPROBES (sizeof PROBES / sizeof PROBES[0]) +static const uint32_t NS[] = { 1, 5, 31, 32, 33, 63, 64, 65, 96, 127, 128, 129, 200, 1000, 4096, 8191, 8192, 8193, 20000 }; +#define NNS (sizeof NS / sizeof NS[0]) +#define CHUNK (OMEGA_GPU_EW_MAX_CTAS * OMEGA_GPU_EW_THREADS) + +/* ----------------------------------------------------------------- oracle */ +static uint32_t count_loop(uint32_t x, uint32_t y, int math, uint32_t *v_out) { + uint32_t v = x & 255u, st = (y & 15u) + 1u, cnt = 0, bound = math ? 255u : 256u; + while (v < bound) { v += st; cnt++; } + *v_out = v; return cnt; +} +static uint32_t rc_r(OmegaGpuEwOp op, int math, uint32_t x, uint32_t y, uint32_t partner_x) { + uint32_t v, cnt; + switch (op) { + case OMEGA_GPU_EW_RC_DIAMOND: + if (x >= y) { uint32_t t = 3u * x + y, u = t ^ x, r = 5u * u + 1u; r += r >> 3; return math ? r + 1u : r; } + return x + 1u; + case OMEGA_GPU_EW_RC_LOOPBREAK: cnt = count_loop(x, y, math, &v); return cnt * 256u + v; + case OMEGA_GPU_EW_RC_NESTED: if (x & 1u) return 7u; cnt = count_loop(x, y, math, &v); return cnt * 256u + v + 100u; + case OMEGA_GPU_EW_RC_EXIT_BAR: return 3u * x + (partner_x | 1u); + case OMEGA_GPU_EW_RC_DEPTH16: return x + (x < y ? (uint32_t)BW_RECONV_MAX_BAR : 0u); + default: return 0xdeadbeefu; + } +} +static void rc_oracle(OmegaGpuEwOp op, int math, uint32_t n, const uint32_t *a, const uint32_t *b, uint32_t *out) { + for (uint32_t i0 = 0; i0 < n; i0 += CHUNK) { + uint32_t ni = n - i0 < CHUNK ? n - i0 : CHUNK; + uint32_t threads = ((ni + OMEGA_GPU_EW_THREADS - 1) / OMEGA_GPU_EW_THREADS) * OMEGA_GPU_EW_THREADS; + uint32_t *r = malloc((size_t)threads * 4); + for (uint32_t t = 0; t < threads; t++) { + uint32_t idx = t < ni ? t : ni - 1, pt = t ^ 1u, pidx = pt < ni ? pt : ni - 1; + r[t] = rc_r(op, math, a[i0 + idx], b[i0 + idx], a[i0 + pidx]); + } + for (uint32_t i = 0; i < ni; i++) { uint32_t nb = (i & 31u) == 31u ? i : i + 1u; out[i0 + i] = r[i] + 4099u * r[nb]; } + free(r); + } +} +/* An LCG's low bits cycle quickly (bit 0 has period 2), and the probes key on low bits + * (x & 255, y & 15, x & 1), so each draw goes through a mixing step. */ +static uint32_t mix(uint32_t r) { r ^= r >> 16; r *= 0x45d9f3bu; r ^= r >> 16; r *= 0x45d9f3bu; r ^= r >> 16; return r; } +static void fill(uint32_t *a, uint32_t *b, uint32_t n, uint32_t seed) { + for (uint32_t i = 0; i < n; i++) { a[i] = mix(lcg(&seed)); b[i] = mix(lcg(&seed)); } +} +static size_t diff_count(const uint32_t *got, const uint32_t *want, uint32_t n, uint32_t *first) { + size_t bad = 0; *first = n; + for (uint32_t i = 0; i < n; i++) if (got[i] != want[i]) { if (!bad) *first = i; bad++; } + return bad; +} + +/* --------------------------------------------------------- simulator driver */ +static uint8_t g_smem[1024]; +/* Mirrors run_1d: 64 CTAs x 128 threads per launch, P0 = words in the chunk. Returns the + * simulator's error code (0 clean); the output words are written to out. */ +static int ws_probe(const BlackwellIRProgram *p, uint32_t n, const uint32_t *a, const uint32_t *b, uint32_t *out, int order, WsSim *stats) { + for (uint32_t i = 0; i < n; i++) out[i] = 0xffbadbadu; + uint64_t wi = 0, li = 0, sp = 0; int mf = 0, pb = 0; + for (uint32_t i0 = 0; i0 < n; i0 += CHUNK) { + uint32_t ni = n - i0 < CHUNK ? n - i0 : CHUNK; + WsSim s; memset(&s, 0, sizeof s); + s.p = p; s.threads = OMEGA_GPU_EW_THREADS; s.smem = g_smem; s.smem_bytes = sizeof g_smem; s.order = order; + uint64_t aa = (uintptr_t)(a + i0), ba = (uintptr_t)(b + i0), ca = (uintptr_t)(out + i0); + s.cbank[0x380 / 4] = (uint32_t)aa; s.cbank[0x380 / 4 + 1] = (uint32_t)(aa >> 32); + s.cbank[0x388 / 4] = (uint32_t)ba; s.cbank[0x388 / 4 + 1] = (uint32_t)(ba >> 32); + s.cbank[0x390 / 4] = (uint32_t)ca; s.cbank[0x390 / 4 + 1] = (uint32_t)(ca >> 32); + s.cbank[0x398 / 4] = ni; + s.ranges[0] = (WsRange){ (uintptr_t)aa, (uintptr_t)aa + (size_t)ni * 4 }; + s.ranges[1] = (WsRange){ (uintptr_t)ba, (uintptr_t)ba + (size_t)ni * 4 }; + s.ranges[2] = (WsRange){ (uintptr_t)ca, (uintptr_t)ca + (size_t)ni * 4 }; + s.nranges = 3; + int err = ws_run(&s, (ni + OMEGA_GPU_EW_THREADS - 1) / OMEGA_GPU_EW_THREADS, 1); + wi += s.warp_issues; li += s.lane_issues; sp += s.splits; + if (s.max_frags > mf) mf = s.max_frags; + if (s.peak_bars > pb) pb = s.peak_bars; + if (err) { if (stats) *stats = s; return err; } + } + if (stats) { memset(stats, 0, sizeof *stats); stats->warp_issues = wi; stats->lane_issues = li; stats->splits = sp; stats->max_frags = mf; stats->peak_bars = pb; } + return 0; +} +static int codegen(OmegaGpuEwOp op, int mutant, BlackwellIRProgram *p, OmegaBlackwellKernel *k) { + memset(k, 0, sizeof *k); + return omega_gpu_elementwise_codegen_ir(op, mutant, p, k); +} +static int is_reconv_err(int e) { + return e == WS_E_SHFL_SPLIT || e == WS_E_BAR_SPLIT || e == WS_E_STUCK || e == WS_E_NEVER_JOINED || e == WS_E_JOIN_MISMATCH || + e == WS_E_BSYNC_UNARMED || e == WS_E_BSSY_REARM || e == WS_E_NONMEMBER || e == WS_E_SHFL_EXITED; +} + +/* --------------------------------------------------------- static walks */ +/* Scoreboard walk (as bench/prime_race/tests/gb10_sieve_host_test.c sb_hazards): a destination + * written under write scoreboard 0 is in flight until a wait mask that includes it. */ +static int sb_hazards(const BlackwellIRProgram *p) { + int inflight[64], n = 0, bad = 0; + for (size_t i = 0; i < p->count; i++) { + const BlackwellIRInsn *in = &p->insns[i]; + uint32_t c = in->control; + if ((c >> 20) & 1u) n = 0; + const int src[3] = { in->src1_vreg, in->src2_vreg, in->src3_vreg }; + for (int s = 0; s < 3; s++) for (int k = 0; src[s] >= 0 && k < n; k++) if (inflight[k] == src[s]) bad++; + if (c != 0 && ((c >> 14) & 7u) == 0 && in->dst_vreg >= 0 && n < 64) inflight[n++] = in->dst_vreg; + } + return bad; +} +/* Control-edge walk: the straight-line scoreboard reasoning above holds only if nothing is in + * flight across a branch, barrier op or exit. Every BRA / EXIT / BSSY / BSYNC must wait on every + * scoreboard set since the last wait. Returns the number of control ops that do not. */ +static int edge_waits(const BlackwellIRProgram *p) { + uint32_t pending = 0; int bad = 0; + for (size_t i = 0; i < p->count; i++) { + const BlackwellIRInsn *in = &p->insns[i]; + uint32_t c = in->control, wait = (c >> 20) & 63u, wb = (c >> 14) & 7u, rb = (c >> 17) & 7u; + pending &= ~wait; + if (in->op == BW_IR_BRA || in->op == BW_IR_EXIT || in->op == BW_IR_BSSY || in->op == BW_IR_BSYNC) { if (pending) bad++; } + if (wb != 7u) pending |= 1u << wb; + if (rb != 7u) pending |= 1u << rb; + } + return bad; +} +static int count_op(const BlackwellIRProgram *p, BlackwellIROpcode op, int bar) { + int n = 0; + for (size_t i = 0; i < p->count; i++) if (p->insns[i].op == op && (bar < 0 || p->insns[i].bar_reg == bar)) n++; + return n; +} +static int find_op(const BlackwellIRProgram *p, BlackwellIROpcode op, int nth) { + for (size_t i = 0; i < p->count; i++) if (p->insns[i].op == op && nth-- == 0) return (int)i; + return -1; +} + +/* --------------------------------------------------------- nvdisasm listing */ +static int nvdisasm_bytes(const uint8_t *code, size_t bytes, const char *name, const char *const *must, int n_must) { + const char *nv = "/usr/local/cuda/bin/nvdisasm"; + if (access(nv, X_OK) != 0) { printf("nvdisasm: not present, listing check skipped for %s\n", name); return 0; } + char bin[96], lst[96], cmd[320]; + snprintf(bin, sizeof bin, "/tmp/omega_rc_%s_%d.bin", name, (int)getpid()); + snprintf(lst, sizeof lst, "/tmp/omega_rc_%s_%d.sass", name, (int)getpid()); + FILE *f = fopen(bin, "wb"); if (!f) return -1; + fwrite(code, 1, bytes, f); fclose(f); + snprintf(cmd, sizeof cmd, "%s -b SM121 %s > %s 2>&1", nv, bin, lst); + int rc = system(cmd); + FILE *l = fopen(lst, "r"); if (!l) return -1; + char line[512]; int seen[16] = {0}, bad = 0, insns = 0; + while (fgets(line, sizeof line, l)) { + if (!strstr(line, "/*0")) continue; + insns++; + if (strstr(line, "?") || strstr(line, "error")) bad++; + for (int i = 0; i < n_must && i < 16; i++) if (strstr(line, must[i])) seen[i] = 1; + } + fclose(l); + int miss = 0; + for (int i = 0; i < n_must && i < 16; i++) if (!seen[i]) { printf(" %s: nvdisasm listing lacks %s\n", name, must[i]); miss++; } + printf("nvdisasm %s: rc=%d insns=%d undecodable=%d missing=%d (%s)\n", name, rc, insns, bad, miss, lst); + unlink(bin); + return (rc == 0 && bad == 0 && miss == 0) ? 0 : -1; +} +static int nvdisasm_kernel(OmegaGpuEwOp op, int mutant, const char *name, const char *const *must, int n_must) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + if (!p || codegen(op, mutant, p, &k) != OMEGA_GPU_EW_OK) { free(p); return -1; } + int rc = nvdisasm_bytes(k.code, k.code_size, name, must, n_must); + free(k.code); free(p); + return rc; +} + +/* --------------------------------------------------------- host part */ +typedef struct { const char *name; uint64_t warp_issues, lane_issues, splits; int max_frags, peak_bars; } Model; +static Model g_model[NPROBES * 2]; static int g_nmodel; + +static int host_part(void) { + int before = g_failed; + /* 1. encoder golden words (nvcc 13.0.88 -arch=sm_121 oracle, docs/gpu-reconvergence-308.md) */ + int fx = omega_blackwell_verify_codegen_fixtures_reconv(); + CHECK(fx == 0, "encoder golden words BSSY / BSYNC and refusals (rc %d)", fx); + + /* 2. region bookkeeping on a hand-built program */ + { + BlackwellIRProgram *p = calloc(1, sizeof *p); BwRegions r; memset(&r, 0, sizeof r); + omega_bw_ir_init(p); + BlackwellIRInsn in; memset(&in, 0, sizeof in); in.dst_vreg = in.src1_vreg = in.src2_vreg = in.src3_vreg = -1; + in.op = BW_IR_BSSY; in.bar_reg = 0; in.imm = 2; omega_bw_ir_append(p, &in); /* 0 */ + CHECK(bw_regions_next_bar(&r) == 0 && bw_regions_begin(&r, 0) == 0, "region begin returns B0"); + in.op = BW_IR_BRA; in.imm = 1; in.predicate_p0 = true; omega_bw_ir_append(p, &in); /* 1 */ + CHECK(bw_regions_exit(&r, 1) == 0, "region exit recorded"); + in.op = BW_IR_NOP; in.predicate_p0 = false; omega_bw_ir_append(p, &in); /* 2 */ + in.op = BW_IR_BSYNC; in.bar_reg = 0; omega_bw_ir_append(p, &in); /* 3 */ + CHECK(bw_regions_join(&r, p, 3) == 0, "region join returns B0"); + CHECK(p->insns[0].imm == 4 && p->insns[1].imm == 2, "join patched BSSY to the insn after BSYNC (%u) and the exit to the BSYNC (%u)", p->insns[0].imm, p->insns[1].imm); + CHECK(bw_regions_join(&r, p, 3) == -1, "join with no open region refused"); + CHECK(bw_regions_exit(&r, 1) == -1, "exit with no open region refused"); + /* exits table full */ + memset(&r, 0, sizeof r); bw_regions_begin(&r, 0); + int ok = 1; for (int i = 0; i < BW_RECONV_MAX_EXITS; i++) ok &= bw_regions_exit(&r, 1) == 0; + CHECK(ok && bw_regions_exit(&r, 1) == -1, "exit table bound (%d) fails closed", BW_RECONV_MAX_EXITS); + /* join index inconsistent: BSYNC before BSSY, wrong barrier */ + memset(&r, 0, sizeof r); bw_regions_begin(&r, 3); + CHECK(bw_regions_join(&r, p, 0) == -1, "join with BSSY after BSYNC refused"); + memset(&r, 0, sizeof r); bw_regions_begin(&r, 0); p->insns[3].bar_reg = 1; + CHECK(bw_regions_join(&r, p, 3) == -1, "join with the wrong barrier register refused"); + p->insns[3].bar_reg = 0; + /* depth bound */ + memset(&r, 0, sizeof r); ok = 1; + for (int d = 0; d < BW_RECONV_MAX_DEPTH; d++) ok &= bw_regions_next_bar(&r) == d && bw_regions_begin(&r, d) == d; + CHECK(ok && bw_regions_next_bar(&r) == -1 && bw_regions_begin(&r, 99) == -1, "nesting bound %d: the next begin fails closed", BW_RECONV_MAX_DEPTH); + free(p); + } + + /* 3. codegen: production probes build, refusals refuse, mutants differ */ + static const struct { OmegaGpuEwOp op; const char *name; } refuse[] = { + { OMEGA_GPU_EW_RC_REFUSE_DEPTH17, "17 nested regions" }, { OMEGA_GPU_EW_RC_REFUSE_EXIT_SHFL, "EXIT then BAR/SHFL" }, + { OMEGA_GPU_EW_RC_REFUSE_EXIT_IN_REGION, "EXIT inside a region" }, { OMEGA_GPU_EW_RC_REFUSE_BAR_IN_REGION, "BAR.SYNC inside a region" }, + { OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION, "SHFL inside a region" }, { OMEGA_GPU_EW_RC_REFUSE_UNCLOSED, "region never joined" }, + { OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY, "BSYNC with no BSSY" }, + }; + for (size_t i = 0; i < sizeof refuse / sizeof refuse[0]; i++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + int rc = codegen(refuse[i].op, 0, p, &k); + CHECK(rc == OMEGA_GPU_EW_CODEGEN_FAIL, "%s refused at codegen (rc %s)", refuse[i].name, omega_gpu_elementwise_rc_name(rc)); + if (rc == OMEGA_GPU_EW_OK) free(k.code); + free(p); + } + for (size_t i = 0; i < NPROBES; i++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + int rc = codegen(PROBES[i].op, 0, p, &k); + CHECK(rc == OMEGA_GPU_EW_OK && k.gpr_count <= 64, "%s builds (rc %s, %zu insns, %u gprs)", PROBES[i].name, omega_gpu_elementwise_rc_name(rc), k.insn_count, k.gpr_count); + if (rc != OMEGA_GPU_EW_OK) { free(p); continue; } + int nb = count_op(p, BW_IR_BSSY, -1), ns = count_op(p, BW_IR_BSYNC, -1); + int want = PROBES[i].op == OMEGA_GPU_EW_RC_NESTED ? 2 : PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16 ? BW_RECONV_MAX_BAR : 1; + CHECK(nb == want && ns == want, "%s has %d BSSY and %d BSYNC (want %d)", PROBES[i].name, nb, ns, want); + if (PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16) { + int each = 1; for (int b = 0; b < BW_RECONV_MAX_BAR; b++) each &= count_op(p, BW_IR_BSSY, b) == 1 && count_op(p, BW_IR_BSYNC, b) == 1; + CHECK(each, "depth16 uses every barrier register B0..B15 exactly once"); + } + CHECK(sb_hazards(p) == 0, "%s scoreboard walk clean", PROBES[i].name); + CHECK(edge_waits(p) == 0, "%s every control op waits on everything in flight", PROBES[i].name); + printf("codegen %s: %zu insns, %u gprs, %d regions\n", PROBES[i].name, p->count, k.gpr_count, nb); + uint8_t d0[32]; memcpy(d0, k.code_digest, 32); free(k.code); + for (int m = 1; m <= 3; m++) { + BlackwellIRProgram *mp = calloc(1, sizeof *mp); OmegaBlackwellKernel mk; + int mrc = codegen(PROBES[i].op, m, mp, &mk); + int same_ok = m == 1 && PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16; /* depth16 has no math mutant */ + CHECK(mrc == OMEGA_GPU_EW_OK && ((memcmp(d0, mk.code_digest, 32) != 0) != same_ok), "%s mutant %d builds and %s (rc %s)", PROBES[i].name, m, same_ok ? "equals production" : "differs", omega_gpu_elementwise_rc_name(mrc)); + if (mrc == OMEGA_GPU_EW_OK) { + if (m == 2) CHECK(count_op(mp, BW_IR_BSYNC, -1) == 0 && count_op(mp, BW_IR_BSSY, -1) == nb, "%s mutant 2 has no BSYNC", PROBES[i].name); + if (m == 3) CHECK(count_op(mp, BW_IR_BSYNC, -1) == ns, "%s mutant 3 keeps its BSYNCs", PROBES[i].name); + free(mk.code); + } + free(mp); + } + free(p); + } + /* the four regioned sieves build too (their sweep runs in gb10_sieve_host_test) */ + for (int op = OMEGA_GPU_EW_PRIME_SIEVE_DIV1; op <= OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2; op++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + int rc = codegen((OmegaGpuEwOp)op, 0, p, &k); + int staged = op == OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1 || op == OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2; + int nb = rc == OMEGA_GPU_EW_OK ? count_op(p, BW_IR_BSSY, -1) : -1, want = (op >= OMEGA_GPU_EW_PRIME_SIEVE_DIV2 ? 2 : 1) + staged; + CHECK(rc == OMEGA_GPU_EW_OK && k.gpr_count <= 64 && nb == want && edge_waits(p) == 0, "sieve op %d builds with %d regions (rc %s, %u gprs)", op, nb, omega_gpu_elementwise_rc_name(rc), k.gpr_count); + if (rc == OMEGA_GPU_EW_OK) free(k.code); + free(p); + } + + /* 4. nvdisasm: the words decode as BSSY/BSYNC on the real barrier registers, and the predicated + * and B15 forms the encoder can produce decode too (built by hand: encode_one) */ + { + static const char *const m0[] = { "BSSY", "BSYNC", "B0", "SHFL.DOWN", "@P0 BRA" }; + CHECK(nvdisasm_kernel(OMEGA_GPU_EW_RC_DIAMOND, 0, "diamond", m0, 5) == 0, "diamond listing decodes with BSSY/BSYNC B0"); + static const char *const m1[] = { "BSSY", "BSYNC", "B15", "B7" }; + CHECK(nvdisasm_kernel(OMEGA_GPU_EW_RC_DEPTH16, 0, "depth16", m1, 4) == 0, "depth16 listing decodes with B15"); + static const char *const m2[] = { "BSSY", "B1", "BSYNC" }; + CHECK(nvdisasm_kernel(OMEGA_GPU_EW_PRIME_SIEVE_DIV2, 0, "sieve_div2", m2, 3) == 0, "nested sieve listing decodes with B1"); + uint8_t code[64]; uint32_t w[4]; BlackwellIRInsn in; memset(&in, 0, sizeof in); in.dst_vreg = in.src1_vreg = in.src2_vreg = in.src3_vreg = -1; + in.op = BW_IR_BSSY; in.bar_reg = 3; in.imm = 2; in.predicate_p0 = true; omega_blackwell_encode_one(&in, w); memcpy(code, w, 16); + in.op = BW_IR_BSYNC; in.bar_reg = 3; in.predicate_p0 = true; in.predicate_not = true; omega_blackwell_encode_one(&in, w); memcpy(code + 16, w, 16); + in.op = BW_IR_BSYNC; in.bar_reg = 15; in.predicate_p0 = false; in.predicate_not = false; omega_blackwell_encode_one(&in, w); memcpy(code + 32, w, 16); + in.op = BW_IR_EXIT; omega_blackwell_encode_one(&in, w); memcpy(code + 48, w, 16); + static const char *const m3[] = { "@P0 BSSY", "B3", "@!P0 BSYNC", "BSYNC.RECONVERGENT B15" }; + CHECK(nvdisasm_bytes(code, 64, "forms", m3, 4) == 0, "predicated BSSY/BSYNC and B15 decode"); + } + + /* 5. simulator: every probe, every n, both fragment orders, exact against the oracle */ + uint32_t *a = malloc(20000 * 4), *b = malloc(20000 * 4), *got = malloc(20000 * 4), *want = malloc(20000 * 4); + for (size_t i = 0; i < NPROBES; i++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k; + if (codegen(PROBES[i].op, 0, p, &k) != OMEGA_GPU_EW_OK) { free(p); continue; } + int fails = 0; uint64_t cases = 0; + for (size_t j = 0; j < NNS; j++) for (int order = 0; order < 2; order++) { + uint32_t n = NS[j]; fill(a, b, n, 0x308u + n * 7u + (uint32_t)order); + rc_oracle(PROBES[i].op, 0, n, a, b, want); + WsSim st; int err = ws_probe(p, n, a, b, got, order, &st); + uint32_t first; size_t bad = diff_count(got, want, n, &first); + cases++; + if (err || bad) { fails++; printf(" %s n=%u order=%d: sim %s, %zu words differ (first %u: got %08x want %08x)\n", PROBES[i].name, n, order, err ? st.msg : "clean", bad, first, first < n ? got[first] : 0, first < n ? want[first] : 0); } + if (n == 4096) { Model *m = &g_model[g_nmodel++]; m->name = PROBES[i].name; m->warp_issues = st.warp_issues; m->lane_issues = st.lane_issues; m->splits = st.splits; m->max_frags = st.max_frags; m->peak_bars = st.peak_bars; } + } + CHECK(fails == 0, "%s simulator vs oracle: %d of %" PRIu64 " cases differ", PROBES[i].name, fails, cases); + /* math mutant: simulator clean, result wrong (the oracle sees it) */ + BlackwellIRProgram *mp = calloc(1, sizeof *mp); OmegaBlackwellKernel mk; + if (codegen(PROBES[i].op, 1, mp, &mk) == OMEGA_GPU_EW_OK) { + uint32_t n = 1000; fill(a, b, n, 0x1001u); rc_oracle(PROBES[i].op, 0, n, a, b, want); + WsSim st; int err = ws_probe(mp, n, a, b, got, 0, &st); uint32_t first; size_t bad = diff_count(got, want, n, &first); + if (PROBES[i].op == OMEGA_GPU_EW_RC_EXIT_BAR) CHECK(err == WS_E_SHFL_EXITED || err == WS_E_BAR_SPLIT, "R6 early-exit kernel: simulator reports SHFL_EXITED (got %s)", st.msg); + else if (PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16) CHECK(err == 0 && bad == 0, "depth16 'mutant 1' is the production kernel: clean and exact (err %d bad %zu)", err, bad); + else CHECK(err == 0 && bad > 0, "%s math mutant: simulator clean, %zu words differ (err %d)", PROBES[i].name, bad, err); + free(mk.code); + } + free(mp); + /* dropped join and corrupted target: the simulator must reject both, in both orders */ + for (int m = 2; m <= 3; m++) for (int order = 0; order < 2; order++) { + BlackwellIRProgram *xp = calloc(1, sizeof *xp); OmegaBlackwellKernel xk; + if (codegen(PROBES[i].op, m, xp, &xk) == OMEGA_GPU_EW_OK) { + uint32_t n = 1000; fill(a, b, n, 0x2002u + (uint32_t)m); + WsSim st; int err = ws_probe(xp, n, a, b, got, order, &st); + CHECK(is_reconv_err(err), "%s mutant %d (%s) order %d rejected by the simulator: %s", PROBES[i].name, m, m == 2 ? "BSYNC dropped" : "BSSY target corrupted", order, err ? st.msg : "NOT CAUGHT"); + if (m == 3) CHECK(err == WS_E_JOIN_MISMATCH || err == WS_E_STUCK, "%s corrupted target shows as JOIN_MISMATCH or STUCK (%s)", PROBES[i].name, ws_err_name(err)); + free(xk.code); + } + free(xp); + } + free(k.code); free(p); + } + /* 6. hand mutations of the production diamond (controls on the simulator itself, independent of + * the emitter's mutant hooks): exit jumping past the BSYNC, BSYNC on the wrong barrier, a BSSY + * on an armed barrier. And the unmodified program as the positive control. */ + { + BlackwellIRProgram *p = calloc(1, sizeof *p), *q = calloc(1, sizeof *q); OmegaBlackwellKernel k; + if (codegen(OMEGA_GPU_EW_RC_DIAMOND, 0, p, &k) == OMEGA_GPU_EW_OK) { + uint32_t n = 500; fill(a, b, n, 0x3003u); WsSim st; + int bssy = find_op(p, BW_IR_BSSY, 0), bsync = find_op(p, BW_IR_BSYNC, 0), ex = -1; + for (int i = bssy + 1; i < bsync; i++) if (p->insns[i].op == BW_IR_BRA && !p->insns[i].predicate_p0) ex = i; + CHECK(bssy >= 0 && bsync > bssy && ex > bssy, "diamond shape: BSSY %d, exit %d, BSYNC %d", bssy, ex, bsync); + CHECK(ws_probe(p, n, a, b, got, 0, &st) == 0, "positive control: untouched diamond runs clean"); + *q = *p; q->insns[ex].imm += 1; /* exit lands after the BSYNC */ + int e1 = ws_probe(q, n, a, b, got, 0, &st); CHECK(is_reconv_err(e1), "exit past the BSYNC rejected: %s", st.msg); + *q = *p; q->insns[bsync].bar_reg = 1; /* BSYNC on a barrier nobody armed */ + int e2 = ws_probe(q, n, a, b, got, 0, &st); CHECK(e2 == WS_E_BSYNC_UNARMED, "BSYNC on an unarmed barrier rejected: %s", st.msg); + *q = *p; q->insns[bsync].op = BW_IR_NOP; /* join dropped by hand */ + int e3 = ws_probe(q, n, a, b, got, 0, &st); CHECK(is_reconv_err(e3), "join replaced by NOP rejected: %s", st.msg); + *q = *p; q->insns[bssy].imm += 1; /* names the wrong join */ + int e4 = ws_probe(q, n, a, b, got, 0, &st); CHECK(e4 == WS_E_JOIN_MISMATCH, "wrong join target rejected: %s", st.msg); + *q = *p; q->insns[bssy + 1] = p->insns[bssy]; /* second BSSY on the armed barrier (overwrites the ISETP) */ + int e5 = ws_probe(q, n, a, b, got, 0, &st); CHECK(e5 == WS_E_BSSY_REARM, "BSSY on an armed barrier rejected: %s", st.msg); + free(k.code); + } + free(p); free(q); + } + free(a); free(b); free(got); free(want); + for (int i = 0; i < g_nmodel; i++) printf("model %s n=4096: warp_issues=%" PRIu64 " lane_util=%.3f splits=%" PRIu64 " max_frags=%d peak_bars=%d\n", g_model[i].name, g_model[i].warp_issues, g_model[i].warp_issues ? (double)g_model[i].lane_issues / (32.0 * (double)g_model[i].warp_issues) : 0.0, g_model[i].splits, g_model[i].max_frags, g_model[i].peak_bars); + return g_failed == before ? 0 : -1; +} + +/* --------------------------------------------------------- chip part */ +static int chip(const char *out_path, int observe) { + FILE *out = out_path ? fopen(out_path, "w") : NULL; + if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; } + if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_RECONV_V1\",\"issue\":308,\"comparison\":\"exact u32\",\"cases\":["); + int first = 1; + uint32_t *a = malloc(20000 * 4), *b = malloc(20000 * 4), *got = malloc(20000 * 4), *want = malloc(20000 * 4); + for (size_t i = 0; i < NPROBES; i++) { + for (size_t j = 0; j < NNS; j++) { + uint32_t n = NS[j]; fill(a, b, n, 0x5050u + n); + rc_oracle(PROBES[i].op, 0, n, a, b, want); + OmegaGpuEwInfo info; memset(&info, 0, sizeof info); + int rc = omega_gpu_reconv_probe_u32(PROBES[i].op, n, a, b, got, &info); + uint32_t fi; size_t bad = rc == OMEGA_GPU_EW_OK ? diff_count(got, want, n, &fi) : n; + int pass = rc == OMEGA_GPU_EW_OK && bad == 0; + CHECK(pass, "%s n=%u chip rc=%s violations=%zu err=\"%s\"", PROBES[i].name, n, omega_gpu_elementwise_rc_name(rc), bad, omega_gpu_elementwise_last_error()); + if (out) fprintf(out, "%s{\"op\":\"%s\",\"regression\":\"%s\",\"n\":%u,\"rc\":\"%s\",\"violations\":%zu,\"pass\":%s,\"chip_elapsed_ns\":%" PRIu64 ",\"chip_calls\":%u,\"unwritten_words\":%u,\"cache_hit\":%s}", + first ? "" : ",", PROBES[i].name, PROBES[i].reg, n, omega_gpu_elementwise_rc_name(rc), bad, pass ? "true" : "false", info.elapsed_ns, info.chip_calls, info.unwritten_words, info.kernel_cache_hit ? "true" : "false"); + first = 0; + if (!pass) { printf("STOP: %s failed on the chip at n=%u; no further launches of this probe\n", PROBES[i].name, n); break; } + } + } + if (out) fprintf(out, "],\"mutants\":["); + first = 1; + for (size_t i = 0; i < NPROBES; i++) { + if (PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16) continue; /* no math mutant */ + uint32_t n = 1000; fill(a, b, n, 0x6060u); rc_oracle(PROBES[i].op, 0, n, a, b, want); + omega_gpu_elementwise_test_set_mutant((int)PROBES[i].op); + /* R6's math mutant is the early-exit kernel: the emitter refuses it on the launch path + * (the bypass exists only for codegen_ir), so the catch is CODEGEN_FAIL: no undefined + * SHFL ever reaches the chip. */ + OmegaGpuEwInfo info; memset(&info, 0, sizeof info); + int rc = omega_gpu_reconv_probe_u32(PROBES[i].op, n, a, b, got, &info); + uint32_t fi; size_t bad = rc == OMEGA_GPU_EW_OK ? diff_count(got, want, n, &fi) : n; + int caught = rc != OMEGA_GPU_EW_OK || bad > 0; + omega_gpu_elementwise_test_set_mutant(0); + CHECK(caught, "%s math mutant caught (rc %s, violations %zu)", PROBES[i].name, omega_gpu_elementwise_rc_name(rc), bad); + if (PROBES[i].op == OMEGA_GPU_EW_RC_EXIT_BAR) CHECK(rc == OMEGA_GPU_EW_CODEGEN_FAIL, "R6 early-exit form refused at codegen on the launch path (rc %s)", omega_gpu_elementwise_rc_name(rc)); + if (out) fprintf(out, "%s{\"op\":\"%s\",\"mutant\":1,\"rc\":\"%s\",\"violations\":%zu,\"caught\":%s}", first ? "" : ",", PROBES[i].name, omega_gpu_elementwise_rc_name(rc), bad, caught ? "true" : "false"); + first = 0; + } + if (out) fprintf(out, "],\"observations\":["); + if (observe) { + /* The dropped-join kernel (rejected by the simulator above) on the chip once, small n: + * recorded as an observation of what the hardware does without the BSYNC. Not a gate. */ + uint32_t n = 1000; fill(a, b, n, 0x7070u); rc_oracle(OMEGA_GPU_EW_RC_DIAMOND, 0, n, a, b, want); + omega_gpu_elementwise_test_set_mutant_level(OMEGA_GPU_EW_RC_DIAMOND, 2); + OmegaGpuEwInfo info; memset(&info, 0, sizeof info); + int rc = omega_gpu_reconv_probe_u32(OMEGA_GPU_EW_RC_DIAMOND, n, a, b, got, &info); + uint32_t fi; size_t bad = rc == OMEGA_GPU_EW_OK ? diff_count(got, want, n, &fi) : n; + omega_gpu_elementwise_test_set_mutant(0); + printf("observation: diamond with the BSYNC dropped, n=%u: rc=%s, %zu of %u words differ from the oracle (first %u)\n", n, omega_gpu_elementwise_rc_name(rc), bad, n, fi); + if (out) fprintf(out, "{\"op\":\"rc_diamond\",\"mutant\":2,\"what\":\"BSYNC dropped\",\"n\":%u,\"rc\":\"%s\",\"violations\":%zu,\"first_bad\":%u,\"gate\":false}", n, omega_gpu_elementwise_rc_name(rc), bad, fi); + } + CHECK(omega_gpu_session_open_count() == 1, "device opened once for the battery (opens=%u)", omega_gpu_session_open_count()); + if (out) { + fprintf(out, "],\"sim_model_n4096\":["); + for (int i = 0; i < g_nmodel; i++) fprintf(out, "%s{\"op\":\"%s\",\"order\":%d,\"warp_issues\":%" PRIu64 ",\"lane_issues\":%" PRIu64 ",\"splits\":%" PRIu64 ",\"max_frags\":%d,\"peak_bars\":%d}", i ? "," : "", g_model[i].name, i & 1, g_model[i].warp_issues, g_model[i].lane_issues, g_model[i].splits, g_model[i].max_frags, g_model[i].peak_bars); + 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_RECONV_PASS" : "OMEGA_GPU_RECONV_FAIL"); + fclose(out); + } + free(a); free(b); free(got); free(want); + return 0; +} + +int main(int argc, char **argv) { + const char *out_path = NULL; int host = 0, observe = 0; + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "--host-only") == 0) host = 1; + else if (strcmp(argv[i], "--observe-dropped-join") == 0) observe = 1; + else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i]; + } + int hrc = host_part(); + if (!host) { + if (hrc != 0) { printf("host part failed: no chip launches\nFAIL: %d checks, %d failed\n", g_checks, g_failed); return 1; } + if (chip(out_path, observe) != 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 d18dfc02f..a28495829 100644 --- a/tools/.crumb +++ b/tools/.crumb @@ -56,14 +56,14 @@ } ], "children_root": "sha256:8a042dc49a250f0d1b7ae1358fc70bf7bdc9eff651a5ff7c9ee41fd49d329041", - "compiled_at": "2026-10-06T00:26:33Z", + "compiled_at": "2026-10-06T01:57:59Z", "compiler": "crumb-compile/1", - "digest": "sha256:ef22f063312bab17b11019f16b48d0e30d907c7bd1af458b6dbc6923907b517f", + "digest": "sha256:808edb958b46d236f503828399cbdb7f1cb62f8eebfd1604d4768b42734b459f", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 135, - "generated_at_commit": "96748a2078862997c4ee423797ed4d1a99dcf300", - "source_tree": "sha256:e430810a2eee6932f03a14a7e782944d048baf24d877ab95de7aed6ec6e8cfe3" + "files": 136, + "generated_at_commit": "1f4a3c7821d769a99ce1fcc896ac8ee30a33ea69", + "source_tree": "sha256:edd75db96bda888946c0299792d0e240668f4d7a38378c60359e693585700bad" }, "owner_lane": "", "provenance": { diff --git a/tools/run_gpu_reconv_chip.sh b/tools/run_gpu_reconv_chip.sh new file mode 100644 index 000000000..b1320d70d --- /dev/null +++ b/tools/run_gpu_reconv_chip.sh @@ -0,0 +1,43 @@ +#!/bin/sh +# tools/run_gpu_reconv_chip.sh [OUT_ROOT] [-- extra gate args] +# omega #308 chip gate: structured warp reconvergence. Runs the host part first (encoder +# goldens, region bookkeeping, refusals, nvdisasm, every probe through the SIMT warp +# simulator, mutation controls); a kernel the simulator rejects never gets chip time. Then +# every probe (R3 diamond, R4 loop break, R5 nested, R6 lanes past the end + BAR/SHFL, 16-deep +# barrier bound) on the chip against the host oracle, bit-exact, then each math mutant must be +# caught. Sealed receipt folder OUT_ROOT/GPU-RECONV-/ (default ~/workspace/evidence-out): +# receipt.json machine-readable verdict per case run.log test output +# device.txt kernel, driver, omega and physics shas build.log compiler output +# copies of the source files the verdict depends on, SHA256SUMS over all +# Pattern of tools/run_gpu_elementwise_chip.sh. Never creates or touches the quiet flag (the +# operator runs it inside an own quiet window) and never kills or times out the run. +# PHYSICS_DIR must be at the physics.lock commit (the Makefile refuses otherwise). +set -u +cd "$(dirname "$0")/.." || exit 2 +P=${PHYSICS_DIR:-../physics} +OUT_ROOT=$HOME/workspace/evidence-out +EXTRA="" +if [ $# -gt 0 ] && [ "$1" != "--" ]; then OUT_ROOT=$1; shift; fi +if [ $# -gt 0 ] && [ "$1" = "--" ]; then shift; EXTRA="$*"; fi +SHA=$(git rev-parse --short HEAD) +OUT=$OUT_ROOT/GPU-RECONV-$SHA +mkdir -p "$OUT" +if ! make PHYSICS_DIR="$P" build/gpu_reconv_test > "$OUT/build.log" 2>&1; then + echo "GPU-RECONV BUILD FAIL (see $OUT/build.log)"; exit 1 +fi +{ + uname -a + nvidia-smi --query-gpu=name,driver_version --format=csv,noheader 2>/dev/null || echo "nvidia-smi: unavailable" + echo "omega $(git rev-parse HEAD) ($(git status --porcelain | wc -l) uncommitted paths)" + echo "physics $(git -C "$P" rev-parse HEAD)" + date -u +%FT%TZ +} > "$OUT/device.txt" +# shellcheck disable=SC2086 +./build/gpu_reconv_test --out "$OUT/receipt.json" $EXTRA > "$OUT/run.log" 2>&1 +rc=$? +cp src/omega_gpu_elementwise_api.c src/omega_gpu_elementwise_api.h src/omega_bw_reconv.h src/omega_blackwell_codegen.c \ + tests/gpu_reconv_test.c tests/bw_warp_sim.h "$OUT/" +(cd "$OUT" && sha256sum -- * > SHA256SUMS) +tail -1 "$OUT/run.log" +echo "GPU-RECONV chip gate rc=$rc receipt=$OUT/receipt.json" +exit $rc