diff --git a/Makefile b/Makefile index 0a8a8b80..a6b03768 100644 --- a/Makefile +++ b/Makefile @@ -375,6 +375,9 @@ $(GPU_QMMA_PROBE_TEST): tests/gpu_qmma_probe_test.c tests/bw_qmma_model.h src/om GPU_QGEMM_TEST = $(OUT_DIR)/gpu_qgemm_test $(GPU_QGEMM_TEST): tests/gpu_qgemm_test.c tests/bw_qmma_model.h tests/bw_warp_sim_tc.h src/omega_gpu_prefill.h src/omega_blackwell_codegen_qg.h $(OUT_DIR)/libomega_gpu.a $(CC) $(CFLAGS) -ffp-contract=off -DWS_MAX_THREADS=1024 -Itests -o $@ tests/gpu_qgemm_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm +GPU_PFQ_TEST = $(OUT_DIR)/gpu_pfq_test +$(GPU_PFQ_TEST): tests/gpu_pfq_test.c tests/pfq_legacy_words.h tests/bw_qmma_model.h tests/bw_warp_sim_tc.h src/omega_gpu_pfq_api.h src/omega_blackwell_codegen_qg.h $(OUT_DIR)/libomega_gpu.a + $(CC) $(CFLAGS) -ffp-contract=off -DWS_MAX_THREADS=1024 -Itests -o $@ tests/gpu_pfq_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm .PHONY: test-gemm-selectors test-gemm-selectors: $(GPU_PFGEMM_TEST) $(GPU_QGEMM_TEST) $(GPU_FP8GEMM_TEST) python3 tests/gemm_selectors.py $(OUT_DIR) @@ -382,11 +385,21 @@ test-gemm-selectors: $(GPU_PFGEMM_TEST) $(GPU_QGEMM_TEST) $(GPU_FP8GEMM_TEST) GPU_PFHOST_TEST = $(OUT_DIR)/gpu_pfhost_test $(GPU_PFHOST_TEST): tests/gpu_pfhost_test.c tests/bw_warp_sim_tc.h src/omega_gpu_pfhost.h src/omega_blackwell_codegen_pfhost.h $(OUT_DIR)/libomega_gpu.a $(CC) $(CFLAGS) -ffp-contract=off -DWS_MAX_THREADS=1024 -Itests -o $@ tests/gpu_pfhost_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm +# DRA-83: the q36 prefill chunk (row bounds, chunk knob, layout bytes, per-chunk streamed count, mutant 7, grid-y split). +# The graph source is compiled in with the session/cmdlist/GEMM/prefill-kernel entry points it reaches stubbed (the DRA-82 +# QMMA planner and its code generator's spec checks linked); references only reachable from unexercised paths (native MoE, +# device submit, kernel generation) stay unresolved and are never called. No device is opened. +GPU_Q36_PFCHUNK_TEST = $(OUT_DIR)/gpu_q36_pfchunk_test +$(GPU_Q36_PFCHUNK_TEST): tests/gpu_q36_pfchunk_test.c src/omega_gpu_q36_graph.c src/omega_gpu_q36_graph.h src/omega_gpu_pfq_api.c \ + src/omega_gpu_pfq_api.h src/omega_blackwell_codegen_qg.c + @mkdir -p $(OUT_DIR) + $(CC) $(CFLAGS) -ffunction-sections -fdata-sections -o $@ tests/gpu_q36_pfchunk_test.c src/omega_gpu_pfq_api.c \ + src/omega_blackwell_codegen_qg.c -Wl,--gc-sections -Wl,--unresolved-symbols=ignore-in-object-files -lm # omega#456: the prefill tests above, host model and warp simulator only. Every run names --host-only or --sim, the two # modes that never open the device (each test's main returns before its chip part); chip runs stay under the GB10 lock. .PHONY: test-gpu-prefill-host test-gpu-prefill-host: $(GPU_TC_PROBE_TEST) $(GPU_TCGEMM_TEST) $(GPU_PATTN_TEST) $(GPU_PF_MEM_TEST) $(GPU_PFGEMM_TEST) \ - $(GPU_QMMA_PROBE_TEST) $(GPU_QGEMM_TEST) $(GPU_PFHOST_TEST) + $(GPU_QMMA_PROBE_TEST) $(GPU_QGEMM_TEST) $(GPU_PFHOST_TEST) $(GPU_PFQ_TEST) $(GPU_Q36_PFCHUNK_TEST) ./$(GPU_TC_PROBE_TEST) --host-only ./$(GPU_QMMA_PROBE_TEST) --host-only ./$(GPU_PF_MEM_TEST) --host-only @@ -400,7 +413,10 @@ test-gpu-prefill-host: $(GPU_TC_PROBE_TEST) $(GPU_TCGEMM_TEST) $(GPU_PATTN_TEST) ./$(GPU_QGEMM_TEST) --sim ./$(GPU_PFHOST_TEST) --host-only ./$(GPU_PFHOST_TEST) --sim - @echo "test-gpu-prefill-host: PASS (8 prefill tests, host model + warp simulator, no device)" + ./$(GPU_PFQ_TEST) --host-only + ./$(GPU_PFQ_TEST) --sim + ./$(GPU_Q36_PFCHUNK_TEST) + @echo "test-gpu-prefill-host: PASS (10 prefill tests, host model + warp simulator, no device)" # omega#456: tools/own30b_decode_profile.c is a link-only recorder built by hand on the Spark; compile it with the # project's -Werror flags so a warning cannot land unseen. .PHONY: check-own30b-decode-profile diff --git a/src/omega_blackwell_codegen_qg.c b/src/omega_blackwell_codegen_qg.c index 1455f9f9..a6471b00 100644 --- a/src/omega_blackwell_codegen_qg.c +++ b/src/omega_blackwell_codegen_qg.c @@ -14,12 +14,13 @@ static void fmax_r(Em *e, int d, int a, unsigned asub, int b, unsigned bsub) { int omega_pq_spec_ok(const OmegaPqSpec *s) { if (!s || s->k < 128u || s->k > 32768u || s->k % 128u) return 0; const uint32_t kb = s->k / 128u, g = s->groups ? s->groups : kb; + if (s->stride && (s->stride < s->k || s->stride % 4u || s->stride > (1u << 24))) return 0; return g <= 32u && kb % g == 0 && s->in_type <= OMEGA_QG_IN_BF16 && s->mutant >= 0 && s->mutant <= 3; } int omega_blackwell_codegen_pq_prog(const OmegaPqSpec *spec, BlackwellIRProgram *prog) { if (!omega_pq_spec_ok(spec) || !prog) return -1; - const uint32_t K = spec->k, KB = K / 128u; + const uint32_t K = spec->k, KB = K / 128u, XS = spec->stride ? spec->stride : K; omega_bw_ir_init(prog); Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em; int udesc = UV64(e), px = V64(e), pq = V64(e), ps = V64(e), tid = V(e), row = V(e), warp = V(e); @@ -33,8 +34,8 @@ int omega_blackwell_codegen_pq_prog(const OmegaPqSpec *spec, BlackwellIRProgram s2r(e, cy, BW_SR_CTAID_Y); if (spec->mutant != 3) imadi(e, tid, cy, 0, 32u * spec->groups, tid); } shr(e, warp, tid, 0, 5); - if (spec->in_type == OMEGA_QG_IN_F32) { imadw(e, px, row, K * 4u, px); imadw(e, px, tid, 16u, px); ldg_wide(e, BW_IR_LDG_E_128, v, px, 0, udesc, 1); } - else { imadw(e, px, row, K * 2u, px); imadw(e, px, tid, 8u, px); ldg_wide(e, BW_IR_LDG_E_64, w, px, 0, udesc, 1); } + if (spec->in_type == OMEGA_QG_IN_F32) { imadw(e, px, row, XS * 4u, px); imadw(e, px, tid, 16u, px); ldg_wide(e, BW_IR_LDG_E_128, v, px, 0, udesc, 1); } + else { imadw(e, px, row, XS * 2u, px); imadw(e, px, tid, 8u, px); ldg_wide(e, BW_IR_LDG_E_64, w, px, 0, udesc, 1); } imadw(e, pq, row, K, pq); imadw(e, pq, tid, 4u, pq); imadw(e, ps, row, KB * 4u, ps); imadw(e, ps, warp, 4u, ps); movi(e, fl, 0x0d800000u); /* 2^-100 */ @@ -79,6 +80,7 @@ int omega_qg_spec_ok(const OmegaQgSpec *s) { if (s->n == 0 || s->n % (s->warps * 16u) || (uint64_t)(s->epi == OMEGA_QG_EPI_F32 ? 4u : 1u) * s->n * 16u * s->rt >= (1u << 23)) return 0; /* row stride (n f32, or n / 2 bf16 = n bytes) x 16 rt: the store offsets fit the signed 24-bit field */ if ((16u * s->rt * 4u) % (32u * s->warps)) return 0; /* whole 16-byte staging slots per thread */ if (s->mutant < 0 || s->mutant > 4) return 0; + if (s->ymap > 1u || s->x64 > 1u || (s->ymap && s->epi != OMEGA_QG_EPI_F32)) return 0; return 1; } uint32_t omega_qg_shared_bytes(const OmegaQgSpec *s) { return omega_qg_spec_ok(s) ? 2u * (16u * s->rt * 64u + 16u * s->rt * 4u) : 0u; } @@ -152,7 +154,8 @@ int omega_blackwell_codegen_qgemm_prog(const OmegaQgSpec *spec, BlackwellIRProgr for (uint32_t jj = 0; jj < NJ; jj++) { stgr[jj] = V128(e); scv[jj] = V(e); } if (e->err) return -1; movi(e, onef1, 0x3f800000u); - movi(e, c2m56, 0x23800000u); /* 2^-56: undoes the GEMV tensor's scale fold exactly (sw * 2^-56 is the checkpoint bf16 scale) */ + movi(e, c2m56, spec->x64 ? 0x03800000u : 0x23800000u); /* 2^-56 undoes the GEMV tensor's scale fold exactly (sw * 2^-56 is the + checkpoint bf16 scale); x64 also removes the activations' 2^64: 2^-120 */ #define ISSUE(wset, sb, xoff, woff, with_scale) do { \ for (uint32_t jj = 0; jj < NJ; jj++) { ldw_pf(e, BW_IR_LDG_E_128, stgr[jj], gx[jj], (xoff), udesc, (sb)); ldw_pf(e, BW_IR_LDG_E, scv[jj], gs[jj], 0u, udesc, (sb)); } \ @@ -216,6 +219,8 @@ int omega_blackwell_codegen_qgemm_prog(const OmegaQgSpec *spec, BlackwellIRProgr /* ---- epilogue (as omega_blackwell_codegen_tc2.c, natural SwiGLU order, no extra scale) ---- */ int g2 = V(e), cnt2 = V(e), rrow = V(e), onee = V(e), pyr = V64(e); + int pym = spec->ymap ? V64(e) : NOREG, colb = spec->ymap ? V(e) : NOREG, py0 = spec->ymap ? V64(e) : NOREG, ysx = spec->ymap ? V64(e) : NOREG; + int yrow = spec->ymap ? V(e) : NOREG, r0y = spec->ymap ? V(e) : NOREG, n2y = spec->ymap ? V(e) : NOREG; { int tid2 = V(e), cx2 = V(e), cy2 = V(e), lane2 = V(e), tq3 = V(e), tab2 = V64(e), ten2 = V64(e), rc2 = V64(e), py2 = V64(e), n2 = V(e), nw2 = V(e), r02 = V(e); if (e->err) return -1; @@ -226,7 +231,10 @@ int omega_blackwell_codegen_qgemm_prog(const OmegaQgSpec *spec, BlackwellIRProgr andi(e, lane2, tid2, 0, 31); shr(e, g2, lane2, 0, 2); andi(e, tq3, lane2, 0, 3); imadi(e, r02, rc2, 0, 1, NOREG); imadi(e, cnt2, rc2, 1, 1, NOREG); movi(e, onee, 1); shr(e, tmp, tid2, 0, 5); imadi(e, tmp, tmp, 0, 16u, NOREG); imadi(e, nw2, cx2, 0, SPAN, tmp); - if (spec->epi == OMEGA_QG_EPI_F32) { + if (spec->ymap) { /* DRA-82: row r goes to y row ymap[row0 + r]; the column part is shared by every row */ + ldc64(e, pym, 0x3b0); imadi(e, tmp2, tq3, 0, 2u, nw2); imadi(e, colb, tmp2, 0, 1u, NOREG); + imadw(e, py0, onee, 0, py2); imadi(e, r0y, r02, 0, 1u, NOREG); imadi(e, n2y, n2, 0, 1u, NOREG); + } else if (spec->epi == OMEGA_QG_EPI_F32) { iadd(e, tmp, r02, g2); imadr(e, tmp, tmp, n2, nw2); imadi(e, tmp2, tq3, 0, 2u, NOREG); iadd(e, tmp, tmp, tmp2); imadw(e, pyr, tmp, 4u, py2); } else { iadd(e, tmp, r02, g2); imadr(e, tmp, tmp, n2, NOREG); imadw(e, pyr, tmp, 1u, py2); @@ -245,10 +253,14 @@ int omega_blackwell_codegen_qgemm_prog(const OmegaQgSpec *spec, BlackwellIRProgr int skips[16]; uint32_t nskip = 0; const int at_bssy = bssy_fwd(e); for (uint32_t i = 0; i < RT; i++) for (uint32_t h = 0; h < 2; h++) { - const uint32_t roff = (16u * i + 8u * h) * ystride; + const uint32_t roff = spec->ymap ? 0u : (16u * i + 8u * h) * ystride; imadi(e, rrow, onee, 0, 16u * i + 8u * h, g2); isetp_ge_u32(e, rrow, cnt2); skips[nskip++] = bra_if_p0_fwd(e); + if (spec->ymap) { + iadd(e, yrow, r0y, rrow); imadw(e, ysx, yrow, 4u, pym); ldg32(e, yrow, ysx, udesc, 1); + imadr(e, yrow, yrow, n2y, colb); imadw(e, pyr, yrow, 4u, py0); + } if (spec->epi == OMEGA_QG_EPI_F32) { for (uint32_t j = 0; j < 2; j++) for (unsigned q = 0; q < 2; q++) { BlackwellIRInsn st = I0(BW_IR_STG_E); st.src1_vreg = pyr; st.src2_vreg = acc[i * 2 + j]; st.src2_subreg = (uint8_t)(2u * h + q); st.imm = roff + j * 32u + 4u * q; st.ureg = udesc; emit(e, st, K_STORE); } } else { diff --git a/src/omega_blackwell_codegen_qg.h b/src/omega_blackwell_codegen_qg.h index 55202831..35a95d1a 100644 --- a/src/omega_blackwell_codegen_qg.h +++ b/src/omega_blackwell_codegen_qg.h @@ -19,6 +19,8 @@ * CTA y covers groups yG .. yG+G-1 (thread index offset by 32 G y). groups 0 is the one-CTA form (programs unchanged). * Arguments (cbank 0x380): x u64, q u64, s u64. * mutant (tests only): 1 skips the butterfly (lane-local amax), 2 uses c = 0, 3 drops the ctaid.y offset (groups form). + * stride (DRA-82): input row stride in elements, 0 = k (programs unchanged); a nonzero stride (>= k, multiple of 4) lets + * the quantiser read rows of a wider buffer in place (the q36 router rows, lb_stride / 4). q and s stay dense. */ #include #include "omega_blackwell_codegen.h" @@ -27,7 +29,7 @@ extern "C" { #endif enum { OMEGA_QG_IN_F32 = 0, OMEGA_QG_IN_BF16 = 1 }; -typedef struct { uint32_t k, in_type; int mutant; uint32_t groups; } OmegaPqSpec; +typedef struct { uint32_t k, in_type; int mutant; uint32_t groups; uint32_t stride; } OmegaPqSpec; #define OMEGA_PQ_ARGS_WORDS 6u int omega_pq_spec_ok(const OmegaPqSpec *s); int omega_blackwell_codegen_pq_prog(const OmegaPqSpec *s, BlackwellIRProgram *prog); @@ -47,7 +49,12 @@ int omega_blackwell_codegen_pq_prog(const OmegaPqSpec *s, BlackwellIRProgram *pr * Constant bank 0x380: tiles, rowidx (ignored when direct), xq, y, xs (u64 each), n u32 at 0x3a8. * Grid: n / (warps * 16) x tiles; 32 * warps threads; shared 2 * (16 rt * 64 + 16 rt * 4) bytes. * mutant (tests only): 1 drops sa (uses sw only), 2 never advances the sw pointer, 3 row tile i reads tile i^1, 4 drops step 1 - * of every odd chunk. */ + * of every odd chunk. + * DRA-82 (each 0 = programs unchanged): + * ymap 1 (F32 only): row r of a tile is stored to y row ymap[row0 + r] (u32 table, u64 pointer at cbank 0x3b0; + * OMEGA_QG_ARGS_WORDS_YMAP argument words), read only for rows below cnt. Without ymap y row = row0 + r. + * x64 1: the activations carry an extra 2^64 (the q36 graph's x' and act rows): the scale fold uses 2^-120 instead of + * 2^-56, so y is the true product; exact, since 2^64 moves only exponents of the power-of-two pq scales. */ enum { OMEGA_QG_EPI_F32 = 0, OMEGA_QG_EPI_SWIGLU = 1 }; typedef struct { uint32_t k; /* multiple of 128, 128 .. 32768 */ @@ -58,8 +65,11 @@ typedef struct { uint32_t n; /* output width (weight rows), multiple of warps * 16 */ uint32_t wait; /* stall cycles between a row tile's last QMMA and the first FFMA reading its result */ int mutant; + uint32_t ymap; /* DRA-82: 1 scatters rows through the y map (F32 only) */ + uint32_t x64; /* DRA-82: 1 removes a 2^64 activation scale in the fold */ } OmegaQgSpec; #define OMEGA_QG_ARGS_WORDS 11u +#define OMEGA_QG_ARGS_WORDS_YMAP 14u #define OMEGA_QG_TILE_BYTES 32u int omega_qg_spec_ok(const OmegaQgSpec *s); uint32_t omega_qg_shared_bytes(const OmegaQgSpec *s); diff --git a/src/omega_gpu_pfq_api.c b/src/omega_gpu_pfq_api.c new file mode 100644 index 00000000..9117354c --- /dev/null +++ b/src/omega_gpu_pfq_api.c @@ -0,0 +1,141 @@ +/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ +/* omega_gpu_pfq_api.c: tiled mma.sync (QMMA) FP8 path for prefill expert GEMMs (DRA-82). See the header. */ +#include "omega_gpu_pfq_api.h" +#include "omega_gpu_prefill.h" +#include "omega_gpu_cmdlist.h" +#include +#include + +_Static_assert(sizeof(OmegaPfqTile) == OMEGA_QG_TILE_BYTES, "tile entry = the q36 graph's segment entry"); +_Static_assert(sizeof(OmegaQgSpec) <= sizeof(((OmegaPfKey *)0)->words), "gemm spec fits the kernel key"); +_Static_assert(sizeof(OmegaPqSpec) <= sizeof(((OmegaPfKey *)0)->words), "quant spec fits the kernel key"); + +static int mutant = -1; +int omega_gpu_pfq_mutant(void) { + if (mutant < 0) { + const char *v = getenv("AIEN_OMEGA_PFQ_MUTANT"); + const long m = v && *v ? strtol(v, NULL, 10) : 0; + mutant = m >= 1 && m <= 4 ? (int)m : 0; + } + return mutant; +} +void omega_gpu_pfq_test_set_mutant(int m) { mutant = m >= 1 && m <= 4 ? m : 0; } + +static uint32_t pfq_wait(void) { + static int w = -1; + if (w < 0) { + const char *v = getenv("AIEN_OMEGA_PFQ_WAIT"); + const long x = v && *v ? strtol(v, NULL, 10) : 45; + w = x >= 0 && x <= 120 ? (int)x : 45; /* 45 cycles: the chip-safe QMMA read distance (evidence OWN30B-L1G-QMMA-PFGEMM-c742aa13) */ + } + return (uint32_t)w; +} + +uint32_t omega_pfq_tile_cap(uint32_t rows, uint32_t segs) { return segs + rows / OMEGA_PFQ_BM; } + +uint32_t omega_pfq_tiles(const OmegaPfqTile *segs, uint32_t nsegs, OmegaPfqTile *tiles, uint32_t cap) { + if (!segs || !tiles) return 0; + const uint32_t skew = omega_gpu_pfq_mutant() == 1 ? 1u : 0u; + uint32_t nt = 0; + for (uint32_t i = 0; i < nsegs; i++) { + const OmegaPfqTile *s = &segs[i]; + for (uint32_t r = 0; r < s->cnt; r += OMEGA_PFQ_BM) { + if (nt >= cap) return 0; + tiles[nt].w_va = s->w_va; tiles[nt].s_va = s->s_va; + tiles[nt].row0 = s->row0 + r - (r ? skew : 0u); + tiles[nt].cnt = s->cnt - r < OMEGA_PFQ_BM ? s->cnt - r : OMEGA_PFQ_BM; + tiles[nt].pad = 0; nt++; + } + } + return nt; +} + +static void put64(uint32_t *a, uint64_t v) { a[0] = (uint32_t)v; a[1] = (uint32_t)(v >> 32); } + +int omega_pfq_quant_plan(const OmegaPfqQuant *q, OmegaPqSpec *spec, OmegaPfqLaunch *L) { + if (!q || !spec || !L || !q->x_va || !q->q_va || !q->s_va) return OMEGA_GPU_PF_BAD_ARGS; + memset(spec, 0, sizeof *spec); memset(L, 0, sizeof *L); + spec->k = q->k; spec->in_type = OMEGA_QG_IN_F32; spec->stride = q->stride; + spec->mutant = omega_gpu_pfq_mutant() == 4 ? 2 : 0; + const uint32_t kb = q->k / 128u; + if (q->k % 128u == 0 && kb > 32u) for (spec->groups = 32u; kb % spec->groups; spec->groups--) {} /* wide row: split over ctaid.y */ + if (!omega_pq_spec_ok(spec)) return OMEGA_GPU_PF_BAD_ARGS; + if (q->rows > 0x7fffffffu) return OMEGA_GPU_PF_TOO_LARGE; + L->threads = 32u * (spec->groups ? spec->groups : kb); + L->grid_x = q->rows; L->grid_y = spec->groups ? kb / spec->groups : 1u; + L->nargs = OMEGA_PQ_ARGS_WORDS; + put64(&L->args[0], q->x_va); put64(&L->args[2], q->q_va); put64(&L->args[4], q->s_va); + return OMEGA_GPU_PF_OK; +} + +int omega_pfq_gemm_plan(const OmegaPfqGemm *c, OmegaQgSpec *spec, OmegaPfqLaunch *L) { + if (!c || !spec || !L || !c->tiles_va || !c->xq_va || !c->xs_va || !c->y_va || c->x64 > 1u) return OMEGA_GPU_PF_BAD_ARGS; + memset(spec, 0, sizeof *spec); memset(L, 0, sizeof *L); + const int m = omega_gpu_pfq_mutant(); + spec->k = c->k; spec->rt = 4u; spec->warps = 8u; spec->wait = pfq_wait(); spec->epi = OMEGA_QG_EPI_F32; + spec->direct = c->rowidx_va == 0; spec->n = c->n; spec->mutant = m == 2 || m == 3 ? m : 0; + spec->ymap = c->ymap_va != 0; spec->x64 = c->x64; + if (!omega_qg_spec_ok(spec)) return OMEGA_GPU_PF_BAD_ARGS; + if (c->ntiles > 65535u) return OMEGA_GPU_PF_TOO_LARGE; + L->threads = 32u * spec->warps; L->grid_x = c->n / omega_qg_span(spec); L->grid_y = c->ntiles; + L->shared = omega_qg_shared_bytes(spec); + const uint64_t va[5] = { c->tiles_va, c->rowidx_va, c->xq_va, c->y_va, c->xs_va }; + for (int i = 0; i < 5; i++) put64(&L->args[2 * i], va[i]); + L->args[10] = c->n; + if (spec->ymap) { put64(&L->args[12], c->ymap_va); L->nargs = OMEGA_QG_ARGS_WORDS_YMAP; } + else L->nargs = OMEGA_QG_ARGS_WORDS; + return OMEGA_GPU_PF_OK; +} + +static int gen_quant(const OmegaPfKey *key, void *prog) { OmegaPqSpec s; memcpy(&s, key->words, sizeof s); return omega_blackwell_codegen_pq_prog(&s, prog); } +static int gen_gemm(const OmegaPfKey *key, void *prog) { OmegaQgSpec s; memcpy(&s, key->words, sizeof s); return omega_blackwell_codegen_qgemm_prog(&s, prog); } + +/* cl NULL: generate and upload only (the prepare calls) */ +static int run_locked(OmegaGpuCmdList *cl, uint32_t kind, const void *spec, size_t spec_bytes, OmegaPfGen gen, const OmegaPfqLaunch *L) { + OmegaPfKey key; memset(&key, 0, sizeof key); key.kind = kind; memcpy(key.words, spec, spec_bytes); + int rc = omega_gpu_pf_open_locked(); + OmegaPfKernel kern; + if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_kernel_locked(&key, gen, &kern); + if (rc == OMEGA_GPU_PF_OK && cl && L->grid_x && L->grid_y) + rc = omega_gpu_pf_record_locked(cl, &kern, L->threads, L->grid_x, L->grid_y, L->shared, L->args, L->nargs); + return rc; +} + +int omega_gpu_pfq_quant_record_locked(OmegaGpuCmdList *cl, const OmegaPfqQuant *q) { + if (!cl) return OMEGA_GPU_PF_BAD_ARGS; + OmegaPqSpec s; OmegaPfqLaunch L; + int rc = omega_pfq_quant_plan(q, &s, &L); + if (rc != OMEGA_GPU_PF_OK || q->rows == 0) return rc; + return run_locked(cl, OMEGA_PFQ_KIND_QUANT, &s, sizeof s, gen_quant, &L); +} + +int omega_gpu_pfq_gemm_record_locked(OmegaGpuCmdList *cl, const OmegaPfqGemm *c) { + if (!cl) return OMEGA_GPU_PF_BAD_ARGS; + OmegaQgSpec s; OmegaPfqLaunch L; + int rc = omega_pfq_gemm_plan(c, &s, &L); + if (rc != OMEGA_GPU_PF_OK || c->ntiles == 0) return rc; + return run_locked(cl, OMEGA_PFQ_KIND_GEMM, &s, sizeof s, gen_gemm, &L); +} + +int omega_gpu_pfq_quant_prepare(uint32_t k, uint32_t stride) { + const OmegaPfqQuant q = { .k = k, .rows = 0, .stride = stride, .x_va = 1, .q_va = 1, .s_va = 1 }; + OmegaPqSpec s; OmegaPfqLaunch L; + int rc = omega_pfq_quant_plan(&q, &s, &L); + if (rc != OMEGA_GPU_PF_OK) return rc; + omega_gpu_session_lock(); + rc = run_locked(NULL, OMEGA_PFQ_KIND_QUANT, &s, sizeof s, gen_quant, &L); + omega_gpu_session_unlock(); + return rc; +} + +int omega_gpu_pfq_gemm_prepare(uint32_t n, uint32_t k, uint32_t x64, int indexed, int ymap) { + const OmegaPfqGemm c = { .n = n, .k = k, .x64 = x64, .ntiles = 0, .tiles_va = 1, .rowidx_va = indexed ? 1u : 0u, .xq_va = 1, + .xs_va = 1, .ymap_va = ymap ? 1u : 0u, .y_va = 1 }; + OmegaQgSpec s; OmegaPfqLaunch L; + int rc = omega_pfq_gemm_plan(&c, &s, &L); + if (rc != OMEGA_GPU_PF_OK) return rc; + omega_gpu_session_lock(); + rc = run_locked(NULL, OMEGA_PFQ_KIND_GEMM, &s, sizeof s, gen_gemm, &L); + omega_gpu_session_unlock(); + return rc; +} diff --git a/src/omega_gpu_pfq_api.h b/src/omega_gpu_pfq_api.h new file mode 100644 index 00000000..6fec418d --- /dev/null +++ b/src/omega_gpu_pfq_api.h @@ -0,0 +1,71 @@ +/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ +#ifndef OMEGA_GPU_PFQ_API_H +#define OMEGA_GPU_PFQ_API_H +/* + * omega_gpu_pfq_api: the tiled mma.sync (QMMA) FP8 path for grouped expert GEMMs in prefill (DRA-82, CUDApp #220). + * One expert GEMM is two recorded launches of the native FP8 kernels (omega_blackwell_codegen_qg.h): + * quant f32 activation rows (row stride in elements, 0 = k) -> e4m3 rows of k + f32 power-of-two scales [rows][k / 128] + * gemm grouped GEMM on QMMA over 64-row tiles, weights read IN PLACE from the FP8 GEMV tensor (no second copy) + * Rows come in the segment form of the fp8 gemm record path (OmegaPfqTile is the q36 graph's 32-byte Seg32: w_va, s_va, + * row0, cnt, pad; cnt any size). omega_pfq_tiles cuts each segment into tiles of at most OMEGA_PFQ_BM rows. + * Tile row r: activation row rowidx[row0 + r] (rowidx_va 0: row0 + r), output y row ymap[row0 + r] (ymap_va 0: row0 + r), + * f32 rows of n. The gemm reads rowidx for every row of a tile, cnt or not: a row map needs OMEGA_PFQ_MAP_PAD entries + * past its last row, each naming a row the quantiser wrote (0 does). ymap is read only below cnt. + * x64 1: the activations carry an extra 2^64 (the q36 graph's x' and act rows) and y is the true product. Exact: the + * quantiser's scales are powers of two, so q(2^64 x) = q(x) and s(2^64 x) = 2^64 s(x) unless amax(x) < 2^-164. + * Limit (x64): the fold sa * sw overflows when amax(x) * S >= 2^16 (S the checkpoint weight scale). + * Arithmetic: the qg header's (QMMA chunks accumulated in f32, an exact power-of-two scale fold). NOT bit-identical to the + * FP8 GEMV / fp8gemm FFMA path (activations quantised to e4m3, another summation order): a consumer is gated on output + * tolerance, not bit-identity (q36: AIEN_OMEGA_Q36_PF_QMMA=1, off by default). + * Kernel cache kinds (omega_gpu_prefill.h OmegaPfKey): OMEGA_PFQ_KIND_QUANT, OMEGA_PFQ_KIND_GEMM. + * Shape: 4 row tiles of 16, 8 warps; QMMA read distance AIEN_OMEGA_PFQ_WAIT (0 .. 120, default 45). + * AIEN_OMEGA_PFQ_MUTANT (tests and chip gates only; read once, omega_gpu_pfq_test_set_mutant overrides): + * 1 the tiler starts tile p >= 1 of a segment one row early, 2 the gemm never advances the weight scale pointer (the + * wrong-scale-block mutant), 3 gemm row tile i reads tile i^1, 4 the quantiser uses c = 0. + * Locking: the *_record_locked calls run with the session lock held (as omega_gpu_fp8gemm_record); prepare takes it. + */ +#include +#include "omega_blackwell_codegen_qg.h" + +#ifdef __cplusplus +extern "C" { +#endif +#define OMEGA_PFQ_BM 64u +#define OMEGA_PFQ_MAP_PAD 64u +#define OMEGA_PFQ_KIND_QUANT 0x60u +#define OMEGA_PFQ_KIND_GEMM 0x61u +typedef struct { uint64_t w_va, s_va; uint32_t row0, cnt; uint64_t pad; } OmegaPfqTile; + +/* tiles needed for rows spread over segs segments: segs + rows / OMEGA_PFQ_BM (an upper bound) */ +uint32_t omega_pfq_tile_cap(uint32_t rows, uint32_t segs); +/* cuts segs[0 .. nsegs) into tiles; returns the tile count, 0 when cap is too small or an argument is NULL + * (a table of empty segments also returns 0: nothing to launch) */ +uint32_t omega_pfq_tiles(const OmegaPfqTile *segs, uint32_t nsegs, OmegaPfqTile *tiles, uint32_t cap); + +typedef struct { uint32_t k, rows, stride; uint64_t x_va, q_va, s_va; } OmegaPfqQuant; +typedef struct { + uint32_t n, k, x64, ntiles; + uint64_t tiles_va, rowidx_va, xq_va, xs_va, ymap_va, y_va; +} OmegaPfqGemm; +/* the launch a record call makes (host-checkable: tests run these programs, arguments and grids in the warp simulator) */ +typedef struct { + uint32_t threads, grid_x, grid_y, shared, nargs; + uint32_t args[OMEGA_QG_ARGS_WORDS_YMAP]; +} OmegaPfqLaunch; +/* OMEGA_GPU_PF_OK, or BAD_ARGS / TOO_LARGE (omega_gpu_prefill.h); rows 0 / ntiles 0 plan nothing (grid 0) */ +int omega_pfq_quant_plan(const OmegaPfqQuant *q, OmegaPqSpec *spec, OmegaPfqLaunch *L); +int omega_pfq_gemm_plan(const OmegaPfqGemm *c, OmegaQgSpec *spec, OmegaPfqLaunch *L); + +struct OmegaGpuCmdList; +int omega_gpu_pfq_quant_record_locked(struct OmegaGpuCmdList *cl, const OmegaPfqQuant *q); +int omega_gpu_pfq_gemm_record_locked(struct OmegaGpuCmdList *cl, const OmegaPfqGemm *c); +/* generates and uploads the kernels a later record of this shape uses (a record never has to generate code) */ +int omega_gpu_pfq_quant_prepare(uint32_t k, uint32_t stride); +int omega_gpu_pfq_gemm_prepare(uint32_t n, uint32_t k, uint32_t x64, int indexed, int ymap); + +int omega_gpu_pfq_mutant(void); +void omega_gpu_pfq_test_set_mutant(int m); +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/omega_gpu_q36_graph.c b/src/omega_gpu_q36_graph.c index 31736829..760a3777 100644 --- a/src/omega_gpu_q36_graph.c +++ b/src/omega_gpu_q36_graph.c @@ -13,6 +13,7 @@ #include "omega_gpu_fmt.h" #include "omega_gpu_mem_cached.h" #include "omega_gpu_fp8gemm_api.h" +#include "omega_gpu_pfq_api.h" #include "omega_gpu_q36_native.h" #include "omega_gpu_cmdlist_native.h" #include @@ -99,9 +100,14 @@ struct OmegaGpuQ36Graph { /* the prefill (L369): one conversation's prompt chunk as rows through the batch's GEMMs; GDN and attention per token in order */ struct { uint32_t cap; OmegaGpuMem hb, db; size_t x, rlb, sgu, sdn, sgu2, sdn2, xmap, ymap, xmap2, ymap2; size_t xq, xp, mixN, abN, aggN, attN, op, oN, xp2, guN, actN, actp, dnN; int mutant; - uint64_t streamed; /* fp8 GEMM weight bytes every prefill call so far streamed (one pass per mt rows) */ + uint64_t streamed; /* fp8 GEMM weight bytes every prefill call so far streamed (legacy: one pass per mt rows; qmma: once per chunk) */ + uint64_t passes; /* the same weights per GEMM pass as launched */ int attn_multi; /* 1 (default): each attention layer in one prep + attend launch pair; AIEN_OMEGA_Q36_PF_ATTN=0: one decode step per token */ - int gdn_multi; /* 1 (default): the GDN layers in one multi-token launch; AIEN_OMEGA_Q36_PF_GDN=0: one decode step per token */ } pf; + int gdn_multi; /* 1 (default): the GDN layers in one multi-token launch; AIEN_OMEGA_Q36_PF_GDN=0: one decode step per token */ + /* DRA-82, AIEN_OMEGA_Q36_PF_QMMA=1 (default 0): the prefill GEMMs on the tensor cores (omega_gpu_pfq_api.h): pq quantises + * natural rows into q8|qs, qg reads the weights in place through 64-row tiles; qxm|qym the combined routed+shared + * row maps, qtgu|qtdn their tile tables, qtd the dense mix|o tables of every layer (qtd_cap tiles each) */ + int qmma; size_t q8, qs, qxm, qym, qtgu, qtdn, qtd; uint32_t qtd_cap, qtd_n, qtg_cap, qtg_n; } pf; int stamps; uint32_t n_kind, tagged_in_list, n_stamp; uint8_t kind[OMEGA_GPU_CMDLIST_MAX_LAUNCHES]; @@ -954,18 +960,41 @@ const char *omega_gpu_q36_graph_kind_name(uint8_t kind) { * Every arithmetic step of a prompt token is the decode step's: the dense trunk and the experts through the fp8 gemm record * path (bit-identical to the GEMV per row), a|b through the bf16 ROWS form, the router through its ROWS form, and the GDN step * and the attention launched once per token, in token order, on the conversation's own state at pos0 + t. The expert rows are - * grouped on the host between two lists per layer (the GROUP kernel holds 128 rows; a chunk holds up to 128 x 8). */ + * grouped on the host between two lists per layer (a legacy chunk holds up to 128 x 8 rows, the fp8 gemm's 1024; the + * tensor-core chunk up to OMEGA_GPU_Q36_PREFILL_TC_MAX_ROWS x 8). */ #define PF_PAD 8u #define PF_EXPERT_BYTES ((uint64_t)3u * EI * H) /* one expert's e4m3 gate|up (2 x EI x H) and down (H x EI) */ +#define PF_GRID_Y_MAX 65535u /* rows per row-launch (grid y); the tensor-core chunk's expert rows (up to 8192 x 9) are split */ static int pf_fail(OmegaGpuQ36Graph *g, const char *what) { return fail(g, what, OMEGA_GPU_DGRAPH_BAD_ARGS); } -int omega_gpu_q36_graph_prefill_alloc(OmegaGpuQ36Graph *g, uint32_t cap) { - if (!g || g->in_token || cap == 0 || cap > OMEGA_GPU_Q36_PREFILL_MAX_ROWS) return OMEGA_GPU_DGRAPH_BAD_ARGS; - if (g->pf.cap >= cap) return OMEGA_GPU_DGRAPH_OK; - const size_t P = cap, R = P * NG, lbs = g->lo.lb_stride; - size_t ho = 0, dof = 0; -#define HREG(f, b) do { g->pf.f = ho; ho += al(b); } while (0) -#define DREG(f, b) do { g->pf.f = dof; dof += al(b); } while (0) +static int pf_qmma_env(void) { const char *e = getenv("AIEN_OMEGA_Q36_PF_QMMA"); return e && e[0] == '1' && e[1] == 0; } +uint32_t omega_gpu_q36_graph_prefill_rows_max(void) { return pf_qmma_env() ? OMEGA_GPU_Q36_PREFILL_TC_MAX_ROWS : OMEGA_GPU_Q36_PREFILL_MAX_ROWS; } +int omega_gpu_q36_graph_prefill_chunk(uint32_t *chunk) { + if (!chunk) return OMEGA_GPU_DGRAPH_BAD_ARGS; + const char *e = getenv("AIEN_OMEGA_Q36_PF_CHUNK"); + if (!e) { *chunk = OMEGA_GPU_Q36_PREFILL_MAX_ROWS; return OMEGA_GPU_DGRAPH_OK; } + const uint32_t max = omega_gpu_q36_graph_prefill_rows_max(); + uint32_t v = 0; + if (!*e) return OMEGA_GPU_DGRAPH_BAD_ARGS; + for (const char *p = e; *p; p++) { + if (*p < '0' || *p > '9') return OMEGA_GPU_DGRAPH_BAD_ARGS; + v = v * 10u + (uint32_t)(*p - '0'); + if (v > max) return OMEGA_GPU_DGRAPH_BAD_ARGS; + } + if (v == 0) return OMEGA_GPU_DGRAPH_BAD_ARGS; + *chunk = v; + return OMEGA_GPU_DGRAPH_OK; +} + +/* The prefill regions for a chunk of P tokens (R = P x 9 expert rows): the host buffer (ho bytes) and the device buffer (dof + * bytes); with qmma (DRA-82) also the quantised operand, the QMMA row maps and the tile tables (tcg routed+shared tiles, + * tcd dense tiles for each of `layers` x 2). Region offsets go to g->pf when g is not NULL. */ +static void pf_layout(OmegaGpuQ36Graph *g, size_t P, size_t lbs, int qmma, uint32_t layers, uint32_t tcg, uint32_t tcd, + size_t *host, size_t *dev) { + const size_t R = P * NG; + size_t ho = 0, dof = 0, sink = 0; +#define HREG(f, b) do { *(g ? &g->pf.f : &sink) = ho; ho += al(b); } while (0) +#define DREG(f, b) do { *(g ? &g->pf.f : &sink) = dof; dof += al(b); } while (0) HREG(x, P * H * 4); HREG(rlb, P * lbs); HREG(sgu, 256u * OMEGA_GEMM_SEG_BYTES); HREG(sdn, 256u * OMEGA_GEMM_SEG_BYTES); HREG(sgu2, OMEGA_GEMM_SEG_BYTES); HREG(sdn2, OMEGA_GEMM_SEG_BYTES); HREG(xmap, (R + PF_PAD) * 4); HREG(ymap, (R + PF_PAD) * 4); HREG(xmap2, (P + PF_PAD) * 4); HREG(ymap2, (P + PF_PAD) * 4); @@ -973,8 +1002,48 @@ int omega_gpu_q36_graph_prefill_alloc(OmegaGpuQ36Graph *g, uint32_t cap) { 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); DREG(oN, P * H * 4); DREG(xp2, (P + PF_PAD) * H * 4); DREG(guN, R * 2 * EI * 4); DREG(actN, R * EI * 4); DREG(actp, (R + PF_PAD) * EI * 4); DREG(dnN, R * H * 4); + if (qmma) { /* q8|qs hold the widest quantised operand: o's input (P x 4096) or the act rows (R x 512) */ + const size_t qk = P * OMEGA_GPU_Q36_MIXER_OUT > R * EI ? P * OMEGA_GPU_Q36_MIXER_OUT : R * EI; + HREG(qxm, (R + OMEGA_PFQ_MAP_PAD) * 4); HREG(qym, (R + OMEGA_PFQ_MAP_PAD) * 4); + HREG(qtgu, tcg * sizeof(OmegaPfqTile)); HREG(qtdn, tcg * sizeof(OmegaPfqTile)); + HREG(qtd, (size_t)layers * 2u * tcd * sizeof(OmegaPfqTile)); + DREG(q8, qk); DREG(qs, qk / 128u * 4u); + } #undef HREG #undef DREG + (void)sink; + *host = ho; *dev = dof; +} +int omega_gpu_q36_graph_prefill_bytes(uint32_t cap, uint32_t lb_stride, uint32_t layers, uint64_t *host_bytes, uint64_t *dev_bytes) { + const int qmma = pf_qmma_env(); + if (cap == 0 || cap > omega_gpu_q36_graph_prefill_rows_max() || !host_bytes || !dev_bytes) return OMEGA_GPU_DGRAPH_BAD_ARGS; + size_t ho = 0, dof = 0; + const uint32_t tcg = qmma ? omega_pfq_tile_cap(cap * NG, 257u) : 0, tcd = qmma ? omega_pfq_tile_cap(cap, 1u) : 0; + pf_layout(NULL, cap, lb_stride, qmma, layers, tcg, tcd, &ho, &dof); + *host_bytes = ho; *dev_bytes = dof; + return OMEGA_GPU_DGRAPH_OK; +} + +int omega_gpu_q36_graph_prefill_alloc(OmegaGpuQ36Graph *g, uint32_t cap) { + if (!g || g->in_token || cap == 0) return OMEGA_GPU_DGRAPH_BAD_ARGS; + const int qmma = pf_qmma_env(); + if (cap > (qmma ? OMEGA_GPU_Q36_PREFILL_TC_MAX_ROWS : OMEGA_GPU_Q36_PREFILL_MAX_ROWS)) { omega_gpu_session_set_error("q36 prefill_alloc: cap above the row bound"); return OMEGA_GPU_DGRAPH_BAD_ARGS; } + const char *gm = getenv("AIEN_OMEGA_Q36_PF_GDN"), *am = getenv("AIEN_OMEGA_Q36_PF_ATTN"), *m = getenv("AIEN_OMEGA_Q36_PF_MUTANT"); + const int gdn_multi = !(gm && gm[0] == '0' && gm[1] == 0), attn_multi = !(am && am[0] == '0' && am[1] == 0), mutant = m ? atoi(m) : 0; + /* above the legacy bound only the one-launch-per-layer forms fit a command list (the per-token GDN, attention and + * COMBINE|NORM launches are n per layer), and the native MoE window plan holds 128 tokens */ + if (cap > OMEGA_GPU_Q36_PREFILL_MAX_ROWS && (g->native || !gdn_multi || !attn_multi || mutant == 1 || mutant == 3 || !q36_cn_rows())) + { omega_gpu_session_set_error("q36 prefill_alloc: a chunk above 128 needs the multi-token GDN/attention/COMBINE forms, no native MoE"); + return OMEGA_GPU_DGRAPH_BAD_ARGS; } + if (g->pf.cap >= cap && g->pf.qmma == qmma) return OMEGA_GPU_DGRAPH_OK; + /* a gate change reallocates at the larger capacity when the new gate's row bound still holds it */ + if (g->pf.cap > cap && g->pf.cap <= (qmma ? OMEGA_GPU_Q36_PREFILL_TC_MAX_ROWS : OMEGA_GPU_Q36_PREFILL_MAX_ROWS)) cap = g->pf.cap; + const size_t lbs = g->lo.lb_stride; + /* QMMA bypasses the native-artifact GEMM records (they stay the incumbent GEMV's); the quantiser reads x' rows lbs apart */ + if (qmma && (g->native || (lbs & 15u))) return OMEGA_GPU_DGRAPH_BAD_ARGS; + const uint32_t tcg = qmma ? omega_pfq_tile_cap(cap * NG, 257u) : 0, tcd = qmma ? omega_pfq_tile_cap(cap, 1u) : 0; + size_t ho = 0, dof = 0; + pf_layout(g, cap, lbs, qmma, g->cfg.layers, tcg, tcd, &ho, &dof); omega_gpu_session_lock(); if (g->pf.hb.cpu) omega_gpu_session_free(&g->pf.hb); if (g->pf.db.cpu) omega_gpu_session_free(&g->pf.db); @@ -985,7 +1054,13 @@ int omega_gpu_q36_graph_prefill_alloc(OmegaGpuQ36Graph *g, uint32_t cap) { : OMEGA_GPU_DGRAPH_OK; if (rc == OMEGA_GPU_DGRAPH_OK) memset(g->pf.hb.cpu, 0, ho); if (rc == OMEGA_GPU_DGRAPH_OK && !(g->pf_generation = q36_owner_next())) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; + if (rc != OMEGA_GPU_DGRAPH_OK) { /* a refused allocation leaves nothing behind (omega_gpu_q36_graph_prefill_bytes) */ + if (g->pf.hb.cpu) omega_gpu_session_free(&g->pf.hb); + if (g->pf.db.cpu) omega_gpu_session_free(&g->pf.db); + memset(&g->pf.hb, 0, sizeof g->pf.hb); memset(&g->pf.db, 0, sizeof g->pf.db); + } omega_gpu_session_unlock(); + if (rc != OMEGA_GPU_DGRAPH_OK) { omega_gpu_session_set_error("q36 prefill_alloc: the session refused the prefill buffers"); return rc; } /* pin the shapes the prefill records (a kernel cache miss while recording is refused): the dense trunk without maps (8 rows * per pass), the expert blocks with the row maps at 4 rows per pass, the router's ROWS kernel */ if (rc == OMEGA_GPU_DGRAPH_OK) { @@ -994,30 +1069,101 @@ int omega_gpu_q36_graph_prefill_alloc(OmegaGpuQ36Graph *g, uint32_t cap) { if (r == 0) r = omega_gpu_fp8gemm_prepare_shape(H, 64, 4, 1); if (r == 0) r = omega_gpu_fp8gemm_prepare_shape(EI, 64, 4, 1); if (r == 0 && omega_gpu_router_prepare_rows(g->cfg.router, H * 4u, g->lo.lb_stride) != OMEGA_GPU_ROUTER_OK) r = -1; + if (r == 0 && qmma) { /* the pq/qg programs of every prefill shape, x64 1 (the q36 activations carry 2^64) */ + r = omega_gpu_pfq_quant_prepare(H, 0); + if (r == 0) r = omega_gpu_pfq_quant_prepare(H, (uint32_t)(lbs / 4u)); + if (r == 0) r = omega_gpu_pfq_quant_prepare(OMEGA_GPU_Q36_MIXER_OUT, 0); + if (r == 0) r = omega_gpu_pfq_quant_prepare(EI, 0); + if (r == 0) r = omega_gpu_pfq_gemm_prepare(OMEGA_GPU_Q36_GDN_MIX, H, 1, 0, 0); + if (r == 0) r = omega_gpu_pfq_gemm_prepare(OMEGA_GPU_Q36_ATT_MIX, H, 1, 0, 0); + if (r == 0) r = omega_gpu_pfq_gemm_prepare(H, OMEGA_GPU_Q36_MIXER_OUT, 1, 0, 0); + if (r == 0) r = omega_gpu_pfq_gemm_prepare(2u * EI, H, 1, 1, 1); + if (r == 0) r = omega_gpu_pfq_gemm_prepare(H, EI, 1, 1, 1); + } if (r != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; } if (rc != OMEGA_GPU_DGRAPH_OK) return rc; - const char *m = getenv("AIEN_OMEGA_Q36_PF_MUTANT"); - g->pf.mutant = m ? atoi(m) : 0; - const char *gm = getenv("AIEN_OMEGA_Q36_PF_GDN"); - g->pf.gdn_multi = !(gm && gm[0] == '0' && gm[1] == 0); + g->pf.mutant = mutant; + g->pf.qmma = qmma; + g->pf.gdn_multi = gdn_multi; for (uint32_t h = 0; h < g->heads && g->pf.gdn_multi; h++) if (g->gdn_h[h] && omega_gpu_gdn_multi_prepare(g->gdn_h[h]) != OMEGA_GPU_GDN_OK) return OMEGA_GPU_DGRAPH_CHIP_FAIL; - const char *am = getenv("AIEN_OMEGA_Q36_PF_ATTN"); - g->pf.attn_multi = !(am && am[0] == '0' && am[1] == 0); + g->pf.attn_multi = attn_multi; for (uint32_t h = 0; h < g->heads && g->pf.attn_multi; h++) if (g->attn_h[h] && omega_gpu_decode_attn256_multi_prepare(g->attn_h[h]) != OMEGA_GPU_DATTN_OK) return OMEGA_GPU_DGRAPH_CHIP_FAIL; omega_gpu_decode_attn256_test_set_multi_mutant(g->pf.mutant == 6 ? 1 : 0); /* TEST mutant 6: the attention prep launch dropped */ + g->pf.qmma = qmma; g->pf.qtd_cap = tcd; g->pf.qtg_cap = tcg; g->pf.qtd_n = 0; g->pf.qtg_n = 0; g->pf.cap = cap; return OMEGA_GPU_DGRAPH_OK; } uint32_t omega_gpu_q36_graph_prefill_cap(const OmegaGpuQ36Graph *g) { return g ? g->pf.cap : 0; } uint64_t omega_gpu_q36_graph_prefill_streamed(const OmegaGpuQ36Graph *g) { return g ? g->pf.streamed : 0; } +uint64_t omega_gpu_q36_graph_prefill_passes_streamed(const OmegaGpuQ36Graph *g) { return g ? g->pf.passes : 0; } + +/* DRA-82: o runs on the tensor cores only where the mixer leaves natural attN rows (the attention layers and the multi-token + * GDN launch); the per-token GDN steps (AIEN_OMEGA_Q36_PF_GDN=0, pf mutants 1|3) write the permuted op rows only */ +static int pf_qmma_o(const OmegaGpuQ36Graph *g, uint32_t l) { + return g->pf.qmma && (g->layer[l].attn || (g->pf.gdn_multi && g->pf.mutant != 1 && g->pf.mutant != 3)); +} + +/* The weight count of one expert GEMM pair over cnt rows. passes: as launched, one weight pass per 4 GEMV rows or per 64-row + * QMMA tile (DRA-82). streamed (DRA-83 G2): once per chunk on the tensor-core path, else the passes. The dense mix/o of a + * layer the same way (8 GEMV rows per pass; o counts as QMMA only where it runs there, pf_qmma_o). */ +static void pf_count_expert(OmegaGpuQ36Graph *g, uint32_t cnt) { + if (!cnt) return; + const uint32_t ep = g->pf.qmma ? OMEGA_PFQ_BM : 4u; + const uint64_t passes = (uint64_t)((cnt + ep - 1u) / ep) * PF_EXPERT_BYTES; + g->pf.passes += passes; + g->pf.streamed += g->pf.qmma ? PF_EXPERT_BYTES : passes; +} +static void pf_count_dense(OmegaGpuQ36Graph *g, uint32_t l, uint32_t n) { + const uint64_t mix = (uint64_t)H * (g->layer[l].attn ? OMEGA_GPU_Q36_ATT_MIX : OMEGA_GPU_Q36_GDN_MIX), o = (uint64_t)H * OMEGA_GPU_Q36_MIXER_OUT; + const uint32_t mp = g->pf.qmma ? OMEGA_PFQ_BM : 8u, opp = pf_qmma_o(g, l) ? OMEGA_PFQ_BM : 8u; + const uint64_t pm = (uint64_t)((n + mp - 1u) / mp) * mix, po = (uint64_t)((n + opp - 1u) / opp) * o; + g->pf.passes += pm + po; + g->pf.streamed += g->pf.qmma ? mix + (pf_qmma_o(g, l) ? o : po) : pm + po; +} +/* TEST mutant 7 (DRA-83 G2): the chunk at pos0 0, layer 0, loses its highest-id routed expert segment */ +static int pf_skip_segment(const OmegaGpuQ36Graph *g, uint32_t l, uint32_t pos0) { return g->pf.mutant == 7 && pos0 == 0 && l == 0; } static uint64_t pf_h(const OmegaGpuQ36Graph *g, size_t off) { return g->pf.hb.va + off; } static void *pf_hc(const OmegaGpuQ36Graph *g, size_t off) { return (uint8_t *)g->pf.hb.cpu + off; } static uint64_t pf_d(const OmegaGpuQ36Graph *g, size_t off) { return g->pf.db.va + off; } +/* the dense mix|o tile tables of every layer for an n-row chunk (one 64-row tile per pass, weight entry 0, rows 0 .. n-1) */ +static int pf_qmma_dense_tables(OmegaGpuQ36Graph *g, uint32_t n) { + OmegaPfqTile *t = pf_hc(g, g->pf.qtd); + uint32_t nt = 0; + for (uint32_t l = 0; l < g->cfg.layers; l++) + for (uint32_t w = 0; w < 2u; w++) { + OmegaPfqTile seg; memset(&seg, 0, sizeof seg); + (void)omega_gpu_fp8_tensor_entry(w ? g->layer[l].o : g->layer[l].mix, 0, &seg.w_va, &seg.s_va); + seg.cnt = n; + nt = omega_pfq_tiles(&seg, 1, t + ((size_t)l * 2u + w) * g->pf.qtd_cap, g->pf.qtd_cap); + if (nt == 0) return pf_fail(g, "q36 prefill qmma: dense tile table over capacity"); + } + g->pf.qtd_n = nt; + return OMEGA_GPU_DGRAPH_OK; +} + +/* quantise rows x (k wide, row stride `stride` floats, 0 = k; carrying 2^64) into q8|qs, then y = x W^T on QMMA through the + * tile table at tiles_va; rowidx/ymap 0 = direct rows. Both launches carry the GEMM's kind. */ +static int pf_qmma_gemm(OmegaGpuQ36Graph *g, uint8_t kind, uint64_t x, uint32_t rows, uint32_t stride, uint32_t k, uint32_t nout, + uint64_t tiles_va, uint32_t ntiles, uint64_t rowidx, uint64_t ymap, uint64_t y) { + const uint64_t q8 = pf_d(g, g->pf.q8), qs = pf_d(g, g->pf.qs); + const OmegaPfqQuant q = { .k = k, .rows = rows, .stride = stride, .x_va = x, .q_va = q8, .s_va = qs }; + int rc = after_op(g, kind, omega_gpu_pfq_quant_record_locked(g->cl, &q)); + const OmegaPfqGemm c = { .n = nout, .k = k, .x64 = 1, .ntiles = ntiles, .tiles_va = tiles_va, .rowidx_va = rowidx, + .xq_va = q8, .xs_va = qs, .ymap_va = ymap, .y_va = y }; + if (rc == OMEGA_GPU_DGRAPH_OK) rc = after_op(g, kind, omega_gpu_pfq_gemm_record_locked(g->cl, &c)); + return rc; +} + +static int pf_qmma_dense(OmegaGpuQ36Graph *g, uint8_t kind, uint32_t l, uint32_t w, uint32_t n, uint64_t x, uint32_t k, uint32_t nout, uint64_t y) { + const uint64_t tiles = pf_h(g, g->pf.qtd) + ((uint64_t)l * 2u + w) * g->pf.qtd_cap * sizeof(OmegaPfqTile); + return pf_qmma_gemm(g, kind, x, n, 0, k, nout, tiles, g->pf.qtd_n, 0, 0, y); +} + /* part (a) of layer l: the mixer of every row and the router; ends with the router outputs in pf.rlb */ 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, OmegaGpuRouterDevOut *first_router) { @@ -1028,13 +1174,16 @@ static int pf_record_mixer(OmegaGpuQ36Graph *g, uint32_t head, uint32_t l, uint3 const uint64_t pt2048 = va_of(g, g->lo.ptab), pt4096 = pt2048 + (uint64_t)H * 4u; const uint64_t in_norm = va_of(g, g->lo.norms) + (uint64_t)l * H * 4u, segd = va_of(g, g->lo.segd) + (uint64_t)l * SEGD_ROWS * OMEGA_GEMM_SEG_BYTES; const uint64_t XS = (uint64_t)H * 4u, MOS = (uint64_t)OMEGA_GPU_Q36_MIXER_OUT * 4u; + const int qmix = g->pf.qmma, qo = pf_qmma_o(g, l); /* DRA-82: the QMMA GEMMs read the natural rows (no PERM) */ int rc = OMEGA_GPU_DGRAPH_OK; /* layer 0 normalizes the embeddings; after that the previous layer's per-token COMBINE|NORM left layer l's x' in xq */ if (l == 0) rc = rec_res_rows(g, K_NORM_R, x0, 0, in_norm, xq0, SCALE_2P64, n); - if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_XQ, xq0, pt2048, xp, H, n); + if (rc == OMEGA_GPU_DGRAPH_OK && !qmix) rc = rec_perm(g, K_PERM_XQ, xq0, pt2048, xp, H, n); if (!g->layer[l].attn) { 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)); - if (rc == OMEGA_GPU_DGRAPH_OK) rc = native_record_gemm(g, OMEGA_GPU_Q36_K_MIX, g->layer[l].mix, n, 1, segd, xp, mixN, 0, 0, 0); + if (rc == OMEGA_GPU_DGRAPH_OK) + rc = qmix ? pf_qmma_dense(g, OMEGA_GPU_Q36_K_MIX, l, 0, n, xq0, H, OMEGA_GPU_Q36_GDN_MIX, mixN) + : native_record_gemm(g, OMEGA_GPU_Q36_K_MIX, g->layer[l].mix, n, 1, segd, xp, mixN, 0, 0, 0); if (g->pf.gdn_multi && g->pf.mutant != 1 && g->pf.mutant != 3) { /* every token's recurrence step in one launch (the decode step T times, bit for bit: omega_gpu_gdn_api.h), the rows * into attN (unused on a GDN layer), then one PERM over the rows. TEST mutant 5: every token reads token 0's a|b. */ @@ -1042,7 +1191,7 @@ static int pf_record_mixer(OmegaGpuQ36Graph *g, uint32_t head, uint32_t l, uint3 rc = after_op(g, OMEGA_GPU_Q36_K_GDN, omega_gpu_gdn_multi_record(g->cl, g->gdn_h[head], ki, n, mixN, mixN + OMEGA_GPU_Q36_GDN_QKV * 4u, OMEGA_GPU_Q36_GDN_MIX * 4u, abN, g->pf.mutant == 5 ? 0u : OMEGA_GPU_Q36_AB * 4u, attN, (uint32_t)MOS, g->cfg.eps_l2, g->cfg.eps, 1.0f, SCALE_2P64)); - if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_O, attN, pt4096, op, OMEGA_GPU_Q36_MIXER_OUT, n); + if (rc == OMEGA_GPU_DGRAPH_OK && !qo) rc = rec_perm(g, K_PERM_O, attN, pt4096, op, OMEGA_GPU_Q36_MIXER_OUT, n); } else for (uint32_t i = 0; i < n && rc == OMEGA_GPU_DGRAPH_OK; i++) { /* TEST mutant 1: the last two tokens' recurrence steps in swapped order */ @@ -1057,7 +1206,9 @@ static int pf_record_mixer(OmegaGpuQ36Graph *g, uint32_t head, uint32_t l, uint3 if (rc == OMEGA_GPU_DGRAPH_OK && g->pf.mutant == 3 && t > 0) rc = rec_perm(g, K_PERM_O, mo, pt4096, op + (t - 1) * MOS, OMEGA_GPU_Q36_MIXER_OUT, 1); } } else { - if (rc == OMEGA_GPU_DGRAPH_OK) rc = native_record_gemm(g, OMEGA_GPU_Q36_K_MIX, g->layer[l].mix, n, 1, segd, xp, mixN, 0, 0, 0); + if (rc == OMEGA_GPU_DGRAPH_OK) + rc = qmix ? pf_qmma_dense(g, OMEGA_GPU_Q36_K_MIX, l, 0, n, xq0, H, OMEGA_GPU_Q36_ATT_MIX, mixN) + : native_record_gemm(g, OMEGA_GPU_Q36_K_MIX, g->layer[l].mix, n, 1, segd, xp, mixN, 0, 0, 0); uint32_t w[OMEGA_DEC_GA_WORDS]; memset(w, 0, sizeof w); put64(w, OMEGA_DEC_GA_IN, mixN); put64(w, OMEGA_DEC_GA_TAB, va_of(g, g->lo.gtab)); put64(w, OMEGA_DEC_GA_OUT, aggN); if (rc == OMEGA_GPU_DGRAPH_OK) @@ -1084,9 +1235,11 @@ static int pf_record_mixer(OmegaGpuQ36Graph *g, uint32_t head, uint32_t l, uint3 rc = add_glue(g, OMEGA_GPU_Q36_K_GATE, g->mutant == 3 ? K_GATE_M1 : K_GATE, OMEGA_GPU_Q36_MIXER_OUT / OMEGA_DEC_THREADS, 1, OMEGA_GPU_Q36_MIXER_OUT, w2, OMEGA_DEC_GT_WORDS); } - if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_O, attN, pt4096, op, OMEGA_GPU_Q36_MIXER_OUT, n); + if (rc == OMEGA_GPU_DGRAPH_OK && !qo) rc = rec_perm(g, K_PERM_O, attN, pt4096, op, OMEGA_GPU_Q36_MIXER_OUT, n); } - if (rc == OMEGA_GPU_DGRAPH_OK) rc = native_record_gemm(g, OMEGA_GPU_Q36_K_O, g->layer[l].o, n, 1, segd + OMEGA_GEMM_SEG_BYTES, op, oN, 0, 0, 0); + if (rc == OMEGA_GPU_DGRAPH_OK) + rc = qo ? pf_qmma_dense(g, OMEGA_GPU_Q36_K_O, l, 1, n, attN, OMEGA_GPU_Q36_MIXER_OUT, H, oN) + : native_record_gemm(g, OMEGA_GPU_Q36_K_O, g->layer[l].o, n, 1, segd + OMEGA_GEMM_SEG_BYTES, op, oN, 0, 0, 0); if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_res_rows(g, K_ADD_R, x0, oN, 0, 0, 1.0f, n); for (uint32_t r0 = 0; r0 < n && rc == OMEGA_GPU_DGRAPH_OK; r0 += OMEGA_GPU_ROUTER_MAX_ROWS) { const uint32_t rows = n - r0 < OMEGA_GPU_ROUTER_MAX_ROWS ? n - r0 : OMEGA_GPU_ROUTER_MAX_ROWS; @@ -1103,8 +1256,10 @@ static int pf_record_mixer(OmegaGpuQ36Graph *g, uint32_t head, uint32_t l, uint3 } /* the host GROUP: the n * 8 routed picks sorted by expert id (then token, then slot) into segments and row maps, the shared - * expert as its own one-segment table (x' row t, y row t * 9 + 8). Returns the routed segment count. */ -static uint32_t pf_group(OmegaGpuQ36Graph *g, uint32_t l, uint32_t n, uint32_t ids_off) { + * expert as its own one-segment table (x' row t, y row t * 9 + 8). Returns the routed segment count. With QMMA (DRA-82) the + * same segments, the shared one appended at rows n * 8 .., are cut into 64-row tiles over one combined row map. skip (TEST + * mutant 7): the highest-id routed expert keeps its map rows but gets no segment (its rows are never computed, nor counted). */ +static uint32_t pf_group(OmegaGpuQ36Graph *g, uint32_t l, uint32_t n, uint32_t ids_off, int skip) { const uint32_t S = NG - 1u; uint32_t cnt[256], start[256]; memset(cnt, 0, sizeof cnt); @@ -1113,16 +1268,18 @@ static uint32_t pf_group(OmegaGpuQ36Graph *g, uint32_t l, uint32_t n, uint32_t i for (uint32_t s = 0; s < S; s++) cnt[id[s] & 255u]++; } Seg32 *sg = pf_hc(g, g->pf.sgu), *sd = pf_hc(g, g->pf.sdn); - uint32_t segs = 0, row = 0; + uint32_t segs = 0, row = 0, top = 0; + for (uint32_t e = 0; e < 256u; e++) if (cnt[e]) top = e; for (uint32_t e = 0; e < 256u; e++) { start[e] = row; if (!cnt[e]) continue; + if (skip && e == top) { row += cnt[e]; continue; } memset(&sg[segs], 0, sizeof *sg); memset(&sd[segs], 0, sizeof *sd); (void)omega_gpu_fp8_tensor_entry(g->layer[l].gu, e, &sg[segs].w_va, &sg[segs].s_va); (void)omega_gpu_fp8_tensor_entry(g->layer[l].dn, e, &sd[segs].w_va, &sd[segs].s_va); sg[segs].row0 = sd[segs].row0 = row; sg[segs].cnt = sd[segs].cnt = cnt[e]; if (g->pf.mutant == 2 && cnt[e] > 1u) { sg[segs].cnt--; sd[segs].cnt--; } /* TEST mutant 2: every segment one row short */ - g->pf.streamed += (uint64_t)((cnt[e] + 3u) / 4u) * PF_EXPERT_BYTES; /* gate|up and down, 4 rows per pass */ + pf_count_expert(g, cnt[e]); /* gate|up and down */ row += cnt[e]; segs++; } uint32_t *xm = pf_hc(g, g->pf.xmap), *ym = pf_hc(g, g->pf.ymap); @@ -1136,10 +1293,22 @@ static uint32_t pf_group(OmegaGpuQ36Graph *g, uint32_t l, uint32_t n, uint32_t i (void)omega_gpu_fp8_tensor_entry(g->layer[l].gu, 256u, &g2->w_va, &g2->s_va); /* expert 256 = the shared expert */ (void)omega_gpu_fp8_tensor_entry(g->layer[l].dn, 256u, &d2->w_va, &d2->s_va); g2->cnt = d2->cnt = n; - g->pf.streamed += (uint64_t)((n + 3u) / 4u) * PF_EXPERT_BYTES; + pf_count_expert(g, n); uint32_t *xm2 = pf_hc(g, g->pf.xmap2), *ym2 = pf_hc(g, g->pf.ymap2); for (uint32_t t = 0; t < n; t++) { xm2[t] = t; ym2[t] = t * NG + S; } for (uint32_t p = n; p < n + PF_PAD; p++) { xm2[p] = 0; ym2[p] = 0; } + if (g->pf.qmma) { + OmegaPfqTile qg[257], qd[257]; + memcpy(qg, sg, (size_t)segs * sizeof *qg); memcpy(qd, sd, (size_t)segs * sizeof *qd); /* Seg32 is the tile entry */ + qg[segs] = (OmegaPfqTile){ .w_va = g2->w_va, .s_va = g2->s_va, .row0 = n * S, .cnt = g2->cnt, .pad = 0 }; + qd[segs] = (OmegaPfqTile){ .w_va = d2->w_va, .s_va = d2->s_va, .row0 = n * S, .cnt = d2->cnt, .pad = 0 }; + uint32_t *qxm = pf_hc(g, g->pf.qxm), *qym = pf_hc(g, g->pf.qym); + memcpy(qxm, xm, (size_t)n * S * 4u); memcpy(qym, ym, (size_t)n * S * 4u); + for (uint32_t t = 0; t < n; t++) { qxm[n * S + t] = t; qym[n * S + t] = t * NG + S; } + for (uint32_t p = n * NG; p < n * NG + OMEGA_PFQ_MAP_PAD; p++) { qxm[p] = 0; qym[p] = 0; } + g->pf.qtg_n = omega_pfq_tiles(qg, segs + 1u, pf_hc(g, g->pf.qtgu), g->pf.qtg_cap); + if (omega_pfq_tiles(qd, segs + 1u, pf_hc(g, g->pf.qtdn), g->pf.qtg_cap) != g->pf.qtg_n) g->pf.qtg_n = 0; + } return segs; } @@ -1150,24 +1319,45 @@ static int pf_record_experts(OmegaGpuQ36Graph *g, uint32_t head, uint32_t l, uin const uint64_t pt2048 = va_of(g, g->lo.ptab), pt512 = pt2048 + (uint64_t)(H + OMEGA_GPU_Q36_MIXER_OUT) * 4u; const uint64_t in_norm = va_of(g, g->lo.norms) + (uint64_t)l * H * 4u, XS = (uint64_t)H * 4u; const uint32_t R = n * NG; const uint32_t S = NG - 1u; /* rows each expert GEMM touches: 8 routed per token, 1 shared */ - int rc = rec_perm(g, K_PERM_XO, rlb + xo_off, pt2048, xp2, H, n); /* the router's x' rows, one launch buffer apart */ - if (rc == OMEGA_GPU_DGRAPH_OK) - rc = native_record_gemm(g, OMEGA_GPU_Q36_K_GATE_UP, g->layer[l].gu, n * S, segs, pf_h(g, g->pf.sgu), xp2, guN, pf_h(g, g->pf.xmap), - pf_h(g, g->pf.ymap), 4); - if (rc == OMEGA_GPU_DGRAPH_OK) - rc = native_record_gemm(g, OMEGA_GPU_Q36_K_GATE_UP, g->layer[l].gu, n, 1, pf_h(g, g->pf.sgu2), xp2, guN, pf_h(g, g->pf.xmap2), - pf_h(g, g->pf.ymap2), 4); - uint32_t w3[OMEGA_DEC_SW_WORDS]; memset(w3, 0, sizeof w3); - put64(w3, OMEGA_DEC_SW_GU, guN); put64(w3, OMEGA_DEC_SW_ACT, actN); putf(w3, OMEGA_DEC_SW_SCALE, SCALE_2P64); - if (rc == OMEGA_GPU_DGRAPH_OK) - rc = add_glue(g, OMEGA_GPU_Q36_K_SWIGLU, g->mutant == 1 ? K_SW_M1 : K_SW, EI / OMEGA_DEC_THREADS, R, (size_t)R * EI, w3, OMEGA_DEC_SW_WORDS); - if (rc == OMEGA_GPU_DGRAPH_OK) rc = rec_perm(g, K_PERM_ACT, actN, pt512, actp, EI, R); - if (rc == OMEGA_GPU_DGRAPH_OK) - rc = native_record_gemm(g, OMEGA_GPU_Q36_K_DOWN, g->layer[l].dn, n * S, segs, pf_h(g, g->pf.sdn), actp, dnN, pf_h(g, g->pf.ymap), - pf_h(g, g->pf.ymap), 4); - if (rc == OMEGA_GPU_DGRAPH_OK) - rc = native_record_gemm(g, OMEGA_GPU_Q36_K_DOWN, g->layer[l].dn, n, 1, pf_h(g, g->pf.sdn2), actp, dnN, pf_h(g, g->pf.ymap2), - pf_h(g, g->pf.ymap2), 4); + int rc = OMEGA_GPU_DGRAPH_OK; + if (g->pf.qmma) { /* DRA-82: the x' rows quantised in place (one launch buffer apart), routed + shared in one QMMA launch */ + if (g->pf.qtg_n == 0) return pf_fail(g, "q36 prefill qmma: expert tile table over capacity"); + rc = pf_qmma_gemm(g, OMEGA_GPU_Q36_K_GATE_UP, rlb + xo_off, n, (uint32_t)(g->lo.lb_stride / 4u), H, 2u * EI, pf_h(g, g->pf.qtgu), + g->pf.qtg_n, pf_h(g, g->pf.qxm), pf_h(g, g->pf.qym), guN); + } else { + rc = rec_perm(g, K_PERM_XO, rlb + xo_off, pt2048, xp2, H, n); /* the router's x' rows, one launch buffer apart */ + if (rc == OMEGA_GPU_DGRAPH_OK) + rc = native_record_gemm(g, OMEGA_GPU_Q36_K_GATE_UP, g->layer[l].gu, n * S, segs, pf_h(g, g->pf.sgu), xp2, guN, pf_h(g, g->pf.xmap), + pf_h(g, g->pf.ymap), 4); + if (rc == OMEGA_GPU_DGRAPH_OK) + rc = native_record_gemm(g, OMEGA_GPU_Q36_K_GATE_UP, g->layer[l].gu, n, 1, pf_h(g, g->pf.sgu2), xp2, guN, pf_h(g, g->pf.xmap2), + pf_h(g, g->pf.ymap2), 4); + } + /* SWIGLU (and the legacy ACT permutation) over the R rows, at most PF_GRID_Y_MAX rows per launch (row r reads gu row r and + * act row r only, so a split at row r0 is the same arithmetic; a legacy chunk is one launch, as before) */ + for (uint32_t r0 = 0; r0 < R && rc == OMEGA_GPU_DGRAPH_OK; r0 += PF_GRID_Y_MAX) { + const uint32_t rows = R - r0 < PF_GRID_Y_MAX ? R - r0 : PF_GRID_Y_MAX; + uint32_t w3[OMEGA_DEC_SW_WORDS]; memset(w3, 0, sizeof w3); + put64(w3, OMEGA_DEC_SW_GU, guN + (uint64_t)r0 * 2u * EI * 4u); put64(w3, OMEGA_DEC_SW_ACT, actN + (uint64_t)r0 * EI * 4u); + putf(w3, OMEGA_DEC_SW_SCALE, SCALE_2P64); + 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, w3, OMEGA_DEC_SW_WORDS); + } + if (g->pf.qmma) { /* act row p is gate_up's y row p: the down GEMM reads and writes through the same y map */ + if (rc == OMEGA_GPU_DGRAPH_OK) + rc = pf_qmma_gemm(g, OMEGA_GPU_Q36_K_DOWN, actN, R, 0, EI, H, pf_h(g, g->pf.qtdn), g->pf.qtg_n, pf_h(g, g->pf.qym), + pf_h(g, g->pf.qym), dnN); + } else { + for (uint32_t r0 = 0; r0 < R && rc == OMEGA_GPU_DGRAPH_OK; r0 += PF_GRID_Y_MAX) { + const uint32_t rows = R - r0 < PF_GRID_Y_MAX ? R - r0 : PF_GRID_Y_MAX; + rc = rec_perm(g, K_PERM_ACT, actN + (uint64_t)r0 * EI * 4u, pt512, actp + (uint64_t)r0 * EI * 4u, EI, rows); + } + if (rc == OMEGA_GPU_DGRAPH_OK) + rc = native_record_gemm(g, OMEGA_GPU_Q36_K_DOWN, g->layer[l].dn, n * S, segs, pf_h(g, g->pf.sdn), actp, dnN, pf_h(g, g->pf.ymap), + pf_h(g, g->pf.ymap), 4); + if (rc == OMEGA_GPU_DGRAPH_OK) + rc = native_record_gemm(g, OMEGA_GPU_Q36_K_DOWN, g->layer[l].dn, n, 1, pf_h(g, g->pf.sdn2), actp, dnN, pf_h(g, g->pf.ymap2), + pf_h(g, g->pf.ymap2), 4); + } const int last = l + 1 == g->cfg.layers; const uint64_t nw = last ? va_of(g, g->lo.fnorm) : in_norm + H * 4u; /* the last layer: only the last token's COMBINE|NORM is needed (its hidden feeds the head; the state is complete) */ @@ -1557,6 +1747,7 @@ int omega_gpu_q36_graph_prefill(OmegaGpuQ36Graph *g, uint32_t head, const float uint32_t *token, OmegaGpuDecodeGraphStats *st) { if (!g || g->in_token || !x_embed || n == 0 || head >= g->heads) return OMEGA_GPU_DGRAPH_BAD_ARGS; if (n > g->pf.cap) return OMEGA_GPU_DGRAPH_BAD_ARGS; + if (n > OMEGA_GPU_Q36_PREFILL_MAX_ROWS && g->native) return OMEGA_GPU_DGRAPH_BAD_ARGS; /* the native MoE window plan holds 128 tokens */ if (!g->gdn_h[head] || !g->attn_h[head]) return OMEGA_GPU_DGRAPH_BAD_ARGS; for (uint32_t l = 0; l < g->cfg.layers; l++) if (!g->layer[l].set) return OMEGA_GPU_DGRAPH_BAD_ARGS; if (!g->final_set) return OMEGA_GPU_DGRAPH_BAD_ARGS; @@ -1573,7 +1764,9 @@ int omega_gpu_q36_graph_prefill(OmegaGpuQ36Graph *g, uint32_t head, const float uint32_t *tok = cpu_of(g, token_of(g, head)); tok[0] = TOKEN_POISON; tok[1] = TOKEN_POISON; memcpy(pf_hc(g, g->pf.x), x_embed, (size_t)n * H * 4u); write_dense_segments(g, n); - if (omega_gpu_session_sync_to_gpu(&g->buf) != 0 || omega_gpu_session_sync_to_gpu(&g->pf.hb) != 0) rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); + if (g->pf.qmma) rc = pf_qmma_dense_tables(g, n); + if (rc == OMEGA_GPU_DGRAPH_OK && (omega_gpu_session_sync_to_gpu(&g->buf) != 0 || omega_gpu_session_sync_to_gpu(&g->pf.hb) != 0)) + rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); } for (uint32_t l = 0; l < g->cfg.layers && rc == OMEGA_GPU_DGRAPH_OK; l++) { uint32_t ids_off = 0, wts_off = 0, xo_off = 0; @@ -1601,9 +1794,9 @@ int omega_gpu_q36_graph_prefill(OmegaGpuQ36Graph *g, uint32_t head, const float 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); uint32_t segs = 0; if (rc == OMEGA_GPU_DGRAPH_OK) { - /* the dense trunk: mix (or q|k|v) and o, 8 rows per pass */ - g->pf.streamed += (uint64_t)((n + 7u) / 8u) * H * ((g->layer[l].attn ? OMEGA_GPU_Q36_ATT_MIX : OMEGA_GPU_Q36_GDN_MIX) + OMEGA_GPU_Q36_MIXER_OUT); - segs = pf_group(g, l, n, ids_off); + /* the dense trunk: mix (or q|k|v) and o */ + pf_count_dense(g, l, n); + segs = pf_group(g, l, n, ids_off, pf_skip_segment(g, l, pos0)); if (segs == 0 || segs > 256u) rc = pf_fail(g, "q36 prefill: router picks out of range"); } if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_session_sync_to_gpu(&g->pf.hb) != 0) rc = fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); diff --git a/src/omega_gpu_q36_graph.h b/src/omega_gpu_q36_graph.h index 076b03f7..df6b58b1 100644 --- a/src/omega_gpu_q36_graph.h +++ b/src/omega_gpu_q36_graph.h @@ -143,21 +143,48 @@ int omega_gpu_q36_graph_read_buf(OmegaGpuQ36Graph *g, int which, float *out, uin * in their ROWS forms; the GDN step and the attention launched per token in order on the head's own state). Per layer two * lists: the mixers and the router, then (after a host sort of the expert picks) the experts and COMBINE|NORM. want_token: * the last token's head and argmax as record_head_final (token, read_head(head) as after a token). prefill_alloc(cap) - * sizes the buffers for chunks of up to cap tokens (1 .. OMEGA_GPU_Q36_PREFILL_MAX_ROWS; nothing shrinks). + * sizes the buffers for chunks of up to cap tokens (1 .. omega_gpu_q36_graph_prefill_rows_max(); nothing shrinks). * TEST ONLY: AIEN_OMEGA_Q36_PF_MUTANT=1 swaps the last two tokens' GDN steps, 2 drops one row of every expert segment, * 3 defers each GDN output copy past the next token's step, 5 gives the multi-token GDN a|b row stride 0 (every token - * reads token 0's gates), 6 drops the attention prep launch (the chunk's own K/V rows never reach the cache). The GDN runs + * reads token 0's gates), 6 drops the attention prep launch (the chunk's own K/V rows never reach the cache), 7 (DRA-83) + * skips one chunk's expert segment: in the chunk at pos0 0, layer 0's highest-id routed segment is left out of the + * segment tables (its rows' expert outputs are never written) and out of the streamed count. The GDN runs * as ONE multi-token launch per layer (the decode step's arithmetic in a loop, bit-identical to the per-token launches; * mutants 1 and 3 use the per-token form); AIEN_OMEGA_Q36_PF_GDN=0 forces the per-token launches. Each attention layer is * one prep launch (every token's K/V rows) and one attend launch (grid (splits, tokens x rows), the decode kernel per - * token, bit-identical; omega_gpu_decode_attn256_multi_record); AIEN_OMEGA_Q36_PF_ATTN=0 forces the per-token launches. */ + * token, bit-identical; omega_gpu_decode_attn256_multi_record); AIEN_OMEGA_Q36_PF_ATTN=0 forces the per-token launches. + * DRA-82: AIEN_OMEGA_Q36_PF_QMMA=1 (read by prefill_alloc, which reallocates when it changed; default off) moves the mix, + * o, gate_up and down GEMMs onto the tensor cores (omega_gpu_pfq_api.h: per-row e4m3 quantiser + QMMA tiles of 64 rows, + * the routed and shared experts in one launch, no PERM launches). Not bit-identical to the GEMV: the activations are + * quantised to e4m3 per 128-wide block. o stays on the GEMV where the GDN runs per token. Refused with native artifacts. + * Row bounds (DRA-83): the legacy fp8 GEMV/GEMM prefill holds OMEGA_GPU_Q36_PREFILL_MAX_ROWS tokens per chunk; the + * tensor-core prefill (DRA-82, AIEN_OMEGA_Q36_PF_QMMA=1, default off) holds OMEGA_GPU_Q36_PREFILL_TC_MAX_ROWS. A cap above + * the legacy bound also needs the multi-token GDN, attention and COMBINE|NORM forms (their per-token fallbacks would + * overflow one command list) and no native MoE window path (its plan is sized for 128 tokens); prefill_alloc refuses + * it otherwise (BAD_ARGS). An allocation the session refuses returns CHIP_FAIL with both buffers freed and cap 0. */ #define OMEGA_GPU_Q36_PREFILL_MAX_ROWS 128u /* x 8 routed expert rows <= the fp8 gemm's 1024 (the shared expert is its own GEMM) */ +#define OMEGA_GPU_Q36_PREFILL_TC_MAX_ROWS 8192u /* the tensor-core prefill only (DRA-83); the decode GEMV bound is unchanged */ +/* The row bound in force: OMEGA_GPU_Q36_PREFILL_TC_MAX_ROWS when AIEN_OMEGA_Q36_PF_QMMA=1, else + * OMEGA_GPU_Q36_PREFILL_MAX_ROWS (read from the environment on every call). */ +uint32_t omega_gpu_q36_graph_prefill_rows_max(void); +/* The chunk knob (DRA-83): AIEN_OMEGA_Q36_PF_CHUNK, tokens per prefill chunk, read on every call. Unset: 128. A decimal + * 1 .. omega_gpu_q36_graph_prefill_rows_max() is written to *chunk (OK); anything else (0, text, above the bound) is + * BAD_ARGS and *chunk is left alone. */ +int omega_gpu_q36_graph_prefill_chunk(uint32_t *chunk); +/* Bytes prefill_alloc(cap) asks for (host and device buffers; lb_stride the router layout stride and layers the layer + * count of the graph) at the same layout, with the QMMA regions when AIEN_OMEGA_Q36_PF_QMMA=1, or BAD_ARGS for a cap of 0 + * or above omega_gpu_q36_graph_prefill_rows_max(). Host only. */ +int omega_gpu_q36_graph_prefill_bytes(uint32_t cap, uint32_t lb_stride, uint32_t layers, uint64_t *host_bytes, uint64_t *dev_bytes); int omega_gpu_q36_graph_prefill_alloc(OmegaGpuQ36Graph *g, uint32_t cap); uint32_t omega_gpu_q36_graph_prefill_cap(const OmegaGpuQ36Graph *g); -/* fp8 GEMM weight bytes every prefill call on g so far streamed (a running count: take the difference around a call): every expert once per 4 of its rows, the dense mix/o once per 8 - * rows (the ceiling receipt: these bytes over the call time against the memory bandwidth). The bf16 a|b, router and head - * reads are not counted. */ +/* fp8 GEMM weight bytes every prefill call on g so far streamed (a running count: take the difference around a call). + * Legacy path: every expert once per 4 of its rows, the dense mix/o once per 8 rows. Tensor-core path (pf qmma, DRA-83 + * G2): every expert a chunk touches once, the shared expert once and the dense mix/o once per layer per chunk (the bytes + * a chunk must stream at least). The bf16 a|b, router and head reads are not counted. */ uint64_t omega_gpu_q36_graph_prefill_streamed(const OmegaGpuQ36Graph *g); +/* The same weights counted per GEMM pass as launched (legacy: 4 expert rows and 8 dense rows per pass, the two counts equal + * there; QMMA: one 64-row tile per pass). The gap to prefill_streamed is the re-read cache reuse must absorb. */ +uint64_t omega_gpu_q36_graph_prefill_passes_streamed(const OmegaGpuQ36Graph *g); int omega_gpu_q36_graph_prefill(OmegaGpuQ36Graph *g, uint32_t head, const float *x_embed, uint32_t n, uint32_t pos0, int want_token, uint32_t *token, OmegaGpuDecodeGraphStats *st); /* The gather table (u32 [9216]): agg[n] = mix[tab[n]]. */ diff --git a/tests/bw_hazard_test.c b/tests/bw_hazard_test.c index e052b090..32450efd 100644 --- a/tests/bw_hazard_test.c +++ b/tests/bw_hazard_test.c @@ -487,7 +487,7 @@ static void sweep(void) { } for (unsigned a = 0; a < 5; a++) for (unsigned w = 8; w <= 16; w *= 2) for (unsigned epi = 0; epi <= 1; epi++) for (unsigned dir = 0; dir <= 1; dir++) for (unsigned wt = 0; wt <= 120; wt += 60) { - OmegaQgSpec s = { ks[a], 4, w, epi, dir, 512, wt, 0 }; + OmegaQgSpec s = { ks[a], 4, w, epi, dir, 512, wt, 0, 0, 0 }; snprintf(v, sizeof v, "k%u warps%u epi%u direct%u n512 wait%u", ks[a], w, epi, dir, wt); if (BUILD(omega_blackwell_codegen_qgemm_prog(&s, &g_prog))) sweep_prog("qgemm", v, P_TC | P_QMMA); } diff --git a/tests/gpu_pfq_test.c b/tests/gpu_pfq_test.c new file mode 100644 index 00000000..824e8c37 --- /dev/null +++ b/tests/gpu_pfq_test.c @@ -0,0 +1,607 @@ +/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ +/* gpu_pfq_test.c (DRA-82, CUDApp #220): the tiled mma.sync FP8 prefill path, omega_gpu_pfq_api.h. + * --host-only tiler, launch plans, the pre-DRA-82 kernel words (digest of CUDApp main 4bbccf0), the x64 quantiser + * identity, the C reference against a double oracle + * --sim the planned kernels and arguments in the warp simulator: quantiser row stride, row map (rowidx), output + * map (ymap), x64 scales, segments longer than a tile, both issue orders; bit-identical to the C reference, + * untouched output rows left alone; AIEN_OMEGA_PFQ_MUTANT 1..4 each caught + * --chip (inside a GB10 window only) the Qwen3.6 expert shapes of one prefill chunk through a command list: + * quantiser and GEMM outputs bit-identical to the reference, mutants caught, per-launch GPU time + * AIEN_OMEGA_PFQ_MUTANT set in the environment applies to every planned launch, so a clean case then FAILS: the + * end-to-end check of the knob. Verdict line last: "GPU_PFQ: PASS|FAIL". */ +#include "omega_gpu_pfq_api.h" +#include "omega_gpu_prefill.h" +#include "omega_gpu_fp8_api.h" +#include "omega_gpu_cmdlist.h" +#include "bw_warp_sim_tc.h" +#include "pfq_legacy_words.h" +#include +#include +#include +#include + +/* printed by tests/pfq_legacy_words.h built against CUDApp main 4bbccf0 (evidence/DRA-82-pfq/golden-main-4bbccf0.txt) */ +#define PFQ_LEGACY_PROGRAMS 264 +#define PFQ_LEGACY_DIGEST 0x0dd37a9ee7562178ull +#define PAD OMEGA_PFQ_MAP_PAD + +static uint64_t rng = 0x9e3779b97f4a7c15ull; +static uint32_t rnd(void) { rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; return (uint32_t)(rng >> 16); } +static float u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } +static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } +static int cmp_u64(const void *a, const void *b) { uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b; return x < y ? -1 : x > y; } +static int env_mutant; + +/* the quantiser's header semantics (tests/gpu_qgemm_test.c pq_ref; mutant 2 = c 0) */ +static void pq_ref(const float *x, uint32_t k, uint8_t *q, float *s, int mutant) { + for (uint32_t b = 0; b < k / 128; b++) { + float m = ldexpf(1.0f, -100); + for (uint32_t j = 0; j < 128; j++) m = fmaxf(m, fabsf(x[128 * b + j])); + uint32_t mb = f2u(m), E = mb >> 23, M = mb & 0x7fffff, c = mutant == 2 ? 0 : (M + 0x1fffff) >> 23; + s[b] = u2f((E + c - 8) << 23); + const float inv = u2f((262 - E - c) << 23); + for (uint32_t j = 0; j < 128; j++) q[128 * b + j] = qm_e4m3_rne_sat(x[128 * b + j] * inv); + } +} +static float rand_act(void) { + float v = ((float)(rnd() & 0xffffff) / 16777216.0f) * 2.0f - 1.0f; + int r = rnd() % 32; if (r == 0) v *= 300.0f; else if (r == 1) v *= 1e-3f; else if (r == 2) v = 0.0f; + return v; +} +static uint8_t rand_e4m3(void) { for (;;) { uint8_t c = (uint8_t)rnd(); if ((c & 0x7f) != 0x7f) return c; } } + +/* ---------------- one grouped expert GEMM problem ---------------- */ +typedef struct { + uint32_t E, n, k, KB, M, XR, YR, indexed, ymapped, x64; + uint8_t *w; uint16_t *sbf; float *swf; /* [E][n][k] e4m3, scale_inv bf16, GEMV-tensor scales (f32 x 2^56) */ + float *x; /* XR true activation rows */ + uint8_t *xq; float *xs, *xs64; /* XR + PAD rows: e4m3, true scales, the scales of 2^64 x */ + uint32_t *eid, *ridx, *ymap; /* M + PAD entries */ + uint32_t *counts; OmegaPfqTile *seg; /* one segment per expert, in order (zero counts kept) */ + uint32_t x64_mismatch; /* q(2^64 x) != q(x) or s(2^64 x) != 2^64 s(x) outside all-zero blocks */ +} Prob; + +static void prob_make(Prob *P, uint32_t E, uint32_t n, uint32_t k, const uint32_t *counts, uint32_t XR, uint32_t indexed, uint32_t ymapped, uint32_t YR) { + memset(P, 0, sizeof *P); + uint32_t M = 0; for (uint32_t e = 0; e < E; e++) M += counts[e]; + P->E = E; P->n = n; P->k = k; P->KB = k / 128; P->M = M; P->indexed = indexed; P->ymapped = ymapped; + P->XR = indexed ? XR : M; P->YR = ymapped ? YR : M; + const size_t wn = (size_t)E * n * k, sn = (size_t)E * (n / 128) * P->KB, XA = (size_t)P->XR + PAD; + P->w = malloc(wn); P->sbf = malloc(sn * 2); P->swf = malloc(sn * 4); + for (size_t i = 0; i < wn; i++) P->w[i] = rand_e4m3(); + for (size_t i = 0; i < sn; i++) { float s = ldexpf(1.0f + (float)(rnd() & 127) / 128.0f, -(int)(rnd() % 12) - 4); P->sbf[i] = (uint16_t)(f2u(s) >> 16); } + if (omega_fp8_pack_scales(P->sbf, sn, P->swf, 0) != 0) { printf("scale pack refused\n"); exit(2); } + P->x = calloc(XA * k, 4); P->xq = calloc(XA * k, 1); P->xs = calloc(XA * P->KB, 4); P->xs64 = calloc(XA * P->KB, 4); + for (size_t i = 0; i < (size_t)P->XR * k; i++) P->x[i] = rand_act(); + if (P->XR > 3) memset(P->x + 3 * (size_t)k, 0, k * 4); /* an all-zero row: the amax floor */ + if (P->XR > 5) P->x[5 * (size_t)k + 77] = 3.0e4f; /* an outlier */ + float *x64 = malloc(k * 4); uint8_t *q64 = malloc(k); + for (size_t r = 0; r < XA; r++) { + pq_ref(P->x + r * k, k, P->xq + r * k, P->xs + r * P->KB, 0); + for (uint32_t j = 0; j < k; j++) x64[j] = ldexpf(P->x[r * k + j], 64); + pq_ref(x64, k, q64, P->xs64 + r * P->KB, 0); + for (uint32_t b = 0; b < P->KB; b++) { + int zero = 1; for (uint32_t j = 0; j < 128; j++) if (P->x[r * k + 128 * b + j] != 0.0f) zero = 0; + if (memcmp(q64 + 128 * b, P->xq + r * k + 128 * b, 128)) P->x64_mismatch++; + else if (!zero && f2u(P->xs64[r * P->KB + b]) != f2u(ldexpf(P->xs[r * P->KB + b], 64))) P->x64_mismatch++; + } + } + free(x64); free(q64); + P->eid = calloc(M + PAD, 4); P->ridx = calloc(M + PAD, 4); P->ymap = calloc(M + PAD, 4); + P->counts = malloc(E * 4); memcpy(P->counts, counts, E * 4); + P->seg = calloc(E, sizeof *P->seg); + uint32_t row = 0; + for (uint32_t e = 0; e < E; e++) { + P->seg[e].w_va = (uintptr_t)(P->w + (size_t)e * n * k); P->seg[e].s_va = (uintptr_t)(P->swf + (size_t)e * (n / 128) * P->KB); + P->seg[e].row0 = row; P->seg[e].cnt = counts[e]; + for (uint32_t q = 0; q < counts[e]; q++) P->eid[row + q] = e; + row += counts[e]; + } + for (uint32_t p = 0; p < M; p++) P->ridx[p] = indexed ? rnd() % P->XR : p; + uint32_t *perm = malloc((size_t)P->YR * 4); for (uint32_t i = 0; i < P->YR; i++) perm[i] = i; + for (uint32_t i = P->YR; i > 1; i--) { uint32_t j = rnd() % i, t = perm[i - 1]; perm[i - 1] = perm[j]; perm[j] = t; } + for (uint32_t p = 0; p < M; p++) P->ymap[p] = ymapped ? perm[p] : p; + free(perm); +} +static void prob_free(Prob *P) { + free(P->w); free(P->sbf); free(P->swf); free(P->x); free(P->xq); free(P->xs); free(P->xs64); + free(P->eid); free(P->ridx); free(P->ymap); free(P->counts); free(P->seg); +} + +/* the kernel's arithmetic for routed row p, output column c (weight row c), with the TRUE activation scales */ +static const QmmaModel GB10Q = { 32, 26, 0, 2, 0 }; /* chip-exact, evidence OWN30B-L1G-QMMA-PFGEMM-c742aa13 */ +static float qref(const Prob *P, uint32_t p, uint32_t c) { + const uint32_t k = P->k, KB = P->KB, e = P->eid[p], s = P->ridx[p]; + const uint8_t *xr = P->xq + (size_t)s * k, *wr = P->w + ((size_t)e * P->n + c) * k; + const float *swt = P->swf + ((size_t)e * (P->n / 128) + c / 128) * KB; + float acc = 0.0f; + for (uint32_t kb = 0; kb < KB; kb++) { + const float sc = P->xs[(size_t)s * KB + kb] * (swt[kb] * 0x1p-56f); + for (uint32_t h = 0; h < 2; h++) { + float T = 0.0f; + for (uint32_t v = 0; v < 2; v++) { + float av[32], bv[32]; + for (uint32_t t = 0; t < 4; t++) for (uint32_t j = 0; j < 4; j++) { + const uint32_t o = 128 * kb + 64 * h + 16 * t + 8 * v + j; + av[4 * t + j] = qm_e4m3(xr[o]); bv[4 * t + j] = qm_e4m3(wr[o]); + av[16 + 4 * t + j] = qm_e4m3(xr[o + 4]); bv[16 + 4 * t + j] = qm_e4m3(wr[o + 4]); + } + T = qm_dot32(&GB10Q, av, bv, T); + } + acc = fmaf(T, sc, acc); + } + } + return acc; +} +/* double oracle: original bf16 weight scales, dequantised activations */ +static double qoracle(const Prob *P, uint32_t p, uint32_t c, double *mag) { + const uint32_t k = P->k, KB = P->KB, e = P->eid[p], s = P->ridx[p]; + double acc = 0, m = 0; + for (uint32_t j = 0; j < k; j++) { + const double sw = u2f((uint32_t)P->sbf[((size_t)e * (P->n / 128) + c / 128) * KB + j / 128] << 16); + const double t = (double)qm_e4m3(P->w[((size_t)e * P->n + c) * k + j]) * sw * qm_e4m3(P->xq[(size_t)s * k + j]) * P->xs[(size_t)s * KB + j / 128]; + acc += t; m += fabs(t); + } + *mag = m; return acc; +} +/* expected y (YR x n f32): routed rows at their map rows, every other row the 0xee fill */ +static uint32_t *expect_y(const Prob *P) { + uint32_t *y = malloc((size_t)P->YR * P->n * 4); memset(y, 0xee, (size_t)P->YR * P->n * 4); + for (uint32_t p = 0; p < P->M; p++) for (uint32_t c = 0; c < P->n; c++) y[(size_t)P->ymap[p] * P->n + c] = f2u(qref(P, p, c)); + return y; +} +static size_t ydiff(const Prob *P, const uint32_t *got, const uint32_t *want) { + size_t bad = 0; + for (size_t i = 0; i < (size_t)P->YR * P->n; i++) if (got[i] != want[i]) { if (getenv("PFDBG") && bad < 6) printf(" y[%zu][%zu] got %08x want %08x\n", i / P->n, i % P->n, got[i], want[i]); bad++; } + return bad; +} + +/* ---------------- host ---------------- */ +static int tiler_check(const OmegaPfqTile *seg, uint32_t nseg, uint32_t rows, const OmegaPfqTile *t, uint32_t nt) { + int bad = 0; uint8_t *seen = calloc(rows + 1, 1); + for (uint32_t i = 0; i < nt; i++) { + if (t[i].cnt == 0 || t[i].cnt > OMEGA_PFQ_BM || t[i].pad) bad++; + const OmegaPfqTile *s = NULL; + for (uint32_t j = 0; j < nseg; j++) if (seg[j].cnt && t[i].row0 >= seg[j].row0 && t[i].row0 < seg[j].row0 + seg[j].cnt) s = &seg[j]; + if (!s || t[i].w_va != s->w_va || t[i].s_va != s->s_va || t[i].row0 + t[i].cnt > s->row0 + s->cnt) { bad++; continue; } + for (uint32_t r = 0; r < t[i].cnt; r++) seen[t[i].row0 + r]++; + } + for (uint32_t r = 0; r < rows; r++) if (seen[r] != 1) bad++; + free(seen); + return bad; +} +static int host_tiler(void) { + static const uint32_t cnt[] = { 0, 1, 63, 64, 65, 130, 7 }; + const uint32_t ns = sizeof cnt / sizeof cnt[0]; + OmegaPfqTile seg[7], t[32]; uint32_t row = 0, want = 0; + for (uint32_t i = 0; i < ns; i++) { memset(&seg[i], 0, sizeof seg[i]); seg[i].w_va = 0x1000u * (i + 1); seg[i].s_va = 0x100000u * (i + 1); seg[i].row0 = row; seg[i].cnt = cnt[i]; row += cnt[i]; want += (cnt[i] + 63) / 64; } + const uint32_t cap = omega_pfq_tile_cap(row, ns), nt = omega_pfq_tiles(seg, ns, t, 32); + const int bad = tiler_check(seg, ns, row, t, nt); + const int over = omega_pfq_tiles(seg, ns, t, want - 1) != 0, null = omega_pfq_tiles(NULL, ns, t, 32) != 0; + const int ok = nt == want && cap >= want && !bad && !over && !null; + printf("%s tiler: segments {0,1,63,64,65,130,7} -> %u tiles (want %u, cap %u), %d coverage errors, cap-1 %s, NULL %s\n", ok ? "PASS" : "FAIL", + nt, want, cap, bad, over ? "ACCEPTED" : "refused", null ? "ACCEPTED" : "refused"); + return ok ? 0 : 1; +} +static uint64_t words_hash(int qg, const void *spec) { + BlackwellIRProgram *prog = calloc(1, sizeof *prog); uint8_t *code = malloc(1u << 20); uint64_t h = 0xcbf29ce484222325ull; + const int cg = qg ? omega_blackwell_codegen_qgemm_prog(spec, prog) : omega_blackwell_codegen_pq_prog(spec, prog); + if (cg != 0 || pfq_encode_hash(prog, code, 1u << 20, &h, 0) != 0) h = 0; + free(prog); free(code); return h; +} +static int host_words(void) { + uint64_t h = 0; const int n = pfq_legacy_digest(&h); + const int same = n == PFQ_LEGACY_PROGRAMS && h == PFQ_LEGACY_DIGEST; + printf("%s pre-DRA-82 qg/pq kernels: %d programs, digest 0x%016llx (main 4bbccf0: %d, 0x%016llx)\n", same ? "PASS" : "FAIL", + n, (unsigned long long)h, PFQ_LEGACY_PROGRAMS, (unsigned long long)PFQ_LEGACY_DIGEST); + /* negative control: the new fields do change the words, so the digest above would see them leak into legacy specs */ + OmegaQgSpec b; memset(&b, 0, sizeof b); b.k = 2048; b.rt = 4; b.warps = 8; b.epi = OMEGA_QG_EPI_F32; b.n = 1024; b.wait = 45; + OmegaQgSpec y = b, x = b; y.ymap = 1; x.x64 = 1; + OmegaPqSpec p; memset(&p, 0, sizeof p); p.k = 2048; OmegaPqSpec ps = p; ps.stride = 2048 + 68; + const uint64_t hb = words_hash(1, &b), hy = words_hash(1, &y), hx = words_hash(1, &x), hp = words_hash(0, &p), hs = words_hash(0, &ps); + const int ctl = hb && hy && hx && hp && hs && hb != hy && hb != hx && hy != hx && hp != hs; + printf("%s negative control: qg ymap=1 / x64=1 and pq stride change the encoded words (%016llx %016llx %016llx | %016llx %016llx)\n", ctl ? "PASS" : "FAIL", + (unsigned long long)hb, (unsigned long long)hy, (unsigned long long)hx, (unsigned long long)hp, (unsigned long long)hs); + return (same ? 0 : 1) + (ctl ? 0 : 1); +} +static int host_plans(void) { + int bad = 0; OmegaQgSpec s; OmegaPqSpec q; OmegaPfqLaunch L; + OmegaPfqGemm c = { .n = 1024, .k = 2048, .x64 = 1, .ntiles = 21, .tiles_va = 0x10, .rowidx_va = 0x20, .xq_va = 0x30, .xs_va = 0x40, .ymap_va = 0x1234567890ull, .y_va = 0x50 }; + if (omega_pfq_gemm_plan(&c, &s, &L) != OMEGA_GPU_PF_OK || L.nargs != OMEGA_QG_ARGS_WORDS_YMAP || L.args[12] != 0x34567890u || L.args[13] != 0x12u || + L.args[10] != 1024 || L.args[11] != 0 || L.args[6] != 0x50 || L.args[8] != 0x40 || L.threads != 256 || L.grid_x != 1024 / omega_qg_span(&s) || + L.grid_y != 21 || L.shared != omega_qg_shared_bytes(&s) || !s.ymap || !s.x64 || s.direct || s.epi != OMEGA_QG_EPI_F32) { printf(" gemm plan (ymap) wrong\n"); bad++; } + c.ymap_va = 0; c.rowidx_va = 0; c.x64 = 0; + if (omega_pfq_gemm_plan(&c, &s, &L) != OMEGA_GPU_PF_OK || L.nargs != OMEGA_QG_ARGS_WORDS || s.ymap || !s.direct || s.x64) { printf(" gemm plan (plain) wrong\n"); bad++; } + OmegaPfqGemm r = c; r.n = 1000; if (omega_pfq_gemm_plan(&r, &s, &L) != OMEGA_GPU_PF_BAD_ARGS) { printf(" n 1000 accepted\n"); bad++; } + r = c; r.x64 = 2; if (omega_pfq_gemm_plan(&r, &s, &L) != OMEGA_GPU_PF_BAD_ARGS) { printf(" x64 2 accepted\n"); bad++; } + r = c; r.ntiles = 70000; if (omega_pfq_gemm_plan(&r, &s, &L) != OMEGA_GPU_PF_TOO_LARGE) { printf(" 70000 tiles accepted\n"); bad++; } + OmegaPfqQuant u = { .k = 2048, .rows = 128, .stride = 2048 + 1032, .x_va = 1, .q_va = 2, .s_va = 3 }; + if (omega_pfq_quant_plan(&u, &q, &L) != OMEGA_GPU_PF_OK || q.stride != 3080 || L.threads != 512 || L.grid_x != 128 || L.grid_y != 1 || L.nargs != 6) { printf(" quant plan wrong\n"); bad++; } + u.k = 5120; u.stride = 0; + if (omega_pfq_quant_plan(&u, &q, &L) != OMEGA_GPU_PF_OK || q.groups != 20 || L.threads != 640 || L.grid_y != 2) { printf(" quant plan (split row) wrong\n"); bad++; } + u.k = 2048; u.stride = 2040; if (omega_pfq_quant_plan(&u, &q, &L) != OMEGA_GPU_PF_BAD_ARGS) { printf(" stride < k accepted\n"); bad++; } + u.stride = 2050; if (omega_pfq_quant_plan(&u, &q, &L) != OMEGA_GPU_PF_BAD_ARGS) { printf(" stride 2050 accepted\n"); bad++; } + printf("%s launch plans: argument words, grids, refusals (%d wrong)\n", bad ? "FAIL" : "PASS", bad); + return bad ? 1 : 0; +} +static int host_ref(void) { + static const uint32_t cnt[] = { 30, 0, 9, 70 }; + Prob P; prob_make(&P, 4, 256, 384, cnt, 40, 1, 1, 120); + double worst = 0; + for (uint32_t p = 0; p < P.M; p += 3) for (uint32_t c = 0; c < P.n; c++) { double m, o = qoracle(&P, p, c, &m); double d = fabs(qref(&P, p, c) - o) / (m + 1e-30); if (d > worst) worst = d; } + const int ok = worst < 1e-6; + printf("%s C reference vs double oracle: worst error %.3g of the sum of |terms|\n", ok ? "PASS" : "FAIL", worst); + const int x64ok = P.x64_mismatch == 0; + printf("%s x64 quantiser identity: q(2^64 x) == q(x), s(2^64 x) == 2^64 s(x) on %u rows x %u blocks (%u differ; all-zero blocks keep the floor scale, q 0)\n", + x64ok ? "PASS" : "FAIL", P.XR + PAD, P.KB, P.x64_mismatch); + /* INFO (no gate): what the activation quantiser costs against the incumbent GEMV's unquantised f32 activations, on these + * synthetic rows (uniform +-1 with 1/32 x300 outliers, 1/32 x1e-3, 1/32 zeros; NOT model activations) */ + double wa = 0, se = 0, sy = 0; + for (uint32_t p = 0; p < P.M; p += 3) + for (uint32_t c = 0; c < P.n; c++) { + const uint32_t e = P.eid[p], s = P.ridx[p]; + double a = 0, m = 0; + for (uint32_t j = 0; j < P.k; j++) { + const double sw = u2f((uint32_t)P.sbf[((size_t)e * (P.n / 128) + c / 128) * P.KB + j / 128] << 16); + const double t = (double)qm_e4m3(P.w[((size_t)e * P.n + c) * P.k + j]) * sw * P.x[(size_t)s * P.k + j]; + a += t; m += fabs(t); + } + const double d = fabs((double)qref(&P, p, c) - a); + if (m > 0 && d / m > wa) wa = d / m; + se += d * d; sy += a * a; + } + printf("INFO activation e4m3 (per row, per 128-col block, power-of-two scale) vs f32 activations, synthetic rows k %u: worst |dy| %.3g of the sum of |terms|, rms |dy|/rms |y| %.3g\n", + P.k, wa, sy > 0 ? sqrt(se / sy) : 0.0); + prob_free(&P); + return (ok ? 0 : 1) + (x64ok ? 0 : 1); +} + +/* ---------------- warp simulator ---------------- */ +static int sim_quant(uint32_t k, uint32_t stride, uint32_t rows, int x64, size_t *bad, char *msg, size_t msgn) { + const uint32_t XS = stride ? stride : k, KB = k / 128; + float *x = malloc((size_t)rows * XS * 4), *xin = malloc((size_t)rows * XS * 4); + for (size_t i = 0; i < (size_t)rows * XS; i++) x[i] = (i % XS) < k ? rand_act() : 1e30f; /* the gap columns must never be read */ + if (rows > 3) for (uint32_t j = 0; j < k; j++) x[3 * (size_t)XS + j] = 0.0f; + for (size_t i = 0; i < (size_t)rows * XS; i++) xin[i] = (x64 && (i % XS) < k) ? ldexpf(x[i], 64) : x[i]; + uint8_t *q = malloc((size_t)rows * k), *rq = malloc(k); float *s = malloc((size_t)rows * KB * 4), *rs = malloc(KB * 4); + memset(q, 0xee, (size_t)rows * k); memset(s, 0xee, (size_t)rows * KB * 4); + OmegaPfqQuant u = { .k = k, .rows = rows, .stride = stride, .x_va = (uintptr_t)xin, .q_va = (uintptr_t)q, .s_va = (uintptr_t)s }; + OmegaPqSpec sp; OmegaPfqLaunch L; int rc = -1; + static BlackwellIRProgram prog; + if (omega_pfq_quant_plan(&u, &sp, &L) != OMEGA_GPU_PF_OK) snprintf(msg, msgn, "plan refused"); + else if (omega_blackwell_codegen_pq_prog(&sp, &prog) != 0) snprintf(msg, msgn, "codegen refused"); + else { + static WsSim ws; memset(&ws, 0, sizeof ws); + ws.p = &prog; ws.threads = L.threads; + for (uint32_t i = 0; i < L.nargs; i++) ws.cbank[0x380 / 4 + i] = L.args[i]; + ws.ranges[0].lo = (uintptr_t)xin; ws.ranges[0].hi = (uintptr_t)(xin + (size_t)rows * XS); + ws.ranges[1].lo = (uintptr_t)q; ws.ranges[1].hi = (uintptr_t)(q + (size_t)rows * k); + ws.ranges[2].lo = (uintptr_t)s; ws.ranges[2].hi = (uintptr_t)(s + (size_t)rows * KB); + ws.nranges = 3; + rc = ws_run(&ws, L.grid_x, L.grid_y) ? -1 : 0; + if (rc) snprintf(msg, msgn, "simulator %.150s", ws.msg); + } + *bad = 0; + if (!rc) for (uint32_t r = 0; r < rows; r++) { + pq_ref(xin + (size_t)r * XS, k, rq, rs, 0); + for (uint32_t j = 0; j < k; j++) if (rq[j] != q[(size_t)r * k + j]) (*bad)++; + for (uint32_t b = 0; b < KB; b++) if (f2u(rs[b]) != f2u(s[(size_t)r * KB + b])) (*bad)++; + } + free(x); free(xin); free(q); free(rq); free(s); free(rs); + return rc; +} +/* plans, generates and simulates one gemm of P; y gets YR x n f32 */ +static int sim_gemm(const Prob *P, int x64, int order, uint32_t *y, char *msg, size_t msgn) { + const uint32_t cap = omega_pfq_tile_cap(P->M, P->E); + OmegaPfqTile *t = calloc(cap + 1, sizeof *t); + const uint32_t nt = omega_pfq_tiles(P->seg, P->E, t, cap); + const float *xs = x64 ? P->xs64 : P->xs; + memset(y, 0xee, (size_t)P->YR * P->n * 4); + OmegaPfqGemm c = { .n = P->n, .k = P->k, .x64 = (uint32_t)x64, .ntiles = nt, .tiles_va = (uintptr_t)t, .rowidx_va = P->indexed ? (uintptr_t)P->ridx : 0, + .xq_va = (uintptr_t)P->xq, .xs_va = (uintptr_t)xs, .ymap_va = P->ymapped ? (uintptr_t)P->ymap : 0, .y_va = (uintptr_t)y }; + OmegaQgSpec sp; OmegaPfqLaunch L; int rc = -1; + static BlackwellIRProgram prog; + if (!nt) snprintf(msg, msgn, "tiler refused"); + else if (omega_pfq_gemm_plan(&c, &sp, &L) != OMEGA_GPU_PF_OK) snprintf(msg, msgn, "plan refused"); + else if (omega_blackwell_codegen_qgemm_prog(&sp, &prog) != 0) snprintf(msg, msgn, "codegen refused"); + else { + uint8_t *smem = malloc(L.shared); + static WsSim s; memset(&s, 0, sizeof s); + s.p = &prog; s.threads = L.threads; s.smem = smem; s.smem_bytes = L.shared; s.order = order; + for (uint32_t i = 0; i < L.nargs; i++) s.cbank[0x380 / 4 + i] = L.args[i]; + const size_t XA = (size_t)P->XR + PAD; + s.ranges[0].lo = (uintptr_t)t; s.ranges[0].hi = (uintptr_t)(t + nt); + s.ranges[1].lo = (uintptr_t)P->ridx; s.ranges[1].hi = (uintptr_t)(P->ridx + P->M + PAD); + s.ranges[2].lo = (uintptr_t)P->xq; s.ranges[2].hi = (uintptr_t)(P->xq + XA * P->k); + s.ranges[3].lo = (uintptr_t)y; s.ranges[3].hi = (uintptr_t)(y + (size_t)P->YR * P->n); + s.ranges[4].lo = (uintptr_t)P->w; s.ranges[4].hi = (uintptr_t)(P->w + (size_t)P->E * P->n * P->k); + s.ranges[5].lo = (uintptr_t)P->swf; s.ranges[5].hi = (uintptr_t)(P->swf + (size_t)P->E * (P->n / 128) * P->KB); + s.ranges[6].lo = (uintptr_t)xs; s.ranges[6].hi = (uintptr_t)(xs + XA * P->KB); + s.ranges[7].lo = (uintptr_t)P->ymap; s.ranges[7].hi = (uintptr_t)(P->ymap + P->M + PAD); + s.nranges = 8; + rc = ws_run(&s, L.grid_x, L.grid_y) ? -1 : 0; + if (rc) snprintf(msg, msgn, "simulator %.150s", s.msg); + free(smem); + } + free(t); + return rc; +} +/* the q36 prefill's expert GEMMs as omega_gpu_q36_graph.c pf_group lays them out: T tokens x 8 routed picks (distinct experts + * of E-1) sorted by expert, then the shared expert (segment E-1) at rows T*8 .. with x' row t and y row t*9+8. gate_up reads + * x' row = token through rowidx; down reads act row = its own y row (rowidx == ymap). */ +static void q36_layout(Prob *P, uint32_t T, uint32_t E, uint32_t n, uint32_t k, int down) { + uint32_t *pick = malloc((size_t)T * 8 * 4), *cnt = calloc(E, 4); + for (uint32_t t = 0; t < T; t++) + for (uint32_t s = 0; s < 8; s++) { + uint32_t e; int dup; + do { e = rnd() % (E - 1); dup = 0; for (uint32_t j = 0; j < s; j++) dup |= pick[t * 8 + j] == e; } while (dup); + pick[t * 8 + s] = e; cnt[e]++; + } + cnt[E - 1] = T; + prob_make(P, E, n, k, cnt, down ? T * 9 : T, 1, 1, T * 9); + uint32_t *start = calloc(E, 4); for (uint32_t e = 1; e < E; e++) start[e] = start[e - 1] + cnt[e - 1]; + for (uint32_t t = 0; t < T; t++) + for (uint32_t s = 0; s < 8; s++) { const uint32_t p = start[pick[t * 8 + s]]++; P->ymap[p] = t * 9 + s; P->ridx[p] = t; } + for (uint32_t t = 0; t < T; t++) { P->ymap[T * 8 + t] = t * 9 + 8; P->ridx[T * 8 + t] = t; } + if (down) for (uint32_t p = 0; p < P->M; p++) P->ridx[p] = P->ymap[p]; + free(pick); free(cnt); free(start); +} +static int sim(void) { + int fail = 0; char msg[200]; + static const struct { uint32_t k, stride, rows; int x64; } qc[] = { { 2048, 2048 + 68, 9, 1 }, { 2048, 0, 9, 0 }, { 512, 512, 11, 1 }, { 5120, 5120 + 128, 5, 1 }, + { 4096, 0, 9, 1 } /* the q36 o input */ }; + for (size_t i = 0; i < sizeof qc / sizeof qc[0]; i++) { + size_t bad = 0; const int rc = sim_quant(qc[i].k, qc[i].stride, qc[i].rows, qc[i].x64, &bad, msg, sizeof msg); + printf("sim quant k %u stride %u rows %u %s: %s, %zu differ\n", qc[i].k, qc[i].stride, qc[i].rows, qc[i].x64 ? "2^64 x" : "x", rc ? msg : "ran", bad); + if (rc || bad) fail++; + } + { + omega_gpu_pfq_test_set_mutant(4); + size_t bad = 0; const int rc = sim_quant(2048, 2048 + 68, 9, 1, &bad, msg, sizeof msg); + omega_gpu_pfq_test_set_mutant(env_mutant); + printf("sim quant mutant 4 (c = 0): %s\n", rc ? msg : bad ? "caught" : "NOT CAUGHT"); + if (rc || !bad) fail++; + } + static const uint32_t ca[] = { 130, 0, 1, 64, 65, 7 }, cb[] = { 65, 63 }, cc[] = { 20, 64, 100 }; + static const struct { const char *name; const uint32_t *cnt; uint32_t E, n, k, XR, indexed, ymapped, extra; int x64; } gc[] = { + { "A indexed+ymap+x64", ca, 6, 256, 384, 90, 1, 1, 37, 1 }, + { "B direct+ymap", cb, 2, 256, 256, 0, 0, 1, 5, 0 }, + { "C indexed+x64", cc, 3, 256, 128, 50, 1, 0, 0, 1 }, + }; + for (size_t i = 0; i < sizeof gc / sizeof gc[0]; i++) { + uint32_t M = 0; for (uint32_t e = 0; e < gc[i].E; e++) M += gc[i].cnt[e]; + Prob P; prob_make(&P, gc[i].E, gc[i].n, gc[i].k, gc[i].cnt, gc[i].XR, gc[i].indexed, gc[i].ymapped, M + gc[i].extra); + uint32_t *want = expect_y(&P), *y = malloc((size_t)P.YR * P.n * 4), *y0 = malloc((size_t)P.YR * P.n * 4); + for (int order = 0; order < 2; order++) { + const int rc = sim_gemm(&P, gc[i].x64, order, y, msg, sizeof msg); + const size_t bad = rc ? 0 : ydiff(&P, y, want); + printf("sim gemm %s (M %u n %u k %u, %u y rows) order %d: %s, %zu words differ\n", gc[i].name, P.M, P.n, P.k, P.YR, order, rc ? msg : "ran", bad); + if (rc || bad) fail++; + } + if (gc[i].x64) { /* the same problem with true scales and x64 0: the same bits */ + const int rc = sim_gemm(&P, 0, 0, y0, msg, sizeof msg), rc1 = sim_gemm(&P, 1, 0, y, msg, sizeof msg); + const int same = !rc && !rc1 && !memcmp(y0, y, (size_t)P.YR * P.n * 4); + printf("sim gemm %s: x64 on 2^64 scales == x64 off on true scales: %s\n", gc[i].name, same ? "identical" : "DIFFER"); + if (!same) fail++; + } + if (i == 0) for (int m = 1; m <= 3; m++) { + omega_gpu_pfq_test_set_mutant(m); + const int rc = sim_gemm(&P, 1, 0, y, msg, sizeof msg); + omega_gpu_pfq_test_set_mutant(env_mutant); + const size_t bad = rc ? 0 : ydiff(&P, y, want); + printf("sim gemm mutant %d (%s): %s\n", m, m == 1 ? "tiler row skew" : m == 2 ? "wrong scale block" : "tile i^1", rc ? msg : bad ? "caught" : "NOT CAUGHT"); + if (rc || !bad) fail++; + } + free(want); free(y); free(y0); prob_free(&P); + } + /* the q36 shapes: gate_up (n 1024, k 2048) and down (n 2048, k 512) over 12 tokens x (8 routed of 10 + shared); the dense + * mix|o (direct rows, x64) at k 2048 and k 4096 with n cut to 256 (n only multiplies the grid) */ + static const struct { const char *name; uint32_t T, E, n, k; int down; } q36[] = { + { "q36 gate_up routed+shared", 12, 11, 1024, 2048, 0 }, { "q36 down routed+shared", 12, 11, 2048, 512, 1 }, + { "q36 down 72 tokens (segments > 64 rows, n cut to 256)", 72, 11, 256, 512, 1 }, + }; + for (size_t i = 0; i < sizeof q36 / sizeof q36[0]; i++) { + Prob P; q36_layout(&P, q36[i].T, q36[i].E, q36[i].n, q36[i].k, q36[i].down); + uint32_t *want = expect_y(&P), *y = malloc((size_t)P.YR * P.n * 4); + const int rc = sim_gemm(&P, 1, 0, y, msg, sizeof msg); + const size_t bad = rc ? 0 : ydiff(&P, y, want); + printf("sim gemm %s (M %u n %u k %u, %u y rows): %s, %zu words differ\n", q36[i].name, P.M, P.n, P.k, P.YR, rc ? msg : "ran", bad); + if (rc || bad) fail++; + if (i == 0) { /* negative control: the shared expert's y rows dropped from the map (pointed at routed row 0's slot) */ + for (uint32_t t = 0; t < q36[i].T; t++) P.ymap[q36[i].T * 8 + t] = P.ymap[0]; + const int rc2 = sim_gemm(&P, 1, 0, y, msg, sizeof msg); + const size_t bad2 = rc2 ? 0 : ydiff(&P, y, want); + printf("sim gemm %s negative control (shared rows mis-mapped): %s\n", q36[i].name, rc2 ? msg : bad2 ? "caught" : "NOT CAUGHT"); + if (rc2 || !bad2) fail++; + } + free(want); free(y); prob_free(&P); + } + static const uint32_t cd[] = { 9 }; + static const uint32_t dk[] = { 2048, 4096 }; + for (size_t i = 0; i < 2; i++) { + Prob P; prob_make(&P, 1, 256, dk[i], cd, 0, 0, 0, 0); + uint32_t *want = expect_y(&P), *y = malloc((size_t)P.YR * P.n * 4); + const int rc = sim_gemm(&P, 1, 0, y, msg, sizeof msg); + const size_t bad = rc ? 0 : ydiff(&P, y, want); + printf("sim gemm q36 dense (M %u n %u k %u): %s, %zu words differ\n", P.M, P.n, P.k, rc ? msg : "ran", bad); + if (rc || bad) fail++; + free(want); free(y); prob_free(&P); + } + return fail; +} + +/* ---------------- chip: one Qwen3.6 prefill chunk's routed expert GEMMs ---------------- */ +typedef struct { OmegaGpuMem xf, xq, xs, y, tab, ridx, ymap; OmegaGpuFp8Tensor *wt; uint32_t nt, stride; } Dev; +static void dev_free(Dev *d) { + omega_gpu_fp8_tensor_free(d->wt); omega_gpu_pf_free(&d->xf); omega_gpu_pf_free(&d->xq); omega_gpu_pf_free(&d->xs); + omega_gpu_pf_free(&d->y); omega_gpu_pf_free(&d->tab); omega_gpu_pf_free(&d->ridx); omega_gpu_pf_free(&d->ymap); +} +static size_t r16(size_t b) { return (b + 15) & ~(size_t)15; } +static int dev_setup(const Prob *P, uint32_t stride, Dev *d) { + memset(d, 0, sizeof *d); d->stride = stride; + const size_t XA = (size_t)P->XR + PAD; + int rc = omega_gpu_fp8_tensor_upload(P->E, P->n, P->k, P->w, P->sbf, &d->wt) == 0 ? 0 : OMEGA_GPU_PF_CHIP_FAIL; + float *xf = malloc((size_t)P->XR * stride * 4); + for (size_t r = 0; r < P->XR; r++) for (uint32_t j = 0; j < stride; j++) xf[r * stride + j] = j < P->k ? ldexpf(P->x[r * P->k + j], 64) : 1e30f; + if (!rc) rc = omega_gpu_pf_alloc_dev(r16((size_t)P->XR * stride * 4), &d->xf); + if (!rc) rc = omega_gpu_pf_alloc_dev(r16(XA * P->k), &d->xq); + if (!rc) rc = omega_gpu_pf_alloc_dev(r16(XA * P->KB * 4), &d->xs); + if (!rc) rc = omega_gpu_pf_alloc_dev(r16((size_t)P->YR * P->n * 4), &d->y); + if (!rc) rc = omega_gpu_pf_upload(&d->xf, 0, xf, (size_t)P->XR * stride * 4 & ~(size_t)15); + free(xf); + const uint32_t cap = omega_pfq_tile_cap(P->M, P->E); + if (!rc) rc = omega_gpu_pf_alloc_host((size_t)cap * sizeof(OmegaPfqTile), &d->tab); + if (!rc) rc = omega_gpu_pf_alloc_host((size_t)(P->M + PAD) * 4, &d->ridx); + if (!rc) rc = omega_gpu_pf_alloc_host((size_t)(P->M + PAD) * 4, &d->ymap); + if (rc) return rc; + OmegaPfqTile *seg = malloc(P->E * sizeof *seg); memcpy(seg, P->seg, P->E * sizeof *seg); + for (uint32_t e = 0; e < P->E; e++) if (omega_gpu_fp8_tensor_entry(d->wt, e, &seg[e].w_va, &seg[e].s_va) != 0) rc = OMEGA_GPU_PF_CHIP_FAIL; + d->nt = rc ? 0 : omega_pfq_tiles(seg, P->E, d->tab.cpu, cap); + free(seg); + memcpy(d->ridx.cpu, P->ridx, (size_t)(P->M + PAD) * 4); memcpy(d->ymap.cpu, P->ymap, (size_t)(P->M + PAD) * 4); + omega_gpu_session_lock(); + if (omega_gpu_session_sync_to_gpu(&d->tab) || omega_gpu_session_sync_to_gpu(&d->ridx) || omega_gpu_session_sync_to_gpu(&d->ymap)) rc = OMEGA_GPU_PF_CHIP_FAIL; + omega_gpu_session_unlock(); + return rc ? rc : d->nt ? 0 : OMEGA_GPU_PF_BAD_ARGS; +} +static int dev_record(OmegaGpuCmdList *cl, const Prob *P, const Dev *d) { + const OmegaPfqQuant u = { .k = P->k, .rows = P->XR, .stride = d->stride, .x_va = d->xf.va, .q_va = d->xq.va, .s_va = d->xs.va }; + const OmegaPfqGemm c = { .n = P->n, .k = P->k, .x64 = 1, .ntiles = d->nt, .tiles_va = d->tab.va, .rowidx_va = d->ridx.va, .xq_va = d->xq.va, + .xs_va = d->xs.va, .ymap_va = d->ymap.va, .y_va = d->y.va }; + int rc = omega_gpu_pfq_quant_record_locked(cl, &u); + if (!rc) rc = omega_gpu_pfq_gemm_record_locked(cl, &c); + return rc; +} +/* runs both GEMMs once in one list; ns[0..3] = quant, gemm, quant, gemm GPU time */ +static int chip_run(OmegaGpuCmdList *cl, const Prob *P1, const Dev *d1, const Prob *P2, const Dev *d2, uint64_t ns[4]) { + omega_gpu_session_lock(); + omega_gpu_cmdlist_reset(cl); + int rc = dev_record(cl, P1, d1); + if (!rc) rc = dev_record(cl, P2, d2); + OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); o.stamps = 1; + OmegaGpuCmdListStats st; + if (!rc && omega_gpu_cmdlist_submit(cl, &o, &st) != 0) rc = OMEGA_GPU_PF_CHIP_FAIL; + uint64_t ts[5] = { 0 }; + if (!rc && omega_gpu_cmdlist_stamps(cl, ts, 5) != 5) rc = OMEGA_GPU_PF_CHIP_FAIL; + omega_gpu_session_unlock(); + for (int i = 0; i < 4; i++) ns[i] = ts[i + 1] - ts[i]; + return rc; +} +/* quantiser and gemm outputs against the reference: xq/xs whole, y on sampled routed rows plus every untouched row */ +static size_t chip_check(const Prob *P, const Dev *d, int rows, size_t *qbad) { + const size_t XR = P->XR, yb = (size_t)P->YR * P->n * 4; + uint8_t *xq = malloc(r16(XR * P->k)); float *xs = malloc(r16(XR * P->KB * 4)); uint32_t *y = malloc(r16(yb)); + *qbad = 0; size_t bad = 0; + if (omega_gpu_pf_download(&d->xq, 0, xq, r16(XR * P->k)) || omega_gpu_pf_download(&d->xs, 0, xs, r16(XR * P->KB * 4)) || omega_gpu_pf_download(&d->y, 0, y, r16(yb))) { *qbad = 1; bad = 1; } + else { + for (size_t i = 0; i < XR * P->k; i++) if (xq[i] != P->xq[i]) (*qbad)++; + for (size_t i = 0; i < XR * P->KB; i++) if (f2u(xs[i]) != f2u(P->xs64[i])) (*qbad)++; + uint8_t *hit = calloc(P->YR, 1); for (uint32_t p = 0; p < P->M; p++) hit[P->ymap[p]] = 1; + for (uint32_t r = 0; r < P->YR; r++) if (!hit[r]) for (uint32_t c = 0; c < P->n; c++) if (y[(size_t)r * P->n + c] != 0) bad++; + free(hit); + for (int q = 0; q < rows; q++) { + const uint32_t p = q == 0 ? 0 : q == 1 ? P->M - 1 : rnd() % P->M; + for (uint32_t c = 0; c < P->n; c++) if (y[(size_t)P->ymap[p] * P->n + c] != f2u(qref(P, p, c))) bad++; + } + } + free(xq); free(xs); free(y); + return bad; +} +static int chip(uint32_t tokens, int reps, int rows, uint32_t experts) { + const uint32_t E = experts, T = tokens, NG = 9, R = T * NG; /* 8 routed + the shared slot per token, as the q36 graph lays out actN */ + uint32_t *cnt = calloc(E, 4), *tok_e = malloc((size_t)T * 8 * 4); + for (uint32_t t = 0; t < T; t++) for (uint32_t s = 0; s < 8; s++) { + uint32_t e; int dup; + do { e = rnd() % E; dup = 0; for (uint32_t j = 0; j < s; j++) if (tok_e[t * 8 + j] == e) dup = 1; } while (dup); + tok_e[t * 8 + s] = e; cnt[e]++; + } + uint32_t touched = 0; for (uint32_t e = 0; e < E; e++) touched += cnt[e] != 0; + Prob P1, P2; + prob_make(&P1, E, 1024, 2048, cnt, T, 1, 1, R); /* gate|up: x' rows (2^64), routed by token, out to gu row t * 9 + s */ + prob_make(&P2, E, 2048, 512, cnt, R, 1, 1, R); /* down: act rows t * 9 + s in and out */ + { /* the q36 graph's maps (pf_group): rows grouped by expert, xmap = token, ymap = token * 9 + slot */ + uint32_t *fill = calloc(E, 4), *base = calloc(E, 4); + for (uint32_t e = 1; e < E; e++) base[e] = base[e - 1] + cnt[e - 1]; + for (uint32_t t = 0; t < T; t++) for (uint32_t s = 0; s < 8; s++) { + const uint32_t e = tok_e[t * 8 + s], p = base[e] + fill[e]++; + P1.ridx[p] = t; P1.ymap[p] = t * NG + s; P2.ridx[p] = t * NG + s; P2.ymap[p] = t * NG + s; + } + free(fill); free(base); + } + Dev d1, d2; int fail = 0; + int rc = dev_setup(&P1, 2048 + 64, &d1); + if (!rc) rc = dev_setup(&P2, 0, &d2); + OmegaGpuCmdList *cl = NULL; + if (!rc && omega_gpu_cmdlist_create(8, &cl) != 0) rc = OMEGA_GPU_PF_CHIP_FAIL; + if (!rc) { + omega_gpu_pfq_quant_prepare(2048, 2048 + 64); omega_gpu_pfq_quant_prepare(512, 0); + omega_gpu_pfq_gemm_prepare(1024, 2048, 1, 1, 1); omega_gpu_pfq_gemm_prepare(2048, 512, 1, 1, 1); + } + uint64_t ns[4][32]; if (reps > 32) reps = 32; if (reps < 1) reps = 1; + for (int r = 0; r < reps && !rc; r++) { uint64_t t4[4]; rc = chip_run(cl, &P1, &d1, &P2, &d2, t4); for (int i = 0; i < 4; i++) ns[i][r] = t4[i]; } + if (rc) { printf("chip setup/run: %s (%s)\n", omega_gpu_pf_rc_name(rc), omega_gpu_session_last_error()); fail++; } + else { + size_t q1, q2; const size_t b1 = chip_check(&P1, &d1, rows, &q1), b2 = chip_check(&P2, &d2, rows, &q2); + printf("chip gate_up (T %u, M %u, %u experts touched): quantiser %zu differ, y %zu words differ on %d sampled rows + untouched rows\n", T, P1.M, touched, q1, b1, rows); + printf("chip down (R %u rows): quantiser %zu differ, y %zu words differ\n", R, q2, b2); + if (q1 || q2 || b1 || b2) fail++; + static const char *nm[4] = { "quant x' k 2048", "gemm gate_up n 1024 k 2048", "quant act k 512", "gemm down n 2048 k 512" }; + const double fl[4] = { 0, 2.0 * P1.M * 1024 * 2048, 0, 2.0 * P2.M * 2048 * 512 }, wb[4] = { 0, (double)touched * 1024 * 2048, 0, (double)touched * 2048 * 512 }; + for (int i = 0; i < 4; i++) { + qsort(ns[i], reps, 8, cmp_u64); + const double med = (double)ns[i][reps / 2]; + printf("PFQ %-28s median %.3f us (min %.3f)", nm[i], med / 1e3, ns[i][0] / 1e3); + if (fl[i] > 0) printf(", %.2f TFLOPS, %.1f GB/s of distinct weights", fl[i] / med / 1e3, wb[i] / med); + printf("\n"); + } + for (int m = 1; m <= 4; m++) { + omega_gpu_pfq_test_set_mutant(m); + uint64_t t4[4]; int mrc = 0; + if (m == 1) { /* the tiler mutant changes the table: rebuild it */ + OmegaPfqTile *seg = malloc(E * sizeof *seg); memcpy(seg, P1.seg, E * sizeof *seg); + for (uint32_t e = 0; e < E; e++) (void)omega_gpu_fp8_tensor_entry(d1.wt, e, &seg[e].w_va, &seg[e].s_va); + d1.nt = omega_pfq_tiles(seg, E, d1.tab.cpu, omega_pfq_tile_cap(P1.M, E)); free(seg); + omega_gpu_session_lock(); (void)omega_gpu_session_sync_to_gpu(&d1.tab); omega_gpu_session_unlock(); + } + (void)omega_gpu_pf_upload(&d1.y, 0, calloc(1, r16((size_t)P1.YR * P1.n * 4)), r16((size_t)P1.YR * P1.n * 4)); + mrc = chip_run(cl, &P1, &d1, &P2, &d2, t4); + omega_gpu_pfq_test_set_mutant(env_mutant); + size_t mq1 = 0, mq2 = 0; const size_t mb = mrc ? 0 : chip_check(&P1, &d1, rows > 8 ? 8 : rows, &mq1) + chip_check(&P2, &d2, rows > 8 ? 8 : rows, &mq2); + printf("chip mutant %d: %s\n", m, mrc ? "run failed" : (mb || mq1 || mq2) ? "caught" : "NOT CAUGHT"); + if (mrc || !(mb || mq1 || mq2)) fail++; + if (m == 1) { + OmegaPfqTile *seg = malloc(E * sizeof *seg); memcpy(seg, P1.seg, E * sizeof *seg); + for (uint32_t e = 0; e < E; e++) (void)omega_gpu_fp8_tensor_entry(d1.wt, e, &seg[e].w_va, &seg[e].s_va); + d1.nt = omega_pfq_tiles(seg, E, d1.tab.cpu, omega_pfq_tile_cap(P1.M, E)); free(seg); + omega_gpu_session_lock(); (void)omega_gpu_session_sync_to_gpu(&d1.tab); omega_gpu_session_unlock(); + } + } + } + if (cl) omega_gpu_cmdlist_free(cl); + dev_free(&d1); dev_free(&d2); prob_free(&P1); prob_free(&P2); free(cnt); free(tok_e); + return fail; +} + +int main(int argc, char **argv) { + int host = 1, simm = 1, chipm = 0, reps = 9, rows = 32; uint32_t tokens = 128, experts = 256; + for (int i = 1; i < argc; i++) { + if (!strcmp(argv[i], "--host-only")) simm = 0; + else if (!strcmp(argv[i], "--sim")) host = 0; + else if (!strcmp(argv[i], "--chip")) { chipm = 1; host = 0; simm = 0; } + else if (!strcmp(argv[i], "--tokens") && i + 1 < argc) tokens = (uint32_t)atoi(argv[++i]); + else if (!strcmp(argv[i], "--reps") && i + 1 < argc) reps = atoi(argv[++i]); + else if (!strcmp(argv[i], "--rows") && i + 1 < argc) rows = atoi(argv[++i]); + else if (!strcmp(argv[i], "--experts") && i + 1 < argc) experts = (uint32_t)atoi(argv[++i]); + else { fprintf(stderr, "GPU_PFQ: INVALID argument %s\n", argv[i]); return 2; } + } + if (tokens == 0 || tokens > 4096 || experts < 8 || experts > 256 || rows < 2) { fprintf(stderr, "GPU_PFQ: INVALID --tokens/--experts/--rows\n"); return 2; } + env_mutant = omega_gpu_pfq_mutant(); + printf("AIEN_OMEGA_PFQ_MUTANT=%d%s\n", env_mutant, env_mutant ? " (every planned launch is the mutant: clean cases must FAIL)" : ""); + int fail = 0, cases = 0; + if (host) { fail += host_tiler(); fail += host_words(); fail += host_plans(); fail += host_ref(); cases++; } + if (simm) { fail += sim(); cases++; } + if (chipm) { fail += chip(tokens, reps, rows, experts); cases++; } + if (!cases) { fprintf(stderr, "GPU_PFQ: NOT_RUN (no mode selected)\n"); return 2; } + printf("GPU_PFQ: %s\n", fail ? "FAIL" : "PASS"); + return fail ? 1 : 0; +} diff --git a/tests/gpu_q36_pfchunk_test.c b/tests/gpu_q36_pfchunk_test.c new file mode 100644 index 00000000..e9984318 --- /dev/null +++ b/tests/gpu_q36_pfchunk_test.c @@ -0,0 +1,377 @@ +/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ +/* DRA-83 (P-DRA-4 G2) host test of the prefill chunk: the row bound split (legacy 128, tensor-core 8192), the + * AIEN_OMEGA_Q36_PF_CHUNK knob, the buffer layout bytes (Wolfram-computed constants, with and without the DRA-82 QMMA + * regions), a refused allocation, the per-chunk streamed count (33,617,346,560 B for a 40-layer chunk touching every + * expert), TEST mutant 7 (one chunk's expert segment skipped: red) against the baseline (green), and the grid-y split of + * the expert row launches on the legacy and the QMMA expert path. + * Host only: the canonical graph source is compiled in (with omega_gpu_pfq_api.c and the QMMA code generator's spec + * checks linked), the session / command-list / GEMM / prefill-kernel entry points it reaches are stubbed below, function + * sections + linker GC drop the rest. No device node or GPU library is opened. + * Build with -DPFCHUNK_BASE against a tree without DRA-83 (the negative control): the same checks then run on the old + * entry points and must fail. */ +#define _GNU_SOURCE 1 +#include +#include +#include "../src/omega_gpu_q36_graph.c" +#include +#include "../src/omega_gpu_prefill.h" + +static int fails, checks; +#define CHECK(c, ...) do { checks++; if (!(c)) { fails++; printf("FAIL %s:%d ", __FILE__, __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0) + +/* ---- stubs: the session, command list and GEMM entry points the included graph reaches ---- */ +static int st_alloc, st_free, st_refuse_dev, st_last_ok_n; +static size_t st_host_bytes, st_dev_bytes; +void omega_gpu_session_lock(void) {} +void omega_gpu_session_unlock(void) {} +OmegaGpuSession *omega_gpu_session_open(void) { static char s[64]; return (OmegaGpuSession *)(void *)s; } +static char st_err[256]; +void omega_gpu_session_set_error(const char *e) { snprintf(st_err, sizeof st_err, "%s", e ? e : ""); } +static int st_do_alloc(size_t b, OmegaGpuMem *m, int dev) { + if (dev && st_refuse_dev) return -1; + memset(m, 0, sizeof *m); + m->cpu = calloc(1, b ? b : 1); m->va = 0x100000000ull + (uint64_t)st_alloc * 0x10000000ull; m->size = b; + st_alloc++; st_last_ok_n++; + if (dev) st_dev_bytes = b; else st_host_bytes = b; + return m->cpu ? 0 : -1; +} +static int st_dev_next; +int omega_gpu_session_alloc(size_t b, OmegaGpuMem *m) { const int dev = st_dev_next; st_dev_next = !st_dev_next; return st_do_alloc(b, m, dev); } +int omega_gpu_session_alloc_cached(size_t b, OmegaGpuMem *m) { st_dev_next = 0; return st_do_alloc(b, m, 1); } +void omega_gpu_session_free(OmegaGpuMem *m) { if (m && m->cpu) { free(m->cpu); st_free++; } if (m) memset(m, 0, sizeof *m); } +uint32_t omega_native_revision(uint64_t *next, uint64_t cur) { *next = cur + 1u; return OMEGA_OK; } +int omega_gpu_fp8gemm_prepare_shape(uint32_t n, uint32_t a, uint32_t b, int c) { (void)n; (void)a; (void)b; (void)c; return 0; } +int omega_gpu_router_prepare_rows(OmegaGpuRouter *r, uint32_t xs, uint32_t lbs) { (void)r; (void)xs; (void)lbs; return OMEGA_GPU_ROUTER_OK; } +void omega_gpu_decode_attn256_test_set_multi_mutant(int on) { (void)on; } +int omega_gpu_fp8_tensor_entry(const OmegaGpuFp8Tensor *t, uint32_t expert, uint64_t *w, uint64_t *s) { + (void)t; *w = (uint64_t)expert * 256u + 4096u; *s = *w + 128u; return 0; +} +/* the record path of pf_record_experts: every launch is captured (pf = 1: a QMMA quant/gemm launch) */ +typedef struct { uint32_t gx, gy; int pf; uint64_t a0, a1; } Cap; +static Cap cap_l[4096]; static uint32_t cap_n; +static void cap_add(uint32_t gx, uint32_t gy, int pf, const uint32_t *a) { + if (cap_n < 4096) { cap_l[cap_n] = (Cap){ .gx = gx, .gy = gy, .pf = pf, .a0 = (uint64_t)a[0] | (uint64_t)a[1] << 32, + .a1 = (uint64_t)a[2] | (uint64_t)a[3] << 32 }; } + cap_n++; +} +int omega_gpu_cmdlist_add(OmegaGpuCmdList *cl, const OmegaGpuLaunch *L) { (void)cl; cap_add(L->grid_x, L->grid_y, 0, L->args); return 0; } +uint32_t omega_gpu_cmdlist_count(const OmegaGpuCmdList *cl) { (void)cl; return cap_n; } +int omega_gpu_pf_open_locked(void) { return OMEGA_GPU_PF_OK; } +int omega_gpu_pf_kernel_locked(const OmegaPfKey *key, OmegaPfGen gen, OmegaPfKernel *out) { (void)key; (void)gen; memset(out, 0, sizeof *out); return OMEGA_GPU_PF_OK; } +int omega_gpu_pf_record_locked(struct OmegaGpuCmdList *cl, const OmegaPfKernel *k, uint32_t threads, uint32_t grid_x, uint32_t grid_y, + uint32_t shared_bytes, const uint32_t *args, uint32_t n_args) { + (void)cl; (void)k; (void)threads; (void)shared_bytes; (void)n_args; cap_add(grid_x, grid_y, 1, args); return OMEGA_GPU_PF_OK; +} +int omega_gpu_fp8gemm_record(struct OmegaGpuCmdList *cl, const OmegaGpuFp8Tensor *t, uint32_t rows, uint32_t segs, uint64_t seg, uint64_t xp, uint64_t y, + uint64_t xm, uint64_t ym, uint32_t mt, uint32_t f, OmegaGpuFp8GemmInfo *p) { + (void)cl; (void)t; (void)rows; (void)segs; (void)seg; (void)xp; (void)y; (void)xm; (void)ym; (void)mt; (void)f; (void)p; return 0; +} + +/* ---- helpers ---- */ +#define LBS 16896u /* al(16800): the 35B router layout (shared slot) */ +#define IDS_OFF 16384u +static void env(const char *k, const char *v) { if (v) setenv(k, v, 1); else unsetenv(k); } +static void env_clear(void) { + const char *k[] = { "AIEN_OMEGA_Q36_PF_QMMA", "AIEN_OMEGA_Q36_PF_CHUNK", "AIEN_OMEGA_Q36_PF_GDN", "AIEN_OMEGA_Q36_PF_ATTN", + "AIEN_OMEGA_Q36_PF_MUTANT", "AIEN_OMEGA_Q36_CN_ROWS", "AIEN_OMEGA_Q36_BUF_CACHED" }; + for (unsigned i = 0; i < sizeof k / sizeof *k; i++) unsetenv(k[i]); + g_cn_rows = -1; +} +static OmegaGpuQ36Graph *graph_new(void) { + OmegaGpuQ36Graph *g = calloc(1, sizeof *g); + g->lo.lb_stride = LBS; g->cfg.layers = 40; + g->pf.gdn_multi = g->pf.attn_multi = 1; /* prefill_alloc's defaults: o on the QMMA path in every layer */ + for (uint32_t l = 0; l < 40; l++) g->layer[l].attn = (l % 4u) == 3u; /* the 35B: full attention every 4th layer */ + return g; +} +static void graph_free(OmegaGpuQ36Graph *g) { omega_gpu_session_free(&g->pf.hb); omega_gpu_session_free(&g->pf.db); free(g); } +/* host-only pf buffers for pf_group (no session): the QMMA layout (a superset of the legacy one) */ +static void pf_host(OmegaGpuQ36Graph *g, uint32_t P) { +#ifndef PFCHUNK_BASE + size_t ho = 0, dof = 0; + const uint32_t tcg = omega_pfq_tile_cap(P * NG, 257u), tcd = omega_pfq_tile_cap(P, 1u); + pf_layout(g, P, LBS, 1, 40u, tcg, tcd, &ho, &dof); + g->pf.hb.cpu = calloc(1, ho); g->pf.cap = P; g->pf.qtg_cap = tcg; g->pf.qtd_cap = tcd; +#else + /* the base tree has no layout helper: its prefill_alloc, which refuses more than 128 rows */ + const int rc = omega_gpu_q36_graph_prefill_alloc(g, P); + CHECK(rc == OMEGA_GPU_DGRAPH_OK, "base prefill_alloc(%u) rc %d", P, rc); + if (rc) (void)omega_gpu_q36_graph_prefill_alloc(g, 128u); + g->pf.mutant = 0; +#endif +} +static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s >> 8; } +/* 8 distinct routed picks per token; full = 1 makes every expert appear (token t also picks t % 256) */ +static void picks(OmegaGpuQ36Graph *g, uint32_t n, uint32_t seed, int full) { + uint32_t s = seed; + for (uint32_t t = 0; t < n; t++) { + uint32_t *id = pf_hc(g, g->pf.rlb + (size_t)t * LBS + IDS_OFF); + for (uint32_t j = 0; j < 8; j++) { + uint32_t e; + for (;;) { + e = (full && j == 0) ? t % 256u : lcg(&s) % 256u; + int dup = 0; + for (uint32_t k = 0; k < j; k++) dup |= id[k] == e; + if (!dup) break; + full = full && j != 0; + } + id[j] = e; + } + } +} +/* The MoE rows the segment tables compute: y[ymap[p]] = f(expert, xmap[p]) for each segment's rows (the shared expert + * from the second table); then every (token, slot) row must hold f(its pick, token). Returns the rows that do not. */ +static uint32_t moe_rows_wrong(OmegaGpuQ36Graph *g, uint32_t n, uint32_t segs) { + const uint32_t R = n * NG; + uint32_t *y = malloc(R * sizeof *y); + for (uint32_t r = 0; r < R; r++) y[r] = 0xffffffffu; + const Seg32 *sg = pf_hc(g, g->pf.sgu), *sd = pf_hc(g, g->pf.sdn); + const uint32_t *xm = pf_hc(g, g->pf.xmap), *ym = pf_hc(g, g->pf.ymap); + for (uint32_t j = 0; j < segs; j++) { + const uint32_t e = (uint32_t)((sg[j].w_va - 4096u) / 256u); + if (sd[j].w_va != sg[j].w_va || sd[j].cnt != sg[j].cnt) { free(y); return R; } + for (uint32_t p = sg[j].row0; p < sg[j].row0 + sg[j].cnt; p++) y[ym[p]] = e * 65536u + xm[p]; + } + const Seg32 *g2 = pf_hc(g, g->pf.sgu2); + const uint32_t *xm2 = pf_hc(g, g->pf.xmap2), *ym2 = pf_hc(g, g->pf.ymap2); + for (uint32_t p = 0; p < g2->cnt; p++) y[ym2[p]] = 256u * 65536u + xm2[p]; + uint32_t wrong = 0; + for (uint32_t t = 0; t < n; t++) { + const uint32_t *id = pf_hc(g, g->pf.rlb + (size_t)t * LBS + IDS_OFF); + for (uint32_t s = 0; s < 8; s++) wrong += y[t * NG + s] != id[s] * 65536u + t; + wrong += y[t * NG + 8] != 256u * 65536u + t; + } + free(y); + return wrong; +} + +#define EX ((uint64_t)3145728u) /* Wolfram: 3*512*2048 */ +#define G2_CHUNK ((uint64_t)33617346560ull) /* Wolfram: 40*257*3*512*2048 + 2048*(30*(12288+4096)+10*(9216+4096)) */ + +#ifndef PFCHUNK_BASE +#define PF_GROUP(g, l, n, skip) pf_group(g, l, n, IDS_OFF, skip) +#define SKIP(g, l, pos0) pf_skip_segment(g, l, pos0) +#else +/* the base tree: pf_group has no skip argument and no mutant 7; the chunk knob and the TC bound do not exist */ +#define PF_GROUP(g, l, n, skip) ((void)(skip), pf_group(g, l, n, IDS_OFF)) +#define SKIP(g, l, pos0) ((g)->pf.mutant == 7 && (pos0) == 0 && (l) == 0) +#define pf_count_dense(g, l, n) ((g)->pf.streamed += (uint64_t)(((n) + 7u) / 8u) * H * (((g)->layer[l].attn ? OMEGA_GPU_Q36_ATT_MIX : OMEGA_GPU_Q36_GDN_MIX) + OMEGA_GPU_Q36_MIXER_OUT)) +#endif + +/* One chunk through the host half of the prefill (dense count + GROUP per layer) for 40 layers: the streamed bytes and the + * MoE rows the tables leave wrong. The G2 gate: bytes == G2_CHUNK when every expert is touched, and 0 wrong rows. */ +static void chunk(OmegaGpuQ36Graph *g, uint32_t n, uint32_t pos0, uint64_t *bytes, uint32_t *wrong) { + const uint64_t s0 = g->pf.streamed; + *wrong = 0; + for (uint32_t l = 0; l < 40; l++) { + picks(g, n, 1000u + l, 1); + pf_count_dense(g, l, n); + const uint32_t segs = PF_GROUP(g, l, n, SKIP(g, l, pos0)); + *wrong += moe_rows_wrong(g, n, segs); + } + *bytes = g->pf.streamed - s0; +} + +static void test_bound_and_knob(void) { +#ifndef PFCHUNK_BASE + uint32_t c = 0; + env_clear(); + CHECK(omega_gpu_q36_graph_prefill_rows_max() == 128u, "legacy bound %u", omega_gpu_q36_graph_prefill_rows_max()); + CHECK(omega_gpu_q36_graph_prefill_chunk(&c) == OMEGA_GPU_DGRAPH_OK && c == 128u, "default chunk %u", c); + env("AIEN_OMEGA_Q36_PF_CHUNK", "2048"); c = 7; + CHECK(omega_gpu_q36_graph_prefill_chunk(&c) == OMEGA_GPU_DGRAPH_BAD_ARGS && c == 7, "2048 refused without QMMA"); + env("AIEN_OMEGA_Q36_PF_CHUNK", "64"); + CHECK(omega_gpu_q36_graph_prefill_chunk(&c) == OMEGA_GPU_DGRAPH_OK && c == 64u, "64 legacy"); + env("AIEN_OMEGA_Q36_PF_QMMA", "1"); + CHECK(omega_gpu_q36_graph_prefill_rows_max() == 8192u, "TC bound"); + const char *ok[] = { "1", "128", "2048", "4096", "8192" }; const uint32_t okv[] = { 1, 128, 2048, 4096, 8192 }; + for (unsigned i = 0; i < 5; i++) { + env("AIEN_OMEGA_Q36_PF_CHUNK", ok[i]); c = 0; + CHECK(omega_gpu_q36_graph_prefill_chunk(&c) == OMEGA_GPU_DGRAPH_OK && c == okv[i], "chunk %s -> %u", ok[i], c); + } + const char *bad[] = { "8193", "0", "", "x", "12x", "-5", "99999999999", " 64" }; + for (unsigned i = 0; i < 8; i++) { + env("AIEN_OMEGA_Q36_PF_CHUNK", bad[i]); c = 7; + CHECK(omega_gpu_q36_graph_prefill_chunk(&c) == OMEGA_GPU_DGRAPH_BAD_ARGS && c == 7, "chunk '%s' must be refused", bad[i]); + } + const char *qbad[] = { "0", "10", "yes", "" }; + for (unsigned i = 0; i < 4; i++) { env("AIEN_OMEGA_Q36_PF_QMMA", qbad[i]); CHECK(omega_gpu_q36_graph_prefill_rows_max() == 128u, "QMMA '%s' is off", qbad[i]); } + CHECK(omega_gpu_q36_graph_prefill_chunk(NULL) == OMEGA_GPU_DGRAPH_BAD_ARGS, "NULL chunk"); + printf("bound+knob: legacy 128, TC 8192, chunk default 128, 5 accepted, 8 refused, 4 QMMA spellings off\n"); +#else + printf("bound+knob: not in the base tree (prefill_rows_max / prefill_chunk absent)\n"); + checks++; fails++; +#endif +} + +static void test_layout_and_alloc(void) { + /* Wolfram (layout of pf_layout, lbs 16896, al = 256-byte rounding): {cap, host, device} without QMMA; qx the DRA-82 QMMA + * regions added with AIEN_OMEGA_Q36_PF_QMMA=1: 2 al((9 cap + 64) 4) + 2 al(tcg 32) + al(40 2 tcd 32) host, + * al(4608 cap) + al(4608 cap / 128 4) device, tcg = 257 + 9 cap / 64, tcd = 1 + cap / 64 */ + static const uint64_t want[4][3] = { { 128, 3239424ull, 38584320ull }, { 2048, 51561984ull, 613171200ull }, + { 4096, 103106048ull, 1226063872ull }, { 8192, 206194176ull, 2451849216ull } }; + static const uint64_t qx[4][2] = { { 35328ull, 608256ull }, { 267776ull, 9732096ull }, { 515584ull, 19464192ull }, { 1011200ull, 38928384ull } }; +#ifndef PFCHUNK_BASE + env_clear(); + for (unsigned i = 0; i < 4; i++) { + uint64_t hb = 0, db = 0; + const int rc = omega_gpu_q36_graph_prefill_bytes((uint32_t)want[i][0], LBS, 40u, &hb, &db); + CHECK(i == 0 ? rc == OMEGA_GPU_DGRAPH_OK && hb == want[i][1] && db == want[i][2] : rc == OMEGA_GPU_DGRAPH_BAD_ARGS, + "layout cap %llu without QMMA: rc %d host %llu dev %llu", (unsigned long long)want[i][0], rc, (unsigned long long)hb, (unsigned long long)db); + } + env("AIEN_OMEGA_Q36_PF_QMMA", "1"); + for (unsigned i = 0; i < 4; i++) { + uint64_t hb = 0, db = 0; + CHECK(omega_gpu_q36_graph_prefill_bytes((uint32_t)want[i][0], LBS, 40u, &hb, &db) == OMEGA_GPU_DGRAPH_OK && hb == want[i][1] + qx[i][0] + && db == want[i][2] + qx[i][1], "layout cap %llu QMMA: host %llu dev %llu", (unsigned long long)want[i][0], (unsigned long long)hb, + (unsigned long long)db); + } + uint64_t hb = 0, db = 0; + CHECK(omega_gpu_q36_graph_prefill_bytes(8193u, LBS, 40u, &hb, &db) == OMEGA_GPU_DGRAPH_BAD_ARGS, "bytes 8193"); + CHECK(omega_gpu_q36_graph_prefill_bytes(0u, LBS, 40u, &hb, &db) == OMEGA_GPU_DGRAPH_BAD_ARGS, "bytes 0"); +#endif + env_clear(); + OmegaGpuQ36Graph *g = graph_new(); + st_alloc = st_free = 0; + CHECK(omega_gpu_q36_graph_prefill_alloc(g, 129u) == OMEGA_GPU_DGRAPH_BAD_ARGS && st_alloc == 0, "129 without QMMA"); + st_host_bytes = st_dev_bytes = 0; + CHECK(omega_gpu_q36_graph_prefill_alloc(g, 128u) == OMEGA_GPU_DGRAPH_OK && omega_gpu_q36_graph_prefill_cap(g) == 128u, "128 legacy"); + CHECK(st_host_bytes == want[0][1] && st_dev_bytes == want[0][2], "128 asked host %zu dev %zu", st_host_bytes, st_dev_bytes); + env("AIEN_OMEGA_Q36_PF_QMMA", "1"); + st_host_bytes = st_dev_bytes = 0; + CHECK(omega_gpu_q36_graph_prefill_alloc(g, 8192u) == OMEGA_GPU_DGRAPH_OK && omega_gpu_q36_graph_prefill_cap(g) == 8192u, "8192 with QMMA"); + CHECK(st_host_bytes == want[3][1] + qx[3][0] && st_dev_bytes == want[3][2] + qx[3][1], "8192 asked host %zu dev %zu", st_host_bytes, st_dev_bytes); + CHECK(st_free == 2, "the 128 buffers freed on growth (%d)", st_free); +#ifndef PFCHUNK_BASE + CHECK(g->pf.qmma == 1, "qmma latched"); +#endif + CHECK(omega_gpu_q36_graph_prefill_alloc(g, 8193u) == OMEGA_GPU_DGRAPH_BAD_ARGS, "8193 with QMMA"); + graph_free(g); + /* above 128 the per-token fallbacks and the native MoE are refused; at 128 they stay allowed */ + const char *fk[] = { "AIEN_OMEGA_Q36_PF_GDN", "AIEN_OMEGA_Q36_PF_ATTN", "AIEN_OMEGA_Q36_CN_ROWS", "AIEN_OMEGA_Q36_PF_MUTANT", "AIEN_OMEGA_Q36_PF_MUTANT" }; + const char *fv[] = { "0", "0", "0", "1", "3" }; + for (unsigned i = 0; i < 5; i++) { + env_clear(); env("AIEN_OMEGA_Q36_PF_QMMA", "1"); env(fk[i], fv[i]); + g = graph_new(); st_alloc = 0; + CHECK(omega_gpu_q36_graph_prefill_alloc(g, 4096u) == OMEGA_GPU_DGRAPH_BAD_ARGS && st_alloc == 0, "4096 with %s=%s refused", fk[i], fv[i]); + CHECK(omega_gpu_q36_graph_prefill_alloc(g, 128u) == OMEGA_GPU_DGRAPH_OK, "128 with %s=%s allowed", fk[i], fv[i]); + graph_free(g); + } + env_clear(); env("AIEN_OMEGA_Q36_PF_QMMA", "1"); + g = graph_new(); g->native = (OmegaGpuQ36NativeGraph *)(uintptr_t)1; + CHECK(omega_gpu_q36_graph_prefill_alloc(g, 4096u) == OMEGA_GPU_DGRAPH_BAD_ARGS, "4096 refused with the native MoE"); + g->native = NULL; graph_free(g); + /* the session refuses the device buffer: CHIP_FAIL, cap 0, nothing left allocated */ + g = graph_new(); st_alloc = st_free = 0; st_refuse_dev = 1; + const int rc = omega_gpu_q36_graph_prefill_alloc(g, 4096u); + st_refuse_dev = 0; + CHECK(rc == OMEGA_GPU_DGRAPH_CHIP_FAIL && omega_gpu_q36_graph_prefill_cap(g) == 0 && !g->pf.hb.cpu && !g->pf.db.cpu && st_alloc == st_free, + "refused allocation: rc %d cap %u hb %p db %p alloc %d free %d", rc, omega_gpu_q36_graph_prefill_cap(g), g->pf.hb.cpu, g->pf.db.cpu, st_alloc, st_free); + graph_free(g); + env_clear(); + printf("layout+alloc: bytes at 128/2048/4096/8192 match Wolfram; 8192 needs QMMA; fallbacks/native refused above 128; refusal frees\n"); +} + +static void test_streamed_and_mutant(void) { + env_clear(); + OmegaGpuQ36Graph *g = graph_new(); + /* legacy chunk (128 rows): the per-pass count, unchanged */ + pf_host(g, 4096u); + picks(g, 128u, 7u, 0); + uint32_t cnt[256] = {0}; + for (uint32_t t = 0; t < 128u; t++) { const uint32_t *id = pf_hc(g, g->pf.rlb + (size_t)t * LBS + IDS_OFF); for (int s = 0; s < 8; s++) cnt[id[s]]++; } + uint64_t legacy = (uint64_t)((128u + 3u) / 4u) * EX; + for (int e = 0; e < 256; e++) legacy += (uint64_t)((cnt[e] + 3u) / 4u) * EX; + uint64_t s0 = g->pf.streamed; + (void)PF_GROUP(g, 0, 128u, 0); + CHECK(g->pf.streamed - s0 == legacy, "legacy expert count %llu want %llu", (unsigned long long)(g->pf.streamed - s0), (unsigned long long)legacy); +#ifndef PFCHUNK_BASE + CHECK(g->pf.passes == legacy, "legacy passes == streamed"); +#endif + /* the tensor-core chunk: every touched expert once, the shared once, the dense once per layer */ + g->pf.streamed = 0; +#ifndef PFCHUNK_BASE + g->pf.passes = 0; +#endif + const uint32_t N = g->pf.cap; /* 4096 (the base tree: 128) */ +#ifndef PFCHUNK_BASE + g->pf.qmma = 1; +#endif + uint64_t bytes = 0; uint32_t wrong = 0; + chunk(g, N, 0u, &bytes, &wrong); + CHECK(bytes == G2_CHUNK, "G2 chunk bytes %llu want %llu (Wolfram)", (unsigned long long)bytes, (unsigned long long)G2_CHUNK); + CHECK(wrong == 0, "baseline MoE rows wrong %u", wrong); + printf("G2 baseline (4096-token chunk, 40 layers): streamed %llu B (want %llu), wrong rows %u -> %s\n", (unsigned long long)bytes, + (unsigned long long)G2_CHUNK, wrong, bytes == G2_CHUNK && wrong == 0 ? "GREEN" : "RED"); +#ifndef PFCHUNK_BASE + printf(" passes as launched (one weight pass per 64-row QMMA tile): %llu B\n", (unsigned long long)g->pf.passes); +#endif + /* mutant 7: the chunk at pos0 0 loses one expert segment (layer 0, highest id): the gate goes red; a later chunk is clean */ + g->pf.mutant = 7; + CHECK(SKIP(g, 0, 0) && !SKIP(g, 1, 0) && !SKIP(g, 0, 4096), "mutant 7 fires at layer 0 of the pos0-0 chunk only"); + chunk(g, N, 0u, &bytes, &wrong); + const int red = bytes != G2_CHUNK || wrong != 0; + CHECK(red, "mutant 7 must be red (bytes %llu wrong %u)", (unsigned long long)bytes, wrong); + CHECK(bytes == G2_CHUNK - EX && wrong > 0, "mutant 7: one expert short (%llu) and its rows unwritten (%u)", (unsigned long long)(G2_CHUNK - bytes), wrong); + printf("G2 mutant 7: streamed %llu B (short %llu), wrong rows %u -> %s\n", (unsigned long long)bytes, (unsigned long long)(G2_CHUNK - bytes), wrong, + red ? "RED" : "GREEN"); + chunk(g, N, N, &bytes, &wrong); + CHECK(bytes == G2_CHUNK && wrong == 0, "mutant 7 leaves the second chunk alone"); + g->pf.mutant = 0; + free(g->pf.hb.cpu); g->pf.hb.cpu = NULL; free(g); +} + +static void test_grid_split(void) { +#ifndef PFCHUNK_BASE + /* the expert rows of an 8192-token chunk (73728) exceed one launch's grid y: SWIGLU (and on the legacy path the ACT PERM) + * split at 65535 rows, the pieces' row pointers advanced by r0 rows; a 128-token chunk stays one launch each. The QMMA + * path (DRA-82) has no PERM: quant(n x' rows), gemm(gate_up tiles), SWIGLU, quant(R act rows), gemm(down tiles). */ + static const uint32_t ns[4] = { 128u, 8192u, 128u, 8192u }; + for (unsigned i = 0; i < 4; i++) { + const int qm = i >= 2; + env_clear(); env("AIEN_OMEGA_Q36_PF_QMMA", "1"); + OmegaGpuQ36Graph *g = graph_new(); + pf_host(g, ns[i]); + g->pf.qmma = qm; + g->cl = (OmegaGpuCmdList *)(uintptr_t)1; /* a handle the stubbed record calls never dereference (the QMMA record refuses NULL) */ + const uint32_t n = ns[i], R = n * NG; + (void)PF_GROUP(g, 0, n, 0); + cap_n = 0; + const int rc = pf_record_experts(g, 0, 0, n, 1, 0, 0); + uint32_t sw = 0, pa = 0, maxy = 0, rows_sw = 0, rows_pa = 0, pfn = 0; int ptr_ok = 1, pf_ok = 1; + const uint64_t guN = pf_d(g, g->pf.guN), actN = pf_d(g, g->pf.actN), actp = pf_d(g, g->pf.actp); + for (uint32_t k = 0; k < cap_n && k < 4096; k++) { + if (cap_l[k].gy > maxy) maxy = cap_l[k].gy; + if (cap_l[k].pf) { /* quant: grid x = rows; gemm: grid y = the expert tiles */ + const uint32_t want_gx = pfn == 0 ? n : R; + pf_ok &= (pfn & 1u) ? cap_l[k].gy == g->pf.qtg_n : cap_l[k].gx == want_gx; + pfn++; + } else if (cap_l[k].a0 >= guN && cap_l[k].a0 < actN && cap_l[k].gy > n) { /* SWIGLU: gu, act */ + ptr_ok &= cap_l[k].a0 == guN + (uint64_t)rows_sw * 2u * EI * 4u && cap_l[k].a1 == actN + (uint64_t)rows_sw * EI * 4u; + rows_sw += cap_l[k].gy; sw++; + } else if (cap_l[k].a0 >= actN && cap_l[k].a0 < actp && cap_l[k].gy > n) { /* PERM ACT: in, tab */ + ptr_ok &= cap_l[k].a0 == actN + (uint64_t)rows_pa * EI * 4u; + rows_pa += cap_l[k].gy; pa++; + } + } + const uint32_t nsw = (R + 65534u) / 65535u; + CHECK(rc == OMEGA_GPU_DGRAPH_OK && maxy <= 65535u && rows_sw == R && ptr_ok && sw == nsw + && (qm ? pa == 0 && pfn == 4 && pf_ok && g->pf.qtg_n > 0 : rows_pa == R && pa == sw && pfn == 0), + "%s n %u: rc %d maxy %u swiglu %u launches/%u rows, perm %u/%u, ptrs %d, qmma launches %u ok %d", qm ? "qmma" : "legacy", n, rc, maxy, + sw, rows_sw, pa, rows_pa, ptr_ok, pfn, pf_ok); + printf("grid split %s n=%u: R=%u -> swiglu %u launch(es), perm %u launch(es), qmma %u launch(es) (%u expert tiles), max grid y %u\n", + qm ? "qmma" : "legacy", n, R, sw, pa, pfn, g->pf.qtg_n, maxy); + free(g->pf.hb.cpu); free(g); + } + env_clear(); +#endif +} + +int main(void) { + test_bound_and_knob(); + test_layout_and_alloc(); + test_streamed_and_mutant(); + test_grid_split(); + printf("gpu_q36_pfchunk_test: %d/%d checks pass%s\n", checks - fails, checks, fails ? "" : " (PASS)"); + return fails ? 1 : 0; +} diff --git a/tests/gpu_qgemm_test.c b/tests/gpu_qgemm_test.c index daf0e9dc..3e47d327 100644 --- a/tests/gpu_qgemm_test.c +++ b/tests/gpu_qgemm_test.c @@ -72,7 +72,7 @@ static int pq_host(void) { } static int pq_sim_one(uint32_t k, uint32_t in_type, uint32_t groups, uint32_t rows, int mutant, size_t *bad_out, char *msg, size_t msgn) { - OmegaPqSpec sp = { k, in_type, mutant, groups }; + OmegaPqSpec sp = { k, in_type, mutant, groups, 0 }; static BlackwellIRProgram prog; if (omega_blackwell_codegen_pq_prog(&sp, &prog) != 0) { snprintf(msg, msgn, "codegen refused"); return -1; } float *x = malloc((size_t)rows * k * 4); uint16_t *xb = malloc((size_t)rows * k * 2); @@ -339,7 +339,7 @@ static int qg_sim(void) { for (size_t c = 0; c < sizeof cs / sizeof cs[0]; c++) { const uint32_t bm = 16 * cs[c].rt; QProb p; qprob_make(&p, cs[c].E, cs[c].n, cs[c].k, cs[c].M, cs[c].xr, cs[c].layout, cs[c].direct, bm); - OmegaQgSpec sp = { cs[c].k, cs[c].rt, cs[c].warps, cs[c].layout == OMEGA_GPU_PF_PLAIN ? OMEGA_QG_EPI_F32 : OMEGA_QG_EPI_SWIGLU, cs[c].direct, cs[c].n, 45, 0 }; + OmegaQgSpec sp = { cs[c].k, cs[c].rt, cs[c].warps, cs[c].layout == OMEGA_GPU_PF_PLAIN ? OMEGA_QG_EPI_F32 : OMEGA_QG_EPI_SWIGLU, cs[c].direct, cs[c].n, 45, 0, 0, 0 }; uint32_t *rows = malloc(p.M * 4); for (uint32_t i = 0; i < p.M; i++) rows[i] = i; void *y = malloc((size_t)(p.M + bm) * p.n * 4); for (int order = 0; order < 2; order++) { diff --git a/tests/moe/.crumb b/tests/moe/.crumb index afa95484..ddd3c0d3 100644 --- a/tests/moe/.crumb +++ b/tests/moe/.crumb @@ -3,14 +3,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-29T02:02:50Z", + "compiled_at": "2026-10-31T15:02:31Z", "compiler": "crumb-compile/1", - "digest": "sha256:a530bb9ed36c79924ea6964f0a16a2a5c9d31a74601e9ee9d592305604916335", + "digest": "sha256:3f6470620cd95d3ae5338d619efdd312d17f209488475ae4f20eee6c80353d3c", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 6, - "generated_at_commit": "3c08b264e0c4931dbdc494b0d635edf7c912250f", - "source_tree": "sha256:14e853877be4db965553f24e27ebf7f757c4200f8b08a9109e0062aba054ab90" + "generated_at_commit": "b19b84e37c4b74c18b53bd39adf578e7dbdd3e53", + "source_tree": "sha256:fcede0c738fdc1299d0be850d61d821f00237e623478482a123fd88bef8928a8" } }, "name": "moevf", diff --git a/tests/moe/incumbent.c b/tests/moe/incumbent.c index f75d4918..ee3373b6 100644 --- a/tests/moe/incumbent.c +++ b/tests/moe/incumbent.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ /* Verification oracle only: compile the actual incumbent pf_group unchanged. - * Function sections + linker GC exclude the rest of the graph; no GPU object is linked. */ + * Function sections + linker GC exclude the rest of the graph; no GPU object is linked. + * pf_group's QMMA branch calls the real host tiler omega_pfq_tiles; run.sh links src/omega_gpu_pfq_api.c for it. */ #include "../../src/omega_gpu_q36_graph.c" int omega_gpu_fp8_tensor_entry(const OmegaGpuFp8Tensor *t, uint32_t expert, uint64_t *w, uint64_t *s) { (void)t; *w = (uint64_t)expert * 256 + 4096; *s = *w + 128; return 0; @@ -14,7 +15,7 @@ uint32_t moe_incumbent(uint32_t n, const uint32_t *ids, uint32_t *x, uint32_t *y g.pf.xmap = 20544; g.pf.ymap = 24672; g.pf.xmap2 = 28800; g.pf.ymap2 = 29344; memcpy(mem, ids, n * 8 * sizeof *ids); - uint32_t ns = pf_group(&g, 0, n, 0); + uint32_t ns = pf_group(&g, 0, n, 0, 0); memcpy(x, mem + g.pf.xmap, n * 8 * sizeof *x); memcpy(y, mem + g.pf.ymap, n * 8 * sizeof *y); memcpy(x + n * 8, mem + g.pf.xmap2, n * sizeof *x); diff --git a/tests/moe/run.sh b/tests/moe/run.sh index 83e9ad8b..326446df 100755 --- a/tests/moe/run.sh +++ b/tests/moe/run.sh @@ -14,7 +14,8 @@ trap 'rm -rf "$T"' EXIT HUP INT TERM OMEGA_UNITS="om/moe/moe_plan.om om/moe/moe_group.om om/moe/moe_ids32.om om/moe/moe_maps32.om om/moe/moe_segs32.om om/moe/moe_windows.om tests/moe/moe_verify.om" OMEGA_UNITS_DEST="$T" sh "$ROOT/om/units.sh" # The included canonical graph uses the same GNU/POSIX interfaces as the main build. ${CC:-cc} -std=gnu11 -O2 -Wall -Wextra -Werror -ffunction-sections -fdata-sections -I"$ROOT/src" -c "$ROOT/tests/moe/incumbent.c" -o "$T/incumbent.o" -${CC:-cc} -std=c11 -O2 -Wall -Wextra -Werror -I"$T" "$ROOT/tests/moe/caller.c" "$T/cudapp_om_units.c" "$T/incumbent.o" -Wl,--gc-sections -o "$T/moe-test" +${CC:-cc} -std=gnu11 -O2 -Wall -Wextra -Werror -ffunction-sections -fdata-sections -I"$ROOT/src" -c "$ROOT/src/omega_gpu_pfq_api.c" -o "$T/pfq_api.o" +${CC:-cc} -std=c11 -O2 -Wall -Wextra -Werror -I"$T" "$ROOT/tests/moe/caller.c" "$T/cudapp_om_units.c" "$T/incumbent.o" "$T/pfq_api.o" -Wl,--gc-sections -o "$T/moe-test" "$T/moe-test" cat "$QUANT_ROOT/om/quant/quant.om" "$COMPOSE_ROOT/om/components/realize.om" "$CACHE_ROOT/om/cache/digest.om" "$CACHE_ROOT/om/cache/residency.om" "$PIPELINE_ROOT/om/pipeline/select.om" "$PIPELINE_ROOT/om/pipeline/graph.om" "$ROOT/om/moe/moe_group.om" "$ROOT/om/moe/moe_admit.om" "$PIPELINE_ROOT/tests/pipeline/fixture.om" "$ROOT/tests/moe/admit-caller.om" > "$T/admit.om" "$OMEGA_OMC" < "$T/admit.om" > "$T/admit"; chmod +x "$T/admit" diff --git a/tests/pfq_legacy_words.h b/tests/pfq_legacy_words.h new file mode 100644 index 00000000..700b6122 --- /dev/null +++ b/tests/pfq_legacy_words.h @@ -0,0 +1,58 @@ +/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ +/* pfq_legacy_words.h: digest of the encoded words of every qg / pq spec that existed before DRA-82 (the new spec fields + * pq.stride, qg.ymap, qg.x64 left 0). tests/gpu_pfq_test.c compares it with PFQ_LEGACY_DIGEST, the value this same code + * printed when built against CUDApp main 4bbccf0 (evidence/DRA-82-pfq/). Uses only the pre-DRA-82 fields. */ +#ifndef PFQ_LEGACY_WORDS_H +#define PFQ_LEGACY_WORDS_H +#include +#include +#include +#include "omega_blackwell_codegen.h" +#include "omega_blackwell_codegen_qg.h" +#include "omega_blackwell_codegen_tc.h" +#include "omega_blackwell_codegen_qmma.h" + +/* FNV-1a 64 over (program index, code length, code bytes) of every accepted spec, encoded and patched as + * omega_gpu_pf_kernel_locked does. Returns the number of programs, or -1 when one failed to encode. */ +static uint64_t pfq_fnv(uint64_t h, const void *p, size_t n) { + const uint8_t *b = p; for (size_t i = 0; i < n; i++) { h ^= b[i]; h *= 0x100000001b3ull; } return h; +} +static int pfq_encode_hash(BlackwellIRProgram *prog, uint8_t *code, size_t cap, uint64_t *h, uint32_t idx) { + size_t need = omega_bw_program_code_bytes(prog); + OmegaBlackwellKernel kern; + if (need == 0 || need > cap || omega_blackwell_kernel_encode_into(prog, code, need, &kern) != 0) return -1; + if (omega_tc_patch_code(prog, code, need) < 0 || omega_qmma_patch_code(prog, code, need) < 0) return -1; + uint64_t len = need; + *h = pfq_fnv(*h, &idx, sizeof idx); *h = pfq_fnv(*h, &len, sizeof len); *h = pfq_fnv(*h, code, need); + return 0; +} +static int pfq_legacy_digest(uint64_t *out) { + *out = 0; + BlackwellIRProgram *prog = calloc(1, sizeof *prog); + size_t cap = 1u << 20; uint8_t *code = malloc(cap); + uint64_t h = 0xcbf29ce484222325ull; uint32_t idx = 0; int bad = 0; + if (!prog || !code) { free(prog); free(code); return -1; } + static const uint32_t ks[] = { 128, 512, 2048, 4096 }; + for (unsigned a = 0; a < 4; a++) for (uint32_t w = 8; w <= 16; w *= 2) for (uint32_t epi = 0; epi <= 1; epi++) + for (uint32_t dir = 0; dir <= 1; dir++) for (uint32_t wt = 0; wt <= 45; wt += 45) for (int m = 0; m <= 4; m++) { + OmegaQgSpec s; memset(&s, 0, sizeof s); + s.k = ks[a]; s.rt = 4; s.warps = w; s.epi = epi; s.direct = dir; s.n = epi ? 1024u : 512u; s.wait = wt; s.mutant = m; + if (!omega_qg_spec_ok(&s)) continue; + memset(prog, 0, sizeof *prog); + if (omega_blackwell_codegen_qgemm_prog(&s, prog) != 0 || pfq_encode_hash(prog, code, cap, &h, idx) != 0) bad = 1; + idx++; + } + static const uint32_t pk[] = { 128, 512, 2048, 4096, 8192 }; + for (unsigned a = 0; a < 5; a++) for (uint32_t it = 0; it <= 1; it++) for (int m = 0; m <= 3; m++) for (uint32_t gr = 0; gr <= 2; gr++) { + OmegaPqSpec s; memset(&s, 0, sizeof s); + s.k = pk[a]; s.in_type = it; s.mutant = m; s.groups = gr; + if (!omega_pq_spec_ok(&s) || (gr == 0 && pk[a] > 4096u)) continue; + memset(prog, 0, sizeof *prog); + if (omega_blackwell_codegen_pq_prog(&s, prog) != 0 || pfq_encode_hash(prog, code, cap, &h, idx) != 0) bad = 1; + idx++; + } + free(prog); free(code); + *out = h; + return bad ? -1 : (int)idx; +} +#endif