mistralrs_core/dummy_paged_attention/
cache_engine.rs

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
use std::{
    collections::HashMap,
    sync::{Arc, Mutex, MutexGuard},
};

use candle_core::{DType, Device, Result, Tensor};

use super::config::ModelConfigLike;

#[derive(Clone, Debug)]
pub struct CacheConfig {
    pub block_size: usize,
    pub num_gpu_blocks: usize,
    pub num_cpu_blocks: usize,
}

pub type KVCache = (Tensor, Tensor);

pub struct CacheEngine {
    dummy_cache: Arc<Mutex<Vec<KVCache>>>,
}

impl CacheEngine {
    pub fn new(
        _model_config: &dyn ModelConfigLike,
        _cache_config: &CacheConfig,
        _dtype: DType,
        _device: &Device,
        _layer_devices: Vec<Option<Device>>,
    ) -> Result<Self> {
        Ok(Self {
            dummy_cache: Arc::new(Mutex::new(Vec::new())),
        })
    }

    pub fn get_kv_cache(&self) -> MutexGuard<'_, Vec<KVCache>> {
        loop {
            if let Ok(v) = self.dummy_cache.try_lock() {
                return v;
            }
        }
    }
}

impl CacheEngine {
    pub fn execute_scheduler_ops(
        &self,
        blocks_to_swap_in: HashMap<usize, usize>,
        blocks_to_swap_out: HashMap<usize, usize>,
        blocks_to_copy: HashMap<usize, Vec<usize>>,
    ) -> Result<()> {
        if !blocks_to_swap_in.is_empty() {
            self.swap_in(blocks_to_swap_in)?;
        }
        if !blocks_to_swap_out.is_empty() {
            self.swap_out(blocks_to_swap_out)?;
        }
        if !blocks_to_copy.is_empty() {
            self.copy(blocks_to_copy)?;
        }
        Ok(())
    }

    pub fn swap_in(&self, _src_to_dst: HashMap<usize, usize>) -> Result<()> {
        Ok(())
    }

    pub fn swap_out(&self, _src_to_dst: HashMap<usize, usize>) -> Result<()> {
        Ok(())
    }

    pub fn copy(&self, _src_to_dst: HashMap<usize, Vec<usize>>) -> Result<()> {
        Ok(())
    }
}