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

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