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