okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b2de2641f
main
1#include "common.cuh" 2#include "dsv4-hc.cuh" 3 4 5static constexpr int DSV4_HC = 4; 6 7 8static __device__ void dsv4_hc_comb_norm_cols(float * comb, float eps) { 9 for (int idst = 0; idst < DSV4_HC; ++idst) { 10 float sum = eps; 11 for (int isrc = 0; isrc < DSV4_HC; ++isrc) { 12 sum += comb[idst + DSV4_HC*isrc]; 13 } 14 15 const float inv_sum = 1.0f / sum; 16 for (int isrc = 0; isrc < DSV4_HC; ++isrc) { 17 comb[idst + DSV4_HC*isrc] *= inv_sum; 18 } 19 } 20} 21 22static __device__ void dsv4_hc_comb_norm_rows(float * comb, float eps) { 23 for (int isrc = 0; isrc < DSV4_HC; ++isrc) { 24 float sum = eps; 25 for (int idst = 0; idst < DSV4_HC; ++idst) { 26 sum += comb[idst + DSV4_HC*isrc]; 27 } 28 29 const float inv_sum = 1.0f / sum; 30 for (int idst = 0; idst < DSV4_HC; ++idst) { 31 comb[idst + DSV4_HC*isrc] *= inv_sum; 32 } 33 } 34} 35 36static __global__ void dsv4_hc_comb_f32( 37 const float * mixes, 38 const float * scale, 39 const float * base, 40 float * dst, 41 int64_t n_tokens, 42 int64_t sm0, 43 int64_t sm1, 44 int64_t ss0, 45 int64_t sb0, 46 int64_t sd0, 47 int64_t sd1, 48 int64_t sd2, 49 float eps, 50 int32_t n_iter) { 51 constexpr int comb_offset = 2*DSV4_HC; 52 53 ggml_cuda_pdl_lc(); 54 const int64_t it = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; 55 56 if (it >= n_tokens) { 57 return; 58 } 59 60 ggml_cuda_pdl_sync(); 61 62 const float scale_comb = scale[2*ss0]; 63 float comb[DSV4_HC*DSV4_HC]; 64 65 for (int isrc = 0; isrc < DSV4_HC; ++isrc) { 66 float max = -INFINITY; 67 for (int idst = 0; idst < DSV4_HC; ++idst) { 68 const int idx = idst + DSV4_HC*isrc; 69 const float v = mixes[(comb_offset + idx)*sm0 + it*sm1] * scale_comb + base[(comb_offset + idx)*sb0]; 70 comb[idx] = v; 71 max = fmaxf(max, v); 72 } 73 74 float sum = 0.0f; 75 for (int idst = 0; idst < DSV4_HC; ++idst) { 76 const int idx = idst + DSV4_HC*isrc; 77 const float v = expf(comb[idx] - max); 78 comb[idx] = v; 79 sum += v; 80 } 81 82 const float inv_sum = 1.0f / sum; 83 for (int idst = 0; idst < DSV4_HC; ++idst) { 84 const int idx = idst + DSV4_HC*isrc; 85 comb[idx] = comb[idx] * inv_sum + eps; 86 } 87 } 88 89 dsv4_hc_comb_norm_cols(comb, eps); 90 for (int32_t i = 1; i < n_iter; ++i) { 91 dsv4_hc_comb_norm_rows(comb, eps); 92 dsv4_hc_comb_norm_cols(comb, eps); 93 } 94 95 for (int isrc = 0; isrc < DSV4_HC; ++isrc) { 96 for (int idst = 0; idst < DSV4_HC; ++idst) { 97 const int idx = idst + DSV4_HC*isrc; 98 dst[idst*sd0 + isrc*sd1 + it*sd2] = comb[idx]; 99 } 100 } 101} 102 103template <bool gated> 104static __global__ void dsv4_hc_pre_f32( 105 const float * x, 106 const float * weights, 107 float * dst, 108 int64_t n_embd, 109 int64_t hc, 110 int64_t n_tokens, 111 int64_t sx0, 112 int64_t sx1, 113 int64_t sx2, 114 int64_t sw0, 115 int64_t sw1, 116 int64_t sw2, 117 int64_t sd0, 118 int64_t sd1, 119 float scale) { 120 ggml_cuda_pdl_lc(); 121 const int64_t ir = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; 122 const int64_t nr = n_embd * n_tokens; 123 124 if (ir >= nr) { 125 return; 126 } 127 128 ggml_cuda_pdl_sync(); 129 130 const int64_t i0 = ir % n_embd; 131 const int64_t it = ir / n_embd; 132 133 float sum = 0.0f; 134 for (int64_t ih = 0; ih < hc; ++ih) { 135 const float xv = x[i0*sx0 + ih*sx1 + it*sx2]; 136 float wv; 137 if constexpr (gated) { 138 wv = 1.0f / (1.0f + expf(-weights[i0*sw0 + ih*sw1 + it*sw2])); 139 } else { 140 wv = weights[ih*sw0 + it*sw1]; 141 } 142 sum += xv * wv; 143 } 144 145 dst[i0*sd0 + it*sd1] = scale * sum; 146} 147 148template <bool has_comb> 149static __global__ void dsv4_hc_post_f32( 150 const float * x, 151 const float * residual, 152 const float * post, 153 const float * comb, 154 float * dst, 155 int64_t n_embd, 156 int64_t hc, 157 int64_t n_tokens, 158 int64_t sx0, 159 int64_t sx1, 160 int64_t sr0, 161 int64_t sr1, 162 int64_t sr2, 163 int64_t sp0, 164 int64_t sp1, 165 int64_t sc0, 166 int64_t sc1, 167 int64_t sc2, 168 int64_t sd0, 169 int64_t sd1, 170 int64_t sd2) { 171 ggml_cuda_pdl_lc(); 172 const int64_t ir = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; 173 const int64_t nr = n_embd * hc * n_tokens; 174 175 if (ir >= nr) { 176 return; 177 } 178 179 ggml_cuda_pdl_sync(); 180 181 const int64_t i0 = ir % n_embd; 182 const int64_t idst = (ir / n_embd) % hc; 183 const int64_t it = ir / (n_embd * hc); 184 185 float sum = x[i0*sx0 + it*sx1] * post[idst*sp0 + it*sp1]; 186 if constexpr (has_comb) { 187 for (int64_t isrc = 0; isrc < hc; ++isrc) { 188 sum += residual[i0*sr0 + isrc*sr1 + it*sr2] * comb[idst*sc0 + isrc*sc1 + it*sc2]; 189 } 190 } else { 191 sum += residual[i0*sr0 + idst*sr1 + it*sr2]; 192 } 193 194 dst[i0*sd0 + idst*sd1 + it*sd2] = sum; 195} 196 197void ggml_cuda_op_dsv4_hc_comb(ggml_backend_cuda_context & ctx, ggml_tensor * dst) { 198 const ggml_tensor * mixes = dst->src[0]; 199 const ggml_tensor * scale = dst->src[1]; 200 const ggml_tensor * base = dst->src[2]; 201 202 GGML_ASSERT(mixes->type == GGML_TYPE_F32); 203 GGML_ASSERT(scale->type == GGML_TYPE_F32); 204 GGML_ASSERT(base->type == GGML_TYPE_F32); 205 GGML_ASSERT(dst->type == GGML_TYPE_F32); 206 207 constexpr int64_t hc_mix_dim = (2 + DSV4_HC)*DSV4_HC; 208 209 GGML_ASSERT(mixes->ne[0] == hc_mix_dim); 210 GGML_ASSERT(dst->ne[0] == DSV4_HC); 211 GGML_ASSERT(dst->ne[1] == DSV4_HC); 212 GGML_ASSERT(dst->ne[2] == mixes->ne[1]); 213 GGML_ASSERT(scale->ne[0] >= 3); 214 GGML_ASSERT(base->ne[0] == hc_mix_dim); 215 216 GGML_TENSOR_LOCALS(size_t, nbm, mixes, nb); 217 GGML_TENSOR_LOCALS(size_t, nbs, scale, nb); 218 GGML_TENSOR_LOCALS(size_t, nbb, base, nb); 219 GGML_TENSOR_LOCALS(size_t, nbd, dst, nb); 220 221 const int64_t n_tokens = mixes->ne[1]; 222 const float eps = ggml_get_op_params_f32(dst, 0); 223 const int32_t n_iter = ggml_get_op_params_i32(dst, 1); 224 225 const int block_size = 256; 226 const dim3 block_dims(block_size, 1, 1); 227 const dim3 grid_dims((n_tokens + block_size - 1) / block_size, 1, 1); 228 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(grid_dims, block_dims, 0, ctx.stream()); 229 230 ggml_cuda_kernel_launch(dsv4_hc_comb_f32, launch_params, 231 (const float *) mixes->data, (const float *) scale->data, (const float *) base->data, (float *) dst->data, 232 n_tokens, 233 nbm0 / sizeof(float), nbm1 / sizeof(float), 234 nbs0 / sizeof(float), 235 nbb0 / sizeof(float), 236 nbd0 / sizeof(float), nbd1 / sizeof(float), nbd2 / sizeof(float), 237 eps, n_iter); 238} 239 240void ggml_cuda_op_dsv4_hc_pre(ggml_backend_cuda_context & ctx, ggml_tensor * dst) { 241 const ggml_tensor * x = dst->src[0]; 242 const ggml_tensor * weights = dst->src[1]; 243 244 GGML_ASSERT(x->type == GGML_TYPE_F32); 245 GGML_ASSERT(weights->type == GGML_TYPE_F32); 246 GGML_ASSERT(dst->type == GGML_TYPE_F32); 247 248 GGML_TENSOR_LOCALS(size_t, nbx, x, nb); 249 GGML_TENSOR_LOCALS(size_t, nbw, weights, nb); 250 GGML_TENSOR_LOCALS(size_t, nbd, dst, nb); 251 252 const int64_t n_embd = x->ne[0]; 253 const int64_t hc = x->ne[1]; 254 const int64_t n_tokens = x->ne[2]; 255 256 const float scale = ggml_get_op_params_f32(dst, 0); 257 const bool gated = ggml_get_op_params_i32(dst, 1) != 0; 258 259 const int block_size = 256; 260 const int64_t nr = n_embd * n_tokens; 261 const dim3 block_dims(block_size, 1, 1); 262 const dim3 grid_dims((nr + block_size - 1) / block_size, 1, 1); 263 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(grid_dims, block_dims, 0, ctx.stream()); 264 265 auto kernel = gated ? dsv4_hc_pre_f32<true> : dsv4_hc_pre_f32<false>; 266 ggml_cuda_kernel_launch(kernel, launch_params, 267 (const float *) x->data, (const float *) weights->data, (float *) dst->data, 268 n_embd, hc, n_tokens, 269 nbx0 / sizeof(float), nbx1 / sizeof(float), nbx2 / sizeof(float), 270 nbw0 / sizeof(float), nbw1 / sizeof(float), nbw2 / sizeof(float), 271 nbd0 / sizeof(float), nbd1 / sizeof(float), 272 scale); 273} 274 275void ggml_cuda_op_dsv4_hc_post(ggml_backend_cuda_context & ctx, ggml_tensor * dst) { 276 const ggml_tensor * x = dst->src[0]; 277 const ggml_tensor * residual = dst->src[1]; 278 const ggml_tensor * post = dst->src[2]; 279 const ggml_tensor * comb = dst->src[3]; 280 281 GGML_ASSERT(x->type == GGML_TYPE_F32); 282 GGML_ASSERT(residual->type == GGML_TYPE_F32); 283 GGML_ASSERT(post->type == GGML_TYPE_F32); 284 GGML_ASSERT(comb == nullptr || comb->type == GGML_TYPE_F32); 285 GGML_ASSERT(dst->type == GGML_TYPE_F32); 286 287 GGML_TENSOR_LOCALS(size_t, nbx, x, nb); 288 GGML_TENSOR_LOCALS(size_t, nbr, residual, nb); 289 GGML_TENSOR_LOCALS(size_t, nbp, post, nb); 290 GGML_TENSOR_LOCALS(size_t, nbd, dst, nb); 291 292 const size_t nbc0 = comb ? comb->nb[0] : 0; 293 const size_t nbc1 = comb ? comb->nb[1] : 0; 294 const size_t nbc2 = comb ? comb->nb[2] : 0; 295 296 const int64_t n_embd = x->ne[0]; 297 const int64_t n_tokens = x->ne[1]; 298 const int64_t hc = residual->ne[1]; 299 300 const int block_size = 256; 301 const int64_t nr = n_embd * hc * n_tokens; 302 const dim3 block_dims(block_size, 1, 1); 303 const dim3 grid_dims((nr + block_size - 1) / block_size, 1, 1); 304 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(grid_dims, block_dims, 0, ctx.stream()); 305 306 auto kernel = comb ? dsv4_hc_post_f32<true> : dsv4_hc_post_f32<false>; 307 ggml_cuda_kernel_launch(kernel, launch_params, 308 (const float *) x->data, (const float *) residual->data, 309 (const float *) post->data, comb ? (const float *) comb->data : nullptr, (float *) dst->data, 310 n_embd, hc, n_tokens, 311 nbx0 / sizeof(float), nbx1 / sizeof(float), 312 nbr0 / sizeof(float), nbr1 / sizeof(float), nbr2 / sizeof(float), 313 nbp0 / sizeof(float), nbp1 / sizeof(float), 314 nbc0 / sizeof(float), nbc1 / sizeof(float), nbc2 / sizeof(float), 315 nbd0 / sizeof(float), nbd1 / sizeof(float), nbd2 / sizeof(float)); 316}