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

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