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/omega-mv/blob/main/src/omega_gpu_prefill_attn_api.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / omega_gpu_prefill_attn_api.c` 400 lines ยท 23224 bytes Raw Blame History 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_prefill_attn_api.c: chunked launches of the tensor-core causal prefill attention (see the header). 19 * Session use mirrors omega_gpu_decode_attn_api.c: buffers from omega_gpu_session_alloc (GPU-uncached, host-visible), 20 * sync_to_gpu after host writes, sync_to_cpu before host reads, one omega_gpu_session_launch per chunk. 21 */ 22 #include "omega_gpu_prefill_attn_api.h" 23 #include "omega_blackwell_codegen_tc.h" 24 #include "omega_gpu_session.h" 25 #include "omega_gpu_fmt.h" 26 #include 27 #if defined(__has_include) 28 #if __has_include("omega_gpu_prefill.h") 29 #include "omega_gpu_prefill.h" /* GPU-cached device buffers + copy kernel (integration builds) */ 30 #include "omega_blackwell_codegen_pf.h" 31 #define PATTN_HAVE_PF 1 32 #endif 33 #endif 34 #ifndef PATTN_HAVE_PF 35 #define PATTN_HAVE_PF 0 36 #endif 37 38 #define MARKER_TIMEOUT_MS 20000ull 39 #define POISON_F32 (-999.0f) 40 41 typedef struct { 42 bool valid; 43 OmegaPrefillAttnSpec spec; 44 OmegaBlackwellKernel kernel; 45 OmegaGpuMem code; 46 } Slot; 47 48 #define MAX_LAUNCH_LOG 256u 49 50 static struct { 51 bool hooked; 52 Slot slot[3]; /* one per OMEGA_PATTN_MODE_* */ 53 uint32_t warps; 54 int mutant; 55 int fused; /* 1: 64-token fused chunks (cut 1 path); 0 (default): APPEND + ATTEND launch pairs */ 56 bool have_bufs; 57 uint32_t buf_rows; /* host-entry staging capacity in tokens */ 58 OmegaGpuMem qkv, out; 59 uint64_t launch_ns[MAX_LAUNCH_LOG]; 60 uint32_t n_launch; /* launches of the last call (log keeps the first MAX_LAUNCH_LOG) */ 61 int mirror_off; /* 1: ATTEND reads the uncached cache (cut 3); 0 (default with PATTN_HAVE_PF): GPU-cached K/V mirror */ 62 int mirror_per_call; /* TEST: 1 forgets the mirrored rows before every call (cut 4 behaviour: history copied every time) */ 63 struct Mir { uint64_t key; uint32_t max_pos, valid; uint64_t plane; OmegaGpuMem mem; } mir[OMEGA_GPU_PATTN_MAX_MIRRORS]; 64 uint32_t n_mir; /* persistent mirrors, one per (cache, layer), keyed by the layer's K plane VA */ 65 } g; 66 67 static void slot_clear_locked(void) { 68 for (unsigned i = 0; i < 3; i++) { if (g.slot[i].valid) omega_gpu_session_free(&g.slot[i].code); memset(&g.slot[i], 0, sizeof g.slot[i]); } 69 } 70 static void on_session_close(void) { memset(g.mir, 0, sizeof g.mir); g.n_mir = 0; memset(g.slot, 0, sizeof g.slot); g.have_bufs = false; g.buf_rows = 0; memset(&g.qkv, 0, sizeof g.qkv); memset(&g.out, 0, sizeof g.out); } 71 static OmegaGpuSession *dev_open_locked(void) { 72 if (!g.hooked) { (void)omega_gpu_session_on_close(on_session_close); g.hooked = true; } 73 return omega_gpu_session_open(); 74 } 75 76 void omega_gpu_prefill_attn_set_warps(uint32_t warps) { omega_gpu_session_lock(); g.warps = warps; slot_clear_locked(); omega_gpu_session_unlock(); } 77 void omega_gpu_prefill_attn_test_set_mutant(int mutant) { omega_gpu_session_lock(); g.mutant = mutant; slot_clear_locked(); omega_gpu_session_unlock(); } 78 void omega_gpu_prefill_attn_set_fused(int fused) { omega_gpu_session_lock(); g.fused = fused ? 1 : 0; omega_gpu_session_unlock(); } 79 void omega_gpu_prefill_attn_set_kv_mirror(int on) { omega_gpu_session_lock(); g.mirror_off = on ? 0 : 1; omega_gpu_session_unlock(); } 80 int omega_gpu_prefill_attn_kv_mirror_available(void) { return PATTN_HAVE_PF; } 81 uint32_t omega_gpu_prefill_attn_test_launch_ns(uint64_t *ns, uint32_t cap) { 82 omega_gpu_session_lock(); 83 uint32_t n = g.n_launch < MAX_LAUNCH_LOG ? g.n_launch : MAX_LAUNCH_LOG; 84 for (uint32_t i = 0; i < n && i < cap; i++) ns[i] = g.launch_ns[i]; 85 uint32_t total = g.n_launch; 86 omega_gpu_session_unlock(); 87 return total; 88 } 89 void omega_gpu_prefill_attn_release(void) { 90 omega_gpu_session_lock(); 91 slot_clear_locked(); 92 if (g.have_bufs) { omega_gpu_session_free(&g.qkv); omega_gpu_session_free(&g.out); g.have_bufs = false; g.buf_rows = 0; } 93 omega_gpu_session_unlock(); 94 #if PATTN_HAVE_PF 95 for (uint32_t i = 0; i < g.n_mir; i++) omega_gpu_pf_free(&g.mir[i].mem); 96 memset(g.mir, 0, sizeof g.mir); g.n_mir = 0; 97 #endif 98 } 99 100 #if PATTN_HAVE_PF 101 /* Persistent mirror of one (cache, layer): the decode cache's exact layout ([K plane | V plane], plane = v_va - k_va, 102 * row (kv_head, pos) at plane + (kv_head * max_pos + pos) * 256) in GPU-cached device memory. Rows 0 .. valid-1 equal 103 * the cache. Zero-filled when allocated, so rows past the last key of a 32-key block are finite (their P is exactly 0, 104 * and 0 * NaN would poison O). Lock NOT held (the pf calls take it). */ 105 static struct Mir *mirror_get(const OmegaGpuDecodeAttnCacheView *v) { 106 for (uint32_t i = 0; i < g.n_mir; i++) 107 if (g.mir[i].key == v->k_va && g.mir[i].max_pos == v->max_pos) return &g.mir[i]; 108 if (g.n_mir >= OMEGA_GPU_PATTN_MAX_MIRRORS) { omega_gpu_session_set_error("prefill attn: too many KV mirrors"); return NULL; } 109 struct Mir *m = &g.mir[g.n_mir]; 110 memset(m, 0, sizeof *m); 111 const uint64_t plane = v->v_va - v->k_va, bytes = 2u * plane; 112 if (v->v_va <= v->k_va || (plane & 15u)) { omega_gpu_session_set_error("prefill attn: cache planes"); return NULL; } 113 if (omega_gpu_pf_alloc_dev(bytes, &m->mem) != OMEGA_GPU_PF_OK) { memset(m, 0, sizeof *m); return NULL; } 114 const size_t step = (size_t)16u << 20; /* zero-fill in 16 MB uploads */ 115 void *z = omega_gpu_host_calloc(1, step); 116 int ok = z != NULL; 117 for (uint64_t off = 0; ok && off < bytes; off += step) { 118 const size_t part = bytes - off < step ? (size_t)(bytes - off) : step; 119 ok = omega_gpu_pf_upload(&m->mem, off, z, part) == OMEGA_GPU_PF_OK; 120 } 121 omega_gpu_host_free(z); 122 if (!ok) { omega_gpu_pf_free(&m->mem); memset(m, 0, sizeof *m); omega_gpu_session_set_error("prefill attn: KV mirror zero-fill"); return NULL; } 123 m->key = v->k_va; m->max_pos = v->max_pos; m->plane = plane; m->valid = 0; 124 g.n_mir++; 125 return m; 126 } 127 static struct Mir *mirror_find(const OmegaGpuDecodeAttnCacheView *v) { 128 for (uint32_t i = 0; i < g.n_mir; i++) if (g.mir[i].key == v->k_va && g.mir[i].max_pos == v->max_pos) return &g.mir[i]; 129 return NULL; 130 } 131 /* lock held: the copy kernel of the prefill module (same key and generator as omega_gpu_pf_copy_va) */ 132 static int gen_copy(const OmegaPfKey *key, void *prog) { (void)key; return omega_blackwell_codegen_pf_copy_prog(prog); } 133 static int copy_locked(uint64_t dst, uint64_t src, size_t bytes, uint64_t *ns) { 134 if ((bytes & 15u) || (dst & 15u) || (src & 15u)) return OMEGA_GPU_PF_BAD_ARGS; 135 OmegaPfKey key; memset(&key, 0, sizeof key); key.kind = 1u; /* KIND_COPY of omega_gpu_prefill.c */ 136 OmegaPfKernel k; int rc = omega_gpu_pf_open_locked(); 137 if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_kernel_locked(&key, gen_copy, &k); 138 if (rc != OMEGA_GPU_PF_OK || bytes == 0) return rc; 139 const uint32_t units = (uint32_t)(bytes / 16u), grid = (units + OMEGA_PF_COPY_THREADS - 1u) / OMEGA_PF_COPY_THREADS; 140 uint32_t args[5] = { (uint32_t)dst, (uint32_t)(dst >> 32), (uint32_t)src, (uint32_t)(src >> 32), units }; 141 return omega_gpu_pf_launch_locked(&k, OMEGA_PF_COPY_THREADS, grid, 1, 0, args, 5, ns); 142 } 143 #endif 144 145 size_t omega_gpu_prefill_attn_encode(const OmegaPrefillAttnSpec *spec, uint8_t *code, size_t cap, uint32_t *gprs) { 146 BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); 147 if (!prog) return 0; 148 size_t need = 0; 149 if (omega_blackwell_codegen_prefill_attn_prog(spec, prog) == 0) need = omega_bw_program_code_bytes(prog); 150 OmegaBlackwellKernel k; 151 if (need == 0 || need > cap || omega_blackwell_kernel_encode_into(prog, code, cap, &k) != 0) { omega_gpu_host_free(prog); return 0; } 152 if (gprs) *gprs = k.gpr_count; 153 omega_gpu_host_free(prog); 154 return need; 155 } 156 157 static int kernel_for(const OmegaPrefillAttnSpec *spec, Slot **out, bool *hit) { 158 Slot *s = &g.slot[spec->mode]; 159 if (s->valid && memcmp(&s->spec, spec, sizeof *spec) == 0) { *out = s; *hit = true; return OMEGA_GPU_DATTN_OK; } 160 if (s->valid) omega_gpu_session_free(&s->code); 161 memset(s, 0, sizeof *s); 162 BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); 163 if (!prog) { omega_gpu_session_set_error("prefill attn codegen: host memory"); return OMEGA_GPU_DATTN_CODEGEN_FAIL; } 164 int cg = omega_blackwell_codegen_prefill_attn_prog(spec, prog); 165 size_t need = cg == 0 ? omega_bw_program_code_bytes(prog) : 0; 166 if (cg != 0 || need == 0) { omega_gpu_host_free(prog); omega_gpu_session_set_error("prefill attn codegen refused the spec"); return OMEGA_GPU_DATTN_CODEGEN_FAIL; } 167 if ((uint64_t)32u * spec->warps * omega_bw_gpr_declared(prog->regalloc.peak_gpr_usage) > 65536u) { omega_gpu_host_free(prog); omega_gpu_session_set_error("prefill 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("prefill 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 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 /* host-entry staging for `rows` tokens (grown on demand, contents not kept) */ 182 static int bufs_locked(uint32_t rows) { 183 if (g.have_bufs && g.buf_rows >= rows) return 0; 184 if (g.have_bufs) { omega_gpu_session_free(&g.qkv); omega_gpu_session_free(&g.out); g.have_bufs = false; g.buf_rows = 0; } 185 if (omega_gpu_session_alloc((size_t)rows * OMEGA_PATTN_QKV_WORDS * 4u, &g.qkv) != 0) return -1; 186 if (omega_gpu_session_alloc((size_t)rows * OMEGA_PATTN_OUT_WORDS * 4u, &g.out) != 0) { omega_gpu_session_free(&g.qkv); return -1; } 187 g.have_bufs = true; g.buf_rows = rows; 188 return 0; 189 } 190 191 typedef struct { OmegaGpuDecodeAttnCacheView v; uint32_t ue, uo, uq; OmegaPrefillAttnSpec base; struct Mir *mir; } Run; 192 193 static int prepare(OmegaGpuDecodeAttnCache *c, uint32_t layer, uint32_t pos0, uint32_t m, float eps, float out_scale, Run *r) { 194 memset(r, 0, sizeof *r); 195 if (!c || m == 0 || omega_gpu_decode_attn_cache_view(c, layer, &r->v) != OMEGA_GPU_DATTN_OK) return OMEGA_GPU_DATTN_BAD_ARGS; 196 if (pos0 > r->v.max_pos || m > r->v.max_pos - pos0) return OMEGA_GPU_DATTN_BAD_ARGS; 197 r->base.warps = g.warps ? g.warps : OMEGA_GPU_PATTN_WARPS; r->base.mutant = g.mutant; 198 if (!omega_pattn_spec_ok(&r->base)) return OMEGA_GPU_DATTN_BAD_ARGS; 199 float qk_f = (float)(0.`08838834764831845` * 1.`4426950408889634`); /* log2(e) / sqrt(128) */ 200 memcpy(&r->ue, &eps, 4); memcpy(&r->uo, &out_scale, 4); memcpy(&r->uq, &qk_f, 4); 201 return OMEGA_GPU_DATTN_OK; 202 } 203 204 /* lock held, device open: one launch of `mode` over tokens pos0 .. pos0+mc-1 whose qkv / out rows start at the VAs */ 205 /* K/V planes a launch reads (kc, vc, kvs) and, for APPEND with a mirror, also writes (kc2, vc2, kvs2) */ 206 typedef struct { uint64_t kc, vc, kc2, vc2; uint32_t kvs, kvs2; } KvPlanes; 207 static int launch_locked(const Run *r, uint32_t mode, const KvPlanes *kv, uint64_t qkv_va, uint64_t out_va, uint32_t p0, uint32_t mc, 208 OmegaGpuPrefillAttnInfo *info) { 209 OmegaPrefillAttnSpec spec = r->base; spec.mode = mode; spec.mirror = kv->kc2 ? 1u : 0u; 210 Slot *ks = NULL; bool hit = false; 211 int rc = kernel_for(&spec, &ks, &hit); 212 if (rc != OMEGA_GPU_DATTN_OK) return rc; 213 const uint32_t W = spec.warps; 214 uint32_t args[OMEGA_PATTN_ARGS_WORDS]; 215 memset(args, 0, sizeof args); 216 #define A64(i, val) do { uint64_t _x = (val); args[i] = (uint32_t)_x; args[(i) + 1] = (uint32_t)(_x >> 32); } while (0) 217 A64(0, qkv_va); A64(2, kv->kc); A64(4, kv->vc); A64(6, r->v.rope_va); A64(8, r->v.q_norm_va); A64(10, r->v.k_norm_va); A64(12, out_va); A64(20, kv->kc2); A64(22, kv->vc2); 218 #undef A64 219 args[14] = p0; args[15] = mc; args[16] = kv->kvs; args[24] = kv->kvs2; args[17] = r->ue; args[18] = r->uo; args[19] = r->uq; 220 OmegaGpuLaunch L = { 221 .code_va = ks->code.va, .gpr_count = omega_bw_gpr_declared(ks->kernel.gpr_count), 222 .threads_x = 32u * W, .threads_y = 1, .grid_x = (mc + W - 1u) / W, .grid_y = OMEGA_GPU_DATTN_KV_HEADS, 223 .num_elements = (size_t)mc * OMEGA_PATTN_OUT_WORDS, 224 .args = args, .n_args = OMEGA_PATTN_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS, 225 .shared_bytes = OMEGA_PATTN_SHARED_BYTES, 226 }; 227 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"); return OMEGA_GPU_DATTN_CHIP_FAIL; } 228 uint64_t el = 0; uint32_t marker = 0; 229 if (omega_gpu_session_launch(&L, &el, &marker) != 0) return OMEGA_GPU_DATTN_CHIP_FAIL; 230 if (g.n_launch < MAX_LAUNCH_LOG) g.launch_ns[g.n_launch] = el; 231 g.n_launch++; 232 if (info) { 233 if (info->chunks == 0) { info->grid_x = L.grid_x; info->grid_y = L.grid_y; info->threads = L.threads_x; info->splits = 1; info->per_split = p0 + mc; info->kernel_cache_hit = hit; } 234 else info->kernel_cache_hit &= hit; 235 info->chunks++; info->elapsed_ns += el; 236 } 237 return OMEGA_GPU_DATTN_OK; 238 } 239 240 /* lock held, device open: all the launches for tokens pos0 .. pos0+m-1, rows at the VAs (device memory, row strides 241 * 5120 / 4096 f32). Split (default): one APPEND then one ATTEND launch over all m tokens. Fused: 64-token chunks. */ 242 static int run_locked(const Run *r, uint32_t pos0, uint32_t m, uint64_t qkv_va, uint64_t out_va, OmegaGpuPrefillAttnInfo *info) { 243 int rc = OMEGA_GPU_DATTN_OK; 244 const KvPlanes cache = { .kc = r->v.k_va, .vc = r->v.v_va, .kvs = r->v.max_pos * OMEGA_DATTN_ROW_BYTES }; 245 if (!g.fused) { 246 #if PATTN_HAVE_PF 247 if (!g.mirror_off) { 248 /* persistent GPU-cached mirror (same layout as the cache): rows valid .. pos0-1 that the cache gained since the 249 * last call (decode steps) are copied in, rows >= pos0 are (re)written by APPEND to both places, ATTEND reads 250 * only the mirror (L2 reuse across CTAs). Rows 0 .. min(valid, pos0)-1 are trusted: see the header contract. */ 251 struct Mir *mm = r->mir ? mirror_find(&r->v) : NULL; /* re-found under the lock (forget may have moved it) */ 252 if (!mm) { omega_gpu_session_set_error("prefill attn: KV mirror not allocated"); return OMEGA_GPU_DATTN_CHIP_FAIL; } 253 if (g.mirror_per_call) mm->valid = 0; 254 const uint32_t t = mm->valid < pos0 ? mm->valid : pos0; 255 mm->valid = 0; /* until this call succeeds */ 256 const KvPlanes mir = { .kc = mm->mem.va, .vc = mm->mem.va + mm->plane, .kvs = cache.kvs }; 257 for (uint32_t h = 0; h < OMEGA_GPU_DATTN_KV_HEADS && t < pos0 && rc == OMEGA_GPU_DATTN_OK; h++) { 258 uint64_t ns = 0; 259 const uint64_t off = (uint64_t)h * cache.kvs + (uint64_t)t * OMEGA_DATTN_ROW_BYTES; 260 const size_t bytes = (size_t)(pos0 - t) * OMEGA_DATTN_ROW_BYTES; 261 int prc = copy_locked(mir.kc + off, cache.kc + off, bytes, &ns); 262 if (prc == OMEGA_GPU_PF_OK) prc = copy_locked(mir.vc + off, cache.vc + off, bytes, &ns); 263 if (prc != OMEGA_GPU_PF_OK) rc = OMEGA_GPU_DATTN_CHIP_FAIL; 264 if (g.n_launch < MAX_LAUNCH_LOG) g.launch_ns[g.n_launch] = ns; 265 g.n_launch++; 266 if (info) info->elapsed_ns += ns; 267 } 268 KvPlanes app = cache; app.kc2 = mir.kc; app.vc2 = mir.vc; app.kvs2 = mir.kvs; 269 if (rc == OMEGA_GPU_DATTN_OK) rc = launch_locked(r, OMEGA_PATTN_MODE_APPEND, &app, qkv_va, out_va, pos0, m, info); 270 if (rc == OMEGA_GPU_DATTN_OK) rc = launch_locked(r, OMEGA_PATTN_MODE_ATTEND, &mir, qkv_va, out_va, pos0, m, info); 271 if (rc == OMEGA_GPU_DATTN_OK) mm->valid = pos0 + m; 272 return rc; 273 } 274 #endif 275 rc = launch_locked(r, OMEGA_PATTN_MODE_APPEND, &cache, qkv_va, out_va, pos0, m, info); 276 if (rc == OMEGA_GPU_DATTN_OK) rc = launch_locked(r, OMEGA_PATTN_MODE_ATTEND, &cache, qkv_va, out_va, pos0, m, info); 277 return rc; 278 } 279 for (uint32_t done = 0; done < m && rc == OMEGA_GPU_DATTN_OK; done += OMEGA_PATTN_CHUNK) { 280 const uint32_t mc = m - done < OMEGA_PATTN_CHUNK ? m - done : OMEGA_PATTN_CHUNK; 281 rc = launch_locked(r, OMEGA_PATTN_MODE_FUSED, &cache, qkv_va + (uint64_t)done * OMEGA_PATTN_QKV_WORDS * 4u, 282 out_va + (uint64_t)done * OMEGA_PATTN_OUT_WORDS * 4u, pos0 + done, mc, info); 283 } 284 return rc; 285 } 286 287 static void info_head(OmegaGpuPrefillAttnInfo *info, uint32_t layer, uint32_t pos0, uint32_t m) { 288 if (!info) return; 289 memset(info, 0, sizeof *info); 290 info->layer = layer; info->pos0 = pos0; info->m = m; 291 } 292 293 int omega_gpu_prefill_attn(OmegaGpuDecodeAttnCache *c, uint32_t layer, uint32_t pos0, uint32_t m, const float *qkv, float eps, 294 float out_scale, float *out, OmegaGpuPrefillAttnInfo *info) { 295 uint64_t t0 = omega_gpu_session_now_ns(); 296 info_head(info, layer, pos0, m); 297 Run r; 298 if (!qkv || !out) return OMEGA_GPU_DATTN_BAD_ARGS; 299 int rc = prepare(c, layer, pos0, m, eps, out_scale, &r); 300 if (rc != OMEGA_GPU_DATTN_OK) return rc; 301 const uint32_t pass = g.fused ? OMEGA_PATTN_CHUNK : OMEGA_GPU_PATTN_HOST_PASS; 302 #if PATTN_HAVE_PF 303 if (!g.fused && !g.mirror_off && !(r.mir = mirror_get(&r.v))) return OMEGA_GPU_DATTN_CHIP_FAIL; 304 #endif 305 306 omega_gpu_session_lock(); 307 if (!dev_open_locked()) { omega_gpu_session_unlock(); return OMEGA_GPU_DATTN_CHIP_FAIL; } 308 g.n_launch = 0; 309 if (bufs_locked(m < pass ? m : pass) != 0) rc = OMEGA_GPU_DATTN_CHIP_FAIL; 310 for (uint32_t done = 0; done < m && rc == OMEGA_GPU_DATTN_OK; done += pass) { 311 const uint32_t mp = m - done < pass ? m - done : pass; 312 memcpy(g.qkv.cpu, qkv + (size_t)done * OMEGA_PATTN_QKV_WORDS, (size_t)mp * OMEGA_PATTN_QKV_WORDS * 4u); 313 float *od = g.out.cpu; 314 for (size_t i = 0; i < (size_t)mp * OMEGA_PATTN_OUT_WORDS; i++) od[i] = POISON_F32; 315 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; break; } 316 rc = run_locked(&r, pos0 + done, mp, g.qkv.va, g.out.va, info); 317 if (rc != OMEGA_GPU_DATTN_OK) break; 318 if (omega_gpu_session_sync_to_cpu(&g.out) != 0) { rc = OMEGA_GPU_DATTN_CHIP_FAIL; break; } 319 size_t unchanged = 0; 320 for (size_t i = 0; i < (size_t)mp * OMEGA_PATTN_OUT_WORDS; i++) if (od[i] == POISON_F32) unchanged++; 321 if (unchanged) { omega_gpu_session_set_error("prefill attn: output poison survived"); rc = OMEGA_GPU_DATTN_CHIP_FAIL; break; } 322 memcpy(out + (size_t)done * OMEGA_PATTN_OUT_WORDS, od, (size_t)mp * OMEGA_PATTN_OUT_WORDS * 4u); 323 } 324 omega_gpu_session_unlock(); 325 if (info) info->call_ns = omega_gpu_session_now_ns() - t0; 326 return rc; 327 } 328 329 int omega_gpu_prefill_attn_dev(OmegaGpuDecodeAttnCache *c, uint32_t layer, uint32_t pos0, uint32_t m, uint64_t qkv_va, float eps, 330 float out_scale, uint64_t out_va, OmegaGpuPrefillAttnInfo *info) { 331 uint64_t t0 = omega_gpu_session_now_ns(); 332 info_head(info, layer, pos0, m); 333 Run r; 334 if (!qkv_va || !out_va || (qkv_va & 15u) || (out_va & 15u)) return OMEGA_GPU_DATTN_BAD_ARGS; 335 int rc = prepare(c, layer, pos0, m, eps, out_scale, &r); 336 if (rc != OMEGA_GPU_DATTN_OK) return rc; 337 #if PATTN_HAVE_PF 338 if (!g.fused && !g.mirror_off && !(r.mir = mirror_get(&r.v))) return OMEGA_GPU_DATTN_CHIP_FAIL; 339 #endif 340 omega_gpu_session_lock(); 341 if (!dev_open_locked()) { omega_gpu_session_unlock(); return OMEGA_GPU_DATTN_CHIP_FAIL; } 342 g.n_launch = 0; 343 rc = run_locked(&r, pos0, m, qkv_va, out_va, info); 344 omega_gpu_session_unlock(); 345 if (info) info->call_ns = omega_gpu_session_now_ns() - t0; 346 return rc; 347 } 348 349 /* ---- persistent K/V mirror: view and contract (see the header) ---- */ 350 int omega_gpu_prefill_attn_kv_mirror_view(const OmegaGpuDecodeAttnCache *c, uint32_t layer, OmegaGpuPrefillAttnMirrorView *out) { 351 OmegaGpuDecodeAttnCacheView v; 352 if (!out) return OMEGA_GPU_DATTN_BAD_ARGS; 353 memset(out, 0, sizeof *out); 354 if (!c || omega_gpu_decode_attn_cache_view(c, layer, &v) != OMEGA_GPU_DATTN_OK) return OMEGA_GPU_DATTN_BAD_ARGS; 355 omega_gpu_session_lock(); 356 #if PATTN_HAVE_PF 357 struct Mir *m = mirror_find(&v); 358 if (m) { out->k_va = m->mem.va; out->v_va = m->mem.va + m->plane; out->kv_stride = v.max_pos * OMEGA_DATTN_ROW_BYTES; out->max_pos = v.max_pos; out->valid = m->valid; } 359 #endif 360 omega_gpu_session_unlock(); 361 return out->k_va ? OMEGA_GPU_DATTN_OK : OMEGA_GPU_DATTN_BAD_ARGS; 362 } 363 static uint32_t layers_of(const OmegaGpuDecodeAttnCache *c) { 364 OmegaGpuDecodeAttnCacheView v; 365 return c && omega_gpu_decode_attn_cache_view(c, 0, &v) == OMEGA_GPU_DATTN_OK ? v.layers : 0; 366 } 367 void omega_gpu_prefill_attn_kv_mirror_invalidate(const OmegaGpuDecodeAttnCache *c, uint32_t layer, uint32_t from_pos) { 368 const uint32_t nl = layers_of(c); 369 omega_gpu_session_lock(); 370 #if PATTN_HAVE_PF 371 for (uint32_t l = 0; l < nl; l++) { 372 if (layer != UINT32_MAX && l != layer) continue; 373 OmegaGpuDecodeAttnCacheView v; 374 if (omega_gpu_decode_attn_cache_view(c, l, &v) != OMEGA_GPU_DATTN_OK) continue; 375 struct Mir *m = mirror_find(&v); 376 if (m && m->valid > from_pos) m->valid = from_pos; 377 } 378 #else 379 (void)layer; (void)from_pos; (void)nl; 380 #endif 381 omega_gpu_session_unlock(); 382 } 383 void omega_gpu_prefill_attn_kv_mirror_forget(const OmegaGpuDecodeAttnCache *c) { 384 #if PATTN_HAVE_PF 385 const uint32_t nl = layers_of(c); 386 for (uint32_t l = 0; l < nl; l++) { 387 OmegaGpuDecodeAttnCacheView v; 388 if (omega_gpu_decode_attn_cache_view(c, l, &v) != OMEGA_GPU_DATTN_OK) continue; 389 omega_gpu_session_lock(); 390 struct Mir *m = mirror_find(&v); 391 OmegaGpuMem mem; memset(&mem, 0, sizeof mem); 392 if (m) { mem = m->mem; *m = g.mir[g.n_mir - 1]; memset(&g.mir[g.n_mir - 1], 0, sizeof g.mir[0]); g.n_mir--; } 393 omega_gpu_session_unlock(); 394 if (mem.va) omega_gpu_pf_free(&mem); 395 } 396 #else 397 (void)c; 398 #endif 399 } 400 void omega_gpu_prefill_attn_test_set_mirror_per_call(int on) { omega_gpu_session_lock(); g.mirror_per_call = on ? 1 : 0; omega_gpu_session_unlock(); }