interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / crates / aien-omega-gpu / src / ffi.rs

482 lines · 16051 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//! Raw declarations, written by hand from omega `src/omega_gpu_matmul_api.h`,
17//! `omega_gpu_session.h`, `omega_gpu_serving.h`, `omega_gpu_elementwise_api.h` and
18//! `omega_gpu_attention_api.h` at the commit pinned in `omega.lock`. Nothing generated.
19//!
20//! Hand-written, but not unchecked: every mirrored constant, struct and function is declared
21//! through the `aien-abi-conformance` macros, and `abi/omega_gpu.manifest` is the contract.
22//! `tests/abi_manifest.rs` checks these declarations against it in every build (stub included);
23//! `build.rs` checks the pinned C headers (field names and order, prototypes), a compiled layout
24//! probe (sizes, alignments, offsets, widths, signedness, constant values) and the archive's
25//! symbols against it before linking.
26use aien_abi_conformance::{abi_consts, abi_externs, abi_struct};
27#[cfg(has_omega_gpu)]
28use std::os::raw::c_void;
29use std::os::raw::{c_char, c_int};
30
31abi_consts! { MATMUL_CONSTS;
32pub const OMEGA_GPU_MATMUL_OK: c_int = 0;
33pub const OMEGA_GPU_MATMUL_BAD_ARGS: c_int = -1;
34pub const OMEGA_GPU_MATMUL_TOO_LARGE: c_int = -2;
35pub const OMEGA_GPU_MATMUL_CODEGEN_FAIL: c_int = -3;
36pub const OMEGA_GPU_MATMUL_CHIP_FAIL: c_int = -4;
37pub const OMEGA_GPU_MATMUL_PARITY_FAIL: c_int = -5;
38
39pub const OMEGA_GPU_MATMUL_TILE_M: u32 = 16;
40pub const OMEGA_GPU_MATMUL_TILE_N: u32 = 8;
41pub const OMEGA_GPU_MATMUL_TILE_K: u32 = 16;
42pub const OMEGA_GPU_MATMUL_MAX_CTAS: u32 = 1024;
43}
44
45abi_struct! {
46/// Mirrors `OmegaGpuMatmulInfo` field for field, same order.
47#[repr(C)]
48#[derive(Clone, Copy, Debug)]
49pub struct OmegaGpuMatmulInfo {
50 pub m: u32,
51 pub k: u32,
52 pub n: u32,
53 pub padded_m: u32,
54 pub padded_n: u32,
55 pub k_slices: u32,
56 pub chip_calls: u32,
57 pub rows_per_call: u32,
58 pub elapsed_ns: u64,
59 pub max_abs_err: f32,
60 pub max_rel_err: f32,
61 pub mismatch_count: usize,
62 pub parity_verified: bool,
63 pub completion_marker: u32,
64 pub kernel_cache_hit: bool,
65 pub target_chip: [c_char; 64],
66 pub sm_architecture: u32,
67 // cut 1b additions (appended in the C header, same order)
68 pub grid_x: u32,
69 pub grid_y: u32,
70 pub padded_k: u32,
71 pub resident: bool,
72 pub oracle_ran: bool,
73 pub call_ns: u64,
74}
75}
76
77impl OmegaGpuMatmulInfo {
78 pub fn zeroed() -> Self {
79 Self {
80 m: 0,
81 k: 0,
82 n: 0,
83 padded_m: 0,
84 padded_n: 0,
85 k_slices: 0,
86 chip_calls: 0,
87 rows_per_call: 0,
88 elapsed_ns: 0,
89 max_abs_err: 0.0,
90 max_rel_err: 0.0,
91 mismatch_count: 0,
92 parity_verified: false,
93 completion_marker: 0,
94 kernel_cache_hit: false,
95 target_chip: [0; 64],
96 sm_architecture: 0,
97 grid_x: 0,
98 grid_y: 0,
99 padded_k: 0,
100 resident: false,
101 oracle_ran: false,
102 call_ns: 0,
103 }
104 }
105}
106
107/// Opaque resident weight tensor (omega `OmegaGpuTensor`).
108#[repr(C)]
109pub struct OmegaGpuTensor {
110 _private: [u8; 0],
111}
112abi_struct! {
113/// omega `OmegaGpuAllocStats` (`src/omega_gpu_session.h`), field for field.
114#[repr(C)]
115#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
116pub struct OmegaGpuAllocStats {
117 pub allocs: u64,
118 pub alloc_bytes: u64,
119 pub alloc_failures: u64,
120 pub frees: u64,
121 pub stale_refusals: u64,
122}
123}
124
125abi_struct! {
126/// omega `OmegaGpuServingBounds` (`src/omega_gpu_serving.h`), field for field.
127#[repr(C)]
128#[derive(Debug, Clone, Copy)]
129pub struct OmegaGpuServingBounds {
130 pub max_context: u32,
131 pub max_seqs: u32,
132 pub num_q_heads: u32,
133 pub num_kv_heads: u32,
134 pub head_dim: u32,
135 pub kv_block_size: u32,
136 pub max_rows: u32,
137 pub max_k: u32,
138 pub max_n: u32,
139 pub max_n_one_row: u32,
140 pub kernel_slots: u32,
141}
142}
143
144abi_externs! { MATMUL_FNS;
145#[cfg(has_omega_gpu)]
146extern "C" {
147 pub fn omega_gpu_matmul_bf16(
148 m: u32,
149 k: u32,
150 n: u32,
151 a: *const u16,
152 b: *const u16,
153 c: *mut f32,
154 info: *mut OmegaGpuMatmulInfo,
155 ) -> c_int;
156 pub fn omega_gpu_matmul_f32(
157 m: u32,
158 k: u32,
159 n: u32,
160 a: *const f32,
161 b: *const f32,
162 c: *mut f32,
163 info: *mut OmegaGpuMatmulInfo,
164 ) -> c_int;
165 pub fn omega_gpu_matmul_rc_name(rc: c_int) -> *const c_char;
166 pub fn omega_gpu_matmul_cache_clear();
167 pub fn omega_gpu_tensor_upload_bf16(
168 k: u32,
169 n: u32,
170 b: *const u16,
171 out: *mut *mut OmegaGpuTensor,
172 ) -> c_int;
173 pub fn omega_gpu_tensor_upload_f32(
174 k: u32,
175 n: u32,
176 b: *const f32,
177 out: *mut *mut OmegaGpuTensor,
178 ) -> c_int;
179 pub fn omega_gpu_tensor_free(t: *mut OmegaGpuTensor);
180 pub fn omega_gpu_tensor_shape(t: *const OmegaGpuTensor, k: *mut u32, n: *mut u32);
181 pub fn omega_gpu_matmul_resident_bf16(
182 m: u32,
183 a: *const u16,
184 b: *const OmegaGpuTensor,
185 c: *mut f32,
186 info: *mut OmegaGpuMatmulInfo,
187 ) -> c_int;
188 pub fn omega_gpu_matmul_resident_f32(
189 m: u32,
190 a: *const f32,
191 b: *const OmegaGpuTensor,
192 c: *mut f32,
193 info: *mut OmegaGpuMatmulInfo,
194 ) -> c_int;
195 pub fn omega_gpu_matmul_last_error() -> *const c_char;
196 pub fn omega_gpu_matmul_is_blocked() -> c_int;
197 pub fn omega_gpu_device_close();
198 // omega c0369e6 `src/omega_gpu_matmul_api.h:130-133`: CTA budget per launch
199 // (default OMEGA_GPU_MATMUL_MAX_CTAS; 0 restores it). Clears the kernel cache.
200 pub fn omega_gpu_matmul_set_cta_budget(ctas: u32);
201 pub fn omega_gpu_matmul_cta_budget() -> u32;
202 // omega d6d82f/session-spin-default `src/omega_gpu_session.h`: process-wide marker-wait spin
203 // window in us for launches whose own spin_us is 0 (0 = sleep-poll, the default);
204 // -1 above OMEGA_GPU_SESSION_MAX_SPIN_US with the setting unchanged (omega#328).
205 pub fn omega_gpu_session_set_spin_us(us: u32) -> c_int;
206 pub fn omega_gpu_session_spin_us() -> u32;
207 // omega c1/lib-serving `src/omega_gpu_serving.h` (omega#333 reservation, shipped in libomega_gpu.a by
208 // omega#338): one all-or-nothing call; 0 on success, else the first failing OMEGA_GPU_MATMUL_* /
209 // OMEGA_GPU_ATTN_* code (omega_gpu_matmul_last_error() has the text).
210 pub fn omega_gpu_reserve_serving(b: *const OmegaGpuServingBounds) -> c_int;
211 pub fn omega_gpu_serving_release();
212 // omega c1/lib-serving 80c4daa (omega#338): build and pin the kernel one call shape will use;
213 // then seal so any unprepared kernel is refused TOO_LARGE with no driver call.
214 pub fn omega_gpu_matmul_prepare(m: u32, k: u32, n: u32) -> c_int;
215 pub fn omega_gpu_serving_seal();
216 // omega c1/lib-serving bc9fa71 `src/omega_gpu_session.h`: process-wide driver allocation counters.
217 pub fn omega_gpu_session_alloc_stats(out: *mut OmegaGpuAllocStats);
218}
219}
220
221// ---- omega `src/omega_gpu_elementwise_api.h` (FB-1 cut 4, pinned 2636409) ----
222abi_consts! { EW_CONSTS;
223pub const OMEGA_GPU_EW_OK: c_int = 0;
224pub const OMEGA_GPU_EW_BAD_ARGS: c_int = -1;
225pub const OMEGA_GPU_EW_TOO_LARGE: c_int = -2;
226pub const OMEGA_GPU_EW_CODEGEN_FAIL: c_int = -3;
227pub const OMEGA_GPU_EW_CHIP_FAIL: c_int = -4;
228pub const OMEGA_GPU_EW_UNWRITTEN: c_int = -5;
229}
230
231abi_struct! {
232/// Mirrors `OmegaGpuEwInfo` field for field, same order.
233#[repr(C)]
234#[derive(Clone, Copy, Debug)]
235pub struct OmegaGpuEwInfo {
236 pub chip_calls: u32,
237 pub elapsed_ns: u64,
238 pub completion_marker: u32,
239 pub unwritten_words: u32,
240 pub kernel_cache_hit: bool,
241 pub gpr_count: u32,
242 pub insn_count: u32,
243 pub threads_per_cta: u32,
244 pub ctas_last_launch: u32,
245 pub target_chip: [c_char; 64],
246 pub sm_architecture: u32,
247 pub call_ns: u64,
248}
249}
250
251impl OmegaGpuEwInfo {
252 pub fn zeroed() -> Self {
253 Self {
254 chip_calls: 0,
255 elapsed_ns: 0,
256 completion_marker: 0,
257 unwritten_words: 0,
258 kernel_cache_hit: false,
259 gpr_count: 0,
260 insn_count: 0,
261 threads_per_cta: 0,
262 ctas_last_launch: 0,
263 target_chip: [0; 64],
264 sm_architecture: 0,
265 call_ns: 0,
266 }
267 }
268}
269
270abi_externs! { EW_FNS;
271#[cfg(has_omega_gpu)]
272extern "C" {
273 pub fn omega_gpu_rmsnorm_f32(
274 rows: u32,
275 dim: u32,
276 x: *const f32,
277 weight: *const f32,
278 eps: f32,
279 out: *mut f32,
280 info: *mut OmegaGpuEwInfo,
281 ) -> c_int;
282 pub fn omega_gpu_rope_f32(
283 heads: u32,
284 head_dim: u32,
285 v: *const f32,
286 cos_half: *const f32,
287 sin_half: *const f32,
288 out: *mut f32,
289 info: *mut OmegaGpuEwInfo,
290 ) -> c_int;
291 pub fn omega_gpu_swiglu_f32(
292 n: u32,
293 gate: *const f32,
294 up: *const f32,
295 out: *mut f32,
296 info: *mut OmegaGpuEwInfo,
297 ) -> c_int;
298 pub fn omega_gpu_elementwise_rc_name(rc: c_int) -> *const c_char;
299}
300}
301
302// ---- omega `src/omega_gpu_attention_api.h` (FB-1 cut 5 + 4b + OM-1/OM-2, pinned e5593ae) ----
303abi_consts! { ATTN_CONSTS;
304pub const OMEGA_GPU_ATTN_OK: c_int = 0;
305pub const OMEGA_GPU_ATTN_BAD_ARGS: c_int = -1;
306pub const OMEGA_GPU_ATTN_TOO_LARGE: c_int = -2;
307pub const OMEGA_GPU_ATTN_CODEGEN_FAIL: c_int = -3;
308pub const OMEGA_GPU_ATTN_CHIP_FAIL: c_int = -4;
309pub const OMEGA_GPU_ATTN_UNWRITTEN: c_int = -5;
310/// `OmegaGpuAttnInfo::kv_source`: KV reached the kernel through the per-call staging copy.
311pub const OMEGA_GPU_ATTN_KV_STAGED: u32 = 0;
312}
313
314abi_struct! {
315/// Mirrors `OmegaGpuAttnInfo` field for field, same order (note `call_ns` is third).
316#[repr(C)]
317#[derive(Clone, Copy, Debug)]
318pub struct OmegaGpuAttnInfo {
319 pub chip_calls: u32,
320 pub elapsed_ns: u64,
321 pub call_ns: u64,
322 pub completion_marker: u32,
323 pub unwritten_words: u32,
324 pub gpr_count: u32,
325 pub insn_count: u32,
326 pub kernel_cache_hit: bool,
327 pub ctas_last_launch: u32,
328 pub threads_per_cta: u32,
329 pub target_chip: [c_char; 64],
330 pub sm_architecture: u32,
331 // OM-2 (omega e5593ae): appended after sm_architecture, ABI order preserved.
332 /// Bytes of KV copied into the staging buffer by this call (one copy per physical block per launch).
333 pub kv_bytes_staged: u64,
334 /// Bytes per-reference staging would have copied (every block-table entry once).
335 pub kv_bytes_naive: u64,
336 /// Block-table entries walked.
337 pub kv_blocks_logical: u32,
338 /// Distinct physical blocks actually copied.
339 pub kv_blocks_unique: u32,
340 pub q_bytes: u64,
341 pub tab_bytes: u64,
342 pub out_bytes: u64,
343 /// `OMEGA_GPU_ATTN_KV_STAGED` (0): the only source in this cut; there is no resident KV.
344 pub kv_source: u32,
345}
346}
347
348impl OmegaGpuAttnInfo {
349 pub fn zeroed() -> Self {
350 Self {
351 chip_calls: 0,
352 elapsed_ns: 0,
353 call_ns: 0,
354 completion_marker: 0,
355 unwritten_words: 0,
356 gpr_count: 0,
357 insn_count: 0,
358 kernel_cache_hit: false,
359 ctas_last_launch: 0,
360 threads_per_cta: 0,
361 target_chip: [0; 64],
362 sm_architecture: 0,
363 kv_bytes_staged: 0,
364 kv_bytes_naive: 0,
365 kv_blocks_logical: 0,
366 kv_blocks_unique: 0,
367 q_bytes: 0,
368 tab_bytes: 0,
369 out_bytes: 0,
370 kv_source: 0,
371 }
372 }
373}
374
375// Field-for-field mirror guard against omega `src/omega_gpu_attention_api.h` at e5593ae: the C
376// struct is 176 bytes and these are its offsets (checked with _Static_assert against the header on
377// 2026-10-04; e5593ae is the squash-merge of OM-2 (f777036 was cd80bf4 rebased onto omega main 55d05d6, same attention sources)). A header change that moves a field fails this build instead of misreading a counter.
378const _: () = assert!(std::mem::size_of::<OmegaGpuAttnInfo>() == 176);
379const _: () = assert!(std::mem::offset_of!(OmegaGpuAttnInfo, sm_architecture) == 116);
380const _: () = assert!(std::mem::offset_of!(OmegaGpuAttnInfo, kv_bytes_staged) == 120);
381const _: () = assert!(std::mem::offset_of!(OmegaGpuAttnInfo, kv_blocks_logical) == 136);
382const _: () = assert!(std::mem::offset_of!(OmegaGpuAttnInfo, q_bytes) == 144);
383const _: () = assert!(std::mem::offset_of!(OmegaGpuAttnInfo, kv_source) == 168);
384
385abi_struct! {
386/// Mirrors `OmegaGpuKvLayout` (itself a mirror of aien-kv-cache `KvLayoutDesc`),
387/// field for field, strides in bytes.
388#[repr(C)]
389#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
390pub struct OmegaGpuKvLayout {
391 pub block_stride_bytes: u64,
392 pub layer_stride_bytes: u64,
393 pub kv_plane_stride_bytes: u64,
394 pub token_stride_bytes: u64,
395 pub head_stride_bytes: u64,
396 pub pool_bytes: u64,
397 pub num_blocks: u32,
398 pub num_layers: u32,
399 pub block_size: u32,
400}
401}
402
403abi_externs! { ATTN_FNS;
404#[cfg(has_omega_gpu)]
405extern "C" {
406 pub fn omega_gpu_gqa_attention_f32(
407 q: *const f32,
408 k_cache: *const f32,
409 v_cache: *const f32,
410 seq_len: u32,
411 num_q_heads: u32,
412 num_kv_heads: u32,
413 head_dim: u32,
414 out: *mut f32,
415 info: *mut OmegaGpuAttnInfo,
416 ) -> c_int;
417 // omega#406 (2026-10-12): the prompt read's rows path. q/out [num_rows][num_q_heads][head_dim];
418 // k/v hold base_len + num_rows tokens; row i attends over base_len + i + 1 of them.
419 pub fn omega_gpu_gqa_attention_rows_f32(
420 q: *const f32,
421 k_cache: *const f32,
422 v_cache: *const f32,
423 base_len: u32,
424 num_rows: u32,
425 num_q_heads: u32,
426 num_kv_heads: u32,
427 head_dim: u32,
428 out: *mut f32,
429 info: *mut OmegaGpuAttnInfo,
430 ) -> c_int;
431 pub fn omega_gpu_attention_pool_alloc(bytes: usize, cpu: *mut *mut c_void) -> c_int;
432 pub fn omega_gpu_attention_pool_free();
433 pub fn omega_gpu_paged_attention_bf16(
434 q: *const f32,
435 pool: *const c_void,
436 layout: *const OmegaGpuKvLayout,
437 block_ids: *const u32,
438 num_block_ids: u32,
439 context_len: u32,
440 layer_idx: u32,
441 num_q_heads: u32,
442 num_kv_heads: u32,
443 head_dim: u32,
444 out: *mut f32,
445 info: *mut OmegaGpuAttnInfo,
446 ) -> c_int;
447 pub fn omega_gpu_paged_attention_batch_bf16(
448 q: *const f32,
449 pool: *const c_void,
450 layout: *const OmegaGpuKvLayout,
451 block_tables: *const i32,
452 context_lens: *const i32,
453 max_blocks_per_seq: u32,
454 num_seqs: u32,
455 layer_idx: u32,
456 num_q_heads: u32,
457 num_kv_heads: u32,
458 head_dim: u32,
459 out: *mut f32,
460 info: *mut OmegaGpuAttnInfo,
461 ) -> c_int;
462 pub fn omega_gpu_attention_rc_name(rc: c_int) -> *const c_char;
463 pub fn omega_gpu_attention_last_error() -> *const c_char;
464}
465}
466
467/// The Rust side of the omega GPU contract, for the manifest check.
468pub fn rust_abi() -> aien_abi_conformance::RustAbi {
469 aien_abi_conformance::RustAbi::default()
470 .with_struct::<OmegaGpuMatmulInfo>()
471 .with_struct::<OmegaGpuAllocStats>()
472 .with_struct::<OmegaGpuServingBounds>()
473 .with_struct::<OmegaGpuEwInfo>()
474 .with_struct::<OmegaGpuAttnInfo>()
475 .with_struct::<OmegaGpuKvLayout>()
476 .with_consts(MATMUL_CONSTS)
477 .with_consts(EW_CONSTS)
478 .with_consts(ATTN_CONSTS)
479 .with_fns(MATMUL_FNS)
480 .with_fns(EW_FNS)
481 .with_fns(ATTN_FNS)
482}