interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / omega_gpu_tern_api.c

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