flash.h 5.7 KB

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