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/omega-mv/blob/main/src/omega_gpu_bf16_gemv_api.h # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / omega_gpu_bf16_gemv_api.h` 125 lines ยท 7789 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_BF16_GEMV_API_H 18 #define OMEGA_GPU_BF16_GEMV_API_H 19 /* 20 * omega_gpu_bf16_gemv_api: native (no CUDA) bf16 streaming GEMV for the OWN-30B lane: the lm_head 21 * (vocab 151936 x hidden 2048, bf16 weights, f32 logits). New file on the shared session; no existing API changes. 22 * 23 * ============ C signatures and semantics (for the Rust binding) ============ 24 * typedef struct OmegaGpuBf16Tensor OmegaGpuBf16Tensor; // opaque, device resident 25 * 26 * int omega_gpu_bf16_tensor_upload(uint32_t n, uint32_t k, const uint16_t *w_bf16, OmegaGpuBf16Tensor **out); 27 * w: n * k bf16 bit patterns, row-major [n][k] (n output rows, k input columns): the checkpoint's lm_head.weight 28 * as is, copied byte for byte. Requires n >= 1, k % 256 == 0, 256 <= k <= 16384. Refuses a NaN or infinite bf16 29 * (exponent all ones) with OMEGA_GPU_BF16_NAN_WEIGHT (nothing uploaded). Opens the device on first use. Resident 30 * until omega_gpu_bf16_tensor_free. 31 * 32 * int omega_gpu_bf16_gemv_f32 (const OmegaGpuBf16Tensor *t, const float *x, float *y, OmegaGpuBf16Info *info); 33 * int omega_gpu_bf16_gemv_bf16(const OmegaGpuBf16Tensor *t, const uint16_t *x, float *y, OmegaGpuBf16Info *info); 34 * y[r] = sum_c W[r][c] * x[c], r < n, c < k, f32 accumulate (summation order: omega_blackwell_codegen_bf16.h; the 35 * result is deterministic and the test's host emulation reproduces it bit for bit). x: k values, y: n f32. One 36 * launch. info may be NULL. 37 * 38 * int omega_gpu_bf16_prepare(uint32_t k); build and pin the kernel for width k now (serving). 39 * void omega_gpu_bf16_tensor_free(OmegaGpuBf16Tensor *t); 40 * void omega_gpu_bf16_tensor_shape(const OmegaGpuBf16Tensor *t, uint32_t *n, uint32_t *k); 41 * const char *omega_gpu_bf16_rc_name(int rc); const char *omega_gpu_bf16_last_error(void); 42 * 43 * Return codes: 0 OK; negative OMEGA_GPU_BF16_* below. An uncertain completion latches the process (every later call 44 * of every API returns CHIP_FAIL), as the other APIs do. Every output word is poisoned before the launch and the 45 * call refuses a result that still holds poison. No host oracle runs inside the call. 46 */ 47 #include 48 #include 49 #include 50 #include "omega_blackwell_codegen_bf16.h" 51 52 #ifdef __cplusplus 53 extern "C" { 54 #endif 55 56 enum { 57 OMEGA_GPU_BF16_OK = 0, 58 OMEGA_GPU_BF16_BAD_ARGS = -1, 59 OMEGA_GPU_BF16_TOO_LARGE = -2, 60 OMEGA_GPU_BF16_CODEGEN_FAIL = -3, 61 OMEGA_GPU_BF16_CHIP_FAIL = -4, 62 OMEGA_GPU_BF16_NAN_WEIGHT = -5 63 }; 64 65 typedef struct { 66 uint32_t n, k, rows_per_warp, warps_per_cta, depth, grid_x, steps; 67 uint64_t weight_bytes; /* bf16 bytes the launch reads (n * k * 2) */ 68 uint64_t elapsed_ns; /* chip submit-to-second-marker */ 69 uint64_t call_ns; /* whole API call */ 70 uint32_t completion_marker; 71 bool kernel_cache_hit; 72 char target_chip[64]; 73 uint32_t sm_architecture; 74 } OmegaGpuBf16Info; 75 76 typedef struct OmegaGpuBf16Tensor OmegaGpuBf16Tensor; 77 78 int omega_gpu_bf16_tensor_upload(uint32_t n, uint32_t k, const uint16_t *w_bf16, OmegaGpuBf16Tensor **out); 79 void omega_gpu_bf16_tensor_free(OmegaGpuBf16Tensor *t); 80 void omega_gpu_bf16_tensor_shape(const OmegaGpuBf16Tensor *t, uint32_t *n, uint32_t *k); 81 int omega_gpu_bf16_gemv_f32(const OmegaGpuBf16Tensor *t, const float *x, float *y, OmegaGpuBf16Info *info); 82 int omega_gpu_bf16_gemv_bf16(const OmegaGpuBf16Tensor *t, const uint16_t *x, float *y, OmegaGpuBf16Info *info); 83 /* Record path (OWN-30B W-L2a, decode graph; omega_gpu_cmdlist.h): the same launch as gemv_f32 appended to cl instead 84 * of submitted. x_va: k f32 on the device (plain, not prescaled); y_va: n f32 written when the list runs. The caller 85 * holds omega_gpu_session_lock(). Nothing staged, poisoned or read back. A kernel cache miss that would evict a resident 86 * kernel is refused (TOO_LARGE): warm the shape up with one non-record call (or omega_gpu_bf16_prepare) first. */ 87 struct OmegaGpuCmdList; 88 int omega_gpu_bf16_gemv_record(struct OmegaGpuCmdList *cl, const OmegaGpuBf16Tensor *t, uint64_t x_va, uint64_t y_va); 89 /* ROWS form (sc #642 prefill): `rows` token rows in one launch (grid_y = rows <= 65535); row r reads x_va + r * k f32 90 * and writes y_va + r * n f32. Record-only; the kernel (spec.ystride = n) is cached per n like any other shape. */ 91 int omega_gpu_bf16_gemv_rows_record(struct OmegaGpuCmdList *cl, const OmegaGpuBf16Tensor *t, uint32_t rows, uint64_t x_va, uint64_t y_va); 92 /* MASK form (HUM L314 exact two-pass head; omega_blackwell_codegen_bf16.h): record-only, rows 1. Row r with 93 * hi[r] < *tau (hi_va: n f32, tau_va: one f32, both device) gets -inf in y; every other row gets the plain kernel's 94 * bits. Caller holds omega_gpu_session_lock(); the first call compiles into a free cache slot. */ 95 int omega_gpu_bf16_gemv_mask_record(struct OmegaGpuCmdList *cl, const OmegaGpuBf16Tensor *t, uint64_t x_va, uint64_t y_va, uint64_t hi_va, uint64_t tau_va); 96 /* XBATCH form (the 35B stroke: one lm_head read for several conversations): `xbatch` (1..OMEGA_BF16_MAX_XBATCH) x rows 97 * at x_va + m * k f32 share every weight read inside one CTA (every row staged in shared memory, so xbatch * k * 4 bytes 98 * must fit the launch descriptor's shared limit); y row m at y_va + m * ystride f32 (ystride >= n; the gap stays 99 * untouched). Row m equals the one-row result of its x bit for bit (the same FMA order). xbatch 1 is the plain launch. 100 * Record-only; the kernel (spec.xbatch, spec.ystride) is cached per (k, xbatch, ystride): pin it with prepare_xbatch. */ 101 int omega_gpu_bf16_gemv_xbatch_record(struct OmegaGpuCmdList *cl, const OmegaGpuBf16Tensor *t, uint32_t xbatch, uint64_t x_va, uint64_t y_va, uint32_t ystride); 102 int omega_gpu_bf16_prepare(uint32_t k); 103 int omega_gpu_bf16_prepare_mask(uint32_t k); /* build and pin the MASK kernel for width k (the exact head, serving) */ 104 int omega_gpu_bf16_prepare_xbatch(uint32_t k, uint32_t xbatch, uint32_t ystride); /* build and pin the XBATCH kernel (xbatch <= 1: prepare(k)) */ 105 const char *omega_gpu_bf16_rc_name(int rc); 106 const char *omega_gpu_bf16_last_error(void); 107 void omega_gpu_bf16_cache_clear(void); 108 109 /* Register-file check (chip-found rule: 32 * warps * declared registers <= 65536, declared = peak + 1 rounded up to 8). */ 110 int omega_gpu_bf16_kernel_fits(const OmegaBf16GemvSpec *spec, uint32_t *declared); 111 /* The shape the API picks for width k (rows, warps, depth, CTAs): for tests and the bandwidth table. */ 112 void omega_gpu_bf16_shape_for(uint32_t k, uint32_t *rows, uint32_t *warps, uint32_t *depth, uint32_t *ctas); 113 114 /* ---- measurement knobs (tests; each clears the kernel cache; 0 restores the automatic choice) ---- */ 115 void omega_gpu_bf16_set_shape(uint32_t rows, uint32_t warps, uint32_t depth); 116 void omega_gpu_bf16_set_cta_budget(uint32_t ctas); 117 /* TEST ONLY: deliberately wrong kernels 1, 2, 3 (see the codegen header). Production never calls this. */ 118 void omega_gpu_bf16_test_set_mutant(int mutant); 119 /* TEST ONLY: the MASK form alone under mutant m (4: rows with hi >= tau get -inf, the others the plain bits). 0 none. */ 120 void omega_gpu_bf16_test_set_mask_mutant(int m); 121 122 #ifdef __cplusplus 123 } 124 #endif 125 #endif