CodeQL / File created without restricting permissions
A file may be created here with mode 0666, which would make it world-writable.
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
A file may be created here with mode 0666, which would make it world-writable.
git checkout -b patch-ab514338bee02eba 278b6c3c5840e5f13472a2f60f50542d81c3821e curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/ab514338bee02eba.patch | git am
github-import 1
aien-dev/omega-mv
516
2f4672e0dece9ebef2475838c23b3c193cde3de3
{"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":516,"url":"https://github.com/aien-dev/omega-mv/pull/516","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:38:09Z","metadata":"unavailable","state_derived":true}
diff --git a/.crumb b/.crumb index 90ded1fe5..d0ba3d2fd 100644 --- a/.crumb +++ b/.crumb @@ -48,7 +48,7 @@ }, { "digest": "d2eb8967a939ebdb0b54e15fe5d849dd5af71c23b812941e4ba86ad2b0f21d39", - "last_commit": "979ac54c", + "last_commit": "4870b0a4", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, @@ -77,14 +77,14 @@ "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "dfa36de2ac8649f66f80587abea51a7f166285ee0b92e0e09774c98689055250", - "last_commit": "333091a6", + "digest": "aa2a756962b2afc365533dfae935f9aae337d11f7cd7c85ba0397ca3e9d4c3c6", + "last_commit": "b6f7a7f3", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "1fb9acb543bcc5bb34df8ff3f2bc12e5e75931b3698e8870e95a0c753dc44d87", - "last_commit": "43d8d20d", + "digest": "392d42cef45d31a5a27b58fa82d1ef89594ba9db17f3bcb973777119b2f97986", + "last_commit": "2f655fbf", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, @@ -95,15 +95,15 @@ "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:58cd6740ca04b3c398bc6ba89fbe52c2999a9064e5a1f889c27b409143af4203", - "compiled_at": "2026-10-16T09:04:56Z", + "children_root": "sha256:ad43fe7ac833928d1cf09e0b8a22f3f5048748e7d78eb1d3dae3bcffe9b4c14e", + "compiled_at": "2026-10-16T20:31:51Z", "compiler": "crumb-compile/1", - "digest": "sha256:33cb95985ca81cb38d05cb52c1292ab59a65a523e2d9399039d740d5801d34dd", + "digest": "sha256:fccbf6f324d7bf226b48c8928819ee6f0147f79b70808ec446a2b506f6da0cf1", "evidence_files": 11127, "evidence_root": "sha256:4528085ec1661c442b64fb5dc8def65067ea2620ff88e42bcf52d58b7642ea6e", - "files": 14109, - "generated_at_commit": "9d368679f13ea58c46bf7580899aee92e39f5885", - "source_tree": "sha256:63dba4a6717442292a2be7a2bc5f0da5d3ce5350849634a11630446a24597727" + "files": 14112, + "generated_at_commit": "05d0d8e9ab225dba82698bf89128072f3a6f3a7a", + "source_tree": "sha256:120f9833a6157832d67609a0b5753b49eca7f3b91cf988bed4cda46eed28a7b1" }, "owner_lane": "", "provenance": { diff --git a/Makefile b/Makefile index 3551fea3f..d0c10814f 100644 --- a/Makefile +++ b/Makefile @@ -37,7 +37,7 @@ SRCS += src/omega_gpu_hostmath.c # OWN-30B cut 8 item 2 (2026-10-13): one-launch MoE router (omega_gpu_router_api.h), new files only SRCS += src/omega_blackwell_codegen_router.c src/omega_gpu_router_api.c # OWN-30B W-L2 (2026-10-13): decode launch collapse, new files only: command list (N launches, one submit) + glue kernels -SRCS += src/omega_gpu_cmdlist.c src/omega_blackwell_codegen_dec.c +SRCS += src/omega_gpu_cmdlist.c src/omega_blackwell_codegen_dec.c src/omega_gpu_dep.c SRCS += src/omega_gpu_mem_cached.c SRCS += src/omega_gpu_decode_graph.c # OWN-30B (2026-10-13): bf16 streaming GEMV for the lm_head (omega_gpu_bf16_gemv_api.h), new files only diff --git a/calibration/.crumb b/calibration/.crumb index 7a4f08dbb..79ce4b629 100644 --- a/calibration/.crumb +++ b/calibration/.crumb @@ -64,13 +64,13 @@ } ], "children_root": "sha256:e58b9cc78de81a51c7ed21c725616ddb80bd3d24d171b24a189e8f7132499433", - "compiled_at": "2026-10-14T21:46:29Z", + "compiled_at": "2026-10-16T20:31:51Z", "compiler": "crumb-compile/1", "digest": "sha256:fb7cc6e608fdbe68a9735a1313285782b9717ff12bde17343f8ce635de3644f6", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 110, - "generated_at_commit": "2a6d6d7b9091bd2184b15dcde893035dbcca40d3", + "generated_at_commit": "05d0d8e9ab225dba82698bf89128072f3a6f3a7a", "source_tree": "sha256:b39f40915ce2ed4092ade2a38665d99f6daab78fd462433177cdd0aec5f00bd7" }, "owner_lane": "", diff --git a/docs/.crumb b/docs/.crumb index ca5d5ec00..a6e043aa4 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -124,13 +124,13 @@ } ], "children_root": "sha256:ac7f7ea4b00d4620a993c6dd88a0267f2d49008d415162976e45fa4341e43407", - "compiled_at": "2026-10-16T09:04:56Z", + "compiled_at": "2026-10-16T20:31:51Z", "compiler": "crumb-compile/1", "digest": "sha256:d2eb8967a939ebdb0b54e15fe5d849dd5af71c23b812941e4ba86ad2b0f21d39", "evidence_files": 34, "evidence_root": "sha256:589d8b3706e6d7a202ab20b278dac5fc5bafc973f1c2a867b2668aa7210b65cc", "files": 127, - "generated_at_commit": "9d368679f13ea58c46bf7580899aee92e39f5885", + "generated_at_commit": "05d0d8e9ab225dba82698bf89128072f3a6f3a7a", "source_tree": "sha256:29d159b6950500f96de859834fd2c5a9e9ba037f9bc88fd97a8ed47a1688233b" }, "owner_lane": "", diff --git a/docs/estimation/.crumb b/docs/estimation/.crumb index f45a9743b..251c4cc79 100644 --- a/docs/estimation/.crumb +++ b/docs/estimation/.crumb @@ -24,13 +24,13 @@ } ], "children_root": "sha256:35cd3af03e079556db7f3ebcfd7590fa68659e84dd603795ae6b5b048dc2af0d", - "compiled_at": "2026-10-14T21:46:29Z", + "compiled_at": "2026-10-16T20:31:51Z", "compiler": "crumb-compile/1", "digest": "sha256:56a5a999ce726e90d89c1c88b9e20c81857c8e6ee82af8d651ac80ea70e2c46d", "evidence_files": 34, "evidence_root": "sha256:589d8b3706e6d7a202ab20b278dac5fc5bafc973f1c2a867b2668aa7210b65cc", "files": 47, - "generated_at_commit": "2a6d6d7b9091bd2184b15dcde893035dbcca40d3", + "generated_at_commit": "05d0d8e9ab225dba82698bf89128072f3a6f3a7a", "source_tree": "sha256:67ae8d20aca3ae36b8cf0d75c6939058cc9ff8762c977f70d6963c545e3215d8" }, "owner_lane": "", diff --git a/research/.crumb b/research/.crumb index df383ba0d..b2b356140 100644 --- a/research/.crumb +++ b/research/.crumb @@ -22,7 +22,7 @@ "children": [ { "digest": "fce91110a9847be58e4068fc9c24497369672f3f60ca6c7deb5b0699df0e045c", - "last_commit": "e0abd0b5", + "last_commit": "5901baa5", "name": "atemporal", "purpose": "AT-0 Clock-Free Universe research program: relational (Page-Wootters) quantum models with no ext" }, @@ -34,13 +34,13 @@ } ], "children_root": "sha256:47fde1e530a64eb6f5fbecb27fddf6335c32069f1acb4de243826d601361a138", - "compiled_at": "2026-10-14T01:16:48Z", + "compiled_at": "2026-10-16T20:31:51Z", "compiler": "crumb-compile/1", "digest": "sha256:eccc4aec1037ae222d6370836953732b031b8ed70de28967b7af7ca62c2abe0b", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 538, - "generated_at_commit": "e0abd0b524f5bf8e3ba6960b53868980bdd41b4e", + "generated_at_commit": "05d0d8e9ab225dba82698bf89128072f3a6f3a7a", "source_tree": "sha256:2c362ea1c5900e78486cbe9cfc017bd87b53ab97bd93317cd9be0efb6923d9ea" }, "owner_lane": "", diff --git a/research/atemporal/.crumb b/research/atemporal/.crumb index fc9132860..18b99f07d 100644 --- a/research/atemporal/.crumb +++ b/research/atemporal/.crumb @@ -10,13 +10,13 @@ } ], "children_root": "sha256:f7bd1d7019a69ef57140cd019f86634f3cb80c1e31ca3063dfdf73ac9deef7c6", - "compiled_at": "2026-10-14T01:16:48Z", + "compiled_at": "2026-10-16T20:31:51Z", "compiler": "crumb-compile/1", "digest": "sha256:fce91110a9847be58e4068fc9c24497369672f3f60ca6c7deb5b0699df0e045c", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 392, - "generated_at_commit": "e0abd0b524f5bf8e3ba6960b53868980bdd41b4e", + "generated_at_commit": "05d0d8e9ab225dba82698bf89128072f3a6f3a7a", "source_tree": "sha256:2bdda175595d2ff0ed4424af8e04f326e0c69c0bcac44151aa21771afd647cbe" } }, diff --git a/research/m15/spbm/.crumb b/research/m15/spbm/.crumb index 52b5fb51f..00df50a9e 100644 --- a/research/m15/spbm/.crumb +++ b/research/m15/spbm/.crumb @@ -13,13 +13,13 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-14T21:46:29Z", + "compiled_at": "2026-10-16T20:31:51Z", "compiler": "crumb-compile/1", "digest": "sha256:baf4a6094854fe437a8880b2c90733bbf9048212af7f7b45381db4050d8e128c", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 129, - "generated_at_commit": "2a6d6d7b9091bd2184b15dcde893035dbcca40d3", + "generated_at_commit": "05d0d8e9ab225dba82698bf89128072f3a6f3a7a", "source_tree": "sha256:4b183d9ff80e7d0e7c8ab2925b20c1a8072ffbfa3c6d14d3c1024c1796eb3f5c" }, "owner_lane": "", diff --git a/src/.crumb b/src/.crumb index 4fe5298a4..42e354988 100644 --- a/src/.crumb +++ b/src/.crumb @@ -182,14 +182,14 @@ } ], "children_root": "sha256:15012a017b26a269feb256b64875ece2912f5f82c57804baafa3777c4fcb80b9", - "compiled_at": "2026-10-16T07:33:43Z", + "compiled_at": "2026-10-16T19:08:36Z", "compiler": "crumb-compile/1", - "digest": "sha256:dfa36de2ac8649f66f80587abea51a7f166285ee0b92e0e09774c98689055250", + "digest": "sha256:aa2a756962b2afc365533dfae935f9aae337d11f7cd7c85ba0397ca3e9d4c3c6", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 537, - "generated_at_commit": "1d481ab853a14d3c11eff926ce1e25993085f76a", - "source_tree": "sha256:200c9243151691313acb5f86d3e9cb3058f1a1b92ece2a255b3340b691dfb894" + "files": 540, + "generated_at_commit": "b6f7a7f3dd11a1eb1c98591a926e3e565c5981f7", + "source_tree": "sha256:c361e783d77e59a8903b4bc370b6a3cd9f502ca21d664df7cc19b56ba988c93e" }, "owner_lane": "", "provenance": { diff --git a/src/omega_blackwell_codegen.c b/src/omega_blackwell_codegen.c index b09fa1e54..928ce87eb 100644 --- a/src/omega_blackwell_codegen.c +++ b/src/omega_blackwell_codegen.c @@ -522,6 +522,47 @@ static int encode_single_insn(const BlackwellIRInsn *insn, const OmegaRegAlloc * w[3] = insn->control ? insn->control : 0x00321e00; break; + case BW_IR_LDG_STRONG_GPU: + /* LDG.E.STRONG.GPU Rd, desc[URd][Ra.64]: LDG.E.STRONG.SYS with the scope field (w[2] bits 13..16) 0x7 (nvdisasm: + * 0x0000000402007981 / 0x004ea8000c1ef900). */ + w[0] = 0x7981 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff); + w[2] = 0x0c1ef900; + w[3] = insn->control ? insn->control : 0x000f2200; + break; + + case BW_IR_ATOMG_ADD_STRONG_GPU: + /* ATOMG.E.ADD.STRONG.GPU PT, Rd, desc[URd][Ra.64], Rb (nvdisasm: 0x80000009040509a8 / 0x001ea200081ef104). */ + w[0] = 0x79a8 | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(src2 & 0xff) | 0x80000000u; + w[2] = 0x081ef100 | (uint32_t)(ureg & 0xff); + w[3] = insn->control ? insn->control : 0x00321e00; + break; + + case BW_IR_MEMBAR_ALL_GPU: + /* MEMBAR.ALL.GPU (nvdisasm: 0x0000000000007992 / 0x000fec000000a000) */ + w[0] = 0x00007992; w[1] = 0; w[2] = 0x0000a000; + w[3] = insn->control ? insn->control : 0x000fec00; + break; + + case BW_IR_ERRBAR: + /* ERRBAR (nvdisasm: 0x00000000000079ab / 0x000fc00000000000) */ + w[0] = 0x000079ab; w[1] = 0; w[2] = 0; + w[3] = insn->control ? insn->control : 0x000fc000; + break; + + case BW_IR_CGAERRBAR: + /* CGAERRBAR (nvdisasm: 0x00000000000075ab / 0x000fec0000000000) */ + w[0] = 0x000075ab; w[1] = 0; w[2] = 0; + w[3] = insn->control ? insn->control : 0x000fec00; + break; + + case BW_IR_CS2R_GLOBALTIMER: + /* CS2R Rd:Rd+1, SR_GLOBALTIMERLO (nvdisasm: 0x0000000000087805 / 0x000fca0000015200; the .32 form has w[2] 0x5200) */ + w[0] = 0x7805 | ((uint32_t)(dst & 0xff) << 16); w[1] = 0; w[2] = 0x00015200; + w[3] = insn->control ? insn->control : 0x000fca00; + 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; diff --git a/src/omega_blackwell_codegen.h b/src/omega_blackwell_codegen.h index cc36e237d..cacaa527b 100644 --- a/src/omega_blackwell_codegen.h +++ b/src/omega_blackwell_codegen.h @@ -6,7 +6,8 @@ #include <stdint.h> #include "omega_blackwell_matmul.h" -#define BW_MAX_IR_INSNS 1024 /* cut K: the four-head hd 128 attention kernel is ~700 instructions */ +#define BW_MAX_IR_INSNS 1280 /* cut K: the four-head hd 128 attention kernel is ~700 instructions; DEPFLAGS (2026-10-16): the depth-4 decode attention + kernel (1012) plus the counter prologue and epilogue passes 1024. Programs are heap-allocated and code is sized by need. */ #define BW_MAX_VREGS 256 #define BW_MAX_UVREGS 32 #define BW_PHYS_GPR_START 2 /* Reserve R0, R1 */ @@ -100,7 +101,17 @@ typedef enum { BW_IR_LDG_E_128, /* LDG.E.128 Rd..Rd+3, desc[URd][Ra.64+imm] */ /* Batched attention loads (2026-10-12). Words matched against nvcc 13.0.88 -arch=sm_121 * (omega_blackwell_verify_codegen_fixtures_fragmm). imm = unsigned byte offset < 2^23; dst an aligned quad. */ - BW_IR_LDS128 /* LDS.128 Rd..Rd+3, [Ra+imm] */ + BW_IR_LDS128, /* LDS.128 Rd..Rd+3, [Ra+imm] */ + /* Dependency flags between the launches of one command list (DEPFLAGS, 2026-10-16). Words from nvcc 13.0.88 + * -arch=sm_121 + nvdisasm (investigations/2026-10-16-DEPFLAGS/sass): ld.acquire.gpu -> LDG.E.STRONG.GPU + CCTL.IVALL; + * red.release.gpu.add -> MEMBAR.ALL.GPU; ERRBAR; CGAERRBAR; REDG/ATOMG.E.ADD.STRONG.GPU; mov.u64 %globaltimer -> + * CS2R Rd:Rd+1, SR_GLOBALTIMERLO. Scope field = w[2] bits 13..16 (0xa SYS, 0x7 GPU), as PR #484 found for the F32 twins. */ + BW_IR_LDG_STRONG_GPU, /* LDG.E.STRONG.GPU Rd, desc[URd][Ra.64]: a flag another CTA of this GPU writes */ + BW_IR_ATOMG_ADD_STRONG_GPU,/* ATOMG.E.ADD.STRONG.GPU PT, Rd, desc[URd][Ra.64], Rb */ + BW_IR_MEMBAR_ALL_GPU, /* MEMBAR.ALL.GPU */ + BW_IR_ERRBAR, /* ERRBAR */ + BW_IR_CGAERRBAR, /* CGAERRBAR */ + BW_IR_CS2R_GLOBALTIMER /* CS2R Rd:Rd+1, SR_GLOBALTIMERLO (64-bit ns timer; dst a register pair) */ } BlackwellIROpcode; /* Barrier (reconvergence) registers: the sm_121 BSSY/BSYNC words carry a 4-bit barrier id diff --git a/src/omega_blackwell_codegen_attn.c b/src/omega_blackwell_codegen_attn.c index 37a67d8df..d7e81ff72 100644 --- a/src/omega_blackwell_codegen_attn.c +++ b/src/omega_blackwell_codegen_attn.c @@ -104,6 +104,7 @@ static void isetp_ge_u32(Em *e, int a, int b) { BlackwellIRInsn i = I0(BW_IR_ISE static void bra(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } static int bra_if_p0_fwd(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 patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); } +#include "omega_bw_dep.h" /* DEPFLAGS: needs Em, emit, I0, V, V64, here, patch_bra, K_* above */ static int bssy_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = 0; i.imm = 2; emit(e, i, K_BSSY); return at; } static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); } static void exit_k(Em *e) { emit(e, I0(BW_IR_EXIT), K_EXIT); } @@ -163,6 +164,7 @@ int omega_blackwell_codegen_decode_attn_prog(const OmegaDecodeAttnSpec *spec, Bl s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); movi(e, one, 1); movrz(e, R(zero)); movi(e, c7fff, 0x7fffu); movi(e, c16, 16); shr(e, warp, tid, 5); + OmegaBwDep Dp; dep_prologue(e, spec->dep, udesc, tid, &Dp); andi(e, R(lane), R(tid), 31u); /* range of this CTA: base = cx * per; end = min(base + per, pos + 1) */ @@ -476,6 +478,7 @@ int omega_blackwell_codegen_decode_attn_prog(const OmegaDecodeAttnSpec *spec, Bl lds32(e, R(ep[0]), t1, 1); isetp_ge_u32(e, ep[0], splitsm1); const int bra_last = bra_if_p0_fwd(e); + dep_epilogue(e, udesc, tid, &Dp); /* CTA-uniform exit (ep[0] is one shared word): all threads take it */ exit_k(e); patch_bra(e, bra_last, here(e)); /* combine: thread tid, outputs o = tid + 256 r (head o >> 7, dim o & 127), r = 0..3 */ @@ -535,6 +538,7 @@ int omega_blackwell_codegen_decode_attn_prog(const OmegaDecodeAttnSpec *spec, Bl /* thread 0 resets the counter for the next launch (every other CTA of this head has already added) */ isetp_ge_u32(e, tid, one); { Region r = region_skip_if_p0(e); imadw(e, pb64, cy, 4u, pcnt); stg(e, BW_IR_STG_STRONG_SYS, pb64, R(zero), udesc); region_end(e, r); } + dep_epilogue(e, udesc, tid, &Dp); tail(e); if (e->err) return -1; return omega_bw_regalloc_solve(prog); diff --git a/src/omega_blackwell_codegen_attn.h b/src/omega_blackwell_codegen_attn.h index 102a3f2e5..bd67b8fe0 100644 --- a/src/omega_blackwell_codegen_attn.h +++ b/src/omega_blackwell_codegen_attn.h @@ -79,6 +79,7 @@ typedef struct { uint32_t depth; /* prefetch depth of the K and V streams, 1..4 */ int mutant; /* TEST ONLY. 0 none; 1 no RoPE on q and k; 2 V read one position late; 3 the new K/V are not * written to the cache; 4 the cross-split combine ignores the max (differs only when splits > 1). */ + uint32_t dep; /* OMEGA_DEP_FLAGS | OMEGA_DEP_CAMERA (omega_bw_dep.h); 0 = none */ } OmegaDecodeAttnSpec; /* Builds the IR program (register-allocated) into *prog. 0 ok, -1 refused (depth 0 or > 4, mutant outside 0..4, diff --git a/src/omega_blackwell_codegen_bf16.c b/src/omega_blackwell_codegen_bf16.c index 08125e6c6..b1a2483c9 100644 --- a/src/omega_blackwell_codegen_bf16.c +++ b/src/omega_blackwell_codegen_bf16.c @@ -95,6 +95,7 @@ static void bra(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = /* a forward branch whose target is patched when known: returns the instruction index */ static int bra_if_p0_fwd(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 patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); } +#include "omega_bw_dep.h" /* DEPFLAGS: needs Em, emit, I0, V, V64, here, patch_bra, K_* above */ static int bssy_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = 0; i.imm = 2; emit(e, i, K_BSSY); return at; } static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); } static void tail(Em *e) { @@ -151,6 +152,7 @@ int omega_blackwell_codegen_bf16_gemv_prog(const OmegaBf16GemvSpec *spec, Blackw s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); movi(e, one, 1); shr(e, warp, tid, 0, 5); + OmegaBwDep Dp; dep_prologue(e, spec->dep, udesc, tid, &Dp); andi(e, lane, tid, 0, 31); imadi(e, wlane, lane, 0, 16, NOREG); /* byte offset of this lane's chunk in a row step */ imadi(e, rg, cx, 0, W, warp); /* first row group of this warp */ @@ -232,6 +234,7 @@ int omega_blackwell_codegen_bf16_gemv_prog(const OmegaBf16GemvSpec *spec, Blackw loop_end(e); const int L_end = here(e); patch_bra(e, bra_end, L_end); + dep_epilogue(e, udesc, tid, &Dp); tail(e); if (e->err) return -1; return omega_bw_regalloc_solve(prog); diff --git a/src/omega_blackwell_codegen_bf16.h b/src/omega_blackwell_codegen_bf16.h index c7a22cdfd..1cb9b4fbc 100644 --- a/src/omega_blackwell_codegen_bf16.h +++ b/src/omega_blackwell_codegen_bf16.h @@ -51,6 +51,7 @@ typedef struct { uint32_t warps; /* 1 .. 32 (the host simulator runs up to 8) */ uint32_t depth; /* K steps per loop iteration: divides k / 256; rows * depth <= 16 */ int mutant; /* 0 production; 1, 2, 3 as above */ + uint32_t dep; /* OMEGA_DEP_FLAGS | OMEGA_DEP_CAMERA (omega_bw_dep.h); 0 = none */ } OmegaBf16GemvSpec; int omega_blackwell_codegen_bf16_gemv_prog(const OmegaBf16GemvSpec *spec, BlackwellIRProgram *prog); diff --git a/src/omega_blackwell_codegen_dec.c b/src/omega_blackwell_codegen_dec.c index b2ec67d7f..07f010a8d 100644 --- a/src/omega_blackwell_codegen_dec.c +++ b/src/omega_blackwell_codegen_dec.c @@ -94,6 +94,7 @@ static void isetp_ge_u32(Em *e, int a, int b) { BlackwellIRInsn i = I0(BW_IR_ISE static void bra(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } static int bra_if_p0_fwd(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 patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); } +#include "omega_bw_dep.h" /* DEPFLAGS: needs Em, emit, I0, V, V64, here, patch_bra, K_* above */ static int bssy_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = 0; i.imm = 2; emit(e, i, K_BSSY); return at; } static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); } static void exit_k(Em *e) { emit(e, I0(BW_IR_EXIT), K_EXIT); } @@ -156,6 +157,7 @@ static int gen_axpy(Em *e, const OmegaDecSpec *sp) { ldc64(e, px, OMEGA_DEC_AXPY_X); ldc64(e, pa, OMEGA_DEC_AXPY_A); ldc32(e, iters, OMEGA_DEC_AXPY_ITERS); s2r(e, tid, BW_SR_TID_X); s2r(e, cta, BW_SR_CTAID_X); imadi(e, R(gid), R(cta), OMEGA_DEC_THREADS, tid); + OmegaBwDep D; dep_prologue(e, sp->dep, udesc, tid, &D); imadw(e, xa, gid, 4u, px); imadw(e, aa, gid, 4u, pa); movrz(e, R(it)); movi(e, one, 1); const int L = here(e); @@ -170,6 +172,7 @@ static int gen_axpy(Em *e, const OmegaDecSpec *sp) { bra(e, L); loop_end(e); patch_bra(e, done, here(e)); + dep_epilogue(e, udesc, tid, &D); tail(e); return e->err; } @@ -188,6 +191,7 @@ static int gen_res(Em *e, const OmegaDecSpec *sp) { movi(e, one, 1); shr(e, warp, tid, 5); andi(e, R(lane), R(tid), 31u); + OmegaBwDep D; dep_prologue(e, sp->dep, udesc, tid, &D); imadi(e, R(toff), R(tid), 32u, NOREG); imadw(e, xa, toff, 1u, px); ldg(e, BW_IR_LDG_E_128, xq0, xa, 0, udesc, 1); ldg(e, BW_IR_LDG_E_128, xq1, xa, 16, udesc, 0); @@ -247,6 +251,7 @@ static int gen_res(Em *e, const OmegaDecSpec *sp) { stg(e, ya, R(yv), udesc); } } + dep_epilogue(e, udesc, tid, &D); tail(e); return e->err; } @@ -259,6 +264,7 @@ static int gen_swiglu(Em *e, const OmegaDecSpec *sp) { ldc64(e, pgu, OMEGA_DEC_SW_GU); ldc64(e, pact, OMEGA_DEC_SW_ACT); ldc32(e, scale, OMEGA_DEC_SW_SCALE); s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); imadi(e, R(c), R(cx), OMEGA_DEC_THREADS, tid); + OmegaBwDep D; dep_prologue(e, sp->dep, udesc, tid, &D); imadi(e, R(gi), R(cy), 2u * OMEGA_DEC_INTER, c); imadi(e, R(ai), R(cy), OMEGA_DEC_INTER, c); /* the up half sits INTER floats after the gate: its own address (the 32-bit LDG.E encoding carries no immediate @@ -280,6 +286,7 @@ static int gen_swiglu(Em *e, const OmegaDecSpec *sp) { fmul(e, R(t), R(t), R(uv)); fmul(e, R(t), R(t), R(scale)); stg(e, aa, R(t), udesc); + dep_epilogue(e, udesc, tid, &D); tail(e); return e->err; } @@ -298,6 +305,7 @@ static int gen_argmax(Em *e, const OmegaDecSpec *sp) { shr(e, warp, tid, 5); andi(e, R(lane), R(tid), 31u); imadi(e, R(gid), R(cta), OMEGA_DEC_THREADS, tid); + OmegaBwDep D; dep_prologue(e, sp->dep, udesc, tid, &D); /* before the first global load: the logits are lm_head-written */ imadi(e, R(off), R(gid), 32u, NOREG); imadw(e, va, off, 1u, pv); ldg(e, BW_IR_LDG_E_128, vq0, va, 0, udesc, 1); ldg(e, BW_IR_LDG_E_128, vq1, va, 16, udesc, 0); @@ -339,6 +347,7 @@ static int gen_argmax(Em *e, const OmegaDecSpec *sp) { imadw(e, oa, cta, 4u, poi); stg(e, oa, R(best), udesc); region_end(e, r); } + dep_epilogue(e, udesc, tid, &D); tail(e); return e->err; } diff --git a/src/omega_blackwell_codegen_dec.h b/src/omega_blackwell_codegen_dec.h index 0ea33231e..4f8ab29e0 100644 --- a/src/omega_blackwell_codegen_dec.h +++ b/src/omega_blackwell_codegen_dec.h @@ -74,6 +74,7 @@ typedef struct { uint32_t stride; /* AXPY: elements per iteration (grid_x * 256) */ int mutant; /* TEST ONLY. RES: 1 = norm without the weight; 2 = COMBINE skips expert 7. SWIGLU: 1 = up half read at +767. ARGMAX: 1 = last equal index instead of first. 0 none */ + uint32_t dep; /* OMEGA_DEP_FLAGS | OMEGA_DEP_CAMERA (omega_bw_dep.h); 0 = none */ } OmegaDecSpec; int omega_blackwell_codegen_dec_prog(const OmegaDecSpec *spec, BlackwellIRProgram *prog); diff --git a/src/omega_blackwell_codegen_moe.c b/src/omega_blackwell_codegen_moe.c index 14d9d5c4f..979562e37 100644 --- a/src/omega_blackwell_codegen_moe.c +++ b/src/omega_blackwell_codegen_moe.c @@ -101,6 +101,7 @@ static void bra(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = /* a forward branch whose target is patched when known: returns the instruction index */ static int bra_if_p0_fwd(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 patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); } +#include "omega_bw_dep.h" /* DEPFLAGS: needs Em, emit, I0, V, V64, here, patch_bra, K_* above */ static int bssy_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = 0; i.imm = 2; emit(e, i, K_BSSY); return at; } static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); } static void tail(Em *e) { @@ -181,6 +182,7 @@ int omega_blackwell_codegen_fp8_gemv_prog(const OmegaFp8GemvSpec *spec, Blackwel s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); movi(e, one, 1); shr(e, warp, tid, 0, 5); + OmegaBwDep Dp; dep_prologue(e, spec->dep, udesc, tid, &Dp); /* before the first LDG: the table entry is router-written */ andi(e, lane, tid, 0, 31); if (spec->mutant == 3) xorr(e, gidx, cy, one); else imadi(e, gidx, cy, 0, 1, NOREG); imadw(e, tent, gidx, 16, ptab); @@ -298,6 +300,7 @@ int omega_blackwell_codegen_fp8_gemv_prog(const OmegaFp8GemvSpec *spec, Blackwel loop_end(e); const int L_end = here(e); patch_bra(e, bra_end, L_end); + dep_epilogue(e, udesc, tid, &Dp); tail(e); if (e->err) return -1; return omega_bw_regalloc_solve(prog); diff --git a/src/omega_blackwell_codegen_moe.h b/src/omega_blackwell_codegen_moe.h index 4140f2fc7..e0c47e4d8 100644 --- a/src/omega_blackwell_codegen_moe.h +++ b/src/omega_blackwell_codegen_moe.h @@ -84,6 +84,7 @@ typedef struct { * flight per warp); x comes from shared memory (LDS.128). Needs KS % D == 0, * rows * D * lane_bytes <= 128, k % (32 * warps) == 0 and k <= 512 * warps (staging rounds). */ int mutant; /* 0 production; 1, 2, 3 as above */ + uint32_t dep; /* OMEGA_DEP_FLAGS | OMEGA_DEP_CAMERA (omega_bw_dep.h): launch ordering by GPU-side counters; 0 = none */ } OmegaFp8GemvSpec; /* 0 on success (prog solved and register-allocated), -1 on a refused spec or an allocator failure. */ diff --git a/src/omega_blackwell_codegen_moe2.c b/src/omega_blackwell_codegen_moe2.c index 967822300..ad41423b5 100644 --- a/src/omega_blackwell_codegen_moe2.c +++ b/src/omega_blackwell_codegen_moe2.c @@ -104,6 +104,7 @@ static void isetp_ge_u32(Em *e, int a, int b) { BlackwellIRInsn i = I0(BW_IR_ISE static void bra(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } static int bra_if_p0_fwd(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 patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); } +#include "omega_bw_dep.h" /* DEPFLAGS: needs Em, emit, I0, V, V64, here, patch_bra, K_* above */ static int bssy_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = 0; i.imm = 2; emit(e, i, K_BSSY); return at; } static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); } static void tail(Em *e) { @@ -188,6 +189,7 @@ int omega_blackwell_codegen_fp8_gemv2_prog(const OmegaFp8GemvSpec *spec, Blackwe ldc64(e, ptab, OMEGA_FP8_ARG_TAB); ldc64(e, px, OMEGA_FP8_ARG_X); ldc64(e, py, OMEGA_FP8_ARG_Y); ldc32(e, rgn, OMEGA_FP8_ARG_RG); ldc32(e, xs, OMEGA_FP8_ARG_XSTRIDE); ldc32(e, ys, OMEGA_FP8_ARG_YSTRIDE); ldc32(e, stride, OMEGA_FP8_ARG_STRIDE); s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); + OmegaBwDep Dp; dep_prologue(e, spec->dep, udesc, tid, &Dp); /* before the first LDG: the table entry is router-written */ movi(e, one, 1); shr(e, warp, tid, 0, 5); andi(e, lane, tid, 0, 31); @@ -311,6 +313,7 @@ int omega_blackwell_codegen_fp8_gemv2_prog(const OmegaFp8GemvSpec *spec, Blackwe const int L_end = here(e); patch_bra(e, bra_none, L_end); patch_bra(e, bra_done, L_end); + dep_epilogue(e, udesc, tid, &Dp); tail(e); if (e->err) return -1; return omega_bw_regalloc_solve(prog); diff --git a/src/omega_blackwell_codegen_router.c b/src/omega_blackwell_codegen_router.c index 31e21331d..c2956ffdf 100644 --- a/src/omega_blackwell_codegen_router.c +++ b/src/omega_blackwell_codegen_router.c @@ -100,6 +100,7 @@ static void isetp_ge_u32(Em *e, int a, int b) { BlackwellIRInsn i = I0(BW_IR_ISE static void bra(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } static int bra_if_p0_fwd(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 patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); } +#include "omega_bw_dep.h" /* DEPFLAGS: needs Em, emit, I0, V, V64, here, patch_bra, K_* above */ static int bssy_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = 0; i.imm = 2; emit(e, i, K_BSSY); return at; } static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); } static void exit_k(Em *e) { emit(e, I0(BW_IR_EXIT), K_EXIT); } @@ -171,6 +172,7 @@ int omega_blackwell_codegen_router_prog(const OmegaRouterSpec *spec, BlackwellIR s2r(e, tid, BW_SR_TID_X); movi(e, one, 1); movrz(e, R(zero)); movi(e, c2p23, C_2P23); movi(e, cneg, NEG_BIG); shr(e, warp, tid, 5); + OmegaBwDep Dp; dep_prologue(e, spec->dep, udesc, tid, &Dp); andi(e, R(lane), R(tid), 31u); /* ---- phase 1: RMSNorm ---- */ @@ -390,6 +392,7 @@ int omega_blackwell_codegen_router_prog(const OmegaRouterSpec *spec, BlackwellIR region_end(e, rg); } patch_bra(e, bra_tail, here(e)); + dep_epilogue(e, udesc, tid, &Dp); tail(e); (void)zero; if (e->err) return -1; diff --git a/src/omega_blackwell_codegen_router.h b/src/omega_blackwell_codegen_router.h index 2b2b62f8d..d71cc1fef 100644 --- a/src/omega_blackwell_codegen_router.h +++ b/src/omega_blackwell_codegen_router.h @@ -73,6 +73,7 @@ typedef struct { int mutant; /* TEST ONLY. 0 none; 1 RMSNorm without the weight; 2 the mask after each argmax round removes * expert id ^ 1 (the winner can be picked again); 3 the directory entries copied are those of * id ^ 1; 4 the weights are not normalised (den = 1). */ + uint32_t dep; /* OMEGA_DEP_FLAGS | OMEGA_DEP_CAMERA (omega_bw_dep.h); 0 = none */ } OmegaRouterSpec; /* Builds the register-allocated IR program into *prog. 0 ok, -1 refused. */ diff --git a/src/omega_bw_dep.h b/src/omega_bw_dep.h new file mode 100644 index 000000000..f53809ccf --- /dev/null +++ b/src/omega_bw_dep.h @@ -0,0 +1,125 @@ +/* omega_bw_dep.h: dependency flags between the launches of one command list (DEPFLAGS, 2026-10-16), the kernel side. + * The protocol (one shared done counter per command list, bank words, host check) is described in omega_gpu_dep.h. + * + * Why: the decode graph serialised its ~436 dependent launches per token with WAIT_FOR_IDLE + INVALIDATE_SHADER_CACHES, + * and the launch-cost study (evidence/OWN30B-L2D-BARRIER-20261014) measured 20-28 us per dependent launch for that drain + * and refill against ~4 us without a barrier. With flags the list runs with barrier NONE and every kernel orders itself: + * prologue warp 0 polls the shared done counter (LDG.E.STRONG.GPU) until done - target < 2^31 (wrap-safe), then + * BAR.SYNC and CCTL.IVALL (nvcc's lowering of ld.acquire.gpu, probe in investigations/2026-10-16-DEPFLAGS/sass); + * a poll that never succeeds (OMEGA_DEP_POLL_LIMIT iterations) adds OMEGA_DEP_TIMEOUT_SENTINEL once (lane 0) to the counter + * and continues, so the host sees the wrong count and nothing hangs the chip. + * epilogue before every EXIT: MEMBAR.ALL.GPU; ERRBAR; CGAERRBAR (nvcc's lowering of fence + red.release.gpu), BAR.SYNC, + * thread 0 ATOMG.E.ADD.STRONG.GPU done, 1 (the CUDA Programming Guide reduction sample: a device-scope fence + * before the count increment, and a device-scope acquire load of the flag on the other side). + * camera (OMEGA_DEP_CAMERA) CS2R SR_GLOBALTIMERLO at entry and before EXIT; thread 0 stores {t0, t1} (16 B) at + * cam_base + 16 * (slot & (OMEGA_DEP_CAM_SLOTS - 1)), slot from an atomic add on the counter line's word 1. + * Every data buffer the kernels touch is GPU-uncached (omega_gpu_session.h), so only L1 can hold a stale line and + * CCTL.IVALL drops it. Header-only, included by a generator AFTER its Em / emit / I0 / V / V64 / here / patch_bra + * definitions and its K_ enum (all six generators share them). */ +#ifndef OMEGA_BW_DEP_H +#define OMEGA_BW_DEP_H +#include "omega_gpu_dep.h" /* the constants and the protocol description */ + +typedef struct { int pdone, t0; uint32_t dep; } OmegaBwDep; + +static void dep_insn(Em *e, BlackwellIROpcode op, int d, int a, int b, int u, int kind) { + BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.ureg = u; emit(e, i, kind); +} +static void dep_imadi(Em *e, int d, int a, uint32_t imm, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c; emit(e, i, K_FIXED); } +static void dep_movi(Em *e, int d, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_MOV_IMM); i.dst_vreg = d; i.imm = imm; emit(e, i, K_FIXED); } +static void dep_shr(Em *e, int d, int a, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_SHF_R); i.dst_vreg = d; i.src1_vreg = a; i.imm = imm; emit(e, i, K_FIXED); } +static void dep_isetp_ge(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 int dep_bra_p0_fwd(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 dep_bra_to(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } +static void dep_ldc64(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC64); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); } +static void dep_ldc32(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); } +static void dep_bar(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } +/* The linear-scan allocator knows no back edges (same rule as the generators' loop_end): a value defined before the poll + * loop and read inside it must stay live until the back-branch, else the allocator hands its register to a loop temporary. */ +static void dep_loop_extend(Em *e, int start, int end) { + 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; + } +} + +/* Prologue: call after the kernel read its thread id (tid, warp index `warp` = tid >> 5) and the uniform descriptor, BEFORE + * its first global load. dep 0: emits nothing. */ +static void dep_prologue(Em *e, uint32_t dep, int udesc, int tid, OmegaBwDep *D) { + memset(D, 0, sizeof *D); D->dep = dep; D->pdone = -1; D->t0 = -1; + if (!(dep & (OMEGA_DEP_FLAGS | OMEGA_DEP_CAMERA))) return; + D->pdone = V64(e); + if (dep & OMEGA_DEP_CAMERA) { D->t0 = V64(e); if (e->err) return; dep_insn(e, BW_IR_CS2R_GLOBALTIMER, D->t0, -1, -1, -1, K_VAR); } + dep_ldc64(e, D->pdone, OMEGA_DEP_ARG_DONE); + if (!(dep & OMEGA_DEP_FLAGS) || (dep & OMEGA_DEP_TEST_NO_PROLOGUE)) return; /* TEST bit: no wait (the sim test must catch the early read) */ + int pwait = V64(e), target = V(e), v = V(e), diff = V(e), cnt = V(e), one = V(e), zero = V(e), lim = V(e), sent = V(e), scratch = V(e), warp = V(e); + if (e->err) return; + dep_shr(e, warp, tid, 5); + dep_ldc64(e, pwait, OMEGA_DEP_ARG_WAIT); dep_ldc32(e, target, OMEGA_DEP_ARG_TARGET); + dep_movi(e, one, 1); dep_movi(e, zero, 0); dep_movi(e, lim, OMEGA_DEP_POLL_LIMIT); dep_movi(e, cnt, 0); + /* Reconvergence region B0 around the poll (nvcc wraps every data-dependent loop so): lanes may leave the loop on different + * iterations (the sim test makes the producer land mid-warp), and a split warp must not reach the epilogue BSSY. */ + const int at_bssy = here(e); { BlackwellIRInsn bs = I0(BW_IR_BSSY); bs.bar_reg = 0; bs.imm = 2; emit(e, bs, K_BSSY); } + dep_isetp_ge(e, warp, one); /* P0: warp >= 1 -> straight to the join (warp 0 polls) */ + const int to_join = dep_bra_p0_fwd(e); + const int L = here(e); + dep_insn(e, BW_IR_LDG_STRONG_GPU, v, pwait, -1, udesc, K_VAR); + dep_imadi(e, diff, target, 0xffffffffu, v); /* v - target */ + dep_shr(e, diff, diff, 31); /* 0 when 0 <= v - target < 2^31 (wrap-safe) */ + dep_isetp_ge(e, zero, diff); /* P0: satisfied, i.e. diff == 0. (The WIP compared one >= diff, true for diff in {0, 1}: never waited.) */ + const int to_done = dep_bra_p0_fwd(e); + dep_imadi(e, cnt, one, 1u, cnt); + dep_isetp_ge(e, cnt, lim); /* P0: timed out */ + const int to_timeout = dep_bra_p0_fwd(e); + dep_bra_to(e, L); + dep_loop_extend(e, L, here(e) - 1); + const int at_timeout = here(e); + /* only lane 0 adds the sentinel (a warp-wide ATOMG adds once per active lane): sent = ((tid - 1) >> 31) * SENTINEL, tid < 32 here */ + dep_imadi(e, sent, one, 0xffffffffu, tid); dep_shr(e, sent, sent, 31); dep_imadi(e, sent, sent, OMEGA_DEP_TIMEOUT_SENTINEL, -1); + dep_insn(e, BW_IR_ATOMG_ADD_STRONG_GPU, scratch, D->pdone, sent, udesc, K_VAR); /* names this launch to the host; no hang */ + const int at_join = here(e); + patch_bra(e, to_join, at_join); patch_bra(e, to_done, at_join); patch_bra(e, to_timeout, at_timeout); + { BlackwellIRInsn bsy = I0(BW_IR_BSYNC); bsy.bar_reg = 0; emit(e, bsy, K_BSYNC); } patch_bra(e, at_bssy, at_join + 1); /* BSYNC at at_join */ + dep_bar(e); + emit(e, I0(BW_IR_CCTL_IVALL), K_FIXED); /* acquire: no stale L1 line of a buffer the previous launch rewrote */ +} + +/* Epilogue: call before EVERY exit of the kernel, with all threads of the CTA converged (after the output stores). */ +static void dep_epilogue(Em *e, int udesc, int tid, OmegaBwDep *D) { + if (!D->dep) return; + int one = V(e), scratch = V(e); + if (e->err) return; + dep_movi(e, one, 1); + if (D->dep & OMEGA_DEP_FLAGS) { + emit(e, I0(BW_IR_MEMBAR_ALL_GPU), K_FIXED); emit(e, I0(BW_IR_ERRBAR), K_FIXED); emit(e, I0(BW_IR_CGAERRBAR), K_FIXED); + } + dep_bar(e); + dep_isetp_ge(e, tid, one); /* P0: not thread 0 -> skip */ + const int at_bssy = here(e); { BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = 0; i.imm = 2; emit(e, i, K_BSSY); } + const int skip = dep_bra_p0_fwd(e); + if ((D->dep & OMEGA_DEP_FLAGS) && !(D->dep & OMEGA_DEP_TEST_NO_EPILOGUE)) dep_insn(e, BW_IR_ATOMG_ADD_STRONG_GPU, scratch, D->pdone, one, udesc, K_VAR); /* TEST bit: never reports */ + if (D->dep & OMEGA_DEP_CAMERA) { + int t1 = V64(e), pslot = V64(e), slot = V(e), cam = V64(e), rec = V64(e); + if (e->err) return; + dep_insn(e, BW_IR_CS2R_GLOBALTIMER, t1, -1, -1, -1, K_VAR); + { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = pslot; i.src1_vreg = one; i.imm = 4u; i.src3_vreg = D->pdone; emit(e, i, K_FIXED); } + dep_insn(e, BW_IR_ATOMG_ADD_STRONG_GPU, slot, pslot, one, udesc, K_VAR); + { BlackwellIRInsn i = I0(BW_IR_LDG_E_64); i.dst_vreg = cam; i.src1_vreg = D->pdone; i.imm = 8u; i.ureg = udesc; emit(e, i, K_VAR); } + { BlackwellIRInsn i = I0(BW_IR_LOP3_AND); i.dst_vreg = slot; i.src1_vreg = slot; i.imm = OMEGA_DEP_CAM_SLOTS - 1u; emit(e, i, K_FIXED); } + { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = rec; i.src1_vreg = slot; i.imm = 16u; i.src3_vreg = cam; emit(e, i, K_FIXED); } + dep_isetp_ge(e, cam, one); /* P0: camera base non-zero (low word; the host keeps bases above 4 GB-aligned pages) */ + const int at_bssy2 = here(e); { BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = 1; i.imm = 2; emit(e, i, K_BSSY); } + const int to_store = dep_bra_p0_fwd(e); + const int to_end = here(e); dep_bra_to(e, to_end + 1); + patch_bra(e, to_store, here(e)); + for (unsigned q = 0; q < 4; q++) { BlackwellIRInsn st = I0(BW_IR_STG_E); st.src1_vreg = rec; st.src2_vreg = q < 2 ? D->t0 : t1; st.src2_subreg = (uint8_t)(q & 1u); st.imm = 4u * q; st.ureg = udesc; emit(e, st, K_STORE); } + patch_bra(e, to_end, here(e)); + { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 1; emit(e, i, K_BSYNC); } + patch_bra(e, at_bssy2, here(e)); /* BSSY imm = distance to its BSYNC + 1 as bssy_fwd/patch_bra use (target = at_bsync + 1) */ + } + const int at_bsync = here(e); + { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); } + patch_bra(e, skip, at_bsync); patch_bra(e, at_bssy, at_bsync + 1); +} +#endif diff --git a/src/omega_gpu_bf16_gemv_api.c b/src/omega_gpu_bf16_gemv_api.c index a0d6b3d5e..be0ec7526 100644 --- a/src/omega_gpu_bf16_gemv_api.c +++ b/src/omega_gpu_bf16_gemv_api.c @@ -196,7 +196,7 @@ static int gemv_core(const OmegaGpuBf16Tensor *t, const float *x32, const uint16 if (rec && (!dev_x || !dev_y)) return OMEGA_GPU_BF16_BAD_ARGS; if (!t || !t->w.cpu || (!rec && ((!x32 && !x16) || !y))) return OMEGA_GPU_BF16_BAD_ARGS; const uint32_t n = t->n, k = t->k; - OmegaBf16GemvSpec spec; uint32_t ctas; spec_for(k, &spec, &ctas); + OmegaBf16GemvSpec spec; uint32_t ctas; spec_for(k, &spec, &ctas); spec.dep = rec ? omega_gpu_cmdlist_dep(rec) : 0; /* dep-mode list: the kernel carries the counter protocol */ if (n % spec.rows) return OMEGA_GPU_BF16_CODEGEN_FAIL; const uint32_t RG = n / spec.rows; uint32_t grid_x = (RG + spec.warps - 1) / spec.warps; diff --git a/src/omega_gpu_cmdlist.c b/src/omega_gpu_cmdlist.c index a403e250f..b5cc85da3 100644 --- a/src/omega_gpu_cmdlist.c +++ b/src/omega_gpu_cmdlist.c @@ -7,6 +7,8 @@ * written by the CPU, WAIT_FOR_IDLE sits between launches, and the PCAS launch method. */ #include "omega_gpu_cmdlist.h" +#include "omega_gpu_dep.h" +#include "omega_gpu_fmt.h" #include "omega_blackwell_codegen.h" #include "omega_blackwell_qmd.h" #include "omega_blackwell_submit.h" @@ -46,18 +48,48 @@ struct OmegaGpuCmdList { uint32_t nop_gpr; uint8_t *indep; /* per launch: 1 = shares no data with the launch before it (mark_independent): no barrier before it */ int stamped; /* the last submit had opts.stamps (the accessor refuses stale stamps from an earlier submit) */ + /* dependency flags (omega_gpu_dep.h): the counter line (+0 done, +4 camera slot allocator, +8 u64 camera base), the camera + * records (OMEGA_DEP_CAM_SLOTS x 16 B, allocated with OMEGA_DEP_CAMERA), per-launch CTA counts, the running CTA total */ + OmegaGpuMem dep_line, dep_cam; + uint32_t dep, dep_sum, dep_observed; + uint32_t *ctas; }; static uint32_t g_latched; int omega_gpu_cmdlist_is_latched(void) { return __atomic_load_n(&g_latched, __ATOMIC_ACQUIRE) ? 1 : 0; } uint32_t omega_gpu_cmdlist_count(const OmegaGpuCmdList *cl) { return cl ? cl->n : 0; } -void omega_gpu_cmdlist_reset(OmegaGpuCmdList *cl) { if (cl) cl->n = 0; } +void omega_gpu_cmdlist_reset(OmegaGpuCmdList *cl) { if (cl) { cl->n = 0; cl->dep_sum = 0; } } +static inline uint32_t *dep_target_word(OmegaGpuCmdList *cl, uint32_t i) { + return (uint32_t *)((uint8_t *)cl->banks.cpu + (size_t)i * OMEGA_GPU_CMDLIST_BANK_BYTES + OMEGA_DEP_ARG_TARGET); +} int omega_gpu_cmdlist_mark_independent(OmegaGpuCmdList *cl) { if (!cl || cl->n == 0) { omega_gpu_session_set_error("cmdlist mark_independent: no launch recorded"); return -1; } + if (cl->indep[cl->n - 1]) return 0; /* already marked: do not lower the target twice */ cl->indep[cl->n - 1] = 1; + /* dep mode: wait for the launches before the previous one only (S_(i-2)); the launch after this one keeps S_i, which counts both */ + if ((cl->dep & OMEGA_DEP_FLAGS) && cl->n >= 2) *dep_target_word(cl, cl->n - 1) -= cl->ctas[cl->n - 2]; + return 0; +} +int omega_gpu_cmdlist_set_dep(OmegaGpuCmdList *cl, uint32_t dep) { + if (!cl || (dep & ~(OMEGA_DEP_FLAGS | OMEGA_DEP_CAMERA))) { omega_gpu_session_set_error("cmdlist set_dep: arguments (FLAGS | CAMERA only)"); return -1; } + if (cl->n) { omega_gpu_session_set_error("cmdlist set_dep: the list is not empty (reset first)"); return -1; } + if ((dep & OMEGA_DEP_CAMERA) && !cl->dep_cam.cpu) { + /* one slot past the ring so the base can be moved up by 16 B when its low word would read as zero (the kernel tests the low word) */ + if (omega_gpu_session_alloc((size_t)(OMEGA_DEP_CAM_SLOTS + 1u) * 16u, &cl->dep_cam) != 0) { omega_gpu_session_set_error("cmdlist set_dep: camera alloc"); return -1; } + memset(cl->dep_cam.cpu, 0, (size_t)(OMEGA_DEP_CAM_SLOTS + 1u) * 16u); + if (omega_gpu_session_sync_to_gpu(&cl->dep_cam) != 0) return -1; + } + uint64_t cam = (dep & OMEGA_DEP_CAMERA) ? cl->dep_cam.va : 0; + if (cam && (uint32_t)cam == 0) cam += 16u; + uint64_t *line = cl->dep_line.cpu; + line[0] = 0; line[1] = cam; + if (omega_gpu_session_sync_to_gpu(&cl->dep_line) != 0) return -1; + cl->dep = dep; cl->dep_sum = 0; cl->dep_observed = 0; return 0; } +uint32_t omega_gpu_cmdlist_dep(const OmegaGpuCmdList *cl) { return cl ? cl->dep : 0; } +uint32_t omega_gpu_cmdlist_dep_counter(const OmegaGpuCmdList *cl) { return cl ? cl->dep_observed : 0; } static int build_nop(OmegaGpuCmdList *cl) { BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); @@ -88,19 +120,22 @@ int omega_gpu_cmdlist_create(uint32_t cap, OmegaGpuCmdList **out) { cl->words_cap = RING_WORDS; cl->words = omega_gpu_host_alloc((size_t)cl->words_cap * 4u); cl->indep = omega_gpu_host_calloc(cap, 1); - int rc = cl->words && cl->indep ? 0 : -1; + cl->ctas = omega_gpu_host_calloc(cap, sizeof *cl->ctas); + int rc = cl->words && cl->indep && cl->ctas ? 0 : -1; omega_gpu_session_lock(); if (rc == 0 && !omega_gpu_session_open()) rc = -1; if (rc == 0 && (omega_gpu_session_alloc((size_t)cap * OMEGA_GPU_CMDLIST_BANK_BYTES, &cl->banks) != 0 || omega_gpu_session_alloc((size_t)cap * OMEGA_GPU_CMDLIST_QMD_BYTES, &cl->qmds) != 0 || omega_gpu_session_alloc((size_t)cap * 16u, &cl->sems) != 0 || omega_gpu_session_alloc((size_t)(cap + 1u) * 16u, &cl->stamps) != 0 || - omega_gpu_session_alloc(0x4000, &cl->scratch) != 0)) + omega_gpu_session_alloc(0x4000, &cl->scratch) != 0 || + omega_gpu_session_alloc(OMEGA_DEP_LINE_BYTES, &cl->dep_line) != 0)) rc = -1; if (rc == 0 && build_nop(cl) != 0) { omega_gpu_session_set_error("cmdlist create: nop kernel"); rc = -1; } if (rc == 0) { memset(cl->scratch.cpu, 0, 0x4000); - if (omega_gpu_session_sync_to_gpu(&cl->scratch) != 0) rc = -1; + memset(cl->dep_line.cpu, 0, OMEGA_DEP_LINE_BYTES); + if (omega_gpu_session_sync_to_gpu(&cl->scratch) != 0 || omega_gpu_session_sync_to_gpu(&cl->dep_line) != 0) rc = -1; } omega_gpu_session_unlock(); if (rc != 0) { omega_gpu_cmdlist_free(cl); return -1; } @@ -111,11 +146,12 @@ int omega_gpu_cmdlist_create(uint32_t cap, OmegaGpuCmdList **out) { void omega_gpu_cmdlist_free(OmegaGpuCmdList *cl) { if (!cl) return; omega_gpu_session_lock(); - OmegaGpuMem *m[] = { &cl->banks, &cl->qmds, &cl->sems, &cl->stamps, &cl->scratch, &cl->nop_code }; + OmegaGpuMem *m[] = { &cl->banks, &cl->qmds, &cl->sems, &cl->stamps, &cl->scratch, &cl->nop_code, &cl->dep_line, &cl->dep_cam }; for (unsigned i = 0; i < sizeof m / sizeof m[0]; i++) if (m[i]->cpu) omega_gpu_session_free(m[i]); omega_gpu_session_unlock(); omega_gpu_host_free(cl->words); omega_gpu_host_free(cl->indep); + omega_gpu_host_free(cl->ctas); omega_gpu_host_free(cl); } @@ -125,6 +161,8 @@ int omega_gpu_cmdlist_add(OmegaGpuCmdList *cl, const OmegaGpuLaunch *L) { if (!cl || !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->code_va == 0) { omega_gpu_session_set_error("cmdlist add: arguments"); return -1; } if (cl->n >= cl->cap) { omega_gpu_session_set_error("cmdlist add: list full"); return -1; } + const int depmode = (cl->dep & OMEGA_DEP_FLAGS) != 0; + if (cl->dep && L->n_args > OMEGA_DEP_MAX_KERNEL_ARGS) { omega_gpu_session_set_error("cmdlist add: more than 26 argument words in dep mode (words 0x3e8.. carry the counter)"); return -1; } const uint32_t i = cl->n; const uint32_t ty = L->threads_y ? L->threads_y : 1; const uint64_t bank_va = cl->banks.va + (uint64_t)i * OMEGA_GPU_CMDLIST_BANK_BYTES; @@ -132,8 +170,15 @@ int omega_gpu_cmdlist_add(OmegaGpuCmdList *cl, const OmegaGpuLaunch *L) { uint32_t drv[OMEGA_BW_CBANK_DRIVER_WORDS]; omega_blackwell_build_cbank_driver_2d(drv, bank_va, L->threads_x, ty, L->grid_x, L->grid_y); memcpy(bank, drv, sizeof drv); - memset(bank + sizeof drv, 0, 0x380 - sizeof drv); + memset(bank + sizeof drv, 0, OMEGA_GPU_CMDLIST_BANK_BYTES - sizeof drv); memcpy(bank + 0x380, L->args, (size_t)L->n_args * 4u); + if (cl->dep) { /* counter address for the wait and the add, target = CTAs of every launch before this one (camera-only lists too: the add stays off) */ + const uint64_t line_va = cl->dep_line.va; + memcpy(bank + OMEGA_DEP_ARG_WAIT, &line_va, 8); memcpy(bank + OMEGA_DEP_ARG_DONE, &line_va, 8); + memcpy(bank + OMEGA_DEP_ARG_TARGET, &cl->dep_sum, 4); + } + cl->ctas[i] = L->grid_x * L->grid_y; + if (depmode) cl->dep_sum += cl->ctas[i]; const uint64_t q1_va = cl->qmds.va + (uint64_t)i * OMEGA_GPU_CMDLIST_QMD_BYTES, q0_va = q1_va + 0x200; const uint64_t sem_va = cl->sems.va + (uint64_t)i * 16u; @@ -243,12 +288,20 @@ int omega_gpu_cmdlist_submit(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *opt const uint64_t timeout = o.timeout_ms ? o.timeout_ms : DEFAULT_TIMEOUT_MS; const uint32_t spin = o.spin_us ? o.spin_us : omega_gpu_session_spin_us(); const uint64_t th0 = p->time_now_ns(p->ctx); + const int depmode = (cl->dep & OMEGA_DEP_FLAGS) != 0; + if (depmode && o.mode == OMEGA_GPU_CMDLIST_INLINE) { omega_gpu_session_set_error("cmdlist submit: dep flags need PCAS mode (one submission, no per-launch barrier)"); return -1; } + if (depmode && o.skip_launch) { omega_gpu_session_set_error("cmdlist submit: skip_launch with dep flags would hang the launches after it (the sentinel bounds the wait)"); return -1; } - /* semaphores INIT, markers 0, everything the CPU wrote made visible */ + /* semaphores INIT, markers 0, dep counter and camera slot 0, everything the CPU wrote made visible */ uint32_t *sem = cl->sems.cpu; for (uint32_t i = 0; i < cl->n; i++) sem[4 * i] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT; volatile uint32_t *mk = (volatile uint32_t *)((uint8_t *)marker->cpu + MARKER_OFF); mk[0] = 0; mk[4] = 0; + if (cl->dep) { + uint32_t *line = cl->dep_line.cpu; + line[0] = 0; line[1] = 0; cl->dep_observed = 0; + if (p->sync_cpu_to_gpu(p->ctx, cl->dep_line.handle, 0, OMEGA_DEP_LINE_BYTES) != OMEGA_GPU_PLAT_OK) { omega_gpu_session_set_error("cmdlist submit: sync dep line"); return -1; } + } if (p->sync_cpu_to_gpu(p->ctx, cl->banks.handle, 0, (uint64_t)cl->n * OMEGA_GPU_CMDLIST_BANK_BYTES) != OMEGA_GPU_PLAT_OK || p->sync_cpu_to_gpu(p->ctx, cl->qmds.handle, 0, (uint64_t)cl->n * OMEGA_GPU_CMDLIST_QMD_BYTES) != OMEGA_GPU_PLAT_OK || p->sync_cpu_to_gpu(p->ctx, cl->sems.handle, 0, (uint64_t)cl->n * 16u) != OMEGA_GPU_PLAT_OK || @@ -271,6 +324,7 @@ int omega_gpu_cmdlist_submit(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *opt } } const uint32_t room = (avail - prefix) / per_launch; + if (depmode && room < cl->n) { omega_gpu_session_set_error("cmdlist submit: dep flags need the whole list in one submission (ring too small for this many launches)"); return -1; } uint64_t elapsed = 0, host = p->time_now_ns(p->ctx) - th0; uint32_t done = 0, submits = 0, words = 0; while (done < cl->n) { @@ -282,7 +336,8 @@ int omega_gpu_cmdlist_submit(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *opt for (uint32_t i = done; i < end; i++) { if (o.skip_launch && i == (uint32_t)(o.skip_launch - 1)) continue; /* the first launch of a submission always gets the full barrier (CPU-written QMDs, code and banks before it) */ - const int indep = i != done && cl->indep[i]; /* marked independent: no barrier, overlaps the launch before it */ + /* marked independent, or dep flags (the kernels order themselves): no barrier, the grid may overlap the launch before it */ + const int indep = i != done && (cl->indep[i] || depmode); const OmegaGpuCmdListBarrier b = i == done ? OMEGA_GPU_CMDLIST_BARRIER_WFI : indep ? OMEGA_GPU_CMDLIST_BARRIER_NONE : o.barrier; if (indep && o.stamps) n = emit_stamp(pb, n, cl->stamps.va, i); /* no WFI: slot i is the front-end time, launch i - 1 may still run */ if (b != OMEGA_GPU_CMDLIST_BARRIER_NONE) { @@ -341,6 +396,19 @@ int omega_gpu_cmdlist_submit(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *opt } if (st) { st->launches = cl->n; st->submits = submits; st->words = words; st->elapsed_ns = elapsed; st->host_ns = host; st->sems_done = ok; st->first_missing = missing; } if (missing >= 0) { omega_gpu_session_set_error("cmdlist: a launch semaphore is not DONE (launch did not run)"); return -1; } + if (cl->dep) { + (void)p->sync_gpu_to_cpu(p->ctx, cl->dep_line.handle, 0, OMEGA_DEP_LINE_BYTES); + if (cl->dep_cam.cpu) (void)p->sync_gpu_to_cpu(p->ctx, cl->dep_cam.handle, 0, (uint64_t)(OMEGA_DEP_CAM_SLOTS + 1u) * 16u); + cl->dep_observed = ((const uint32_t *)cl->dep_line.cpu)[0]; + if (st) { st->dep_expected = cl->dep_sum; st->dep_observed = cl->dep_observed; } + if (depmode && cl->dep_observed != cl->dep_sum) { + char e[160]; + omega_gpu_fmt(e, sizeof e, "cmdlist: dep counter %u after completion, expected %u CTAs%s", cl->dep_observed, cl->dep_sum, + cl->dep_observed > cl->dep_sum ? " (a launch's poll timed out: sentinel added)" : " (a launch did not count itself in)"); + omega_gpu_session_set_error(e); + return -1; + } + } return 0; } diff --git a/src/omega_gpu_cmdlist.h b/src/omega_gpu_cmdlist.h index aaebf037e..5deb360df 100644 --- a/src/omega_gpu_cmdlist.h +++ b/src/omega_gpu_cmdlist.h @@ -21,6 +21,14 @@ * here is preceded by WAIT_FOR_IDLE and the session's INVALIDATE_SHADER_CACHES_ALL (instruction, data, constant). * OMEGA_GPU_CMDLIST_BARRIER_NONE exists only as the negative control of the chip test. * + * Dependency flags (DEPFLAGS, 2026-10-16, opt-in, omega_gpu_dep.h): omega_gpu_cmdlist_set_dep(cl, OMEGA_DEP_FLAGS) on an + * empty list makes submit emit NO barrier between launches; instead every launch's kernel (built with spec.dep = the list's + * value, see omega_bw_dep.h) polls one shared counter until every CTA of every launch before it has added 1, and adds 1 per + * CTA at exit. add() writes the counter address and the launch's target (CTAs of launches 0..i-1) into bank words + * 0x3e8..0x3fb, so a dep-mode launch may carry at most 26 argument words. The host zeroes the counter before the submit and + * checks it against the CTA total after the completion marker (stats dep_expected / dep_observed). dep 0 (the default) + * changes nothing: the barrier orders the launches exactly as before. + * * Launch methods (opts.mode): * PCAS QMD in memory, SEND_PCAS_A (qmd >> 8) + SEND_SIGNALING_PCAS2_B INVALIDATE_COPY_SCHEDULE (clcec0.h 0x02b4 / * 0x02c0; the NVK dispatch sequence, nvk_cmd_dispatch.c), no dependent QMD: 8 words per launch, a whole @@ -76,6 +84,8 @@ typedef struct { uint64_t host_ns; /* building the method stream (CPU) */ uint32_t sems_done; /* launch semaphores found DONE */ int32_t first_missing; /* index of the first launch whose semaphore was not DONE, -1 if none */ + uint32_t dep_expected; /* dep mode: CTAs over all launches (what the counter must read after completion); 0 in dep 0 */ + uint32_t dep_observed; /* dep mode: the counter read back after completion */ } OmegaGpuCmdListStats; typedef struct OmegaGpuCmdList OmegaGpuCmdList; @@ -91,9 +101,16 @@ uint32_t omega_gpu_cmdlist_count(const OmegaGpuCmdList *cl); * prefetch beside the latency-bound attention). The launch after it gets the normal barrier, which waits for both. * Ignored for the first launch of a submission. 0 ok, -1 (no launch recorded). */ int omega_gpu_cmdlist_mark_independent(OmegaGpuCmdList *cl); +/* Caller holds the lock. Dependency flags for every launch recorded from now on (omega_gpu_dep.h: OMEGA_DEP_FLAGS, | OMEGA_DEP_CAMERA; + * 0 = the barrier, today's behaviour). Only on an empty list (reset first); other bits refused. Kernels recorded into a dep-mode + * list must be built with spec.dep == omega_gpu_cmdlist_dep(cl) (every kernel API does so when given the list). 0 ok, -1. */ +int omega_gpu_cmdlist_set_dep(OmegaGpuCmdList *cl, uint32_t dep); +uint32_t omega_gpu_cmdlist_dep(const OmegaGpuCmdList *cl); +/* After a submit in dep mode: the counter read back (== CTAs of all launches when every launch counted itself in). */ +uint32_t omega_gpu_cmdlist_dep_counter(const OmegaGpuCmdList *cl); /* Caller holds the lock. Writes the launch's bank and QMD into the next slot. The launch's code must stay resident - * until submit returns. Returns the launch index, or -1 (full, bad arguments, QMD refused). L->timeout_ms and - * L->spin_us are ignored (the list has one completion). */ + * until submit returns. Returns the launch index, or -1 (full, bad arguments, QMD refused, more than 26 argument words in + * dep mode). L->timeout_ms and L->spin_us are ignored (the list has one completion). */ int omega_gpu_cmdlist_add(OmegaGpuCmdList *cl, const OmegaGpuLaunch *L); /* Caller holds the lock, device open, not latched. Submits every recorded launch in order, waits for the completion, * checks every launch semaphore. 0 ok; -1 with the stage named (omega_gpu_session_last_error). */ diff --git a/src/omega_gpu_decode_attn_api.c b/src/omega_gpu_decode_attn_api.c index e69f65ad1..ff4907deb 100644 --- a/src/omega_gpu_decode_attn_api.c +++ b/src/omega_gpu_decode_attn_api.c @@ -32,14 +32,15 @@ typedef struct { static struct { bool hooked; - Slot slot; + Slot slot[2]; /* [0] today's kernel, [1] the dep-mode kernel (DEPFLAGS): a dep-mode record right after the host path must not evict the resident kernel */ uint32_t depth, per_split; int mutant; } g; static int g_rec; /* 1 while a record call runs (lock held): kernel_for must not evict */ -static void slot_clear_locked(void) { if (g.slot.valid) omega_gpu_session_free(&g.slot.code); memset(&g.slot, 0, sizeof g.slot); } -static void on_session_close(void) { memset(&g.slot, 0, sizeof g.slot); } +static void slot_free_locked(Slot *s) { if (s->valid) omega_gpu_session_free(&s->code); memset(s, 0, sizeof *s); } +static void slot_clear_locked(void) { slot_free_locked(&g.slot[0]); slot_free_locked(&g.slot[1]); } +static void on_session_close(void) { memset(g.slot, 0, sizeof g.slot); } 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(); @@ -149,10 +150,10 @@ int omega_gpu_decode_attn_cache_read(OmegaGpuDecodeAttnCache *c, uint32_t layer, /* ---- kernel ---- */ static int kernel_for(const OmegaDecodeAttnSpec *spec, Slot **out, bool *hit) { - Slot *s = &g.slot; + Slot *s = &g.slot[spec->dep ? 1 : 0]; /* one resident kernel per dep mode */ if (s->valid && memcmp(&s->spec, spec, sizeof *spec) == 0) { *out = s; *hit = true; return OMEGA_GPU_DATTN_OK; } if (g_rec && s->valid) { omega_gpu_session_set_error("decode attn record: kernel cache miss would evict the resident kernel (warm up before recording)"); return OMEGA_GPU_DATTN_TOO_LARGE; } - slot_clear_locked(); + slot_free_locked(s); BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); if (!prog) { omega_gpu_session_set_error("decode attn codegen: host memory"); return OMEGA_GPU_DATTN_CODEGEN_FAIL; } int cg = omega_blackwell_codegen_decode_attn_prog(spec, prog); @@ -186,7 +187,7 @@ static int dattn_core(OmegaGpuDecodeAttnCache *c, uint32_t layer, uint32_t pos, const uint32_t splits = (pos + per) / per; /* ceil((pos + 1) / per) */ if (splits > c->max_splits) return OMEGA_GPU_DATTN_TOO_LARGE; OmegaDecodeAttnSpec spec; memset(&spec, 0, sizeof spec); - spec.depth = g.depth ? g.depth : OMEGA_GPU_DATTN_DEPTH; spec.mutant = g.mutant; + spec.depth = g.depth ? g.depth : OMEGA_GPU_DATTN_DEPTH; spec.mutant = g.mutant; spec.dep = rec ? omega_gpu_cmdlist_dep(rec) : 0; /* dep-mode list: the kernel carries the counter protocol */ if (!rec) omega_gpu_session_lock(); #define UNLOCK() do { if (!rec) omega_gpu_session_unlock(); } while (0) diff --git a/src/omega_gpu_decode_graph.c b/src/omega_gpu_decode_graph.c index 5a07c7bb6..9097e3ee4 100644 --- a/src/omega_gpu_decode_graph.c +++ b/src/omega_gpu_decode_graph.c @@ -5,6 +5,7 @@ */ #include "omega_gpu_decode_graph.h" #include "omega_gpu_cmdlist.h" +#include "omega_gpu_dep.h" #include "omega_blackwell_codegen_dec.h" #include "omega_gpu_session.h" #include "omega_gpu_fmt.h" @@ -61,8 +62,8 @@ static int fail(OmegaGpuDecodeGraph *g, const char *what, int rc) { } /* lock held */ -static int kern_build(Kern *k, OmegaDecKind kind, uint32_t mode, int mutant) { - OmegaDecSpec s; memset(&s, 0, sizeof s); s.kind = kind; s.mode = mode; s.mutant = mutant; +static int kern_build(Kern *k, OmegaDecKind kind, uint32_t mode, int mutant, uint32_t dep) { + OmegaDecSpec s; memset(&s, 0, sizeof s); s.kind = kind; s.mode = mode; s.mutant = mutant; s.dep = dep; BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); if (!prog) return OMEGA_GPU_DGRAPH_CODEGEN_FAIL; int rc = OMEGA_GPU_DGRAPH_OK; @@ -116,6 +117,8 @@ int omega_gpu_decode_graph_create(const OmegaGpuDecodeGraphConfig *cfg, OmegaGpu if (omega_gpu_cmdlist_create(OMEGA_GPU_CMDLIST_MAX_LAUNCHES, &g->cl) != 0) { omega_gpu_decode_graph_free(g); return OMEGA_GPU_DGRAPH_CHIP_FAIL; } omega_gpu_session_lock(); if (!omega_gpu_session_open() || omega_gpu_session_alloc(L->bytes, &g->buf) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; + /* DEPFLAGS (opt-in, AIEN_OMEGA_DEP / omega_gpu_dep_set_mode): the list and every kernel recorded into it carry the counter protocol; 0 = the barrier */ + if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_cmdlist_set_dep(g->cl, omega_gpu_dep_mode()) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; static const struct { OmegaDecKind kind; uint32_t mode; int mutant; } spec[K_COUNT] = { [K_NORM] = { OMEGA_DEC_RES, OMEGA_DEC_RES_NORM, 0 }, [K_ADD] = { OMEGA_DEC_RES, OMEGA_DEC_RES_ADD, 0 }, @@ -126,7 +129,7 @@ int omega_gpu_decode_graph_create(const OmegaGpuDecodeGraphConfig *cfg, OmegaGpu [K_SW_M1] = { OMEGA_DEC_SWIGLU, 0, 1 }, [K_CN_M2] = { OMEGA_DEC_RES, OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM, 2 }, }; - for (unsigned i = 0; i < K_COUNT && rc == OMEGA_GPU_DGRAPH_OK; i++) rc = kern_build(&g->k[i], spec[i].kind, spec[i].mode, spec[i].mutant); + for (unsigned i = 0; i < K_COUNT && rc == OMEGA_GPU_DGRAPH_OK; i++) rc = kern_build(&g->k[i], spec[i].kind, spec[i].mode, spec[i].mutant, omega_gpu_cmdlist_dep(g->cl)); if (rc == OMEGA_GPU_DGRAPH_OK) { memset(g->buf.cpu, 0, L->bytes); float *lg = cpu_of(g, L->logits), *amv = cpu_of(g, L->am_v); diff --git a/src/omega_gpu_dep.c b/src/omega_gpu_dep.c new file mode 100644 index 000000000..069d3301f --- /dev/null +++ b/src/omega_gpu_dep.c @@ -0,0 +1,9 @@ +/* omega_gpu_dep.c: see the header. */ +#include "omega_gpu_dep.h" +#include <stdlib.h> +static int g_set; static uint32_t g_mode; +uint32_t omega_gpu_dep_mode(void) { + if (!g_set) { const char *e = getenv("AIEN_OMEGA_DEP"); g_mode = e ? (uint32_t)atoi(e) & 3u : 0u; g_set = 1; } + return g_mode; +} +void omega_gpu_dep_set_mode(uint32_t mode) { g_mode = mode & 3u; g_set = 1; } diff --git a/src/omega_gpu_dep.h b/src/omega_gpu_dep.h new file mode 100644 index 000000000..63dbef618 --- /dev/null +++ b/src/omega_gpu_dep.h @@ -0,0 +1,48 @@ +#ifndef OMEGA_GPU_DEP_H +#define OMEGA_GPU_DEP_H +/* omega_gpu_dep: dependency flags between the launches of one command list (DEPFLAGS, 2026-10-16), the host-visible part. + * + * Mode (process-wide, omega_gpu_dep_mode): 0 = none (the WAIT_FOR_IDLE barrier orders the launches, the default), + * 1 = flags (OMEGA_DEP_FLAGS), 2 = camera only, 3 = flags + camera. Setter, else env AIEN_OMEGA_DEP read once. The decode + * graph passes it to its command list (omega_gpu_cmdlist_set_dep); every kernel API folds the list's value into its spec + * (kernel cache key), so a direct launch (no list) always builds dep 0 kernels. Changing the mode after kernels were built + * rebuilds them on the next call (different spec); the caller clears caches it pinned. + * + * Protocol (one shared counter per command list, omega_gpu_cmdlist.c writes the bank words, omega_bw_dep.h the kernel): + * counter line 64 B, GPU-uncached (omega_gpu_session_alloc): +0 u32 done (every CTA of every launch adds 1 before it + * exits), +4 u32 camera slot allocator, +8 u64 camera base (0 = off). The host zeroes +0 and +4 before + * every submit, so targets are plain CTA counts. + * bank words OMEGA_DEP_ARG_WAIT u64 = line va, OMEGA_DEP_ARG_DONE u64 = line va (the same line; two words so a + * per-launch-line layout stays possible), OMEGA_DEP_ARG_TARGET u32 = S_(i-1) = CTAs of launches 0..i-1. + * kernel warp 0 polls until done - target < 2^31 (wrap-safe), BAR.SYNC, CCTL.IVALL; at exit MEMBAR.ALL.GPU, + * BAR.SYNC, thread 0 adds 1. Why that orders the list: the first moment done reaches S_i, no CTA of a launch + * j > i has passed its poll (it needs done >= S_(j-1) >= S_i), so all S_i adds came from launches 0..i, which + * have therefore all finished. A launch marked independent waits for S_(i-2) instead; the launch after it + * keeps S_i, which still counts both. A poll that never succeeds (OMEGA_DEP_POLL_LIMIT reads) adds + * OMEGA_DEP_TIMEOUT_SENTINEL and continues, so nothing hangs the chip and the host sees the wrong count. + * host check after the completion marker: done == sum of all CTAs, else an error naming the stage. */ +#include <stdint.h> +#ifdef __cplusplus +extern "C" { +#endif + +#define OMEGA_DEP_FLAGS 1u /* kernels order themselves with the counter (barrier NONE in the list) */ +#define OMEGA_DEP_CAMERA 2u /* every CTA's thread 0 records {GLOBALTIMER at entry, at exit} */ +#define OMEGA_DEP_TEST_NO_PROLOGUE 4u /* TEST ONLY: emit no wait (a kernel that reads too early; the sim test must catch it) */ +#define OMEGA_DEP_TEST_NO_EPILOGUE 8u /* TEST ONLY: emit no add (a kernel that never reports; the host count must catch it) */ +#define OMEGA_DEP_ARG_WAIT 0x3e8u /* u64 va of the counter to wait on */ +#define OMEGA_DEP_ARG_DONE 0x3f0u /* u64 va of the counter this launch adds to */ +#define OMEGA_DEP_ARG_TARGET 0x3f8u /* u32 */ +#define OMEGA_DEP_ARGS_FIRST 0x3e8u /* kernel arguments must end here: at most (0x3e8 - 0x380) / 4 = 26 words in dep mode */ +#define OMEGA_DEP_MAX_KERNEL_ARGS 26u +#define OMEGA_DEP_LINE_BYTES 64u +#define OMEGA_DEP_CAM_SLOTS 4096u /* records per launch (16 B each): CTAs beyond that wrap onto the first slots */ +#define OMEGA_DEP_POLL_LIMIT (1u << 18) +#define OMEGA_DEP_TIMEOUT_SENTINEL 0x12345678u + +uint32_t omega_gpu_dep_mode(void); +void omega_gpu_dep_set_mode(uint32_t mode); /* masked to OMEGA_DEP_FLAGS | OMEGA_DEP_CAMERA */ +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/omega_gpu_fp8_api.c b/src/omega_gpu_fp8_api.c index 199dab47a..ae9b5bfd4 100644 --- a/src/omega_gpu_fp8_api.c +++ b/src/omega_gpu_fp8_api.c @@ -303,7 +303,7 @@ static int gemv_core(const OmegaGpuFp8Tensor *t, uint32_t groups, const uint32_t if (groups > OMEGA_GPU_FP8_MAX_GROUPS) return OMEGA_GPU_FP8_TOO_LARGE; if (!dev_tab) for (uint32_t gi = 0; gi < groups; gi++) if (ids[gi] >= t->experts) return OMEGA_GPU_FP8_BAD_ARGS; const uint32_t n = t->n, k = t->k; - OmegaFp8GemvSpec spec; spec_for(k, n, groups, &spec); + OmegaFp8GemvSpec spec; spec_for(k, n, groups, &spec); spec.dep = rec ? omega_gpu_cmdlist_dep(rec) : 0; /* dep-mode list: the kernel carries the counter protocol */ if (n % spec.rows) return OMEGA_GPU_FP8_CODEGEN_FAIL; const uint32_t RG = n / spec.rows; uint32_t grid_x = (RG + spec.warps - 1) / spec.warps; diff --git a/src/omega_gpu_router_api.c b/src/omega_gpu_router_api.c index b786e10d4..9f7a42dbf 100644 --- a/src/omega_gpu_router_api.c +++ b/src/omega_gpu_router_api.c @@ -40,14 +40,15 @@ typedef struct { static struct { bool hooked; - Slot slot; + Slot slot[2]; /* [0] today's kernel, [1] the dep-mode kernel (DEPFLAGS): a dep-mode record right after the host path must not evict the resident kernel */ uint32_t warps, depth; int mutant; } g; static int g_rec; /* 1 while a record call runs (lock held): kernel_for must not evict */ -static void slot_clear_locked(void) { if (g.slot.valid) omega_gpu_session_free(&g.slot.code); memset(&g.slot, 0, sizeof g.slot); } -static void on_session_close(void) { memset(&g.slot, 0, sizeof g.slot); } +static void slot_free_locked(Slot *s) { if (s->valid) omega_gpu_session_free(&s->code); memset(s, 0, sizeof *s); } +static void slot_clear_locked(void) { slot_free_locked(&g.slot[0]); slot_free_locked(&g.slot[1]); } +static void on_session_close(void) { memset(g.slot, 0, sizeof g.slot); } 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(); @@ -157,10 +158,10 @@ int omega_gpu_router_read_back(OmegaGpuRouter *r, float *xout, uint64_t *tab_gu, /* ---- kernel ---- */ static int kernel_for(const OmegaRouterSpec *spec, Slot **out, bool *hit) { - Slot *s = &g.slot; + Slot *s = &g.slot[spec->dep ? 1 : 0]; /* one resident kernel per dep mode */ if (s->valid && memcmp(&s->spec, spec, sizeof *spec) == 0) { *out = s; *hit = true; return OMEGA_GPU_ROUTER_OK; } if (g_rec && s->valid) { omega_gpu_session_set_error("router record: kernel cache miss would evict the resident kernel (warm up before recording)"); return OMEGA_GPU_ROUTER_TOO_LARGE; } - slot_clear_locked(); + slot_free_locked(s); BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); if (!prog) { omega_gpu_session_set_error("router codegen: host memory"); return OMEGA_GPU_ROUTER_CODEGEN_FAIL; } int cg = omega_blackwell_codegen_router_prog(spec, prog); @@ -188,7 +189,7 @@ static int router_core(OmegaGpuRouter *r, uint32_t layer, const float *x, float if (info) memset(info, 0, sizeof *info); if (!r || layer >= r->layers || (rec ? !dev_x : (!x || !ids || !weights))) return OMEGA_GPU_ROUTER_BAD_ARGS; OmegaRouterSpec spec; memset(&spec, 0, sizeof spec); - spec.warps = g.warps ? g.warps : OMEGA_GPU_ROUTER_WARPS; spec.depth = g.depth ? g.depth : OMEGA_GPU_ROUTER_DEPTH; spec.mutant = g.mutant; + spec.warps = g.warps ? g.warps : OMEGA_GPU_ROUTER_WARPS; spec.depth = g.depth ? g.depth : OMEGA_GPU_ROUTER_DEPTH; spec.mutant = g.mutant; spec.dep = rec ? omega_gpu_cmdlist_dep(rec) : 0; /* dep-mode list: the kernel carries the counter protocol */ if (!rec) omega_gpu_session_lock(); #define UNLOCK() do { if (!rec) omega_gpu_session_unlock(); } while (0) diff --git a/tests/.crumb b/tests/.crumb index 5887c156f..f46cd099a 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -296,14 +296,14 @@ } ], "children_root": "sha256:ddc3618190458bc5073e3bee67ffccc68a2ee9f065f2cb0d86545386a29fddf8", - "compiled_at": "2026-10-16T07:53:23Z", + "compiled_at": "2026-10-16T20:31:51Z", "compiler": "crumb-compile/1", - "digest": "sha256:1fb9acb543bcc5bb34df8ff3f2bc12e5e75931b3698e8870e95a0c753dc44d87", + "digest": "sha256:392d42cef45d31a5a27b58fa82d1ef89594ba9db17f3bcb973777119b2f97986", "evidence_files": 16, "evidence_root": "sha256:96d08097822ad95f4e562c19e0ba5df5a035774c48632f1832912c40a27bd27a", "files": 1270, - "generated_at_commit": "4197e0bb49ad1e67d9dac37f259246470ffecedc", - "source_tree": "sha256:62a9c26a26b833178ac09a9dc02479bcd2d7d836e9f3560cc3a43afc9a60975d" + "generated_at_commit": "05d0d8e9ab225dba82698bf89128072f3a6f3a7a", + "source_tree": "sha256:1e2a55983734341d926e907067cdc9d5ffc01739de42f7337941ed107280c0c2" }, "owner_lane": "", "provenance": { diff --git a/tests/bw_warp_sim.h b/tests/bw_warp_sim.h index 3e98e8545..e0810e4ae 100644 --- a/tests/bw_warp_sim.h +++ b/tests/bw_warp_sim.h @@ -39,6 +39,7 @@ #define BW_WARP_SIM_H #include "omega_blackwell_codegen.h" +#include "omega_gpu_dep.h" #include <math.h> #include <stdint.h> #include <stdio.h> @@ -92,8 +93,21 @@ typedef struct { 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; + uint64_t timer; /* DEPFLAGS camera: the value CS2R SR_GLOBALTIMERLO returns, +1 per read */ } WsSim; +/* DEPFLAGS: arm a pre-satisfied counter line for a dep-mode kernel the way omega_gpu_cmdlist_add does for the first launch of a + * list: bank words 0x3e8 / 0x3f0 = the line, 0x3f8 = target 0 (nothing to wait for), the line declared in the range table. + * After ws_run the test checks line[0] == CTAs of the grid (every CTA's thread 0 added 1 at exit). Same helper as bw_warp_sim_k.h. */ +static inline void ws_dep_arm(WsSim *s, uint32_t *line16) { + memset(line16, 0, 64); + const uint64_t va = (uintptr_t)line16; + s->cbank[OMEGA_DEP_ARG_WAIT / 4] = (uint32_t)va; s->cbank[OMEGA_DEP_ARG_WAIT / 4 + 1] = (uint32_t)(va >> 32); + s->cbank[OMEGA_DEP_ARG_DONE / 4] = (uint32_t)va; s->cbank[OMEGA_DEP_ARG_DONE / 4 + 1] = (uint32_t)(va >> 32); + s->cbank[OMEGA_DEP_ARG_TARGET / 4] = 0; + if (s->nranges < WS_MAX_RANGES) { s->ranges[s->nranges].lo = (uintptr_t)va; s->ranges[s->nranges].hi = (uintptr_t)va + 64; s->nranges++; } +} + static void ws_fail(WsSim *s, int err, int pc, const char *what) { if (s->err) return; s->err = err; s->err_pc = pc; @@ -163,6 +177,12 @@ static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc, case BW_IR_MUFU_EX2: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(exp2f(ws_u2f(a))), p); break; case BW_IR_MUFU_RCP: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(1.0f / ws_u2f(a)), p); break; case BW_IR_MUFU_RSQ: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(1.0f / sqrtf(ws_u2f(a))), p); break; + /* DEPFLAGS opcodes (omega_bw_dep.h), as bw_warp_sim_k.h: GPU scope behaves like SYS scope here (CTAs run one after another, + * every earlier add is visible); the fences and the L1 invalidate are no-ops (the simulator has no caches). */ + case BW_IR_LDG_STRONG_GPU: { 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_wrs(l, in->dst_vreg, in->dst_subreg, x, p); break; } + case BW_IR_ATOMG_ADD_STRONG_GPU: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; uint32_t old; memcpy(&old, (const void *)(uintptr_t)ad, 4); uint32_t nw = old + b; memcpy((void *)(uintptr_t)ad, &nw, 4); ws_wrs(l, in->dst_vreg, in->dst_subreg, old, p); break; } + case BW_IR_MEMBAR_ALL_GPU: case BW_IR_ERRBAR: case BW_IR_CGAERRBAR: case BW_IR_CCTL_IVALL: break; + case BW_IR_CS2R_GLOBALTIMER: ws_wr64(l, in->dst_vreg, s->timer++, p); break; default: ws_fail(s, WS_E_OP, pc, "opcode not modelled"); return; } } diff --git a/tests/bw_warp_sim_k.h b/tests/bw_warp_sim_k.h index 676b29344..ce1f045c3 100644 --- a/tests/bw_warp_sim_k.h +++ b/tests/bw_warp_sim_k.h @@ -42,6 +42,7 @@ * The simulator runs CTAs one after another, so an atomic counter sees every earlier CTA's add. */ #include "omega_blackwell_codegen.h" +#include "omega_gpu_dep.h" #include <math.h> #include <stdint.h> #include <stdio.h> @@ -81,7 +82,7 @@ typedef struct { } WsWarp; typedef struct { uintptr_t lo, hi; } WsRange; -typedef struct { +typedef struct WsSim { 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 */ @@ -97,8 +98,24 @@ typedef struct { 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; + /* DEPFLAGS (2026-10-16): called on every LDG.E.STRONG.GPU (once per lane) with the address, so a test can play a late + * producer (bump the counter after N polls). timer: the 64-bit value CS2R SR_GLOBALTIMERLO returns, +1 per read. */ + void (*poll_hook)(struct WsSim *s, uint64_t addr, void *ctx); void *poll_ctx; + uint64_t timer; } WsSim; +/* DEPFLAGS: arm a pre-satisfied counter line for a dep-mode kernel the way omega_gpu_cmdlist_add does for the first launch of a + * list: bank words 0x3e8 / 0x3f0 = the line, 0x3f8 = target 0 (nothing to wait for), the line declared in the range table. + * After ws_run the test checks line[0] == CTAs of the grid (every CTA's thread 0 added 1 at exit). */ +static inline void ws_dep_arm(WsSim *s, uint32_t *line16) { + memset(line16, 0, 64); + const uint64_t va = (uintptr_t)line16; + s->cbank[OMEGA_DEP_ARG_WAIT / 4] = (uint32_t)va; s->cbank[OMEGA_DEP_ARG_WAIT / 4 + 1] = (uint32_t)(va >> 32); + s->cbank[OMEGA_DEP_ARG_DONE / 4] = (uint32_t)va; s->cbank[OMEGA_DEP_ARG_DONE / 4 + 1] = (uint32_t)(va >> 32); + s->cbank[OMEGA_DEP_ARG_TARGET / 4] = 0; + if (s->nranges < WS_MAX_RANGES) { s->ranges[s->nranges].lo = (uintptr_t)va; s->ranges[s->nranges].hi = (uintptr_t)va + 64; s->nranges++; } +} + static void ws_fail(WsSim *s, int err, int pc, const char *what) { if (s->err) return; s->err = err; s->err_pc = pc; @@ -155,7 +172,7 @@ static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc, 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_wrs(l, in->dst_vreg, in->dst_subreg, 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_wrs(l, in->dst_vreg, in->dst_subreg, 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_STG_E: { uint64_t ad = ws_rd64(l, in->src1_vreg, p) + (uint64_t)(int64_t)(int32_t)in->imm; if (!ws_mem_ok(s, ad, 4, pc)) return; memcpy((void *)(uintptr_t)ad, &b, 4); break; } /* + imm: the encoder honours a signed 24-bit offset */ case BW_IR_LDG_E_64: case BW_IR_LDG_E_128: { unsigned n = in->op == BW_IR_LDG_E_64 ? 8u : 16u; uint64_t ad = ws_rd64(l, in->src1_vreg, p) + (uint64_t)(int64_t)(int32_t)in->imm; if (!ws_mem_ok(s, ad, n, pc)) { if (getenv("WS_DBG")) fprintf(stderr, " tid %u cx %u cy %u\n", tid, cx, cy); return; } if (ad & (n - 1)) { ws_fail(s, WS_E_MEM, pc, "wide global load not naturally aligned"); return; } uint32_t x[4]; memcpy(x, (const void *)(uintptr_t)ad, n); for (unsigned i = 0; i < n / 4; i++) ws_wrs(l, in->dst_vreg, i, x[i], p); break; } case BW_IR_LDS128: { uint32_t ad = a + in->imm; if (ad & 15u) { ws_fail(s, WS_E_SMEM, pc, "LDS.128 address not 16-byte aligned"); return; } for (unsigned i = 0; i < 4; i++) { uint32_t *m = ws_smem(s, ad + 4u * i, pc); if (!m) return; ws_wrs(l, in->dst_vreg, i, *m, p); } break; } case BW_IR_LDS32: { uint32_t *m = ws_smem(s, a, pc); if (!m) return; ws_wrs(l, in->dst_vreg, in->dst_subreg, *m, p); break; } @@ -174,6 +191,12 @@ static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc, case BW_IR_STG_STRONG_SYS: { 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_ATOMG_ADD_STRONG_SYS: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; uint32_t old; memcpy(&old, (const void *)(uintptr_t)ad, 4); uint32_t nw = old + b; memcpy((void *)(uintptr_t)ad, &nw, 4); ws_wrs(l, in->dst_vreg, in->dst_subreg, old, p); break; } case BW_IR_MEMBAR_ALL_SYS: break; + /* DEPFLAGS opcodes (omega_bw_dep.h). GPU scope behaves like SYS scope here: CTAs run one after another, so every + * earlier add is visible; the fences and the L1 invalidate are no-ops (the simulator has no caches). */ + case BW_IR_LDG_STRONG_GPU: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (s->poll_hook) s->poll_hook(s, ad, s->poll_ctx); if (!ws_mem_ok(s, ad, 4, pc)) return; uint32_t x; memcpy(&x, (const void *)(uintptr_t)ad, 4); ws_wrs(l, in->dst_vreg, in->dst_subreg, x, p); break; } + case BW_IR_ATOMG_ADD_STRONG_GPU: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; uint32_t old; memcpy(&old, (const void *)(uintptr_t)ad, 4); uint32_t nw = old + b; memcpy((void *)(uintptr_t)ad, &nw, 4); ws_wrs(l, in->dst_vreg, in->dst_subreg, old, p); break; } + case BW_IR_MEMBAR_ALL_GPU: case BW_IR_ERRBAR: case BW_IR_CGAERRBAR: case BW_IR_CCTL_IVALL: break; + case BW_IR_CS2R_GLOBALTIMER: ws_wr64(l, in->dst_vreg, s->timer++, p); break; default: ws_fail(s, WS_E_OP, pc, "opcode not modelled"); return; } } diff --git a/tests/gpu_bf16_gemv_test.c b/tests/gpu_bf16_gemv_test.c index 7b8e231c4..0a23ff813 100644 --- a/tests/gpu_bf16_gemv_test.c +++ b/tests/gpu_bf16_gemv_test.c @@ -80,17 +80,24 @@ static void host_only(void) { free(w); free(x); } uint32_t ws[] = { 4, 8, 16, 32 }, rs[] = { 1, 2, 4 }; for (unsigned i = 0; i < 4; i++) for (unsigned j = 0; j < 3; j++) for (uint32_t d = 1; d <= 8; d *= 2) for (int m = 0; m <= 3; m++) { - OmegaBf16GemvSpec s = { 2048, rs[j], ws[i], d, m }; + OmegaBf16GemvSpec s = { 2048, rs[j], ws[i], d, m, 0 }; if (rs[j] * d > 16u) continue; uint32_t n = 0, g = 0; int rc = omega_bf16_gemv_prog_stats(&s, &n, &g); uint32_t decl = ((g + 1 + 7) / 8) * 8; CHECK(rc == 0 && n <= 1024, "codegen k2048 rows %u warps %u depth %u mutant %d: rc %d insns %u gprs %u", rs[j], ws[i], d, m, rc, n, g); if (m == 0 && rc == 0 && rs[j] == 1 && d == 8) printf("codegen rows %u warps %2u depth %u: %u instructions, %u registers (declared %u, register file %u of 65536)%s\n", rs[j], ws[i], d, n, g, decl, 32u * ws[i] * decl, 32u * ws[i] * decl <= 65536u ? "" : " DOES NOT FIT"); } - { OmegaBf16GemvSpec s = { 2048, 3, 8, 8, 0 }; CHECK(omega_bf16_gemv_prog_stats(&s, NULL, NULL) != 0, "rows 3 refused"); } - { OmegaBf16GemvSpec s = { 2000, 1, 8, 8, 0 }; CHECK(omega_bf16_gemv_prog_stats(&s, NULL, NULL) != 0, "k 2000 refused"); } - { OmegaBf16GemvSpec s = { 2048, 1, 8, 3, 0 }; CHECK(omega_bf16_gemv_prog_stats(&s, NULL, NULL) != 0, "depth 3 refused"); } - { OmegaBf16GemvSpec s = { 2048, 1, 8, 8, 4 }; CHECK(omega_bf16_gemv_prog_stats(&s, NULL, NULL) != 0, "mutant 4 refused"); } + for (uint32_t dep = 1; dep <= 3; dep += 2) { /* DEPFLAGS: the lm_head shape with the counter prologue/epilogue (and the camera) stays within budget */ + OmegaBf16GemvSpec s = { 2048, 1, 8, 8, 0, dep }; uint32_t n = 0, g = 0; + int rc = omega_bf16_gemv_prog_stats(&s, &n, &g); + uint32_t decl = ((g + 1 + 7) / 8) * 8; + CHECK(rc == 0 && n <= 1024 && 32u * 8u * decl <= 65536u, "codegen k2048 rows 1 warps 8 depth 8 dep %u: rc %d insns %u gprs %u declared %u", dep, rc, n, g, decl); + } + CHECK(OMEGA_BF16_ARGS_WORDS <= OMEGA_DEP_MAX_KERNEL_ARGS, "argument words end before the dep words (0x3e8)"); + { OmegaBf16GemvSpec s = { 2048, 3, 8, 8, 0, 0 }; CHECK(omega_bf16_gemv_prog_stats(&s, NULL, NULL) != 0, "rows 3 refused"); } + { OmegaBf16GemvSpec s = { 2000, 1, 8, 8, 0, 0 }; CHECK(omega_bf16_gemv_prog_stats(&s, NULL, NULL) != 0, "k 2000 refused"); } + { OmegaBf16GemvSpec s = { 2048, 1, 8, 3, 0, 0 }; CHECK(omega_bf16_gemv_prog_stats(&s, NULL, NULL) != 0, "depth 3 refused"); } + { OmegaBf16GemvSpec s = { 2048, 1, 8, 8, 4, 0 }; CHECK(omega_bf16_gemv_prog_stats(&s, NULL, NULL) != 0, "mutant 4 refused"); } CHECK(OMEGA_BF16_ARGS_WORDS <= OMEGA_GPU_SESSION_MAX_ARGS, "argument words fit the constant bank window"); uint16_t bad[256]; memset(bad, 0, sizeof bad); bad[7] = 0x7f80; OmegaGpuBf16Tensor *t = NULL; @@ -100,9 +107,10 @@ static void host_only(void) { /* ------------------------------------------------------------- simulator */ static uint64_t g_sim_issues; +static uint32_t g_sim_dep; /* DEPFLAGS: spec.dep for the next sim point (0, or OMEGA_DEP_FLAGS: counter armed by ws_dep_arm) */ static int sim_point(uint32_t k, uint32_t n, uint32_t rows, uint32_t warps, uint32_t depth, uint32_t grid_x, int mutant, uint32_t seed, char *msg, size_t msgn, size_t *bad) { static BlackwellIRProgram prog; - OmegaBf16GemvSpec spec = { k, rows, warps, depth, mutant }; + OmegaBf16GemvSpec spec = { k, rows, warps, depth, mutant, g_sim_dep }; if (omega_blackwell_codegen_bf16_gemv_prog(&spec, &prog) != 0) { snprintf(msg, msgn, "codegen refused"); return -1; } uint16_t *w = aligned_alloc(16, (size_t)n * k * 2); { uint16_t *t = make_w((size_t)n * k, seed); memcpy(w, t, (size_t)n * k * 2); free(t); } float *x = aligned_alloc(16, (size_t)k * 4); { float *t = make_x(k, seed + 1); memcpy(x, t, (size_t)k * 4); free(t); } @@ -120,8 +128,10 @@ static int sim_point(uint32_t k, uint32_t n, uint32_t rows, uint32_t warps, uint s.ranges[0].lo = (uintptr_t)w; s.ranges[0].hi = (uintptr_t)w + (size_t)n * k * 2; s.ranges[1].lo = (uintptr_t)x; s.ranges[1].hi = (uintptr_t)x + (size_t)k * 4; s.ranges[2].lo = (uintptr_t)y; s.ranges[2].hi = (uintptr_t)y + (size_t)n * 4; + static uint32_t depline[16]; if (g_sim_dep) ws_dep_arm(&s, depline); err = ws_run(&s, grid_x, 1); if (err) { snprintf(msg, msgn, "order %d: %s (pc %d)", order, s.msg, s.err_pc); break; } + if (g_sim_dep && depline[0] != grid_x) { err = -1; snprintf(msg, msgn, "dep counter %u after %u CTAs", depline[0], grid_x); break; } g_sim_issues += s.warp_issues; size_t unwritten = 0; for (uint32_t i = 0; i < n; i++) if (y[i] == -999.0f) unwritten++; if (unwritten) { err = -1; snprintf(msg, msgn, "%zu output words never written", unwritten); break; } @@ -144,6 +154,14 @@ static void sim_battery(void) { CHECK(!err && bad == 0, "sim k %u n %u rows %u warps %u depth %u grid %u: %s", pts[i].k, pts[i].n, pts[i].rows, pts[i].warps, pts[i].depth, pts[i].grid, msg); printf("sim k %u n %u rows %u warps %u depth %u grid %u: %s %s\n", pts[i].k, pts[i].n, pts[i].rows, pts[i].warps, pts[i].depth, pts[i].grid, err ? "ERROR" : bad ? "MISMATCH" : "ok", msg); } + { /* DEPFLAGS: the dep-mode kernel computes the same rows and every CTA counts itself in */ + char msg[300]; size_t bad = 1; + g_sim_dep = OMEGA_DEP_FLAGS; + int err = sim_point(2048, 64, 1, 8, 4, 2, 0, 31, msg, sizeof msg, &bad); + g_sim_dep = 0; + CHECK(!err && bad == 0, "sim k 2048 n 64 grid 2 with dep flags: %s", msg); + printf("sim k 2048 n 64 grid 2 dep 1: %s %s\n", err ? "ERROR" : bad ? "MISMATCH" : "ok", msg); + } for (int m = 1; m <= 3; m++) { char msg[300]; size_t bad = 0; int err = sim_point(2048, 64, 1, 8, 4, 2, m, 77, msg, sizeof msg, &bad); diff --git a/tests/gpu_cmdlist_test.c b/tests/gpu_cmdlist_test.c index f0647d615..6d07b4551 100644 --- a/tests/gpu_cmdlist_test.c +++ b/tests/gpu_cmdlist_test.c @@ -15,6 +15,7 @@ #include "omega_gpu_cmdlist.h" #include "omega_blackwell_codegen_dec.h" #include "omega_gpu_session.h" +#include "omega_gpu_dep.h" #include "bw_warp_sim_k.h" #include <inttypes.h> #include <math.h> @@ -122,13 +123,13 @@ static int specs_build(void) { /* ---- host-only ---- */ static int host_only(void) { const struct { const char *name; OmegaDecSpec s; } cases[] = { - { "axpy", { OMEGA_DEC_AXPY, 0, AXPY_STRIDE, 0 } }, - { "res add|norm", { OMEGA_DEC_RES, OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM, 0, 0 } }, - { "res combine|norm", { OMEGA_DEC_RES, OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM, 0, 0 } }, - { "res add", { OMEGA_DEC_RES, OMEGA_DEC_RES_ADD, 0, 0 } }, - { "swiglu", { OMEGA_DEC_SWIGLU, 0, 0, 0 } }, - { "argmax", { OMEGA_DEC_ARGMAX, 0, 0, 0 } }, - { "argmax indexed", { OMEGA_DEC_ARGMAX, 1, 0, 0 } }, + { "axpy", { OMEGA_DEC_AXPY, 0, AXPY_STRIDE, 0, 0 } }, + { "res add|norm", { OMEGA_DEC_RES, OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM, 0, 0, 0 } }, + { "res combine|norm", { OMEGA_DEC_RES, OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM, 0, 0, 0 } }, + { "res add", { OMEGA_DEC_RES, OMEGA_DEC_RES_ADD, 0, 0, 0 } }, + { "swiglu", { OMEGA_DEC_SWIGLU, 0, 0, 0, 0 } }, + { "argmax", { OMEGA_DEC_ARGMAX, 0, 0, 0, 0 } }, + { "argmax indexed", { OMEGA_DEC_ARGMAX, 1, 0, 0, 0 } }, }; for (unsigned i = 0; i < sizeof cases / sizeof cases[0]; i++) { uint32_t insns = 0, gprs = 0; @@ -137,8 +138,20 @@ static int host_only(void) { CHECK(gprs <= 200, "%s gprs %u within budget", cases[i].name, gprs); printf("codegen %s: %u insns, peak %u gprs, declared %u\n", cases[i].name, insns, gprs, omega_bw_gpr_declared(gprs)); } - OmegaDecSpec bad[] = { { OMEGA_DEC_AXPY, 0, 0, 0 }, { OMEGA_DEC_AXPY, 0, 100, 0 }, { OMEGA_DEC_RES, 0, 0, 0 }, - { OMEGA_DEC_RES, OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_COMBINE, 0, 0 }, { (OmegaDecKind)0, 0, 0, 0 }, { OMEGA_DEC_RES, 8, 0, 0 } }; + /* DEPFLAGS: every glue kernel also builds with the flags (dep 1) and flags + camera (dep 3) inside the register budget, + * and the kernels' argument words end before the dependency words the command list writes */ + for (unsigned i = 0; i < sizeof cases / sizeof cases[0]; i++) for (uint32_t dep = 1; dep <= 3; dep += 2) { + OmegaDecSpec d = cases[i].s; d.dep = dep; + uint32_t insns = 0, gprs = 0; + int rc = omega_dec_prog_stats(&d, &insns, &gprs); + CHECK(rc == 0 && gprs <= 200, "codegen %s dep %u: rc %d, %u insns, peak %u gprs", cases[i].name, dep, rc, insns, gprs); + if (dep == 1) printf("codegen %s dep 1: %u insns, peak %u gprs\n", cases[i].name, insns, gprs); + } + CHECK(OMEGA_DEC_AXPY_WORDS <= OMEGA_DEP_MAX_KERNEL_ARGS && OMEGA_DEC_RES_WORDS <= OMEGA_DEP_MAX_KERNEL_ARGS && OMEGA_DEC_SW_WORDS <= OMEGA_DEP_MAX_KERNEL_ARGS && OMEGA_DEC_AM_WORDS <= OMEGA_DEP_MAX_KERNEL_ARGS, + "glue kernel arguments end before the dependency words (%u words max)", OMEGA_DEP_MAX_KERNEL_ARGS); + CHECK(0x380u + 4u * OMEGA_DEP_MAX_KERNEL_ARGS == OMEGA_DEP_ARG_WAIT && OMEGA_DEP_ARG_TARGET + 4u <= 0x380u + 4u * OMEGA_GPU_SESSION_MAX_ARGS, "dependency words sit inside the 1 KB bank after the argument window"); + OmegaDecSpec bad[] = { { OMEGA_DEC_AXPY, 0, 0, 0, 0 }, { OMEGA_DEC_AXPY, 0, 100, 0, 0 }, { OMEGA_DEC_RES, 0, 0, 0, 0 }, + { OMEGA_DEC_RES, OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_COMBINE, 0, 0, 0 }, { (OmegaDecKind)0, 0, 0, 0, 0 }, { OMEGA_DEC_RES, 8, 0, 0, 0 } }; for (unsigned i = 0; i < sizeof bad / sizeof bad[0]; i++) CHECK(omega_dec_prog_stats(&bad[i], NULL, NULL) != 0, "refusal %u", i); return g_failed ? 1 : 0; } @@ -154,6 +167,7 @@ static int sim_one(Kern *k, const uint32_t *args, unsigned n_args, uint32_t gx, CHECK(err == 0, "sim %s: %s (pc %d)", what, s.msg, s.err_pc); return err; } +static int sim_dep(void); static int sim_mode(void) { if (specs_build()) { printf("FAIL: codegen\n"); return 1; } /* AXPY chain: 3 launches x += a over 2048 */ @@ -213,6 +227,125 @@ static int sim_mode(void) { else if (tie) CHECK(oi2[0] != g.am_ref, "sim argmax mutant caught (max at a j == 7 slot)"); printf("sim argmax tie %d mutant %d: index %u value %g (ref %u)\n", tie, mut, oi2[0], ov2[0], g.am_ref); } + if (sim_dep()) return 1; + return g_failed ? 1 : 0; +} + +/* ---- DEPFLAGS (2026-10-16): the kernel side of the dependency flags on the simulator, through a host mirror of the + * command list's protocol (omega_gpu_dep.h): one counter line shared by every launch, bank words OMEGA_DEP_ARG_WAIT / + * _DONE = its address, OMEGA_DEP_ARG_TARGET = the CTAs that must have finished before this launch may read. The + * simulator's poll hook plays the late producer. Negative tests: NO_PROLOGUE (reads too early), NO_EPILOGUE (never + * reports), an unreachable target (times out, sentinel, no hang), and the chain itself. ---- */ +static Kern K_axpy_dep, K_axpy_dep_nopro, K_axpy_dep_noepi, K_axpy_cam; +typedef struct { uint32_t *line; uint64_t polls, fire_at; float *a; unsigned n; float late; uint32_t bump; int fired; } DepPlay; +static void dep_hook(struct WsSim *s, uint64_t addr, void *ctx) { + DepPlay *p = ctx; (void)s; + if (addr != (uint64_t)(uintptr_t)p->line) return; + p->polls++; + if (p->fire_at && p->polls == p->fire_at) { for (unsigned i = 0; i < p->n; i++) p->a[i] = p->late; p->line[0] += p->bump; p->fired = 1; } +} +#define DEP_ARGS 32u /* bank words 0x380..0x3fc: the kernel's arguments, then the three dependency words at 0x3e8.. */ +static void dep_words(uint32_t *args, const uint32_t *line, uint32_t target) { + put64(args, OMEGA_DEP_ARG_WAIT, (uintptr_t)line); put64(args, OMEGA_DEP_ARG_DONE, (uintptr_t)line); args[(OMEGA_DEP_ARG_TARGET - 0x380) / 4] = target; +} +static int sim_dep_run(Kern *k, const uint32_t *args, uint32_t gx, WsRange *ranges, int nr, DepPlay *play, const char *what, double *secs) { + static uint8_t smem[OMEGA_DEC_SHARED_BYTES]; + WsSim s; memset(&s, 0, sizeof s); + s.p = &k->prog; s.threads = OMEGA_DEC_THREADS; s.smem = smem; s.smem_bytes = sizeof smem; + memcpy(&s.cbank[0x380 / 4], args, DEP_ARGS * 4u); + s.nranges = nr; for (int i = 0; i < nr; i++) s.ranges[i] = ranges[i]; + if (play) { s.poll_hook = dep_hook; s.poll_ctx = play; } + struct timespec t0, t1; clock_gettime(CLOCK_MONOTONIC, &t0); + int err = ws_run(&s, gx, 1); + clock_gettime(CLOCK_MONOTONIC, &t1); + if (secs) *secs = (double)(t1.tv_sec - t0.tv_sec) + (double)(t1.tv_nsec - t0.tv_nsec) / 1e9; + CHECK(err == 0, "sim %s: %s (pc %d)", what, s.msg, s.err_pc); + return err; +} +static int sim_dep(void) { + OmegaDecSpec s; + memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_AXPY; s.stride = AXPY_STRIDE; + s.dep = OMEGA_DEP_FLAGS; if (kern_prog(&K_axpy_dep, &s)) { CHECK(0, "codegen axpy dep 1"); return 1; } + s.dep = OMEGA_DEP_FLAGS | OMEGA_DEP_TEST_NO_PROLOGUE; if (kern_prog(&K_axpy_dep_nopro, &s)) { CHECK(0, "codegen axpy dep 1 no-prologue"); return 1; } + s.dep = OMEGA_DEP_FLAGS | OMEGA_DEP_TEST_NO_EPILOGUE; if (kern_prog(&K_axpy_dep_noepi, &s)) { CHECK(0, "codegen axpy dep 1 no-epilogue"); return 1; } + s.dep = OMEGA_DEP_FLAGS | OMEGA_DEP_CAMERA; if (kern_prog(&K_axpy_cam, &s)) { CHECK(0, "codegen axpy dep 3"); return 1; } + const uint32_t CTAS = AXPY_STRIDE / OMEGA_DEC_THREADS; /* 8 */ + static float x[AXPY_STRIDE], a[AXPY_STRIDE], xr[AXPY_STRIDE]; + static uint32_t line[16] __attribute__((aligned(64))); + static uint64_t cam[2 * (OMEGA_DEP_CAM_SLOTS + 1)] __attribute__((aligned(16))); + WsRange r[4] = { { (uintptr_t)x, (uintptr_t)x + sizeof x }, { (uintptr_t)a, (uintptr_t)a + sizeof a }, { (uintptr_t)line, (uintptr_t)line + sizeof line }, { (uintptr_t)cam, (uintptr_t)cam + sizeof cam } }; + uint32_t args[DEP_ARGS]; +#define AXPY_ARGS() do { memset(args, 0, sizeof args); put64(args, OMEGA_DEC_AXPY_X, (uintptr_t)x); put64(args, OMEGA_DEC_AXPY_A, (uintptr_t)a); args[(OMEGA_DEC_AXPY_ITERS - 0x380) / 4] = 1; } while (0) + /* 1. three-launch chain, every target already satisfied when the launch runs (the host mirror: target_i = CTAS * i) */ + { + uint32_t seed = 11; for (unsigned i = 0; i < AXPY_STRIDE; i++) { x[i] = frand(&seed, -1, 1); a[i] = frand(&seed, -1, 1); xr[i] = x[i]; } + memset(line, 0, sizeof line); + for (uint32_t l = 0; l < 3; l++) { + AXPY_ARGS(); dep_words(args, line, CTAS * l); + DepPlay p; memset(&p, 0, sizeof p); p.line = line; + if (sim_dep_run(&K_axpy_dep, args, CTAS, r, 3, &p, "axpy dep chain", NULL)) return 1; + for (unsigned i = 0; i < AXPY_STRIDE; i++) xr[i] = xr[i] + a[i]; + CHECK(line[0] == CTAS * (l + 1), "dep chain launch %u: counter %u (want %u)", l, line[0], CTAS * (l + 1)); + CHECK(p.polls == (uint64_t)CTAS * 32u, "dep chain launch %u: satisfied target read once per CTA by warp 0 (%" PRIu64 " lane reads, want %u)", l, p.polls, CTAS * 32u); + } + CHECK(memcmp(x, xr, sizeof x) == 0, "dep chain: x bit-identical to the host chain"); + printf("dep chain: 3 launches x %u CTAs, counter %u, x %s\n", CTAS, line[0], memcmp(x, xr, sizeof x) == 0 ? "identical" : "DIFFERS"); + } + /* 2. late producer: the counter is below the target when the launch starts; after fire_at lane reads the "previous + * launch" rewrites a[] and finishes (counter += CTAS). A kernel that waits sees the late a[]; one that does not + * (OMEGA_DEP_TEST_NO_PROLOGUE, or a broken poll compare) computes with the old a[] and is caught by parity. */ + for (int variant = 0; variant < 2; variant++) { + uint32_t seed = 23; for (unsigned i = 0; i < AXPY_STRIDE; i++) { x[i] = frand(&seed, -1, 1); a[i] = 1.0f; xr[i] = x[i] + 2.0f; } + memset(line, 0, sizeof line); + AXPY_ARGS(); dep_words(args, line, CTAS); /* target CTAS: one earlier launch of CTAS CTAs has not finished yet */ + DepPlay p; memset(&p, 0, sizeof p); p.line = line; p.fire_at = 1024; p.a = a; p.n = AXPY_STRIDE; p.late = 2.0f; p.bump = CTAS; /* 1024 = whole warps: the bump lands between iterations */ + if (sim_dep_run(variant ? &K_axpy_dep_nopro : &K_axpy_dep, args, CTAS, r, 3, &p, variant ? "late producer no-prologue" : "late producer", NULL)) return 1; + const int parity = memcmp(x, xr, sizeof x) == 0; + if (!variant) { + CHECK(parity && p.fired && p.polls >= 1024, "late producer: the kernel waited (%" PRIu64 " lane reads, fired %d) and read the late a[]: x %s", p.polls, p.fired, parity ? "identical" : "DIFFERS"); + CHECK(line[0] == 2 * CTAS, "late producer: counter %u = producer %u + this launch %u", line[0], CTAS, CTAS); + } else { + CHECK(!parity && !p.fired && p.polls == 0, "late producer NO_PROLOGUE: no wait (%" PRIu64 " reads), stale a[] used: x %s (caught)", p.polls, parity ? "identical: NOT CAUGHT" : "differs"); + } + printf("late producer%s: %" PRIu64 " lane reads, fired %d, counter %u, x %s\n", variant ? " NO_PROLOGUE" : "", p.polls, p.fired, line[0], parity ? "identical" : "DIFFERS"); + } + /* 3. NO_EPILOGUE: the counter stays short by this launch's CTAs; the host's count check is what catches it */ + { + uint32_t seed = 31; for (unsigned i = 0; i < AXPY_STRIDE; i++) { x[i] = frand(&seed, -1, 1); a[i] = frand(&seed, -1, 1); xr[i] = x[i] + a[i]; } + memset(line, 0, sizeof line); + AXPY_ARGS(); dep_words(args, line, 0); + if (sim_dep_run(&K_axpy_dep_noepi, args, CTAS, r, 3, NULL, "no-epilogue", NULL)) return 1; + CHECK(memcmp(x, xr, sizeof x) == 0 && line[0] == 0 && line[0] != CTAS, "NO_EPILOGUE: x right but counter %u (host expects %u): caught by the count check", line[0], CTAS); + } + /* 4. timeout: an unreachable target; every CTA gives up after OMEGA_DEP_POLL_LIMIT reads, adds the sentinel, continues */ + { + uint32_t seed = 37; for (unsigned i = 0; i < AXPY_STRIDE; i++) { x[i] = frand(&seed, -1, 1); a[i] = frand(&seed, -1, 1); xr[i] = x[i] + a[i]; } + memset(line, 0, sizeof line); + AXPY_ARGS(); dep_words(args, line, 125u * CTAS); + DepPlay p; memset(&p, 0, sizeof p); p.line = line; + double secs = 0; + if (sim_dep_run(&K_axpy_dep, args, CTAS, r, 3, &p, "timeout", &secs)) return 1; + /* CTAs run one after another here: CTA 0 gives up and adds the sentinel, which satisfies the other 7 at their first read. + * (On the chip the CTAs run together, so between 1 and CTAS sentinels land; the host only needs counter != expected.) */ + const uint32_t want = OMEGA_DEP_TIMEOUT_SENTINEL + CTAS; + CHECK(line[0] == want, "timeout: counter %08x = sentinel + %u CTAs = %08x: the host sees the stage, nothing hangs", line[0], CTAS, want); + CHECK(p.polls == (uint64_t)OMEGA_DEP_POLL_LIMIT * 32u + (uint64_t)(CTAS - 1) * 32u, "timeout: POLL_LIMIT reads by the first CTA, one by each later CTA (%" PRIu64 " lane reads)", p.polls); + CHECK(memcmp(x, xr, sizeof x) == 0, "timeout: the kernel still ran to completion"); + printf("timeout: 2^18 polls by CTA 0 then %u pass-throughs in %.2f s of simulator time, counter %08x\n", CTAS - 1, secs, line[0]); + } + /* 5. camera (dep 3): line + 8 holds the camera base; every CTA's thread 0 stores {t0, t1} at the slot it allocates at line + 4 */ + { + uint32_t seed = 41; for (unsigned i = 0; i < AXPY_STRIDE; i++) { x[i] = frand(&seed, -1, 1); a[i] = frand(&seed, -1, 1); xr[i] = x[i] + a[i]; } + memset(line, 0, sizeof line); memset(cam, 0, sizeof cam); + uint64_t base = (uintptr_t)cam; memcpy(&line[2], &base, 8); + AXPY_ARGS(); dep_words(args, line, 0); + if (sim_dep_run(&K_axpy_cam, args, CTAS, r, 4, NULL, "camera", NULL)) return 1; + int ok = 1; for (uint32_t c = 0; c < CTAS; c++) if (!(cam[2 * c + 1] > cam[2 * c])) ok = 0; + CHECK(memcmp(x, xr, sizeof x) == 0 && line[0] == CTAS && line[1] == CTAS, "camera: x right, done %u, slots allocated %u (want %u)", line[0], line[1], CTAS); + CHECK(ok && cam[2 * CTAS] == 0 && cam[2 * CTAS + 1] == 0, "camera: %u records with t1 > t0 and none past the CTA count", CTAS); + printf("camera: %u records, first {%" PRIu64 ", %" PRIu64 "}, last {%" PRIu64 ", %" PRIu64 "} (simulator ticks)\n", CTAS, cam[0], cam[1], cam[2 * CTAS - 2], cam[2 * CTAS - 1]); + } +#undef AXPY_ARGS return g_failed ? 1 : 0; } @@ -447,18 +580,94 @@ static int chip_run(const char *out_path, uint32_t reps) { return g_failed ? 1 : 0; } +/* ---- DEPFLAGS chip study (--depflags, 2026-10-16): the heavy dependent AXPY chain (32 launches x 1M elements, 256 CTAs each) three ways. + * RED barrier NONE, dep 0: the kernels carry no ordering of their own, so overlapping grids must corrupt x (the negative control: the + * run where the instrument says "no"; without it a clean D run proves nothing). + * A barrier WFI + INVALIDATE ALL, dep 0: today's default. + * D barrier NONE, dep 1: the kernels order themselves with the list's counter (omega_gpu_cmdlist_set_dep). + * RED reps times first, then A and D interleaved reps times each, GPU PTIMER stamps on: span = stamps[N] - stamps[0] (the first + * stamp sits after the first launch's WFI, the last after the final WFI, so the span covers all N grids in both variants); parity on + * every element after every run; in D the counter read back must equal the CTA total. One line per run; JSON with --out. */ +static uint64_t med64(uint64_t *v, uint32_t n) { for (uint32_t i = 1; i < n; i++) for (uint32_t j = i; j > 0 && v[j - 1] > v[j]; j--) { uint64_t t = v[j]; v[j] = v[j - 1]; v[j - 1] = t; } return n ? v[n / 2] : 0; } +static int chip_depflags(const char *out_path, uint32_t reps) { + FILE *out = out_path ? fopen(out_path, "w") : NULL; + if (out_path && !out) { printf("cannot write %s\n", out_path); return 1; } + if (specs_build()) { printf("FAIL: codegen\n"); return 1; } + static Kern K_dep; + { OmegaDecSpec s; memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_AXPY; s.stride = AXPY_BIG_STRIDE; s.dep = OMEGA_DEP_FLAGS; if (kern_prog(&K_dep, &s)) { printf("FAIL: codegen dep\n"); return 1; } } + OmegaGpuCmdList *cl = NULL; + if (omega_gpu_cmdlist_create(OMEGA_GPU_CMDLIST_MAX_LAUNCHES, &cl) != 0) return chip_fail("cmdlist create"); + omega_gpu_session_lock(); + if (kern_upload(&K_axpy_big) || kern_upload(&K_dep)) { omega_gpu_session_unlock(); return chip_fail("kernel upload"); } + const uint32_t N = 32; const size_t n = (size_t)AXPY_BIG_STRIDE * AXPY_BIG_ITERS, big = n * 4u; + OmegaGpuMem bx, ba, bx0; + if (omega_gpu_session_alloc(big, &bx) || omega_gpu_session_alloc(big, &ba) || omega_gpu_session_alloc(big, &bx0)) { omega_gpu_session_unlock(); return chip_fail("alloc"); } + uint32_t s = 11; float *x0 = bx0.cpu, *a = ba.cpu, *x = bx.cpu; + for (size_t i = 0; i < n; i++) { x0[i] = frand(&s, -1, 1); a[i] = frand(&s, -1, 1); } + omega_gpu_session_sync_to_gpu(&ba); + float *xr = malloc(big); memcpy(xr, x0, big); + for (uint32_t l = 0; l < N; l++) for (size_t i = 0; i < n; i++) xr[i] = xr[i] + a[i]; + uint32_t ab[OMEGA_DEC_AXPY_WORDS] = { 0 }; put64(ab, OMEGA_DEC_AXPY_X, bx.va); put64(ab, OMEGA_DEC_AXPY_A, ba.va); ab[(OMEGA_DEC_AXPY_ITERS - 0x380) / 4] = AXPY_BIG_ITERS; + OmegaGpuLaunch L0 = axpy_launch(&K_axpy_big, ab, AXPY_BIG_STRIDE, AXPY_BIG_ITERS), L1 = axpy_launch(&K_dep, ab, AXPY_BIG_STRIDE, AXPY_BIG_ITERS); + static uint64_t stamps[OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1]; + const uint32_t ctas = AXPY_BIG_STRIDE / OMEGA_DEC_THREADS; + printf("depflags chip study: %u launches x %zu elements, %u CTAs per launch, dep kernel %u gprs (dep 0: %u), reps %u\n", N, n, ctas, K_dep.gpr, K_axpy_big.gpr, reps); + if (out) fprintf(out, "{\"receipt\":\"OMEGA_DEPFLAGS_CHIP_V1\",\"launches\":%u,\"elements\":%zu,\"ctas_per_launch\":%u,\"reps\":%u,\"runs\":[", N, n, ctas, reps); + uint64_t span_a[256], span_d[256]; uint32_t na = 0, nd = 0, red_corrupt = 0, red_runs = 0, par_a = 0, par_d = 0; int first = 1; + if (reps > 256) reps = 256; + for (uint32_t rep = 0; rep < 3 * reps && !omega_gpu_cmdlist_is_latched(); rep++) { + const int variant = rep < reps ? 0 : ((rep - reps) & 1 ? 2 : 1); /* RED x reps, then A, D, A, D, ... */ + memcpy(x, x0, big); omega_gpu_session_sync_to_gpu(&bx); + omega_gpu_cmdlist_reset(cl); + if (omega_gpu_cmdlist_set_dep(cl, variant == 2 ? OMEGA_DEP_FLAGS : 0) != 0) { omega_gpu_session_unlock(); return chip_fail("set_dep"); } + for (uint32_t l = 0; l < N; l++) if (omega_gpu_cmdlist_add(cl, variant == 2 ? &L1 : &L0) < 0) { omega_gpu_session_unlock(); return chip_fail("add"); } + OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); o.mode = OMEGA_GPU_CMDLIST_PCAS; o.timeout_ms = 20000; o.stamps = 1; + o.barrier = variant == 1 ? OMEGA_GPU_CMDLIST_BARRIER_WFI : OMEGA_GPU_CMDLIST_BARRIER_NONE; + OmegaGpuCmdListStats st; int rc = omega_gpu_cmdlist_submit(cl, &o, &st); + omega_gpu_session_sync_to_cpu(&bx); + size_t d = 0; for (size_t i = 0; i < n; i++) if (x[i] != xr[i]) d++; + int cnt = rc == 0 ? omega_gpu_cmdlist_stamps(cl, stamps, N + 1) : -1; + int mono = 1; for (int i = 1; i < cnt; i++) if (stamps[i] < stamps[i - 1]) mono = 0; + const uint64_t span = cnt == (int)N + 1 ? stamps[N] - stamps[0] : 0; + const char *name = variant == 0 ? "RED none dep0" : variant == 1 ? "A wfi dep0" : "D none dep1"; + printf("run %2u %-13s rc %d differ %zu/%zu chip %.1f us stamps %d mono %d span %.1f us per-launch %.2f us counter %u/%u%s%s\n", rep, name, rc, d, n, st.elapsed_ns / 1e3, cnt, mono, + span / 1e3, span / 1e3 / N, st.dep_observed, st.dep_expected, rc ? " ERR: " : "", rc ? omega_gpu_session_last_error() : ""); + if (out) fprintf(out, "%s{\"run\":%u,\"variant\":\"%s\",\"rc\":%d,\"differ\":%zu,\"chip_ns\":%" PRIu64 ",\"stamps\":%d,\"span_ns\":%" PRIu64 ",\"counter\":%u,\"expected\":%u}", + first ? "" : ",", rep, name, rc, d, st.elapsed_ns, cnt, span, st.dep_observed, st.dep_expected); + first = 0; + if (variant == 0) { red_runs++; if (d) red_corrupt++; } + else if (variant == 1) { if (rc == 0 && d == 0 && cnt == (int)N + 1 && mono) par_a++; if (span) span_a[na++] = span; } + else { if (rc == 0 && d == 0 && cnt == (int)N + 1 && mono && st.dep_observed == st.dep_expected && st.dep_expected == N * ctas) par_d++; if (span) span_d[nd++] = span; } + } + CHECK(red_corrupt > 0, "RED: barrier NONE with dep 0 corrupted x in %u of %u runs (the instrument can say no)", red_corrupt, red_runs); + CHECK(par_a == reps, "A wfi dep0: parity, %u stamps, monotonic in %u of %u runs", N + 1, par_a, reps); + CHECK(par_d == reps, "D none dep1: parity, stamps, counter == %u CTAs in %u of %u runs", N * ctas, par_d, reps); + const uint64_t ma = med64(span_a, na), md = med64(span_d, nd); + printf("A wfi dep0 span median %.1f us (min %.1f max %.1f, %u runs), per launch %.2f us\n", ma / 1e3, na ? span_a[0] / 1e3 : 0.0, na ? span_a[na - 1] / 1e3 : 0.0, na, ma / 1e3 / N); + printf("D none dep1 span median %.1f us (min %.1f max %.1f, %u runs), per launch %.2f us; D/A %.3f\n", md / 1e3, nd ? span_d[0] / 1e3 : 0.0, nd ? span_d[nd - 1] / 1e3 : 0.0, nd, md / 1e3 / N, ma ? (double)md / (double)ma : 0.0); + if (out) fprintf(out, "],\"red\":{\"runs\":%u,\"corrupt\":%u},\"A\":{\"parity\":%u,\"span_med_ns\":%" PRIu64 ",\"span_min_ns\":%" PRIu64 ",\"span_max_ns\":%" PRIu64 "},\"D\":{\"parity\":%u,\"span_med_ns\":%" PRIu64 ",\"span_min_ns\":%" PRIu64 ",\"span_max_ns\":%" PRIu64 "},\"latched\":%d}\n", + red_runs, red_corrupt, par_a, ma, na ? span_a[0] : 0, na ? span_a[na - 1] : 0, par_d, md, nd ? span_d[0] : 0, nd ? span_d[nd - 1] : 0, omega_gpu_cmdlist_is_latched()); + if (out) fclose(out); + free(xr); + omega_gpu_session_free(&bx); omega_gpu_session_free(&ba); omega_gpu_session_free(&bx0); + omega_gpu_session_unlock(); + omega_gpu_cmdlist_free(cl); + return g_failed ? 1 : 0; +} + int main(int argc, char **argv) { - int host = 0, sim = 0; uint32_t reps = 20; const char *out = NULL; + int host = 0, sim = 0, depflags = 0; uint32_t reps = 20; const char *out = NULL; 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], "--depflags") == 0) depflags = 1; /* DEPFLAGS chip study (RED, A, D; --reps N each, --out) */ else if (strcmp(argv[i], "--reps") == 0 && i + 1 < argc) reps = (uint32_t)atoi(argv[++i]); else if (strcmp(argv[i], "--spin-us") == 0 && i + 1 < argc) omega_gpu_session_set_spin_us((uint32_t)atoi(argv[++i])); else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out = argv[++i]; else { printf("usage: %s [--host-only|--sim] [--reps N] [--spin-us U] [--out receipt.json]\n", argv[0]); return 2; } } if (reps == 0) reps = 1; - int rc = host ? host_only() : sim ? sim_mode() : chip_run(out, reps); - printf("%s: %d checks, %d failed: %s\n", host ? "host-only" : sim ? "sim" : "chip", g_checks, g_failed, rc ? "FAIL" : "PASS"); + int rc = host ? host_only() : sim ? sim_mode() : depflags ? chip_depflags(out, reps) : chip_run(out, reps); + printf("%s: %d checks, %d failed: %s\n", host ? "host-only" : sim ? "sim" : depflags ? "chip-depflags" : "chip", g_checks, g_failed, rc ? "FAIL" : "PASS"); return rc; } diff --git a/tests/gpu_decode_attn_test.c b/tests/gpu_decode_attn_test.c index 6a30d1835..2a0256d1a 100644 --- a/tests/gpu_decode_attn_test.c +++ b/tests/gpu_decode_attn_test.c @@ -192,12 +192,18 @@ static void host_only(void) { free(ref); case_free(&c); } /* codegen budget */ for (uint32_t d = 1; d <= 4; d++) for (int m = 0; m <= 4; m++) { - OmegaDecodeAttnSpec s = { d, m }; uint32_t n = 0, gp = 0; + OmegaDecodeAttnSpec s = { d, m, 0 }; uint32_t n = 0, gp = 0; int rc = omega_decode_attn_prog_stats(&s, &n, &gp); CHECK(rc == 0 && n <= 1024 && gp <= 128, "codegen depth %u mutant %d: rc %d insns %u gprs %u", d, m, rc, n, gp); if (rc == 0 && m == 0) printf("codegen depth %u: %u instructions, %u registers (declared %u, 256 threads -> %u of 65536)\n", d, n, gp, ((gp + 8) / 8) * 8, 256 * ((gp + 8) / 8) * 8); } - { OmegaDecodeAttnSpec s = { 0, 0 }; CHECK(omega_decode_attn_prog_stats(&s, NULL, NULL) != 0, "depth 0 refused"); + for (uint32_t dep = 1; dep <= 3; dep += 2) { /* DEPFLAGS: the production depth with the counter prologue/epilogue (and the camera) stays within budget */ + OmegaDecodeAttnSpec s = { OMEGA_GPU_DATTN_DEPTH, 0, dep }; uint32_t n = 0, gp = 0; + int rc = omega_decode_attn_prog_stats(&s, &n, &gp); + CHECK(rc == 0 && n <= BW_MAX_IR_INSNS && gp <= 128, "codegen depth %u dep %u: rc %d insns %u gprs %u (IR cap %u)", OMEGA_GPU_DATTN_DEPTH, dep, rc, n, gp, BW_MAX_IR_INSNS); + } + CHECK(OMEGA_DATTN_ARGS_WORDS <= OMEGA_DEP_MAX_KERNEL_ARGS, "argument words end before the dep words (0x3e8)"); + { OmegaDecodeAttnSpec s = { 0, 0, 0 }; CHECK(omega_decode_attn_prog_stats(&s, NULL, NULL) != 0, "depth 0 refused"); s.depth = 5; CHECK(omega_decode_attn_prog_stats(&s, NULL, NULL) != 0, "depth 5 refused"); s.depth = 1; s.mutant = 5; CHECK(omega_decode_attn_prog_stats(&s, NULL, NULL) != 0, "mutant 5 refused"); } CHECK(omega_gpu_decode_attn_cache_alloc(0, 256, THETA, NULL) == OMEGA_GPU_DATTN_BAD_ARGS, "NULL out refused"); @@ -208,9 +214,10 @@ static void host_only(void) { /* ------------------------------------------------------------- simulator */ static uint64_t g_sim_issues; +static uint32_t g_sim_dep; /* DEPFLAGS: spec.dep for the next sim point (0, or OMEGA_DEP_FLAGS: counter armed by ws_dep_arm) */ static int sim_point(uint32_t max_pos, uint32_t pos, uint32_t depth, int mutant, uint32_t per_split, float qscale, uint32_t seed, char *msg, size_t n, double *worst, size_t *bad, char *rows, size_t rn) { static BlackwellIRProgram prog; - OmegaDecodeAttnSpec spec = { depth, mutant }; + OmegaDecodeAttnSpec spec = { depth, mutant, g_sim_dep }; if (omega_blackwell_codegen_decode_attn_prog(&spec, &prog) != 0) { snprintf(msg, n, "codegen refused"); return -1; } Case c; case_make(&c, max_pos, pos, seed, qscale); const uint32_t splits = (pos + per_split) / per_split; @@ -243,8 +250,10 @@ static int sim_point(uint32_t max_pos, uint32_t pos, uint32_t depth, int mutant, s.ranges[4].lo = (uintptr_t)out; s.ranges[4].hi = (uintptr_t)out + 4096 * 4; s.ranges[5].lo = (uintptr_t)part; s.ranges[5].hi = (uintptr_t)part + (size_t)4 * splits * OMEGA_DATTN_PARTIAL_WORDS * 4; s.ranges[6].lo = (uintptr_t)cnt; s.ranges[6].hi = (uintptr_t)cnt + 16; + static uint32_t depline[16]; if (g_sim_dep) ws_dep_arm(&s, depline); err = ws_run(&s, splits, 4); if (err) { snprintf(msg, n, "order %d: %s (pc %d)", order, s.msg, s.err_pc); break; } + if (g_sim_dep && depline[0] != splits * 4u) { err = -1; snprintf(msg, n, "dep counter %u after %u CTAs", depline[0], splits * 4u); break; } g_sim_issues += s.warp_issues; size_t unwritten = 0; for (unsigned i = 0; i < 4096; i++) if (out[i] == -999.0f) unwritten++; if (unwritten) { err = -1; snprintf(msg, n, "%zu output words never written", unwritten); break; } @@ -282,6 +291,13 @@ static void sim_battery(void) { CHECK(err == 0 && bad == 0, "sim %s depth %u: %s", pts[i].name, pts[i].depth, msg); printf("sim %-50s depth %u: %s %s\n", pts[i].name, pts[i].depth, err ? "ERROR" : bad ? "MISMATCH" : "ok", msg); } + { /* DEPFLAGS: the dep-mode kernel (2 splits x 4 groups = 8 CTAs) computes the same output and every CTA counts itself in */ + g_sim_dep = OMEGA_DEP_FLAGS; + int err = sim_point(512, 300, 2, 0, 256, 1.0f, 100, msg, sizeof msg, &worst, &bad, rows, sizeof rows); + g_sim_dep = 0; + CHECK(err == 0 && bad == 0, "sim context 301 depth 2 with dep flags: %s", msg); + printf("sim context 301 (2 splits) depth 2 dep 1: %s %s\n", err ? "ERROR" : bad ? "MISMATCH" : "ok", msg); + } for (int m = 1; m <= 4; m++) { int err = sim_point(512, 300, 2, m, 256, 1.0f, 200, msg, sizeof msg, &worst, &bad, rows, sizeof rows); int caught = err != 0 || bad > 0; diff --git a/tests/gpu_fp8_api_test.c b/tests/gpu_fp8_api_test.c index bec0db1b9..3b643bf6c 100644 --- a/tests/gpu_fp8_api_test.c +++ b/tests/gpu_fp8_api_test.c @@ -170,6 +170,15 @@ static void host_only(void) { } printf("codegen: %d kernels built, largest %u IR instructions, peak %u GPRs\n", built, worst_insns, worst_gprs); CHECK(worst_insns <= BW_MAX_IR_INSNS && worst_gprs <= 128, "budgets"); + for (uint32_t dep = 1; dep <= 3; dep += 2) { /* DEPFLAGS: the decode shapes with the counter prologue/epilogue (and the camera) stay within budget */ + OmegaFp8GemvSpec sp = { .k = 2048, .lane_bytes = 16, .rows = 1, .warps = 8, .dep = dep }; uint32_t ni = 0, ng = 0; + int rc = omega_fp8_gemv_prog_stats(&sp, &ni, &ng); + CHECK(rc == 0 && ni <= BW_MAX_IR_INSNS && ng <= 128, "codegen k=2048 lb=16 rows=1 warps=8 dep=%u: rc %d insns %u gprs %u", dep, rc, ni, ng); + OmegaFp8GemvSpec gs = { .k = 768, .lane_bytes = 8, .rows = 1, .warps = 32, .dep = dep }; /* the down width: k % 512 != 0, so 8 B lanes */ + rc = omega_fp8_gemv_prog_stats(&gs, &ni, &ng); + CHECK(rc == 0 && ni <= BW_MAX_IR_INSNS && 32u * 32u * (((ng + 1 + 7) / 8) * 8) <= 65536u, "codegen grouped k=768 lb=8 rows=1 warps=32 dep=%u: rc %d insns %u gprs %u", dep, rc, ni, ng); + } + CHECK(OMEGA_FP8_ARGS_WORDS <= OMEGA_DEP_MAX_KERNEL_ARGS, "argument words end before the dep words (0x3e8)"); { OmegaFp8GemvSpec bad = { .k = 192, .lane_bytes = 8, .rows = 1, .warps = 1 }; CHECK(omega_fp8_gemv_prog_stats(&bad, NULL, NULL) != 0, "k not a multiple of 128 refused"); bad.k = 256; bad.lane_bytes = 4; CHECK(omega_fp8_gemv_prog_stats(&bad, NULL, NULL) != 0, "lane_bytes 4 refused (LDG.E has no immediate offset)"); bad.lane_bytes = 8; bad.k = 2048; bad.rows = 3; CHECK(omega_fp8_gemv_prog_stats(&bad, NULL, NULL) != 0, "rows 3 refused"); @@ -279,8 +288,10 @@ static int sim_gemv(const OmegaFp8GemvSpec *spec, const Weights *W, uint32_t gro /* the table is read too */ s.nranges = 5; s.ranges[4].lo = (uintptr_t)tab; s.ranges[4].hi = (uintptr_t)tab + 16 * (size_t)groups; for (size_t i = 0; i < (size_t)groups * n; i++) yd[i] = -999.0f; + static uint32_t depline[16]; if (spec->dep & OMEGA_DEP_FLAGS) ws_dep_arm(&s, depline); err = ws_run(&s, grid_x, groups); if (err) { snprintf(msg, msglen, "order %d: %s", order, s.msg); break; } + if ((spec->dep & OMEGA_DEP_FLAGS) && depline[0] != grid_x * groups) { err = -1; snprintf(msg, msglen, "dep counter %u after %u CTAs", depline[0], grid_x * groups); break; } g_sim_issues += s.warp_issues; size_t unwritten = 0; for (size_t i = 0; i < (size_t)groups * n; i++) if (yd[i] == -999.0f) unwritten++; if (unwritten) { err = -1; snprintf(msg, msglen, "%zu output words never written", unwritten); break; } @@ -357,6 +368,13 @@ static void sim_battery(uint32_t cta_budget) { if (worst > worst_all) worst_all = worst; CHECK(rc == 0 && bad == 0, "sim %s: rc=%d bad=%zu worst_rel=%.3e %s", nm, rc, bad, worst, msg); printf("sim %-45s rc=%d worst_rel=%.3e bad=%zu (%.0f ms)\n", nm, rc, worst, bad, now_ms() - t0); + if (!deep && (i == 1 || i == 7)) { /* DEPFLAGS: one single and one grouped point with the counter protocol (variant 1; variant 2 below) */ + OmegaFp8GemvSpec ds = sp; ds.dep = OMEGA_DEP_FLAGS; + int rcd = sim_gemv(&ds, &W, p->groups, ids, x, p->x_rows, y, cta_budget, 0, msg, sizeof msg); + size_t badd = 0; double worstd = rcd == 0 ? parity(&W, p->groups, ids, x, p->x_rows, y, &badd) : 1.0; + CHECK(rcd == 0 && badd == 0, "sim %s dep 1: rc=%d bad=%zu worst_rel=%.3e %s", p->name, rcd, badd, worstd, msg); + printf("sim %-38s dep 1 rc=%d worst_rel=%.3e bad=%zu\n", p->name, rcd, worstd, badd); + } /* mutants on the two grouped points and the first two single points: each must be caught */ if (i == 1 || i == 4 || i == 7 || i == 8) { for (int m = 1; m <= 4; m++) { @@ -414,6 +432,13 @@ static void sim_battery(uint32_t cta_budget) { CHECK(rc2 == 0 && bad == 0 && same, "sim %s: rc=%d bad=%zu worst_rel=%.3e identical_to_v1=%d %s", p->name, rc2, bad, worst, same, msg); if (same) identical++; printf("sim %-48s rc=%d worst_rel=%.3e bad=%zu identical_to_v1=%d (%.0f ms)\n", p->name, rc2, worst, bad, same, now_ms() - t0); + if (i == 0 || i == 6) { /* DEPFLAGS: variant 2 with the counter protocol stays bit-identical to variant 1 and every CTA counts itself in */ + OmegaFp8GemvSpec ds = sp; ds.dep = OMEGA_DEP_FLAGS; + int rcd = sim_gemv(&ds, &W, p->groups, ids, x, p->x_rows, y2, cta_budget, 0, msg, sizeof msg); + int samed = rc1 == 0 && rcd == 0 && memcmp(y1, y2, ny * 4) == 0; + CHECK(rcd == 0 && samed, "sim %s dep 1: rc=%d identical_to_v1=%d %s", p->name, rcd, samed, msg); + printf("sim %-48s dep 1 rc=%d identical_to_v1=%d\n", p->name, rcd, samed); + } if (i == 0 || i == 6 || i == 7) { for (int m = 1; m <= 3; m++) { if (m == 3 && p->groups == 1) continue; diff --git a/tests/gpu_router_test.c b/tests/gpu_router_test.c index 1c274e79a..c6d1d8530 100644 --- a/tests/gpu_router_test.c +++ b/tests/gpu_router_test.c @@ -107,26 +107,34 @@ static void host_only(void) { /* codegen budgets: every production shape fits the register file (32 * warps * declared <= 65536) and the IR limit */ uint32_t ws[] = { 8, 16, 32 }, ds[] = { 1, 2, 4, 8 }; for (unsigned i = 0; i < 3; i++) for (unsigned j = 0; j < 4; j++) for (int m = 0; m <= 4; m++) { - OmegaRouterSpec s = { ws[i], ds[j], m }; uint32_t n = 0, g = 0; + OmegaRouterSpec s = { ws[i], ds[j], m, 0 }; uint32_t n = 0, g = 0; int rc = omega_router_prog_stats(&s, &n, &g); uint32_t decl = ((g + 1 + 7) / 8) * 8; CHECK(rc == 0 && n <= 1024 && 32u * ws[i] * decl <= 65536u, "codegen warps %u depth %u mutant %d: rc %d insns %u gprs %u declared %u", ws[i], ds[j], m, rc, n, g, decl); if (m == 0 && rc == 0) printf("codegen warps %2u depth %u: %u instructions, %u registers (declared %u, register file %u of 65536)\n", ws[i], ds[j], n, g, decl, 32u * ws[i] * decl); } - { OmegaRouterSpec s = { 4, 8, 0 }; CHECK(omega_router_prog_stats(&s, NULL, NULL) != 0, "warps 4 refused"); } - { OmegaRouterSpec s = { 8, 3, 0 }; CHECK(omega_router_prog_stats(&s, NULL, NULL) != 0, "depth 3 refused"); } - { OmegaRouterSpec s = { 8, 8, 5 }; CHECK(omega_router_prog_stats(&s, NULL, NULL) != 0, "mutant 5 refused"); } + for (uint32_t dep = 1; dep <= 3; dep += 2) { /* DEPFLAGS: the production shape with the counter prologue/epilogue (and the camera) stays within budget */ + OmegaRouterSpec s = { OMEGA_GPU_ROUTER_WARPS, OMEGA_GPU_ROUTER_DEPTH, 0, dep }; uint32_t n = 0, g = 0; + int rc = omega_router_prog_stats(&s, &n, &g); + uint32_t decl = ((g + 1 + 7) / 8) * 8; + CHECK(rc == 0 && n <= 1024 && 32u * OMEGA_GPU_ROUTER_WARPS * decl <= 65536u, "codegen production shape dep %u: rc %d insns %u gprs %u declared %u", dep, rc, n, g, decl); + } + CHECK(OMEGA_ROUTER_ARGS_WORDS <= OMEGA_DEP_MAX_KERNEL_ARGS, "argument words end before the dep words (0x3e8)"); + { OmegaRouterSpec s = { 4, 8, 0, 0 }; CHECK(omega_router_prog_stats(&s, NULL, NULL) != 0, "warps 4 refused"); } + { OmegaRouterSpec s = { 8, 3, 0, 0 }; CHECK(omega_router_prog_stats(&s, NULL, NULL) != 0, "depth 3 refused"); } + { OmegaRouterSpec s = { 8, 8, 5, 0 }; CHECK(omega_router_prog_stats(&s, NULL, NULL) != 0, "mutant 5 refused"); } CHECK(OMEGA_ROUTER_ARGS_WORDS <= OMEGA_GPU_SESSION_MAX_ARGS, "argument words fit the constant bank window"); } /* ------------------------------------------------------------- simulator */ static uint64_t g_sim_issues; +static uint32_t g_sim_dep; /* DEPFLAGS: spec.dep for the next sim point (0, or OMEGA_DEP_FLAGS: counter armed by ws_dep_arm) */ /* one "device" block: x | xout | ids | wts | tab_gu | tab_dn | dir_gu | dir_dn | norm; the gate row block apart */ enum { B_X = 0, B_XOUT = 8192, B_IDS = 16384, B_WTS = 16448, B_TGU = 16512, B_TDN = 16640, B_DGU = 16768, B_DDN = 16768 + 2048, B_NORM = 16768 + 4096, B_BYTES = 16768 + 4096 + 8192 }; static void fake_dirs(uint64_t *dgu, uint64_t *ddn) { for (unsigned e = 0; e < NE; e++) { dgu[2 * e] = 0x100000000ull + e * 16; dgu[2 * e + 1] = 0x200000000ull + e * 16 + 8; ddn[2 * e] = 0x300000000ull + e * 32; ddn[2 * e + 1] = 0x400000000ull + e * 32 + 16; } } static int sim_point(uint32_t depth, int mutant, int norm_topk, float out_scale, uint32_t seed, char *msg, size_t n, int *bad, int *margin_case) { static BlackwellIRProgram prog; - OmegaRouterSpec spec = { 8, depth, mutant }; + OmegaRouterSpec spec = { 8, depth, mutant, g_sim_dep }; if (omega_blackwell_codegen_router_prog(&spec, &prog) != 0) { snprintf(msg, n, "codegen refused"); return -1; } Case c; case_make(&c, seed, 1.0f, norm_topk); uint8_t *blk = aligned_alloc(16, B_BYTES); memset(blk, 0, B_BYTES); @@ -152,8 +160,10 @@ static int sim_point(uint32_t depth, int mutant, int norm_topk, float out_scale, s.nranges = 2; s.ranges[0].lo = (uintptr_t)blk; s.ranges[0].hi = (uintptr_t)blk + B_BYTES; s.ranges[1].lo = (uintptr_t)gate; s.ranges[1].hi = (uintptr_t)gate + (size_t)NE * H * 2; + static uint32_t depline[16]; if (g_sim_dep) ws_dep_arm(&s, depline); err = ws_run(&s, 1, 1); if (err) { snprintf(msg, n, "order %d: %s (pc %d)", order, s.msg, s.err_pc); break; } + if (g_sim_dep && depline[0] != 1u) { err = -1; snprintf(msg, n, "dep counter %u after 1 CTA (want 1)", depline[0]); break; } g_sim_issues += s.warp_issues; size_t unwritten = 0; for (unsigned i = 0; i < H; i++) if (xo[i] == -999.0f) unwritten++; for (unsigned i = 0; i < TK; i++) if (wd[i] == -999.0f || idd[i] == 0xffffffffu) unwritten++; @@ -177,6 +187,14 @@ static void sim_battery(void) { CHECK(!err && bad == 0, "sim %s: %s", pts[i].name, msg); printf("sim %s: %s %s\n", pts[i].name, err ? "ERROR" : bad ? "MISMATCH" : "ok", msg); } + { /* DEPFLAGS: the dep-mode kernel (poll, BAR, CCTL.IVALL in front; fence, BAR, counter add behind) computes the same answer and counts itself in */ + char msg[300]; int bad = 0, mc = 0; + g_sim_dep = OMEGA_DEP_FLAGS; + int err = sim_point(8, 0, 1, 1.0f, 11, msg, sizeof msg, &bad, &mc); + g_sim_dep = 0; + CHECK(!err && bad == 0, "sim depth 8 norm with dep flags: %s", msg); + printf("sim depth 8 norm dep 1: %s %s\n", err ? "ERROR" : bad ? "MISMATCH" : "ok", msg); + } for (int m = 1; m <= 4; m++) { char msg[300]; int bad = 0, mc = 0; int err = sim_point(8, m, 1, 1.0f, 21, msg, sizeof msg, &bad, &mc); diff --git a/tests/r16_inventory/.crumb b/tests/r16_inventory/.crumb index e69f87353..5312658da 100644 --- a/tests/r16_inventory/.crumb +++ b/tests/r16_inventory/.crumb @@ -20,13 +20,13 @@ } ], "children_root": "sha256:36bda09ce85e797adb7f7712504802354c0adb1e1fc65e0d2ebf6a252f6ee1f0", - "compiled_at": "2026-10-14T21:46:29Z", + "compiled_at": "2026-10-16T20:31:51Z", "compiler": "crumb-compile/1", "digest": "sha256:ba0c09f67d12b5d7de906129bdbe2f6e0a228e1a34236ecc4b779c523cb2061e", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 9, - "generated_at_commit": "2a6d6d7b9091bd2184b15dcde893035dbcca40d3", + "generated_at_commit": "05d0d8e9ab225dba82698bf89128072f3a6f3a7a", "source_tree": "sha256:c5359714281d3aee892eff6e268df24c436e97d07a5ac36ca452894067359808" }, "owner_lane": "", diff --git a/tests/r16_inventory/fixture/.crumb b/tests/r16_inventory/fixture/.crumb index 74db6babe..7dca9e9c7 100644 --- a/tests/r16_inventory/fixture/.crumb +++ b/tests/r16_inventory/fixture/.crumb @@ -36,13 +36,13 @@ } ], "children_root": "sha256:a7d5f146d5c99c75701c8f384732dbdf7158a8fcd7295d0c0761c1e84d2c4e9d", - "compiled_at": "2026-10-14T21:46:29Z", + "compiled_at": "2026-10-16T20:31:51Z", "compiler": "crumb-compile/1", "digest": "sha256:3b8ee545f13c1a168892b617671eebf2ee65413994332269f5d2f2dcf492aba7", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 8, - "generated_at_commit": "2a6d6d7b9091bd2184b15dcde893035dbcca40d3", + "generated_at_commit": "05d0d8e9ab225dba82698bf89128072f3a6f3a7a", "source_tree": "sha256:be8537e95e5ed29a32f2e6267c290f8b999a1537c3dd5c7e4f691791eabb74b6" }, "owner_lane": "",