omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / omega_gpu_prefill_attn256_api.c
| 1 | /* |
| 2 | * omega_gpu_prefill_attn256_api.c: the two launches of the head_dim 256 prefill attention (omega-mv#545; see the |
| 3 | * header). The prep kernel is the decode kernel's own program in prep mode (omega_blackwell_codegen_attn256.h), the |
| 4 | * attend kernel is omega_blackwell_codegen_pattn256.h (SHFL.BFLY and F2FP.BF16.F32.PACK_AB are native encoder entries |
| 5 | * since training R5). Session use mirrors omega_gpu_prefill_attn_api.c. |
| 6 | */ |
| 7 | #include "omega_gpu_prefill_attn256_api.h" |
| 8 | #include "omega_blackwell_codegen_tc.h" |
| 9 | #include "omega_gpu_session.h" |
| 10 | #include "omega_gpu_cmdlist.h" |
| 11 | #include <string.h> |
| 12 | |
| 13 | #define MARKER_TIMEOUT_MS 60000ull |
| 14 | #define POISON_F32 (-999.0f) |
| 15 | enum { KK_PREP, KK_ATTEND, KK_N }; |
| 16 | |
| 17 | typedef struct { |
| 18 | bool valid; |
| 19 | union { OmegaDecodeAttn256Spec prep; OmegaPrefillAttn256Spec att; } spec; |
| 20 | OmegaBlackwellKernel kernel; |
| 21 | OmegaGpuMem code; |
| 22 | uint32_t insns; |
| 23 | } Slot; |
| 24 | |
| 25 | static struct { |
| 26 | bool hooked; |
| 27 | Slot slot[KK_N]; |
| 28 | int prep_mut, att_mut; |
| 29 | OmegaGpuMem qf; size_t qf_size; bool have_qf; /* q' scratch */ |
| 30 | OmegaGpuMem qkv, out; size_t qkv_size, out_size; bool have_bufs; /* host-entry staging */ |
| 31 | } g; |
| 32 | |
| 33 | static void bufs_forget(void) { g.have_qf = false; g.qf_size = 0; g.have_bufs = false; g.qkv_size = g.out_size = 0; } |
| 34 | static void on_session_close(void) { memset(g.slot, 0, sizeof g.slot); bufs_forget(); } |
| 35 | static OmegaGpuSession *dev_open_locked(void) { |
| 36 | if (!g.hooked) { (void)omega_gpu_session_on_close(on_session_close); g.hooked = true; } |
| 37 | return omega_gpu_session_open(); |
| 38 | } |
| 39 | static void slots_clear_locked(void) { |
| 40 | for (unsigned k = 0; k < KK_N; k++) { if (g.slot[k].valid) omega_gpu_session_free(&g.slot[k].code); memset(&g.slot[k], 0, sizeof g.slot[k]); } |
| 41 | } |
| 42 | void omega_gpu_prefill_attn256_test_set_mutant(int prep_mutant, int attend_mutant) { |
| 43 | omega_gpu_session_lock(); g.prep_mut = prep_mutant; g.att_mut = attend_mutant; slots_clear_locked(); omega_gpu_session_unlock(); |
| 44 | } |
| 45 | void omega_gpu_prefill_attn256_release(void) { |
| 46 | omega_gpu_session_lock(); |
| 47 | if (g.have_qf) omega_gpu_session_free(&g.qf); |
| 48 | if (g.have_bufs) { omega_gpu_session_free(&g.qkv); omega_gpu_session_free(&g.out); } |
| 49 | bufs_forget(); |
| 50 | omega_gpu_session_unlock(); |
| 51 | } |
| 52 | |
| 53 | /* sh: the cache's head shape (q_heads / kv_heads / q_per_cta; NULL = the 27B) */ |
| 54 | static void spec_of(unsigned kind, const uint32_t *sh, Slot *tmp) { |
| 55 | memset(tmp, 0, sizeof *tmp); |
| 56 | if (kind == KK_PREP) { |
| 57 | tmp->spec.prep.depth = OMEGA_GPU_DATTN256_DEPTH; tmp->spec.prep.mutant = g.prep_mut; tmp->spec.prep.prep = 1; |
| 58 | if (sh) { tmp->spec.prep.q_heads = sh[0]; tmp->spec.prep.kv_heads = sh[1]; tmp->spec.prep.q_per_cta = sh[2]; } |
| 59 | } else { |
| 60 | tmp->spec.att.mutant = g.att_mut; |
| 61 | if (sh) { tmp->spec.att.q_heads = sh[0]; tmp->spec.att.kv_heads = sh[1]; } |
| 62 | } |
| 63 | } |
| 64 | static int prog_of(unsigned kind, const Slot *tmp, BlackwellIRProgram *prog) { |
| 65 | return kind == KK_PREP ? omega_blackwell_codegen_decode_attn256_prog(&tmp->spec.prep, prog) |
| 66 | : omega_blackwell_codegen_prefill_attn256_prog(&tmp->spec.att, prog); |
| 67 | } |
| 68 | size_t omega_gpu_prefill_attn256_encode(unsigned kind, uint8_t *code, size_t cap, uint32_t *gprs) { |
| 69 | if (kind >= KK_N) return 0; |
| 70 | Slot tmp; spec_of(kind, NULL, &tmp); |
| 71 | BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); |
| 72 | if (!prog) return 0; |
| 73 | size_t need = 0; |
| 74 | if (prog_of(kind, &tmp, prog) == 0) need = omega_bw_program_code_bytes(prog); |
| 75 | OmegaBlackwellKernel k; |
| 76 | if (need == 0 || need > cap || omega_blackwell_kernel_encode_into(prog, code, cap, &k) != 0) { omega_gpu_host_free(prog); return 0; } |
| 77 | if (gprs) *gprs = k.gpr_count; |
| 78 | omega_gpu_host_free(prog); |
| 79 | return need; |
| 80 | } |
| 81 | |
| 82 | /* rec: a record call is running (lock held): a miss is refused instead of compiled */ |
| 83 | static int kernel_for(unsigned kind, const uint32_t *sh, int rec, Slot **out, bool *hit) { |
| 84 | Slot tmp; spec_of(kind, sh, &tmp); |
| 85 | Slot *s = &g.slot[kind]; |
| 86 | if (s->valid && memcmp(&s->spec, &tmp.spec, sizeof tmp.spec) == 0) { *out = s; *hit = true; return OMEGA_GPU_DATTN_OK; } |
| 87 | if (rec) { omega_gpu_session_set_error("prefill attn256 record: kernel cache miss (warm up before recording)"); return OMEGA_GPU_DATTN_TOO_LARGE; } |
| 88 | if (s->valid) omega_gpu_session_free(&s->code); |
| 89 | memset(s, 0, sizeof *s); |
| 90 | BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); |
| 91 | if (!prog) { omega_gpu_session_set_error("prefill attn256 codegen: host memory"); return OMEGA_GPU_DATTN_CODEGEN_FAIL; } |
| 92 | int cg = prog_of(kind, &tmp, prog); |
| 93 | size_t need = cg == 0 ? omega_bw_program_code_bytes(prog) : 0; |
| 94 | if (cg != 0 || need == 0) { omega_gpu_host_free(prog); omega_gpu_session_set_error("prefill attn256 codegen refused the spec"); return OMEGA_GPU_DATTN_CODEGEN_FAIL; } |
| 95 | /* both launches run 256 threads per CTA: the declared registers must fit the 64K register file (CUDA Programming |
| 96 | * Guide, Compute Capabilities: "maximum number of 32-bit registers per thread block is 64K") */ |
| 97 | if ((uint64_t)OMEGA_PATTN256_THREADS * omega_bw_gpr_declared(prog->regalloc.peak_gpr_usage) > 65536u) { omega_gpu_host_free(prog); omega_gpu_session_set_error("prefill attn256: register file"); return OMEGA_GPU_DATTN_TOO_LARGE; } |
| 98 | const uint32_t insns = (uint32_t)prog->count; |
| 99 | OmegaGpuMem code; |
| 100 | if (omega_gpu_session_alloc_code(need, &code) != 0) { omega_gpu_host_free(prog); return OMEGA_GPU_DATTN_CHIP_FAIL; } |
| 101 | OmegaBlackwellKernel kernel; |
| 102 | cg = omega_blackwell_kernel_encode_into(prog, code.cpu, need, &kernel); |
| 103 | omega_gpu_host_free(prog); |
| 104 | if (cg != 0 || !kernel.code || kernel.code_size != need) { omega_gpu_session_free(&code); omega_gpu_session_set_error("prefill attn256 encode"); return OMEGA_GPU_DATTN_CODEGEN_FAIL; } |
| 105 | if (omega_gpu_session_sync_to_gpu(&code) != 0) { omega_gpu_session_free(&code); return OMEGA_GPU_DATTN_CHIP_FAIL; } |
| 106 | memset(s, 0, sizeof *s); |
| 107 | s->valid = true; s->spec = tmp.spec; s->kernel = kernel; s->code = code; s->insns = insns; |
| 108 | *out = s; *hit = false; |
| 109 | return OMEGA_GPU_DATTN_OK; |
| 110 | } |
| 111 | |
| 112 | /* q heads: a token pair of q' takes q_heads * 1024 bytes (OMEGA_PATTN256_QF_OFF_SHAPE) */ |
| 113 | static size_t qf_bytes(uint32_t rows, uint32_t q_heads) { return (size_t)((rows + 1u) / 2u) * q_heads * 1024u; } |
| 114 | static int qf_locked(size_t need) { |
| 115 | if (g.have_qf && g.qf_size >= need) return 0; |
| 116 | if (g.have_qf) { omega_gpu_session_free(&g.qf); g.have_qf = false; g.qf_size = 0; } |
| 117 | if (omega_gpu_session_alloc(need, &g.qf) != 0) return -1; |
| 118 | g.have_qf = true; g.qf_size = need; |
| 119 | return 0; |
| 120 | } |
| 121 | int omega_gpu_prefill_attn256_reserve(uint32_t max_m) { return omega_gpu_prefill_attn256_reserve_for(NULL, max_m); } |
| 122 | int omega_gpu_prefill_attn256_reserve_for(const OmegaGpuDecodeAttn256Cache *c, uint32_t max_m) { |
| 123 | if (max_m == 0 || max_m > OMEGA_GPU_PATTN256_MAX_M) return OMEGA_GPU_DATTN_BAD_ARGS; |
| 124 | OmegaGpuDecodeAttn256CacheView v; |
| 125 | if (c && omega_gpu_decode_attn256_cache_view(c, 0, &v) != OMEGA_GPU_DATTN_OK) return OMEGA_GPU_DATTN_BAD_ARGS; |
| 126 | const uint32_t shv[3] = { c ? v.q_heads : OMEGA_DATTN256_Q_HEADS, c ? v.kv_heads : OMEGA_DATTN256_KV_HEADS, c ? v.q_per_cta : OMEGA_DATTN256_Q_PER_KV }; |
| 127 | const uint32_t *sh = c ? shv : NULL; |
| 128 | omega_gpu_session_lock(); |
| 129 | int rc = dev_open_locked() && qf_locked(qf_bytes(max_m, shv[0])) == 0 ? OMEGA_GPU_DATTN_OK : OMEGA_GPU_DATTN_CHIP_FAIL; |
| 130 | /* and builds both kernels, so a record call after reserve never misses the cache (sc #642 prefill graph) */ |
| 131 | for (unsigned k = 0; k < KK_N && rc == OMEGA_GPU_DATTN_OK; k++) { Slot *s = NULL; bool hit = false; rc = kernel_for(k, sh, 0, &s, &hit); } |
| 132 | omega_gpu_session_unlock(); |
| 133 | return rc; |
| 134 | } |
| 135 | static int bufs_locked(size_t qkv_size, size_t out_size) { |
| 136 | if (g.have_bufs && g.qkv_size >= qkv_size && g.out_size >= out_size) return 0; |
| 137 | if (g.have_bufs) { omega_gpu_session_free(&g.qkv); omega_gpu_session_free(&g.out); g.have_bufs = false; g.qkv_size = g.out_size = 0; } |
| 138 | if (omega_gpu_session_alloc(qkv_size, &g.qkv) != 0) return -1; |
| 139 | if (omega_gpu_session_alloc(out_size, &g.out) != 0) { omega_gpu_session_free(&g.qkv); return -1; } |
| 140 | g.have_bufs = true; g.qkv_size = qkv_size; g.out_size = out_size; |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | /* host != 0: qkv / out are host arrays staged through the module buffers (poison-checked); else qkv_va / out_va are |
| 145 | * device buffers. rec non-NULL: record path (lock held, nothing allocated, compiled or submitted). */ |
| 146 | static int core(OmegaGpuDecodeAttn256Cache *c, uint32_t layer, uint32_t pos0, uint32_t m, const float *qkv, float *out, |
| 147 | uint64_t qkv_va, uint64_t out_va, float eps, float out_scale, OmegaGpuPrefillAttn256Info *info, OmegaGpuCmdList *rec) { |
| 148 | uint64_t t0 = omega_gpu_session_now_ns(); |
| 149 | if (info) memset(info, 0, sizeof *info); |
| 150 | const int host = qkv != NULL; |
| 151 | OmegaGpuDecodeAttn256CacheView v; |
| 152 | if (!c || m == 0 || m > OMEGA_GPU_PATTN256_MAX_M || omega_gpu_decode_attn256_cache_view(c, layer, &v) != OMEGA_GPU_DATTN_OK || |
| 153 | pos0 > v.max_pos || m > v.max_pos - pos0 || (host ? !out : (!qkv_va || !out_va || (qkv_va & 15u) || (out_va & 15u)))) return OMEGA_GPU_DATTN_BAD_ARGS; |
| 154 | const uint32_t sh[3] = { v.q_heads, v.kv_heads, v.q_per_cta }, QKVW = v.qkv_words, OUTW = v.out_words; |
| 155 | if (!rec) omega_gpu_session_lock(); |
| 156 | #define UNLOCK() do { if (!rec) omega_gpu_session_unlock(); } while (0) |
| 157 | if (!dev_open_locked()) { UNLOCK(); return OMEGA_GPU_DATTN_CHIP_FAIL; } |
| 158 | Slot *ks[KK_N] = { NULL, NULL }; bool hit[KK_N] = { false, false }; |
| 159 | int rc = OMEGA_GPU_DATTN_OK; |
| 160 | for (unsigned k = 0; k < KK_N && rc == OMEGA_GPU_DATTN_OK; k++) rc = kernel_for(k, sh, rec != NULL, &ks[k], &hit[k]); |
| 161 | if (rc != OMEGA_GPU_DATTN_OK) { UNLOCK(); return rc; } |
| 162 | if (rec) { if (!g.have_qf || g.qf_size < qf_bytes(m, v.q_heads)) { omega_gpu_session_set_error("prefill attn256 record: q' scratch not reserved for m tokens"); return OMEGA_GPU_DATTN_TOO_LARGE; } } |
| 163 | else if (qf_locked(qf_bytes(m, v.q_heads)) != 0) { UNLOCK(); return OMEGA_GPU_DATTN_CHIP_FAIL; } |
| 164 | if (host) { |
| 165 | if (bufs_locked((size_t)m * QKVW * 4u, (size_t)m * OUTW * 4u) != 0) { UNLOCK(); return OMEGA_GPU_DATTN_CHIP_FAIL; } |
| 166 | memcpy(g.qkv.cpu, qkv, (size_t)m * QKVW * 4u); |
| 167 | float *od = g.out.cpu; |
| 168 | for (size_t i = 0; i < (size_t)m * OUTW; i++) od[i] = POISON_F32; |
| 169 | if (omega_gpu_session_sync_to_gpu(&g.qkv) != 0 || omega_gpu_session_sync_to_gpu(&g.out) != 0) rc = OMEGA_GPU_DATTN_CHIP_FAIL; |
| 170 | qkv_va = g.qkv.va; out_va = g.out.va; |
| 171 | } |
| 172 | const uint32_t kvs = v.max_pos * OMEGA_DATTN256_ROW_BYTES, gx_att = (m + OMEGA_PATTN256_TOKENS - 1u) / OMEGA_PATTN256_TOKENS; |
| 173 | float fe = eps, f1 = 1.0f, fo = out_scale, fq = (float)(0.0625 * 1.4426950408889634); /* log2(e) / sqrt(256) */ |
| 174 | uint32_t ue, u1, uo, uq; memcpy(&ue, &fe, 4); memcpy(&u1, &f1, 4); memcpy(&uo, &fo, 4); memcpy(&uq, &fq, 4); |
| 175 | uint32_t ap[OMEGA_DATTN256_ARGS_WORDS], aa[OMEGA_PATTN256_ARGS_WORDS]; |
| 176 | #define A64(a, i, x) do { (a)[i] = (uint32_t)(x); (a)[(i) + 1] = (uint32_t)((uint64_t)(x) >> 32); } while (0) |
| 177 | A64(ap, 0, qkv_va); A64(ap, 2, v.k_va); A64(ap, 4, v.v_va); A64(ap, 6, v.rope_va); A64(ap, 8, v.q_norm_va); A64(ap, 10, v.k_norm_va); |
| 178 | A64(ap, 12, g.qf.va); A64(ap, 14, v.part_va); A64(ap, 16, v.cnt_va); |
| 179 | ap[18] = pos0; ap[19] = OMEGA_DATTN256_CHUNK; ap[20] = 1u; ap[21] = kvs; ap[22] = ue; ap[23] = u1; ap[24] = uq; |
| 180 | A64(aa, 0, g.qf.va); A64(aa, 2, v.k_va); A64(aa, 4, v.v_va); A64(aa, 6, out_va); |
| 181 | aa[8] = pos0; aa[9] = m; aa[10] = kvs; aa[11] = uo; |
| 182 | #undef A64 |
| 183 | OmegaGpuLaunch L[KK_N] = { |
| 184 | { .code_va = ks[KK_PREP]->code.va, .gpr_count = omega_bw_gpr_declared(ks[KK_PREP]->kernel.gpr_count), |
| 185 | .threads_x = OMEGA_PATTN256_THREADS, .threads_y = 1, .grid_x = m, .grid_y = v.q_heads / v.q_per_cta, |
| 186 | .num_elements = (size_t)m * QKVW, .args = ap, .n_args = OMEGA_DATTN256_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS, |
| 187 | .shared_bytes = OMEGA_DATTN256_SHARED_BYTES }, |
| 188 | { .code_va = ks[KK_ATTEND]->code.va, .gpr_count = omega_bw_gpr_declared(ks[KK_ATTEND]->kernel.gpr_count), |
| 189 | .threads_x = OMEGA_PATTN256_THREADS, .threads_y = 1, .grid_x = gx_att, .grid_y = v.kv_heads, |
| 190 | .num_elements = (size_t)m * OUTW, .args = aa, .n_args = OMEGA_PATTN256_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS, |
| 191 | .shared_bytes = OMEGA_PATTN256_SHARED_BYTES }, |
| 192 | }; |
| 193 | if (info) { |
| 194 | info->layer = layer; info->pos0 = pos0; info->m = m; info->grid_x[0] = m; info->grid_x[1] = gx_att; |
| 195 | for (unsigned k = 0; k < KK_N; k++) { info->insns[k] = ks[k]->insns; info->gprs[k] = ks[k]->kernel.gpr_count; info->kernel_cache_hits += hit[k]; } |
| 196 | } |
| 197 | for (unsigned k = 0; k < KK_N && rc == OMEGA_GPU_DATTN_OK; k++) { |
| 198 | if (!omega_bw_gpr_declared_ok(L[k].gpr_count, ks[k]->kernel.gpr_count)) { omega_gpu_session_set_error("declared registers below peak + 2: refusing to launch"); rc = OMEGA_GPU_DATTN_CHIP_FAIL; } |
| 199 | else if (rec) { if (omega_gpu_cmdlist_add(rec, &L[k]) < 0) rc = OMEGA_GPU_DATTN_CHIP_FAIL; } |
| 200 | else if (omega_gpu_session_launch(&L[k], info ? &info->elapsed_ns[k] : NULL, NULL) != 0) rc = OMEGA_GPU_DATTN_CHIP_FAIL; |
| 201 | } |
| 202 | if (rc == OMEGA_GPU_DATTN_OK && host) { |
| 203 | if (omega_gpu_session_sync_to_cpu(&g.out) != 0) rc = OMEGA_GPU_DATTN_CHIP_FAIL; |
| 204 | else { |
| 205 | const float *od = g.out.cpu; |
| 206 | size_t unchanged = 0; |
| 207 | for (size_t i = 0; i < (size_t)m * OUTW; i++) if (od[i] == POISON_F32) unchanged++; |
| 208 | if (unchanged) { omega_gpu_session_set_error("prefill attn256: output poison survived"); rc = OMEGA_GPU_DATTN_CHIP_FAIL; } |
| 209 | else memcpy(out, od, (size_t)m * OUTW * 4u); |
| 210 | } |
| 211 | } |
| 212 | UNLOCK(); |
| 213 | #undef UNLOCK |
| 214 | if (info) info->call_ns = omega_gpu_session_now_ns() - t0; |
| 215 | return rc; |
| 216 | } |
| 217 | |
| 218 | int omega_gpu_prefill_attn256(OmegaGpuDecodeAttn256Cache *c, uint32_t layer, uint32_t pos0, uint32_t m, |
| 219 | const float *qkv, float eps, float out_scale, float *out, OmegaGpuPrefillAttn256Info *info) { |
| 220 | if (!qkv) return OMEGA_GPU_DATTN_BAD_ARGS; |
| 221 | return core(c, layer, pos0, m, qkv, out, 0, 0, eps, out_scale, info, NULL); |
| 222 | } |
| 223 | int omega_gpu_prefill_attn256_dev(OmegaGpuDecodeAttn256Cache *c, uint32_t layer, uint32_t pos0, uint32_t m, |
| 224 | uint64_t qkv_va, float eps, float out_scale, uint64_t out_va, OmegaGpuPrefillAttn256Info *info) { |
| 225 | return core(c, layer, pos0, m, NULL, NULL, qkv_va, out_va, eps, out_scale, info, NULL); |
| 226 | } |
| 227 | int omega_gpu_prefill_attn256_record(OmegaGpuCmdList *cl, OmegaGpuDecodeAttn256Cache *c, uint32_t layer, uint32_t pos0, |
| 228 | uint32_t m, uint64_t qkv_va, float eps, float out_scale, uint64_t out_va) { |
| 229 | if (!cl) return OMEGA_GPU_DATTN_BAD_ARGS; |
| 230 | return core(c, layer, pos0, m, NULL, NULL, qkv_va, out_va, eps, out_scale, NULL, cl); |
| 231 | } |