interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / omega_gpu_prefill.h

128 lines · 9369 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef OMEGA_GPU_PREFILL_H
18#define OMEGA_GPU_PREFILL_H
19/*
20 * omega_gpu_prefill (OWN-30B T3b, 2026-10-13): the device-resident one-pass prefill API on the shared session. New file.
21 * Every buffer the prefill pipeline passes between its kernels stays on the device; the host builds small tables only.
22 *
23 * Memory policy (nvos.h:1116-1118, read 2026-10-13: GPU caching "For system memory ... will not be coherent with direct CPU
24 * mappings"; clc96f.h:72 L2_FLUSH_DIRTY):
25 * - DEVICE buffers (omega_gpu_pf_alloc_dev) are GPU-cached and device-private: the CPU never reads them and never writes
26 * them; they are filled only by GPU writes (omega_gpu_pf_upload copies through an uncached staging buffer with the copy
27 * kernel), so the GPU L2 never holds a stale line, and GPU producers / consumers share the L2 (the whole point: the
28 * uncached buffers get no cache reuse, evidence OWN30B-T3-TC-PEAK-BW).
29 * - HOST buffers (omega_gpu_pf_alloc_host) are the session's GPU-uncached buffers: tables and inputs the host writes,
30 * results the host reads.
31 * - Reading a device buffer back (tests, logits) goes through omega_gpu_pf_download: a GPU copy into uncached staging.
32 * One session hook (on_close) frees every kernel and staging buffer of this module.
33 *
34 * All functions take the session lock themselves unless named _locked. Return codes OMEGA_GPU_PF_*.
35 */
36#include <stdbool.h>
37#include <stddef.h>
38#include <stdint.h>
39#include "omega_gpu_session.h"
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44enum { OMEGA_GPU_PF_OK = 0, OMEGA_GPU_PF_BAD_ARGS = -1, OMEGA_GPU_PF_TOO_LARGE = -2, OMEGA_GPU_PF_CODEGEN_FAIL = -3, OMEGA_GPU_PF_CHIP_FAIL = -4 };
45const char *omega_gpu_pf_rc_name(int rc);
46
47/* buffers */
48int omega_gpu_pf_alloc_dev(size_t bytes, OmegaGpuMem *out); /* GPU-cached, device-private */
49int omega_gpu_pf_alloc_host(size_t bytes, OmegaGpuMem *out); /* GPU-uncached, host reads and writes */
50void omega_gpu_pf_free(OmegaGpuMem *m);
51/* host bytes -> device buffer at byte offset off (16-byte multiples), through the staging buffer and the copy kernel */
52int omega_gpu_pf_upload(const OmegaGpuMem *dst, size_t off, const void *src, size_t bytes);
53/* device buffer bytes at off -> host (16-byte multiples) */
54int omega_gpu_pf_download(const OmegaGpuMem *src, size_t off, void *dst, size_t bytes);
55/* device-to-device copy by VA (16-byte multiples) */
56int omega_gpu_pf_copy_va(uint64_t dst_va, uint64_t src_va, size_t bytes);
57
58/* accumulated chip time and launch count of this module since the last reset (prefill timing) */
59void omega_gpu_pf_stats(uint64_t *chip_ns, uint64_t *launches);
60void omega_gpu_pf_stats_reset(void);
61
62/* ---- internal to the prefill module family (omega_gpu_prefill*.c) ---- */
63typedef struct { uint32_t kind; uint32_t words[15]; } OmegaPfKey; /* kernel identity: kind + the generator's spec words */
64typedef int (*OmegaPfGen)(const OmegaPfKey *key, void *prog); /* builds the BlackwellIRProgram for key */
65typedef struct { uint64_t code_va; uint32_t gpr_declared, gpr_peak; } OmegaPfKernel;
66/* lock held, device open: the resident kernel for key (generated, encoded, patched and uploaded on first use) */
67int omega_gpu_pf_kernel_locked(const OmegaPfKey *key, OmegaPfGen gen, OmegaPfKernel *out);
68/* lock held: one launch, chip time added to the module stats (and to *ns when given) */
69int omega_gpu_pf_launch_locked(const OmegaPfKernel *k, uint32_t threads, uint32_t grid_x, uint32_t grid_y, uint32_t shared_bytes,
70 const uint32_t *args, uint32_t n_args, uint64_t *ns);
71int omega_gpu_pf_open_locked(void); /* opens the device, registers the hook: OMEGA_GPU_PF_OK or CHIP_FAIL */
72/* lock held: the same launch appended to a command list (omega_gpu_cmdlist.h) instead of submitted (sc #642: a whole Qwen3.8
73 * prefill chunk is one list). Prefill kernels are never evicted, so a recorded launch's code stays valid until the submit. */
74struct OmegaGpuCmdList;
75int omega_gpu_pf_record_locked(struct OmegaGpuCmdList *cl, const OmegaPfKernel *k, uint32_t threads, uint32_t grid_x, uint32_t grid_y,
76 uint32_t shared_bytes, const uint32_t *args, uint32_t n_args);
77
78/* ---- GEMM v2 (omega_gpu_pfgemm.c, kernels omega_blackwell_codegen_tc2.h) ----
79 * Weights live in device buffers in the kernel's PHYSICAL row order with the per-8-row ratio table (tc2 header).
80 * Layout PLAIN: rows unchanged. Layout SWIGLU: w is [E][2I][k] with the I gate rows first then the I up rows (scales
81 * [E][2I/128][k/128]); physical row p = 16G + h holds gate (h < 8) or up (h >= 8) of intermediate index
82 * i = perm(8G + h % 8), perm = the (0,2,1,3) group permutation, so the SwiGLU epilogue writes act rows in stored order. */
83#ifndef OMEGA_GPU_PF_LAYOUTS
84#define OMEGA_GPU_PF_LAYOUTS
85enum { OMEGA_GPU_PF_PLAIN = 0, OMEGA_GPU_PF_SWIGLU = 1, OMEGA_GPU_PF_SWIGLU_NAT = 2 };
86#endif
87typedef struct { OmegaGpuMem w, r; uint32_t experts, n, k, layout; } OmegaPfWeights;
88/* w: experts * n * k e4m3 bytes; scale_inv: experts * (n/128) * (k/128) bf16 (checkpoint weight_scale_inv) */
89int omega_gpu_pf_weights_upload(uint32_t experts, uint32_t n, uint32_t k, const uint8_t *w, const uint16_t *scale_inv, uint32_t layout, OmegaPfWeights *out);
90void omega_gpu_pf_weights_free(OmegaPfWeights *w);
91/* the host-side physical form (tests and emulation): pw experts * n * k, ratio experts * (n/8) * (k/128 + 1) f32 */
92int omega_gpu_pf_weights_host_form(uint32_t experts, uint32_t n, uint32_t k, const uint8_t *w, const uint16_t *scale_inv, uint32_t layout, uint8_t *pw, float *ratio);
93uint32_t omega_gpu_pf_gemm_bm(void); /* rows per tile */
94/* tile table (32-byte entries) for rows sorted by expert: counts[e] rows of expert e; returns the tile count (<= max) or 0 */
95uint32_t omega_gpu_pf_gemm_tiles(const OmegaPfWeights *w, const uint32_t *counts, void *tab, uint32_t max);
96/* one launch. rowidx_va 0 = direct rows (row r reads x row r). x: bf16 stored rows of k (rows to row0+bm-1 readable);
97 * y: f32 rows of n (PLAIN) or bf16 act rows of n/2 (SWIGLU). */
98int omega_gpu_pf_gemm(const OmegaPfWeights *w, uint64_t tiles_va, uint32_t ntiles, uint64_t rowidx_va, uint64_t x_va, uint64_t y_va, uint64_t *ns);
99/* measurement knobs (0 restores): rt, warps, aqn; test-only mutant */
100void omega_gpu_pf_gemm_set_shape(uint32_t rt, uint32_t warps, uint32_t aqn);
101void omega_gpu_pf_gemm_test_set_mutant(int m);
102/* ---- native FP8 path (W-L1G) ----
103 * activation quantiser: x (f32 or bf16 rows of k, in_type OMEGA_QG_IN_*) -> q (e4m3 rows of k) + s (f32 [rows][k/128] power-of-two
104 * scales); semantics in omega_blackwell_codegen_qg.h */
105int omega_gpu_pf_quant(uint32_t k, uint32_t in_type, uint32_t rows, uint64_t x_va, uint64_t q_va, uint64_t s_va, uint64_t *ns);
106void omega_gpu_pf_quant_test_set_mutant(int m);
107/* grouped GEMM on QMMA, weights read in place from an FP8 GEMV tensor (omega_gpu_fp8_api.h, checkpoint row order, no copy).
108 * layout PLAIN: y f32 rows of n. layout SWIGLU_NAT: the tensor is [E][2I][k] gate rows then up rows; y = bf16 act rows of I = n/2,
109 * act[i] = silu(gate_i) * up_i (capped exponent, omega_blackwell_codegen_qg.h). xq: e4m3 rows of k, xs: f32 [rows][k/128]
110 * power-of-two scales (omega_gpu_pf_quant). Rows grouped by expert: counts[e] rows of expert e, tile table from qgemm_tiles. */
111typedef struct OmegaGpuFp8Tensor OmegaGpuFp8Tensor;
112uint32_t omega_gpu_pf_qgemm_bm(void);
113uint32_t omega_gpu_pf_qgemm_tiles(const OmegaGpuFp8Tensor *t, const uint32_t *counts, void *tab, uint32_t max);
114/* the same from per-expert base addresses of a GEMV-layout copy (e.g. a GPU-cached device buffer) */
115uint32_t omega_gpu_pf_qgemm_tiles_va(uint32_t experts, const uint64_t *w_va, const uint64_t *s_va, const uint32_t *counts, void *tab, uint32_t max);
116int omega_gpu_pf_qgemm(uint32_t n, uint32_t k, uint32_t layout, uint64_t tiles_va, uint32_t ntiles, uint64_t rowidx_va, uint64_t xq_va, uint64_t xs_va, uint64_t y_va, uint64_t *ns);
117/* Record paths (sc #642): the same launches appended to cl; the caller holds omega_gpu_session_lock(). A first use compiles the
118 * kernel there (prefill kernels are never evicted). */
119struct OmegaGpuCmdList;
120int omega_gpu_pf_quant_record(struct OmegaGpuCmdList *cl, uint32_t k, uint32_t in_type, uint32_t rows, uint64_t x_va, uint64_t q_va, uint64_t s_va);
121int omega_gpu_pf_qgemm_record(struct OmegaGpuCmdList *cl, uint32_t n, uint32_t k, uint32_t layout, uint64_t tiles_va, uint32_t ntiles,
122 uint64_t rowidx_va, uint64_t xq_va, uint64_t xs_va, uint64_t y_va);
123void omega_gpu_pf_qgemm_set_shape(uint32_t rt, uint32_t warps, uint32_t wait); /* knobs (all 0 restores rt 4, warps 8, wait 45) */
124void omega_gpu_pf_qgemm_test_set_mutant(int m);
125#ifdef __cplusplus
126}
127#endif
128#endif