torch_bindings.cpp 4.2 KB

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