diff --git a/.crumb b/.crumb index 6a863531d..ce1ff603a 100644 --- a/.crumb +++ b/.crumb @@ -87,14 +87,14 @@ "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "c33506c2d8631760724d58bb289fd919708ba36544f9f2e9cb965b98135655ce", - "last_commit": "09eed552", + "digest": "a0886818e3da6cdc8deeb7830808467b75798b7f3a538204557da7998b52e564", + "last_commit": "45eef1d1", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "c8932aae0268325ccc74e09284de055134b022abdfaa080227f5be40c0a638e7", - "last_commit": "09eed552", + "digest": "57ed2a1bfe7a7a524224ed3b38f88a32b0bda81322a3f1267fdf9da2db161d45", + "last_commit": "89913b0b", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, @@ -105,15 +105,15 @@ "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:e08d6114f714cb983afbd6163802e3755302dc35def33c5a4a9af4959aa30c94", - "compiled_at": "2026-10-12T08:17:13Z", + "children_root": "sha256:5f18309aa63bf3ef664c738098ca2d7b13b088fd79ee1ee2aebc66552213fb4a", + "compiled_at": "2026-10-12T09:00:14Z", "compiler": "crumb-compile/1", - "digest": "sha256:4b4c03d378eee5119797d16061cf58e9a5d62ea7446b1861077ae242c659b831", + "digest": "sha256:fb21f51a731200840f038c9f2da76a3f2cf8001f7d8d6c7d851968eacc05f2e6", "evidence_files": 7245, "evidence_root": "sha256:035beef5cdb99afaefd1743640f5e7eeaaed13c4a5ec6c8b4b6a4cb0c528407a", "files": 9734, - "generated_at_commit": "09eed5528137abc1103768858538db18815654ab", - "source_tree": "sha256:6f903c535c14d9ce6bfad05e181e87819a35923c891380ddc7ffe8c98692d9a4" + "generated_at_commit": "45eef1d18b4cd6cf5daf3f02a329a6e4d5b6fd23", + "source_tree": "sha256:a76f7787d26bcef60783f61ba0d87844a6d9872dcd2d2bb8bfa0010e746a510e" }, "owner_lane": "", "provenance": { diff --git a/src/.crumb b/src/.crumb index 98860b109..428e0d12a 100644 --- a/src/.crumb +++ b/src/.crumb @@ -176,14 +176,14 @@ } ], "children_root": "sha256:2f64d32b2351272f4de10e1143efdb74dbc3cae56b1f0a3039ca9edcfebf6e8c", - "compiled_at": "2026-10-12T08:17:13Z", + "compiled_at": "2026-10-12T09:00:14Z", "compiler": "crumb-compile/1", - "digest": "sha256:c33506c2d8631760724d58bb289fd919708ba36544f9f2e9cb965b98135655ce", + "digest": "sha256:a0886818e3da6cdc8deeb7830808467b75798b7f3a538204557da7998b52e564", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 451, - "generated_at_commit": "09eed5528137abc1103768858538db18815654ab", - "source_tree": "sha256:7147cd5fbc544c2eec82bad9702336692840f284b328b67c08025a8c528b0326" + "generated_at_commit": "45eef1d18b4cd6cf5daf3f02a329a6e4d5b6fd23", + "source_tree": "sha256:ce50af03cd4d5f414ee4deca3c874adf424d31a7c206620a1ed7f7fe4a0e7857" }, "owner_lane": "", "provenance": { diff --git a/src/omega_gpu_attention_api.c b/src/omega_gpu_attention_api.c index 3edadbae5..868e0a796 100644 --- a/src/omega_gpu_attention_api.c +++ b/src/omega_gpu_attention_api.c @@ -261,7 +261,12 @@ static int check_loop_invariant(const Em *e) { * lane-dependent "j >= ctx" branch restored inside a reconvergence region (BSSY/BSYNC) so * the SHFL reductions run on a joined warp; 2 = the same with the BSYNC dropped (the * 2026-10-04 bug shape, a negative control the host simulator must catch). */ -typedef struct { bool f32; uint32_t log2bs, log2gqa; int mutant; int divergent; uint32_t hd; } KSpec; +typedef struct { bool f32; uint32_t log2bs, log2gqa; int mutant; int divergent; uint32_t hd; int split; } KSpec; +/* split (cut F, 2026-10-12): -1 = direct output (the gqa f32 and rows callers; byte for byte the kernel before). + * 0..MAX_LOG2SPLIT = the paged path: 2^split CTAs share one (sequence, head). Each sequence derives its own split + * count from its own context (so its result never depends on the other sequences in the launch), every CTA writes + * an unnormalised partial (acc[hd], running max, running sum; an empty slice writes acc 0, max -inf) and the host + * combines them, also when split == 0. */ static void gen_attention(Em *e, const KSpec *k) { const uint32_t elem = k->f32 ? 4u : 2u; @@ -275,6 +280,12 @@ static void gen_attention(Em *e, const KSpec *k) { ldc32(e, p_kvp, ARG_KVP); ldc32(e, p_hstride, ARG_HSTRIDE); ldc32(e, p_nqh, ARG_NQH); ldc32(e, p_ctxoff, ARG_CTXOFF); s2r(e, tid, BW_SR_TID_X); s2r(e, h, BW_SR_CTAID_X); s2r(e, seq, BW_SR_CTAID_Y); + /* cut F, partial mode: grid y = sequence * S + split; the split index picks this CTA's slice of the context (below) */ + const bool part = k->split >= 0; + const uint32_t LS = part ? (uint32_t)k->split : 0u, S = 1u << LS, log2hd = (HD == 128u) ? 7u : 6u; + int split = -1, kb = -1, kend = -1; + if (part) { split = V(e); if (LS) { lop3andi(e, split, seq, S - 1u); shri(e, seq, seq, LS); } else movrz(e, split); } + int c1 = V(e), c4 = V(e), c32 = V(e), c64 = V(e), c256 = V(e), c512 = V(e), c768 = V(e), c896 = V(e), zero = V(e); /* c32 / c64 keep their hd 64 names: the inner (dim pair) loop trip count HD / 2 and the chunk size HD */ movi(e, c1, 1); movi(e, c4, 4); movi(e, c32, HD / 2); movi(e, c64, HD); @@ -297,12 +308,14 @@ static void gen_attention(Em *e, const KSpec *k) { wide(e, qaddr, qoff, 1, A); ldg(e, qv, qaddr, udesc); sts32(e, tid4, qv); /* SH_Q + tid*4 */ - if (ho == hq) wide(e, oaddr, qoff, 1, C); - else { - int orow = V(e), ooff = V(e); - imadr(e, orow, seq, p_nqh, ho); - imadi(e, ooff, orow, HD * 4, tid4); - wide(e, oaddr, ooff, 1, C); + if (!part) { /* direct output address; partial mode derives its row in the epilogue */ + if (ho == hq) wide(e, oaddr, qoff, 1, C); + else { + int orow = V(e), ooff = V(e); + imadr(e, orow, seq, p_nqh, ho); + imadi(e, ooff, orow, HD * 4, tid4); + wide(e, oaddr, ooff, 1, C); + } } /* kv head offset, this sequence's table row and context length */ @@ -316,6 +329,31 @@ static void gen_attention(Em *e, const KSpec *k) { imadi(e, t1, seq, 4, p_ctxoff); wide(e, ca, t1, 1, D); ldg(e, ctx, ca, udesc); + if (part) { + /* This sequence's own split count s_own = min(S, pow2floor(chunks / 2)), chunks = ceil(ctx / HD), from its own + * context alone (the host picks the launch S the same way over the batch); per = roundup(ceil(ctx / s_own), HD) + * whole chunks per split; this CTA's slice is [kb, kend), kend = min(ctx, kb + per) through the sign bit of + * ctx - (kb + per). Splits past s_own get kb >= kend: an empty partial. Branch-free: candidate 2^c replaces + * per when chunks / 2 >= 2^c; the candidates nest, so the largest that fits wins. */ + int per = V(e), x = V(e), cand = V(e), dd = V(e), lt = V(e), cH1 = V(e), cK = V(e); + kb = V(e); kend = V(e); + movi(e, cH1, HD - 1u); + iadd3(e, per, ctx, cH1); lop3andi(e, per, per, ~(HD - 1u)); /* roundup(ctx, HD): one split takes it all */ + shri(e, x, per, log2hd + 1u); /* chunks / 2 */ + for (uint32_t c = 1; c <= LS; c++) { + movi(e, cK, (1u << c) - 1u); iadd3(e, cand, ctx, cK); shri(e, cand, cand, c); + iadd3(e, cand, cand, cH1); lop3andi(e, cand, cand, ~(HD - 1u)); /* roundup(ceil(ctx / 2^c), HD) */ + movi(e, cK, 0u - (1u << c)); iadd3(e, dd, x, cK); shri(e, lt, dd, 31); /* lt = (x < 2^c) */ + imadi(e, dd, cand, 0xFFFFFFFFu, per); /* per - cand */ + imadr(e, per, lt, dd, cand); /* lt ? per : cand */ + } + imadr(e, kb, split, per, NOREG); + iadd3(e, kend, kb, per); + imadi(e, dd, kend, 0xFFFFFFFFu, ctx); + shri(e, lt, dd, 31); + imadr(e, kend, lt, dd, kend); + } + const int lim = part ? kend : ctx; /* the end of this CTA's token range */ /* V column base for dimension tid: pool + kv plane + tid * elem. The kv head offset is * already inside the per-token offset phase A leaves in shared memory (found by the host * simulator: adding it here too read V from the wrong head for every kv head but 0). */ @@ -325,10 +363,10 @@ static void gen_attention(Em *e, const KSpec *k) { int M = V(e), L = V(e), acc = V(e), base = V(e), sOff = V(e), oOff = V(e), rOff = V(e); movi(e, M, F32_NEG_INF); if (k->mutant == OMEGA_GPU_ATTN_MUTANT_NO_MAX) movrz(e, L); else movf(e, L, 1.0f); /* alpha = 0 on the first chunk clears it */ - movrz(e, acc); movrz(e, base); + movrz(e, acc); if (part) iadd3(e, base, kb, zero); else movrz(e, base); iadd3(e, sOff, tid4, c256); iadd3(e, oOff, tid4, c512); iadd3(e, rOff, tid4, c768); bar_sync(e); /* q visible to every thread */ - isetp_ge_u32(e, zero, ctx); /* ctx == 0: out = 0 */ + if (part) isetp_ge_u32(e, kb, kend); else isetp_ge_u32(e, zero, ctx); /* empty range: out = 0 (an empty partial) */ int bZ = bra_fwd_if_p0(e); /* ---------------- chunk loop ---------------- */ @@ -336,7 +374,7 @@ static void gen_attention(Em *e, const KSpec *k) { int bi = V(e), sl = V(e), ta = V64(e), blk = V(e), ka = V64(e), dot = V(e), d = V(e); int lo = V(e), hi = V(e), qa = V(e), j1 = V(e); int dneg = V(e), over = V(e), ot = V(e), ctxm1 = V(e), cm1 = V(e), cninf = V(e), nb = V(e), ns = V(e); - movi(e, cm1, 0xFFFFFFFFu); iadd3(e, ctxm1, ctx, cm1); movi(e, cninf, F32_NEG_INF); + movi(e, cm1, 0xFFFFFFFFu); iadd3(e, ctxm1, lim, cm1); movi(e, cninf, F32_NEG_INF); loop_begin(e); int Lout = here(e); /* No branch here: a warp split by "j >= ctx" without a BSSY/BSYNC region stays split, and @@ -353,7 +391,7 @@ static void gen_attention(Em *e, const KSpec *k) { * j >= ctx skip phase A (s stays -inf, koff 0); the BSYNC at region_join rejoins them * before the SHFL reductions below. divergent == 2 drops that BSYNC (negative control). */ region_begin(e); - isetp_ge_u32(e, j, ctx); + isetp_ge_u32(e, j, lim); region_exit(e, RX_IF_P0); } else { imadi(e, dneg, j, 0xFFFFFFFFu, ctxm1); /* ctx - 1 - j */ @@ -475,7 +513,7 @@ static void gen_attention(Em *e, const KSpec *k) { loop_begin(e); int LB = here(e); iadd3(e, tt, base, kk); - isetp_ge_u32(e, tt, ctx); + isetp_ge_u32(e, tt, lim); int bB = bra_fwd_if_p0(e); imadi(e, oa, kk, 4, c512); imadi(e, pa, kk, 4, c256); @@ -498,15 +536,38 @@ static void gen_attention(Em *e, const KSpec *k) { patch_fwd(e, bB); bar_sync(e); /* phase B reads of s/p and offsets finish before the next chunk writes them */ iadd3(e, base, base, c64); - isetp_ge_u32(e, base, ctx); + isetp_ge_u32(e, base, lim); bra_back_if_not_p0(e, Lout); loop_end(e); patch_fwd(e, bZ); - int inv = V(e), o = V(e); - mufu(e, BW_IR_MUFU_RCP, inv, L); - e_fmul(e, o, acc, inv); - stg(e, oaddr, o, udesc); + if (part) { + /* partial row (seq * nqh + out head) * S + split: acc[HD] unnormalised, then M and L at words HD and HD + 1 + * (every lane stores the same CTA-wide M and L). Everything is re-derived here from the CTA ids and the + * constant bank so no extra value lives across the chunk loop (the hd 128 kernel sits at the register budget). */ + int cy = V(e), sq = V(e), sp = V(e), hh = V(e), nq = V(e), t4 = V(e), prow = V(e), poff = V(e); + int paddr = V64(e), maddr = V64(e), laddr = V64(e), Cp = V64(e); + s2r(e, cy, BW_SR_CTAID_Y); s2r(e, hh, BW_SR_CTAID_X); ldc32(e, nq, ARG_NQH); ldc64(e, Cp, ARG_OUT); + if (k->mutant == OMEGA_GPU_ATTN_MUTANT_OUT_ROW) lop3xor(e, hh, hh, c1); + if (LS) { lop3andi(e, sp, cy, S - 1u); shri(e, sq, cy, LS); } else { movrz(e, sp); iadd3(e, sq, cy, zero); } + imadr(e, prow, sq, nq, hh); + imadi(e, prow, prow, S, sp); + imadi(e, t4, tid, 4, NOREG); + imadi(e, poff, prow, (HD + 2u) * 4u, t4); + wide(e, paddr, poff, 1, Cp); + stg(e, paddr, acc, udesc); + imadi(e, poff, prow, (HD + 2u) * 4u, NOREG); + wide(e, maddr, poff, 1, Cp); + wide(e, maddr, c64, 4, maddr); + stg(e, maddr, M, udesc); + wide(e, laddr, c1, 4, maddr); + stg(e, laddr, L, udesc); + } else { + int inv = V(e), o = V(e); + mufu(e, BW_IR_MUFU_RCP, inv, L); + e_fmul(e, o, acc, inv); + stg(e, oaddr, o, udesc); + } tail(e); } @@ -557,7 +618,8 @@ static int g_divergent; /* omega #308 R2 test hook: see KSpec.divergent */ static bool g_dedupe = true; /* shared-prefix staging dedupe; false = legacy per-sequence staging (test hook) */ typedef struct { int used; KSpec k; OmegaBlackwellKernel kernel; BlackwellIRProgram *prog; NvrmMem code; } Slot; static OmegaGpuAttnSim g_sim; -static Slot g_cache[8]; +static Slot g_cache[16]; /* split variants (cut F) multiply the shapes in flight */ +static int g_split = -1; /* test hook: forced log2split (0..OMEGA_GPU_ATTN_MAX_LOG2SPLIT), -1 = chosen per launch from the context */ static OmegaGpuScratch g_q, g_pool, g_tab, g_out; /* staging, grown to the high-water mark, reused per call */ static NvrmMem g_resident; /* the resident KV pool (omega_gpu_attention_pool_alloc); cpu NULL = none */ static bool g_hooked; @@ -566,6 +628,7 @@ static bool g_hooked; void omega_gpu_attention_test_set_simulator(OmegaGpuAttnSim sim) { LOCK(); g_sim = sim; UNLOCK(); } void omega_gpu_attention_test_set_dedupe(bool on) { LOCK(); g_dedupe = on; UNLOCK(); } +void omega_gpu_attention_test_set_split(int log2split) { LOCK(); g_split = (log2split >= 0 && log2split <= (int)OMEGA_GPU_ATTN_MAX_LOG2SPLIT) ? log2split : -1; UNLOCK(); } /* ---- resident KV pool (see the header) ---- */ @@ -636,11 +699,11 @@ void omega_gpu_attention_test_set_mutant(OmegaGpuAttnMutant m) { /* Caller holds the lock. Builds the kernel if needed; uploads the device copy when * `upload` (the chip path; the simulator never needs one). */ -static int kernel_for(bool f32, uint32_t log2bs, uint32_t log2gqa, uint32_t hd, bool upload, Slot **out, bool *hit) { - KSpec k = { .f32 = f32, .log2bs = log2bs, .log2gqa = log2gqa, .mutant = g_mutant, .divergent = g_divergent, .hd = hd }; +static int kernel_for(bool f32, uint32_t log2bs, uint32_t log2gqa, uint32_t hd, int split, bool upload, Slot **out, bool *hit) { + KSpec k = { .f32 = f32, .log2bs = log2bs, .log2gqa = log2gqa, .mutant = g_mutant, .divergent = g_divergent, .hd = hd, .split = split }; Slot *s = NULL; for (size_t i = 0; i < sizeof g_cache / sizeof g_cache[0]; i++) - if (g_cache[i].used && g_cache[i].k.f32 == k.f32 && g_cache[i].k.log2bs == k.log2bs && g_cache[i].k.log2gqa == k.log2gqa && g_cache[i].k.mutant == k.mutant && g_cache[i].k.divergent == k.divergent && g_cache[i].k.hd == k.hd) { /* field compare: memcmp would read struct padding */ + if (g_cache[i].used && g_cache[i].k.f32 == k.f32 && g_cache[i].k.log2bs == k.log2bs && g_cache[i].k.log2gqa == k.log2gqa && g_cache[i].k.mutant == k.mutant && g_cache[i].k.divergent == k.divergent && g_cache[i].k.hd == k.hd && g_cache[i].k.split == k.split) { /* field compare: memcmp would read struct padding */ s = &g_cache[i]; *hit = true; break; } if (!s) { @@ -668,7 +731,7 @@ int omega_gpu_attention_codegen_hd(bool kv_f32, uint32_t log2_block_size, uint32 if (!kernel || log2_block_size > 16 || log2_gqa > 6) return OMEGA_GPU_ATTN_BAD_ARGS; if (head_dim != 64u && head_dim != 128u) return OMEGA_GPU_ATTN_TOO_LARGE; LOCK(); - KSpec k = { .f32 = kv_f32, .log2bs = log2_block_size, .log2gqa = log2_gqa, .mutant = g_mutant, .divergent = g_divergent, .hd = head_dim }; + KSpec k = { .f32 = kv_f32, .log2bs = log2_block_size, .log2gqa = log2_gqa, .mutant = g_mutant, .divergent = g_divergent, .hd = head_dim, .split = -1 }; UNLOCK(); return build_kernel(&k, kernel, NULL); } @@ -860,11 +923,11 @@ static int check_heads(uint32_t num_q_heads, uint32_t num_kv_heads, uint32_t hea } /* Common prologue of every chip entry (lock held on success): device, kernel, info. */ -static int begin(bool f32, uint32_t log2bs, uint32_t log2gqa, uint32_t hd, Slot **ks, OmegaGpuAttnInfo *info) { +static int begin(bool f32, uint32_t log2bs, uint32_t log2gqa, uint32_t hd, int split, Slot **ks, OmegaGpuAttnInfo *info) { LOCK(); int rc = g_sim ? OMEGA_GPU_ATTN_OK : dev_open_locked(); bool hit = false; - if (rc == OMEGA_GPU_ATTN_OK) rc = kernel_for(f32, log2bs, log2gqa, hd, !g_sim, ks, &hit); + if (rc == OMEGA_GPU_ATTN_OK) rc = kernel_for(f32, log2bs, log2gqa, hd, split, !g_sim, ks, &hit); if (rc != OMEGA_GPU_ATTN_OK) { UNLOCK(); return rc; } info_begin(info, &(*ks)->kernel, hit); return OMEGA_GPU_ATTN_OK; @@ -884,7 +947,7 @@ int omega_gpu_gqa_attention_f32(const float *q, const float *k_cache, const floa if (seq_len == 0) { memset(out, 0, n_out * 4); if (info) { memset(info, 0, sizeof *info); info->call_ns = now_ns() - t0; } return OMEGA_GPU_ATTN_OK; } Slot *ks = NULL; const uint32_t log2bs = 12; /* one block of 4096 slots */ - rc = begin(true, log2bs, log2gqa, HD, &ks, info); + rc = begin(true, log2bs, log2gqa, HD, -1, &ks, info); if (rc != OMEGA_GPU_ATTN_OK) return rc; size_t plane = (size_t)seq_len * num_kv_heads * HD * 4; void *host_tmp = NULL; @@ -932,7 +995,7 @@ int omega_gpu_gqa_attention_rows_f32(const float *q, const float *k_cache, const const size_t row = (size_t)num_q_heads * HD; Slot *ks = NULL; const uint32_t log2bs = 12; /* one block of 4096 slots, as the single-row call */ - rc = begin(true, log2bs, log2gqa, HD, &ks, info); + rc = begin(true, log2bs, log2gqa, HD, -1, &ks, info); if (rc != OMEGA_GPU_ATTN_OK) return rc; /* the whole context (every row's keys are a prefix of it) staged once */ size_t plane = (size_t)total * num_kv_heads * HD * 4; @@ -967,6 +1030,57 @@ int omega_gpu_gqa_attention_rows_f32(const float *q, const float *k_cache, const /* Paged path shared by the single and batched entries. tables[s] has n_blocks[s] entries * (already filtered of negatives), ctx[s] the context length. */ +/* cut F: a sequence's own split count from its context alone (the kernel computes the same): log2 of the largest + * power of two <= min(2^MAX_LOG2SPLIT, chunks / 2), chunks = ceil(ctx / HD). */ +static uint32_t own_log2split(uint32_t ctx, uint32_t HD) { + uint32_t x = ((ctx + HD - 1) / HD) / 2, l = 0; + while (l < OMEGA_GPU_ATTN_MAX_LOG2SPLIT && (2u << l) <= x) l++; + return l; +} + +/* 2^x in float without the math library (the engine archive also links where there is none): round x to the + * nearest integer n, 2^f on f in [-0.5, 0.5] by the Cephes exp2f polynomial, scale by 2^n through the exponent + * bits. x <= -127 (so also -inf, an empty split) is 0. The combine only passes x <= 0. */ +static float host_exp2(float x) { + if (x <= -127.0f) return 0.0f; + if (x > 127.0f) x = 127.0f; + int n = (int)(x + (x < 0.0f ? -0.5f : 0.5f)); + const float f = x - (float)n; + float px = 1.535336188319500e-4f; + px = px * f + 1.339887440266574e-3f; + px = px * f + 9.618437357674640e-3f; + px = px * f + 5.550332471162809e-2f; + px = px * f + 2.402264791363012e-1f; + px = px * f + 6.931472028550421e-1f; + px = 1.0f + f * px; + if (n < -126) { px *= 0.5f; n = -126; } + union { uint32_t u; float f; } sc; + sc.u = (uint32_t)(n + 127) << 23; + return px * sc.f; +} + +/* cut F: fold the per-split partials of each (sequence, head) row into out. w_s = 2^(M_s - max M): the kernel's + * running max and sum live in the log2 domain (scale carries log2 e, MUFU.EX2). A row whose every split is empty + * (context 0) is 0. */ +static void combine_splits(const float *part, float *out, size_t rows, uint32_t S, uint32_t HD) { + const size_t pw = (size_t)HD + 2; + for (size_t r = 0; r < rows; r++) { + const float *p = part + r * S * pw; + float *o = out + r * HD; + float mx = -INFINITY; + for (uint32_t s = 0; s < S; s++) if (p[s * pw + HD] > mx) mx = p[s * pw + HD]; + if (!(mx > -INFINITY)) { memset(o, 0, (size_t)HD * 4); continue; } + float w[1u << OMEGA_GPU_ATTN_MAX_LOG2SPLIT], lsum = 0.0f; + for (uint32_t s = 0; s < S; s++) { w[s] = host_exp2(p[s * pw + HD] - mx); lsum += w[s] * p[s * pw + HD + 1]; } + const float inv = 1.0f / lsum; + for (uint32_t d = 0; d < HD; d++) { + float a = 0.0f; + for (uint32_t s = 0; s < S; s++) a += w[s] * p[s * pw + d]; + o[d] = a * inv; + } + } +} + static int paged_core(const float *q, const uint8_t *pool, const OmegaGpuKvLayout *ly, const uint32_t *const *tables, const uint32_t *n_blocks, const uint32_t *ctx, uint32_t num_seqs, uint32_t layer_idx, uint32_t num_q_heads, uint32_t num_kv_heads, @@ -994,11 +1108,20 @@ static int paged_core(const float *q, const uint8_t *pool, const OmegaGpuKvLayou if (id >= ly->num_blocks || id * ly->block_stride_bytes + (uint64_t)(layer_idx + 1) * ly->layer_stride_bytes > ly->pool_bytes) { free(eff); return OMEGA_GPU_ATTN_BAD_ARGS; } } } - Slot *ks = NULL; - rc = begin(false, log2bs, log2gqa, HD, &ks, info); - if (rc != OMEGA_GPU_ATTN_OK) { free(eff); return rc; } uint32_t seqs_per_launch = OMEGA_GPU_ATTN_MAX_CTAS / num_q_heads; if (seqs_per_launch == 0) seqs_per_launch = 1; + /* cut F: split each (sequence, head) over 2^log2split CTAs along the context: at least two whole chunks per + * split, from the longest context in the call, within the split launch bound (the test hook forces it). */ + uint32_t log2split = 0; + if (g_split >= 0) log2split = (uint32_t)g_split; + else for (uint32_t s = 0; s < num_seqs; s++) { uint32_t l = own_log2split(eff[s], HD); if (l > log2split) log2split = l; } + while (log2split && ((uint64_t)num_q_heads * seqs_per_launch << log2split) > OMEGA_GPU_ATTN_SPLIT_MAX_CTAS) log2split--; + const uint32_t S = 1u << log2split; + float *part = malloc((size_t)seqs_per_launch * num_q_heads * S * (HD + 2) * 4); /* the partials of one launch */ + if (!part) { free(eff); return OMEGA_GPU_ATTN_CHIP_FAIL; } + Slot *ks = NULL; + rc = begin(false, log2bs, log2gqa, HD, (int)log2split, &ks, info); + if (rc != OMEGA_GPU_ATTN_OK) { free(eff); free(part); return rc; } uint32_t maxb = 1; for (uint32_t s = 0; s < num_seqs; s++) if (n_blocks[s] > maxb) maxb = n_blocks[s]; /* Resident pool (2026-10-12): the caller's pool IS omega's allocation. Read it in place when the @@ -1037,13 +1160,15 @@ static int paged_core(const float *q, const uint8_t *pool, const OmegaGpuKvLayou Launch L = { .q = q + (size_t)s0 * row, .q_bytes = (size_t)ns * row * 4, .pool = (const uint8_t *)pool + (size_t)layer_idx * ly->layer_stride_bytes, .pool_bytes = 0, - .tab = tab, .tab_bytes = ((size_t)ns * maxb + ns) * 4, .out = out + (size_t)s0 * row, .out_bytes = (size_t)ns * row * 4, - .grid_x = num_q_heads, .grid_y = ns, + .tab = tab, .tab_bytes = ((size_t)ns * maxb + ns) * 4, + .out = part, .out_bytes = (size_t)ns * num_q_heads * S * (HD + 2) * 4, + .grid_x = num_q_heads, .grid_y = ns * S, .params = { maxb, f2u(score_scale(HD)), (uint32_t)ly->block_stride_bytes, (uint32_t)ly->token_stride_bytes, (uint32_t)ly->kv_plane_stride_bytes, (uint32_t)ly->head_stride_bytes, num_q_heads, ns * maxb * 4 }, .pool_va = g_resident.va + (uint64_t)layer_idx * ly->layer_stride_bytes, }; rc = launch(ks, &L, info); + if (rc == OMEGA_GPU_ATTN_OK) combine_splits(part, out + (size_t)s0 * row, (size_t)ns * num_q_heads, S, HD); free(tab); continue; } @@ -1094,16 +1219,18 @@ static int paged_core(const float *q, const uint8_t *pool, const OmegaGpuKvLayou } Launch L = { .q = q + (size_t)s0 * row, .q_bytes = (size_t)ns * row * 4, .pool = staged, .pool_bytes = (size_t)staged_bytes, - .tab = tab, .tab_bytes = ((size_t)ns * maxb + ns) * 4, .out = out + (size_t)s0 * row, .out_bytes = (size_t)ns * row * 4, - .grid_x = num_q_heads, .grid_y = ns, + .tab = tab, .tab_bytes = ((size_t)ns * maxb + ns) * 4, + .out = part, .out_bytes = (size_t)ns * num_q_heads * S * (HD + 2) * 4, + .grid_x = num_q_heads, .grid_y = ns * S, .params = { maxb, f2u(score_scale(HD)), (uint32_t)ly->layer_stride_bytes, (uint32_t)ly->token_stride_bytes, (uint32_t)ly->kv_plane_stride_bytes, (uint32_t)ly->head_stride_bytes, num_q_heads, ns * maxb * 4 } }; rc = launch(ks, &L, info); + if (rc == OMEGA_GPU_ATTN_OK) combine_splits(part, out + (size_t)s0 * row, (size_t)ns * num_q_heads, S, HD); free(host_tmp); free(tab); } UNLOCK(); - free(eff); free(slot_of); free(touched); + free(eff); free(slot_of); free(touched); free(part); if (info) info->call_ns = now_ns() - t0; return rc; } diff --git a/src/omega_gpu_attention_api.h b/src/omega_gpu_attention_api.h index c6c863f4b..d913ebfd3 100644 --- a/src/omega_gpu_attention_api.h +++ b/src/omega_gpu_attention_api.h @@ -36,8 +36,10 @@ * the top half of an f32); bf16 V by LDG.E.U16 and the same shift. f32 K/V are * read directly. Tolerances are stated and justified in tests/gpu_attention_test.c. * - * Launch envelope: at most OMEGA_GPU_ATTN_MAX_CTAS CTAs per launch (investigation - * I42, same as cuts 1b and 4): grid = (num_q_heads, sequences per launch). + * Launch envelope: at most OMEGA_GPU_ATTN_MAX_CTAS / num_q_heads sequences per launch (investigation + * I42, same as cuts 1b and 4): grid = (num_q_heads, sequences per launch * context splits); the paged + * path adds up to 2^OMEGA_GPU_ATTN_MAX_LOG2SPLIT context splits per (sequence, head) within + * OMEGA_GPU_ATTN_SPLIT_MAX_CTAS (cut F, the rows path proved 1024 CTAs on the chip). * Shared memory: the QMD declares 16 * head_dim bytes (clcec0qmd.h SHARED_MEMORY_SIZE_SHIFTED7, * src/omega_blackwell_qmd.c word 36); the kernel uses exactly 4 arrays of head_dim words: * 1024 bytes for head_dim 64 (TinyLlama, Llama-3.2-1B; the QMD default) and 2048 bytes for @@ -81,6 +83,14 @@ enum { #define OMEGA_GPU_ATTN_HEAD_DIM 64u /* the original head_dim; threads per CTA = head_dim (one thread per dimension / per token of a chunk) */ #define OMEGA_GPU_ATTN_HEAD_DIM_128 128u /* the second supported head_dim (Qwen3-4B) */ #define OMEGA_GPU_ATTN_MAX_CTAS 64u +/* Cut F (2026-10-12): a paged call splits each (sequence, head) over up to 2^MAX_LOG2SPLIT CTAs along the context + * (whole chunks each, at least two chunks per split; the launch takes the largest count in the batch) within + * SPLIT_MAX_CTAS CTAs per launch (the rows path proved 1024 on the chip); the CTAs write unnormalised partials + * (acc[head_dim], running max, running sum) that the host combines, also when the split is 1 (the gqa f32 and rows + * kernels keep the direct output and are byte for byte the ones before). Each sequence picks its own split count + * from its own context, so a result never depends on the other sequences in a launch. */ +#define OMEGA_GPU_ATTN_MAX_LOG2SPLIT 3u +#define OMEGA_GPU_ATTN_SPLIT_MAX_CTAS 1024u #define OMEGA_GPU_ATTN_MAX_Q_HEADS 64u #define OMEGA_GPU_ATTN_MAX_GQA_CTX 4096u /* contiguous path: one block of 2^12 slots */ @@ -262,6 +272,8 @@ void omega_gpu_attention_test_set_simulator(OmegaGpuAttnSim sim); * its own blocks), kept as the reference fallback and as the negative control of the * staging-byte tests. Test hook; never cleared in production. */ void omega_gpu_attention_test_set_dedupe(bool on); +/* Test hook: force the context split (log2, 0 = one CTA per head) on every paged call; -1 = automatic. */ +void omega_gpu_attention_test_set_split(int log2split); /* The resident pool's per-block fit rule (see omega_gpu_attention_pool_alloc), exposed so the * 4 GiB edge can be checked without allocating a 4 GiB pool. Test hook. */ diff --git a/tests/.crumb b/tests/.crumb index fdbb08971..8778f120d 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -290,14 +290,14 @@ } ], "children_root": "sha256:9597c068d5bf4eb27f1d91842813a2b10269c2ccae421dceefb4308030ec0fc2", - "compiled_at": "2026-10-12T08:17:13Z", + "compiled_at": "2026-10-12T08:54:18Z", "compiler": "crumb-compile/1", - "digest": "sha256:c8932aae0268325ccc74e09284de055134b022abdfaa080227f5be40c0a638e7", + "digest": "sha256:57ed2a1bfe7a7a524224ed3b38f88a32b0bda81322a3f1267fdf9da2db161d45", "evidence_files": 16, "evidence_root": "sha256:96d08097822ad95f4e562c19e0ba5df5a035774c48632f1832912c40a27bd27a", "files": 1024, - "generated_at_commit": "09eed5528137abc1103768858538db18815654ab", - "source_tree": "sha256:f1e943d7d11af91e696903447bae33deb6b0d1d747bbe25ed20422aad9a4988b" + "generated_at_commit": "89913b0b89f40dd3934e18eecc79708e32508bb2", + "source_tree": "sha256:fe3786166e0e61bc59ecd143bb211b255c9755ee21fae75145bd253a6268de48" }, "owner_lane": "", "provenance": { diff --git a/tests/gpu_attention_test.c b/tests/gpu_attention_test.c index 6da274598..87487a0b9 100644 --- a/tests/gpu_attention_test.c +++ b/tests/gpu_attention_test.c @@ -503,6 +503,53 @@ static int chip(const char *out_path) { } else run_mutants(&c, no_mutants, run_paged, &pc, got, want, n, 2e-4, 2e-5); record(c, out); free(pool); free(ids); free(q); free(got); free(want); } + /* 3b. context split (cut F): the 32q/8kv shape at ctx 1000 under every forced split against the oracle, the + * CTA count per launch grows with the split, the automatic choice at this context is 4 (hd 128: 8 chunks) or + * 8 (hd 64: 16 chunks), and a short context (ctx 100, one chunk) stays unsplit. A forced launch split at or above + * the sequence's own count gives the same bits (the extra CTAs write empty partials that add exact zeros). */ + { + uint32_t nqh = 32, nkv = 8, bs = 16, layers = 2, layer = 1; + const uint32_t cx[2] = { 1000, 100 }; + for (int ci = 0; ci < 2; ci++) { + uint32_t ctx = cx[ci], nblk = (ctx + bs - 1) / bs, pool_blocks = nblk + 3; + OmegaGpuKvLayout ly = layout_for(pool_blocks, bs, layers, nkv, hd); + uint8_t *pool = malloc(ly.pool_bytes); uint16_t *pw = (uint16_t *)pool; + for (size_t i = 0; i < ly.pool_bytes / 2; i++) pw[i] = f2bf(frand(&seed, -1.0f, 1.0f)); + uint32_t *ids = malloc(nblk * 4); + for (uint32_t b = 0; b < nblk; b++) ids[b] = (b * 5 + 1) % pool_blocks; + size_t n = (size_t)nqh * hd; + float *q = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4), *auto_got = malloc(n * 4); + for (size_t i = 0; i < n; i++) q[i] = frand(&seed, -1.0f, 1.0f); + oracle_paged(q, pool, &ly, ids, nblk, ctx, layer, nqh, nkv, hd, want); + PagedCtx pc = { q, pool, &ly, ids, nblk, ctx, layer, nqh, nkv }; + OmegaGpuAttnInfo ai; + int arc = run_paged(&pc, auto_got, &ai); + uint32_t chunks = (ctx + hd - 1) / hd, want_log2 = 0; + while (want_log2 < OMEGA_GPU_ATTN_MAX_LOG2SPLIT && (2u << want_log2) <= chunks / 2) want_log2++; + CHECK(arc == OMEGA_GPU_ATTN_OK && ai.ctas_last_launch == (nqh << want_log2), + "split auto ctx %u: rc=%s ctas=%u (expected %u)", ctx, omega_gpu_attention_rc_name(arc), ai.ctas_last_launch, nqh << want_log2); + for (int ls = 0; ls <= (int)OMEGA_GPU_ATTN_MAX_LOG2SPLIT; ls++) { + omega_gpu_attention_test_set_split(ls); + OmegaGpuAttnInfo fi; + char name[96]; snprintf(name, sizeof name, "paged_bf16_split%d_32q8kv_ctx%u", 1 << ls, ctx); + Case c = { .name = strdup(name), .n = n }; + c.rc = run_paged(&pc, got, &fi); + c.bad = compare(got, want, n, 2e-4, 2e-5, &c.worst); + c.pass = c.rc == OMEGA_GPU_ATTN_OK && c.bad == 0; case_info(&c, &fi); + CHECK(fi.ctas_last_launch == (nqh << ls) && fi.out_bytes == (uint64_t)nqh * (1u << ls) * (hd + 2) * 4, + "%s: ctas=%u out_bytes=%llu", name, fi.ctas_last_launch, (unsigned long long)fi.out_bytes); + if ((uint32_t)ls >= want_log2) CHECK(memcmp(got, auto_got, n * 4) == 0, "%s equals the automatic choice bit for bit (a larger launch split only adds empty partials)", name); + if (ls == 1 && ci == 0) { + static const int ms[3] = { OMEGA_GPU_ATTN_MUTANT_KV_HEAD, OMEGA_GPU_ATTN_MUTANT_SLOT, 0 }; + run_mutants(&c, ms, run_paged, &pc, got, want, n, 2e-4, 2e-5); + } else run_mutants(&c, no_mutants, run_paged, &pc, got, want, n, 2e-4, 2e-5); + record(c, out); + } + omega_gpu_attention_test_set_split(-1); + if (ci == 1) CHECK(ai.ctas_last_launch == nqh, "ctx %u: one chunk stays unsplit (%u CTAs)", ctx, ai.ctas_last_launch); + free(pool); free(ids); free(q); free(got); free(want); free(auto_got); + } + } /* 4. batch: three sequences (short, empty, truncated by its table), a negative table entry, table rows of 16 */ { uint32_t nqh = 32, nkv = 8, bs = 16, layers = 3, layer = 2, maxb = 16, nseq = 3, pool_blocks = 40; @@ -572,7 +619,11 @@ static int chip(const char *out_path) { CHECK(c.blk_logical == logical && c.blk_unique == expect_unique, "%s: %u logical references, %u unique staged (expected %u/%u)", name, c.blk_logical, c.blk_unique, logical, expect_unique); CHECK(c.kv_naive == (uint64_t)logical * ly.layer_stride_bytes && c.kv_staged == (uint64_t)expect_unique * ly.layer_stride_bytes && c.kv_staged < c.kv_naive, "%s: staged %" PRIu64 " < naive %" PRIu64 " bytes (saved %" PRIu64 ")", name, c.kv_staged, c.kv_naive, c.kv_naive - c.kv_staged); - CHECK(info.kv_source == OMEGA_GPU_ATTN_KV_STAGED && info.q_bytes == n * 4 && info.out_bytes == n * 4 && info.tab_bytes > 0, "%s: q/out/table traffic accounted (q=%" PRIu64 " out=%" PRIu64 " tab=%" PRIu64 ")", name, info.q_bytes, info.out_bytes, info.tab_bytes); + /* cut F: the output traffic is the partials, launch split = the largest own split in the call (hd 64: ctx 193 and 207 are 4 chunks, 2 splits; hd 128: 1) */ + uint32_t ls_max = 0; + for (uint32_t s = 0; s < nseq; s++) { uint32_t x = (((uint32_t)ctxs5[s] + hd - 1) / hd) / 2, l = 0; while (l < OMEGA_GPU_ATTN_MAX_LOG2SPLIT && (2u << l) <= x) l++; if (l > ls_max) ls_max = l; } + const uint64_t out_expect = (uint64_t)nseq * nqh * (1u << ls_max) * (hd + 2) * 4; + CHECK(info.kv_source == OMEGA_GPU_ATTN_KV_STAGED && info.q_bytes == n * 4 && info.out_bytes == out_expect && info.tab_bytes > 0, "%s: q/out/table traffic accounted (q=%" PRIu64 " out=%" PRIu64 " tab=%" PRIu64 ")", name, info.q_bytes, info.out_bytes, info.tab_bytes); /* negative control: legacy per-sequence staging duplicates the shared prefix; same answer, more bytes */ omega_gpu_attention_test_set_dedupe(false); OmegaGpuAttnInfo li; int lrc = run_batch(&bc, legacy, &li);