cache.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include <torch/extension.h>
  2. #include <map>
  3. #include <vector>
  4. void swap_blocks(
  5. torch::Tensor& src,
  6. torch::Tensor& dst,
  7. const std::map<int64_t, int64_t>& block_mapping);
  8. void copy_blocks(
  9. std::vector<torch::Tensor>& key_caches,
  10. std::vector<torch::Tensor>& value_caches,
  11. const std::map<int64_t, std::vector<int64_t>>& block_mapping);
  12. void reshape_and_cache(
  13. torch::Tensor& key,
  14. torch::Tensor& value,
  15. torch::Tensor& key_cache,
  16. torch::Tensor& value_cache,
  17. torch::Tensor& slot_mapping);
  18. void gather_cached_kv(
  19. torch::Tensor& key,
  20. torch::Tensor& value,
  21. torch::Tensor& key_cache,
  22. torch::Tensor& value_cache,
  23. torch::Tensor& slot_mapping);
  24. PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
  25. m.def(
  26. "swap_blocks",
  27. &swap_blocks,
  28. "Swap in (out) the cache blocks from src to dst");
  29. m.def(
  30. "copy_blocks",
  31. &copy_blocks,
  32. "Copy the cache blocks from src to dst");
  33. m.def(
  34. "reshape_and_cache",
  35. &reshape_and_cache,
  36. "Reshape the key and value tensors and cache them");
  37. m.def(
  38. "gather_cached_kv",
  39. &gather_cached_kv,
  40. "Gather key and value from the cache into contiguous QKV tensors");
  41. }