ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-cuda / wkv.cu
| 1 | #include "common.cuh" |
| 2 | #include "wkv.cuh" |
| 3 | |
| 4 | template <int block_size> |
| 5 | 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) { |
| 6 | const int tid = threadIdx.x; |
| 7 | const int bid = blockIdx.x; |
| 8 | |
| 9 | const int head_size = block_size; |
| 10 | const int batch_i = bid / H; |
| 11 | const int head_i = bid % H; |
| 12 | const int state_size = C * head_size; |
| 13 | const int n_seq_tokens = T / B; |
| 14 | |
| 15 | float state[head_size]; |
| 16 | __shared__ float _k[head_size], _r[head_size], _tf[head_size], _td[head_size]; |
| 17 | |
| 18 | #pragma unroll |
| 19 | for (int i = 0; i < head_size; i++) { |
| 20 | state[i] = s[batch_i * state_size + head_i * head_size * head_size + i * head_size + tid]; |
| 21 | } |
| 22 | |
| 23 | __syncthreads(); |
| 24 | _tf[tid] = tf[head_i * head_size + tid]; |
| 25 | __syncthreads(); |
| 26 | |
| 27 | 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) { |
| 28 | __syncthreads(); |
| 29 | _k[tid] = k[t]; |
| 30 | _r[tid] = r[t]; |
| 31 | _td[tid] = td[t]; |
| 32 | __syncthreads(); |
| 33 | |
| 34 | const float _v = v[t]; |
| 35 | float y = 0; |
| 36 | for (int j = 0; j < head_size; j += 4) { |
| 37 | const float4& k = (float4&)(_k[j]); |
| 38 | const float4& r = (float4&)(_r[j]); |
| 39 | const float4& tf = (float4&)(_tf[j]); |
| 40 | const float4& td = (float4&)(_td[j]); |
| 41 | float4& s = (float4&)(state[j]); |
| 42 | float4 kv; |
| 43 | |
| 44 | kv.x = k.x * _v; |
| 45 | kv.y = k.y * _v; |
| 46 | kv.z = k.z * _v; |
| 47 | kv.w = k.w * _v; |
| 48 | |
| 49 | y += r.x * (tf.x * kv.x + s.x); |
| 50 | y += r.y * (tf.y * kv.y + s.y); |
| 51 | y += r.z * (tf.z * kv.z + s.z); |
| 52 | y += r.w * (tf.w * kv.w + s.w); |
| 53 | |
| 54 | s.x = s.x * td.x + kv.x; |
| 55 | s.y = s.y * td.y + kv.y; |
| 56 | s.z = s.z * td.z + kv.z; |
| 57 | s.w = s.w * td.w + kv.w; |
| 58 | } |
| 59 | dst[t] = y; |
| 60 | } |
| 61 | |
| 62 | #pragma unroll |
| 63 | for (int i = 0; i < head_size; i++) { |
| 64 | dst[T * C + batch_i * state_size + head_i * head_size * head_size + i * head_size + tid] = state[i]; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | template <int block_size> |
| 69 | static __global__ void rwkv_wkv7_f32(const int B, const int T, const int C, const int H, const float * r, const float * w, const float * k, const float * v, const float * a, const float * b, const float * s, float * dst) { |
| 70 | const int tid = threadIdx.x; |
| 71 | const int bid = blockIdx.x; |
| 72 | |
| 73 | const int head_size = block_size; |
| 74 | const int batch_i = bid / H; |
| 75 | const int head_i = bid % H; |
| 76 | const int state_size = C * head_size; |
| 77 | const int n_seq_tokens = T / B; |
| 78 | |
| 79 | float state[head_size]; |
| 80 | __shared__ float _r[head_size], _w[head_size], _k[head_size], _a[head_size], _b[head_size]; |
| 81 | |
| 82 | #pragma unroll |
| 83 | for (int i = 0; i < head_size; i++) { |
| 84 | state[i] = s[batch_i * state_size + head_i * head_size * head_size + tid * head_size + i]; |
| 85 | } |
| 86 | |
| 87 | 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) { |
| 88 | __syncthreads(); |
| 89 | _r[tid] = r[t]; |
| 90 | _w[tid] = w[t]; |
| 91 | _k[tid] = k[t]; |
| 92 | _a[tid] = a[t]; |
| 93 | _b[tid] = b[t]; |
| 94 | __syncthreads(); |
| 95 | |
| 96 | float sa = 0; |
| 97 | #pragma unroll |
| 98 | for (int j = 0; j < head_size; j += 4) |
| 99 | { |
| 100 | const float4& a = (float4&)(_a[j]); |
| 101 | const float4& s = (float4&)(state[j]); |
| 102 | sa += a.x * s.x; |
| 103 | sa += a.y * s.y; |
| 104 | sa += a.z * s.z; |
| 105 | sa += a.w * s.w; |
| 106 | } |
| 107 | |
| 108 | const float _v = v[t]; |
| 109 | float y = 0; |
| 110 | for (int j = 0; j < head_size; j += 4) { |
| 111 | const float4& r = (float4&)(_r[j]); |
| 112 | const float4& w = (float4&)(_w[j]); |
| 113 | const float4& k = (float4&)(_k[j]); |
| 114 | const float4& b = (float4&)(_b[j]); |
| 115 | float4& s = (float4&)(state[j]); |
| 116 | float4 kv; |
| 117 | |
| 118 | kv.x = k.x * _v; |
| 119 | kv.y = k.y * _v; |
| 120 | kv.z = k.z * _v; |
| 121 | kv.w = k.w * _v; |
| 122 | |
| 123 | s.x = s.x * w.x + kv.x + sa * b.x; |
| 124 | s.y = s.y * w.y + kv.y + sa * b.y; |
| 125 | s.z = s.z * w.z + kv.z + sa * b.z; |
| 126 | s.w = s.w * w.w + kv.w + sa * b.w; |
| 127 | |
| 128 | y += s.x * r.x; |
| 129 | y += s.y * r.y; |
| 130 | y += s.z * r.z; |
| 131 | y += s.w * r.w; |
| 132 | } |
| 133 | dst[t] = y; |
| 134 | } |
| 135 | |
| 136 | #pragma unroll |
| 137 | for (int i = 0; i < head_size; i++) { |
| 138 | dst[T * C + batch_i * state_size + head_i * head_size * head_size + tid * head_size + i] = state[i]; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | template <int rows_per_block> |
| 143 | static __global__ void __launch_bounds__(WARP_SIZE * rows_per_block, 2) |
| 144 | rwkv_wkv7_f32_t1_warp_row(const int T, const int C, const int H, const float * r, const float * w, const float * k, const float * v, const float * a, const float * b, const float * s, float * dst) { |
| 145 | constexpr int head_size = CUDA_WKV_BLOCK_SIZE; |
| 146 | constexpr int half_head = head_size / 2; |
| 147 | |
| 148 | const int lane = threadIdx.x; |
| 149 | const int row = blockIdx.y * rows_per_block + threadIdx.y; |
| 150 | const int bid = blockIdx.x; |
| 151 | |
| 152 | const int batch_i = bid / H; |
| 153 | const int head_i = bid % H; |
| 154 | const int state_size = C * head_size; |
| 155 | const int head_off = head_i * head_size; |
| 156 | const int t = batch_i * C + head_off + row; |
| 157 | |
| 158 | __shared__ float _r[head_size], _w[head_size], _k[head_size], _a[head_size], _b[head_size]; |
| 159 | |
| 160 | if (threadIdx.y == 0) { |
| 161 | _r[lane] = r[batch_i * C + head_off + lane]; |
| 162 | _w[lane] = w[batch_i * C + head_off + lane]; |
| 163 | _k[lane] = k[batch_i * C + head_off + lane]; |
| 164 | _a[lane] = a[batch_i * C + head_off + lane]; |
| 165 | _b[lane] = b[batch_i * C + head_off + lane]; |
| 166 | |
| 167 | _r[lane + half_head] = r[batch_i * C + head_off + lane + half_head]; |
| 168 | _w[lane + half_head] = w[batch_i * C + head_off + lane + half_head]; |
| 169 | _k[lane + half_head] = k[batch_i * C + head_off + lane + half_head]; |
| 170 | _a[lane + half_head] = a[batch_i * C + head_off + lane + half_head]; |
| 171 | _b[lane + half_head] = b[batch_i * C + head_off + lane + half_head]; |
| 172 | } |
| 173 | __syncthreads(); |
| 174 | |
| 175 | const int64_t state_base = batch_i * state_size + head_i * head_size * head_size + row * head_size; |
| 176 | const float s0 = s[state_base + lane]; |
| 177 | const float s1 = s[state_base + lane + half_head]; |
| 178 | const float sa = warp_reduce_sum(_a[lane] * s0 + _a[lane + half_head] * s1); |
| 179 | |
| 180 | const float vt = v[t]; |
| 181 | const float st0 = s0 * _w[lane] + _k[lane] * vt + sa * _b[lane]; |
| 182 | const float st1 = s1 * _w[lane + half_head] + _k[lane + half_head] * vt + sa * _b[lane + half_head]; |
| 183 | const float y = warp_reduce_sum(st0 * _r[lane] + st1 * _r[lane + half_head]); |
| 184 | |
| 185 | dst[T * C + state_base + lane] = st0; |
| 186 | dst[T * C + state_base + lane + half_head] = st1; |
| 187 | |
| 188 | if (lane == 0) { |
| 189 | dst[t] = y; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void ggml_cuda_op_rwkv_wkv6(ggml_backend_cuda_context & ctx, ggml_tensor * dst) { |
| 194 | const float * k_d = (const float *)dst->src[0]->data; |
| 195 | const float * v_d = (const float *)dst->src[1]->data; |
| 196 | const float * r_d = (const float *)dst->src[2]->data; |
| 197 | const float * tf_d = (const float *)dst->src[3]->data; |
| 198 | const float * td_d = (const float *)dst->src[4]->data; |
| 199 | const float * s_d = (const float *)dst->src[5]->data; |
| 200 | |
| 201 | const int64_t B = dst->src[5]->ne[1]; |
| 202 | const int64_t T = dst->src[0]->ne[2]; |
| 203 | const int64_t C = dst->ne[0]; |
| 204 | const int64_t H = dst->src[0]->ne[1]; |
| 205 | |
| 206 | float * dst_d = (float *)dst->data; |
| 207 | |
| 208 | cudaStream_t stream = ctx.stream(); |
| 209 | |
| 210 | GGML_ASSERT(dst->src[5]->type == GGML_TYPE_F32); |
| 211 | GGML_ASSERT(C % H == 0); |
| 212 | GGML_ASSERT(C / H == CUDA_WKV_BLOCK_SIZE || C / H == CUDA_WKV_BLOCK_SIZE * 2); |
| 213 | |
| 214 | if (C / H == CUDA_WKV_BLOCK_SIZE) { |
| 215 | rwkv_wkv_f32<CUDA_WKV_BLOCK_SIZE><<<B * H, C / H, 0, stream>>>(B, T, C, H, k_d, v_d, r_d, tf_d, td_d, s_d, dst_d); |
| 216 | } else { |
| 217 | rwkv_wkv_f32<CUDA_WKV_BLOCK_SIZE * 2><<<B * H, C / H, 0, stream>>>(B, T, C, H, k_d, v_d, r_d, tf_d, td_d, s_d, dst_d); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void ggml_cuda_op_rwkv_wkv7(ggml_backend_cuda_context & ctx, ggml_tensor * dst) { |
| 222 | const float * r_d = (const float *)dst->src[0]->data; |
| 223 | const float * w_d = (const float *)dst->src[1]->data; |
| 224 | const float * k_d = (const float *)dst->src[2]->data; |
| 225 | const float * v_d = (const float *)dst->src[3]->data; |
| 226 | const float * a_d = (const float *)dst->src[4]->data; |
| 227 | const float * b_d = (const float *)dst->src[5]->data; |
| 228 | const float * s_d = (const float *)dst->src[6]->data; |
| 229 | |
| 230 | const int64_t B = dst->src[6]->ne[1]; |
| 231 | const int64_t T = dst->src[0]->ne[2]; |
| 232 | const int64_t C = dst->ne[0]; |
| 233 | const int64_t H = dst->src[0]->ne[1]; |
| 234 | |
| 235 | float * dst_d = (float *)dst->data; |
| 236 | |
| 237 | cudaStream_t stream = ctx.stream(); |
| 238 | |
| 239 | GGML_ASSERT(dst->src[6]->type == GGML_TYPE_F32); |
| 240 | GGML_ASSERT(C % H == 0); |
| 241 | GGML_ASSERT(C / H == CUDA_WKV_BLOCK_SIZE || C / H == CUDA_WKV_BLOCK_SIZE * 2); |
| 242 | |
| 243 | if (T / B == 1 && C / H == CUDA_WKV_BLOCK_SIZE) { |
| 244 | constexpr int rows_per_block = 4; |
| 245 | rwkv_wkv7_f32_t1_warp_row<rows_per_block><<<dim3(B * H, CUDA_WKV_BLOCK_SIZE / rows_per_block), dim3(WARP_SIZE, rows_per_block), 0, stream>>>(T, C, H, r_d, w_d, k_d, v_d, a_d, b_d, s_d, dst_d); |
| 246 | } else if (C / H == CUDA_WKV_BLOCK_SIZE) { |
| 247 | rwkv_wkv7_f32<CUDA_WKV_BLOCK_SIZE><<<B * H, C / H, 0, stream>>>(B, T, C, H, r_d, w_d, k_d, v_d, a_d, b_d, s_d, dst_d); |
| 248 | } else { |
| 249 | rwkv_wkv7_f32<CUDA_WKV_BLOCK_SIZE * 2><<<B * H, C / H, 0, stream>>>(B, T, C, H, r_d, w_d, k_d, v_d, a_d, b_d, s_d, dst_d); |
| 250 | } |
| 251 | } |