Expand description
Engine-replica pool for concurrent serving.
The HTTP server historically wrapped a single InferenceEngine in a
tokio::sync::Mutex, which serialized every request: a long generation
blocked all others for its full duration. This module replaces that single
mutex with a pool of N engine replicas guarded by a semaphore, so up to
N requests can generate concurrently.
§Why a pool works (and is safe)
- Weights are process-global and immutable:
InferenceEngine::from_gguf_pathleaks the mmap and parsedGgufFileto'static, so every replica borrows the same&'static GgufFilezero-copy. The immutable, dequantizedtoken_embdtable is likewise shared across replicas via oneArc<[f32]>(seebuild_pool_from_gguf). Only the per-replicaKvCacheand light wrappers are duplicated. - On CPU tiers (Reference / AVX / NEON),
BonsaiModel::forwardmutates onlyself.kv_cacheover a shared&dyn OneBitKernelon immutable weights, so distinct engine instances run fully parallel. - On the GPU tier (Metal / CUDA), decode funnels through a process-global
singleton graph owning one KV cache and shared scratch.
N > 1GPU replicas give no compute parallelism and would corrupt each other’s KV, so the pool size is clamped to1on the GPU tier (seeresolve_pool_size).
§Back-compatibility
The default path wraps exactly one engine in a 1-element pool whose
EngineLease calls the identical generate* methods on the identical
engine. Single-request behavior — including RNG progression — is therefore
byte-identical to the previous single-mutex design.
§Lock discipline
idle is a synchronous std::sync::Mutex held only for the duration of
a pop/push — never across an .await. Async waiting happens purely on
the tokio::sync::Semaphore, whose permit count equals the pool size.
Structs§
- Engine
Lease - An exclusive lease on one engine from an
EnginePool. - Engine
Pool - A pool of
InferenceEnginereplicas guarded by a semaphore.
Enums§
- Pool
Error - Errors that can occur while acquiring an engine from the pool.
Functions§
- build_
pool_ from_ gguf - Build an
EnginePoolfrom a GGUF file, sizing it for the detected tier. - default_
cpu_ pool_ size - Default pool size on CPU tiers: the host’s available parallelism, capped at
4, with a floor of1. - resolve_
pool_ size - Resolve the effective pool size for a given kernel tier.