interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / src / omega_gpu_bf16_gemv_api.c

325 lines · 16971 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2#include "omega_attn_views.h"
3/* omega_gpu_bf16_gemv_api.c: bf16 streaming GEMV (lm_head) on the shared session, OWN-30B 2026-10-13.
4 * See the header. Submission path and buffer policy are the FP8 API's: every buffer both sides touch is
5 * GPU-uncached (nvos.h NVOS32_ATTR2_GPU_CACHEABLE: "For system memory this will not be coherent with direct CPU
6 * mappings"); one launch = omega_gpu_session_launch. */
7#include "omega_gpu_bf16_gemv_api.h"
8#include "omega_host_policy.h"
9#include "omega_api_plan.h"
10#include "omega_gpu_session.h"
11#include "omega_gpu_fmt.h"
12#include "omega_gpu_cmdlist.h"
13#include <string.h>
14
15#define CACHE_SLOTS 8
16#define MARKER_TIMEOUT_MS 20000ull
17#define POISON_F32 (-999.0f)
18#define DEFAULT_CTAS 96u
19
20struct OmegaGpuBf16Tensor {
21 uint32_t n, k;
22 OmegaGpuMem w;
23};
24
25typedef struct {
26 bool valid, pinned;
27 OmegaBf16GemvSpec spec;
28 OmegaBlackwellKernel kernel;
29 OmegaGpuMem code;
30} Slot;
31
32static struct {
33 OmegaGpuScratch x_buf, y_buf;
34 uint32_t rows, warps, depth, ctas; /* overrides, 0 = automatic */
35 int mutant;
36 bool hooked;
37 Slot cache[CACHE_SLOTS];
38 unsigned next;
39} g;
40static int g_rec; /* 1 while a record call runs (lock held): kernel_for must not evict */
41static int g_mask_mutant; /* TEST ONLY: the MASK form's mutant (4 = keep / drop inverted), other kernels untouched */
42
43const char *omega_gpu_bf16_last_error(void) { return omega_gpu_session_last_error(); }
44const char *omega_gpu_bf16_rc_name(int rc) {
45 switch (rc) {
46 case OMEGA_GPU_BF16_OK: return "OK";
47 case OMEGA_GPU_BF16_BAD_ARGS: return "BAD_ARGS";
48 case OMEGA_GPU_BF16_TOO_LARGE: return "TOO_LARGE";
49 case OMEGA_GPU_BF16_CODEGEN_FAIL: return "CODEGEN_FAIL";
50 case OMEGA_GPU_BF16_CHIP_FAIL: return "CHIP_FAIL";
51 case OMEGA_GPU_BF16_NAN_WEIGHT: return "NAN_WEIGHT";
52 default: return "UNKNOWN";
53 }
54}
55
56static void cache_clear_locked(void) {
57 for (unsigned i = 0; i < CACHE_SLOTS; i++) {
58 if (!g.cache[i].valid) continue;
59 omega_gpu_session_free(&g.cache[i].code);
60 av_memset(&g.cache[i], 0, sizeof g.cache[i]);
61 }
62 g.next = 0;
63}
64static void on_session_close(void) {
65 cache_clear_locked();
66 omega_gpu_session_scratch_free(&g.x_buf);
67 omega_gpu_session_scratch_free(&g.y_buf);
68}
69static OmegaGpuSession *dev_open_locked(void) {
70 if (!g.hooked) { (void)omega_gpu_session_on_close(on_session_close); g.hooked = true; }
71 return omega_gpu_session_open();
72}
73void omega_gpu_bf16_cache_clear(void) { omega_gpu_session_lock(); cache_clear_locked(); omega_gpu_session_unlock(); }
74void omega_gpu_bf16_set_shape(uint32_t rows, uint32_t warps, uint32_t depth) {
75 omega_gpu_session_lock(); g.rows = rows; g.warps = warps; g.depth = depth; cache_clear_locked(); omega_gpu_session_unlock();
76}
77void omega_gpu_bf16_set_cta_budget(uint32_t ctas) { omega_gpu_session_lock(); g.ctas = ctas; omega_gpu_session_unlock(); }
78void omega_gpu_bf16_test_set_mutant(int m) { omega_gpu_session_lock(); g.mutant = m; cache_clear_locked(); omega_gpu_session_unlock(); }
79void omega_gpu_bf16_test_set_mask_mutant(int m) { omega_gpu_session_lock(); g_mask_mutant = m; omega_gpu_session_unlock(); }
80
81/* Declared registers: peak + 1 rounded up to 8; a CTA must fit the 65536-register file (chip-found rules, see
82 * omega_gpu_fp8_api.c and ~/handoffs/own30b-K-report.md). */
83static 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) */
84static bool register_file_fits(uint32_t warps, uint32_t peak) {
85 uint32_t decl = gpr_declared(peak);
86 uint64_t p[16]={warps,decl};
87 if (!ap_plan(AP_BF,4,p)) return true;
88 char e[160]; omega_gpu_fmt(e, sizeof e, "bf16 kernel: %u warps x %u declared registers exceed the 65536-register file", warps, decl);
89 omega_gpu_session_set_error(e);
90 return false;
91}
92int omega_gpu_bf16_kernel_fits(const OmegaBf16GemvSpec *spec, uint32_t *declared) {
93 uint32_t peak = 0;
94 if (!spec || omega_bf16_gemv_prog_stats(spec, NULL, &peak) != 0) { omega_gpu_session_set_error("bf16 codegen refused the shape"); return OMEGA_GPU_BF16_CODEGEN_FAIL; }
95 if (declared) *declared = gpr_declared(peak);
96 return register_file_fits(spec->warps, peak) ? OMEGA_GPU_BF16_OK : OMEGA_GPU_BF16_TOO_LARGE;
97}
98
99/* Automatic shape. DEFAULTS_BEGIN: set from the chip sweep (evidence OWN30B-BF16-*); see the report. */
100void omega_gpu_bf16_shape_for(uint32_t k, uint32_t *rows, uint32_t *warps, uint32_t *depth, uint32_t *ctas) {
101 uint64_t p[16]={k,g.rows,g.warps,g.depth,g.ctas};
102 (void)ap_plan(AP_BF,0,p);
103 if(rows)*rows=p[8];
104 if(warps)*warps=p[9];
105 if(depth)*depth=p[10];
106 if(ctas)*ctas=p[11];
107}
108static void spec_for(uint32_t k, OmegaBf16GemvSpec *spec, uint32_t *ctas) {
109 av_memset(spec, 0, sizeof *spec);
110 spec->k = k;
111 omega_gpu_bf16_shape_for(k, &spec->rows, &spec->warps, &spec->depth, ctas);
112 spec->mutant = g.mutant;
113}
114
115static int kernel_for(const OmegaBf16GemvSpec *spec, Slot **out, bool *hit) {
116 for (unsigned i = 0; i < CACHE_SLOTS; i++) {
117 Slot *s = &g.cache[i];
118 if (s->valid && memcmp(&s->spec, spec, sizeof *spec) == 0) { *out = s; *hit = true; return OMEGA_GPU_BF16_OK; }
119 }
120 Slot *s = NULL;
121 for (unsigned i = 0; i < CACHE_SLOTS && !s; i++) {
122 Slot *c = &g.cache[(g.next + i) % CACHE_SLOTS];
123 if (!c->valid || !c->pinned) { s = c; g.next = (unsigned)((c - g.cache) + 1) % CACHE_SLOTS; }
124 }
125 if (!s) { omega_gpu_session_set_error("bf16 kernel cache: every slot holds a prepared kernel"); return OMEGA_GPU_BF16_TOO_LARGE; }
126 if (g_rec && s->valid) { omega_gpu_session_set_error("bf16 record: kernel cache miss would evict a resident kernel (warm up before recording)"); return OMEGA_GPU_BF16_TOO_LARGE; }
127 BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog);
128 if (!prog) { omega_gpu_session_set_error("bf16 codegen: host memory"); return OMEGA_GPU_BF16_CODEGEN_FAIL; }
129 int cg = omega_blackwell_codegen_bf16_gemv_prog(spec, prog);
130 size_t need = cg == 0 ? omega_bw_program_code_bytes(prog) : 0;
131 if (cg != 0 || need == 0) { omega_gpu_host_free(prog); omega_gpu_session_set_error("bf16 codegen refused the shape"); return OMEGA_GPU_BF16_CODEGEN_FAIL; }
132 if (!register_file_fits(spec->warps, prog->regalloc.peak_gpr_usage)) { omega_gpu_host_free(prog); return OMEGA_GPU_BF16_TOO_LARGE; }
133 OmegaGpuMem code;
134 if (omega_gpu_session_alloc_code(need, &code) != 0) { omega_gpu_host_free(prog); return OMEGA_GPU_BF16_CHIP_FAIL; }
135 OmegaBlackwellKernel kernel;
136 cg = omega_blackwell_kernel_encode_into(prog, code.cpu, need, &kernel);
137 omega_gpu_host_free(prog);
138 if (cg != 0 || !kernel.code || kernel.code_size != need) { omega_gpu_session_free(&code); return OMEGA_GPU_BF16_CODEGEN_FAIL; }
139 if (omega_gpu_session_sync_to_gpu(&code) != 0) { omega_gpu_session_free(&code); return OMEGA_GPU_BF16_CHIP_FAIL; }
140 if (s->valid) omega_gpu_session_free(&s->code);
141 av_memset(s, 0, sizeof *s);
142 s->valid = true; s->spec = *spec; s->kernel = kernel; s->code = code;
143 *out = s; *hit = false;
144 return OMEGA_GPU_BF16_OK;
145}
146
147static int prepare_spec(const OmegaBf16GemvSpec *spec) {
148 omega_gpu_session_lock();
149 int rc = OMEGA_GPU_BF16_OK;
150 if (!dev_open_locked()) rc = OMEGA_GPU_BF16_CHIP_FAIL;
151 else { Slot *s = NULL; bool hit = false; rc = kernel_for(spec, &s, &hit); if (rc == OMEGA_GPU_BF16_OK) s->pinned = true; }
152 omega_gpu_session_unlock();
153 return rc;
154}
155int omega_gpu_bf16_prepare(uint32_t k) {
156 OmegaBf16GemvSpec spec; uint32_t ctas; spec_for(k, &spec, &ctas);
157 return prepare_spec(&spec);
158}
159int omega_gpu_bf16_prepare_xbatch(uint32_t k, uint32_t xbatch, uint32_t ystride) {
160 uint64_t p[16]={xbatch,ystride};
161 int rc=ap_plan(AP_BF,3,p);if(rc)return rc;
162 if(p[8])return omega_gpu_bf16_prepare(k);
163 OmegaBf16GemvSpec spec; uint32_t ctas; spec_for(k, &spec, &ctas);
164 spec.xbatch = xbatch; spec.ystride = ystride;
165 return prepare_spec(&spec);
166}
167/* the MASK kernel as gemv_core builds it (rows 1, mask 1), pinned: the exact head's record never compiles mid-token */
168int omega_gpu_bf16_prepare_mask(uint32_t k) {
169 OmegaBf16GemvSpec spec; uint32_t ctas; spec_for(k, &spec, &ctas);
170 spec.rows = 1; spec.mask = 1;
171 return prepare_spec(&spec);
172}
173
174void omega_gpu_bf16_tensor_shape(const OmegaGpuBf16Tensor *t, uint32_t *n, uint32_t *k) {
175 if (n) *n = t ? t->n : 0;
176 if (k) *k = t ? t->k : 0;
177}
178
179int omega_gpu_bf16_tensor_upload(uint32_t n, uint32_t k, const uint16_t *w, OmegaGpuBf16Tensor **out) {
180 if (out) *out = NULL;
181 if (!w || !out) return OMEGA_GPU_BF16_BAD_ARGS;
182 int shape_rc = omega_host_shape(5, n, k, OMEGA_BF16_MAX_K);
183 if (shape_rc) return shape_rc;
184 const size_t count = (size_t)n * k;
185 const size_t bad_weight = av_bffinite(w, count);
186 if (bad_weight) {
187 char e[120]; omega_gpu_fmt(e, sizeof e, "bf16 upload: NaN or infinity at element %zu", bad_weight - 1);
188 omega_gpu_session_set_error(e);
189 return OMEGA_GPU_BF16_NAN_WEIGHT;
190 }
191 OmegaGpuBf16Tensor *t = omega_gpu_host_calloc(1, sizeof *t);
192 if (!t) return OMEGA_GPU_BF16_BAD_ARGS;
193 t->n = n; t->k = k;
194 int rc = OMEGA_GPU_BF16_OK;
195 omega_gpu_session_lock();
196 if (!dev_open_locked() || omega_gpu_session_alloc(count * 2u, &t->w) != 0) rc = OMEGA_GPU_BF16_CHIP_FAIL;
197 omega_gpu_session_unlock();
198 if (rc == OMEGA_GPU_BF16_OK) {
199 av_memcpy(t->w.cpu, w, count * 2u);
200 if (omega_gpu_session_sync_to_gpu(&t->w) != 0) rc = OMEGA_GPU_BF16_CHIP_FAIL;
201 }
202 if (rc != OMEGA_GPU_BF16_OK) { omega_gpu_bf16_tensor_free(t); return rc; }
203 *out = t;
204 return OMEGA_GPU_BF16_OK;
205}
206
207void omega_gpu_bf16_tensor_free(OmegaGpuBf16Tensor *t) {
208 if (!t) return;
209 omega_gpu_session_lock();
210 if (t->w.cpu) omega_gpu_session_free(&t->w);
211 omega_gpu_session_unlock();
212 omega_gpu_host_free(t);
213}
214
215/* rec non-NULL (record path, caller holds the session lock): x and y are the device buffers dev_x / dev_y, the launch is
216 * appended to rec instead of submitted, nothing is staged, poisoned or read back. */
217static int gemv_core(const OmegaGpuBf16Tensor *t, const float *x32, const uint16_t *x16, float *y, OmegaGpuBf16Info *info,
218 OmegaGpuCmdList *rec, uint64_t dev_x, uint64_t dev_y, uint32_t xrows, uint32_t xbatch, uint32_t ystride,
219 uint64_t mask_hi, uint64_t mask_tau) {
220 uint64_t t0 = omega_gpu_session_now_ns();
221 if (info) av_memset(info, 0, sizeof *info);
222 if (rec && (!dev_x || !dev_y)) return OMEGA_GPU_BF16_BAD_ARGS;
223 if (!t || !t->w.cpu || (!rec && ((!x32 && !x16) || !y))) return OMEGA_GPU_BF16_BAD_ARGS;
224 const uint32_t n = t->n, k = t->k;
225 uint64_t p[16]={rec!=NULL,xrows,xbatch,ystride,mask_hi!=0,mask_tau!=0,n};
226 int plan_rc=ap_plan(AP_BF,2,p);if(plan_rc)return plan_rc;
227 OmegaBf16GemvSpec spec; uint32_t ctas; spec_for(k, &spec, &ctas);
228 const int masked=mask_hi!=0;
229 uint64_t launch[16]={xrows,xbatch,ystride,masked,n,spec.rows};
230 (void)ap_plan(AP_BF,6,launch);
231 spec.rows=launch[8];spec.ystride=launch[9];spec.xbatch=launch[10];
232 spec.mask=masked;if(masked&&g_mask_mutant)spec.mutant=g_mask_mutant;
233 uint64_t grid[16]={n,spec.rows,spec.warps,ctas};
234 plan_rc=ap_plan(AP_BF,1,grid);if(plan_rc)return plan_rc;
235 uint32_t grid_x=grid[8],RG=grid[9];
236
237 if (!rec) omega_gpu_session_lock();
238#define UNLOCK() do { if (!rec) omega_gpu_session_unlock(); } while (0)
239 int rc = OMEGA_GPU_BF16_OK;
240 if (!dev_open_locked()) { UNLOCK(); return OMEGA_GPU_BF16_CHIP_FAIL; }
241 if (!rec) {
242 int src = omega_gpu_session_scratch(&g.x_buf, (size_t)k * 4u);
243 if (src == 0) src = omega_gpu_session_scratch(&g.y_buf, (size_t)n * 4u);
244 if (src != 0) { UNLOCK(); return src == OMEGA_GPU_SESSION_SCRATCH_RESERVED ? OMEGA_GPU_BF16_TOO_LARGE : OMEGA_GPU_BF16_CHIP_FAIL; }
245 }
246 Slot *ks = NULL; bool hit = false;
247 g_rec = rec != NULL;
248 rc = kernel_for(&spec, &ks, &hit);
249 g_rec = 0;
250 if (rc != OMEGA_GPU_BF16_OK) { UNLOCK(); return rc; }
251
252 float *xd = g.x_buf.mem.cpu, *yd = g.y_buf.mem.cpu;
253 if (!rec) {
254 if (x32) av_memcpy(xd, x32, (size_t)k * 4u);
255 else av_bfexpand(xd, x16, k);
256 av_fillf32(yd, n, POISON_F32);
257 if (omega_gpu_session_sync_to_gpu(&g.x_buf.mem) != 0 || omega_gpu_session_sync_to_gpu(&g.y_buf.mem) != 0) rc = OMEGA_GPU_BF16_CHIP_FAIL;
258 }
259
260 uint32_t args[32];
261 const uint64_t x_va = rec ? dev_x : g.x_buf.mem.va, y_va = rec ? dev_y : g.y_buf.mem.va;
262 const uint64_t pack[32] = { [0]=t->w.va, [2]=x_va, [4]=y_va, [6]=RG,
263 [7]=(uint64_t)grid_x * spec.warps, [8]=mask_hi, [10]=mask_tau };
264 if (av_layout(args, pack, (1ull<<0)|(1ull<<2)|(1ull<<4)|(1ull<<8)|(1ull<<10))) rc = OMEGA_GPU_BF16_CHIP_FAIL;
265 if (info) {
266 info->n = n; info->k = k; info->rows_per_warp = spec.rows; info->warps_per_cta = spec.warps; info->depth = spec.depth;
267 info->grid_x = grid_x; info->steps = omega_bf16_gemv_steps(&spec);
268 info->weight_bytes = (uint64_t)n * k * 2u;
269 info->kernel_cache_hit = hit;
270 (void)omega_gpu_fmt(info->target_chip, sizeof info->target_chip, "%s", "NVIDIA DGX Spark (Grace Blackwell GB10)");
271 info->sm_architecture = 121;
272 }
273 if (rc == OMEGA_GPU_BF16_OK) {
274 OmegaGpuLaunch L = {
275 .code_va = ks->code.va, .gpr_count = gpr_declared(ks->kernel.gpr_count),
276 .threads_x = 32u * spec.warps, .threads_y = 1, .grid_x = grid_x, .grid_y = launch[11],
277 .num_elements = launch[12],
278 .args = args, .n_args = masked ? OMEGA_BF16_MASK_ARGS_WORDS : OMEGA_BF16_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS,
279 .shared_bytes = omega_bf16_gemv_shared_bytes(&spec),
280 };
281 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_BF16_CHIP_FAIL; }
282 else if (rec) { if (omega_gpu_cmdlist_add(rec, &L) < 0) rc = OMEGA_GPU_BF16_CHIP_FAIL; }
283 else if (omega_gpu_session_launch(&L, info ? &info->elapsed_ns : NULL, info ? &info->completion_marker : NULL) != 0) rc = OMEGA_GPU_BF16_CHIP_FAIL;
284 }
285 if (rc == OMEGA_GPU_BF16_OK && !rec && omega_gpu_session_sync_to_cpu(&g.y_buf.mem) != 0) rc = OMEGA_GPU_BF16_CHIP_FAIL;
286 if (rc == OMEGA_GPU_BF16_OK && !rec) {
287 size_t unchanged = 0;
288 unchanged = av_countf32(yd, n, POISON_F32);
289 if (unchanged) { omega_gpu_session_set_error("output poison survived"); rc = OMEGA_GPU_BF16_CHIP_FAIL; }
290 else av_memcpy(y, yd, (size_t)n * sizeof *y);
291 }
292 UNLOCK();
293 if (info) info->call_ns = omega_gpu_session_now_ns() - t0;
294 return rc;
295}
296#undef UNLOCK
297
298int omega_gpu_bf16_gemv_f32(const OmegaGpuBf16Tensor *t, const float *x, float *y, OmegaGpuBf16Info *info) { return gemv_core(t, x, NULL, y, info, NULL, 0, 0, 0, 0, 0, 0, 0); }
299int omega_gpu_bf16_gemv_bf16(const OmegaGpuBf16Tensor *t, const uint16_t *x, float *y, OmegaGpuBf16Info *info) { return gemv_core(t, NULL, x, y, info, NULL, 0, 0, 0, 0, 0, 0, 0); }
300
301/* Record path (OWN-30B W-L2a decode graph): see the header. Caller holds omega_gpu_session_lock(). */
302int omega_gpu_bf16_gemv_record(OmegaGpuCmdList *cl, const OmegaGpuBf16Tensor *t, uint64_t x_va, uint64_t y_va) {
303 if (!cl) return OMEGA_GPU_BF16_BAD_ARGS;
304 return gemv_core(t, NULL, NULL, NULL, NULL, cl, x_va, y_va, 0, 0, 0, 0, 0);
305}
306
307/* ROWS form record path (sc #642 prefill): see the header. Caller holds omega_gpu_session_lock(). */
308int omega_gpu_bf16_gemv_rows_record(OmegaGpuCmdList *cl, const OmegaGpuBf16Tensor *t, uint32_t rows, uint64_t x_va, uint64_t y_va) {
309 if (!cl) return OMEGA_GPU_BF16_BAD_ARGS;
310 uint64_t p[16]={rows};int rc=ap_plan(AP_BF,5,p);if(rc)return rc;
311 return gemv_core(t, NULL, NULL, NULL, NULL, cl, x_va, y_va, rows, 0, 0, 0, 0);
312}
313
314/* MASK form record path (HUM L314 exact head): see the header. Caller holds omega_gpu_session_lock(). */
315int omega_gpu_bf16_gemv_mask_record(OmegaGpuCmdList *cl, const OmegaGpuBf16Tensor *t, uint64_t x_va, uint64_t y_va, uint64_t hi_va, uint64_t tau_va) {
316 if (!cl || !hi_va || !tau_va) return OMEGA_GPU_BF16_BAD_ARGS;
317 return gemv_core(t, NULL, NULL, NULL, NULL, cl, x_va, y_va, 0, 0, 0, hi_va, tau_va);
318}
319/* XBATCH form record path (the lm_head of several conversations in one stroke): see the header. Caller holds the lock. */
320int omega_gpu_bf16_gemv_xbatch_record(OmegaGpuCmdList *cl, const OmegaGpuBf16Tensor *t, uint32_t xbatch, uint64_t x_va, uint64_t y_va, uint32_t ystride) {
321 if (!cl) return OMEGA_GPU_BF16_BAD_ARGS;
322 uint64_t p[16]={xbatch};int rc=ap_plan(AP_BF,5,p);if(rc)return rc;
323 if (p[8]) return gemv_core(t, NULL, NULL, NULL, NULL, cl, x_va, y_va, 0, 0, 0, 0, 0);
324 return gemv_core(t, NULL, NULL, NULL, NULL, cl, x_va, y_va, 0, xbatch, ystride, 0, 0);
325}