interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / src / omega_gpu_prefill.h

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