ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-sycl / dsv4-hc.cpp
| 1 | #include "ggml-impl.h" |
| 2 | #include "dsv4-hc.hpp" |
| 3 | |
| 4 | #include <cmath> |
| 5 | #include <type_traits> |
| 6 | |
| 7 | static constexpr int DSV4_HC = 4; |
| 8 | |
| 9 | // tunable: one work-item per (embedding element, token) |
| 10 | static constexpr int dsv4_hc_pre_block_size = 256; |
| 11 | |
| 12 | // gated: the weight is a per-element gate [n_embd, hc, n_tokens] passed through a sigmoid. |
| 13 | // otherwise it is one weight per (stream, token). |
| 14 | template <bool gated> |
| 15 | static void dsv4_hc_pre_f32_sycl( |
| 16 | const float * x, const float * weights, float * dst, |
| 17 | int64_t n_embd, int64_t hc, int64_t n_tokens, |
| 18 | int64_t sx0, int64_t sx1, int64_t sx2, |
| 19 | int64_t sw0, int64_t sw1, int64_t sw2, |
| 20 | int64_t sd0, int64_t sd1, |
| 21 | float scale, |
| 22 | queue_ptr stream) { |
| 23 | const int64_t nr = n_embd * n_tokens; |
| 24 | const int64_t num_blocks = (nr + dsv4_hc_pre_block_size - 1) / dsv4_hc_pre_block_size; |
| 25 | |
| 26 | stream->parallel_for( |
| 27 | sycl::nd_range<1>(sycl::range<1>(num_blocks * dsv4_hc_pre_block_size), |
| 28 | sycl::range<1>(dsv4_hc_pre_block_size)), |
| 29 | [=](sycl::nd_item<1> item) { |
| 30 | const int64_t ir = item.get_global_id(0); |
| 31 | if (ir >= nr) { |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | const int64_t i0 = ir % n_embd; |
| 36 | const int64_t it = ir / n_embd; |
| 37 | |
| 38 | float sum = 0.0f; |
| 39 | for (int64_t ih = 0; ih < hc; ++ih) { |
| 40 | const float xv = x[i0*sx0 + ih*sx1 + it*sx2]; |
| 41 | float wv; |
| 42 | if constexpr (gated) { |
| 43 | const float gv = weights[i0*sw0 + ih*sw1 + it*sw2]; |
| 44 | wv = 1.0f / (1.0f + sycl::exp(-gv)); |
| 45 | } else { |
| 46 | wv = weights[ih*sw0 + it*sw1]; |
| 47 | } |
| 48 | sum += xv * wv; |
| 49 | } |
| 50 | |
| 51 | dst[i0*sd0 + it*sd1] = scale * sum; |
| 52 | }); |
| 53 | } |
| 54 | |
| 55 | static void dsv4_hc_comb_norm_cols(float * comb, float eps) { |
| 56 | for (int idst = 0; idst < DSV4_HC; ++idst) { |
| 57 | float sum = eps; |
| 58 | for (int isrc = 0; isrc < DSV4_HC; ++isrc) { |
| 59 | sum += comb[idst + DSV4_HC*isrc]; |
| 60 | } |
| 61 | |
| 62 | const float inv_sum = 1.0f / sum; |
| 63 | for (int isrc = 0; isrc < DSV4_HC; ++isrc) { |
| 64 | comb[idst + DSV4_HC*isrc] *= inv_sum; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | static void dsv4_hc_comb_norm_rows(float * comb, float eps) { |
| 70 | for (int isrc = 0; isrc < DSV4_HC; ++isrc) { |
| 71 | float sum = eps; |
| 72 | for (int idst = 0; idst < DSV4_HC; ++idst) { |
| 73 | sum += comb[idst + DSV4_HC*isrc]; |
| 74 | } |
| 75 | |
| 76 | const float inv_sum = 1.0f / sum; |
| 77 | for (int idst = 0; idst < DSV4_HC; ++idst) { |
| 78 | comb[idst + DSV4_HC*isrc] *= inv_sum; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static void dsv4_hc_comb_f32_sycl( |
| 84 | const float * mixes, |
| 85 | const float * scale, |
| 86 | const float * base, |
| 87 | float * dst, |
| 88 | int64_t n_tokens, |
| 89 | int64_t sm0, |
| 90 | int64_t sm1, |
| 91 | int64_t ss0, |
| 92 | int64_t sb0, |
| 93 | int64_t sd0, |
| 94 | int64_t sd1, |
| 95 | int64_t sd2, |
| 96 | float eps, |
| 97 | int32_t n_iter, |
| 98 | queue_ptr stream) { |
| 99 | constexpr int comb_offset = 2*DSV4_HC; |
| 100 | |
| 101 | const int64_t block_size = 256; |
| 102 | const int64_t num_blocks = (n_tokens + block_size - 1) / block_size; |
| 103 | |
| 104 | stream->parallel_for( |
| 105 | sycl::nd_range<1>(sycl::range<1>(num_blocks * block_size), sycl::range<1>(block_size)), |
| 106 | [=](sycl::nd_item<1> item_ct1) { |
| 107 | const int64_t it = item_ct1.get_global_id(0); |
| 108 | |
| 109 | if (it >= n_tokens) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | const float scale_comb = scale[2*ss0]; |
| 114 | float comb[DSV4_HC*DSV4_HC]; |
| 115 | |
| 116 | for (int isrc = 0; isrc < DSV4_HC; ++isrc) { |
| 117 | float max = -INFINITY; |
| 118 | for (int idst = 0; idst < DSV4_HC; ++idst) { |
| 119 | const int idx = idst + DSV4_HC*isrc; |
| 120 | const float v = mixes[(comb_offset + idx)*sm0 + it*sm1] * scale_comb + base[(comb_offset + idx)*sb0]; |
| 121 | comb[idx] = v; |
| 122 | max = fmaxf(max, v); |
| 123 | } |
| 124 | |
| 125 | float sum = 0.0f; |
| 126 | for (int idst = 0; idst < DSV4_HC; ++idst) { |
| 127 | const int idx = idst + DSV4_HC*isrc; |
| 128 | const float v = expf(comb[idx] - max); |
| 129 | comb[idx] = v; |
| 130 | sum += v; |
| 131 | } |
| 132 | |
| 133 | const float inv_sum = 1.0f / sum; |
| 134 | for (int idst = 0; idst < DSV4_HC; ++idst) { |
| 135 | const int idx = idst + DSV4_HC*isrc; |
| 136 | comb[idx] = comb[idx] * inv_sum + eps; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | dsv4_hc_comb_norm_cols(comb, eps); |
| 141 | for (int32_t i = 1; i < n_iter; ++i) { |
| 142 | dsv4_hc_comb_norm_rows(comb, eps); |
| 143 | dsv4_hc_comb_norm_cols(comb, eps); |
| 144 | } |
| 145 | |
| 146 | for (int isrc = 0; isrc < DSV4_HC; ++isrc) { |
| 147 | for (int idst = 0; idst < DSV4_HC; ++idst) { |
| 148 | const int idx = idst + DSV4_HC*isrc; |
| 149 | dst[idst*sd0 + isrc*sd1 + it*sd2] = comb[idx]; |
| 150 | } |
| 151 | } |
| 152 | }); |
| 153 | } |
| 154 | |
| 155 | // tunable: one work-item per (embedding element, stream, token) |
| 156 | static constexpr int dsv4_hc_post_block_size = 256; |
| 157 | |
| 158 | // comb == nullptr is identity mixing: each destination stream keeps its own residual |
| 159 | // instead of summing across the streams. |
| 160 | template <bool has_comb> |
| 161 | static void dsv4_hc_post_f32_sycl( |
| 162 | const float * x, const float * residual, const float * post, const float * comb, float * dst, |
| 163 | int64_t n_embd, int64_t hc, int64_t n_tokens, |
| 164 | int64_t sx0, int64_t sx1, |
| 165 | int64_t sr0, int64_t sr1, int64_t sr2, |
| 166 | int64_t sp0, int64_t sp1, |
| 167 | int64_t sc0, int64_t sc1, int64_t sc2, |
| 168 | int64_t sd0, int64_t sd1, int64_t sd2, |
| 169 | queue_ptr stream) { |
| 170 | const int64_t nr = n_embd * hc * n_tokens; |
| 171 | const int64_t block_size = dsv4_hc_post_block_size; |
| 172 | const int64_t num_blocks = (nr + block_size - 1) / block_size; |
| 173 | |
| 174 | stream->parallel_for( |
| 175 | sycl::nd_range<1>(sycl::range<1>(num_blocks * block_size), sycl::range<1>(block_size)), |
| 176 | [=](sycl::nd_item<1> item) { |
| 177 | const int64_t ir = item.get_global_id(0); |
| 178 | if (ir >= nr) { |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | const int64_t i0 = ir % n_embd; |
| 183 | const int64_t idst = (ir / n_embd) % hc; |
| 184 | const int64_t it = ir / (n_embd * hc); |
| 185 | |
| 186 | float sum = x[i0*sx0 + it*sx1] * post[idst*sp0 + it*sp1]; |
| 187 | if constexpr (has_comb) { |
| 188 | for (int64_t isrc = 0; isrc < hc; ++isrc) { |
| 189 | sum += residual[i0*sr0 + isrc*sr1 + it*sr2] * comb[idst*sc0 + isrc*sc1 + it*sc2]; |
| 190 | } |
| 191 | } else { |
| 192 | sum += residual[i0*sr0 + idst*sr1 + it*sr2]; |
| 193 | } |
| 194 | |
| 195 | dst[i0*sd0 + idst*sd1 + it*sd2] = sum; |
| 196 | }); |
| 197 | } |
| 198 | |
| 199 | void ggml_sycl_op_dsv4_hc_pre(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 200 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/2); |
| 201 | const ggml_tensor * x = dst->src[0]; |
| 202 | const ggml_tensor * weights = dst->src[1]; |
| 203 | |
| 204 | GGML_ASSERT(x->type == GGML_TYPE_F32); |
| 205 | GGML_ASSERT(weights->type == GGML_TYPE_F32); |
| 206 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 207 | |
| 208 | GGML_TENSOR_LOCALS(size_t, nbx, x, nb); |
| 209 | GGML_TENSOR_LOCALS(size_t, nbw, weights, nb); |
| 210 | GGML_TENSOR_LOCALS(size_t, nbd, dst, nb); |
| 211 | |
| 212 | const int64_t n_embd = x->ne[0]; |
| 213 | const int64_t hc = x->ne[1]; |
| 214 | const int64_t n_tokens = x->ne[2]; |
| 215 | |
| 216 | const float scale = ggml_get_op_params_f32(dst, 0); |
| 217 | const bool gated = ggml_get_op_params_i32(dst, 1) != 0; |
| 218 | |
| 219 | queue_ptr stream = ctx.stream(); |
| 220 | |
| 221 | if (gated) { |
| 222 | GGML_ASSERT(weights->ne[0] == n_embd); |
| 223 | GGML_ASSERT(weights->ne[1] == hc); |
| 224 | GGML_ASSERT(weights->ne[2] == n_tokens); |
| 225 | dsv4_hc_pre_f32_sycl<true>( |
| 226 | (const float *) x->data, (const float *) weights->data, (float *) dst->data, |
| 227 | n_embd, hc, n_tokens, |
| 228 | nbx0 / sizeof(float), nbx1 / sizeof(float), nbx2 / sizeof(float), |
| 229 | nbw0 / sizeof(float), nbw1 / sizeof(float), nbw2 / sizeof(float), |
| 230 | nbd0 / sizeof(float), nbd1 / sizeof(float), |
| 231 | scale, stream); |
| 232 | } else { |
| 233 | GGML_ASSERT(weights->ne[0] == hc); |
| 234 | GGML_ASSERT(weights->ne[1] == n_tokens); |
| 235 | dsv4_hc_pre_f32_sycl<false>( |
| 236 | (const float *) x->data, (const float *) weights->data, (float *) dst->data, |
| 237 | n_embd, hc, n_tokens, |
| 238 | nbx0 / sizeof(float), nbx1 / sizeof(float), nbx2 / sizeof(float), |
| 239 | nbw0 / sizeof(float), nbw1 / sizeof(float), /*sw2=*/ 0, |
| 240 | nbd0 / sizeof(float), nbd1 / sizeof(float), |
| 241 | scale, stream); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | void ggml_sycl_op_dsv4_hc_comb(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 246 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/3); |
| 247 | |
| 248 | const ggml_tensor * mixes = dst->src[0]; |
| 249 | const ggml_tensor * scale = dst->src[1]; |
| 250 | const ggml_tensor * base = dst->src[2]; |
| 251 | |
| 252 | GGML_ASSERT(mixes->type == GGML_TYPE_F32); |
| 253 | GGML_ASSERT(scale->type == GGML_TYPE_F32); |
| 254 | GGML_ASSERT(base->type == GGML_TYPE_F32); |
| 255 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 256 | |
| 257 | constexpr int64_t hc_mix_dim = (2 + DSV4_HC)*DSV4_HC; |
| 258 | |
| 259 | GGML_ASSERT(mixes->ne[0] == hc_mix_dim); |
| 260 | GGML_ASSERT(dst->ne[0] == DSV4_HC); |
| 261 | GGML_ASSERT(dst->ne[1] == DSV4_HC); |
| 262 | GGML_ASSERT(dst->ne[2] == mixes->ne[1]); |
| 263 | GGML_ASSERT(scale->ne[0] >= 3); |
| 264 | GGML_ASSERT(base->ne[0] == hc_mix_dim); |
| 265 | |
| 266 | GGML_TENSOR_LOCALS(size_t, nbm, mixes, nb); |
| 267 | GGML_TENSOR_LOCALS(size_t, nbs, scale, nb); |
| 268 | GGML_TENSOR_LOCALS(size_t, nbb, base, nb); |
| 269 | GGML_TENSOR_LOCALS(size_t, nbd, dst, nb); |
| 270 | |
| 271 | const int64_t n_tokens = mixes->ne[1]; |
| 272 | const float eps = ggml_get_op_params_f32(dst, 0); |
| 273 | const int32_t n_iter = ggml_get_op_params_i32(dst, 1); |
| 274 | |
| 275 | queue_ptr stream = ctx.stream(); |
| 276 | |
| 277 | dsv4_hc_comb_f32_sycl( |
| 278 | (const float *) mixes->data, (const float *) scale->data, (const float *) base->data, (float *) dst->data, |
| 279 | n_tokens, |
| 280 | nbm0 / sizeof(float), nbm1 / sizeof(float), |
| 281 | nbs0 / sizeof(float), |
| 282 | nbb0 / sizeof(float), |
| 283 | nbd0 / sizeof(float), nbd1 / sizeof(float), nbd2 / sizeof(float), |
| 284 | eps, n_iter, stream); |
| 285 | } |
| 286 | |
| 287 | void ggml_sycl_op_dsv4_hc_post(ggml_backend_sycl_context & ctx, ggml_tensor * dst) { |
| 288 | scope_op_debug_print scope_dbg_print(__func__, dst, /*num_src=*/4); |
| 289 | const ggml_tensor * x = dst->src[0]; |
| 290 | const ggml_tensor * residual = dst->src[1]; |
| 291 | const ggml_tensor * post = dst->src[2]; |
| 292 | const ggml_tensor * comb = dst->src[3]; |
| 293 | |
| 294 | GGML_ASSERT(x->type == GGML_TYPE_F32); |
| 295 | GGML_ASSERT(residual->type == GGML_TYPE_F32); |
| 296 | GGML_ASSERT(post->type == GGML_TYPE_F32); |
| 297 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 298 | |
| 299 | GGML_TENSOR_LOCALS(size_t, nbx, x, nb); |
| 300 | GGML_TENSOR_LOCALS(size_t, nbr, residual, nb); |
| 301 | GGML_TENSOR_LOCALS(size_t, nbp, post, nb); |
| 302 | GGML_TENSOR_LOCALS(size_t, nbd, dst, nb); |
| 303 | |
| 304 | size_t nbc0 = 0; |
| 305 | size_t nbc1 = 0; |
| 306 | size_t nbc2 = 0; |
| 307 | if (comb) { |
| 308 | GGML_ASSERT(comb->type == GGML_TYPE_F32); |
| 309 | nbc0 = comb->nb[0]; |
| 310 | nbc1 = comb->nb[1]; |
| 311 | nbc2 = comb->nb[2]; |
| 312 | } |
| 313 | |
| 314 | const int64_t n_embd = x->ne[0]; |
| 315 | const int64_t n_tokens = x->ne[1]; |
| 316 | const int64_t hc = residual->ne[1]; |
| 317 | |
| 318 | queue_ptr stream = ctx.stream(); |
| 319 | |
| 320 | const auto launch = [&](auto has_comb) { |
| 321 | dsv4_hc_post_f32_sycl<decltype(has_comb)::value>( |
| 322 | (const float *) x->data, (const float *) residual->data, |
| 323 | (const float *) post->data, comb ? (const float *) comb->data : nullptr, (float *) dst->data, |
| 324 | n_embd, hc, n_tokens, |
| 325 | nbx0 / sizeof(float), nbx1 / sizeof(float), |
| 326 | nbr0 / sizeof(float), nbr1 / sizeof(float), nbr2 / sizeof(float), |
| 327 | nbp0 / sizeof(float), nbp1 / sizeof(float), |
| 328 | nbc0 / sizeof(float), nbc1 / sizeof(float), nbc2 / sizeof(float), |
| 329 | nbd0 / sizeof(float), nbd1 / sizeof(float), nbd2 / sizeof(float), |
| 330 | stream); |
| 331 | }; |
| 332 | |
| 333 | if (comb) { |
| 334 | launch(std::true_type{}); |
| 335 | } else { |
| 336 | launch(std::false_type{}); |
| 337 | } |
| 338 | } |