wkv6.cu 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * llama.cpp - commit 46e3556e01b824e52395fb050b29804b6cff2a7c - do not edit this file
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023-2024 The ggml authors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. #include "common.cuh"
  27. #include "wkv6.cuh"
  28. static __global__ void rwkv_wkv_f32(const int B, const int T, const int C, const int H, const float * k, const float * v, const float * r, const float * tf, const float * td, const float * s, float * dst) {
  29. const int tid = threadIdx.x;
  30. const int bid = blockIdx.x;
  31. const int head_size = CUDA_WKV_BLOCK_SIZE;
  32. const int batch_i = bid / H;
  33. const int head_i = bid % H;
  34. const int state_size = C * head_size;
  35. const int n_seq_tokens = T / B;
  36. float state[head_size];
  37. __shared__ float _k[head_size], _r[head_size], _tf[head_size], _td[head_size];
  38. #pragma unroll
  39. for (int i = 0; i < head_size; i++) {
  40. state[i] = s[batch_i * state_size + head_i * head_size * head_size + i * head_size + tid];
  41. }
  42. __syncthreads();
  43. _tf[tid] = tf[head_i * head_size + tid];
  44. __syncthreads();
  45. for (int t = batch_i * n_seq_tokens * C + head_i * head_size + tid; t < (batch_i + 1) * n_seq_tokens * C + head_i * head_size + tid; t += C) {
  46. __syncthreads();
  47. _k[tid] = k[t];
  48. _r[tid] = r[t];
  49. _td[tid] = td[t];
  50. __syncthreads();
  51. const float _v = v[t];
  52. float y = 0;
  53. for (int j = 0; j < head_size; j += 4) {
  54. const float4& k = (float4&)(_k[j]);
  55. const float4& r = (float4&)(_r[j]);
  56. const float4& tf = (float4&)(_tf[j]);
  57. const float4& td = (float4&)(_td[j]);
  58. float4& s = (float4&)(state[j]);
  59. float4 kv;
  60. kv.x = k.x * _v;
  61. kv.y = k.y * _v;
  62. kv.z = k.z * _v;
  63. kv.w = k.w * _v;
  64. y += r.x * (tf.x * kv.x + s.x);
  65. y += r.y * (tf.y * kv.y + s.y);
  66. y += r.z * (tf.z * kv.z + s.z);
  67. y += r.w * (tf.w * kv.w + s.w);
  68. s.x = s.x * td.x + kv.x;
  69. s.y = s.y * td.y + kv.y;
  70. s.z = s.z * td.z + kv.z;
  71. s.w = s.w * td.w + kv.w;
  72. }
  73. dst[t] = y;
  74. }
  75. #pragma unroll
  76. for (int i = 0; i < head_size; i++) {
  77. dst[T * C + batch_i * state_size + head_i * head_size * head_size + i * head_size + tid] = state[i];
  78. }
  79. }
  80. void ggml_cuda_op_rwkv_wkv6(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
  81. const float * k_d = (const float *)dst->src[0]->data;
  82. const float * v_d = (const float *)dst->src[1]->data;
  83. const float * r_d = (const float *)dst->src[2]->data;
  84. const float * tf_d = (const float *)dst->src[3]->data;
  85. const float * td_d = (const float *)dst->src[4]->data;
  86. const float * s_d = (const float *)dst->src[5]->data;
  87. const int64_t B = dst->src[5]->ne[1];
  88. const int64_t T = dst->src[0]->ne[3];
  89. const int64_t C = dst->ne[0];
  90. const int64_t H = dst->src[0]->ne[2];
  91. float * dst_d = (float *)dst->data;
  92. cudaStream_t stream = ctx.stream();
  93. GGML_ASSERT(dst->src[5]->type == GGML_TYPE_F32);
  94. GGML_ASSERT(C % H == 0);
  95. GGML_ASSERT(C / H == CUDA_WKV_BLOCK_SIZE); // The current cuda kernel is designed for RWKV6, HEAD_SIZE == 64
  96. rwkv_wkv_f32<<<B * H, C / H, 0, stream>>>(B, T, C, H, k_d, v_d, r_d, tf_d, td_d, s_d, dst_d);
  97. }