interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / src / omega_blackwell_codegen_router.c

477 lines · 27534 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/*
3 * omega_blackwell_codegen_router.c: the one-launch MoE router kernel (see the header). Emitter rules follow
4 * omega_blackwell_codegen_attn.c / omega_blackwell_codegen_moe.c (control words, read barrier on every load,
5 * loop live ranges).
6 */
7#include "omega_blackwell_codegen_router.h"
8#include <string.h>
9
10#ifndef CUDAPP_MIGRATION_PENDING_C_CODEGEN_ROUTER
11#define CUDAPP_MIGRATION_PENDING_C_CODEGEN_ROUTER 0
12#endif
13#if CUDAPP_MIGRATION_PENDING_C_CODEGEN_ROUTER
14#define CW(stall, wbar, rbar, wait) \
15 (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20))
16#define CW_YIELD 0x2000u
17#define SB_NONE 7u
18#define SB_GPR 0u
19#define SB_RD 1u
20#define SB_UGPR 2u
21#define NOREG (-1)
22#define MAX_LOOPS 4
23
24enum { K_FIXED, K_PRED, K_VAR, K_VAR_MORE, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC };
25
26typedef struct {
27 BlackwellIRProgram *p;
28 uint32_t pending;
29 int err;
30 int open_start[MAX_LOOPS];
31 int depth;
32} Em;
33
34static void emit(Em *e, BlackwellIRInsn in, int kind) {
35 uint32_t wait = e->pending;
36 switch (kind) {
37 case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break;
38 case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break;
39 case K_VAR: in.control = CW(4, SB_GPR, SB_RD, wait) | CW_YIELD; e->pending = (1u << SB_GPR) | (1u << SB_RD); break;
40 case K_VAR_MORE: in.control = CW(4, SB_GPR, SB_RD, (e->pending & (1u << SB_RD))) | CW_YIELD; e->pending |= (1u << SB_GPR) | (1u << SB_RD); break;
41 case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break;
42 case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break;
43 case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break;
44 case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break;
45 case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break;
46 case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break;
47 case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break;
48 default: e->err = -1; return;
49 }
50 if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1;
51}
52
53static BlackwellIRInsn I0(BlackwellIROpcode op) {
54 BlackwellIRInsn i; memset(&i, 0, sizeof i);
55 i.op = op; i.dst_vreg = NOREG; i.src1_vreg = NOREG; i.src2_vreg = NOREG; i.src3_vreg = NOREG; i.ureg = NOREG;
56 return i;
57}
58static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; }
59static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; }
60static int V128(Em *e) { int v = omega_bw_ir_alloc_vreg128(e->p); if (v < 0) e->err = -1; return v; }
61static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; }
62static int here(const Em *e) { return (int)e->p->count; }
63
64/* operand with subregister */
65typedef struct { int v; unsigned s; } Op;
66static Op R(int v) { Op o = { v, 0 }; return o; }
67static Op S(int v, unsigned s) { Op o = { v, s }; return o; }
68
69static void ldcu64(Em *e, int ud, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDCU64); i.dst_vreg = ud; i.imm = off; i.is_uniform = true; emit(e, i, K_UVAR); }
70static void ldc64(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC64); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); }
71static void ldc32(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); }
72static void s2r(Em *e, int d, uint32_t sr) { BlackwellIRInsn i = I0(BW_IR_S2R); i.dst_vreg = d; i.imm = sr; emit(e, i, K_VAR); }
73static void movi(Em *e, int d, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_MOV_IMM); i.dst_vreg = d; i.imm = imm; emit(e, i, K_FIXED); }
74static void movrz(Em *e, Op d) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; emit(e, i, K_FIXED); }
75/* d = a * imm + c */
76static void imadi(Em *e, Op d, Op a, uint32_t imm, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; i.imm = imm; i.src3_vreg = c; emit(e, i, K_FIXED); }
77/* d = a * b + c (registers) */
78/* d64 = a * imm + c64 */
79static void imadw(Em *e, int d64, int a, uint32_t imm, int c64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = d64; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c64; emit(e, i, K_FIXED); }
80static void mov(Em *e, Op d, Op a) { imadi(e, d, a, 1u, NOREG); }
81static void shrs(Em *e, int d, Op a, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_SHF_R); i.dst_vreg = d; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; i.imm = imm; emit(e, i, K_FIXED); }
82static void shr(Em *e, int d, int a, uint32_t imm) { shrs(e, d, R(a), imm); }
83static void andi(Em *e, Op d, Op a, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_LOP3_AND); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; i.imm = imm; emit(e, i, K_FIXED); }
84static void xorr(Em *e, Op d, Op a, Op b) { BlackwellIRInsn i = I0(BW_IR_LOP3_XOR); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; i.src2_vreg = b.v; i.src2_subreg = (uint8_t)b.s; emit(e, i, K_FIXED); }
85static void ldg_wide(Em *e, BlackwellIROpcode op, int d, int addr64, uint32_t imm, int udesc, int first) {
86 BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = addr64; i.imm = imm; i.ureg = udesc; emit(e, i, first ? K_VAR : K_VAR_MORE);
87}
88static void stg(Em *e, BlackwellIROpcode op, int addr64, Op val, int udesc) { BlackwellIRInsn i = I0(op); i.src1_vreg = addr64; i.src2_vreg = val.v; i.src2_subreg = (uint8_t)val.s; i.ureg = udesc; emit(e, i, K_STORE); }
89static void lds128(Em *e, int d128, int addr, uint32_t imm, int first) { BlackwellIRInsn i = I0(BW_IR_LDS128); i.dst_vreg = d128; i.src1_vreg = addr; i.imm = imm; emit(e, i, first ? K_VAR : K_VAR_MORE); }
90static void lds32(Em *e, Op d, int addr, int first) { BlackwellIRInsn i = I0(BW_IR_LDS32); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = addr; emit(e, i, first ? K_VAR : K_VAR_MORE); }
91static void sts32(Em *e, int addr, Op val) { BlackwellIRInsn i = I0(BW_IR_STS32); i.src1_vreg = addr; i.src2_vreg = val.v; i.src2_subreg = (uint8_t)val.s; emit(e, i, K_STORE); }
92static void bar_sync(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); }
93static void fop(Em *e, BlackwellIROpcode op, Op d, Op a, Op b, Op c) {
94 BlackwellIRInsn i = I0(op); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s;
95 i.src2_vreg = b.v; i.src2_subreg = (uint8_t)b.s; i.src3_vreg = c.v; i.src3_subreg = (uint8_t)c.s; emit(e, i, K_FIXED);
96}
97static void fadd(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FADD, d, a, b, R(NOREG)); }
98static void fsub(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FSUB, d, a, b, R(NOREG)); }
99static void fmul(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMUL, d, a, b, R(NOREG)); }
100static void ffma(Em *e, Op d, Op a, Op b, Op c) { fop(e, BW_IR_FFMA, d, a, b, c); }
101static void fmaxx(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMNMX_MAX, d, a, b, R(NOREG)); }
102static void mufu(Em *e, BlackwellIROpcode op, Op d, Op a) { BlackwellIRInsn i = I0(op); i.dst_vreg = d.v; i.dst_subreg = (uint8_t)d.s; i.src1_vreg = a.v; i.src1_subreg = (uint8_t)a.s; emit(e, i, K_VAR); }
103static void shfl_down(Em *e, int d, int a, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_SHFL_DOWN); i.dst_vreg = d; i.src1_vreg = a; i.imm = off; emit(e, i, K_VAR); }
104static void isetp_ge_u32(Em *e, int a, int b) { BlackwellIRInsn i = I0(BW_IR_ISETP_GE_U32); i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_PRED); }
105static void bra(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); }
106static int bra_if_p0_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = 1; emit(e, i, K_BRA); return at; }
107static void patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); }
108static int bssy_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = 0; i.imm = 2; emit(e, i, K_BSSY); return at; }
109static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); }
110static void exit_k(Em *e) { emit(e, I0(BW_IR_EXIT), K_EXIT); }
111static void tail(Em *e) {
112 exit_k(e);
113 BlackwellIRInsn self = I0(BW_IR_BRA);
114 self.control = 0x000fc000u;
115 if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1;
116}
117static void loop_begin(Em *e) { if (e->depth >= MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); }
118static void loop_end(Em *e) {
119 if (e->depth <= 0) { e->err = -1; return; }
120 int start = e->open_start[--e->depth], end = here(e) - 1;
121 OmegaRegAlloc *ra = &e->p->regalloc;
122 for (int v = 0; v < ra->num_vregs; v++) {
123 OmegaLiveInterval *iv = &ra->intervals[v];
124 if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end;
125 }
126}
127
128/* A region only some lanes of a warp execute: BSSY ... @P0 BRA join ... BSYNC. P0 true = skip. */
129typedef struct { int bssy, bra; } Region;
130static Region region_skip_if_p0(Em *e) { Region r; r.bssy = bssy_fwd(e); r.bra = bra_if_p0_fwd(e); return r; }
131static void region_end(Em *e, Region r) { int at = here(e); bsync(e); patch_bra(e, r.bra, at); patch_bra(e, r.bssy, at + 1); }
132
133static void fminn(Em *e, Op d, Op a, Op b) { fop(e, BW_IR_FMNMX_MIN, d, a, b, R(NOREG)); }
134/* d = LUT(a, b, c) with the LOP3 truth table (a = 0xf0, b = 0xcc, c = 0xaa); vreg -1 encodes RZ */
135static void lop3(Em *e, int d, int a, int b, int c, uint32_t lut) { BlackwellIRInsn i = I0(BW_IR_LOP3_LUT); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; i.imm = lut; emit(e, i, K_FIXED); }
136/* ne = (a != b) as 0/1: t = a ^ b; ne = (t | -t) >> 31 */
137static void ne_of(Em *e, int ne, Op a, Op b, int t, int t2) {
138 xorr(e, R(t), a, b);
139 imadi(e, R(t2), R(t), 0xffffffffu, NOREG);
140 lop3(e, t, t, t2, NOREG, 0xfcu);
141 shr(e, ne, t, 31);
142}
143/* warp reductions: the total lands in lane 0 */
144static void warp_sum(Em *e, int v, int t) { for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, t, v, o); fadd(e, R(v), R(v), R(t)); } }
145static void warp_max(Em *e, int v, int t) { for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, t, v, o); fmaxx(e, R(v), R(v), R(t)); } }
146static void warp_min(Em *e, int v, int t) { for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, t, v, o); fminn(e, R(v), R(v), R(t)); } }
147/* lane 0 of every warp stores v to shared [addr] (the warps write identical words when the value is uniform) */
148static void lane0_sts(Em *e, int lane, int one, int addr, Op v) {
149 isetp_ge_u32(e, lane, one);
150 Region r = region_skip_if_p0(e); sts32(e, addr, v); region_end(e, r);
151}
152
153static const uint32_t NEG_BIG = 0xF149F2CAu; /* -1e30f */
154static const uint32_t C_2P23 = 0x4B000000u; /* 8388608.0f; also the OR mask that turns a small integer n into the float 2^23 + n */
155static const uint32_t C_LOG2E = 0x3FB8AA3Bu; /* 1.4426950f */
156static const uint32_t C_INV2048 = 0x3A000000u; /* 1/2048 */
157
158int omega_router_smem(const OmegaRouterSpec *spec, OmegaRouterSmem *m) {
159 const uint32_t ex = spec->experts ? spec->experts : OMEGA_ROUTER_EXPERTS;
160 if ((ex != 128u && ex != 256u) || spec->shared > 1u) return -1;
161 if (ex == OMEGA_ROUTER_EXPERTS && !spec->shared) {
162 m->logit = OMEGA_ROUTER_SM_LOGIT; m->red = OMEGA_ROUTER_SM_RED; m->bc = OMEGA_ROUTER_SM_BC;
163 m->sel = OMEGA_ROUTER_SM_SEL; m->selv = OMEGA_ROUTER_SM_SELV; m->total = OMEGA_ROUTER_SHARED_BYTES;
164 return 0;
165 }
166 m->logit = OMEGA_ROUTER_SM_XN + OMEGA_ROUTER_HIDDEN * 4u;
167 m->red = m->logit + (((ex + spec->shared) * 4u + 15u) & 0xfffffff0u);
168 m->bc = m->red + 128u; m->sel = m->bc + 16u; m->selv = m->sel + 48u; m->total = m->selv + 32u;
169 return 0;
170}
171
172int omega_blackwell_codegen_router_prog(const OmegaRouterSpec *spec, BlackwellIRProgram *prog) {
173 if (!spec || !prog) return -1;
174 const uint32_t W = spec->warps, D = spec->depth;
175 const int MUT = spec->mutant;
176 OmegaRouterSmem L;
177 const uint32_t XST = spec->xstride, LBST = spec->lbstride; /* ROWS form when set */
178 if ((W != 8 && W != 16 && W != 32) || (D != 1 && D != 2 && D != 4 && D != 8) || MUT < 0 || MUT > 6 || omega_router_smem(spec, &L)) return -1;
179 if ((XST != 0) != (LBST != 0) || (XST && (XST < OMEGA_ROUTER_HIDDEN * 4u || (XST & 15u) || (LBST & 15u))) || (MUT == 6 && !XST)) return -1;
180 const uint32_t EX = spec->experts ? spec->experts : OMEGA_ROUTER_EXPERTS, SHX = spec->shared, EPL = EX / 32u; /* logits per lane */
181 if (MUT == 5 && !SHX) return -1;
182 const uint32_t E = 64u / W; /* x values per thread: 8, 4 or 2 */
183 omega_bw_ir_init(prog);
184 Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em;
185
186 /* ---- arguments and identities ---- */
187 int udesc = UV64(e);
188 int px = V64(e), pgate = V64(e), pnorm = V64(e), pxout = V64(e);
189 int eps = V(e), oscale = V(e);
190 int tid = V(e), warp = V(e), lane = V(e), one = V(e), zero = V(e), c2p23 = V(e), cneg = V(e), ts = V(e), t1 = V(e), t2 = V(e);
191 if (e->err) return -1;
192 ldcu64(e, udesc, 0x358);
193 ldc64(e, px, OMEGA_ROUTER_ARG_X); ldc64(e, pgate, OMEGA_ROUTER_ARG_GATE); ldc64(e, pnorm, OMEGA_ROUTER_ARG_NORM); ldc64(e, pxout, OMEGA_ROUTER_ARG_XOUT);
194 if (XST) { /* ROWS form: this CTA's x row and output buffer */
195 int cy = V(e);
196 if (e->err) return -1;
197 s2r(e, cy, BW_SR_CTAID_Y);
198 if (MUT != 6) imadw(e, px, cy, XST, px);
199 imadw(e, pxout, cy, LBST, pxout);
200 }
201 ldc32(e, eps, OMEGA_ROUTER_ARG_EPS); ldc32(e, oscale, OMEGA_ROUTER_ARG_OUT_SCALE);
202 s2r(e, tid, BW_SR_TID_X);
203 movi(e, one, 1); movrz(e, R(zero)); movi(e, c2p23, C_2P23); movi(e, cneg, NEG_BIG);
204 shr(e, warp, tid, 5);
205 andi(e, R(lane), R(tid), 31u);
206
207 /* ---- phase 1: RMSNorm ---- */
208 int toff = V(e), xa = V64(e), wa = V64(e), sq = V(e), rs = V(e), a0 = V(e);
209 int xq[2], wq[2];
210 Op xe[8], we[8];
211 imadi(e, R(toff), R(tid), E * 4u, NOREG);
212 imadw(e, xa, toff, 1u, px); imadw(e, wa, toff, 1u, pnorm);
213 if (E == 2) {
214 xq[0] = V64(e); wq[0] = V64(e);
215 ldg_wide(e, BW_IR_LDG_E_64, xq[0], xa, 0, udesc, 1); ldg_wide(e, BW_IR_LDG_E_64, wq[0], wa, 0, udesc, 0);
216 for (unsigned i = 0; i < 2; i++) { xe[i] = S(xq[0], i); we[i] = S(wq[0], i); }
217 } else {
218 for (unsigned q = 0; q < E / 4; q++) { xq[q] = V128(e); wq[q] = V128(e); }
219 for (unsigned q = 0; q < E / 4; q++) ldg_wide(e, BW_IR_LDG_E_128, xq[q], xa, q * 16u, udesc, q == 0);
220 for (unsigned q = 0; q < E / 4; q++) ldg_wide(e, BW_IR_LDG_E_128, wq[q], wa, q * 16u, udesc, 0);
221 for (unsigned i = 0; i < E; i++) { xe[i] = S(xq[i / 4], i % 4); we[i] = S(wq[i / 4], i % 4); }
222 }
223 if (e->err) return -1;
224 fmul(e, R(sq), xe[0], xe[0]);
225 for (unsigned i = 1; i < E; i++) ffma(e, R(sq), xe[i], xe[i], R(sq));
226 warp_sum(e, sq, ts);
227 imadi(e, R(a0), R(warp), 4u, NOREG); imadi(e, R(a0), R(one), L.red, a0);
228 lane0_sts(e, lane, one, a0, R(sq));
229 bar_sync(e);
230 /* every thread sums the W partials (quads of 4) */
231 {
232 int rq0 = V128(e), rq1 = V128(e);
233 movi(e, a0, L.red);
234 for (uint32_t q = 0; q < W / 4; q += 2) {
235 lds128(e, rq0, a0, q * 16u, 1);
236 if (q + 1 < W / 4) lds128(e, rq1, a0, (q + 1) * 16u, 0);
237 for (unsigned i = 0; i < 4; i++) { if (q == 0 && i == 0) mov(e, R(sq), S(rq0, 0)); else fadd(e, R(sq), R(sq), S(rq0, i)); }
238 if (q + 1 < W / 4) for (unsigned i = 0; i < 4; i++) fadd(e, R(sq), R(sq), S(rq1, i));
239 }
240 }
241 movi(e, ts, C_INV2048);
242 ffma(e, R(rs), R(sq), R(ts), R(eps));
243 mufu(e, BW_IR_MUFU_RSQ, R(rs), R(rs));
244 /* xn = x * r * w -> shared [toff + 4i]; x' = xn * out_scale -> xout */
245 {
246 int xo = V64(e);
247 imadw(e, xo, toff, 1u, pxout);
248 for (unsigned i = 0; i < E; i++) {
249 fmul(e, xe[i], xe[i], R(rs));
250 if (MUT != 1) fmul(e, xe[i], xe[i], we[i]);
251 imadi(e, R(a0), R(one), OMEGA_ROUTER_SM_XN + i * 4u, toff);
252 sts32(e, a0, xe[i]);
253 fmul(e, R(ts), xe[i], R(oscale));
254 if (i) imadw(e, xo, one, 4u, xo);
255 stg(e, BW_IR_STG_E, xo, R(ts), udesc);
256 }
257 }
258 bar_sync(e);
259
260 /* ---- phase 2: logits, warp w -> experts w, w + W, ... ---- */
261 int c128 = V(e), ex = V(e), lane16 = V(e), lane32 = V(e), gl = V64(e), rowa = V64(e), la = V(e);
262 int acc[4], lo = V(e), hi = V(e), xs0 = V128(e), xs1 = V128(e), ld[8];
263 for (unsigned i = 0; i < 4; i++) acc[i] = V(e);
264 for (uint32_t d = 0; d < D; d++) ld[d] = V128(e);
265 if (e->err) return -1;
266 movi(e, c128, EX + SHX); /* rows: the experts, then the shared gate */
267 mov(e, R(ex), R(warp));
268 imadi(e, R(lane16), R(lane), 16u, NOREG);
269 imadi(e, R(lane32), R(lane), 32u, NOREG);
270 imadw(e, gl, lane16, 1u, pgate);
271 const int L_e = here(e);
272 loop_begin(e);
273 isetp_ge_u32(e, ex, c128);
274 const int bra_edone = bra_if_p0_fwd(e);
275 imadw(e, rowa, ex, OMEGA_ROUTER_ROW_BYTES, gl);
276 for (unsigned i = 0; i < 4; i++) movrz(e, R(acc[i]));
277 for (uint32_t b = 0; b < OMEGA_ROUTER_STEPS / D; b++) {
278 for (uint32_t d = 0; d < D; d++) ldg_wide(e, BW_IR_LDG_E_128, ld[d], rowa, (b * D + d) * 512u, udesc, d == 0);
279 for (uint32_t d = 0; d < D; d++) {
280 const uint32_t s = b * D + d;
281 lds128(e, xs0, lane32, OMEGA_ROUTER_SM_XN + s * 1024u, 1);
282 lds128(e, xs1, lane32, OMEGA_ROUTER_SM_XN + s * 1024u + 16u, 0);
283 for (unsigned w = 0; w < 4; w++) {
284 imadi(e, R(lo), S(ld[d], w), 65536u, NOREG);
285 andi(e, R(hi), S(ld[d], w), 0xffff0000u);
286 Op x0 = w < 2 ? S(xs0, 2 * w) : S(xs1, 2 * (w - 2)), x1 = w < 2 ? S(xs0, 2 * w + 1) : S(xs1, 2 * (w - 2) + 1);
287 ffma(e, R(acc[w]), R(lo), x0, R(acc[w]));
288 ffma(e, R(acc[w]), R(hi), x1, R(acc[w]));
289 }
290 }
291 }
292 fadd(e, R(acc[0]), R(acc[0]), R(acc[1])); fadd(e, R(acc[2]), R(acc[2]), R(acc[3])); fadd(e, R(acc[0]), R(acc[0]), R(acc[2]));
293 warp_sum(e, acc[0], ts);
294 imadi(e, R(la), R(ex), 4u, NOREG); imadi(e, R(la), R(one), L.logit, la);
295 lane0_sts(e, lane, one, la, R(acc[0]));
296 imadi(e, R(ex), R(one), W, ex);
297 bra(e, L_e);
298 loop_end(e);
299 patch_bra(e, bra_edone, here(e));
300 bar_sync(e);
301
302 /* ---- phase 3: top-8 (every warp, uniform barriers) ---- */
303 int vq = V128(e), vq2 = EPL > 4u ? V128(e) : -1, lane4 = V(e), ej[8], fj[8], lm = V(e), mr = V(e), lc = V(e), cj = V(e), ne = V(e), ci = V(e), cm = V(e);
304 int bc = V(e), bc4 = V(e), r = V(e), c8 = V(e), sa = V(e);
305 for (unsigned j = 0; j < EPL; j++) { ej[j] = V(e); fj[j] = V(e); }
306 if (e->err) return -1;
307#define VQ(j) ((j) < 4u ? S(vq, (j)) : S(vq2, (j) - 4u))
308 /* lane L holds logits EPL*L .. EPL*L + EPL-1 (the shared gate row, index EX, is never loaded here) */
309 if (EPL == 4u) lds128(e, vq, lane16, L.logit, 1);
310 else { lds128(e, vq, lane32, L.logit, 1); lds128(e, vq2, lane32, L.logit + 16u, 0); }
311 imadi(e, R(lane4), R(lane), EPL, NOREG);
312 for (unsigned j = 0; j < EPL; j++) {
313 imadi(e, R(ej[j]), R(one), j, lane4);
314 lop3(e, fj[j], ej[j], c2p23, NOREG, 0xfcu); /* bits 0x4B000000 | ej = the float 2^23 + ej: monotonic in ej, never zero */
315 }
316 movi(e, bc, L.bc); movi(e, bc4, L.bc + 4u);
317 movrz(e, R(r)); movi(e, c8, OMEGA_ROUTER_TOPK);
318 const int L_r = here(e);
319 loop_begin(e);
320 isetp_ge_u32(e, r, c8);
321 const int bra_rdone = bra_if_p0_fwd(e);
322 /* the round's max */
323 fmaxx(e, R(lm), VQ(0u), VQ(1u));
324 for (unsigned j = 2; j < EPL; j++) fmaxx(e, R(lm), R(lm), VQ(j));
325 warp_max(e, lm, ts);
326 lane0_sts(e, lane, one, bc, R(lm));
327 bar_sync(e);
328 lds32(e, R(mr), bc, 1);
329 /* lowest index holding it: candidate = 2^23 + ej when equal, else lifted by 0x30000000 (about 6e35, still positive) */
330 for (unsigned j = 0; j < EPL; j++) {
331 ne_of(e, ne, VQ(j), R(mr), t1, t2);
332 imadi(e, R(cj), R(ne), 0x30000000u, fj[j]);
333 if (j == 0) mov(e, R(lc), R(cj)); else fminn(e, R(lc), R(lc), R(cj));
334 }
335 warp_min(e, lc, ts);
336 imadi(e, R(sa), R(r), 4u, NOREG); imadi(e, R(sa), R(one), L.selv, sa);
337 isetp_ge_u32(e, lane, one);
338 { Region rg = region_skip_if_p0(e); sts32(e, bc4, R(lc)); sts32(e, sa, R(mr)); region_end(e, rg); }
339 bar_sync(e);
340 lds32(e, R(ci), bc4, 1);
341 andi(e, R(ci), R(ci), EX - 1u); /* the chosen expert id: the low bits of 2^23 + ej */
342 imadi(e, R(sa), R(r), 4u, NOREG); imadi(e, R(sa), R(one), L.sel, sa);
343 lane0_sts(e, lane, one, sa, R(ci));
344 if (MUT == 2) xorr(e, R(cm), R(ci), R(one)); else mov(e, R(cm), R(ci));
345 /* mask the winner: v = min(v, ne ? +1e30 : -1e30) */
346 for (unsigned j = 0; j < EPL; j++) {
347 ne_of(e, ne, R(ej[j]), R(cm), t1, t2);
348 imadi(e, R(cj), R(ne), 0x80000000u, cneg);
349 fminn(e, VQ(j), VQ(j), R(cj));
350 }
351 imadi(e, R(r), R(one), 1u, r);
352 bra(e, L_r);
353 loop_end(e);
354 patch_bra(e, bra_rdone, here(e));
355 if (SHX) { movi(e, t1, EX); movi(e, sa, L.sel + 32u); lane0_sts(e, lane, one, sa, R(t1)); } /* slot 8: the shared expert */
356 bar_sync(e);
357 /* full 128-way softmax sum (den when norm_topk_prob is off): original logits from shared, l_0 = selv[0] */
358 int full = V(e), m0 = V(e), cl2e = V(e);
359 if (e->err) return -1;
360 movi(e, cl2e, C_LOG2E);
361 if (EPL == 4u) lds128(e, vq, lane16, L.logit, 1);
362 else { lds128(e, vq, lane32, L.logit, 1); lds128(e, vq2, lane32, L.logit + 16u, 0); }
363 movi(e, sa, L.selv);
364 lds32(e, R(m0), sa, 0);
365 for (unsigned j = 0; j < EPL; j++) {
366 fsub(e, R(ts), VQ(j), R(m0)); fmul(e, R(ts), R(ts), R(cl2e)); mufu(e, BW_IR_MUFU_EX2, R(ts), R(ts));
367 if (j == 0) mov(e, R(full), R(ts)); else fadd(e, R(full), R(full), R(ts));
368 }
369 warp_sum(e, full, ts);
370 lane0_sts(e, lane, one, bc, R(full));
371 bar_sync(e);
372 lds32(e, R(full), bc, 1);
373
374 /* ---- epilogue: warp 0 only ---- */
375 isetp_ge_u32(e, warp, one);
376 const int bra_tail = bra_if_p0_fwd(e);
377 int pids = V64(e), pwts = V64(e), pdgu = V64(e), pddn = V64(e), ptgu = V64(e), ptdn = V64(e), ntk = V(e);
378 int sv0 = V128(e), sv1 = V128(e), er[8], s8 = V(e), den = V(e), wa64 = V64(e);
379 for (unsigned i = 0; i < 8; i++) er[i] = V(e);
380 if (e->err) return -1;
381 ldc64(e, pids, OMEGA_ROUTER_ARG_IDS); ldc64(e, pwts, OMEGA_ROUTER_ARG_WTS);
382 ldc64(e, pdgu, OMEGA_ROUTER_ARG_DIR_GU); ldc64(e, pddn, OMEGA_ROUTER_ARG_DIR_DN);
383 ldc64(e, ptgu, OMEGA_ROUTER_ARG_TAB_GU); ldc64(e, ptdn, OMEGA_ROUTER_ARG_TAB_DN);
384 if (XST) { /* ROWS form: the outputs of this CTA's row (the directories are shared) */
385 int cy = V(e);
386 if (e->err) return -1;
387 s2r(e, cy, BW_SR_CTAID_Y);
388 imadw(e, pids, cy, LBST, pids); imadw(e, pwts, cy, LBST, pwts);
389 imadw(e, ptgu, cy, LBST, ptgu); imadw(e, ptdn, cy, LBST, ptdn);
390 }
391 ldc32(e, ntk, OMEGA_ROUTER_ARG_NORM_TOPK);
392 movi(e, sa, L.selv);
393 lds128(e, sv0, sa, 0, 1); lds128(e, sv1, sa, 16, 0);
394 for (unsigned i = 0; i < 8; i++) {
395 Op v = i < 4 ? S(sv0, i) : S(sv1, i - 4);
396 fsub(e, R(er[i]), v, S(sv0, 0)); fmul(e, R(er[i]), R(er[i]), R(cl2e)); mufu(e, BW_IR_MUFU_EX2, R(er[i]), R(er[i]));
397 }
398 fadd(e, R(s8), R(er[0]), R(er[1]));
399 for (unsigned i = 2; i < 8; i++) fadd(e, R(s8), R(s8), R(er[i]));
400 /* den = (s8 - full) * float(ntk) + full */
401 lop3(e, t1, ntk, c2p23, NOREG, 0xfcu); fsub(e, R(t1), R(t1), R(c2p23));
402 fsub(e, R(t2), R(s8), R(full));
403 ffma(e, R(den), R(t2), R(t1), R(full));
404 if (MUT == 4) movi(e, den, 0x3f800000u); else mufu(e, BW_IR_MUFU_RCP, R(den), R(den));
405 for (unsigned i = 0; i < 8; i++) fmul(e, R(er[i]), R(er[i]), R(den));
406 /* lane 0: the 8 weights */
407 isetp_ge_u32(e, lane, one);
408 {
409 Region rg = region_skip_if_p0(e);
410 for (unsigned i = 0; i < 8; i++) { imadw(e, wa64, one, i * 4u, pwts); stg(e, BW_IR_STG_E, wa64, R(er[i]), udesc); }
411 if (SHX) { /* slot 8: sigmoid(shared gate) = RCP(1 + EX2(-l * log2 e)) */
412 movi(e, sa, L.logit + EX * 4u); lds32(e, R(ts), sa, 1);
413 movi(e, t1, 0xBFB8AA3Bu); fmul(e, R(ts), R(ts), R(t1)); mufu(e, BW_IR_MUFU_EX2, R(ts), R(ts));
414 movi(e, t1, 0x3f800000u); fadd(e, R(ts), R(ts), R(t1)); mufu(e, BW_IR_MUFU_RCP, R(ts), R(ts));
415 if (MUT == 5) movi(e, ts, 0x3f800000u);
416 imadw(e, wa64, one, 32u, pwts); stg(e, BW_IR_STG_E, wa64, R(ts), udesc);
417 }
418 region_end(e, rg);
419 }
420 /* lanes 0..7 (0..8 with the shared slot): id_g and the two directory entries of id_g into the two group tables */
421 if (SHX) movi(e, c8, OMEGA_ROUTER_TOPK + 1u);
422 isetp_ge_u32(e, lane, c8);
423 {
424 Region rg = region_skip_if_p0(e);
425 int si = V(e), da = V64(e), q0 = V64(e), q1 = V64(e), ta = V64(e), tb = V64(e);
426 if (e->err) return -1;
427 imadi(e, R(sa), R(lane), 4u, NOREG); imadi(e, R(sa), R(one), L.sel, sa);
428 lds32(e, R(si), sa, 1);
429 imadw(e, da, lane, 4u, pids);
430 stg(e, BW_IR_STG_E, da, R(si), udesc);
431 if (MUT == 3) xorr(e, R(si), R(si), R(one));
432 for (unsigned which = 0; which < 2; which++) {
433 imadw(e, da, si, 16u, which == 0 ? pdgu : pddn);
434 ldg_wide(e, BW_IR_LDG_E_64, q0, da, 0, udesc, 1);
435 ldg_wide(e, BW_IR_LDG_E_64, q1, da, 8, udesc, 0);
436 imadw(e, ta, lane, 16u, which == 0 ? ptgu : ptdn);
437 stg(e, BW_IR_STG_E, ta, S(q0, 0), udesc);
438 imadw(e, tb, one, 4u, ta); stg(e, BW_IR_STG_E, tb, S(q0, 1), udesc);
439 imadw(e, tb, one, 8u, ta); stg(e, BW_IR_STG_E, tb, S(q1, 0), udesc);
440 imadw(e, tb, one, 12u, ta); stg(e, BW_IR_STG_E, tb, S(q1, 1), udesc);
441 }
442 region_end(e, rg);
443 }
444 patch_bra(e, bra_tail, here(e));
445 tail(e);
446 (void)zero;
447 if (e->err) return -1;
448 return omega_bw_regalloc_solve(prog);
449}
450
451#else /* default: first-party seeded Omega; C above is differential-only */
452#include "omega_codegen_rest_wire.h"
453int omega_router_smem(const OmegaRouterSpec *spec, OmegaRouterSmem *m) {
454 uint64_t meta[OMEGA_OM_IR_META] = {0};
455 if (omega_rest_query(omega_rest_router_wire, 0, spec, m, meta) != 0) return -1;
456 if (meta[48] == 0 && m) {
457 m->logit = (uint32_t)meta[32];
458 m->red = (uint32_t)meta[33];
459 m->bc = (uint32_t)meta[34];
460 m->sel = (uint32_t)meta[35];
461 m->selv = (uint32_t)meta[36];
462 m->total = (uint32_t)meta[37];
463 }
464 return (int)(int64_t)meta[48];
465}
466int omega_blackwell_codegen_router_prog(const OmegaRouterSpec *spec, BlackwellIRProgram *prog) {
467 return omega_gpu_om_ir_replay(omega_rest_router_wire, 1, (uintptr_t)spec, 0, 0, prog, NULL) == 0 ? 0 : -1;
468}
469#endif /* CUDAPP_MIGRATION_PENDING_C_CODEGEN_ROUTER */
470
471int omega_router_prog_stats(const OmegaRouterSpec *spec, uint32_t *insns, uint32_t *gprs) {
472 static BlackwellIRProgram prog;
473 if (omega_blackwell_codegen_router_prog(spec, &prog) != 0) return -1;
474 if (insns) *insns = (uint32_t)prog.count;
475 if (gprs) *gprs = prog.regalloc.peak_gpr_usage;
476 return 0;
477}