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_elementwise_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_elementwise_api.h` 189 lines · 11479 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 #ifndef OMEGA_GPU_ELEMENTWISE_API_H 3 #define OMEGA_GPU_ELEMENTWISE_API_H 4 /* 5 * omega_gpu_elementwise_api: native (no CUDA) elementwise and row-reduction 6 * kernels for the AIEN inference stack (FB-1 cut 4, 2026-10-04). 7 * 8 * Three inference ops plus two probe ops, every kernel generated by our own 9 * codegen (src/omega_blackwell_codegen.c IR) and launched through the same 10 * chip-proven sequence as the tensor matmul path: all buffers GPU-uncached 11 * (nvos.h NVOS32_ATTR2_GPU_CACHEABLE: system memory cached by the GPU is not 12 * coherent with CPU mappings), first marker, L2_FLUSH_DIRTY, second marker 13 * (C3 tail), poison-filled output scanned for unwritten words after readback. 14 * 15 * The math follows aien-sovereign-core crates/aien-inference-abi/src/tensor.rs 16 * (ReferenceCpuBackend), read 2026-10-04 at main `5fdab70`: 17 * rmsnorm: scale = 1/sqrt(mean(x^2) + eps) (oracle sums in f64; the chip 18 * sums in f32 and uses MUFU.RSQ refined by one Newton step), 19 * out[i] = (x[i] * scale) * weight[i] in that order. 20 * rope: rotate_half pairs (i, i + head_dim/2) per head, cos/sin tables 21 * supplied by the host as f32 (the oracle casts f64 cos/sin to f32 22 * before the f32 math): out0 = q0*cos - q1*sin, out1 = q0*sin + q1*cos, 23 * emitted as FMUL, FMUL, FADD (no fused multiply-add), so the chip 24 * result is bit-identical to the f32 oracle. 25 * swiglu: out = silu(gate) * up, silu(g) = g / (1 + exp(-g)); the chip uses 26 * MUFU.EX2(g * -log2(e)) and MUFU.RCP (approximate units), the 27 * oracle computes in f64. Tolerance is stated per op in the test. 28 * 29 * Cut 4b (2026-10-04): the device, channel and launch scratch are the process-wide 30 * session shared with the matmul and attention APIs (omega_gpu_session.h): one open 31 * per process instead of one per call (~40 ms), kernels resident after first use, 32 * staging buffers reused at their high-water mark. Calls serialise on one lock. 33 * 34 * Launch envelope: at most OMEGA_GPU_EW_MAX_CTAS CTAs per launch; the API splits 35 * larger work on the host and makes several launches. The cap was 64 from the 36 * 2026-10-03 matmul sweep that lost whole tiles at 128 CTAs (investigation I42, 37 * root cause the GPU-cached completion marker, fixed by the uncached marker + 38 * L2_FLUSH_DIRTY release sequence every launch here goes through). Cut L 39 * (2026-10-12): 1024, the CTA budget the matmul path has run every serving leg 40 * at since; a prefill swiglu of 128 x 12288 was 192 launches of 64 CTAs (1.4 ms 41 * of chip time per call), now 12. The poison scan of every output word is the 42 * lost-tile detector and stays. 43 */ 44 #include 45 #include 46 #include 47 #include "omega_blackwell_matmul.h" /* OmegaBlackwellKernel */ 48 49 #ifdef __cplusplus 50 extern "C" { 51 #endif 52 53 enum { 54 OMEGA_GPU_EW_OK = 0, 55 OMEGA_GPU_EW_BAD_ARGS = -1, /* NULL pointer, zero dimension, or a shape rule below broken */ 56 OMEGA_GPU_EW_TOO_LARGE = -2, /* dimension outside the codegen limits */ 57 OMEGA_GPU_EW_CODEGEN_FAIL = -3, /* our codegen refused or exceeded the register budget */ 58 OMEGA_GPU_EW_CHIP_FAIL = -4, /* device open, submit, marker or release failed */ 59 OMEGA_GPU_EW_UNWRITTEN = -5 /* output words still equal to their poison after the run */ 60 }; 61 62 /* Kernel families. The two probes exist to prove the new instructions on the 63 * chip with the smallest possible kernels (and are kept for regression). */ 64 typedef enum { 65 OMEGA_GPU_EW_EX2 = 1, /* out[i] = 2^a[i] (probe for MUFU.EX2) */ 66 OMEGA_GPU_EW_XCHG, /* out[i] = a[i ^ (T-1)] inside each CTA (probe for STS/BAR.SYNC/LDS) */ 67 OMEGA_GPU_EW_RMSNORM, 68 OMEGA_GPU_EW_ROPE, 69 OMEGA_GPU_EW_SWIGLU, 70 OMEGA_GPU_EW_PRIME_SIEVE, /* prime race (2026-10-05): word-parallel odd-only sieve, codegen only 71 * here; launched by bench/prime_race/gb10_native.c (argument words in 72 * gen_prime_sieve's comment) */ 73 OMEGA_GPU_EW_PRIME_SIEVE_SHARED, /* the same sieve with the table staged in shared memory (C3) */ 74 /* Structured warp reconvergence (omega #308). The four sieves below are the production sieve 75 * with its lane-dependent loops put back inside BSSY/BSYNC regions (regression R1): DIV1 wraps 76 * the original mark loop (omega `888a011`), DIV2 also wraps the prime loop (nested regions). */ 77 OMEGA_GPU_EW_PRIME_SIEVE_DIV1, 78 OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1, 79 OMEGA_GPU_EW_PRIME_SIEVE_DIV2, 80 OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2, 81 /* Probes for omega_gpu_reconv_probe_u32 (u32 in a, b; u32 out; oracle in tests/gpu_reconv_test.c) */ 82 OMEGA_GPU_EW_RC_DIAMOND, /* R3: if/else, sides of different length, SHFL after the join */ 83 OMEGA_GPU_EW_RC_LOOPBREAK, /* R4: loop with a lane-dependent break */ 84 OMEGA_GPU_EW_RC_NESTED, /* R5: a lane-dependent loop region inside an if region */ 85 OMEGA_GPU_EW_RC_EXIT_BAR, /* R6: lanes past the end + STS/BAR.SYNC/LDS exchange + SHFL, lanes exit last */ 86 OMEGA_GPU_EW_RC_DEPTH16, /* 16 nested regions: every barrier register B0..B15 in use */ 87 /* Codegen refusals: omega_gpu_elementwise_codegen_ir must return CODEGEN_FAIL for each */ 88 OMEGA_GPU_EW_RC_REFUSE_DEPTH17, /* a 17th nested region: no barrier register left */ 89 OMEGA_GPU_EW_RC_REFUSE_EXIT_SHFL, /* lanes EXIT, then BAR.SYNC/SHFL on the rest */ 90 OMEGA_GPU_EW_RC_REFUSE_EXIT_IN_REGION, /* EXIT inside an open region */ 91 OMEGA_GPU_EW_RC_REFUSE_BAR_IN_REGION, /* BAR.SYNC inside an open region */ 92 OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION, /* SHFL inside an open region */ 93 OMEGA_GPU_EW_RC_REFUSE_UNCLOSED, /* region begun, never joined */ 94 OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY /* BSYNC with no BSSY */ 95 } OmegaGpuEwOp; 96 97 /* Shape rules (checked, refused with BAD_ARGS / TOO_LARGE): 98 * rmsnorm: dim % OMEGA_GPU_EW_THREADS == 0, dim <= OMEGA_GPU_EW_MAX_DIM, rows >= 1 99 * rope: head_dim even, 2 <= head_dim <= 2048 (head_dim/2 threads per CTA), heads >= 1 100 * swiglu: n >= 1 (the kernel guards the tail with a predicated EXIT) 101 * ex2: n >= 1; xchg: n % OMEGA_GPU_EW_THREADS == 0 */ 102 #define OMEGA_GPU_EW_THREADS 128u 103 #define OMEGA_GPU_EW_MAX_DIM 16384u 104 #define OMEGA_GPU_EW_MAX_CTAS 1024u 105 106 typedef struct { 107 uint32_t chip_calls; /* launches made */ 108 uint64_t elapsed_ns; /* sum of launch-to-second-marker over the launches */ 109 uint32_t completion_marker; /* observed first marker word of the last launch */ 110 uint32_t unwritten_words; /* output words still equal to poison (0 on success) */ 111 bool kernel_cache_hit; /* the shape's kernel was already generated in this process */ 112 uint32_t gpr_count; /* registers the generated kernel uses */ 113 uint32_t insn_count; 114 uint32_t threads_per_cta; 115 uint32_t ctas_last_launch; 116 char target_chip[64]; 117 uint32_t sm_architecture; 118 uint64_t call_ns; /* cut 4b: wall time of the whole API call (staging, launches, copy-out) */ 119 } OmegaGpuEwInfo; 120 121 /* out[r*dim + i] = x[r*dim + i] * scale_r * weight[i]; scale_r = 1/sqrt(mean_i(x^2) + eps). */ 122 int omega_gpu_rmsnorm_f32(uint32_t rows, uint32_t dim, const float *x, const float *weight, 123 float eps, float *out, OmegaGpuEwInfo *info); 124 125 /* v holds `heads` heads of head_dim values back to back; cos_half and sin_half 126 * hold head_dim/2 values each (cos(pos*freq_i), sin(pos*freq_i)); out gets the 127 * rotated heads (may alias v: the API copies through its own buffers). */ 128 int omega_gpu_rope_f32(uint32_t heads, uint32_t head_dim, const float *v, 129 const float *cos_half, const float *sin_half, float *out, 130 OmegaGpuEwInfo *info); 131 132 /* out[i] = silu(gate[i]) * up[i] for i < n. */ 133 int omega_gpu_swiglu_f32(uint32_t n, const float *gate, const float *up, float *out, 134 OmegaGpuEwInfo *info); 135 136 /* Probes. ex2: out[i] = 2^x[i]. xchg: out[i] = x[i ^ (OMEGA_GPU_EW_THREADS-1)] within each CTA. */ 137 int omega_gpu_ex2_f32(uint32_t n, const float *x, float *out, OmegaGpuEwInfo *info); 138 int omega_gpu_shared_xchg_u32(uint32_t n, const uint32_t *x, uint32_t *out, OmegaGpuEwInfo *info); 139 /* Reconvergence probes (op = one of OMEGA_GPU_EW_RC_DIAMOND .. OMEGA_GPU_EW_RC_DEPTH16, else 140 * BAD_ARGS). n >= 1, any n: lanes past the end take part in the warp step and exit before 141 * their store. out[i] = r_i + 4099 * r_(i+1 in warp); r per op in src/omega_gpu_elementwise_api.c. */ 142 int omega_gpu_reconv_probe_u32(OmegaGpuEwOp op, uint32_t n, const uint32_t *a, const uint32_t *b, 143 uint32_t *out, OmegaGpuEwInfo *info); 144 145 /* Generate (without launching) the kernel for one op. Kernels read their shape 146 * from the argument words, so one kernel per op serves every shape; dims and eps 147 * are accepted for forward compatibility and ignored today (may be NULL / 0). 148 * Lets tests disassemble the bytes offline (nvdisasm) and check the register 149 * budget. Caller frees kernel->code. */ 150 int omega_gpu_elementwise_codegen(OmegaGpuEwOp op, const uint32_t *dims, float eps, 151 OmegaBlackwellKernel *kernel); 152 153 /* Same, with the mutant chosen by the caller (0 = production kernel, 1 = the op's math mutant, 154 * 2 = the reconvergence join (BSYNC) dropped, 3 = the BSSY names the wrong join; 2 and 3 change 155 * only kernels that open a region, the host simulator must catch both) and, when prog is 156 * not NULL, a copy of the allocated IR program (a BlackwellIRProgram, post register 157 * allocation) for a host IR simulator. Does not touch the device or the kernel cache. 158 * Caller frees kernel->code. */ 159 int omega_gpu_elementwise_codegen_ir(OmegaGpuEwOp op, int mutant, void *prog, 160 OmegaBlackwellKernel *kernel); 161 162 /* TEST ONLY. Selects a deliberately wrong kernel for one op (0 clears). Each 163 * mutant changes the math by far more than the op's tolerance, so a parity 164 * check that still passes against it proves nothing. Clears the kernel cache. 165 * EX2: computes 2^(x+1). XCHG: reads its own shared word (no exchange). 166 * RMSNORM: the warp tree drops its last SHFL step (half the lanes go missing). 167 * ROPE: out0 = q0*cos + q1*sin. SWIGLU: exp(+g) instead of exp(-g). */ 168 void omega_gpu_elementwise_test_set_mutant(int op); 169 /* TEST ONLY. Like test_set_mutant, choosing the mutant number (1 math, 2 join dropped, 3 join 170 * corrupted) that the next launch of `op` builds; lets a chip test observe what a missing 171 * reconvergence does (the simulator must have flagged it first). */ 172 void omega_gpu_elementwise_test_set_mutant_level(int op, int level); 173 /* TEST ONLY. Byte count run_1d stages for each chunk of an n-element call. 174 * Does not open a device or read the caller's buffers. bytes[i] is chunk i. 175 * The plan's full-chunk length is not reused for a shorter tail. */ 176 int omega_gpu_elementwise_test_stage_bytes(uint32_t n, size_t *bytes, uint32_t cap, 177 uint32_t *nchunks); 178 179 const char *omega_gpu_elementwise_rc_name(int rc); 180 void omega_gpu_elementwise_cache_clear(void); 181 182 /* Stage name of the most recent CHIP_FAIL ("" if none), shared with the other GPU APIs 183 * (omega_gpu_session_last_error); an open failure carries the driver text. */ 184 const char *omega_gpu_elementwise_last_error(void); 185 186 #ifdef __cplusplus 187 } 188 #endif 189 #endif