ferrum-kv 0.7.7

KV cache management with PagedAttention for Ferrum inference
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//! Prefix caching for shared prompt optimization

use ferrum_types::{FerrumError, Result, TokenId};
use parking_lot::RwLock;
use std::collections::HashMap;
use std::sync::Arc;
use tracing::{debug, trace};

/// Prefix identifier
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PrefixId(Vec<TokenId>);

impl PrefixId {
    /// Create new prefix ID from tokens
    pub fn new(tokens: Vec<TokenId>) -> Self {
        Self(tokens)
    }

    /// Get tokens
    pub fn tokens(&self) -> &[TokenId] {
        &self.0
    }

    /// Get length
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}

impl From<Vec<TokenId>> for PrefixId {
    fn from(tokens: Vec<TokenId>) -> Self {
        Self::new(tokens)
    }
}

impl From<&[TokenId]> for PrefixId {
    fn from(tokens: &[TokenId]) -> Self {
        Self::new(tokens.to_vec())
    }
}

/// Cached prefix information
#[derive(Debug, Clone)]
pub struct CachedPrefix {
    /// Prefix tokens
    pub prefix_id: PrefixId,
    /// KV cache handle for this prefix
    pub kv_handle: Arc<dyn ferrum_interfaces::KvCacheHandle + Send + Sync>,
    /// Last-token logits from the prefill — used to sample the first generated
    /// token on a cache hit without re-running the executor.
    pub last_logits: Vec<f32>,
    /// Reference count
    pub ref_count: usize,
    /// Last access time
    pub last_access: std::time::Instant,
    /// Size in tokens
    pub size: usize,
}

impl CachedPrefix {
    /// Create new cached prefix
    pub fn new(
        prefix_id: PrefixId,
        kv_handle: Arc<dyn ferrum_interfaces::KvCacheHandle + Send + Sync>,
        last_logits: Vec<f32>,
    ) -> Self {
        let size = prefix_id.len();
        Self {
            prefix_id,
            kv_handle,
            last_logits,
            ref_count: 1,
            last_access: std::time::Instant::now(),
            size,
        }
    }

    /// Add reference
    pub fn add_ref(&mut self) {
        self.ref_count += 1;
        self.touch();
    }

    /// Remove reference
    pub fn remove_ref(&mut self) -> Result<()> {
        if self.ref_count == 0 {
            return Err(FerrumError::invalid_parameter(
                "Cannot remove ref from zero-ref prefix",
            ));
        }
        self.ref_count -= 1;
        Ok(())
    }

    /// Update access time
    pub fn touch(&mut self) {
        self.last_access = std::time::Instant::now();
    }

    /// Check if can be evicted
    pub fn can_evict(&self) -> bool {
        self.ref_count == 0
    }
}

/// Prefix cache for shared prompt optimization
#[derive(Debug)]
pub struct PrefixCache {
    /// Cached prefixes by prefix ID
    prefixes: RwLock<HashMap<PrefixId, CachedPrefix>>,
    /// Maximum number of cached prefixes
    max_prefixes: usize,
    /// Minimum prefix length to cache
    min_prefix_length: usize,
    /// Statistics
    hits: parking_lot::Mutex<usize>,
    misses: parking_lot::Mutex<usize>,
    evictions: parking_lot::Mutex<usize>,
}

impl PrefixCache {
    /// Create new prefix cache
    pub fn new(max_prefixes: usize, min_prefix_length: usize) -> Self {
        Self {
            prefixes: RwLock::new(HashMap::new()),
            max_prefixes,
            min_prefix_length,
            hits: parking_lot::Mutex::new(0),
            misses: parking_lot::Mutex::new(0),
            evictions: parking_lot::Mutex::new(0),
        }
    }

    /// Find matching prefix for given tokens.
    ///
    /// Returns `(PrefixId, KvCacheHandle, last_logits)` for the longest
    /// matching prefix, or `None` on miss.
    pub fn find_prefix(
        &self,
        tokens: &[TokenId],
    ) -> Option<(
        PrefixId,
        Arc<dyn ferrum_interfaces::KvCacheHandle + Send + Sync>,
        Vec<f32>,
    )> {
        if tokens.len() < self.min_prefix_length {
            return None;
        }

        let prefixes = self.prefixes.read();

        // Find longest matching prefix
        let mut best_match = None;
        let mut best_len = 0;

        for (prefix_id, cached_prefix) in prefixes.iter() {
            if tokens.starts_with(prefix_id.tokens()) && prefix_id.len() > best_len {
                best_match = Some((
                    prefix_id.clone(),
                    cached_prefix.kv_handle.clone(),
                    cached_prefix.last_logits.clone(),
                ));
                best_len = prefix_id.len();
            }
        }

        if let Some(ref match_info) = best_match {
            *self.hits.lock() += 1;
            trace!("Prefix cache hit: {} tokens", best_len);

            // Update access time
            drop(prefixes); // Release read lock
            let mut prefixes = self.prefixes.write();
            if let Some(cached_prefix) = prefixes.get_mut(&match_info.0) {
                cached_prefix.touch();
            }
        } else {
            *self.misses.lock() += 1;
            trace!("Prefix cache miss for {} tokens", tokens.len());
        }

        best_match
    }

    /// Store prefix in cache
    pub fn store_prefix(
        &self,
        prefix_tokens: &[TokenId],
        kv_handle: Arc<dyn ferrum_interfaces::KvCacheHandle + Send + Sync>,
        last_logits: Vec<f32>,
    ) -> Result<()> {
        if prefix_tokens.len() < self.min_prefix_length {
            return Ok(()); // Don't cache short prefixes
        }

        let prefix_id = PrefixId::from(prefix_tokens);
        let cached_prefix = CachedPrefix::new(prefix_id.clone(), kv_handle, last_logits);

        let mut prefixes = self.prefixes.write();

        // Check if we need to evict
        if prefixes.len() >= self.max_prefixes && !prefixes.contains_key(&prefix_id) {
            self.evict_lru(&mut prefixes);
        }

        // Store or update prefix
        if let Some(existing) = prefixes.get_mut(&prefix_id) {
            existing.add_ref();
        } else {
            prefixes.insert(prefix_id, cached_prefix);
            debug!("Stored new prefix: {} tokens", prefix_tokens.len());
        }

        Ok(())
    }

    /// Remove reference to prefix
    pub fn remove_ref(&self, prefix_tokens: &[TokenId]) -> Result<()> {
        let prefix_id = PrefixId::from(prefix_tokens);
        let mut prefixes = self.prefixes.write();

        if let Some(cached_prefix) = prefixes.get_mut(&prefix_id) {
            cached_prefix.remove_ref()?;

            // Remove if no more references
            if cached_prefix.ref_count == 0 {
                prefixes.remove(&prefix_id);
                debug!(
                    "Removed unreferenced prefix: {} tokens",
                    prefix_tokens.len()
                );
            }
        }

        Ok(())
    }

    /// Evict least recently used prefix
    fn evict_lru(&self, prefixes: &mut HashMap<PrefixId, CachedPrefix>) {
        let mut oldest_id = None;
        let mut oldest_time = None;

        // First try to find least recently used prefix with ref_count == 0
        for (prefix_id, cached_prefix) in prefixes.iter() {
            if cached_prefix.can_evict() {
                if let Some(current_oldest) = oldest_time {
                    if cached_prefix.last_access < current_oldest {
                        oldest_time = Some(cached_prefix.last_access);
                        oldest_id = Some(prefix_id.clone());
                    }
                } else {
                    oldest_time = Some(cached_prefix.last_access);
                    oldest_id = Some(prefix_id.clone());
                }
            }
        }

        // If no evictable prefix found, evict LRU regardless of ref_count
        if oldest_id.is_none() {
            for (prefix_id, cached_prefix) in prefixes.iter() {
                if let Some(current_oldest) = oldest_time {
                    if cached_prefix.last_access < current_oldest {
                        oldest_time = Some(cached_prefix.last_access);
                        oldest_id = Some(prefix_id.clone());
                    }
                } else {
                    oldest_time = Some(cached_prefix.last_access);
                    oldest_id = Some(prefix_id.clone());
                }
            }
        }

        if let Some(prefix_id) = oldest_id {
            prefixes.remove(&prefix_id);
            *self.evictions.lock() += 1;
            debug!("Evicted LRU prefix: {} tokens", prefix_id.len());
        }
    }

    /// Evict up to n prefixes, returning the number actually evicted
    pub fn evict_n(&self, n: usize) -> usize {
        let mut prefixes = self.prefixes.write();
        let mut evicted = 0;

        for _ in 0..n {
            if prefixes.is_empty() {
                break;
            }
            self.evict_lru(&mut prefixes);
            evicted += 1;
        }

        evicted
    }

    /// Get cache statistics
    pub fn stats(&self) -> PrefixCacheStats {
        // Lock metrics first, then prefixes to avoid potential lock ordering issues
        let hits = *self.hits.lock();
        let misses = *self.misses.lock();
        let evictions = *self.evictions.lock();

        let prefixes = self.prefixes.read();
        let total_size: usize = prefixes.values().map(|p| p.size).sum();
        let active_prefixes = prefixes.len();
        drop(prefixes); // Release read lock as soon as possible

        PrefixCacheStats {
            hits,
            misses,
            evictions,
            active_prefixes,
            total_cached_tokens: total_size,
            hit_rate: {
                if hits + misses > 0 {
                    hits as f32 / (hits + misses) as f32
                } else {
                    0.0
                }
            },
        }
    }

    /// Clear all cached prefixes
    pub fn clear(&self) {
        let mut prefixes = self.prefixes.write();
        prefixes.clear();
        *self.hits.lock() = 0;
        *self.misses.lock() = 0;
        *self.evictions.lock() = 0;
        debug!("Cleared prefix cache");
    }

    /// Get configuration
    pub fn config(&self) -> (usize, usize) {
        (self.max_prefixes, self.min_prefix_length)
    }
}

impl Default for PrefixCache {
    fn default() -> Self {
        Self::new(100, 8) // Default: cache up to 100 prefixes, minimum 8 tokens
    }
}

/// Prefix cache statistics
#[derive(Debug, Clone)]
pub struct PrefixCacheStats {
    pub hits: usize,
    pub misses: usize,
    pub evictions: usize,
    pub active_prefixes: usize,
    pub total_cached_tokens: usize,
    pub hit_rate: f32,
}

#[cfg(test)]
mod tests {
    use super::*;

    // Mock KV cache handle for testing
    #[derive(Debug, Clone)]
    struct MockKvHandle {
        tokens: usize,
        device: ferrum_types::Device,
        block_table: ferrum_interfaces::BlockTable,
    }

    impl MockKvHandle {
        fn new(tokens: usize) -> Self {
            Self {
                tokens,
                device: ferrum_types::Device::CPU,
                block_table: ferrum_interfaces::BlockTable::new(16),
            }
        }
    }

    impl ferrum_interfaces::KvCacheHandle for MockKvHandle {
        fn block_table(&self) -> &ferrum_interfaces::BlockTable {
            &self.block_table
        }

        fn block_table_mut(&mut self) -> &mut ferrum_interfaces::BlockTable {
            &mut self.block_table
        }

        fn as_any(&self) -> &dyn std::any::Any {
            self
        }

        fn device(&self) -> ferrum_types::Device {
            self.device.clone()
        }

        fn num_tokens(&self) -> usize {
            self.tokens
        }

        fn num_layers(&self) -> usize {
            32
        }

        fn num_heads(&self) -> usize {
            32
        }

        fn head_dim(&self) -> usize {
            128
        }

        fn key_cache(
            &self,
            _layer: usize,
        ) -> ferrum_types::Result<Option<ferrum_interfaces::TensorRef>> {
            Ok(None)
        }

        fn value_cache(
            &self,
            _layer: usize,
        ) -> ferrum_types::Result<Option<ferrum_interfaces::TensorRef>> {
            Ok(None)
        }

        fn clone_handle(&self) -> ferrum_types::Result<Arc<dyn ferrum_interfaces::KvCacheHandle>> {
            Ok(Arc::new(Self {
                tokens: self.tokens,
                device: self.device.clone(),
                block_table: self.block_table.clone(),
            }))
        }

        fn stats(&self) -> ferrum_interfaces::kv_cache::CacheHandleStats {
            ferrum_interfaces::kv_cache::CacheHandleStats {
                memory_bytes: 0,
                blocks_allocated: 0,
                tokens_stored: self.tokens,
                utilization: 0.0,
                last_access: std::time::Instant::now(),
            }
        }

        fn is_valid(&self) -> bool {
            true
        }

        fn cache_id(&self) -> String {
            "mock".to_string()
        }
    }

    #[test]
    fn test_prefix_cache_creation() {
        let cache = PrefixCache::new(50, 4);
        let (max_prefixes, min_len) = cache.config();
        assert_eq!(max_prefixes, 50);
        assert_eq!(min_len, 4);
    }

    #[test]
    fn test_prefix_storage_and_retrieval() {
        let cache = PrefixCache::new(10, 2);

        let tokens = vec![TokenId::new(1), TokenId::new(2), TokenId::new(3)];
        let handle = Arc::new(MockKvHandle::new(3));

        // Store prefix
        cache
            .store_prefix(&tokens, handle.clone(), vec![0.1; 10])
            .unwrap();

        // Should find exact match
        let result = cache.find_prefix(&tokens);
        assert!(result.is_some());

        // Should find prefix for longer sequence
        let longer_tokens = vec![
            TokenId::new(1),
            TokenId::new(2),
            TokenId::new(3),
            TokenId::new(4),
        ];
        let result = cache.find_prefix(&longer_tokens);
        assert!(result.is_some());
        let (found_prefix, _, _) = result.unwrap();
        assert_eq!(found_prefix.tokens(), &tokens);
    }

    #[test]
    fn test_prefix_length_filtering() {
        let cache = PrefixCache::new(10, 5); // Minimum 5 tokens

        let short_tokens = vec![TokenId::new(1), TokenId::new(2)]; // Too short
        let handle = Arc::new(MockKvHandle::new(2));

        // Should not store short prefix
        cache
            .store_prefix(&short_tokens, handle, vec![0.1; 10])
            .unwrap();

        let result = cache.find_prefix(&short_tokens);
        assert!(result.is_none());
    }

    #[test]
    fn test_lru_eviction() {
        let cache = PrefixCache::new(2, 1); // Max 2 prefixes

        let tokens1 = vec![TokenId::new(1)];
        let tokens2 = vec![TokenId::new(2)];
        let tokens3 = vec![TokenId::new(3)];

        let handle = Arc::new(MockKvHandle::new(1));

        // Store 2 prefixes
        cache
            .store_prefix(&tokens1, handle.clone(), vec![0.1; 10])
            .unwrap();
        cache
            .store_prefix(&tokens2, handle.clone(), vec![0.1; 10])
            .unwrap();

        // Access first one to make it more recent
        cache.find_prefix(&tokens1);

        // Store third - should evict tokens2 (LRU)
        cache
            .store_prefix(&tokens3, handle.clone(), vec![0.1; 10])
            .unwrap();

        // tokens1 and tokens3 should exist, tokens2 should be evicted
        assert!(cache.find_prefix(&tokens1).is_some());
        assert!(cache.find_prefix(&tokens2).is_none());
        assert!(cache.find_prefix(&tokens3).is_some());
    }

    #[test]
    fn test_cache_stats() {
        let cache = PrefixCache::new(10, 2);
        let tokens = vec![TokenId::new(1), TokenId::new(2)];
        let handle = Arc::new(MockKvHandle::new(2));

        cache.store_prefix(&tokens, handle, vec![0.1; 10]).unwrap();

        // Hit
        cache.find_prefix(&tokens);

        // Miss
        let other_tokens = vec![TokenId::new(3), TokenId::new(4)];
        cache.find_prefix(&other_tokens);

        let stats = cache.stats();
        assert_eq!(stats.hits, 1);
        assert_eq!(stats.misses, 1);
        assert_eq!(stats.hit_rate, 0.5);
        assert_eq!(stats.active_prefixes, 1);
    }
}