cache.cpp 1.12 KB
Newer Older
Woosuk Kwon's avatar
Woosuk Kwon committed
1
2
#include <torch/extension.h>

3
4
5
6
#include <map>
#include <vector>

void swap_blocks(
Woosuk Kwon's avatar
Woosuk Kwon committed
7
8
9
10
  torch::Tensor& src,
  torch::Tensor& dst,
  const std::map<int64_t, int64_t>& block_mapping);

11
void copy_blocks(
12
13
  std::vector<torch::Tensor>& key_caches,
  std::vector<torch::Tensor>& value_caches,
14
15
  const std::map<int64_t, std::vector<int64_t>>& block_mapping);

Woosuk Kwon's avatar
Woosuk Kwon committed
16
17
18
19
20
21
22
void reshape_and_cache(
  torch::Tensor& key,
  torch::Tensor& value,
  torch::Tensor& key_cache,
  torch::Tensor& value_cache,
  torch::Tensor& slot_mapping);

23
24
25
26
27
28
29
void gather_cached_kv(
  torch::Tensor& key,
  torch::Tensor& value,
  torch::Tensor& key_cache,
  torch::Tensor& value_cache,
  torch::Tensor& slot_mapping);

Woosuk Kwon's avatar
Woosuk Kwon committed
30
31
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
  m.def(
32
33
34
35
36
    "swap_blocks",
    &swap_blocks,
    "Swap in (out) the cache blocks from src to dst");
  m.def(
    "copy_blocks",
Woosuk Kwon's avatar
Woosuk Kwon committed
37
    &copy_blocks,
Woosuk Kwon's avatar
Woosuk Kwon committed
38
    "Copy the cache blocks from src to dst");
Woosuk Kwon's avatar
Woosuk Kwon committed
39
40
41
42
  m.def(
    "reshape_and_cache",
    &reshape_and_cache,
    "Reshape the key and value tensors and cache them");
43
44
45
46
  m.def(
    "gather_cached_kv",
    &gather_cached_kv,
    "Gather key and value from the cache into contiguous QKV tensors");
Woosuk Kwon's avatar
Woosuk Kwon committed
47
}