interplane.Get Omega
Read in your language

aien-architecture-mv

Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.

Brought by drakestapleton

branch main Branches Tags History

aien-architecture-mv / evidence / QUANTUM_EFFICIENCY / 2026-10-22 / pinned-source-excerpts.json

1 lines · 17012 bytes Raw Blame History
1{"sc-q36-batch@f0230a59:crates/aien-inference-abi/src/qwen38_omega/moe.rs": "1://! Qwen3.6-35B-A3B (`qwen3_5_moe`) onto the Omega decode graph `aien_omega_q38::q36::Q36Graph`\n2://! (sc L306). The mixers upload as for the 27B (see the parent module) at the 35B's shapes; the\n3://! MLP of every layer is the sparse block:\n4://!\n5://! - `mlp.experts.{e}.gate_proj | up_proj` for e in 0..256, then `mlp.shared_expert.gate_proj |\n6://! up_proj` -> one FP8 tensor of 257 experts `[1024][2048]` (each expert's gate rows, then its up\n7://! rows; the 128-blocks concatenate with them). `down_proj` likewise -> 257 x `[2048][512]`.\n8://! - `mlp.gate.weight` (bf16 `[256][2048]`) with `mlp.shared_expert_gate.weight` (`[1][2048]`) as\n9://! row 256 -> the router (`Router::alloc_shape(layers, 256, 1)`), with the post-attention norm\n10://! folded to `1 + w`; its directories point at the two expert tensors (entry 256 = the shared\n11://! expert, the graph's ninth group, weight sigmoid(shared gate . xn)).\n12://!\n13://! The host reference this must match is [`crate::qwen38`] (the `moe` branch).\n14:use std::path::Path;\n15:\n16:use aien_omega_bf16::Bf16Tensor;\n69:/// Appends one FP8 matrix (whole 128-blocks, `[n][k]`) to an expert tensor's bytes and scales.\n70:fn push_fp8(\n71: name: &str,\n72: m: Matrix,\n73: n: usize,\n74: k: usize,\n75: w: &mut Vec<u8>,\n76: s: &mut Vec<u16>,\n77:) -> Result<(), String> {\n78: match m {\n79: Matrix::Fp8 {\n80: n: mn,\n81: k: mk,\n82: w: mw,\n83: scales,\n84: } if (mn, mk) == (n, k) => {\n85: w.extend_from_slice(&mw);\n86: s.extend_from_slice(&scales);\n87: Ok(())\n88: }\n89: Matrix::Fp8 { n: mn, k: mk, .. } => {\n90: Err(format!(\"{name}: [{mn}][{mk}], expected [{n}][{k}]\"))\n91: }\n92: Matrix::Bf16 { .. } => Err(format!(\"{name}: bf16 (the expert tensors take FP8)\")),\n93: }\n94:}\n95:\n96:/// The layer's 257-expert gate|up and down tensors (routed 0..256, then the shared expert).\n385: )\n386: }\n387: .map_err(gpu(\"q36 graph set_layer\"))?;\n388: }\n389: graph\n390: .set_final_norm(&final_norm)\n391: .map_err(gpu(\"q36 graph set_final_norm\"))?;\n392: // HUM L314 exact two-pass head, on by default (AIEN_Q36_EXACT_HEAD=0 keeps the plain head):\n393: // greedy tokens identical to the full bf16 head by construction (exact_head.rs), half the\n394: // head's bytes. The engine turns it off for sampled requests.\n395: let (head_screen, head_coef) = if std::env::var(\"AIEN_Q36_EXACT_HEAD\").as_deref() == Ok(\"0\") {\n396: eprintln!(\"qwen36 omega: exact two-pass head OFF (AIEN_Q36_EXACT_HEAD=0)\");\n397: (None, None)\n398: } else {\n399: let t = std::time::Instant::now();\n400: let (codes, scales, coef) = crate::exact_head::chip_screen(&head)?;\n401: let screen = Fp8Tensor::upload(1, v, h, &codes, &scales).map_err(gpu(\"exact head screen\"))?;\n402: // SAFETY: the screen is owned by the returned value and dropped after the graph (field order).\n403: unsafe { graph.set_exact_head(screen.as_raw(), &coef) }.map_err(gpu(\"q36 graph set_exact_head\"))?;\n404: eprintln!(\"qwen36 omega: exact two-pass head ON (fp8b screen + Row bound, built in {:.1} s)\", t.elapsed().as_secs_f64());\n405: (Some(screen), Some(coef))\n406: };\n407: drop(head);\n408: // Negative controls for the gates (TEST ONLY): AIEN_Q36_TEST_MUTANT=4 drops the shared expert\n409: // from COMBINE, 1 shifts the SWIGLU up half, 2 the attention GATHER, 3 drops the attention gate,\n410: // 5 inverts the exact head's MASK, 6 drops its bound.\n411: if let Some(m) = graph_mutant() {\n412: eprintln!(\"qwen36 omega: TEST MUTANT {m} set (negative control; never serve this)\");\n413: graph.test_set_mutant(m);\n414: }\n415: let row_src = RowSource {\n416: layers: c.layers,", "omega@6de999da:src/omega_blackwell_codegen_gdn.c": "340: fmul(e, R(mn), R(mn), R(cl2e)); mufu(e, BW_IR_MUFU_EX2, R(mn), R(mn)); fadd(e, R(mn), R(mn), R(fone));\n341: mufu(e, BW_IR_MUFU_LG2, R(mn), R(mn));\n342: movrz(e, R(sp)); fmaxx(e, R(sp), R(xs), R(sp));\n343: movi(e, t1, C_LN2); ffma(e, R(sp), R(mn), R(t1), R(sp));\n344: /* decay = EX2(negA * softplus * log2 e) */\n345: if (MUT == 1) movi(e, dec, C_ONE);\n346: else { fmul(e, R(dec), S(scq, 0), R(sp)); fmul(e, R(dec), R(dec), R(cl2e)); mufu(e, BW_IR_MUFU_EX2, R(dec), R(dec)); }\n347: /* beta = sigmoid(b) */\n348: if (MUT == 2) movi(e, beta, C_ONE);\n349: else {\n350: fmul(e, R(beta), S(abq, 1), R(cnl2e)); mufu(e, BW_IR_MUFU_EX2, R(beta), R(beta));\n351: fadd(e, R(beta), R(beta), R(fone)); mufu(e, BW_IR_MUFU_RCP, R(beta), R(beta));\n352: }\n353:\n354: /* ---- phase D: the state update (fresh registers per step: a reused name stays live across all its uses) ---- */\n355: int aw = V(e), pa = V(e), la = V(e), kq[4], kv[4], dl[4], vq = V128(e);\n356: for (unsigned i = 0; i < 4; i++) { kq[i] = V128(e); kv[i] = V(e); dl[i] = V(e); }\n357: if (e->err) return -1;\n358: imadi(e, R(aw), R(warp), 16u * 4u, NOREG);\n359: imadi(e, R(pa), R(warp), OMEGA_GDN_HEAD_DIM * 4u, NOREG); imadi(e, R(pa), R(lane), 16u, pa); imadi(e, R(pa), R(one), OMEGA_GDN_SM_P, pa);\n360: imadi(e, R(la), R(lane), 16u, NOREG);\n361: consume(e, SB_STATE);\n362: if (spec->state_bf16) for (unsigned r = 0; r < 16; r++) bf16x4_widen(e, sq[r], sw[r]);\n363: for (unsigned r = 0; r < 16; r++) for (unsigned j = 0; j < 4; j++) fmul(e, S(sq[r], j), S(sq[r], j), R(dec));\n364: /* kv partial over this warp's 16 rows: kv_j = sum_r S[r][j] k[r], then the 8-way cross-warp sum */\n365: for (unsigned i = 0; i < 4; i++) lds128(e, kq[i], aw, OMEGA_GDN_SM_K + i * 16u, i == 0);\n366: for (unsigned j = 0; j < 4; j++) fmul(e, R(kv[j]), S(sq[0], j), S(kq[0], 0));\n367: for (unsigned r = 1; r < 16; r++) for (unsigned j = 0; j < 4; j++) ffma(e, R(kv[j]), S(sq[r], j), S(kq[r / 4], r % 4), R(kv[j]));\n368: cross_warp_sum(e, kv, pa, one, la);\n369: /* delta = (v - kv) * beta; S += k (outer) delta; S back to memory */\n370: lds128(e, vq, la, OMEGA_GDN_SM_V, 1);\n371: for (unsigned j = 0; j < 4; j++) { fsub(e, R(dl[j]), S(vq, j), R(kv[j])); fmul(e, R(dl[j]), R(dl[j]), R(beta)); }\n372: {\n373: int k2[4];\n374: for (unsigned i = 0; i < 4; i++) k2[i] = V128(e);\n375: if (e->err) return -1;\n376: for (unsigned i = 0; i < 4; i++) lds128(e, k2[i], aw, OMEGA_GDN_SM_K + i * 16u, i == 0);\n377: for (unsigned r = 0; r < 16; r++) for (unsigned j = 0; j < 4; j++) ffma(e, S(sq[r], j), S(k2[r / 4], r % 4), R(dl[j]), S(sq[r], j));\n378: }\n379: if (spec->state_bf16) {\n380: int pk[16];\n381: for (unsigned r = 0; r < 16; r++) pk[r] = V64(e);\n382: if (e->err) return -1;\n383: for (unsigned r = 0; r < 16; r++) {\n384: bf16x4_pack(e, pk[r], sq[r]);\n385: for (unsigned w = 0; w < 2; w++) stg(e, sa, r * OMEGA_GDN_HEAD_DIM * 2u + w * 4u, S(pk[r], w), udesc);\n386: }\n387: } else {\n388: for (unsigned r = 0; r < 16; r++) for (unsigned j = 0; j < 4; j++) stg(e, sa, r * OMEGA_GDN_HEAD_DIM * 4u + j * 4u, S(sq[r], j), udesc);\n389: }\n390: /* the gate prefetch: z[h] and the norm weight, consumed in phase E */\n1020: i2fp(e, hf, half);\n1021: movi(e, fq, fbits(qs)); movi(e, t1, fbits(1.0f - qs));\n1022: ffma(e, R(fq), R(hf), R(t1), R(fq));\n1023: fmul(e, R(accA), R(accA), R(sqs)); fmul(e, R(accA), R(accA), R(fq));\n1024: imadi(e, R(ra), R(tid), 4u, NOREG);\n1025: sts32(e, ra, R(accA));\n1026: bar_sync(e);\n1027: }\n1028:\n1029: /* ---- phase C: scalars of token t ---- */\n1030: int abq = V64(e), xsc = V(e), mn = V(e), sp = V(e), dec = V(e), beta = V(e);\n1031: if (e->err) return -1;\n1032: ldg_wide(e, BW_IR_LDG_E_64, abq, aab, 0, udesc, 1);\n1033: fadd(e, R(xsc), S(abq, 0), S(scq, 1));\n1034: movi(e, mn, C_M1); fmul(e, R(mn), R(xsc), R(mn)); fminn(e, R(mn), R(xsc), R(mn));\n1035: fmul(e, R(mn), R(mn), R(cl2e)); mufu(e, BW_IR_MUFU_EX2, R(mn), R(mn)); fadd(e, R(mn), R(mn), R(fone));\n1036: mufu(e, BW_IR_MUFU_LG2, R(mn), R(mn));\n1037: movrz(e, R(sp)); fmaxx(e, R(sp), R(xsc), R(sp));\n1038: movi(e, t1, C_LN2); ffma(e, R(sp), R(mn), R(t1), R(sp));\n1039: if (MUT == 1) movi(e, dec, C_ONE);\n1040: else { fmul(e, R(dec), S(scq, 0), R(sp)); fmul(e, R(dec), R(dec), R(cl2e)); mufu(e, BW_IR_MUFU_EX2, R(dec), R(dec)); }\n1041: if (MUT == 2) movi(e, beta, C_ONE);\n1042: else {\n1043: fmul(e, R(beta), S(abq, 1), R(cnl2e)); mufu(e, BW_IR_MUFU_EX2, R(beta), R(beta));\n1044: fadd(e, R(beta), R(beta), R(fone)); mufu(e, BW_IR_MUFU_RCP, R(beta), R(beta));\n1045: }\n1046:\n1047: /* ---- phase D: the state update (S stays in registers) ---- */\n1048: int aw = V(e), pa = V(e), la = V(e), kq[4], kv[4], dl[4], vq = V128(e), zq = V128(e);\n1049: for (unsigned i = 0; i < 4; i++) { kq[i] = V128(e); kv[i] = V(e); dl[i] = V(e); }\n1050: if (e->err) return -1;\n1051: imadi(e, R(aw), R(warp), 16u * 4u, NOREG);\n1052: imadi(e, R(pa), R(warp), OMEGA_GDN_HEAD_DIM * 4u, NOREG); imadi(e, R(pa), R(lane), 16u, pa); imadi(e, R(pa), R(one), OMEGA_GDN_SM_P, pa);\n1053: imadi(e, R(la), R(lane), 16u, NOREG);\n1054: for (unsigned r = 0; r < 16; r++) for (unsigned j = 0; j < 4; j++) fmul(e, S(sq[r], j), S(sq[r], j), R(dec));\n1055: for (unsigned i = 0; i < 4; i++) lds128(e, kq[i], aw, OMEGA_GDN_SM_K + i * 16u, i == 0);\n1056: for (unsigned j = 0; j < 4; j++) fmul(e, R(kv[j]), S(sq[0], j), S(kq[0], 0));\n1057: for (unsigned r = 1; r < 16; r++) for (unsigned j = 0; j < 4; j++) ffma(e, R(kv[j]), S(sq[r], j), S(kq[r / 4], r % 4), R(kv[j]));\n1058: cross_warp_sum(e, kv, pa, one, la);\n1059: lds128(e, vq, la, OMEGA_GDN_SM_V, 1);\n1060: for (unsigned j = 0; j < 4; j++) { fsub(e, R(dl[j]), S(vq, j), R(kv[j])); fmul(e, R(dl[j]), R(dl[j]), R(beta)); }\n1061: {\n1062: int k2[4];\n1063: for (unsigned i = 0; i < 4; i++) k2[i] = V128(e);\n1064: if (e->err) return -1;\n1065: for (unsigned i = 0; i < 4; i++) lds128(e, k2[i], aw, OMEGA_GDN_SM_K + i * 16u, i == 0);\n1066: for (unsigned r = 0; r < 16; r++) for (unsigned j = 0; j < 4; j++) ffma(e, S(sq[r], j), S(k2[r / 4], r % 4), R(dl[j]), S(sq[r], j));\n1067: }\n1068: ldg_pf(e, zq, az, 0, udesc, SB_GATE);\n1069: int p[4];\n1070: {\n1071: int q2[4];\n1072: for (unsigned i = 0; i < 4; i++) { q2[i] = V128(e); p[i] = V(e); }\n1073: if (e->err) return -1;\n1074: for (unsigned i = 0; i < 4; i++) lds128(e, q2[i], aw, OMEGA_GDN_SM_Q + i * 16u, i == 0);\n1075: for (unsigned j = 0; j < 4; j++) fmul(e, R(p[j]), S(sq[0], j), S(q2[0], 0));\n1076: for (unsigned r = 1; r < 16; r++) for (unsigned j = 0; j < 4; j++) ffma(e, R(p[j]), S(sq[r], j), S(q2[r / 4], r % 4), R(p[j]));\n1077: }\n1078: bar_sync(e);\n1079: cross_warp_sum(e, p, pa, one, la);\n1080:\n1081: /* ---- phase E: gated RMSNorm, out row t ---- */\n1082: int ss = V(e), g = V(e);\n1083: if (e->err) return -1;\n1084: fmul(e, R(ss), R(p[0]), R(p[0]));\n1085: for (unsigned j = 1; j < 4; j++) ffma(e, R(ss), R(p[j]), R(p[j]), R(ss));", "omega@6de999da:src/omega_gpu_q36_graph.c": "590:}\n591:/* COMBINE|NORM of every head in one launch (L356): head h's residual x + h * 2048, down outputs a + h * 9 * 2048, weights\n592: * w + h * lb_stride, output y + h * 2048 (the xq or hidden slots). Each row runs the one-head program. ON unless\n593: * AIEN_OMEGA_Q36_CN_ROWS=0 (the per-head launches). */\n594:static int g_cn_rows = -1;\n595:static int q36_cn_rows(void) {\n596: if (g_cn_rows < 0) { const char *e = getenv(\"AIEN_OMEGA_Q36_CN_ROWS\"); g_cn_rows = (e && e[0] == '0' && e[1] == 0) ? 0 : 1; }\n597: return g_cn_rows;\n598:}\n599:static int rec_cn_rows(OmegaGpuQ36Graph *g, uint64_t x, uint64_t a, uint64_t wts, uint64_t nw, uint64_t y, float scale, uint32_t rows) {\n600: uint32_t w[OMEGA_DEC_RES_WORDS]; memset(w, 0, sizeof w);\n601: put64(w, OMEGA_DEC_RES_X, x); put64(w, OMEGA_DEC_RES_A, a); put64(w, OMEGA_DEC_RES_W, wts); put64(w, OMEGA_DEC_RES_NW, nw);\n602: put64(w, OMEGA_DEC_RES_Y, y); putf(w, OMEGA_DEC_RES_EPS, g->cfg.eps); putf(w, OMEGA_DEC_RES_SCALE, scale);\n603: return add_glue(g, OMEGA_GPU_Q36_K_RES, g->mutant == 4 ? K_CN_R_M3 : K_CN_R, 1, rows, (size_t)H * rows, w, OMEGA_DEC_RES_WORDS);\n604:}\n605:/* RES NORM|ROWS or ADD|ROWS over rows of 2048 (x, a, y move by the width per row) */\n606:static int rec_res_rows(OmegaGpuQ36Graph *g, int ki, uint64_t x, uint64_t a, uint64_t nw, uint64_t y, float scale, uint32_t rows) {\n607: uint32_t w[OMEGA_DEC_RES_WORDS]; memset(w, 0, sizeof w);\n608: put64(w, OMEGA_DEC_RES_X, x); put64(w, OMEGA_DEC_RES_A, a); put64(w, OMEGA_DEC_RES_NW, nw); put64(w, OMEGA_DEC_RES_Y, y);\n609: putf(w, OMEGA_DEC_RES_EPS, g->cfg.eps); putf(w, OMEGA_DEC_RES_SCALE, scale);\n610: return add_glue(g, ki == K_NORM_R ? OMEGA_GPU_Q36_K_NORM : OMEGA_GPU_Q36_K_RES, ki, 1, rows, (size_t)H * rows, w, OMEGA_DEC_RES_WORDS);\n611:}\n612:\n613:int omega_gpu_q36_graph_record_layer_heads(OmegaGpuQ36Graph *g, uint32_t l, const uint32_t *pos) {\n614: if (!g || !g->in_token || !pos) return OMEGA_GPU_DGRAPH_BAD_ARGS;\n615: if (g->failed) return OMEGA_GPU_DGRAPH_CHIP_FAIL;\n616: if (!g->batch) return fail(g, \"q36 graph: record_layer_heads needs set_batch\", OMEGA_GPU_DGRAPH_BAD_ARGS);\n617: if (l >= g->cfg.layers || !g->layer[l].set || (l + 1 == g->cfg.layers && !g->final_set))\n618: return fail(g, \"q36 graph: layer not set, out of order, or final norm missing\", OMEGA_GPU_DGRAPH_BAD_ARGS);\n619: const uint32_t N = g->stroke_heads, R = N * NG, ki = g->layer[l].kind_index;\n620: for (uint32_t h = 0; h < N; h++)\n621: if (g->hidden_ready[h] || g->xq_layer[h] != g->xq_layer[0]) return fail(g, \"q36 graph: heads out of step\", OMEGA_GPU_DGRAPH_BAD_ARGS);\n622: const Layout *L = &g->lo;\n623: const uint64_t x0 = va_of(g, x_of(g, 0)), xq0 = va_of(g, xq_of(g, 0)), xp = va_of(g, L->xp), mixN = va_of(g, L->mixN), abN = va_of(g, L->abN);\n624: const uint64_t aggN = va_of(g, L->aggN), attN = va_of(g, L->attN), op = va_of(g, L->op), oN = va_of(g, L->oN), rlb = va_of(g, L->rlb);\n625: const uint64_t sgu = va_of(g, L->sgu), sdn = va_of(g, L->sdn), xmap = va_of(g, L->xmap), ymap = va_of(g, L->ymap), xp2 = va_of(g, L->xp2);\n626: const uint64_t guN = va_of(g, L->guN), actN = va_of(g, L->actN), actp = va_of(g, L->actp), dnN = va_of(g, L->dnN);\n627: const uint64_t pt2048 = va_of(g, L->ptab), pt4096 = pt2048 + (uint64_t)H * 4u, pt512 = pt4096 + (uint64_t)OMEGA_GPU_Q36_MIXER_OUT * 4u;\n628: const uint64_t in_norm = va_of(g, L->norms) + (uint64_t)l * H * 4u, segd = va_of(g, L->segd) + (uint64_t)l * SEGD_ROWS * OMEGA_GEMM_SEG_BYTES;\n629: const uint64_t XS = (uint64_t)H * 4u, MOS = (uint64_t)OMEGA_GPU_Q36_MIXER_OUT * 4u;\n630: int rc = OMEGA_GPU_DGRAPH_OK;\n631: /* input norm of every head (layer 0; after that the previous layer's COMBINE|NORM left layer l's x' in the xq slots) */\n632: if (g->xq_layer[0] != (int)l) rc = rec_res_rows(g, K_NORM_R, x0, 0, in_norm, xq0, SCALE_2P64, N);\n633: /* x' of the dense trunk: the N xq rows in the GEMM's lane layout */\n634: if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_XQ, xq0, pt2048, xp, H, N);\n635: if (!g->layer[l].attn) {\n636: if (rc == OMEGA_GPU_DGRAPH_OK) rc = after_op(g, OMEGA_GPU_Q36_K_AB, omega_gpu_bf16_gemv_rows_record(g->cl, g->layer[l].ab, N, xq0, abN));\n637: if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_gemm(g, OMEGA_GPU_Q36_K_MIX, g->layer[l].mix, N, 1, segd, xp, mixN, 0, 0);\n638: for (uint32_t h = 0; h < N && rc == OMEGA_GPU_DGRAPH_OK; h++) {\n639: const uint64_t mix_h = mixN + (uint64_t)h * OMEGA_GPU_Q36_GDN_MIX * 4u;\n640: uint64_t mo = 0;\n641: rc = after_op(g, OMEGA_GPU_Q36_K_GDN, omega_gpu_gdn_record(g->cl, g->gdn_h[h], ki, mix_h, mix_h + OMEGA_GPU_Q36_GDN_QKV * 4u,\n642: abN + (uint64_t)h * OMEGA_GPU_Q36_AB * 4u, g->cfg.eps_l2, g->cfg.eps, 1.0f, SCALE_2P64, &mo));\n643: if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_O, mo, pt4096, op + h * MOS, OMEGA_GPU_Q36_MIXER_OUT, 1);\n644: }\n645: } else {"}