scripts/dev/split_module.py (lexer-based, with 16 self-tests) moves the top-level fn and impl items of 59 oversize module files in control-plane, desktop-core, model-format, gateway-text, leleka-memory, experimental-ref, context, serving-policy, tokenizer, backend-api, quant-core, harmony, igpu, libc-shim, npu-service and runtime-client into foo/<name>.rs groups of about 250 lines; types, constants, macros, nested modules and tests stay, every path stays valid through pub use re-exports, restricted visibilities keep their scope one level down, include paths and super:: paths are shifted. Not touched: leleka-runtime, cuda-driver, native-cpu-expert, sampler-ref and cpu-* (hot path, next wave) and the files kar-off-ab edits. The baseline drops from 311 to 263 oversize files and from 273,600 to about 233,000 lines; the path-include ratchet ignores dir/name.rs child declarations.
191 lines
6.4 KiB
Rust
191 lines
6.4 KiB
Rust
//! Moved out of the parent module by STRA-338: impl Backend_for_ScriptedEngine.
|
|
|
|
#[allow(unused_imports)]
|
|
use super::*;
|
|
|
|
impl Backend for ScriptedEngine {
|
|
fn identity(&self) -> BackendIdentity {
|
|
BackendIdentity {
|
|
name: "scripted".into(),
|
|
implementation_version: "test".into(),
|
|
api_version: crate::BACKEND_API_VERSION,
|
|
}
|
|
}
|
|
|
|
fn prefix_cache_summary(&self) -> Option<String> {
|
|
self.shared.prefix_summary.lock().unwrap().clone()
|
|
}
|
|
|
|
fn memory_report(&self) -> Option<String> {
|
|
self.shared.memory_report.lock().unwrap().clone()
|
|
}
|
|
|
|
/// Accepts any document that looks like a JSON object unless it names a member `"bad"` (refused as an invalid request) and echoes it.
|
|
fn reconfigure(&mut self, settings_json: &str) -> Result<String, BackendError> {
|
|
let invalid = |message: &str| BackendError {
|
|
code: ErrorCode::InvalidRequest,
|
|
message: message.into(),
|
|
retryable: false,
|
|
};
|
|
let trimmed = settings_json.trim();
|
|
if !(trimmed.starts_with('{') && trimmed.ends_with('}')) {
|
|
return Err(invalid("not an object"));
|
|
}
|
|
if trimmed.contains("\"bad\"") {
|
|
return Err(invalid("bad setting"));
|
|
}
|
|
self.shared
|
|
.settings
|
|
.lock()
|
|
.unwrap()
|
|
.push(trimmed.to_owned());
|
|
Ok(trimmed.to_owned())
|
|
}
|
|
|
|
fn capabilities(&self) -> BackendCapabilities {
|
|
let d = &self.shared.descriptor;
|
|
BackendCapabilities {
|
|
supported_models: if self.shared.ready.load(Ordering::Acquire) {
|
|
vec![SupportedModelTuple {
|
|
family: d.family.clone(),
|
|
architecture: d.architecture.clone(),
|
|
weight_format: d.weight_format.clone(),
|
|
}]
|
|
} else {
|
|
Vec::new()
|
|
},
|
|
max_concurrent_generations: 1,
|
|
supports_cancellation: true,
|
|
supports_tool_call_streaming: false,
|
|
}
|
|
}
|
|
|
|
fn load(
|
|
&mut self,
|
|
model: ModelDescriptor,
|
|
config: RuntimeConfig,
|
|
) -> Result<LoadedModel, BackendError> {
|
|
self.shared
|
|
.loads
|
|
.lock()
|
|
.unwrap()
|
|
.push((model.clone(), config.clone()));
|
|
config.validate().map_err(|_| {
|
|
error(
|
|
ErrorCode::InvalidRequest,
|
|
"invalid load configuration",
|
|
false,
|
|
)
|
|
})?;
|
|
if self.shared.down.load(Ordering::Acquire) {
|
|
return Err(error(ErrorCode::BackendUnavailable, "engine is down", true));
|
|
}
|
|
if model != self.shared.descriptor {
|
|
return Err(error(
|
|
ErrorCode::UnsupportedModel,
|
|
"unsupported model",
|
|
false,
|
|
));
|
|
}
|
|
self.shared.kill.store(false, Ordering::Release);
|
|
let epoch = self.shared.epoch.fetch_add(1, Ordering::AcqRel) + 1;
|
|
self.shared.ready.store(true, Ordering::Release);
|
|
Ok(LoadedModel {
|
|
handle: handle_name(epoch),
|
|
descriptor: model,
|
|
load_config_version: config.load_config_version,
|
|
effective_mode: config.inference_mode,
|
|
mode_identity_digest: format!("sha256:{}", "d".repeat(64)),
|
|
})
|
|
}
|
|
|
|
fn start_generation(
|
|
&mut self,
|
|
model: &LoadedModel,
|
|
request: GenerationRequest,
|
|
) -> Result<GenerationStream, BackendError> {
|
|
let shared = &self.shared;
|
|
if !shared.ready.load(Ordering::Acquire) {
|
|
return Err(error(
|
|
ErrorCode::BackendUnavailable,
|
|
"model is not loaded",
|
|
true,
|
|
));
|
|
}
|
|
if model.handle != handle_name(shared.epoch.load(Ordering::Acquire)) {
|
|
return Err(error(
|
|
ErrorCode::BackendUnavailable,
|
|
"stale model handle",
|
|
true,
|
|
));
|
|
}
|
|
if request.request_id.is_empty() || request.max_output_tokens == 0 {
|
|
return Err(error(ErrorCode::InvalidRequest, "invalid request", false));
|
|
}
|
|
let cancel = Arc::new(AtomicBool::new(false));
|
|
{
|
|
let mut active = shared.active.lock().unwrap();
|
|
if active.is_some() {
|
|
return Err(error(
|
|
ErrorCode::ResourceExhausted,
|
|
"a generation is already running",
|
|
true,
|
|
));
|
|
}
|
|
*active = Some(ActiveGeneration {
|
|
id: request.request_id.clone(),
|
|
cancel: cancel.clone(),
|
|
});
|
|
}
|
|
shared.requests.lock().unwrap().push(request.clone());
|
|
let script = {
|
|
let mut responder = shared.responder.lock().unwrap();
|
|
match responder.as_mut() {
|
|
Some(responder) => responder(&request),
|
|
None => shared.queue.lock().unwrap().pop_front().unwrap_or_else(|| {
|
|
Script::new(vec![Step::Fail {
|
|
code: ErrorCode::Internal,
|
|
message: "no script queued".into(),
|
|
retryable: false,
|
|
}])
|
|
}),
|
|
}
|
|
};
|
|
let (sink, stream) = GenerationSink::channel();
|
|
let shared = Arc::clone(&self.shared);
|
|
thread::spawn(move || play(&shared, &cancel, sink, script));
|
|
Ok(stream)
|
|
}
|
|
|
|
fn cancel(&mut self, request_id: &str) -> Result<(), BackendError> {
|
|
self.shared
|
|
.cancels
|
|
.lock()
|
|
.unwrap()
|
|
.push(request_id.to_owned());
|
|
match self.shared.active.lock().unwrap().as_ref() {
|
|
Some(active) if active.id == request_id => {
|
|
active.cancel.store(true, Ordering::Release);
|
|
Ok(())
|
|
}
|
|
_ => Err(error(
|
|
ErrorCode::InvalidRequest,
|
|
"generation is not active",
|
|
false,
|
|
)),
|
|
}
|
|
}
|
|
|
|
fn unload(&mut self, model: LoadedModel) -> Result<(), BackendError> {
|
|
if model.handle != handle_name(self.shared.epoch.load(Ordering::Acquire)) {
|
|
return Err(error(
|
|
ErrorCode::InvalidRequest,
|
|
"stale model handle",
|
|
false,
|
|
));
|
|
}
|
|
self.shared.ready.store(false, Ordering::Release);
|
|
Ok(())
|
|
}
|
|
}
|