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
aien-sovereign-core-mv / crates / aien-platform / src / runtime.rs
| 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 | use crate::compute::ComputeDevice; |
| 17 | use crate::queue::WorkQueue; |
| 18 | |
| 19 | /// Zero-cost, statically dispatched inference runtime. |
| 20 | /// Avoids dynamic vtables across the platform boundary for maximum inlining and zero context switching. |
| 21 | pub struct Runtime<D, Q> |
| 22 | where |
| 23 | D: ComputeDevice, |
| 24 | Q: WorkQueue, |
| 25 | { |
| 26 | pub device: D, |
| 27 | pub queue: Q, |
| 28 | } |
| 29 | |
| 30 | impl<D, Q> Runtime<D, Q> |
| 31 | where |
| 32 | D: ComputeDevice, |
| 33 | Q: WorkQueue, |
| 34 | { |
| 35 | pub const fn new(device: D, queue: Q) -> Self { |
| 36 | Self { device, queue } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | #[cfg(test)] |
| 41 | mod tests { |
| 42 | use super::*; |
| 43 | use crate::compute::*; |
| 44 | use crate::queue::*; |
| 45 | use crate::PlatformError; |
| 46 | use alloc::vec; |
| 47 | use alloc::vec::Vec; |
| 48 | |
| 49 | struct MockBuffer(Vec<u8>); |
| 50 | impl UnifiedBuffer for MockBuffer { |
| 51 | fn len(&self) -> usize { |
| 52 | self.0.len() |
| 53 | } |
| 54 | fn device_address(&self) -> DeviceAddress { |
| 55 | DeviceAddress(self.0.as_ptr() as u64) |
| 56 | } |
| 57 | fn as_ptr(&self) -> *const u8 { |
| 58 | self.0.as_ptr() |
| 59 | } |
| 60 | fn as_mut_ptr(&mut self) -> *mut u8 { |
| 61 | self.0.as_mut_ptr() |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | struct MockDevice; |
| 66 | impl ComputeDevice for MockDevice { |
| 67 | type Buffer = MockBuffer; |
| 68 | |
| 69 | fn alloc(&self, layout: BufferLayout) -> Result<Self::Buffer, PlatformError> { |
| 70 | Ok(MockBuffer(vec![0u8; layout.size])) |
| 71 | } |
| 72 | |
| 73 | fn submit(&self, _work: ComputeWork<'_>) -> Result<Fence, PlatformError> { |
| 74 | Ok(Fence(1)) |
| 75 | } |
| 76 | |
| 77 | fn synchronize(&self, _fence: Fence) -> Result<(), PlatformError> { |
| 78 | Ok(()) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | struct MockQueue(spin::Mutex<Vec<InferenceWork>>); |
| 83 | impl WorkQueue for MockQueue { |
| 84 | fn push(&self, work: InferenceWork) -> Result<(), QueueError> { |
| 85 | self.0.lock().push(work); |
| 86 | Ok(()) |
| 87 | } |
| 88 | fn pop(&self) -> Option<InferenceWork> { |
| 89 | self.0.lock().pop() |
| 90 | } |
| 91 | fn len(&self) -> usize { |
| 92 | self.0.lock().len() |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | #[test] |
| 97 | fn test_statically_dispatched_runtime() { |
| 98 | let device = MockDevice; |
| 99 | let queue = MockQueue(spin::Mutex::new(Vec::new())); |
| 100 | let runtime = Runtime::new(device, queue); |
| 101 | |
| 102 | let buf = runtime.device.alloc(BufferLayout::new(1024, 64)).unwrap(); |
| 103 | assert_eq!(buf.len(), 1024); |
| 104 | |
| 105 | let work = InferenceWork { |
| 106 | sequence: 1, |
| 107 | model: ModelHandle(10), |
| 108 | kv: KvHandle(100), |
| 109 | priority: Priority::Interactive, |
| 110 | deadline: Some(500), |
| 111 | branch_parent: None, |
| 112 | next_token_budget: 32, |
| 113 | }; |
| 114 | |
| 115 | runtime.queue.push(work.clone()).unwrap(); |
| 116 | assert_eq!(runtime.queue.len(), 1); |
| 117 | let popped = runtime.queue.pop().unwrap(); |
| 118 | assert_eq!(popped, work); |
| 119 | } |
| 120 | } |