pos_encoding.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include "cpu_types.hpp"
  2. namespace {
  3. template <typename scalar_t>
  4. void rotary_embedding_impl(
  5. const int64_t* __restrict__ positions, // [batch_size, seq_len] or
  6. // [num_tokens]
  7. scalar_t* __restrict__ query, /// [batch_size, seq_len, num_heads,
  8. /// head_size] or [num_tokens, num_heads,
  9. /// head_size]
  10. scalar_t* __restrict__ key, // [batch_size, seq_len, num_kv_heads,
  11. // head_size] or [num_tokens, num_kv_heads,
  12. // head_size]
  13. const scalar_t* __restrict__ cos_sin_cache, // [max_position, 2, rot_dim //
  14. // 2]
  15. const int rot_dim, const int64_t query_stride, const int64_t key_stride,
  16. const int num_heads, const int num_kv_heads, const int head_size,
  17. const int num_tokens) {
  18. using scalar_vec_t = vec_op::vec_t<scalar_t>;
  19. constexpr int VEC_ELEM_NUM = scalar_vec_t::get_elem_num();
  20. const int embed_dim = rot_dim / 2;
  21. bool flag = (embed_dim % VEC_ELEM_NUM == 0);
  22. const int loop_upper = flag ? embed_dim : embed_dim - VEC_ELEM_NUM;
  23. auto compute_loop = [&](const int64_t token_head, const scalar_t* cache_ptr,
  24. scalar_t* qk) {
  25. int j = 0;
  26. for (; j < loop_upper; j += VEC_ELEM_NUM) {
  27. const int rot_offset = j;
  28. const int x_index = rot_offset;
  29. const int y_index = embed_dim + rot_offset;
  30. const int64_t out_x = token_head + x_index;
  31. const int64_t out_y = token_head + y_index;
  32. const scalar_vec_t cos(cache_ptr + x_index);
  33. const scalar_vec_t sin(cache_ptr + y_index);
  34. const scalar_vec_t q_x(qk + out_x);
  35. const scalar_vec_t q_y(qk + out_y);
  36. vec_op::FP32Vec8 fp32_cos(cos);
  37. vec_op::FP32Vec8 fp32_sin(sin);
  38. vec_op::FP32Vec8 fp32_q_x(q_x);
  39. vec_op::FP32Vec8 fp32_q_y(q_y);
  40. auto out1 = fp32_q_x * fp32_cos - fp32_q_y * fp32_sin;
  41. scalar_vec_t(out1).save(qk + out_x);
  42. auto out2 = fp32_q_y * fp32_cos + fp32_q_x * fp32_sin;
  43. scalar_vec_t(out2).save(qk + out_y);
  44. }
  45. if (!flag) {
  46. for (; j < embed_dim; ++j) {
  47. const int x_index = j;
  48. const int y_index = embed_dim + j;
  49. const int64_t out_x = token_head + x_index;
  50. const int64_t out_y = token_head + y_index;
  51. const float fp32_cos = cache_ptr[x_index];
  52. const float fp32_sin = cache_ptr[y_index];
  53. const float fp32_q_x = qk[out_x];
  54. const float fp32_q_y = qk[out_y];
  55. qk[out_x] = fp32_q_x * fp32_cos - fp32_q_y * fp32_sin;
  56. qk[out_y] = fp32_q_y * fp32_cos + fp32_q_x * fp32_sin;
  57. }
  58. }
  59. };
  60. #pragma omp parallel for
  61. for (int token_idx = 0; token_idx < num_tokens; ++token_idx) {
  62. int64_t pos = positions[token_idx];
  63. const scalar_t* cache_ptr = cos_sin_cache + pos * rot_dim;
  64. for (int i = 0; i < num_heads; ++i) {
  65. const int head_idx = i;
  66. const int64_t token_head =
  67. token_idx * query_stride + head_idx * head_size;
  68. compute_loop(token_head, cache_ptr, query);
  69. }
  70. for (int i = 0; i < num_kv_heads; ++i) {
  71. const int head_idx = i;
  72. const int64_t token_head = token_idx * key_stride + head_idx * head_size;
  73. compute_loop(token_head, cache_ptr, key);
  74. }
  75. }
  76. }
  77. template <typename scalar_t>
  78. void rotary_embedding_gptj_impl(
  79. const int64_t* __restrict__ positions, // [batch_size, seq_len] or
  80. // [num_tokens]
  81. scalar_t* __restrict__ query, /// [batch_size, seq_len, num_heads,
  82. /// head_size] or [num_tokens, num_heads,
  83. /// head_size]
  84. scalar_t* __restrict__ key, // [batch_size, seq_len, num_kv_heads,
  85. // head_size] or [num_tokens, num_kv_heads,
  86. // head_size]
  87. const scalar_t* __restrict__ cos_sin_cache, // [max_position, 2, rot_dim //
  88. // 2]
  89. const int rot_dim, const int64_t query_stride, const int64_t key_stride,
  90. const int num_heads, const int num_kv_heads, const int head_size,
  91. const int num_tokens) {
  92. const int embed_dim = rot_dim / 2;
  93. #pragma omp parallel for collapse(2)
  94. for (int token_idx = 0; token_idx < num_tokens; ++token_idx) {
  95. for (int i = 0; i < num_heads; ++i) {
  96. int64_t pos = positions[token_idx];
  97. const scalar_t* cache_ptr = cos_sin_cache + pos * rot_dim;
  98. const scalar_t* cos_cache_ptr = cache_ptr;
  99. const scalar_t* sin_cache_ptr = cache_ptr + embed_dim;
  100. const int head_idx = i;
  101. const int64_t token_head =
  102. token_idx * query_stride + head_idx * head_size;
  103. scalar_t* head_query = token_head + query;
  104. for (int j = 0; j < embed_dim; j += 1) {
  105. const int rot_offset = j;
  106. const int x_index = 2 * rot_offset;
  107. const int y_index = 2 * rot_offset + 1;
  108. const float cos = cos_cache_ptr[rot_offset];
  109. const float sin = sin_cache_ptr[rot_offset];
  110. const float x = head_query[x_index];
  111. const float y = head_query[y_index];
  112. head_query[x_index] = x * cos - y * sin;
  113. head_query[y_index] = y * cos + x * sin;
  114. }
  115. }
  116. }
  117. #pragma omp parallel for collapse(2)
  118. for (int token_idx = 0; token_idx < num_tokens; ++token_idx) {
  119. for (int i = 0; i < num_kv_heads; ++i) {
  120. int64_t pos = positions[token_idx];
  121. const scalar_t* cache_ptr = cos_sin_cache + pos * rot_dim;
  122. const scalar_t* cos_cache_ptr = cache_ptr;
  123. const scalar_t* sin_cache_ptr = cache_ptr + embed_dim;
  124. const int head_idx = i;
  125. const int64_t token_head = token_idx * key_stride + head_idx * head_size;
  126. scalar_t* head_key = key + token_head;
  127. for (int j = 0; j < embed_dim; j += 1) {
  128. const int rot_offset = j;
  129. const int x_index = 2 * rot_offset;
  130. const int y_index = 2 * rot_offset + 1;
  131. const float cos = cos_cache_ptr[rot_offset];
  132. const float sin = sin_cache_ptr[rot_offset];
  133. const float x = head_key[x_index];
  134. const float y = head_key[y_index];
  135. head_key[x_index] = x * cos - y * sin;
  136. head_key[y_index] = y * cos + x * sin;
  137. }
  138. }
  139. }
  140. }
  141. }; // namespace
  142. void rotary_embedding(torch::Tensor& positions, torch::Tensor& query,
  143. torch::Tensor& key, int64_t head_size,
  144. torch::Tensor& cos_sin_cache, bool is_neox) {
  145. int num_tokens = query.numel() / query.size(-1);
  146. int rot_dim = cos_sin_cache.size(1);
  147. int num_heads = query.size(-1) / head_size;
  148. int num_kv_heads = key.size(-1) / head_size;
  149. int64_t key_stride = key.stride(-2);
  150. int64_t query_stride = query.stride(-2);
  151. APHRODITE_DISPATCH_FLOATING_TYPES(
  152. query.scalar_type(), "rotary_embedding_impl", [&] {
  153. CPU_KERNEL_GUARD_IN(rotary_embedding_impl)
  154. if (is_neox) {
  155. rotary_embedding_impl(
  156. positions.data_ptr<int64_t>(), query.data_ptr<scalar_t>(),
  157. key.data_ptr<scalar_t>(), cos_sin_cache.data_ptr<scalar_t>(),
  158. rot_dim, query_stride, key_stride, num_heads, num_kv_heads,
  159. head_size, num_tokens);
  160. } else {
  161. rotary_embedding_gptj_impl(
  162. positions.data_ptr<int64_t>(), query.data_ptr<scalar_t>(),
  163. key.data_ptr<scalar_t>(), cos_sin_cache.data_ptr<scalar_t>(),
  164. rot_dim, query_stride, key_stride, num_heads, num_kv_heads,
  165. head_size, num_tokens);
  166. }
  167. CPU_KERNEL_GUARD_OUT(rotary_embedding_impl)
  168. });
  169. }