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_moe_api.h

117 lines · 7773 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2#ifndef OMEGA_GPU_MOE_API_H
3#define OMEGA_GPU_MOE_API_H
4/*
5 * omega_gpu_moe_api: column windows over a resident bf16 weight tensor (OWN-30B cut 2, 2026-10-13).
6 *
7 * Why: a mixture-of-experts layer keeps the 128 experts' gate+up (k 2048, n 128 x 1536) and down
8 * (k 768, n 128 x 2048) projections as ONE resident tensor each and, per token, multiplies the
9 * activation row by the 8 selected experts' column windows. The whole-tensor entry
10 * (omega_gpu_matmul_resident_*) computes every column; this file computes only [col0, col0 + cols)
11 * of a resident tensor with the SAME chip-proven kernel: B is tile-major (column tile nt = kp x 8
12 * elements contiguous at element nt * kp * 8, omega_blackwell_codegen.h, omega_matmul_frag_pack_b),
13 * so a window that starts on a column tile is the same kernel with the B base advanced to that
14 * tile and np = the window's padded width. No kernel change, new file only (8B-lane collision rule).
15 *
16 * ---- FFI contract (for the Rust wrapper; C ABI, every function is thread-safe through the
17 * process-wide session lock, every pointer is a host pointer) ----
18 *
19 * typedef struct OmegaGpuMoeTensor OmegaGpuMoeTensor; // opaque, device resident
20 *
21 * int omega_gpu_moe_tensor_upload_bf16(uint32_t k, uint32_t n, const uint16_t *b, OmegaGpuMoeTensor **out);
22 * int omega_gpu_moe_tensor_upload_f32 (uint32_t k, uint32_t n, const float *b, OmegaGpuMoeTensor **out);
23 * b: k x n row-major (f32 is rounded to bf16, round-to-nearest-even); packed tile-major on the
24 * host and kept in GPU-uncached memory until omega_gpu_moe_tensor_free. Limits: k <= 16384,
25 * n <= 262144 (OMEGA_BW_MATMUL_MAX_*). Returns OMEGA_GPU_MATMUL_OK (0) or an OMEGA_GPU_MATMUL_*
26 * code (omega_gpu_matmul_api.h); *out is NULL on failure.
27 * void omega_gpu_moe_tensor_free (OmegaGpuMoeTensor *t); // NULL is a no-op
28 * void omega_gpu_moe_tensor_shape(const OmegaGpuMoeTensor *t, uint32_t *k, uint32_t *n);
29 *
30 * int omega_gpu_matmul_resident_window_f32 (uint32_t m, const float *a, const OmegaGpuMoeTensor *t,
31 * uint32_t col0, uint32_t cols, float *c, OmegaGpuMatmulInfo *info);
32 * int omega_gpu_matmul_resident_window_bf16(uint32_t m, const uint16_t *a, const OmegaGpuMoeTensor *t,
33 * uint32_t col0, uint32_t cols, float *c, OmegaGpuMatmulInfo *info);
34 * C[m x cols] = A[m x k] * B[:, col0 .. col0 + cols), f32 output row-major with row stride cols,
35 * i.e. element (r, j) of C equals element (r, col0 + j) of the whole-tensor product.
36 * a: m x k row-major (f32 rounded to bf16 on the way in, as the whole-tensor calls do).
37 * info may be NULL; when given it is filled like the whole-tensor call (n = cols, padded_n = the
38 * window's padded width, resident = true, elapsed_ns = chip time, call_ns = whole call).
39 * ALIGNMENT RULE (fail-closed, OMEGA_GPU_MATMUL_BAD_ARGS, omega_gpu_matmul_last_error() names it):
40 * col0 % 8 == 0 (OMEGA_GPU_MATMUL_TILE_N: a window starts on a column tile),
41 * cols >= 1 and col0 + cols <= n (never past the real columns),
42 * m >= 1 and m <= 8192.
43 * cols itself need not be a multiple of 8: the kernel computes whole tiles and only the real
44 * cols columns are copied out (the rest of the last tile is the tensor's next columns or its
45 * zero padding, computed and discarded).
46 * int omega_gpu_matmul_resident_windows_f32(uint32_t m, const float *a, const OmegaGpuMoeTensor *t,
47 * uint32_t nwin, const uint32_t *col0, uint32_t cols, float *c,
48 * OmegaGpuMatmulInfo *info);
49 * nwin windows of the same width (the 8 experts of a token): A is packed and staged once, then one
50 * launch per window. c holds nwin consecutive row-major blocks of m x cols: window w's result is at
51 * c + w * m * cols. Every col0[w] obeys the alignment rule; nwin >= 1 and <= 64. info (if given)
52 * sums elapsed_ns over the windows and counts chip_calls.
53 *
54 * int omega_gpu_moe_reserve(uint32_t max_rows, uint32_t max_k, uint32_t max_cols, uint32_t kernel_slots);
55 * int omega_gpu_moe_prepare(uint32_t m, uint32_t k, uint32_t cols);
56 * void omega_gpu_moe_seal(int on);
57 * void omega_gpu_moe_unreserve(void);
58 * Serving reservation, the same contract as omega_gpu_matmul_reserve / _prepare / _seal (no
59 * driver allocation on the call path once reserved, sealed = an unprepared kernel is refused
60 * with OMEGA_GPU_MATMUL_TOO_LARGE). This API has its own staging buffers and kernel cache, so a
61 * server reserves both APIs.
62 * void omega_gpu_moe_cache_clear(void);
63 * void omega_gpu_moe_set_cta_budget(uint32_t ctas); // 0 restores the default; clears the kernel cache
64 * uint32_t omega_gpu_moe_cta_budget(void);
65 *
66 * Return codes and OmegaGpuMatmulInfo are those of omega_gpu_matmul_api.h; the failure stage is in
67 * omega_gpu_matmul_last_error(). An uncertain completion latches the process exactly as the
68 * whole-tensor API does (omega_gpu_matmul_is_blocked()).
69 */
70#include "omega_gpu_matmul_api.h"
71
72#ifdef __cplusplus
73extern "C" {
74#endif
75
76typedef struct OmegaGpuMoeTensor OmegaGpuMoeTensor;
77
78#define OMEGA_GPU_MOE_MAX_WINDOWS 64u
79
80int omega_gpu_moe_tensor_upload_bf16(uint32_t k, uint32_t n, const uint16_t *b, OmegaGpuMoeTensor **out);
81int omega_gpu_moe_tensor_upload_f32(uint32_t k, uint32_t n, const float *b, OmegaGpuMoeTensor **out);
82void omega_gpu_moe_tensor_free(OmegaGpuMoeTensor *t);
83void omega_gpu_moe_tensor_shape(const OmegaGpuMoeTensor *t, uint32_t *k, uint32_t *n);
84
85int omega_gpu_matmul_resident_window_f32(uint32_t m, const float *a, const OmegaGpuMoeTensor *t,
86 uint32_t col0, uint32_t cols, float *c, OmegaGpuMatmulInfo *info);
87int omega_gpu_matmul_resident_window_bf16(uint32_t m, const uint16_t *a, const OmegaGpuMoeTensor *t,
88 uint32_t col0, uint32_t cols, float *c, OmegaGpuMatmulInfo *info);
89int omega_gpu_matmul_resident_windows_f32(uint32_t m, const float *a, const OmegaGpuMoeTensor *t,
90 uint32_t nwin, const uint32_t *col0, uint32_t cols, float *c,
91 OmegaGpuMatmulInfo *info);
92
93int omega_gpu_moe_reserve(uint32_t max_rows, uint32_t max_k, uint32_t max_cols, uint32_t kernel_slots);
94int omega_gpu_moe_prepare(uint32_t m, uint32_t k, uint32_t cols);
95void omega_gpu_moe_seal(int on);
96void omega_gpu_moe_unreserve(void);
97void omega_gpu_moe_cache_clear(void);
98void omega_gpu_moe_set_cta_budget(uint32_t ctas);
99uint32_t omega_gpu_moe_cta_budget(void);
100
101/* Pure host arithmetic, exported for tests and the Rust side: the byte offset of column tile
102 * (col0 / 8) inside a packed tensor of padded depth kp, i.e. where a window's B starts. */
103uint64_t omega_gpu_moe_window_byte_offset(uint32_t kp, uint32_t col0);
104
105/* TEST ONLY. mutant: deliberately wrong kernels (last K pass dropped), the parity oracle must catch
106 * them. skew: every window's B base is moved by this many column tiles (the "wrong window" control:
107 * the window must then disagree with the whole-tensor columns). Both clear the kernel cache / take
108 * effect on the next call; production code never calls them. */
109void omega_gpu_moe_test_set_mutant(int on);
110void omega_gpu_moe_test_set_window_skew(int32_t tiles);
111/* Test hook: one-row calls so far that rewrote row 0 only of an already zero-padded activation pack. */
112uint64_t omega_gpu_moe_test_a_row0_hits(void);
113
114#ifdef __cplusplus
115}
116#endif
117#endif