oxillama-server 0.1.3

OpenAI-compatible HTTP API server for OxiLLaMa
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//! Inference worker — drains the request queue on a dedicated blocking thread.
//!
//! There is exactly one worker per server process.  It owns the
//! `InferenceEngine` exclusively, which means no mutex is ever contested:
//! route handlers enqueue [`BatchRequest`] items and the worker processes
//! them sequentially.
//!
//! ## Prefix KV cache
//!
//! On each `Generate`/`GenerateStream` request (when `cache_prompt` is true):
//! 1. Tokenize the prompt.
//! 2. Look up the longest matching prefix in `prefix_cache`.
//! 3. **Hit**: call `engine.prime_with_prefix(cached, restore_to, suffix)` to
//!    restore the KV cache and run only the suffix tokens through the forward
//!    pass, then call `engine.generate_with_logits` to decode.
//! 4. **Miss**: call `engine.reset()` then `engine.generate_with_config`.
//! 5. After generation (on hit or miss), if `cache_prompt` is true, store the
//!    full prompt's KV state in `prefix_cache` for future requests.
//!
//! ## Multi-LoRA
//!
//! When `lora_selection` is non-empty:
//! 1. Resolve adapter names against the `loras` registry.
//! 2. Push adapters onto the engine's LoRA stack.
//! 3. Call `engine.apply_lora_stack()`.
//! 4. Generate.
//! 5. Call `engine.unapply_all_loras()` in a scope guard so the stack is
//!    always cleared even on error.

use std::collections::HashMap;
use std::sync::{Arc, Mutex, RwLock};

use tokio::sync::mpsc;
use tracing::{debug, error, warn};

use oxillama_runtime::engine::InferenceEngine;
use oxillama_runtime::{LoadedLora, PrefixKvCache};

use crate::queue::{BatchRequest, UsageStats};

// ── KV cache helpers ─────────────────────────────────────────────────────────

/// Attempt a prefix cache lookup and prime the engine's KV cache.
///
/// Returns `Some((prompt_tokens, initial_logits))` on a cache hit that
/// successfully primed the engine, `None` on a miss or error.
fn try_prefix_cache_hit(
    engine: &mut InferenceEngine,
    prompt: &str,
    prefix_cache: &Arc<Mutex<PrefixKvCache>>,
) -> Option<(Vec<u32>, Vec<f32>)> {
    let tokens = engine.tokenize(prompt).ok()?;
    if tokens.is_empty() {
        return None;
    }

    type PrefixHitData = (usize, Vec<Vec<f32>>, Vec<Vec<f32>>, Vec<u32>);

    // Clone the cached KV state to release the Mutex before calling into the
    // engine (which takes &mut self and thus cannot alias the cache guard).
    let (effective_match, cached_keys, cached_values, suffix_tokens): PrefixHitData = {
        let mut cache = prefix_cache.lock().ok()?;
        let (match_len, cached) = cache.lookup(&tokens)?;
        // Cap at tokens.len()-1 so we always process at least the last
        // prompt token to obtain fresh logits for the decode loop.
        let effective = match_len.min(tokens.len().saturating_sub(1));
        if effective == 0 {
            return None;
        }
        let suffix: Vec<u32> = tokens[effective..].to_vec();
        // Clone the cached KV data before dropping the lock guard.
        let keys = cached.keys().to_vec();
        let values = cached.values().to_vec();
        (effective, keys, values, suffix)
    };
    // Lock released here — safe to mutably borrow engine.

    // Reconstruct a temporary PrefixKvCache from the cloned data and obtain
    // a CachedKvState reference to pass into engine.prime_with_prefix.
    let mut scratch = PrefixKvCache::new(oxillama_runtime::PrefixCacheConfig {
        max_entries: 2,
        max_memory_bytes: usize::MAX,
        min_prefix_len: 1,
    });
    let snap = oxillama_runtime::CachedKvState::new(cached_keys, cached_values, effective_match);
    scratch.store_snapshot(&tokens[..effective_match], snap);

    let logits = if let Some((_m, cached)) = scratch.lookup(&tokens[..effective_match]) {
        engine
            .prime_with_prefix(cached, effective_match, &suffix_tokens)
            .ok()?
    } else {
        return None;
    };

    Some((tokens, logits))
}

/// Store the current KV state into the prefix cache after a successful
/// generation pass.
fn store_prefix_cache(
    engine: &mut InferenceEngine,
    prompt_tokens: &[u32],
    prefix_cache: &Arc<Mutex<PrefixKvCache>>,
) {
    if let Ok(mut cache) = prefix_cache.lock() {
        engine.store_kv_in_prefix_cache(prompt_tokens, &mut cache);
    }
}

// ── LoRA helpers ─────────────────────────────────────────────────────────────

/// Push the requested LoRA adapters onto the engine and apply the stack.
///
/// Returns the number of adapters applied (0 if `lora_selection` is empty or
/// any name is unknown — in the latter case a warning is emitted).
fn apply_lora_selection(
    engine: &mut InferenceEngine,
    lora_selection: &[(String, f32)],
    loras: &Arc<RwLock<HashMap<String, Arc<LoadedLora>>>>,
) -> usize {
    if lora_selection.is_empty() {
        return 0;
    }

    let registry = match loras.read() {
        Ok(r) => r,
        Err(_) => {
            warn!("LoRA registry RwLock poisoned; skipping LoRA application");
            return 0;
        }
    };

    let mut applied = 0usize;
    for (name, scale) in lora_selection {
        match registry.get(name.as_str()) {
            Some(lora) => {
                engine.push_lora(Arc::clone(lora), *scale);
                applied += 1;
            }
            None => {
                warn!(adapter = %name, "unknown LoRA adapter name; skipping");
            }
        }
    }

    if applied > 0 {
        if let Err(e) = engine.apply_lora_stack() {
            warn!(error = %e, "apply_lora_stack failed; proceeding without LoRA");
            engine.unapply_all_loras();
            return 0;
        }
    }
    applied
}

// ── Spawn ─────────────────────────────────────────────────────────────────────

/// Spawn the inference worker on a dedicated Tokio blocking thread.
///
/// The worker runs until the sending side of `rx` is dropped (server shutdown).
pub fn spawn_inference_worker(
    engine: InferenceEngine,
    rx: mpsc::Receiver<BatchRequest>,
    prefix_cache: Arc<Mutex<PrefixKvCache>>,
    loras: Arc<RwLock<HashMap<String, Arc<LoadedLora>>>>,
) {
    tokio::task::spawn_blocking(move || {
        run_worker(engine, rx, prefix_cache, loras);
    });
}

/// Blocking worker loop.
fn run_worker(
    mut engine: InferenceEngine,
    mut rx: mpsc::Receiver<BatchRequest>,
    prefix_cache: Arc<Mutex<PrefixKvCache>>,
    loras: Arc<RwLock<HashMap<String, Arc<LoadedLora>>>>,
) {
    tracing::info!("inference worker started");

    while let Some(req) = rx.blocking_recv() {
        debug!(req = ?req, "processing inference request");

        match req {
            // ----------------------------------------------------------------
            // Non-streaming generation
            // ----------------------------------------------------------------
            BatchRequest::Generate {
                prompt,
                max_tokens,
                config,
                cache_prompt,
                lora_selection,
                reply,
            } => {
                let lora_count = apply_lora_selection(&mut engine, &lora_selection, &loras);

                let result = run_generate(
                    &mut engine,
                    &prompt,
                    max_tokens,
                    config,
                    cache_prompt,
                    &prefix_cache,
                    |_| {},
                );

                if lora_count > 0 {
                    engine.unapply_all_loras();
                }

                let result = result.map_err(|e| e.to_string());
                if reply.send(result).is_err() {
                    warn!("Generate reply channel closed before result was delivered");
                }
            }

            // ----------------------------------------------------------------
            // Streaming generation
            // ----------------------------------------------------------------
            BatchRequest::GenerateStream {
                prompt,
                max_tokens,
                config,
                cache_prompt,
                lora_selection,
                mut callback,
                reply,
            } => {
                let lora_count = apply_lora_selection(&mut engine, &lora_selection, &loras);

                let result = run_generate(
                    &mut engine,
                    &prompt,
                    max_tokens,
                    config,
                    cache_prompt,
                    &prefix_cache,
                    |t| callback(t),
                );

                if lora_count > 0 {
                    engine.unapply_all_loras();
                }

                let result = result.map(|(_, usage)| usage).map_err(|e| e.to_string());

                if reply.send(result).is_err() {
                    warn!("GenerateStream reply channel closed before result was delivered");
                }
            }

            // ----------------------------------------------------------------
            // Embedding
            // ----------------------------------------------------------------
            BatchRequest::Embed { text, reply } => {
                engine.reset();
                let result = engine.embed(&text).map_err(|e| e.to_string());
                if reply.send(result).is_err() {
                    warn!("Embed reply channel closed before result was delivered");
                }
            }
        }
    }

    error!("inference worker channel closed — no more requests can be processed");
}

/// Core generation logic shared by streaming and non-streaming paths.
///
/// Returns `(text, UsageStats)` on success.
fn run_generate(
    engine: &mut InferenceEngine,
    prompt: &str,
    max_tokens: usize,
    config: oxillama_runtime::sampling::SamplerConfig,
    cache_prompt: bool,
    prefix_cache: &Arc<Mutex<PrefixKvCache>>,
    mut callback: impl FnMut(&str),
) -> Result<(String, UsageStats), oxillama_runtime::RuntimeError> {
    let mut completion_tokens = 0usize;

    // ── Attempt prefix cache hit ──────────────────────────────────────────
    let (text, prompt_token_count) = if cache_prompt {
        match try_prefix_cache_hit(engine, prompt, prefix_cache) {
            Some((prompt_tokens, initial_logits)) => {
                let pt_count = prompt_tokens.len();
                let text = engine.generate_with_logits(
                    &prompt_tokens,
                    initial_logits,
                    max_tokens,
                    config,
                    |t| {
                        completion_tokens += 1;
                        callback(t);
                    },
                )?;
                if cache_prompt {
                    store_prefix_cache(engine, &prompt_tokens, prefix_cache);
                }
                (text, pt_count)
            }
            None => {
                // Miss — fall through to full prefill.
                engine.reset();
                let pt_count = engine.tokenize(prompt).map(|t| t.len()).unwrap_or(0);
                let text = engine.generate_with_config(prompt, max_tokens, config, |t| {
                    completion_tokens += 1;
                    callback(t);
                })?;
                if cache_prompt {
                    // Tokenize again to store; tokenize is cheap.
                    if let Ok(tokens) = engine.tokenize(prompt) {
                        store_prefix_cache(engine, &tokens, prefix_cache);
                    }
                }
                (text, pt_count)
            }
        }
    } else {
        // Prefix cache disabled for this request.
        engine.reset();
        let pt_count = engine.tokenize(prompt).map(|t| t.len()).unwrap_or(0);
        let text = engine.generate_with_config(prompt, max_tokens, config, |t| {
            completion_tokens += 1;
            callback(t);
        })?;
        (text, pt_count)
    };

    let usage = UsageStats {
        prompt_tokens: prompt_token_count,
        completion_tokens,
        total_tokens: prompt_token_count + completion_tokens,
    };
    Ok((text, usage))
}

#[cfg(test)]
mod tests {
    use super::*;
    use oxillama_runtime::{engine::EngineConfig, sampling::SamplerConfig, PrefixCacheConfig};
    use tokio::sync::oneshot;

    type WorkerHandles = (
        mpsc::Sender<BatchRequest>,
        Arc<Mutex<PrefixKvCache>>,
        Arc<RwLock<HashMap<String, Arc<LoadedLora>>>>,
    );

    fn make_worker() -> WorkerHandles {
        let engine = InferenceEngine::new(EngineConfig::default());
        let (tx, rx) = mpsc::channel::<BatchRequest>(4);
        let prefix_cache = Arc::new(Mutex::new(PrefixKvCache::new(PrefixCacheConfig::default())));
        let loras = Arc::new(RwLock::new(HashMap::new()));
        spawn_inference_worker(engine, rx, Arc::clone(&prefix_cache), Arc::clone(&loras));
        (tx, prefix_cache, loras)
    }

    /// An unloaded engine returns an error (not panic) for Generate.
    #[tokio::test]
    async fn test_worker_returns_error_for_unloaded_engine() {
        let (tx, _, _) = make_worker();

        let (reply_tx, reply_rx) =
            oneshot::channel::<Result<(String, crate::queue::UsageStats), String>>();
        tx.send(BatchRequest::Generate {
            prompt: "test".to_string(),
            max_tokens: 8,
            config: SamplerConfig::default(),
            cache_prompt: false,
            lora_selection: vec![],
            reply: reply_tx,
        })
        .await
        .expect("send should succeed");

        let result = reply_rx.await.expect("reply future should resolve");
        assert!(
            result.is_err(),
            "unloaded engine should produce an error, got: {result:?}"
        );
    }

    /// An unloaded engine returns an error for Embed.
    #[tokio::test]
    async fn test_worker_embed_error_for_unloaded_engine() {
        let (tx, _, _) = make_worker();

        let (reply_tx, reply_rx) = oneshot::channel::<Result<Vec<f32>, String>>();
        tx.send(BatchRequest::Embed {
            text: "hello world".to_string(),
            reply: reply_tx,
        })
        .await
        .expect("send should succeed");

        let result = reply_rx.await.expect("reply future should resolve");
        assert!(
            result.is_err(),
            "unloaded engine embed should produce an error, got: {result:?}"
        );
    }

    /// An unloaded engine returns an error for GenerateStream.
    #[tokio::test]
    async fn test_worker_generate_stream_error_for_unloaded_engine() {
        let (tx, _, _) = make_worker();

        let (reply_tx, reply_rx) = oneshot::channel::<Result<crate::queue::UsageStats, String>>();
        tx.send(BatchRequest::GenerateStream {
            prompt: "stream test".to_string(),
            max_tokens: 4,
            config: SamplerConfig::default(),
            cache_prompt: false,
            lora_selection: vec![],
            callback: Box::new(|_| {}),
            reply: reply_tx,
        })
        .await
        .expect("send should succeed");

        let result = reply_rx.await.expect("reply future should resolve");
        assert!(
            result.is_err(),
            "unloaded GenerateStream should produce an error"
        );
    }

    /// Unknown LoRA adapter names are silently skipped; the request proceeds
    /// without LoRA (returning an engine error from the unloaded engine, not
    /// a LoRA-related panic).
    #[tokio::test]
    async fn test_worker_unknown_lora_name_does_not_panic() {
        let (tx, _, _) = make_worker();

        let (reply_tx, reply_rx) =
            oneshot::channel::<Result<(String, crate::queue::UsageStats), String>>();
        tx.send(BatchRequest::Generate {
            prompt: "test".to_string(),
            max_tokens: 4,
            config: SamplerConfig::default(),
            cache_prompt: false,
            lora_selection: vec![("nonexistent_adapter".to_string(), 1.0)],
            reply: reply_tx,
        })
        .await
        .expect("send should succeed");

        // The worker should return an error (model not loaded), not panic.
        let result = reply_rx.await.expect("reply future should resolve");
        assert!(
            result.is_err(),
            "should return engine error, got: {result:?}"
        );
    }

    /// `cache_prompt = false` path: worker resets and calls generate_with_config.
    /// Returns an engine error on unloaded engine, not a cache-related error.
    #[tokio::test]
    async fn test_worker_cache_prompt_false_uses_full_prefill() {
        let (tx, _, _) = make_worker();

        let (reply_tx, reply_rx) =
            oneshot::channel::<Result<(String, crate::queue::UsageStats), String>>();
        tx.send(BatchRequest::Generate {
            prompt: "hello".to_string(),
            max_tokens: 8,
            config: SamplerConfig::default(),
            cache_prompt: false,
            lora_selection: vec![],
            reply: reply_tx,
        })
        .await
        .expect("send should succeed");

        let result = reply_rx.await.expect("reply future should resolve");
        assert!(result.is_err(), "unloaded engine should error");
    }

    /// Multiple sequential requests complete without panicking (queue ordering).
    #[tokio::test]
    async fn test_worker_sequential_requests_do_not_panic() {
        let (tx, _, _) = make_worker();

        for _ in 0..3 {
            let (reply_tx, reply_rx) =
                oneshot::channel::<Result<(String, crate::queue::UsageStats), String>>();
            tx.send(BatchRequest::Generate {
                prompt: "hello".to_string(),
                max_tokens: 4,
                config: SamplerConfig::default(),
                cache_prompt: true,
                lora_selection: vec![],
                reply: reply_tx,
            })
            .await
            .expect("send should succeed");
            // Result is an error (model not loaded) — that's fine.
            let _ = reply_rx.await.expect("reply should resolve");
        }
    }
}