torch_bindings.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "cache.h"
  2. #include "ops.h"
  3. #include "core/registration.h"
  4. #include <torch/library.h>
  5. void init_cpu_threads_env(const std::string& cpu_ids);
  6. TORCH_LIBRARY_EXPAND(TORCH_EXTENSION_NAME, ops) {
  7. // Aphrodite custom ops
  8. // Attention ops
  9. // Compute the attention between an input query and the cached keys/values
  10. // using PagedAttention.
  11. ops.def(
  12. "paged_attention_v1("
  13. " Tensor! out, Tensor query, Tensor key_cache,"
  14. " Tensor value_cache, int num_kv_heads, float scale,"
  15. " Tensor block_tables, Tensor seq_lens, int block_size,"
  16. " int max_seq_len, Tensor? alibi_slopes,"
  17. " str kv_cache_dtype, float k_scale, float v_scale,"
  18. " int tp_rank, int blocksparse_local_blocks,"
  19. " int blocksparse_vert_stride, int blocksparse_block_size,"
  20. " int blocksparse_head_sliding_step) -> ()");
  21. ops.impl("paged_attention_v1", torch::kCPU, &paged_attention_v1);
  22. // PagedAttention V2.
  23. ops.def(
  24. "paged_attention_v2("
  25. " Tensor! out, Tensor exp_sums, Tensor max_logits,"
  26. " Tensor tmp_out, Tensor query, Tensor key_cache,"
  27. " Tensor value_cache, int num_kv_heads, float scale,"
  28. " Tensor block_tables, Tensor seq_lens, int block_size,"
  29. " int max_seq_len, Tensor? alibi_slopes,"
  30. " str kv_cache_dtype, float k_scale, float v_scale,"
  31. " int tp_rank, int blocksparse_local_blocks,"
  32. " int blocksparse_vert_stride, int blocksparse_block_size,"
  33. " int blocksparse_head_sliding_step) -> ()");
  34. ops.impl("paged_attention_v2", torch::kCPU, &paged_attention_v2);
  35. // Activation ops
  36. // Activation function used in SwiGLU.
  37. ops.def("silu_and_mul(Tensor! out, Tensor input) -> ()");
  38. ops.impl("silu_and_mul", torch::kCPU, &silu_and_mul);
  39. // Activation function used in GeGLU with `none` approximation.
  40. ops.def("gelu_and_mul(Tensor! out, Tensor input) -> ()");
  41. ops.impl("gelu_and_mul", torch::kCPU, &gelu_and_mul);
  42. // Activation function used in GeGLU with `tanh` approximation.
  43. ops.def("gelu_tanh_and_mul(Tensor! out, Tensor input) -> ()");
  44. ops.impl("gelu_tanh_and_mul", torch::kCPU, &gelu_tanh_and_mul);
  45. // GELU implementation used in GPT-2.
  46. ops.def("gelu_new(Tensor! out, Tensor input) -> ()");
  47. ops.impl("gelu_new", torch::kCPU, &gelu_new);
  48. // Approximate GELU implementation.
  49. ops.def("gelu_fast(Tensor! out, Tensor input) -> ()");
  50. ops.impl("gelu_fast", torch::kCPU, &gelu_fast);
  51. // Quick GELU implementation.
  52. ops.def("gelu_quick(Tensor! out, Tensor input) -> ()");
  53. ops.impl("gelu_quick", torch::kCPU, &gelu_quick);
  54. // Layernorm
  55. // Apply Root Mean Square (RMS) Normalization to the input tensor.
  56. ops.def(
  57. "rms_norm(Tensor! out, Tensor input, Tensor weight, float epsilon) -> "
  58. "()");
  59. ops.impl("rms_norm", torch::kCPU, &rms_norm);
  60. // In-place fused Add and RMS Normalization.
  61. ops.def(
  62. "fused_add_rms_norm(Tensor! input, Tensor! residual, Tensor weight, "
  63. "float epsilon) -> ()");
  64. ops.impl("fused_add_rms_norm", torch::kCPU, &fused_add_rms_norm);
  65. // Rotary embedding
  66. // Apply GPT-NeoX or GPT-J style rotary embedding to query and key.
  67. ops.def(
  68. "rotary_embedding(Tensor positions, Tensor! query,"
  69. " Tensor! key, int head_size,"
  70. " Tensor cos_sin_cache, bool is_neox) -> ()");
  71. ops.impl("rotary_embedding", torch::kCPU, &rotary_embedding);
  72. }
  73. TORCH_LIBRARY_EXPAND(CONCAT(TORCH_EXTENSION_NAME, _cache_ops), cache_ops) {
  74. // Cache ops
  75. // Swap in (out) the cache blocks from src to dst.
  76. cache_ops.def(
  77. "swap_blocks(Tensor src, Tensor! dst, Tensor block_mapping) -> ()");
  78. cache_ops.impl("swap_blocks", torch::kCPU, &swap_blocks);
  79. // Copy the cache blocks from src to dst.
  80. cache_ops.def(
  81. "copy_blocks(Tensor[]! key_caches, Tensor[]! value_caches, Tensor "
  82. "block_mapping) -> ()");
  83. cache_ops.impl("copy_blocks", torch::kCPU, &copy_blocks);
  84. // Reshape the key and value tensors and cache them.
  85. cache_ops.def(
  86. "reshape_and_cache(Tensor key, Tensor value,"
  87. " Tensor! key_cache, Tensor! value_cache,"
  88. " Tensor slot_mapping,"
  89. " str kv_cache_dtype,"
  90. " float k_scale, float v_scale) -> ()");
  91. cache_ops.impl("reshape_and_cache", torch::kCPU, &reshape_and_cache);
  92. }
  93. TORCH_LIBRARY_EXPAND(CONCAT(TORCH_EXTENSION_NAME, _utils), utils) {
  94. // CPU utils
  95. utils.def("init_cpu_threads_env(str cpu_ids) -> ()", &init_cpu_threads_env);
  96. }
  97. REGISTER_EXTENSION(TORCH_EXTENSION_NAME)