diff --git a/Cargo.lock b/Cargo.lock index 0f37eb58..fd1643ef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1317,10 +1317,19 @@ dependencies = [ name = "leleka-engine-core" version = "0.1.0" dependencies = [ + "leleka-backend-api", "serde", "serde_json", ] +[[package]] +name = "leleka-engine-fake" +version = "0.1.0" +dependencies = [ + "leleka-backend-api", + "leleka-engine-core", +] + [[package]] name = "leleka-experimental-ref" version = "0.1.0" @@ -2858,6 +2867,8 @@ dependencies = [ "hex", "jsonschema", "leleka-backend-api", + "leleka-engine-core", + "leleka-engine-fake", "leleka-gateway-text", "leleka-obs", "leleka-runtime", diff --git a/Cargo.toml b/Cargo.toml index 656b7408..ac016573 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [workspace] resolver = "3" -members = ["crates/backend-api", "crates/worker-protocol", "crates/cpu-kernels", "crates/cpu-sys", "crates/cpu-worker", "crates/gguf", "crates/gptoss", "crates/gptoss-igpu", "crates/harmony", "crates/igpu", "crates/kar-codec", "crates/igpu-worker", "crates/shm", "crates/openvino", "crates/npu-service", "crates/gptoss-npu", "crates/npu-worker", "crates/gpu-npu-worker", "crates/tokenizer", "crates/gateway-text", "crates/gateway-domain", "crates/engine-core", "control-plane", "desktop-core", "crates/libc-shim", "crates/runtime-client", "crates/model-format", "crates/offline-tools", "crates/quant-core", "crates/context", "crates/cuda-driver", "crates/leleka-runtime", "crates/leleka-memory", "crates/deploy", "crates/obs", "crates/native-cpu-expert", "crates/native-parity-bench", "crates/sampler-ref", "crates/experimental-ref", "crates/serving-policy"] -default-members = ["crates/backend-api", "crates/worker-protocol", "crates/cpu-kernels", "crates/cpu-sys", "crates/cpu-worker", "crates/gguf", "crates/gptoss", "crates/gptoss-igpu", "crates/harmony", "crates/igpu", "crates/kar-codec", "crates/igpu-worker", "crates/shm", "crates/openvino", "crates/npu-service", "crates/gptoss-npu", "crates/npu-worker", "crates/gpu-npu-worker", "crates/tokenizer", "crates/gateway-text", "crates/gateway-domain", "crates/engine-core", "control-plane", "desktop-core", "crates/runtime-client", "crates/model-format", "crates/offline-tools", "crates/quant-core", "crates/context", "crates/leleka-memory", "crates/sampler-ref", "crates/experimental-ref", "crates/serving-policy", "crates/deploy", "crates/obs"] +members = ["crates/backend-api", "crates/worker-protocol", "crates/cpu-kernels", "crates/cpu-sys", "crates/cpu-worker", "crates/gguf", "crates/gptoss", "crates/gptoss-igpu", "crates/harmony", "crates/igpu", "crates/kar-codec", "crates/igpu-worker", "crates/shm", "crates/openvino", "crates/npu-service", "crates/gptoss-npu", "crates/npu-worker", "crates/gpu-npu-worker", "crates/tokenizer", "crates/gateway-text", "crates/gateway-domain", "crates/engine-core", "crates/engine-fake", "control-plane", "desktop-core", "crates/libc-shim", "crates/runtime-client", "crates/model-format", "crates/offline-tools", "crates/quant-core", "crates/context", "crates/cuda-driver", "crates/leleka-runtime", "crates/leleka-memory", "crates/deploy", "crates/obs", "crates/native-cpu-expert", "crates/native-parity-bench", "crates/sampler-ref", "crates/experimental-ref", "crates/serving-policy"] +default-members = ["crates/backend-api", "crates/worker-protocol", "crates/cpu-kernels", "crates/cpu-sys", "crates/cpu-worker", "crates/gguf", "crates/gptoss", "crates/gptoss-igpu", "crates/harmony", "crates/igpu", "crates/kar-codec", "crates/igpu-worker", "crates/shm", "crates/openvino", "crates/npu-service", "crates/gptoss-npu", "crates/npu-worker", "crates/gpu-npu-worker", "crates/tokenizer", "crates/gateway-text", "crates/gateway-domain", "crates/engine-core", "crates/engine-fake", "control-plane", "desktop-core", "crates/runtime-client", "crates/model-format", "crates/offline-tools", "crates/quant-core", "crates/context", "crates/leleka-memory", "crates/sampler-ref", "crates/experimental-ref", "crates/serving-policy", "crates/deploy", "crates/obs"] [workspace.package] edition = "2024" diff --git a/architecture-baseline.json b/architecture-baseline.json index e8b709fc..3b0e6045 100644 --- a/architecture-baseline.json +++ b/architecture-baseline.json @@ -120,7 +120,7 @@ "desktop-core/src/bin/leleka-inference.rs": 1487, "desktop-core/src/runtime_rpc/impl_proto_runtime_server_runtime.rs": 463, "desktop-core/src/runtime_rpc/tests.rs": 628, - "desktop-core/tests/api_over_runtime.rs": 644, + "desktop-core/tests/api_over_runtime.rs": 646, "desktop-core/tests/gateway.rs": 403, "desktop-core/tests/managed_worker.rs": 500, "desktop-core/tests/native_tools_retry_sdet.rs": 552, @@ -132,7 +132,8 @@ "dyn-trait:leleka-cpu-worker": 4, "dyn-trait:leleka-cuda-driver": 48, "dyn-trait:leleka-deploy": 17, - "dyn-trait:leleka-engine-core": 17, + "dyn-trait:leleka-engine-core": 19, + "dyn-trait:leleka-engine-fake": 1, "dyn-trait:leleka-experimental-ref": 3, "dyn-trait:leleka-gateway-domain": 5, "dyn-trait:leleka-gptoss": 10, @@ -141,7 +142,7 @@ "dyn-trait:leleka-gpu-npu-worker": 6, "dyn-trait:leleka-memory": 17, "dyn-trait:leleka-model-format": 8, - "dyn-trait:leleka-runtime": 51, + "dyn-trait:leleka-runtime": 52, "dyn-trait:leleka-tokenizer": 2, "dyn-trait:strata-control": 16, "dyn-trait:strata-desktop-core": 2, diff --git a/architecture.json b/architecture.json index f7d0f03d..837eacdc 100644 --- a/architecture.json +++ b/architecture.json @@ -15,7 +15,8 @@ "L3": [ "leleka-libc-shim", "leleka-model-format", "leleka-cuda-driver", "leleka-native-cpu-expert", "leleka-gptoss", "leleka-igpu", "leleka-gptoss-igpu", "leleka-gptoss-npu", "leleka-npu-service", "leleka-cpu-worker", "leleka-igpu-worker", "leleka-npu-worker", - "leleka-gpu-npu-worker", "leleka-offline-tools", "leleka-runtime-client" + "leleka-gpu-npu-worker", "leleka-offline-tools", "leleka-runtime-client", + "leleka-engine-fake" ], "L4": ["leleka-runtime", "strata-desktop-core", "strata-control", "leleka-deploy", "leleka-native-parity-bench"] }, diff --git a/crates/engine-core/Cargo.toml b/crates/engine-core/Cargo.toml index 4b7546c5..5acc14b7 100644 --- a/crates/engine-core/Cargo.toml +++ b/crates/engine-core/Cargo.toml @@ -1,12 +1,13 @@ [package] name = "leleka-engine-core" version = "0.1.0" -description = "Pure domain of the Leleka serving engine: the cross-session prefix cache, host-memory admission. No CUDA, no async runtime, no env, no filesystem, no clock." +description = "Pure domain of the Leleka serving engine: the cross-session prefix cache, conversation policy, prompt plan, speculative-decoding policies, request lifecycle and the engine-family seam. No CUDA, no async runtime, no env, no filesystem, no clock." edition.workspace = true license.workspace = true rust-version.workspace = true [dependencies] +leleka-backend-api = { path = "../backend-api" } serde = { version = "1", features = ["derive"] } [features] diff --git a/crates/engine-core/src/family.rs b/crates/engine-core/src/family.rs new file mode 100644 index 00000000..c150ee98 --- /dev/null +++ b/crates/engine-core/src/family.rs @@ -0,0 +1,25 @@ +//! The engine family seam: what the composition root needs to start an engine, whatever model family and device sit behind +//! it. A family is a zero-sized marker that knows its start-up configuration and how to turn it into a running +//! [`Backend`]; the composition root is generic over it (`spawn::`), so the choice of family is one `match` and every +//! family below it is monomorphised, with no trait object between the root and the family's own code. +//! +//! Adapters: `leleka_runtime::backend::Qwen35Cuda` (the product engine) and `leleka-engine-fake` (a scripted engine for +//! GPU-free tests). + +use leleka_backend_api::{Backend, BackendError}; + +/// One model family on one device stack. +pub trait EngineFamily { + /// Everything the family needs to start; owned by the caller until [`EngineFamily::spawn`]. + type Config; + /// Stable name for logs and status. + const NAME: &'static str; + + /// Start the engine; the returned backend owns its device state and threads. + fn spawn(config: Self::Config) -> Result, BackendError>; +} + +/// Start the engine of family `F`. +pub fn spawn(config: F::Config) -> Result, BackendError> { + F::spawn(config) +} diff --git a/crates/engine-core/src/lib.rs b/crates/engine-core/src/lib.rs index a2d673e1..95d70371 100644 --- a/crates/engine-core/src/lib.rs +++ b/crates/engine-core/src/lib.rs @@ -4,6 +4,7 @@ //! The modules keep the names and paths they had inside `leleka-runtime`, which re-exports them under the old paths. pub mod conversation; +pub mod family; pub mod memory; pub mod prefix_cache; pub mod prompt; diff --git a/crates/engine-fake/Cargo.toml b/crates/engine-fake/Cargo.toml new file mode 100644 index 00000000..a0c6dab6 --- /dev/null +++ b/crates/engine-fake/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "leleka-engine-fake" +version = "0.1.0" +description = "A scripted engine family for GPU-free tests of everything above the engine. Never a dependency of a production crate." +edition.workspace = true +license.workspace = true +rust-version.workspace = true + +[dependencies] +leleka-backend-api = { path = "../backend-api", features = ["scripted"] } +leleka-engine-core = { path = "../engine-core" } + +[lints] +workspace = true diff --git a/crates/engine-fake/src/lib.rs b/crates/engine-fake/src/lib.rs new file mode 100644 index 00000000..11f37675 --- /dev/null +++ b/crates/engine-fake/src/lib.rs @@ -0,0 +1,45 @@ +//! The scripted engine as an [`EngineFamily`], so the composition root starts it exactly as it starts the product engine +//! (`spawn::`) and a test drives everything above the engine without a GPU, a model or a gRPC server. +//! +//! It computes nothing and makes no statement about any model: what it returns is what the test scripted through the +//! [`ScriptedHandle`] it receives at start-up. + +use std::sync::mpsc::{Receiver, Sender, channel}; + +use leleka_backend_api::{ + Backend, BackendError, ModelDescriptor, + scripted::{ScriptedEngine, ScriptedHandle}, +}; +use leleka_engine_core::family::EngineFamily; + +/// The scripted engine family. +pub struct EngineFake; + +/// How to start [`EngineFake`]: the model it claims to serve and where to send the handle that scripts and observes it. +pub struct FakeStart { + pub descriptor: ModelDescriptor, + pub handle: Sender, +} + +impl FakeStart { + /// The fixture model of the scripted engine, and the receiving end of its handle. + pub fn fixture() -> (Self, Receiver) { + let (handle, receiver) = channel(); + (Self { descriptor: ScriptedEngine::fixture_descriptor(), handle }, receiver) + } +} + +impl EngineFamily for EngineFake { + type Config = FakeStart; + const NAME: &'static str = "engine-fake"; + + fn spawn(config: FakeStart) -> Result, BackendError> { + let (engine, handle) = ScriptedEngine::new(config.descriptor); + // the receiver may be gone when a test does not script the engine + let _ = config.handle.send(handle); + Ok(Box::new(engine)) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/engine-fake/src/tests.rs b/crates/engine-fake/src/tests.rs new file mode 100644 index 00000000..95e2bd88 --- /dev/null +++ b/crates/engine-fake/src/tests.rs @@ -0,0 +1,47 @@ +use super::*; +use leleka_backend_api::{ + FinishReason, GenerationEvent, GenerationRequest, GenerationTerminal, InferenceMode, RuntimeConfig, Usage, + scripted::Script, +}; +use leleka_engine_core::family::spawn; + +fn config() -> RuntimeConfig { + RuntimeConfig { + load_config_version: leleka_backend_api::LOAD_CONFIG_VERSION, + inference_mode: InferenceMode::Native, + gpu_memory_limit_bytes: None, + allow_experimental: false, + } +} + +#[test] +fn the_family_starts_through_the_composition_root_and_plays_its_script() { + let (start, receiver) = FakeStart::fixture(); + let mut backend = spawn::(start).expect("the fake engine always starts"); + let handle = receiver.recv().expect("the handle is delivered at start-up"); + let usage = Usage { input_tokens: 3, output_tokens: 2, reused_input_tokens: 0 }; + handle.push(Script::stop_with(&["he", "llo"], usage)); + + let model = backend.load(ScriptedEngine::fixture_descriptor(), config()).expect("the fixture model loads"); + let request = GenerationRequest { + request_id: "r1".into(), + prompt: "p".into(), + max_output_tokens: 8, + temperature_milli: 0, + sampling: Default::default(), + }; + let mut stream = backend.start_generation(&model, request).expect("a loaded engine admits a request"); + let mut text = String::new(); + let mut terminal = None; + while let Ok(Some(event)) = stream.recv() { + match event { + GenerationEvent::TextDelta { text: delta } => text.push_str(&delta), + GenerationEvent::Terminal(t) => terminal = Some(t), + _ => {} + } + } + assert_eq!(text, "hello"); + assert!(matches!(terminal, Some(GenerationTerminal::Completed { reason: FinishReason::Stop, .. }))); + assert_eq!(handle.requests().len(), 1); + assert_eq!(::NAME, "engine-fake"); +} diff --git a/crates/leleka-runtime/src/backend.rs b/crates/leleka-runtime/src/backend.rs index b32f007c..6924e5ab 100644 --- a/crates/leleka-runtime/src/backend.rs +++ b/crates/leleka-runtime/src/backend.rs @@ -62,6 +62,8 @@ mod spawn_optimized; pub use spawn_optimized::*; mod spawn_original; pub use spawn_original::*; +mod family; +pub use family::{Qwen35Cuda, Qwen35CudaStart}; mod sources; pub use sources::*; mod proxy; diff --git a/crates/leleka-runtime/src/backend/family.rs b/crates/leleka-runtime/src/backend/family.rs new file mode 100644 index 00000000..1ec50c35 --- /dev/null +++ b/crates/leleka-runtime/src/backend/family.rs @@ -0,0 +1,27 @@ +//! The Qwen3.5 CUDA engine as an [`EngineFamily`] of the composition root (STRA-338 S20). Starting it is the existing +//! `spawn_backend*` code; this file only gives the choice between its two product entry points a single typed door. +use super::*; +use leleka_engine_core::family::EngineFamily; + +/// The product engine: Qwen3.5 on the CUDA owner thread. +pub struct Qwen35Cuda; + +/// How to start [`Qwen35Cuda`]. +pub enum Qwen35CudaStart { + /// The default native backend (`spawn_backend`). + Native(NativeBackendConfig), + /// The Original serve path (`spawn_backend_original_serve`), with its options. + OriginalServe(NativeBackendConfig, OriginalServeOptions), +} + +impl EngineFamily for Qwen35Cuda { + type Config = Qwen35CudaStart; + const NAME: &'static str = "qwen35-cuda"; + + fn spawn(config: Qwen35CudaStart) -> Result, BackendError> { + match config { + Qwen35CudaStart::Native(native) => spawn_backend(native), + Qwen35CudaStart::OriginalServe(native, options) => spawn_backend_original_serve(native, options), + } + } +} diff --git a/desktop-core/Cargo.toml b/desktop-core/Cargo.toml index b887a84c..4deeb06e 100644 --- a/desktop-core/Cargo.toml +++ b/desktop-core/Cargo.toml @@ -11,6 +11,7 @@ path = "src/main.rs" [dependencies] leleka-backend-api = { path = "../crates/backend-api" } +leleka-engine-core = { path = "../crates/engine-core" } leleka-obs = { path = "../crates/obs" } leleka-runtime = { path = "../crates/leleka-runtime" } leleka-runtime-client = { path = "../crates/runtime-client" } @@ -43,6 +44,7 @@ jsonschema = { version = "=0.58.5", default-features = false } rustix = { version = "=1.1.5", features = ["fs"] } [dev-dependencies] +leleka-engine-fake = { path = "../crates/engine-fake" } # The full chain test: the real API router in front of the real RuntimeServer and a scripted engine. leleka-backend-api = { path = "../crates/backend-api", features = ["scripted"] } strata-control = { path = "../control-plane" } diff --git a/desktop-core/src/bin/leleka-inference.rs b/desktop-core/src/bin/leleka-inference.rs index 2b1ffd06..b146cfd1 100644 --- a/desktop-core/src/bin/leleka-inference.rs +++ b/desktop-core/src/bin/leleka-inference.rs @@ -1,8 +1,8 @@ //! Linux CUDA inference process. Operator-owned artifact paths never enter RPCs. use leleka_runtime::backend::{ InheritedKarArchive, NativeBackendConfig, OriginalServeChoice, OriginalServeMode, - OriginalServeOptions, RankedExpertProfileSource, original_serve_choice, set_archive_attested, - set_verify_stamps, spawn_backend, + OriginalServeOptions, Qwen35Cuda, Qwen35CudaStart, RankedExpertProfileSource, original_serve_choice, + set_archive_attested, set_verify_stamps, spawn_backend_experimental_native_integer_iq3_windowed256_retained_gr, spawn_backend_experimental_optimized_windowed256, spawn_backend_experimental_optimized_windowed256_adaptive_slots, @@ -10,9 +10,9 @@ use leleka_runtime::backend::{ spawn_backend_experimental_optimized_windowed256_resident_owners, spawn_backend_experimental_optimized_windowed256_retained_scratch, spawn_backend_experimental_optimized_windowed256_streamed_spill, - spawn_backend_observed_experimental_optimized_windowed256_adaptive_slots, - spawn_backend_original_serve, verify_sources_only, + spawn_backend_observed_experimental_optimized_windowed256_adaptive_slots, verify_sources_only, }; +use leleka_engine_core::family::spawn; use std::{net::SocketAddr, path::PathBuf}; use strata_desktop_core::runtime_rpc::RuntimeServer; #[path = "../evaluation_measurement.rs"] @@ -214,7 +214,7 @@ async fn main() -> Result<(), Box> { eprintln!("leleka-inference: backend=preview ({reason})"); } let backend = if matches!(choice, OriginalServeChoice::Original) { - spawn_backend_original_serve(config, original_options) + spawn::(Qwen35CudaStart::OriginalServe(config, original_options)) } else if let Some(observer) = observer { spawn_backend_observed_experimental_optimized_windowed256_adaptive_slots( config, @@ -251,7 +251,7 @@ async fn main() -> Result<(), Box> { } else if experimental_integer_windowed { spawn_backend_experimental_native_integer_iq3_windowed256_retained_gr(config) } else { - spawn_backend(config) + spawn::(Qwen35CudaStart::Native(config)) } .map_err(|error| { eprintln!("leleka-inference: {}", error.message); diff --git a/desktop-core/tests/api_over_runtime.rs b/desktop-core/tests/api_over_runtime.rs index 376edc7b..464cacb2 100644 --- a/desktop-core/tests/api_over_runtime.rs +++ b/desktop-core/tests/api_over_runtime.rs @@ -106,8 +106,10 @@ struct Runtime { impl Runtime { async fn start(address: Option) -> Self { - let (engine, handle) = ScriptedEngine::new(ScriptedEngine::fixture_descriptor()); - let server = RuntimeServer::new(Box::new(engine), TOKEN.into()).unwrap(); + let (start, handle) = leleka_engine_fake::FakeStart::fixture(); + let engine = leleka_engine_core::family::spawn::(start).unwrap(); + let handle = handle.recv().unwrap(); + let server = RuntimeServer::new(engine, TOKEN.into()).unwrap(); let listener = tokio::net::TcpListener::bind(address.unwrap_or("127.0.0.1:0".parse().unwrap())) .await