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_fp8gemm_api.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #ifndef OMEGA_GPU_FP8GEMM_API_H |
| 3 | #define OMEGA_GPU_FP8GEMM_API_H |
| 4 | /* |
| 5 | * omega_gpu_fp8gemm_api: native (no CUDA) FP8 e4m3 block-128-scaled GROUPED GEMM for prefill (OWN-30B, builder F |
| 6 | * item 26). It streams each distinct expert's weights once per pass of up to MT rows for all the rows routed to it, |
| 7 | * instead of once per row (omega_gpu_fp8_api's grouped GEMV). New file on the shared session; no existing API changes. |
| 8 | * |
| 9 | * ============ C signatures and semantics (for the Rust binding) ============ |
| 10 | * Weights are the existing OmegaGpuFp8Tensor (omega_gpu_fp8_api.h): experts x n x k e4m3 + 128x128 bf16-derived |
| 11 | * scales, uploaded once and shared with the GEMV path. Dense projections (qkv n=5120 k=2048, o n=2048 k=4096) are |
| 12 | * tensors with experts = 1. |
| 13 | * |
| 14 | * int omega_gpu_fp8gemm_f32(const OmegaGpuFp8Tensor *t, uint32_t M, const uint32_t *expert_ids, |
| 15 | * const uint32_t *token_rows, const float *x, uint32_t x_rows, float *y, |
| 16 | * OmegaGpuFp8GemmInfo *info); |
| 17 | * int omega_gpu_fp8gemm_bf16(... const uint16_t *x ...); same with bf16 activations |
| 18 | * The row table: row i (0 <= i < M) computes expert expert_ids[i] on activation row x[token_rows[i]] (x: x_rows * k |
| 19 | * values). expert_ids must be non-decreasing (sorted by expert; equal ids form one segment, the weights of a segment |
| 20 | * are streamed once per pass of MT rows). Output y: M * n f32 in ROW-TABLE order: y[i][r] = sum_c W[expert_ids[i]][r][c] |
| 21 | * * x[token_rows[i]][c] * scale, f32 accumulate, the same summation order as omega_gpu_fp8_gemv_f32 (the result is |
| 22 | * bit-identical to calling the GEMV once per row). Kernel domain: M <= 1024, at most 256 segments, a k the codegen |
| 23 | * accepts (k <= 16384, k % (32 * lane_bytes) == 0); n as the tensor. One launch. info may be NULL. |
| 24 | * General fallback (GemmGeneral, 2026-10-27): any other M, segment count or k the tensor holds runs each segment |
| 25 | * through omega_gpu_fp8_general_rows (weights dequantized exactly, fp32-input GEMM via bf16x3; info->general = 1). |
| 26 | * Those rows are NOT bit-identical to the GEMV; they carry the bf16x3 tolerance (tests/gpu_gemm_ex_test.c). Not when a |
| 27 | * test override (mt, warps, ctas) or a kernel mutant is set, and not on the record path. |
| 28 | * |
| 29 | * int omega_gpu_fp8gemm_prepare(uint32_t k); build and pin the kernel for width k now (serving). |
| 30 | * const char *omega_gpu_fp8gemm_rc_name(int rc); const char *omega_gpu_fp8gemm_last_error(void); |
| 31 | * |
| 32 | * Return codes: 0 OK; negative OMEGA_GPU_FP8GEMM_*. An uncertain completion latches the process (every later call of |
| 33 | * every API returns CHIP_FAIL). Every output word is poisoned before the launch and the call refuses a result that still |
| 34 | * holds poison. No host oracle runs inside the call. |
| 35 | */ |
| 36 | #include <stdbool.h> |
| 37 | #include <stddef.h> |
| 38 | #include <stdint.h> |
| 39 | #include "omega_gpu_fp8_api.h" |
| 40 | #include "omega_blackwell_codegen_gemm.h" |
| 41 | |
| 42 | #ifdef __cplusplus |
| 43 | extern "C" { |
| 44 | #endif |
| 45 | |
| 46 | enum { |
| 47 | OMEGA_GPU_FP8GEMM_OK = 0, |
| 48 | OMEGA_GPU_FP8GEMM_BAD_ARGS = -1, |
| 49 | OMEGA_GPU_FP8GEMM_TOO_LARGE = -2, |
| 50 | OMEGA_GPU_FP8GEMM_CODEGEN_FAIL = -3, |
| 51 | OMEGA_GPU_FP8GEMM_CHIP_FAIL = -4 |
| 52 | }; |
| 53 | #define OMEGA_GPU_FP8GEMM_MAX_ROWS 1024u |
| 54 | #define OMEGA_GPU_FP8GEMM_MAX_SEGMENTS 256u |
| 55 | |
| 56 | typedef struct { |
| 57 | uint32_t rows, segments, n, k, mt, warps_per_cta, lane_bytes, grid_x, grid_y, passes_max; |
| 58 | uint64_t weight_bytes; /* e4m3 bytes of the distinct experts, one pass each */ |
| 59 | uint64_t streamed_bytes; /* bytes the launch reads from the weights (sum over segments of passes * n * k) */ |
| 60 | uint64_t elapsed_ns; /* chip submit-to-second-marker */ |
| 61 | uint64_t call_ns; /* whole API call (host permute, launch, copy back) */ |
| 62 | uint32_t completion_marker; |
| 63 | bool kernel_cache_hit; |
| 64 | uint32_t general; /* 1: the call ran the general fallback, not the grouped GEMM kernel */ |
| 65 | } OmegaGpuFp8GemmInfo; |
| 66 | |
| 67 | int omega_gpu_fp8gemm_f32(const OmegaGpuFp8Tensor *t, uint32_t M, const uint32_t *expert_ids, const uint32_t *token_rows, |
| 68 | const float *x, uint32_t x_rows, float *y, OmegaGpuFp8GemmInfo *info); |
| 69 | int omega_gpu_fp8gemm_bf16(const OmegaGpuFp8Tensor *t, uint32_t M, const uint32_t *expert_ids, const uint32_t *token_rows, |
| 70 | const uint16_t *x, uint32_t x_rows, float *y, OmegaGpuFp8GemmInfo *info); |
| 71 | int omega_gpu_fp8gemm_prepare(uint32_t k); |
| 72 | const char *omega_gpu_fp8gemm_rc_name(int rc); |
| 73 | const char *omega_gpu_fp8gemm_last_error(void); |
| 74 | void omega_gpu_fp8gemm_cache_clear(void); |
| 75 | |
| 76 | /* Register-file check (declared = peak + 1 rounded up to 8; 32 * warps * declared <= 65536). */ |
| 77 | int omega_gpu_fp8gemm_kernel_fits(const OmegaFp8GemmSpec *spec, uint32_t *declared); |
| 78 | /* The shape the API picks for width k: rows per pass, warps per CTA, lane bytes. */ |
| 79 | void omega_gpu_fp8gemm_shape_for(uint32_t k, uint32_t *mt, uint32_t *warps, uint32_t *lane_bytes); |
| 80 | |
| 81 | /* ---- measurement knobs (each clears the kernel cache; 0 restores the automatic choice) ---- */ |
| 82 | void omega_gpu_fp8gemm_set_shape(uint32_t mt, uint32_t warps); |
| 83 | void omega_gpu_fp8gemm_set_cta_budget(uint32_t ctas_per_segment); |
| 84 | /* TEST ONLY: deliberately wrong kernels 1 (scale dropped), 2 (scale pointer never advances), 3 (neighbour row's x). */ |
| 85 | void omega_gpu_fp8gemm_test_set_mutant(int mutant); |
| 86 | |
| 87 | /* ---- the stroke's record path (L312 v2: one weight read per layer for every conversation on the chip) ---- |
| 88 | * omega_gpu_fp8gemm_record appends ONE launch of the grouped GEMM to cl; every buffer is the caller's, in device memory: |
| 89 | * seg_va: segs entries of OMEGA_GEMM_SEG_BYTES {w_va, s_va, row0, cnt} (omega_gpu_fp8_tensor_entry, or a kernel on |
| 90 | * the chip writes them); a segment with cnt 0 is skipped by its CTAs (the group kernel leaves trailing |
| 91 | * segments empty when fewer experts are distinct); |
| 92 | * xp_va: x' rows (k f32 each, permuted and * 2^64, omega_fp8gemm_permute_x) followed by mt rows of padding the |
| 93 | * kernel may read; y_va: rows * n f32; |
| 94 | * xmap_va, ymap_va: both 0 (x' row and y row of sorted row p are p) or both u32 tables of rows + mt entries: |
| 95 | * x' row XMAP[p] feeds sorted row p and its output lands in y row YMAP[p] (padding entries 0). With maps the |
| 96 | * kernel runs at mt <= 4 (rowmap at mt 8 does not fit the register file); mt 0 = the API's choice for segs. |
| 97 | * cta_budget 0 = the API's choice (omega_gpu_fp8gemm_cta_budget_for). rows: the y rows (num_elements), <= 1024. |
| 98 | * The caller holds omega_gpu_session_lock(). Nothing is staged, poisoned or read back (the list's per-launch semaphore |
| 99 | * and the caller's end-of-token checks replace the poison check). A kernel cache miss that would evict a resident kernel |
| 100 | * is refused (TOO_LARGE): call omega_gpu_fp8gemm_prepare_shape for the shape first (segs, mt, rowmap as recorded). */ |
| 101 | struct OmegaGpuCmdList; |
| 102 | int omega_gpu_fp8gemm_record(struct OmegaGpuCmdList *cl, const OmegaGpuFp8Tensor *t, uint32_t rows, uint32_t segs, uint64_t seg_va, |
| 103 | uint64_t xp_va, uint64_t y_va, uint64_t xmap_va, uint64_t ymap_va, uint32_t mt, uint32_t cta_budget, |
| 104 | OmegaGpuFp8GemmInfo *info); |
| 105 | int omega_gpu_fp8gemm_prepare_shape(uint32_t k, uint32_t segs, uint32_t mt, int rowmap); |
| 106 | uint32_t omega_gpu_fp8gemm_cta_budget_for(uint32_t segs); |
| 107 | |
| 108 | /* host helper: the permuted, 2^64-scaled x' row the kernel reads (k values in, k out) */ |
| 109 | void omega_fp8gemm_permute_x(const float *x, uint32_t k, uint32_t lane_bytes, float *out); |
| 110 | |
| 111 | #ifdef __cplusplus |
| 112 | } |
| 113 | #endif |
| 114 | #endif |