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_gdn_api.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #ifndef OMEGA_GPU_GDN_API_H |
| 3 | #define OMEGA_GPU_GDN_API_H |
| 4 | /* |
| 5 | * omega_gpu_gdn_api: the Gated DeltaNet decode step of Qwen3.8-27B in one launch per layer (omega#542, 2026-10-18). |
| 6 | * The kernel is described in omega_blackwell_codegen_gdn.h; the maths is sc qwen38.rs `delta_net`. |
| 7 | * |
| 8 | * int omega_gpu_gdn_alloc(uint32_t layers, OmegaGpuGdn **out); |
| 9 | * Device-resident per-layer parameters, conv tails and recurrent states (zeroed), plus the launch buffer. |
| 10 | * The tails and states are GPU-cached (2026-10-19; AIEN_OMEGA_GDN_STATE_CACHED=0 keeps them uncached): the host |
| 11 | * never touches them directly, reset zeroes them with a chip launch and set_state / get_state copy one layer through |
| 12 | * an uncached staging buffer with chip launches (omega_blackwell_codegen_gdn.h, state copy / zero). |
| 13 | * int omega_gpu_gdn_set_layer(OmegaGpuGdn *g, uint32_t layer, const float *conv_w, const float *a_log, |
| 14 | * const float *dt_bias, const float *norm_w); |
| 15 | * conv_w: linear_attn.conv1d.weight [10240][1][4] widened to f32 ([10240][4]); a_log, dt_bias: [48]; |
| 16 | * norm_w: linear_attn.norm.weight [128]. negA = -exp(a_log) is computed here (double, libm-free omega_hostmath_exp, rounded to float). |
| 17 | * int omega_gpu_gdn_set_state / omega_gpu_gdn_get_state(OmegaGpuGdn *g, uint32_t layer, float *tail, float *state); |
| 18 | * The reference layouts: tail [3][10240] (oldest first, qwen38.rs conv_tail), state [48][128 k][128 v]. |
| 19 | * get_state refuses (CHIP_FAIL) when the three per-head copies of a key head's q/k tail disagree. |
| 20 | * int omega_gpu_gdn_reset(OmegaGpuGdn *g); every layer's tail and state to zero (a new sequence) |
| 21 | * int omega_gpu_gdn_step(OmegaGpuGdn *g, uint32_t layer, const float *qkv, const float *z, const float *ab, |
| 22 | * float eps_l2, float eps_norm, float in_scale, float out_scale, float *out, |
| 23 | * OmegaGpuGdnInfo *info); |
| 24 | * qkv [10240] and z [6144] are the in_proj outputs (times 1 / in_scale: the kernel multiplies them by in_scale); |
| 25 | * ab [48][2] = {a[h], b[h]}: the in_proj_a and in_proj_b rows stacked interleaved (row 2h = a_h, 2h + 1 = b_h) |
| 26 | * give it from one bf16 GEMV. out [6144] = the out_proj input times out_scale. Updates the layer's tail and |
| 27 | * state on the device. eps_l2 is 1e-6 in the reference; eps_norm is rms_norm_eps. |
| 28 | * int omega_gpu_gdn_record(cl, g, layer, qkv_va, z_va, ab_va, eps_l2, eps_norm, in_scale, out_scale, &out_va); |
| 29 | * The same launch appended to a command list (caller holds omega_gpu_session_lock()); inputs are device |
| 30 | * addresses, out lands in the launch buffer (out_va). A kernel cache miss that would evict is refused. |
| 31 | * |
| 32 | * Every buffer is GPU-uncached host-visible session memory (nvos.h:1117 rule), launched through |
| 33 | * omega_gpu_session_launch like the router. The output is poisoned before the launch; a surviving poison word is |
| 34 | * CHIP_FAIL. |
| 35 | */ |
| 36 | #include "omega_blackwell_codegen_gdn.h" |
| 37 | #include <stddef.h> |
| 38 | #include <stdint.h> |
| 39 | |
| 40 | enum { |
| 41 | OMEGA_GPU_GDN_OK = 0, |
| 42 | OMEGA_GPU_GDN_BAD_ARGS = -1, |
| 43 | OMEGA_GPU_GDN_TOO_LARGE = -2, |
| 44 | OMEGA_GPU_GDN_CODEGEN_FAIL = -3, |
| 45 | OMEGA_GPU_GDN_CHIP_FAIL = -4 |
| 46 | }; |
| 47 | |
| 48 | #define OMEGA_GPU_GDN_MAX_LAYERS 64u |
| 49 | #define OMEGA_GPU_GDN_TAIL_FLOATS (3u * OMEGA_GDN_CONV_DIM) /* reference tail */ |
| 50 | #define OMEGA_GPU_GDN_STATE_FLOATS (OMEGA_GDN_VALUE_HEADS * OMEGA_GDN_HEAD_DIM * OMEGA_GDN_HEAD_DIM) /* 786432 */ |
| 51 | |
| 52 | typedef struct OmegaGpuGdn OmegaGpuGdn; |
| 53 | |
| 54 | typedef struct { |
| 55 | uint32_t layer, threads, grid_x, grid_y, insns, gprs; |
| 56 | uint64_t state_bytes; /* state bytes read and written: 2 * 48 * 128 * 128 * 4 */ |
| 57 | uint64_t elapsed_ns; /* marker-to-marker chip time */ |
| 58 | uint64_t call_ns; /* whole call */ |
| 59 | uint32_t completion_marker; |
| 60 | int kernel_cache_hit; |
| 61 | } OmegaGpuGdnInfo; |
| 62 | |
| 63 | int omega_gpu_gdn_alloc(uint32_t layers, OmegaGpuGdn **out); |
| 64 | /* Q36 (sc L306): the same with value heads per key head `ratio` (OmegaGdnSpec.ratio: 0 = 3, Qwen3.8-27B, the plain alloc; |
| 65 | * 2 = Qwen3.6-35B-A3B). Every size the other calls use (conv dim, value width, tail, state, parameter block, prefill |
| 66 | * rows) follows the ratio; the comments elsewhere in this header give the ratio-3 numbers. */ |
| 67 | int omega_gpu_gdn_alloc_ratio(uint32_t layers, uint32_t ratio, OmegaGpuGdn **out); |
| 68 | void omega_gpu_gdn_free(OmegaGpuGdn *g); |
| 69 | int omega_gpu_gdn_set_layer(OmegaGpuGdn *g, uint32_t layer, const float *conv_w, const float *a_log, const float *dt_bias, |
| 70 | const float *norm_w); |
| 71 | int omega_gpu_gdn_reset(OmegaGpuGdn *g); |
| 72 | int omega_gpu_gdn_set_state(OmegaGpuGdn *g, uint32_t layer, const float *tail, const float *state); |
| 73 | int omega_gpu_gdn_get_state(OmegaGpuGdn *g, uint32_t layer, float *tail, float *state); |
| 74 | int omega_gpu_gdn_step(OmegaGpuGdn *g, uint32_t layer, const float *qkv, const float *z, const float *ab, float eps_l2, |
| 75 | float eps_norm, float in_scale, float out_scale, float *out, OmegaGpuGdnInfo *info); |
| 76 | struct OmegaGpuCmdList; |
| 77 | int omega_gpu_gdn_record(struct OmegaGpuCmdList *cl, OmegaGpuGdn *g, uint32_t layer, uint64_t qkv_va, uint64_t z_va, |
| 78 | uint64_t ab_va, float eps_l2, float eps_norm, float in_scale, float out_scale, uint64_t *out_va); |
| 79 | /* Test helper: the out row the last launch wrote to the launch buffer (after a record + submit, for example). */ |
| 80 | int omega_gpu_gdn_read_out(OmegaGpuGdn *g, float *out); |
| 81 | const char *omega_gpu_gdn_last_error(void); |
| 82 | const char *omega_gpu_gdn_rc_name(int rc); |
| 83 | |
| 84 | /* ---- multi-token decode (L369, 2026-10-21): T decode steps of layer `layer` in ONE launch, bit for bit the decode step |
| 85 | * T times (omega_blackwell_codegen_gdn.h, multi-token decode). Token t reads qkv_va + t * xstride, z_va + t * xstride, |
| 86 | * ab_va + t * abstride and writes its [vdim] output row at out_va + t * ostride (bytes; 16-byte aligned rows, 8 for ab). |
| 87 | * int omega_gpu_gdn_multi_prepare(g); builds the kernel now (the record path refuses a kernel cache miss) |
| 88 | * int omega_gpu_gdn_multi_record(cl, g, layer, T, qkv_va, z_va, xstride, ab_va, abstride, out_va, ostride, eps_l2, |
| 89 | * eps_norm, in_scale, out_scale); appended to cl; the caller holds the session lock */ |
| 90 | int omega_gpu_gdn_multi_prepare(OmegaGpuGdn *g); |
| 91 | int omega_gpu_gdn_multi_record(struct OmegaGpuCmdList *cl, OmegaGpuGdn *g, uint32_t layer, uint32_t tokens, uint64_t qkv_va, |
| 92 | uint64_t z_va, uint32_t xstride, uint64_t ab_va, uint32_t abstride, uint64_t out_va, uint32_t ostride, |
| 93 | float eps_l2, float eps_norm, float in_scale, float out_scale); |
| 94 | |
| 95 | /* ---- prefill (omega#543, 2026-10-18): T >= 3 new tokens of one layer in three launches (prep, scan, post; see |
| 96 | * omega_blackwell_codegen_gdn.h), continuing from the layer's tail and state exactly as T decode steps would, and leaving |
| 97 | * them where the next prefill chunk or decode step picks them up. A long prompt is prefilled in chunks. |
| 98 | * int omega_gpu_gdn_prefill(g, layer, T, qkv [T][10240], z [T][6144], ab [T][48][2], eps_l2, eps_norm, in_scale, |
| 99 | * out_scale, out [T][6144], info); the decode step's row layouts, one row per token |
| 100 | * int omega_gpu_gdn_prefill_reserve(g, max_tokens); sizes the prefill scratch now and pins it (the record path needs |
| 101 | * it: a recorded launch must never see its scratch move); a later prefill of more tokens is refused |
| 102 | * int omega_gpu_gdn_prefill_record(cl, g, layer, T, qkv_va, z_va, ab_va, eps_l2, eps_norm, in_scale, out_scale, &out_va); |
| 103 | * the three launches appended to a command list (caller holds the lock); out_va = the [T][6144] rows in the scratch |
| 104 | * Scratch bytes for T tokens: T * (40960 + 3 * 24576 + 384) + ceil8(T) * 74496 (the per-token records dominate). */ |
| 105 | #define OMEGA_GPU_GDN_PF_MAX_T 65536u |
| 106 | |
| 107 | typedef struct { |
| 108 | uint32_t layer, tokens; |
| 109 | uint32_t insns[3], gprs[3]; /* prep, scan, post */ |
| 110 | uint64_t elapsed_ns[3]; /* marker-to-marker chip time per launch (host path) */ |
| 111 | uint64_t call_ns; |
| 112 | uint32_t kernel_cache_hits; /* 0..3 */ |
| 113 | } OmegaGpuGdnPfInfo; |
| 114 | |
| 115 | int omega_gpu_gdn_prefill(OmegaGpuGdn *g, uint32_t layer, uint32_t tokens, const float *qkv, const float *z, const float *ab, |
| 116 | float eps_l2, float eps_norm, float in_scale, float out_scale, float *out, OmegaGpuGdnPfInfo *info); |
| 117 | int omega_gpu_gdn_prefill_reserve(OmegaGpuGdn *g, uint32_t max_tokens); |
| 118 | int omega_gpu_gdn_prefill_record(struct OmegaGpuCmdList *cl, OmegaGpuGdn *g, uint32_t layer, uint32_t tokens, uint64_t qkv_va, |
| 119 | uint64_t z_va, uint64_t ab_va, float eps_l2, float eps_norm, float in_scale, float out_scale, |
| 120 | uint64_t *out_va); |
| 121 | /* Test helper: the out rows the last prefill wrote (after a record + submit). */ |
| 122 | int omega_gpu_gdn_prefill_read_out(OmegaGpuGdn *g, uint32_t tokens, float *out); |
| 123 | |
| 124 | /* Host layout helpers (no device; the test's simulator path uses them too). */ |
| 125 | /* The parameter block (OMEGA_GDN_PAR_BYTES) from the checkpoint tensors (see set_layer). -1 on a NULL pointer. */ |
| 126 | int omega_gdn_pack_params(const float *conv_w, const float *a_log, const float *dt_bias, const float *norm_w, uint8_t *block); |
| 127 | /* reference tail [3][10240] <-> device tail [48][384][4] (unpack: the q/k channels from head 3 * hk; -1 when the |
| 128 | * three copies of a key head disagree bit for bit) */ |
| 129 | void omega_gdn_pack_tail(const float *tail, float *dev); |
| 130 | int omega_gdn_unpack_tail(const float *dev, float *tail); |
| 131 | /* The same for a ratio (see omega_gpu_gdn_alloc_ratio); -1 on an unsupported ratio. */ |
| 132 | int omega_gdn_pack_params_r(uint32_t ratio, const float *conv_w, const float *a_log, const float *dt_bias, const float *norm_w, uint8_t *block); |
| 133 | int omega_gdn_pack_tail_r(uint32_t ratio, const float *tail, float *dev); |
| 134 | int omega_gdn_unpack_tail_r(uint32_t ratio, const float *dev, float *tail); |
| 135 | |
| 136 | void omega_gpu_gdn_test_set_mutant(int mutant); /* TEST ONLY: OmegaGdnSpec.mutant; clears the kernel cache */ |
| 137 | |
| 138 | #endif |