Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/src/omega_gpu_decode_attn_api.c # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / src / omega_gpu_decode_attn_api.c` 285 lines · 17108 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 #include "omega_attn_tables.h" 3 /* 4 * omega_gpu_decode_attn_api.c: resident KV cache, RoPE table and launch for the one-launch decode attention (the 5 * double-precision host reference lives in tests/gpu_decode_attn_test.c so the library links without libm). See 6 * attention (see the header). Session use mirrors omega_gpu_fp8_api.c: every buffer from omega_gpu_session_alloc 7 * (GPU-uncached, host-visible), sync_to_gpu after host writes, sync_to_cpu before host reads, one 8 * omega_gpu_session_launch per call. 9 */ 10 #include "omega_gpu_decode_attn_api.h" 11 #include "omega_host_policy.h" 12 #include "omega_api_plan.h" 13 #include "omega_gpu_session.h" 14 #include "omega_gpu_fmt.h" 15 #include "omega_gpu_hostmath.h" 16 #include 17 #include "omega_gpu_cmdlist.h" 18 19 #define MARKER_TIMEOUT_MS 20000ull 20 #define POISON_F32 (-999.0f) 21 22 struct OmegaGpuDecodeAttnCache { 23 uint32_t layers, max_pos, max_splits; 24 double theta; 25 uint64_t plane_bytes; /* one K or V plane of a layer: 4 * max_pos * 256 + slack */ 26 OmegaGpuMem kv[OMEGA_GPU_DATTN_MAX_LAYERS]; /* [K plane | V plane] per layer */ 27 OmegaGpuMem rope, norms, part, cnt, qkv, out; 28 }; 29 30 typedef struct { 31 bool valid; 32 OmegaDecodeAttnSpec spec; 33 OmegaBlackwellKernel kernel; 34 OmegaGpuMem code; 35 } Slot; 36 37 static struct { 38 bool hooked; 39 Slot slot; 40 uint32_t depth, per_split; 41 int mutant; 42 } g; 43 static int g_rec; /* 1 while a record call runs (lock held): kernel_for must not evict */ 44 45 static void slot_clear_locked(void) { if (g.slot.valid) omega_gpu_session_free(&g.slot.code); av_memset(&g.slot, 0, sizeof g.slot); } 46 static void on_session_close(void) { av_memset(&g.slot, 0, sizeof g.slot); } 47 static OmegaGpuSession *dev_open_locked(void) { 48 if (!g.hooked) { (void)omega_gpu_session_on_close(on_session_close); g.hooked = true; } 49 return omega_gpu_session_open(); 50 } 51 static uint32_t gpr_declared(uint32_t peak) { return omega_bw_gpr_declared(peak); } /* peak + 2 rounded up to 8: the chip hung at peak + 1 (omega_blackwell_codegen.h) */ 52 53 void omega_gpu_decode_attn_set_depth(uint32_t depth) { omega_gpu_session_lock(); g.depth = depth; slot_clear_locked(); omega_gpu_session_unlock(); } 54 void omega_gpu_decode_attn_set_per_split(uint32_t per_split) { omega_gpu_session_lock(); g.per_split = per_split; omega_gpu_session_unlock(); } 55 void omega_gpu_decode_attn_test_set_mutant(int mutant) { omega_gpu_session_lock(); g.mutant = mutant; slot_clear_locked(); omega_gpu_session_unlock(); } 56 const char *omega_gpu_decode_attn_last_error(void) { return omega_gpu_session_last_error(); } 57 const char *omega_gpu_decode_attn_rc_name(int rc) { 58 switch (rc) { 59 case OMEGA_GPU_DATTN_OK: return "OK"; 60 case OMEGA_GPU_DATTN_BAD_ARGS: return "BAD_ARGS"; 61 case OMEGA_GPU_DATTN_TOO_LARGE: return "TOO_LARGE"; 62 case OMEGA_GPU_DATTN_CODEGEN_FAIL: return "CODEGEN_FAIL"; 63 case OMEGA_GPU_DATTN_CHIP_FAIL: return "CHIP_FAIL"; 64 default: return "?"; 65 } 66 } 67 68 /* ---- host math ---- */ 69 uint16_t omega_dattn_bf16_rne(float f) { 70 uint32_t x; av_memcpy(&x, &f, 4); 71 uint64_t r = 0; 72 if (omega_ds_round(&r, x) != OMEGA_OK) __builtin_trap(); 73 return (uint16_t)r; 74 } 75 float omega_dattn_bf16_f32(uint16_t b) { uint32_t x = (uint32_t)b << 16; float f; av_memcpy(&f, &x, 4); return f; } 76 void omega_dattn_rope_row(uint32_t pos, double theta, float *row128) { 77 av_ropegen(row128, pos, theta, 128, 128, 1); 78 } 79 /* ---- cache ---- */ 80 int omega_gpu_decode_attn_cache_alloc(uint32_t layers, uint32_t max_pos, double rope_theta, OmegaGpuDecodeAttnCache **out) { 81 if (!out) return OMEGA_GPU_DATTN_BAD_ARGS; 82 *out = NULL; 83 uint64_t theta;av_memcpy(&theta,&rope_theta,sizeof theta); 84 uint64_t p[16]={layers,max_pos,theta}; 85 int plan_rc=ap_plan(AP_DA,0,p);if(plan_rc)return plan_rc; 86 OmegaGpuDecodeAttnCache *c = omega_gpu_host_calloc(1, sizeof *c); 87 if (!c) return OMEGA_GPU_DATTN_CHIP_FAIL; 88 c->layers = layers; c->max_pos = max_pos; c->theta = rope_theta; 89 c->max_splits=p[8];c->plane_bytes=p[9]; 90 int rc = OMEGA_GPU_DATTN_OK; 91 omega_gpu_session_lock(); 92 if (!dev_open_locked()) rc = OMEGA_GPU_DATTN_CHIP_FAIL; 93 for (uint32_t L = 0; L < layers && rc == OMEGA_GPU_DATTN_OK; L++) if (omega_gpu_session_alloc((size_t)(2 * c->plane_bytes), &c->kv[L]) != 0) rc = OMEGA_GPU_DATTN_CHIP_FAIL; 94 if (rc == OMEGA_GPU_DATTN_OK && (omega_gpu_session_alloc(p[10], &c->rope) != 0 || 95 omega_gpu_session_alloc(p[11], &c->norms) != 0 || 96 omega_gpu_session_alloc(p[12], &c->part) != 0 || 97 omega_gpu_session_alloc(64, &c->cnt) != 0 || 98 omega_gpu_session_alloc((size_t)OMEGA_GPU_DATTN_QKV_WORDS * 4, &c->qkv) != 0 || 99 omega_gpu_session_alloc((size_t)OMEGA_GPU_DATTN_Q_HEADS * 128 * 4, &c->out) != 0)) rc = OMEGA_GPU_DATTN_CHIP_FAIL; 100 if (rc == OMEGA_GPU_DATTN_OK) { 101 float *r = c->rope.cpu; 102 av_ropegen(r, 0, rope_theta, 128, 128, max_pos); 103 av_memset(c->cnt.cpu, 0, 64); 104 av_memset(c->norms.cpu, 0, (size_t)layers * 2 * 128 * 4); 105 for (uint32_t L = 0; L < layers; L++) av_memset(c->kv[L].cpu, 0, (size_t)(2 * c->plane_bytes)); 106 if (omega_gpu_session_sync_to_gpu(&c->rope) != 0 || omega_gpu_session_sync_to_gpu(&c->cnt) != 0 || omega_gpu_session_sync_to_gpu(&c->norms) != 0) rc = OMEGA_GPU_DATTN_CHIP_FAIL; 107 for (uint32_t L = 0; L < layers && rc == OMEGA_GPU_DATTN_OK; L++) if (omega_gpu_session_sync_to_gpu(&c->kv[L]) != 0) rc = OMEGA_GPU_DATTN_CHIP_FAIL; 108 } 109 omega_gpu_session_unlock(); 110 if (rc != OMEGA_GPU_DATTN_OK) { omega_gpu_decode_attn_cache_free(c); return rc; } 111 *out = c; 112 return OMEGA_GPU_DATTN_OK; 113 } 114 void omega_gpu_decode_attn_cache_free(OmegaGpuDecodeAttnCache *c) { 115 if (!c) return; 116 omega_gpu_session_lock(); 117 for (uint32_t L = 0; L < c->layers; L++) if (c->kv[L].cpu) omega_gpu_session_free(&c->kv[L]); 118 OmegaGpuMem *ms[] = { &c->rope, &c->norms, &c->part, &c->cnt, &c->qkv, &c->out }; 119 for (unsigned i = 0; i < 6; i++) if (ms[i]->cpu) omega_gpu_session_free(ms[i]); 120 omega_gpu_session_unlock(); 121 omega_gpu_host_free(c); 122 } 123 int omega_gpu_decode_attn_set_norms(OmegaGpuDecodeAttnCache *c, uint32_t layer, const float *q_norm, const float *k_norm) { 124 if (!c || !q_norm || !k_norm) return OMEGA_GPU_DATTN_BAD_ARGS; 125 uint64_t p[16]={layer,c->layers,0,0,1,1};int pr=ap_plan(AP_DA,3,p);if(pr)return pr; 126 omega_gpu_session_lock(); 127 float *n = (float *)c->norms.cpu + (size_t)layer * 256; 128 av_memcpy(n, q_norm, 512); av_memcpy(n + 128, k_norm, 512); 129 int rc = omega_gpu_session_sync_to_gpu(&c->norms) == 0 ? OMEGA_GPU_DATTN_OK : OMEGA_GPU_DATTN_CHIP_FAIL; 130 omega_gpu_session_unlock(); 131 return rc; 132 } 133 static uint16_t *row_ptr(OmegaGpuDecodeAttnCache *c, uint32_t layer, uint32_t kv_head, uint32_t pos, int plane) { 134 return (uint16_t *)((uint8_t *)c->kv[layer].cpu + (plane ? c->plane_bytes : 0) + ((size_t)kv_head * c->max_pos + pos) * OMEGA_DATTN_ROW_BYTES); 135 } 136 int omega_gpu_decode_attn_cache_write(OmegaGpuDecodeAttnCache *c, uint32_t layer, uint32_t kv_head, uint32_t pos, uint32_t count, const uint16_t *k, const uint16_t *v) { 137 if (!c || !k || !v) return OMEGA_GPU_DATTN_BAD_ARGS; 138 uint64_t p[16]={layer,c->layers,kv_head,pos,count,c->max_pos};int pr=ap_plan(AP_DA,3,p);if(pr)return pr; 139 omega_gpu_session_lock(); 140 av_memcpy(row_ptr(c, layer, kv_head, pos, 0), k, (size_t)count * 256); av_memcpy(row_ptr(c, layer, kv_head, pos, 1), v, (size_t)count * 256); 141 int rc = omega_gpu_session_sync_to_gpu(&c->kv[layer]) == 0 ? OMEGA_GPU_DATTN_OK : OMEGA_GPU_DATTN_CHIP_FAIL; 142 omega_gpu_session_unlock(); 143 return rc; 144 } 145 int omega_gpu_decode_attn_cache_read(OmegaGpuDecodeAttnCache *c, uint32_t layer, uint32_t kv_head, uint32_t pos, uint16_t *k, uint16_t *v) { 146 if (!c || !k || !v) return OMEGA_GPU_DATTN_BAD_ARGS; 147 uint64_t p[16]={layer,c->layers,kv_head,pos,1,c->max_pos};int pr=ap_plan(AP_DA,3,p);if(pr)return pr; 148 omega_gpu_session_lock(); 149 int rc = omega_gpu_session_sync_to_cpu(&c->kv[layer]) == 0 ? OMEGA_GPU_DATTN_OK : OMEGA_GPU_DATTN_CHIP_FAIL; 150 if (rc == OMEGA_GPU_DATTN_OK) { av_memcpy(k, row_ptr(c, layer, kv_head, pos, 0), 256); av_memcpy(v, row_ptr(c, layer, kv_head, pos, 1), 256); } 151 omega_gpu_session_unlock(); 152 return rc; 153 } 154 155 /* ---- kernel ---- */ 156 static int kernel_for(const OmegaDecodeAttnSpec *spec, Slot **out, bool *hit) { 157 Slot *s = &g.slot; 158 if (s->valid && memcmp(&s->spec, spec, sizeof *spec) == 0) { *out = s; *hit = true; return OMEGA_GPU_DATTN_OK; } 159 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; } 160 slot_clear_locked(); 161 BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); 162 if (!prog) { omega_gpu_session_set_error("decode attn codegen: host memory"); return OMEGA_GPU_DATTN_CODEGEN_FAIL; } 163 int cg = omega_blackwell_codegen_decode_attn_prog(spec, prog); 164 size_t need = cg == 0 ? omega_bw_program_code_bytes(prog) : 0; 165 if (cg != 0 || need == 0) { omega_gpu_host_free(prog); omega_gpu_session_set_error("decode attn codegen refused the spec"); return OMEGA_GPU_DATTN_CODEGEN_FAIL; } 166 uint64_t budget[16]={gpr_declared(prog->regalloc.peak_gpr_usage)}; 167 if(ap_plan(AP_DA,4,budget)){omega_gpu_host_free(prog);omega_gpu_session_set_error("decode attn: register file");return OMEGA_GPU_DATTN_TOO_LARGE;} 168 OmegaGpuMem code; 169 if (omega_gpu_session_alloc_code(need, &code) != 0) { omega_gpu_host_free(prog); return OMEGA_GPU_DATTN_CHIP_FAIL; } 170 OmegaBlackwellKernel kernel; 171 cg = omega_blackwell_kernel_encode_into(prog, code.cpu, need, &kernel); 172 omega_gpu_host_free(prog); 173 if (cg != 0 || !kernel.code || kernel.code_size != need) { omega_gpu_session_free(&code); omega_gpu_session_set_error("decode attn encode"); return OMEGA_GPU_DATTN_CODEGEN_FAIL; } 174 if (omega_gpu_session_sync_to_gpu(&code) != 0) { omega_gpu_session_free(&code); return OMEGA_GPU_DATTN_CHIP_FAIL; } 175 av_memset(s, 0, sizeof *s); 176 s->valid = true; s->spec = *spec; s->kernel = kernel; s->code = code; 177 *out = s; *hit = false; 178 return OMEGA_GPU_DATTN_OK; 179 } 180 181 /* rec non-NULL (record path, caller holds the session lock): qkv / out are the device buffers dev_qkv / dev_out, the 182 * launch is appended to rec instead of submitted, nothing is staged, poisoned or read back. The split counters are not 183 * zeroed by the host here: the kernel's last CTA of each KV head resets its own (header); omega_gpu_decode_attn_counters 184 * checks them after the list ran. */ 185 static int dattn_core(OmegaGpuDecodeAttnCache *c, uint32_t layer, uint32_t pos, const float *qkv, float eps, float out_scale, 186 float *out, OmegaGpuDecodeAttnInfo *info, OmegaGpuCmdList *rec, uint64_t dev_qkv, uint64_t dev_out) { 187 uint64_t t0 = omega_gpu_session_now_ns(); 188 if (info) av_memset(info, 0, sizeof *info); 189 if (!c || (rec ? (!dev_qkv || !dev_out) : (!qkv || !out))) return OMEGA_GPU_DATTN_BAD_ARGS; 190 uint64_t span[16]={layer,c->layers,0,pos,1,c->max_pos};int span_rc=ap_plan(AP_DA,3,span);if(span_rc)return span_rc; 191 uint64_t p[16]={pos,g.per_split,c->max_splits,g.depth}; 192 int plan_rc=ap_plan(AP_DA,2,p);if(plan_rc)return plan_rc; 193 const uint32_t per=p[8],splits=p[9]; 194 OmegaDecodeAttnSpec spec; av_memset(&spec, 0, sizeof spec); 195 spec.depth=p[10];spec.mutant=g.mutant; 196 197 if (!rec) omega_gpu_session_lock(); 198 #define UNLOCK() do { if (!rec) omega_gpu_session_unlock(); } while (0) 199 int rc = OMEGA_GPU_DATTN_OK; 200 if (!dev_open_locked()) { UNLOCK(); return OMEGA_GPU_DATTN_CHIP_FAIL; } 201 Slot *ks = NULL; bool hit = false; 202 g_rec = rec != NULL; 203 rc = kernel_for(&spec, &ks, &hit); 204 g_rec = 0; 205 if (rc != OMEGA_GPU_DATTN_OK) { UNLOCK(); return rc; } 206 float *od = c->out.cpu; 207 if (!rec) { 208 av_memcpy(c->qkv.cpu, qkv, (size_t)OMEGA_GPU_DATTN_QKV_WORDS * 4); 209 av_fillf32(od, 32 * 128, POISON_F32); 210 av_memset(c->cnt.cpu, 0, 64); 211 if (omega_gpu_session_sync_to_gpu(&c->qkv) != 0 || omega_gpu_session_sync_to_gpu(&c->out) != 0 || omega_gpu_session_sync_to_gpu(&c->cnt) != 0) rc = OMEGA_GPU_DATTN_CHIP_FAIL; 212 } 213 214 uint32_t args[32]; 215 const uint64_t kc = c->kv[layer].va, vc = kc + c->plane_bytes, nm = c->norms.va + (uint64_t)layer * 1024; 216 const uint64_t qkv_va = rec ? dev_qkv : c->qkv.va, out_va = rec ? dev_out : c->out.va; 217 float fe = eps, fo = out_scale, fq = (float)(0.`08838834764831845` * 1.`4426950408889634`); /* log2(e) / sqrt(128) */ 218 uint32_t ue, uo, uq; av_memcpy(&ue, &fe, 4); av_memcpy(&uo, &fo, 4); av_memcpy(&uq, &fq, 4); 219 const uint64_t pack[32] = { qkv_va, kc, vc, c->rope.va, nm, nm + 512, out_va, c->part.va, c->cnt.va, 220 pos, per, splits, (uint32_t)(c->max_pos * OMEGA_DATTN_ROW_BYTES), ue, uo, uq }; 221 if (av_args(args, pack, 9, OMEGA_DATTN_ARGS_WORDS)) rc = OMEGA_GPU_DATTN_CHIP_FAIL; 222 if (info) { 223 info->layer = layer; info->pos = pos; info->splits = splits; info->per_split = per; info->depth = spec.depth; 224 info->grid_x = splits; info->grid_y = OMEGA_GPU_DATTN_KV_HEADS; info->threads = OMEGA_DATTN_CHUNK; 225 info->kv_bytes=p[11]; 226 info->kernel_cache_hit = hit; 227 } 228 if (rc == OMEGA_GPU_DATTN_OK) { 229 OmegaGpuLaunch L = { 230 .code_va = ks->code.va, .gpr_count = gpr_declared(ks->kernel.gpr_count), 231 .threads_x = OMEGA_DATTN_CHUNK, .threads_y = 1, .grid_x = splits, .grid_y = OMEGA_GPU_DATTN_KV_HEADS, 232 .num_elements = (size_t)32 * 128, 233 .args = args, .n_args = OMEGA_DATTN_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS, 234 .shared_bytes = OMEGA_DATTN_SHARED_BYTES, 235 }; 236 if (!omega_bw_gpr_declared_ok(L.gpr_count, ks->kernel.gpr_count)) { omega_gpu_session_set_error("declared registers below peak + 2: refusing to launch"); rc = OMEGA_GPU_DATTN_CHIP_FAIL; } 237 else if (rec) { if (omega_gpu_cmdlist_add(rec, &L) < 0) rc = OMEGA_GPU_DATTN_CHIP_FAIL; } 238 else if (omega_gpu_session_launch(&L, info ? &info->elapsed_ns : NULL, info ? &info->completion_marker : NULL) != 0) rc = OMEGA_GPU_DATTN_CHIP_FAIL; 239 } 240 if (rc == OMEGA_GPU_DATTN_OK && !rec && (omega_gpu_session_sync_to_cpu(&c->out) != 0 || omega_gpu_session_sync_to_cpu(&c->cnt) != 0)) rc = OMEGA_GPU_DATTN_CHIP_FAIL; 241 if (rc == OMEGA_GPU_DATTN_OK && !rec) { 242 size_t unchanged = 0; 243 unchanged = av_countf32(od, 32 * 128, POISON_F32); 244 const uint32_t *cn = c->cnt.cpu; 245 if (unchanged) { omega_gpu_session_set_error("decode attn: output poison survived"); rc = OMEGA_GPU_DATTN_CHIP_FAIL; } 246 else if (av_any32(cn, 4)) { omega_gpu_session_set_error("decode attn: a split counter did not return to zero"); rc = OMEGA_GPU_DATTN_CHIP_FAIL; } 247 else av_memcpy(out, od, (size_t)32 * 128 * 4); 248 } 249 UNLOCK(); 250 #undef UNLOCK 251 if (info) info->call_ns = omega_gpu_session_now_ns() - t0; 252 return rc; 253 } 254 255 /* ---- read-only view for the prefill attention (omega_gpu_prefill_attn_api.c) ---- */ 256 int omega_gpu_decode_attn_cache_view(const OmegaGpuDecodeAttnCache *c, uint32_t layer, OmegaGpuDecodeAttnCacheView *v) { 257 if (!c || !v) return OMEGA_GPU_DATTN_BAD_ARGS; 258 uint64_t p[16]={layer,c->layers,0,0,1,1};int pr=ap_plan(AP_DA,3,p);if(pr)return pr; 259 v->layers = c->layers; v->max_pos = c->max_pos; 260 v->k_va = c->kv[layer].va; v->v_va = c->kv[layer].va + c->plane_bytes; 261 v->rope_va = c->rope.va; 262 v->q_norm_va = c->norms.va + (uint64_t)layer * 1024u; v->k_norm_va = v->q_norm_va + 512u; 263 return OMEGA_GPU_DATTN_OK; 264 } 265 266 int omega_gpu_decode_attn(OmegaGpuDecodeAttnCache *c, uint32_t layer, uint32_t pos, const float *qkv, float eps, float out_scale, float *out, OmegaGpuDecodeAttnInfo *info) { 267 return dattn_core(c, layer, pos, qkv, eps, out_scale, out, info, NULL, 0, 0); 268 } 269 270 /* Record path (OWN-30B W-L2a decode graph): see the header. Caller holds omega_gpu_session_lock(). */ 271 int omega_gpu_decode_attn_record(OmegaGpuCmdList *cl, OmegaGpuDecodeAttnCache *c, uint32_t layer, uint32_t pos, uint64_t qkv_va, 272 float eps, float out_scale, uint64_t out_va) { 273 if (!cl) return OMEGA_GPU_DATTN_BAD_ARGS; 274 return dattn_core(c, layer, pos, NULL, eps, out_scale, NULL, NULL, cl, qkv_va, out_va); 275 } 276 277 /* After a list ran (caller holds the lock): the four split counters must be back at zero (a non-zero word means a CTA 278 * never arrived). OK or CHIP_FAIL. */ 279 int omega_gpu_decode_attn_counters_check(OmegaGpuDecodeAttnCache *c) { 280 if (!c || !c->cnt.cpu) return OMEGA_GPU_DATTN_BAD_ARGS; 281 if (omega_gpu_session_sync_to_cpu(&c->cnt) != 0) return OMEGA_GPU_DATTN_CHIP_FAIL; 282 const uint32_t *cn = c->cnt.cpu; 283 if (cn[0] | cn[1] | cn[2] | cn[3]) { omega_gpu_session_set_error("decode attn: a split counter did not return to zero"); return OMEGA_GPU_DATTN_CHIP_FAIL; } 284 return OMEGA_GPU_DATTN_OK; 285 }