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.h

121 lines · 6919 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#ifndef OMEGA_GPU_TERN_API_H
18#define OMEGA_GPU_TERN_API_H
19/*
20 * omega_gpu_tern_api: KT1, the native (no CUDA) W2A8 ternary GEMV on the shared session, ternary engine stage T0
21 * (2026-10-15). New file; no existing API changes. Kernel: omega_blackwell_codegen_tern.h.
22 *
23 * typedef struct OmegaGpuTernTensor OmegaGpuTernTensor; // opaque, device resident
24 * int omega_gpu_tern_tensor_upload(uint32_t n, uint32_t k, const int8_t *w, const OmegaGpuTernSegments *seg,
25 * OmegaGpuTernTensor **out);
26 * w: n * k values in {-1, 0, +1}, row-major [n][k]. Any other value is refused (BAD_TRIT, nothing uploaded).
27 * Requires n >= 1, k % 256 == 0 (k % 512 above 8192), k <= 16384. Packed to omega's 2-bit trit code
28 * (src/algebra/oma_trit.h) in the layout set by omega_gpu_tern_set_layout (read at upload), zero rows padding n to a
29 * multiple of 8. Weights are GPU-cached by default (written once by the CPU, then only read by the GPU;
30 * omega_gpu_mem_cached.h). seg may be NULL (one segment, scale 1).
31 * seg: the per-matrix weight scales of a fused projection, one per OUTPUT SEGMENT with explicit row boundaries:
32 * count 1..4, end[i] the first row after segment i (strictly increasing, end[count - 1] == n), scale[i] finite.
33 * Example q|k|v: count 3, end {2560, 3200, 3840}. Row r uses scale[i] for the segment i that contains r.
34 * int omega_gpu_tern_gemv_i32(const OmegaGpuTernTensor *t, const int8_t *x, int32_t *y, OmegaGpuTernInfo *info);
35 * y[r] = sum_c W[r][c] * x[c], exact int32 (the contract of oma_rz_oracle). x: k int8, y: n int32. One launch.
36 * int omega_gpu_tern_gemv_f32(const OmegaGpuTernTensor *t, const int8_t *x, float a_scale, float *y, OmegaGpuTernInfo *info);
37 * y[r] = ((float)y_int[r] * a_scale) * scale[seg(r)], two round-to-nearest f32 multiplies in that order. a_scale finite.
38 * k <= 8192 only (one x chunk). Not the same rounding as the divide form (float)acc / s_a * w_scale; the T2 host
39 * reference defines which one the engine uses.
40 * Return codes: 0 OK; negative OMEGA_GPU_TERN_* below. Every output word is poisoned before the launch and a result that
41 * still holds poison is refused. An uncertain completion latches the process (the session's rule).
42 */
43#include <stdbool.h>
44#include <stddef.h>
45#include <stdint.h>
46#include "omega_blackwell_codegen_tern.h"
47
48#ifdef __cplusplus
49extern "C" {
50#endif
51
52enum {
53 OMEGA_GPU_TERN_OK = 0,
54 OMEGA_GPU_TERN_BAD_ARGS = -1,
55 OMEGA_GPU_TERN_TOO_LARGE = -2,
56 OMEGA_GPU_TERN_CODEGEN_FAIL = -3,
57 OMEGA_GPU_TERN_CHIP_FAIL = -4,
58 OMEGA_GPU_TERN_BAD_TRIT = -5
59};
60
61#define OMEGA_GPU_TERN_POISON_I32 0x7f7f7f7f /* |y| <= 128 * 16384 < 2^22: never a result */
62#define OMEGA_GPU_TERN_POISON_F32_BITS 0x7fc0deadu
63
64typedef struct {
65 uint32_t count;
66 uint32_t end[4];
67 float scale[4];
68} OmegaGpuTernSegments;
69
70typedef struct {
71 uint32_t n, k, n_padded, layout, rows_per_half, warps_per_cta, depth, grid_x, chunks, epi;
72 uint64_t weight_bytes; /* packed trit bytes the launch reads (n_padded * k / 4) */
73 uint64_t elapsed_ns; /* chip submit-to-second-marker */
74 uint64_t call_ns; /* whole API call */
75 uint32_t completion_marker;
76 bool kernel_cache_hit, weights_cached;
77 uint32_t seg_bounds[3]; /* the row boundaries the launch used */
78 char target_chip[64];
79 uint32_t sm_architecture;
80} OmegaGpuTernInfo;
81
82typedef struct OmegaGpuTernTensor OmegaGpuTernTensor;
83
84int omega_gpu_tern_tensor_upload(uint32_t n, uint32_t k, const int8_t *w, const OmegaGpuTernSegments *seg, OmegaGpuTernTensor **out);
85void omega_gpu_tern_tensor_free(OmegaGpuTernTensor *t);
86void omega_gpu_tern_tensor_shape(const OmegaGpuTernTensor *t, uint32_t *n, uint32_t *k);
87uint64_t omega_gpu_tern_tensor_bytes(const OmegaGpuTernTensor *t); /* packed bytes on the device */
88int omega_gpu_tern_gemv_i32(const OmegaGpuTernTensor *t, const int8_t *x, int32_t *y, OmegaGpuTernInfo *info);
89int omega_gpu_tern_gemv_f32(const OmegaGpuTernTensor *t, const int8_t *x, float a_scale, float *y, OmegaGpuTernInfo *info);
90/* Record path (omega_gpu_cmdlist.h): the same launch appended to cl. x_va: k int8 on the device (16-byte aligned);
91 * y_va: n words; as_va: the f32 a_scale word for the f32 epilogue, 0 for int32 output. The caller holds
92 * omega_gpu_session_lock(). Nothing staged, poisoned or read back. A kernel cache miss that would evict is refused
93 * (TOO_LARGE): warm the shape up with one non-record call first. */
94struct OmegaGpuCmdList;
95int omega_gpu_tern_gemv_record(struct OmegaGpuCmdList *cl, const OmegaGpuTernTensor *t, uint64_t x_va, uint64_t y_va, uint64_t as_va);
96const char *omega_gpu_tern_rc_name(int rc);
97const char *omega_gpu_tern_last_error(void);
98void omega_gpu_tern_cache_clear(void);
99
100/* Trit packing and segment boundaries as the device sees them (tests and the host emulation use the same code). */
101int omega_gpu_tern_pack(uint32_t n, uint32_t k, const int8_t *w, uint32_t layout, uint32_t R, uint32_t *out_words, uint32_t n_padded);
102uint32_t omega_gpu_tern_padded_rows(uint32_t n);
103int omega_gpu_tern_segment_bounds(uint32_t n, const OmegaGpuTernSegments *seg, int mutant, uint32_t bounds[3]);
104
105/* Register-file check (32 * warps * declared registers <= 65536). */
106int omega_gpu_tern_kernel_fits(const OmegaTernGemvSpec *spec, uint32_t *declared);
107/* The kernel shape the API picks for (k, layout, R): rows, warps, depth, CTAs. */
108void 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);
109
110/* ---- measurement knobs (tests; 0 restores the automatic choice) ---- */
111void omega_gpu_tern_set_layout(uint32_t layout, uint32_t block_rows); /* read by later uploads; default row-major (rows 1, 160 CTAs: T0 sweep) */
112void omega_gpu_tern_set_shape(uint32_t rows, uint32_t warps, uint32_t depth); /* clears the kernel cache */
113void omega_gpu_tern_set_cta_budget(uint32_t ctas);
114void omega_gpu_tern_set_weight_cache(int cached); /* 1 GPU-cached (default), 0 uncached; read by later uploads */
115/* TEST ONLY: 1 flipped planes, 3 dropped pair (kernel mutants); 2 maps segments as r / (n / count) (the API). */
116void omega_gpu_tern_test_set_mutant(int mutant);
117
118#ifdef __cplusplus
119}
120#endif
121#endif