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_tern_api.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* omega_gpu_tern_api.c: KT1 W2A8 ternary GEMV on the shared session, ternary engine stage T0 (2026-10-15). See the header. |
| 3 | * Buffer policy: weights GPU-cached by default (written once by the CPU before any GPU read, then only read by the GPU; |
| 4 | * omega_gpu_mem_cached.h). Everything else both sides touch (x, a_scale, scales, y) is GPU-uncached (nvos.h |
| 5 | * NVOS32_ATTR2_GPU_CACHEABLE: "For system memory this will not be coherent with direct CPU mappings"). Submission path |
| 6 | * and kernel cache are the bf16 API's. */ |
| 7 | #include "omega_gpu_tern_api.h" |
| 8 | #include "omega_gpu_session.h" |
| 9 | #include "omega_gpu_mem_cached.h" |
| 10 | #include "omega_gpu_fmt.h" |
| 11 | #include "omega_gpu_cmdlist.h" |
| 12 | #include <math.h> |
| 13 | #include "omega_api_shells.h" |
| 14 | #include "omega_api_views_rest.h" |
| 15 | #include <string.h> |
| 16 | |
| 17 | #define CACHE_SLOTS 8 |
| 18 | #define MARKER_TIMEOUT_MS 20000ull |
| 19 | #define DEFAULT_CTAS 160u /* T0 sweep: 160 beat 96 and 256 on the four BitNet shapes (row-major, rows 1) */ |
| 20 | #define DEFAULT_WARPS 8u |
| 21 | |
| 22 | struct OmegaGpuTernTensor { |
| 23 | uint32_t n, k, n_padded, layout, R; |
| 24 | bool cached; |
| 25 | OmegaGpuTernSegments seg; |
| 26 | OmegaGpuMem w, ws; |
| 27 | }; |
| 28 | |
| 29 | typedef struct { |
| 30 | bool valid; |
| 31 | OmegaTernGemvSpec spec; |
| 32 | OmegaBlackwellKernel kernel; |
| 33 | OmegaGpuMem code; |
| 34 | } Slot; |
| 35 | |
| 36 | static struct { |
| 37 | OmegaGpuScratch x_buf, y_buf, as_buf; |
| 38 | uint32_t rows, warps, depth, ctas; /* overrides, 0 = automatic */ |
| 39 | uint32_t layout, block_rows; /* upload layout; block_rows 0 = 4 */ |
| 40 | int uncached; /* 1 = uploads use uncached weights */ |
| 41 | int mutant; |
| 42 | bool hooked; |
| 43 | Slot cache[CACHE_SLOTS]; |
| 44 | unsigned next; |
| 45 | } g = { .layout = OMEGA_TERN_LAYOUT_ROWMAJOR }; |
| 46 | static int g_rec; /* 1 while a record call runs (lock held): kernel_for must not evict */ |
| 47 | |
| 48 | const char *omega_gpu_tern_last_error(void) { return omega_gpu_session_last_error(); } |
| 49 | const char *omega_gpu_tern_rc_name(int rc) { |
| 50 | switch (rc) { |
| 51 | case OMEGA_GPU_TERN_OK: return "OK"; |
| 52 | case OMEGA_GPU_TERN_BAD_ARGS: return "BAD_ARGS"; |
| 53 | case OMEGA_GPU_TERN_TOO_LARGE: return "TOO_LARGE"; |
| 54 | case OMEGA_GPU_TERN_CODEGEN_FAIL: return "CODEGEN_FAIL"; |
| 55 | case OMEGA_GPU_TERN_CHIP_FAIL: return "CHIP_FAIL"; |
| 56 | case OMEGA_GPU_TERN_BAD_TRIT: return "BAD_TRIT"; |
| 57 | default: return "UNKNOWN"; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | static void cache_clear_locked(void) { |
| 62 | for (unsigned i = 0; i < CACHE_SLOTS; i++) { |
| 63 | if (!g.cache[i].valid) continue; |
| 64 | omega_gpu_session_free(&g.cache[i].code); |
| 65 | memset(&g.cache[i], 0, sizeof g.cache[i]); |
| 66 | } |
| 67 | g.next = 0; |
| 68 | } |
| 69 | static void on_session_close(void) { |
| 70 | cache_clear_locked(); |
| 71 | omega_gpu_session_scratch_free(&g.x_buf); |
| 72 | omega_gpu_session_scratch_free(&g.y_buf); |
| 73 | omega_gpu_session_scratch_free(&g.as_buf); |
| 74 | } |
| 75 | static OmegaGpuSession *dev_open_locked(void) { |
| 76 | if (!g.hooked) { (void)omega_gpu_session_on_close(on_session_close); g.hooked = true; } |
| 77 | return omega_gpu_session_open(); |
| 78 | } |
| 79 | void omega_gpu_tern_cache_clear(void) { omega_gpu_session_lock(); cache_clear_locked(); omega_gpu_session_unlock(); } |
| 80 | void omega_gpu_tern_set_layout(uint32_t layout, uint32_t block_rows) { omega_gpu_session_lock(); g.layout = layout; g.block_rows = block_rows; omega_gpu_session_unlock(); } |
| 81 | void omega_gpu_tern_set_shape(uint32_t rows, uint32_t warps, uint32_t depth) { |
| 82 | omega_gpu_session_lock(); g.rows = rows; g.warps = warps; g.depth = depth; cache_clear_locked(); omega_gpu_session_unlock(); |
| 83 | } |
| 84 | void omega_gpu_tern_set_cta_budget(uint32_t ctas) { omega_gpu_session_lock(); g.ctas = ctas; omega_gpu_session_unlock(); } |
| 85 | void omega_gpu_tern_set_weight_cache(int cached) { omega_gpu_session_lock(); g.uncached = !cached; omega_gpu_session_unlock(); } |
| 86 | void omega_gpu_tern_test_set_mutant(int m) { omega_gpu_session_lock(); g.mutant = m; cache_clear_locked(); omega_gpu_session_unlock(); } |
| 87 | |
| 88 | static uint32_t gpr_declared(uint32_t peak) { return omega_bw_gpr_declared(peak); } |
| 89 | static bool register_file_fits(uint32_t warps, uint32_t peak) { |
| 90 | uint32_t decl = gpr_declared(peak); |
| 91 | if (omega_api_regfit(warps, decl)) return true; |
| 92 | char e[160]; omega_gpu_fmt(e, sizeof e, "tern kernel: %u warps x %u declared registers exceed the 65536-register file", warps, decl); |
| 93 | omega_gpu_session_set_error(e); |
| 94 | return false; |
| 95 | } |
| 96 | int omega_gpu_tern_kernel_fits(const OmegaTernGemvSpec *spec, uint32_t *declared) { |
| 97 | uint32_t peak = 0; |
| 98 | if (!spec || omega_tern_gemv_prog_stats(spec, NULL, &peak) != 0) { omega_gpu_session_set_error("tern codegen refused the shape"); return OMEGA_GPU_TERN_CODEGEN_FAIL; } |
| 99 | if (declared) *declared = gpr_declared(peak); |
| 100 | return register_file_fits(spec->warps, peak) ? OMEGA_GPU_TERN_OK : OMEGA_GPU_TERN_TOO_LARGE; |
| 101 | } |
| 102 | |
| 103 | /* Automatic shape. Defaults from the T0 chip sweep (evidence TERN-T0-*). */ |
| 104 | void omega_gpu_tern_shape_for(uint32_t k, uint32_t layout, uint32_t R, uint32_t *rows, uint32_t *warps, uint32_t *depth, uint32_t *ctas) { |
| 105 | uint32_t r = 0, d = 0, w = 0, c = 0; |
| 106 | omega_api_ternd(k, layout == OMEGA_TERN_LAYOUT_BLOCK ? 1u : 0u, R, g.rows, g.depth, &r, &d); |
| 107 | omega_api_ternwc(g.warps, g.ctas, &w, &c); |
| 108 | if (rows) *rows = r; |
| 109 | if (warps) *warps = w; |
| 110 | if (depth) *depth = d; |
| 111 | if (ctas) *ctas = c; |
| 112 | } |
| 113 | |
| 114 | static int kernel_for(const OmegaTernGemvSpec *spec, Slot **out, bool *hit) { |
| 115 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 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_TERN_OK; } |
| 119 | } |
| 120 | #else |
| 121 | const size_t found = ar_cache(g.cache, sizeof g.cache, CACHE_SLOTS, sizeof(Slot), |
| 122 | offsetof(Slot, valid), offsetof(Slot, spec), spec, sizeof *spec); |
| 123 | if (found != SIZE_MAX) { *out = &g.cache[found]; *hit = true; return OMEGA_GPU_TERN_OK; } |
| 124 | #endif |
| 125 | Slot *s = &g.cache[g.next]; |
| 126 | if (g_rec && s->valid) { omega_gpu_session_set_error("tern record: kernel cache miss would evict a resident kernel (warm up before recording)"); return OMEGA_GPU_TERN_TOO_LARGE; } |
| 127 | BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); |
| 128 | if (!prog) { omega_gpu_session_set_error("tern codegen: host memory"); return OMEGA_GPU_TERN_CODEGEN_FAIL; } |
| 129 | int cg = omega_blackwell_codegen_tern_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("tern codegen refused the shape"); return OMEGA_GPU_TERN_CODEGEN_FAIL; } |
| 132 | if (!register_file_fits(spec->warps, prog->regalloc.peak_gpr_usage)) { omega_gpu_host_free(prog); return OMEGA_GPU_TERN_TOO_LARGE; } |
| 133 | OmegaGpuMem code; |
| 134 | if (omega_gpu_session_alloc_code(need, &code) != 0) { omega_gpu_host_free(prog); return OMEGA_GPU_TERN_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_TERN_CODEGEN_FAIL; } |
| 139 | if (omega_gpu_session_sync_to_gpu(&code) != 0) { omega_gpu_session_free(&code); return OMEGA_GPU_TERN_CHIP_FAIL; } |
| 140 | if (s->valid) omega_gpu_session_free(&s->code); |
| 141 | memset(s, 0, sizeof *s); |
| 142 | s->valid = true; s->spec = *spec; s->kernel = kernel; s->code = code; |
| 143 | g.next = (g.next + 1u) % CACHE_SLOTS; |
| 144 | *out = s; *hit = false; |
| 145 | return OMEGA_GPU_TERN_OK; |
| 146 | } |
| 147 | |
| 148 | uint32_t omega_gpu_tern_padded_rows(uint32_t n) { return omega_api_pad8(n); } |
| 149 | |
| 150 | int omega_gpu_tern_pack(uint32_t n, uint32_t k, const int8_t *w, uint32_t layout, uint32_t R, uint32_t *out, uint32_t n_padded) { |
| 151 | if (!w || !out || omega_api_tpack(n, k, layout == OMEGA_TERN_LAYOUT_BLOCK ? 1u : 0u, R, n_padded)) return OMEGA_GPU_TERN_BAD_ARGS; |
| 152 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 153 | const uint32_t kw = k / 16u; |
| 154 | memset(out, 0, (size_t)n_padded * kw * 4u); |
| 155 | for (uint32_t r = 0; r < n; r++) { |
| 156 | const int8_t *row = w + (size_t)r * k; |
| 157 | for (uint32_t c = 0; c < kw; c++) { |
| 158 | uint32_t word = 0; |
| 159 | for (uint32_t i = 0; i < 16u; i++) { |
| 160 | const int8_t v = row[16u * c + i]; |
| 161 | const uint32_t code = v == 1 ? 1u : v == -1 ? 2u : v == 0 ? 0u : 3u; |
| 162 | if (code == 3u) return OMEGA_GPU_TERN_BAD_TRIT; |
| 163 | word |= code << (2u * i); |
| 164 | } |
| 165 | out[omega_tern_word_offset(layout, R, k, r, c) / 4u] = word; |
| 166 | } |
| 167 | } |
| 168 | #else |
| 169 | const uint64_t q[4] = { n, k, layout == OMEGA_TERN_LAYOUT_BLOCK, R }; |
| 170 | uint64_t r = 0; |
| 171 | ar_check(omega_artern(&r, q, 4, w, (uint64_t)n * k, out, (uint64_t)n_padded * (k / 16u))); |
| 172 | if (r) return OMEGA_GPU_TERN_BAD_TRIT; |
| 173 | #endif |
| 174 | return OMEGA_GPU_TERN_OK; |
| 175 | } |
| 176 | |
| 177 | int omega_gpu_tern_segment_bounds(uint32_t n, const OmegaGpuTernSegments *seg, int mutant, uint32_t b[3]) { |
| 178 | const uint32_t cnt = seg ? seg->count : 1u; |
| 179 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 180 | if (omega_api_segend(0, cnt, 1u, 1u, 0u)) return OMEGA_GPU_TERN_BAD_ARGS; |
| 181 | for (uint32_t i = 0; i < 3; i++) |
| 182 | b[i] = omega_api_segb(i, cnt, n, seg ? seg->end[i] : 0u, mutant == 2 ? 2u : 0u); |
| 183 | #else |
| 184 | const uint32_t zero[3] = {0, 0, 0}; |
| 185 | uint64_t r = 0; |
| 186 | ar_check(omega_arsegb(&r, n, cnt, mutant == 2 ? 2u : 0u, seg ? seg->end : zero, 3, b, 3)); |
| 187 | if (r) return OMEGA_GPU_TERN_BAD_ARGS; |
| 188 | #endif |
| 189 | return OMEGA_GPU_TERN_OK; |
| 190 | } |
| 191 | |
| 192 | static int seg_ok(uint32_t n, const OmegaGpuTernSegments *seg) { |
| 193 | if (!seg) return 1; |
| 194 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 195 | if (omega_api_segend(0, seg->count, 1u, 1u, 0u)) return 0; |
| 196 | uint32_t prev = 0; |
| 197 | for (uint32_t i = 0; i < seg->count; i++) { |
| 198 | uint32_t bits; |
| 199 | memcpy(&bits, &seg->scale[i], 4); |
| 200 | if (!omega_api_finite(bits) || omega_api_segend(i, seg->count, n, seg->end[i], prev)) return 0; |
| 201 | prev = seg->end[i]; |
| 202 | } |
| 203 | return 1; |
| 204 | #else |
| 205 | uint64_t r = 0; |
| 206 | ar_check(omega_arsegok(&r, n, seg->count, seg->end, 4, seg->scale, 4)); |
| 207 | return r != 0; |
| 208 | #endif |
| 209 | } |
| 210 | |
| 211 | void omega_gpu_tern_tensor_shape(const OmegaGpuTernTensor *t, uint32_t *n, uint32_t *k) { |
| 212 | if (n) *n = t ? t->n : 0; |
| 213 | if (k) *k = t ? t->k : 0; |
| 214 | } |
| 215 | uint64_t omega_gpu_tern_tensor_bytes(const OmegaGpuTernTensor *t) { return t ? (uint64_t)t->n_padded * t->k / 4u : 0; } |
| 216 | |
| 217 | int omega_gpu_tern_tensor_upload(uint32_t n, uint32_t k, const int8_t *w, const OmegaGpuTernSegments *seg, OmegaGpuTernTensor **out) { |
| 218 | if (out) *out = NULL; |
| 219 | uint32_t nk = omega_api_nkbad(n, k); |
| 220 | if (!w || !out || nk == 1u || !seg_ok(n, seg)) return OMEGA_GPU_TERN_BAD_ARGS; |
| 221 | if (nk == 2u) return OMEGA_GPU_TERN_TOO_LARGE; |
| 222 | omega_gpu_session_lock(); |
| 223 | const uint32_t layout = g.layout, R = g.block_rows ? g.block_rows : 4u; const int uncached = g.uncached; |
| 224 | omega_gpu_session_unlock(); |
| 225 | if (omega_api_layok(layout, R)) return OMEGA_GPU_TERN_BAD_ARGS; |
| 226 | const size_t count = (size_t)n * k; |
| 227 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 228 | for (size_t i = 0; i < count; i++) if (!omega_api_tritok((uint8_t)w[i])) { |
| 229 | char e[120]; omega_gpu_fmt(e, sizeof e, "tern upload: value %d at element %zu is not a trit", (int)w[i], i); |
| 230 | omega_gpu_session_set_error(e); |
| 231 | return OMEGA_GPU_TERN_BAD_TRIT; |
| 232 | } |
| 233 | #else |
| 234 | uint64_t bad = 0; |
| 235 | ar_check(omega_artrits(&bad, w, count)); |
| 236 | if (bad != count) { |
| 237 | char e[120]; omega_gpu_fmt(e, sizeof e, "tern upload: value %d at element %zu is not a trit", (int)w[bad], (size_t)bad); |
| 238 | omega_gpu_session_set_error(e); |
| 239 | return OMEGA_GPU_TERN_BAD_TRIT; |
| 240 | } |
| 241 | #endif |
| 242 | OmegaGpuTernTensor *t = omega_gpu_host_calloc(1, sizeof *t); |
| 243 | if (!t) return OMEGA_GPU_TERN_BAD_ARGS; |
| 244 | t->n = n; t->k = k; t->n_padded = omega_gpu_tern_padded_rows(n); t->layout = layout; t->R = R; t->cached = !uncached; |
| 245 | if (seg) t->seg = *seg; else { t->seg.count = 1; t->seg.end[0] = n; t->seg.scale[0] = 1.0f; } |
| 246 | const size_t bytes = (size_t)t->n_padded * k / 4u; |
| 247 | int rc = OMEGA_GPU_TERN_OK; |
| 248 | omega_gpu_session_lock(); |
| 249 | if (!dev_open_locked() || (uncached ? omega_gpu_session_alloc(bytes, &t->w) : omega_gpu_session_alloc_cached(bytes, &t->w)) != 0 |
| 250 | || omega_gpu_session_alloc(16u, &t->ws) != 0) rc = OMEGA_GPU_TERN_CHIP_FAIL; |
| 251 | omega_gpu_session_unlock(); |
| 252 | if (rc == OMEGA_GPU_TERN_OK) rc = omega_gpu_tern_pack(n, k, w, layout, R, t->w.cpu, t->n_padded); |
| 253 | if (rc == OMEGA_GPU_TERN_OK) { |
| 254 | float rev[4] = { 0, 0, 0, 0 }; |
| 255 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 256 | for (uint32_t i = 0; i < t->seg.count; i++) rev[3u - i] = t->seg.scale[i]; |
| 257 | #else |
| 258 | uint64_t r = 0; |
| 259 | ar_check(omega_arrev(&r, t->seg.scale, t->seg.count, rev, 4)); |
| 260 | #endif |
| 261 | memcpy(t->ws.cpu, rev, sizeof rev); |
| 262 | if (omega_gpu_session_sync_to_gpu(&t->w) != 0 || omega_gpu_session_sync_to_gpu(&t->ws) != 0) rc = OMEGA_GPU_TERN_CHIP_FAIL; |
| 263 | } |
| 264 | if (rc != OMEGA_GPU_TERN_OK) { omega_gpu_tern_tensor_free(t); return rc; } |
| 265 | *out = t; |
| 266 | return OMEGA_GPU_TERN_OK; |
| 267 | } |
| 268 | |
| 269 | void omega_gpu_tern_tensor_free(OmegaGpuTernTensor *t) { |
| 270 | if (!t) return; |
| 271 | omega_gpu_session_lock(); |
| 272 | if (t->w.cpu) omega_gpu_session_free(&t->w); |
| 273 | if (t->ws.cpu) omega_gpu_session_free(&t->ws); |
| 274 | omega_gpu_session_unlock(); |
| 275 | omega_gpu_host_free(t); |
| 276 | } |
| 277 | |
| 278 | /* rec non-NULL (record path, caller holds the session lock): device buffers dev_x / dev_y / dev_as, the launch is appended |
| 279 | * to rec, nothing staged, poisoned or read back. */ |
| 280 | static int gemv_core(const OmegaGpuTernTensor *t, const int8_t *x, int f32, float a_scale, void *y, OmegaGpuTernInfo *info, |
| 281 | OmegaGpuCmdList *rec, uint64_t dev_x, uint64_t dev_y, uint64_t dev_as) { |
| 282 | uint64_t t0 = omega_gpu_session_now_ns(); |
| 283 | if (info) memset(info, 0, sizeof *info); |
| 284 | if (rec && (!dev_x || !dev_y || (dev_x & 15u))) return OMEGA_GPU_TERN_BAD_ARGS; |
| 285 | if (!t || !t->w.cpu || (!rec && (!x || !y))) return OMEGA_GPU_TERN_BAD_ARGS; |
| 286 | if (f32 && !rec && !isfinite(a_scale)) return OMEGA_GPU_TERN_BAD_ARGS; |
| 287 | const uint32_t n = t->n, k = t->k; |
| 288 | |
| 289 | if (!rec) omega_gpu_session_lock(); |
| 290 | #define UNLOCK() do { if (!rec) omega_gpu_session_unlock(); } while (0) |
| 291 | OmegaTernGemvSpec spec; uint32_t ctas; |
| 292 | memset(&spec, 0, sizeof spec); |
| 293 | spec.k = k; spec.layout = t->layout; spec.epi = f32 ? OMEGA_TERN_EPI_F32 : OMEGA_TERN_EPI_I32; |
| 294 | omega_gpu_tern_shape_for(k, t->layout, t->R, &spec.rows, &spec.warps, &spec.depth, &ctas); |
| 295 | spec.mutant = g.mutant == 2 ? 0 : g.mutant; |
| 296 | uint32_t bounds[3]; |
| 297 | if (!omega_tern_gemv_spec_ok(&spec) || omega_gpu_tern_segment_bounds(n, &t->seg, g.mutant, bounds) != 0) { |
| 298 | omega_gpu_session_set_error("tern: kernel shape refused for this tensor"); |
| 299 | UNLOCK(); return OMEGA_GPU_TERN_CODEGEN_FAIL; |
| 300 | } |
| 301 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 302 | const uint32_t U = t->n_padded / (2u * spec.rows); |
| 303 | if (t->n_padded % (2u * spec.rows)) { UNLOCK(); return OMEGA_GPU_TERN_CODEGEN_FAIL; } |
| 304 | uint32_t grid_x = (U + spec.warps - 1u) / spec.warps; |
| 305 | if (grid_x > ctas) grid_x = ctas; |
| 306 | if (grid_x == 0) grid_x = 1; |
| 307 | #else |
| 308 | uint64_t plan[32] = {4, t->n_padded, spec.rows, spec.warps, ctas, n, bounds[0], bounds[1], bounds[2]}; |
| 309 | if (ar_plan(plan)) { UNLOCK(); return OMEGA_GPU_TERN_CODEGEN_FAIL; } |
| 310 | const uint32_t U = (uint32_t)plan[16], grid_x = (uint32_t)plan[17]; |
| 311 | #endif |
| 312 | |
| 313 | int rc = OMEGA_GPU_TERN_OK; |
| 314 | if (!dev_open_locked()) { UNLOCK(); return OMEGA_GPU_TERN_CHIP_FAIL; } |
| 315 | if (!rec) { |
| 316 | int src = omega_gpu_session_scratch(&g.x_buf, (size_t)k); |
| 317 | if (src == 0) src = omega_gpu_session_scratch(&g.y_buf, (size_t)n * 4u); |
| 318 | if (src == 0) src = omega_gpu_session_scratch(&g.as_buf, 16u); |
| 319 | if (src != 0) { UNLOCK(); return src == OMEGA_GPU_SESSION_SCRATCH_RESERVED ? OMEGA_GPU_TERN_TOO_LARGE : OMEGA_GPU_TERN_CHIP_FAIL; } |
| 320 | if (g.x_buf.mem.va & 15u) { omega_gpu_session_set_error("tern: x scratch not 16-byte aligned"); UNLOCK(); return OMEGA_GPU_TERN_CHIP_FAIL; } |
| 321 | } |
| 322 | Slot *ks = NULL; bool hit = false; |
| 323 | g_rec = rec != NULL; |
| 324 | rc = kernel_for(&spec, &ks, &hit); |
| 325 | g_rec = 0; |
| 326 | if (rc != OMEGA_GPU_TERN_OK) { UNLOCK(); return rc; } |
| 327 | |
| 328 | uint32_t *yd = g.y_buf.mem.cpu; |
| 329 | if (!rec) { |
| 330 | memcpy(g.x_buf.mem.cpu, x, k); |
| 331 | memcpy(g.as_buf.mem.cpu, &a_scale, 4); |
| 332 | const uint32_t poison = f32 ? OMEGA_GPU_TERN_POISON_F32_BITS : (uint32_t)OMEGA_GPU_TERN_POISON_I32; |
| 333 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 334 | for (size_t i = 0; i < n; i++) yd[i] = poison; |
| 335 | #else |
| 336 | ar_fill(yd, n, poison); |
| 337 | #endif |
| 338 | if (omega_gpu_session_sync_to_gpu(&g.x_buf.mem) != 0 || omega_gpu_session_sync_to_gpu(&g.y_buf.mem) != 0 |
| 339 | || omega_gpu_session_sync_to_gpu(&g.as_buf.mem) != 0) rc = OMEGA_GPU_TERN_CHIP_FAIL; |
| 340 | } |
| 341 | |
| 342 | uint32_t args[OMEGA_TERN_ARGS_WORDS]; |
| 343 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 344 | memset(args, 0, sizeof args); |
| 345 | const uint64_t x_va = rec ? dev_x : g.x_buf.mem.va, y_va = rec ? dev_y : g.y_buf.mem.va; |
| 346 | const uint64_t as_va = rec ? dev_as : g.as_buf.mem.va, ws_va = t->ws.va; |
| 347 | args[0] = (uint32_t)t->w.va; args[1] = (uint32_t)(t->w.va >> 32); |
| 348 | args[2] = (uint32_t)x_va; args[3] = (uint32_t)(x_va >> 32); |
| 349 | args[4] = (uint32_t)y_va; args[5] = (uint32_t)(y_va >> 32); |
| 350 | args[6] = (uint32_t)ws_va; args[7] = (uint32_t)(ws_va >> 32); |
| 351 | args[8] = (uint32_t)as_va; args[9] = (uint32_t)(as_va >> 32); |
| 352 | args[10] = U; args[11] = grid_x * spec.warps; |
| 353 | args[12] = 0u - n; |
| 354 | for (int i = 0; i < 3; i++) args[13 + i] = 0u - bounds[i]; |
| 355 | #else |
| 356 | const uint64_t va[] = {t->w.va, rec ? dev_x : g.x_buf.mem.va, rec ? dev_y : g.y_buf.mem.va, |
| 357 | t->ws.va, rec ? dev_as : g.as_buf.mem.va}; |
| 358 | const uint32_t tail[] = {U, (uint32_t)plan[18], (uint32_t)plan[19], |
| 359 | (uint32_t)plan[20], (uint32_t)plan[21], (uint32_t)plan[22]}; |
| 360 | ar_args_extra(args, OMEGA_TERN_ARGS_WORDS, va, 5, tail, 6); |
| 361 | #endif |
| 362 | if (info) { |
| 363 | info->n = n; info->k = k; info->n_padded = t->n_padded; info->layout = t->layout; info->rows_per_half = spec.rows; |
| 364 | info->warps_per_cta = spec.warps; info->depth = spec.depth; info->grid_x = grid_x; |
| 365 | info->chunks = omega_tern_gemv_chunks(&spec); info->epi = spec.epi; |
| 366 | info->weight_bytes = omega_gpu_tern_tensor_bytes(t); |
| 367 | info->kernel_cache_hit = hit; info->weights_cached = t->cached; |
| 368 | memcpy(info->seg_bounds, bounds, sizeof bounds); |
| 369 | (void)omega_gpu_fmt(info->target_chip, sizeof info->target_chip, "%s", "NVIDIA DGX Spark (Grace Blackwell GB10)"); |
| 370 | info->sm_architecture = 121; |
| 371 | } |
| 372 | if (rc == OMEGA_GPU_TERN_OK) { |
| 373 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 374 | OmegaGpuLaunch L = { |
| 375 | .code_va = ks->code.va, .gpr_count = gpr_declared(ks->kernel.gpr_count), |
| 376 | .threads_x = 32u * spec.warps, .threads_y = 1, .grid_x = grid_x, .grid_y = 1, |
| 377 | .num_elements = n, |
| 378 | .args = args, .n_args = OMEGA_TERN_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS, |
| 379 | .shared_bytes = omega_tern_gemv_shared_bytes(&spec), |
| 380 | }; |
| 381 | #else |
| 382 | OmegaGpuLaunch L; |
| 383 | const uint64_t launch[16] = {ks->code.va, gpr_declared(ks->kernel.gpr_count), plan[23], 1, |
| 384 | grid_x, 1, n, (uintptr_t)args, OMEGA_TERN_ARGS_WORDS, |
| 385 | MARKER_TIMEOUT_MS, omega_tern_gemv_shared_bytes(&spec)}; |
| 386 | ar_launch(&L, launch); |
| 387 | #endif |
| 388 | 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_TERN_CHIP_FAIL; } |
| 389 | else if (rec) { if (omega_gpu_cmdlist_add(rec, &L) < 0) rc = OMEGA_GPU_TERN_CHIP_FAIL; } |
| 390 | else if (omega_gpu_session_launch(&L, info ? &info->elapsed_ns : NULL, info ? &info->completion_marker : NULL) != 0) rc = OMEGA_GPU_TERN_CHIP_FAIL; |
| 391 | } |
| 392 | if (rc == OMEGA_GPU_TERN_OK && !rec && omega_gpu_session_sync_to_cpu(&g.y_buf.mem) != 0) rc = OMEGA_GPU_TERN_CHIP_FAIL; |
| 393 | if (rc == OMEGA_GPU_TERN_OK && !rec) { |
| 394 | const uint32_t poison = f32 ? OMEGA_GPU_TERN_POISON_F32_BITS : (uint32_t)OMEGA_GPU_TERN_POISON_I32; |
| 395 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 396 | size_t unchanged = 0; |
| 397 | for (size_t i = 0; i < n; i++) if (yd[i] == poison) unchanged++; |
| 398 | #else |
| 399 | size_t unchanged = ar_count(yd, n, poison); |
| 400 | #endif |
| 401 | if (unchanged) { |
| 402 | char e[120]; omega_gpu_fmt(e, sizeof e, "tern: output poison survived in %zu of %u rows", unchanged, n); |
| 403 | omega_gpu_session_set_error(e); rc = OMEGA_GPU_TERN_CHIP_FAIL; |
| 404 | } else memcpy(y, yd, (size_t)n * 4u); |
| 405 | } |
| 406 | UNLOCK(); |
| 407 | if (info) info->call_ns = omega_gpu_session_now_ns() - t0; |
| 408 | return rc; |
| 409 | } |
| 410 | #undef UNLOCK |
| 411 | |
| 412 | int omega_gpu_tern_gemv_i32(const OmegaGpuTernTensor *t, const int8_t *x, int32_t *y, OmegaGpuTernInfo *info) { |
| 413 | return gemv_core(t, x, 0, 1.0f, y, info, NULL, 0, 0, 0); |
| 414 | } |
| 415 | int omega_gpu_tern_gemv_f32(const OmegaGpuTernTensor *t, const int8_t *x, float a_scale, float *y, OmegaGpuTernInfo *info) { |
| 416 | return gemv_core(t, x, 1, a_scale, y, info, NULL, 0, 0, 0); |
| 417 | } |
| 418 | int omega_gpu_tern_gemv_record(OmegaGpuCmdList *cl, const OmegaGpuTernTensor *t, uint64_t x_va, uint64_t y_va, uint64_t as_va) { |
| 419 | if (!cl) return OMEGA_GPU_TERN_BAD_ARGS; |
| 420 | return gemv_core(t, NULL, as_va != 0, 1.0f, NULL, NULL, cl, x_va, y_va, as_va); |
| 421 | } |