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_decode_attn_api.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * omega_gpu_decode_attn_api.c: resident KV cache, RoPE table and launch for the one-launch decode attention (the |
| 19 | * double-precision host reference lives in tests/gpu_decode_attn_test.c so the library links without libm). See |
| 20 | * attention (see the header). Session use mirrors omega_gpu_fp8_api.c: every buffer from omega_gpu_session_alloc |
| 21 | * (GPU-uncached, host-visible), sync_to_gpu after host writes, sync_to_cpu before host reads, one |
| 22 | * omega_gpu_session_launch per call. |
| 23 | */ |
| 24 | #include "omega_gpu_decode_attn_api.h" |
| 25 | #include "omega_gpu_session.h" |
| 26 | #include "omega_gpu_fmt.h" |
| 27 | #include "omega_gpu_hostmath.h" |
| 28 | #include <string.h> |
| 29 | #include "omega_gpu_cmdlist.h" |
| 30 | |
| 31 | #define MARKER_TIMEOUT_MS 20000ull |
| 32 | #define POISON_F32 (-999.0f) |
| 33 | |
| 34 | struct OmegaGpuDecodeAttnCache { |
| 35 | uint32_t layers, max_pos, max_splits; |
| 36 | double theta; |
| 37 | uint64_t plane_bytes; /* one K or V plane of a layer: 4 * max_pos * 256 + slack */ |
| 38 | OmegaGpuMem kv[OMEGA_GPU_DATTN_MAX_LAYERS]; /* [K plane | V plane] per layer */ |
| 39 | OmegaGpuMem rope, norms, part, cnt, qkv, out; |
| 40 | }; |
| 41 | |
| 42 | typedef struct { |
| 43 | bool valid; |
| 44 | OmegaDecodeAttnSpec spec; |
| 45 | OmegaBlackwellKernel kernel; |
| 46 | OmegaGpuMem code; |
| 47 | } Slot; |
| 48 | |
| 49 | static struct { |
| 50 | bool hooked; |
| 51 | Slot slot; |
| 52 | uint32_t depth, per_split; |
| 53 | int mutant; |
| 54 | } g; |
| 55 | static int g_rec; /* 1 while a record call runs (lock held): kernel_for must not evict */ |
| 56 | |
| 57 | static void slot_clear_locked(void) { if (g.slot.valid) omega_gpu_session_free(&g.slot.code); memset(&g.slot, 0, sizeof g.slot); } |
| 58 | static void on_session_close(void) { memset(&g.slot, 0, sizeof g.slot); } |
| 59 | static OmegaGpuSession *dev_open_locked(void) { |
| 60 | if (!g.hooked) { (void)omega_gpu_session_on_close(on_session_close); g.hooked = true; } |
| 61 | return omega_gpu_session_open(); |
| 62 | } |
| 63 | 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) */ |
| 64 | |
| 65 | void omega_gpu_decode_attn_set_depth(uint32_t depth) { omega_gpu_session_lock(); g.depth = depth; slot_clear_locked(); omega_gpu_session_unlock(); } |
| 66 | 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(); } |
| 67 | void omega_gpu_decode_attn_test_set_mutant(int mutant) { omega_gpu_session_lock(); g.mutant = mutant; slot_clear_locked(); omega_gpu_session_unlock(); } |
| 68 | const char *omega_gpu_decode_attn_last_error(void) { return omega_gpu_session_last_error(); } |
| 69 | const char *omega_gpu_decode_attn_rc_name(int rc) { |
| 70 | switch (rc) { |
| 71 | case OMEGA_GPU_DATTN_OK: return "OK"; |
| 72 | case OMEGA_GPU_DATTN_BAD_ARGS: return "BAD_ARGS"; |
| 73 | case OMEGA_GPU_DATTN_TOO_LARGE: return "TOO_LARGE"; |
| 74 | case OMEGA_GPU_DATTN_CODEGEN_FAIL: return "CODEGEN_FAIL"; |
| 75 | case OMEGA_GPU_DATTN_CHIP_FAIL: return "CHIP_FAIL"; |
| 76 | default: return "?"; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /* ---- host math ---- */ |
| 81 | uint16_t omega_dattn_bf16_rne(float f) { |
| 82 | uint32_t x; memcpy(&x, &f, 4); |
| 83 | uint32_t r = x + 0x7fffu + ((x >> 16) & 1u); |
| 84 | return (uint16_t)(r >> 16); |
| 85 | } |
| 86 | float omega_dattn_bf16_f32(uint16_t b) { uint32_t x = (uint32_t)b << 16; float f; memcpy(&f, &x, 4); return f; } |
| 87 | void omega_dattn_rope_row(uint32_t pos, double theta, float *row128) { |
| 88 | for (unsigned i = 0; i < 64; i++) { |
| 89 | double inv = omega_hostmath_pow(theta, -(double)(2 * i) / 128.0), ang = (double)pos * inv, s, c; |
| 90 | omega_hostmath_sincos(ang, &s, &c); |
| 91 | row128[i] = (float)c; row128[64 + i] = (float)s; |
| 92 | } |
| 93 | } |
| 94 | /* ---- cache ---- */ |
| 95 | int omega_gpu_decode_attn_cache_alloc(uint32_t layers, uint32_t max_pos, double rope_theta, OmegaGpuDecodeAttnCache **out) { |
| 96 | if (!out) return OMEGA_GPU_DATTN_BAD_ARGS; |
| 97 | *out = NULL; |
| 98 | if (layers == 0 || layers > OMEGA_GPU_DATTN_MAX_LAYERS || max_pos == 0 || max_pos > OMEGA_GPU_DATTN_MAX_POS || max_pos % OMEGA_DATTN_CHUNK || !(rope_theta > 0)) return OMEGA_GPU_DATTN_BAD_ARGS; |
| 99 | OmegaGpuDecodeAttnCache *c = omega_gpu_host_calloc(1, sizeof *c); |
| 100 | if (!c) return OMEGA_GPU_DATTN_CHIP_FAIL; |
| 101 | c->layers = layers; c->max_pos = max_pos; c->theta = rope_theta; |
| 102 | c->max_splits = max_pos / OMEGA_DATTN_CHUNK; |
| 103 | c->plane_bytes = (uint64_t)OMEGA_GPU_DATTN_KV_HEADS * max_pos * OMEGA_DATTN_ROW_BYTES + OMEGA_DATTN_PLANE_SLACK; |
| 104 | int rc = OMEGA_GPU_DATTN_OK; |
| 105 | omega_gpu_session_lock(); |
| 106 | if (!dev_open_locked()) rc = OMEGA_GPU_DATTN_CHIP_FAIL; |
| 107 | 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; |
| 108 | if (rc == OMEGA_GPU_DATTN_OK && (omega_gpu_session_alloc((size_t)max_pos * OMEGA_DATTN_ROPE_ROW_BYTES, &c->rope) != 0 || |
| 109 | omega_gpu_session_alloc((size_t)layers * 2 * 128 * 4, &c->norms) != 0 || |
| 110 | omega_gpu_session_alloc((size_t)OMEGA_GPU_DATTN_KV_HEADS * c->max_splits * OMEGA_DATTN_PARTIAL_WORDS * 4, &c->part) != 0 || |
| 111 | omega_gpu_session_alloc(64, &c->cnt) != 0 || |
| 112 | omega_gpu_session_alloc((size_t)OMEGA_GPU_DATTN_QKV_WORDS * 4, &c->qkv) != 0 || |
| 113 | omega_gpu_session_alloc((size_t)OMEGA_GPU_DATTN_Q_HEADS * 128 * 4, &c->out) != 0)) rc = OMEGA_GPU_DATTN_CHIP_FAIL; |
| 114 | if (rc == OMEGA_GPU_DATTN_OK) { |
| 115 | float *r = c->rope.cpu; |
| 116 | for (uint32_t p = 0; p < max_pos; p++) omega_dattn_rope_row(p, rope_theta, r + (size_t)p * 128); |
| 117 | memset(c->cnt.cpu, 0, 64); |
| 118 | memset(c->norms.cpu, 0, (size_t)layers * 2 * 128 * 4); |
| 119 | for (uint32_t L = 0; L < layers; L++) memset(c->kv[L].cpu, 0, (size_t)(2 * c->plane_bytes)); |
| 120 | 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; |
| 121 | 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; |
| 122 | } |
| 123 | omega_gpu_session_unlock(); |
| 124 | if (rc != OMEGA_GPU_DATTN_OK) { omega_gpu_decode_attn_cache_free(c); return rc; } |
| 125 | *out = c; |
| 126 | return OMEGA_GPU_DATTN_OK; |
| 127 | } |
| 128 | void omega_gpu_decode_attn_cache_free(OmegaGpuDecodeAttnCache *c) { |
| 129 | if (!c) return; |
| 130 | omega_gpu_session_lock(); |
| 131 | for (uint32_t L = 0; L < c->layers; L++) if (c->kv[L].cpu) omega_gpu_session_free(&c->kv[L]); |
| 132 | OmegaGpuMem *ms[] = { &c->rope, &c->norms, &c->part, &c->cnt, &c->qkv, &c->out }; |
| 133 | for (unsigned i = 0; i < 6; i++) if (ms[i]->cpu) omega_gpu_session_free(ms[i]); |
| 134 | omega_gpu_session_unlock(); |
| 135 | omega_gpu_host_free(c); |
| 136 | } |
| 137 | int omega_gpu_decode_attn_set_norms(OmegaGpuDecodeAttnCache *c, uint32_t layer, const float *q_norm, const float *k_norm) { |
| 138 | if (!c || layer >= c->layers || !q_norm || !k_norm) return OMEGA_GPU_DATTN_BAD_ARGS; |
| 139 | omega_gpu_session_lock(); |
| 140 | float *n = (float *)c->norms.cpu + (size_t)layer * 256; |
| 141 | memcpy(n, q_norm, 512); memcpy(n + 128, k_norm, 512); |
| 142 | int rc = omega_gpu_session_sync_to_gpu(&c->norms) == 0 ? OMEGA_GPU_DATTN_OK : OMEGA_GPU_DATTN_CHIP_FAIL; |
| 143 | omega_gpu_session_unlock(); |
| 144 | return rc; |
| 145 | } |
| 146 | static uint16_t *row_ptr(OmegaGpuDecodeAttnCache *c, uint32_t layer, uint32_t kv_head, uint32_t pos, int plane) { |
| 147 | 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); |
| 148 | } |
| 149 | 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) { |
| 150 | if (!c || layer >= c->layers || kv_head >= 4 || pos >= c->max_pos || count == 0 || count > c->max_pos - pos || !k || !v) return OMEGA_GPU_DATTN_BAD_ARGS; |
| 151 | omega_gpu_session_lock(); |
| 152 | memcpy(row_ptr(c, layer, kv_head, pos, 0), k, (size_t)count * 256); memcpy(row_ptr(c, layer, kv_head, pos, 1), v, (size_t)count * 256); |
| 153 | int rc = omega_gpu_session_sync_to_gpu(&c->kv[layer]) == 0 ? OMEGA_GPU_DATTN_OK : OMEGA_GPU_DATTN_CHIP_FAIL; |
| 154 | omega_gpu_session_unlock(); |
| 155 | return rc; |
| 156 | } |
| 157 | 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) { |
| 158 | if (!c || layer >= c->layers || kv_head >= 4 || pos >= c->max_pos || !k || !v) return OMEGA_GPU_DATTN_BAD_ARGS; |
| 159 | omega_gpu_session_lock(); |
| 160 | int rc = omega_gpu_session_sync_to_cpu(&c->kv[layer]) == 0 ? OMEGA_GPU_DATTN_OK : OMEGA_GPU_DATTN_CHIP_FAIL; |
| 161 | if (rc == OMEGA_GPU_DATTN_OK) { memcpy(k, row_ptr(c, layer, kv_head, pos, 0), 256); memcpy(v, row_ptr(c, layer, kv_head, pos, 1), 256); } |
| 162 | omega_gpu_session_unlock(); |
| 163 | return rc; |
| 164 | } |
| 165 | |
| 166 | /* ---- kernel ---- */ |
| 167 | static int kernel_for(const OmegaDecodeAttnSpec *spec, Slot **out, bool *hit) { |
| 168 | Slot *s = &g.slot; |
| 169 | if (s->valid && memcmp(&s->spec, spec, sizeof *spec) == 0) { *out = s; *hit = true; return OMEGA_GPU_DATTN_OK; } |
| 170 | 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; } |
| 171 | slot_clear_locked(); |
| 172 | BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); |
| 173 | if (!prog) { omega_gpu_session_set_error("decode attn codegen: host memory"); return OMEGA_GPU_DATTN_CODEGEN_FAIL; } |
| 174 | int cg = omega_blackwell_codegen_decode_attn_prog(spec, prog); |
| 175 | size_t need = cg == 0 ? omega_bw_program_code_bytes(prog) : 0; |
| 176 | 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; } |
| 177 | if ((uint64_t)OMEGA_DATTN_CHUNK * gpr_declared(prog->regalloc.peak_gpr_usage) > 65536u) { omega_gpu_host_free(prog); omega_gpu_session_set_error("decode attn: register file"); return OMEGA_GPU_DATTN_TOO_LARGE; } |
| 178 | OmegaGpuMem code; |
| 179 | if (omega_gpu_session_alloc_code(need, &code) != 0) { omega_gpu_host_free(prog); return OMEGA_GPU_DATTN_CHIP_FAIL; } |
| 180 | OmegaBlackwellKernel kernel; |
| 181 | cg = omega_blackwell_kernel_encode_into(prog, code.cpu, need, &kernel); |
| 182 | omega_gpu_host_free(prog); |
| 183 | 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; } |
| 184 | if (omega_gpu_session_sync_to_gpu(&code) != 0) { omega_gpu_session_free(&code); return OMEGA_GPU_DATTN_CHIP_FAIL; } |
| 185 | memset(s, 0, sizeof *s); |
| 186 | s->valid = true; s->spec = *spec; s->kernel = kernel; s->code = code; |
| 187 | *out = s; *hit = false; |
| 188 | return OMEGA_GPU_DATTN_OK; |
| 189 | } |
| 190 | |
| 191 | /* rec non-NULL (record path, caller holds the session lock): qkv / out are the device buffers dev_qkv / dev_out, the |
| 192 | * launch is appended to rec instead of submitted, nothing is staged, poisoned or read back. The split counters are not |
| 193 | * zeroed by the host here: the kernel's last CTA of each KV head resets its own (header); omega_gpu_decode_attn_counters |
| 194 | * checks them after the list ran. */ |
| 195 | static int dattn_core(OmegaGpuDecodeAttnCache *c, uint32_t layer, uint32_t pos, const float *qkv, float eps, float out_scale, |
| 196 | float *out, OmegaGpuDecodeAttnInfo *info, OmegaGpuCmdList *rec, uint64_t dev_qkv, uint64_t dev_out) { |
| 197 | uint64_t t0 = omega_gpu_session_now_ns(); |
| 198 | if (info) memset(info, 0, sizeof *info); |
| 199 | if (!c || layer >= c->layers || pos >= c->max_pos || (rec ? (!dev_qkv || !dev_out) : (!qkv || !out))) return OMEGA_GPU_DATTN_BAD_ARGS; |
| 200 | const uint32_t per = g.per_split ? g.per_split : OMEGA_GPU_DATTN_PER_SPLIT; |
| 201 | if (per % OMEGA_DATTN_CHUNK) return OMEGA_GPU_DATTN_BAD_ARGS; |
| 202 | const uint32_t splits = (pos + per) / per; /* ceil((pos + 1) / per) */ |
| 203 | if (splits > c->max_splits) return OMEGA_GPU_DATTN_TOO_LARGE; |
| 204 | OmegaDecodeAttnSpec spec; memset(&spec, 0, sizeof spec); |
| 205 | spec.depth = g.depth ? g.depth : OMEGA_GPU_DATTN_DEPTH; spec.mutant = g.mutant; |
| 206 | |
| 207 | if (!rec) omega_gpu_session_lock(); |
| 208 | #define UNLOCK() do { if (!rec) omega_gpu_session_unlock(); } while (0) |
| 209 | int rc = OMEGA_GPU_DATTN_OK; |
| 210 | if (!dev_open_locked()) { UNLOCK(); return OMEGA_GPU_DATTN_CHIP_FAIL; } |
| 211 | Slot *ks = NULL; bool hit = false; |
| 212 | g_rec = rec != NULL; |
| 213 | rc = kernel_for(&spec, &ks, &hit); |
| 214 | g_rec = 0; |
| 215 | if (rc != OMEGA_GPU_DATTN_OK) { UNLOCK(); return rc; } |
| 216 | float *od = c->out.cpu; |
| 217 | if (!rec) { |
| 218 | memcpy(c->qkv.cpu, qkv, (size_t)OMEGA_GPU_DATTN_QKV_WORDS * 4); |
| 219 | for (size_t i = 0; i < 32 * 128; i++) od[i] = POISON_F32; |
| 220 | memset(c->cnt.cpu, 0, 64); |
| 221 | 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; |
| 222 | } |
| 223 | |
| 224 | uint32_t args[OMEGA_DATTN_ARGS_WORDS]; |
| 225 | const uint64_t kc = c->kv[layer].va, vc = kc + c->plane_bytes, nm = c->norms.va + (uint64_t)layer * 1024; |
| 226 | const uint64_t qkv_va = rec ? dev_qkv : c->qkv.va, out_va = rec ? dev_out : c->out.va; |
| 227 | float fe = eps, fo = out_scale, fq = (float)(0.08838834764831845 * 1.4426950408889634); /* log2(e) / sqrt(128) */ |
| 228 | uint32_t ue, uo, uq; memcpy(&ue, &fe, 4); memcpy(&uo, &fo, 4); memcpy(&uq, &fq, 4); |
| 229 | #define A64(i, v) do { args[i] = (uint32_t)(v); args[(i) + 1] = (uint32_t)((v) >> 32); } while (0) |
| 230 | A64(0, qkv_va); A64(2, kc); A64(4, vc); A64(6, c->rope.va); A64(8, nm); A64(10, nm + 512); A64(12, out_va); A64(14, c->part.va); A64(16, c->cnt.va); |
| 231 | #undef A64 |
| 232 | args[18] = pos; args[19] = per; args[20] = splits; args[21] = (uint32_t)(c->max_pos * OMEGA_DATTN_ROW_BYTES); args[22] = ue; args[23] = uo; args[24] = uq; |
| 233 | if (info) { |
| 234 | info->layer = layer; info->pos = pos; info->splits = splits; info->per_split = per; info->depth = spec.depth; |
| 235 | info->grid_x = splits; info->grid_y = OMEGA_GPU_DATTN_KV_HEADS; info->threads = OMEGA_DATTN_CHUNK; |
| 236 | info->kv_bytes = 2ull * OMEGA_GPU_DATTN_KV_HEADS * (pos + 1ull) * OMEGA_DATTN_ROW_BYTES; |
| 237 | info->kernel_cache_hit = hit; |
| 238 | } |
| 239 | if (rc == OMEGA_GPU_DATTN_OK) { |
| 240 | OmegaGpuLaunch L = { |
| 241 | .code_va = ks->code.va, .gpr_count = gpr_declared(ks->kernel.gpr_count), |
| 242 | .threads_x = OMEGA_DATTN_CHUNK, .threads_y = 1, .grid_x = splits, .grid_y = OMEGA_GPU_DATTN_KV_HEADS, |
| 243 | .num_elements = (size_t)32 * 128, |
| 244 | .args = args, .n_args = OMEGA_DATTN_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS, |
| 245 | .shared_bytes = OMEGA_DATTN_SHARED_BYTES, |
| 246 | }; |
| 247 | 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; } |
| 248 | else if (rec) { if (omega_gpu_cmdlist_add(rec, &L) < 0) rc = OMEGA_GPU_DATTN_CHIP_FAIL; } |
| 249 | else if (omega_gpu_session_launch(&L, info ? &info->elapsed_ns : NULL, info ? &info->completion_marker : NULL) != 0) rc = OMEGA_GPU_DATTN_CHIP_FAIL; |
| 250 | } |
| 251 | 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; |
| 252 | if (rc == OMEGA_GPU_DATTN_OK && !rec) { |
| 253 | size_t unchanged = 0; |
| 254 | for (size_t i = 0; i < 32 * 128; i++) if (od[i] == POISON_F32) unchanged++; |
| 255 | const uint32_t *cn = c->cnt.cpu; |
| 256 | if (unchanged) { omega_gpu_session_set_error("decode attn: output poison survived"); rc = OMEGA_GPU_DATTN_CHIP_FAIL; } |
| 257 | else if (cn[0] | cn[1] | cn[2] | cn[3]) { omega_gpu_session_set_error("decode attn: a split counter did not return to zero"); rc = OMEGA_GPU_DATTN_CHIP_FAIL; } |
| 258 | else memcpy(out, od, (size_t)32 * 128 * 4); |
| 259 | } |
| 260 | UNLOCK(); |
| 261 | #undef UNLOCK |
| 262 | if (info) info->call_ns = omega_gpu_session_now_ns() - t0; |
| 263 | return rc; |
| 264 | } |
| 265 | |
| 266 | /* ---- read-only view for the prefill attention (omega_gpu_prefill_attn_api.c) ---- */ |
| 267 | int omega_gpu_decode_attn_cache_view(const OmegaGpuDecodeAttnCache *c, uint32_t layer, OmegaGpuDecodeAttnCacheView *v) { |
| 268 | if (!c || !v || layer >= c->layers) return OMEGA_GPU_DATTN_BAD_ARGS; |
| 269 | v->layers = c->layers; v->max_pos = c->max_pos; |
| 270 | v->k_va = c->kv[layer].va; v->v_va = c->kv[layer].va + c->plane_bytes; |
| 271 | v->rope_va = c->rope.va; |
| 272 | v->q_norm_va = c->norms.va + (uint64_t)layer * 1024u; v->k_norm_va = v->q_norm_va + 512u; |
| 273 | return OMEGA_GPU_DATTN_OK; |
| 274 | } |
| 275 | |
| 276 | 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) { |
| 277 | return dattn_core(c, layer, pos, qkv, eps, out_scale, out, info, NULL, 0, 0); |
| 278 | } |
| 279 | |
| 280 | /* Record path (OWN-30B W-L2a decode graph): see the header. Caller holds omega_gpu_session_lock(). */ |
| 281 | int omega_gpu_decode_attn_record(OmegaGpuCmdList *cl, OmegaGpuDecodeAttnCache *c, uint32_t layer, uint32_t pos, uint64_t qkv_va, |
| 282 | float eps, float out_scale, uint64_t out_va) { |
| 283 | if (!cl) return OMEGA_GPU_DATTN_BAD_ARGS; |
| 284 | return dattn_core(c, layer, pos, NULL, eps, out_scale, NULL, NULL, cl, qkv_va, out_va); |
| 285 | } |
| 286 | |
| 287 | /* After a list ran (caller holds the lock): the four split counters must be back at zero (a non-zero word means a CTA |
| 288 | * never arrived). OK or CHIP_FAIL. */ |
| 289 | int omega_gpu_decode_attn_counters_check(OmegaGpuDecodeAttnCache *c) { |
| 290 | if (!c || !c->cnt.cpu) return OMEGA_GPU_DATTN_BAD_ARGS; |
| 291 | if (omega_gpu_session_sync_to_cpu(&c->cnt) != 0) return OMEGA_GPU_DATTN_CHIP_FAIL; |
| 292 | const uint32_t *cn = c->cnt.cpu; |
| 293 | 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; } |
| 294 | return OMEGA_GPU_DATTN_OK; |
| 295 | } |