gla.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <sycl/sycl.hpp>
  2. #include "common.hpp"
  3. template <u_int HEAD_SIZE>
  4. static void gated_linear_attn_f32_kernel(const dpct::queue_ptr stream, u_int B, u_int T, u_int C, u_int H, float scale,
  5. const float * k, const float * v, const float * r, const float * td,
  6. const float * s, float * dst) {
  7. const u_int head_size = HEAD_SIZE;
  8. const u_int state_size = C * head_size;
  9. const u_int n_seq_tokens = T / B;
  10. sycl::range<1> block_dims((C / H));
  11. sycl::range<1> grid_dims((B * H));
  12. stream->submit([&](sycl::handler & cgh) {
  13. /* local memory accessors*/
  14. auto _k = sycl::local_accessor<float, 1>(sycl::range<1>(head_size), cgh);
  15. auto _r = sycl::local_accessor<float, 1>(sycl::range<1>(head_size), cgh);
  16. auto _td = sycl::local_accessor<float, 1>(sycl::range<1>(head_size), cgh);
  17. cgh.parallel_for(sycl::nd_range<1>(grid_dims * block_dims, block_dims), [=](sycl::nd_item<1> item) {
  18. u_int tid = item.get_local_id(0);
  19. u_int bid = item.get_group(0);
  20. u_int batch_i = bid / H;
  21. u_int head_i = bid % H;
  22. float state[head_size];
  23. #pragma unroll
  24. for (u_int i = 0; i < head_size; i++) {
  25. state[i] = s[batch_i * state_size + head_i * head_size * head_size + i * head_size + tid];
  26. }
  27. for (u_int t = batch_i * n_seq_tokens * C + head_i * head_size + tid;
  28. t < (batch_i + 1) * n_seq_tokens * C + head_i * head_size + tid; t += C) {
  29. item.barrier(sycl::access::fence_space::local_space); //sync threads
  30. _k[tid] = k[t];
  31. _r[tid] = r[t];
  32. _td[tid] = td[t];
  33. item.barrier(sycl::access::fence_space::local_space); //sync threads
  34. const float _v = v[t];
  35. float y = 0;
  36. for (u_int j = 0; j < head_size; j += 4) {
  37. const sycl::float4 & k = (sycl::float4 &) (_k[j]);
  38. const sycl::float4 & r = (sycl::float4 &) (_r[j]);
  39. const sycl::float4 & td = (sycl::float4 &) (_td[j]);
  40. sycl::float4 & s = (sycl::float4 &) (state[j]);
  41. sycl::float4 kv;
  42. kv.x() = k.x() * _v;
  43. kv.y() = k.y() * _v;
  44. kv.z() = k.z() * _v;
  45. kv.w() = k.w() * _v;
  46. s.x() = s.x() * td.x() + kv.x();
  47. s.y() = s.y() * td.y() + kv.y();
  48. s.z() = s.z() * td.z() + kv.z();
  49. s.w() = s.w() * td.w() + kv.w();
  50. y += r.x() * s.x();
  51. y += r.y() * s.y();
  52. y += r.z() * s.z();
  53. y += r.w() * s.w();
  54. }
  55. dst[t] = y * scale;
  56. }
  57. #pragma unroll
  58. for (u_int i = 0; i < head_size; i++) {
  59. dst[T * C + batch_i * state_size + head_i * head_size * head_size + i * head_size + tid] = state[i];
  60. }
  61. });
  62. });
  63. }
  64. void ggml_sycl_op_gated_linear_attn(ggml_backend_sycl_context & ctx, ggml_tensor * dst) {
  65. scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/5);
  66. const float * k_d = static_cast<const float *>(dst->src[0]->data);
  67. const float * v_d = static_cast<const float *>(dst->src[1]->data);
  68. const float * r_d = static_cast<const float *>(dst->src[2]->data);
  69. const float * td_d = static_cast<const float *>(dst->src[3]->data);
  70. const float * s_d = static_cast<const float *>(dst->src[4]->data);
  71. const int64_t B = dst->src[4]->ne[1];
  72. const int64_t T = dst->src[0]->ne[2];
  73. const int64_t C = dst->ne[0];
  74. const int64_t H = dst->src[0]->ne[1];
  75. dpct::queue_ptr stream = ctx.stream();
  76. GGML_ASSERT(dst->src[4]->type == GGML_TYPE_F32);
  77. GGML_ASSERT(C % H == 0);
  78. GGML_ASSERT(C / H == 64 || C / H == 128);
  79. float scale;
  80. memcpy(&scale, dst->op_params, sizeof(float));
  81. float * dst_d = (float *) dst->data;
  82. if (C / H == 64) {
  83. gated_linear_attn_f32_kernel<64>(stream, B, T, C, H, scale, k_d, v_d, r_d, td_d, s_d, dst_d);
  84. } else {
  85. gated_linear_attn_f32_kernel<128>(stream, B, T, C, H, scale, k_d, v_d, r_d, td_d, s_d, dst_d);
  86. }
  87. }