pos_encoding.cpp 7.3 KB

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