cache.cpp 821 Bytes
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
12
13
14
15
void copy_blocks(
  torch::Tensor& src,
  torch::Tensor& dst,
  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);

Woosuk Kwon's avatar
Woosuk Kwon committed
23
24
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
  m.def(
25
26
27
28
29
    "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
30
    &copy_blocks,
Woosuk Kwon's avatar
Woosuk Kwon committed
31
    "Copy the cache blocks from src to dst");
Woosuk Kwon's avatar
Woosuk Kwon committed
32
33
34
35
  m.def(
    "reshape_and_cache",
    &reshape_and_cache,
    "Reshape the key and value tensors and cache them");
Woosuk Kwon's avatar
Woosuk Kwon committed
36
}