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