flash.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /******************************************************************************
  2. * Copyright (c) 2023, Tri Dao.
  3. ******************************************************************************/
  4. #pragma once
  5. #include <cuda.h>
  6. #include <vector>
  7. #ifdef OLD_GENERATOR_PATH
  8. #include <ATen/CUDAGeneratorImpl.h>
  9. #else
  10. #include <ATen/cuda/CUDAGeneratorImpl.h>
  11. #endif
  12. #include <ATen/cuda/CUDAGraphsUtils.cuh> // For at::cuda::philox::unpack
  13. #include "cutlass/fast_math.h" // For cutlass::FastDivmod
  14. ////////////////////////////////////////////////////////////////////////////////////////////////////
  15. struct Qkv_params {
  16. using index_t = int64_t;
  17. // The QKV matrices.
  18. void *__restrict__ q_ptr;
  19. void *__restrict__ k_ptr;
  20. void *__restrict__ v_ptr;
  21. // The stride between rows of the Q, K and V matrices.
  22. index_t q_batch_stride;
  23. index_t k_batch_stride;
  24. index_t v_batch_stride;
  25. index_t q_row_stride;
  26. index_t k_row_stride;
  27. index_t v_row_stride;
  28. index_t q_head_stride;
  29. index_t k_head_stride;
  30. index_t v_head_stride;
  31. // The number of heads.
  32. int h, h_k;
  33. // In the case of multi-query and grouped-query attention (MQA/GQA), nheads_k could be
  34. // different from nheads (query).
  35. int h_h_k_ratio; // precompute h / h_k,
  36. };
  37. ////////////////////////////////////////////////////////////////////////////////////////////////////
  38. struct Flash_fwd_params : public Qkv_params {
  39. // The O matrix (output).
  40. void * __restrict__ o_ptr;
  41. void * __restrict__ oaccum_ptr;
  42. // The stride between rows of O.
  43. index_t o_batch_stride;
  44. index_t o_row_stride;
  45. index_t o_head_stride;
  46. // The pointer to the P matrix.
  47. void * __restrict__ p_ptr;
  48. // The pointer to the softmax sum.
  49. void * __restrict__ softmax_lse_ptr;
  50. void * __restrict__ softmax_lseaccum_ptr;
  51. // The dimensions.
  52. int b, seqlen_q, seqlen_k, seqlen_knew, d, seqlen_q_rounded, seqlen_k_rounded, d_rounded, rotary_dim;
  53. // The scaling factors for the kernel.
  54. float scale_softmax;
  55. float scale_softmax_log2;
  56. uint32_t scale_softmax_log2_half2;
  57. // array of length b+1 holding starting offset of each sequence.
  58. int * __restrict__ cu_seqlens_q;
  59. int * __restrict__ cu_seqlens_k;
  60. // If provided, the actual length of each k sequence.
  61. int * __restrict__ seqused_k;
  62. int *__restrict__ blockmask;
  63. // The K_new and V_new matrices.
  64. void * __restrict__ knew_ptr;
  65. void * __restrict__ vnew_ptr;
  66. // The stride between rows of the Q, K and V matrices.
  67. index_t knew_batch_stride;
  68. index_t vnew_batch_stride;
  69. index_t knew_row_stride;
  70. index_t vnew_row_stride;
  71. index_t knew_head_stride;
  72. index_t vnew_head_stride;
  73. // The cos and sin matrices for rotary embedding.
  74. void * __restrict__ rotary_cos_ptr;
  75. void * __restrict__ rotary_sin_ptr;
  76. // The indices to index into the KV cache.
  77. int * __restrict__ cache_batch_idx;
  78. // Paged KV cache
  79. int * __restrict__ block_table;
  80. index_t block_table_batch_stride;
  81. int page_block_size;
  82. // The dropout probability (probability of keeping an activation).
  83. float p_dropout;
  84. // uint32_t p_dropout_in_uint;
  85. // uint16_t p_dropout_in_uint16_t;
  86. uint8_t p_dropout_in_uint8_t;
  87. // Scale factor of 1 / (1 - p_dropout).
  88. float rp_dropout;
  89. float scale_softmax_rp_dropout;
  90. // Local window size
  91. int window_size_left, window_size_right;
  92. // Random state.
  93. at::PhiloxCudaState philox_args;
  94. // Pointer to the RNG seed (idx 0) and offset (idx 1).
  95. uint64_t * rng_state;
  96. bool is_bf16;
  97. bool is_e4m3;
  98. bool is_causal;
  99. // If is_seqlens_k_cumulative, then seqlen_k is cu_seqlens_k[bidb + 1] - cu_seqlens_k[bidb].
  100. // Otherwise it's cu_seqlens_k[bidb], i.e., we use cu_seqlens_k to store the sequence lengths of K.
  101. bool is_seqlens_k_cumulative;
  102. bool is_rotary_interleaved;
  103. int num_splits; // For split-KV version
  104. void * __restrict__ alibi_slopes_ptr;
  105. index_t alibi_slopes_batch_stride;
  106. int * __restrict__ tile_count_semaphore;
  107. };
  108. ////////////////////////////////////////////////////////////////////////////////////////////////////
  109. struct Flash_bwd_params : public Flash_fwd_params {
  110. // The dO and dQKV matrices.
  111. void *__restrict__ do_ptr;
  112. void *__restrict__ dq_ptr;
  113. void *__restrict__ dk_ptr;
  114. void *__restrict__ dv_ptr;
  115. // To accumulate dQ
  116. void *__restrict__ dq_accum_ptr;
  117. void *__restrict__ dk_accum_ptr;
  118. void *__restrict__ dv_accum_ptr;
  119. // // To accumulate dK and dV in case we're splitting the bwd along seqlen_q
  120. // dimension void *__restrict__ dk_accum_ptr; void *__restrict__
  121. // dv_accum_ptr;
  122. // The stride between rows of the dO, dQ, dK and dV matrices.
  123. // TD [2022-04-16]: We're using 32-bit indexing to save registers.
  124. // The code probably won't work for arrays larger than 2GB.
  125. index_t do_batch_stride;
  126. index_t do_row_stride;
  127. index_t do_head_stride;
  128. index_t dq_batch_stride;
  129. index_t dk_batch_stride;
  130. index_t dv_batch_stride;
  131. index_t dq_row_stride;
  132. index_t dk_row_stride;
  133. index_t dv_row_stride;
  134. index_t dq_head_stride;
  135. index_t dk_head_stride;
  136. index_t dv_head_stride;
  137. // The pointer to the softmax d sum.
  138. void *__restrict__ dsoftmax_sum;
  139. int *__restrict__ dq_semaphore;
  140. bool deterministic;
  141. index_t dq_accum_split_stride;
  142. };
  143. ////////////////////////////////////////////////////////////////////////////////////////////////////
  144. template<typename T, int Headdim> void run_mha_fwd_(Flash_fwd_params &params, cudaStream_t stream);
  145. template<typename T, int Headdim> void run_mha_bwd_(Flash_bwd_params &params, cudaStream_t stream);