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_tern.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_tern.h` 136 lines · 9382 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 #ifndef OMEGA_BLACKWELL_CODEGEN_TERN_H 3 #define OMEGA_BLACKWELL_CODEGEN_TERN_H 4 /* 5 * omega_blackwell_codegen_tern: KT1, the W2A8 ternary GEMV (m = 1) for the BitNet b1.58 lane (ternary engine design 6 * stage T0, 2026-10-15). New file; same emitter rules as omega_blackwell_codegen_bf16.c (every instruction waits on 7 * everything still pending, loads of one iteration issued back to back). 8 * 9 * y[r] = sum_c W[r][c] * x[c], W in {-1, 0, +1} (omega's 2-bit trit code), x int8, y int32, EXACT 10 * (the contract of oma_rz_oracle, src/algebra/realize_common.h). Optional f32 epilogue (below). 11 * 12 * Trit code (src/algebra/oma_trit.h): bit0 = plus plane, bit1 = minus plane, 0b11 invalid (the uploader refuses it). 13 * A 32-bit word holds 16 trits of one row, columns 16c .. 16c+15, trit i at bits 2i, 2i+1. 14 * 15 * Packed layouts (the uploader writes whichever the tensor was created with; n is padded with zero rows to a multiple 16 * of 8, kw = k / 16 words per row): 17 * OMEGA_TERN_LAYOUT_ROWMAJOR (0): word c of row r at byte (r * kw + c) * 4. 18 * OMEGA_TERN_LAYOUT_BLOCK (1): rows in blocks of 2R (R = spec.rows, 2 or 4); in block b, word c of block row j 19 * (j = h * R + i, h = 0/1) at byte b * 2R * kw * 4 + c * 2R * 4 + j * 4. One 20 * LDG.64 / LDG.128 gives a lane word c of R rows; a warp's step is 2R * 64 contiguous bytes. 21 * 22 * Thread mapping: a warp is two half-warps of 16 lanes (h = lane >> 4, l = lane & 15). Unit u = 2R consecutive rows; 23 * half h owns rows u * 2R + h * R + i (i < R). Per K step s (KS = k / 256 steps) lane l reads word c = 16 s + l of each of 24 * its rows. Warp w of CTA cx handles units cx * warps + w, + stride, ... < U (U = padded n / 2R). 25 * 26 * Arithmetic (exact, integer only; IMAD low 32 bits): the CTA stages x in shared memory as, per word c and pair 27 * j < 8, X_j = x[16c+j] * 65536 + x[16c+j+8] (signed): 32 bytes per c, 2 bytes per column. For a weight word W and 28 * pair j: U = W >> 2j, P = U & 0x00010001 (plus bits of trits j and j+8 at bits 0 and 16), M = U & 0x00020002 (minus 29 * bits at 1 and 17). accp += P * X_j, accm += M * X_j, both from K0 = 2^31 + 2^15. Mod 2^32 the high half of accp 30 * collects HP = sum (p_j x_j + p_{j+8} x_{j+8}) and its low half |LP| <= 1024 never borrows, so accp >> 16 (logical) is 31 * exactly 2^15 + HP; likewise accm >> 17 is exactly 2^14 + HM. Each lane adds (2^15 + HP) - (2^14 + HM) = 2^14 + dot 32 * per word to an int32 total T; a SHFL.DOWN tree (8, 4, 2, 1) sums its half; y = T - KSC * 2^18 (mod 2^32, exact 33 * because |y| <= 128 * k < 2^31). 34 * 35 * Shared memory: the kernel keeps to OMEGA_TERN_SHARED_MAX (16 KiB) per CTA, the launch descriptor cap when KT1 was 36 * measured (OMEGA_BW_QMD_MAX_SHARED_BYTES is 32 KiB since `ce155d47`, omega_blackwell_qmd.h; the budget stays at the 37 * measured 16 KiB). x takes 2 bytes per column in shared memory, so one chunk of OMEGA_TERN_CHUNK_MAX columns uses 38 * 16 KiB, and x is staged 39 * in chunks of at most OMEGA_TERN_CHUNK_MAX = 8192 columns (k <= 8192: one chunk; 8192 < k <= 16384: two equal chunks, 40 * k % 512 == 0). With two chunks each lane l == 0 adds the earlier chunk's int32 y it stored itself (same thread, same 41 * address, program order); the f32 epilogue is refused there. 42 * 43 * Output path (single chunk, omega_tern_gemv_coalesced_out): every warp of a CTA works on consecutive units in a round 44 * (unit = CTA * W + warp + round * stride, all warps of a CTA take the same number of rounds; a warp past the last unit 45 * recomputes unit U - 1 and its rows fall at or beyond n_padded, never stored). Lane l == 0 of each half-warp writes the 46 * row's result into a shared slot, one CTA barrier, then warp 0 lanes 0 .. 2 R W - 1 store the round's 2 R W consecutive 47 * rows (those < n) with one coalesced STG. Two slot sets alternate by round so one barrier per round suffices. Measured 48 * reason (investigation 2026-10-16-kt1-speed): the per-row 4-byte GPU-uncached stores cost 9 to 11 us per launch. 49 * Chunked kernels keep the per-row store. Coalescing needs 2 R W <= 32 (warp 0 stores the round), so R = 4 with W = 8 50 * (the block layout's usual shape) keeps the per-row store too; the speed receipts are row-major R = 1. 51 * 52 * Epilogue: 53 * OMEGA_TERN_EPI_I32 (0): y int32. 54 * OMEGA_TERN_EPI_F32 (1): y f32 = ((float)y_int * a_scale) * w_scale[seg(r)], two round-to-nearest FMULs in that order. 55 * a_scale: one f32 device word (the activation dequantisation step, 1 / s_a; the producer writes it). 56 * seg(r) = number of boundaries b0 <= b1 <= b2 that are <= r (the API passes -b_i). w_scale: 4 f32 device words 57 * stored REVERSED (ws_rev[i] = w_scale[3 - i]) so the address is ws_rev + 4 * (count of boundaries above r). 58 * Per output segment, explicit boundaries (q|k|v: 2560, 3200, 3840): never ws_idx = r / (n / segments). 59 * 60 * Constant bank 0 argument words (cbank + 0x380): 61 * 0x380 w u64 packed weights 0x388 x u64 int8 x row (16-byte aligned, k bytes) 62 * 0x390 y u64 y (n words) 0x398 ws u64 w_scale reversed (f32 [4], EPI_F32) 63 * 0x3a0 as u64 a_scale (f32 word, EPI_F32) 64 * 0x3a8 U u32 units 0x3ac stride u32 units per warp iteration (grid_x * warps) 65 * 0x3b0 negn u32 -n (store rows < n) 0x3b4, 0x3b8, 0x3bc: -b0, -b1, -b2 66 * 67 * mutant (tests only): 1 flips the planes (the staged X_j are negated: plus acts as minus, y becomes -y), 2 is host-side only 68 * (the API maps segments as ws_idx = r / (n / segments)), 3 drops pair j = 7 (trits 7 and 15 of every word), 4 emits the 69 * pre-fix I2FP (fixed stall 6, no scoreboard): the generator must refuse it (omega_tern_i2fp_violations, Sass#4). 70 * 5 uses one output slot set (the round race, simulator with a reversed warp order; omega-mv train r4 calls this mutant 4). 71 * 6 drops the unit clamp of the coalesced path (omega-mv train r4 calls this mutant 5). Mutant 4 stayed the I2FP refusal 72 * because that negative control already ships here. 73 */ 74 #include 75 #include "omega_blackwell_codegen.h" 76 #include "omega_blackwell_qmd.h" 77 78 #ifdef __cplusplus 79 extern "C" { 80 #endif 81 82 #define OMEGA_TERN_MAX_K 16384u 83 #define OMEGA_TERN_CHUNK_MAX 8192u 84 /* Cycles every user stays behind a fixed-latency producer (chip-proven rule: >= 5; omega_gpu_elementwise_api.c:13). 85 * The generator sets each stall from the register dependencies after allocation (tern_schedule_stalls). */ 86 #define OMEGA_TERN_FIXED_LAT 6u 87 /* Cycles every reader stays behind an I2FP write (Sass#4 chip sweep lat-20261027T025808Z: i2fp->fmul 18, i2f->fmul 19). 88 * I2FP carries no scoreboard (a wait on one did not hold its reader on the GB10); a program with a reader closer than 89 * this is refused (omega_tern_i2fp_violations). */ 90 #define OMEGA_TERN_I2FP_LAT 19u 91 #define OMEGA_TERN_LAYOUT_ROWMAJOR 0 92 #define OMEGA_TERN_LAYOUT_BLOCK 1 93 #define OMEGA_TERN_EPI_I32 0 94 #define OMEGA_TERN_EPI_F32 1 95 #define OMEGA_TERN_ARG_W 0x380u 96 #define OMEGA_TERN_ARG_X 0x388u 97 #define OMEGA_TERN_ARG_Y 0x390u 98 #define OMEGA_TERN_ARG_WS 0x398u 99 #define OMEGA_TERN_ARG_AS 0x3a0u 100 #define OMEGA_TERN_ARG_U 0x3a8u 101 #define OMEGA_TERN_ARG_STRIDE 0x3acu 102 #define OMEGA_TERN_ARG_NEGN 0x3b0u 103 #define OMEGA_TERN_ARG_NEGB 0x3b4u 104 #define OMEGA_TERN_ARGS_WORDS 16u 105 #define OMEGA_TERN_ROW_PAD 8u 106 107 typedef struct { 108 uint32_t k; /* multiple of 256 (512 above 8192), <= OMEGA_TERN_MAX_K */ 109 uint32_t layout; /* OMEGA_TERN_LAYOUT_* */ 110 uint32_t rows; /* rows per half-warp R: row-major 1, 2, 4; block 2, 4 */ 111 uint32_t warps; /* 1 .. 32 */ 112 uint32_t depth; /* K steps per loop iteration: divides chunk / 256; rows * depth <= 16 */ 113 uint32_t epi; /* OMEGA_TERN_EPI_* */ 114 int mutant; /* 0 production; 1, 3 kernel mutants (2 is host-side); 4 refused (I2FP stall 6); 5 one slot set; 6 no unit clamp */ 115 } OmegaTernGemvSpec; 116 117 int omega_blackwell_codegen_tern_gemv_prog(const OmegaTernGemvSpec *spec, BlackwellIRProgram *prog); 118 int omega_tern_gemv_spec_ok(const OmegaTernGemvSpec *spec); 119 uint32_t omega_tern_gemv_steps(const OmegaTernGemvSpec *spec); /* k / 256 (0 when refused) */ 120 uint32_t omega_tern_gemv_shared_bytes(const OmegaTernGemvSpec *spec); /* 2 * chunk width, plus two output slot sets when coalesced */ 121 int omega_tern_gemv_coalesced_out(const OmegaTernGemvSpec *spec); /* 1 when the round's rows leave through shared memory in one store */ 122 uint32_t omega_tern_gemv_out_slot_bytes(const OmegaTernGemvSpec *spec); /* 2 * rows * warps * 4: one slot set */ 123 #define OMEGA_TERN_SHARED_MAX 16384u /* the measured KT1 budget, at most the launch descriptor cap */ 124 _Static_assert(OMEGA_TERN_SHARED_MAX <= OMEGA_BW_QMD_MAX_SHARED_BYTES, "the KT1 shared budget must fit the launch descriptor"); 125 uint32_t omega_tern_gemv_chunks(const OmegaTernGemvSpec *spec); /* x chunks: 1 for k <= 8192, else 2 */ 126 int omega_tern_gemv_prog_stats(const OmegaTernGemvSpec *spec, uint32_t *insns, uint32_t *gprs); 127 /* I2FP consumer violations in a scheduled program (see the .c); the generator returns -1 when this is not 0. */ 128 long omega_tern_i2fp_violations(const BlackwellIRProgram *prog); 129 130 /* Byte offset of word c of padded row r in a packed tensor (layout, R = rows for the block layout). */ 131 uint64_t omega_tern_word_offset(uint32_t layout, uint32_t R, uint32_t k, uint32_t r, uint32_t c); 132 133 #ifdef __cplusplus 134 } 135 #endif 136 #endif