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-inference-runtime / src / service.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 super::model::EmbeddedModel; |
| 17 | use aien_inference_service::{InferenceEvent, InferenceRequest, InferenceService}; |
| 18 | use async_trait::async_trait; |
| 19 | use parking_lot::Mutex; |
| 20 | use std::sync::Arc; |
| 21 | use tokio::sync::mpsc; |
| 22 | |
| 23 | #[derive(Clone)] |
| 24 | pub struct EmbeddedInferenceService { |
| 25 | model: Arc<Mutex<EmbeddedModel>>, |
| 26 | model_id: String, |
| 27 | } |
| 28 | |
| 29 | impl EmbeddedInferenceService { |
| 30 | pub fn new(model: EmbeddedModel) -> Self { |
| 31 | let model_id = model.config.model_id.clone(); |
| 32 | Self { |
| 33 | model: Arc::new(Mutex::new(model)), |
| 34 | model_id, |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | pub fn with_reference_weights() -> Result<Self, String> { |
| 39 | let config = aien_inference_abi::ModelConfig::tinyllama_1_1b(); |
| 40 | let model = EmbeddedModel::with_reference_weights(&config)?; |
| 41 | Ok(Self::new(model)) |
| 42 | } |
| 43 | |
| 44 | pub fn load_default_or_fallback() -> Result<Self, String> { |
| 45 | let model = EmbeddedModel::load_default_or_fallback()?; |
| 46 | Ok(Self::new(model)) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | #[async_trait] |
| 51 | impl InferenceService for EmbeddedInferenceService { |
| 52 | fn model_id(&self) -> String { |
| 53 | self.model_id.clone() |
| 54 | } |
| 55 | |
| 56 | fn endpoint(&self) -> String { |
| 57 | "embedded://sovereign-core".to_string() |
| 58 | } |
| 59 | |
| 60 | async fn generate(&self, request: &InferenceRequest) -> Result<String, String> { |
| 61 | let model = Arc::clone(&self.model); |
| 62 | let prompt = if let Some(ref sys) = request.system_prompt { |
| 63 | format!( |
| 64 | "<|system|>\n{}</s>\n<|user|>\n{}</s>\n<|assistant|>\n", |
| 65 | sys.trim(), |
| 66 | request.prompt.trim() |
| 67 | ) |
| 68 | } else { |
| 69 | format!("<|user|>\n{}</s>\n<|assistant|>\n", request.prompt.trim()) |
| 70 | }; |
| 71 | let max_tokens = request.limits.max_tokens; |
| 72 | let temp = request.limits.temperature; |
| 73 | |
| 74 | tokio::task::spawn_blocking(move || { |
| 75 | let mut guard = model.lock(); |
| 76 | guard.generate(&prompt, max_tokens, temp) |
| 77 | }) |
| 78 | .await |
| 79 | .map_err(|e| format!("TaskJoin error: {}", e))? |
| 80 | } |
| 81 | |
| 82 | async fn stream_events( |
| 83 | &self, |
| 84 | request: &InferenceRequest, |
| 85 | ) -> Result<mpsc::Receiver<InferenceEvent>, String> { |
| 86 | let model = Arc::clone(&self.model); |
| 87 | let prompt = if let Some(ref sys) = request.system_prompt { |
| 88 | format!( |
| 89 | "<|system|>\n{}</s>\n<|user|>\n{}</s>\n<|assistant|>\n", |
| 90 | sys.trim(), |
| 91 | request.prompt.trim() |
| 92 | ) |
| 93 | } else { |
| 94 | format!("<|user|>\n{}</s>\n<|assistant|>\n", request.prompt.trim()) |
| 95 | }; |
| 96 | let max_tokens = request.limits.max_tokens; |
| 97 | let temp = request.limits.temperature; |
| 98 | |
| 99 | let (tx, rx) = mpsc::channel(64); |
| 100 | |
| 101 | tokio::task::spawn_blocking(move || { |
| 102 | let mut token_count = 0; |
| 103 | let res = { |
| 104 | let mut guard = model.lock(); |
| 105 | guard.generate_stream(&prompt, max_tokens, temp, |piece| { |
| 106 | token_count += 1; |
| 107 | tx.blocking_send(InferenceEvent::Token(piece.to_string())) |
| 108 | .is_ok() |
| 109 | }) |
| 110 | }; |
| 111 | |
| 112 | match res { |
| 113 | Ok(()) => { |
| 114 | let _ = tx.blocking_send(InferenceEvent::Completed { |
| 115 | total_tokens: token_count, |
| 116 | prompt_tokens: 0, |
| 117 | completion_tokens: token_count, |
| 118 | }); |
| 119 | } |
| 120 | Err(e) => { |
| 121 | let _ = tx.blocking_send(InferenceEvent::Error(e)); |
| 122 | } |
| 123 | } |
| 124 | }); |
| 125 | |
| 126 | Ok(rx) |
| 127 | } |
| 128 | } |