rope.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include "rope.hpp"
  2. struct rope_corr_dims {
  3. float v[2];
  4. };
  5. static float rope_yarn_ramp(const float low, const float high, const int i0) {
  6. const float y = (i0 / 2 - low) / sycl::max(0.001f, high - low);
  7. return 1.0f - sycl::min(1.0f, sycl::max(0.0f, y));
  8. }
  9. // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
  10. // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
  11. static void rope_yarn(
  12. float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
  13. float * cos_theta, float * sin_theta) {
  14. // Get n-d rotational scaling corrected for extrapolation
  15. float theta_interp = freq_scale * theta_extrap;
  16. float theta = theta_interp;
  17. if (ext_factor != 0.0f) {
  18. float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
  19. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  20. // Get n-d magnitude scaling corrected for interpolation
  21. mscale *= 1.0f + 0.1f * sycl::log(1.0f / freq_scale);
  22. }
  23. *cos_theta = sycl::cos(theta) * mscale;
  24. *sin_theta = sycl::sin(theta) * mscale;
  25. }
  26. template<typename T, bool has_ff>
  27. static void rope_norm(
  28. const T * x, T * dst, int ne0, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  29. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, const float * freq_factors,
  30. const sycl::nd_item<3> &item_ct1) {
  31. const int i0 = 2 * (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  32. item_ct1.get_local_id(1));
  33. if (i0 >= ne0) {
  34. return;
  35. }
  36. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  37. item_ct1.get_local_id(2);
  38. if (i0 >= n_dims) {
  39. const int i = row*ne0 + i0;
  40. dst[i + 0] = x[i + 0];
  41. dst[i + 1] = x[i + 1];
  42. return;
  43. }
  44. const int i = row*ne0 + i0;
  45. const int i2 = row/p_delta_rows;
  46. const float theta_base = pos[i2] * sycl::pow(theta_scale, i0 / 2.0f);
  47. const float freq_factor = has_ff ? freq_factors[i0/2] : 1.0f;
  48. float cos_theta;
  49. float sin_theta;
  50. rope_yarn(theta_base/freq_factor, freq_scale, corr_dims, i0, ext_factor, attn_factor, &cos_theta, &sin_theta);
  51. const float x0 = x[i + 0];
  52. const float x1 = x[i + 1];
  53. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  54. dst[i + 1] = x0*sin_theta + x1*cos_theta;
  55. }
  56. template<typename T, bool has_ff>
  57. static void rope_neox(
  58. const T * x, T * dst, int ne0, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
  59. float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, const float * freq_factors,
  60. const sycl::nd_item<3> &item_ct1) {
  61. const int i0 = 2 * (item_ct1.get_local_range(1) * item_ct1.get_group(1) +
  62. item_ct1.get_local_id(1));
  63. if (i0 >= ne0) {
  64. return;
  65. }
  66. const int row = item_ct1.get_local_range(2) * item_ct1.get_group(2) +
  67. item_ct1.get_local_id(2);
  68. if (i0 >= n_dims) {
  69. const int i = row*ne0 + i0;
  70. dst[i + 0] = x[i + 0];
  71. dst[i + 1] = x[i + 1];
  72. return;
  73. }
  74. const int i = row*ne0 + i0/2;
  75. const int i2 = row/p_delta_rows;
  76. const float theta_base = pos[i2] * sycl::pow(theta_scale, i0 / 2.0f);
  77. const float freq_factor = has_ff ? freq_factors[i0/2] : 1.0f;
  78. float cos_theta;
  79. float sin_theta;
  80. rope_yarn(theta_base/freq_factor, freq_scale, corr_dims, i0, ext_factor, attn_factor, &cos_theta, &sin_theta);
  81. const float x0 = x[i + 0];
  82. const float x1 = x[i + n_dims/2];
  83. dst[i + 0] = x0*cos_theta - x1*sin_theta;
  84. dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
  85. }
  86. template <typename T>
  87. static void rope_norm_sycl(
  88. const T *x, T *dst, int ne0, int n_dims, int nr, const int32_t *pos, float freq_scale, int p_delta_rows,
  89. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, const float * freq_factors, queue_ptr stream) {
  90. GGML_ASSERT(ne0 % 2 == 0);
  91. const sycl::range<3> block_dims(1, SYCL_ROPE_BLOCK_SIZE, 1);
  92. const int num_blocks_x = (ne0 + 2*SYCL_ROPE_BLOCK_SIZE - 1) / (2*SYCL_ROPE_BLOCK_SIZE);
  93. const sycl::range<3> block_nums(1, num_blocks_x, nr);
  94. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  95. dpct::has_capability_or_fail(stream->get_device(),
  96. {sycl::aspect::fp16});
  97. if (freq_factors == nullptr) {
  98. /*
  99. DPCT1049:40: The work-group size passed to the SYCL kernel may exceed
  100. the limit. To get the device limit, query
  101. info::device::max_work_group_size. Adjust the work-group size if needed.
  102. */
  103. stream->parallel_for(
  104. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  105. [=](sycl::nd_item<3> item_ct1) {
  106. rope_norm<T, false>(x, dst, ne0, n_dims, pos, freq_scale, p_delta_rows,
  107. ext_factor, attn_factor, corr_dims, theta_scale, freq_factors,
  108. item_ct1);
  109. });
  110. } else {
  111. /*
  112. DPCT1049:41: The work-group size passed to the SYCL kernel may exceed
  113. the limit. To get the device limit, query
  114. info::device::max_work_group_size. Adjust the work-group size if needed.
  115. */
  116. stream->parallel_for(
  117. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  118. [=](sycl::nd_item<3> item_ct1) {
  119. rope_norm<T, true>(x, dst, ne0, n_dims, pos, freq_scale, p_delta_rows,
  120. ext_factor, attn_factor, corr_dims, theta_scale, freq_factors,
  121. item_ct1);
  122. });
  123. }
  124. }
  125. template <typename T>
  126. static void rope_neox_sycl(
  127. const T *x, T *dst, int ne0, int n_dims, int nr, const int32_t *pos, float freq_scale, int p_delta_rows,
  128. float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, const float * freq_factors, queue_ptr stream) {
  129. GGML_ASSERT(ne0 % 2 == 0);
  130. const sycl::range<3> block_dims(1, SYCL_ROPE_BLOCK_SIZE, 1);
  131. const int num_blocks_x = (ne0 + 2*SYCL_ROPE_BLOCK_SIZE - 1) / (2*SYCL_ROPE_BLOCK_SIZE);
  132. const sycl::range<3> block_nums(1, num_blocks_x, nr);
  133. const float theta_scale = powf(freq_base, -2.0f/n_dims);
  134. dpct::has_capability_or_fail(stream->get_device(),
  135. {sycl::aspect::fp16});
  136. if (freq_factors == nullptr) {
  137. stream->parallel_for(
  138. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  139. [=](sycl::nd_item<3> item_ct1) {
  140. rope_neox<T, false>(x, dst, ne0, n_dims, pos, freq_scale,
  141. p_delta_rows, ext_factor, attn_factor,
  142. corr_dims, theta_scale, freq_factors,
  143. item_ct1);
  144. });
  145. } else {
  146. stream->parallel_for(
  147. sycl::nd_range<3>(block_nums * block_dims, block_dims),
  148. [=](sycl::nd_item<3> item_ct1) {
  149. rope_neox<T, true>(x, dst, ne0, n_dims, pos, freq_scale,
  150. p_delta_rows, ext_factor, attn_factor,
  151. corr_dims, theta_scale, freq_factors,
  152. item_ct1);
  153. });
  154. }
  155. }
  156. void ggml_sycl_op_rope(
  157. ggml_backend_sycl_context & ctx, const ggml_tensor *src0, const ggml_tensor *src1, ggml_tensor *dst,
  158. const float *src0_dd, const float *src1_dd, float *dst_dd, const queue_ptr &main_stream) {
  159. const ggml_tensor * src2 = dst->src[2];
  160. GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
  161. GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
  162. GGML_ASSERT(src0->type == dst->type);
  163. const int64_t ne00 = src0->ne[0];
  164. const int64_t ne01 = src0->ne[1];
  165. const int64_t nr = ggml_nrows(src0);
  166. //const int n_past = ((int32_t *) dst->op_params)[0];
  167. const int n_dims = ((int32_t *) dst->op_params)[1];
  168. const int mode = ((int32_t *) dst->op_params)[2];
  169. //const int n_ctx = ((int32_t *) dst->op_params)[3];
  170. const int n_ctx_orig = ((int32_t *) dst->op_params)[4];
  171. // RoPE alteration for extended context
  172. float freq_base;
  173. float freq_scale;
  174. float ext_factor;
  175. float attn_factor;
  176. float beta_fast;
  177. float beta_slow;
  178. memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
  179. memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
  180. memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
  181. memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
  182. memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
  183. memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
  184. const bool is_neox = mode & GGML_ROPE_TYPE_NEOX;
  185. const int32_t * pos = (const int32_t *) src1_dd;
  186. const float * freq_factors = nullptr;
  187. if (src2 != nullptr) {
  188. freq_factors = (const float *) src2->data;
  189. }
  190. rope_corr_dims corr_dims;
  191. ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims.v);
  192. // compute
  193. if (is_neox) {
  194. if (src0->type == GGML_TYPE_F32) {
  195. rope_neox_sycl(
  196. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nr, pos, freq_scale, ne01, freq_base, ext_factor,
  197. attn_factor, corr_dims, freq_factors, main_stream
  198. );
  199. } else if (src0->type == GGML_TYPE_F16) {
  200. rope_neox_sycl(
  201. (const sycl::half *)src0_dd, (sycl::half *)dst_dd, ne00, n_dims, nr, pos, freq_scale, ne01, freq_base, ext_factor,
  202. attn_factor, corr_dims, freq_factors, main_stream
  203. );
  204. } else {
  205. GGML_ABORT("fatal error");
  206. }
  207. } else {
  208. if (src0->type == GGML_TYPE_F32) {
  209. rope_norm_sycl(
  210. (const float *)src0_dd, (float *)dst_dd, ne00, n_dims, nr, pos, freq_scale, ne01, freq_base, ext_factor,
  211. attn_factor, corr_dims, freq_factors, main_stream
  212. );
  213. } else if (src0->type == GGML_TYPE_F16) {
  214. rope_norm_sycl(
  215. (const sycl::half *)src0_dd, (sycl::half *)dst_dd, ne00, n_dims, nr, pos, freq_scale, ne01, freq_base, ext_factor,
  216. attn_factor, corr_dims, freq_factors, main_stream
  217. );
  218. } else {
  219. GGML_ABORT("fatal error");
  220. }
  221. }
  222. GGML_UNUSED(src1);
  223. GGML_UNUSED(dst);
  224. GGML_UNUSED(src1_dd);
  225. GGML_UNUSED(ctx);
  226. }