omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / evidence / q38-decode-speed-chip-20261019T0216Z / worktree.diff
| 1 | diff --git a/src/omega_gpu_q38_graph.c b/src/omega_gpu_q38_graph.c |
| 2 | index 3648be4f..d890d826 100644 |
| 3 | --- a/src/omega_gpu_q38_graph.c |
| 4 | +++ b/src/omega_gpu_q38_graph.c |
| 5 | @@ -1,14 +1,16 @@ |
| 6 | /* |
| 7 | * omega_gpu_q38_graph.c: one Qwen3.8-27B decode token as one command list (see the header). Records the FP8 / bf16 GEMV, |
| 8 | * GDN and hd256 attention APIs' *_record launches and the omega_blackwell_codegen_dec glue kernels at width 5120 into an |
| 9 | - * omega_gpu_cmdlist. Buffers: omega_gpu_session_alloc only (GPU-uncached, nvos.h:1117); the host touches x (before), |
| 10 | - * token and logits (after). |
| 11 | + * omega_gpu_cmdlist. Buffers: buf from omega_gpu_session_alloc (GPU-uncached, nvos.h:1117) holds every region the host |
| 12 | + * touches: x (before), token and logits (after), the tables; dbuf (GPU-cached, omega_gpu_mem_cached.h) holds the chip-only |
| 13 | + * intermediates (see Layout). |
| 14 | */ |
| 15 | #include "omega_gpu_q38_graph.h" |
| 16 | #include "omega_gpu_cmdlist.h" |
| 17 | #include "omega_blackwell_codegen_dec.h" |
| 18 | #include "omega_gpu_session.h" |
| 19 | #include "omega_gpu_fmt.h" |
| 20 | +#include "omega_gpu_mem_cached.h" |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | @@ -20,20 +22,26 @@ |
| 25 | #define I OMEGA_GPU_Q38_INTER |
| 26 | |
| 27 | /* glue kernels */ |
| 28 | -enum { K_NORM, K_ADDNORM, K_SW, K_GATHER, K_GATE, K_AM0, K_AM1, K_SW_M1, K_GATHER_M1, K_GATE_M1, K_ADDNORM_M1, K_COUNT }; |
| 29 | +enum { K_NORM, K_ADDNORM, K_SW, K_GATHER, K_GATE, K_AM0, K_AM1, K_SW_M1, K_GATHER_M1, K_GATE_M1, K_ADDNORM_M1, K_COPY, K_COUNT }; |
| 30 | +#define COPY_ROW 256u /* K_COPY: GATHER rows of 256 floats through the identity table */ |
| 31 | |
| 32 | typedef struct { OmegaGpuMem code; uint32_t gpr; } Kern; |
| 33 | |
| 34 | -/* the one buffer: byte offsets, every region 256-byte aligned (RES loads 16-byte vectors) */ |
| 35 | +/* byte offsets into two buffers, every region 256-byte aligned (RES loads 16-byte vectors). [0, split): buf, GPU-uncached, |
| 36 | + * every region the host writes or reads. [split, bytes): dbuf, the chip-only intermediates, GPU-cached unless |
| 37 | + * AIEN_OMEGA_Q38_BUF_CACHED=0 (measured 2026-10-19: every FP8 GEMV CTA reads the whole x' row, and uncached each of those |
| 38 | + * reads goes to DRAM; cached, down 2100 -> 1735 us and mix 1596 -> 1458 us over 4 layers, runs/probe-buf). read_buf copies |
| 39 | + * a dbuf region into stage with a chip launch (K_COPY) before the host reads it. */ |
| 40 | typedef struct { |
| 41 | - size_t x, xq, mix, ab, agg, att, o, gu, act, hidden, logits, am_v, am_i, token, norms, fnorm, tabs, gtab, bytes; |
| 42 | + size_t x, ab, hidden, logits, am_v, am_i, token, norms, fnorm, tabs, gtab, idtab, stage, split; |
| 43 | + size_t xq, mix, agg, att, o, gu, act, bytes; |
| 44 | } Layout; |
| 45 | |
| 46 | struct OmegaGpuQ38Graph { |
| 47 | OmegaGpuQ38GraphConfig cfg; |
| 48 | uint32_t am_ctas; /* stage-1 ARGMAX CTAs = ceil(vocab / 2048) */ |
| 49 | Layout lo; |
| 50 | - OmegaGpuMem buf; |
| 51 | + OmegaGpuMem buf, dbuf; /* the host-visible regions; the chip-only regions (see Layout) */ |
| 52 | Kern k[K_COUNT]; |
| 53 | OmegaGpuCmdList *cl; |
| 54 | struct { const OmegaGpuFp8Tensor *mix, *o, *gu, *dn; const OmegaGpuBf16Tensor *ab; uint32_t kind_index; int attn, set; } layer[OMEGA_GPU_Q38_MAX_LAYERS]; |
| 55 | @@ -53,8 +61,8 @@ struct OmegaGpuQ38Graph { |
| 56 | }; |
| 57 | |
| 58 | static size_t al(size_t v) { return (v + 255u) & ~(size_t)255u; } |
| 59 | -static uint64_t va_of(const OmegaGpuQ38Graph *g, size_t off) { return g->buf.va + off; } |
| 60 | -static void *cpu_of(const OmegaGpuQ38Graph *g, size_t off) { return (uint8_t *)g->buf.cpu + off; } |
| 61 | +static uint64_t va_of(const OmegaGpuQ38Graph *g, size_t off) { return off < g->lo.split ? g->buf.va + off : g->dbuf.va + (off - g->lo.split); } |
| 62 | +static void *cpu_of(const OmegaGpuQ38Graph *g, size_t off) { return (uint8_t *)g->buf.cpu + off; } /* host regions only (off < split) */ |
| 63 | static void put64(uint32_t *a, unsigned off, uint64_t v) { a[(off - 0x380) / 4] = (uint32_t)v; a[(off - 0x380) / 4 + 1] = (uint32_t)(v >> 32); } |
| 64 | static void putf(uint32_t *a, unsigned off, float f) { memcpy(&a[(off - 0x380) / 4], &f, 4); } |
| 65 | static int fail(OmegaGpuQ38Graph *g, const char *what, int rc) { |
| 66 | @@ -100,6 +108,7 @@ void omega_gpu_q38_graph_free(OmegaGpuQ38Graph *g) { |
| 67 | omega_gpu_session_lock(); |
| 68 | for (unsigned i = 0; i < K_COUNT; i++) if (g->k[i].code.cpu) omega_gpu_session_free(&g->k[i].code); |
| 69 | if (g->buf.cpu) omega_gpu_session_free(&g->buf); |
| 70 | + if (g->dbuf.cpu) omega_gpu_session_free(&g->dbuf); |
| 71 | omega_gpu_session_unlock(); |
| 72 | omega_gpu_host_free(g); |
| 73 | } |
| 74 | @@ -123,17 +132,22 @@ int omega_gpu_q38_graph_create(const OmegaGpuQ38GraphConfig *cfg, OmegaGpuQ38Gra |
| 75 | } |
| 76 | Layout *L = &g->lo; size_t o = 0; |
| 77 | #define REG(f, bytes) do { L->f = o; o = al(o + (bytes)); } while (0) |
| 78 | - REG(x, H * 4); REG(xq, H * 4); REG(mix, OMEGA_GPU_Q38_GDN_MIX * 4); REG(ab, OMEGA_GPU_Q38_AB * 4); REG(agg, OMEGA_GPU_Q38_ATT_MIX * 4); |
| 79 | - REG(att, OMEGA_GPU_Q38_MIXER_OUT * 4); REG(o, H * 4); REG(gu, 2 * I * 4); REG(act, I * 4); REG(hidden, H * 4); |
| 80 | + REG(x, H * 4); REG(ab, OMEGA_GPU_Q38_AB * 4); REG(hidden, H * 4); |
| 81 | REG(logits, (size_t)g->am_ctas * OMEGA_DEC_ARGMAX_SPAN * 4); REG(am_v, OMEGA_DEC_ARGMAX_SPAN * 4); REG(am_i, OMEGA_DEC_ARGMAX_SPAN * 4); |
| 82 | REG(token, 16); REG(norms, (size_t)cfg->layers * 2 * H * 4); REG(fnorm, H * 4); REG(tabs, (size_t)cfg->layers * 4 * 16 + 16); /* + the FP8 lm_head entry */ |
| 83 | - REG(gtab, OMEGA_GPU_Q38_ATT_MIX * 4); |
| 84 | + REG(gtab, OMEGA_GPU_Q38_ATT_MIX * 4); REG(idtab, COPY_ROW * 4); REG(stage, 2 * I * 4); |
| 85 | + L->split = o; |
| 86 | + REG(xq, H * 4); REG(mix, OMEGA_GPU_Q38_GDN_MIX * 4); REG(agg, OMEGA_GPU_Q38_ATT_MIX * 4); |
| 87 | + REG(att, OMEGA_GPU_Q38_MIXER_OUT * 4); REG(o, H * 4); REG(gu, 2 * I * 4); REG(act, I * 4); |
| 88 | #undef REG |
| 89 | L->bytes = o; |
| 90 | int rc = OMEGA_GPU_DGRAPH_OK; |
| 91 | if (omega_gpu_cmdlist_create(OMEGA_GPU_CMDLIST_MAX_LAUNCHES, &g->cl) != 0) { omega_gpu_q38_graph_free(g); return OMEGA_GPU_DGRAPH_CHIP_FAIL; } |
| 92 | omega_gpu_session_lock(); |
| 93 | - if (!omega_gpu_session_open() || omega_gpu_session_alloc(L->bytes, &g->buf) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; |
| 94 | + const char *pbc = getenv("AIEN_OMEGA_Q38_BUF_CACHED"); |
| 95 | + int (*const dalloc)(size_t, OmegaGpuMem *) = (pbc && pbc[0] == '0' && pbc[1] == 0) ? omega_gpu_session_alloc : omega_gpu_session_alloc_cached; |
| 96 | + if (!omega_gpu_session_open() || omega_gpu_session_alloc(L->split, &g->buf) != 0 || dalloc(L->bytes - L->split, &g->dbuf) != 0) |
| 97 | + rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; |
| 98 | static const OmegaDecSpec spec[K_COUNT] = { |
| 99 | [K_NORM] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_NORM, .width = H }, |
| 100 | [K_ADDNORM] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM, .width = H }, |
| 101 | @@ -146,15 +160,19 @@ int omega_gpu_q38_graph_create(const OmegaGpuQ38GraphConfig *cfg, OmegaGpuQ38Gra |
| 102 | [K_GATHER_M1] = { .kind = OMEGA_DEC_GATHER, .width = OMEGA_GPU_Q38_ATT_MIX, .stride = OMEGA_GPU_Q38_ATT_MIX, .mutant = 1 }, |
| 103 | [K_GATE_M1] = { .kind = OMEGA_DEC_GATE, .mutant = 1 }, |
| 104 | [K_ADDNORM_M1] = { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM, .width = H, .mutant = 1 }, |
| 105 | + [K_COPY] = { .kind = OMEGA_DEC_GATHER, .width = COPY_ROW, .stride = COPY_ROW }, |
| 106 | }; |
| 107 | for (unsigned i = 0; i < K_COUNT && rc == OMEGA_GPU_DGRAPH_OK; i++) rc = kern_build(&g->k[i], &spec[i]); |
| 108 | if (rc == OMEGA_GPU_DGRAPH_OK) { |
| 109 | - memset(g->buf.cpu, 0, L->bytes); |
| 110 | + memset(g->buf.cpu, 0, L->split); |
| 111 | + memset(g->dbuf.cpu, 0, L->bytes - L->split); /* fresh; every region is written by the chip before a launch reads it */ |
| 112 | + uint32_t *id = cpu_of(g, L->idtab); |
| 113 | + for (uint32_t i = 0; i < COPY_ROW; i++) id[i] = i; |
| 114 | float *lg = cpu_of(g, L->logits), *amv = cpu_of(g, L->am_v); |
| 115 | for (size_t i = cfg->vocab; i < (size_t)g->am_ctas * OMEGA_DEC_ARGMAX_SPAN; i++) lg[i] = NEG_PAD; /* lm_head writes [0, vocab) only */ |
| 116 | for (size_t i = 0; i < OMEGA_DEC_ARGMAX_SPAN; i++) amv[i] = NEG_PAD; /* stage 1 writes [0, ctas) only */ |
| 117 | omega_q38_gather_table(cpu_of(g, L->gtab)); |
| 118 | - if (omega_gpu_session_sync_to_gpu(&g->buf) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; |
| 119 | + if (omega_gpu_session_sync_to_gpu(&g->buf) != 0 || omega_gpu_session_sync_to_gpu(&g->dbuf) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; |
| 120 | } |
| 121 | omega_gpu_session_unlock(); |
| 122 | if (rc != OMEGA_GPU_DGRAPH_OK) { omega_gpu_q38_graph_free(g); return rc; } |
| 123 | @@ -434,8 +452,21 @@ int omega_gpu_q38_graph_read_buf(OmegaGpuQ38Graph *g, int which, float *out, uin |
| 124 | }; |
| 125 | if (n > b[which].cap) return OMEGA_GPU_DGRAPH_BAD_ARGS; |
| 126 | omega_gpu_session_lock(); |
| 127 | - int rc = omega_gpu_session_sync_to_cpu(&g->buf) == 0 ? OMEGA_GPU_DGRAPH_OK : OMEGA_GPU_DGRAPH_CHIP_FAIL; |
| 128 | - if (rc == OMEGA_GPU_DGRAPH_OK) memcpy(out, cpu_of(g, b[which].off), (size_t)n * 4u); |
| 129 | + int rc = OMEGA_GPU_DGRAPH_OK; |
| 130 | + size_t from = b[which].off; |
| 131 | + if (from >= g->lo.split && n) { |
| 132 | + /* a chip-only region: K_COPY moves ceil(n / 256) rows of 256 floats into stage (every cap is a multiple of 256) */ |
| 133 | + uint32_t w[OMEGA_DEC_GA_WORDS]; memset(w, 0, sizeof w); |
| 134 | + put64(w, OMEGA_DEC_GA_IN, va_of(g, from)); put64(w, OMEGA_DEC_GA_TAB, va_of(g, g->lo.idtab)); put64(w, OMEGA_DEC_GA_OUT, va_of(g, g->lo.stage)); |
| 135 | + const Kern *k = &g->k[K_COPY]; |
| 136 | + OmegaGpuLaunch L; memset(&L, 0, sizeof L); |
| 137 | + L.code_va = k->code.va; L.gpr_count = k->gpr; L.threads_x = OMEGA_DEC_THREADS; L.threads_y = 1; L.grid_x = 1; L.grid_y = (n + COPY_ROW - 1) / COPY_ROW; |
| 138 | + L.num_elements = (size_t)L.grid_y * COPY_ROW; L.args = w; L.n_args = OMEGA_DEC_GA_WORDS; L.timeout_ms = TIMEOUT_MS; |
| 139 | + if (!omega_gpu_session_open() || omega_gpu_session_launch(&L, NULL, NULL) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; |
| 140 | + from = g->lo.stage; |
| 141 | + } |
| 142 | + if (rc == OMEGA_GPU_DGRAPH_OK && omega_gpu_session_sync_to_cpu(&g->buf) != 0) rc = OMEGA_GPU_DGRAPH_CHIP_FAIL; |
| 143 | + if (rc == OMEGA_GPU_DGRAPH_OK) memcpy(out, cpu_of(g, from), (size_t)n * 4u); |
| 144 | omega_gpu_session_unlock(); |
| 145 | return rc; |
| 146 | } |
| 147 | diff --git a/tests/gpu_q38_graph_test.c b/tests/gpu_q38_graph_test.c |
| 148 | index 9a16f205..69de826c 100644 |
| 149 | --- a/tests/gpu_q38_graph_test.c |
| 150 | +++ b/tests/gpu_q38_graph_test.c |
| 151 | @@ -261,6 +261,23 @@ static int chip_run(uint32_t layers, uint64_t mask, uint32_t reps) { |
| 152 | } |
| 153 | omega_gpu_q38_graph_set_flush_each(g, 0); |
| 154 | CHECK(bsame, "flush_each == one list, bit for bit, all %u tokens", TOKENS); |
| 155 | + /* read_buf: x from the host-visible buffer; gu and act from the GPU-cached one (a chip copy into the staging region first). |
| 156 | + * act = silu(gu[c]) * gu[I + c] * 2^64 (the last layer's SwiGLU) ties the two copies together. */ |
| 157 | + { |
| 158 | + static float rx[H], rgu[2 * I], ract[I], rgu100[100]; |
| 159 | + const int r1 = omega_gpu_q38_graph_read_buf(g, OMEGA_GPU_Q38_BUF_X, rx, H), r2 = omega_gpu_q38_graph_read_buf(g, OMEGA_GPU_Q38_BUF_GU, rgu, 2 * I); |
| 160 | + const int r3 = omega_gpu_q38_graph_read_buf(g, OMEGA_GPU_Q38_BUF_ACT, ract, I), r4 = omega_gpu_q38_graph_read_buf(g, OMEGA_GPU_Q38_BUF_GU, rgu100, 100); |
| 161 | + CHECK(r1 == 0 && r2 == 0 && r3 == 0 && r4 == 0, "read_buf x, gu, act, gu[0..100): rc %d %d %d %d", r1, r2, r3, r4); |
| 162 | + CHECK(memcmp(rx, B.x, sizeof rx) == 0, "read_buf x == read x, bit for bit"); |
| 163 | + CHECK(memcmp(rgu100, rgu, sizeof rgu100) == 0, "read_buf of 100 values == the first 100 of the whole row"); |
| 164 | + double worst = 0, amax = 0; |
| 165 | + for (uint32_t c = 0; c < I; c++) { |
| 166 | + const double v = rgu[c], want = v / (1.0 + exp(-v)) * (double)rgu[I + c] * 0x1p64; |
| 167 | + if (fabs(want) > amax) amax = fabs(want); |
| 168 | + if (fabs(want - ract[c]) > worst) worst = fabs(want - ract[c]); |
| 169 | + } |
| 170 | + CHECK(amax > 0 && worst <= 1e-5 * amax, "read_buf act == silu(gu) * up * 2^64 from read_buf gu: worst %.3g of %.3g", worst, amax); |
| 171 | + } |
| 172 | /* negative controls: each graph mutant must fail the A comparison on some token */ |
| 173 | const char *mname[5] = { "", "swiglu up half -1", "gather index +1", "gate dropped", "post-norm weight dropped" }; |
| 174 | for (int mu = 1; mu <= 4; mu++) { |