wkv.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #include <sycl/sycl.hpp>
  2. #include "wkv.hpp"
  3. constexpr int WKV_BLOCK_SIZE = 64; // Matching CUDA_WKV_BLOCK_SIZE
  4. // Helper function for the main kernel
  5. template <int block_size>
  6. static void rwkv_wkv6_f32_kernel(
  7. const int B, const int T, const int C, const int H,
  8. const float* k, const float* v, const float* r,
  9. const float* tf, const float* td, const float* s,
  10. float* dst, const sycl::nd_item<3>& item_ct1, float* shared_mem) {
  11. const int tid = item_ct1.get_local_id(2);
  12. const int bid = item_ct1.get_group(2);
  13. const int head_size = block_size;
  14. const int batch_i = bid / H;
  15. const int head_i = bid % H;
  16. const int state_size = C * head_size;
  17. const int n_seq_tokens = T / B;
  18. // Set up shared memory pointers
  19. float* _k = shared_mem;
  20. float* _r = _k + head_size;
  21. float* _tf = _r + head_size;
  22. float* _td = _tf + head_size;
  23. // Local state array
  24. float state[block_size];
  25. // Load initial state
  26. #pragma unroll
  27. for (int i = 0; i < head_size; i++) {
  28. state[i] = s[batch_i * state_size + head_i * head_size * head_size + i * head_size + tid];
  29. }
  30. // Sync threads before shared memory operations
  31. item_ct1.barrier(sycl::access::fence_space::local_space);
  32. // Load time-mixing parameters
  33. _tf[tid] = tf[head_i * head_size + tid];
  34. item_ct1.barrier(sycl::access::fence_space::local_space);
  35. // Main sequence processing loop
  36. for (int t = batch_i * n_seq_tokens * C + head_i * head_size + tid;
  37. t < (batch_i + 1) * n_seq_tokens * C + head_i * head_size + tid;
  38. t += C) {
  39. item_ct1.barrier(sycl::access::fence_space::local_space);
  40. // Load current timestep data to shared memory
  41. _k[tid] = k[t];
  42. _r[tid] = r[t];
  43. _td[tid] = td[t];
  44. item_ct1.barrier(sycl::access::fence_space::local_space);
  45. const float _v = v[t];
  46. float y = 0;
  47. // Process in chunks of 4 for better vectorization
  48. sycl::float4 k4, r4, tf4, td4, s4;
  49. #pragma unroll
  50. for (int j = 0; j < head_size; j += 4) {
  51. // Load data in vec4 chunks
  52. k4 = sycl::float4(_k[j], _k[j+1], _k[j+2], _k[j+3]);
  53. r4 = sycl::float4(_r[j], _r[j+1], _r[j+2], _r[j+3]);
  54. tf4 = sycl::float4(_tf[j], _tf[j+1], _tf[j+2], _tf[j+3]);
  55. td4 = sycl::float4(_td[j], _td[j+1], _td[j+2], _td[j+3]);
  56. s4 = sycl::float4(state[j], state[j+1], state[j+2], state[j+3]);
  57. // Compute key-value product
  58. sycl::float4 kv4 = k4 * _v;
  59. // Accumulate weighted sum
  60. y += sycl::dot(r4, tf4 * kv4 + s4);
  61. // Update state
  62. s4 = s4 * td4 + kv4;
  63. // Store updated state
  64. state[j] = s4.x();
  65. state[j+1] = s4.y();
  66. state[j+2] = s4.z();
  67. state[j+3] = s4.w();
  68. }
  69. dst[t] = y;
  70. }
  71. // Save final state
  72. #pragma unroll
  73. for (int i = 0; i < head_size; i++) {
  74. dst[T * C + batch_i * state_size + head_i * head_size * head_size + i * head_size + tid] = state[i];
  75. }
  76. }
  77. template <int block_size>
  78. static void rwkv_wkv7_f32_kernel(
  79. const int B, const int T, const int C, const int H,
  80. const float* r, const float* w, const float* k, const float* v,
  81. const float* a, const float* b, const float* s,
  82. float* dst, const sycl::nd_item<3>& item_ct1, float* shared_mem) {
  83. const int tid = item_ct1.get_local_id(2);
  84. const int bid = item_ct1.get_group(2);
  85. const int head_size = block_size;
  86. const int batch_i = bid / H;
  87. const int head_i = bid % H;
  88. const int state_size = C * head_size;
  89. const int n_seq_tokens = T / B;
  90. float* _r = shared_mem;
  91. float* _w = _r + head_size;
  92. float* _k = _w + head_size;
  93. float* _a = _k + head_size;
  94. float* _b = _a + head_size;
  95. float state[block_size];
  96. #pragma unroll
  97. for (int i = 0; i < head_size; i++) {
  98. state[i] = s[batch_i * state_size + head_i * head_size * head_size + tid * head_size + i];
  99. }
  100. for (int t = batch_i * n_seq_tokens * C + head_i * head_size + tid;
  101. t < (batch_i + 1) * n_seq_tokens * C + head_i * head_size + tid;
  102. t += C) {
  103. item_ct1.barrier(sycl::access::fence_space::local_space);
  104. _r[tid] = r[t];
  105. _w[tid] = w[t];
  106. _k[tid] = k[t];
  107. _a[tid] = a[t];
  108. _b[tid] = b[t];
  109. item_ct1.barrier(sycl::access::fence_space::local_space);
  110. const float _v = v[t];
  111. float y = 0, sa = 0;
  112. sycl::float4 a4, s4;
  113. #pragma unroll
  114. for (int j = 0; j < head_size; j += 4) {
  115. a4 = sycl::float4(_a[j], _a[j+1], _a[j+2], _a[j+3]);
  116. s4 = sycl::float4(state[j], state[j+1], state[j+2], state[j+3]);
  117. sa += sycl::dot(a4, s4);
  118. }
  119. sycl::float4 r4, w4, k4, b4;
  120. #pragma unroll
  121. for (int j = 0; j < head_size; j += 4) {
  122. r4 = sycl::float4(_r[j], _r[j+1], _r[j+2], _r[j+3]);
  123. w4 = sycl::float4(_w[j], _w[j+1], _w[j+2], _w[j+3]);
  124. k4 = sycl::float4(_k[j], _k[j+1], _k[j+2], _k[j+3]);
  125. b4 = sycl::float4(_b[j], _b[j+1], _b[j+2], _b[j+3]);
  126. s4 = sycl::float4(state[j], state[j+1], state[j+2], state[j+3]);
  127. sycl::float4 kv4 = k4 * _v;
  128. s4 = s4 * w4 + kv4 + sa * b4;
  129. y += sycl::dot(r4, s4);
  130. state[j] = s4.x();
  131. state[j+1] = s4.y();
  132. state[j+2] = s4.z();
  133. state[j+3] = s4.w();
  134. }
  135. dst[t] = y;
  136. }
  137. #pragma unroll
  138. for (int i = 0; i < head_size; i++) {
  139. dst[T * C + batch_i * state_size + head_i * head_size * head_size + tid * head_size + i] = state[i];
  140. }
  141. }
  142. void ggml_sycl_op_rwkv_wkv6(ggml_backend_sycl_context& ctx, ggml_tensor* dst) {
  143. const ggml_tensor *src0 = dst->src[0];
  144. const ggml_tensor *src1 = dst->src[1];
  145. const float* k_d = (const float*)dst->src[0]->data;
  146. const float* v_d = (const float*)dst->src[1]->data;
  147. const float* r_d = (const float*)dst->src[2]->data;
  148. const float* tf_d = (const float*)dst->src[3]->data;
  149. const float* td_d = (const float*)dst->src[4]->data;
  150. const float* s_d = (const float*)dst->src[5]->data;
  151. float* dst_d = (float*)dst->data;
  152. const int64_t B = dst->src[5]->ne[1];
  153. const int64_t T = dst->src[0]->ne[2];
  154. const int64_t C = dst->ne[0];
  155. const int64_t H = dst->src[0]->ne[1];
  156. GGML_ASSERT(dst->src[5]->type == GGML_TYPE_F32);
  157. GGML_ASSERT(C % H == 0);
  158. GGML_ASSERT(C / H == WKV_BLOCK_SIZE || C / H == WKV_BLOCK_SIZE * 2); // The current sycl kernel is designed for RWKV6, HEAD_SIZE == 64
  159. dpct::queue_ptr stream = ctx.stream();
  160. // Calculate execution configuration
  161. const size_t shared_mem_size = C / H * 4 * sizeof(float); // For k, r, tf, td
  162. sycl::range<3> block_dims(1, 1, C / H);
  163. sycl::range<3> grid_dims(1, 1, B * H);
  164. // Submit kernel
  165. if (C / H == WKV_BLOCK_SIZE) {
  166. stream->submit([&](sycl::handler& cgh) {
  167. sycl::local_accessor<float, 1> shared_mem_acc(shared_mem_size, cgh);
  168. cgh.parallel_for(
  169. sycl::nd_range<3>(grid_dims * block_dims, block_dims),
  170. [=](sycl::nd_item<3> item_ct1) {
  171. rwkv_wkv6_f32_kernel<WKV_BLOCK_SIZE>(
  172. B, T, C, H, k_d, v_d, r_d, tf_d, td_d, s_d, dst_d,
  173. item_ct1, (float*)shared_mem_acc.get_multi_ptr<sycl::access::decorated::no>().get()
  174. );
  175. });
  176. });
  177. } else {
  178. stream->submit([&](sycl::handler& cgh) {
  179. sycl::local_accessor<float, 1> shared_mem_acc(shared_mem_size, cgh);
  180. cgh.parallel_for(
  181. sycl::nd_range<3>(grid_dims * block_dims, block_dims),
  182. [=](sycl::nd_item<3> item_ct1) {
  183. rwkv_wkv6_f32_kernel<WKV_BLOCK_SIZE * 2>(
  184. B, T, C, H, k_d, v_d, r_d, tf_d, td_d, s_d, dst_d,
  185. item_ct1, (float*)shared_mem_acc.get_multi_ptr<sycl::access::decorated::no>().get()
  186. );
  187. });
  188. });
  189. }
  190. GGML_UNUSED(src0);
  191. GGML_UNUSED(src1);
  192. }
  193. void ggml_sycl_op_rwkv_wkv7(ggml_backend_sycl_context& ctx, ggml_tensor* dst) {
  194. const ggml_tensor *src0 = dst->src[0];
  195. const ggml_tensor *src1 = dst->src[1];
  196. const float* r_d = (const float*)dst->src[0]->data;
  197. const float* w_d = (const float*)dst->src[1]->data;
  198. const float* k_d = (const float*)dst->src[2]->data;
  199. const float* v_d = (const float*)dst->src[3]->data;
  200. const float* a_d = (const float*)dst->src[4]->data;
  201. const float* b_d = (const float*)dst->src[5]->data;
  202. const float* s_d = (const float*)dst->src[6]->data;
  203. float* dst_d = (float*)dst->data;
  204. const int64_t B = dst->src[6]->ne[1];
  205. const int64_t T = dst->src[0]->ne[2];
  206. const int64_t C = dst->ne[0];
  207. const int64_t H = dst->src[0]->ne[1];
  208. GGML_ASSERT(dst->src[6]->type == GGML_TYPE_F32);
  209. GGML_ASSERT(C % H == 0);
  210. GGML_ASSERT(C / H == WKV_BLOCK_SIZE || C / H == WKV_BLOCK_SIZE * 2);
  211. dpct::queue_ptr stream = ctx.stream();
  212. // Calculate execution configuration
  213. const size_t shared_mem_size = C / H * 5 * sizeof(float); // For r, w, k, a, b
  214. sycl::range<3> block_dims(1, 1, C / H);
  215. sycl::range<3> grid_dims(1, 1, B * H);
  216. // Submit kernel
  217. if (C / H == WKV_BLOCK_SIZE) {
  218. stream->submit([&](sycl::handler& cgh) {
  219. sycl::local_accessor<float, 1> shared_mem_acc(shared_mem_size, cgh);
  220. cgh.parallel_for(
  221. sycl::nd_range<3>(grid_dims * block_dims, block_dims),
  222. [=](sycl::nd_item<3> item_ct1) {
  223. rwkv_wkv7_f32_kernel<WKV_BLOCK_SIZE>(
  224. B, T, C, H, r_d, w_d, k_d, v_d, a_d, b_d, s_d, dst_d,
  225. item_ct1, (float*)shared_mem_acc.get_multi_ptr<sycl::access::decorated::no>().get()
  226. );
  227. });
  228. });
  229. } else {
  230. stream->submit([&](sycl::handler& cgh) {
  231. sycl::local_accessor<float, 1> shared_mem_acc(shared_mem_size, cgh);
  232. cgh.parallel_for(
  233. sycl::nd_range<3>(grid_dims * block_dims, block_dims),
  234. [=](sycl::nd_item<3> item_ct1) {
  235. rwkv_wkv7_f32_kernel<WKV_BLOCK_SIZE * 2>(
  236. B, T, C, H, r_d, w_d, k_d, v_d, a_d, b_d, s_d, dst_d,
  237. item_ct1, (float*)shared_mem_acc.get_multi_ptr<sycl::access::decorated::no>().get()
  238. );
  239. });
  240. });
  241. }
  242. GGML_UNUSED(src0);
  243. GGML_UNUSED(src1);
  244. }