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
CUDApp / om / moe / q36-prefill-integration.patch
| 1 | --- a/src/omega_gpu_q36_graph.c |
| 2 | +++ b/src/omega_gpu_q36_graph.c |
| 3 | @@ -888,12 +888,12 @@ |
| 4 | int omega_gpu_q36_graph_prefill_alloc(OmegaGpuQ36Graph *g, uint32_t cap) { |
| 5 | if (!g || g->in_token || cap == 0 || cap > OMEGA_GPU_Q36_PREFILL_MAX_ROWS) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 6 | if (g->pf.cap >= cap) return OMEGA_GPU_DGRAPH_OK; |
| 7 | - const size_t P = cap, R = P * NG, RR = P * (NG - 1u), lbs = g->lo.lb_stride; |
| 8 | + const size_t P = cap, R = P * NG, lbs = g->lo.lb_stride; |
| 9 | size_t ho = 0, dof = 0; |
| 10 | #define HREG(f, b) do { g->pf.f = ho; ho += al(b); } while (0) |
| 11 | #define DREG(f, b) do { g->pf.f = dof; dof += al(b); } while (0) |
| 12 | HREG(x, P * H * 4); HREG(rlb, P * lbs); HREG(sgu, 256u * OMEGA_GEMM_SEG_BYTES); HREG(sdn, 256u * OMEGA_GEMM_SEG_BYTES); |
| 13 | - HREG(sgu2, OMEGA_GEMM_SEG_BYTES); HREG(sdn2, OMEGA_GEMM_SEG_BYTES); HREG(xmap, (RR + PF_PAD) * 4); HREG(ymap, (RR + PF_PAD) * 4); |
| 14 | + HREG(sgu2, OMEGA_GEMM_SEG_BYTES); HREG(sdn2, OMEGA_GEMM_SEG_BYTES); HREG(xmap, (R + PF_PAD) * 4); HREG(ymap, (R + PF_PAD) * 4); |
| 15 | HREG(xmap2, (P + PF_PAD) * 4); HREG(ymap2, (P + PF_PAD) * 4); |
| 16 | DREG(xq, P * H * 4); DREG(xp, (P + PF_PAD) * H * 4); DREG(mixN, P * OMEGA_GPU_Q36_GDN_MIX * 4); DREG(abN, P * OMEGA_GPU_Q36_AB * 4); |
| 17 | DREG(aggN, P * OMEGA_GPU_Q36_ATT_MIX * 4); DREG(attN, P * OMEGA_GPU_Q36_MIXER_OUT * 4); DREG(op, (P + PF_PAD) * OMEGA_GPU_Q36_MIXER_OUT * 4); |
| 18 | @@ -944,7 +944,8 @@ |
| 19 | static uint64_t pf_d(const OmegaGpuQ36Graph *g, size_t off) { return g->pf.db.va + off; } |
| 20 | |
| 21 | /* part (a) of layer l: the mixer of every row and the router; ends with the router outputs in pf.rlb */ |
| 22 | -static int pf_record_mixer(OmegaGpuQ36Graph *g, uint32_t head, uint32_t l, uint32_t n, uint32_t pos0, uint32_t *ids_off, uint32_t *wts_off, uint32_t *xo_off) { |
| 23 | +static int pf_record_mixer(OmegaGpuQ36Graph *g, uint32_t head, uint32_t l, uint32_t n, uint32_t pos0, uint32_t *ids_off, uint32_t *wts_off, uint32_t *xo_off, |
| 24 | + OmegaGpuRouterDevOut *first_router) { |
| 25 | const uint32_t ki = g->layer[l].kind_index; |
| 26 | const uint64_t x0 = pf_h(g, g->pf.x), xq0 = pf_d(g, g->pf.xq), xp = pf_d(g, g->pf.xp), mixN = pf_d(g, g->pf.mixN), abN = pf_d(g, g->pf.abN); |
| 27 | const uint64_t aggN = pf_d(g, g->pf.aggN), attN = pf_d(g, g->pf.attN), op = pf_d(g, g->pf.op), oN = pf_d(g, g->pf.oN), rlb = pf_h(g, g->pf.rlb); |
| 28 | @@ -1017,7 +1018,10 @@ |
| 29 | const uint64_t lb = rlb + (uint64_t)r0 * g->lo.lb_stride; |
| 30 | rc = after_op(g, OMEGA_GPU_Q36_K_ROUTER, omega_gpu_router_record_rows(g->cl, g->cfg.router, l, x0 + r0 * XS, (uint32_t)XS, g->cfg.eps, 1, SCALE_2P64, |
| 31 | lb, g->lo.lb_stride, rows, &r)); |
| 32 | - if (rc == OMEGA_GPU_DGRAPH_OK && r0 == 0) { *ids_off = (uint32_t)(r.ids_va - lb); *wts_off = (uint32_t)(r.wts_va - lb); *xo_off = (uint32_t)(r.xout_va - lb); } |
| 33 | + if (rc == OMEGA_GPU_DGRAPH_OK && r0 == 0) { |
| 34 | + *ids_off = (uint32_t)(r.ids_va - lb); *wts_off = (uint32_t)(r.wts_va - lb); *xo_off = (uint32_t)(r.xout_va - lb); |
| 35 | + if (first_router) *first_router = r; |
| 36 | + } |
| 37 | } |
| 38 | return rc; |
| 39 | } |
| 40 | @@ -1096,6 +1100,44 @@ |
| 41 | for (uint32_t t = last ? n - 1u : 0u; t < n && rc == OMEGA_GPU_DGRAPH_OK; t++) |
| 42 | rc = rec_res_w(g, g->mutant == 4 ? K_CN_M3 : K_CN, x0 + t * XS, dnN + (uint64_t)t * NG * H * 4u, rlb + (uint64_t)t * g->lo.lb_stride + wts_off, nw, |
| 43 | last ? va_of(g, hidden_of(g, head)) : xq0 + t * XS, last && !g->head8 ? 1.0f : SCALE_2P64); |
| 44 | + return rc; |
| 45 | +} |
| 46 | + |
| 47 | +/* Private adapter: graph resolver validates/pins canonical realization first. |
| 48 | + * w is one moe_windows record; group_words is exact current-router moe_admit output. |
| 49 | + * No submit/sync or new executor. Refuse flush_each via moe_windows cfg7. */ |
| 50 | +static inline int pf_record_moe_window(OmegaGpuQ36Graph *g, uint32_t l, const uint64_t w[8], |
| 51 | + const uint32_t group_words[OMEGA_DEC_GR_WORDS], uint64_t router_xo) { |
| 52 | + const uint32_t n = (uint32_t)w[1], rows = (uint32_t)w[2]; |
| 53 | + const uint64_t xp = pf_d(g, g->pf.xp2) + w[3], gu = pf_d(g, g->pf.guN) + w[4]; |
| 54 | + const uint64_t act = pf_d(g, g->pf.actN) + w[5], actp = pf_d(g, g->pf.actp) + w[5], dn = pf_d(g, g->pf.dnN) + w[6]; |
| 55 | + const uint64_t xm = pf_h(g, g->pf.xmap), ym = pf_h(g, g->pf.ymap); |
| 56 | + const uint64_t pt2048 = va_of(g, g->lo.ptab), pt512 = pt2048 + (uint64_t)(H + OMEGA_GPU_Q36_MIXER_OUT) * 4u; |
| 57 | + int rc = add_glue(g, OMEGA_GPU_Q36_K_GROUP, K_GROUP, 1, 1, rows, group_words, OMEGA_DEC_GR_WORDS); |
| 58 | + if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_XO, router_xo + w[7], pt2048, xp, H, n); |
| 59 | + if (rc == OMEGA_GPU_DGRAPH_OK) |
| 60 | + rc = native_record_gemm(g, OMEGA_GPU_Q36_K_GATE_UP, g->layer[l].gu, rows, rows, pf_h(g, g->pf.sgu), xp, gu, xm, ym); |
| 61 | + uint32_t sw[OMEGA_DEC_SW_WORDS]; memset(sw, 0, sizeof sw); |
| 62 | + put64(sw, OMEGA_DEC_SW_GU, gu); put64(sw, OMEGA_DEC_SW_ACT, act); putf(sw, OMEGA_DEC_SW_SCALE, SCALE_2P64); |
| 63 | + if (rc == OMEGA_GPU_DGRAPH_OK) |
| 64 | + rc = add_glue(g, OMEGA_GPU_Q36_K_SWIGLU, g->mutant == 1 ? K_SW_M1 : K_SW, EI / OMEGA_DEC_THREADS, rows, (size_t)rows * EI, sw, OMEGA_DEC_SW_WORDS); |
| 65 | + if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_ACT, act, pt512, actp, EI, rows); |
| 66 | + if (rc == OMEGA_GPU_DGRAPH_OK) |
| 67 | + rc = native_record_gemm(g, OMEGA_GPU_Q36_K_DOWN, g->layer[l].dn, rows, rows, pf_h(g, g->pf.sdn), actp, dn, ym, ym); |
| 68 | + return rc; |
| 69 | +} |
| 70 | + |
| 71 | +/* Same incumbent contribution order after all windows, not #18 fusion. */ |
| 72 | +static inline int pf_record_moe_combine(OmegaGpuQ36Graph *g, uint32_t head, uint32_t l, uint32_t n, uint32_t wts_off) { |
| 73 | + const uint64_t x0 = pf_h(g, g->pf.x), dn = pf_d(g, g->pf.dnN), xq = pf_d(g, g->pf.xq), rlb = pf_h(g, g->pf.rlb); |
| 74 | + const uint64_t in_norm = va_of(g, g->lo.norms) + (uint64_t)l * H * 4u, XS = (uint64_t)H * 4u; |
| 75 | + const int last = l + 1 == g->cfg.layers; |
| 76 | + const uint64_t nw = last ? va_of(g, g->lo.fnorm) : in_norm + H * 4u; |
| 77 | + if (!last && n > 1u && q36_cn_rows()) return rec_cn_rows(g, x0, dn, rlb + wts_off, nw, xq, SCALE_2P64, n); |
| 78 | + int rc = OMEGA_GPU_DGRAPH_OK; |
| 79 | + for (uint32_t t = last ? n - 1u : 0u; t < n && rc == OMEGA_GPU_DGRAPH_OK; t++) |
| 80 | + rc = rec_res_w(g, g->mutant == 4 ? K_CN_M3 : K_CN, x0 + t * XS, dn + (uint64_t)t * NG * H * 4u, rlb + (uint64_t)t * g->lo.lb_stride + wts_off, |
| 81 | + nw, last ? va_of(g, hidden_of(g, head)) : xq + t * XS, last && !g->head8 ? 1.0f : SCALE_2P64); |
| 82 | return rc; |
| 83 | } |
| 84 | |
| 85 | @@ -1123,7 +1165,7 @@ |
| 86 | } |
| 87 | for (uint32_t l = 0; l < g->cfg.layers && rc == OMEGA_GPU_DGRAPH_OK; l++) { |
| 88 | uint32_t ids_off = 0, wts_off = 0, xo_off = 0; |
| 89 | - rc = pf_record_mixer(g, head, l, n, pos0, &ids_off, &wts_off, &xo_off); |
| 90 | + rc = pf_record_mixer(g, head, l, n, pos0, &ids_off, &wts_off, &xo_off, NULL); |
| 91 | if (rc == OMEGA_GPU_DGRAPH_OK) rc = submit_now(g); |
| 92 | if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_session_sync_to_cpu(&g->pf.hb) != 0) rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); |
| 93 | uint32_t segs = 0; |