ops.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #pragma once
  2. #include <optional>
  3. #include <torch/library.h>
  4. void paged_attention_v1(
  5. torch::Tensor& out, torch::Tensor& query, torch::Tensor& key_cache,
  6. torch::Tensor& value_cache, int64_t num_kv_heads, double scale,
  7. torch::Tensor& block_tables, torch::Tensor& seq_lens, int64_t block_size,
  8. int64_t max_seq_len, const c10::optional<torch::Tensor>& alibi_slopes,
  9. const std::string& kv_cache_dtype, double k_scale, double v_scale,
  10. const int64_t tp_rank, const int64_t blocksparse_local_blocks,
  11. const int64_t blocksparse_vert_stride, const int64_t blocksparse_block_size,
  12. const int64_t blocksparse_head_sliding_step);
  13. void paged_attention_v2(
  14. torch::Tensor& out, torch::Tensor& exp_sums, torch::Tensor& max_logits,
  15. torch::Tensor& tmp_out, torch::Tensor& query, torch::Tensor& key_cache,
  16. torch::Tensor& value_cache, int64_t num_kv_heads, double scale,
  17. torch::Tensor& block_tables, torch::Tensor& seq_lens, int64_t block_size,
  18. int64_t max_seq_len, const c10::optional<torch::Tensor>& alibi_slopes,
  19. const std::string& kv_cache_dtype, double k_scale, double v_scale,
  20. const int64_t tp_rank, const int64_t blocksparse_local_blocks,
  21. const int64_t blocksparse_vert_stride, const int64_t blocksparse_block_size,
  22. const int64_t blocksparse_head_sliding_step);
  23. void rms_norm(torch::Tensor& out, torch::Tensor& input, torch::Tensor& weight,
  24. double epsilon);
  25. void fused_add_rms_norm(torch::Tensor& input, torch::Tensor& residual,
  26. torch::Tensor& weight, double epsilon);
  27. void rotary_embedding(torch::Tensor& positions, torch::Tensor& query,
  28. torch::Tensor& key, int64_t head_size,
  29. torch::Tensor& cos_sin_cache, bool is_neox);
  30. void batched_rotary_embedding(torch::Tensor& positions, torch::Tensor& query,
  31. torch::Tensor& key, int64_t head_size,
  32. torch::Tensor& cos_sin_cache, bool is_neox,
  33. int64_t rot_dim,
  34. torch::Tensor& cos_sin_cache_offsets);
  35. void silu_and_mul(torch::Tensor& out, torch::Tensor& input);
  36. void gelu_and_mul(torch::Tensor& out, torch::Tensor& input);
  37. void gelu_tanh_and_mul(torch::Tensor& out, torch::Tensor& input);
  38. void gelu_new(torch::Tensor& out, torch::Tensor& input);
  39. void gelu_fast(torch::Tensor& out, torch::Tensor& input);
  40. void gelu_quick(torch::Tensor& out, torch::Tensor& input);
  41. void advance_step(int64_t num_seqs, int64_t num_queries, int64_t block_size,
  42. torch::Tensor& input_tokens, torch::Tensor& sampled_token_ids,
  43. torch::Tensor& input_positions, torch::Tensor& seq_lens,
  44. torch::Tensor& slot_mapping, torch::Tensor& block_tables);
  45. void moe_align_block_size(torch::Tensor topk_ids, int64_t num_experts,
  46. int64_t block_size, torch::Tensor sorted_token_ids,
  47. torch::Tensor expert_ids,
  48. torch::Tensor num_tokens_post_pad);
  49. #ifndef USE_ROCM
  50. using fptr_t = int64_t;
  51. fptr_t init_custom_ar(torch::Tensor& meta, torch::Tensor& rank_data,
  52. const std::vector<std::string>& handles,
  53. const std::vector<int64_t>& offsets, int64_t rank,
  54. bool full_nvlink);
  55. bool should_custom_ar(torch::Tensor& inp, int64_t max_size, int64_t world_size,
  56. bool full_nvlink);
  57. void all_reduce_reg(fptr_t _fa, torch::Tensor& inp, torch::Tensor& out);
  58. void all_reduce_unreg(fptr_t _fa, torch::Tensor& inp, torch::Tensor& reg_buffer,
  59. torch::Tensor& out);
  60. void dispose(fptr_t _fa);
  61. int64_t meta_size();
  62. void register_buffer(fptr_t _fa, torch::Tensor& t,
  63. const std::vector<std::string>& handles,
  64. const std::vector<int64_t>& offsets);
  65. std::tuple<torch::Tensor, std::vector<int64_t>> get_graph_buffer_ipc_meta(
  66. fptr_t _fa);
  67. void register_graph_buffers(fptr_t _fa, const std::vector<std::string>& handles,
  68. const std::vector<std::vector<int64_t>>& offsets);
  69. std::vector<torch::Tensor> selective_scan_fwd(
  70. const torch::Tensor& u, const torch::Tensor& delta, const torch::Tensor& A,
  71. const torch::Tensor& B, const torch::Tensor& C,
  72. const c10::optional<torch::Tensor>& D_,
  73. const c10::optional<torch::Tensor>& z_,
  74. const c10::optional<torch::Tensor>& delta_bias_, bool delta_softplus,
  75. const c10::optional<torch::Tensor>& index_,
  76. const c10::optional<torch::Tensor>& x);
  77. at::Tensor causal_conv1d_update(const at::Tensor& x,
  78. const at::Tensor& conv_state,
  79. const at::Tensor& weight,
  80. const c10::optional<at::Tensor>& bias_,
  81. bool silu_activation);
  82. at::Tensor causal_conv1d_fwd(const at::Tensor& x, const at::Tensor& weight,
  83. const c10::optional<at::Tensor>& bias_,
  84. const c10::optional<at::Tensor>& seq_idx_,
  85. const c10::optional<at::Tensor>& seq_pos_idx_,
  86. const c10::optional<at::Tensor>& initial_states_,
  87. const c10::optional<at::Tensor>& final_states_out_,
  88. bool silu_activation);
  89. // Sampling kernels
  90. torch::Tensor sampling_from_probs(torch::Tensor probs,
  91. torch::Tensor uniform_samples,
  92. bool deterministic);
  93. std::vector<torch::Tensor> top_p_sampling_from_probs(
  94. torch::Tensor probs, torch::Tensor uniform_samples,
  95. std::optional<torch::Tensor> maybe_top_p_arr, double top_p_val,
  96. bool deterministic);
  97. std::vector<torch::Tensor> top_k_sampling_from_probs(
  98. torch::Tensor probs, torch::Tensor uniform_samples,
  99. std::optional<torch::Tensor> maybe_top_k_arr, int64_t top_k_val,
  100. bool deterministic);
  101. std::vector<torch::Tensor> min_p_sampling_from_probs(
  102. torch::Tensor probs, torch::Tensor uniform_samples,
  103. std::optional<torch::Tensor> maybe_min_p_arr, double min_p_val,
  104. bool deterministic);
  105. std::vector<torch::Tensor> top_k_top_p_sampling_from_probs(
  106. torch::Tensor probs, torch::Tensor uniform_samples,
  107. std::optional<torch::Tensor> maybe_top_k_arr, double top_k_val,
  108. std::optional<torch::Tensor> maybe_top_p_arr, double top_p_val,
  109. bool deterministic);
  110. torch::Tensor top_p_renorm_prob(torch::Tensor probs,
  111. std::optional<torch::Tensor> maybe_top_p_arr,
  112. double top_p_val);
  113. torch::Tensor top_k_renorm_prob(torch::Tensor probs,
  114. std::optional<torch::Tensor> maybe_top_k_arr,
  115. int64_t top_k_val);
  116. torch::Tensor top_k_mask_logits(torch::Tensor logits,
  117. std::optional<torch::Tensor> maybe_top_k_arr,
  118. int64_t top_k_val);
  119. #endif