Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-cuda/dsv4-hc.cu # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-cuda / dsv4-hc.cu` 333 lines ยท 11322 bytes Raw Blame History 1 #include "common.cuh" 2 #include "dsv4-hc.cuh" 3 4 5 static constexpr int DSV4_HC = 4; 6 7 8 static __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 22 static __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 36 static __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 103 template 104 static __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 148 // gated: post = gate_scale_out*sigmoid(gate_scale_in*post), the fused SCALE -> SIGMOID -> SCALE chain of qwen4exp 149 template 150 static __global__ void dsv4_hc_post_f32( 151 const float * x, 152 const float * residual, 153 const float * post, 154 const float * comb, 155 float * dst, 156 int64_t n_embd, 157 int64_t hc, 158 int64_t n_tokens, 159 int64_t sx0, 160 int64_t sx1, 161 int64_t sr0, 162 int64_t sr1, 163 int64_t sr2, 164 int64_t sp0, 165 int64_t sp1, 166 int64_t sc0, 167 int64_t sc1, 168 int64_t sc2, 169 int64_t sd0, 170 int64_t sd1, 171 int64_t sd2, 172 float gate_scale_in, 173 float gate_scale_out) { 174 ggml_cuda_pdl_lc(); 175 const int64_t ir = (int64_t) blockIdx.x * blockDim.x + threadIdx.x; 176 const int64_t nr = n_embd * hc * n_tokens; 177 178 if (ir >= nr) { 179 return; 180 } 181 182 ggml_cuda_pdl_sync(); 183 184 const int64_t i0 = ir % n_embd; 185 const int64_t idst = (ir / n_embd) % hc; 186 const int64_t it = ir / (n_embd * hc); 187 188 float p = post[idst*sp0 + it*sp1]; 189 if constexpr (gated) { 190 p = gate_scale_out * (1.0f / (1.0f + expf(-(gate_scale_in * p)))); 191 } 192 193 float sum = x[i0*sx0 + it*sx1] * p; 194 if constexpr (has_comb) { 195 for (int64_t isrc = 0; isrc < hc; ++isrc) { 196 sum += residual[i0*sr0 + isrc*sr1 + it*sr2] * comb[idst*sc0 + isrc*sc1 + it*sc2]; 197 } 198 } else { 199 sum += residual[i0*sr0 + idst*sr1 + it*sr2]; 200 } 201 202 dst[i0*sd0 + idst*sd1 + it*sd2] = sum; 203 } 204 205 void ggml_cuda_op_dsv4_hc_comb(ggml_backend_cuda_context & ctx, ggml_tensor * dst) { 206 const ggml_tensor * mixes = dst->src[0]; 207 const ggml_tensor * scale = dst->src[1]; 208 const ggml_tensor * base = dst->src[2]; 209 210 GGML_ASSERT(mixes->type == GGML_TYPE_F32); 211 GGML_ASSERT(scale->type == GGML_TYPE_F32); 212 GGML_ASSERT(base->type == GGML_TYPE_F32); 213 GGML_ASSERT(dst->type == GGML_TYPE_F32); 214 215 constexpr int64_t hc_mix_dim = (2 + DSV4_HC)*DSV4_HC; 216 217 GGML_ASSERT(mixes->ne[0] == hc_mix_dim); 218 GGML_ASSERT(dst->ne[0] == DSV4_HC); 219 GGML_ASSERT(dst->ne[1] == DSV4_HC); 220 GGML_ASSERT(dst->ne[2] == mixes->ne[1]); 221 GGML_ASSERT(scale->ne[0] >= 3); 222 GGML_ASSERT(base->ne[0] == hc_mix_dim); 223 224 GGML_TENSOR_LOCALS(size_t, nbm, mixes, nb); 225 GGML_TENSOR_LOCALS(size_t, nbs, scale, nb); 226 GGML_TENSOR_LOCALS(size_t, nbb, base, nb); 227 GGML_TENSOR_LOCALS(size_t, nbd, dst, nb); 228 229 const int64_t n_tokens = mixes->ne[1]; 230 const float eps = ggml_get_op_params_f32(dst, 0); 231 const int32_t n_iter = ggml_get_op_params_i32(dst, 1); 232 233 const int block_size = 256; 234 const dim3 block_dims(block_size, 1, 1); 235 const dim3 grid_dims((n_tokens + block_size - 1) / block_size, 1, 1); 236 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(grid_dims, block_dims, 0, ctx.stream()); 237 238 ggml_cuda_kernel_launch(dsv4_hc_comb_f32, launch_params, 239 (const float *) mixes->data, (const float *) scale->data, (const float *) base->data, (float *) dst->data, 240 n_tokens, 241 nbm0 / sizeof(float), nbm1 / sizeof(float), 242 nbs0 / sizeof(float), 243 nbb0 / sizeof(float), 244 nbd0 / sizeof(float), nbd1 / sizeof(float), nbd2 / sizeof(float), 245 eps, n_iter); 246 } 247 248 void ggml_cuda_op_dsv4_hc_pre(ggml_backend_cuda_context & ctx, ggml_tensor * dst) { 249 const ggml_tensor * x = dst->src[0]; 250 const ggml_tensor * weights = dst->src[1]; 251 252 GGML_ASSERT(x->type == GGML_TYPE_F32); 253 GGML_ASSERT(weights->type == GGML_TYPE_F32); 254 GGML_ASSERT(dst->type == GGML_TYPE_F32); 255 256 GGML_TENSOR_LOCALS(size_t, nbx, x, nb); 257 GGML_TENSOR_LOCALS(size_t, nbw, weights, nb); 258 GGML_TENSOR_LOCALS(size_t, nbd, dst, nb); 259 260 const int64_t n_embd = x->ne[0]; 261 const int64_t hc = x->ne[1]; 262 const int64_t n_tokens = x->ne[2]; 263 264 const float scale = ggml_get_op_params_f32(dst, 0); 265 const bool gated = ggml_get_op_params_i32(dst, 1) != 0; 266 267 const int block_size = 256; 268 const int64_t nr = n_embd * n_tokens; 269 const dim3 block_dims(block_size, 1, 1); 270 const dim3 grid_dims((nr + block_size - 1) / block_size, 1, 1); 271 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(grid_dims, block_dims, 0, ctx.stream()); 272 273 auto kernel = gated ? dsv4_hc_pre_f32 : dsv4_hc_pre_f32; 274 ggml_cuda_kernel_launch(kernel, launch_params, 275 (const float *) x->data, (const float *) weights->data, (float *) dst->data, 276 n_embd, hc, n_tokens, 277 nbx0 / sizeof(float), nbx1 / sizeof(float), nbx2 / sizeof(float), 278 nbw0 / sizeof(float), nbw1 / sizeof(float), nbw2 / sizeof(float), 279 nbd0 / sizeof(float), nbd1 / sizeof(float), 280 scale); 281 } 282 283 void ggml_cuda_op_dsv4_hc_post(ggml_backend_cuda_context & ctx, ggml_tensor * dst, 284 const ggml_tensor * gate_scale_in, const ggml_tensor * gate_scale_out) { 285 const ggml_tensor * x = dst->src[0]; 286 const ggml_tensor * residual = dst->src[1]; 287 const ggml_tensor * comb = dst->src[3]; 288 289 // when fused, read the raw post weights from before the SCALE -> SIGMOID -> SCALE chain 290 const bool gated = gate_scale_in != nullptr; 291 const ggml_tensor * post = gated ? gate_scale_in->src[0] : dst->src[2]; 292 293 const float gate_in = gated ? ggml_get_op_params_f32(gate_scale_in, 0) : 1.0f; 294 const float gate_out = gated ? ggml_get_op_params_f32(gate_scale_out, 0) : 1.0f; 295 296 GGML_ASSERT(x->type == GGML_TYPE_F32); 297 GGML_ASSERT(residual->type == GGML_TYPE_F32); 298 GGML_ASSERT(post->type == GGML_TYPE_F32); 299 GGML_ASSERT(comb == nullptr || comb->type == GGML_TYPE_F32); 300 GGML_ASSERT(dst->type == GGML_TYPE_F32); 301 302 GGML_TENSOR_LOCALS(size_t, nbx, x, nb); 303 GGML_TENSOR_LOCALS(size_t, nbr, residual, nb); 304 GGML_TENSOR_LOCALS(size_t, nbp, post, nb); 305 GGML_TENSOR_LOCALS(size_t, nbd, dst, nb); 306 307 const size_t nbc0 = comb ? comb->nb[0] : 0; 308 const size_t nbc1 = comb ? comb->nb[1] : 0; 309 const size_t nbc2 = comb ? comb->nb[2] : 0; 310 311 const int64_t n_embd = x->ne[0]; 312 const int64_t n_tokens = x->ne[1]; 313 const int64_t hc = residual->ne[1]; 314 315 const int block_size = 256; 316 const int64_t nr = n_embd * hc * n_tokens; 317 const dim3 block_dims(block_size, 1, 1); 318 const dim3 grid_dims((nr + block_size - 1) / block_size, 1, 1); 319 const ggml_cuda_kernel_launch_params launch_params = ggml_cuda_kernel_launch_params(grid_dims, block_dims, 0, ctx.stream()); 320 321 auto kernel = comb ? (gated ? dsv4_hc_post_f32 : dsv4_hc_post_f32) 322 : (gated ? dsv4_hc_post_f32 : dsv4_hc_post_f32); 323 ggml_cuda_kernel_launch(kernel, launch_params, 324 (const float *) x->data, (const float *) residual->data, 325 (const float *) post->data, comb ? (const float *) comb->data : nullptr, (float *) dst->data, 326 n_embd, hc, n_tokens, 327 nbx0 / sizeof(float), nbx1 / sizeof(float), 328 nbr0 / sizeof(float), nbr1 / sizeof(float), nbr2 / sizeof(float), 329 nbp0 / sizeof(float), nbp1 / sizeof(float), 330 nbc0 / sizeof(float), nbc1 / sizeof(float), nbc2 / sizeof(float), 331 nbd0 / sizeof(float), nbd1 / sizeof(float), nbd2 / sizeof(float), 332 gate_in, gate_out); 333 }