interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / src / omega_blackwell_codegen_tern.h

142 lines · 9147 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef OMEGA_BLACKWELL_CODEGEN_TERN_H
18#define OMEGA_BLACKWELL_CODEGEN_TERN_H
19/*
20 * omega_blackwell_codegen_tern: KT1, the W2A8 ternary GEMV (m = 1) for the BitNet b1.58 lane (ternary engine design
21 * stage T0, 2026-10-15). New file; same emitter rules as omega_blackwell_codegen_bf16.c (every instruction waits on
22 * everything still pending, loads of one iteration issued back to back).
23 *
24 * 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
25 * (the contract of oma_rz_oracle, src/algebra/realize_common.h). Optional f32 epilogue (below).
26 *
27 * Trit code (src/algebra/oma_trit.h): bit0 = plus plane, bit1 = minus plane, 0b11 invalid (the uploader refuses it).
28 * A 32-bit word holds 16 trits of one row, columns 16c .. 16c+15, trit i at bits 2i, 2i+1.
29 *
30 * Packed layouts (the uploader writes whichever the tensor was created with; n is padded with zero rows to a multiple
31 * of 8, kw = k / 16 words per row):
32 * OMEGA_TERN_LAYOUT_ROWMAJOR (0): word c of row r at byte (r * kw + c) * 4.
33 * 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
34 * (j = h * R + i, h = 0/1) at byte b * 2R * kw * 4 + c * 2R * 4 + j * 4. One
35 * LDG.64 / LDG.128 gives a lane word c of R rows; a warp's step is 2R * 64 contiguous bytes.
36 *
37 * Thread mapping: a warp is two half-warps of 16 lanes (h = lane >> 4, l = lane & 15). Unit u = 2R consecutive rows;
38 * 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
39 * its rows. Warp w of CTA cx handles units cx * warps + w, + stride, ... < U (U = padded n / 2R).
40 *
41 * Arithmetic (exact, integer only; IMAD low 32 bits): the CTA stages x in shared memory as, per word c and pair
42 * 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
43 * 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
44 * 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
45 * 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
46 * exactly 2^15 + HP; likewise accm >> 17 is exactly 2^14 + HM. Each lane adds (2^15 + HP) - (2^14 + HM) = 2^14 + dot
47 * 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
48 * because |y| <= 128 * k < 2^31).
49 *
50 * Shared memory: the kernel keeps to OMEGA_TERN_SHARED_MAX (16 KiB) per CTA, the launch descriptor cap when KT1 was
51 * measured (OMEGA_BW_QMD_MAX_SHARED_BYTES is 32 KiB since ce155d47, omega_blackwell_qmd.h; the budget stays at the
52 * measured 16 KiB). x takes 2 bytes per column in shared memory, so one chunk of OMEGA_TERN_CHUNK_MAX columns uses
53 * 16 KiB, and x is staged
54 * in chunks of at most OMEGA_TERN_CHUNK_MAX = 8192 columns (k <= 8192: one chunk; 8192 < k <= 16384: two equal chunks,
55 * k % 512 == 0). With two chunks each lane l == 0 adds the earlier chunk's int32 y it stored itself (same thread, same
56 * address, program order); the f32 epilogue is refused there.
57 *
58 * Output path (single chunk, omega_tern_gemv_coalesced_out): every warp of a CTA works on consecutive units in a round
59 * (unit = CTA * W + warp + round * stride, all warps of a CTA take the same number of rounds; a warp past the last unit
60 * recomputes unit U - 1 and its rows fall at or beyond n_padded, never stored). Lane l == 0 of each half-warp writes the
61 * 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
62 * rows (those < n) with one coalesced STG. Two slot sets alternate by round so one barrier per round suffices. Measured
63 * reason (investigation 2026-10-16-kt1-speed): the per-row 4-byte GPU-uncached stores cost 9 to 11 us per launch.
64 * Chunked kernels keep the per-row store. Coalescing needs 2 R W <= 32 (warp 0 stores the round), so R = 4 with W = 8
65 * (the block layout's usual shape) keeps the per-row store too; the speed receipts are row-major R = 1.
66 *
67 * Epilogue:
68 * OMEGA_TERN_EPI_I32 (0): y int32.
69 * OMEGA_TERN_EPI_F32 (1): y f32 = ((float)y_int * a_scale) * w_scale[seg(r)], two round-to-nearest FMULs in that order.
70 * a_scale: one f32 device word (the activation dequantisation step, 1 / s_a; the producer writes it).
71 * seg(r) = number of boundaries b0 <= b1 <= b2 that are <= r (the API passes -b_i). w_scale: 4 f32 device words
72 * stored REVERSED (ws_rev[i] = w_scale[3 - i]) so the address is ws_rev + 4 * (count of boundaries above r).
73 * Per output segment, explicit boundaries (q|k|v: 2560, 3200, 3840): never ws_idx = r / (n / segments).
74 *
75 * Constant bank 0 argument words (cbank + 0x380):
76 * 0x380 w u64 packed weights 0x388 x u64 int8 x row (16-byte aligned, k bytes)
77 * 0x390 y u64 y (n words) 0x398 ws u64 w_scale reversed (f32 [4], EPI_F32)
78 * 0x3a0 as u64 a_scale (f32 word, EPI_F32)
79 * 0x3a8 U u32 units 0x3ac stride u32 units per warp iteration (grid_x * warps)
80 * 0x3b0 negn u32 -n (store rows < n) 0x3b4, 0x3b8, 0x3bc: -b0, -b1, -b2
81 *
82 * 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
83 * (the API maps segments as ws_idx = r / (n / segments)), 3 drops pair j = 7 (trits 7 and 15 of every word), 4 uses one
84 * output slot set (the round race, simulator with a reversed warp order), 5 drops the unit clamp of the coalesced path.
85 */
86#include <stdint.h>
87#include "omega_blackwell_codegen.h"
88#include "omega_blackwell_qmd.h"
89
90#ifdef __cplusplus
91extern "C" {
92#endif
93
94#define OMEGA_TERN_MAX_K 16384u
95#define OMEGA_TERN_CHUNK_MAX 8192u
96/* Cycles every user stays behind a fixed-latency producer (chip-proven rule: >= 5; omega_gpu_elementwise_api.c:13).
97 * The generator sets each stall from the register dependencies after allocation (tern_schedule_stalls). */
98#define OMEGA_TERN_FIXED_LAT 6u
99#define OMEGA_TERN_LAYOUT_ROWMAJOR 0
100#define OMEGA_TERN_LAYOUT_BLOCK 1
101#define OMEGA_TERN_EPI_I32 0
102#define OMEGA_TERN_EPI_F32 1
103#define OMEGA_TERN_ARG_W 0x380u
104#define OMEGA_TERN_ARG_X 0x388u
105#define OMEGA_TERN_ARG_Y 0x390u
106#define OMEGA_TERN_ARG_WS 0x398u
107#define OMEGA_TERN_ARG_AS 0x3a0u
108#define OMEGA_TERN_ARG_U 0x3a8u
109#define OMEGA_TERN_ARG_STRIDE 0x3acu
110#define OMEGA_TERN_ARG_NEGN 0x3b0u
111#define OMEGA_TERN_ARG_NEGB 0x3b4u
112#define OMEGA_TERN_ARGS_WORDS 16u
113#define OMEGA_TERN_ROW_PAD 8u
114
115typedef struct {
116 uint32_t k; /* multiple of 256 (512 above 8192), <= OMEGA_TERN_MAX_K */
117 uint32_t layout; /* OMEGA_TERN_LAYOUT_* */
118 uint32_t rows; /* rows per half-warp R: row-major 1, 2, 4; block 2, 4 */
119 uint32_t warps; /* 1 .. 32 */
120 uint32_t depth; /* K steps per loop iteration: divides chunk / 256; rows * depth <= 16 */
121 uint32_t epi; /* OMEGA_TERN_EPI_* */
122 int mutant; /* 0 production; 1, 3, 4, 5 kernel mutants (2 is host-side, kernel identical to 0) */
123} OmegaTernGemvSpec;
124
125int omega_blackwell_codegen_tern_gemv_prog(const OmegaTernGemvSpec *spec, BlackwellIRProgram *prog);
126int omega_tern_gemv_spec_ok(const OmegaTernGemvSpec *spec);
127uint32_t omega_tern_gemv_steps(const OmegaTernGemvSpec *spec); /* k / 256 (0 when refused) */
128uint32_t omega_tern_gemv_shared_bytes(const OmegaTernGemvSpec *spec); /* 2 * chunk width, plus two output slot sets when coalesced */
129int omega_tern_gemv_coalesced_out(const OmegaTernGemvSpec *spec); /* 1 when the round's rows leave through shared memory in one store */
130uint32_t omega_tern_gemv_out_slot_bytes(const OmegaTernGemvSpec *spec); /* 2 * rows * warps * 4: one slot set */
131#define OMEGA_TERN_SHARED_MAX 16384u /* the measured KT1 budget, at most the launch descriptor cap */
132_Static_assert(OMEGA_TERN_SHARED_MAX <= OMEGA_BW_QMD_MAX_SHARED_BYTES, "the KT1 shared budget must fit the launch descriptor");
133uint32_t omega_tern_gemv_chunks(const OmegaTernGemvSpec *spec); /* x chunks: 1 for k <= 8192, else 2 */
134int omega_tern_gemv_prog_stats(const OmegaTernGemvSpec *spec, uint32_t *insns, uint32_t *gprs);
135
136/* Byte offset of word c of padded row r in a packed tensor (layout, R = rows for the block layout). */
137uint64_t omega_tern_word_offset(uint32_t layout, uint32_t R, uint32_t k, uint32_t r, uint32_t c);
138
139#ifdef __cplusplus
140}
141#endif
142#endif