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

124 lines · 8827 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_ROUTER_API_H
18#define OMEGA_GPU_ROUTER_API_H
19/*
20 * omega_gpu_router_api: one-launch MoE router for Qwen3-30B-A3B (OWN-30B cut 8 item 2, 2026-10-13).
21 * C signatures for the Rust side first; the kernel is described in omega_blackwell_codegen_router.h.
22 *
23 * int omega_gpu_router_alloc(uint32_t layers, OmegaGpuRouter **out);
24 * Device-resident per-layer router weights (bf16 [128][2048]), post-attention norm weights (f32 [2048]) and
25 * the two expert directories (u64 [128][2] {w_va, s_va} for the gate+up and the down FP8 tensors), plus the
26 * launch buffers: x in, x' out, ids, weights and the two group tables the grouped FP8 GEMV reads.
27 * int omega_gpu_router_set_layer(OmegaGpuRouter *r, uint32_t layer, const uint16_t *gate_bf16, const float *norm_w);
28 * gate_bf16: the checkpoint's mlp.gate.weight [128][2048] as bf16 bits; norm_w: post_attention_layernorm.weight
29 * widened to f32 by the caller.
30 * int omega_gpu_router_set_directory_fp8(OmegaGpuRouter *r, uint32_t layer, const OmegaGpuFp8Tensor *gate_up,
31 * const OmegaGpuFp8Tensor *down);
32 * Builds the layer's directories from two resident FP8 tensors with 128 experts (omega_gpu_fp8_tensor_entry).
33 * int omega_gpu_router_set_directory(OmegaGpuRouter *r, uint32_t layer, const uint64_t *gu, const uint64_t *dn);
34 * Raw form of the above (tests): gu / dn are [128][2] {w_va, s_va}.
35 * int omega_gpu_router(OmegaGpuRouter *r, uint32_t layer, const float *x, float eps, int norm_topk, float out_scale,
36 * uint32_t *ids, float *weights, OmegaGpuRouterInfo *info);
37 * x: f32 [2048] residual row (host). One launch: RMSNorm(x) with the layer's norm weight and eps, logits,
38 * softmax, top-8, weights renormalised when norm_topk != 0 (config.json norm_topk_prob). Outputs: ids[8]
39 * (descending by logit) and weights[8] to the host; on the device x' = RMSNorm(x) * out_scale (2^64 gives the
40 * x' row of omega_gpu_fp8_gemv_grouped_dev) and the two group tables, entry g = directory[ids[g]], at the
41 * addresses omega_gpu_router_device_outputs returns. The grouped GEMV consumes them with no host round trip:
42 * omega_gpu_fp8_gemv_grouped_dev(gate_up, 8, tab_gu_va, xout_va, y, info).
43 * void omega_gpu_router_device_outputs(const OmegaGpuRouter *r, uint64_t *xout_va, uint64_t *tab_gu_va, uint64_t *tab_dn_va);
44 * int omega_gpu_router_read_back(OmegaGpuRouter *r, float *xout, uint64_t *tab_gu, uint64_t *tab_dn);
45 * Test helper: syncs the device outputs of the last call to the host (any pointer may be NULL).
46 *
47 * Every buffer is GPU-uncached host-visible session memory (nvos.h:1117 rule), launched through
48 * omega_gpu_session_launch like the FP8 GEMV and the decode attention. Output words are poisoned before the launch
49 * and a surviving poison word is CHIP_FAIL.
50 */
51#include "omega_blackwell_codegen_router.h"
52#include "omega_gpu_fp8_api.h"
53#include <stdint.h>
54#include <stddef.h>
55
56enum {
57 OMEGA_GPU_ROUTER_OK = 0,
58 OMEGA_GPU_ROUTER_BAD_ARGS = -1,
59 OMEGA_GPU_ROUTER_TOO_LARGE = -2,
60 OMEGA_GPU_ROUTER_CODEGEN_FAIL = -3,
61 OMEGA_GPU_ROUTER_CHIP_FAIL = -4
62};
63
64#define OMEGA_GPU_ROUTER_MAX_LAYERS 64u
65#define OMEGA_GPU_ROUTER_WARPS 32u /* default kernel shape (omega_gpu_router_set_shape overrides) */
66#define OMEGA_GPU_ROUTER_DEPTH 4u
67
68typedef struct OmegaGpuRouter OmegaGpuRouter;
69
70typedef struct {
71 uint32_t layer, warps, depth, threads;
72 uint64_t weight_bytes; /* bf16 router bytes the launch streams: 128 * 2048 * 2 */
73 uint64_t elapsed_ns; /* marker-to-marker chip time */
74 uint64_t call_ns; /* whole call */
75 uint32_t completion_marker;
76 int kernel_cache_hit;
77} OmegaGpuRouterInfo;
78
79int omega_gpu_router_alloc(uint32_t layers, OmegaGpuRouter **out);
80/* Qwen3.6-35B-A3B shape (sc L306): experts 256 (0 = 128) and shared 1. Then set_layer's gate_bf16 is [experts + 1][2048]
81 * (the last row = shared_expert_gate), the directories have experts + 1 entries (entry `experts` = the shared expert),
82 * and ids / weights / both tables have 9 slots: slot 8 = {experts, sigmoid(shared gate . xn), directory[experts]}. */
83int omega_gpu_router_alloc_shape(uint32_t layers, uint32_t experts, uint32_t shared, OmegaGpuRouter **out);
84void omega_gpu_router_free(OmegaGpuRouter *r);
85int omega_gpu_router_set_layer(OmegaGpuRouter *r, uint32_t layer, const uint16_t *gate_bf16, const float *norm_w);
86int omega_gpu_router_set_directory(OmegaGpuRouter *r, uint32_t layer, const uint64_t *gu, const uint64_t *dn);
87int omega_gpu_router_set_directory_fp8(OmegaGpuRouter *r, uint32_t layer, const OmegaGpuFp8Tensor *gate_up, const OmegaGpuFp8Tensor *down);
88int omega_gpu_router(OmegaGpuRouter *r, uint32_t layer, const float *x, float eps, int norm_topk, float out_scale,
89 uint32_t *ids, float *weights, OmegaGpuRouterInfo *info);
90void omega_gpu_router_device_outputs(const OmegaGpuRouter *r, uint64_t *xout_va, uint64_t *tab_gu_va, uint64_t *tab_dn_va);
91/* Record path (OWN-30B W-L2a, decode graph; omega_gpu_cmdlist.h): the launch of omega_gpu_router with the residual row
92 * read from x_va (f32 [2048] on the device) appended to cl instead of submitted. The caller holds
93 * omega_gpu_session_lock(). Nothing is staged, poisoned or read back. The outputs land, when the list runs, at the
94 * router's launch buffer (one per router: a later record of another layer overwrites them, in list order):
95 * xout = RMSNorm(x) * out_scale [2048] f32, ids [8] u32, wts [8] f32 (the COMBINE weights), and the two group tables
96 * (8 x {w_va, s_va}) for gate+up and down. A kernel cache miss that would evict the resident kernel is refused. */
97typedef struct { uint64_t xout_va, ids_va, wts_va, tab_gu_va, tab_dn_va; } OmegaGpuRouterDevOut;
98struct OmegaGpuCmdList;
99int omega_gpu_router_record(struct OmegaGpuCmdList *cl, OmegaGpuRouter *r, uint32_t layer, uint64_t x_va, float eps, int norm_topk,
100 float out_scale, OmegaGpuRouterDevOut *out);
101/* The stroke (L312 v2): the same launch with every output in the caller's device buffer at lb_va (256-byte aligned,
102 * omega_gpu_router_layout_bytes(r) bytes, the launch buffer's layout: xout, ids, wts, tab_gu, tab_dn at the offsets
103 * out reports). One buffer per conversation, so N routers of one layer can sit in one list. */
104int omega_gpu_router_record_to(struct OmegaGpuCmdList *cl, OmegaGpuRouter *r, uint32_t layer, uint64_t x_va, float eps, int norm_topk,
105 float out_scale, uint64_t lb_va, OmegaGpuRouterDevOut *out);
106/* Lever 2 (L312, the stroke): one launch routes `rows` conversations of one layer, each exactly as it is routed alone:
107 * row i reads the residual row at x_va + i * xstride (bytes, >= 8192, a multiple of 16) and writes the record_to layout at
108 * lb_va + i * lbstride (lb_va 256-aligned, lbstride a multiple of 256 >= omega_gpu_router_layout_bytes). out = row 0's
109 * buffers (row i's are + i * lbstride). rows 1 = record_to. The rows kernel has its own cache slot beside the plain one and
110 * must be resident before recording: omega_gpu_router_prepare_rows (a cache miss while recording is refused). */
111#define OMEGA_GPU_ROUTER_MAX_ROWS 64u
112int omega_gpu_router_prepare_rows(OmegaGpuRouter *r, uint32_t xstride, uint32_t lbstride);
113int omega_gpu_router_record_rows(struct OmegaGpuCmdList *cl, OmegaGpuRouter *r, uint32_t layer, uint64_t x_va, uint32_t xstride, float eps,
114 int norm_topk, float out_scale, uint64_t lb_va, uint32_t lbstride, uint32_t rows, OmegaGpuRouterDevOut *out);
115uint32_t omega_gpu_router_layout_bytes(const OmegaGpuRouter *r);
116int omega_gpu_router_read_back(OmegaGpuRouter *r, float *xout, uint64_t *tab_gu, uint64_t *tab_dn);
117const char *omega_gpu_router_last_error(void);
118const char *omega_gpu_router_rc_name(int rc);
119
120/* ---- measurement knobs (tests) ---- */
121void omega_gpu_router_set_shape(uint32_t warps, uint32_t depth); /* 0 = default; clears the kernel cache */
122void omega_gpu_router_test_set_mutant(int mutant); /* TEST ONLY: OmegaRouterSpec.mutant */
123
124#endif