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_blackwell_codegen_moe.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_blackwell_codegen_moe.h` 141 lines · 10309 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 #ifndef OMEGA_BLACKWELL_CODEGEN_MOE_H 3 #define OMEGA_BLACKWELL_CODEGEN_MOE_H 4 /* 5 * omega_blackwell_codegen_moe: FP8 (e4m3) GEMV kernel family for the OWN-30B MoE lane 6 * (Cut 6 + Cut 5, 2026-10-13). New file: nothing in omega_blackwell_codegen.{c,h} changes. 7 * 8 * One kernel: y[g][r] = sum_k W_g[r][k] * x[g or 0][k] * S_g[r/128][k/128], m = 1 (decode), for 9 * G groups (experts) in one launch. Weights are e4m3 bytes in the checkpoint's row-major 10 * [n][k] order (n output rows, k input columns), scales are the checkpoint's 128 x 128 11 * `weight_scale_inv` grid, stored on the device as f32 (times 2^OMEGA_FP8_SCALE_EXP, see below). 12 * 13 * Numeric scheme (hand-checked bit arithmetic, proven exhaustively in tests/gpu_fp8_api_test.c): 14 * f32 bits ((b & 0x7F) << 20) | ((b & 0x80) << 24) equal e4m3(b) * 2^-120 exactly, for normals, 15 * subnormals and both zeros (bf16 field exponent 0 with the 3 mantissa bits shifted reproduces 16 * 2^-6 * 0.M). The e4m3 NaN codes 0x7F / 0xFF decode wrong (a finite number): the upload refuses 17 * them. The 2^120 is folded on the host: x' = x * 2^OMEGA_FP8_X_EXP and S' = S * 2^OMEGA_FP8_SCALE_EXP 18 * (64 + 56 = 120), chosen so that every product w' * x' is a normal f32 for any practical x. 19 * The kernel accumulates in f32: per lane, a dot of lane_bytes weights (FFMA chain), then 20 * acc_r = fma(dot, S', acc_r) once per 128-block, then a warp reduction (SHFL.DOWN) per row. 21 * 22 * Thread mapping: a CTA has `warps` warps (threads_x = 32 * warps, threads_y = 1); warp w of CTA 23 * (cx, cy) handles group cy and row groups rg = cx * warps + w, rg + stride, ... < RG (RG = n / rows, 24 * stride = grid_x * warps, both launch arguments). A row group is `rows` consecutive rows, inside 25 * one 128-row scale block (rows divides 128). Per K step a warp reads 32 * lane_bytes contiguous 26 * bytes of each of its rows (lane L: bytes [L * lane_bytes, +lane_bytes)), the same lane_bytes 27 * floats of x', and one scale word per lane (the block of its chunk; 128 / lane_bytes lanes share 28 * it). Step count KS = k / (32 * lane_bytes) is baked into the kernel; k must be a multiple of 29 * 32 * lane_bytes and of 128. 30 * 31 * Constant bank 0 argument words (cbank + 0x380, as the other APIs): 32 * 0x380 tab u64 group table: entry g at tab + 16 g = { w_va u64, s_va u64 } 33 * 0x388 x u64 x' rows (f32, k each; row g at x + g * x_stride) 34 * 0x390 y u64 y rows (f32, n each; row g at y + g * y_stride) 35 * 0x398 RG u32 row groups per group (n / rows) 36 * 0x39c x_stride u32 bytes between x rows (0: one x row shared by every group) 37 * 0x3a0 y_stride u32 bytes between y rows 38 * 0x3a4 stride u32 row groups per warp iteration (grid_x * warps) 39 * Grid: grid_x CTAs per group, grid_y = groups. The kernel does not depend on n, RG, the strides 40 * or the grid, so one kernel per (k, lane_bytes, rows, warps, mutant) serves every n and G. 41 * 42 * Only lane 0 of a warp stores (R words per row group) inside a BSSY/BSYNC region; the warp is 43 * whole again for the next row group's SHFLs. 44 * 45 * mutant (tests only, never production): 1 drops the block scale (acc += dot), 2 never advances the 46 * scale pointer (every K step uses the step-0 block: wrong whenever KS >= 2), 3 reads the group 47 * table entry of group cy ^ 1 (swapped bases). Each must be caught by the parity test. 48 * 49 * Instruction encodings: every IR op used here already exists in omega_blackwell_codegen.h; no new 50 * encodings were added. ISETP_GE (immediate) and IADD3 carry fixed control words in the encoder, 51 * so this generator uses ISETP_GE_U32 (register) and IMAD for adds instead. 52 */ 53 #include 54 #include "omega_blackwell_codegen.h" 55 56 #ifdef __cplusplus 57 extern "C" { 58 #endif 59 60 #define OMEGA_FP8_X_EXP 64 /* x' = x * 2^64 (host, exact) */ 61 #define OMEGA_FP8_SCALE_EXP 56 /* S' = S * 2^56 (host, exact for every finite bf16 scale below 2^72) */ 62 #define OMEGA_FP8_BLOCK 128u /* scale block (rows and columns) */ 63 #define OMEGA_FP8_MAX_K 32768u /* the step kernel and the tensor-core kernel loop over k; 17408 is the Qwen3.8-27B down_proj (omega#541) */ 64 #define OMEGA_FP8_MAX_GROUPS 64u /* group table entries per launch buffer (8 experts today) */ 65 66 /* argument word offsets */ 67 #define OMEGA_FP8_ARG_TAB 0x380u 68 #define OMEGA_FP8_ARG_X 0x388u 69 #define OMEGA_FP8_ARG_Y 0x390u 70 #define OMEGA_FP8_ARG_RG 0x398u 71 #define OMEGA_FP8_ARG_XSTRIDE 0x39cu 72 #define OMEGA_FP8_ARG_YSTRIDE 0x3a0u 73 #define OMEGA_FP8_ARG_STRIDE 0x3a4u 74 #define OMEGA_FP8_ARGS_WORDS 10u 75 76 typedef struct { 77 uint32_t k; /* input columns: multiple of 128 and of 32 * lane_bytes, <= OMEGA_FP8_MAX_K */ 78 uint32_t lane_bytes; /* 8 or 16 weight bytes per lane per step (LDG.E.64 / LDG.E.128); k must be a multiple of 256 */ 79 uint32_t rows; /* rows per warp: 1, 2, 4 or 8 */ 80 uint32_t warps; /* warps per CTA: 1 .. 32 (the host simulator runs up to 8) */ 81 uint32_t depth; /* 0: one K step per loop iteration, x read from global memory each step. 82 * D >= 1: "deep" kernel: the CTA stages its group's x' row in shared memory once 83 * (k * 4 bytes, BAR.SYNC), then every loop iteration issues the weight and scale 84 * loads of D consecutive K steps before using any of them (D x more bytes in 85 * flight per warp); x comes from shared memory (LDS.128). Needs KS % D == 0, 86 * rows * D * lane_bytes <= 128, k % (32 * warps) == 0 and k <= 512 * warps (staging rounds). */ 87 int mutant; /* 0 production; 1, 2, 3 as above; 4, 5 with coal = 1 (below) */ 88 uint32_t sched; /* 0 (default, chip-proven): every fixed-latency instruction stalls 6, one byte decoded at a time. 89 * 1 (CUT5, measurement variant): the bytes of a step are decoded in a 3-deep software pipeline with 90 * their own temporaries, and a pass after register allocation (omega_fp8_schedule_stalls) gives each 91 * instruction the smallest stall that keeps every user >= OMEGA_FP8_FIXED_LAT cycles behind each 92 * fixed-latency write (the repo rule, omega_gpu_elementwise_api.c:13). The FFMA order is unchanged, 93 * so y is bit-identical to sched 0. Appended last: earlier fields keep their offsets. */ 94 uint32_t coal; /* 0 (default): lane 0 of each warp stores its R row results (4-byte STG each) to y. 95 * 1 (CUT5 variant A, after omega#490 KT1): the CTA works in rounds (row groups uc .. uc + W - 1, 96 * uc uniform); lane 0 of each warp parks its R results in shared slots (two slot sets alternating by 97 * round, one BAR.SYNC per round) and warp 0 stores the round's W * R consecutive rows with one 98 * coalesced STG. A warp past the last row group recomputes row group RG - 1 and its slot is never 99 * stored. Needs warps * rows <= 32. Test mutants: 4 = one slot set (the round race), 5 = no clamp. */ 100 uint32_t wfirst; /* 0 (default): each K step issues the x' loads, then the scale address IMADs, then the weight and 101 * scale loads. The IMADs wait on the x' loads' scoreboard (every fixed-latency op waits on the pending 102 * loads, emit()), so each step pays the x' round trip before any weight load goes out. 103 * 1 (CUT5 variant B): scale addresses first, then weight, scale and x' loads in one batch with no wait 104 * between them. Ignored by deep kernels (depth >= 1), which read x' from shared memory. */ 105 } OmegaFp8GemvSpec; 106 107 #define OMEGA_FP8_FIXED_LAT 6u /* cycles a fixed-latency result needs before a user (the repo keeps 6; hardware minimum 5) */ 108 109 /* 0 on success (prog solved and register-allocated), -1 on a refused spec or an allocator failure. */ 110 int omega_blackwell_codegen_fp8_gemv_prog(const OmegaFp8GemvSpec *spec, BlackwellIRProgram *prog); 111 112 /* Hosted build: allocate and encode (omega_blackwell_kernel_free releases). */ 113 int omega_blackwell_codegen_fp8_gemv(const OmegaFp8GemvSpec *spec, OmegaBlackwellKernel *kernel); 114 115 /* K steps the kernel runs: k / (32 * lane_bytes). 0 when the spec is refused. */ 116 uint32_t omega_fp8_gemv_steps(const OmegaFp8GemvSpec *spec); 117 118 /* Shared memory bytes the launch must declare: k * 4 + 16 for a deep kernel, 0 otherwise, plus 2 * warps * rows * 4 119 * (the two output slot sets) when coal = 1. */ 120 uint32_t omega_fp8_gemv_shared_bytes(const OmegaFp8GemvSpec *spec); 121 122 /* The IR instruction count and peak GPR count of the solved program (for budgets in tests). */ 123 int omega_fp8_gemv_prog_stats(const OmegaFp8GemvSpec *spec, uint32_t *insns, uint32_t *gprs); 124 125 /* ---- ragged k (Qwen3.8-Flash-Next expert down_proj, k = 640) ---- 126 * The four functions below do what their plain twins above do and additionally accept a spec whose k is a multiple of 128 but not of 127 * 32 * lane_bytes (a k below 32 * lane_bytes included: one lane-masked step), depth 0, variant 1 (the row-pipelined variant 2 refuses it). The OmegaFp8GemvSpec 128 * struct is unchanged, so every existing spec keeps its layout; the plain functions keep refusing such a k. For every spec the plain 129 * functions accept, these return the same program, step count and shared bytes. A ragged kernel runs floor(k / (32 * lane_bytes)) full 130 * steps and one more step, in which only the first (k mod 32 * lane_bytes) / lane_bytes lanes load, decode and accumulate (a BSSY 131 * region skipped by the others, selected per iteration by lane * (steps left < 2)); each active lane reads its own 128-column scale 132 * block, exactly as in a full step. Test mutants (ragged specs only): 6 = tail not masked, 7 = tail step never run. */ 133 int omega_blackwell_codegen_fp8_gemv_ragged_prog(const OmegaFp8GemvSpec *spec, BlackwellIRProgram *prog); 134 uint32_t omega_fp8_gemv_ragged_steps(const OmegaFp8GemvSpec *spec); /* floor(k / (32 * lane_bytes)) + 1 for a ragged k */ 135 uint32_t omega_fp8_gemv_ragged_shared_bytes(const OmegaFp8GemvSpec *spec); 136 int omega_fp8_gemv_ragged_prog_stats(const OmegaFp8GemvSpec *spec, uint32_t *insns, uint32_t *gprs); 137 138 #ifdef __cplusplus 139 } 140 #endif 141 #endif