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