Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core-mv/blob/main/crates/aien-serve-http/src/cpu_engine.rs # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core-mv / crates / aien-serve-http / src / cpu_engine.rs` 112 lines ยท 3929 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 //! The real-but-slow engine: the workspace's CPU reference forward for 17 //! Qwen3-30B-A3B (`aien_inference_abi::qwen3_coder`), exact FP8 expert 18 //! dequant, f32 attention. It makes the server end-to-end usable before the 19 //! Omega (own GPU kernels) engine lands. Expect seconds per token. 20 21 use std::path::Path; 22 use std::time::Instant; 23 24 use aien_inference_abi::qwen3_coder::{ 25 compute_logits, forward_token, load_qwen3_coder, Qwen3CoderState, Qwen3CoderWeights, 26 }; 27 28 use crate::engine::{Engine, EngineError, FinishReason, GenerateOutcome, GenerateParams}; 29 use crate::sampling::SamplingObserver; 30 31 pub struct CpuReferenceEngine { 32 weights: Qwen3CoderWeights, 33 max_context: usize, 34 } 35 36 impl CpuReferenceEngine { 37 /// Loads the whole checkpoint (31 GB of FP8 plus f32 attention copies; several 38 /// minutes and roughly 40 GB of host memory). 39 pub fn load(checkpoint_dir: &Path, max_context: usize) -> Result { 40 let weights = load_qwen3_coder(checkpoint_dir) 41 .map_err(|e| format!("load {}: {e}", checkpoint_dir.display()))?; 42 Ok(Self { 43 weights, 44 max_context, 45 }) 46 } 47 } 48 49 impl Engine for CpuReferenceEngine { 50 fn kind(&self) -> &str { 51 "cpu-reference" 52 } 53 54 fn max_context_tokens(&self) -> usize { 55 self.max_context 56 } 57 58 fn generate( 59 &self, 60 prompt: &[u32], 61 params: &GenerateParams, 62 on_token: &mut dyn FnMut(u32) -> bool, 63 ) -> Result { 64 if prompt.is_empty() { 65 return Err(EngineError::Failed("empty prompt".into())); 66 } 67 if prompt.len() >= self.max_context { 68 return Err(EngineError::PromptTooLong { 69 len: prompt.len(), 70 max: self.max_context, 71 }); 72 } 73 let mut state = Qwen3CoderState::new(); 74 let t0 = Instant::now(); 75 let mut hidden = Vec::new(); 76 for (pos, &tok) in prompt.iter().enumerate() { 77 hidden = forward_token(&self.weights, tok, pos, &mut state); 78 } 79 let prefill_ms = t0.elapsed().as_millis() as u64; 80 81 let t1 = Instant::now(); 82 let mut sampler = SamplingObserver::new(params); 83 let mut pos = prompt.len(); 84 let mut emitted = 0usize; 85 let finish = loop { 86 if emitted >= params.max_new_tokens || pos >= self.max_context { 87 break FinishReason::Length; 88 } 89 let logits = compute_logits(&self.weights, &hidden); 90 let next = sampler.sample(&logits, emitted); 91 if params.stop_token_ids.contains(&next) { 92 break FinishReason::Stop; 93 } 94 emitted += 1; 95 if !on_token(next) { 96 break FinishReason::Cancelled; 97 } 98 if emitted >= params.max_new_tokens || pos >= self.max_context { 99 break FinishReason::Length; 100 } 101 hidden = forward_token(&self.weights, next, pos, &mut state); 102 pos += 1; 103 }; 104 Ok(GenerateOutcome { 105 emitted_tokens: emitted, 106 finish, 107 prefill_ms, 108 decode_ms: t1.elapsed().as_millis() as u64, 109 sampling: Some(sampler.record()), 110 }) 111 } 112 }