ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-cpu / ops.cpp
| 1 | #include "ops.h" |
| 2 | |
| 3 | #include "ggml-cpu.h" |
| 4 | #include "ggml-impl.h" |
| 5 | #include "binary-ops.h" |
| 6 | #include "simd-gemm.h" |
| 7 | #include "ggml.h" |
| 8 | #include "unary-ops.h" |
| 9 | #include "vec.h" |
| 10 | |
| 11 | #include <algorithm> |
| 12 | #include <cfloat> |
| 13 | #include <cmath> |
| 14 | |
| 15 | // ggml_compute_forward_dup |
| 16 | |
| 17 | static void ggml_compute_forward_dup_same_cont( |
| 18 | const ggml_compute_params * params, |
| 19 | ggml_tensor * dst) { |
| 20 | |
| 21 | const ggml_tensor * src0 = dst->src[0]; |
| 22 | |
| 23 | GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0)); |
| 24 | GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0)); |
| 25 | GGML_ASSERT(src0->type == dst->type); |
| 26 | |
| 27 | const size_t nb0 = ggml_type_size(src0->type); |
| 28 | |
| 29 | const int ith = params->ith; // thread index |
| 30 | const int nth = params->nth; // number of threads |
| 31 | |
| 32 | // parallelize by blocks |
| 33 | const int nk = ggml_nelements(src0)/ggml_blck_size(src0->type); |
| 34 | const int dr = (nk + nth - 1) / nth; |
| 35 | const int k0 = dr * ith; |
| 36 | const int k1 = MIN(k0 + dr, nk); |
| 37 | |
| 38 | if (k0 < k1) { |
| 39 | memcpy( |
| 40 | ((char *) dst->data + k0*nb0), |
| 41 | ((char *) src0->data + k0*nb0), |
| 42 | (k1 - k0) * nb0); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | template<typename src_t, typename dst_t> |
| 47 | static void ggml_compute_forward_dup_flt( |
| 48 | const ggml_compute_params * params, |
| 49 | ggml_tensor * dst) { |
| 50 | |
| 51 | const ggml_tensor * src0 = dst->src[0]; |
| 52 | |
| 53 | GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0)); |
| 54 | GGML_ASSERT(!ggml_is_quantized(src0->type) && !ggml_is_quantized(dst->type)); |
| 55 | |
| 56 | GGML_TENSOR_UNARY_OP_LOCALS |
| 57 | |
| 58 | const int ith = params->ith; // thread index |
| 59 | const int nth = params->nth; // number of threads |
| 60 | |
| 61 | // parallelize by rows |
| 62 | const int nr = ne01; |
| 63 | // number of rows per thread |
| 64 | const int dr = (nr + nth - 1) / nth; |
| 65 | // row range for this thread |
| 66 | const int ir0 = dr * ith; |
| 67 | const int ir1 = MIN(ir0 + dr, nr); |
| 68 | |
| 69 | // case: type & row size equal |
| 70 | if (src0->type == dst->type && |
| 71 | ne00 == ne0 && |
| 72 | nb00 == ggml_type_size(src0->type) && nb0 == ggml_type_size(dst->type)) { |
| 73 | // copy by rows |
| 74 | const size_t rs = ne00*nb00; |
| 75 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 76 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 77 | for (int64_t i01 = ir0; i01 < ir1; i01++) { |
| 78 | memcpy( |
| 79 | ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3), |
| 80 | ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03), |
| 81 | rs); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | // case: dst tensor is contiguous |
| 89 | if (ggml_is_contiguous(dst)) { |
| 90 | if (nb00 == sizeof(src_t)) { |
| 91 | if constexpr (std::is_same_v<dst_t, src_t>) { |
| 92 | // same type |
| 93 | size_t id = 0; |
| 94 | const size_t rs = ne00 * nb00; |
| 95 | char * dst_ptr = (char *) dst->data; |
| 96 | |
| 97 | for (int i03 = 0; i03 < ne03; i03++) { |
| 98 | for (int i02 = 0; i02 < ne02; i02++) { |
| 99 | id += rs * ir0; |
| 100 | for (int i01 = ir0; i01 < ir1; i01++) { |
| 101 | const char * src0_ptr = (char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03; |
| 102 | memcpy(dst_ptr + id, src0_ptr, rs); |
| 103 | id += rs; |
| 104 | } |
| 105 | id += rs * (ne01 - ir1); |
| 106 | } |
| 107 | } |
| 108 | } else { |
| 109 | // casting between non-quantized types |
| 110 | size_t id = 0; |
| 111 | dst_t * dst_ptr = (dst_t *) dst->data; |
| 112 | |
| 113 | for (int i03 = 0; i03 < ne03; i03++) { |
| 114 | for (int i02 = 0; i02 < ne02; i02++) { |
| 115 | id += ne00 * ir0; |
| 116 | for (int i01 = ir0; i01 < ir1; i01++) { |
| 117 | const src_t * src0_ptr = (src_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03); |
| 118 | for (int i00 = 0; i00 < ne00; i00++) { |
| 119 | float tmp = type_conversion_table<src_t>::to_f32(src0_ptr[i00]); |
| 120 | dst_ptr[id] = type_conversion_table<dst_t>::from_f32(tmp); |
| 121 | id++; |
| 122 | } |
| 123 | } |
| 124 | id += ne00 * (ne01 - ir1); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } else { |
| 129 | //printf("%s: this is not optimal - fix me\n", __func__); |
| 130 | |
| 131 | size_t id = 0; |
| 132 | dst_t * dst_ptr = (dst_t *) dst->data; |
| 133 | |
| 134 | for (int i03 = 0; i03 < ne03; i03++) { |
| 135 | for (int i02 = 0; i02 < ne02; i02++) { |
| 136 | id += ne00 * ir0; |
| 137 | for (int i01 = ir0; i01 < ir1; i01++) { |
| 138 | for (int i00 = 0; i00 < ne00; i00++) { |
| 139 | const src_t * src0_ptr = (src_t *) ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03); |
| 140 | |
| 141 | float tmp = type_conversion_table<src_t>::to_f32(*src0_ptr); |
| 142 | dst_ptr[id] = type_conversion_table<dst_t>::from_f32(tmp); |
| 143 | id++; |
| 144 | } |
| 145 | } |
| 146 | id += ne00 * (ne01 - ir1); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | // dst counters |
| 154 | int64_t i10 = 0; |
| 155 | int64_t i11 = 0; |
| 156 | int64_t i12 = 0; |
| 157 | int64_t i13 = 0; |
| 158 | |
| 159 | if constexpr (std::is_same_v<dst_t, src_t>) { |
| 160 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 161 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 162 | i10 += ne00 * ir0; |
| 163 | while (i10 >= ne0) { |
| 164 | i10 -= ne0; |
| 165 | if (++i11 == ne1) { |
| 166 | i11 = 0; |
| 167 | if (++i12 == ne2) { |
| 168 | i12 = 0; |
| 169 | if (++i13 == ne3) { |
| 170 | i13 = 0; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | for (int64_t i01 = ir0; i01 < ir1; i01++) { |
| 176 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 177 | const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03); |
| 178 | char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3); |
| 179 | |
| 180 | memcpy(dst_ptr, src0_ptr, sizeof(dst_t)); |
| 181 | |
| 182 | if (++i10 == ne00) { |
| 183 | i10 = 0; |
| 184 | if (++i11 == ne01) { |
| 185 | i11 = 0; |
| 186 | if (++i12 == ne02) { |
| 187 | i12 = 0; |
| 188 | if (++i13 == ne03) { |
| 189 | i13 = 0; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | i10 += ne00 * (ne01 - ir1); |
| 197 | while (i10 >= ne0) { |
| 198 | i10 -= ne0; |
| 199 | if (++i11 == ne1) { |
| 200 | i11 = 0; |
| 201 | if (++i12 == ne2) { |
| 202 | i12 = 0; |
| 203 | if (++i13 == ne3) { |
| 204 | i13 = 0; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | } else { |
| 213 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 214 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 215 | i10 += ne00 * ir0; |
| 216 | while (i10 >= ne0) { |
| 217 | i10 -= ne0; |
| 218 | if (++i11 == ne1) { |
| 219 | i11 = 0; |
| 220 | if (++i12 == ne2) { |
| 221 | i12 = 0; |
| 222 | if (++i13 == ne3) { |
| 223 | i13 = 0; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | for (int64_t i01 = ir0; i01 < ir1; i01++) { |
| 229 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 230 | const char * src0_ptr = ((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03); |
| 231 | char * dst_ptr = ((char *) dst->data + i10*nb0 + i11*nb1 + i12*nb2 + i13*nb3); |
| 232 | |
| 233 | float tmp = type_conversion_table<src_t>::to_f32(*(const src_t *) src0_ptr); |
| 234 | *(dst_t *) dst_ptr = type_conversion_table<dst_t>::from_f32(tmp); |
| 235 | |
| 236 | if (++i10 == ne0) { |
| 237 | i10 = 0; |
| 238 | if (++i11 == ne1) { |
| 239 | i11 = 0; |
| 240 | if (++i12 == ne2) { |
| 241 | i12 = 0; |
| 242 | if (++i13 == ne3) { |
| 243 | i13 = 0; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | i10 += ne00 * (ne01 - ir1); |
| 251 | while (i10 >= ne0) { |
| 252 | i10 -= ne0; |
| 253 | if (++i11 == ne1) { |
| 254 | i11 = 0; |
| 255 | if (++i12 == ne2) { |
| 256 | i12 = 0; |
| 257 | if (++i13 == ne3) { |
| 258 | i13 = 0; |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | |
| 269 | template<typename src_t> |
| 270 | static void ggml_compute_forward_dup_to_q( |
| 271 | const ggml_compute_params * params, |
| 272 | ggml_tensor * dst) { |
| 273 | |
| 274 | const ggml_tensor * src0 = dst->src[0]; |
| 275 | |
| 276 | GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0)); |
| 277 | GGML_ASSERT(!ggml_is_quantized(src0->type)); |
| 278 | |
| 279 | GGML_TENSOR_UNARY_OP_LOCALS |
| 280 | |
| 281 | const int ith = params->ith; // thread index |
| 282 | const int nth = params->nth; // number of threads |
| 283 | |
| 284 | // parallelize by rows |
| 285 | const int nr = ne01; |
| 286 | // number of rows per thread |
| 287 | const int dr = (nr + nth - 1) / nth; |
| 288 | // row range for this thread |
| 289 | const int ir0 = dr * ith; |
| 290 | const int ir1 = MIN(ir0 + dr, nr); |
| 291 | |
| 292 | if (ggml_is_contiguous(dst) && |
| 293 | nb00 == sizeof(src_t) && |
| 294 | ggml_get_type_traits_cpu(dst->type)->from_float) { |
| 295 | // casting non-quantized types --> intermediate f32 --> quantized |
| 296 | ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(dst->type)->from_float; |
| 297 | float * src0_f32 = (float *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith; |
| 298 | |
| 299 | size_t id = 0; |
| 300 | size_t rs = nb0 * (ne00 / ggml_blck_size(dst->type)); |
| 301 | char * dst_ptr = (char *) dst->data; |
| 302 | |
| 303 | for (int i03 = 0; i03 < ne03; i03++) { |
| 304 | for (int i02 = 0; i02 < ne02; i02++) { |
| 305 | id += rs * ir0; |
| 306 | for (int i01 = ir0; i01 < ir1; i01++) { |
| 307 | const src_t * src0_ptr = (src_t *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03); |
| 308 | |
| 309 | for (int i00 = 0; i00 < ne00; i00++) { |
| 310 | src0_f32[i00] = type_conversion_table<src_t>::to_f32(src0_ptr[i00]); |
| 311 | } |
| 312 | |
| 313 | quantize_row_q(src0_f32, dst_ptr + id, ne00); |
| 314 | id += rs; |
| 315 | } |
| 316 | id += rs * (ne01 - ir1); |
| 317 | } |
| 318 | } |
| 319 | } else { |
| 320 | // printf("%s %s\n", ggml_type_name(src0->type), ggml_type_name(dst->type)); |
| 321 | GGML_ABORT("not implemented"); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | // A simplified version of ggml_compute_forward_dup that doesn't do float upcasting, and just plain old memcpy. |
| 326 | static void ggml_compute_forward_dup_bytes( |
| 327 | const ggml_compute_params * params, |
| 328 | ggml_tensor * dst) { |
| 329 | const ggml_tensor * src0 = dst->src[0]; |
| 330 | |
| 331 | GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0)); |
| 332 | GGML_ASSERT(src0->type == dst->type); |
| 333 | |
| 334 | GGML_TENSOR_UNARY_OP_LOCALS; |
| 335 | |
| 336 | if (ggml_is_contiguous(src0) && ggml_is_contiguous(dst)) { |
| 337 | ggml_compute_forward_dup_same_cont(params, dst); |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | const size_t type_size = ggml_type_size(src0->type); |
| 342 | |
| 343 | const int ith = params->ith; // thread index |
| 344 | const int nth = params->nth; // number of threads |
| 345 | |
| 346 | // parallelize by rows |
| 347 | const int nr = ne01; |
| 348 | // number of rows per thread |
| 349 | const int dr = (nr + nth - 1) / nth; |
| 350 | // row range for this thread |
| 351 | const int ir0 = dr * ith; |
| 352 | const int ir1 = MIN(ir0 + dr, nr); |
| 353 | |
| 354 | if (src0->type == dst->type && |
| 355 | ggml_are_same_shape(src0, dst) && |
| 356 | nb00 == type_size && nb0 == type_size) { |
| 357 | // copy by rows |
| 358 | const size_t rs = ggml_row_size(src0->type, ne00); |
| 359 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 360 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 361 | for (int64_t i01 = ir0; i01 < ir1; i01++) { |
| 362 | memcpy( |
| 363 | ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3), |
| 364 | ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03), |
| 365 | rs); |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | if (ggml_is_contiguous(dst)) { |
| 373 | size_t id = 0; |
| 374 | char * dst_ptr = (char *) dst->data; |
| 375 | const size_t rs = ne00 * type_size; |
| 376 | |
| 377 | if (nb00 == type_size) { |
| 378 | // src0 is contiguous on first dimension, copy by rows |
| 379 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 380 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 381 | id += rs * ir0; |
| 382 | for (int64_t i01 = ir0; i01 < ir1; i01++) { |
| 383 | const char * src0_ptr = (char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03; |
| 384 | memcpy(dst_ptr + id, src0_ptr, rs); |
| 385 | id += rs; |
| 386 | } |
| 387 | id += rs * (ne01 - ir1); |
| 388 | } |
| 389 | } |
| 390 | } else { |
| 391 | //printf("%s: this is not optimal - fix me\n", __func__); |
| 392 | |
| 393 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 394 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 395 | id += rs * ir0; |
| 396 | for (int64_t i01 = ir0; i01 < ir1; i01++) { |
| 397 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 398 | const char * src0_ptr = (char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03; |
| 399 | memcpy(dst_ptr + id, src0_ptr, type_size); |
| 400 | |
| 401 | id += type_size; |
| 402 | } |
| 403 | } |
| 404 | id += rs * (ne01 - ir1); |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | // dst counters |
| 413 | int64_t k10 = 0; |
| 414 | int64_t i11 = 0; |
| 415 | int64_t i12 = 0; |
| 416 | int64_t i13 = 0; |
| 417 | |
| 418 | // number of blocks in a row |
| 419 | const int64_t nk00 = ne00 / ggml_blck_size(src0->type); |
| 420 | const int64_t nk0 = ne0 / ggml_blck_size(dst->type); |
| 421 | |
| 422 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 423 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 424 | k10 += nk00 * ir0; |
| 425 | while (k10 >= nk0) { |
| 426 | k10 -= nk0; |
| 427 | if (++i11 == ne1) { |
| 428 | i11 = 0; |
| 429 | if (++i12 == ne2) { |
| 430 | i12 = 0; |
| 431 | if (++i13 == ne3) { |
| 432 | i13 = 0; |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | for (int64_t i01 = ir0; i01 < ir1; i01++) { |
| 438 | for (int64_t k00 = 0; k00 < nk00; k00++) { |
| 439 | const char * src0_ptr = ((char *) src0->data + k00*nb00 + i01*nb01 + i02*nb02 + i03*nb03); |
| 440 | char * dst_ptr = ((char *) dst->data + k10*nb0 + i11*nb1 + i12*nb2 + i13*nb3); |
| 441 | |
| 442 | memcpy(dst_ptr, src0_ptr, type_size); |
| 443 | |
| 444 | if (++k10 == nk0) { |
| 445 | k10 = 0; |
| 446 | if (++i11 == ne1) { |
| 447 | i11 = 0; |
| 448 | if (++i12 == ne2) { |
| 449 | i12 = 0; |
| 450 | if (++i13 == ne3) { |
| 451 | i13 = 0; |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | k10 += nk00 * (ne01 - ir1); |
| 459 | while (k10 >= nk0) { |
| 460 | k10 -= nk0; |
| 461 | if (++i11 == ne1) { |
| 462 | i11 = 0; |
| 463 | if (++i12 == ne2) { |
| 464 | i12 = 0; |
| 465 | if (++i13 == ne3) { |
| 466 | i13 = 0; |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | static void ggml_compute_forward_dup_from_q( |
| 476 | const ggml_compute_params * params, |
| 477 | ggml_tensor * dst) { |
| 478 | |
| 479 | const ggml_tensor * src0 = dst->src[0]; |
| 480 | const ggml_tensor * src1 = dst->src[1]; |
| 481 | |
| 482 | GGML_TENSOR_BINARY_OP_LOCALS |
| 483 | |
| 484 | const ggml_type type = src0->type; |
| 485 | ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float; |
| 486 | |
| 487 | size_t qk = ggml_blck_size(type); |
| 488 | const int64_t nr = ggml_nelements(src1) / qk; |
| 489 | |
| 490 | // destination must be contiguous in the first dimension |
| 491 | GGML_ASSERT(nb10 == ggml_type_size(dst->type)); |
| 492 | // must either have first dimension large enough to hold a row, or fully contiguous |
| 493 | GGML_ASSERT((ne10 % qk) == 0 || ggml_is_contiguous(dst)); |
| 494 | |
| 495 | const int ith = params->ith; |
| 496 | const int nth = params->nth; |
| 497 | |
| 498 | const int dr = (nr + nth - 1)/nth; |
| 499 | |
| 500 | // row range for this thread |
| 501 | const int ir0 = dr*ith; |
| 502 | const int ir1 = MIN(ir0 + dr, nr); |
| 503 | |
| 504 | for (int64_t ir = ir0; ir < ir1; ++ir) { |
| 505 | |
| 506 | uint32_t i = ir * qk; |
| 507 | |
| 508 | const int64_t i03 = i/(ne00 * ne01 * ne02); |
| 509 | const int64_t i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01); |
| 510 | const int64_t i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00; |
| 511 | const int64_t i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00; |
| 512 | const int64_t x_offset = (i00/qk)*nb00 + i01*nb01 + i02*nb02 + i03 * nb03; |
| 513 | |
| 514 | const int64_t i13 = i/(ne10 * ne11 * ne12); |
| 515 | const int64_t i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11); |
| 516 | const int64_t i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10; |
| 517 | const int64_t i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10; |
| 518 | const int64_t dst_offset = i10*nb10 + i11*nb11 + i12*nb12 + i13*nb13; |
| 519 | |
| 520 | dequantize_row_q( |
| 521 | (const void *) ((char *) src0->data + x_offset), |
| 522 | (float *) ((char *) dst->data + dst_offset), qk); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | void ggml_compute_forward_dup( |
| 527 | const ggml_compute_params * params, |
| 528 | ggml_tensor * dst) { |
| 529 | |
| 530 | const ggml_tensor * src0 = dst->src[0]; |
| 531 | |
| 532 | if (src0->type == dst->type) { |
| 533 | ggml_compute_forward_dup_bytes(params, dst); |
| 534 | return; |
| 535 | } |
| 536 | |
| 537 | switch (src0->type) { |
| 538 | case GGML_TYPE_F16: |
| 539 | { |
| 540 | /**/ if (dst->type == GGML_TYPE_F16) ggml_compute_forward_dup_flt<ggml_fp16_t, ggml_fp16_t>(params, dst); |
| 541 | else if (dst->type == GGML_TYPE_BF16) ggml_compute_forward_dup_flt<ggml_fp16_t, ggml_bf16_t>(params, dst); |
| 542 | else if (dst->type == GGML_TYPE_F32) ggml_compute_forward_dup_flt<ggml_fp16_t, float >(params, dst); |
| 543 | else if (dst->type == GGML_TYPE_F8_E4M3) ggml_compute_forward_dup_flt<ggml_fp16_t, ggml_fp8_e4m3_t>(params, dst); |
| 544 | else ggml_compute_forward_dup_to_q<ggml_fp16_t>(params, dst); |
| 545 | } break; |
| 546 | case GGML_TYPE_BF16: |
| 547 | { |
| 548 | /**/ if (dst->type == GGML_TYPE_F16) ggml_compute_forward_dup_flt<ggml_bf16_t, ggml_fp16_t>(params, dst); |
| 549 | else if (dst->type == GGML_TYPE_BF16) ggml_compute_forward_dup_flt<ggml_bf16_t, ggml_bf16_t>(params, dst); |
| 550 | else if (dst->type == GGML_TYPE_F32) ggml_compute_forward_dup_flt<ggml_bf16_t, float >(params, dst); |
| 551 | else if (dst->type == GGML_TYPE_F8_E4M3) ggml_compute_forward_dup_flt<ggml_bf16_t, ggml_fp8_e4m3_t>(params, dst); |
| 552 | else ggml_compute_forward_dup_to_q<ggml_bf16_t>(params, dst); |
| 553 | } break; |
| 554 | case GGML_TYPE_F32: |
| 555 | { |
| 556 | /**/ if (dst->type == GGML_TYPE_F16) ggml_compute_forward_dup_flt<float, ggml_fp16_t>(params, dst); |
| 557 | else if (dst->type == GGML_TYPE_BF16) ggml_compute_forward_dup_flt<float, ggml_bf16_t>(params, dst); |
| 558 | else if (dst->type == GGML_TYPE_F32) ggml_compute_forward_dup_flt<float, float >(params, dst); |
| 559 | else if (dst->type == GGML_TYPE_F8_E4M3) ggml_compute_forward_dup_flt<float, ggml_fp8_e4m3_t>(params, dst); |
| 560 | else if (dst->type == GGML_TYPE_I32) ggml_compute_forward_dup_flt<float, int32_t >(params, dst); |
| 561 | else ggml_compute_forward_dup_to_q<float>(params, dst); |
| 562 | } break; |
| 563 | case GGML_TYPE_F8_E4M3: |
| 564 | { |
| 565 | /**/ if (dst->type == GGML_TYPE_F16) ggml_compute_forward_dup_flt<ggml_fp8_e4m3_t, ggml_fp16_t>(params, dst); |
| 566 | else if (dst->type == GGML_TYPE_BF16) ggml_compute_forward_dup_flt<ggml_fp8_e4m3_t, ggml_bf16_t>(params, dst); |
| 567 | else if (dst->type == GGML_TYPE_F32) ggml_compute_forward_dup_flt<ggml_fp8_e4m3_t, float >(params, dst); |
| 568 | else GGML_ABORT("not implemented"); |
| 569 | } break; |
| 570 | case GGML_TYPE_I32: |
| 571 | { |
| 572 | if (dst->type == GGML_TYPE_F32) ggml_compute_forward_dup_flt<int32_t, float>(params, dst); |
| 573 | else GGML_ABORT("not implemented"); |
| 574 | } break; |
| 575 | default: |
| 576 | { |
| 577 | if (ggml_is_quantized(src0->type) && dst->type == GGML_TYPE_F32) { |
| 578 | ggml_compute_forward_dup_from_q(params, dst); |
| 579 | break; |
| 580 | } |
| 581 | GGML_ABORT("fatal error"); |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | // ggml_compute_forward_add |
| 587 | |
| 588 | static void ggml_compute_forward_add_q_f32( |
| 589 | const ggml_compute_params * params, |
| 590 | ggml_tensor * dst) { |
| 591 | |
| 592 | const ggml_tensor * src0 = dst->src[0]; |
| 593 | const ggml_tensor * src1 = dst->src[1]; |
| 594 | |
| 595 | GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst)); |
| 596 | |
| 597 | const int nr = ggml_nrows(src0); |
| 598 | |
| 599 | GGML_TENSOR_BINARY_OP_LOCALS |
| 600 | |
| 601 | const int ith = params->ith; |
| 602 | const int nth = params->nth; |
| 603 | |
| 604 | const ggml_type type = src0->type; |
| 605 | const ggml_type dtype = dst->type; |
| 606 | ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float; |
| 607 | ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(dtype)->from_float; |
| 608 | |
| 609 | // we don't support permuted src0 or src1 |
| 610 | GGML_ASSERT(nb00 == ggml_type_size(type)); |
| 611 | GGML_ASSERT(nb10 == sizeof(float)); |
| 612 | |
| 613 | // dst cannot be transposed or permuted |
| 614 | GGML_ASSERT(nb0 <= nb1); |
| 615 | GGML_ASSERT(nb1 <= nb2); |
| 616 | GGML_ASSERT(nb2 <= nb3); |
| 617 | |
| 618 | GGML_ASSERT(ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F8_E4M3); |
| 619 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 620 | GGML_ASSERT(ne00 % ggml_blck_size(type) == 0); |
| 621 | |
| 622 | // rows per thread |
| 623 | const int dr = (nr + nth - 1)/nth; |
| 624 | |
| 625 | // row range for this thread |
| 626 | const int ir0 = dr*ith; |
| 627 | const int ir1 = MIN(ir0 + dr, nr); |
| 628 | |
| 629 | float * wdata = (float *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith; |
| 630 | |
| 631 | for (int ir = ir0; ir < ir1; ++ir) { |
| 632 | // src0 indices |
| 633 | const int i03 = ir/(ne02*ne01); |
| 634 | const int i02 = (ir - i03*ne02*ne01)/ne01; |
| 635 | const int i01 = (ir - i03*ne02*ne01 - i02*ne01); |
| 636 | |
| 637 | // src1 and dst are same shape as src0 => same indices |
| 638 | const int i13 = i03; |
| 639 | const int i12 = i02; |
| 640 | const int i11 = i01; |
| 641 | |
| 642 | const int i3 = i03; |
| 643 | const int i2 = i02; |
| 644 | const int i1 = i01; |
| 645 | |
| 646 | void * src0_row = (void *) ((char *) src0->data + (i01*nb01 + i02*nb02 + i03*nb03)); |
| 647 | float * src1_row = (float *)((char *) src1->data + (i11*nb11 + i12*nb12 + i13*nb13)); |
| 648 | void * dst_row = (void *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3)); |
| 649 | |
| 650 | // unquantize row from src0 to temp buffer |
| 651 | dequantize_row_q(src0_row, wdata, ne00); |
| 652 | // add src1 |
| 653 | ggml_vec_acc_f32(ne00, wdata, src1_row); |
| 654 | // quantize row to dst |
| 655 | if (quantize_row_q != NULL) { |
| 656 | quantize_row_q(wdata, dst_row, ne00); |
| 657 | } else { |
| 658 | memcpy(dst_row, wdata, ne0*nb0); |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | void ggml_compute_forward_add( |
| 664 | const ggml_compute_params * params, |
| 665 | ggml_tensor * dst) { |
| 666 | |
| 667 | const ggml_tensor * src0 = dst->src[0]; |
| 668 | |
| 669 | switch (src0->type) { |
| 670 | case GGML_TYPE_F32: |
| 671 | case GGML_TYPE_F16: |
| 672 | case GGML_TYPE_BF16: |
| 673 | { |
| 674 | ggml_compute_forward_add_non_quantized(params, dst); |
| 675 | } break; |
| 676 | case GGML_TYPE_Q1_0: |
| 677 | case GGML_TYPE_Q2_0: |
| 678 | case GGML_TYPE_Q4_0: |
| 679 | case GGML_TYPE_Q4_1: |
| 680 | case GGML_TYPE_Q5_0: |
| 681 | case GGML_TYPE_Q5_1: |
| 682 | case GGML_TYPE_Q8_0: |
| 683 | case GGML_TYPE_MXFP4: |
| 684 | case GGML_TYPE_NVFP4: |
| 685 | case GGML_TYPE_F8_E4M3: |
| 686 | case GGML_TYPE_Q2_K: |
| 687 | case GGML_TYPE_Q3_K: |
| 688 | case GGML_TYPE_Q4_K: |
| 689 | case GGML_TYPE_Q5_K: |
| 690 | case GGML_TYPE_Q6_K: |
| 691 | case GGML_TYPE_TQ1_0: |
| 692 | case GGML_TYPE_TQ2_0: |
| 693 | case GGML_TYPE_IQ2_XXS: |
| 694 | case GGML_TYPE_IQ2_XS: |
| 695 | case GGML_TYPE_IQ3_XXS: |
| 696 | case GGML_TYPE_IQ1_S: |
| 697 | case GGML_TYPE_IQ1_M: |
| 698 | case GGML_TYPE_IQ4_NL: |
| 699 | case GGML_TYPE_IQ4_XS: |
| 700 | case GGML_TYPE_IQ3_S: |
| 701 | case GGML_TYPE_IQ2_S: |
| 702 | { |
| 703 | ggml_compute_forward_add_q_f32(params, dst); |
| 704 | } break; |
| 705 | default: |
| 706 | { |
| 707 | GGML_ABORT("fatal error"); |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | // ggml_compute_forward_add_id |
| 713 | |
| 714 | static void ggml_compute_forward_add_id_f32( |
| 715 | const ggml_compute_params * params, |
| 716 | ggml_tensor * dst) { |
| 717 | |
| 718 | const ggml_tensor * src0 = dst->src[0]; |
| 719 | const ggml_tensor * src1 = dst->src[1]; |
| 720 | const ggml_tensor * src2 = dst->src[2]; |
| 721 | |
| 722 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 723 | GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 724 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 725 | GGML_ASSERT(src2->type == GGML_TYPE_I32); |
| 726 | |
| 727 | GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 728 | GGML_ASSERT(src1->nb[0] == sizeof(float)); |
| 729 | |
| 730 | const int ith = params->ith; |
| 731 | const int nth = params->nth; |
| 732 | |
| 733 | const int nr = ggml_nrows(src0); |
| 734 | |
| 735 | GGML_TENSOR_TERNARY_OP_LOCALS |
| 736 | |
| 737 | GGML_ASSERT( nb0 == sizeof(float)); |
| 738 | GGML_ASSERT(nb10 == sizeof(float)); |
| 739 | |
| 740 | // rows per thread |
| 741 | const int dr = (nr + nth - 1)/nth; |
| 742 | |
| 743 | // row range for this thread |
| 744 | const int ir0 = dr*ith; |
| 745 | const int ir1 = MIN(ir0 + dr, nr); |
| 746 | |
| 747 | for (int ir = ir0; ir < ir1; ++ir) { |
| 748 | // src0 indices |
| 749 | const int i3 = ir/(ne2*ne1); |
| 750 | const int i2 = (ir - i3*ne2*ne1)/ne1; |
| 751 | const int i1 = (ir - i3*ne2*ne1 - i2*ne1); |
| 752 | |
| 753 | // src1 indices |
| 754 | const int i11 = *(int32_t *) ((char *) src2->data + i1*nb20 + i2*nb21); |
| 755 | |
| 756 | GGML_ASSERT(i11 == -1 || (i11 >= 0 && i11 < ne11)); |
| 757 | |
| 758 | float * dst_row = (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ); |
| 759 | const float * src0_row = (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01); |
| 760 | |
| 761 | if (i11 == -1) { |
| 762 | memcpy(dst_row, src0_row, ne0*sizeof(float)); |
| 763 | continue; |
| 764 | } |
| 765 | |
| 766 | ggml_vec_add_f32(ne0, dst_row, src0_row, (float *) ((char *) src1->data + i11*nb11)); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | void ggml_compute_forward_add_id( |
| 771 | const ggml_compute_params * params, |
| 772 | ggml_tensor * dst) { |
| 773 | |
| 774 | const ggml_tensor * src0 = dst->src[0]; |
| 775 | |
| 776 | switch (src0->type) { |
| 777 | case GGML_TYPE_F32: |
| 778 | { |
| 779 | ggml_compute_forward_add_id_f32(params, dst); |
| 780 | } break; |
| 781 | default: |
| 782 | { |
| 783 | GGML_ABORT("unsupported type for ggml_compute_forward_add_id: %s", ggml_type_name(src0->type)); |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | // ggml_compute_forward_add1 |
| 789 | |
| 790 | static void ggml_compute_forward_add1_f32( |
| 791 | const ggml_compute_params * params, |
| 792 | ggml_tensor * dst) { |
| 793 | |
| 794 | const ggml_tensor * src0 = dst->src[0]; |
| 795 | const ggml_tensor * src1 = dst->src[1]; |
| 796 | |
| 797 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 798 | GGML_ASSERT(ggml_is_scalar(src1)); |
| 799 | |
| 800 | const int ith = params->ith; |
| 801 | const int nth = params->nth; |
| 802 | |
| 803 | const int nr = ggml_nrows(src0); |
| 804 | |
| 805 | GGML_TENSOR_UNARY_OP_LOCALS |
| 806 | |
| 807 | GGML_ASSERT( nb0 == sizeof(float)); |
| 808 | GGML_ASSERT(nb00 == sizeof(float)); |
| 809 | |
| 810 | // rows per thread |
| 811 | const int dr = (nr + nth - 1)/nth; |
| 812 | |
| 813 | // row range for this thread |
| 814 | const int ir0 = dr*ith; |
| 815 | const int ir1 = MIN(ir0 + dr, nr); |
| 816 | |
| 817 | for (int ir = ir0; ir < ir1; ++ir) { |
| 818 | // src0 and dst are same shape => same indices |
| 819 | const int i3 = ir/(ne2*ne1); |
| 820 | const int i2 = (ir - i3*ne2*ne1)/ne1; |
| 821 | const int i1 = (ir - i3*ne2*ne1 - i2*ne1); |
| 822 | |
| 823 | #ifdef GGML_USE_ACCELERATE |
| 824 | GGML_UNUSED(ggml_vec_add1_f32); |
| 825 | |
| 826 | vDSP_vadd( |
| 827 | (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01), 1, |
| 828 | (float *) ((char *) src1->data), 0, |
| 829 | (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ), 1, |
| 830 | ne0); |
| 831 | #else |
| 832 | ggml_vec_add1_f32(ne0, |
| 833 | (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ), |
| 834 | (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01), |
| 835 | *(float *) src1->data); |
| 836 | #endif |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | static void ggml_compute_forward_add1_f16_f32( |
| 841 | const ggml_compute_params * params, |
| 842 | ggml_tensor * dst) { |
| 843 | |
| 844 | const ggml_tensor * src0 = dst->src[0]; |
| 845 | const ggml_tensor * src1 = dst->src[1]; |
| 846 | |
| 847 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 848 | GGML_ASSERT(ggml_is_scalar(src1)); |
| 849 | |
| 850 | // scalar to add |
| 851 | const float v = *(float *) src1->data; |
| 852 | |
| 853 | const int ith = params->ith; |
| 854 | const int nth = params->nth; |
| 855 | |
| 856 | const int nr = ggml_nrows(src0); |
| 857 | |
| 858 | GGML_TENSOR_UNARY_OP_LOCALS |
| 859 | |
| 860 | GGML_ASSERT(src0->type == GGML_TYPE_F16); |
| 861 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 862 | GGML_ASSERT(dst->type == GGML_TYPE_F16); |
| 863 | |
| 864 | GGML_ASSERT( nb0 == sizeof(ggml_fp16_t)); |
| 865 | GGML_ASSERT(nb00 == sizeof(ggml_fp16_t)); |
| 866 | |
| 867 | // rows per thread |
| 868 | const int dr = (nr + nth - 1)/nth; |
| 869 | |
| 870 | // row range for this thread |
| 871 | const int ir0 = dr*ith; |
| 872 | const int ir1 = MIN(ir0 + dr, nr); |
| 873 | |
| 874 | for (int ir = ir0; ir < ir1; ++ir) { |
| 875 | // src0 and dst are same shape => same indices |
| 876 | const int i3 = ir/(ne2*ne1); |
| 877 | const int i2 = (ir - i3*ne2*ne1)/ne1; |
| 878 | const int i1 = (ir - i3*ne2*ne1 - i2*ne1); |
| 879 | |
| 880 | ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ); |
| 881 | ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01); |
| 882 | for (int i = 0; i < ne0; i++) { |
| 883 | dst_ptr[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(src0_ptr[i]) + v); |
| 884 | } |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | static void ggml_compute_forward_add1_f16_f16( |
| 889 | const ggml_compute_params * params, |
| 890 | ggml_tensor * dst) { |
| 891 | |
| 892 | const ggml_tensor * src0 = dst->src[0]; |
| 893 | const ggml_tensor * src1 = dst->src[1]; |
| 894 | |
| 895 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 896 | GGML_ASSERT(ggml_is_scalar(src1)); |
| 897 | |
| 898 | // scalar to add |
| 899 | const float v = GGML_CPU_FP16_TO_FP32(*(ggml_fp16_t *) src1->data); |
| 900 | |
| 901 | const int ith = params->ith; |
| 902 | const int nth = params->nth; |
| 903 | |
| 904 | const int nr = ggml_nrows(src0); |
| 905 | |
| 906 | GGML_TENSOR_UNARY_OP_LOCALS |
| 907 | |
| 908 | GGML_ASSERT(src0->type == GGML_TYPE_F16); |
| 909 | GGML_ASSERT(src1->type == GGML_TYPE_F16); |
| 910 | GGML_ASSERT(dst->type == GGML_TYPE_F16); |
| 911 | |
| 912 | GGML_ASSERT( nb0 == sizeof(ggml_fp16_t)); |
| 913 | GGML_ASSERT(nb00 == sizeof(ggml_fp16_t)); |
| 914 | |
| 915 | // rows per thread |
| 916 | const int dr = (nr + nth - 1)/nth; |
| 917 | |
| 918 | // row range for this thread |
| 919 | const int ir0 = dr*ith; |
| 920 | const int ir1 = MIN(ir0 + dr, nr); |
| 921 | |
| 922 | for (int ir = ir0; ir < ir1; ++ir) { |
| 923 | // src0 and dst are same shape => same indices |
| 924 | const int i3 = ir/(ne2*ne1); |
| 925 | const int i2 = (ir - i3*ne2*ne1)/ne1; |
| 926 | const int i1 = (ir - i3*ne2*ne1 - i2*ne1); |
| 927 | |
| 928 | ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ); |
| 929 | ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01); |
| 930 | for (int i = 0; i < ne0; i++) { |
| 931 | dst_ptr[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(src0_ptr[i]) + v); |
| 932 | } |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | static void ggml_compute_forward_add1_q_f32( |
| 937 | const ggml_compute_params * params, |
| 938 | ggml_tensor * dst) { |
| 939 | |
| 940 | const ggml_tensor * src0 = dst->src[0]; |
| 941 | const ggml_tensor * src1 = dst->src[1]; |
| 942 | |
| 943 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 944 | GGML_ASSERT(ggml_is_scalar(src1)); |
| 945 | |
| 946 | // scalar to add |
| 947 | const float v = *(float *) src1->data; |
| 948 | |
| 949 | const int ith = params->ith; |
| 950 | const int nth = params->nth; |
| 951 | |
| 952 | const int nr = ggml_nrows(src0); |
| 953 | |
| 954 | GGML_TENSOR_UNARY_OP_LOCALS |
| 955 | |
| 956 | const ggml_type type = src0->type; |
| 957 | ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float; |
| 958 | ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(type)->from_float; |
| 959 | |
| 960 | // we don't support permuted src0 |
| 961 | GGML_ASSERT(nb00 == ggml_type_size(type)); |
| 962 | |
| 963 | // dst cannot be transposed or permuted |
| 964 | GGML_ASSERT(nb0 <= nb1); |
| 965 | GGML_ASSERT(nb1 <= nb2); |
| 966 | GGML_ASSERT(nb2 <= nb3); |
| 967 | |
| 968 | GGML_ASSERT(ggml_is_quantized(src0->type) || src0->type == GGML_TYPE_F8_E4M3); |
| 969 | GGML_ASSERT(dst->type == src0->type); |
| 970 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 971 | |
| 972 | // rows per thread |
| 973 | const int dr = (nr + nth - 1)/nth; |
| 974 | |
| 975 | // row range for this thread |
| 976 | const int ir0 = dr*ith; |
| 977 | const int ir1 = MIN(ir0 + dr, nr); |
| 978 | |
| 979 | float * wdata = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32) * ith; |
| 980 | |
| 981 | for (int ir = ir0; ir < ir1; ++ir) { |
| 982 | // src0 and dst are same shape => same indices |
| 983 | const int i3 = ir/(ne2*ne1); |
| 984 | const int i2 = (ir - i3*ne2*ne1)/ne1; |
| 985 | const int i1 = (ir - i3*ne2*ne1 - i2*ne1); |
| 986 | |
| 987 | void * src0_row = (void *) ((char *) src0->data + (i1*nb01 + i2*nb02 + i3*nb03)); |
| 988 | void * dst_row = (void *) ((char *) dst->data + (i1*nb1 + i2*nb2 + i3*nb0 )); |
| 989 | |
| 990 | assert(ne0 % 32 == 0); |
| 991 | |
| 992 | // unquantize row from src0 to temp buffer |
| 993 | dequantize_row_q(src0_row, wdata, ne0); |
| 994 | // add src1 |
| 995 | ggml_vec_acc1_f32(ne0, wdata, v); |
| 996 | // quantize row to dst |
| 997 | quantize_row_q(wdata, dst_row, ne0); |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | static void ggml_compute_forward_add1_bf16_f32( |
| 1002 | const ggml_compute_params * params, |
| 1003 | ggml_tensor * dst) { |
| 1004 | |
| 1005 | const ggml_tensor * src0 = dst->src[0]; |
| 1006 | const ggml_tensor * src1 = dst->src[1]; |
| 1007 | |
| 1008 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 1009 | GGML_ASSERT(ggml_is_scalar(src1)); |
| 1010 | |
| 1011 | // scalar to add |
| 1012 | const float v = *(float *) src1->data; |
| 1013 | |
| 1014 | const int ith = params->ith; |
| 1015 | const int nth = params->nth; |
| 1016 | |
| 1017 | const int nr = ggml_nrows(src0); |
| 1018 | |
| 1019 | GGML_TENSOR_UNARY_OP_LOCALS |
| 1020 | |
| 1021 | GGML_ASSERT(src0->type == GGML_TYPE_BF16); |
| 1022 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 1023 | GGML_ASSERT(dst->type == GGML_TYPE_BF16); |
| 1024 | |
| 1025 | GGML_ASSERT( nb0 == sizeof(ggml_bf16_t)); |
| 1026 | GGML_ASSERT(nb00 == sizeof(ggml_bf16_t)); |
| 1027 | |
| 1028 | // rows per thread |
| 1029 | const int dr = (nr + nth - 1)/nth; |
| 1030 | |
| 1031 | // row range for this thread |
| 1032 | const int ir0 = dr*ith; |
| 1033 | const int ir1 = MIN(ir0 + dr, nr); |
| 1034 | |
| 1035 | for (int ir = ir0; ir < ir1; ++ir) { |
| 1036 | // src0 and dst are same shape => same indices |
| 1037 | const int i3 = ir/(ne2*ne1); |
| 1038 | const int i2 = (ir - i3*ne2*ne1)/ne1; |
| 1039 | const int i1 = (ir - i3*ne2*ne1 - i2*ne1); |
| 1040 | |
| 1041 | ggml_bf16_t * dst_ptr = (ggml_bf16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ); |
| 1042 | ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01); |
| 1043 | for (int i = 0; i < ne0; i++) { |
| 1044 | dst_ptr[i] = GGML_FP32_TO_BF16(GGML_BF16_TO_FP32(src0_ptr[i]) + v); |
| 1045 | } |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | static void ggml_compute_forward_add1_bf16_bf16( |
| 1050 | const ggml_compute_params * params, |
| 1051 | ggml_tensor * dst) { |
| 1052 | |
| 1053 | const ggml_tensor * src0 = dst->src[0]; |
| 1054 | const ggml_tensor * src1 = dst->src[1]; |
| 1055 | |
| 1056 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 1057 | GGML_ASSERT(ggml_is_scalar(src1)); |
| 1058 | |
| 1059 | // scalar to add |
| 1060 | const float v = GGML_BF16_TO_FP32(*(ggml_bf16_t *) src1->data); |
| 1061 | |
| 1062 | const int ith = params->ith; |
| 1063 | const int nth = params->nth; |
| 1064 | |
| 1065 | const int nr = ggml_nrows(src0); |
| 1066 | |
| 1067 | GGML_TENSOR_UNARY_OP_LOCALS |
| 1068 | |
| 1069 | GGML_ASSERT(src0->type == GGML_TYPE_BF16); |
| 1070 | GGML_ASSERT(src1->type == GGML_TYPE_BF16); |
| 1071 | GGML_ASSERT(dst->type == GGML_TYPE_BF16); |
| 1072 | |
| 1073 | GGML_ASSERT( nb0 == sizeof(ggml_bf16_t)); |
| 1074 | GGML_ASSERT(nb00 == sizeof(ggml_bf16_t)); |
| 1075 | |
| 1076 | // rows per thread |
| 1077 | const int dr = (nr + nth - 1)/nth; |
| 1078 | |
| 1079 | // row range for this thread |
| 1080 | const int ir0 = dr*ith; |
| 1081 | const int ir1 = MIN(ir0 + dr, nr); |
| 1082 | |
| 1083 | for (int ir = ir0; ir < ir1; ++ir) { |
| 1084 | // src0 and dst are same shape => same indices |
| 1085 | const int i3 = ir/(ne2*ne1); |
| 1086 | const int i2 = (ir - i3*ne2*ne1)/ne1; |
| 1087 | const int i1 = (ir - i3*ne2*ne1 - i2*ne1); |
| 1088 | |
| 1089 | ggml_bf16_t * dst_ptr = (ggml_bf16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ); |
| 1090 | ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01); |
| 1091 | for (int i = 0; i < ne0; i++) { |
| 1092 | dst_ptr[i] = GGML_FP32_TO_BF16(GGML_BF16_TO_FP32(src0_ptr[i]) + v); |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | void ggml_compute_forward_add1( |
| 1098 | const ggml_compute_params * params, |
| 1099 | ggml_tensor * dst) { |
| 1100 | |
| 1101 | const ggml_tensor * src0 = dst->src[0]; |
| 1102 | const ggml_tensor * src1 = dst->src[1]; |
| 1103 | |
| 1104 | switch (src0->type) { |
| 1105 | case GGML_TYPE_F32: |
| 1106 | { |
| 1107 | ggml_compute_forward_add1_f32(params, dst); |
| 1108 | } break; |
| 1109 | case GGML_TYPE_F16: |
| 1110 | { |
| 1111 | if (src1->type == GGML_TYPE_F16) { |
| 1112 | ggml_compute_forward_add1_f16_f16(params, dst); |
| 1113 | } |
| 1114 | else if (src1->type == GGML_TYPE_F32) { |
| 1115 | ggml_compute_forward_add1_f16_f32(params, dst); |
| 1116 | } |
| 1117 | else { |
| 1118 | GGML_ABORT("fatal error"); |
| 1119 | } |
| 1120 | } break; |
| 1121 | case GGML_TYPE_BF16: |
| 1122 | { |
| 1123 | if (src1->type == GGML_TYPE_BF16) { |
| 1124 | ggml_compute_forward_add1_bf16_bf16(params, dst); |
| 1125 | } |
| 1126 | else if (src1->type == GGML_TYPE_F32) { |
| 1127 | ggml_compute_forward_add1_bf16_f32(params, dst); |
| 1128 | } |
| 1129 | else { |
| 1130 | GGML_ABORT("fatal error"); |
| 1131 | } |
| 1132 | } break; |
| 1133 | case GGML_TYPE_Q1_0: |
| 1134 | case GGML_TYPE_Q2_0: |
| 1135 | case GGML_TYPE_Q4_0: |
| 1136 | case GGML_TYPE_Q4_1: |
| 1137 | case GGML_TYPE_Q5_0: |
| 1138 | case GGML_TYPE_Q5_1: |
| 1139 | case GGML_TYPE_Q8_0: |
| 1140 | case GGML_TYPE_Q8_1: |
| 1141 | case GGML_TYPE_MXFP4: |
| 1142 | case GGML_TYPE_NVFP4: |
| 1143 | case GGML_TYPE_F8_E4M3: |
| 1144 | case GGML_TYPE_Q2_K: |
| 1145 | case GGML_TYPE_Q3_K: |
| 1146 | case GGML_TYPE_Q4_K: |
| 1147 | case GGML_TYPE_Q5_K: |
| 1148 | case GGML_TYPE_Q6_K: |
| 1149 | case GGML_TYPE_TQ1_0: |
| 1150 | case GGML_TYPE_TQ2_0: |
| 1151 | case GGML_TYPE_IQ2_XXS: |
| 1152 | case GGML_TYPE_IQ2_XS: |
| 1153 | case GGML_TYPE_IQ3_XXS: |
| 1154 | case GGML_TYPE_IQ1_S: |
| 1155 | case GGML_TYPE_IQ1_M: |
| 1156 | case GGML_TYPE_IQ4_NL: |
| 1157 | case GGML_TYPE_IQ4_XS: |
| 1158 | case GGML_TYPE_IQ3_S: |
| 1159 | case GGML_TYPE_IQ2_S: |
| 1160 | { |
| 1161 | ggml_compute_forward_add1_q_f32(params, dst); |
| 1162 | } break; |
| 1163 | default: |
| 1164 | { |
| 1165 | GGML_ABORT("fatal error"); |
| 1166 | } |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | // ggml_compute_forward_acc |
| 1171 | |
| 1172 | static void ggml_compute_forward_acc_f32( |
| 1173 | const ggml_compute_params * params, |
| 1174 | ggml_tensor * dst) { |
| 1175 | |
| 1176 | const ggml_tensor * src0 = dst->src[0]; |
| 1177 | const ggml_tensor * src1 = dst->src[1]; |
| 1178 | |
| 1179 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 1180 | GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0)); |
| 1181 | |
| 1182 | // view src0 and dst with these strides and data offset inbytes during acc |
| 1183 | // nb0 is implicitly element_size because src0 and dst are contiguous |
| 1184 | size_t nb1 = ((int32_t *) dst->op_params)[0]; |
| 1185 | size_t nb2 = ((int32_t *) dst->op_params)[1]; |
| 1186 | size_t nb3 = ((int32_t *) dst->op_params)[2]; |
| 1187 | size_t offset = ((int32_t *) dst->op_params)[3]; |
| 1188 | bool inplace = (bool) ((int32_t *) dst->op_params)[4]; |
| 1189 | |
| 1190 | if (!inplace) { |
| 1191 | if (params->ith == 0) { |
| 1192 | // memcpy needs to be synchronized across threads to avoid race conditions. |
| 1193 | // => do it in INIT phase |
| 1194 | memcpy( |
| 1195 | ((char *) dst->data), |
| 1196 | ((char *) src0->data), |
| 1197 | ggml_nbytes(dst)); |
| 1198 | } |
| 1199 | ggml_barrier(params->threadpool); |
| 1200 | } |
| 1201 | |
| 1202 | const int ith = params->ith; |
| 1203 | const int nth = params->nth; |
| 1204 | |
| 1205 | const int nr = ggml_nrows(src1); |
| 1206 | const int nc = src1->ne[0]; |
| 1207 | |
| 1208 | GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) |
| 1209 | GGML_TENSOR_LOCALS(size_t, nb1, src1, nb) |
| 1210 | |
| 1211 | // src0 and dst as viewed during acc |
| 1212 | const size_t nb0 = ggml_element_size(src0); |
| 1213 | |
| 1214 | const size_t nb00 = nb0; |
| 1215 | const size_t nb01 = nb1; |
| 1216 | const size_t nb02 = nb2; |
| 1217 | const size_t nb03 = nb3; |
| 1218 | |
| 1219 | GGML_ASSERT(offset + (ne10 == 0 ? 0 : ne10-1)*nb0 + (ne11 == 0 ? 0 : ne11-1)*nb1 + (ne12 == 0 ? 0 : ne12-1)*nb2 + (ne13 == 0 ? 0 : ne13-1)*nb3 < ggml_nbytes(dst)); |
| 1220 | GGML_ASSERT(offset + (ne10 == 0 ? 0 : ne10-1)*nb00 + (ne11 == 0 ? 0 : ne11-1)*nb01 + (ne12 == 0 ? 0 : ne12-1)*nb02 + (ne13 == 0 ? 0 : ne13-1)*nb03 < ggml_nbytes(src0)); |
| 1221 | |
| 1222 | GGML_ASSERT(nb10 == sizeof(float)); |
| 1223 | |
| 1224 | // rows per thread |
| 1225 | const int dr = (nr + nth - 1)/nth; |
| 1226 | |
| 1227 | // row range for this thread |
| 1228 | const int ir0 = dr*ith; |
| 1229 | const int ir1 = MIN(ir0 + dr, nr); |
| 1230 | |
| 1231 | for (int ir = ir0; ir < ir1; ++ir) { |
| 1232 | // src0 and dst are viewed with shape of src1 and offset |
| 1233 | // => same indices |
| 1234 | const int i3 = ir/(ne12*ne11); |
| 1235 | const int i2 = (ir - i3*ne12*ne11)/ne11; |
| 1236 | const int i1 = (ir - i3*ne12*ne11 - i2*ne11); |
| 1237 | |
| 1238 | #ifdef GGML_USE_ACCELERATE |
| 1239 | vDSP_vadd( |
| 1240 | (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + offset), 1, |
| 1241 | (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11), 1, |
| 1242 | (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset), 1, nc); |
| 1243 | #else |
| 1244 | ggml_vec_add_f32(nc, |
| 1245 | (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset), |
| 1246 | (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + offset), |
| 1247 | (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11)); |
| 1248 | #endif |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | void ggml_compute_forward_acc( |
| 1253 | const ggml_compute_params * params, |
| 1254 | ggml_tensor * dst) { |
| 1255 | |
| 1256 | const ggml_tensor * src0 = dst->src[0]; |
| 1257 | |
| 1258 | switch (src0->type) { |
| 1259 | case GGML_TYPE_F32: |
| 1260 | { |
| 1261 | ggml_compute_forward_acc_f32(params, dst); |
| 1262 | } break; |
| 1263 | case GGML_TYPE_F16: |
| 1264 | case GGML_TYPE_BF16: |
| 1265 | case GGML_TYPE_Q1_0: |
| 1266 | case GGML_TYPE_Q2_0: |
| 1267 | case GGML_TYPE_Q4_0: |
| 1268 | case GGML_TYPE_Q4_1: |
| 1269 | case GGML_TYPE_Q5_0: |
| 1270 | case GGML_TYPE_Q5_1: |
| 1271 | case GGML_TYPE_Q8_0: |
| 1272 | case GGML_TYPE_Q8_1: |
| 1273 | case GGML_TYPE_MXFP4: |
| 1274 | case GGML_TYPE_NVFP4: |
| 1275 | case GGML_TYPE_F8_E4M3: |
| 1276 | case GGML_TYPE_Q2_K: |
| 1277 | case GGML_TYPE_Q3_K: |
| 1278 | case GGML_TYPE_Q4_K: |
| 1279 | case GGML_TYPE_Q5_K: |
| 1280 | case GGML_TYPE_Q6_K: |
| 1281 | case GGML_TYPE_TQ1_0: |
| 1282 | case GGML_TYPE_TQ2_0: |
| 1283 | case GGML_TYPE_IQ2_XXS: |
| 1284 | case GGML_TYPE_IQ2_XS: |
| 1285 | case GGML_TYPE_IQ3_XXS: |
| 1286 | case GGML_TYPE_IQ1_S: |
| 1287 | case GGML_TYPE_IQ1_M: |
| 1288 | case GGML_TYPE_IQ4_NL: |
| 1289 | case GGML_TYPE_IQ4_XS: |
| 1290 | case GGML_TYPE_IQ3_S: |
| 1291 | case GGML_TYPE_IQ2_S: |
| 1292 | default: |
| 1293 | { |
| 1294 | GGML_ABORT("fatal error"); |
| 1295 | } |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | // ggml_compute_forward_sum |
| 1300 | |
| 1301 | static void ggml_compute_forward_sum_f32( |
| 1302 | const ggml_compute_params * params, |
| 1303 | ggml_tensor * dst) { |
| 1304 | |
| 1305 | const ggml_tensor * src0 = dst->src[0]; |
| 1306 | |
| 1307 | if (params->ith != 0) { |
| 1308 | return; |
| 1309 | } |
| 1310 | |
| 1311 | assert(ggml_is_scalar(dst)); |
| 1312 | |
| 1313 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 1314 | GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
| 1315 | |
| 1316 | ggml_float sum = 0; |
| 1317 | |
| 1318 | if (nb00 == sizeof(float)) { |
| 1319 | ggml_float row_sum = 0; |
| 1320 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 1321 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 1322 | for (int64_t i01 = 0; i01 < ne01; i01++) { |
| 1323 | ggml_vec_sum_f32_ggf(ne00, |
| 1324 | &row_sum, |
| 1325 | (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03)); |
| 1326 | sum += row_sum; |
| 1327 | } |
| 1328 | } |
| 1329 | } |
| 1330 | } else { |
| 1331 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 1332 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 1333 | for (int64_t i01 = 0; i01 < ne01; i01++) { |
| 1334 | const char * row = (const char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03; |
| 1335 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 1336 | sum += (ggml_float) *(const float *) (row + i00*nb00); |
| 1337 | } |
| 1338 | } |
| 1339 | } |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | ((float *) dst->data)[0] = sum; |
| 1344 | } |
| 1345 | |
| 1346 | static void ggml_compute_forward_sum_f16( |
| 1347 | const ggml_compute_params * params, |
| 1348 | ggml_tensor * dst) { |
| 1349 | |
| 1350 | const ggml_tensor * src0 = dst->src[0]; |
| 1351 | |
| 1352 | if (params->ith != 0) { |
| 1353 | return; |
| 1354 | } |
| 1355 | |
| 1356 | assert(ggml_is_scalar(dst)); |
| 1357 | |
| 1358 | assert(src0->nb[0] == sizeof(ggml_fp16_t)); |
| 1359 | |
| 1360 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 1361 | GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
| 1362 | |
| 1363 | float sum = 0; |
| 1364 | float row_sum = 0; |
| 1365 | |
| 1366 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 1367 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 1368 | for (int64_t i01 = 0; i01 < ne01; i01++) { |
| 1369 | ggml_vec_sum_f16_ggf(ne00, |
| 1370 | &row_sum, |
| 1371 | (ggml_fp16_t *) ((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03)); |
| 1372 | sum += row_sum; |
| 1373 | } |
| 1374 | } |
| 1375 | } |
| 1376 | ((ggml_fp16_t *) dst->data)[0] = GGML_CPU_FP32_TO_FP16(sum); |
| 1377 | } |
| 1378 | |
| 1379 | static void ggml_compute_forward_sum_bf16( |
| 1380 | const ggml_compute_params * params, |
| 1381 | ggml_tensor * dst) { |
| 1382 | |
| 1383 | const ggml_tensor * src0 = dst->src[0]; |
| 1384 | |
| 1385 | if (params->ith != 0) { |
| 1386 | return; |
| 1387 | } |
| 1388 | |
| 1389 | assert(ggml_is_scalar(dst)); |
| 1390 | |
| 1391 | assert(src0->nb[0] == sizeof(ggml_bf16_t)); |
| 1392 | |
| 1393 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 1394 | GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
| 1395 | |
| 1396 | float sum = 0; |
| 1397 | float row_sum = 0; |
| 1398 | |
| 1399 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 1400 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 1401 | for (int64_t i01 = 0; i01 < ne01; i01++) { |
| 1402 | ggml_vec_sum_bf16_ggf(ne00, |
| 1403 | &row_sum, |
| 1404 | (ggml_bf16_t *) ((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03)); |
| 1405 | sum += row_sum; |
| 1406 | } |
| 1407 | } |
| 1408 | } |
| 1409 | ((ggml_bf16_t *) dst->data)[0] = GGML_FP32_TO_BF16(sum); |
| 1410 | } |
| 1411 | |
| 1412 | void ggml_compute_forward_sum( |
| 1413 | const ggml_compute_params * params, |
| 1414 | ggml_tensor * dst) { |
| 1415 | |
| 1416 | const ggml_tensor * src0 = dst->src[0]; |
| 1417 | |
| 1418 | switch (src0->type) { |
| 1419 | case GGML_TYPE_F32: |
| 1420 | { |
| 1421 | ggml_compute_forward_sum_f32(params, dst); |
| 1422 | } break; |
| 1423 | case GGML_TYPE_F16: |
| 1424 | { |
| 1425 | ggml_compute_forward_sum_f16(params, dst); |
| 1426 | } break; |
| 1427 | case GGML_TYPE_BF16: |
| 1428 | { |
| 1429 | ggml_compute_forward_sum_bf16(params, dst); |
| 1430 | } break; |
| 1431 | default: |
| 1432 | { |
| 1433 | GGML_ABORT("fatal error"); |
| 1434 | } |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | // ggml_compute_forward_cumsum |
| 1439 | |
| 1440 | static void ggml_compute_forward_cumsum_f32( |
| 1441 | const ggml_compute_params * params, |
| 1442 | ggml_tensor * dst) { |
| 1443 | |
| 1444 | const ggml_tensor * src0 = dst->src[0]; |
| 1445 | |
| 1446 | GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 1447 | GGML_ASSERT(dst->nb[0] == sizeof(float)); |
| 1448 | |
| 1449 | GGML_TENSOR_UNARY_OP_LOCALS |
| 1450 | |
| 1451 | GGML_ASSERT(ne0 == ne00); |
| 1452 | GGML_ASSERT(ne1 == ne01); |
| 1453 | GGML_ASSERT(ne2 == ne02); |
| 1454 | GGML_ASSERT(ne3 == ne03); |
| 1455 | |
| 1456 | const auto [ir0, ir1] = get_thread_range(params, src0); |
| 1457 | |
| 1458 | for (int64_t ir = ir0; ir < ir1; ++ir) { |
| 1459 | const int64_t i03 = ir/(ne02*ne01); |
| 1460 | const int64_t i02 = (ir - i03*ne02*ne01)/ne01; |
| 1461 | const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01); |
| 1462 | |
| 1463 | float * src_row = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03); |
| 1464 | float * dst_row = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3); |
| 1465 | |
| 1466 | ggml_vec_cumsum_f32(ne00, dst_row, src_row); |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | void ggml_compute_forward_cumsum( |
| 1471 | const ggml_compute_params * params, |
| 1472 | ggml_tensor * dst) { |
| 1473 | |
| 1474 | const ggml_tensor * src0 = dst->src[0]; |
| 1475 | |
| 1476 | switch (src0->type) { |
| 1477 | case GGML_TYPE_F32: |
| 1478 | { |
| 1479 | ggml_compute_forward_cumsum_f32(params, dst); |
| 1480 | } break; |
| 1481 | default: |
| 1482 | { |
| 1483 | GGML_ABORT("fatal error"); |
| 1484 | } |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | // ggml_compute_forward_sum_rows |
| 1489 | |
| 1490 | static void ggml_compute_forward_sum_rows_f32( |
| 1491 | const ggml_compute_params * params, |
| 1492 | ggml_tensor * dst) { |
| 1493 | |
| 1494 | const ggml_tensor * src0 = dst->src[0]; |
| 1495 | |
| 1496 | if (params->ith != 0) { |
| 1497 | return; |
| 1498 | } |
| 1499 | |
| 1500 | GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 1501 | GGML_ASSERT(dst->nb[0] == sizeof(float)); |
| 1502 | |
| 1503 | GGML_TENSOR_UNARY_OP_LOCALS |
| 1504 | |
| 1505 | GGML_ASSERT(ne0 == 1); |
| 1506 | GGML_ASSERT(ne1 == ne01); |
| 1507 | GGML_ASSERT(ne2 == ne02); |
| 1508 | GGML_ASSERT(ne3 == ne03); |
| 1509 | |
| 1510 | for (int64_t i3 = 0; i3 < ne03; i3++) { |
| 1511 | for (int64_t i2 = 0; i2 < ne02; i2++) { |
| 1512 | for (int64_t i1 = 0; i1 < ne01; i1++) { |
| 1513 | float * src_row = (float *) ((char *) src0->data + i1*nb01 + i2*nb02 + i3*nb03); |
| 1514 | float * dst_row = (float *) ((char *) dst->data + i1*nb1 + i2*nb2 + i3*nb3); |
| 1515 | float row_sum = 0; |
| 1516 | ggml_vec_sum_f32(ne00, &row_sum, src_row); |
| 1517 | dst_row[0] = row_sum; |
| 1518 | } |
| 1519 | } |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | void ggml_compute_forward_sum_rows( |
| 1524 | const ggml_compute_params * params, |
| 1525 | ggml_tensor * dst) { |
| 1526 | |
| 1527 | const ggml_tensor * src0 = dst->src[0]; |
| 1528 | |
| 1529 | switch (src0->type) { |
| 1530 | case GGML_TYPE_F32: |
| 1531 | { |
| 1532 | ggml_compute_forward_sum_rows_f32(params, dst); |
| 1533 | } break; |
| 1534 | default: |
| 1535 | { |
| 1536 | GGML_ABORT("fatal error"); |
| 1537 | } |
| 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | // ggml_compute_forward_mean |
| 1542 | |
| 1543 | static void ggml_compute_forward_mean_f32( |
| 1544 | const ggml_compute_params * params, |
| 1545 | ggml_tensor * dst) { |
| 1546 | |
| 1547 | const ggml_tensor * src0 = dst->src[0]; |
| 1548 | |
| 1549 | if (params->ith != 0) { |
| 1550 | return; |
| 1551 | } |
| 1552 | |
| 1553 | assert(src0->nb[0] == sizeof(float)); |
| 1554 | |
| 1555 | GGML_TENSOR_UNARY_OP_LOCALS |
| 1556 | |
| 1557 | assert(ne0 == 1); |
| 1558 | assert(ne1 == ne01); |
| 1559 | assert(ne2 == ne02); |
| 1560 | assert(ne3 == ne03); |
| 1561 | |
| 1562 | GGML_UNUSED(ne0); |
| 1563 | GGML_UNUSED(ne1); |
| 1564 | GGML_UNUSED(ne2); |
| 1565 | GGML_UNUSED(ne3); |
| 1566 | |
| 1567 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 1568 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 1569 | for (int64_t i01 = 0; i01 < ne01; i01++) { |
| 1570 | ggml_vec_sum_f32(ne00, |
| 1571 | (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3), |
| 1572 | (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03)); |
| 1573 | |
| 1574 | *(float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3) /= (float) ne00; |
| 1575 | } |
| 1576 | } |
| 1577 | } |
| 1578 | } |
| 1579 | |
| 1580 | void ggml_compute_forward_mean( |
| 1581 | const ggml_compute_params * params, |
| 1582 | ggml_tensor * dst) { |
| 1583 | |
| 1584 | const ggml_tensor * src0 = dst->src[0]; |
| 1585 | |
| 1586 | switch (src0->type) { |
| 1587 | case GGML_TYPE_F32: |
| 1588 | { |
| 1589 | ggml_compute_forward_mean_f32(params, dst); |
| 1590 | } break; |
| 1591 | default: |
| 1592 | { |
| 1593 | GGML_ABORT("fatal error"); |
| 1594 | } |
| 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | // ggml_compute_forward_argmax |
| 1599 | |
| 1600 | static void ggml_compute_forward_argmax_f32( |
| 1601 | const ggml_compute_params * params, |
| 1602 | ggml_tensor * dst) { |
| 1603 | |
| 1604 | const ggml_tensor * src0 = dst->src[0]; |
| 1605 | |
| 1606 | if (params->ith != 0) { |
| 1607 | return; |
| 1608 | } |
| 1609 | |
| 1610 | assert(src0->nb[0] == sizeof(float)); |
| 1611 | assert(dst->nb[0] == sizeof(float)); |
| 1612 | |
| 1613 | const int64_t ne00 = src0->ne[0]; |
| 1614 | const int64_t ne01 = src0->ne[1]; |
| 1615 | |
| 1616 | const size_t nb01 = src0->nb[1]; |
| 1617 | const size_t nb0 = dst->nb[0]; |
| 1618 | |
| 1619 | for (int64_t i1 = 0; i1 < ne01; i1++) { |
| 1620 | float * src = (float *) ((char *) src0->data + i1*nb01); |
| 1621 | int32_t * dst_ = (int32_t *) ((char *) dst->data + i1*nb0); |
| 1622 | int v = 0; |
| 1623 | ggml_vec_argmax_f32(ne00, &v, src); |
| 1624 | dst_[0] = v; |
| 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | void ggml_compute_forward_argmax( |
| 1629 | const ggml_compute_params * params, |
| 1630 | ggml_tensor * dst) { |
| 1631 | |
| 1632 | const ggml_tensor * src0 = dst->src[0]; |
| 1633 | |
| 1634 | switch (src0->type) { |
| 1635 | case GGML_TYPE_F32: |
| 1636 | { |
| 1637 | ggml_compute_forward_argmax_f32(params, dst); |
| 1638 | } break; |
| 1639 | default: |
| 1640 | { |
| 1641 | GGML_ABORT("fatal error"); |
| 1642 | } |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | // ggml_compute_forward_count_equal |
| 1647 | |
| 1648 | static void ggml_compute_forward_count_equal_i32( |
| 1649 | const ggml_compute_params * params, |
| 1650 | ggml_tensor * dst) { |
| 1651 | |
| 1652 | const ggml_tensor * src0 = dst->src[0]; |
| 1653 | const ggml_tensor * src1 = dst->src[1]; |
| 1654 | |
| 1655 | GGML_TENSOR_BINARY_OP_LOCALS; |
| 1656 | |
| 1657 | GGML_ASSERT(src0->type == GGML_TYPE_I32); |
| 1658 | GGML_ASSERT(src1->type == GGML_TYPE_I32); |
| 1659 | GGML_ASSERT(ggml_are_same_shape(src0, src1)); |
| 1660 | GGML_ASSERT(ggml_is_scalar(dst)); |
| 1661 | GGML_ASSERT(dst->type == GGML_TYPE_I64); |
| 1662 | |
| 1663 | const int64_t nr = ggml_nrows(src0); |
| 1664 | |
| 1665 | const int ith = params->ith; |
| 1666 | const int nth = params->nth; |
| 1667 | |
| 1668 | int64_t * sums = (int64_t *) params->wdata; |
| 1669 | int64_t sum_thread = 0; |
| 1670 | |
| 1671 | // rows per thread |
| 1672 | const int64_t dr = (nr + nth - 1)/nth; |
| 1673 | |
| 1674 | // row range for this thread |
| 1675 | const int64_t ir0 = dr*ith; |
| 1676 | const int64_t ir1 = MIN(ir0 + dr, nr); |
| 1677 | |
| 1678 | for (int64_t ir = ir0; ir < ir1; ++ir) { |
| 1679 | const int64_t i03 = ir / (ne02*ne01); |
| 1680 | const int64_t i02 = (ir - i03*ne03) / ne01; |
| 1681 | const int64_t i01 = ir - i03*ne03 - i02*ne02; |
| 1682 | |
| 1683 | const char * data0 = (const char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01; |
| 1684 | const char * data1 = (const char *) src1->data + i03*nb13 + i02*nb12 + i01*nb11; |
| 1685 | |
| 1686 | for (int64_t i00 = 0; i00 < ne00; ++i00) { |
| 1687 | const int32_t val0 = *((const int32_t *) (data0 + i00*nb00)); |
| 1688 | const int32_t val1 = *((const int32_t *) (data1 + i00*nb10)); |
| 1689 | |
| 1690 | sum_thread += val0 == val1; |
| 1691 | } |
| 1692 | } |
| 1693 | if (ith != 0) { |
| 1694 | sums[ith] = sum_thread; |
| 1695 | } |
| 1696 | ggml_barrier(params->threadpool); |
| 1697 | |
| 1698 | if (ith != 0) { |
| 1699 | return; |
| 1700 | } |
| 1701 | |
| 1702 | for (int ith_other = 1; ith_other < nth; ++ith_other) { |
| 1703 | sum_thread += sums[ith_other]; |
| 1704 | } |
| 1705 | *((int64_t *) dst->data) = sum_thread; |
| 1706 | } |
| 1707 | |
| 1708 | void ggml_compute_forward_count_equal( |
| 1709 | const ggml_compute_params * params, |
| 1710 | ggml_tensor * dst) { |
| 1711 | |
| 1712 | const ggml_tensor * src0 = dst->src[0]; |
| 1713 | |
| 1714 | switch (src0->type) { |
| 1715 | case GGML_TYPE_I32: |
| 1716 | { |
| 1717 | ggml_compute_forward_count_equal_i32(params, dst); |
| 1718 | } break; |
| 1719 | default: |
| 1720 | { |
| 1721 | GGML_ABORT("fatal error"); |
| 1722 | } |
| 1723 | } |
| 1724 | } |
| 1725 | |
| 1726 | // ggml_compute_forward_repeat |
| 1727 | |
| 1728 | static void ggml_compute_forward_repeat_f32( |
| 1729 | const ggml_compute_params * params, |
| 1730 | ggml_tensor * dst) { |
| 1731 | |
| 1732 | const ggml_tensor * src0 = dst->src[0]; |
| 1733 | |
| 1734 | if (params->ith != 0) { |
| 1735 | return; |
| 1736 | } |
| 1737 | |
| 1738 | GGML_ASSERT(ggml_can_repeat(src0, dst)); |
| 1739 | |
| 1740 | GGML_TENSOR_UNARY_OP_LOCALS |
| 1741 | |
| 1742 | // guaranteed to be an integer due to the check in ggml_can_repeat |
| 1743 | const int nr0 = (int)(ne0/ne00); |
| 1744 | const int nr1 = (int)(ne1/ne01); |
| 1745 | const int nr2 = (int)(ne2/ne02); |
| 1746 | const int nr3 = (int)(ne3/ne03); |
| 1747 | |
| 1748 | // TODO: support for transposed / permuted tensors |
| 1749 | GGML_ASSERT(nb0 == sizeof(float)); |
| 1750 | GGML_ASSERT(nb00 == sizeof(float)); |
| 1751 | |
| 1752 | // TODO: maybe this is not optimal? |
| 1753 | for (int i3 = 0; i3 < nr3; i3++) { |
| 1754 | for (int k3 = 0; k3 < ne03; k3++) { |
| 1755 | for (int i2 = 0; i2 < nr2; i2++) { |
| 1756 | for (int k2 = 0; k2 < ne02; k2++) { |
| 1757 | for (int i1 = 0; i1 < nr1; i1++) { |
| 1758 | for (int k1 = 0; k1 < ne01; k1++) { |
| 1759 | for (int i0 = 0; i0 < nr0; i0++) { |
| 1760 | ggml_vec_cpy_f32(ne00, |
| 1761 | (float *) ((char *) dst->data + (i3*ne03 + k3)*nb3 + (i2*ne02 + k2)*nb2 + (i1*ne01 + k1)*nb1 + (i0*ne00)*nb0), |
| 1762 | (float *) ((char *) src0->data + ( k3)*nb03 + ( k2)*nb02 + ( k1)*nb01)); |
| 1763 | } |
| 1764 | } |
| 1765 | } |
| 1766 | } |
| 1767 | } |
| 1768 | } |
| 1769 | } |
| 1770 | } |
| 1771 | |
| 1772 | static void ggml_compute_forward_repeat_f16( |
| 1773 | const ggml_compute_params * params, |
| 1774 | ggml_tensor * dst) { |
| 1775 | |
| 1776 | const ggml_tensor * src0 = dst->src[0]; |
| 1777 | |
| 1778 | if (params->ith != 0) { |
| 1779 | return; |
| 1780 | } |
| 1781 | |
| 1782 | GGML_ASSERT(ggml_can_repeat(src0, dst)); |
| 1783 | |
| 1784 | GGML_TENSOR_UNARY_OP_LOCALS |
| 1785 | |
| 1786 | // guaranteed to be an integer due to the check in ggml_can_repeat |
| 1787 | const int nr0 = (int)(ne0/ne00); |
| 1788 | const int nr1 = (int)(ne1/ne01); |
| 1789 | const int nr2 = (int)(ne2/ne02); |
| 1790 | const int nr3 = (int)(ne3/ne03); |
| 1791 | |
| 1792 | // TODO: support for transposed / permuted tensors |
| 1793 | GGML_ASSERT(nb0 == sizeof(ggml_fp16_t)); |
| 1794 | GGML_ASSERT(nb00 == sizeof(ggml_fp16_t)); |
| 1795 | |
| 1796 | // TODO: maybe this is not optimal? |
| 1797 | for (int i3 = 0; i3 < nr3; i3++) { |
| 1798 | for (int k3 = 0; k3 < ne03; k3++) { |
| 1799 | for (int i2 = 0; i2 < nr2; i2++) { |
| 1800 | for (int k2 = 0; k2 < ne02; k2++) { |
| 1801 | for (int i1 = 0; i1 < nr1; i1++) { |
| 1802 | for (int k1 = 0; k1 < ne01; k1++) { |
| 1803 | for (int i0 = 0; i0 < nr0; i0++) { |
| 1804 | ggml_fp16_t * y = (ggml_fp16_t *) ((char *) dst->data + (i3*ne03 + k3)*nb3 + (i2*ne02 + k2)*nb2 + (i1*ne01 + k1)*nb1 + (i0*ne00)*nb0); |
| 1805 | ggml_fp16_t * x = (ggml_fp16_t *) ((char *) src0->data + ( k3)*nb03 + ( k2)*nb02 + ( k1)*nb01); |
| 1806 | // ggml_vec_cpy_f16(ne00, y, x) |
| 1807 | for (int i = 0; i < ne00; ++i) { |
| 1808 | y[i] = x[i]; |
| 1809 | } |
| 1810 | } |
| 1811 | } |
| 1812 | } |
| 1813 | } |
| 1814 | } |
| 1815 | } |
| 1816 | } |
| 1817 | } |
| 1818 | |
| 1819 | void ggml_compute_forward_repeat( |
| 1820 | const ggml_compute_params * params, |
| 1821 | ggml_tensor * dst) { |
| 1822 | |
| 1823 | const ggml_tensor * src0 = dst->src[0]; |
| 1824 | |
| 1825 | switch (src0->type) { |
| 1826 | case GGML_TYPE_F16: |
| 1827 | case GGML_TYPE_BF16: |
| 1828 | case GGML_TYPE_I16: |
| 1829 | { |
| 1830 | ggml_compute_forward_repeat_f16(params, dst); |
| 1831 | } break; |
| 1832 | case GGML_TYPE_F32: |
| 1833 | case GGML_TYPE_I32: |
| 1834 | { |
| 1835 | ggml_compute_forward_repeat_f32(params, dst); |
| 1836 | } break; |
| 1837 | // TODO: templateify the implementation and support for I64 |
| 1838 | // ref https://github.com/ggml-org/llama.cpp/pull/14274#discussion_r2169492225 |
| 1839 | //case GGML_TYPE_I64: |
| 1840 | // { |
| 1841 | // ggml_compute_forward_repeat_i64(params, dst); |
| 1842 | // } break; |
| 1843 | default: |
| 1844 | { |
| 1845 | GGML_ABORT("fatal error"); |
| 1846 | } |
| 1847 | } |
| 1848 | } |
| 1849 | |
| 1850 | // ggml_compute_forward_repeat_back |
| 1851 | |
| 1852 | static void ggml_compute_forward_repeat_back_f32( |
| 1853 | const ggml_compute_params * params, |
| 1854 | ggml_tensor * dst) { |
| 1855 | |
| 1856 | const ggml_tensor * src0 = dst->src[0]; |
| 1857 | |
| 1858 | if (params->ith != 0) { |
| 1859 | return; |
| 1860 | } |
| 1861 | |
| 1862 | GGML_ASSERT(ggml_can_repeat(dst, src0)); |
| 1863 | |
| 1864 | GGML_TENSOR_UNARY_OP_LOCALS |
| 1865 | |
| 1866 | // guaranteed to be an integer due to the check in ggml_can_repeat |
| 1867 | const int nr0 = (int)(ne00/ne0); |
| 1868 | const int nr1 = (int)(ne01/ne1); |
| 1869 | const int nr2 = (int)(ne02/ne2); |
| 1870 | const int nr3 = (int)(ne03/ne3); |
| 1871 | |
| 1872 | // TODO: support for transposed / permuted tensors |
| 1873 | GGML_ASSERT(nb0 == sizeof(float)); |
| 1874 | GGML_ASSERT(nb00 == sizeof(float)); |
| 1875 | |
| 1876 | if (ggml_is_contiguous(dst)) { |
| 1877 | ggml_vec_set_f32(ne0*ne1*ne2*ne3, (float *)dst->data, 0); |
| 1878 | } else { |
| 1879 | for (int k3 = 0; k3 < ne3; k3++) { |
| 1880 | for (int k2 = 0; k2 < ne2; k2++) { |
| 1881 | for (int k1 = 0; k1 < ne1; k1++) { |
| 1882 | ggml_vec_set_f32(ne0, |
| 1883 | (float *) ((char *) dst->data + k1*nb1 + k2*nb2 + k3*nb3), |
| 1884 | 0); |
| 1885 | } |
| 1886 | } |
| 1887 | } |
| 1888 | } |
| 1889 | |
| 1890 | // TODO: maybe this is not optimal? |
| 1891 | for (int i3 = 0; i3 < nr3; i3++) { |
| 1892 | for (int k3 = 0; k3 < ne3; k3++) { |
| 1893 | for (int i2 = 0; i2 < nr2; i2++) { |
| 1894 | for (int k2 = 0; k2 < ne2; k2++) { |
| 1895 | for (int i1 = 0; i1 < nr1; i1++) { |
| 1896 | for (int k1 = 0; k1 < ne1; k1++) { |
| 1897 | for (int i0 = 0; i0 < nr0; i0++) { |
| 1898 | ggml_vec_acc_f32(ne0, |
| 1899 | (float *) ((char *) dst->data + ( k3)*nb3 + ( k2)*nb2 + ( k1)*nb1), |
| 1900 | (float *) ((char *) src0->data + (i3*ne3 + k3)*nb03 + (i2*ne2 + k2)*nb02 + (i1*ne1 + k1)*nb01 + (i0*ne0)*nb00)); |
| 1901 | } |
| 1902 | } |
| 1903 | } |
| 1904 | } |
| 1905 | } |
| 1906 | } |
| 1907 | } |
| 1908 | } |
| 1909 | |
| 1910 | void ggml_compute_forward_repeat_back( |
| 1911 | const ggml_compute_params * params, |
| 1912 | ggml_tensor * dst) { |
| 1913 | |
| 1914 | const ggml_tensor * src0 = dst->src[0]; |
| 1915 | |
| 1916 | switch (src0->type) { |
| 1917 | case GGML_TYPE_F32: |
| 1918 | { |
| 1919 | ggml_compute_forward_repeat_back_f32(params, dst); |
| 1920 | } break; |
| 1921 | default: |
| 1922 | { |
| 1923 | GGML_ABORT("fatal error"); |
| 1924 | } |
| 1925 | } |
| 1926 | } |
| 1927 | |
| 1928 | // ggml_compute_forward_concat |
| 1929 | |
| 1930 | // rows of dst copied together, so that a strided source is read along neighboring rows |
| 1931 | static constexpr int64_t GGML_CONCAT_TILE = 32; |
| 1932 | |
| 1933 | // copy n_rows rows of n elements of es bytes from a source with element stride nb0, element by element |
| 1934 | template <size_t es> |
| 1935 | static void ggml_concat_copy_strided(char * y, size_t nby, const char * x, size_t nbx, size_t nb0, int64_t n, int64_t n_rows) { |
| 1936 | for (int64_t i0 = 0; i0 < n; i0++) { |
| 1937 | for (int64_t r = 0; r < n_rows; r++) { |
| 1938 | memcpy(y + r*nby + i0*es, x + r*nbx + i0*nb0, es); |
| 1939 | } |
| 1940 | } |
| 1941 | } |
| 1942 | |
| 1943 | // copy n_rows rows of n elements of src into dst, rows are nbx bytes apart in src and nby bytes apart in dst |
| 1944 | static void ggml_concat_copy_rows(char * y, size_t nby, const ggml_tensor * src, const char * x, size_t nbx, int64_t n, int64_t n_rows) { |
| 1945 | const size_t es = ggml_type_size(src->type); |
| 1946 | |
| 1947 | if (src->nb[0] == es) { |
| 1948 | const size_t row_size = ggml_row_size(src->type, n); |
| 1949 | for (int64_t r = 0; r < n_rows; r++) { |
| 1950 | memcpy(y + r*nby, x + r*nbx, row_size); |
| 1951 | } |
| 1952 | return; |
| 1953 | } |
| 1954 | |
| 1955 | switch (es) { |
| 1956 | case 1: ggml_concat_copy_strided<1>(y, nby, x, nbx, src->nb[0], n, n_rows); break; |
| 1957 | case 2: ggml_concat_copy_strided<2>(y, nby, x, nbx, src->nb[0], n, n_rows); break; |
| 1958 | case 4: ggml_concat_copy_strided<4>(y, nby, x, nbx, src->nb[0], n, n_rows); break; |
| 1959 | case 8: ggml_concat_copy_strided<8>(y, nby, x, nbx, src->nb[0], n, n_rows); break; |
| 1960 | default: GGML_ABORT("unsupported element size"); |
| 1961 | } |
| 1962 | } |
| 1963 | |
| 1964 | // each dst row comes from src0 or src1, or for dim 0 from the row of src0 followed by the row of src1 |
| 1965 | void ggml_compute_forward_concat( |
| 1966 | const ggml_compute_params * params, |
| 1967 | ggml_tensor * dst) { |
| 1968 | |
| 1969 | const ggml_tensor * src0 = dst->src[0]; |
| 1970 | const ggml_tensor * src1 = dst->src[1]; |
| 1971 | |
| 1972 | GGML_TENSOR_BINARY_OP_LOCALS |
| 1973 | |
| 1974 | const int32_t dim = ggml_get_op_params_i32(dst, 0); |
| 1975 | |
| 1976 | GGML_ASSERT(dim >= 0 && dim < 4); |
| 1977 | GGML_ASSERT(nb0 == ggml_type_size(dst->type)); |
| 1978 | GGML_ASSERT(!ggml_is_quantized(dst->type) || (nb00 == nb0 && nb10 == nb0)); |
| 1979 | GGML_ASSERT(dim != 0 || (ne00 % ggml_blck_size(dst->type) == 0 && ne10 % ggml_blck_size(dst->type) == 0)); |
| 1980 | |
| 1981 | const size_t y1 = dim == 0 ? ggml_row_size(dst->type, ne00) : 0; |
| 1982 | |
| 1983 | const int64_t nr = ne1*ne2*ne3; |
| 1984 | const int64_t dr = (nr + params->nth - 1)/params->nth; |
| 1985 | |
| 1986 | const int64_t ir0 = dr*params->ith; |
| 1987 | const int64_t ir1 = MIN(ir0 + dr, nr); |
| 1988 | |
| 1989 | for (int64_t ir = ir0; ir < ir1; ) { |
| 1990 | int64_t i[4] = { 0, ir % ne1, (ir/ne1) % ne2, ir/(ne1*ne2) }; |
| 1991 | |
| 1992 | // a tile of consecutive rows sharing i2, i3 and their source |
| 1993 | int64_t nt = MIN(MIN(ir1 - ir, ne1 - i[1]), GGML_CONCAT_TILE); |
| 1994 | if (dim == 1 && i[1] < ne01) { |
| 1995 | nt = MIN(nt, ne01 - i[1]); |
| 1996 | } |
| 1997 | |
| 1998 | char * y = (char *) dst->data + i[1]*nb1 + i[2]*nb2 + i[3]*nb3; |
| 1999 | |
| 2000 | if (dim == 0 || i[dim] < src0->ne[dim]) { |
| 2001 | ggml_concat_copy_rows(y, nb1, src0, (const char *) src0->data + i[1]*nb01 + i[2]*nb02 + i[3]*nb03, nb01, ne00, nt); |
| 2002 | } |
| 2003 | |
| 2004 | if (dim == 0 || i[dim] >= src0->ne[dim]) { |
| 2005 | i[dim] -= src0->ne[dim]; // i[0] does not address rows |
| 2006 | ggml_concat_copy_rows(y + y1, nb1, src1, (const char *) src1->data + i[1]*nb11 + i[2]*nb12 + i[3]*nb13, nb11, ne10, nt); |
| 2007 | } |
| 2008 | |
| 2009 | ir += nt; |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | // row loop shared by the BF16 unary ops |
| 2014 | template <void (*vec_op)(const int, ggml_bf16_t *, const ggml_bf16_t *)> |
| 2015 | static void ggml_compute_forward_unary_bf16_rows( |
| 2016 | const ggml_compute_params * params, |
| 2017 | ggml_tensor * dst) { |
| 2018 | |
| 2019 | const ggml_tensor * src0 = dst->src[0]; |
| 2020 | |
| 2021 | assert(ggml_is_contiguous_rows(src0)); |
| 2022 | assert(ggml_are_same_shape(src0, dst)); |
| 2023 | |
| 2024 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 2025 | GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
| 2026 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 2027 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 2028 | |
| 2029 | const int ith = params->ith; |
| 2030 | const int nth = params->nth; |
| 2031 | |
| 2032 | const int nc = src0->ne[0]; |
| 2033 | const int nr = ggml_nrows(src0); |
| 2034 | |
| 2035 | // rows per thread |
| 2036 | const int dr = (nr + nth - 1)/nth; |
| 2037 | |
| 2038 | // row range for this thread |
| 2039 | const int ir0 = dr*ith; |
| 2040 | const int ir1 = MIN(ir0 + dr, nr); |
| 2041 | |
| 2042 | for (int ir = ir0; ir < ir1; ++ir) { |
| 2043 | const int i3 = ir/(ne02*ne01); |
| 2044 | const int i2 = (ir - i3*ne02*ne01)/ne01; |
| 2045 | const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
| 2046 | |
| 2047 | vec_op(nc, |
| 2048 | (ggml_bf16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
| 2049 | (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
| 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | // ggml_compute_forward_gelu |
| 2054 | |
| 2055 | static void ggml_compute_forward_gelu_f32( |
| 2056 | const ggml_compute_params * params, |
| 2057 | ggml_tensor * dst) { |
| 2058 | |
| 2059 | const ggml_tensor * src0 = dst->src[0]; |
| 2060 | |
| 2061 | assert(ggml_is_contiguous_rows(src0)); |
| 2062 | assert(ggml_are_same_shape(src0, dst)); |
| 2063 | |
| 2064 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 2065 | GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
| 2066 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 2067 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 2068 | |
| 2069 | const int ith = params->ith; |
| 2070 | const int nth = params->nth; |
| 2071 | |
| 2072 | const int nc = src0->ne[0]; |
| 2073 | const int nr = ggml_nrows(src0); |
| 2074 | |
| 2075 | // rows per thread |
| 2076 | const int dr = (nr + nth - 1)/nth; |
| 2077 | |
| 2078 | // row range for this thread |
| 2079 | const int ir0 = dr*ith; |
| 2080 | const int ir1 = MIN(ir0 + dr, nr); |
| 2081 | |
| 2082 | for (int ir = ir0; ir < ir1; ++ir) { |
| 2083 | const int i3 = ir/(ne02*ne01); |
| 2084 | const int i2 = (ir - i3*ne02*ne01)/ne01; |
| 2085 | const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
| 2086 | |
| 2087 | ggml_vec_gelu_f32(nc, |
| 2088 | (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
| 2089 | (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
| 2090 | |
| 2091 | #ifndef NDEBUG |
| 2092 | for (int k = 0; k < nc; k++) { |
| 2093 | const float x = ((float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*(dst->nb[1])))[k]; |
| 2094 | GGML_UNUSED(x); |
| 2095 | assert(!isnan(x)); |
| 2096 | assert(!isinf(x)); |
| 2097 | } |
| 2098 | #endif // NDEBUG |
| 2099 | } |
| 2100 | } |
| 2101 | |
| 2102 | static void ggml_compute_forward_gelu_f16( |
| 2103 | const ggml_compute_params * params, |
| 2104 | ggml_tensor * dst) { |
| 2105 | |
| 2106 | const ggml_tensor * src0 = dst->src[0]; |
| 2107 | |
| 2108 | assert(ggml_is_contiguous_rows(src0)); |
| 2109 | assert(ggml_are_same_shape(src0, dst)); |
| 2110 | |
| 2111 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 2112 | GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
| 2113 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 2114 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 2115 | |
| 2116 | const int ith = params->ith; |
| 2117 | const int nth = params->nth; |
| 2118 | |
| 2119 | const int nc = src0->ne[0]; |
| 2120 | const int nr = ggml_nrows(src0); |
| 2121 | |
| 2122 | // rows per thread |
| 2123 | const int dr = (nr + nth - 1)/nth; |
| 2124 | |
| 2125 | // row range for this thread |
| 2126 | const int ir0 = dr*ith; |
| 2127 | const int ir1 = MIN(ir0 + dr, nr); |
| 2128 | |
| 2129 | for (int ir = ir0; ir < ir1; ++ir) { |
| 2130 | const int i3 = ir/(ne02*ne01); |
| 2131 | const int i2 = (ir - i3*ne02*ne01)/ne01; |
| 2132 | const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
| 2133 | |
| 2134 | ggml_vec_gelu_f16(nc, |
| 2135 | (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
| 2136 | (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
| 2137 | |
| 2138 | #ifndef NDEBUG |
| 2139 | for (int k = 0; k < nc; k++) { |
| 2140 | const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*( dst->nb[1])))[k]; |
| 2141 | const float v = GGML_CPU_FP16_TO_FP32(x); |
| 2142 | GGML_UNUSED(v); |
| 2143 | assert(!isnan(v)); |
| 2144 | assert(!isinf(v)); |
| 2145 | } |
| 2146 | #endif // NDEBUG |
| 2147 | } |
| 2148 | } |
| 2149 | |
| 2150 | static void ggml_compute_forward_gelu_bf16( |
| 2151 | const ggml_compute_params * params, |
| 2152 | ggml_tensor * dst) { |
| 2153 | ggml_compute_forward_unary_bf16_rows<ggml_vec_gelu_bf16>(params, dst); |
| 2154 | } |
| 2155 | |
| 2156 | static void ggml_compute_forward_gelu( |
| 2157 | const ggml_compute_params * params, |
| 2158 | ggml_tensor * dst) { |
| 2159 | |
| 2160 | const ggml_tensor * src0 = dst->src[0]; |
| 2161 | |
| 2162 | switch (src0->type) { |
| 2163 | case GGML_TYPE_F32: |
| 2164 | { |
| 2165 | ggml_compute_forward_gelu_f32(params, dst); |
| 2166 | } break; |
| 2167 | case GGML_TYPE_F16: |
| 2168 | { |
| 2169 | ggml_compute_forward_gelu_f16(params, dst); |
| 2170 | } break; |
| 2171 | case GGML_TYPE_BF16: |
| 2172 | { |
| 2173 | ggml_compute_forward_gelu_bf16(params, dst); |
| 2174 | } break; |
| 2175 | default: |
| 2176 | { |
| 2177 | GGML_ABORT("fatal error"); |
| 2178 | } |
| 2179 | } |
| 2180 | } |
| 2181 | |
| 2182 | // ggml_compute_fill |
| 2183 | |
| 2184 | static void ggml_compute_forward_fill_f32(const ggml_compute_params * params, ggml_tensor * dst) { |
| 2185 | const float c = ggml_get_op_params_f32(dst, 0); |
| 2186 | |
| 2187 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne); |
| 2188 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb); |
| 2189 | |
| 2190 | const auto [ir0, ir1] = get_thread_range(params, dst); |
| 2191 | |
| 2192 | for (int64_t ir = ir0; ir < ir1; ++ir) { |
| 2193 | const int64_t i03 = ir/(ne2*ne1); |
| 2194 | const int64_t i02 = (ir - i03*ne2*ne1)/ne1; |
| 2195 | const int64_t i01 = (ir - i03*ne2*ne1 - i02*ne1); |
| 2196 | |
| 2197 | float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1); |
| 2198 | |
| 2199 | ggml_vec_set_f32(ne0, dst_ptr, c); |
| 2200 | } |
| 2201 | } |
| 2202 | |
| 2203 | static void ggml_compute_forward_fill_f16(const ggml_compute_params * params, ggml_tensor * dst) { |
| 2204 | const ggml_fp16_t c = GGML_CPU_FP32_TO_FP16(ggml_get_op_params_f32(dst, 0)); |
| 2205 | |
| 2206 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne); |
| 2207 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb); |
| 2208 | |
| 2209 | const auto [ir0, ir1] = get_thread_range(params, dst); |
| 2210 | |
| 2211 | for (int64_t ir = ir0; ir < ir1; ++ir) { |
| 2212 | const int64_t i03 = ir/(ne2*ne1); |
| 2213 | const int64_t i02 = (ir - i03*ne2*ne1)/ne1; |
| 2214 | const int64_t i01 = (ir - i03*ne2*ne1 - i02*ne1); |
| 2215 | |
| 2216 | ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1); |
| 2217 | |
| 2218 | ggml_vec_set_f16(ne0, dst_ptr, c); |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | void ggml_compute_forward_fill(const ggml_compute_params * params, ggml_tensor * dst) { |
| 2223 | const ggml_tensor * src0 = dst->src[0]; |
| 2224 | |
| 2225 | switch (src0->type) { |
| 2226 | case GGML_TYPE_F32: |
| 2227 | { |
| 2228 | ggml_compute_forward_fill_f32(params, dst); |
| 2229 | } break; |
| 2230 | case GGML_TYPE_F16: |
| 2231 | { |
| 2232 | ggml_compute_forward_fill_f16(params, dst); |
| 2233 | } break; |
| 2234 | default: |
| 2235 | { |
| 2236 | GGML_ABORT("unsupported type for ggml_compute_forward_fill: %s", ggml_type_name(src0->type)); |
| 2237 | } |
| 2238 | } |
| 2239 | } |
| 2240 | |
| 2241 | // ggml_compute_tri |
| 2242 | |
| 2243 | static void ggml_compute_forward_tri_f32(const ggml_compute_params * params, ggml_tensor * dst) { |
| 2244 | const ggml_tensor * src0 = dst->src[0]; |
| 2245 | |
| 2246 | const ggml_tri_type ttype = (ggml_tri_type) ggml_get_op_params_i32(dst, 0); |
| 2247 | |
| 2248 | GGML_ASSERT(ggml_is_contiguous(src0)); |
| 2249 | |
| 2250 | GGML_TENSOR_UNARY_OP_LOCALS |
| 2251 | |
| 2252 | const auto [ir0, ir1] = get_thread_range(params, src0); |
| 2253 | |
| 2254 | bool (*bipred)(int, int); |
| 2255 | |
| 2256 | switch (ttype) { |
| 2257 | case GGML_TRI_TYPE_LOWER: bipred = [](int i, int r) { return i < r; }; break; |
| 2258 | case GGML_TRI_TYPE_LOWER_DIAG: bipred = [](int i, int r) { return i <= r; }; break; |
| 2259 | case GGML_TRI_TYPE_UPPER: bipred = [](int i, int r) { return i > r; }; break; |
| 2260 | case GGML_TRI_TYPE_UPPER_DIAG: bipred = [](int i, int r) { return i >= r; }; break; |
| 2261 | default: GGML_ABORT("invalid tri type"); |
| 2262 | } |
| 2263 | |
| 2264 | for (int64_t ir = ir0; ir < ir1; ++ir) { |
| 2265 | const int64_t i03 = ir/(ne02*ne01); |
| 2266 | const int64_t i02 = (ir - i03*ne02*ne01)/ne01; |
| 2267 | const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01); |
| 2268 | |
| 2269 | const float * src_ptr = (const float *) ((const char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01); |
| 2270 | float * dst_ptr = ( float *) (( char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1); |
| 2271 | |
| 2272 | for (int i0 = 0; i0 < ne0; ++i0) { |
| 2273 | dst_ptr[i0] = bipred(i0, i01) ? src_ptr[i0] : 0.0f; |
| 2274 | } |
| 2275 | } |
| 2276 | } |
| 2277 | |
| 2278 | void ggml_compute_forward_tri(const ggml_compute_params * params, ggml_tensor * dst) { |
| 2279 | const ggml_tensor * src0 = dst->src[0]; |
| 2280 | |
| 2281 | switch (src0->type) { |
| 2282 | case GGML_TYPE_F32: |
| 2283 | { |
| 2284 | ggml_compute_forward_tri_f32(params, dst); |
| 2285 | } break; |
| 2286 | default: |
| 2287 | { |
| 2288 | GGML_ABORT("fatal error"); |
| 2289 | } |
| 2290 | } |
| 2291 | } |
| 2292 | |
| 2293 | // ggml_compute_forward_gelu_erf |
| 2294 | |
| 2295 | static void ggml_compute_forward_gelu_erf_f32( |
| 2296 | const ggml_compute_params * params, |
| 2297 | ggml_tensor * dst) { |
| 2298 | |
| 2299 | const ggml_tensor * src0 = dst->src[0]; |
| 2300 | |
| 2301 | assert(ggml_is_contiguous_rows(src0)); |
| 2302 | assert(ggml_are_same_shape(src0, dst)); |
| 2303 | |
| 2304 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 2305 | GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
| 2306 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 2307 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 2308 | |
| 2309 | const int ith = params->ith; |
| 2310 | const int nth = params->nth; |
| 2311 | |
| 2312 | const int nc = src0->ne[0]; |
| 2313 | const int nr = ggml_nrows(src0); |
| 2314 | |
| 2315 | // rows per thread |
| 2316 | const int dr = (nr + nth - 1)/nth; |
| 2317 | |
| 2318 | // row range for this thread |
| 2319 | const int ir0 = dr*ith; |
| 2320 | const int ir1 = MIN(ir0 + dr, nr); |
| 2321 | |
| 2322 | for (int ir = ir0; ir < ir1; ++ir) { |
| 2323 | const int i3 = ir/(ne02*ne01); |
| 2324 | const int i2 = (ir - i3*ne02*ne01)/ne01; |
| 2325 | const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
| 2326 | |
| 2327 | ggml_vec_gelu_erf_f32(nc, |
| 2328 | (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
| 2329 | (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
| 2330 | |
| 2331 | #ifndef NDEBUG |
| 2332 | for (int k = 0; k < nc; k++) { |
| 2333 | const float x = ((float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*(dst->nb[1])))[k]; |
| 2334 | GGML_UNUSED(x); |
| 2335 | assert(!isnan(x)); |
| 2336 | assert(!isinf(x)); |
| 2337 | } |
| 2338 | #endif // NDEBUG |
| 2339 | } |
| 2340 | } |
| 2341 | |
| 2342 | static void ggml_compute_forward_gelu_erf_f16( |
| 2343 | const ggml_compute_params * params, |
| 2344 | ggml_tensor * dst) { |
| 2345 | |
| 2346 | const ggml_tensor * src0 = dst->src[0]; |
| 2347 | |
| 2348 | assert(ggml_is_contiguous_rows(src0)); |
| 2349 | assert(ggml_are_same_shape(src0, dst)); |
| 2350 | |
| 2351 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 2352 | GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
| 2353 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 2354 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 2355 | |
| 2356 | const int ith = params->ith; |
| 2357 | const int nth = params->nth; |
| 2358 | |
| 2359 | const int nc = src0->ne[0]; |
| 2360 | const int nr = ggml_nrows(src0); |
| 2361 | |
| 2362 | // rows per thread |
| 2363 | const int dr = (nr + nth - 1)/nth; |
| 2364 | |
| 2365 | // row range for this thread |
| 2366 | const int ir0 = dr*ith; |
| 2367 | const int ir1 = MIN(ir0 + dr, nr); |
| 2368 | |
| 2369 | for (int ir = ir0; ir < ir1; ++ir) { |
| 2370 | const int i3 = ir/(ne02*ne01); |
| 2371 | const int i2 = (ir - i3*ne02*ne01)/ne01; |
| 2372 | const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
| 2373 | |
| 2374 | ggml_vec_gelu_erf_f16(nc, |
| 2375 | (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
| 2376 | (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
| 2377 | |
| 2378 | #ifndef NDEBUG |
| 2379 | for (int k = 0; k < nc; k++) { |
| 2380 | const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*( dst->nb[1])))[k]; |
| 2381 | const float v = GGML_CPU_FP16_TO_FP32(x); |
| 2382 | GGML_UNUSED(v); |
| 2383 | assert(!isnan(v)); |
| 2384 | assert(!isinf(v)); |
| 2385 | } |
| 2386 | #endif // NDEBUG |
| 2387 | } |
| 2388 | } |
| 2389 | |
| 2390 | static void ggml_compute_forward_gelu_erf_bf16( |
| 2391 | const ggml_compute_params * params, |
| 2392 | ggml_tensor * dst) { |
| 2393 | ggml_compute_forward_unary_bf16_rows<ggml_vec_gelu_erf_bf16>(params, dst); |
| 2394 | } |
| 2395 | |
| 2396 | static void ggml_compute_forward_gelu_erf( |
| 2397 | const ggml_compute_params * params, |
| 2398 | ggml_tensor * dst) { |
| 2399 | |
| 2400 | const ggml_tensor * src0 = dst->src[0]; |
| 2401 | |
| 2402 | switch (src0->type) { |
| 2403 | case GGML_TYPE_F32: |
| 2404 | { |
| 2405 | ggml_compute_forward_gelu_erf_f32(params, dst); |
| 2406 | } break; |
| 2407 | case GGML_TYPE_F16: |
| 2408 | { |
| 2409 | ggml_compute_forward_gelu_erf_f16(params, dst); |
| 2410 | } break; |
| 2411 | case GGML_TYPE_BF16: |
| 2412 | { |
| 2413 | ggml_compute_forward_gelu_erf_bf16(params, dst); |
| 2414 | } break; |
| 2415 | default: |
| 2416 | { |
| 2417 | GGML_ABORT("fatal error"); |
| 2418 | } |
| 2419 | } |
| 2420 | } |
| 2421 | |
| 2422 | // ggml_compute_forward_gelu_quick |
| 2423 | |
| 2424 | static void ggml_compute_forward_gelu_quick_f32( |
| 2425 | const ggml_compute_params * params, |
| 2426 | ggml_tensor * dst) { |
| 2427 | |
| 2428 | const ggml_tensor * src0 = dst->src[0]; |
| 2429 | |
| 2430 | assert(ggml_is_contiguous_rows(src0)); |
| 2431 | assert(ggml_are_same_shape(src0, dst)); |
| 2432 | |
| 2433 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 2434 | GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
| 2435 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 2436 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 2437 | |
| 2438 | const int ith = params->ith; |
| 2439 | const int nth = params->nth; |
| 2440 | |
| 2441 | const int nc = src0->ne[0]; |
| 2442 | const int nr = ggml_nrows(src0); |
| 2443 | |
| 2444 | // rows per thread |
| 2445 | const int dr = (nr + nth - 1)/nth; |
| 2446 | |
| 2447 | // row range for this thread |
| 2448 | const int ir0 = dr*ith; |
| 2449 | const int ir1 = MIN(ir0 + dr, nr); |
| 2450 | |
| 2451 | for (int ir = ir0; ir < ir1; ++ir) { |
| 2452 | const int i3 = ir/(ne02*ne01); |
| 2453 | const int i2 = (ir - i3*ne02*ne01)/ne01; |
| 2454 | const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
| 2455 | |
| 2456 | ggml_vec_gelu_quick_f32(nc, |
| 2457 | (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
| 2458 | (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
| 2459 | |
| 2460 | #ifndef NDEBUG |
| 2461 | for (int k = 0; k < nc; k++) { |
| 2462 | const float x = ((float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*(dst->nb[1])))[k]; |
| 2463 | GGML_UNUSED(x); |
| 2464 | assert(!isnan(x)); |
| 2465 | assert(!isinf(x)); |
| 2466 | } |
| 2467 | #endif // NDEBUG |
| 2468 | } |
| 2469 | } |
| 2470 | |
| 2471 | static void ggml_compute_forward_gelu_quick_f16( |
| 2472 | const ggml_compute_params * params, |
| 2473 | ggml_tensor * dst) { |
| 2474 | |
| 2475 | const ggml_tensor * src0 = dst->src[0]; |
| 2476 | |
| 2477 | assert(ggml_is_contiguous_rows(src0)); |
| 2478 | assert(ggml_are_same_shape(src0, dst)); |
| 2479 | |
| 2480 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 2481 | GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
| 2482 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 2483 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 2484 | |
| 2485 | const int ith = params->ith; |
| 2486 | const int nth = params->nth; |
| 2487 | |
| 2488 | const int nc = src0->ne[0]; |
| 2489 | const int nr = ggml_nrows(src0); |
| 2490 | |
| 2491 | // rows per thread |
| 2492 | const int dr = (nr + nth - 1)/nth; |
| 2493 | |
| 2494 | // row range for this thread |
| 2495 | const int ir0 = dr*ith; |
| 2496 | const int ir1 = MIN(ir0 + dr, nr); |
| 2497 | |
| 2498 | for (int ir = ir0; ir < ir1; ++ir) { |
| 2499 | const int i3 = ir/(ne02*ne01); |
| 2500 | const int i2 = (ir - i3*ne02*ne01)/ne01; |
| 2501 | const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
| 2502 | |
| 2503 | ggml_vec_gelu_quick_f16(nc, |
| 2504 | (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
| 2505 | (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
| 2506 | |
| 2507 | #ifndef NDEBUG |
| 2508 | for (int k = 0; k < nc; k++) { |
| 2509 | const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*( dst->nb[1])))[k]; |
| 2510 | const float v = GGML_CPU_FP16_TO_FP32(x); |
| 2511 | GGML_UNUSED(v); |
| 2512 | assert(!isnan(v)); |
| 2513 | assert(!isinf(v)); |
| 2514 | } |
| 2515 | #endif // NDEBUG |
| 2516 | } |
| 2517 | } |
| 2518 | |
| 2519 | static void ggml_compute_forward_gelu_quick_bf16( |
| 2520 | const ggml_compute_params * params, |
| 2521 | ggml_tensor * dst) { |
| 2522 | ggml_compute_forward_unary_bf16_rows<ggml_vec_gelu_quick_bf16>(params, dst); |
| 2523 | } |
| 2524 | |
| 2525 | static void ggml_compute_forward_gelu_quick( |
| 2526 | const ggml_compute_params * params, |
| 2527 | ggml_tensor * dst) { |
| 2528 | |
| 2529 | const ggml_tensor * src0 = dst->src[0]; |
| 2530 | |
| 2531 | switch (src0->type) { |
| 2532 | case GGML_TYPE_F32: |
| 2533 | { |
| 2534 | ggml_compute_forward_gelu_quick_f32(params, dst); |
| 2535 | } break; |
| 2536 | case GGML_TYPE_F16: |
| 2537 | { |
| 2538 | ggml_compute_forward_gelu_quick_f16(params, dst); |
| 2539 | } break; |
| 2540 | case GGML_TYPE_BF16: |
| 2541 | { |
| 2542 | ggml_compute_forward_gelu_quick_bf16(params, dst); |
| 2543 | } break; |
| 2544 | default: |
| 2545 | { |
| 2546 | GGML_ABORT("fatal error"); |
| 2547 | } |
| 2548 | } |
| 2549 | } |
| 2550 | |
| 2551 | // ggml_compute_forward_silu |
| 2552 | |
| 2553 | static void ggml_compute_forward_silu_f32( |
| 2554 | const ggml_compute_params * params, |
| 2555 | ggml_tensor * dst) { |
| 2556 | |
| 2557 | const ggml_tensor * src0 = dst->src[0]; |
| 2558 | |
| 2559 | assert(ggml_is_contiguous_rows(src0)); |
| 2560 | assert(ggml_are_same_shape(src0, dst)); |
| 2561 | |
| 2562 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 2563 | GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
| 2564 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 2565 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 2566 | |
| 2567 | const int ith = params->ith; |
| 2568 | const int nth = params->nth; |
| 2569 | |
| 2570 | const int nc = src0->ne[0]; |
| 2571 | const int nr = ggml_nrows(src0); |
| 2572 | |
| 2573 | // rows per thread |
| 2574 | const int dr = (nr + nth - 1)/nth; |
| 2575 | |
| 2576 | // row range for this thread |
| 2577 | const int ir0 = dr*ith; |
| 2578 | const int ir1 = MIN(ir0 + dr, nr); |
| 2579 | |
| 2580 | for (int ir = ir0; ir < ir1; ++ir) { |
| 2581 | const int i3 = ir/(ne02*ne01); |
| 2582 | const int i2 = (ir - i3*ne02*ne01)/ne01; |
| 2583 | const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
| 2584 | |
| 2585 | ggml_vec_silu_f32(nc, |
| 2586 | (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
| 2587 | (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
| 2588 | |
| 2589 | #ifndef NDEBUG |
| 2590 | for (int k = 0; k < nc; k++) { |
| 2591 | const float x = ((float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*(dst->nb[1])))[k]; |
| 2592 | GGML_UNUSED(x); |
| 2593 | assert(!isnan(x)); |
| 2594 | assert(!isinf(x)); |
| 2595 | } |
| 2596 | #endif // NDEBUG |
| 2597 | } |
| 2598 | } |
| 2599 | |
| 2600 | static void ggml_compute_forward_silu_f16( |
| 2601 | const ggml_compute_params * params, |
| 2602 | ggml_tensor * dst) { |
| 2603 | |
| 2604 | const ggml_tensor * src0 = dst->src[0]; |
| 2605 | |
| 2606 | assert(ggml_is_contiguous_rows(src0)); |
| 2607 | assert(ggml_are_same_shape(src0, dst)); |
| 2608 | |
| 2609 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 2610 | GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
| 2611 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 2612 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 2613 | |
| 2614 | const int ith = params->ith; |
| 2615 | const int nth = params->nth; |
| 2616 | |
| 2617 | const int nc = src0->ne[0]; |
| 2618 | const int nr = ggml_nrows(src0); |
| 2619 | |
| 2620 | // rows per thread |
| 2621 | const int dr = (nr + nth - 1)/nth; |
| 2622 | |
| 2623 | // row range for this thread |
| 2624 | const int ir0 = dr*ith; |
| 2625 | const int ir1 = MIN(ir0 + dr, nr); |
| 2626 | |
| 2627 | for (int ir = ir0; ir < ir1; ++ir) { |
| 2628 | const int i3 = ir/(ne02*ne01); |
| 2629 | const int i2 = (ir - i3*ne02*ne01)/ne01; |
| 2630 | const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
| 2631 | |
| 2632 | ggml_vec_silu_f16(nc, |
| 2633 | (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
| 2634 | (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
| 2635 | |
| 2636 | #ifndef NDEBUG |
| 2637 | for (int k = 0; k < nc; k++) { |
| 2638 | const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*( dst->nb[1])))[k]; |
| 2639 | const float v = GGML_CPU_FP16_TO_FP32(x); |
| 2640 | GGML_UNUSED(v); |
| 2641 | assert(!isnan(v)); |
| 2642 | assert(!isinf(v)); |
| 2643 | } |
| 2644 | #endif // NDEBUG |
| 2645 | } |
| 2646 | } |
| 2647 | |
| 2648 | static void ggml_compute_forward_silu_bf16( |
| 2649 | const ggml_compute_params * params, |
| 2650 | ggml_tensor * dst) { |
| 2651 | ggml_compute_forward_unary_bf16_rows<ggml_vec_silu_bf16>(params, dst); |
| 2652 | } |
| 2653 | |
| 2654 | static void ggml_compute_forward_silu( |
| 2655 | const ggml_compute_params * params, |
| 2656 | ggml_tensor * dst) { |
| 2657 | |
| 2658 | const ggml_tensor * src0 = dst->src[0]; |
| 2659 | |
| 2660 | switch (src0->type) { |
| 2661 | case GGML_TYPE_F32: |
| 2662 | { |
| 2663 | ggml_compute_forward_silu_f32(params, dst); |
| 2664 | } break; |
| 2665 | case GGML_TYPE_F16: |
| 2666 | { |
| 2667 | ggml_compute_forward_silu_f16(params, dst); |
| 2668 | } break; |
| 2669 | case GGML_TYPE_BF16: |
| 2670 | { |
| 2671 | ggml_compute_forward_silu_bf16(params, dst); |
| 2672 | } break; |
| 2673 | default: |
| 2674 | { |
| 2675 | GGML_ABORT("fatal error"); |
| 2676 | } |
| 2677 | } |
| 2678 | } |
| 2679 | // ggml_compute_forward_leaky_relu |
| 2680 | |
| 2681 | static void ggml_compute_forward_leaky_relu_f32( |
| 2682 | const ggml_compute_params * params, |
| 2683 | ggml_tensor * dst) { |
| 2684 | |
| 2685 | const ggml_tensor * src0 = dst->src[0]; |
| 2686 | |
| 2687 | if (params->ith != 0) { |
| 2688 | return; |
| 2689 | } |
| 2690 | |
| 2691 | assert(ggml_is_contiguous_1(src0)); |
| 2692 | assert(ggml_is_contiguous_1(dst)); |
| 2693 | assert(ggml_are_same_shape(src0, dst)); |
| 2694 | |
| 2695 | const int n = ggml_nrows(src0); |
| 2696 | const int nc = src0->ne[0]; |
| 2697 | |
| 2698 | float negative_slope; |
| 2699 | memcpy(&negative_slope, dst->op_params, sizeof(float)); |
| 2700 | |
| 2701 | assert(dst->nb[0] == sizeof(float)); |
| 2702 | assert(src0->nb[0] == sizeof(float)); |
| 2703 | |
| 2704 | for (int i = 0; i < n; i++) { |
| 2705 | ggml_vec_leaky_relu_f32(nc, |
| 2706 | (float *) ((char *) dst->data + i*( dst->nb[1])), |
| 2707 | (float *) ((char *) src0->data + i*(src0->nb[1])), negative_slope); |
| 2708 | } |
| 2709 | } |
| 2710 | |
| 2711 | static void ggml_compute_forward_leaky_relu_f16( |
| 2712 | const ggml_compute_params * params, |
| 2713 | ggml_tensor * dst) { |
| 2714 | |
| 2715 | const ggml_tensor * src0 = dst->src[0]; |
| 2716 | |
| 2717 | if (params->ith != 0) { |
| 2718 | return; |
| 2719 | } |
| 2720 | |
| 2721 | assert(ggml_is_contiguous_1(src0)); |
| 2722 | assert(ggml_is_contiguous_1(dst)); |
| 2723 | assert(ggml_are_same_shape(src0, dst)); |
| 2724 | |
| 2725 | const int n = ggml_nrows(src0); |
| 2726 | const int nc = src0->ne[0]; |
| 2727 | |
| 2728 | float negative_slope; |
| 2729 | memcpy(&negative_slope, dst->op_params, sizeof(float)); |
| 2730 | |
| 2731 | assert(dst->nb[0] == sizeof(ggml_fp16_t)); |
| 2732 | assert(src0->nb[0] == sizeof(ggml_fp16_t)); |
| 2733 | |
| 2734 | for (int i = 0; i < n; i++) { |
| 2735 | ggml_vec_leaky_relu_f16(nc, |
| 2736 | (ggml_fp16_t *) ((char *) dst->data + i*( dst->nb[1])), |
| 2737 | (ggml_fp16_t *) ((char *) src0->data + i*(src0->nb[1])), negative_slope); |
| 2738 | } |
| 2739 | } |
| 2740 | |
| 2741 | void ggml_compute_forward_leaky_relu( |
| 2742 | const ggml_compute_params * params, |
| 2743 | ggml_tensor * dst) { |
| 2744 | |
| 2745 | const ggml_tensor * src0 = dst->src[0]; |
| 2746 | |
| 2747 | switch (src0->type) { |
| 2748 | case GGML_TYPE_F32: |
| 2749 | { |
| 2750 | ggml_compute_forward_leaky_relu_f32(params, dst); |
| 2751 | } break; |
| 2752 | case GGML_TYPE_F16: |
| 2753 | { |
| 2754 | ggml_compute_forward_leaky_relu_f16(params, dst); |
| 2755 | } break; |
| 2756 | default: |
| 2757 | { |
| 2758 | GGML_ABORT("fatal error"); |
| 2759 | } |
| 2760 | } |
| 2761 | } |
| 2762 | |
| 2763 | // ggml_compute_forward_silu_back |
| 2764 | |
| 2765 | static void ggml_compute_forward_silu_back_f32( |
| 2766 | const ggml_compute_params * params, |
| 2767 | ggml_tensor * dst) { |
| 2768 | |
| 2769 | const ggml_tensor * grad = dst->src[0]; |
| 2770 | const ggml_tensor * src1 = dst->src[1]; |
| 2771 | |
| 2772 | assert(ggml_is_contiguous_1(grad)); |
| 2773 | assert(ggml_is_contiguous_1(src1)); |
| 2774 | assert(ggml_is_contiguous_1(dst)); |
| 2775 | assert(ggml_are_same_shape(src1, dst)); |
| 2776 | assert(ggml_are_same_shape(src1, grad)); |
| 2777 | |
| 2778 | const int ith = params->ith; |
| 2779 | const int nth = params->nth; |
| 2780 | |
| 2781 | const int nc = src1->ne[0]; |
| 2782 | const int nr = ggml_nrows(src1); |
| 2783 | |
| 2784 | // rows per thread |
| 2785 | const int dr = (nr + nth - 1)/nth; |
| 2786 | |
| 2787 | // row range for this thread |
| 2788 | const int ir0 = dr*ith; |
| 2789 | const int ir1 = MIN(ir0 + dr, nr); |
| 2790 | |
| 2791 | for (int i1 = ir0; i1 < ir1; i1++) { |
| 2792 | ggml_vec_silu_backward_f32(nc, |
| 2793 | (float *) ((char *) dst->data + i1*( dst->nb[1])), |
| 2794 | (float *) ((char *) src1->data + i1*(src1->nb[1])), |
| 2795 | (float *) ((char *) grad->data + i1*(grad->nb[1]))); |
| 2796 | |
| 2797 | #ifndef NDEBUG |
| 2798 | for (int k = 0; k < nc; k++) { |
| 2799 | const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 2800 | GGML_UNUSED(x); |
| 2801 | assert(!isnan(x)); |
| 2802 | assert(!isinf(x)); |
| 2803 | } |
| 2804 | #endif // NDEBUG |
| 2805 | } |
| 2806 | } |
| 2807 | |
| 2808 | static void ggml_compute_forward_silu_back_f16( |
| 2809 | const ggml_compute_params * params, |
| 2810 | ggml_tensor * dst) { |
| 2811 | |
| 2812 | const ggml_tensor * grad = dst->src[0]; |
| 2813 | const ggml_tensor * src1 = dst->src[1]; |
| 2814 | |
| 2815 | assert(ggml_is_contiguous_1(grad)); |
| 2816 | assert(ggml_is_contiguous_1(src1)); |
| 2817 | assert(ggml_is_contiguous_1(dst)); |
| 2818 | assert(ggml_are_same_shape(src1, dst)); |
| 2819 | assert(ggml_are_same_shape(src1, grad)); |
| 2820 | |
| 2821 | const int ith = params->ith; |
| 2822 | const int nth = params->nth; |
| 2823 | |
| 2824 | const int nc = src1->ne[0]; |
| 2825 | const int nr = ggml_nrows(src1); |
| 2826 | |
| 2827 | // rows per thread |
| 2828 | const int dr = (nr + nth - 1)/nth; |
| 2829 | |
| 2830 | // row range for this thread |
| 2831 | const int ir0 = dr*ith; |
| 2832 | const int ir1 = MIN(ir0 + dr, nr); |
| 2833 | |
| 2834 | for (int i1 = ir0; i1 < ir1; i1++) { |
| 2835 | ggml_vec_silu_backward_f16(nc, |
| 2836 | (ggml_fp16_t *) ((char *) dst->data + i1*( dst->nb[1])), |
| 2837 | (ggml_fp16_t *) ((char *) src1->data + i1*(src1->nb[1])), |
| 2838 | (ggml_fp16_t *) ((char *) grad->data + i1*(grad->nb[1]))); |
| 2839 | |
| 2840 | #ifndef NDEBUG |
| 2841 | for (int k = 0; k < nc; k++) { |
| 2842 | const float x = ((ggml_fp16_t *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 2843 | const float v = GGML_CPU_FP16_TO_FP32(x); |
| 2844 | GGML_UNUSED(v); |
| 2845 | assert(!isnan(v)); |
| 2846 | assert(!isinf(v)); |
| 2847 | } |
| 2848 | #endif // NDEBUG |
| 2849 | } |
| 2850 | } |
| 2851 | |
| 2852 | void ggml_compute_forward_silu_back( |
| 2853 | const ggml_compute_params * params, |
| 2854 | ggml_tensor * dst) { |
| 2855 | |
| 2856 | const ggml_tensor * src0 = dst->src[0]; |
| 2857 | |
| 2858 | switch (src0->type) { |
| 2859 | case GGML_TYPE_F32: |
| 2860 | { |
| 2861 | ggml_compute_forward_silu_back_f32(params, dst); |
| 2862 | } break; |
| 2863 | case GGML_TYPE_F16: |
| 2864 | { |
| 2865 | ggml_compute_forward_silu_back_f16(params, dst); |
| 2866 | } break; |
| 2867 | default: |
| 2868 | { |
| 2869 | GGML_ABORT("fatal error"); |
| 2870 | } |
| 2871 | } |
| 2872 | } |
| 2873 | |
| 2874 | // ggml_compute_forward_reglu |
| 2875 | |
| 2876 | // row loop shared by the BF16 GLU ops |
| 2877 | template <void (*vec_op)(const int, ggml_bf16_t *, const ggml_bf16_t *, const ggml_bf16_t *)> |
| 2878 | static void ggml_compute_forward_glu_bf16_rows( |
| 2879 | const ggml_compute_params * params, |
| 2880 | ggml_tensor * dst) { |
| 2881 | |
| 2882 | const ggml_tensor * src0 = dst->src[0]; |
| 2883 | const ggml_tensor * src1 = dst->src[1]; |
| 2884 | char * src0_d = (char *) src0->data; |
| 2885 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 2886 | const size_t src0_o = src0->nb[1]; |
| 2887 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 2888 | |
| 2889 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 2890 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 2891 | |
| 2892 | if (src1) { |
| 2893 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 2894 | GGML_ASSERT(src0->type == src1->type); |
| 2895 | } |
| 2896 | |
| 2897 | const int ith = params->ith; |
| 2898 | const int nth = params->nth; |
| 2899 | |
| 2900 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 2901 | const int nr = ggml_nrows(src0); |
| 2902 | |
| 2903 | GGML_ASSERT(dst->ne[0] == nc); |
| 2904 | GGML_ASSERT(ggml_nrows(dst) == nr); |
| 2905 | |
| 2906 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 2907 | |
| 2908 | // rows per thread |
| 2909 | const int dr = (nr + nth - 1)/nth; |
| 2910 | |
| 2911 | // row range for this thread |
| 2912 | const int ir0 = dr*ith; |
| 2913 | const int ir1 = MIN(ir0 + dr, nr); |
| 2914 | |
| 2915 | for (int i1 = ir0; i1 < ir1; i1++) { |
| 2916 | const ggml_bf16_t * src0_p = (const ggml_bf16_t *) (src0_d + i1*src0_o); |
| 2917 | const ggml_bf16_t * src1_p = (const ggml_bf16_t *) (src1_d + i1*src1_o); |
| 2918 | ggml_bf16_t * dst_p = (ggml_bf16_t *) ((char *) dst->data + i1*(dst->nb[1])); |
| 2919 | |
| 2920 | if (!src1) { |
| 2921 | src0_p += swapped ? nc : 0; |
| 2922 | src1_p += swapped ? 0 : nc; |
| 2923 | } |
| 2924 | |
| 2925 | vec_op(nc, dst_p, src0_p, src1_p); |
| 2926 | } |
| 2927 | } |
| 2928 | |
| 2929 | static void ggml_compute_forward_reglu_f32( |
| 2930 | const ggml_compute_params * params, |
| 2931 | ggml_tensor * dst) { |
| 2932 | |
| 2933 | const ggml_tensor * src0 = dst->src[0]; |
| 2934 | const ggml_tensor * src1 = dst->src[1]; |
| 2935 | char * src0_d = (char *) src0->data; |
| 2936 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 2937 | const size_t src0_o = src0->nb[1]; |
| 2938 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 2939 | |
| 2940 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 2941 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 2942 | |
| 2943 | if (src1) { |
| 2944 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 2945 | GGML_ASSERT(src0->type == src1->type); |
| 2946 | } |
| 2947 | |
| 2948 | const int ith = params->ith; |
| 2949 | const int nth = params->nth; |
| 2950 | |
| 2951 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 2952 | const int nr = ggml_nrows(src0); |
| 2953 | |
| 2954 | GGML_ASSERT(dst->ne[0] == nc); |
| 2955 | GGML_ASSERT(ggml_nrows(dst) == nr); |
| 2956 | |
| 2957 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 2958 | |
| 2959 | // rows per thread |
| 2960 | const int dr = (nr + nth - 1)/nth; |
| 2961 | |
| 2962 | // row range for this thread |
| 2963 | const int ir0 = dr*ith; |
| 2964 | const int ir1 = MIN(ir0 + dr, nr); |
| 2965 | |
| 2966 | for (int i1 = ir0; i1 < ir1; i1++) { |
| 2967 | float * src0_p = (float *) (src0_d + i1*src0_o); |
| 2968 | float * src1_p = (float *) (src1_d + i1*src1_o); |
| 2969 | |
| 2970 | if (!src1) { |
| 2971 | src0_p += swapped ? nc : 0; |
| 2972 | src1_p += swapped ? 0 : nc; |
| 2973 | } |
| 2974 | |
| 2975 | ggml_vec_reglu_f32(nc, (float *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
| 2976 | |
| 2977 | #ifndef NDEBUG |
| 2978 | for (int k = 0; k < nc; k++) { |
| 2979 | const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 2980 | GGML_UNUSED(x); |
| 2981 | assert(!isnan(x)); |
| 2982 | assert(!isinf(x)); |
| 2983 | } |
| 2984 | #endif // NDEBUG |
| 2985 | } |
| 2986 | } |
| 2987 | |
| 2988 | static void ggml_compute_forward_reglu_f16( |
| 2989 | const ggml_compute_params * params, |
| 2990 | ggml_tensor * dst) { |
| 2991 | |
| 2992 | const ggml_tensor * src0 = dst->src[0]; |
| 2993 | const ggml_tensor * src1 = dst->src[1]; |
| 2994 | char * src0_d = (char *) src0->data; |
| 2995 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 2996 | const size_t src0_o = src0->nb[1]; |
| 2997 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 2998 | |
| 2999 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 3000 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 3001 | |
| 3002 | if (src1) { |
| 3003 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 3004 | GGML_ASSERT(src0->type == src1->type); |
| 3005 | } |
| 3006 | |
| 3007 | const int ith = params->ith; |
| 3008 | const int nth = params->nth; |
| 3009 | |
| 3010 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 3011 | const int nr = ggml_nrows(src0); |
| 3012 | |
| 3013 | GGML_ASSERT(dst->ne[0] == nc); |
| 3014 | GGML_ASSERT(ggml_nrows(dst) == nr); |
| 3015 | |
| 3016 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 3017 | |
| 3018 | // rows per thread |
| 3019 | const int dr = (nr + nth - 1)/nth; |
| 3020 | |
| 3021 | // row range for this thread |
| 3022 | const int ir0 = dr*ith; |
| 3023 | const int ir1 = MIN(ir0 + dr, nr); |
| 3024 | |
| 3025 | for (int i1 = ir0; i1 < ir1; i1++) { |
| 3026 | ggml_fp16_t * src0_p = (ggml_fp16_t *) (src0_d + i1*src0_o); |
| 3027 | ggml_fp16_t * src1_p = (ggml_fp16_t *) (src1_d + i1*src1_o); |
| 3028 | |
| 3029 | if (!src1) { |
| 3030 | src0_p += swapped ? nc : 0; |
| 3031 | src1_p += swapped ? 0 : nc; |
| 3032 | } |
| 3033 | |
| 3034 | ggml_vec_reglu_f16(nc, (ggml_fp16_t *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
| 3035 | |
| 3036 | #ifndef NDEBUG |
| 3037 | for (int k = 0; k < nc; k++) { |
| 3038 | const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 3039 | const float v = GGML_FP16_TO_FP32(x); |
| 3040 | GGML_UNUSED(v); |
| 3041 | assert(!isnan(v)); |
| 3042 | assert(!isinf(v)); |
| 3043 | } |
| 3044 | #endif // NDEBUG |
| 3045 | } |
| 3046 | } |
| 3047 | |
| 3048 | static void ggml_compute_forward_reglu_bf16( |
| 3049 | const ggml_compute_params * params, |
| 3050 | ggml_tensor * dst) { |
| 3051 | ggml_compute_forward_glu_bf16_rows<ggml_vec_reglu_bf16>(params, dst); |
| 3052 | } |
| 3053 | |
| 3054 | static void ggml_compute_forward_reglu( |
| 3055 | const ggml_compute_params * params, |
| 3056 | ggml_tensor * dst) { |
| 3057 | |
| 3058 | const ggml_tensor * src0 = dst->src[0]; |
| 3059 | |
| 3060 | switch (src0->type) { |
| 3061 | case GGML_TYPE_F32: |
| 3062 | { |
| 3063 | ggml_compute_forward_reglu_f32(params, dst); |
| 3064 | } break; |
| 3065 | case GGML_TYPE_F16: |
| 3066 | { |
| 3067 | ggml_compute_forward_reglu_f16(params, dst); |
| 3068 | } break; |
| 3069 | case GGML_TYPE_BF16: |
| 3070 | { |
| 3071 | ggml_compute_forward_reglu_bf16(params, dst); |
| 3072 | } break; |
| 3073 | default: |
| 3074 | { |
| 3075 | GGML_ABORT("fatal error"); |
| 3076 | } |
| 3077 | } |
| 3078 | } |
| 3079 | |
| 3080 | // ggml_compute_forward_geglu |
| 3081 | |
| 3082 | static void ggml_compute_forward_geglu_f32( |
| 3083 | const ggml_compute_params * params, |
| 3084 | ggml_tensor * dst) { |
| 3085 | |
| 3086 | const ggml_tensor * src0 = dst->src[0]; |
| 3087 | const ggml_tensor * src1 = dst->src[1]; |
| 3088 | char * src0_d = (char *) src0->data; |
| 3089 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 3090 | const size_t src0_o = src0->nb[1]; |
| 3091 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 3092 | |
| 3093 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 3094 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 3095 | |
| 3096 | if (src1) { |
| 3097 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 3098 | GGML_ASSERT(src0->type == src1->type); |
| 3099 | } |
| 3100 | |
| 3101 | const int ith = params->ith; |
| 3102 | const int nth = params->nth; |
| 3103 | |
| 3104 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 3105 | const int nr = ggml_nrows(src0); |
| 3106 | |
| 3107 | GGML_ASSERT(dst->ne[0] == nc); |
| 3108 | GGML_ASSERT(ggml_nrows(dst) == nr); |
| 3109 | |
| 3110 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 3111 | |
| 3112 | // rows per thread |
| 3113 | const int dr = (nr + nth - 1)/nth; |
| 3114 | |
| 3115 | // row range for this thread |
| 3116 | const int ir0 = dr*ith; |
| 3117 | const int ir1 = MIN(ir0 + dr, nr); |
| 3118 | |
| 3119 | for (int i1 = ir0; i1 < ir1; i1++) { |
| 3120 | float * src0_p = (float *) (src0_d + i1*src0_o); |
| 3121 | float * src1_p = (float *) (src1_d + i1*src1_o); |
| 3122 | |
| 3123 | if (!src1) { |
| 3124 | src0_p += swapped ? nc : 0; |
| 3125 | src1_p += swapped ? 0 : nc; |
| 3126 | } |
| 3127 | |
| 3128 | ggml_vec_geglu_f32(nc, (float *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
| 3129 | |
| 3130 | #ifndef NDEBUG |
| 3131 | for (int k = 0; k < nc; k++) { |
| 3132 | const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 3133 | GGML_UNUSED(x); |
| 3134 | assert(!isnan(x)); |
| 3135 | assert(!isinf(x)); |
| 3136 | } |
| 3137 | #endif // NDEBUG |
| 3138 | } |
| 3139 | } |
| 3140 | |
| 3141 | static void ggml_compute_forward_geglu_f16( |
| 3142 | const ggml_compute_params * params, |
| 3143 | ggml_tensor * dst) { |
| 3144 | |
| 3145 | const ggml_tensor * src0 = dst->src[0]; |
| 3146 | const ggml_tensor * src1 = dst->src[1]; |
| 3147 | char * src0_d = (char *) src0->data; |
| 3148 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 3149 | const size_t src0_o = src0->nb[1]; |
| 3150 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 3151 | |
| 3152 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 3153 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 3154 | |
| 3155 | if (src1) { |
| 3156 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 3157 | GGML_ASSERT(src0->type == src1->type); |
| 3158 | } |
| 3159 | |
| 3160 | const int ith = params->ith; |
| 3161 | const int nth = params->nth; |
| 3162 | |
| 3163 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 3164 | const int nr = ggml_nrows(src0); |
| 3165 | |
| 3166 | GGML_ASSERT(dst->ne[0] == nc); |
| 3167 | GGML_ASSERT(ggml_nrows(dst) == nr); |
| 3168 | |
| 3169 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 3170 | |
| 3171 | // rows per thread |
| 3172 | const int dr = (nr + nth - 1)/nth; |
| 3173 | |
| 3174 | // row range for this thread |
| 3175 | const int ir0 = dr*ith; |
| 3176 | const int ir1 = MIN(ir0 + dr, nr); |
| 3177 | |
| 3178 | for (int i1 = ir0; i1 < ir1; i1++) { |
| 3179 | ggml_fp16_t * src0_p = (ggml_fp16_t *) (src0_d + i1*src0_o); |
| 3180 | ggml_fp16_t * src1_p = (ggml_fp16_t *) (src1_d + i1*src1_o); |
| 3181 | |
| 3182 | if (!src1) { |
| 3183 | src0_p += swapped ? nc : 0; |
| 3184 | src1_p += swapped ? 0 : nc; |
| 3185 | } |
| 3186 | |
| 3187 | ggml_vec_geglu_f16(nc, (ggml_fp16_t *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
| 3188 | |
| 3189 | #ifndef NDEBUG |
| 3190 | for (int k = 0; k < nc; k++) { |
| 3191 | const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 3192 | const float v = GGML_FP16_TO_FP32(x); |
| 3193 | GGML_UNUSED(v); |
| 3194 | assert(!isnan(v)); |
| 3195 | assert(!isinf(v)); |
| 3196 | } |
| 3197 | #endif // NDEBUG |
| 3198 | } |
| 3199 | } |
| 3200 | |
| 3201 | static void ggml_compute_forward_geglu_bf16( |
| 3202 | const ggml_compute_params * params, |
| 3203 | ggml_tensor * dst) { |
| 3204 | ggml_compute_forward_glu_bf16_rows<ggml_vec_geglu_bf16>(params, dst); |
| 3205 | } |
| 3206 | |
| 3207 | static void ggml_compute_forward_geglu( |
| 3208 | const ggml_compute_params * params, |
| 3209 | ggml_tensor * dst) { |
| 3210 | |
| 3211 | const ggml_tensor * src0 = dst->src[0]; |
| 3212 | |
| 3213 | switch (src0->type) { |
| 3214 | case GGML_TYPE_F32: |
| 3215 | { |
| 3216 | ggml_compute_forward_geglu_f32(params, dst); |
| 3217 | } break; |
| 3218 | case GGML_TYPE_F16: |
| 3219 | { |
| 3220 | ggml_compute_forward_geglu_f16(params, dst); |
| 3221 | } break; |
| 3222 | case GGML_TYPE_BF16: |
| 3223 | { |
| 3224 | ggml_compute_forward_geglu_bf16(params, dst); |
| 3225 | } break; |
| 3226 | default: |
| 3227 | { |
| 3228 | GGML_ABORT("fatal error"); |
| 3229 | } |
| 3230 | } |
| 3231 | } |
| 3232 | |
| 3233 | // ggml_compute_forward_swiglu |
| 3234 | |
| 3235 | static void ggml_compute_forward_swiglu_f32( |
| 3236 | const ggml_compute_params * params, |
| 3237 | ggml_tensor * dst) { |
| 3238 | |
| 3239 | const ggml_tensor * src0 = dst->src[0]; |
| 3240 | const ggml_tensor * src1 = dst->src[1]; |
| 3241 | char * src0_d = (char *) src0->data; |
| 3242 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 3243 | const size_t src0_o = src0->nb[1]; |
| 3244 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 3245 | |
| 3246 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 3247 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 3248 | |
| 3249 | if (src1) { |
| 3250 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 3251 | GGML_ASSERT(src0->type == src1->type); |
| 3252 | } |
| 3253 | |
| 3254 | const int ith = params->ith; |
| 3255 | const int nth = params->nth; |
| 3256 | |
| 3257 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 3258 | const int nr = ggml_nrows(src0); |
| 3259 | |
| 3260 | GGML_ASSERT(dst->ne[0] == nc); |
| 3261 | GGML_ASSERT(ggml_nrows(dst) == nr); |
| 3262 | |
| 3263 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 3264 | |
| 3265 | // rows per thread |
| 3266 | const int dr = (nr + nth - 1)/nth; |
| 3267 | |
| 3268 | // row range for this thread |
| 3269 | const int ir0 = dr*ith; |
| 3270 | const int ir1 = MIN(ir0 + dr, nr); |
| 3271 | |
| 3272 | for (int i1 = ir0; i1 < ir1; i1++) { |
| 3273 | float * src0_p = (float *) (src0_d + i1*src0_o); |
| 3274 | float * src1_p = (float *) (src1_d + i1*src1_o); |
| 3275 | |
| 3276 | if (!src1) { |
| 3277 | src0_p += swapped ? nc : 0; |
| 3278 | src1_p += swapped ? 0 : nc; |
| 3279 | } |
| 3280 | |
| 3281 | ggml_vec_swiglu_f32(nc, (float *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
| 3282 | |
| 3283 | #ifndef NDEBUG |
| 3284 | for (int k = 0; k < nc; k++) { |
| 3285 | const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 3286 | GGML_UNUSED(x); |
| 3287 | assert(!isnan(x)); |
| 3288 | assert(!isinf(x)); |
| 3289 | } |
| 3290 | #endif // NDEBUG |
| 3291 | } |
| 3292 | } |
| 3293 | |
| 3294 | static void ggml_compute_forward_swiglu_f16( |
| 3295 | const ggml_compute_params * params, |
| 3296 | ggml_tensor * dst) { |
| 3297 | |
| 3298 | const ggml_tensor * src0 = dst->src[0]; |
| 3299 | const ggml_tensor * src1 = dst->src[1]; |
| 3300 | char * src0_d = (char *) src0->data; |
| 3301 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 3302 | const size_t src0_o = src0->nb[1]; |
| 3303 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 3304 | |
| 3305 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 3306 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 3307 | |
| 3308 | if (src1) { |
| 3309 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 3310 | GGML_ASSERT(src0->type == src1->type); |
| 3311 | } |
| 3312 | |
| 3313 | const int ith = params->ith; |
| 3314 | const int nth = params->nth; |
| 3315 | |
| 3316 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 3317 | const int nr = ggml_nrows(src0); |
| 3318 | |
| 3319 | GGML_ASSERT(dst->ne[0] == nc); |
| 3320 | GGML_ASSERT(ggml_nrows(dst) == nr); |
| 3321 | |
| 3322 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 3323 | |
| 3324 | // rows per thread |
| 3325 | const int dr = (nr + nth - 1)/nth; |
| 3326 | |
| 3327 | // row range for this thread |
| 3328 | const int ir0 = dr*ith; |
| 3329 | const int ir1 = MIN(ir0 + dr, nr); |
| 3330 | |
| 3331 | for (int i1 = ir0; i1 < ir1; i1++) { |
| 3332 | ggml_fp16_t * src0_p = (ggml_fp16_t *) (src0_d + i1*src0_o); |
| 3333 | ggml_fp16_t * src1_p = (ggml_fp16_t *) (src1_d + i1*src1_o); |
| 3334 | |
| 3335 | if (!src1) { |
| 3336 | src0_p += swapped ? nc : 0; |
| 3337 | src1_p += swapped ? 0 : nc; |
| 3338 | } |
| 3339 | |
| 3340 | ggml_vec_swiglu_f16(nc, (ggml_fp16_t *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
| 3341 | |
| 3342 | #ifndef NDEBUG |
| 3343 | for (int k = 0; k < nc; k++) { |
| 3344 | const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 3345 | const float v = GGML_FP16_TO_FP32(x); |
| 3346 | GGML_UNUSED(v); |
| 3347 | assert(!isnan(v)); |
| 3348 | assert(!isinf(v)); |
| 3349 | } |
| 3350 | #endif // NDEBUG |
| 3351 | } |
| 3352 | } |
| 3353 | |
| 3354 | static void ggml_compute_forward_swiglu_bf16( |
| 3355 | const ggml_compute_params * params, |
| 3356 | ggml_tensor * dst) { |
| 3357 | ggml_compute_forward_glu_bf16_rows<ggml_vec_swiglu_bf16>(params, dst); |
| 3358 | } |
| 3359 | |
| 3360 | static void ggml_compute_forward_swiglu( |
| 3361 | const ggml_compute_params * params, |
| 3362 | ggml_tensor * dst) { |
| 3363 | |
| 3364 | const ggml_tensor * src0 = dst->src[0]; |
| 3365 | |
| 3366 | switch (src0->type) { |
| 3367 | case GGML_TYPE_F32: |
| 3368 | { |
| 3369 | ggml_compute_forward_swiglu_f32(params, dst); |
| 3370 | } break; |
| 3371 | case GGML_TYPE_F16: |
| 3372 | { |
| 3373 | ggml_compute_forward_swiglu_f16(params, dst); |
| 3374 | } break; |
| 3375 | case GGML_TYPE_BF16: |
| 3376 | { |
| 3377 | ggml_compute_forward_swiglu_bf16(params, dst); |
| 3378 | } break; |
| 3379 | default: |
| 3380 | { |
| 3381 | GGML_ABORT("fatal error"); |
| 3382 | } |
| 3383 | } |
| 3384 | } |
| 3385 | |
| 3386 | // ggml_compute_forward_swiglu_oai |
| 3387 | |
| 3388 | static void ggml_compute_forward_swiglu_oai_f32( |
| 3389 | const ggml_compute_params * params, |
| 3390 | ggml_tensor * dst) { |
| 3391 | |
| 3392 | const ggml_tensor * src0 = dst->src[0]; |
| 3393 | const ggml_tensor * src1 = dst->src[1]; |
| 3394 | char * src0_d = (char *) src0->data; |
| 3395 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 3396 | const size_t src0_o = src0->nb[1]; |
| 3397 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 3398 | |
| 3399 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 3400 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 3401 | |
| 3402 | if (src1) { |
| 3403 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 3404 | GGML_ASSERT(src0->type == src1->type); |
| 3405 | } |
| 3406 | |
| 3407 | const int ith = params->ith; |
| 3408 | const int nth = params->nth; |
| 3409 | |
| 3410 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 3411 | const int nr = ggml_nrows(src0); |
| 3412 | |
| 3413 | GGML_ASSERT(dst->ne[0] == nc); |
| 3414 | GGML_ASSERT(ggml_nrows(dst) == nr); |
| 3415 | |
| 3416 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 3417 | const float alpha = ggml_get_op_params_f32(dst, 2); |
| 3418 | const float limit = ggml_get_op_params_f32(dst, 3); |
| 3419 | |
| 3420 | // rows per thread |
| 3421 | const int dr = (nr + nth - 1)/nth; |
| 3422 | |
| 3423 | // row range for this thread |
| 3424 | const int ir0 = dr*ith; |
| 3425 | const int ir1 = MIN(ir0 + dr, nr); |
| 3426 | |
| 3427 | for (int i1 = ir0; i1 < ir1; i1++) { |
| 3428 | float * src0_p = (float *) (src0_d + i1*src0_o); |
| 3429 | float * src1_p = (float *) (src1_d + i1*src1_o); |
| 3430 | float * dst_p = (float *) ((char *) dst->data + i1*(dst->nb[1])); |
| 3431 | |
| 3432 | if (!src1) { |
| 3433 | src0_p += swapped ? nc : 0; |
| 3434 | src1_p += swapped ? 0 : nc; |
| 3435 | } |
| 3436 | |
| 3437 | for (int k = 0; k < nc; k++) { |
| 3438 | const float x = std::min(src0_p[k], limit); |
| 3439 | const float y = std::clamp(src1_p[k], -limit, limit); |
| 3440 | const float out_glu = x / (1.f + expf(alpha * (-x))); |
| 3441 | dst_p[k] = out_glu * (y + 1.f); |
| 3442 | } |
| 3443 | |
| 3444 | #ifndef NDEBUG |
| 3445 | for (int k = 0; k < nc; k++) { |
| 3446 | const float x = dst_p[k]; |
| 3447 | GGML_UNUSED(x); |
| 3448 | assert(!isnan(x)); |
| 3449 | assert(!isinf(x)); |
| 3450 | } |
| 3451 | #endif // NDEBUG |
| 3452 | } |
| 3453 | } |
| 3454 | |
| 3455 | static void ggml_compute_forward_swiglu_oai_f16( |
| 3456 | const ggml_compute_params * params, |
| 3457 | ggml_tensor * dst) { |
| 3458 | |
| 3459 | const ggml_tensor * src0 = dst->src[0]; |
| 3460 | const ggml_tensor * src1 = dst->src[1]; |
| 3461 | char * src0_d = (char *) src0->data; |
| 3462 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 3463 | const size_t src0_o = src0->nb[1]; |
| 3464 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 3465 | |
| 3466 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 3467 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 3468 | |
| 3469 | if (src1) { |
| 3470 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 3471 | GGML_ASSERT(src0->type == src1->type); |
| 3472 | } |
| 3473 | |
| 3474 | const int ith = params->ith; |
| 3475 | const int nth = params->nth; |
| 3476 | |
| 3477 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 3478 | const int nr = ggml_nrows(src0); |
| 3479 | |
| 3480 | GGML_ASSERT(dst->ne[0] == nc); |
| 3481 | GGML_ASSERT(ggml_nrows(dst) == nr); |
| 3482 | |
| 3483 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 3484 | const float alpha = ggml_get_op_params_f32(dst, 2); |
| 3485 | const float limit = ggml_get_op_params_f32(dst, 3); |
| 3486 | |
| 3487 | // rows per thread |
| 3488 | const int dr = (nr + nth - 1)/nth; |
| 3489 | |
| 3490 | // row range for this thread |
| 3491 | const int ir0 = dr*ith; |
| 3492 | const int ir1 = MIN(ir0 + dr, nr); |
| 3493 | |
| 3494 | for (int i1 = ir0; i1 < ir1; i1++) { |
| 3495 | ggml_fp16_t * src0_p = (ggml_fp16_t *) (src0_d + i1*src0_o); |
| 3496 | ggml_fp16_t * src1_p = (ggml_fp16_t *) (src1_d + i1*src1_o); |
| 3497 | ggml_fp16_t * dst_p = (ggml_fp16_t *) ((char *) dst->data + i1*(dst->nb[1])); |
| 3498 | |
| 3499 | if (!src1) { |
| 3500 | src0_p += swapped ? nc : 0; |
| 3501 | src1_p += swapped ? 0 : nc; |
| 3502 | } |
| 3503 | |
| 3504 | for (int k = 0; k < nc; k++) { |
| 3505 | const float x = std::min(GGML_CPU_FP16_TO_FP32(src0_p[k]), limit); |
| 3506 | const float y = std::clamp(GGML_CPU_FP16_TO_FP32(src1_p[k]), -limit, limit); |
| 3507 | const float out_glu = x / (1.f + expf(alpha * (-x))); |
| 3508 | dst_p[k] = GGML_CPU_FP32_TO_FP16(out_glu * (y + 1.f)); |
| 3509 | } |
| 3510 | |
| 3511 | #ifndef NDEBUG |
| 3512 | for (int k = 0; k < nc; k++) { |
| 3513 | const float x = GGML_CPU_FP16_TO_FP32(dst_p[k]); |
| 3514 | GGML_UNUSED(x); |
| 3515 | assert(!isnan(x)); |
| 3516 | assert(!isinf(x)); |
| 3517 | } |
| 3518 | #endif // NDEBUG |
| 3519 | } |
| 3520 | } |
| 3521 | |
| 3522 | static void ggml_compute_forward_swiglu_oai( |
| 3523 | const ggml_compute_params * params, |
| 3524 | ggml_tensor * dst) { |
| 3525 | |
| 3526 | const ggml_tensor * src0 = dst->src[0]; |
| 3527 | |
| 3528 | switch (src0->type) { |
| 3529 | case GGML_TYPE_F32: |
| 3530 | { |
| 3531 | ggml_compute_forward_swiglu_oai_f32(params, dst); |
| 3532 | } break; |
| 3533 | case GGML_TYPE_F16: |
| 3534 | { |
| 3535 | ggml_compute_forward_swiglu_oai_f16(params, dst); |
| 3536 | } break; |
| 3537 | default: |
| 3538 | { |
| 3539 | GGML_ABORT("fatal error"); |
| 3540 | } |
| 3541 | } |
| 3542 | } |
| 3543 | |
| 3544 | // ggml_compute_forward_swiglu_clamp |
| 3545 | |
| 3546 | static void ggml_compute_forward_swiglu_clamp_f32(const ggml_compute_params * params, ggml_tensor * dst) { |
| 3547 | const ggml_tensor * src0 = dst->src[0]; |
| 3548 | const ggml_tensor * src1 = dst->src[1]; |
| 3549 | char * src0_d = (char *) src0->data; |
| 3550 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 3551 | const size_t src0_o = src0->nb[1]; |
| 3552 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 3553 | |
| 3554 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 3555 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 3556 | |
| 3557 | if (src1) { |
| 3558 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 3559 | GGML_ASSERT(src0->type == src1->type); |
| 3560 | } |
| 3561 | |
| 3562 | const int ith = params->ith; |
| 3563 | const int nth = params->nth; |
| 3564 | |
| 3565 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 3566 | const int nr = ggml_nrows(src0); |
| 3567 | |
| 3568 | GGML_ASSERT(dst->ne[0] == nc); |
| 3569 | GGML_ASSERT(ggml_nrows(dst) == nr); |
| 3570 | |
| 3571 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 3572 | const float limit = ggml_get_op_params_f32(dst, 3); |
| 3573 | |
| 3574 | const int dr = (nr + nth - 1) / nth; |
| 3575 | const int ir0 = dr * ith; |
| 3576 | const int ir1 = MIN(ir0 + dr, nr); |
| 3577 | |
| 3578 | for (int i1 = ir0; i1 < ir1; i1++) { |
| 3579 | float * src0_p = (float *) (src0_d + i1 * src0_o); |
| 3580 | float * src1_p = (float *) (src1_d + i1 * src1_o); |
| 3581 | float * dst_p = (float *) ((char *) dst->data + i1 * (dst->nb[1])); |
| 3582 | |
| 3583 | if (!src1) { |
| 3584 | src0_p += swapped ? nc : 0; |
| 3585 | src1_p += swapped ? 0 : nc; |
| 3586 | } |
| 3587 | |
| 3588 | for (int k = 0; k < nc; k++) { |
| 3589 | const float gate = std::min(src0_p[k], limit); |
| 3590 | const float up = std::clamp(src1_p[k], -limit, limit); |
| 3591 | dst_p[k] = gate / (1.f + expf(-gate)) * up; |
| 3592 | } |
| 3593 | |
| 3594 | #ifndef NDEBUG |
| 3595 | for (int k = 0; k < nc; k++) { |
| 3596 | const float x = dst_p[k]; |
| 3597 | GGML_UNUSED(x); |
| 3598 | assert(!isnan(x)); |
| 3599 | assert(!isinf(x)); |
| 3600 | } |
| 3601 | #endif // NDEBUG |
| 3602 | } |
| 3603 | } |
| 3604 | |
| 3605 | static void ggml_compute_forward_swiglu_clamp_f16(const ggml_compute_params * params, ggml_tensor * dst) { |
| 3606 | const ggml_tensor * src0 = dst->src[0]; |
| 3607 | const ggml_tensor * src1 = dst->src[1]; |
| 3608 | char * src0_d = (char *) src0->data; |
| 3609 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 3610 | const size_t src0_o = src0->nb[1]; |
| 3611 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 3612 | |
| 3613 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 3614 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 3615 | |
| 3616 | if (src1) { |
| 3617 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 3618 | GGML_ASSERT(src0->type == src1->type); |
| 3619 | } |
| 3620 | |
| 3621 | const int ith = params->ith; |
| 3622 | const int nth = params->nth; |
| 3623 | |
| 3624 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 3625 | const int nr = ggml_nrows(src0); |
| 3626 | |
| 3627 | GGML_ASSERT(dst->ne[0] == nc); |
| 3628 | GGML_ASSERT(ggml_nrows(dst) == nr); |
| 3629 | |
| 3630 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 3631 | const float limit = ggml_get_op_params_f32(dst, 3); |
| 3632 | |
| 3633 | const int dr = (nr + nth - 1) / nth; |
| 3634 | const int ir0 = dr * ith; |
| 3635 | const int ir1 = MIN(ir0 + dr, nr); |
| 3636 | |
| 3637 | for (int i1 = ir0; i1 < ir1; i1++) { |
| 3638 | ggml_fp16_t * src0_p = (ggml_fp16_t *) (src0_d + i1 * src0_o); |
| 3639 | ggml_fp16_t * src1_p = (ggml_fp16_t *) (src1_d + i1 * src1_o); |
| 3640 | ggml_fp16_t * dst_p = (ggml_fp16_t *) ((char *) dst->data + i1 * (dst->nb[1])); |
| 3641 | |
| 3642 | if (!src1) { |
| 3643 | src0_p += swapped ? nc : 0; |
| 3644 | src1_p += swapped ? 0 : nc; |
| 3645 | } |
| 3646 | |
| 3647 | for (int k = 0; k < nc; k++) { |
| 3648 | const float gate = std::min(GGML_FP16_TO_FP32(src0_p[k]), limit); |
| 3649 | const float up = std::clamp(GGML_FP16_TO_FP32(src1_p[k]), -limit, limit); |
| 3650 | dst_p[k] = GGML_FP32_TO_FP16(gate / (1.f + expf(-gate)) * up); |
| 3651 | } |
| 3652 | |
| 3653 | #ifndef NDEBUG |
| 3654 | for (int k = 0; k < nc; k++) { |
| 3655 | const float x = GGML_FP16_TO_FP32(dst_p[k]); |
| 3656 | GGML_UNUSED(x); |
| 3657 | assert(!isnan(x)); |
| 3658 | assert(!isinf(x)); |
| 3659 | } |
| 3660 | #endif // NDEBUG |
| 3661 | } |
| 3662 | } |
| 3663 | |
| 3664 | static void ggml_compute_forward_swiglu_clamp(const ggml_compute_params * params, ggml_tensor * dst) { |
| 3665 | switch (dst->src[0]->type) { |
| 3666 | case GGML_TYPE_F32: |
| 3667 | ggml_compute_forward_swiglu_clamp_f32(params, dst); |
| 3668 | break; |
| 3669 | case GGML_TYPE_F16: |
| 3670 | ggml_compute_forward_swiglu_clamp_f16(params, dst); |
| 3671 | break; |
| 3672 | default: |
| 3673 | GGML_ABORT("fatal error"); |
| 3674 | } |
| 3675 | } |
| 3676 | |
| 3677 | // ggml_compute_forward_geglu_erf |
| 3678 | |
| 3679 | static void ggml_compute_forward_geglu_erf_f32( |
| 3680 | const ggml_compute_params * params, |
| 3681 | ggml_tensor * dst) { |
| 3682 | |
| 3683 | const ggml_tensor * src0 = dst->src[0]; |
| 3684 | const ggml_tensor * src1 = dst->src[1]; |
| 3685 | char * src0_d = (char *) src0->data; |
| 3686 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 3687 | const size_t src0_o = src0->nb[1]; |
| 3688 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 3689 | |
| 3690 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 3691 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 3692 | |
| 3693 | if (src1) { |
| 3694 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 3695 | GGML_ASSERT(src0->type == src1->type); |
| 3696 | } |
| 3697 | |
| 3698 | const int ith = params->ith; |
| 3699 | const int nth = params->nth; |
| 3700 | |
| 3701 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 3702 | const int nr = ggml_nrows(src0); |
| 3703 | |
| 3704 | GGML_ASSERT(dst->ne[0] == nc); |
| 3705 | GGML_ASSERT(ggml_nrows(dst) == nr); |
| 3706 | |
| 3707 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 3708 | |
| 3709 | // rows per thread |
| 3710 | const int dr = (nr + nth - 1)/nth; |
| 3711 | |
| 3712 | // row range for this thread |
| 3713 | const int ir0 = dr*ith; |
| 3714 | const int ir1 = MIN(ir0 + dr, nr); |
| 3715 | |
| 3716 | for (int i1 = ir0; i1 < ir1; i1++) { |
| 3717 | float * src0_p = (float *) (src0_d + i1*src0_o); |
| 3718 | float * src1_p = (float *) (src1_d + i1*src1_o); |
| 3719 | |
| 3720 | if (!src1) { |
| 3721 | src0_p += swapped ? nc : 0; |
| 3722 | src1_p += swapped ? 0 : nc; |
| 3723 | } |
| 3724 | |
| 3725 | ggml_vec_geglu_erf_f32(nc, (float *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
| 3726 | |
| 3727 | #ifndef NDEBUG |
| 3728 | for (int k = 0; k < nc; k++) { |
| 3729 | const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 3730 | GGML_UNUSED(x); |
| 3731 | assert(!isnan(x)); |
| 3732 | assert(!isinf(x)); |
| 3733 | } |
| 3734 | #endif // NDEBUG |
| 3735 | } |
| 3736 | } |
| 3737 | |
| 3738 | static void ggml_compute_forward_geglu_erf_f16( |
| 3739 | const ggml_compute_params * params, |
| 3740 | ggml_tensor * dst) { |
| 3741 | |
| 3742 | const ggml_tensor * src0 = dst->src[0]; |
| 3743 | const ggml_tensor * src1 = dst->src[1]; |
| 3744 | char * src0_d = (char *) src0->data; |
| 3745 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 3746 | const size_t src0_o = src0->nb[1]; |
| 3747 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 3748 | |
| 3749 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 3750 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 3751 | |
| 3752 | if (src1) { |
| 3753 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 3754 | GGML_ASSERT(src0->type == src1->type); |
| 3755 | } |
| 3756 | |
| 3757 | const int ith = params->ith; |
| 3758 | const int nth = params->nth; |
| 3759 | |
| 3760 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 3761 | const int nr = ggml_nrows(src0); |
| 3762 | |
| 3763 | GGML_ASSERT(dst->ne[0] == nc); |
| 3764 | GGML_ASSERT(ggml_nrows(dst) == nr); |
| 3765 | |
| 3766 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 3767 | |
| 3768 | // rows per thread |
| 3769 | const int dr = (nr + nth - 1)/nth; |
| 3770 | |
| 3771 | // row range for this thread |
| 3772 | const int ir0 = dr*ith; |
| 3773 | const int ir1 = MIN(ir0 + dr, nr); |
| 3774 | |
| 3775 | for (int i1 = ir0; i1 < ir1; i1++) { |
| 3776 | ggml_fp16_t * src0_p = (ggml_fp16_t *) (src0_d + i1*src0_o); |
| 3777 | ggml_fp16_t * src1_p = (ggml_fp16_t *) (src1_d + i1*src1_o); |
| 3778 | |
| 3779 | if (!src1) { |
| 3780 | src0_p += swapped ? nc : 0; |
| 3781 | src1_p += swapped ? 0 : nc; |
| 3782 | } |
| 3783 | |
| 3784 | ggml_vec_geglu_erf_f16(nc, (ggml_fp16_t *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
| 3785 | |
| 3786 | #ifndef NDEBUG |
| 3787 | for (int k = 0; k < nc; k++) { |
| 3788 | const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 3789 | const float v = GGML_FP16_TO_FP32(x); |
| 3790 | GGML_UNUSED(v); |
| 3791 | assert(!isnan(v)); |
| 3792 | assert(!isinf(v)); |
| 3793 | } |
| 3794 | #endif // NDEBUG |
| 3795 | } |
| 3796 | } |
| 3797 | |
| 3798 | static void ggml_compute_forward_geglu_erf_bf16( |
| 3799 | const ggml_compute_params * params, |
| 3800 | ggml_tensor * dst) { |
| 3801 | ggml_compute_forward_glu_bf16_rows<ggml_vec_geglu_erf_bf16>(params, dst); |
| 3802 | } |
| 3803 | |
| 3804 | static void ggml_compute_forward_geglu_erf( |
| 3805 | const ggml_compute_params * params, |
| 3806 | ggml_tensor * dst) { |
| 3807 | |
| 3808 | const ggml_tensor * src0 = dst->src[0]; |
| 3809 | |
| 3810 | switch (src0->type) { |
| 3811 | case GGML_TYPE_F32: |
| 3812 | { |
| 3813 | ggml_compute_forward_geglu_erf_f32(params, dst); |
| 3814 | } break; |
| 3815 | case GGML_TYPE_F16: |
| 3816 | { |
| 3817 | ggml_compute_forward_geglu_erf_f16(params, dst); |
| 3818 | } break; |
| 3819 | case GGML_TYPE_BF16: |
| 3820 | { |
| 3821 | ggml_compute_forward_geglu_erf_bf16(params, dst); |
| 3822 | } break; |
| 3823 | default: |
| 3824 | { |
| 3825 | GGML_ABORT("fatal error"); |
| 3826 | } |
| 3827 | } |
| 3828 | } |
| 3829 | |
| 3830 | // ggml_compute_forward_geglu_quick |
| 3831 | |
| 3832 | static void ggml_compute_forward_geglu_quick_f32( |
| 3833 | const ggml_compute_params * params, |
| 3834 | ggml_tensor * dst) { |
| 3835 | |
| 3836 | const ggml_tensor * src0 = dst->src[0]; |
| 3837 | const ggml_tensor * src1 = dst->src[1]; |
| 3838 | char * src0_d = (char *) src0->data; |
| 3839 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 3840 | const size_t src0_o = src0->nb[1]; |
| 3841 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 3842 | |
| 3843 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 3844 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 3845 | |
| 3846 | if (src1) { |
| 3847 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 3848 | GGML_ASSERT(src0->type == src1->type); |
| 3849 | } |
| 3850 | |
| 3851 | const int ith = params->ith; |
| 3852 | const int nth = params->nth; |
| 3853 | |
| 3854 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 3855 | const int nr = ggml_nrows(src0); |
| 3856 | |
| 3857 | GGML_ASSERT(dst->ne[0] == nc); |
| 3858 | GGML_ASSERT(ggml_nrows(dst) == nr); |
| 3859 | |
| 3860 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 3861 | |
| 3862 | // rows per thread |
| 3863 | const int dr = (nr + nth - 1)/nth; |
| 3864 | |
| 3865 | // row range for this thread |
| 3866 | const int ir0 = dr*ith; |
| 3867 | const int ir1 = MIN(ir0 + dr, nr); |
| 3868 | |
| 3869 | for (int i1 = ir0; i1 < ir1; i1++) { |
| 3870 | float * src0_p = (float *) (src0_d + i1*src0_o); |
| 3871 | float * src1_p = (float *) (src1_d + i1*src1_o); |
| 3872 | |
| 3873 | if (!src1) { |
| 3874 | src0_p += swapped ? nc : 0; |
| 3875 | src1_p += swapped ? 0 : nc; |
| 3876 | } |
| 3877 | |
| 3878 | ggml_vec_geglu_quick_f32(nc, (float *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
| 3879 | |
| 3880 | #ifndef NDEBUG |
| 3881 | for (int k = 0; k < nc; k++) { |
| 3882 | const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 3883 | GGML_UNUSED(x); |
| 3884 | assert(!isnan(x)); |
| 3885 | assert(!isinf(x)); |
| 3886 | } |
| 3887 | #endif // NDEBUG |
| 3888 | } |
| 3889 | } |
| 3890 | |
| 3891 | static void ggml_compute_forward_geglu_quick_f16( |
| 3892 | const ggml_compute_params * params, |
| 3893 | ggml_tensor * dst) { |
| 3894 | |
| 3895 | const ggml_tensor * src0 = dst->src[0]; |
| 3896 | const ggml_tensor * src1 = dst->src[1]; |
| 3897 | char * src0_d = (char *) src0->data; |
| 3898 | char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 3899 | const size_t src0_o = src0->nb[1]; |
| 3900 | const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 3901 | |
| 3902 | GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 3903 | GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 3904 | |
| 3905 | if (src1) { |
| 3906 | GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 3907 | GGML_ASSERT(src0->type == src1->type); |
| 3908 | } |
| 3909 | |
| 3910 | const int ith = params->ith; |
| 3911 | const int nth = params->nth; |
| 3912 | |
| 3913 | const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 3914 | const int nr = ggml_nrows(src0); |
| 3915 | |
| 3916 | GGML_ASSERT(dst->ne[0] == nc); |
| 3917 | GGML_ASSERT(ggml_nrows(dst) == nr); |
| 3918 | |
| 3919 | const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 3920 | |
| 3921 | // rows per thread |
| 3922 | const int dr = (nr + nth - 1)/nth; |
| 3923 | |
| 3924 | // row range for this thread |
| 3925 | const int ir0 = dr*ith; |
| 3926 | const int ir1 = MIN(ir0 + dr, nr); |
| 3927 | |
| 3928 | for (int i1 = ir0; i1 < ir1; i1++) { |
| 3929 | ggml_fp16_t * src0_p = (ggml_fp16_t *) (src0_d + i1*src0_o); |
| 3930 | ggml_fp16_t * src1_p = (ggml_fp16_t *) (src1_d + i1*src1_o); |
| 3931 | |
| 3932 | if (!src1) { |
| 3933 | src0_p += swapped ? nc : 0; |
| 3934 | src1_p += swapped ? 0 : nc; |
| 3935 | } |
| 3936 | |
| 3937 | ggml_vec_geglu_quick_f16(nc, (ggml_fp16_t *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
| 3938 | |
| 3939 | #ifndef NDEBUG |
| 3940 | for (int k = 0; k < nc; k++) { |
| 3941 | const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 3942 | const float v = GGML_FP16_TO_FP32(x); |
| 3943 | GGML_UNUSED(v); |
| 3944 | assert(!isnan(v)); |
| 3945 | assert(!isinf(v)); |
| 3946 | } |
| 3947 | #endif // NDEBUG |
| 3948 | } |
| 3949 | } |
| 3950 | |
| 3951 | static void ggml_compute_forward_geglu_quick_bf16( |
| 3952 | const ggml_compute_params * params, |
| 3953 | ggml_tensor * dst) { |
| 3954 | ggml_compute_forward_glu_bf16_rows<ggml_vec_geglu_quick_bf16>(params, dst); |
| 3955 | } |
| 3956 | |
| 3957 | static void ggml_compute_forward_geglu_quick( |
| 3958 | const ggml_compute_params * params, |
| 3959 | ggml_tensor * dst) { |
| 3960 | |
| 3961 | const ggml_tensor * src0 = dst->src[0]; |
| 3962 | |
| 3963 | switch (src0->type) { |
| 3964 | case GGML_TYPE_F32: |
| 3965 | { |
| 3966 | ggml_compute_forward_geglu_quick_f32(params, dst); |
| 3967 | } break; |
| 3968 | case GGML_TYPE_F16: |
| 3969 | { |
| 3970 | ggml_compute_forward_geglu_quick_f16(params, dst); |
| 3971 | } break; |
| 3972 | case GGML_TYPE_BF16: |
| 3973 | { |
| 3974 | ggml_compute_forward_geglu_quick_bf16(params, dst); |
| 3975 | } break; |
| 3976 | default: |
| 3977 | { |
| 3978 | GGML_ABORT("fatal error"); |
| 3979 | } |
| 3980 | } |
| 3981 | } |
| 3982 | |
| 3983 | // ggml_compute_forward_norm |
| 3984 | |
| 3985 | static void ggml_compute_forward_norm_f32( |
| 3986 | const ggml_compute_params * params, |
| 3987 | ggml_tensor * dst) { |
| 3988 | |
| 3989 | const ggml_tensor * src0 = dst->src[0]; |
| 3990 | |
| 3991 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 3992 | |
| 3993 | const int ith = params->ith; |
| 3994 | const int nth = params->nth; |
| 3995 | |
| 3996 | GGML_TENSOR_UNARY_OP_LOCALS |
| 3997 | |
| 3998 | float eps; |
| 3999 | memcpy(&eps, dst->op_params, sizeof(float)); |
| 4000 | |
| 4001 | GGML_ASSERT(eps >= 0.0f); |
| 4002 | |
| 4003 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 4004 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 4005 | for (int64_t i01 = ith; i01 < ne01; i01 += nth) { |
| 4006 | const char * x = (const char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03; |
| 4007 | char * y = (char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3; |
| 4008 | |
| 4009 | if (nb00 == sizeof(float) && nb0 == sizeof(float)) { |
| 4010 | const float * xf = (const float *) x; |
| 4011 | |
| 4012 | float sum = 0.0; |
| 4013 | ggml_vec_sum_f32(ne00, &sum, xf); |
| 4014 | float mean = sum/ne00; |
| 4015 | |
| 4016 | float * yf = (float *) y; |
| 4017 | float variance = 0; |
| 4018 | |
| 4019 | #ifdef GGML_USE_ACCELERATE |
| 4020 | mean = -mean; |
| 4021 | vDSP_vsadd(xf, 1, &mean, yf, 1, ne00); |
| 4022 | vDSP_measqv(yf, 1, &variance, ne00); |
| 4023 | #else |
| 4024 | variance = ggml_vec_cvar_f32(ne00, yf, xf, mean); |
| 4025 | #endif //GGML_USE_ACCELERATE |
| 4026 | |
| 4027 | const float scale = 1.0f/sqrtf(variance + eps); |
| 4028 | ggml_vec_scale_f32(ne00, yf, scale); |
| 4029 | } else { |
| 4030 | float sum = 0.0; |
| 4031 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 4032 | sum += *(const float *) (x + i00*nb00); |
| 4033 | } |
| 4034 | const float mean = sum/ne00; |
| 4035 | |
| 4036 | float variance = 0.0f; |
| 4037 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 4038 | const float v = *(const float *) (x + i00*nb00) - mean; |
| 4039 | *(float *) (y + i00*nb0) = v; |
| 4040 | variance += v * v; |
| 4041 | } |
| 4042 | variance /= ne00; |
| 4043 | |
| 4044 | const float scale = 1.0f/sqrtf(variance + eps); |
| 4045 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 4046 | *(float *) (y + i00*nb0) *= scale; |
| 4047 | } |
| 4048 | } |
| 4049 | } |
| 4050 | } |
| 4051 | } |
| 4052 | } |
| 4053 | |
| 4054 | void ggml_compute_forward_norm( |
| 4055 | const ggml_compute_params * params, |
| 4056 | ggml_tensor * dst) { |
| 4057 | |
| 4058 | const ggml_tensor * src0 = dst->src[0]; |
| 4059 | |
| 4060 | switch (src0->type) { |
| 4061 | case GGML_TYPE_F32: |
| 4062 | { |
| 4063 | ggml_compute_forward_norm_f32(params, dst); |
| 4064 | } break; |
| 4065 | default: |
| 4066 | { |
| 4067 | GGML_ABORT("fatal error"); |
| 4068 | } |
| 4069 | } |
| 4070 | } |
| 4071 | |
| 4072 | // ggml_compute_forward_group_rms_norm |
| 4073 | |
| 4074 | // fusion kinds that can be combined with the rms_norm computation in a single pass. |
| 4075 | // extend this enum when adding new fused variants (e.g. FUSE_ADD, FUSE_MUL_ADD, ...). |
| 4076 | enum ggml_rms_norm_fuse_op { |
| 4077 | GGML_RMS_NORM_FUSE_OP_NONE, |
| 4078 | GGML_RMS_NORM_FUSE_OP_MUL, |
| 4079 | }; |
| 4080 | |
| 4081 | template <ggml_rms_norm_fuse_op FUSE_OP> |
| 4082 | static void ggml_compute_forward_rms_norm_f32( |
| 4083 | const ggml_compute_params * params, |
| 4084 | ggml_tensor * dst_rms_norm, |
| 4085 | ggml_tensor * dst_fused = nullptr) { |
| 4086 | |
| 4087 | const ggml_tensor * src0 = dst_rms_norm->src[0]; |
| 4088 | const ggml_tensor * src1 = nullptr; |
| 4089 | ggml_tensor * dst = dst_rms_norm; |
| 4090 | |
| 4091 | if constexpr (FUSE_OP == GGML_RMS_NORM_FUSE_OP_MUL) { |
| 4092 | src1 = (dst_fused->src[0] == dst_rms_norm) ? dst_fused->src[1] : dst_fused->src[0]; |
| 4093 | dst = dst_fused; |
| 4094 | } |
| 4095 | |
| 4096 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 4097 | |
| 4098 | GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 4099 | |
| 4100 | const int ith = params->ith; |
| 4101 | const int nth = params->nth; |
| 4102 | |
| 4103 | GGML_TENSOR_BINARY_OP_LOCALS |
| 4104 | |
| 4105 | float eps; |
| 4106 | memcpy(&eps, dst_rms_norm->op_params, sizeof(float)); |
| 4107 | GGML_ASSERT(eps >= 0.0f); |
| 4108 | |
| 4109 | // TODO: optimize |
| 4110 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 4111 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 4112 | for (int64_t i01 = ith; i01 < ne01; i01 += nth) { |
| 4113 | const float * x = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03); |
| 4114 | |
| 4115 | ggml_float sum = 0.0; |
| 4116 | // worth switching to explicit SIMD? |
| 4117 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 4118 | sum += (ggml_float)(x[i00] * x[i00]); |
| 4119 | } |
| 4120 | |
| 4121 | const float mean = sum/ne00; |
| 4122 | const float scale = 1.0f/sqrtf(mean + eps); |
| 4123 | |
| 4124 | // if you hit this, likely you got an inf somewhere earlier |
| 4125 | assert(scale > 0.0f); |
| 4126 | |
| 4127 | float * y = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3); |
| 4128 | |
| 4129 | if constexpr (FUSE_OP == GGML_RMS_NORM_FUSE_OP_MUL) { |
| 4130 | const int64_t i11 = i01 % ne11; |
| 4131 | const int64_t i12 = i02 % ne12; |
| 4132 | const int64_t i13 = i03 % ne13; |
| 4133 | const float * w = (float *) ((char *) src1->data + i11*nb11 + i12*nb12 + i13*nb13); |
| 4134 | |
| 4135 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 4136 | y[i00] = x[i00] * scale * w[i00]; |
| 4137 | } |
| 4138 | } else { |
| 4139 | memcpy(y, x, ne00 * sizeof(float)); |
| 4140 | ggml_vec_scale_f32(ne00, y, scale); |
| 4141 | } |
| 4142 | } |
| 4143 | } |
| 4144 | } |
| 4145 | } |
| 4146 | |
| 4147 | void ggml_compute_forward_rms_norm( |
| 4148 | const ggml_compute_params * params, |
| 4149 | ggml_tensor * dst) { |
| 4150 | |
| 4151 | const ggml_tensor * src0 = dst->src[0]; |
| 4152 | |
| 4153 | switch (src0->type) { |
| 4154 | case GGML_TYPE_F32: |
| 4155 | { |
| 4156 | ggml_compute_forward_rms_norm_f32<GGML_RMS_NORM_FUSE_OP_NONE>(params, dst); |
| 4157 | } break; |
| 4158 | default: |
| 4159 | { |
| 4160 | GGML_ABORT("fatal error"); |
| 4161 | } |
| 4162 | } |
| 4163 | } |
| 4164 | |
| 4165 | // Fused RMS_NORM + MUL: computes dst = rms_norm(src0) * src1 in a single pass. |
| 4166 | // This avoids materializing the intermediate rms_norm result in memory. |
| 4167 | void ggml_compute_forward_rms_norm_mul_fused( |
| 4168 | const ggml_compute_params * params, |
| 4169 | ggml_tensor * dst_rms_norm, |
| 4170 | ggml_tensor * dst_mul) { |
| 4171 | |
| 4172 | GGML_ASSERT(dst_mul != nullptr); |
| 4173 | GGML_ASSERT(dst_mul->src[0] == dst_rms_norm || dst_mul->src[1] == dst_rms_norm); |
| 4174 | |
| 4175 | const ggml_tensor * src0 = dst_rms_norm->src[0]; |
| 4176 | |
| 4177 | switch (src0->type) { |
| 4178 | case GGML_TYPE_F32: |
| 4179 | { |
| 4180 | ggml_compute_forward_rms_norm_f32<GGML_RMS_NORM_FUSE_OP_MUL>(params, dst_rms_norm, dst_mul); |
| 4181 | } break; |
| 4182 | default: |
| 4183 | { |
| 4184 | GGML_ABORT("fatal error"); |
| 4185 | } |
| 4186 | } |
| 4187 | } |
| 4188 | |
| 4189 | static void ggml_compute_forward_rms_norm_back_f32( |
| 4190 | const ggml_compute_params * params, |
| 4191 | ggml_tensor * dst) { |
| 4192 | |
| 4193 | const ggml_tensor * src0 = dst->src[0]; // gradients from forward pass output |
| 4194 | const ggml_tensor * src1 = dst->src[1]; // src1 from forward pass |
| 4195 | |
| 4196 | GGML_ASSERT(ggml_are_same_shape(src0, dst) && ggml_are_same_shape(src0, src1)); |
| 4197 | |
| 4198 | GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 4199 | GGML_ASSERT(src1->nb[0] == sizeof(float)); |
| 4200 | |
| 4201 | const int ith = params->ith; |
| 4202 | const int nth = params->nth; |
| 4203 | |
| 4204 | GGML_TENSOR_BINARY_OP_LOCALS |
| 4205 | |
| 4206 | float eps; |
| 4207 | memcpy(&eps, dst->op_params, sizeof(float)); |
| 4208 | |
| 4209 | // TODO: optimize |
| 4210 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 4211 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 4212 | for (int64_t i01 = ith; i01 < ne01; i01 += nth) { |
| 4213 | // src1 is same shape as src0 => same indices |
| 4214 | const int64_t i11 = i01; |
| 4215 | const int64_t i12 = i02; |
| 4216 | const int64_t i13 = i03; |
| 4217 | |
| 4218 | const float * dz = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03); |
| 4219 | const float * x = (float *) ((char *) src1->data + i11*nb11 + i12*nb12 + i13*nb13); |
| 4220 | |
| 4221 | ggml_float sum_xx = 0.0; |
| 4222 | ggml_float sum_xdz = 0.0; |
| 4223 | |
| 4224 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 4225 | sum_xx += (ggml_float)(x[i00] * x[i00]); |
| 4226 | sum_xdz += (ggml_float)(x[i00] * dz[i00]); |
| 4227 | } |
| 4228 | |
| 4229 | //const float mean = (float)(sum_xx)/ne00; |
| 4230 | const float mean_eps = (float)(sum_xx)/ne00 + eps; |
| 4231 | const float sum_eps = (float)(sum_xx) + eps*ne00; |
| 4232 | //const float mean_xdz = (float)(sum_xdz)/ne00; |
| 4233 | // we could cache rms from forward pass to improve performance. |
| 4234 | // to do this implement ggml_rms and compose ggml_rms_norm using ggml_rms. |
| 4235 | //const float rms = sqrtf(mean_eps); |
| 4236 | const float rrms = 1.0f / sqrtf(mean_eps); |
| 4237 | //const float scale = -rrms/(ne00 * mean_eps); // -1/(n*rms**3) |
| 4238 | |
| 4239 | { |
| 4240 | // z = rms_norm(x) |
| 4241 | // |
| 4242 | // rms_norm(src1) = |
| 4243 | // scale( |
| 4244 | // src1, |
| 4245 | // div( |
| 4246 | // 1, |
| 4247 | // sqrt( |
| 4248 | // add( |
| 4249 | // scale( |
| 4250 | // sum( |
| 4251 | // sqr( |
| 4252 | // src1)), |
| 4253 | // (1.0/N)), |
| 4254 | // eps)))); |
| 4255 | |
| 4256 | // postorder: |
| 4257 | // ## op args grad |
| 4258 | // 00 param src1 grad[#00] |
| 4259 | // 01 const 1 |
| 4260 | // 02 sqr (#00) grad[#02] |
| 4261 | // 03 sum (#02) grad[#03] |
| 4262 | // 04 const 1/N |
| 4263 | // 05 scale (#03, #04) grad[#05] |
| 4264 | // 06 const eps |
| 4265 | // 07 add (#05, #06) grad[#07] |
| 4266 | // 08 sqrt (#07) grad[#08] |
| 4267 | // 09 div (#01,#08) grad[#09] |
| 4268 | // 10 scale (#00,#09) grad[#10] |
| 4269 | // |
| 4270 | // backward pass, given grad[#10] |
| 4271 | // #10: scale |
| 4272 | // grad[#00] += scale(grad[#10],#09) |
| 4273 | // grad[#09] += sum(mul(grad[#10],#00)) |
| 4274 | // #09: div |
| 4275 | // grad[#08] += neg(mul(grad[#09], div(#09,#08))) |
| 4276 | // #08: sqrt |
| 4277 | // grad[#07] += mul(grad[#08], div(0.5, #08)) |
| 4278 | // #07: add |
| 4279 | // grad[#05] += grad[#07] |
| 4280 | // #05: scale |
| 4281 | // grad[#03] += scale(grad[#05],#04) |
| 4282 | // #03: sum |
| 4283 | // grad[#02] += repeat(grad[#03], #02) |
| 4284 | // #02: |
| 4285 | // grad[#00] += scale(mul(#00, grad[#02]), 2.0) |
| 4286 | // |
| 4287 | // substitute and simplify: |
| 4288 | // grad[#00] = scale(grad(#10), #09) + scale(mul(#00, grad[#02]), 2.0) |
| 4289 | // grad[#02] = repeat(grad[#03], #02) |
| 4290 | // grad[#02] = repeat(scale(grad[#05],#04), #02) |
| 4291 | // grad[#02] = repeat(scale(grad[#07],#04), #02) |
| 4292 | // grad[#02] = repeat(scale(mul(grad[#08], div(0.5, #08)),#04), #02) |
| 4293 | // grad[#02] = repeat(scale(mul(neg(mul(grad[#09], div(#09,#08))), div(0.5, #08)),#04), #02) |
| 4294 | // grad[#02] = repeat(scale(mul(neg(mul(sum(mul(grad[#10],#00)), div(#09,#08))), div(0.5, #08)),#04), #02) |
| 4295 | // grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(#09,#08) * div(0.5, #08) * (1/N)), #02) |
| 4296 | // grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(div(#01,#08),#08) * div(0.5, #08) * (1/N)), #02) |
| 4297 | // grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(1,#08*#08) * div(0.5, #08) * (1/N)), #02) |
| 4298 | // grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(1,#07) * div(0.5, #08) * (1/N)), #02) |
| 4299 | // grad[#00] = scale(grad(#10), #09) + scale(mul(#00, grad[#02]), 2.0) |
| 4300 | // grad[#00] = scale(grad(#10), #09) + scale(mul(#00, repeat(-(sum(mul(grad[#10],#00)) * div(1,#07) * div(0.5, #08) * (1/N)), #02)), 2.0) |
| 4301 | // grad[#00] = scale(grad(#10), #09) + scale(scale(#00, -(sum(mul(grad[#10],#00)) * div(1,#07) * div(0.5, #08) * (1/N))), 2.0) |
| 4302 | // grad[#00] = scale(grad(#10), #09) + scale(#00, -(sum(mul(grad[#10],#00)) * div(1,#07) * div(1,#08) * (1/N))) |
| 4303 | // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(1,#07*#08) * (-1/N)) |
| 4304 | // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(1,#07*#08) * (-1/N)) |
| 4305 | // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(1,mean_eps*rms) * (-1/N)) |
| 4306 | // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(-1,rms*N*mean_eps)) |
| 4307 | // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(-1,rms*N*(sum_xx/N+eps))) |
| 4308 | // grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(-1,rms*N*sum_xx+rms*N*eps)) |
| 4309 | // grad[#00] = scale(dz, rrms) + scale(x, sum(mul(dz,x)) * div(-1,rms*N*mean_eps)) |
| 4310 | // grad[#00] = scale(dz, rrms) + scale(x, sum_xdz * div(-1,rms*N*mean_eps)) |
| 4311 | // a = b*c + d*e |
| 4312 | // a = b*c*f/f + d*e*f/f |
| 4313 | // a = (b*c*f + d*e*f)*(1/f) |
| 4314 | // a = (b*c*(1/c) + d*e*(1/c))*(1/(1/c)) |
| 4315 | // a = (b + d*e/c)*c |
| 4316 | // b = dz, c = rrms, d = x, e = sum_xdz * div(-1,rms*N*mean_eps) |
| 4317 | // a = (dz + x*sum_xdz * div(-1,rms*N*mean_eps)/rrms)*rrms |
| 4318 | // a = (dz + x*sum_xdz * div(-1,rms*N*mean_eps)*rms)*rrms |
| 4319 | // a = (dz + x*sum_xdz * div(-rms,rms*N*mean_eps))*rrms |
| 4320 | // a = (dz + x*sum_xdz * div(-1,N*mean_eps))*rrms |
| 4321 | // a = (dz + x*div(-sum_xdz,N*mean_eps))*rrms |
| 4322 | // a = (dz + x*div(-mean_xdz,mean_eps))*rrms |
| 4323 | // grad[#00] = scale(dz + scale(x, div(-mean_xdz,mean_eps)),rrms) |
| 4324 | // grad[#00] = scale(dz + scale(x, -mean_xdz/mean_eps),rrms) |
| 4325 | // dx = scale(dz + scale(x, -mean_xdz/mean_eps),rrms) |
| 4326 | } |
| 4327 | // dx = scale(dz + scale(x, -mean_xdz/mean_eps),rrms) |
| 4328 | // post-order: |
| 4329 | // dx := x |
| 4330 | // dx := scale(dx,-mean_xdz/mean_eps) |
| 4331 | // dx := add(dx, dz) |
| 4332 | // dx := scale(dx, rrms) |
| 4333 | float * dx = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3); |
| 4334 | |
| 4335 | // dx[i00] = (dz + x*(-sum_xdz/sum_eps)) * rrms |
| 4336 | // note: https://github.com/ggml-org/ggml/issues/1491 |
| 4337 | const float scale_x = (float) (-sum_xdz) / sum_eps; |
| 4338 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 4339 | dx[i00] = (dz[i00] + x[i00] * scale_x) * rrms; |
| 4340 | } |
| 4341 | } |
| 4342 | } |
| 4343 | } |
| 4344 | } |
| 4345 | |
| 4346 | void ggml_compute_forward_rms_norm_back( |
| 4347 | const ggml_compute_params * params, |
| 4348 | ggml_tensor * dst) { |
| 4349 | |
| 4350 | const ggml_tensor * src0 = dst->src[0]; |
| 4351 | |
| 4352 | switch (src0->type) { |
| 4353 | case GGML_TYPE_F32: |
| 4354 | { |
| 4355 | ggml_compute_forward_rms_norm_back_f32(params, dst); |
| 4356 | } break; |
| 4357 | default: |
| 4358 | { |
| 4359 | GGML_ABORT("fatal error"); |
| 4360 | } |
| 4361 | } |
| 4362 | } |
| 4363 | |
| 4364 | // ggml_compute_forward_group_norm |
| 4365 | |
| 4366 | static void ggml_compute_forward_group_norm_f32( |
| 4367 | const ggml_compute_params * params, |
| 4368 | ggml_tensor * dst) { |
| 4369 | |
| 4370 | const ggml_tensor * src0 = dst->src[0]; |
| 4371 | |
| 4372 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 4373 | |
| 4374 | GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 4375 | |
| 4376 | const int ith = params->ith; |
| 4377 | const int nth = params->nth; |
| 4378 | |
| 4379 | GGML_TENSOR_UNARY_OP_LOCALS |
| 4380 | |
| 4381 | // TODO: optimize |
| 4382 | |
| 4383 | float eps; |
| 4384 | memcpy(&eps, dst->op_params + 1, sizeof(float)); |
| 4385 | |
| 4386 | int n_channels = src0->ne[2]; |
| 4387 | int n_groups = dst->op_params[0]; |
| 4388 | int n_channels_per_group = (n_channels + n_groups - 1) / n_groups; |
| 4389 | for (int i = ith; i < n_groups; i += nth) { |
| 4390 | int start = i * n_channels_per_group; |
| 4391 | int end = start + n_channels_per_group; |
| 4392 | if (end > n_channels) { |
| 4393 | end = n_channels; |
| 4394 | } |
| 4395 | int step = end - start; |
| 4396 | |
| 4397 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 4398 | ggml_float sum = 0.0; |
| 4399 | for (int64_t i02 = start; i02 < end; i02++) { |
| 4400 | for (int64_t i01 = 0; i01 < ne01; i01++) { |
| 4401 | const float * x = (float *)((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03); |
| 4402 | |
| 4403 | ggml_float sumr = 0.0; |
| 4404 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 4405 | sumr += (ggml_float)x[i00]; |
| 4406 | } |
| 4407 | sum += sumr; |
| 4408 | } |
| 4409 | } |
| 4410 | const float mean = sum / (ne00 * ne01 * step); |
| 4411 | |
| 4412 | ggml_float sum2 = 0.0; |
| 4413 | for (int64_t i02 = start; i02 < end; i02++) { |
| 4414 | for (int64_t i01 = 0; i01 < ne01; i01++) { |
| 4415 | const float * x = (float *)((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03); |
| 4416 | |
| 4417 | float * y = (float *)((char *) dst->data + i01 * nb1 + i02 * nb2 + i03 * nb3); |
| 4418 | |
| 4419 | ggml_float sumr = 0.0; |
| 4420 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 4421 | float v = x[i00] - mean; |
| 4422 | y[i00] = v; |
| 4423 | sumr += (ggml_float)(v * v); |
| 4424 | } |
| 4425 | sum2 += sumr; |
| 4426 | } |
| 4427 | } |
| 4428 | const float variance = sum2 / (ne00 * ne01 * step); |
| 4429 | const float scale = 1.0f / sqrtf(variance + eps); |
| 4430 | |
| 4431 | for (int64_t i02 = start; i02 < end; i02++) { |
| 4432 | for (int64_t i01 = 0; i01 < ne01; i01++) { |
| 4433 | float * y = (float *)((char *) dst->data + i01 * nb1 + i02 * nb2 + i03 * nb3); |
| 4434 | ggml_vec_scale_f32(ne00, y, scale); |
| 4435 | } |
| 4436 | } |
| 4437 | } |
| 4438 | } |
| 4439 | } |
| 4440 | |
| 4441 | void ggml_compute_forward_group_norm( |
| 4442 | const ggml_compute_params * params, |
| 4443 | ggml_tensor * dst) { |
| 4444 | |
| 4445 | const ggml_tensor * src0 = dst->src[0]; |
| 4446 | |
| 4447 | switch (src0->type) { |
| 4448 | case GGML_TYPE_F32: |
| 4449 | { |
| 4450 | ggml_compute_forward_group_norm_f32(params, dst); |
| 4451 | } break; |
| 4452 | default: |
| 4453 | { |
| 4454 | GGML_ABORT("fatal error"); |
| 4455 | } |
| 4456 | } |
| 4457 | } |
| 4458 | |
| 4459 | // ggml_compute_forward_l2_norm |
| 4460 | |
| 4461 | static void ggml_compute_forward_l2_norm_f32( |
| 4462 | const ggml_compute_params * params, |
| 4463 | ggml_tensor * dst) { |
| 4464 | |
| 4465 | const ggml_tensor * src0 = dst->src[0]; |
| 4466 | |
| 4467 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 4468 | |
| 4469 | const int ith = params->ith; |
| 4470 | const int nth = params->nth; |
| 4471 | |
| 4472 | GGML_TENSOR_UNARY_OP_LOCALS |
| 4473 | |
| 4474 | float eps; |
| 4475 | memcpy(&eps, dst->op_params, sizeof(float)); |
| 4476 | |
| 4477 | GGML_ASSERT(eps >= 0.0f); |
| 4478 | |
| 4479 | // TODO: optimize |
| 4480 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 4481 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 4482 | for (int64_t i01 = ith; i01 < ne01; i01 += nth) { |
| 4483 | const char * x = (const char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03; |
| 4484 | |
| 4485 | ggml_float sum = 0.0; |
| 4486 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 4487 | const float xi = *(const float *) (x + i00*nb00); |
| 4488 | sum += (ggml_float)(xi * xi); |
| 4489 | } |
| 4490 | |
| 4491 | const float scale = 1.0f/fmaxf(sqrtf(sum), eps); |
| 4492 | |
| 4493 | char * y = (char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3; |
| 4494 | |
| 4495 | if (nb00 == sizeof(float) && nb0 == sizeof(float)) { |
| 4496 | memcpy(y, x, ne00 * sizeof(float)); |
| 4497 | ggml_vec_scale_f32(ne00, (float *) y, scale); |
| 4498 | } else { |
| 4499 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 4500 | const float xi = *(const float *) (x + i00*nb00); |
| 4501 | *(float *) (y + i00*nb0) = xi * scale; |
| 4502 | } |
| 4503 | } |
| 4504 | } |
| 4505 | } |
| 4506 | } |
| 4507 | } |
| 4508 | |
| 4509 | void ggml_compute_forward_l2_norm( |
| 4510 | const ggml_compute_params * params, |
| 4511 | ggml_tensor * dst) { |
| 4512 | |
| 4513 | const ggml_tensor * src0 = dst->src[0]; |
| 4514 | |
| 4515 | switch (src0->type) { |
| 4516 | case GGML_TYPE_F32: |
| 4517 | { |
| 4518 | ggml_compute_forward_l2_norm_f32(params, dst); |
| 4519 | } break; |
| 4520 | default: |
| 4521 | { |
| 4522 | GGML_ABORT("fatal error"); |
| 4523 | } |
| 4524 | } |
| 4525 | } |
| 4526 | |
| 4527 | // ggml_compute_forward_out_prod |
| 4528 | |
| 4529 | static void ggml_compute_forward_out_prod_f32( |
| 4530 | const ggml_compute_params * params, |
| 4531 | ggml_tensor * dst) { |
| 4532 | |
| 4533 | const ggml_tensor * src0 = dst->src[0]; |
| 4534 | const ggml_tensor * src1 = dst->src[1]; |
| 4535 | |
| 4536 | GGML_TENSOR_BINARY_OP_LOCALS |
| 4537 | |
| 4538 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 4539 | GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 4540 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 4541 | |
| 4542 | const int ith = params->ith; |
| 4543 | const int nth = params->nth; |
| 4544 | |
| 4545 | GGML_ASSERT(ne0 == ne00); |
| 4546 | GGML_ASSERT(ne1 == ne10); |
| 4547 | GGML_ASSERT(ne2 == ne12); |
| 4548 | GGML_ASSERT(ne3 == ne13); |
| 4549 | |
| 4550 | GGML_ASSERT(ne2 % ne02 == 0); |
| 4551 | GGML_ASSERT(ne3 % ne03 == 0); |
| 4552 | |
| 4553 | // we don't support permuted src0 or src1 |
| 4554 | GGML_ASSERT(nb00 == sizeof(float)); |
| 4555 | |
| 4556 | // dst cannot be transposed or permuted |
| 4557 | GGML_ASSERT(nb0 == sizeof(float)); |
| 4558 | // GGML_ASSERT(nb0 <= nb1); |
| 4559 | // GGML_ASSERT(nb1 <= nb2); |
| 4560 | // GGML_ASSERT(nb2 <= nb3); |
| 4561 | |
| 4562 | // nb01 >= nb00 - src0 is not transposed |
| 4563 | // compute by src0 rows |
| 4564 | |
| 4565 | if (ith == 0) { |
| 4566 | ggml_vec_set_f32(ne0*ne1*ne2*ne3, (float *)dst->data, 0); |
| 4567 | } |
| 4568 | ggml_barrier(params->threadpool); |
| 4569 | |
| 4570 | // dst[:,:,:,:] = 0 |
| 4571 | // for i2,i3: |
| 4572 | // for i1: |
| 4573 | // for i01: |
| 4574 | // for i0: |
| 4575 | // dst[i0,i1,i2,i3] += src0[i0,i01,i2,i3] * src1[i1,i01,i2,i3] |
| 4576 | |
| 4577 | // parallelize by last three dimensions |
| 4578 | |
| 4579 | // total rows in dst |
| 4580 | const int64_t nr = ne1*ne2*ne3; |
| 4581 | |
| 4582 | // rows per thread |
| 4583 | const int64_t dr = (nr + nth - 1)/nth; |
| 4584 | |
| 4585 | // row range for this thread |
| 4586 | const int64_t ir0 = dr*ith; |
| 4587 | const int64_t ir1 = MIN(ir0 + dr, nr); |
| 4588 | |
| 4589 | // block-tiling attempt |
| 4590 | const int64_t blck_0 = MAX(GGML_VEC_MAD_UNROLL, 32); |
| 4591 | const int64_t blck_1 = 16; |
| 4592 | |
| 4593 | // dps == dst per src0, used for group query attention |
| 4594 | const int64_t dps2 = ne2 / ne02; |
| 4595 | const int64_t dps3 = ne3 / ne03; |
| 4596 | |
| 4597 | for (int64_t bir = ir0; bir < ir1; bir += blck_1) { |
| 4598 | const int64_t bir1 = MIN(bir + blck_1, ir1); |
| 4599 | for (int64_t bi01 = 0; bi01 < ne01; bi01 += blck_0) { |
| 4600 | const int64_t bne01 = MIN(bi01 + blck_0, ne01); |
| 4601 | for (int64_t ir = bir; ir < bir1; ++ir) { |
| 4602 | // dst indices |
| 4603 | const int64_t i3 = ir/(ne2*ne1); |
| 4604 | const int64_t i2 = (ir - i3*ne2*ne1)/ne1; |
| 4605 | const int64_t i1 = (ir - i3*ne2*ne1 - i2*ne1); |
| 4606 | |
| 4607 | const int64_t i02 = i2 / dps2; |
| 4608 | const int64_t i03 = i3 / dps3; |
| 4609 | |
| 4610 | //const int64_t i10 = i1; |
| 4611 | const int64_t i12 = i2; |
| 4612 | const int64_t i13 = i3; |
| 4613 | |
| 4614 | #if GGML_VEC_MAD_UNROLL > 2 |
| 4615 | const int64_t bne01_unroll = bne01 - (bne01 % GGML_VEC_MAD_UNROLL); |
| 4616 | for (int64_t i01 = bi01; i01 < bne01_unroll; i01 += GGML_VEC_MAD_UNROLL) { |
| 4617 | const int64_t i11 = i01; |
| 4618 | |
| 4619 | float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03)); |
| 4620 | float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13)); |
| 4621 | float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3)); |
| 4622 | |
| 4623 | ggml_vec_mad_f32_unroll(ne0, nb01, nb11, d, s0, s1); |
| 4624 | } |
| 4625 | for (int64_t i01 = bne01_unroll; i01 < bne01; ++i01) { |
| 4626 | const int64_t i11 = i01; |
| 4627 | |
| 4628 | float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03)); |
| 4629 | float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13)); |
| 4630 | float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3)); |
| 4631 | |
| 4632 | ggml_vec_mad_f32(ne0, d, s0, *s1); |
| 4633 | } |
| 4634 | #else |
| 4635 | for (int64_t i01 = bi01; i01 < bne01; ++i01) { |
| 4636 | const int64_t i11 = i01; |
| 4637 | |
| 4638 | float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03)); |
| 4639 | float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13)); |
| 4640 | float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3)); |
| 4641 | |
| 4642 | ggml_vec_mad_f32(ne0, d, s0, *s1); |
| 4643 | } |
| 4644 | #endif |
| 4645 | } |
| 4646 | } |
| 4647 | } |
| 4648 | } |
| 4649 | |
| 4650 | static void ggml_compute_forward_out_prod_q_f32( |
| 4651 | const ggml_compute_params * params, |
| 4652 | ggml_tensor * dst) { |
| 4653 | |
| 4654 | const ggml_tensor * src0 = dst->src[0]; |
| 4655 | const ggml_tensor * src1 = dst->src[1]; |
| 4656 | |
| 4657 | GGML_TENSOR_BINARY_OP_LOCALS; |
| 4658 | |
| 4659 | const int ith = params->ith; |
| 4660 | const int nth = params->nth; |
| 4661 | |
| 4662 | const ggml_type type = src0->type; |
| 4663 | ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float; |
| 4664 | |
| 4665 | GGML_ASSERT(ne02 == ne12); |
| 4666 | GGML_ASSERT(ne03 == ne13); |
| 4667 | GGML_ASSERT(ne2 == ne12); |
| 4668 | GGML_ASSERT(ne3 == ne13); |
| 4669 | |
| 4670 | // we don't support permuted src0 dim0 |
| 4671 | GGML_ASSERT(nb00 == ggml_type_size(type)); |
| 4672 | |
| 4673 | // dst dim0 cannot be transposed or permuted |
| 4674 | GGML_ASSERT(nb0 == sizeof(float)); |
| 4675 | // GGML_ASSERT(nb0 <= nb1); |
| 4676 | // GGML_ASSERT(nb1 <= nb2); |
| 4677 | // GGML_ASSERT(nb2 <= nb3); |
| 4678 | |
| 4679 | GGML_ASSERT(ne0 == ne00); |
| 4680 | GGML_ASSERT(ne1 == ne10); |
| 4681 | GGML_ASSERT(ne2 == ne02); |
| 4682 | GGML_ASSERT(ne3 == ne03); |
| 4683 | |
| 4684 | // nb01 >= nb00 - src0 is not transposed |
| 4685 | // compute by src0 rows |
| 4686 | |
| 4687 | if (ith == 0) { |
| 4688 | ggml_vec_set_f32(ne0*ne1*ne2*ne3, (float *)dst->data, 0); |
| 4689 | } |
| 4690 | ggml_barrier(params->threadpool); |
| 4691 | |
| 4692 | // parallelize by last three dimensions |
| 4693 | |
| 4694 | // total rows in dst |
| 4695 | const int64_t nr = ne1*ne2*ne3; |
| 4696 | |
| 4697 | // rows per thread |
| 4698 | const int64_t dr = (nr + nth - 1)/nth; |
| 4699 | |
| 4700 | // row range for this thread |
| 4701 | const int64_t ir0 = dr*ith; |
| 4702 | const int64_t ir1 = MIN(ir0 + dr, nr); |
| 4703 | |
| 4704 | // dst[:,:,:,:] = 0 |
| 4705 | // for i2,i3: |
| 4706 | // for i1: |
| 4707 | // for i01: |
| 4708 | // for i0: |
| 4709 | // dst[i0,i1,i2,i3] += src0[i0,i01,i2,i3] * src1[i1,i01,i2,i3] |
| 4710 | |
| 4711 | float * wdata = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32) * ith; |
| 4712 | |
| 4713 | for (int64_t ir = ir0; ir < ir1; ++ir) { |
| 4714 | // dst indices |
| 4715 | const int64_t i3 = ir/(ne2*ne1); |
| 4716 | const int64_t i2 = (ir - i3*ne2*ne1)/ne1; |
| 4717 | const int64_t i1 = (ir - i3*ne2*ne1 - i2*ne1); |
| 4718 | |
| 4719 | const int64_t i02 = i2; |
| 4720 | const int64_t i03 = i3; |
| 4721 | |
| 4722 | //const int64_t i10 = i1; |
| 4723 | const int64_t i12 = i2; |
| 4724 | const int64_t i13 = i3; |
| 4725 | |
| 4726 | for (int64_t i01 = 0; i01 < ne01; ++i01) { |
| 4727 | const int64_t i11 = i01; |
| 4728 | |
| 4729 | float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03)); |
| 4730 | float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13)); |
| 4731 | float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3)); |
| 4732 | |
| 4733 | dequantize_row_q(s0, wdata, ne0); |
| 4734 | ggml_vec_mad_f32(ne0, d, wdata, *s1); |
| 4735 | } |
| 4736 | } |
| 4737 | } |
| 4738 | |
| 4739 | static void ggml_compute_forward_out_prod_f16_f32( |
| 4740 | const ggml_compute_params * params, |
| 4741 | ggml_tensor * dst) { |
| 4742 | |
| 4743 | const ggml_tensor * src0 = dst->src[0]; |
| 4744 | const ggml_tensor * src1 = dst->src[1]; |
| 4745 | |
| 4746 | GGML_TENSOR_BINARY_OP_LOCALS; |
| 4747 | |
| 4748 | const int ith = params->ith; |
| 4749 | const int nth = params->nth; |
| 4750 | |
| 4751 | GGML_ASSERT(src0->type == GGML_TYPE_F16); |
| 4752 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 4753 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 4754 | |
| 4755 | GGML_ASSERT(ne02 == ne12); |
| 4756 | GGML_ASSERT(ne03 == ne13); |
| 4757 | GGML_ASSERT(ne2 == ne12); |
| 4758 | GGML_ASSERT(ne3 == ne13); |
| 4759 | |
| 4760 | GGML_ASSERT(nb00 == sizeof(ggml_fp16_t)); |
| 4761 | GGML_ASSERT(nb0 == sizeof(float)); |
| 4762 | |
| 4763 | GGML_ASSERT(ne0 == ne00); |
| 4764 | GGML_ASSERT(ne1 == ne10); |
| 4765 | GGML_ASSERT(ne2 == ne02); |
| 4766 | GGML_ASSERT(ne3 == ne03); |
| 4767 | |
| 4768 | if (ith == 0) { |
| 4769 | ggml_vec_set_f32(ne0*ne1*ne2*ne3, (float *)dst->data, 0); |
| 4770 | } |
| 4771 | ggml_barrier(params->threadpool); |
| 4772 | |
| 4773 | const int64_t nr = ne1*ne2*ne3; |
| 4774 | const int64_t dr = (nr + nth - 1)/nth; |
| 4775 | const int64_t ir0 = dr*ith; |
| 4776 | const int64_t ir1 = MIN(ir0 + dr, nr); |
| 4777 | |
| 4778 | float * wdata = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32) * ith; |
| 4779 | |
| 4780 | for (int64_t ir = ir0; ir < ir1; ++ir) { |
| 4781 | const int64_t i3 = ir/(ne2*ne1); |
| 4782 | const int64_t i2 = (ir - i3*ne2*ne1)/ne1; |
| 4783 | const int64_t i1 = (ir - i3*ne2*ne1 - i2*ne1); |
| 4784 | |
| 4785 | const int64_t i02 = i2; |
| 4786 | const int64_t i03 = i3; |
| 4787 | |
| 4788 | const int64_t i12 = i2; |
| 4789 | const int64_t i13 = i3; |
| 4790 | |
| 4791 | float * d = (float *) ((char *) dst->data + (i1*nb1 + i2*nb2 + i3*nb3)); |
| 4792 | |
| 4793 | for (int64_t i01 = 0; i01 < ne01; ++i01) { |
| 4794 | const int64_t i11 = i01; |
| 4795 | ggml_fp16_t * s0 = (ggml_fp16_t *) ((char *) src0->data + (i01*nb01 + i02*nb02 + i03*nb03)); |
| 4796 | float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13)); |
| 4797 | ggml_fp16_to_fp32_row(s0, wdata, ne0); |
| 4798 | ggml_vec_mad_f32(ne0, d, wdata, *s1); |
| 4799 | } |
| 4800 | } |
| 4801 | } |
| 4802 | |
| 4803 | void ggml_compute_forward_out_prod( |
| 4804 | const ggml_compute_params * params, |
| 4805 | ggml_tensor * dst) { |
| 4806 | |
| 4807 | const ggml_tensor * src0 = dst->src[0]; |
| 4808 | |
| 4809 | switch (src0->type) { |
| 4810 | case GGML_TYPE_Q1_0: |
| 4811 | case GGML_TYPE_Q2_0: |
| 4812 | case GGML_TYPE_Q4_0: |
| 4813 | case GGML_TYPE_Q4_1: |
| 4814 | case GGML_TYPE_Q5_0: |
| 4815 | case GGML_TYPE_Q5_1: |
| 4816 | case GGML_TYPE_Q8_0: |
| 4817 | case GGML_TYPE_MXFP4: |
| 4818 | case GGML_TYPE_NVFP4: |
| 4819 | case GGML_TYPE_F8_E4M3: |
| 4820 | case GGML_TYPE_Q2_K: |
| 4821 | case GGML_TYPE_Q3_K: |
| 4822 | case GGML_TYPE_Q4_K: |
| 4823 | case GGML_TYPE_Q5_K: |
| 4824 | case GGML_TYPE_Q6_K: |
| 4825 | case GGML_TYPE_TQ1_0: |
| 4826 | case GGML_TYPE_TQ2_0: |
| 4827 | case GGML_TYPE_IQ2_XXS: |
| 4828 | case GGML_TYPE_IQ2_XS: |
| 4829 | case GGML_TYPE_IQ3_XXS: |
| 4830 | case GGML_TYPE_IQ1_S: |
| 4831 | case GGML_TYPE_IQ1_M: |
| 4832 | case GGML_TYPE_IQ4_NL: |
| 4833 | case GGML_TYPE_IQ4_XS: |
| 4834 | case GGML_TYPE_IQ3_S: |
| 4835 | case GGML_TYPE_IQ2_S: |
| 4836 | { |
| 4837 | ggml_compute_forward_out_prod_q_f32(params, dst); |
| 4838 | } break; |
| 4839 | case GGML_TYPE_F16: |
| 4840 | { |
| 4841 | ggml_compute_forward_out_prod_f16_f32(params, dst); |
| 4842 | } break; |
| 4843 | case GGML_TYPE_F32: |
| 4844 | { |
| 4845 | ggml_compute_forward_out_prod_f32(params, dst); |
| 4846 | } break; |
| 4847 | default: |
| 4848 | { |
| 4849 | GGML_ABORT("fatal error"); |
| 4850 | } |
| 4851 | } |
| 4852 | } |
| 4853 | |
| 4854 | // ggml_compute_forward_scale |
| 4855 | |
| 4856 | static void ggml_compute_forward_scale_f32( |
| 4857 | const ggml_compute_params * params, |
| 4858 | ggml_tensor * dst) { |
| 4859 | |
| 4860 | const ggml_tensor * src0 = dst->src[0]; |
| 4861 | |
| 4862 | GGML_ASSERT(ggml_is_contiguous_rows(src0)); |
| 4863 | GGML_ASSERT(ggml_is_contiguous_rows(dst)); |
| 4864 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 4865 | |
| 4866 | float s; // scale factor |
| 4867 | float b; // bias |
| 4868 | |
| 4869 | memcpy(&s, (float *) dst->op_params + 0, sizeof(float)); |
| 4870 | memcpy(&b, (float *) dst->op_params + 1, sizeof(float)); |
| 4871 | |
| 4872 | const int ith = params->ith; |
| 4873 | const int nth = params->nth; |
| 4874 | |
| 4875 | const int nc = src0->ne[0]; |
| 4876 | const int nr = ggml_nrows(src0); |
| 4877 | |
| 4878 | GGML_TENSOR_UNARY_OP_LOCALS |
| 4879 | |
| 4880 | // rows per thread |
| 4881 | const int dr = (nr + nth - 1)/nth; |
| 4882 | |
| 4883 | // row range for this thread |
| 4884 | const int ir0 = dr*ith; |
| 4885 | const int ir1 = MIN(ir0 + dr, nr); |
| 4886 | |
| 4887 | for (int i = ir0; i < ir1; i++) { |
| 4888 | const int64_t i1 = i % ne01; |
| 4889 | const int64_t i2 = (i / ne01) % ne02; |
| 4890 | const int64_t i3 = i / (ne01*ne02); |
| 4891 | |
| 4892 | float * y = (float *) ((char *) dst->data + i1*nb1 + i2*nb2 + i3*nb3); |
| 4893 | const float * x = (const float *) ((char *) src0->data + i1*nb01 + i2*nb02 + i3*nb03); |
| 4894 | |
| 4895 | if (b == 0.0f) { |
| 4896 | if (dst->data != src0->data) { |
| 4897 | // src0 is same shape as dst => same indices |
| 4898 | // TODO: add x parameter to ggml_vec_scale_f32 and remove this memcpy |
| 4899 | memcpy(y, x, nc * sizeof(float)); |
| 4900 | } |
| 4901 | ggml_vec_scale_f32(nc, y, s); |
| 4902 | } else { |
| 4903 | ggml_vec_mad1_f32(nc, y, x, s, b); |
| 4904 | } |
| 4905 | } |
| 4906 | } |
| 4907 | |
| 4908 | static void ggml_compute_forward_scale_bf16( |
| 4909 | const ggml_compute_params * params, |
| 4910 | ggml_tensor * dst) { |
| 4911 | |
| 4912 | const ggml_tensor * src0 = dst->src[0]; |
| 4913 | |
| 4914 | GGML_ASSERT(ggml_is_contiguous_rows(src0)); |
| 4915 | GGML_ASSERT(ggml_is_contiguous_rows(dst)); |
| 4916 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 4917 | |
| 4918 | float s; // scale factor |
| 4919 | float b; // bias |
| 4920 | |
| 4921 | memcpy(&s, (float *) dst->op_params + 0, sizeof(float)); |
| 4922 | memcpy(&b, (float *) dst->op_params + 1, sizeof(float)); |
| 4923 | |
| 4924 | const int ith = params->ith; |
| 4925 | const int nth = params->nth; |
| 4926 | |
| 4927 | const int nc = src0->ne[0]; |
| 4928 | const int nr = ggml_nrows(src0); |
| 4929 | |
| 4930 | GGML_TENSOR_UNARY_OP_LOCALS |
| 4931 | |
| 4932 | // rows per thread |
| 4933 | const int dr = (nr + nth - 1)/nth; |
| 4934 | |
| 4935 | // row range for this thread |
| 4936 | const int ir0 = dr*ith; |
| 4937 | const int ir1 = MIN(ir0 + dr, nr); |
| 4938 | |
| 4939 | for (int i = ir0; i < ir1; i++) { |
| 4940 | const int64_t i1 = i % ne01; |
| 4941 | const int64_t i2 = (i / ne01) % ne02; |
| 4942 | const int64_t i3 = i / (ne01*ne02); |
| 4943 | |
| 4944 | const ggml_bf16_t * x = (const ggml_bf16_t *) ((const char *) src0->data + i1*nb01 + i2*nb02 + i3*nb03); |
| 4945 | ggml_bf16_t * y = (ggml_bf16_t *) ((char *) dst->data + i1*nb1 + i2*nb2 + i3*nb3); |
| 4946 | for (int k = 0; k < nc; k++) { |
| 4947 | y[k] = GGML_FP32_TO_BF16(s*GGML_BF16_TO_FP32(x[k]) + b); |
| 4948 | } |
| 4949 | } |
| 4950 | } |
| 4951 | |
| 4952 | void ggml_compute_forward_scale( |
| 4953 | const ggml_compute_params * params, |
| 4954 | ggml_tensor * dst) { |
| 4955 | |
| 4956 | const ggml_tensor * src0 = dst->src[0]; |
| 4957 | |
| 4958 | switch (src0->type) { |
| 4959 | case GGML_TYPE_F32: |
| 4960 | { |
| 4961 | ggml_compute_forward_scale_f32(params, dst); |
| 4962 | } break; |
| 4963 | case GGML_TYPE_BF16: |
| 4964 | { |
| 4965 | ggml_compute_forward_scale_bf16(params, dst); |
| 4966 | } break; |
| 4967 | default: |
| 4968 | { |
| 4969 | GGML_ABORT("fatal error"); |
| 4970 | } |
| 4971 | } |
| 4972 | } |
| 4973 | |
| 4974 | // ggml_compute_forward_set |
| 4975 | |
| 4976 | static void ggml_compute_forward_set_f32( |
| 4977 | const ggml_compute_params * params, |
| 4978 | ggml_tensor * dst) { |
| 4979 | |
| 4980 | const ggml_tensor * src0 = dst->src[0]; |
| 4981 | const ggml_tensor * src1 = dst->src[1]; |
| 4982 | |
| 4983 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 4984 | GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0)); |
| 4985 | |
| 4986 | // view src0 and dst with these strides and data offset inbytes during set |
| 4987 | // nb0 is implicitly element_size because src0 and dst are contiguous |
| 4988 | size_t nb1 = ((int32_t *) dst->op_params)[0]; |
| 4989 | size_t nb2 = ((int32_t *) dst->op_params)[1]; |
| 4990 | size_t nb3 = ((int32_t *) dst->op_params)[2]; |
| 4991 | size_t offset = ((int32_t *) dst->op_params)[3]; |
| 4992 | bool inplace = (bool) ((int32_t *) dst->op_params)[4]; |
| 4993 | |
| 4994 | if (!inplace) { |
| 4995 | if (params->ith == 0) { |
| 4996 | // memcpy needs to be synchronized across threads to avoid race conditions. |
| 4997 | // => do it in INIT phase |
| 4998 | memcpy( |
| 4999 | ((char *) dst->data), |
| 5000 | ((char *) src0->data), |
| 5001 | ggml_nbytes(dst)); |
| 5002 | } |
| 5003 | ggml_barrier(params->threadpool); |
| 5004 | } |
| 5005 | |
| 5006 | const int ith = params->ith; |
| 5007 | const int nth = params->nth; |
| 5008 | |
| 5009 | const int nr = ggml_nrows(src1); |
| 5010 | const int nc = src1->ne[0]; |
| 5011 | |
| 5012 | GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) |
| 5013 | GGML_TENSOR_LOCALS(size_t, nb1, src1, nb) |
| 5014 | |
| 5015 | // src0 and dst as viewed during set |
| 5016 | const size_t nb0 = ggml_element_size(src0); |
| 5017 | |
| 5018 | const int im0 = (ne10 == 0 ? 0 : ne10-1); |
| 5019 | const int im1 = (ne11 == 0 ? 0 : ne11-1); |
| 5020 | const int im2 = (ne12 == 0 ? 0 : ne12-1); |
| 5021 | const int im3 = (ne13 == 0 ? 0 : ne13-1); |
| 5022 | |
| 5023 | GGML_ASSERT(offset + im0*nb0 + im1*nb1 + im2*nb2 + im3*nb3 <= ggml_nbytes(dst)); |
| 5024 | |
| 5025 | GGML_ASSERT(nb10 == sizeof(float)); |
| 5026 | |
| 5027 | // rows per thread |
| 5028 | const int dr = (nr + nth - 1)/nth; |
| 5029 | |
| 5030 | // row range for this thread |
| 5031 | const int ir0 = dr*ith; |
| 5032 | const int ir1 = MIN(ir0 + dr, nr); |
| 5033 | |
| 5034 | for (int ir = ir0; ir < ir1; ++ir) { |
| 5035 | // src0 and dst are viewed with shape of src1 and offset |
| 5036 | // => same indices |
| 5037 | const int i3 = ir/(ne12*ne11); |
| 5038 | const int i2 = (ir - i3*ne12*ne11)/ne11; |
| 5039 | const int i1 = (ir - i3*ne12*ne11 - i2*ne11); |
| 5040 | |
| 5041 | ggml_vec_cpy_f32(nc, |
| 5042 | (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset), |
| 5043 | (float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11)); |
| 5044 | } |
| 5045 | } |
| 5046 | |
| 5047 | static void ggml_compute_forward_set_i32( |
| 5048 | const ggml_compute_params * params, |
| 5049 | ggml_tensor * dst) { |
| 5050 | |
| 5051 | const ggml_tensor * src0 = dst->src[0]; |
| 5052 | const ggml_tensor * src1 = dst->src[1]; |
| 5053 | |
| 5054 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 5055 | GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0)); |
| 5056 | |
| 5057 | // view src0 and dst with these strides and data offset inbytes during set |
| 5058 | // nb0 is implicitly element_size because src0 and dst are contiguous |
| 5059 | size_t nb1 = ((int32_t *) dst->op_params)[0]; |
| 5060 | size_t nb2 = ((int32_t *) dst->op_params)[1]; |
| 5061 | size_t nb3 = ((int32_t *) dst->op_params)[2]; |
| 5062 | size_t offset = ((int32_t *) dst->op_params)[3]; |
| 5063 | bool inplace = (bool) ((int32_t *) dst->op_params)[4]; |
| 5064 | |
| 5065 | if (!inplace) { |
| 5066 | if (params->ith == 0) { |
| 5067 | // memcpy needs to be synchronized across threads to avoid race conditions. |
| 5068 | // => do it in INIT phase |
| 5069 | memcpy( |
| 5070 | ((char *) dst->data), |
| 5071 | ((char *) src0->data), |
| 5072 | ggml_nbytes(dst)); |
| 5073 | } |
| 5074 | ggml_barrier(params->threadpool); |
| 5075 | } |
| 5076 | |
| 5077 | const int ith = params->ith; |
| 5078 | const int nth = params->nth; |
| 5079 | |
| 5080 | const int nr = ggml_nrows(src1); |
| 5081 | const int nc = src1->ne[0]; |
| 5082 | |
| 5083 | GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) |
| 5084 | GGML_TENSOR_LOCALS(size_t, nb1, src1, nb) |
| 5085 | |
| 5086 | // src0 and dst as viewed during set |
| 5087 | const size_t nb0 = ggml_element_size(src0); |
| 5088 | |
| 5089 | const int im0 = (ne10 == 0 ? 0 : ne10-1); |
| 5090 | const int im1 = (ne11 == 0 ? 0 : ne11-1); |
| 5091 | const int im2 = (ne12 == 0 ? 0 : ne12-1); |
| 5092 | const int im3 = (ne13 == 0 ? 0 : ne13-1); |
| 5093 | |
| 5094 | GGML_ASSERT(offset + im0*nb0 + im1*nb1 + im2*nb2 + im3*nb3 <= ggml_nbytes(dst)); |
| 5095 | |
| 5096 | GGML_ASSERT(nb10 == sizeof(int32_t)); |
| 5097 | |
| 5098 | // rows per thread |
| 5099 | const int dr = (nr + nth - 1)/nth; |
| 5100 | |
| 5101 | // row range for this thread |
| 5102 | const int ir0 = dr*ith; |
| 5103 | const int ir1 = MIN(ir0 + dr, nr); |
| 5104 | |
| 5105 | for (int ir = ir0; ir < ir1; ++ir) { |
| 5106 | // src0 and dst are viewed with shape of src1 and offset |
| 5107 | // => same indices |
| 5108 | const int i3 = ir/(ne12*ne11); |
| 5109 | const int i2 = (ir - i3*ne12*ne11)/ne11; |
| 5110 | const int i1 = (ir - i3*ne12*ne11 - i2*ne11); |
| 5111 | |
| 5112 | ggml_vec_cpy_i32(nc, |
| 5113 | (int32_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset), |
| 5114 | (int32_t *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11)); |
| 5115 | } |
| 5116 | } |
| 5117 | |
| 5118 | void ggml_compute_forward_set( |
| 5119 | const ggml_compute_params * params, |
| 5120 | ggml_tensor * dst) { |
| 5121 | |
| 5122 | const ggml_tensor * src0 = dst->src[0]; |
| 5123 | |
| 5124 | switch (src0->type) { |
| 5125 | case GGML_TYPE_F32: |
| 5126 | { |
| 5127 | ggml_compute_forward_set_f32(params, dst); |
| 5128 | } break; |
| 5129 | case GGML_TYPE_I32: |
| 5130 | { |
| 5131 | ggml_compute_forward_set_i32(params, dst); |
| 5132 | } break; |
| 5133 | case GGML_TYPE_F16: |
| 5134 | case GGML_TYPE_BF16: |
| 5135 | case GGML_TYPE_Q1_0: |
| 5136 | case GGML_TYPE_Q2_0: |
| 5137 | case GGML_TYPE_Q4_0: |
| 5138 | case GGML_TYPE_Q4_1: |
| 5139 | case GGML_TYPE_Q5_0: |
| 5140 | case GGML_TYPE_Q5_1: |
| 5141 | case GGML_TYPE_Q8_0: |
| 5142 | case GGML_TYPE_Q8_1: |
| 5143 | case GGML_TYPE_MXFP4: |
| 5144 | case GGML_TYPE_NVFP4: |
| 5145 | case GGML_TYPE_F8_E4M3: |
| 5146 | case GGML_TYPE_Q2_K: |
| 5147 | case GGML_TYPE_Q3_K: |
| 5148 | case GGML_TYPE_Q4_K: |
| 5149 | case GGML_TYPE_Q5_K: |
| 5150 | case GGML_TYPE_Q6_K: |
| 5151 | case GGML_TYPE_TQ1_0: |
| 5152 | case GGML_TYPE_TQ2_0: |
| 5153 | case GGML_TYPE_IQ2_XXS: |
| 5154 | case GGML_TYPE_IQ2_XS: |
| 5155 | case GGML_TYPE_IQ3_XXS: |
| 5156 | case GGML_TYPE_IQ1_S: |
| 5157 | case GGML_TYPE_IQ1_M: |
| 5158 | case GGML_TYPE_IQ4_NL: |
| 5159 | case GGML_TYPE_IQ4_XS: |
| 5160 | case GGML_TYPE_IQ3_S: |
| 5161 | case GGML_TYPE_IQ2_S: |
| 5162 | default: |
| 5163 | { |
| 5164 | GGML_ABORT("fatal error"); |
| 5165 | } |
| 5166 | } |
| 5167 | } |
| 5168 | |
| 5169 | // ggml_compute_forward_cpy |
| 5170 | |
| 5171 | void ggml_compute_forward_cpy( |
| 5172 | const ggml_compute_params * params, |
| 5173 | ggml_tensor * dst) { |
| 5174 | ggml_compute_forward_dup(params, dst); |
| 5175 | } |
| 5176 | |
| 5177 | // ggml_compute_forward_cont |
| 5178 | |
| 5179 | void ggml_compute_forward_cont( |
| 5180 | const ggml_compute_params * params, |
| 5181 | ggml_tensor * dst) { |
| 5182 | ggml_compute_forward_dup(params, dst); |
| 5183 | } |
| 5184 | |
| 5185 | // ggml_compute_forward_get_rows |
| 5186 | |
| 5187 | static void ggml_compute_forward_get_rows_q( |
| 5188 | const ggml_compute_params * params, |
| 5189 | ggml_tensor * dst) { |
| 5190 | |
| 5191 | const ggml_tensor * src0 = dst->src[0]; |
| 5192 | const ggml_tensor * src1 = dst->src[1]; |
| 5193 | |
| 5194 | GGML_TENSOR_BINARY_OP_LOCALS |
| 5195 | |
| 5196 | const int64_t nc = ne00; |
| 5197 | const int64_t nr = ggml_nelements(src1); |
| 5198 | |
| 5199 | const ggml_type type = src0->type; |
| 5200 | ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float; |
| 5201 | |
| 5202 | assert(ne0 == nc); |
| 5203 | assert(ne02 == ne11); |
| 5204 | assert(nb00 == ggml_type_size(type)); |
| 5205 | assert(ggml_nrows(dst) == nr); |
| 5206 | |
| 5207 | const int ith = params->ith; |
| 5208 | const int nth = params->nth; |
| 5209 | |
| 5210 | // rows per thread |
| 5211 | const int dr = (nr + nth - 1)/nth; |
| 5212 | |
| 5213 | // row range for this thread |
| 5214 | const int ir0 = dr*ith; |
| 5215 | const int ir1 = MIN(ir0 + dr, nr); |
| 5216 | |
| 5217 | for (int64_t i = ir0; i < ir1; ++i) { |
| 5218 | const int64_t i12 = i/(ne11*ne10); |
| 5219 | const int64_t i11 = (i - i12*ne11*ne10)/ne10; |
| 5220 | const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10); |
| 5221 | const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12); |
| 5222 | |
| 5223 | GGML_ASSERT(i01 >= 0 && i01 < ne01); |
| 5224 | |
| 5225 | dequantize_row_q( |
| 5226 | (const void *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03), |
| 5227 | (float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3), nc); |
| 5228 | } |
| 5229 | } |
| 5230 | |
| 5231 | static void ggml_compute_forward_get_rows_f16( |
| 5232 | const ggml_compute_params * params, |
| 5233 | ggml_tensor * dst) { |
| 5234 | |
| 5235 | const ggml_tensor * src0 = dst->src[0]; |
| 5236 | const ggml_tensor * src1 = dst->src[1]; |
| 5237 | |
| 5238 | GGML_TENSOR_BINARY_OP_LOCALS |
| 5239 | |
| 5240 | const int64_t nc = ne00; |
| 5241 | const int64_t nr = ggml_nelements(src1); |
| 5242 | |
| 5243 | assert(ne0 == nc); |
| 5244 | assert(ne02 == ne11); |
| 5245 | assert(nb00 == sizeof(ggml_fp16_t)); |
| 5246 | assert(ggml_nrows(dst) == nr); |
| 5247 | |
| 5248 | const int ith = params->ith; |
| 5249 | const int nth = params->nth; |
| 5250 | |
| 5251 | // rows per thread |
| 5252 | const int dr = (nr + nth - 1)/nth; |
| 5253 | |
| 5254 | // row range for this thread |
| 5255 | const int ir0 = dr*ith; |
| 5256 | const int ir1 = MIN(ir0 + dr, nr); |
| 5257 | |
| 5258 | for (int64_t i = ir0; i < ir1; ++i) { |
| 5259 | const int64_t i12 = i/(ne11*ne10); |
| 5260 | const int64_t i11 = (i - i12*ne11*ne10)/ne10; |
| 5261 | const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10); |
| 5262 | const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12); |
| 5263 | |
| 5264 | GGML_ASSERT(i01 >= 0 && i01 < ne01); |
| 5265 | |
| 5266 | ggml_cpu_fp16_to_fp32( |
| 5267 | (const ggml_fp16_t*) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03), |
| 5268 | (float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3), nc); |
| 5269 | } |
| 5270 | } |
| 5271 | |
| 5272 | static void ggml_compute_forward_get_rows_bf16( |
| 5273 | const ggml_compute_params * params, |
| 5274 | ggml_tensor * dst) { |
| 5275 | |
| 5276 | const ggml_tensor * src0 = dst->src[0]; |
| 5277 | const ggml_tensor * src1 = dst->src[1]; |
| 5278 | |
| 5279 | GGML_TENSOR_BINARY_OP_LOCALS |
| 5280 | |
| 5281 | const int64_t nc = ne00; |
| 5282 | const int64_t nr = ggml_nelements(src1); |
| 5283 | |
| 5284 | assert(ne0 == nc); |
| 5285 | assert(ne02 == ne11); |
| 5286 | assert(nb00 == sizeof(ggml_bf16_t)); |
| 5287 | assert(ggml_nrows(dst) == nr); |
| 5288 | |
| 5289 | const int ith = params->ith; |
| 5290 | const int nth = params->nth; |
| 5291 | |
| 5292 | // rows per thread |
| 5293 | const int dr = (nr + nth - 1)/nth; |
| 5294 | |
| 5295 | // row range for this thread |
| 5296 | const int ir0 = dr*ith; |
| 5297 | const int ir1 = MIN(ir0 + dr, nr); |
| 5298 | |
| 5299 | for (int64_t i = ir0; i < ir1; ++i) { |
| 5300 | const int64_t i12 = i/(ne11*ne10); |
| 5301 | const int64_t i11 = (i - i12*ne11*ne10)/ne10; |
| 5302 | const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10); |
| 5303 | const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12); |
| 5304 | |
| 5305 | GGML_ASSERT(i01 >= 0 && i01 < ne01); |
| 5306 | |
| 5307 | ggml_cpu_bf16_to_fp32( |
| 5308 | (const ggml_bf16_t *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03), |
| 5309 | (float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3), nc); |
| 5310 | } |
| 5311 | } |
| 5312 | |
| 5313 | static void ggml_compute_forward_get_rows_f32( |
| 5314 | const ggml_compute_params * params, |
| 5315 | ggml_tensor * dst) { |
| 5316 | |
| 5317 | const ggml_tensor * src0 = dst->src[0]; |
| 5318 | const ggml_tensor * src1 = dst->src[1]; |
| 5319 | |
| 5320 | GGML_TENSOR_BINARY_OP_LOCALS |
| 5321 | |
| 5322 | const int64_t nc = ne00; |
| 5323 | const int64_t nr = ggml_nelements(src1); |
| 5324 | |
| 5325 | assert(ne0 == nc); |
| 5326 | assert(ne02 == ne11); |
| 5327 | assert(nb00 == sizeof(float)); |
| 5328 | assert(ggml_nrows(dst) == nr); |
| 5329 | |
| 5330 | const int ith = params->ith; |
| 5331 | const int nth = params->nth; |
| 5332 | |
| 5333 | // rows per thread |
| 5334 | const int dr = (nr + nth - 1)/nth; |
| 5335 | |
| 5336 | // row range for this thread |
| 5337 | const int ir0 = dr*ith; |
| 5338 | const int ir1 = MIN(ir0 + dr, nr); |
| 5339 | |
| 5340 | for (int64_t i = ir0; i < ir1; ++i) { |
| 5341 | const int64_t i12 = i/(ne11*ne10); |
| 5342 | const int64_t i11 = (i - i12*ne11*ne10)/ne10; |
| 5343 | const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10); |
| 5344 | const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12); |
| 5345 | |
| 5346 | GGML_ASSERT(i01 >= 0 && i01 < ne01); |
| 5347 | |
| 5348 | ggml_vec_cpy_f32(nc, |
| 5349 | (float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3), |
| 5350 | (float *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03)); |
| 5351 | } |
| 5352 | } |
| 5353 | |
| 5354 | void ggml_compute_forward_get_rows( |
| 5355 | const ggml_compute_params * params, |
| 5356 | ggml_tensor * dst) { |
| 5357 | |
| 5358 | const ggml_tensor * src0 = dst->src[0]; |
| 5359 | |
| 5360 | switch (src0->type) { |
| 5361 | case GGML_TYPE_Q1_0: |
| 5362 | case GGML_TYPE_Q2_0: |
| 5363 | case GGML_TYPE_Q4_0: |
| 5364 | case GGML_TYPE_Q4_1: |
| 5365 | case GGML_TYPE_Q5_0: |
| 5366 | case GGML_TYPE_Q5_1: |
| 5367 | case GGML_TYPE_Q8_0: |
| 5368 | case GGML_TYPE_Q8_1: |
| 5369 | case GGML_TYPE_MXFP4: |
| 5370 | case GGML_TYPE_NVFP4: |
| 5371 | case GGML_TYPE_F8_E4M3: |
| 5372 | case GGML_TYPE_Q2_K: |
| 5373 | case GGML_TYPE_Q3_K: |
| 5374 | case GGML_TYPE_Q4_K: |
| 5375 | case GGML_TYPE_Q5_K: |
| 5376 | case GGML_TYPE_Q6_K: |
| 5377 | case GGML_TYPE_TQ1_0: |
| 5378 | case GGML_TYPE_TQ2_0: |
| 5379 | case GGML_TYPE_IQ2_XXS: |
| 5380 | case GGML_TYPE_IQ2_XS: |
| 5381 | case GGML_TYPE_IQ3_XXS: |
| 5382 | case GGML_TYPE_IQ1_S: |
| 5383 | case GGML_TYPE_IQ1_M: |
| 5384 | case GGML_TYPE_IQ4_NL: |
| 5385 | case GGML_TYPE_IQ4_XS: |
| 5386 | case GGML_TYPE_IQ3_S: |
| 5387 | case GGML_TYPE_IQ2_S: |
| 5388 | { |
| 5389 | ggml_compute_forward_get_rows_q(params, dst); |
| 5390 | } break; |
| 5391 | case GGML_TYPE_F16: |
| 5392 | { |
| 5393 | ggml_compute_forward_get_rows_f16(params, dst); |
| 5394 | } break; |
| 5395 | case GGML_TYPE_BF16: |
| 5396 | { |
| 5397 | ggml_compute_forward_get_rows_bf16(params, dst); |
| 5398 | } break; |
| 5399 | case GGML_TYPE_F32: |
| 5400 | case GGML_TYPE_I32: |
| 5401 | { |
| 5402 | ggml_compute_forward_get_rows_f32(params, dst); |
| 5403 | } break; |
| 5404 | default: |
| 5405 | { |
| 5406 | GGML_ABORT("fatal error"); |
| 5407 | } |
| 5408 | } |
| 5409 | |
| 5410 | //static bool first = true; |
| 5411 | //printf("ne0 = %d, ne1 = %d, ne2 = %d\n", dst->ne[0], dst->ne[1], dst->ne[2]); |
| 5412 | //if (first) { |
| 5413 | // first = false; |
| 5414 | //} else { |
| 5415 | // for (int k = 0; k < dst->ne[1]; ++k) { |
| 5416 | // for (int j = 0; j < dst->ne[0]/16; ++j) { |
| 5417 | // for (int i = 0; i < 16; ++i) { |
| 5418 | // printf("%8.4f ", ((float *) dst->data)[k*dst->ne[0] + j*16 + i]); |
| 5419 | // } |
| 5420 | // printf("\n"); |
| 5421 | // } |
| 5422 | // printf("\n"); |
| 5423 | // } |
| 5424 | // printf("\n"); |
| 5425 | // exit(0); |
| 5426 | //} |
| 5427 | } |
| 5428 | |
| 5429 | template<typename src_t, typename idx_t> |
| 5430 | static void ggml_compute_forward_set_rows_impl( |
| 5431 | const ggml_compute_params * params, |
| 5432 | ggml_tensor * dst) { |
| 5433 | |
| 5434 | const ggml_tensor * src0 = dst->src[0]; |
| 5435 | const ggml_tensor * src1 = dst->src[1]; |
| 5436 | |
| 5437 | GGML_TENSOR_BINARY_OP_LOCALS |
| 5438 | |
| 5439 | const int64_t nc = ne00; |
| 5440 | const int64_t nr = ne01; |
| 5441 | |
| 5442 | assert(ne0 == nc); |
| 5443 | assert(ne2 == ne02); |
| 5444 | assert(ne3 == ne03); |
| 5445 | GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); |
| 5446 | assert(ne02 % ne11 == 0); |
| 5447 | assert(ne03 % ne12 == 0); |
| 5448 | |
| 5449 | const int ith = params->ith; |
| 5450 | const int nth = params->nth; |
| 5451 | |
| 5452 | // rows per thread |
| 5453 | const int64_t dr = (nr + nth - 1)/nth; |
| 5454 | |
| 5455 | // row range for this thread |
| 5456 | const int64_t ir0 = dr*ith; |
| 5457 | const int64_t ir1 = std::min(ir0 + dr, nr); |
| 5458 | |
| 5459 | const size_t rs = ggml_row_size(src0->type, nc); |
| 5460 | |
| 5461 | ggml_from_float_t const from_float = ggml_get_type_traits_cpu(dst->type)->from_float; |
| 5462 | |
| 5463 | for (int64_t i03 = 0; i03 < ne03; ++i03) { |
| 5464 | for (int64_t i02 = 0; i02 < ne02; ++i02) { |
| 5465 | for (int64_t i = ir0; i < ir1; ++i) { |
| 5466 | const int64_t i12 = i03%ne12; |
| 5467 | const int64_t i11 = i02%ne11; |
| 5468 | const int64_t i10 = i; |
| 5469 | |
| 5470 | const int64_t i1 = *(idx_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12); |
| 5471 | |
| 5472 | GGML_ASSERT(i1 >= 0 && i1 < ne1); |
| 5473 | |
| 5474 | if constexpr (std::is_same_v<src_t, float>) { |
| 5475 | from_float( |
| 5476 | (const float *) ((char *) src0->data + i*nb01 + i02*nb02 + i03*nb03), |
| 5477 | ((char *) dst->data + i1*nb1 + i02*nb2 + i03*nb3), nc); |
| 5478 | } else if constexpr (std::is_same_v<src_t, ggml_fp16_t>) { |
| 5479 | if (dst->type == GGML_TYPE_F16) { |
| 5480 | memcpy( |
| 5481 | ((char *) dst->data + i1*nb1 + i02*nb2 + i03*nb3), |
| 5482 | ((char *) src0->data + i*nb01 + i02*nb02 + i03*nb03), |
| 5483 | rs); |
| 5484 | } else { |
| 5485 | float * wdata = (float *) params->wdata + (nc + CACHE_LINE_SIZE_F32) * ith; |
| 5486 | ggml_fp16_to_fp32_row( |
| 5487 | (const ggml_fp16_t *) ((char *) src0->data + i*nb01 + i02*nb02 + i03*nb03), |
| 5488 | wdata, nc); |
| 5489 | from_float(wdata, |
| 5490 | ((char *) dst->data + i1*nb1 + i02*nb2 + i03*nb3), nc); |
| 5491 | } |
| 5492 | } else { |
| 5493 | GGML_ABORT("src0->type = %d (%s) not supported", src0->type, ggml_type_name(src0->type)); |
| 5494 | } |
| 5495 | } |
| 5496 | } |
| 5497 | } |
| 5498 | } |
| 5499 | |
| 5500 | void ggml_compute_forward_set_rows( |
| 5501 | const ggml_compute_params * params, |
| 5502 | ggml_tensor * dst) { |
| 5503 | |
| 5504 | const ggml_tensor * src0 = dst->src[0]; |
| 5505 | const ggml_tensor * src1 = dst->src[1]; |
| 5506 | |
| 5507 | switch (src0->type) { |
| 5508 | case GGML_TYPE_F32: |
| 5509 | { |
| 5510 | if (src1->type == GGML_TYPE_I64) { |
| 5511 | ggml_compute_forward_set_rows_impl<float, int64_t>(params, dst); |
| 5512 | } else if (src1->type == GGML_TYPE_I32) { |
| 5513 | ggml_compute_forward_set_rows_impl<float, int32_t>(params, dst); |
| 5514 | } else { |
| 5515 | GGML_ABORT("src1->type = %d (%s) not supported", src1->type, ggml_type_name(src1->type)); |
| 5516 | } |
| 5517 | } break; |
| 5518 | case GGML_TYPE_F16: |
| 5519 | { |
| 5520 | if (src1->type == GGML_TYPE_I64) { |
| 5521 | ggml_compute_forward_set_rows_impl<ggml_fp16_t, int64_t>(params, dst); |
| 5522 | } else if (src1->type == GGML_TYPE_I32) { |
| 5523 | ggml_compute_forward_set_rows_impl<ggml_fp16_t, int32_t>(params, dst); |
| 5524 | } else { |
| 5525 | GGML_ABORT("src1->type = %d (%s) not supported", src1->type, ggml_type_name(src1->type)); |
| 5526 | } |
| 5527 | } break; |
| 5528 | default: |
| 5529 | { |
| 5530 | GGML_ABORT("src0->type = %d (%s) not supported", src0->type, ggml_type_name(src0->type)); |
| 5531 | } |
| 5532 | } |
| 5533 | } |
| 5534 | |
| 5535 | // ggml_compute_forward_get_rows_back |
| 5536 | |
| 5537 | static void ggml_compute_forward_get_rows_back_f32_f16( |
| 5538 | const ggml_compute_params * params, |
| 5539 | ggml_tensor * dst) { |
| 5540 | |
| 5541 | const ggml_tensor * src0 = dst->src[0]; |
| 5542 | const ggml_tensor * src1 = dst->src[1]; |
| 5543 | |
| 5544 | if (params->ith != 0) { |
| 5545 | return; |
| 5546 | } |
| 5547 | |
| 5548 | GGML_ASSERT(ggml_is_contiguous(dst)); |
| 5549 | |
| 5550 | // ggml_compute_forward_dup_same_cont(params, opt0, dst); |
| 5551 | |
| 5552 | memset(dst->data, 0, ggml_nbytes(dst)); |
| 5553 | |
| 5554 | const int nc = src0->ne[0]; |
| 5555 | const int nr = ggml_nelements(src1); |
| 5556 | |
| 5557 | GGML_ASSERT( dst->ne[0] == nc); |
| 5558 | GGML_ASSERT(src0->nb[0] == sizeof(ggml_fp16_t)); |
| 5559 | |
| 5560 | for (int i = 0; i < nr; ++i) { |
| 5561 | const int r = ((int32_t *) src1->data)[i]; |
| 5562 | |
| 5563 | GGML_ASSERT(r >= 0 && r < dst->ne[1]); |
| 5564 | |
| 5565 | for (int j = 0; j < nc; ++j) { |
| 5566 | ggml_fp16_t v = ((ggml_fp16_t *) ((char *) src0->data + i*src0->nb[1]))[j]; |
| 5567 | ((float *) ((char *) dst->data + r*dst->nb[1]))[j] += GGML_CPU_FP16_TO_FP32(v); |
| 5568 | } |
| 5569 | } |
| 5570 | } |
| 5571 | |
| 5572 | static void ggml_compute_forward_get_rows_back_f32( |
| 5573 | const ggml_compute_params * params, |
| 5574 | ggml_tensor * dst) { |
| 5575 | |
| 5576 | const ggml_tensor * src0 = dst->src[0]; |
| 5577 | const ggml_tensor * src1 = dst->src[1]; |
| 5578 | |
| 5579 | if (params->ith != 0) { |
| 5580 | return; |
| 5581 | } |
| 5582 | |
| 5583 | GGML_ASSERT(ggml_is_contiguous(dst)); |
| 5584 | |
| 5585 | // ggml_compute_forward_dup_same_cont(params, opt0, dst); |
| 5586 | |
| 5587 | memset(dst->data, 0, ggml_nbytes(dst)); |
| 5588 | |
| 5589 | const int nc = src0->ne[0]; |
| 5590 | const int nr = ggml_nelements(src1); |
| 5591 | |
| 5592 | GGML_ASSERT( dst->ne[0] == nc); |
| 5593 | GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 5594 | |
| 5595 | for (int i = 0; i < nr; ++i) { |
| 5596 | const int r = ((int32_t *) src1->data)[i]; |
| 5597 | |
| 5598 | GGML_ASSERT(r >= 0 && r < dst->ne[1]); |
| 5599 | |
| 5600 | ggml_vec_add_f32(nc, |
| 5601 | (float *) ((char *) dst->data + r*dst->nb[1]), |
| 5602 | (float *) ((char *) dst->data + r*dst->nb[1]), |
| 5603 | (float *) ((char *) src0->data + i*src0->nb[1])); |
| 5604 | } |
| 5605 | } |
| 5606 | |
| 5607 | void ggml_compute_forward_get_rows_back( |
| 5608 | const ggml_compute_params * params, |
| 5609 | ggml_tensor * dst) { |
| 5610 | |
| 5611 | const ggml_tensor * src0 = dst->src[0]; |
| 5612 | |
| 5613 | switch (src0->type) { |
| 5614 | case GGML_TYPE_F16: |
| 5615 | { |
| 5616 | ggml_compute_forward_get_rows_back_f32_f16(params, dst); |
| 5617 | } break; |
| 5618 | case GGML_TYPE_F32: |
| 5619 | { |
| 5620 | ggml_compute_forward_get_rows_back_f32(params, dst); |
| 5621 | } break; |
| 5622 | default: |
| 5623 | { |
| 5624 | GGML_ABORT("fatal error"); |
| 5625 | } |
| 5626 | } |
| 5627 | |
| 5628 | //static bool first = true; |
| 5629 | //printf("ne0 = %d, ne1 = %d, ne2 = %d\n", dst->ne[0], dst->ne[1], dst->ne[2]); |
| 5630 | //if (first) { |
| 5631 | // first = false; |
| 5632 | //} else { |
| 5633 | // for (int k = 0; k < dst->ne[1]; ++k) { |
| 5634 | // for (int j = 0; j < dst->ne[0]/16; ++j) { |
| 5635 | // for (int i = 0; i < 16; ++i) { |
| 5636 | // printf("%8.4f ", ((float *) dst->data)[k*dst->ne[0] + j*16 + i]); |
| 5637 | // } |
| 5638 | // printf("\n"); |
| 5639 | // } |
| 5640 | // printf("\n"); |
| 5641 | // } |
| 5642 | // printf("\n"); |
| 5643 | // exit(0); |
| 5644 | //} |
| 5645 | } |
| 5646 | |
| 5647 | // ggml_compute_forward_diag |
| 5648 | |
| 5649 | static void ggml_compute_forward_diag_f32( |
| 5650 | const ggml_compute_params * params, |
| 5651 | ggml_tensor * dst) { |
| 5652 | |
| 5653 | const ggml_tensor * src0 = dst->src[0]; |
| 5654 | |
| 5655 | if (params->ith != 0) { |
| 5656 | return; |
| 5657 | } |
| 5658 | |
| 5659 | // TODO: handle transposed/permuted matrices |
| 5660 | |
| 5661 | GGML_TENSOR_UNARY_OP_LOCALS |
| 5662 | |
| 5663 | GGML_ASSERT(ne00 == ne0); |
| 5664 | GGML_ASSERT(ne00 == ne1); |
| 5665 | GGML_ASSERT(ne01 == 1); |
| 5666 | GGML_ASSERT(ne02 == ne2); |
| 5667 | GGML_ASSERT(ne03 == ne3); |
| 5668 | |
| 5669 | GGML_ASSERT(nb00 == sizeof(float)); |
| 5670 | GGML_ASSERT(nb0 == sizeof(float)); |
| 5671 | |
| 5672 | for (int i3 = 0; i3 < ne3; i3++) { |
| 5673 | for (int i2 = 0; i2 < ne2; i2++) { |
| 5674 | for (int i1 = 0; i1 < ne1; i1++) { |
| 5675 | float * d = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1); |
| 5676 | float * s = (float *)((char *) src0->data + i3*nb03 + i2*nb02); |
| 5677 | for (int i0 = 0; i0 < i1; i0++) { |
| 5678 | d[i0] = 0; |
| 5679 | } |
| 5680 | d[i1] = s[i1]; |
| 5681 | for (int i0 = i1+1; i0 < ne0; i0++) { |
| 5682 | d[i0] = 0; |
| 5683 | } |
| 5684 | } |
| 5685 | } |
| 5686 | } |
| 5687 | } |
| 5688 | |
| 5689 | void ggml_compute_forward_diag( |
| 5690 | const ggml_compute_params * params, |
| 5691 | ggml_tensor * dst) { |
| 5692 | |
| 5693 | const ggml_tensor * src0 = dst->src[0]; |
| 5694 | |
| 5695 | switch (src0->type) { |
| 5696 | case GGML_TYPE_F32: |
| 5697 | { |
| 5698 | ggml_compute_forward_diag_f32(params, dst); |
| 5699 | } break; |
| 5700 | default: |
| 5701 | { |
| 5702 | GGML_ABORT("fatal error"); |
| 5703 | } |
| 5704 | } |
| 5705 | } |
| 5706 | |
| 5707 | // ggml_compute_forward_diag_mask_inf |
| 5708 | |
| 5709 | static void ggml_compute_forward_diag_mask_f32( |
| 5710 | const ggml_compute_params * params, |
| 5711 | ggml_tensor * dst, |
| 5712 | const float value) { |
| 5713 | |
| 5714 | const ggml_tensor * src0 = dst->src[0]; |
| 5715 | |
| 5716 | const int ith = params->ith; |
| 5717 | const int nth = params->nth; |
| 5718 | |
| 5719 | const int n_past = ((int32_t *) dst->op_params)[0]; |
| 5720 | const bool inplace = src0->data == dst->data; |
| 5721 | |
| 5722 | GGML_ASSERT(n_past >= 0); |
| 5723 | |
| 5724 | if (!inplace) { |
| 5725 | if (ith == 0) { |
| 5726 | // memcpy needs to be synchronized across threads to avoid race conditions. |
| 5727 | // => do it in INIT phase |
| 5728 | GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0)); |
| 5729 | GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0)); |
| 5730 | memcpy( |
| 5731 | ((char *) dst->data), |
| 5732 | ((char *) src0->data), |
| 5733 | ggml_nbytes(dst)); |
| 5734 | } |
| 5735 | ggml_barrier(params->threadpool); |
| 5736 | } |
| 5737 | |
| 5738 | // TODO: handle transposed/permuted matrices |
| 5739 | |
| 5740 | const int n = ggml_nrows(src0); |
| 5741 | const int nc = src0->ne[0]; |
| 5742 | const int nr = src0->ne[1]; |
| 5743 | const int nz = n/nr; |
| 5744 | |
| 5745 | GGML_ASSERT( dst->nb[0] == sizeof(float)); |
| 5746 | GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 5747 | |
| 5748 | for (int k = 0; k < nz; k++) { |
| 5749 | for (int j = ith; j < nr; j += nth) { |
| 5750 | for (int i = n_past; i < nc; i++) { |
| 5751 | if (i > n_past + j) { |
| 5752 | *(float *)((char *) dst->data + k*dst->nb[2] + j*dst->nb[1] + i*dst->nb[0]) = value; |
| 5753 | } |
| 5754 | } |
| 5755 | } |
| 5756 | } |
| 5757 | } |
| 5758 | |
| 5759 | void ggml_compute_forward_diag_mask_inf( |
| 5760 | const ggml_compute_params * params, |
| 5761 | ggml_tensor * dst) { |
| 5762 | |
| 5763 | const ggml_tensor * src0 = dst->src[0]; |
| 5764 | |
| 5765 | switch (src0->type) { |
| 5766 | case GGML_TYPE_F32: |
| 5767 | { |
| 5768 | ggml_compute_forward_diag_mask_f32(params, dst, -INFINITY); |
| 5769 | } break; |
| 5770 | default: |
| 5771 | { |
| 5772 | GGML_ABORT("fatal error"); |
| 5773 | } |
| 5774 | } |
| 5775 | } |
| 5776 | |
| 5777 | void ggml_compute_forward_diag_mask_zero( |
| 5778 | const ggml_compute_params * params, |
| 5779 | ggml_tensor * dst) { |
| 5780 | |
| 5781 | const ggml_tensor * src0 = dst->src[0]; |
| 5782 | |
| 5783 | switch (src0->type) { |
| 5784 | case GGML_TYPE_F32: |
| 5785 | { |
| 5786 | ggml_compute_forward_diag_mask_f32(params, dst, 0); |
| 5787 | } break; |
| 5788 | default: |
| 5789 | { |
| 5790 | GGML_ABORT("fatal error"); |
| 5791 | } |
| 5792 | } |
| 5793 | } |
| 5794 | |
| 5795 | // ggml_compute_forward_soft_max |
| 5796 | |
| 5797 | static void ggml_compute_forward_soft_max_f32( |
| 5798 | const ggml_compute_params * params, |
| 5799 | ggml_tensor * dst) { |
| 5800 | |
| 5801 | const ggml_tensor * src0 = dst->src[0]; |
| 5802 | const ggml_tensor * src1 = dst->src[1]; |
| 5803 | const ggml_tensor * src2 = dst->src[2]; |
| 5804 | |
| 5805 | assert(ggml_is_contiguous(dst)); |
| 5806 | assert(ggml_are_same_shape(src0, dst)); |
| 5807 | |
| 5808 | float scale = 1.0f; |
| 5809 | float max_bias = 0.0f; |
| 5810 | |
| 5811 | memcpy(&scale, (float *) dst->op_params + 0, sizeof(float)); |
| 5812 | memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float)); |
| 5813 | |
| 5814 | const int ith = params->ith; |
| 5815 | const int nth = params->nth; |
| 5816 | |
| 5817 | GGML_TENSOR_UNARY_OP_LOCALS |
| 5818 | |
| 5819 | const int64_t nb11 = src1 ? src1->nb[1] : 1; |
| 5820 | const int64_t nb12 = src1 ? src1->nb[2] : 1; |
| 5821 | const int64_t nb13 = src1 ? src1->nb[3] : 1; |
| 5822 | |
| 5823 | const int64_t ne12 = src1 ? src1->ne[2] : 1; |
| 5824 | const int64_t ne13 = src1 ? src1->ne[3] : 1; |
| 5825 | |
| 5826 | // TODO: is this supposed to be ceil instead of floor? |
| 5827 | // https://huggingface.co/mosaicml/mpt-7b/blob/main/attention.py#L370 |
| 5828 | const uint32_t n_head = ne02; |
| 5829 | const uint32_t n_head_log2 = 1u << (uint32_t) floor(log2(n_head)); |
| 5830 | |
| 5831 | const float m0 = powf(2.0f, -(max_bias ) / n_head_log2); |
| 5832 | const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2); |
| 5833 | |
| 5834 | float * wp = (float *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith; |
| 5835 | |
| 5836 | const bool use_f16 = (src1 && src1->type == GGML_TYPE_F16); |
| 5837 | |
| 5838 | // sinks |
| 5839 | const float * sk = src2 ? (float *)((char *) src2->data) : nullptr; |
| 5840 | |
| 5841 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 5842 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 5843 | for (int64_t i01 = ith; i01 < ne01; i01 += nth) { |
| 5844 | const int64_t i11 = i01; |
| 5845 | const int64_t i12 = i02%ne12; |
| 5846 | const int64_t i13 = i03%ne13; |
| 5847 | |
| 5848 | // ALiBi |
| 5849 | const uint32_t h = i02; // head |
| 5850 | const float slope = (max_bias > 0.0f) ? h < n_head_log2 ? powf(m0, h + 1) : powf(m1, 2*(h - n_head_log2) + 1) : 1.0f; |
| 5851 | |
| 5852 | float * sp = (float *)((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03); |
| 5853 | float * dp = (float *)((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3); |
| 5854 | |
| 5855 | // broadcast the mask across rows |
| 5856 | ggml_fp16_t * mp_f16 = src1 ? (ggml_fp16_t *)((char *) src1->data + i11*nb11 + i12*nb12 + i13*nb13) : NULL; |
| 5857 | float * mp_f32 = src1 ? (float *)((char *) src1->data + i11*nb11 + i12*nb12 + i13*nb13) : NULL; |
| 5858 | |
| 5859 | ggml_vec_cpy_f32 (ne00, wp, sp); |
| 5860 | ggml_vec_scale_f32(ne00, wp, scale); |
| 5861 | if (mp_f32) { |
| 5862 | if (use_f16) { |
| 5863 | for (int i = 0; i < ne00; ++i) { |
| 5864 | wp[i] += slope*GGML_CPU_FP16_TO_FP32(mp_f16[i]); |
| 5865 | } |
| 5866 | } else { |
| 5867 | for (int i = 0; i < ne00; ++i) { |
| 5868 | wp[i] += slope*mp_f32[i]; |
| 5869 | } |
| 5870 | } |
| 5871 | } |
| 5872 | |
| 5873 | #ifndef NDEBUG |
| 5874 | for (int i = 0; i < ne00; ++i) { |
| 5875 | //printf("p[%d] = %f\n", i, p[i]); |
| 5876 | assert(!isnan(wp[i])); |
| 5877 | } |
| 5878 | #endif // NDEBUG |
| 5879 | |
| 5880 | float max = -INFINITY; |
| 5881 | ggml_vec_max_f32(ne00, &max, wp); |
| 5882 | |
| 5883 | // if we have sinks, make a correction as if they were included in the softmax |
| 5884 | if (sk) { |
| 5885 | max = MAX(max, sk[i02]); |
| 5886 | } |
| 5887 | |
| 5888 | ggml_float sum = ggml_vec_soft_max_f32(ne00, dp, wp, max); |
| 5889 | assert(sum > 0.0); |
| 5890 | |
| 5891 | if (sk) { |
| 5892 | sum += (ggml_float) expf(sk[i02] - max); |
| 5893 | } |
| 5894 | |
| 5895 | sum = 1.0/sum; |
| 5896 | ggml_vec_scale_f32(ne00, dp, sum); |
| 5897 | |
| 5898 | #ifndef NDEBUG |
| 5899 | for (int i = 0; i < ne00; ++i) { |
| 5900 | assert(!isnan(dp[i])); |
| 5901 | assert(!isinf(dp[i])); |
| 5902 | } |
| 5903 | #endif // NDEBUG |
| 5904 | } |
| 5905 | } |
| 5906 | } |
| 5907 | } |
| 5908 | |
| 5909 | void ggml_compute_forward_soft_max( |
| 5910 | const ggml_compute_params * params, |
| 5911 | ggml_tensor * dst) { |
| 5912 | |
| 5913 | const ggml_tensor * src0 = dst->src[0]; |
| 5914 | |
| 5915 | switch (src0->type) { |
| 5916 | case GGML_TYPE_F32: |
| 5917 | { |
| 5918 | ggml_compute_forward_soft_max_f32(params, dst); |
| 5919 | } break; |
| 5920 | default: |
| 5921 | { |
| 5922 | GGML_ABORT("fatal error"); |
| 5923 | } |
| 5924 | } |
| 5925 | } |
| 5926 | |
| 5927 | |
| 5928 | // ggml_compute_forward_soft_max_ext_back |
| 5929 | |
| 5930 | static void ggml_compute_forward_soft_max_ext_back_f32( |
| 5931 | const ggml_compute_params * params, |
| 5932 | ggml_tensor * dst) { |
| 5933 | |
| 5934 | const ggml_tensor * src0 = dst->src[0]; |
| 5935 | const ggml_tensor * src1 = dst->src[1]; |
| 5936 | |
| 5937 | GGML_ASSERT(ggml_is_contiguous(src0)); |
| 5938 | GGML_ASSERT(ggml_is_contiguous(src1)); |
| 5939 | GGML_ASSERT(ggml_is_contiguous(dst)); |
| 5940 | GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 5941 | GGML_ASSERT(ggml_are_same_shape(src1, dst)); |
| 5942 | |
| 5943 | float scale = 1.0f; |
| 5944 | float max_bias = 0.0f; |
| 5945 | |
| 5946 | memcpy(&scale, (const float *) dst->op_params + 0, sizeof(float)); |
| 5947 | memcpy(&max_bias, (const float *) dst->op_params + 1, sizeof(float)); |
| 5948 | |
| 5949 | GGML_ASSERT(max_bias == 0.0f); |
| 5950 | |
| 5951 | // TODO: handle transposed/permuted matrices |
| 5952 | |
| 5953 | const int ith = params->ith; |
| 5954 | const int nth = params->nth; |
| 5955 | |
| 5956 | const int nc = src0->ne[0]; |
| 5957 | const int nr = ggml_nrows(src0); |
| 5958 | |
| 5959 | // rows per thread |
| 5960 | const int dr = (nr + nth - 1)/nth; |
| 5961 | |
| 5962 | // row range for this thread |
| 5963 | const int ir0 = dr*ith; |
| 5964 | const int ir1 = MIN(ir0 + dr, nr); |
| 5965 | |
| 5966 | for (int i1 = ir0; i1 < ir1; i1++) { |
| 5967 | float *dy = (float *)((char *) src0->data + i1*src0->nb[1]); |
| 5968 | float *y = (float *)((char *) src1->data + i1*src1->nb[1]); |
| 5969 | float *dx = (float *)((char *) dst->data + i1*dst->nb[1]); |
| 5970 | |
| 5971 | #ifndef NDEBUG |
| 5972 | for (int i = 0; i < nc; ++i) { |
| 5973 | //printf("p[%d] = %f\n", i, p[i]); |
| 5974 | assert(!isnan(dy[i])); |
| 5975 | assert(!isnan(y[i])); |
| 5976 | } |
| 5977 | #endif // NDEBUG |
| 5978 | // Jii = yi - yi*yi |
| 5979 | // Jij = -yi*yj |
| 5980 | // J = diag(y)-y.T*y |
| 5981 | // dx = J * dy |
| 5982 | // dxk = sum_i(Jki * dyi) |
| 5983 | // dxk = sum_i(-yk*yi * dyi) - (-yk*yk)*dyk + (yk - yk*yk)*dyk |
| 5984 | // dxk = sum_i(-yk*yi * dyi) + yk*yk*dyk + yk*dyk - yk*yk*dyk |
| 5985 | // dxk = sum_i(-yk*yi * dyi) + yk*dyk |
| 5986 | // dxk = -yk * sum_i(yi * dyi) + yk*dyk |
| 5987 | // dxk = -yk * dot(y, dy) + yk*dyk |
| 5988 | // dxk = yk * (- dot(y, dy) + dyk) |
| 5989 | // dxk = yk * (dyk - dot(y, dy)) |
| 5990 | // |
| 5991 | // post-order: |
| 5992 | // dot_y_dy := dot(y, dy) |
| 5993 | // dx := dy |
| 5994 | // dx := dx - dot_y_dy |
| 5995 | // dx := dx * y |
| 5996 | |
| 5997 | // linear runtime, no additional memory |
| 5998 | float dot_y_dy = 0; |
| 5999 | ggml_vec_dot_f32(nc, &dot_y_dy, 0, y, 0, dy, 0, 1); |
| 6000 | for (int i = 0; i < nc; i++) { |
| 6001 | dx[i] = scale * (dy[i] - dot_y_dy) * y[i]; |
| 6002 | } |
| 6003 | |
| 6004 | #ifndef NDEBUG |
| 6005 | for (int i = 0; i < nc; ++i) { |
| 6006 | assert(!isnan(dx[i])); |
| 6007 | assert(!isinf(dx[i])); |
| 6008 | } |
| 6009 | #endif // NDEBUG |
| 6010 | } |
| 6011 | } |
| 6012 | |
| 6013 | void ggml_compute_forward_soft_max_ext_back( |
| 6014 | const ggml_compute_params * params, |
| 6015 | ggml_tensor * dst) { |
| 6016 | |
| 6017 | const ggml_tensor * src0 = dst->src[0]; |
| 6018 | |
| 6019 | switch (src0->type) { |
| 6020 | case GGML_TYPE_F32: |
| 6021 | { |
| 6022 | ggml_compute_forward_soft_max_ext_back_f32(params, dst); |
| 6023 | } break; |
| 6024 | default: |
| 6025 | { |
| 6026 | GGML_ABORT("fatal error"); |
| 6027 | } |
| 6028 | } |
| 6029 | } |
| 6030 | |
| 6031 | // ggml_compute_forward_clamp |
| 6032 | |
| 6033 | static void ggml_compute_forward_clamp_f32( |
| 6034 | const ggml_compute_params * params, |
| 6035 | ggml_tensor * dst) { |
| 6036 | |
| 6037 | const ggml_tensor * src0 = dst->src[0]; |
| 6038 | |
| 6039 | float min; |
| 6040 | float max; |
| 6041 | memcpy(&min, (float *) dst->op_params + 0, sizeof(float)); |
| 6042 | memcpy(&max, (float *) dst->op_params + 1, sizeof(float)); |
| 6043 | |
| 6044 | const int ith = params->ith; |
| 6045 | const int nth = params->nth; |
| 6046 | |
| 6047 | const int n = ggml_nrows(src0); |
| 6048 | const int nc = src0->ne[0]; |
| 6049 | |
| 6050 | GGML_TENSOR_UNARY_OP_LOCALS |
| 6051 | |
| 6052 | GGML_ASSERT( nb0 == sizeof(float)); |
| 6053 | GGML_ASSERT(nb00 == sizeof(float)); |
| 6054 | |
| 6055 | for (int j = ith; j < n; j += nth) { |
| 6056 | const int64_t i1 = j % ne01; |
| 6057 | const int64_t i2 = (j / ne01) % ne02; |
| 6058 | const int64_t i3 = j / (ne01*ne02); |
| 6059 | |
| 6060 | float * dst_ptr = (float *) ((char *) dst->data + i1*nb1 + i2*nb2 + i3*nb3); |
| 6061 | float * src0_ptr = (float *) ((char *) src0->data + i1*nb01 + i2*nb02 + i3*nb03); |
| 6062 | |
| 6063 | for (int i = 0; i < nc; i++) { |
| 6064 | dst_ptr[i] = MAX(MIN(src0_ptr[i], max), min); |
| 6065 | } |
| 6066 | } |
| 6067 | } |
| 6068 | |
| 6069 | static void ggml_compute_forward_clamp_f16( |
| 6070 | const ggml_compute_params * params, |
| 6071 | ggml_tensor * dst) { |
| 6072 | |
| 6073 | const ggml_tensor * src0 = dst->src[0]; |
| 6074 | |
| 6075 | float min; |
| 6076 | float max; |
| 6077 | memcpy(&min, (float *) dst->op_params + 0, sizeof(float)); |
| 6078 | memcpy(&max, (float *) dst->op_params + 1, sizeof(float)); |
| 6079 | |
| 6080 | const int ith = params->ith; |
| 6081 | const int nth = params->nth; |
| 6082 | |
| 6083 | const int n = ggml_nrows(src0); |
| 6084 | const int nc = src0->ne[0]; |
| 6085 | |
| 6086 | GGML_TENSOR_UNARY_OP_LOCALS |
| 6087 | |
| 6088 | GGML_ASSERT( nb0 == sizeof(ggml_fp16_t)); |
| 6089 | GGML_ASSERT(nb00 == sizeof(ggml_fp16_t)); |
| 6090 | |
| 6091 | for (int j = ith; j < n; j += nth) { |
| 6092 | const int64_t i1 = j % ne01; |
| 6093 | const int64_t i2 = (j / ne01) % ne02; |
| 6094 | const int64_t i3 = j / (ne01*ne02); |
| 6095 | |
| 6096 | ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i1*nb1 + i2*nb2 + i3*nb3); |
| 6097 | ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i1*nb01 + i2*nb02 + i3*nb03); |
| 6098 | |
| 6099 | for (int i = 0; i < nc; i++) { |
| 6100 | float v = GGML_CPU_FP16_TO_FP32(src0_ptr[i]); |
| 6101 | dst_ptr[i] = GGML_CPU_FP32_TO_FP16(MAX(MIN(v, max), min)); |
| 6102 | } |
| 6103 | } |
| 6104 | } |
| 6105 | |
| 6106 | void ggml_compute_forward_clamp( |
| 6107 | const ggml_compute_params * params, |
| 6108 | ggml_tensor * dst) { |
| 6109 | |
| 6110 | const ggml_tensor * src0 = dst->src[0]; |
| 6111 | |
| 6112 | switch (src0->type) { |
| 6113 | case GGML_TYPE_F32: |
| 6114 | { |
| 6115 | ggml_compute_forward_clamp_f32(params, dst); |
| 6116 | } break; |
| 6117 | case GGML_TYPE_F16: |
| 6118 | { |
| 6119 | ggml_compute_forward_clamp_f16(params, dst); |
| 6120 | } break; |
| 6121 | case GGML_TYPE_BF16: |
| 6122 | case GGML_TYPE_Q1_0: |
| 6123 | case GGML_TYPE_Q2_0: |
| 6124 | case GGML_TYPE_Q4_0: |
| 6125 | case GGML_TYPE_Q4_1: |
| 6126 | case GGML_TYPE_Q5_0: |
| 6127 | case GGML_TYPE_Q5_1: |
| 6128 | case GGML_TYPE_Q8_0: |
| 6129 | case GGML_TYPE_Q8_1: |
| 6130 | case GGML_TYPE_MXFP4: |
| 6131 | case GGML_TYPE_NVFP4: |
| 6132 | case GGML_TYPE_F8_E4M3: |
| 6133 | case GGML_TYPE_Q2_K: |
| 6134 | case GGML_TYPE_Q3_K: |
| 6135 | case GGML_TYPE_Q4_K: |
| 6136 | case GGML_TYPE_Q5_K: |
| 6137 | case GGML_TYPE_Q6_K: |
| 6138 | case GGML_TYPE_TQ1_0: |
| 6139 | case GGML_TYPE_TQ2_0: |
| 6140 | case GGML_TYPE_IQ2_XXS: |
| 6141 | case GGML_TYPE_IQ2_XS: |
| 6142 | case GGML_TYPE_IQ3_XXS: |
| 6143 | case GGML_TYPE_IQ1_S: |
| 6144 | case GGML_TYPE_IQ1_M: |
| 6145 | case GGML_TYPE_IQ4_NL: |
| 6146 | case GGML_TYPE_IQ4_XS: |
| 6147 | case GGML_TYPE_IQ3_S: |
| 6148 | case GGML_TYPE_IQ2_S: |
| 6149 | case GGML_TYPE_Q8_K: |
| 6150 | case GGML_TYPE_I8: |
| 6151 | case GGML_TYPE_I16: |
| 6152 | case GGML_TYPE_I32: |
| 6153 | case GGML_TYPE_I64: |
| 6154 | case GGML_TYPE_F64: |
| 6155 | case GGML_TYPE_COUNT: |
| 6156 | { |
| 6157 | GGML_ABORT("fatal error"); |
| 6158 | } |
| 6159 | } |
| 6160 | } |
| 6161 | |
| 6162 | // ggml_compute_forward_rope |
| 6163 | |
| 6164 | static float rope_yarn_ramp(const float low, const float high, const int i0) { |
| 6165 | const float y = (i0 / 2 - low) / MAX(0.001f, high - low); |
| 6166 | return 1 - MIN(1, MAX(0, y)); |
| 6167 | } |
| 6168 | |
| 6169 | // YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn |
| 6170 | // MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng. |
| 6171 | static void rope_yarn( |
| 6172 | float theta_extrap, float freq_scale, float corr_dims[2], int64_t i0, float ext_factor, float mscale, |
| 6173 | float * cos_theta, float * sin_theta) { |
| 6174 | // Get n-d rotational scaling corrected for extrapolation |
| 6175 | float theta_interp = freq_scale * theta_extrap; |
| 6176 | float theta = theta_interp; |
| 6177 | if (ext_factor != 0.0f) { |
| 6178 | float ramp_mix = rope_yarn_ramp(corr_dims[0], corr_dims[1], i0) * ext_factor; |
| 6179 | theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix; |
| 6180 | |
| 6181 | // Get n-d magnitude scaling corrected for interpolation |
| 6182 | mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale); |
| 6183 | } |
| 6184 | *cos_theta = cosf(theta) * mscale; |
| 6185 | *sin_theta = sinf(theta) * mscale; |
| 6186 | } |
| 6187 | |
| 6188 | static void ggml_rope_cache_init( |
| 6189 | float theta_base, float freq_scale, const float * freq_factors, float corr_dims[2], int64_t ne0, float ext_factor, float mscale, |
| 6190 | float * cache, float sin_sign, float theta_scale) { |
| 6191 | // ref: https://github.com/jquesnelle/yarn/blob/master/scaled_rope/LlamaYaRNScaledRotaryEmbedding.py |
| 6192 | float theta = theta_base; |
| 6193 | for (int64_t i0 = 0; i0 < ne0; i0 += 2) { |
| 6194 | const float ff = freq_factors ? freq_factors[i0/2] : 1.0f; |
| 6195 | rope_yarn( |
| 6196 | theta/ff, freq_scale, corr_dims, i0, ext_factor, mscale, &cache[i0 + 0], &cache[i0 + 1] |
| 6197 | ); |
| 6198 | cache[i0 + 1] *= sin_sign; |
| 6199 | |
| 6200 | theta *= theta_scale; |
| 6201 | } |
| 6202 | } |
| 6203 | |
| 6204 | static void ggml_mrope_cache_init( |
| 6205 | float theta_base_t, float theta_base_h, float theta_base_w, float theta_base_e, int sections[4], bool is_imrope, bool indep_sects, |
| 6206 | float freq_scale, const float * freq_factors, float corr_dims[2], int64_t ne0, float ext_factor, float mscale, |
| 6207 | float * cache, float sin_sign, float theta_scale) { |
| 6208 | // ref: https://github.com/jquesnelle/yarn/blob/master/scaled_rope/LlamaYaRNScaledRotaryEmbedding.py |
| 6209 | float theta_t = theta_base_t; |
| 6210 | float theta_h = theta_base_h; |
| 6211 | float theta_w = theta_base_w; |
| 6212 | float theta_e = theta_base_e; // extra position id for vision encoder |
| 6213 | int sect_dims = sections[0] + sections[1] + sections[2] + sections[3]; |
| 6214 | int sec_w = sections[1] + sections[0]; |
| 6215 | int sec_e = sections[2] + sec_w; |
| 6216 | GGML_ASSERT(sect_dims <= ne0); |
| 6217 | |
| 6218 | for (int64_t i0 = 0; i0 < ne0; i0 += 2) { |
| 6219 | const float ff = freq_factors ? freq_factors[i0/2] : 1.0f; |
| 6220 | |
| 6221 | int sector = (i0 / 2) % sect_dims; |
| 6222 | if (indep_sects) { |
| 6223 | // compute theta independently for each dim sections |
| 6224 | // (i.e. reset corresponding theta when `i0` go from one section to another) |
| 6225 | if (sector == 0) { |
| 6226 | theta_t = theta_base_t; |
| 6227 | } |
| 6228 | else if (sector == sections[0]) { |
| 6229 | theta_h = theta_base_h;; |
| 6230 | } |
| 6231 | else if (sector == sec_w) { |
| 6232 | theta_w = theta_base_w; |
| 6233 | } |
| 6234 | else if (sector == sec_e) { |
| 6235 | theta_e = theta_base_e; |
| 6236 | } |
| 6237 | } |
| 6238 | |
| 6239 | float theta = theta_t; |
| 6240 | if (is_imrope) { // qwen3vl apply interleaved mrope |
| 6241 | if (sector % 3 == 1 && sector < 3 * sections[1]) { |
| 6242 | theta = theta_h; |
| 6243 | } else if (sector % 3 == 2 && sector < 3 * sections[2]) { |
| 6244 | theta = theta_w; |
| 6245 | } else if (sector % 3 == 0 && sector < 3 * sections[0]) { |
| 6246 | theta = theta_t; |
| 6247 | } else { |
| 6248 | theta = theta_e; |
| 6249 | } |
| 6250 | } else { |
| 6251 | if (sector >= sections[0] && sector < sec_w) { |
| 6252 | theta = theta_h; |
| 6253 | } |
| 6254 | else if (sector >= sec_w && sector < sec_w + sections[2]) { |
| 6255 | theta = theta_w; |
| 6256 | } |
| 6257 | else if (sector >= sec_w + sections[2]) { |
| 6258 | theta = theta_e; |
| 6259 | } |
| 6260 | } |
| 6261 | |
| 6262 | rope_yarn( |
| 6263 | theta/ff, freq_scale, corr_dims, i0, ext_factor, mscale, &cache[i0 + 0], &cache[i0 + 1] |
| 6264 | ); |
| 6265 | cache[i0 + 1] *= sin_sign; |
| 6266 | |
| 6267 | theta_t *= theta_scale; |
| 6268 | theta_w *= theta_scale; |
| 6269 | theta_h *= theta_scale; |
| 6270 | theta_e *= theta_scale; |
| 6271 | } |
| 6272 | } |
| 6273 | |
| 6274 | |
| 6275 | template<typename T> |
| 6276 | static void rotate_pairs(const int64_t n, const int64_t n_offset, const float * cache, const T * src_data, T * dst_data, const int scale = 2) { |
| 6277 | for (int64_t i0 = 0; i0 < n; i0 += 2) { |
| 6278 | const int64_t ic = i0/scale; // hack for GGML_ROPE_TYPE_NORMAL, where we need ic = i0; for all other cases, ic = i0/2 |
| 6279 | |
| 6280 | const float cos_theta = cache[i0 + 0]; |
| 6281 | const float sin_theta = cache[i0 + 1]; |
| 6282 | |
| 6283 | const T * const src = src_data + ic; |
| 6284 | T * dst = dst_data + ic; |
| 6285 | |
| 6286 | const float x0 = type_conversion_table<T>::to_f32(src[0]); |
| 6287 | const float x1 = type_conversion_table<T>::to_f32(src[n_offset]); |
| 6288 | |
| 6289 | dst[0] = type_conversion_table<T>::from_f32(x0*cos_theta - x1*sin_theta); |
| 6290 | dst[n_offset] = type_conversion_table<T>::from_f32(x0*sin_theta + x1*cos_theta); |
| 6291 | } |
| 6292 | } |
| 6293 | |
| 6294 | template<typename T> //float or ggml_fp16_t |
| 6295 | static void ggml_compute_forward_rope_flt( |
| 6296 | const ggml_compute_params * params, |
| 6297 | ggml_tensor * dst, |
| 6298 | const bool forward) { |
| 6299 | |
| 6300 | const ggml_tensor * src0 = dst->src[0]; |
| 6301 | const ggml_tensor * src1 = dst->src[1]; |
| 6302 | const ggml_tensor * src2 = dst->src[2]; |
| 6303 | |
| 6304 | GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); |
| 6305 | GGML_ASSERT(src1->type == GGML_TYPE_I32); |
| 6306 | |
| 6307 | float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow; |
| 6308 | int sections[4]; |
| 6309 | |
| 6310 | //const int n_past = ((int32_t *) dst->op_params)[0]; |
| 6311 | const int n_dims = ((int32_t *) dst->op_params)[1]; |
| 6312 | const int mode = ((int32_t *) dst->op_params)[2]; |
| 6313 | //const int n_ctx = ((int32_t *) dst->op_params)[3]; |
| 6314 | const int n_ctx_orig = ((int32_t *) dst->op_params)[4]; |
| 6315 | |
| 6316 | memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float)); |
| 6317 | memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float)); |
| 6318 | memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float)); |
| 6319 | memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float)); |
| 6320 | memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float)); |
| 6321 | memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float)); |
| 6322 | memcpy(§ions, (int32_t *) dst->op_params + 11, sizeof(int)*4); |
| 6323 | |
| 6324 | const int n_offs = ((int32_t *) dst->op_params)[15]; |
| 6325 | |
| 6326 | GGML_TENSOR_UNARY_OP_LOCALS |
| 6327 | |
| 6328 | //printf("ne0: %d, ne1: %d, ne2: %d, ne3: %d\n", ne0, ne1, ne2, ne3); |
| 6329 | //printf("n_past = %d, ne2 = %d\n", n_past, ne2); |
| 6330 | |
| 6331 | GGML_ASSERT(nb0 == nb00); |
| 6332 | GGML_ASSERT(nb0 == sizeof(T)); |
| 6333 | |
| 6334 | const int ith = params->ith; |
| 6335 | const int nth = params->nth; |
| 6336 | |
| 6337 | const int nr = ggml_nrows(dst); |
| 6338 | |
| 6339 | GGML_ASSERT(n_dims <= ne0); |
| 6340 | GGML_ASSERT(n_dims % 2 == 0); |
| 6341 | |
| 6342 | GGML_ASSERT(n_offs >= 0); |
| 6343 | GGML_ASSERT(n_offs % 2 == 0); |
| 6344 | GGML_ASSERT(n_offs + n_dims <= ne0); |
| 6345 | |
| 6346 | // rows per thread |
| 6347 | const int dr = (nr + nth - 1)/nth; |
| 6348 | |
| 6349 | // row range for this thread |
| 6350 | const int ir0 = dr*ith; |
| 6351 | const int ir1 = MIN(ir0 + dr, nr); |
| 6352 | |
| 6353 | // row index used to determine which thread to use |
| 6354 | int ir = 0; |
| 6355 | |
| 6356 | const float theta_scale = powf(freq_base, -2.0f/n_dims); |
| 6357 | |
| 6358 | float corr_dims[2]; |
| 6359 | ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims); |
| 6360 | |
| 6361 | const bool is_imrope = mode == GGML_ROPE_TYPE_IMROPE; // qwen3vl apply interleaved mrope |
| 6362 | const bool mrope_used = mode & GGML_ROPE_TYPE_MROPE; // ggml_rope_multi, note: also true for vision (24 & 8 == true) and for imrope |
| 6363 | const bool is_vision = mode == GGML_ROPE_TYPE_VISION; |
| 6364 | |
| 6365 | if (mrope_used) { |
| 6366 | GGML_ASSERT(sections[0] > 0 || sections[1] > 0 || sections[2] > 0); |
| 6367 | } |
| 6368 | |
| 6369 | if (is_vision) { |
| 6370 | GGML_ASSERT(n_dims == ne0/2); |
| 6371 | GGML_ASSERT(n_offs == 0); |
| 6372 | } |
| 6373 | |
| 6374 | const float * freq_factors = NULL; |
| 6375 | if (src2 != NULL) { |
| 6376 | GGML_ASSERT(src2->type == GGML_TYPE_F32); |
| 6377 | GGML_ASSERT(src2->ne[0] >= n_dims / 2); |
| 6378 | freq_factors = (const float *) src2->data; |
| 6379 | } |
| 6380 | |
| 6381 | // backward process uses inverse rotation by cos and sin. |
| 6382 | // cos and sin build a rotation matrix, where the inverse is the transpose. |
| 6383 | // this essentially just switches the sign of sin. |
| 6384 | const float sin_sign = forward ? 1.0f : -1.0f; |
| 6385 | |
| 6386 | const int32_t * pos = (const int32_t *) src1->data; |
| 6387 | |
| 6388 | int64_t last_i2 = -1; |
| 6389 | |
| 6390 | for (int64_t i3 = 0; i3 < ne3; i3++) { // batch |
| 6391 | for (int64_t i2 = 0; i2 < ne2; i2++) { // seq-len |
| 6392 | for (int64_t i1 = 0; i1 < ne1; i1++) { // attn-heads |
| 6393 | if (ir++ < ir0) continue; // skip rows mapped to other threads |
| 6394 | if (ir > ir1) break; |
| 6395 | |
| 6396 | float * cache = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32)*ith; |
| 6397 | if (last_i2 != i2) { |
| 6398 | if (!mrope_used) { |
| 6399 | const int64_t p = pos[i2]; |
| 6400 | ggml_rope_cache_init(p, freq_scale, freq_factors, corr_dims, ne0, ext_factor, attn_factor, cache, sin_sign, theta_scale); |
| 6401 | } |
| 6402 | else { |
| 6403 | const int64_t p_t = pos[i2]; |
| 6404 | const int64_t p_h = pos[i2 + ne2]; |
| 6405 | const int64_t p_w = pos[i2 + ne2 * 2]; |
| 6406 | const int64_t p_e = pos[i2 + ne2 * 3]; |
| 6407 | ggml_mrope_cache_init( |
| 6408 | p_t, p_h, p_w, p_e, sections, is_imrope, is_vision, |
| 6409 | freq_scale, freq_factors, corr_dims, ne0, ext_factor, attn_factor, cache, sin_sign, theta_scale); |
| 6410 | } |
| 6411 | |
| 6412 | last_i2 = i2; |
| 6413 | } |
| 6414 | |
| 6415 | T * src = (T *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01); |
| 6416 | T * dst_data = (T *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1); |
| 6417 | |
| 6418 | switch (mode) { |
| 6419 | case GGML_ROPE_TYPE_NORMAL: |
| 6420 | rotate_pairs<T>(n_dims, 1, cache, src + n_offs, dst_data + n_offs, 1); |
| 6421 | break; |
| 6422 | case GGML_ROPE_TYPE_NEOX: |
| 6423 | case GGML_ROPE_TYPE_MROPE: |
| 6424 | case GGML_ROPE_TYPE_IMROPE: |
| 6425 | rotate_pairs<T>(n_dims, n_dims/2, cache, src + n_offs, dst_data + n_offs); |
| 6426 | break; |
| 6427 | case GGML_ROPE_TYPE_VISION: |
| 6428 | rotate_pairs<T>(ne0, n_dims, cache, src, dst_data); |
| 6429 | break; |
| 6430 | default: |
| 6431 | GGML_ABORT("rope type not supported"); |
| 6432 | } |
| 6433 | |
| 6434 | if (!is_vision) { |
| 6435 | // fill the remain channels with data from src tensor |
| 6436 | for (int64_t i0 = 0; i0 < ne0; i0 += 2) { |
| 6437 | if (i0 == n_offs) { |
| 6438 | i0 += n_dims - 2; // skip the rotated channels |
| 6439 | continue; |
| 6440 | } |
| 6441 | const T * const src = (T *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00); |
| 6442 | T * dst_data = (T *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0); |
| 6443 | |
| 6444 | dst_data[0] = src[0]; |
| 6445 | dst_data[1] = src[1]; |
| 6446 | } |
| 6447 | } |
| 6448 | } //attn-heads |
| 6449 | } |
| 6450 | } |
| 6451 | } |
| 6452 | |
| 6453 | void ggml_compute_forward_rope( |
| 6454 | const ggml_compute_params * params, |
| 6455 | ggml_tensor * dst) { |
| 6456 | |
| 6457 | const ggml_tensor * src0 = dst->src[0]; |
| 6458 | |
| 6459 | switch (src0->type) { |
| 6460 | case GGML_TYPE_F16: |
| 6461 | { |
| 6462 | ggml_compute_forward_rope_flt<ggml_fp16_t>(params, dst, true); |
| 6463 | } break; |
| 6464 | case GGML_TYPE_F32: |
| 6465 | { |
| 6466 | ggml_compute_forward_rope_flt<float>(params, dst, true); |
| 6467 | } break; |
| 6468 | default: |
| 6469 | { |
| 6470 | GGML_ABORT("fatal error"); |
| 6471 | } |
| 6472 | } |
| 6473 | } |
| 6474 | |
| 6475 | // ggml_compute_forward_rope_back |
| 6476 | |
| 6477 | void ggml_compute_forward_rope_back( |
| 6478 | const ggml_compute_params * params, |
| 6479 | ggml_tensor * dst) { |
| 6480 | |
| 6481 | const ggml_tensor * src0 = dst->src[0]; |
| 6482 | |
| 6483 | switch (src0->type) { |
| 6484 | case GGML_TYPE_F16: |
| 6485 | { |
| 6486 | ggml_compute_forward_rope_flt<ggml_fp16_t>(params, dst, false); |
| 6487 | } break; |
| 6488 | case GGML_TYPE_F32: |
| 6489 | { |
| 6490 | ggml_compute_forward_rope_flt<float>(params, dst, false); |
| 6491 | } break; |
| 6492 | default: |
| 6493 | { |
| 6494 | GGML_ABORT("fatal error"); |
| 6495 | } |
| 6496 | } |
| 6497 | } |
| 6498 | |
| 6499 | // ggml_compute_forward_conv_transpose_1d |
| 6500 | |
| 6501 | static void ggml_compute_forward_conv_transpose_1d_f16_f32( |
| 6502 | const ggml_compute_params * params, |
| 6503 | ggml_tensor * dst) { |
| 6504 | |
| 6505 | const ggml_tensor * src0 = dst->src[0]; |
| 6506 | const ggml_tensor * src1 = dst->src[1]; |
| 6507 | |
| 6508 | GGML_ASSERT(src0->type == GGML_TYPE_F16); |
| 6509 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 6510 | GGML_ASSERT( dst->type == GGML_TYPE_F32); |
| 6511 | |
| 6512 | GGML_TENSOR_BINARY_OP_LOCALS |
| 6513 | |
| 6514 | const int ith = params->ith; |
| 6515 | const int nth = params->nth; |
| 6516 | |
| 6517 | const int nk = ne00*ne01*ne02; |
| 6518 | |
| 6519 | GGML_ASSERT(nb00 == sizeof(ggml_fp16_t)); |
| 6520 | GGML_ASSERT(nb10 == sizeof(float)); |
| 6521 | |
| 6522 | if (ith == 0) { |
| 6523 | memset(params->wdata, 0, params->wsize); |
| 6524 | |
| 6525 | // permute kernel data (src0) from (K x Cout x Cin) to (Cin x K x Cout) |
| 6526 | { |
| 6527 | ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + 0; |
| 6528 | |
| 6529 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 6530 | for (int64_t i01 = 0; i01 < ne01; i01++) { |
| 6531 | const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i02*nb02 + i01*nb01); |
| 6532 | ggml_fp16_t * dst_data = wdata + i01*ne00*ne02; |
| 6533 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 6534 | dst_data[i00*ne02 + i02] = src[i00]; |
| 6535 | } |
| 6536 | } |
| 6537 | } |
| 6538 | } |
| 6539 | |
| 6540 | // permute source data (src1) from (L x Cin) to (Cin x L) |
| 6541 | { |
| 6542 | ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + nk; |
| 6543 | ggml_fp16_t * dst_data = wdata; |
| 6544 | |
| 6545 | for (int64_t i11 = 0; i11 < ne11; i11++) { |
| 6546 | const float * const src = (float *)((char *) src1->data + i11*nb11); |
| 6547 | for (int64_t i10 = 0; i10 < ne10; i10++) { |
| 6548 | dst_data[i10*ne11 + i11] = GGML_CPU_FP32_TO_FP16(src[i10]); |
| 6549 | } |
| 6550 | } |
| 6551 | } |
| 6552 | |
| 6553 | // need to zero dst since we are accumulating into it |
| 6554 | memset(dst->data, 0, ggml_nbytes(dst)); |
| 6555 | } |
| 6556 | ggml_barrier(params->threadpool); |
| 6557 | |
| 6558 | const int32_t s0 = ((const int32_t*)(dst->op_params))[0]; |
| 6559 | |
| 6560 | // total rows in dst |
| 6561 | const int nr = ne1; |
| 6562 | |
| 6563 | // rows per thread |
| 6564 | const int dr = (nr + nth - 1)/nth; |
| 6565 | |
| 6566 | // row range for this thread |
| 6567 | const int ir0 = dr*ith; |
| 6568 | const int ir1 = MIN(ir0 + dr, nr); |
| 6569 | |
| 6570 | ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + 0; |
| 6571 | ggml_fp16_t * const wdata_src = wdata + nk; |
| 6572 | |
| 6573 | for (int i1 = ir0; i1 < ir1; i1++) { |
| 6574 | float * dst_data = (float *)((char *) dst->data + i1*nb1); |
| 6575 | ggml_fp16_t * wdata_kernel = wdata + i1*ne02*ne00; |
| 6576 | for (int i10 = 0; i10 < ne10; i10++) { |
| 6577 | const int i1n = i10*ne11; |
| 6578 | for (int i00 = 0; i00 < ne00; i00++) { |
| 6579 | float v = 0; |
| 6580 | ggml_vec_dot_f16(ne02, &v, 0, |
| 6581 | (ggml_fp16_t *) wdata_src + i1n, 0, |
| 6582 | (ggml_fp16_t *) wdata_kernel + i00*ne02, 0, 1); |
| 6583 | dst_data[i10*s0 + i00] += v; |
| 6584 | } |
| 6585 | } |
| 6586 | } |
| 6587 | } |
| 6588 | |
| 6589 | static void ggml_compute_forward_conv_transpose_1d_f32( |
| 6590 | const ggml_compute_params * params, |
| 6591 | ggml_tensor * dst) { |
| 6592 | |
| 6593 | const ggml_tensor * src0 = dst->src[0]; |
| 6594 | const ggml_tensor * src1 = dst->src[1]; |
| 6595 | |
| 6596 | GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 6597 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 6598 | GGML_ASSERT( dst->type == GGML_TYPE_F32); |
| 6599 | |
| 6600 | GGML_TENSOR_BINARY_OP_LOCALS |
| 6601 | |
| 6602 | const int ith = params->ith; |
| 6603 | const int nth = params->nth; |
| 6604 | |
| 6605 | const int nk = ne00*ne01*ne02; |
| 6606 | |
| 6607 | GGML_ASSERT(nb00 == sizeof(float)); |
| 6608 | GGML_ASSERT(nb10 == sizeof(float)); |
| 6609 | |
| 6610 | if (ith == 0) { |
| 6611 | memset(params->wdata, 0, params->wsize); |
| 6612 | |
| 6613 | // prepare kernel data (src0) from (K x Cout x Cin) to (Cin x K x Cout) |
| 6614 | { |
| 6615 | float * const wdata = (float *) params->wdata + 0; |
| 6616 | |
| 6617 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 6618 | for (int64_t i01 = 0; i01 < ne01; i01++) { |
| 6619 | const float * const src = (float *)((char *) src0->data + i02*nb02 + i01*nb01); |
| 6620 | float * dst_data = wdata + i01*ne00*ne02; |
| 6621 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 6622 | dst_data[i00*ne02 + i02] = src[i00]; |
| 6623 | } |
| 6624 | } |
| 6625 | } |
| 6626 | } |
| 6627 | |
| 6628 | // prepare source data (src1) |
| 6629 | { |
| 6630 | float * const wdata = (float *) params->wdata + nk; |
| 6631 | float * dst_data = wdata; |
| 6632 | |
| 6633 | for (int64_t i11 = 0; i11 < ne11; i11++) { |
| 6634 | const float * const src = (float *)((char *) src1->data + i11*nb11); |
| 6635 | for (int64_t i10 = 0; i10 < ne10; i10++) { |
| 6636 | dst_data[i10*ne11 + i11] = src[i10]; |
| 6637 | } |
| 6638 | } |
| 6639 | } |
| 6640 | |
| 6641 | // need to zero dst since we are accumulating into it |
| 6642 | memset(dst->data, 0, ggml_nbytes(dst)); |
| 6643 | } |
| 6644 | ggml_barrier(params->threadpool); |
| 6645 | |
| 6646 | const int32_t s0 = ((const int32_t*)(dst->op_params))[0]; |
| 6647 | |
| 6648 | // total rows in dst |
| 6649 | const int nr = ne1; |
| 6650 | |
| 6651 | // rows per thread |
| 6652 | const int dr = (nr + nth - 1)/nth; |
| 6653 | |
| 6654 | // row range for this thread |
| 6655 | const int ir0 = dr*ith; |
| 6656 | const int ir1 = MIN(ir0 + dr, nr); |
| 6657 | |
| 6658 | float * const wdata = (float *) params->wdata + 0; |
| 6659 | float * const wdata_src = wdata + nk; |
| 6660 | |
| 6661 | for (int i1 = ir0; i1 < ir1; i1++) { |
| 6662 | float * dst_data = (float *)((char *) dst->data + i1*nb1); |
| 6663 | float * wdata_kernel = wdata + i1*ne02*ne00; |
| 6664 | for (int i10 = 0; i10 < ne10; i10++) { |
| 6665 | const int i1n = i10*ne11; |
| 6666 | for (int i00 = 0; i00 < ne00; i00++) { |
| 6667 | float v = 0; |
| 6668 | ggml_vec_dot_f32(ne02, &v, 0, |
| 6669 | wdata_src + i1n, 0, |
| 6670 | wdata_kernel + i00*ne02, 0, 1); |
| 6671 | dst_data[i10*s0 + i00] += v; |
| 6672 | } |
| 6673 | } |
| 6674 | } |
| 6675 | } |
| 6676 | |
| 6677 | void ggml_compute_forward_conv_transpose_1d( |
| 6678 | const ggml_compute_params * params, |
| 6679 | ggml_tensor * dst) { |
| 6680 | |
| 6681 | const ggml_tensor * src0 = dst->src[0]; |
| 6682 | |
| 6683 | switch (src0->type) { |
| 6684 | case GGML_TYPE_F16: |
| 6685 | { |
| 6686 | ggml_compute_forward_conv_transpose_1d_f16_f32(params, dst); |
| 6687 | } break; |
| 6688 | case GGML_TYPE_F32: |
| 6689 | { |
| 6690 | ggml_compute_forward_conv_transpose_1d_f32(params, dst); |
| 6691 | } break; |
| 6692 | default: |
| 6693 | { |
| 6694 | GGML_ABORT("fatal error"); |
| 6695 | } |
| 6696 | } |
| 6697 | } |
| 6698 | |
| 6699 | // ggml_compute_forward_im2col_f32 |
| 6700 | // src0: kernel [OC, IC, KH, KW] |
| 6701 | // src1: image [N, IC, IH, IW] |
| 6702 | // dst: result [N, OH, OW, IC*KH*KW] |
| 6703 | static void ggml_compute_forward_im2col_f32( |
| 6704 | const ggml_compute_params * params, |
| 6705 | ggml_tensor * dst) { |
| 6706 | |
| 6707 | const ggml_tensor * src0 = dst->src[0]; |
| 6708 | const ggml_tensor * src1 = dst->src[1]; |
| 6709 | |
| 6710 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 6711 | GGML_ASSERT( dst->type == GGML_TYPE_F32); |
| 6712 | |
| 6713 | GGML_TENSOR_BINARY_OP_LOCALS; |
| 6714 | |
| 6715 | const int32_t s0 = ((const int32_t *)(dst->op_params))[0]; |
| 6716 | const int32_t s1 = ((const int32_t *)(dst->op_params))[1]; |
| 6717 | const int32_t p0 = ((const int32_t *)(dst->op_params))[2]; |
| 6718 | const int32_t p1 = ((const int32_t *)(dst->op_params))[3]; |
| 6719 | const int32_t d0 = ((const int32_t *)(dst->op_params))[4]; |
| 6720 | const int32_t d1 = ((const int32_t *)(dst->op_params))[5]; |
| 6721 | const bool is_2D = ((const int32_t *)(dst->op_params))[6] == 1; |
| 6722 | |
| 6723 | const int ith = params->ith; |
| 6724 | const int nth = params->nth; |
| 6725 | |
| 6726 | const int64_t N = is_2D ? ne13 : ne12; |
| 6727 | const int64_t IC = is_2D ? ne12 : ne11; |
| 6728 | const int64_t IH = is_2D ? ne11 : 1; |
| 6729 | const int64_t IW = ne10; |
| 6730 | |
| 6731 | const int64_t KH = is_2D ? ne01 : 1; |
| 6732 | const int64_t KW = ne00; |
| 6733 | |
| 6734 | const int64_t OH = is_2D ? ne2 : 1; |
| 6735 | const int64_t OW = ne1; |
| 6736 | |
| 6737 | int ofs0 = is_2D ? nb13 : nb12; |
| 6738 | int ofs1 = is_2D ? nb12 : nb11; |
| 6739 | |
| 6740 | GGML_ASSERT(nb10 == sizeof(float)); |
| 6741 | |
| 6742 | // im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW] |
| 6743 | { |
| 6744 | float * const wdata = (float *) dst->data; |
| 6745 | |
| 6746 | for (int64_t in = 0; in < N; in++) { |
| 6747 | for (int64_t ioh = 0; ioh < OH; ioh++) { // 1 |
| 6748 | for (int64_t iow = 0; iow < OW; iow++) { |
| 6749 | for (int64_t iic = ith; iic < IC; iic += nth) { |
| 6750 | |
| 6751 | // micro kernel |
| 6752 | float * dst_data = wdata + (in*OH*OW + ioh*OW + iow)*(IC*KH*KW); // [IC, KH, KW] |
| 6753 | const float * const src_data = (float *)((char *) src1->data + in*ofs0 + iic*ofs1); // [IH, IW] |
| 6754 | |
| 6755 | for (int64_t ikh = 0; ikh < KH; ikh++) { // 1 |
| 6756 | for (int64_t ikw = 0; ikw < KW; ikw++) { |
| 6757 | const int64_t iiw = iow*s0 + ikw*d0 - p0; |
| 6758 | const int64_t iih = ioh*s1 + ikh*d1 - p1; |
| 6759 | |
| 6760 | if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) { |
| 6761 | dst_data[iic*(KH*KW) + ikh*KW + ikw] = 0; |
| 6762 | } else { |
| 6763 | dst_data[iic*(KH*KW) + ikh*KW + ikw] = (src_data[iih*IW + iiw]); |
| 6764 | } |
| 6765 | } |
| 6766 | } |
| 6767 | } |
| 6768 | } |
| 6769 | } |
| 6770 | } |
| 6771 | } |
| 6772 | } |
| 6773 | |
| 6774 | |
| 6775 | // ggml_compute_forward_im2col_f16 |
| 6776 | // src0: kernel [OC, IC, KH, KW] |
| 6777 | // src1: image [N, IC, IH, IW] |
| 6778 | // dst: result [N, OH, OW, IC*KH*KW] |
| 6779 | static void ggml_compute_forward_im2col_f16( |
| 6780 | const ggml_compute_params * params, |
| 6781 | ggml_tensor * dst) { |
| 6782 | |
| 6783 | const ggml_tensor * src0 = dst->src[0]; |
| 6784 | const ggml_tensor * src1 = dst->src[1]; |
| 6785 | |
| 6786 | GGML_ASSERT(src1->type == GGML_TYPE_F16 || src1->type == GGML_TYPE_F32); |
| 6787 | GGML_ASSERT( dst->type == GGML_TYPE_F16); |
| 6788 | |
| 6789 | GGML_TENSOR_BINARY_OP_LOCALS; |
| 6790 | |
| 6791 | const int32_t s0 = ((const int32_t *)(dst->op_params))[0]; |
| 6792 | const int32_t s1 = ((const int32_t *)(dst->op_params))[1]; |
| 6793 | const int32_t p0 = ((const int32_t *)(dst->op_params))[2]; |
| 6794 | const int32_t p1 = ((const int32_t *)(dst->op_params))[3]; |
| 6795 | const int32_t d0 = ((const int32_t *)(dst->op_params))[4]; |
| 6796 | const int32_t d1 = ((const int32_t *)(dst->op_params))[5]; |
| 6797 | const bool is_2D = ((const int32_t *)(dst->op_params))[6] == 1; |
| 6798 | |
| 6799 | const int ith = params->ith; |
| 6800 | const int nth = params->nth; |
| 6801 | |
| 6802 | const int64_t N = is_2D ? ne13 : ne12; |
| 6803 | const int64_t IC = is_2D ? ne12 : ne11; |
| 6804 | const int64_t IH = is_2D ? ne11 : 1; |
| 6805 | const int64_t IW = ne10; |
| 6806 | |
| 6807 | const int64_t KH = is_2D ? ne01 : 1; |
| 6808 | const int64_t KW = ne00; |
| 6809 | |
| 6810 | const int64_t OH = is_2D ? ne2 : 1; |
| 6811 | const int64_t OW = ne1; |
| 6812 | |
| 6813 | int ofs0 = is_2D ? nb13 : nb12; |
| 6814 | int ofs1 = is_2D ? nb12 : nb11; |
| 6815 | |
| 6816 | GGML_ASSERT(nb10 == ggml_type_size(src1->type)); |
| 6817 | |
| 6818 | // im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW] |
| 6819 | { |
| 6820 | ggml_fp16_t * const wdata = (ggml_fp16_t *) dst->data; |
| 6821 | |
| 6822 | for (int64_t in = 0; in < N; in++) { |
| 6823 | for (int64_t ioh = 0; ioh < OH; ioh++) { // 1 |
| 6824 | for (int64_t iow = 0; iow < OW; iow++) { |
| 6825 | for (int64_t iic = ith; iic < IC; iic += nth) { |
| 6826 | |
| 6827 | // micro kernel |
| 6828 | ggml_fp16_t * dst_data = wdata + (in*OH*OW + ioh*OW + iow)*(IC*KH*KW); // [IC, KH, KW] |
| 6829 | const float * const src_data_f32 = src1->type == GGML_TYPE_F32 |
| 6830 | ? (const float *)((const char *) src1->data + in*ofs0 + iic*ofs1) |
| 6831 | : nullptr; // [IH, IW] |
| 6832 | const ggml_fp16_t * const src_data_f16 = src1->type == GGML_TYPE_F16 |
| 6833 | ? (const ggml_fp16_t *)((const char *) src1->data + in*ofs0 + iic*ofs1) |
| 6834 | : nullptr; // [IH, IW] |
| 6835 | |
| 6836 | for (int64_t ikh = 0; ikh < KH; ikh++) { // 1 |
| 6837 | for (int64_t ikw = 0; ikw < KW; ikw++) { |
| 6838 | const int64_t iiw = iow*s0 + ikw*d0 - p0; |
| 6839 | const int64_t iih = ioh*s1 + ikh*d1 - p1; |
| 6840 | |
| 6841 | if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) { |
| 6842 | dst_data[iic*(KH*KW) + ikh*KW + ikw] = 0; |
| 6843 | } else { |
| 6844 | if (src_data_f32 != nullptr) { |
| 6845 | dst_data[iic*(KH*KW) + ikh*KW + ikw] = GGML_CPU_FP32_TO_FP16(src_data_f32[iih*IW + iiw]); |
| 6846 | } else { |
| 6847 | dst_data[iic*(KH*KW) + ikh*KW + ikw] = src_data_f16[iih*IW + iiw]; |
| 6848 | } |
| 6849 | } |
| 6850 | } |
| 6851 | } |
| 6852 | } |
| 6853 | } |
| 6854 | } |
| 6855 | } |
| 6856 | } |
| 6857 | } |
| 6858 | |
| 6859 | void ggml_compute_forward_im2col( |
| 6860 | const ggml_compute_params * params, |
| 6861 | ggml_tensor * dst) { |
| 6862 | switch (dst->type) { |
| 6863 | case GGML_TYPE_F16: |
| 6864 | { |
| 6865 | ggml_compute_forward_im2col_f16(params, dst); |
| 6866 | } break; |
| 6867 | case GGML_TYPE_F32: |
| 6868 | { |
| 6869 | ggml_compute_forward_im2col_f32(params, dst); |
| 6870 | } break; |
| 6871 | default: |
| 6872 | { |
| 6873 | GGML_ABORT("fatal error"); |
| 6874 | } |
| 6875 | } |
| 6876 | } |
| 6877 | |
| 6878 | // ggml_compute_forward_im2col_back_f32 |
| 6879 | |
| 6880 | void ggml_compute_forward_im2col_back_f32( |
| 6881 | const ggml_compute_params * params, |
| 6882 | ggml_tensor * dst) { |
| 6883 | |
| 6884 | const ggml_tensor * src0 = dst->src[0]; // gradients of forward pass output |
| 6885 | const ggml_tensor * src1 = dst->src[1]; // convolution kernel |
| 6886 | |
| 6887 | GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 6888 | GGML_ASSERT(src1->type == GGML_TYPE_F32 || src1->type == GGML_TYPE_F16); |
| 6889 | GGML_ASSERT( dst->type == GGML_TYPE_F32); |
| 6890 | |
| 6891 | GGML_TENSOR_BINARY_OP_LOCALS; |
| 6892 | |
| 6893 | const int32_t s0 = ((const int32_t *)(dst->op_params))[0]; |
| 6894 | const int32_t s1 = ((const int32_t *)(dst->op_params))[1]; |
| 6895 | const int32_t p0 = ((const int32_t *)(dst->op_params))[2]; |
| 6896 | const int32_t p1 = ((const int32_t *)(dst->op_params))[3]; |
| 6897 | const int32_t d0 = ((const int32_t *)(dst->op_params))[4]; |
| 6898 | const int32_t d1 = ((const int32_t *)(dst->op_params))[5]; |
| 6899 | const bool is_2D = ((const int32_t *)(dst->op_params))[6] == 1; |
| 6900 | |
| 6901 | const int ith = params->ith; |
| 6902 | const int nth = params->nth; |
| 6903 | |
| 6904 | const int64_t N = is_2D ? ne3 : ne2; |
| 6905 | const int64_t IC = is_2D ? ne2 : ne1; |
| 6906 | const int64_t IH = is_2D ? ne1 : 1; |
| 6907 | const int64_t IW = ne0; |
| 6908 | |
| 6909 | const int64_t KH = is_2D ? ne11 : 1; |
| 6910 | const int64_t KW = ne10; |
| 6911 | |
| 6912 | const int64_t OH = is_2D ? ne02 : 1; |
| 6913 | const int64_t OW = ne01; |
| 6914 | |
| 6915 | int ofs0 = is_2D ? nb3 : nb2; |
| 6916 | int ofs1 = is_2D ? nb2 : nb1; |
| 6917 | |
| 6918 | GGML_ASSERT(nb0 == sizeof(float)); |
| 6919 | |
| 6920 | // im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW] |
| 6921 | { |
| 6922 | float * const wdata = (float *) dst->data; |
| 6923 | |
| 6924 | for (int64_t in = 0; in < N; in++) { |
| 6925 | for (int64_t iic = ith; iic < IC; iic += nth) { |
| 6926 | for (int64_t iih = 0; iih < IH; iih++) { |
| 6927 | for (int64_t iiw = 0; iiw < IW; iiw++) { |
| 6928 | |
| 6929 | // micro kernel |
| 6930 | float grad = 0.0f; |
| 6931 | for (int64_t ikh = 0; ikh < KH; ikh++) { |
| 6932 | for (int64_t ikw = 0; ikw < KW; ikw++) { |
| 6933 | // For s0 > 1 some values were skipped over in the forward pass. |
| 6934 | // These values have tmpw % s0 != 0 and need to be skipped in the backwards pass as well. |
| 6935 | const int64_t tmpw = (iiw + p0 - ikw*d0); |
| 6936 | if (tmpw % s0 != 0) { |
| 6937 | continue; |
| 6938 | } |
| 6939 | const int64_t iow = tmpw / s0; |
| 6940 | |
| 6941 | // Equivalent logic as above except for s1. |
| 6942 | int64_t ioh; |
| 6943 | if (is_2D) { |
| 6944 | const int64_t tmph = iih + p1 - ikh*d1; |
| 6945 | |
| 6946 | if (tmph % s1 != 0) { |
| 6947 | continue; |
| 6948 | } |
| 6949 | |
| 6950 | ioh = tmph / s1; |
| 6951 | } else { |
| 6952 | ioh = 0; |
| 6953 | } |
| 6954 | |
| 6955 | if (iow < 0 || iow >= OW || ioh < 0 || ioh >= OH) { |
| 6956 | continue; |
| 6957 | } |
| 6958 | |
| 6959 | const float * const grad_in = (const float *) src0->data |
| 6960 | + (in*OH*OW + ioh*OW + iow)*(IC*KH*KW); // [IC, KH, KW] |
| 6961 | grad += grad_in[iic*(KH*KW) + ikh*KW + ikw]; |
| 6962 | } |
| 6963 | } |
| 6964 | float * dst_data = (float *)((char *) wdata + (in*ofs0 + iic*ofs1)); // [IH, IW] |
| 6965 | dst_data[iih*IW + iiw] = grad; |
| 6966 | } |
| 6967 | } |
| 6968 | } |
| 6969 | } |
| 6970 | } |
| 6971 | } |
| 6972 | |
| 6973 | |
| 6974 | // ggml_compute_forward_im2col_3d_f16 |
| 6975 | // src0: kernel [OC*IC, KD, KH, KW] |
| 6976 | // src1: image [N*IC, ID, IH, IW] |
| 6977 | // dst: result [N*OD, OH, OW, IC * KD * KH * KW] |
| 6978 | static void ggml_compute_forward_im2col_3d_f16( |
| 6979 | const ggml_compute_params * params, |
| 6980 | ggml_tensor * dst) { |
| 6981 | |
| 6982 | const ggml_tensor * src0 = dst->src[0]; |
| 6983 | const ggml_tensor * src1 = dst->src[1]; |
| 6984 | |
| 6985 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 6986 | GGML_ASSERT( dst->type == GGML_TYPE_F16); |
| 6987 | |
| 6988 | GGML_TENSOR_BINARY_OP_LOCALS; |
| 6989 | |
| 6990 | const int32_t s0 = ((const int32_t *)(dst->op_params))[0]; |
| 6991 | const int32_t s1 = ((const int32_t *)(dst->op_params))[1]; |
| 6992 | const int32_t s2 = ((const int32_t *)(dst->op_params))[2]; |
| 6993 | const int32_t p0 = ((const int32_t *)(dst->op_params))[3]; |
| 6994 | const int32_t p1 = ((const int32_t *)(dst->op_params))[4]; |
| 6995 | const int32_t p2 = ((const int32_t *)(dst->op_params))[5]; |
| 6996 | const int32_t d0 = ((const int32_t *)(dst->op_params))[6]; |
| 6997 | const int32_t d1 = ((const int32_t *)(dst->op_params))[7]; |
| 6998 | const int32_t d2 = ((const int32_t *)(dst->op_params))[8]; |
| 6999 | const int32_t IC = ((const int32_t *)(dst->op_params))[9]; |
| 7000 | |
| 7001 | |
| 7002 | const int ith = params->ith; |
| 7003 | const int nth = params->nth; |
| 7004 | |
| 7005 | const int64_t N = ne13 / IC; |
| 7006 | const int64_t ID = ne12; |
| 7007 | const int64_t IH = ne11; |
| 7008 | const int64_t IW = ne10; |
| 7009 | |
| 7010 | const int64_t OC = ne03 / IC; |
| 7011 | GGML_UNUSED(OC); |
| 7012 | const int64_t KD = ne02; |
| 7013 | const int64_t KH = ne01; |
| 7014 | const int64_t KW = ne00; |
| 7015 | |
| 7016 | const int64_t OD = ne3 / N; |
| 7017 | const int64_t OH = ne2; |
| 7018 | const int64_t OW = ne1; |
| 7019 | const int64_t OH_OW = OH*OW; |
| 7020 | const int64_t KD_KH_KW = KD*KH*KW; |
| 7021 | const int64_t KH_KW = KH*KW; |
| 7022 | const int64_t IC_KD_KH_KW = IC*KD*KH*KW; |
| 7023 | |
| 7024 | GGML_ASSERT(nb10 == sizeof(float)); |
| 7025 | |
| 7026 | // im2col: [N*IC, ID, IH, IW] => [N*OD, OH, OW, IC * KD * KH * KW] |
| 7027 | { |
| 7028 | ggml_fp16_t * const wdata = (ggml_fp16_t *) dst->data; |
| 7029 | |
| 7030 | for (int64_t in = 0; in < N; in++) { |
| 7031 | for (int64_t iod = 0; iod < OD; iod++) { |
| 7032 | for (int64_t ioh = 0; ioh < OH; ioh++) { |
| 7033 | for (int64_t iow = 0; iow < OW; iow++) { |
| 7034 | for (int64_t iic = ith; iic < IC; iic += nth) { |
| 7035 | |
| 7036 | // micro kernel |
| 7037 | ggml_fp16_t * dst_data = wdata + (in*OD*OH_OW + iod*OH_OW + ioh*OW + iow)*IC_KD_KH_KW; // [IC, KD, KH, KW] |
| 7038 | const float * const src_data = (const float *) ((const char *)src1->data + (in*IC + iic)*nb13); // [ID, IH, IW] |
| 7039 | |
| 7040 | for (int64_t ikd = 0; ikd < KD; ikd++) { |
| 7041 | for (int64_t ikh = 0; ikh < KH; ikh++) { |
| 7042 | for (int64_t ikw = 0; ikw < KW; ikw++) { |
| 7043 | const int64_t iiw = iow*s0 + ikw*d0 - p0; |
| 7044 | const int64_t iih = ioh*s1 + ikh*d1 - p1; |
| 7045 | const int64_t iid = iod*s2 + ikd*d2 - p2; |
| 7046 | |
| 7047 | if (iid < 0 || iid >= ID || iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) { |
| 7048 | dst_data[iic*KD_KH_KW + ikd * KH_KW + ikh*KW + ikw] = 0; |
| 7049 | } else { |
| 7050 | const float * const s = (const float *) ((const char *)src_data + iid*nb12 + iih*nb11 + iiw*nb10); // [ID, IH, IW] |
| 7051 | dst_data[iic*KD_KH_KW + ikd * KH_KW + ikh*KW + ikw] = GGML_CPU_FP32_TO_FP16(*s); |
| 7052 | } |
| 7053 | } |
| 7054 | } |
| 7055 | } |
| 7056 | } |
| 7057 | } |
| 7058 | } |
| 7059 | } |
| 7060 | } |
| 7061 | } |
| 7062 | } |
| 7063 | |
| 7064 | // ggml_compute_forward_im2col_3d_f32 |
| 7065 | // src0: kernel [OC*IC, KD, KH, KW] |
| 7066 | // src1: image [N*IC, ID, IH, IW] |
| 7067 | // dst: result [N*OD, OH, OW, IC * KD * KH * KW] |
| 7068 | static void ggml_compute_forward_im2col_3d_f32( |
| 7069 | const ggml_compute_params * params, |
| 7070 | ggml_tensor * dst) { |
| 7071 | |
| 7072 | const ggml_tensor * src0 = dst->src[0]; |
| 7073 | const ggml_tensor * src1 = dst->src[1]; |
| 7074 | |
| 7075 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 7076 | GGML_ASSERT( dst->type == GGML_TYPE_F32); |
| 7077 | |
| 7078 | GGML_TENSOR_BINARY_OP_LOCALS; |
| 7079 | |
| 7080 | const int32_t s0 = ((const int32_t *)(dst->op_params))[0]; |
| 7081 | const int32_t s1 = ((const int32_t *)(dst->op_params))[1]; |
| 7082 | const int32_t s2 = ((const int32_t *)(dst->op_params))[2]; |
| 7083 | const int32_t p0 = ((const int32_t *)(dst->op_params))[3]; |
| 7084 | const int32_t p1 = ((const int32_t *)(dst->op_params))[4]; |
| 7085 | const int32_t p2 = ((const int32_t *)(dst->op_params))[5]; |
| 7086 | const int32_t d0 = ((const int32_t *)(dst->op_params))[6]; |
| 7087 | const int32_t d1 = ((const int32_t *)(dst->op_params))[7]; |
| 7088 | const int32_t d2 = ((const int32_t *)(dst->op_params))[8]; |
| 7089 | const int32_t IC = ((const int32_t *)(dst->op_params))[9]; |
| 7090 | |
| 7091 | |
| 7092 | const int ith = params->ith; |
| 7093 | const int nth = params->nth; |
| 7094 | |
| 7095 | const int64_t N = ne13 / IC; |
| 7096 | const int64_t ID = ne12; |
| 7097 | const int64_t IH = ne11; |
| 7098 | const int64_t IW = ne10; |
| 7099 | |
| 7100 | const int64_t OC = ne03 / IC; |
| 7101 | GGML_UNUSED(OC); |
| 7102 | const int64_t KD = ne02; |
| 7103 | const int64_t KH = ne01; |
| 7104 | const int64_t KW = ne00; |
| 7105 | |
| 7106 | const int64_t OD = ne3 / N; |
| 7107 | const int64_t OH = ne2; |
| 7108 | const int64_t OW = ne1; |
| 7109 | |
| 7110 | const int64_t OH_OW = OH*OW; |
| 7111 | const int64_t KD_KH_KW = KD*KH*KW; |
| 7112 | const int64_t KH_KW = KH*KW; |
| 7113 | const int64_t IC_KD_KH_KW = IC*KD*KH*KW; |
| 7114 | |
| 7115 | GGML_ASSERT(nb10 == sizeof(float)); |
| 7116 | |
| 7117 | // im2col: [N*IC, ID, IH, IW] => [N*OD, OH, OW, IC * KD * KH * KW] |
| 7118 | { |
| 7119 | float * const wdata = (float *) dst->data; |
| 7120 | |
| 7121 | for (int64_t in = 0; in < N; in++) { |
| 7122 | for (int64_t iod = 0; iod < OD; iod++) { |
| 7123 | for (int64_t ioh = 0; ioh < OH; ioh++) { |
| 7124 | for (int64_t iow = 0; iow < OW; iow++) { |
| 7125 | for (int64_t iic = ith; iic < IC; iic += nth) { |
| 7126 | |
| 7127 | // micro kernel |
| 7128 | float * dst_data = wdata + (in*OD*OH_OW + iod*OH_OW + ioh*OW + iow)*IC_KD_KH_KW; // [IC, KD, KH, KW] |
| 7129 | const float * const src_data = (const float *) ((const char *)src1->data + (in*IC + iic)*nb13); // [ID, IH, IW] |
| 7130 | |
| 7131 | for (int64_t ikd = 0; ikd < KD; ikd++) { |
| 7132 | for (int64_t ikh = 0; ikh < KH; ikh++) { |
| 7133 | for (int64_t ikw = 0; ikw < KW; ikw++) { |
| 7134 | const int64_t iiw = iow*s0 + ikw*d0 - p0; |
| 7135 | const int64_t iih = ioh*s1 + ikh*d1 - p1; |
| 7136 | const int64_t iid = iod*s2 + ikd*d2 - p2; |
| 7137 | |
| 7138 | if (iid < 0 || iid >= ID || iih < 0 || iih >= IH || iiw < 0 || iiw >= IW || iid < 0 || iid >= ID) { |
| 7139 | dst_data[iic*KD_KH_KW + ikd * KH_KW + ikh*KW + ikw] = 0; |
| 7140 | } else { |
| 7141 | const float * const s = (const float *) ((const char *)src_data + iid*nb12 + iih*nb11 + iiw*nb10); // [ID, IH, IW] |
| 7142 | dst_data[iic*KD_KH_KW + ikd * KH_KW + ikh*KW + ikw] = *s; |
| 7143 | } |
| 7144 | } |
| 7145 | } |
| 7146 | } |
| 7147 | } |
| 7148 | } |
| 7149 | } |
| 7150 | } |
| 7151 | } |
| 7152 | } |
| 7153 | } |
| 7154 | |
| 7155 | |
| 7156 | void ggml_compute_forward_im2col_3d( |
| 7157 | const ggml_compute_params * params, |
| 7158 | ggml_tensor * dst) { |
| 7159 | switch (dst->type) { |
| 7160 | case GGML_TYPE_F16: |
| 7161 | { |
| 7162 | ggml_compute_forward_im2col_3d_f16(params, dst); |
| 7163 | } break; |
| 7164 | case GGML_TYPE_F32: |
| 7165 | { |
| 7166 | ggml_compute_forward_im2col_3d_f32(params, dst); |
| 7167 | } break; |
| 7168 | default: |
| 7169 | { |
| 7170 | GGML_ABORT("fatal error"); |
| 7171 | } |
| 7172 | } |
| 7173 | } |
| 7174 | |
| 7175 | static void ggml_call_mul_mat(ggml_type type, const ggml_compute_params * params, int64_t m, int64_t n, int64_t k, |
| 7176 | void * a, void * b, float * c) { |
| 7177 | const ggml_type_traits * traits = ggml_get_type_traits(type); |
| 7178 | struct ggml_tensor src1 = {}; |
| 7179 | src1.type = type; |
| 7180 | src1.ne[0] = k; |
| 7181 | src1.ne[1] = m; |
| 7182 | src1.ne[2] = 1; |
| 7183 | src1.ne[3] = 1; |
| 7184 | src1.nb[0] = traits->type_size; |
| 7185 | src1.nb[1] = k * traits->type_size; |
| 7186 | src1.nb[2] = src1.nb[1]; |
| 7187 | src1.nb[3] = src1.nb[2]; |
| 7188 | src1.data = a; |
| 7189 | |
| 7190 | struct ggml_tensor src0 = {}; |
| 7191 | src0.type = type; |
| 7192 | src0.ne[0] = k; |
| 7193 | src0.ne[1] = n; |
| 7194 | src0.ne[2] = 1; |
| 7195 | src0.ne[3] = 1; |
| 7196 | src0.nb[0] = traits->type_size; |
| 7197 | src0.nb[1] = k * traits->type_size; |
| 7198 | src0.nb[2] = src0.nb[1]; |
| 7199 | src0.nb[3] = src0.nb[2]; |
| 7200 | src0.data = b; |
| 7201 | |
| 7202 | struct ggml_tensor dst = {}; |
| 7203 | dst.ne[0] = n; |
| 7204 | dst.ne[1] = m; |
| 7205 | dst.ne[2] = 1; |
| 7206 | dst.ne[3] = 1; |
| 7207 | dst.nb[0] = sizeof(float); |
| 7208 | dst.nb[1] = n * sizeof(float); |
| 7209 | dst.nb[2] = dst.nb[1]; |
| 7210 | dst.nb[3] = dst.nb[2]; |
| 7211 | dst.data = c; |
| 7212 | dst.src[0] = &src0; |
| 7213 | dst.src[1] = &src1; |
| 7214 | |
| 7215 | ggml_compute_forward_mul_mat(params, &dst); |
| 7216 | } |
| 7217 | |
| 7218 | static inline int64_t ggml_wrap_around(int64_t coord, int64_t size) { |
| 7219 | return (coord + size) % size; // adding size avoids negative number weirdness |
| 7220 | } |
| 7221 | |
| 7222 | // ggml_compute_forward_col2im_1d |
| 7223 | // |
| 7224 | // Scatter-add columns [K*OC, T_in] -> signal [T_out, OC] |
| 7225 | // where T_out = (T_in - 1)*s + K - 2*p. Gather approach: each output reads ceil(K/s) inputs. |
| 7226 | // Parallelized over the time axis so the split stays balanced whatever OC is. |
| 7227 | // Supports F32, F16, BF16 input/output (same type), F32 accumulator. |
| 7228 | |
| 7229 | template <typename elem_t> |
| 7230 | static void ggml_compute_forward_col2im_1d_impl( |
| 7231 | const ggml_compute_params * params, |
| 7232 | ggml_tensor * dst) { |
| 7233 | |
| 7234 | const ggml_tensor * src = dst->src[0]; // [K*OC, T_in] |
| 7235 | |
| 7236 | GGML_ASSERT(ggml_is_contiguous(src)); |
| 7237 | GGML_ASSERT(ggml_is_contiguous(dst)); |
| 7238 | |
| 7239 | const int32_t s0 = ((const int32_t *)(dst->op_params))[0]; |
| 7240 | const int32_t OC = ((const int32_t *)(dst->op_params))[1]; |
| 7241 | const int32_t p0 = ((const int32_t *)(dst->op_params))[2]; |
| 7242 | |
| 7243 | const int64_t K_OC = src->ne[0]; |
| 7244 | const int64_t T_in = src->ne[1]; |
| 7245 | const int64_t K = K_OC / OC; |
| 7246 | const int64_t T_out = dst->ne[0]; |
| 7247 | |
| 7248 | const elem_t * col_data = (const elem_t *) src->data; |
| 7249 | elem_t * dst_data = (elem_t *) dst->data; |
| 7250 | |
| 7251 | const int ith = params->ith; |
| 7252 | const int nth = params->nth; |
| 7253 | |
| 7254 | // Parallelize over the time axis: the split stays balanced whatever OC is, |
| 7255 | // down to OC = 1 for mono audio, and threads read disjoint column bands |
| 7256 | const int64_t dr = (T_out + nth - 1) / nth; |
| 7257 | const int64_t it0 = dr * ith; |
| 7258 | const int64_t it1 = it0 + dr < T_out ? it0 + dr : T_out; |
| 7259 | |
| 7260 | for (int64_t oc = 0; oc < OC; oc++) { |
| 7261 | for (int64_t t_out = it0; t_out < it1; t_out++) { |
| 7262 | const int64_t t_abs = t_out + p0; // absolute position in uncropped signal |
| 7263 | // Gather: find all (t_in, k) where t_in * s + k == t_abs, 0 <= k < K |
| 7264 | int64_t t_in_min = (t_abs - K + 1 + s0 - 1) / s0; // ceil((t_abs-K+1)/s) |
| 7265 | if (t_in_min < 0) t_in_min = 0; |
| 7266 | int64_t t_in_max = t_abs / s0; |
| 7267 | if (t_in_max >= T_in) t_in_max = T_in - 1; |
| 7268 | |
| 7269 | float sum = 0.0f; |
| 7270 | for (int64_t t_in = t_in_min; t_in <= t_in_max; t_in++) { |
| 7271 | int64_t k = t_abs - t_in * s0; |
| 7272 | if (k >= 0 && k < K) { |
| 7273 | // col layout: [K*OC, T_in], element (oc*K+k, t_in) |
| 7274 | sum += type_conversion_table<elem_t>::to_f32(col_data[(oc * K + k) + t_in * K_OC]); |
| 7275 | } |
| 7276 | } |
| 7277 | // dst layout: [T_out, OC], element (t_out, oc) |
| 7278 | dst_data[t_out + oc * T_out] = type_conversion_table<elem_t>::from_f32(sum); |
| 7279 | } |
| 7280 | } |
| 7281 | } |
| 7282 | |
| 7283 | void ggml_compute_forward_col2im_1d( |
| 7284 | const ggml_compute_params * params, |
| 7285 | ggml_tensor * dst) { |
| 7286 | switch (dst->src[0]->type) { |
| 7287 | case GGML_TYPE_F32: ggml_compute_forward_col2im_1d_impl<float> (params, dst); break; |
| 7288 | case GGML_TYPE_F16: ggml_compute_forward_col2im_1d_impl<ggml_fp16_t>(params, dst); break; |
| 7289 | case GGML_TYPE_BF16: ggml_compute_forward_col2im_1d_impl<ggml_bf16_t>(params, dst); break; |
| 7290 | default: GGML_ABORT("col2im_1d: unsupported type %d", dst->src[0]->type); |
| 7291 | } |
| 7292 | } |
| 7293 | |
| 7294 | // ggml_compute_forward_conv_2d |
| 7295 | |
| 7296 | |
| 7297 | static void ggml_compute_forward_conv_2d_impl(const ggml_compute_params * params, |
| 7298 | const ggml_tensor * kernel, // [KW, KH, IC, OC] |
| 7299 | const ggml_tensor * src, // [W, H, C, N] |
| 7300 | ggml_tensor * dst, // [OW, OH, OC, N] |
| 7301 | ggml_type kernel_type) { |
| 7302 | |
| 7303 | GGML_ASSERT(ggml_is_contiguous(kernel)); |
| 7304 | GGML_ASSERT(kernel_type == GGML_TYPE_F16 || kernel_type == GGML_TYPE_F32); |
| 7305 | GGML_ASSERT(kernel->type == kernel_type); |
| 7306 | |
| 7307 | const ggml_type_traits * traits = ggml_get_type_traits(kernel_type); |
| 7308 | |
| 7309 | const int32_t stride_x = dst->op_params[0]; |
| 7310 | const int32_t stride_y = dst->op_params[1]; |
| 7311 | const int32_t pad_x = dst->op_params[2]; |
| 7312 | const int32_t pad_y = dst->op_params[3]; |
| 7313 | const int32_t dilation_x = dst->op_params[4]; |
| 7314 | const int32_t dilation_y = dst->op_params[5]; |
| 7315 | |
| 7316 | const int64_t c_in = src->ne[2]; |
| 7317 | const int64_t c_out = kernel->ne[3]; |
| 7318 | GGML_ASSERT(c_in == kernel->ne[2]); |
| 7319 | |
| 7320 | const int64_t src_w = src->ne[0]; |
| 7321 | const int64_t src_h = src->ne[1]; |
| 7322 | const int64_t knl_w = kernel->ne[0]; |
| 7323 | const int64_t knl_h = kernel->ne[1]; |
| 7324 | const int64_t dst_w = dst->ne[0]; |
| 7325 | const int64_t dst_h = dst->ne[1]; |
| 7326 | |
| 7327 | const float * src_data = (float *) src->data; |
| 7328 | void * knl_data = kernel->data; |
| 7329 | float * dst_data = (float *) dst->data; |
| 7330 | |
| 7331 | const int64_t knl_n = knl_w * knl_h * c_in; |
| 7332 | const int64_t patch_total = dst->ne[3] * dst_w * dst_h; |
| 7333 | |
| 7334 | const int64_t space_per_patch = knl_n * traits->type_size + c_out * sizeof(float); |
| 7335 | const int64_t batch_size = params->wsize / space_per_patch; |
| 7336 | const int64_t patches_per_batch = batch_size > 8 ? (batch_size / 8) * 8 : batch_size; |
| 7337 | const int64_t batch_n = (patch_total + patches_per_batch - 1) / patches_per_batch; |
| 7338 | |
| 7339 | GGML_ASSERT(patches_per_batch > 0 && batch_size >= 1); |
| 7340 | |
| 7341 | void * tmp = params->wdata; |
| 7342 | |
| 7343 | for (int64_t batch_i = 0; batch_i < batch_n; ++batch_i) { |
| 7344 | |
| 7345 | const int64_t patch_start_batch = batch_i * patches_per_batch; |
| 7346 | const int64_t patch_end_batch = std::min(patch_start_batch + patches_per_batch, |
| 7347 | patch_total); |
| 7348 | const int64_t patch_n = patch_end_batch - patch_start_batch; |
| 7349 | |
| 7350 | const int64_t patch_per_thread = (patch_n + params->nth - 1) / params->nth; |
| 7351 | const int64_t patch_start = patch_start_batch + params->ith * patch_per_thread; |
| 7352 | const int64_t patch_end = std::min(patch_start + patch_per_thread, patch_end_batch); |
| 7353 | |
| 7354 | //im2col for a patch |
| 7355 | for (int64_t p = patch_start; p < patch_end; ++p) { |
| 7356 | const int64_t batch_n = p / (dst_w * dst_h); |
| 7357 | const int64_t src_x = (p / dst_w) % dst_h; |
| 7358 | const int64_t src_y = p % dst_w; |
| 7359 | |
| 7360 | const float * src_base = (const float *)((const char *)src_data + batch_n * src->nb[3]); |
| 7361 | char * dst_row = (char *) tmp + (p % patches_per_batch) * knl_n * traits->type_size; |
| 7362 | |
| 7363 | for (int64_t ic = 0; ic < c_in; ++ic) { |
| 7364 | for (int64_t ky = 0; ky < knl_h; ++ky) { |
| 7365 | for (int64_t kx = 0; kx < knl_w; ++kx) { |
| 7366 | const int64_t sy = src_x * stride_y + ky * dilation_y - pad_y; |
| 7367 | const int64_t sx = src_y * stride_x + kx * dilation_x - pad_x; |
| 7368 | |
| 7369 | int64_t dst_idx = ic * (knl_h * knl_w) + ky * knl_w + kx; |
| 7370 | |
| 7371 | float src_val; |
| 7372 | if (sy < 0 || sy >= src_h || sx < 0 || sx >= src_w) { |
| 7373 | src_val = 0.0f; |
| 7374 | } else { |
| 7375 | const float * src_ptr = (const float *)((const char *)src_base + sx * src->nb[0] + sy * src->nb[1] + ic * src->nb[2]); |
| 7376 | src_val = *src_ptr; |
| 7377 | } |
| 7378 | |
| 7379 | char * element_ptr = dst_row + dst_idx * traits->type_size; |
| 7380 | if (kernel_type == GGML_TYPE_F32) { |
| 7381 | *(float *) element_ptr = src_val; |
| 7382 | } else if (kernel_type == GGML_TYPE_F16) { |
| 7383 | *(ggml_fp16_t *) element_ptr = GGML_CPU_FP32_TO_FP16(src_val); |
| 7384 | } |
| 7385 | } |
| 7386 | } |
| 7387 | } |
| 7388 | } // patches handled by this thread |
| 7389 | |
| 7390 | ggml_barrier(params->threadpool); |
| 7391 | |
| 7392 | float * gemm_output = (float *) ((char *) tmp + patches_per_batch * knl_n * traits->type_size); |
| 7393 | |
| 7394 | GGML_ASSERT(gemm_output + patch_n * c_out <= (float*)tmp + params->wsize); |
| 7395 | |
| 7396 | // GEMM: patches[patch_n, knl_n] × kernel[knl_n, c_out] = output[patch_n, c_out] |
| 7397 | ggml_call_mul_mat(kernel_type, params, patch_n, c_out, knl_n, tmp, knl_data, gemm_output); |
| 7398 | |
| 7399 | ggml_barrier(params->threadpool); |
| 7400 | |
| 7401 | |
| 7402 | //permute back [OC, N, OH, OW] to [N, OC, OH, OW] |
| 7403 | const int64_t permute_per_thread = (patch_n + params->nth - 1) / params->nth; |
| 7404 | const int64_t permute_start = params->ith * permute_per_thread; |
| 7405 | const int64_t permute_end = std::min(permute_start + permute_per_thread, patch_n); |
| 7406 | |
| 7407 | for (int64_t i = permute_start; i < permute_end; ++i) { |
| 7408 | const int64_t p = patch_start_batch + i; |
| 7409 | const int64_t batch_n = p / (dst_w * dst_h); |
| 7410 | const int64_t dst_y = (p / dst_w) % dst_h; |
| 7411 | const int64_t dst_x = p % dst_w; |
| 7412 | |
| 7413 | for (int64_t oc = 0; oc < c_out; ++oc) { |
| 7414 | const float value = gemm_output[i * c_out + oc]; |
| 7415 | float * dst_ptr = (float *)((char *)dst_data + dst_x * dst->nb[0] + dst_y * dst->nb[1] + oc * dst->nb[2] + batch_n * dst->nb[3]); |
| 7416 | *dst_ptr = value; |
| 7417 | } |
| 7418 | } |
| 7419 | } |
| 7420 | } |
| 7421 | |
| 7422 | void ggml_compute_forward_conv_2d( |
| 7423 | const ggml_compute_params * params, |
| 7424 | ggml_tensor * dst) { |
| 7425 | |
| 7426 | const ggml_tensor * src0 = dst->src[0]; |
| 7427 | const ggml_tensor * src1 = dst->src[1]; |
| 7428 | |
| 7429 | ggml_compute_forward_conv_2d_impl(params, src0, src1, dst, src0->type); |
| 7430 | } |
| 7431 | |
| 7432 | // ggml_compute_forward_conv_3d |
| 7433 | |
| 7434 | static void ggml_compute_forward_conv_3d_impl(const ggml_compute_params * params, |
| 7435 | const ggml_tensor * kernel, |
| 7436 | const ggml_tensor * src, |
| 7437 | ggml_tensor * dst, |
| 7438 | ggml_type kernel_type) { |
| 7439 | |
| 7440 | GGML_ASSERT(ggml_is_contiguous(kernel)); |
| 7441 | GGML_ASSERT(kernel_type == GGML_TYPE_F16 || kernel_type == GGML_TYPE_F32); |
| 7442 | GGML_ASSERT(kernel->type == kernel_type); |
| 7443 | |
| 7444 | const ggml_type_traits * traits = ggml_get_type_traits(kernel_type); |
| 7445 | |
| 7446 | const int32_t s0 = dst->op_params[0]; |
| 7447 | const int32_t s1 = dst->op_params[1]; |
| 7448 | const int32_t s2 = dst->op_params[2]; |
| 7449 | const int32_t p0 = dst->op_params[3]; |
| 7450 | const int32_t p1 = dst->op_params[4]; |
| 7451 | const int32_t p2 = dst->op_params[5]; |
| 7452 | const int32_t d0 = dst->op_params[6]; |
| 7453 | const int32_t d1 = dst->op_params[7]; |
| 7454 | const int32_t d2 = dst->op_params[8]; |
| 7455 | const int32_t c = dst->op_params[9]; |
| 7456 | const int32_t n = dst->op_params[10]; |
| 7457 | const int32_t oc = dst->op_params[11]; |
| 7458 | |
| 7459 | const int64_t src_w = src->ne[0]; |
| 7460 | const int64_t src_h = src->ne[1]; |
| 7461 | const int64_t src_d = src->ne[2]; |
| 7462 | const int64_t knl_w = kernel->ne[0]; |
| 7463 | const int64_t knl_h = kernel->ne[1]; |
| 7464 | const int64_t knl_d = kernel->ne[2]; |
| 7465 | const int64_t dst_w = dst->ne[0]; |
| 7466 | const int64_t dst_h = dst->ne[1]; |
| 7467 | const int64_t dst_d = dst->ne[2]; |
| 7468 | |
| 7469 | const float * src_data = (float *) src->data; |
| 7470 | void * knl_data = kernel->data; |
| 7471 | float * dst_data = (float *) dst->data; |
| 7472 | |
| 7473 | const int64_t knl_n_per_channel = knl_w * knl_h * knl_d; |
| 7474 | const int64_t knl_n_total = knl_n_per_channel * c; |
| 7475 | const int64_t patch_total = n * dst_w * dst_h * dst_d; |
| 7476 | |
| 7477 | const int64_t space_per_patch = knl_n_total * traits->type_size + oc * sizeof(float); |
| 7478 | const int64_t batch_size = params->wsize / space_per_patch; |
| 7479 | const int64_t patches_per_batch = batch_size > 8 ? (batch_size / 8) * 8 : batch_size; |
| 7480 | const int64_t batch_n = (patch_total + patches_per_batch - 1) / patches_per_batch; |
| 7481 | |
| 7482 | GGML_ASSERT(patches_per_batch > 0 && batch_size >= 1); |
| 7483 | |
| 7484 | void * tmp = params->wdata; |
| 7485 | |
| 7486 | for (int64_t batch_i = 0; batch_i < batch_n; ++batch_i) { |
| 7487 | const int64_t patch_start_batch = batch_i * patches_per_batch; |
| 7488 | const int64_t patch_end_batch = std::min(patch_start_batch + patches_per_batch, patch_total); |
| 7489 | const int64_t patch_n_in_batch = patch_end_batch - patch_start_batch; |
| 7490 | |
| 7491 | const int64_t patch_per_thread = (patch_n_in_batch + params->nth - 1) / params->nth; |
| 7492 | const int64_t patch_start = patch_start_batch + params->ith * patch_per_thread; |
| 7493 | const int64_t patch_end = std::min(patch_start + patch_per_thread, patch_end_batch); |
| 7494 | |
| 7495 | for (int64_t p = patch_start; p < patch_end; ++p) { |
| 7496 | const int64_t p_in_batch = p % (dst_w * dst_h * dst_d); |
| 7497 | const int64_t p_in_depth = p_in_batch % (dst_w * dst_h); |
| 7498 | const int64_t batch_idx = p / (dst_w * dst_h * dst_d); |
| 7499 | const int64_t dst_z = p_in_batch / (dst_w * dst_h); |
| 7500 | const int64_t dst_y = p_in_depth / dst_w; |
| 7501 | const int64_t dst_x = p_in_depth % dst_w; |
| 7502 | |
| 7503 | char * dst_row = (char *) tmp + (p % patches_per_batch) * knl_n_total * traits->type_size; |
| 7504 | |
| 7505 | for (int64_t ic = 0; ic < c; ++ic) { |
| 7506 | for (int64_t kz = 0; kz < knl_d; ++kz) { |
| 7507 | for (int64_t ky = 0; ky < knl_h; ++ky) { |
| 7508 | for (int64_t kx = 0; kx < knl_w; ++kx) { |
| 7509 | const int64_t sz = dst_z * s2 + kz * d2 - p2; |
| 7510 | const int64_t sy = dst_y * s1 + ky * d1 - p1; |
| 7511 | const int64_t sx = dst_x * s0 + kx * d0 - p0; |
| 7512 | |
| 7513 | int64_t dst_idx = ic * knl_n_per_channel + kz * (knl_h * knl_w) + ky * knl_w + kx; |
| 7514 | |
| 7515 | float src_val; |
| 7516 | if (sz < 0 || sz >= src_d || sy < 0 || sy >= src_h || sx < 0 || sx >= src_w) { |
| 7517 | src_val = 0.0f; |
| 7518 | } else { |
| 7519 | const int64_t cn_idx = batch_idx * c + ic; |
| 7520 | const float * src_ptr = (const float *)((const char *)src_data + sx*src->nb[0] + sy*src->nb[1] + sz*src->nb[2] + cn_idx*src->nb[3]); |
| 7521 | src_val = *src_ptr; |
| 7522 | } |
| 7523 | |
| 7524 | char * element_ptr = dst_row + dst_idx * traits->type_size; |
| 7525 | if (kernel_type == GGML_TYPE_F32) { |
| 7526 | *(float *)element_ptr = src_val; |
| 7527 | } else if (kernel_type == GGML_TYPE_F16) { |
| 7528 | *(ggml_fp16_t *)element_ptr = GGML_CPU_FP32_TO_FP16(src_val); |
| 7529 | } |
| 7530 | } |
| 7531 | } |
| 7532 | } |
| 7533 | } |
| 7534 | } |
| 7535 | |
| 7536 | ggml_barrier(params->threadpool); |
| 7537 | |
| 7538 | float * gemm_output = (float *) ((char *) tmp + patches_per_batch * knl_n_total * traits->type_size); |
| 7539 | ggml_call_mul_mat(kernel_type, params, patch_n_in_batch, oc, knl_n_total, tmp, knl_data, gemm_output); |
| 7540 | |
| 7541 | ggml_barrier(params->threadpool); |
| 7542 | |
| 7543 | const int64_t permute_per_thread = (patch_n_in_batch + params->nth - 1) / params->nth; |
| 7544 | const int64_t permute_start = params->ith * permute_per_thread; |
| 7545 | const int64_t permute_end = std::min(permute_start + permute_per_thread, patch_n_in_batch); |
| 7546 | |
| 7547 | for (int64_t i = permute_start; i < permute_end; ++i) { |
| 7548 | const int64_t p = patch_start_batch + i; |
| 7549 | const int64_t p_in_batch = p % (dst_w * dst_h * dst_d); |
| 7550 | const int64_t p_in_depth = p_in_batch % (dst_w * dst_h); |
| 7551 | const int64_t batch_idx = p / (dst_w * dst_h * dst_d); |
| 7552 | const int64_t dst_z = p_in_batch / (dst_w * dst_h); |
| 7553 | const int64_t dst_y = p_in_depth / dst_w; |
| 7554 | const int64_t dst_x = p_in_depth % dst_w; |
| 7555 | |
| 7556 | for (int64_t ioc = 0; ioc < oc; ++ioc) { |
| 7557 | const float value = gemm_output[i * oc + ioc]; |
| 7558 | const int64_t ocn_idx = batch_idx * oc + ioc; |
| 7559 | float * dst_ptr = (float *)((char *)dst_data + dst_x*dst->nb[0] + dst_y*dst->nb[1] + dst_z*dst->nb[2] + ocn_idx*dst->nb[3]); |
| 7560 | *dst_ptr = value; |
| 7561 | } |
| 7562 | } |
| 7563 | } |
| 7564 | } |
| 7565 | |
| 7566 | void ggml_compute_forward_conv_3d( |
| 7567 | const ggml_compute_params * params, |
| 7568 | ggml_tensor * dst) { |
| 7569 | const ggml_tensor * src0 = dst->src[0]; |
| 7570 | const ggml_tensor * src1 = dst->src[1]; |
| 7571 | ggml_compute_forward_conv_3d_impl(params, src0, src1, dst, src0->type); |
| 7572 | } |
| 7573 | |
| 7574 | template <typename kernel_t> |
| 7575 | static void ggml_compute_forward_conv_transpose_2d_impl( |
| 7576 | const ggml_compute_params * params, |
| 7577 | ggml_tensor * dst) { |
| 7578 | |
| 7579 | const ggml_tensor * src0 = dst->src[0]; |
| 7580 | const ggml_tensor * src1 = dst->src[1]; |
| 7581 | |
| 7582 | GGML_ASSERT(src0->type == GGML_TYPE_F16 || src0->type == GGML_TYPE_F32); |
| 7583 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 7584 | GGML_ASSERT( dst->type == GGML_TYPE_F32); |
| 7585 | |
| 7586 | GGML_TENSOR_BINARY_OP_LOCALS |
| 7587 | |
| 7588 | const int ith = params->ith; |
| 7589 | const int nth = params->nth; |
| 7590 | |
| 7591 | const int nk = ne00*ne01*ne02*ne03; |
| 7592 | |
| 7593 | GGML_ASSERT(nb00 == ggml_type_size(src0->type)); |
| 7594 | GGML_ASSERT(nb10 == sizeof(float)); |
| 7595 | |
| 7596 | if (ith == 0) { |
| 7597 | memset(params->wdata, 0, params->wsize); |
| 7598 | |
| 7599 | // permute kernel data (src0) from (Kw x Kh x Cout x Cin) to (Cin x Kw x Kh x Cout) |
| 7600 | { |
| 7601 | kernel_t * const wdata = (kernel_t *) params->wdata + 0; |
| 7602 | |
| 7603 | for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 7604 | for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 7605 | const kernel_t * const src = (kernel_t *)((char *) src0->data + i03*nb03 + i02*nb02); |
| 7606 | kernel_t * dst_data = wdata + i02*ne01*ne00*ne03; |
| 7607 | for (int64_t i01 = 0; i01 < ne01; i01++) { |
| 7608 | for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 7609 | dst_data[i01*ne00*ne03 + i00*ne03 + i03] = src[i01 * ne00 + i00]; |
| 7610 | } |
| 7611 | } |
| 7612 | } |
| 7613 | } |
| 7614 | } |
| 7615 | |
| 7616 | // permute source data (src1) from (Sw x Sh x Cin) to (Cin x Sw x Sh), for all batches |
| 7617 | { |
| 7618 | kernel_t * const wdata = (kernel_t *) params->wdata + nk; |
| 7619 | for (int i13 = 0; i13 < ne13; i13++) { |
| 7620 | kernel_t * const wdata_b = wdata + i13*ne10*ne11*ne12; |
| 7621 | for (int i12 = 0; i12 < ne12; i12++) { |
| 7622 | for (int i11 = 0; i11 < ne11; i11++) { |
| 7623 | const float * const src = (float *)((char *) src1->data + i13*nb13 + i12*nb12 + i11*nb11); |
| 7624 | kernel_t * dst_data = wdata_b + i11*ne10*ne12; |
| 7625 | for (int i10 = 0; i10 < ne10; i10++) { |
| 7626 | if constexpr (std::is_same_v<kernel_t, ggml_fp16_t>) { |
| 7627 | dst_data[i10*ne12 + i12] = GGML_CPU_FP32_TO_FP16(src[i10]); |
| 7628 | } else { |
| 7629 | dst_data[i10*ne12 + i12] = src[i10]; |
| 7630 | } |
| 7631 | } |
| 7632 | } |
| 7633 | } |
| 7634 | } |
| 7635 | } |
| 7636 | |
| 7637 | memset(dst->data, 0, ggml_nbytes(dst)); |
| 7638 | } |
| 7639 | ggml_barrier(params->threadpool); |
| 7640 | |
| 7641 | const int32_t stride = ggml_get_op_params_i32(dst, 0); |
| 7642 | |
| 7643 | // total patches in dst |
| 7644 | const int np = ne2; |
| 7645 | |
| 7646 | // patches per thread |
| 7647 | const int dp = (np + nth - 1)/nth; |
| 7648 | |
| 7649 | // patch range for this thread |
| 7650 | const int ip0 = dp*ith; |
| 7651 | const int ip1 = MIN(ip0 + dp, np); |
| 7652 | |
| 7653 | kernel_t * const wdata = (kernel_t *) params->wdata + 0; |
| 7654 | kernel_t * const wdata_src = wdata + nk; |
| 7655 | |
| 7656 | for (int i2 = ip0; i2 < ip1; i2++) { // Cout |
| 7657 | kernel_t * wdata_kernel = wdata + i2*ne01*ne00*ne03; |
| 7658 | for (int i3 = 0; i3 < ne3; i3++) { // batch |
| 7659 | float * dst_data = (float *)((char *) dst->data + i3*nb3 + i2*nb2); |
| 7660 | kernel_t * wdata_src_b = wdata_src + i3*ne10*ne11*ne12; |
| 7661 | for (int i11 = 0; i11 < ne11; i11++) { |
| 7662 | for (int i10 = 0; i10 < ne10; i10++) { |
| 7663 | const int i1n = i11*ne10*ne12 + i10*ne12; |
| 7664 | for (int i01 = 0; i01 < ne01; i01++) { |
| 7665 | for (int i00 = 0; i00 < ne00; i00++) { |
| 7666 | float v = 0; |
| 7667 | if constexpr (std::is_same_v<kernel_t, ggml_fp16_t>) { |
| 7668 | ggml_vec_dot_f16(ne03, &v, 0, |
| 7669 | wdata_src_b + i1n, 0, |
| 7670 | wdata_kernel + i01*ne00*ne03 + i00*ne03, 0, 1); |
| 7671 | } else { |
| 7672 | ggml_vec_dot_f32(ne03, &v, 0, |
| 7673 | wdata_src_b + i1n, 0, |
| 7674 | wdata_kernel + i01*ne00*ne03 + i00*ne03, 0, 1); |
| 7675 | } |
| 7676 | dst_data[(i11*stride + i01)*ne0 + i10*stride + i00] += v; |
| 7677 | } |
| 7678 | } |
| 7679 | } |
| 7680 | } |
| 7681 | } |
| 7682 | } |
| 7683 | } |
| 7684 | |
| 7685 | void ggml_compute_forward_conv_transpose_2d( |
| 7686 | const ggml_compute_params * params, |
| 7687 | ggml_tensor * dst) { |
| 7688 | |
| 7689 | const ggml_tensor * src0 = dst->src[0]; |
| 7690 | |
| 7691 | switch (src0->type) { |
| 7692 | case GGML_TYPE_F16: |
| 7693 | { |
| 7694 | ggml_compute_forward_conv_transpose_2d_impl<ggml_fp16_t>(params, dst); |
| 7695 | } break; |
| 7696 | case GGML_TYPE_F32: |
| 7697 | { |
| 7698 | ggml_compute_forward_conv_transpose_2d_impl<float>(params, dst); |
| 7699 | } break; |
| 7700 | default: |
| 7701 | { |
| 7702 | GGML_ABORT("fatal error"); |
| 7703 | } |
| 7704 | } |
| 7705 | } |
| 7706 | |
| 7707 | // ggml_compute_forward_conv_2d_dw |
| 7708 | |
| 7709 | struct ggml_conv_2d_dw_params { |
| 7710 | int64_t channels; |
| 7711 | int64_t batch; |
| 7712 | int64_t src_w; |
| 7713 | int64_t src_h; |
| 7714 | int64_t dst_w; |
| 7715 | int64_t dst_h; |
| 7716 | int64_t knl_w; |
| 7717 | int64_t knl_h; |
| 7718 | int stride_x; |
| 7719 | int stride_y; |
| 7720 | int pad_x; |
| 7721 | int pad_y; |
| 7722 | int dilation_x; |
| 7723 | int dilation_y; |
| 7724 | }; |
| 7725 | |
| 7726 | static inline float ggml_conv_2d_dw_knl_f32(const char * data, int64_t i, ggml_type type) { |
| 7727 | if (type == GGML_TYPE_F16) { |
| 7728 | return GGML_FP16_TO_FP32(((const ggml_fp16_t *)data)[i]); |
| 7729 | } |
| 7730 | return ((const float *)data)[i]; |
| 7731 | } |
| 7732 | |
| 7733 | static void ggml_compute_forward_conv_2d_dw_cwhn( |
| 7734 | const ggml_compute_params * params, |
| 7735 | const ggml_tensor * src, |
| 7736 | const ggml_tensor * kernel, |
| 7737 | ggml_tensor * dst, |
| 7738 | const ggml_conv_2d_dw_params & p) { |
| 7739 | |
| 7740 | const int64_t c = p.channels; |
| 7741 | const char * knl_data = (const char *)kernel->data; |
| 7742 | const ggml_type knl_type = kernel->type; |
| 7743 | |
| 7744 | const int64_t rows_total = p.dst_h * p.batch; |
| 7745 | const int64_t rows_per_thread = (rows_total + params->nth - 1) / params->nth; |
| 7746 | const int64_t row_start = params->ith * rows_per_thread; |
| 7747 | const int64_t row_end = MIN(row_start + rows_per_thread, rows_total); |
| 7748 | |
| 7749 | #ifdef GGML_SIMD |
| 7750 | int64_t c_pkg_end = 0; |
| 7751 | int64_t pkg_size = GGML_F32_EPR; |
| 7752 | if (knl_type == GGML_TYPE_F32) { |
| 7753 | #if defined(__ARM_FEATURE_SVE) |
| 7754 | pkg_size = svcntw(); |
| 7755 | #else |
| 7756 | pkg_size = GGML_F32_EPR; |
| 7757 | #endif |
| 7758 | c_pkg_end = (c / pkg_size) * pkg_size; |
| 7759 | } |
| 7760 | #else |
| 7761 | const int64_t c_pkg_end = 0; |
| 7762 | #endif |
| 7763 | |
| 7764 | for (int64_t row = row_start; row < row_end; ++row) { |
| 7765 | const int64_t dst_y = row % p.dst_h; |
| 7766 | const float * src_data = (const float *)src->data + (row / p.dst_h) * p.src_w * p.src_h * c; |
| 7767 | for (int64_t dst_x = 0; dst_x < p.dst_w; ++dst_x) { |
| 7768 | float * dst_data = (float *)dst->data + (row * p.dst_w + dst_x) * c; |
| 7769 | const int64_t src_y_base = dst_y * p.stride_y - p.pad_y; |
| 7770 | const int64_t src_x_base = dst_x * p.stride_x - p.pad_x; |
| 7771 | |
| 7772 | #ifdef GGML_SIMD |
| 7773 | for (int64_t c_i = 0; c_i < c_pkg_end; c_i += pkg_size) { |
| 7774 | GGML_F32_VEC sum = GGML_F32_VEC_ZERO; |
| 7775 | for (int64_t knl_y = 0; knl_y < p.knl_h; ++knl_y) { |
| 7776 | const int64_t src_y = src_y_base + knl_y * p.dilation_y; |
| 7777 | if (src_y < 0 || src_y >= p.src_h) { |
| 7778 | continue; |
| 7779 | } |
| 7780 | for (int64_t knl_x = 0; knl_x < p.knl_w; ++knl_x) { |
| 7781 | const int64_t src_x = src_x_base + knl_x * p.dilation_x; |
| 7782 | if (src_x < 0 || src_x >= p.src_w) { |
| 7783 | continue; |
| 7784 | } |
| 7785 | const float * kp = (const float *)knl_data + (knl_y * p.knl_w + knl_x) * c + c_i; |
| 7786 | GGML_F32_VEC k = GGML_F32_VEC_LOAD(kp); |
| 7787 | GGML_F32_VEC s = GGML_F32_VEC_LOAD(src_data + (src_y * p.src_w + src_x) * c + c_i); |
| 7788 | sum = GGML_F32_VEC_FMA(sum, k, s); |
| 7789 | } |
| 7790 | } |
| 7791 | GGML_F32_VEC_STORE(dst_data + c_i, sum); |
| 7792 | } |
| 7793 | #endif |
| 7794 | for (int64_t c_i = c_pkg_end; c_i < c; ++c_i) { |
| 7795 | float sum = 0.0f; |
| 7796 | for (int64_t knl_y = 0; knl_y < p.knl_h; ++knl_y) { |
| 7797 | const int64_t src_y = src_y_base + knl_y * p.dilation_y; |
| 7798 | if (src_y < 0 || src_y >= p.src_h) { |
| 7799 | continue; |
| 7800 | } |
| 7801 | for (int64_t knl_x = 0; knl_x < p.knl_w; ++knl_x) { |
| 7802 | const int64_t src_x = src_x_base + knl_x * p.dilation_x; |
| 7803 | if (src_x < 0 || src_x >= p.src_w) { |
| 7804 | continue; |
| 7805 | } |
| 7806 | sum += ggml_conv_2d_dw_knl_f32(knl_data, (knl_y * p.knl_w + knl_x) * c + c_i, knl_type) |
| 7807 | * src_data[(src_y * p.src_w + src_x) * c + c_i]; |
| 7808 | } |
| 7809 | } |
| 7810 | dst_data[c_i] = sum; |
| 7811 | } |
| 7812 | } |
| 7813 | } |
| 7814 | } |
| 7815 | |
| 7816 | static void ggml_compute_forward_conv_2d_dw_whcn( |
| 7817 | const ggml_compute_params * params, |
| 7818 | const ggml_tensor * src, |
| 7819 | const ggml_tensor * kernel, |
| 7820 | ggml_tensor * dst, |
| 7821 | const ggml_conv_2d_dw_params & p) { |
| 7822 | |
| 7823 | const int64_t n = p.channels * p.batch; |
| 7824 | const int64_t per_thread = (n + params->nth - 1) / params->nth; |
| 7825 | const int64_t start = params->ith * per_thread; |
| 7826 | const int64_t end = MIN(start + per_thread, n); |
| 7827 | const char * knl_base = (const char *)kernel->data; |
| 7828 | const ggml_type knl_type = kernel->type; |
| 7829 | |
| 7830 | for (int64_t i = start; i < end; ++i) { |
| 7831 | const int64_t knl_offset = (i % p.channels) * p.knl_w * p.knl_h; |
| 7832 | const float * src_data = (const float *)src->data + i * p.src_w * p.src_h; |
| 7833 | float * dst_data = (float *)dst->data + i * p.dst_w * p.dst_h; |
| 7834 | |
| 7835 | for (int64_t dst_y = 0; dst_y < p.dst_h; ++dst_y) { |
| 7836 | for (int64_t dst_x = 0; dst_x < p.dst_w; ++dst_x) { |
| 7837 | |
| 7838 | float sum = 0.0f; |
| 7839 | for (int64_t knl_y = 0; knl_y < p.knl_h; ++knl_y) { |
| 7840 | const int64_t src_y = dst_y * p.stride_y + knl_y * p.dilation_y - p.pad_y; |
| 7841 | if (src_y < 0 || src_y >= p.src_h) { |
| 7842 | continue; |
| 7843 | } |
| 7844 | for (int64_t knl_x = 0; knl_x < p.knl_w; ++knl_x) { |
| 7845 | const int64_t src_x = dst_x * p.stride_x + knl_x * p.dilation_x - p.pad_x; |
| 7846 | if (src_x < 0 || src_x >= p.src_w) { |
| 7847 | continue; |
| 7848 | } |
| 7849 | sum += ggml_conv_2d_dw_knl_f32(knl_base, knl_offset + knl_y * p.knl_w + knl_x, knl_type) |
| 7850 | * src_data[src_y * p.src_w + src_x]; |
| 7851 | } |
| 7852 | } |
| 7853 | dst_data[dst_y * p.dst_w + dst_x] = sum; |
| 7854 | } |
| 7855 | } |
| 7856 | } |
| 7857 | } |
| 7858 | |
| 7859 | void ggml_compute_forward_conv_2d_dw( |
| 7860 | const ggml_compute_params * params, |
| 7861 | ggml_tensor * dst) { |
| 7862 | |
| 7863 | const ggml_tensor * kernel = dst->src[0]; |
| 7864 | const ggml_tensor * src = dst->src[1]; |
| 7865 | ggml_conv_2d_dw_params p; |
| 7866 | p.channels = src->ne[2]; |
| 7867 | p.batch = src->ne[3]; |
| 7868 | p.src_w = src->ne[0]; |
| 7869 | p.src_h = src->ne[1]; |
| 7870 | p.dst_w = dst->ne[0]; |
| 7871 | p.dst_h = dst->ne[1]; |
| 7872 | p.knl_w = kernel->ne[0]; |
| 7873 | p.knl_h = kernel->ne[1]; |
| 7874 | p.stride_x = dst->op_params[0]; |
| 7875 | p.stride_y = dst->op_params[1]; |
| 7876 | p.pad_x = dst->op_params[2]; |
| 7877 | p.pad_y = dst->op_params[3]; |
| 7878 | p.dilation_x = dst->op_params[4]; |
| 7879 | p.dilation_y = dst->op_params[5]; |
| 7880 | |
| 7881 | GGML_ASSERT(kernel->type == GGML_TYPE_F32 || kernel->type == GGML_TYPE_F16); |
| 7882 | GGML_ASSERT(kernel->ne[3] == p.channels); |
| 7883 | GGML_ASSERT(dst->ne[3] == p.batch); |
| 7884 | |
| 7885 | if (ggml_is_contiguous(src)) { |
| 7886 | ggml_compute_forward_conv_2d_dw_whcn(params, src, kernel, dst, p); |
| 7887 | } else if (ggml_is_contiguous_channels(src)) { |
| 7888 | GGML_ASSERT(kernel->nb[0] >= kernel->nb[2] && kernel->nb[1] >= kernel->nb[0]); |
| 7889 | ggml_compute_forward_conv_2d_dw_cwhn(params, src, kernel, dst, p); |
| 7890 | } else { |
| 7891 | GGML_ABORT("non-contiguous memory layout not supported"); |
| 7892 | } |
| 7893 | } |
| 7894 | |
| 7895 | // ggml_compute_forward_pool_1d_ksp |
| 7896 | static void ggml_compute_forward_pool_1d_ksp( |
| 7897 | const ggml_compute_params * params, |
| 7898 | const ggml_op_pool op, |
| 7899 | const int k, |
| 7900 | const int s, |
| 7901 | const int p, |
| 7902 | ggml_tensor * dst) { |
| 7903 | |
| 7904 | const ggml_tensor * src = dst->src[0]; |
| 7905 | |
| 7906 | assert(src->type == GGML_TYPE_F32 || src->type == GGML_TYPE_F16); |
| 7907 | |
| 7908 | if (params->ith != 0) { |
| 7909 | return; |
| 7910 | } |
| 7911 | |
| 7912 | const int64_t IW = src->ne[0]; |
| 7913 | const int64_t OW = dst->ne[0]; |
| 7914 | |
| 7915 | const int64_t nr = ggml_nrows(src); |
| 7916 | |
| 7917 | for (int64_t ir = 0; ir < nr; ++ir) { |
| 7918 | const char * srow_bytes = (const char *) src->data + ir * src->nb[1]; |
| 7919 | float * drow = (float *) (( char *) dst->data + ir * dst->nb[1]); |
| 7920 | |
| 7921 | for (int64_t ow = 0; ow < OW; ++ow) { |
| 7922 | float res = 0; |
| 7923 | switch (op) { |
| 7924 | case GGML_OP_POOL_AVG: res = 0.0f; break; |
| 7925 | case GGML_OP_POOL_MAX: res = -FLT_MAX; break; |
| 7926 | case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error"); |
| 7927 | } |
| 7928 | |
| 7929 | int count = 0; |
| 7930 | const int base = (int) ow * s - p; |
| 7931 | |
| 7932 | for (int ki = 0; ki < k; ++ki) { |
| 7933 | const int j = base + ki; |
| 7934 | if (j < 0 || j >= (int) IW) { |
| 7935 | continue; |
| 7936 | } |
| 7937 | |
| 7938 | float v; |
| 7939 | if (src->type == GGML_TYPE_F32) { |
| 7940 | v = ((const float *) srow_bytes)[j]; |
| 7941 | } else { |
| 7942 | v = GGML_CPU_FP16_TO_FP32(((const ggml_fp16_t *) srow_bytes)[j]); |
| 7943 | } |
| 7944 | |
| 7945 | switch (op) { |
| 7946 | case GGML_OP_POOL_AVG: res += v; break; |
| 7947 | case GGML_OP_POOL_MAX: res = std::max(v, res); break; |
| 7948 | case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error"); |
| 7949 | } |
| 7950 | |
| 7951 | ++count; |
| 7952 | } |
| 7953 | |
| 7954 | switch (op) { |
| 7955 | case GGML_OP_POOL_AVG: res = (count > 0) ? (res / count) : 0.0f; break; |
| 7956 | case GGML_OP_POOL_MAX: break; |
| 7957 | case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error"); |
| 7958 | } |
| 7959 | |
| 7960 | drow[ow] = res; |
| 7961 | } |
| 7962 | } |
| 7963 | } |
| 7964 | |
| 7965 | // ggml_compute_forward_pool_1d |
| 7966 | |
| 7967 | void ggml_compute_forward_pool_1d( |
| 7968 | const ggml_compute_params * params, |
| 7969 | ggml_tensor * dst) { |
| 7970 | |
| 7971 | const int32_t * opts = (const int32_t *)dst->op_params; |
| 7972 | ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]); |
| 7973 | const int k0 = opts[1]; |
| 7974 | const int s0 = opts[2]; |
| 7975 | const int p0 = opts[3]; |
| 7976 | |
| 7977 | ggml_compute_forward_pool_1d_ksp(params, op, k0, s0, p0, dst); |
| 7978 | } |
| 7979 | |
| 7980 | // ggml_compute_forward_pool_2d |
| 7981 | |
| 7982 | void ggml_compute_forward_pool_2d( |
| 7983 | const ggml_compute_params * params, |
| 7984 | ggml_tensor * dst) { |
| 7985 | |
| 7986 | const ggml_tensor * src = dst->src[0]; |
| 7987 | |
| 7988 | assert(src->type == GGML_TYPE_F32 || src->type == GGML_TYPE_F16); |
| 7989 | |
| 7990 | if (params->ith != 0) { |
| 7991 | return; |
| 7992 | } |
| 7993 | |
| 7994 | const int32_t * opts = (const int32_t *)dst->op_params; |
| 7995 | |
| 7996 | ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]); |
| 7997 | const int k0 = opts[1]; |
| 7998 | const int k1 = opts[2]; |
| 7999 | const int s0 = opts[3]; |
| 8000 | const int s1 = opts[4]; |
| 8001 | const int p0 = opts[5]; |
| 8002 | const int p1 = opts[6]; |
| 8003 | const char * cdata = (const char*)src->data; |
| 8004 | const char * const data_end = cdata + ggml_nbytes(src); |
| 8005 | |
| 8006 | const int64_t px = dst->ne[0]; |
| 8007 | const int64_t py = dst->ne[1]; |
| 8008 | const int64_t pa = px * py; |
| 8009 | |
| 8010 | float * dplane = (float *)dst->data; |
| 8011 | |
| 8012 | const int ka = k0 * k1; |
| 8013 | const int offset0 = -p0; |
| 8014 | const int offset1 = -p1; |
| 8015 | |
| 8016 | while (cdata < data_end) { |
| 8017 | for (int oy = 0; oy < py; ++oy) { |
| 8018 | float * const drow = dplane + oy * px; |
| 8019 | float * const out = drow; |
| 8020 | |
| 8021 | for (int ox = 0; ox < px; ++ox) { |
| 8022 | float res = 0; |
| 8023 | switch (op) { |
| 8024 | case GGML_OP_POOL_AVG: res = 0; break; |
| 8025 | case GGML_OP_POOL_MAX: res = -FLT_MAX; break; |
| 8026 | case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error"); |
| 8027 | } |
| 8028 | |
| 8029 | const int ix = offset0 + ox * s0; |
| 8030 | const int iy = offset1 + oy * s1; |
| 8031 | |
| 8032 | for (int ky = 0; ky < k1; ++ky) { |
| 8033 | if (iy + ky < 0 || iy + ky >= src->ne[1]) { |
| 8034 | continue; |
| 8035 | } |
| 8036 | |
| 8037 | const void * srow = (const void *)(cdata + src->nb[1] * (iy + ky)); |
| 8038 | for (int kx = 0; kx < k0; ++kx) { |
| 8039 | int j = ix + kx; |
| 8040 | if (j < 0 || j >= src->ne[0]) { |
| 8041 | continue; |
| 8042 | } |
| 8043 | |
| 8044 | const float srow_j = (src->type == GGML_TYPE_F32) ? ((const float*)srow)[j] : GGML_CPU_FP16_TO_FP32(((const ggml_fp16_t*)srow)[j]); |
| 8045 | switch (op) { |
| 8046 | case GGML_OP_POOL_AVG: res += srow_j; break; |
| 8047 | case GGML_OP_POOL_MAX: res = std::max(srow_j, res); break; |
| 8048 | case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error"); |
| 8049 | } |
| 8050 | } |
| 8051 | } |
| 8052 | switch (op) { |
| 8053 | case GGML_OP_POOL_AVG: res /= ka; break; |
| 8054 | case GGML_OP_POOL_MAX: break; |
| 8055 | case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error"); |
| 8056 | } |
| 8057 | |
| 8058 | out[ox] = res; |
| 8059 | } |
| 8060 | } |
| 8061 | |
| 8062 | cdata += src->nb[2]; |
| 8063 | dplane += pa; |
| 8064 | } |
| 8065 | } |
| 8066 | |
| 8067 | // ggml_compute_forward_pool_2d_back |
| 8068 | |
| 8069 | void ggml_compute_forward_pool_2d_back( |
| 8070 | const ggml_compute_params * params, |
| 8071 | ggml_tensor * dst) { |
| 8072 | |
| 8073 | const ggml_tensor * src = dst->src[0]; |
| 8074 | const ggml_tensor * dstf = dst->src[1]; // forward tensor of dst |
| 8075 | |
| 8076 | assert(dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16); |
| 8077 | |
| 8078 | if (params->ith != 0) { |
| 8079 | return; |
| 8080 | } |
| 8081 | |
| 8082 | const int32_t * opts = (const int32_t *)dst->op_params; |
| 8083 | ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]); |
| 8084 | const int k0 = opts[1]; |
| 8085 | const int k1 = opts[2]; |
| 8086 | const int s0 = opts[3]; |
| 8087 | const int s1 = opts[4]; |
| 8088 | const int p0 = opts[5]; |
| 8089 | const int p1 = opts[6]; |
| 8090 | |
| 8091 | char * cdata = (char *) dst->data; |
| 8092 | const char * cdataf = (const char *) dstf->data; |
| 8093 | const char * const data_end = cdata + ggml_nbytes(dst); |
| 8094 | |
| 8095 | GGML_ASSERT(params->ith == 0); |
| 8096 | memset(cdata, 0, ggml_nbytes(dst)); |
| 8097 | |
| 8098 | const int64_t px = src->ne[0]; |
| 8099 | const int64_t py = src->ne[1]; |
| 8100 | const int64_t pa = px * py; |
| 8101 | |
| 8102 | const float * splane = (const float *) src->data; |
| 8103 | |
| 8104 | const int ka = k0 * k1; |
| 8105 | const int offset0 = -p0; |
| 8106 | const int offset1 = -p1; |
| 8107 | |
| 8108 | while (cdata < data_end) { |
| 8109 | for (int oy = 0; oy < py; ++oy) { |
| 8110 | const float * const srow = splane + oy * px; |
| 8111 | for (int ox = 0; ox < px; ++ox) { |
| 8112 | const float grad0 = srow[ox]; |
| 8113 | |
| 8114 | const int ix = offset0 + ox * s0; |
| 8115 | const int iy = offset1 + oy * s1; |
| 8116 | |
| 8117 | if (op == GGML_OP_POOL_MAX) { |
| 8118 | float maxval = -FLT_MAX; |
| 8119 | int kxmax = -1; |
| 8120 | int kymax = -1; |
| 8121 | |
| 8122 | for (int ky = 0; ky < k1; ++ky) { |
| 8123 | if (iy + ky < 0 || iy + ky >= dst->ne[1]) { |
| 8124 | continue; |
| 8125 | } |
| 8126 | const void * drowf = (const void *)(cdataf + dst->nb[1] * (iy + ky)); |
| 8127 | for (int kx = 0; kx < k0; ++kx) { |
| 8128 | int j = ix + kx; |
| 8129 | if (j < 0 || j >= dst->ne[0]) { |
| 8130 | continue; |
| 8131 | } |
| 8132 | |
| 8133 | const float val = dst->type == GGML_TYPE_F32 ? |
| 8134 | ((const float *) drowf)[j] : GGML_CPU_FP16_TO_FP32(((const ggml_fp16_t *) drowf)[j]); |
| 8135 | if (val <= maxval) { |
| 8136 | continue; |
| 8137 | } |
| 8138 | |
| 8139 | maxval = val; |
| 8140 | kxmax = kx; |
| 8141 | kymax = ky; |
| 8142 | } |
| 8143 | } |
| 8144 | |
| 8145 | if (kxmax == -1 || kymax == -1) { |
| 8146 | continue; |
| 8147 | } |
| 8148 | |
| 8149 | void * drow = (void *)(cdata + dst->nb[1] * (iy + kymax)); |
| 8150 | const int j = ix + kxmax; |
| 8151 | if (dst->type == GGML_TYPE_F32) { |
| 8152 | ((float *) drow)[j] += grad0; |
| 8153 | } else { |
| 8154 | ((ggml_fp16_t *) drow)[j] = GGML_CPU_FP32_TO_FP16(grad0 + GGML_CPU_FP16_TO_FP32(((const ggml_fp16_t *) drow)[j])); |
| 8155 | } |
| 8156 | } else if (op == GGML_OP_POOL_AVG) { |
| 8157 | const float grad = grad0 / ka; |
| 8158 | |
| 8159 | for (int ky = 0; ky < k1; ++ky) { |
| 8160 | if (iy + ky < 0 || iy + ky >= dst->ne[1]) { |
| 8161 | continue; |
| 8162 | } |
| 8163 | void * drow = (void *)(cdata + dst->nb[1] * (iy + ky)); |
| 8164 | for (int kx = 0; kx < k0; ++kx) { |
| 8165 | int j = ix + kx; |
| 8166 | if (j < 0 || j >= dst->ne[0]) { |
| 8167 | continue; |
| 8168 | } |
| 8169 | |
| 8170 | if (dst->type == GGML_TYPE_F32) { |
| 8171 | ((float *) drow)[j] += grad; |
| 8172 | } else { |
| 8173 | ((ggml_fp16_t *) drow)[j] += GGML_CPU_FP32_TO_FP16(grad); |
| 8174 | } |
| 8175 | } |
| 8176 | } |
| 8177 | } else { |
| 8178 | GGML_ASSERT(false); |
| 8179 | } |
| 8180 | } |
| 8181 | } |
| 8182 | |
| 8183 | cdata += dst->nb[2]; |
| 8184 | cdataf += dst->nb[2]; |
| 8185 | splane += pa; |
| 8186 | } |
| 8187 | } |
| 8188 | |
| 8189 | // ggml_compute_forward_upscale |
| 8190 | |
| 8191 | static void ggml_compute_forward_upscale_f32( |
| 8192 | const ggml_compute_params * params, |
| 8193 | ggml_tensor * dst) { |
| 8194 | |
| 8195 | const ggml_tensor * src0 = dst->src[0]; |
| 8196 | |
| 8197 | GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 8198 | |
| 8199 | const int ith = params->ith; |
| 8200 | const int nth = params->nth; |
| 8201 | |
| 8202 | GGML_TENSOR_UNARY_OP_LOCALS |
| 8203 | |
| 8204 | float sf0 = (float)ne0/src0->ne[0]; |
| 8205 | float sf1 = (float)ne1/src0->ne[1]; |
| 8206 | float sf2 = (float)ne2/src0->ne[2]; |
| 8207 | float sf3 = (float)ne3/src0->ne[3]; |
| 8208 | float pixel_offset = 0.5f; |
| 8209 | |
| 8210 | const int32_t mode_flags = ggml_get_op_params_i32(dst, 0); |
| 8211 | const ggml_scale_mode mode = (ggml_scale_mode) (mode_flags & 0xFF); |
| 8212 | |
| 8213 | if (mode_flags & GGML_SCALE_FLAG_ALIGN_CORNERS) { |
| 8214 | pixel_offset = 0.0f; |
| 8215 | sf0 = ne0 > 1 && ne00 > 1 ? (float)(ne0 - 1) / (ne00 - 1) : sf0; |
| 8216 | sf1 = ne1 > 1 && ne01 > 1 ? (float)(ne1 - 1) / (ne01 - 1) : sf1; |
| 8217 | } |
| 8218 | |
| 8219 | if (mode == GGML_SCALE_MODE_NEAREST) { |
| 8220 | for (int64_t i3 = 0; i3 < ne3; i3++) { |
| 8221 | const int64_t i03 = i3 / sf3; |
| 8222 | for (int64_t i2 = ith; i2 < ne2; i2 += nth) { |
| 8223 | const int64_t i02 = i2 / sf2; |
| 8224 | for (int64_t i1 = 0; i1 < ne1; i1++) { |
| 8225 | const int64_t i01 = i1 / sf1; |
| 8226 | for (int64_t i0 = 0; i0 < ne0; i0++) { |
| 8227 | const int64_t i00 = i0 / sf0; |
| 8228 | |
| 8229 | const float * x = (float *)((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03); |
| 8230 | float * y = (float *)((char *) dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3); |
| 8231 | |
| 8232 | *y = *x; |
| 8233 | } |
| 8234 | } |
| 8235 | } |
| 8236 | } |
| 8237 | } else if (mode == GGML_SCALE_MODE_BILINEAR && (mode_flags & GGML_SCALE_FLAG_ANTIALIAS)) { |
| 8238 | // Similar to F.interpolate(..., mode="bilinear", align_corners=False, antialias=True) |
| 8239 | // https://github.com/pytorch/pytorch/blob/8871ff29b743948d1225389d5b7068f37b22750b/aten/src/ATen/native/cpu/UpSampleKernel.cpp |
| 8240 | auto triangle_filter = [](float x) -> float { |
| 8241 | return std::max(1.0f - fabsf(x), 0.0f); |
| 8242 | }; |
| 8243 | |
| 8244 | // support and invscale, minimum 1 pixel for bilinear |
| 8245 | const float support1 = std::max(1.0f, 1.0f / sf1); |
| 8246 | const float invscale1 = 1.0f / support1; |
| 8247 | const float support0 = std::max(1.0f, 1.0f / sf0); |
| 8248 | const float invscale0 = 1.0f / support0; |
| 8249 | |
| 8250 | for (int64_t i3 = 0; i3 < ne3; i3++) { |
| 8251 | const int64_t i03 = i3 / sf3; |
| 8252 | for (int64_t i2 = ith; i2 < ne2; i2 += nth) { |
| 8253 | const int64_t i02 = i2 / sf2; |
| 8254 | for (int64_t i1 = 0; i1 < ne1; i1++) { |
| 8255 | const float y = ((float) i1 + pixel_offset) / sf1; |
| 8256 | for (int64_t i0 = 0; i0 < ne0; i0++) { |
| 8257 | const float x = ((float) i0 + pixel_offset) / sf0; |
| 8258 | |
| 8259 | // the range of source pixels that contribute |
| 8260 | const int64_t x_min = std::max<int64_t>(x - support0 + pixel_offset, 0); |
| 8261 | const int64_t x_max = std::min<int64_t>(x + support0 + pixel_offset, ne00); |
| 8262 | const int64_t y_min = std::max<int64_t>(y - support1 + pixel_offset, 0); |
| 8263 | const int64_t y_max = std::min<int64_t>(y + support1 + pixel_offset, ne01); |
| 8264 | |
| 8265 | // bilinear filter with antialiasing |
| 8266 | float val = 0.0f; |
| 8267 | float total_weight = 0.0f; |
| 8268 | |
| 8269 | for (int64_t sy = y_min; sy < y_max; sy++) { |
| 8270 | const float weight_y = triangle_filter((sy - y + pixel_offset) * invscale1); |
| 8271 | |
| 8272 | for (int64_t sx = x_min; sx < x_max; sx++) { |
| 8273 | const float weight_x = triangle_filter((sx - x + pixel_offset) * invscale0); |
| 8274 | const float weight = weight_x * weight_y; |
| 8275 | |
| 8276 | if (weight <= 0.0f) { |
| 8277 | continue; |
| 8278 | } |
| 8279 | |
| 8280 | const float pixel = *(const float *)((const char *)src0->data + sx*nb00 + sy*nb01 + i02*nb02 + i03*nb03); |
| 8281 | val += pixel * weight; |
| 8282 | total_weight += weight; |
| 8283 | } |
| 8284 | } |
| 8285 | |
| 8286 | if (total_weight > 0.0f) { |
| 8287 | val /= total_weight; |
| 8288 | } |
| 8289 | |
| 8290 | float * dst_ptr = (float *)((char *)dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3); |
| 8291 | *dst_ptr = val; |
| 8292 | } |
| 8293 | } |
| 8294 | } |
| 8295 | } |
| 8296 | } else if (mode == GGML_SCALE_MODE_BILINEAR) { |
| 8297 | for (int64_t i3 = 0; i3 < ne3; i3++) { |
| 8298 | const int64_t i03 = i3 / sf3; |
| 8299 | for (int64_t i2 = ith; i2 < ne2; i2 += nth) { |
| 8300 | const int64_t i02 = i2 / sf2; |
| 8301 | for (int64_t i1 = 0; i1 < ne1; i1++) { |
| 8302 | const float y = ((float)i1 + pixel_offset) / sf1 - pixel_offset; |
| 8303 | int64_t y0 = (int64_t)floorf(y); |
| 8304 | int64_t y1 = y0 + 1; |
| 8305 | |
| 8306 | y0 = std::max(int64_t(0), std::min(y0, ne01 - 1)); |
| 8307 | y1 = std::max(int64_t(0), std::min(y1, ne01 - 1)); |
| 8308 | |
| 8309 | float dy = y - (float)y0; |
| 8310 | dy = std::max(0.0f, std::min(dy, 1.0f)); |
| 8311 | |
| 8312 | for (int64_t i0 = 0; i0 < ne0; i0++) { |
| 8313 | const float x = ((float)i0 + pixel_offset) / sf0 - pixel_offset; |
| 8314 | int64_t x0 = (int64_t)floorf(x); |
| 8315 | int64_t x1 = x0 + 1; |
| 8316 | |
| 8317 | x0 = std::max(int64_t(0), std::min(x0, ne00 - 1)); |
| 8318 | x1 = std::max(int64_t(0), std::min(x1, ne00 - 1)); |
| 8319 | |
| 8320 | float dx = x - (float)x0; |
| 8321 | dx = std::max(0.0f, std::min(dx, 1.0f)); |
| 8322 | |
| 8323 | // fetch the four surrounding pixel values and interpolate |
| 8324 | const float a = *(const float *)((const char *)src0->data + x0*nb00 + y0*nb01 + i02*nb02 + i03*nb03); |
| 8325 | const float b = *(const float *)((const char *)src0->data + x1*nb00 + y0*nb01 + i02*nb02 + i03*nb03); |
| 8326 | const float c = *(const float *)((const char *)src0->data + x0*nb00 + y1*nb01 + i02*nb02 + i03*nb03); |
| 8327 | const float d = *(const float *)((const char *)src0->data + x1*nb00 + y1*nb01 + i02*nb02 + i03*nb03); |
| 8328 | |
| 8329 | const float val = a*(1 - dx)*(1 - dy) + b*dx*(1 - dy) + c*(1 - dx)*dy + d*dx*dy; |
| 8330 | |
| 8331 | float * y_dst = (float *)((char *)dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3); |
| 8332 | *y_dst = val; |
| 8333 | } |
| 8334 | } |
| 8335 | } |
| 8336 | } |
| 8337 | } else if (mode == GGML_SCALE_MODE_BICUBIC) { |
| 8338 | // https://en.wikipedia.org/wiki/Bicubic_interpolation#Bicubic_convolution_algorithm |
| 8339 | const float a = -0.75f; // use alpha = -0.75 (same as PyTorch) |
| 8340 | auto weight1 = [a](float x) { return ((a + 2) * x - (a + 3)) * x * x + 1; }; |
| 8341 | auto weight2 = [a](float x) { return ((a * x - 5 * a) * x + 8 * a) * x - 4 * a; }; |
| 8342 | auto bicubic = [=](float p0, float p1, float p2, float p3, float x) { |
| 8343 | const float w0 = weight2(x + 1); |
| 8344 | const float w1 = weight1(x + 0); |
| 8345 | const float w2 = weight1(1 - x); |
| 8346 | const float w3 = weight2(2 - x); |
| 8347 | return p0*w0 + p1*w1 + p2*w2 + p3*w3; |
| 8348 | }; |
| 8349 | |
| 8350 | for (int64_t i3 = 0; i3 < ne3; i3++) { |
| 8351 | const int64_t i03 = i3 / sf3; |
| 8352 | for (int64_t i2 = ith; i2 < ne2; i2 += nth) { |
| 8353 | const int64_t i02 = i2 / sf2; |
| 8354 | for (int64_t i1 = 0; i1 < ne1; i1++) { |
| 8355 | const float y = ((float)i1 + pixel_offset) / sf1 - pixel_offset; |
| 8356 | const int64_t y0 = (int64_t)floorf(y); |
| 8357 | const float dy = y - (float)y0; |
| 8358 | |
| 8359 | for (int64_t i0 = 0; i0 < ne0; i0++) { |
| 8360 | const float x = ((float)i0 + pixel_offset) / sf0 - pixel_offset; |
| 8361 | const int64_t x0 = (int64_t)floorf(x); |
| 8362 | const float dx = x - (float)x0; |
| 8363 | |
| 8364 | auto p = [=](int64_t x_off, int64_t y_off) -> float { |
| 8365 | int64_t i00 = std::max(int64_t(0), std::min(x0 + x_off, ne00 - 1)); |
| 8366 | int64_t i01 = std::max(int64_t(0), std::min(y0 + y_off, ne01 - 1)); |
| 8367 | return *(const float *)((const char *)src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03); |
| 8368 | }; |
| 8369 | |
| 8370 | const float val = bicubic( |
| 8371 | bicubic(p(-1,-1), p(0,-1), p(1,-1), p(2,-1), dx), |
| 8372 | bicubic(p(-1, 0), p(0, 0), p(1, 0), p(2, 0), dx), |
| 8373 | bicubic(p(-1, 1), p(0, 1), p(1, 1), p(2, 1), dx), |
| 8374 | bicubic(p(-1, 2), p(0, 2), p(1, 2), p(2, 2), dx), dy); |
| 8375 | |
| 8376 | float * y_dst = (float *)((char *)dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3); |
| 8377 | *y_dst = val; |
| 8378 | } |
| 8379 | } |
| 8380 | } |
| 8381 | } |
| 8382 | } else { |
| 8383 | GGML_ABORT("unsupported upscale mode"); |
| 8384 | } |
| 8385 | } |
| 8386 | |
| 8387 | void ggml_compute_forward_upscale( |
| 8388 | const ggml_compute_params * params, |
| 8389 | ggml_tensor * dst) { |
| 8390 | |
| 8391 | const ggml_tensor * src0 = dst->src[0]; |
| 8392 | |
| 8393 | switch (src0->type) { |
| 8394 | case GGML_TYPE_F32: |
| 8395 | { |
| 8396 | ggml_compute_forward_upscale_f32(params, dst); |
| 8397 | } break; |
| 8398 | default: |
| 8399 | { |
| 8400 | GGML_ABORT("fatal error"); |
| 8401 | } |
| 8402 | } |
| 8403 | } |
| 8404 | |
| 8405 | |
| 8406 | // ggml_compute_forward_pad |
| 8407 | |
| 8408 | template<bool circular_t> |
| 8409 | static void ggml_compute_forward_pad_f32( |
| 8410 | const ggml_compute_params * params, |
| 8411 | ggml_tensor * dst) { |
| 8412 | |
| 8413 | const ggml_tensor * src0 = dst->src[0]; |
| 8414 | |
| 8415 | assert(dst->nb[0] == sizeof(float)); |
| 8416 | |
| 8417 | const int ith = params->ith; |
| 8418 | const int nth = params->nth; |
| 8419 | |
| 8420 | GGML_TENSOR_UNARY_OP_LOCALS |
| 8421 | |
| 8422 | float * dst_ptr = (float *) dst->data; |
| 8423 | const int32_t lp0 = ggml_get_op_params_i32(dst, 0); |
| 8424 | const int32_t rp0 = ggml_get_op_params_i32(dst, 1); |
| 8425 | const int32_t lp1 = ggml_get_op_params_i32(dst, 2); |
| 8426 | const int32_t rp1 = ggml_get_op_params_i32(dst, 3); |
| 8427 | const int32_t lp2 = ggml_get_op_params_i32(dst, 4); |
| 8428 | const int32_t rp2 = ggml_get_op_params_i32(dst, 5); |
| 8429 | const int32_t lp3 = ggml_get_op_params_i32(dst, 6); |
| 8430 | const int32_t rp3 = ggml_get_op_params_i32(dst, 7); |
| 8431 | |
| 8432 | const int64_t nrows = ne1 * ne2 * ne3; |
| 8433 | const bool src_row_contig = src0->nb[0] == sizeof(float); |
| 8434 | |
| 8435 | for (int64_t row = ith; row < nrows; row += nth) { |
| 8436 | const int64_t i1 = row % ne1; |
| 8437 | const int64_t i2 = (row / ne1) % ne2; |
| 8438 | const int64_t i3 = row / (ne1 * ne2); |
| 8439 | |
| 8440 | float * dst_row = dst_ptr + row * ne0; |
| 8441 | |
| 8442 | if constexpr (circular_t) { |
| 8443 | // circular padding wraps around on a torus |
| 8444 | const int64_t src_i1 = ggml_wrap_around(i1 - lp1, ne01); |
| 8445 | const int64_t src_i2 = ggml_wrap_around(i2 - lp2, ne02); |
| 8446 | const int64_t src_i3 = ggml_wrap_around(i3 - lp3, ne03); |
| 8447 | |
| 8448 | for (int64_t i0 = 0; i0 < ne0; ++i0) { |
| 8449 | const int64_t src_i0 = ggml_wrap_around(i0 - lp0, ne00); |
| 8450 | dst_row[i0] = *(const float *)((const char *) src0->data + src_i3*nb03 + src_i2*nb02 + src_i1*nb01 + src_i0*nb00); |
| 8451 | } |
| 8452 | } else { |
| 8453 | const bool interior = (i1 >= lp1 && i1 < ne1 - rp1) |
| 8454 | && (i2 >= lp2 && i2 < ne2 - rp2) |
| 8455 | && (i3 >= lp3 && i3 < ne3 - rp3); |
| 8456 | if (!interior) { |
| 8457 | memset(dst_row, 0, ne0 * sizeof(float)); |
| 8458 | continue; |
| 8459 | } |
| 8460 | |
| 8461 | const char * src_row = (const char *) src0->data + (i3 - lp3)*nb03 + (i2 - lp2)*nb02 + (i1 - lp1)*nb01; |
| 8462 | |
| 8463 | if (src_row_contig && lp0 >= 0 && rp0 >= 0) { |
| 8464 | memset(dst_row, 0, lp0 * sizeof(float)); |
| 8465 | memcpy(dst_row + lp0, src_row, ne00 * sizeof(float)); |
| 8466 | memset(dst_row + lp0 + ne00, 0, rp0 * sizeof(float)); |
| 8467 | } else { |
| 8468 | for (int64_t i0 = 0; i0 < ne0; ++i0) { |
| 8469 | if (i0 >= lp0 && i0 < ne0 - rp0) { |
| 8470 | dst_row[i0] = *(const float *)(src_row + (i0 - lp0)*nb00); |
| 8471 | } else { |
| 8472 | dst_row[i0] = 0.0f; |
| 8473 | } |
| 8474 | } |
| 8475 | } |
| 8476 | } |
| 8477 | } |
| 8478 | } |
| 8479 | |
| 8480 | |
| 8481 | void ggml_compute_forward_pad( |
| 8482 | const ggml_compute_params * params, |
| 8483 | ggml_tensor * dst) { |
| 8484 | const ggml_tensor * src0 = dst->src[0]; |
| 8485 | const bool circular = (bool) ggml_get_op_params_i32(dst, 8); |
| 8486 | switch (src0->type) { |
| 8487 | case GGML_TYPE_F32: |
| 8488 | { |
| 8489 | if (circular) { |
| 8490 | ggml_compute_forward_pad_f32<true>(params, dst); |
| 8491 | } else { |
| 8492 | ggml_compute_forward_pad_f32<false>(params, dst); |
| 8493 | } |
| 8494 | } break; |
| 8495 | default: |
| 8496 | { |
| 8497 | GGML_ABORT("fatal error"); |
| 8498 | } |
| 8499 | } |
| 8500 | } |
| 8501 | |
| 8502 | // ggml_compute_forward_pad_reflect_1d |
| 8503 | |
| 8504 | void ggml_compute_forward_pad_reflect_1d( |
| 8505 | const ggml_compute_params * params, |
| 8506 | ggml_tensor * dst) { |
| 8507 | |
| 8508 | const ggml_tensor * src0 = dst->src[0]; |
| 8509 | |
| 8510 | GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 8511 | GGML_ASSERT( dst->type == GGML_TYPE_F32); |
| 8512 | |
| 8513 | const int ith = params->ith; |
| 8514 | const int nth = params->nth; |
| 8515 | |
| 8516 | const int32_t * opts = (const int32_t *) dst->op_params; |
| 8517 | const int p0 = opts[0]; |
| 8518 | const int p1 = opts[1]; |
| 8519 | |
| 8520 | GGML_TENSOR_UNARY_OP_LOCALS |
| 8521 | |
| 8522 | for (int64_t i3 = 0; i3 < ne3; i3++) { |
| 8523 | for (int64_t i2 = 0; i2 < ne2; i2++) { |
| 8524 | for (int64_t i1 = ith; i1 < ne1; i1 += nth) { |
| 8525 | float * left = (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + p0*nb0); |
| 8526 | float * right = (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + (ne0-p1-1)*nb0); |
| 8527 | |
| 8528 | ggml_vec_cpy_f32(ne00, left, (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
| 8529 | |
| 8530 | for (int i0 = 1; i0 <= p0; i0++) { left[-i0] = left[i0]; } |
| 8531 | for (int i0 = 1; i0 <= p1; i0++) { right[i0] = right[-i0]; } |
| 8532 | } |
| 8533 | } |
| 8534 | } |
| 8535 | } |
| 8536 | |
| 8537 | // ggml_compute_forward_roll |
| 8538 | |
| 8539 | static int64_t ggml_wrap_index(int64_t i, int64_t ne) { |
| 8540 | if (i < 0) { |
| 8541 | return i + ne; |
| 8542 | } else if (i >= ne) { |
| 8543 | return i - ne; |
| 8544 | } |
| 8545 | return i; |
| 8546 | } |
| 8547 | |
| 8548 | static void ggml_compute_forward_roll_f32( |
| 8549 | const ggml_compute_params * params, |
| 8550 | ggml_tensor * dst) { |
| 8551 | |
| 8552 | const ggml_tensor * src0 = dst->src[0]; |
| 8553 | const float * src_data = (const float *) src0->data; |
| 8554 | float * dst_data = (float *) dst->data; |
| 8555 | |
| 8556 | GGML_TENSOR_UNARY_OP_LOCALS |
| 8557 | |
| 8558 | const int s0 = ggml_get_op_params_i32(dst, 0); |
| 8559 | const int s1 = ggml_get_op_params_i32(dst, 1); |
| 8560 | const int s2 = ggml_get_op_params_i32(dst, 2); |
| 8561 | const int s3 = ggml_get_op_params_i32(dst, 3); |
| 8562 | |
| 8563 | const int64_t total = ne1 * ne2 * ne3; |
| 8564 | const int64_t per_thread = (total + params->nth) / params->nth; |
| 8565 | const int64_t start = params->ith * per_thread; |
| 8566 | const int64_t end = std::min(start + per_thread, total); |
| 8567 | |
| 8568 | for (int64_t i = start; i < end; ++i) { |
| 8569 | const int64_t i1 = i % ne1; |
| 8570 | const int64_t i2 = (i / ne1) % ne2; |
| 8571 | const int64_t i3 = i / (ne2 * ne1); |
| 8572 | float * dst_row = dst_data + (i3*nb3 + i2*nb2 + i1*nb1) / sizeof(float); |
| 8573 | |
| 8574 | const int64_t i01 = ggml_wrap_index(i1 - s1, ne01); |
| 8575 | const int64_t i02 = ggml_wrap_index(i2 - s2, ne02); |
| 8576 | const int64_t i03 = ggml_wrap_index(i3 - s3, ne03); |
| 8577 | const float * src_row = src_data + (i03*nb03 + i02*nb02 + i01*nb01) / sizeof(float); |
| 8578 | |
| 8579 | const int64_t s = ggml_wrap_index(-s0, ne00); |
| 8580 | const int64_t n = ne00 - s; |
| 8581 | ggml_vec_cpy_f32(n, dst_row, src_row + s); |
| 8582 | ggml_vec_cpy_f32(s, dst_row + n, src_row); |
| 8583 | } |
| 8584 | } |
| 8585 | |
| 8586 | void ggml_compute_forward_roll( |
| 8587 | const ggml_compute_params * params, |
| 8588 | ggml_tensor * dst) { |
| 8589 | |
| 8590 | const ggml_tensor * src0 = dst->src[0]; |
| 8591 | |
| 8592 | switch (src0->type) { |
| 8593 | case GGML_TYPE_F32: |
| 8594 | { |
| 8595 | ggml_compute_forward_roll_f32(params, dst); |
| 8596 | } break; |
| 8597 | default: |
| 8598 | { |
| 8599 | GGML_ABORT("fatal error"); |
| 8600 | } |
| 8601 | } |
| 8602 | } |
| 8603 | |
| 8604 | // ggml_compute_forward_arange |
| 8605 | |
| 8606 | static void ggml_compute_forward_arange_f32( |
| 8607 | const ggml_compute_params * params, |
| 8608 | ggml_tensor * dst) { |
| 8609 | |
| 8610 | GGML_ASSERT(dst->nb[0] == sizeof(float)); |
| 8611 | |
| 8612 | const int ith = params->ith; |
| 8613 | const int nth = params->nth; |
| 8614 | |
| 8615 | const float start = ggml_get_op_params_f32(dst, 0); |
| 8616 | const float stop = ggml_get_op_params_f32(dst, 1); |
| 8617 | const float step = ggml_get_op_params_f32(dst, 2); |
| 8618 | |
| 8619 | const int64_t steps = (int64_t) ceilf((stop - start) / step); |
| 8620 | |
| 8621 | GGML_ASSERT(ggml_nelements(dst) == steps); |
| 8622 | |
| 8623 | for (int64_t i = ith; i < steps; i+= nth) { |
| 8624 | float value = start + step * i; |
| 8625 | ((float *)dst->data)[i] = value; |
| 8626 | } |
| 8627 | } |
| 8628 | |
| 8629 | void ggml_compute_forward_arange( |
| 8630 | const ggml_compute_params * params, |
| 8631 | ggml_tensor * dst) { |
| 8632 | switch (dst->type) { |
| 8633 | case GGML_TYPE_F32: |
| 8634 | { |
| 8635 | ggml_compute_forward_arange_f32(params, dst); |
| 8636 | } break; |
| 8637 | default: |
| 8638 | { |
| 8639 | GGML_ABORT("fatal error"); |
| 8640 | } |
| 8641 | } |
| 8642 | } |
| 8643 | |
| 8644 | static void ggml_compute_forward_timestep_embedding_f32( |
| 8645 | const ggml_compute_params * params, |
| 8646 | ggml_tensor * dst) { |
| 8647 | |
| 8648 | const ggml_tensor * src0 = dst->src[0]; |
| 8649 | |
| 8650 | GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 8651 | |
| 8652 | const int ith = params->ith; |
| 8653 | const int nth = params->nth; |
| 8654 | |
| 8655 | GGML_TENSOR_UNARY_OP_LOCALS |
| 8656 | |
| 8657 | const int dim = ggml_get_op_params_i32(dst, 0); |
| 8658 | const int max_period = ggml_get_op_params_i32(dst, 1); |
| 8659 | |
| 8660 | int half = dim / 2; |
| 8661 | |
| 8662 | for (int64_t i = 0; i < ne00; i++) { |
| 8663 | float * embed_data = (float *)((char *) dst->data + i*nb1); |
| 8664 | for (int64_t j = ith; j < half; j += nth) { |
| 8665 | float timestep = ((float *)src0->data)[i]; |
| 8666 | float freq = (float)expf(-logf(max_period) * j / half); |
| 8667 | float arg = timestep * freq; |
| 8668 | embed_data[j] = cosf(arg); |
| 8669 | embed_data[j + half] = sinf(arg); |
| 8670 | } |
| 8671 | if (dim % 2 != 0 && ith == 0) { |
| 8672 | embed_data[2 * half] = 0.f; |
| 8673 | } |
| 8674 | } |
| 8675 | } |
| 8676 | |
| 8677 | void ggml_compute_forward_timestep_embedding( |
| 8678 | const ggml_compute_params * params, |
| 8679 | ggml_tensor * dst) { |
| 8680 | |
| 8681 | const ggml_tensor * src0 = dst->src[0]; |
| 8682 | |
| 8683 | switch (src0->type) { |
| 8684 | case GGML_TYPE_F32: |
| 8685 | { |
| 8686 | ggml_compute_forward_timestep_embedding_f32(params, dst); |
| 8687 | } break; |
| 8688 | default: |
| 8689 | { |
| 8690 | GGML_ABORT("fatal error"); |
| 8691 | } |
| 8692 | } |
| 8693 | } |
| 8694 | |
| 8695 | // ggml_compute_forward_argsort |
| 8696 | |
| 8697 | template<enum ggml_sort_order order> |
| 8698 | struct cmp_argsort { |
| 8699 | const float * data; |
| 8700 | bool operator()(int32_t a, int32_t b) const { |
| 8701 | if constexpr (order == GGML_SORT_ORDER_ASC) { |
| 8702 | return data[a] < data[b]; |
| 8703 | } else { |
| 8704 | return data[a] > data[b]; |
| 8705 | } |
| 8706 | } |
| 8707 | }; |
| 8708 | |
| 8709 | static void ggml_compute_forward_argsort_f32( |
| 8710 | const ggml_compute_params * params, |
| 8711 | ggml_tensor * dst) { |
| 8712 | |
| 8713 | const ggml_tensor * src0 = dst->src[0]; |
| 8714 | |
| 8715 | GGML_TENSOR_UNARY_OP_LOCALS |
| 8716 | |
| 8717 | GGML_ASSERT(nb0 == sizeof(float)); |
| 8718 | |
| 8719 | const int ith = params->ith; |
| 8720 | const int nth = params->nth; |
| 8721 | |
| 8722 | const int64_t nr = ggml_nrows(src0); |
| 8723 | |
| 8724 | ggml_sort_order order = (ggml_sort_order) ggml_get_op_params_i32(dst, 0); |
| 8725 | |
| 8726 | for (int64_t i = ith; i < nr; i += nth) { |
| 8727 | const float * src_data = (float *)((char *) src0->data + i*nb01); |
| 8728 | |
| 8729 | int32_t * dst_data = (int32_t *)((char *) dst->data + i*nb1); |
| 8730 | |
| 8731 | for (int64_t j = 0; j < ne0; j++) { |
| 8732 | dst_data[j] = j; |
| 8733 | } |
| 8734 | |
| 8735 | switch (order) { |
| 8736 | case GGML_SORT_ORDER_ASC: |
| 8737 | std::sort(dst_data, dst_data + ne0, cmp_argsort<GGML_SORT_ORDER_ASC>{src_data}); |
| 8738 | break; |
| 8739 | |
| 8740 | case GGML_SORT_ORDER_DESC: |
| 8741 | std::sort(dst_data, dst_data + ne0, cmp_argsort<GGML_SORT_ORDER_DESC>{src_data}); |
| 8742 | break; |
| 8743 | |
| 8744 | default: |
| 8745 | GGML_ABORT("invalid sort order"); |
| 8746 | } |
| 8747 | } |
| 8748 | } |
| 8749 | |
| 8750 | void ggml_compute_forward_argsort( |
| 8751 | const ggml_compute_params * params, |
| 8752 | ggml_tensor * dst) { |
| 8753 | |
| 8754 | const ggml_tensor * src0 = dst->src[0]; |
| 8755 | |
| 8756 | switch (src0->type) { |
| 8757 | case GGML_TYPE_F32: |
| 8758 | { |
| 8759 | ggml_compute_forward_argsort_f32(params, dst); |
| 8760 | } break; |
| 8761 | default: |
| 8762 | { |
| 8763 | GGML_ABORT("fatal error"); |
| 8764 | } |
| 8765 | } |
| 8766 | } |
| 8767 | |
| 8768 | // ggml_compute_forward_top_k |
| 8769 | |
| 8770 | struct cmp_top_k { |
| 8771 | const float * data; |
| 8772 | bool operator()(int32_t a, int32_t b) const { |
| 8773 | return data[a] > data[b]; |
| 8774 | } |
| 8775 | }; |
| 8776 | |
| 8777 | static void ggml_compute_forward_top_k_f32( |
| 8778 | const ggml_compute_params * params, |
| 8779 | ggml_tensor * dst) { |
| 8780 | |
| 8781 | const ggml_tensor * src0 = dst->src[0]; |
| 8782 | |
| 8783 | GGML_TENSOR_UNARY_OP_LOCALS |
| 8784 | |
| 8785 | GGML_ASSERT(nb0 == sizeof(float)); |
| 8786 | |
| 8787 | const int ith = params->ith; |
| 8788 | const int nth = params->nth; |
| 8789 | |
| 8790 | const int64_t nr = ggml_nrows(src0); |
| 8791 | |
| 8792 | const int top_k = ne0; |
| 8793 | |
| 8794 | int32_t * tmp = (int32_t *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith; |
| 8795 | |
| 8796 | for (int64_t i = ith; i < nr; i += nth) { |
| 8797 | const float * src_data = (float *)((char *) src0->data + i*nb01); |
| 8798 | |
| 8799 | for (int64_t j = 0; j < ne00; j++) { |
| 8800 | tmp[j] = j; |
| 8801 | } |
| 8802 | |
| 8803 | std::partial_sort(tmp, tmp + top_k, tmp + ne00, cmp_top_k{src_data}); |
| 8804 | |
| 8805 | int32_t * dst_data = (int32_t *)((char *) dst->data + i*nb1); |
| 8806 | |
| 8807 | std::copy(tmp, tmp + top_k, dst_data); |
| 8808 | |
| 8809 | // emphasize that the order is not important |
| 8810 | if (top_k > 1) { |
| 8811 | std::swap(dst_data[0], dst_data[1]); |
| 8812 | } |
| 8813 | } |
| 8814 | } |
| 8815 | |
| 8816 | void ggml_compute_forward_top_k( |
| 8817 | const ggml_compute_params * params, |
| 8818 | ggml_tensor * dst) { |
| 8819 | |
| 8820 | const ggml_tensor * src0 = dst->src[0]; |
| 8821 | |
| 8822 | switch (src0->type) { |
| 8823 | case GGML_TYPE_F32: |
| 8824 | { |
| 8825 | ggml_compute_forward_top_k_f32(params, dst); |
| 8826 | } break; |
| 8827 | default: |
| 8828 | { |
| 8829 | GGML_ABORT("fatal error"); |
| 8830 | } |
| 8831 | } |
| 8832 | } |
| 8833 | |
| 8834 | static void ggml_compute_forward_flash_attn_ext_f16_one_chunk( |
| 8835 | const ggml_compute_params * params, |
| 8836 | ggml_tensor * dst, |
| 8837 | int ir0, int ir1, |
| 8838 | int64_t ic_start, int64_t ic_end, |
| 8839 | float * partials, int64_t partial_stride) { |
| 8840 | |
| 8841 | const bool write_partials = (partials != nullptr); |
| 8842 | const ggml_tensor * q = dst->src[0]; |
| 8843 | const ggml_tensor * k = dst->src[1]; |
| 8844 | const ggml_tensor * v = dst->src[2]; |
| 8845 | const ggml_tensor * mask = dst->src[3]; |
| 8846 | const ggml_tensor * sinks = dst->src[4]; |
| 8847 | const ggml_tensor * kv_rows = dst->src[5]; |
| 8848 | |
| 8849 | GGML_TENSOR_LOCALS(int64_t, neq, q, ne) |
| 8850 | GGML_TENSOR_LOCALS(size_t, nbq, q, nb) |
| 8851 | GGML_TENSOR_LOCALS(int64_t, nek, k, ne) |
| 8852 | GGML_TENSOR_LOCALS(size_t, nbk, k, nb) |
| 8853 | GGML_TENSOR_LOCALS(int64_t, nev, v, ne) |
| 8854 | GGML_TENSOR_LOCALS(size_t, nbv, v, nb) |
| 8855 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 8856 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 8857 | |
| 8858 | const int64_t DK = nek0; |
| 8859 | const int64_t DV = nev0; |
| 8860 | const int64_t N = neq1; |
| 8861 | |
| 8862 | GGML_ASSERT(ne0 == DV); |
| 8863 | GGML_ASSERT(ne2 == N); |
| 8864 | |
| 8865 | // input tensor rows must be contiguous |
| 8866 | GGML_ASSERT(nbq0 == ggml_type_size(q->type)); |
| 8867 | GGML_ASSERT(nbk0 == ggml_type_size(k->type)); |
| 8868 | GGML_ASSERT(nbv0 == ggml_type_size(v->type)); |
| 8869 | |
| 8870 | GGML_ASSERT(neq0 == DK); |
| 8871 | GGML_ASSERT(nek0 == DK); |
| 8872 | GGML_ASSERT(nev0 == DV); |
| 8873 | |
| 8874 | GGML_ASSERT(neq1 == N); |
| 8875 | |
| 8876 | // dst cannot be transposed or permuted |
| 8877 | GGML_ASSERT(nb0 == sizeof(float)); |
| 8878 | GGML_ASSERT(nb0 <= nb1); |
| 8879 | GGML_ASSERT(nb1 <= nb2); |
| 8880 | GGML_ASSERT(nb2 <= nb3); |
| 8881 | |
| 8882 | // broadcast factors |
| 8883 | const int64_t rk2 = neq2/nek2; |
| 8884 | const int64_t rk3 = neq3/nek3; |
| 8885 | |
| 8886 | const int64_t rv2 = neq2/nev2; |
| 8887 | const int64_t rv3 = neq3/nev3; |
| 8888 | |
| 8889 | // parallelize by q rows using ggml_vec_dot_f32 |
| 8890 | |
| 8891 | float scale = 1.0f; |
| 8892 | float max_bias = 0.0f; |
| 8893 | float logit_softcap = 0.0f; |
| 8894 | |
| 8895 | memcpy(&scale, (float *) dst->op_params + 0, sizeof(float)); |
| 8896 | memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float)); |
| 8897 | memcpy(&logit_softcap, (float *) dst->op_params + 2, sizeof(float)); |
| 8898 | |
| 8899 | if (logit_softcap != 0) { |
| 8900 | scale /= logit_softcap; |
| 8901 | } |
| 8902 | |
| 8903 | const uint32_t n_head = neq2; |
| 8904 | const uint32_t n_head_log2 = 1u << (uint32_t) floor(log2(n_head)); |
| 8905 | |
| 8906 | const float m0 = powf(2.0f, -(max_bias ) / n_head_log2); |
| 8907 | const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2); |
| 8908 | |
| 8909 | ggml_type const k_vec_dot_type = ggml_get_type_traits_cpu(k->type)->vec_dot_type; |
| 8910 | ggml_from_float_t const q_to_vec_dot = ggml_get_type_traits_cpu(k_vec_dot_type)->from_float; |
| 8911 | ggml_vec_dot_t const kq_vec_dot = ggml_get_type_traits_cpu(k->type)->vec_dot; |
| 8912 | ggml_to_float_t const v_to_float = ggml_get_type_traits(v->type)->to_float; |
| 8913 | |
| 8914 | GGML_ASSERT(( q_to_vec_dot) && "fattn: unsupported K-type"); |
| 8915 | GGML_ASSERT((v->type == GGML_TYPE_F32 || v_to_float ) && "fattn: unsupported V-type"); |
| 8916 | |
| 8917 | int ith = params->ith; |
| 8918 | |
| 8919 | for (int ir = ir0; ir < ir1; ++ir) { |
| 8920 | // q indices |
| 8921 | const int iq3 = ir/(neq2*neq1); |
| 8922 | const int iq2 = (ir - iq3*neq2*neq1)/neq1; |
| 8923 | const int iq1 = (ir - iq3*neq2*neq1 - iq2*neq1); |
| 8924 | |
| 8925 | const uint32_t h = iq2; // head index |
| 8926 | const float slope = (max_bias > 0.0f) ? h < n_head_log2 ? powf(m0, h + 1) : powf(m1, 2*(h - n_head_log2) + 1) : 1.0f; |
| 8927 | |
| 8928 | float S = 0.0f; // sum |
| 8929 | float M = -INFINITY; // maximum KQ value |
| 8930 | |
| 8931 | float * VKQ32 = (float *) params->wdata + ith*(1*DK + 2*DV + CACHE_LINE_SIZE_F32); // FP32 VKQ accumulator |
| 8932 | float * V32 = (VKQ32 + 1*DV); // (temporary) FP32 V buffer |
| 8933 | ggml_fp16_t * VKQ16 = (ggml_fp16_t *) (VKQ32 + 1*DV); // (temporary) FP16 VKQ accumulator |
| 8934 | ggml_fp16_t * Q_q = (ggml_fp16_t *) (VKQ32 + 2*DV); // (temporary) buffer for Q converted to quantized/FP16 |
| 8935 | |
| 8936 | if (v->type == GGML_TYPE_F16) { |
| 8937 | memset(VKQ16, 0, DV*sizeof(ggml_fp16_t)); |
| 8938 | } else { |
| 8939 | memset(VKQ32, 0, DV*sizeof(float)); |
| 8940 | } |
| 8941 | |
| 8942 | const ggml_fp16_t * mp = mask ? (ggml_fp16_t *)((char *) mask->data + iq1*mask->nb[1] + (iq2%mask->ne[2])*mask->nb[2] + (iq3%mask->ne[3])*mask->nb[3]) : NULL; |
| 8943 | |
| 8944 | // k indices |
| 8945 | const int ik3 = iq3 / rk3; |
| 8946 | const int ik2 = iq2 / rk2; |
| 8947 | |
| 8948 | // v indices |
| 8949 | const int iv3 = iq3 / rv3; |
| 8950 | const int iv2 = iq2 / rv2; |
| 8951 | |
| 8952 | const float * pq = (const float *) ((char *) q->data + (iq1*nbq1 + iq2*nbq2 + iq3*nbq3)); |
| 8953 | q_to_vec_dot(pq, Q_q, DK); |
| 8954 | |
| 8955 | const int32_t * rows = kv_rows ? (const int32_t *) ((const char *) kv_rows->data + iq3*kv_rows->nb[1]) : nullptr; |
| 8956 | |
| 8957 | // online softmax / attention |
| 8958 | // loop over n_kv and n_head_kv |
| 8959 | // ref: https://arxiv.org/pdf/2112.05682.pdf |
| 8960 | |
| 8961 | for (int64_t ic = ic_start; ic < ic_end; ++ic) { |
| 8962 | const float mv = mp ? slope*GGML_CPU_FP16_TO_FP32(mp[ic]) : 0.0f; |
| 8963 | if (mv == -INFINITY) { |
| 8964 | continue; |
| 8965 | } |
| 8966 | |
| 8967 | const int64_t ik1 = rows ? rows[ic] : ic; |
| 8968 | if (ik1 < 0) { |
| 8969 | continue; |
| 8970 | } |
| 8971 | |
| 8972 | float s; // KQ value |
| 8973 | |
| 8974 | const char * k_data = (const char *) k->data + (ik1*nbk1 + ik2*nbk2 + ik3*nbk3); |
| 8975 | kq_vec_dot(DK, &s, 0, k_data, 0, Q_q, 0, 1); |
| 8976 | |
| 8977 | s = s*scale; // scale KQ value |
| 8978 | |
| 8979 | if (logit_softcap != 0.0f) { |
| 8980 | s = logit_softcap*tanhf(s); |
| 8981 | } |
| 8982 | |
| 8983 | s += mv; // apply mask |
| 8984 | |
| 8985 | const float Mold = M; |
| 8986 | |
| 8987 | float ms = 1.0f; // upon new higher max val, scale VKQ and KQ sum with this value |
| 8988 | float vs = 1.0f; // post-softmax KQ value, expf(s - M) |
| 8989 | |
| 8990 | const char * v_data = ((const char *) v->data + (ik1*nbv1 + iv2*nbv2 + iv3*nbv3)); |
| 8991 | |
| 8992 | if (v->type == GGML_TYPE_F16) { |
| 8993 | if (s > M) { |
| 8994 | // s is new maximum, ms < 1.0f, vs == expf(s - s) == 1.0f |
| 8995 | M = s; |
| 8996 | ms = expf(Mold - M); |
| 8997 | |
| 8998 | // V = V*expf(Mold - M) |
| 8999 | ggml_vec_scale_f16(DV, VKQ16, ms); |
| 9000 | } else { |
| 9001 | // no new maximum, ms == 1.0f, vs != 1.0f |
| 9002 | vs = expf(s - M); |
| 9003 | } |
| 9004 | |
| 9005 | // V += v*expf(s - M) |
| 9006 | ggml_vec_mad_f16(DV, VKQ16, (const ggml_fp16_t *) v_data, vs); |
| 9007 | } else { |
| 9008 | if (s > M) { |
| 9009 | // s is new maximum, ms < 1.0f, vs == expf(s - s) == 1.0f |
| 9010 | M = s; |
| 9011 | ms = expf(Mold - M); |
| 9012 | |
| 9013 | // V = V*expf(Mold - M) |
| 9014 | ggml_vec_scale_f32(DV, VKQ32, ms); |
| 9015 | } else { |
| 9016 | // no new maximum, ms == 1.0f, vs != 1.0f |
| 9017 | vs = expf(s - M); |
| 9018 | } |
| 9019 | |
| 9020 | // V += v*expf(s - M) |
| 9021 | if (v_to_float) { |
| 9022 | v_to_float(v_data, V32, DV); |
| 9023 | ggml_vec_mad_f32(DV, VKQ32, V32, vs); |
| 9024 | } else { |
| 9025 | // V is F32 |
| 9026 | ggml_vec_mad_f32(DV, VKQ32, (const float *) v_data, vs); |
| 9027 | } |
| 9028 | } |
| 9029 | |
| 9030 | S = S*ms + vs; // scale and increment sum with partial sum |
| 9031 | } |
| 9032 | |
| 9033 | if (v->type == GGML_TYPE_F16) { |
| 9034 | for (int64_t d = 0; d < DV; ++d) { |
| 9035 | VKQ32[d] = GGML_CPU_FP16_TO_FP32(VKQ16[d]); |
| 9036 | } |
| 9037 | } |
| 9038 | |
| 9039 | // sinks - apply only on the first kv-chunk |
| 9040 | if (sinks && ic_start == 0) { |
| 9041 | const float s = ((float *)((char *) sinks->data))[h]; |
| 9042 | |
| 9043 | float ms = 1.0f; |
| 9044 | float vs = 1.0f; |
| 9045 | |
| 9046 | if (s > M) { |
| 9047 | ms = expf(M - s); |
| 9048 | M = s; |
| 9049 | ggml_vec_scale_f32(DV, VKQ32, ms); |
| 9050 | } else { |
| 9051 | vs = expf(s - M); |
| 9052 | } |
| 9053 | |
| 9054 | S = S*ms + vs; |
| 9055 | } |
| 9056 | |
| 9057 | if (write_partials) { |
| 9058 | // Write M, S, VKQ to partials for later reduction |
| 9059 | // partials layout: [M, S, VKQ[DV]] per query head |
| 9060 | float * partial = partials + ir * partial_stride; |
| 9061 | partial[0] = M; |
| 9062 | partial[1] = S; |
| 9063 | memcpy(partial + 2, VKQ32, DV * sizeof(float)); |
| 9064 | } else { |
| 9065 | // V /= S |
| 9066 | const float S_inv = S == 0.0f ? 0.0f : 1.0f/S; |
| 9067 | ggml_vec_scale_f32(DV, VKQ32, S_inv); |
| 9068 | |
| 9069 | // dst indices |
| 9070 | const int i1 = iq1; |
| 9071 | const int i2 = iq2; |
| 9072 | const int i3 = iq3; |
| 9073 | |
| 9074 | // permute(0, 2, 1, 3) |
| 9075 | memcpy((char *) dst->data + (i3*ne2*ne1 + i2 + i1*ne1)*nb1, VKQ32, nb1); |
| 9076 | } |
| 9077 | } |
| 9078 | } |
| 9079 | |
| 9080 | static void ggml_compute_forward_flash_attn_ext_tiled( |
| 9081 | const ggml_compute_params * params, |
| 9082 | ggml_tensor * dst, |
| 9083 | int ir0, int ir1) { |
| 9084 | const ggml_tensor * q = dst->src[0]; |
| 9085 | const ggml_tensor * k = dst->src[1]; |
| 9086 | const ggml_tensor * v = dst->src[2]; |
| 9087 | const ggml_tensor * mask = dst->src[3]; |
| 9088 | const ggml_tensor * sinks = dst->src[4]; |
| 9089 | |
| 9090 | GGML_TENSOR_LOCALS(int64_t, neq, q, ne) |
| 9091 | GGML_TENSOR_LOCALS(size_t, nbq, q, nb) |
| 9092 | GGML_TENSOR_LOCALS(int64_t, nek, k, ne) |
| 9093 | GGML_TENSOR_LOCALS(size_t, nbk, k, nb) |
| 9094 | GGML_TENSOR_LOCALS(int64_t, nev, v, ne) |
| 9095 | GGML_TENSOR_LOCALS(size_t, nbv, v, nb) |
| 9096 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 9097 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 9098 | |
| 9099 | const int64_t DK = nek0; |
| 9100 | const int64_t DV = nev0; |
| 9101 | const int64_t N = neq1; |
| 9102 | |
| 9103 | GGML_ASSERT(ne0 == DV); |
| 9104 | GGML_ASSERT(ne2 == N); |
| 9105 | |
| 9106 | // input tensor rows must be contiguous |
| 9107 | GGML_ASSERT(nbq0 == ggml_type_size(q->type)); |
| 9108 | GGML_ASSERT(nbk0 == ggml_type_size(k->type)); |
| 9109 | GGML_ASSERT(nbv0 == ggml_type_size(v->type)); |
| 9110 | |
| 9111 | GGML_ASSERT(neq0 == DK); |
| 9112 | GGML_ASSERT(nek0 == DK); |
| 9113 | GGML_ASSERT(nev0 == DV); |
| 9114 | |
| 9115 | GGML_ASSERT(neq1 == N); |
| 9116 | |
| 9117 | // dst cannot be transposed or permuted |
| 9118 | GGML_ASSERT(nb0 == sizeof(float)); |
| 9119 | GGML_ASSERT(nb0 <= nb1); |
| 9120 | GGML_ASSERT(nb1 <= nb2); |
| 9121 | GGML_ASSERT(nb2 <= nb3); |
| 9122 | |
| 9123 | GGML_ASSERT(k->type == v->type); |
| 9124 | const ggml_type kv_type = k->type; |
| 9125 | |
| 9126 | |
| 9127 | // broadcast factors |
| 9128 | const int64_t rk2 = neq2/nek2; |
| 9129 | const int64_t rk3 = neq3/nek3; |
| 9130 | |
| 9131 | const int64_t rv2 = neq2/nev2; |
| 9132 | const int64_t rv3 = neq3/nev3; |
| 9133 | |
| 9134 | float scale = 1.0f; |
| 9135 | float max_bias = 0.0f; |
| 9136 | float logit_softcap = 0.0f; |
| 9137 | |
| 9138 | memcpy(&scale, (float *) dst->op_params + 0, sizeof(float)); |
| 9139 | memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float)); |
| 9140 | memcpy(&logit_softcap, (float *) dst->op_params + 2, sizeof(float)); |
| 9141 | |
| 9142 | if (logit_softcap != 0) { |
| 9143 | scale /= logit_softcap; |
| 9144 | } |
| 9145 | |
| 9146 | const uint32_t n_head = neq2; |
| 9147 | const uint32_t n_head_log2 = 1u << (uint32_t) floor(log2(n_head)); |
| 9148 | |
| 9149 | const float m0 = powf(2.0f, -(max_bias ) / n_head_log2); |
| 9150 | const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2); |
| 9151 | |
| 9152 | int ith = params->ith; |
| 9153 | |
| 9154 | static constexpr int Q_TILE_SZ = ggml_fa_tile_config::Q; |
| 9155 | static constexpr int KV_TILE_SZ = ggml_fa_tile_config::KV; |
| 9156 | |
| 9157 | // with kv_rows the loop runs over the mask columns and reads K/V rows through kv_rows |
| 9158 | const ggml_tensor * kv_rows = dst->src[5]; |
| 9159 | const int64_t n_kv_cols = kv_rows ? kv_rows->ne[0] : nek1; |
| 9160 | |
| 9161 | int ir = ir0; |
| 9162 | while (ir < ir1) { |
| 9163 | // q indices for the start of this tile |
| 9164 | const int iq3 = ir/(neq2*neq1); |
| 9165 | const int iq2 = (ir - iq3*neq2*neq1)/neq1; |
| 9166 | const int iq1 = (ir - iq3*neq2*neq1 - iq2*neq1); |
| 9167 | |
| 9168 | // padding rows are masked, read row 0 for them |
| 9169 | const int32_t * rows = kv_rows ? (const int32_t *) ((const char *) kv_rows->data + iq3*kv_rows->nb[1]) : nullptr; |
| 9170 | auto kv_row = [rows](int64_t i) -> int64_t { return rows ? std::max<int32_t>(rows[i], 0) : i; }; |
| 9171 | |
| 9172 | // Number of valid rows in this tile: |
| 9173 | // - limited by tile size (Q_TILE_SZ) |
| 9174 | // - limited by chunk boundary (ir1 - ir) |
| 9175 | // - limited by head boundary (neq1 - iq1) to avoid crossing into next head |
| 9176 | const int tile_rows = MIN(Q_TILE_SZ, MIN((int)(ir1 - ir), (int)(neq1 - iq1))); |
| 9177 | GGML_ASSERT(tile_rows > 0); |
| 9178 | |
| 9179 | const uint32_t h = iq2; // head index |
| 9180 | const float slope = (max_bias > 0.0f) ? h < n_head_log2 ? powf(m0, h + 1) : powf(m1, 2*(h - n_head_log2) + 1) : 1.0f; |
| 9181 | |
| 9182 | float S[Q_TILE_SZ]; |
| 9183 | float M[Q_TILE_SZ]; |
| 9184 | |
| 9185 | for (int i = 0 ; i < Q_TILE_SZ; ++i) { |
| 9186 | S[i] = 0.; |
| 9187 | M[i] = -INFINITY; |
| 9188 | } |
| 9189 | |
| 9190 | // Per-thread scratch layout: |
| 9191 | // Q_q: Q_TILE_SZ * DK (converted Q tile — F32 for GEMM, KV type for scalar) |
| 9192 | // KQ: Q_TILE_SZ * KV_TILE_SZ (attention scores in float) |
| 9193 | // mask: Q_TILE_SZ * KV_TILE_SZ (mask in float) |
| 9194 | // VKQ32: Q_TILE_SZ * DV (FP32 output accumulator) |
| 9195 | // V32: KV_TILE_SZ * DV (F32 buffer for V tile) |
| 9196 | // K_f32: KV_TILE_SZ * DK (F32 buffer for K tile — GEMM path) |
| 9197 | float * base = (float *) params->wdata + ith*(Q_TILE_SZ*DK + 2*Q_TILE_SZ*KV_TILE_SZ + Q_TILE_SZ*DV + KV_TILE_SZ*DV + KV_TILE_SZ*DK + CACHE_LINE_SIZE_F32); |
| 9198 | |
| 9199 | void * Q_q = base; |
| 9200 | float * KQ = (float *)((char *)base + Q_TILE_SZ * DK * sizeof(float)); |
| 9201 | float * mask32 = KQ + Q_TILE_SZ * KV_TILE_SZ; |
| 9202 | float * VKQ32 = mask32 + Q_TILE_SZ * KV_TILE_SZ; |
| 9203 | float * V32 = VKQ32 + Q_TILE_SZ * DV; |
| 9204 | float * K_f32 = V32 + KV_TILE_SZ * DV; |
| 9205 | |
| 9206 | memset(VKQ32, 0, Q_TILE_SZ * DV * sizeof(float)); |
| 9207 | memset(mask32, 0, Q_TILE_SZ * KV_TILE_SZ * sizeof(float)); |
| 9208 | |
| 9209 | // k indices |
| 9210 | const int ik3 = iq3 / rk3; |
| 9211 | const int ik2 = iq2 / rk2; |
| 9212 | |
| 9213 | // v indices |
| 9214 | const int iv3 = iq3 / rv3; |
| 9215 | const int iv2 = iq2 / rv2; |
| 9216 | |
| 9217 | { |
| 9218 | float * Q_f32 = (float *)Q_q; |
| 9219 | for (int tq = 0; tq < tile_rows; tq++) { |
| 9220 | const float * pq = (const float *) ((char *) q->data + ((iq1 + tq)*nbq1 + iq2*nbq2 + iq3*nbq3)); |
| 9221 | memcpy(Q_f32 + tq * DK, pq, DK * sizeof(float)); |
| 9222 | } |
| 9223 | for (int tq = tile_rows; tq < Q_TILE_SZ; tq++) { |
| 9224 | memset(Q_f32 + tq * DK, 0, DK * sizeof(float)); |
| 9225 | } |
| 9226 | } |
| 9227 | |
| 9228 | memset(K_f32, 0, DK * KV_TILE_SZ * sizeof(float)); |
| 9229 | memset(V32, 0, KV_TILE_SZ * DV * sizeof(float)); |
| 9230 | |
| 9231 | for (int64_t ic = 0; ic < n_kv_cols; ic += KV_TILE_SZ) { |
| 9232 | const int kv_tile = (int)std::min((int64_t)KV_TILE_SZ, n_kv_cols - ic); |
| 9233 | |
| 9234 | // skip the tile entirely if all the masks are -inf |
| 9235 | if (mask) { |
| 9236 | bool can_skip = true; |
| 9237 | for (int tq = 0; tq < tile_rows; tq++) { |
| 9238 | const ggml_fp16_t * mp_row = (const ggml_fp16_t *)((const char *) mask->data + (iq1 + tq)*mask->nb[1] + (iq2%mask->ne[2])*mask->nb[2] + (iq3%mask->ne[3])*mask->nb[3]); |
| 9239 | for (int tk = 0; tk < kv_tile; tk++) { |
| 9240 | mask32[tq * KV_TILE_SZ + tk] = slope * GGML_CPU_FP16_TO_FP32(mp_row[ic + tk]); |
| 9241 | if (mask32[tq * KV_TILE_SZ + tk] != -INFINITY) { |
| 9242 | can_skip = false; |
| 9243 | } |
| 9244 | } |
| 9245 | // Pad remaining mask entries with -inf |
| 9246 | for (int tk = kv_tile; tk < KV_TILE_SZ; tk++) { |
| 9247 | mask32[tq * KV_TILE_SZ + tk] = -INFINITY; |
| 9248 | } |
| 9249 | } |
| 9250 | |
| 9251 | if (can_skip) { |
| 9252 | continue; |
| 9253 | } |
| 9254 | } |
| 9255 | |
| 9256 | // Pack K tile transposed: K_f32[dk][kv] so KV_TILE is contiguous (SIMD dim) |
| 9257 | // Zero-pad the last tile so the GEMM always operates on KV_TILE_SZ columns |
| 9258 | for (int tk = 0; tk < kv_tile; tk++) { |
| 9259 | const char * k_data = (const char *)k->data + kv_row(ic + tk)*nbk1 + ik2*nbk2 + ik3*nbk3; |
| 9260 | if (kv_type == GGML_TYPE_F16) { |
| 9261 | const ggml_fp16_t * k_f16 = (const ggml_fp16_t *)k_data; |
| 9262 | for (int64_t dk = 0; dk < DK; dk++) { |
| 9263 | K_f32[dk * KV_TILE_SZ + tk] = GGML_CPU_FP16_TO_FP32(k_f16[dk]); |
| 9264 | } |
| 9265 | } else { |
| 9266 | const float * k_f32_src = (const float *)k_data; |
| 9267 | for (int64_t dk = 0; dk < DK; dk++) { |
| 9268 | K_f32[dk * KV_TILE_SZ + tk] = k_f32_src[dk]; |
| 9269 | } |
| 9270 | } |
| 9271 | } |
| 9272 | memset(KQ, 0, Q_TILE_SZ * KV_TILE_SZ * sizeof(float)); |
| 9273 | simd_gemm(KQ, (const float *)Q_q, K_f32, Q_TILE_SZ, DK, KV_TILE_SZ); |
| 9274 | ggml_vec_scale_f32(Q_TILE_SZ * KV_TILE_SZ, KQ, scale); |
| 9275 | |
| 9276 | if (logit_softcap != 0.0f) { |
| 9277 | ggml_vec_tanh_f32(Q_TILE_SZ * KV_TILE_SZ, KQ, KQ); |
| 9278 | ggml_vec_scale_f32(Q_TILE_SZ * KV_TILE_SZ, KQ, logit_softcap); |
| 9279 | } |
| 9280 | |
| 9281 | // Set padded KQ entries to -inf so softmax gives them zero weight |
| 9282 | if (kv_tile < KV_TILE_SZ) { |
| 9283 | for (int tq = 0; tq < Q_TILE_SZ; tq++) { |
| 9284 | for (int tk = kv_tile; tk < KV_TILE_SZ; tk++) { |
| 9285 | KQ[tq * KV_TILE_SZ + tk] = -INFINITY; |
| 9286 | } |
| 9287 | } |
| 9288 | } |
| 9289 | |
| 9290 | if (mask) { |
| 9291 | ggml_vec_add_f32(tile_rows * KV_TILE_SZ, KQ, KQ, mask32); |
| 9292 | } |
| 9293 | |
| 9294 | bool skip[Q_TILE_SZ] = {}; |
| 9295 | |
| 9296 | for (int tq = 0; tq < Q_TILE_SZ; tq++) { |
| 9297 | float * kq_row = KQ + tq * KV_TILE_SZ; |
| 9298 | |
| 9299 | float tile_max; |
| 9300 | ggml_vec_max_f32(KV_TILE_SZ, &tile_max, kq_row); |
| 9301 | |
| 9302 | if (tile_max == -INFINITY) { |
| 9303 | skip[tq] = true; |
| 9304 | continue; |
| 9305 | } |
| 9306 | |
| 9307 | const float Mold = M[tq]; |
| 9308 | const float Mnew = fmaxf(Mold, tile_max); |
| 9309 | |
| 9310 | if (Mnew > Mold) { |
| 9311 | const float ms = expf(Mold - Mnew); |
| 9312 | ggml_vec_scale_f32(DV, VKQ32 + tq * DV, ms); |
| 9313 | S[tq] *= ms; |
| 9314 | } |
| 9315 | M[tq] = Mnew; |
| 9316 | |
| 9317 | |
| 9318 | S[tq] += ggml_vec_soft_max_f32(KV_TILE_SZ, kq_row, kq_row, Mnew); |
| 9319 | } |
| 9320 | |
| 9321 | // V accumulation: VKQ32 += softmax(KQ) * V |
| 9322 | // Pack V tile to contiguous F32, zero-padded |
| 9323 | for (int tk = 0; tk < kv_tile; tk++) { |
| 9324 | const char * v_data = (const char *)v->data + kv_row(ic + tk)*nbv1 + iv2*nbv2 + iv3*nbv3; |
| 9325 | if (kv_type == GGML_TYPE_F16) { |
| 9326 | ggml_cpu_fp16_to_fp32((const ggml_fp16_t *)v_data, V32 + tk * DV, DV); |
| 9327 | } else { |
| 9328 | memcpy(V32 + tk * DV, v_data, DV * sizeof(float)); |
| 9329 | } |
| 9330 | } |
| 9331 | for (int tq = 0; tq < Q_TILE_SZ; tq++) { |
| 9332 | if (skip[tq]) { |
| 9333 | memset(KQ + tq * KV_TILE_SZ, 0, KV_TILE_SZ * sizeof(float)); |
| 9334 | } |
| 9335 | } |
| 9336 | simd_gemm(VKQ32, KQ, V32, Q_TILE_SZ, KV_TILE_SZ, DV); |
| 9337 | } |
| 9338 | |
| 9339 | // sinks (apply only to valid rows in the tile) |
| 9340 | if (sinks) { |
| 9341 | const float s = ((float *)((char *) sinks->data))[h]; |
| 9342 | |
| 9343 | for (int tq = 0; tq < tile_rows; tq++) { |
| 9344 | float ms = 1.0f; |
| 9345 | float vs = 1.0f; |
| 9346 | |
| 9347 | if (s > M[tq]) { |
| 9348 | ms = expf(M[tq] - s); |
| 9349 | ggml_vec_scale_f32(DV, VKQ32 + tq * DV, ms); |
| 9350 | } else { |
| 9351 | vs = expf(s - M[tq]); |
| 9352 | } |
| 9353 | |
| 9354 | S[tq] = S[tq] * ms + vs; |
| 9355 | } |
| 9356 | } |
| 9357 | |
| 9358 | for (int tq = 0; tq < tile_rows; tq++) { |
| 9359 | // V /= S |
| 9360 | const float S_inv = S[tq] == 0.0f ? 0.0f : 1.0f / S[tq]; |
| 9361 | ggml_vec_scale_f32(DV, VKQ32 + tq * DV, S_inv); |
| 9362 | |
| 9363 | // dst indices |
| 9364 | const int i1 = iq1 + tq; |
| 9365 | const int i2 = iq2; |
| 9366 | const int i3 = iq3; |
| 9367 | |
| 9368 | // permute(0, 2, 1, 3) |
| 9369 | memcpy((char *) dst->data + (i3*ne2*ne1 + i2 + i1*ne1)*nb1, VKQ32 + tq * DV, nb1); |
| 9370 | } |
| 9371 | |
| 9372 | ir += tile_rows; |
| 9373 | } |
| 9374 | } |
| 9375 | |
| 9376 | // Reduction function: combines partial results across KV chunks |
| 9377 | // Partials layout in wdata: [n_q_heads][n_chunks][2 + DV] |
| 9378 | static void ggml_flash_attn_ext_reduce_partials( |
| 9379 | const ggml_compute_params * params, |
| 9380 | ggml_tensor * dst, |
| 9381 | const int64_t n_chunks, |
| 9382 | const int64_t chunk_size) { |
| 9383 | |
| 9384 | const ggml_tensor * q = dst->src[0]; |
| 9385 | const ggml_tensor * k = dst->src[1]; |
| 9386 | const ggml_tensor * v = dst->src[2]; |
| 9387 | |
| 9388 | const int64_t DK = k->ne[0]; |
| 9389 | const int64_t DV = v->ne[0]; |
| 9390 | const int64_t nek1 = k->ne[1]; |
| 9391 | const int64_t n_q_heads = q->ne[2]; |
| 9392 | |
| 9393 | const int ith = params->ith; |
| 9394 | const int nth = params->nth; |
| 9395 | |
| 9396 | const int64_t wdata_per_thread = DK + 2*DV + CACHE_LINE_SIZE_F32; |
| 9397 | float * thread_wdata = (float *) params->wdata + ith * wdata_per_thread; |
| 9398 | |
| 9399 | const int64_t partials_offset = nth * (DK + 2*DV + CACHE_LINE_SIZE_F32); |
| 9400 | const int64_t partial_size = 2 + DV; |
| 9401 | const float * partials_base = (const float *) params->wdata + partials_offset; |
| 9402 | |
| 9403 | // Output layout |
| 9404 | const int64_t ne1 = dst->ne[1]; |
| 9405 | const int64_t ne2 = dst->ne[2]; |
| 9406 | const size_t nb1 = dst->nb[1]; |
| 9407 | |
| 9408 | // Each thread reduces a subset of query heads |
| 9409 | for (int64_t q_head = ith; q_head < n_q_heads; q_head += nth) { |
| 9410 | float M_final = -INFINITY; |
| 9411 | float S_final = 0.0f; |
| 9412 | float * VKQ_final = thread_wdata; |
| 9413 | memset(VKQ_final, 0, DV * sizeof(float)); |
| 9414 | |
| 9415 | // Combine partials from all chunks |
| 9416 | for (int64_t chunk_idx = 0; chunk_idx < n_chunks; ++chunk_idx) { |
| 9417 | const int64_t ic_start = chunk_idx * chunk_size; |
| 9418 | if (ic_start >= nek1) continue; |
| 9419 | |
| 9420 | const float * partial = partials_base + (q_head * n_chunks + chunk_idx) * partial_size; |
| 9421 | const float M_chunk = partial[0]; |
| 9422 | const float S_chunk = partial[1]; |
| 9423 | const float * VKQ_chunk = partial + 2; |
| 9424 | |
| 9425 | if (S_chunk == 0.0f) continue; |
| 9426 | |
| 9427 | const float M_new = fmaxf(M_final, M_chunk); |
| 9428 | const float scale_old = expf(M_final - M_new); |
| 9429 | const float scale_new = expf(M_chunk - M_new); |
| 9430 | |
| 9431 | for (int64_t d = 0; d < DV; ++d) { |
| 9432 | VKQ_final[d] = VKQ_final[d] * scale_old + VKQ_chunk[d] * scale_new; |
| 9433 | } |
| 9434 | S_final = S_final * scale_old + S_chunk * scale_new; |
| 9435 | M_final = M_new; |
| 9436 | } |
| 9437 | |
| 9438 | // Normalize and write to output |
| 9439 | if (S_final != 0.0f) { |
| 9440 | const float S_inv = 1.0f / S_final; |
| 9441 | ggml_vec_scale_f32(DV, VKQ_final, S_inv); |
| 9442 | } |
| 9443 | // iq1=0, iq3=0 for decode |
| 9444 | memcpy((char *) dst->data + (0*ne2*ne1 + q_head + 0*ne1)*nb1, VKQ_final, nb1); |
| 9445 | } |
| 9446 | } |
| 9447 | |
| 9448 | static void ggml_compute_forward_flash_attn_ext_f16( |
| 9449 | const ggml_compute_params * params, |
| 9450 | ggml_tensor * dst) { |
| 9451 | |
| 9452 | const ggml_tensor * q = dst->src[0]; |
| 9453 | const ggml_tensor * k = dst->src[1]; |
| 9454 | const ggml_tensor * v = dst->src[2]; |
| 9455 | |
| 9456 | GGML_TENSOR_LOCALS(int64_t, neq, q, ne) |
| 9457 | GGML_TENSOR_LOCALS(size_t, nbq, q, nb) |
| 9458 | GGML_TENSOR_LOCALS(int64_t, nek, k, ne) |
| 9459 | GGML_TENSOR_LOCALS(size_t, nbk, k, nb) |
| 9460 | GGML_TENSOR_LOCALS(int64_t, nev, v, ne) |
| 9461 | GGML_TENSOR_LOCALS(size_t, nbv, v, nb) |
| 9462 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 9463 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 9464 | |
| 9465 | const int64_t DK = nek0; |
| 9466 | const int64_t DV = nev0; |
| 9467 | const int64_t N = neq1; |
| 9468 | |
| 9469 | |
| 9470 | GGML_ASSERT(ne0 == DV); |
| 9471 | GGML_ASSERT(ne2 == N); |
| 9472 | |
| 9473 | // input tensor rows must be contiguous |
| 9474 | GGML_ASSERT(nbq0 == ggml_type_size(q->type)); |
| 9475 | GGML_ASSERT(nbk0 == ggml_type_size(k->type)); |
| 9476 | GGML_ASSERT(nbv0 == ggml_type_size(v->type)); |
| 9477 | |
| 9478 | GGML_ASSERT(neq0 == DK); |
| 9479 | GGML_ASSERT(nek0 == DK); |
| 9480 | GGML_ASSERT(nev0 == DV); |
| 9481 | |
| 9482 | GGML_ASSERT(neq1 == N); |
| 9483 | |
| 9484 | // dst cannot be transposed or permuted |
| 9485 | GGML_ASSERT(nb0 == sizeof(float)); |
| 9486 | GGML_ASSERT(nb0 <= nb1); |
| 9487 | GGML_ASSERT(nb1 <= nb2); |
| 9488 | GGML_ASSERT(nb2 <= nb3); |
| 9489 | |
| 9490 | const int ith = params->ith; |
| 9491 | const int nth = params->nth; |
| 9492 | |
| 9493 | // When use_ref is set, force the vec-only reference implementation (no tiling, no KV-chunking) |
| 9494 | const bool use_ref = params->use_ref; |
| 9495 | |
| 9496 | // with kv_rows the loops run over the mask columns, the split KV path does not support it |
| 9497 | const ggml_tensor * kv_rows = dst->src[5]; |
| 9498 | const int64_t n_kv_cols = kv_rows ? kv_rows->ne[0] : nek1; |
| 9499 | |
| 9500 | const bool kv_is_f32_or_f16 = (k->type == GGML_TYPE_F32 || k->type == GGML_TYPE_F16); |
| 9501 | const bool use_split_kv_path = !use_ref && !kv_rows && (neq1 == 1 && neq3 == 1) && kv_is_f32_or_f16 && (k->type == v->type) && q->type == GGML_TYPE_F32 && nek1 >= 512; |
| 9502 | |
| 9503 | if (use_split_kv_path) { |
| 9504 | const int64_t chunk_size = (nek1 + nth - 1) / nth; |
| 9505 | |
| 9506 | // Partials buffer layout: [q_head][kv_chunk][M, S, VKQ] |
| 9507 | const int64_t partial_size = 2 + DV; |
| 9508 | float * partials_base = (float *) params->wdata + nth * (DK + 2*DV + CACHE_LINE_SIZE_F32); |
| 9509 | |
| 9510 | const int64_t ic_start = ith * chunk_size; |
| 9511 | const int64_t ic_end = std::min(ic_start + chunk_size, nek1); |
| 9512 | |
| 9513 | const int64_t partial_stride = nth * partial_size; |
| 9514 | float * chunk_partials = partials_base + ith * partial_size; |
| 9515 | |
| 9516 | if (ic_start < nek1) { |
| 9517 | for (int64_t q_head = 0; q_head < neq2; q_head++) { |
| 9518 | ggml_compute_forward_flash_attn_ext_f16_one_chunk( |
| 9519 | params, dst, q_head, q_head + 1, ic_start, ic_end, |
| 9520 | chunk_partials, partial_stride); |
| 9521 | } |
| 9522 | } else { |
| 9523 | for (int64_t q_head = 0; q_head < neq2; q_head++) { |
| 9524 | float * q_partials = chunk_partials + q_head * partial_stride; |
| 9525 | q_partials[0] = -INFINITY; // M |
| 9526 | q_partials[1] = 0.0f; // S |
| 9527 | } |
| 9528 | } |
| 9529 | |
| 9530 | ggml_barrier(params->threadpool); |
| 9531 | ggml_flash_attn_ext_reduce_partials(params, dst, nth, chunk_size); |
| 9532 | } else { |
| 9533 | |
| 9534 | // total rows in q |
| 9535 | const int64_t nr = neq1*neq2*neq3; |
| 9536 | |
| 9537 | // disable for NUMA |
| 9538 | const bool disable_chunking = ggml_is_numa(); |
| 9539 | |
| 9540 | // 4x chunks per thread |
| 9541 | int nth_scaled = nth * 4; |
| 9542 | int64_t chunk_size = (nr + nth_scaled - 1) / nth_scaled; |
| 9543 | int64_t nchunk = (nr + chunk_size - 1) / chunk_size; |
| 9544 | |
| 9545 | if (nth == 1 || nchunk < nth || disable_chunking) { |
| 9546 | nchunk = nth; |
| 9547 | } |
| 9548 | |
| 9549 | if (ith == 0) { |
| 9550 | ggml_threadpool_chunk_set(params->threadpool, nth); |
| 9551 | } |
| 9552 | |
| 9553 | ggml_barrier(params->threadpool); |
| 9554 | |
| 9555 | const int64_t dr = (nr + nchunk - 1) / nchunk; |
| 9556 | |
| 9557 | static constexpr int64_t Q_TILE_SZ = ggml_fa_tile_config::Q; |
| 9558 | bool use_tiled = !use_ref && |
| 9559 | (q->type == GGML_TYPE_F32 && |
| 9560 | kv_is_f32_or_f16 && |
| 9561 | k->type == v->type && |
| 9562 | neq1 >= Q_TILE_SZ); |
| 9563 | #if defined(GGML_SIMD) && !defined(__x86_64__) && !defined(_M_X64) |
| 9564 | #if defined(__ARM_FEATURE_SVE) |
| 9565 | const int64_t f32_epr = svcntw(); |
| 9566 | #else |
| 9567 | const int64_t f32_epr = GGML_F32_EPR; |
| 9568 | #endif |
| 9569 | use_tiled &= (DV % f32_epr == 0); |
| 9570 | #endif |
| 9571 | int current_chunk = ith; |
| 9572 | |
| 9573 | while (current_chunk < nchunk) { |
| 9574 | const int64_t ir0 = dr * current_chunk; |
| 9575 | const int64_t ir1 = MIN(ir0 + dr, nr); |
| 9576 | |
| 9577 | if (use_tiled) { |
| 9578 | ggml_compute_forward_flash_attn_ext_tiled(params, dst, ir0, ir1); |
| 9579 | } else { |
| 9580 | ggml_compute_forward_flash_attn_ext_f16_one_chunk(params, dst, ir0, ir1, 0, n_kv_cols, nullptr, 0); |
| 9581 | } |
| 9582 | |
| 9583 | current_chunk = ggml_threadpool_chunk_add(params->threadpool, 1); |
| 9584 | } |
| 9585 | } |
| 9586 | } |
| 9587 | |
| 9588 | void ggml_compute_forward_flash_attn_ext( |
| 9589 | const ggml_compute_params * params, |
| 9590 | ggml_tensor * dst) { |
| 9591 | switch (dst->op_params[3]) { |
| 9592 | case GGML_PREC_DEFAULT: |
| 9593 | case GGML_PREC_F32: |
| 9594 | { |
| 9595 | // uses F32 accumulators |
| 9596 | ggml_compute_forward_flash_attn_ext_f16(params, dst); |
| 9597 | } break; |
| 9598 | default: |
| 9599 | { |
| 9600 | GGML_ABORT("fatal error"); |
| 9601 | } |
| 9602 | } |
| 9603 | } |
| 9604 | |
| 9605 | // ggml_compute_forward_flash_attn_back |
| 9606 | |
| 9607 | static void ggml_compute_forward_flash_attn_back_f32( |
| 9608 | const ggml_compute_params * params, |
| 9609 | const bool masked, |
| 9610 | ggml_tensor * dst) { |
| 9611 | |
| 9612 | const ggml_tensor * q = dst->src[0]; |
| 9613 | const ggml_tensor * k = dst->src[1]; |
| 9614 | const ggml_tensor * v = dst->src[2]; |
| 9615 | const ggml_tensor * d = dst->src[3]; |
| 9616 | |
| 9617 | GGML_TENSOR_LOCALS(int64_t, neq, q, ne) |
| 9618 | GGML_TENSOR_LOCALS(size_t, nbq, q, nb) |
| 9619 | GGML_TENSOR_LOCALS(int64_t, nek, k, ne) |
| 9620 | GGML_TENSOR_LOCALS(size_t, nbk, k, nb) |
| 9621 | GGML_TENSOR_LOCALS(int64_t, nev, v, ne) |
| 9622 | GGML_TENSOR_LOCALS(size_t, nbv, v, nb) |
| 9623 | GGML_TENSOR_LOCALS(int64_t, ned, d, ne) |
| 9624 | GGML_TENSOR_LOCALS(size_t, nbd, d, nb) |
| 9625 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 9626 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 9627 | |
| 9628 | const int ith = params->ith; |
| 9629 | const int nth = params->nth; |
| 9630 | |
| 9631 | const int64_t D = neq0; |
| 9632 | const int64_t N = neq1; |
| 9633 | const int64_t P = nek1 - N; |
| 9634 | const int64_t M = P + N; |
| 9635 | |
| 9636 | const int Mup = ggml_up(M, GGML_SOFT_MAX_UNROLL); |
| 9637 | const int mxDM = MAX(D, Mup); |
| 9638 | |
| 9639 | // GGML_ASSERT(ne0 == D); |
| 9640 | // GGML_ASSERT(ne1 == N); |
| 9641 | GGML_ASSERT(P >= 0); |
| 9642 | |
| 9643 | GGML_ASSERT(nbq0 == sizeof(float)); |
| 9644 | GGML_ASSERT(nbk0 == sizeof(float)); |
| 9645 | GGML_ASSERT(nbv0 == sizeof(float)); |
| 9646 | |
| 9647 | GGML_ASSERT(neq0 == D); |
| 9648 | GGML_ASSERT(nek0 == D); |
| 9649 | GGML_ASSERT(nev1 == D); |
| 9650 | GGML_ASSERT(ned0 == D); |
| 9651 | |
| 9652 | GGML_ASSERT(neq1 == N); |
| 9653 | GGML_ASSERT(nek1 == N + P); |
| 9654 | GGML_ASSERT(nev1 == D); |
| 9655 | GGML_ASSERT(ned1 == N); |
| 9656 | |
| 9657 | // dst cannot be transposed or permuted |
| 9658 | GGML_ASSERT(nb0 == sizeof(float)); |
| 9659 | GGML_ASSERT(nb0 <= nb1); |
| 9660 | GGML_ASSERT(nb1 <= nb2); |
| 9661 | GGML_ASSERT(nb2 <= nb3); |
| 9662 | |
| 9663 | if (ith == 0) { |
| 9664 | memset(dst->data, 0, nb0*ne0*ne1*ne2*ne3); |
| 9665 | } |
| 9666 | ggml_barrier(params->threadpool); |
| 9667 | |
| 9668 | const int64_t elem_q = ggml_nelements(q); |
| 9669 | const int64_t elem_k = ggml_nelements(k); |
| 9670 | |
| 9671 | ggml_type result_type = dst->type; |
| 9672 | GGML_ASSERT(ggml_blck_size(result_type) == 1); |
| 9673 | const size_t tsize = ggml_type_size(result_type); |
| 9674 | |
| 9675 | const size_t offs_q = 0; |
| 9676 | const size_t offs_k = offs_q + GGML_PAD(elem_q * tsize, GGML_MEM_ALIGN); |
| 9677 | const size_t offs_v = offs_k + GGML_PAD(elem_k * tsize, GGML_MEM_ALIGN); |
| 9678 | |
| 9679 | void * grad_q = (char *) dst->data; |
| 9680 | void * grad_k = (char *) dst->data + offs_k; |
| 9681 | void * grad_v = (char *) dst->data + offs_v; |
| 9682 | |
| 9683 | const size_t nbgq1 = nb0*neq0; |
| 9684 | const size_t nbgq2 = nb0*neq0*neq1; |
| 9685 | const size_t nbgq3 = nb0*neq0*neq1*neq2; |
| 9686 | |
| 9687 | const size_t nbgk1 = nb0*nek0; |
| 9688 | const size_t nbgk2 = nb0*nek0*nek1; |
| 9689 | const size_t nbgk3 = nb0*nek0*nek1*neq2; |
| 9690 | |
| 9691 | const size_t nbgv1 = nb0*nev0; |
| 9692 | const size_t nbgv2 = nb0*nev0*nev1; |
| 9693 | const size_t nbgv3 = nb0*nev0*nev1*neq2; |
| 9694 | |
| 9695 | // parallelize by k rows using ggml_vec_dot_f32 |
| 9696 | |
| 9697 | // total rows in k |
| 9698 | const int nr = nek2*nek3; |
| 9699 | |
| 9700 | // rows per thread |
| 9701 | const int dr = (nr + nth - 1)/nth; |
| 9702 | |
| 9703 | // row range for this thread |
| 9704 | const int ir0 = dr*ith; |
| 9705 | const int ir1 = MIN(ir0 + dr, nr); |
| 9706 | |
| 9707 | const float scale = 1.0f/sqrtf(D); |
| 9708 | |
| 9709 | //printf("P=%d N=%d D=%d ir0=%d ir1=%d scale = %f\n", P, N, D, ir0, ir1, scale); |
| 9710 | |
| 9711 | // how often k2 (and v2) is repeated in q2 |
| 9712 | int nrep = neq2/nek2; |
| 9713 | |
| 9714 | for (int ir = ir0; ir < ir1; ++ir) { |
| 9715 | // q indices |
| 9716 | const int ik3 = ir/(nek2); |
| 9717 | const int ik2 = ir - ik3*nek2; |
| 9718 | |
| 9719 | const int iq3 = ik3; |
| 9720 | const int id3 = ik3; |
| 9721 | const int iv3 = ik3; |
| 9722 | const int iv2 = ik2; |
| 9723 | |
| 9724 | for (int irep = 0; irep < nrep; ++irep) { |
| 9725 | const int iq2 = ik2 + irep*nek2; |
| 9726 | const int id2 = iq2; |
| 9727 | |
| 9728 | // (ik2 + irep*nek2) % nek2 == ik2 |
| 9729 | for (int iq1 = 0; iq1 < neq1; ++iq1) { |
| 9730 | const int id1 = iq1; |
| 9731 | |
| 9732 | // not sure about CACHE_LINE_SIZE_F32.. |
| 9733 | // - maybe it must not be multiplied by 2 and excluded from .. in SM 1*(..) offset? |
| 9734 | float * S = (float *) params->wdata + ith*2*(mxDM + CACHE_LINE_SIZE_F32) + 0*(mxDM+CACHE_LINE_SIZE_F32); |
| 9735 | float * SM = (float *) params->wdata + ith*2*(mxDM + CACHE_LINE_SIZE_F32) + 1*(mxDM+CACHE_LINE_SIZE_F32); |
| 9736 | |
| 9737 | for (int i = M; i < Mup; ++i) { |
| 9738 | S[i] = -INFINITY; |
| 9739 | } |
| 9740 | |
| 9741 | const int64_t masked_begin = masked ? (P + iq1 + 1) : M; |
| 9742 | for (int64_t ic = 0; ic < masked_begin; ++ic) { |
| 9743 | // k indices |
| 9744 | const int ik1 = ic; |
| 9745 | |
| 9746 | // S indices |
| 9747 | const int i1 = ik1; |
| 9748 | |
| 9749 | ggml_vec_dot_f32(neq0, |
| 9750 | S + i1, 0, |
| 9751 | (float *) ((char *) k->data + (ik1*nbk1 + ik2*nbk2 + ik3*nbk3)), 0, |
| 9752 | (float *) ((char *) q->data + (iq1*nbq1 + iq2*nbq2 + iq3*nbq3)), 0, 1); |
| 9753 | } |
| 9754 | |
| 9755 | // scale |
| 9756 | ggml_vec_scale_f32(masked_begin, S, scale); |
| 9757 | |
| 9758 | for (int64_t i = masked_begin; i < M; i++) { |
| 9759 | S[i] = -INFINITY; |
| 9760 | } |
| 9761 | |
| 9762 | // softmax |
| 9763 | // exclude known -INF S[..] values from max and loop |
| 9764 | // dont forget to set their SM values to zero |
| 9765 | { |
| 9766 | float max = -INFINITY; |
| 9767 | ggml_vec_max_f32(masked_begin, &max, S); |
| 9768 | |
| 9769 | ggml_float sum = 0.0; |
| 9770 | { |
| 9771 | #ifdef GGML_SOFT_MAX_ACCELERATE |
| 9772 | max = -max; |
| 9773 | vDSP_vsadd(SM, 1, &max, SM, 1, Mup); |
| 9774 | vvexpf(SM, SM, &Mup); |
| 9775 | ggml_vec_sum_f32(Mup, &sum, SM); |
| 9776 | #else |
| 9777 | sum = ggml_vec_soft_max_f32(Mup, SM, S, max); |
| 9778 | #endif |
| 9779 | } |
| 9780 | |
| 9781 | assert(sum > 0.0); |
| 9782 | |
| 9783 | sum = 1.0/sum; |
| 9784 | ggml_vec_scale_f32(masked_begin, SM, sum); |
| 9785 | |
| 9786 | } |
| 9787 | |
| 9788 | // step-by-step explanation |
| 9789 | { |
| 9790 | // forward-process shape grads from backward process |
| 9791 | // parallel_for ik2,ik3: |
| 9792 | // for irep: |
| 9793 | // iq2 = ik2 + irep*nek2 |
| 9794 | // k[:D,:M,:,:] [D,M,:,:] grad[k][:D,:M,ik2,ik3] += grad[kcur] |
| 9795 | // q[:D,:N,:,:] [D,N,:,:] grad[q][:D,iq1,iq2,iq3] += grad[qcur] |
| 9796 | // v[:M,:D,:,:] [M,D,:,:] grad[v][:M,:D,iv2,iv3] += grad[vcur] |
| 9797 | // for iq1: |
| 9798 | // kcur = k[:D,:M,ik2,ik3] [D,M,1,1] grad[kcur] = grad[S1].T @ qcur |
| 9799 | // qcur = q[:D,iq1,iq2,iq3] [D,1,1,1] grad[qcur] = grad[S1] @ kcur |
| 9800 | // vcur = v[:M,:D,iv2,iv3] [M,D,1,1] grad[vcur] = grad[S5].T @ S4 |
| 9801 | // S0 = -Inf [D,1,1,1] |
| 9802 | // ~S1[i] = dot(kcur[:D,i], qcur) |
| 9803 | // S1 = qcur @ kcur.T [M,1,1,1] grad[S1] = grad[S2] * scale |
| 9804 | // S2 = S1 * scale [M,1,1,1] grad[S2] = diag_mask_zero(grad[S3], P) |
| 9805 | // S3 = diag_mask_inf(S2, P) [M,1,1,1] grad[S3] = S4 * (grad[S4] - dot(S4, grad[S4])) |
| 9806 | // S4 = softmax(S3) [M,1,1,1] grad[S4] = grad[S5] @ vcur |
| 9807 | // ~S5[i] = dot(vcur[:,i], S4) |
| 9808 | // S5 = S4 @ vcur.T [D,1,1,1] grad[S5] = d[:D,id1,id2,id3] |
| 9809 | // ~dst[i,iq1,iq2,iq3] = S5[i] ^ |
| 9810 | // dst[:D,iq1,iq2,iq3] = S5 | grad[dst[:D,iq1,iq2,iq3]] = d[:D,id1,id2,id3] |
| 9811 | // dst backward-/ grad[dst] = d |
| 9812 | // |
| 9813 | // output gradients with their dependencies: |
| 9814 | // |
| 9815 | // grad[kcur] = grad[S1].T @ qcur |
| 9816 | // grad[S1] = diag_mask_zero(grad[S3], P) * scale |
| 9817 | // grad[S3] = S4 * (grad[S4] - dot(S4, grad[S4])) |
| 9818 | // grad[S4] = grad[S5] @ vcur |
| 9819 | // grad[S4] = d[:D,id1,id2,id3] @ vcur |
| 9820 | // grad[qcur] = grad[S1] @ kcur |
| 9821 | // grad[vcur] = grad[S5].T @ S4 |
| 9822 | // grad[vcur] = d[:D,id1,id2,id3].T @ S4 |
| 9823 | // |
| 9824 | // in post-order: |
| 9825 | // |
| 9826 | // S1 = qcur @ kcur.T |
| 9827 | // S2 = S1 * scale |
| 9828 | // S3 = diag_mask_inf(S2, P) |
| 9829 | // S4 = softmax(S3) |
| 9830 | // grad[S4] = d[:D,id1,id2,id3] @ vcur |
| 9831 | // grad[S3] = S4 * (grad[S4] - dot(S4, grad[S4])) |
| 9832 | // grad[S1] = diag_mask_zero(grad[S3], P) * scale |
| 9833 | // grad[qcur] = grad[S1] @ kcur |
| 9834 | // grad[kcur] = grad[S1].T @ qcur |
| 9835 | // grad[vcur] = d[:D,id1,id2,id3].T @ S4 |
| 9836 | // |
| 9837 | // using less variables (SM=S4): |
| 9838 | // |
| 9839 | // S = diag_mask_inf(qcur @ kcur.T * scale, P) |
| 9840 | // SM = softmax(S) |
| 9841 | // S = d[:D,iq1,iq2,iq3] @ vcur |
| 9842 | // dot_SM_gradSM = dot(SM, S) |
| 9843 | // S = SM * (S - dot(SM, S)) |
| 9844 | // S = diag_mask_zero(S, P) * scale |
| 9845 | // |
| 9846 | // grad[q][:D,iq1,iq2,iq3] += S @ kcur |
| 9847 | // grad[k][:D,:M,ik2,ik3] += S.T @ qcur |
| 9848 | // grad[v][:M,:D,iv2,iv3] += d[:D,id1,id2,id3].T @ SM |
| 9849 | } |
| 9850 | |
| 9851 | // S = gradSM = d[:D,id1,id2,id3] @ vcur[:,:,iv2,iv3] |
| 9852 | // S = d[:D,id1,id2,id3] @ vcur[:,:,iv2,iv3] |
| 9853 | // for ic: |
| 9854 | // S[:M] += vcur[:M,ic,iv2,iv3] * d[ic,id1,id2,id3] |
| 9855 | // exclude known future zero S[..] values from operation |
| 9856 | ggml_vec_set_f32(masked_begin, S, 0); |
| 9857 | for (int64_t ic = 0; ic < D; ++ic) { |
| 9858 | ggml_vec_mad_f32(masked_begin, |
| 9859 | S, |
| 9860 | (float *) ((char *) v->data + ( ic*nbv1 + iv2*nbv2 + iv3*nbv3)), |
| 9861 | *(float *) ((char *) d->data + (ic*nbd0 + id1*nbd1 + id2*nbd2 + id3*nbd3))); |
| 9862 | } |
| 9863 | |
| 9864 | // S = SM * (S - dot(SM, S)) |
| 9865 | float dot_SM_gradSM = 0; |
| 9866 | ggml_vec_dot_f32 (masked_begin, &dot_SM_gradSM, 0, SM, 0, S, 0, 1); |
| 9867 | ggml_vec_acc1_f32(M, S, -dot_SM_gradSM); |
| 9868 | ggml_vec_mul_f32 (masked_begin, S, S, SM); |
| 9869 | |
| 9870 | // S = diag_mask_zero(S, P) * scale |
| 9871 | // already done by above ggml_vec_set_f32 |
| 9872 | |
| 9873 | // exclude known zero S[..] values from operation |
| 9874 | ggml_vec_scale_f32(masked_begin, S, scale); |
| 9875 | |
| 9876 | // S shape [M,1] |
| 9877 | // SM shape [M,1] |
| 9878 | // kcur shape [D,M] |
| 9879 | // qcur shape [D,1] |
| 9880 | // vcur shape [M,D] |
| 9881 | |
| 9882 | // grad[q][:D,iq1,iq2,iq3] += S @ kcur |
| 9883 | // grad[q][:D,iq1,iq2,iq3] += shape[M,1] @ shape[D,M] |
| 9884 | // for ic: |
| 9885 | // grad[q][:D,iq1,iq2,iq3] += S[ic] * kcur[:D,ic,ik2,ik3] |
| 9886 | // exclude known zero S[..] values from loop |
| 9887 | for (int64_t ic = 0; ic < masked_begin; ++ic) { |
| 9888 | ggml_vec_mad_f32(D, |
| 9889 | (float *) ((char *) grad_q + (iq1*nbgq1 + iq2*nbgq2 + iq3*nbgq3)), |
| 9890 | (float *) ((char *) k->data + (ic*nbk1 + ik2*nbk2 + ik3*nbk3)), |
| 9891 | S[ic]); |
| 9892 | } |
| 9893 | |
| 9894 | // grad[k][:D,:M,iq2,iq3] += S.T @ qcur |
| 9895 | // for ic: |
| 9896 | // grad[k][:D,ic,iq2,iq3] += S.T[0,ic] * qcur[:D,0] |
| 9897 | // grad[k][:D,ic,iq2,iq3] += S[ic] * qcur[:D,0] |
| 9898 | // exclude known zero S[..] values from loop |
| 9899 | for (int64_t ic = 0; ic < masked_begin; ++ic) { |
| 9900 | ggml_vec_mad_f32(D, |
| 9901 | (float *) ((char *) grad_k + (ic*nbgk1 + ik2*nbgk2 + ik3*nbgk3)), |
| 9902 | (float *) ((char *) q->data + (iq1*nbq1 + iq2*nbq2 + iq3*nbq3)), |
| 9903 | S[ic]); |
| 9904 | } |
| 9905 | |
| 9906 | // grad[v][:M,:D,iv2,iv3] += d[:D,id1,id2,id3].T @ SM |
| 9907 | // for ic: |
| 9908 | // grad[v][:M,ic,iv2,iv3] += d[:D,id1,id2,id3].T[0,ic] * SM[:M] |
| 9909 | // grad[v][:M,ic,iv2,iv3] += d[ic,id1,id2,id3] * SM[:M] |
| 9910 | // exclude known zero SM[..] values from mad |
| 9911 | for (int64_t ic = 0; ic < D; ++ic) { |
| 9912 | ggml_vec_mad_f32(masked_begin, |
| 9913 | (float *) ((char *) grad_v + ( ic*nbgv1 + iv2*nbgv2 + iv3*nbgv3)), |
| 9914 | SM, |
| 9915 | *(float *) ((char *) d->data + (ic*nbd0 + id1*nbd1 + id2*nbd2 + id3*nbd3))); |
| 9916 | } |
| 9917 | } |
| 9918 | } |
| 9919 | } |
| 9920 | } |
| 9921 | |
| 9922 | void ggml_compute_forward_flash_attn_back( |
| 9923 | const ggml_compute_params * params, |
| 9924 | const bool masked, |
| 9925 | ggml_tensor * dst) { |
| 9926 | |
| 9927 | const ggml_tensor * q = dst->src[0]; |
| 9928 | |
| 9929 | switch (q->type) { |
| 9930 | case GGML_TYPE_F32: |
| 9931 | { |
| 9932 | ggml_compute_forward_flash_attn_back_f32(params, masked, dst); |
| 9933 | } break; |
| 9934 | default: |
| 9935 | { |
| 9936 | GGML_ABORT("fatal error"); |
| 9937 | } |
| 9938 | } |
| 9939 | } |
| 9940 | |
| 9941 | // ggml_compute_forward_ssm_conv |
| 9942 | |
| 9943 | static void ggml_compute_forward_ssm_conv_f32( |
| 9944 | const ggml_compute_params * params, |
| 9945 | ggml_tensor * dst) { |
| 9946 | const ggml_tensor * src0 = dst->src[0]; // conv_x |
| 9947 | const ggml_tensor * src1 = dst->src[1]; // conv1d.weight |
| 9948 | |
| 9949 | const int ith = params->ith; |
| 9950 | const int nth = params->nth; |
| 9951 | |
| 9952 | const int nc = src1->ne[0]; // d_conv |
| 9953 | const int ncs = src0->ne[0]; // d_conv - 1 + n_t |
| 9954 | const int nr = src0->ne[1]; // d_inner |
| 9955 | const int n_t = dst->ne[1]; // tokens per sequence |
| 9956 | const int n_s = dst->ne[2]; // number of sequences in the batch |
| 9957 | |
| 9958 | GGML_ASSERT( dst->ne[0] == nr); |
| 9959 | GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 9960 | GGML_ASSERT(src1->nb[0] == sizeof(float)); |
| 9961 | GGML_ASSERT(src0->nb[1] == src0->ne[0]*sizeof(float)); |
| 9962 | |
| 9963 | // rows per thread |
| 9964 | const int dr = (nr + nth - 1)/nth; |
| 9965 | |
| 9966 | // row range for this thread |
| 9967 | const int ir0 = dr*ith; |
| 9968 | const int ir1 = MIN(ir0 + dr, nr); |
| 9969 | const int ir = ir1 - ir0; |
| 9970 | |
| 9971 | for (int i3 = 0; i3 < n_s; ++i3) { |
| 9972 | for (int i2 = 0; i2 < n_t; ++i2) { |
| 9973 | // {d_conv - 1 + n_t, d_inner, n_seqs} |
| 9974 | // sliding window |
| 9975 | const float * s = (const float *) ((const char *) src0->data + ir0*(src0->nb[1]) + i2*(src0->nb[0]) + i3*(src0->nb[2])); // {d_conv, d_inner, n_s} |
| 9976 | const float * c = (const float *) ((const char *) src1->data + ir0*(src1->nb[1])); // {d_conv, d_inner} |
| 9977 | float * x = (float *) ((char *) dst->data + ir0*(dst->nb[0]) + i2*(dst->nb[1]) + i3*(dst->nb[2])); // {d_inner, n_t, n_s} |
| 9978 | |
| 9979 | // TODO: transpose the output for smaller strides for big batches? |
| 9980 | // d_inner |
| 9981 | for (int i1 = 0; i1 < ir; ++i1) { |
| 9982 | // rowwise dot product |
| 9983 | // NOTE: not using ggml_vec_dot_f32, because its sum is in double precision |
| 9984 | float sumf = 0.0f; |
| 9985 | |
| 9986 | // d_conv |
| 9987 | for (int i0 = 0; i0 < nc; ++i0) { |
| 9988 | sumf += s[i0 + i1*ncs] * c[i0 + i1*nc]; |
| 9989 | } |
| 9990 | x[i1] = sumf; |
| 9991 | } |
| 9992 | } |
| 9993 | } |
| 9994 | } |
| 9995 | |
| 9996 | void ggml_compute_forward_ssm_conv( |
| 9997 | const ggml_compute_params * params, |
| 9998 | ggml_tensor * dst) { |
| 9999 | switch (dst->src[0]->type) { |
| 10000 | case GGML_TYPE_F32: |
| 10001 | { |
| 10002 | ggml_compute_forward_ssm_conv_f32(params, dst); |
| 10003 | } break; |
| 10004 | default: |
| 10005 | { |
| 10006 | GGML_ABORT("fatal error"); |
| 10007 | } |
| 10008 | } |
| 10009 | } |
| 10010 | |
| 10011 | // ggml_compute_forward_ssm_scan |
| 10012 | |
| 10013 | static void ggml_compute_forward_ssm_scan_f32( |
| 10014 | const ggml_compute_params * params, |
| 10015 | ggml_tensor * dst) { |
| 10016 | const ggml_tensor * src0 = dst->src[0]; // s {d_state, dim, n_head, n_seqs+} |
| 10017 | const ggml_tensor * src1 = dst->src[1]; // x {dim, n_head, n_seq_tokens, n_seqs} |
| 10018 | const ggml_tensor * src2 = dst->src[2]; // dt {n_head, n_seq_tokens, n_seqs} |
| 10019 | const ggml_tensor * src3 = dst->src[3]; // A {d_state, n_head} or {1, n_head} |
| 10020 | const ggml_tensor * src4 = dst->src[4]; // B {d_state, n_group, n_seq_tokens, n_seqs} |
| 10021 | const ggml_tensor * src5 = dst->src[5]; // C {d_state, n_group, n_seq_tokens, n_seqs} |
| 10022 | const ggml_tensor * src6 = dst->src[6]; // ids {n_seqs} |
| 10023 | |
| 10024 | const int ith = params->ith; |
| 10025 | const int nth = params->nth; |
| 10026 | |
| 10027 | const int64_t nc = src0->ne[0]; // d_state |
| 10028 | const int64_t nr = src0->ne[1]; // dim |
| 10029 | const int64_t nh = src1->ne[1]; // n_head |
| 10030 | const int64_t ng = src4->ne[1]; |
| 10031 | const int64_t nt = src1->ne[2]; // number of tokens per sequence |
| 10032 | const int64_t ns = src1->ne[3]; // number of sequences in the batch |
| 10033 | const int64_t K = ggml_get_op_params_i32(dst, 0); |
| 10034 | |
| 10035 | // can't use ggml_nbytes because src1 is not necessarily contiguous |
| 10036 | const int64_t s_off = ggml_nelements(src1) * ggml_element_size(src1); |
| 10037 | |
| 10038 | GGML_ASSERT(K >= 1); |
| 10039 | GGML_ASSERT(ggml_nelements(src1) + K*nc*nr*nh*ns == ggml_nelements(dst)); |
| 10040 | GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 10041 | GGML_ASSERT(src1->nb[0] == sizeof(float)); |
| 10042 | GGML_ASSERT(src2->nb[0] == sizeof(float)); |
| 10043 | GGML_ASSERT(src3->nb[0] == sizeof(float)); |
| 10044 | GGML_ASSERT(src4->nb[0] == sizeof(float)); |
| 10045 | GGML_ASSERT(src5->nb[0] == sizeof(float)); |
| 10046 | GGML_ASSERT(src6->nb[0] == sizeof(int32_t)); |
| 10047 | GGML_ASSERT(nh % ng == 0); |
| 10048 | GGML_ASSERT(src3->ne[0] == 1 || K == 1); |
| 10049 | |
| 10050 | // heads per thread |
| 10051 | const int dh = (nh + nth - 1)/nth; |
| 10052 | |
| 10053 | // head range for this thread |
| 10054 | const int ih0 = dh*ith; |
| 10055 | const int ih1 = MIN(ih0 + dh, nh); |
| 10056 | |
| 10057 | const int32_t * ids = (const int32_t *) src6->data; |
| 10058 | |
| 10059 | for (int i3 = 0; i3 < ns; ++i3) { |
| 10060 | const float * s0 = (const float *) ((const char *) src0->data + ids[i3]*(src0->nb[3])); // {d_state, dim, nh, ns} |
| 10061 | float * s = ( float *) (( char *) dst->data + i3*(src0->nb[3]) + s_off); // {d_state, dim, nh, ns} |
| 10062 | |
| 10063 | for (int i2 = 0; i2 < nt; ++i2) { |
| 10064 | const float * x = (const float *) ((const char *) src1->data + i2*(src1->nb[2]) + i3*(src1->nb[3])); // {dim, nh, nt, ns} |
| 10065 | const float * dt = (const float *) ((const char *) src2->data + i2*(src2->nb[1]) + i3*(src2->nb[2])); // {nh, nt, ns} |
| 10066 | const float * A = (const float *) ((const char *) src3->data); // {d_state, nh} or {1, nh} |
| 10067 | const float * B = (const float *) ((const char *) src4->data + i2*(src4->nb[2]) + i3*(src4->nb[3])); // {d_state, ng, nt, ns} |
| 10068 | const float * C = (const float *) ((const char *) src5->data + i2*(src5->nb[2]) + i3*(src5->nb[3])); // {d_state, ng, nt, ns} |
| 10069 | float * y = ( float *) (( char *) dst->data + i2*(nh*nr*sizeof(float)) + i3*(nt*nh*nr*sizeof(float))); // {dim, nh, nt, ns} |
| 10070 | |
| 10071 | if (src3->ne[0] == 1) { |
| 10072 | // Mamba-2 has a scalar decay factor per head; dA can be outside the state-wise loop |
| 10073 | |
| 10074 | // n_head |
| 10075 | for (int h = ih0; h < ih1; ++h) { |
| 10076 | // ref: https://github.com/state-spaces/mamba/blob/62db608da60f6fc790b8ed9f4b3225e95ca15fde/mamba_ssm/ops/triton/softplus.py#L16 |
| 10077 | const float dt_soft_plus = ggml_compute_softplus_f32(dt[h]); |
| 10078 | const float dA = expf(dt_soft_plus * A[h]); |
| 10079 | const int g = h / (nh / ng); // repeat_interleave |
| 10080 | |
| 10081 | // dim |
| 10082 | for (int i1 = 0; i1 < nr; ++i1) { |
| 10083 | const int ii = i1 + h*nr; |
| 10084 | const float x_dt = x[ii] * dt_soft_plus; |
| 10085 | float sumf = 0.0f; |
| 10086 | #if defined(GGML_SIMD) |
| 10087 | #if defined(__ARM_FEATURE_SVE) |
| 10088 | const int ggml_f32_epr = svcntw(); |
| 10089 | const int ggml_f32_step = 1 * ggml_f32_epr; |
| 10090 | |
| 10091 | const int np = (nc & ~(ggml_f32_step - 1)); |
| 10092 | |
| 10093 | GGML_F32_VEC sum = GGML_F32_VEC_ZERO; |
| 10094 | |
| 10095 | GGML_F32_VEC adA = GGML_F32_VEC_SET1(dA); |
| 10096 | GGML_F32_VEC axdt = GGML_F32_VEC_SET1(x_dt); |
| 10097 | |
| 10098 | for (int i = 0; i < np; i += ggml_f32_step) { |
| 10099 | // TODO: maybe unroll more? |
| 10100 | for (int j = 0; j < 1; j++) { |
| 10101 | GGML_F32_VEC t0 = GGML_F32_VEC_LOAD(s0 + i + j*ggml_f32_epr + ii*nc); |
| 10102 | GGML_F32_VEC t1 = GGML_F32_VEC_LOAD(B + i + j*ggml_f32_epr + g*nc); |
| 10103 | GGML_F32_VEC t2 = GGML_F32_VEC_LOAD(C + i + j*ggml_f32_epr + g*nc); |
| 10104 | |
| 10105 | t0 = GGML_F32_VEC_MUL(t0, adA); |
| 10106 | t1 = GGML_F32_VEC_MUL(t1, axdt); |
| 10107 | |
| 10108 | t0 = GGML_F32_VEC_ADD(t0, t1); |
| 10109 | |
| 10110 | sum = GGML_F32_VEC_FMA(sum, t0, t2); |
| 10111 | |
| 10112 | GGML_F32_VEC_STORE(s + i + j*ggml_f32_epr + ii*nc, t0); |
| 10113 | } |
| 10114 | } |
| 10115 | |
| 10116 | sumf = GGML_F32xt_REDUCE_ONE(sum); |
| 10117 | #elif defined(__riscv_v) |
| 10118 | // todo: RVV implementation |
| 10119 | const int np = 0; |
| 10120 | #else |
| 10121 | const int np = (nc & ~(GGML_F32_STEP - 1)); |
| 10122 | |
| 10123 | GGML_F32_VEC sum[GGML_F32_ARR] = { GGML_F32_VEC_ZERO }; |
| 10124 | |
| 10125 | GGML_F32_VEC adA = GGML_F32_VEC_SET1(dA); |
| 10126 | GGML_F32_VEC axdt = GGML_F32_VEC_SET1(x_dt); |
| 10127 | |
| 10128 | GGML_F32_VEC ax[GGML_F32_ARR]; |
| 10129 | GGML_F32_VEC ay[GGML_F32_ARR]; |
| 10130 | GGML_F32_VEC az[GGML_F32_ARR]; |
| 10131 | |
| 10132 | for (int i = 0; i < np; i += GGML_F32_STEP) { |
| 10133 | for (int j = 0; j < GGML_F32_ARR; j++) { |
| 10134 | ax[j] = GGML_F32_VEC_LOAD(s0 + i + j*GGML_F32_EPR + ii*nc); |
| 10135 | ay[j] = GGML_F32_VEC_LOAD(B + i + j*GGML_F32_EPR + g*nc); |
| 10136 | az[j] = GGML_F32_VEC_LOAD(C + i + j*GGML_F32_EPR + g*nc); |
| 10137 | |
| 10138 | ax[j] = GGML_F32_VEC_MUL(ax[j], adA); |
| 10139 | ay[j] = GGML_F32_VEC_MUL(ay[j], axdt); |
| 10140 | |
| 10141 | ax[j] = GGML_F32_VEC_ADD(ax[j], ay[j]); |
| 10142 | |
| 10143 | sum[j] = GGML_F32_VEC_FMA(sum[j], ax[j], az[j]); |
| 10144 | |
| 10145 | GGML_F32_VEC_STORE(s + i + j*GGML_F32_EPR + ii*nc, ax[j]); |
| 10146 | } |
| 10147 | } |
| 10148 | |
| 10149 | // reduce sum0..sum3 to sum0 |
| 10150 | GGML_F32_VEC_REDUCE(sumf, sum); |
| 10151 | #endif |
| 10152 | #else |
| 10153 | const int np = 0; |
| 10154 | #endif |
| 10155 | // d_state |
| 10156 | for (int i0 = np; i0 < nc; ++i0) { |
| 10157 | const int i = i0 + ii*nc; |
| 10158 | const int ig = i0 + g*nc; |
| 10159 | // state = prev_state * dA + dB * x |
| 10160 | const float state = (s0[i] * dA) + (B[ig] * x_dt); |
| 10161 | // y = rowwise_dotprod(state, C) |
| 10162 | sumf += state * C[ig]; |
| 10163 | s[i] = state; |
| 10164 | } |
| 10165 | y[ii] = sumf; |
| 10166 | } |
| 10167 | } |
| 10168 | } else { |
| 10169 | // Mamba-1 has an element-wise decay factor for the states |
| 10170 | |
| 10171 | // n_head |
| 10172 | for (int h = ih0; h < ih1; ++h) { |
| 10173 | // ref: https://github.com/state-spaces/mamba/blob/62db608da60f6fc790b8ed9f4b3225e95ca15fde/mamba_ssm/ops/triton/softplus.py#L16 |
| 10174 | const float dt_soft_plus = ggml_compute_softplus_f32(dt[h]); |
| 10175 | const int g = h / (nh / ng); // repeat_interleave |
| 10176 | |
| 10177 | // dim |
| 10178 | for (int i1 = 0; i1 < nr; ++i1) { |
| 10179 | const int ii = i1 + h*nr; |
| 10180 | const float x_dt = x[ii] * dt_soft_plus; |
| 10181 | #if defined(__ARM_FEATURE_SVE) |
| 10182 | svfloat32_t vx_dt = GGML_F32_VEC_SET1(x_dt); |
| 10183 | svfloat32_t vdt_soft_plus = GGML_F32_VEC_SET1(dt_soft_plus); |
| 10184 | svfloat32_t r1_vector = GGML_F32_VEC_ZERO; |
| 10185 | |
| 10186 | // d_state |
| 10187 | // TODO: what happens when (d_state % svcntw()) != 0? |
| 10188 | for (int64_t k = 0; k < nc; k += svcntw()) { |
| 10189 | svfloat32_t vA = GGML_F32_VEC_LOAD(&A[h*nc + k]); |
| 10190 | svfloat32_t vB = GGML_F32_VEC_LOAD(&B[k + g*nc]); |
| 10191 | svfloat32_t vC = GGML_F32_VEC_LOAD(&C[k + g*nc]); |
| 10192 | svfloat32_t vs0 = GGML_F32_VEC_LOAD(&s0[ii*nc + k]); |
| 10193 | |
| 10194 | svfloat32_t t1 = GGML_F32_VEC_MUL(vdt_soft_plus, vA); |
| 10195 | t1 = exp_ps_sve(svptrue_b32(), t1); |
| 10196 | svfloat32_t t2 = GGML_F32_VEC_MUL(vx_dt, vB); |
| 10197 | |
| 10198 | vs0 = GGML_F32_VEC_FMA(t2, vs0, t1); |
| 10199 | r1_vector = GGML_F32_VEC_ADD(GGML_F32_VEC_MUL(vs0, vC), r1_vector); |
| 10200 | |
| 10201 | GGML_F32_VEC_STORE(&s[ii*nc + k], vs0); |
| 10202 | } |
| 10203 | y[ii] = GGML_F32xt_REDUCE_ONE(r1_vector); |
| 10204 | #else |
| 10205 | float sumf = 0.0f; |
| 10206 | // NOTE: can't really use GGML_SIMD here because d_state is usually 16 |
| 10207 | // and also because expf is used within the loop. |
| 10208 | // d_state |
| 10209 | for (int i0 = 0; i0 < nc; ++i0) { |
| 10210 | const int i = i0 + ii*nc; |
| 10211 | const int ig = i0 + g*nc; |
| 10212 | // state = prev_state * dA + dB * x |
| 10213 | const float state = (s0[i] * expf(dt_soft_plus * A[i0 + h*nc])) + (B[ig] * x_dt); |
| 10214 | // y = rowwise_dotprod(state, C) |
| 10215 | sumf += state * C[ig]; |
| 10216 | s[i] = state; |
| 10217 | } |
| 10218 | y[ii] = sumf; |
| 10219 | #endif |
| 10220 | } |
| 10221 | } |
| 10222 | } |
| 10223 | const int64_t slot = nt - 1 - i2; |
| 10224 | if (K > 1 && slot > 0 && slot < K) { |
| 10225 | float * s_snapshot = (float *) ((char *) dst->data + s_off + (slot*ns + i3)*(src0->nb[3])); |
| 10226 | for (int h = ih0; h < ih1; ++h) { |
| 10227 | memcpy((char *) s_snapshot + h*src0->nb[2], (char *) s + h*src0->nb[2], src0->nb[2]); |
| 10228 | } |
| 10229 | } |
| 10230 | // use the output as the source when it's not the first token-wise iteration |
| 10231 | s0 = s; |
| 10232 | } |
| 10233 | } |
| 10234 | } |
| 10235 | |
| 10236 | void ggml_compute_forward_ssm_scan( |
| 10237 | const ggml_compute_params * params, |
| 10238 | ggml_tensor * dst) { |
| 10239 | switch (dst->src[0]->type) { |
| 10240 | case GGML_TYPE_F32: |
| 10241 | { |
| 10242 | ggml_compute_forward_ssm_scan_f32(params, dst); |
| 10243 | } break; |
| 10244 | default: |
| 10245 | { |
| 10246 | GGML_ABORT("fatal error"); |
| 10247 | } |
| 10248 | } |
| 10249 | } |
| 10250 | |
| 10251 | // ggml_compute_forward_win_part |
| 10252 | |
| 10253 | static void ggml_compute_forward_win_part_f32( |
| 10254 | const ggml_compute_params * params, |
| 10255 | ggml_tensor * dst) { |
| 10256 | GGML_UNUSED(params); |
| 10257 | |
| 10258 | const ggml_tensor * src0 = dst->src[0]; |
| 10259 | |
| 10260 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 10261 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 10262 | |
| 10263 | const int32_t nep0 = ((const int32_t *)(dst->op_params))[0]; |
| 10264 | const int32_t nep1 = ((const int32_t *)(dst->op_params))[1]; |
| 10265 | const int32_t w = ((const int32_t *)(dst->op_params))[2]; |
| 10266 | |
| 10267 | assert(ne00 == ne0); |
| 10268 | assert(ne3 == nep0*nep1); |
| 10269 | |
| 10270 | // TODO: optimize / multi-thread |
| 10271 | for (int py = 0; py < nep1; ++py) { |
| 10272 | for (int px = 0; px < nep0; ++px) { |
| 10273 | const int64_t i3 = py*nep0 + px; |
| 10274 | for (int64_t i2 = 0; i2 < ne2; ++i2) { |
| 10275 | for (int64_t i1 = 0; i1 < ne1; ++i1) { |
| 10276 | for (int64_t i0 = 0; i0 < ne0; ++i0) { |
| 10277 | const int64_t i02 = py*w + i2; |
| 10278 | const int64_t i01 = px*w + i1; |
| 10279 | const int64_t i00 = i0; |
| 10280 | |
| 10281 | const int64_t i = i3*ne2*ne1*ne0 + i2*ne1*ne0 + i1*ne0 + i0; |
| 10282 | const int64_t j = i02*ne01*ne00 + i01*ne00 + i00; |
| 10283 | |
| 10284 | if (py*w + i2 >= ne02 || px*w + i1 >= ne01) { |
| 10285 | ((float *) dst->data)[i] = 0.0f; |
| 10286 | } else { |
| 10287 | ((float *) dst->data)[i] = ((float *) src0->data)[j]; |
| 10288 | } |
| 10289 | } |
| 10290 | } |
| 10291 | } |
| 10292 | } |
| 10293 | } |
| 10294 | } |
| 10295 | |
| 10296 | void ggml_compute_forward_win_part( |
| 10297 | const ggml_compute_params * params, |
| 10298 | ggml_tensor * dst) { |
| 10299 | |
| 10300 | const ggml_tensor * src0 = dst->src[0]; |
| 10301 | |
| 10302 | switch (src0->type) { |
| 10303 | case GGML_TYPE_F32: |
| 10304 | { |
| 10305 | ggml_compute_forward_win_part_f32(params, dst); |
| 10306 | } break; |
| 10307 | default: |
| 10308 | { |
| 10309 | GGML_ABORT("fatal error"); |
| 10310 | } |
| 10311 | } |
| 10312 | } |
| 10313 | |
| 10314 | // ggml_compute_forward_win_unpart |
| 10315 | |
| 10316 | static void ggml_compute_forward_win_unpart_f32( |
| 10317 | const ggml_compute_params * params, |
| 10318 | ggml_tensor * dst) { |
| 10319 | GGML_UNUSED(params); |
| 10320 | |
| 10321 | const ggml_tensor * src0 = dst->src[0]; |
| 10322 | |
| 10323 | GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 10324 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 10325 | |
| 10326 | const int32_t w = ((const int32_t *)(dst->op_params))[0]; |
| 10327 | |
| 10328 | // padding |
| 10329 | const int px = (w - ne1%w)%w; |
| 10330 | //const int py = (w - ne2%w)%w; |
| 10331 | |
| 10332 | const int npx = (px + ne1)/w; |
| 10333 | //const int npy = (py + ne2)/w; |
| 10334 | |
| 10335 | assert(ne0 == ne00); |
| 10336 | |
| 10337 | // TODO: optimize / multi-thread |
| 10338 | for (int64_t i2 = 0; i2 < ne2; ++i2) { |
| 10339 | for (int64_t i1 = 0; i1 < ne1; ++i1) { |
| 10340 | for (int64_t i0 = 0; i0 < ne0; ++i0) { |
| 10341 | const int ip2 = i2/w; |
| 10342 | const int ip1 = i1/w; |
| 10343 | |
| 10344 | const int64_t i02 = i2%w; |
| 10345 | const int64_t i01 = i1%w; |
| 10346 | const int64_t i00 = i0; |
| 10347 | |
| 10348 | const int64_t i = (ip2*npx + ip1)*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00 + i00; |
| 10349 | const int64_t j = i2*ne1*ne0 + i1*ne0 + i0; |
| 10350 | |
| 10351 | ((float *) dst->data)[j] = ((float *) src0->data)[i]; |
| 10352 | } |
| 10353 | } |
| 10354 | } |
| 10355 | } |
| 10356 | |
| 10357 | void ggml_compute_forward_win_unpart( |
| 10358 | const ggml_compute_params * params, |
| 10359 | ggml_tensor * dst) { |
| 10360 | |
| 10361 | const ggml_tensor * src0 = dst->src[0]; |
| 10362 | |
| 10363 | switch (src0->type) { |
| 10364 | case GGML_TYPE_F32: |
| 10365 | { |
| 10366 | ggml_compute_forward_win_unpart_f32(params, dst); |
| 10367 | } break; |
| 10368 | default: |
| 10369 | { |
| 10370 | GGML_ABORT("fatal error"); |
| 10371 | } |
| 10372 | } |
| 10373 | } |
| 10374 | |
| 10375 | //ggml_compute_forward_unary |
| 10376 | |
| 10377 | void ggml_compute_forward_unary( |
| 10378 | const ggml_compute_params * params, |
| 10379 | ggml_tensor * dst) { |
| 10380 | |
| 10381 | const ggml_unary_op op = ggml_get_unary_op(dst); |
| 10382 | |
| 10383 | switch (op) { |
| 10384 | case GGML_UNARY_OP_ABS: |
| 10385 | { |
| 10386 | ggml_compute_forward_abs(params, dst); |
| 10387 | } break; |
| 10388 | case GGML_UNARY_OP_SGN: |
| 10389 | { |
| 10390 | ggml_compute_forward_sgn(params, dst); |
| 10391 | } break; |
| 10392 | case GGML_UNARY_OP_NEG: |
| 10393 | { |
| 10394 | ggml_compute_forward_neg(params, dst); |
| 10395 | } break; |
| 10396 | case GGML_UNARY_OP_STEP: |
| 10397 | { |
| 10398 | ggml_compute_forward_step(params, dst); |
| 10399 | } break; |
| 10400 | case GGML_UNARY_OP_TANH: |
| 10401 | { |
| 10402 | ggml_compute_forward_tanh(params, dst); |
| 10403 | } break; |
| 10404 | case GGML_UNARY_OP_ELU: |
| 10405 | { |
| 10406 | ggml_compute_forward_elu(params, dst); |
| 10407 | } break; |
| 10408 | case GGML_UNARY_OP_RELU: |
| 10409 | { |
| 10410 | ggml_compute_forward_relu(params, dst); |
| 10411 | } break; |
| 10412 | case GGML_UNARY_OP_SIGMOID: |
| 10413 | { |
| 10414 | ggml_compute_forward_sigmoid(params, dst); |
| 10415 | } break; |
| 10416 | case GGML_UNARY_OP_GELU: |
| 10417 | { |
| 10418 | ggml_compute_forward_gelu(params, dst); |
| 10419 | } break; |
| 10420 | case GGML_UNARY_OP_GELU_ERF: |
| 10421 | { |
| 10422 | ggml_compute_forward_gelu_erf(params, dst); |
| 10423 | } break; |
| 10424 | case GGML_UNARY_OP_GELU_QUICK: |
| 10425 | { |
| 10426 | ggml_compute_forward_gelu_quick(params, dst); |
| 10427 | } break; |
| 10428 | case GGML_UNARY_OP_SILU: |
| 10429 | { |
| 10430 | ggml_compute_forward_silu(params, dst); |
| 10431 | } break; |
| 10432 | case GGML_UNARY_OP_HARDSWISH: |
| 10433 | { |
| 10434 | ggml_compute_forward_hardswish(params, dst); |
| 10435 | } break; |
| 10436 | case GGML_UNARY_OP_HARDSIGMOID: |
| 10437 | { |
| 10438 | ggml_compute_forward_hardsigmoid(params, dst); |
| 10439 | } break; |
| 10440 | case GGML_UNARY_OP_EXP: |
| 10441 | { |
| 10442 | ggml_compute_forward_exp(params, dst); |
| 10443 | } break; |
| 10444 | case GGML_UNARY_OP_FLOOR: |
| 10445 | { |
| 10446 | ggml_compute_forward_floor(params, dst); |
| 10447 | } break; |
| 10448 | case GGML_UNARY_OP_CEIL: |
| 10449 | { |
| 10450 | ggml_compute_forward_ceil(params, dst); |
| 10451 | } break; |
| 10452 | case GGML_UNARY_OP_ROUND: |
| 10453 | { |
| 10454 | ggml_compute_forward_round(params, dst); |
| 10455 | } break; |
| 10456 | case GGML_UNARY_OP_TRUNC: |
| 10457 | { |
| 10458 | ggml_compute_forward_trunc(params, dst); |
| 10459 | } break; |
| 10460 | case GGML_UNARY_OP_XIELU: |
| 10461 | { |
| 10462 | ggml_compute_forward_xielu(params, dst); |
| 10463 | } break; |
| 10464 | case GGML_UNARY_OP_EXPM1: |
| 10465 | { |
| 10466 | ggml_compute_forward_expm1(params, dst); |
| 10467 | } break; |
| 10468 | case GGML_UNARY_OP_SOFTPLUS: |
| 10469 | { |
| 10470 | ggml_compute_forward_softplus(params, dst); |
| 10471 | } break; |
| 10472 | default: |
| 10473 | { |
| 10474 | GGML_ABORT("fatal error"); |
| 10475 | } |
| 10476 | } |
| 10477 | } |
| 10478 | |
| 10479 | //ggml_compute_forward_glu |
| 10480 | |
| 10481 | void ggml_compute_forward_glu( |
| 10482 | const ggml_compute_params * params, |
| 10483 | ggml_tensor * dst) { |
| 10484 | |
| 10485 | const ggml_glu_op op = ggml_get_glu_op(dst); |
| 10486 | |
| 10487 | switch (op) { |
| 10488 | case GGML_GLU_OP_REGLU: |
| 10489 | { |
| 10490 | ggml_compute_forward_reglu(params, dst); |
| 10491 | } break; |
| 10492 | case GGML_GLU_OP_GEGLU: |
| 10493 | { |
| 10494 | ggml_compute_forward_geglu(params, dst); |
| 10495 | } break; |
| 10496 | case GGML_GLU_OP_SWIGLU: |
| 10497 | { |
| 10498 | ggml_compute_forward_swiglu(params, dst); |
| 10499 | } break; |
| 10500 | case GGML_GLU_OP_SWIGLU_OAI: |
| 10501 | { |
| 10502 | ggml_compute_forward_swiglu_oai(params, dst); |
| 10503 | } break; |
| 10504 | case GGML_GLU_OP_GEGLU_ERF: |
| 10505 | { |
| 10506 | ggml_compute_forward_geglu_erf(params, dst); |
| 10507 | } break; |
| 10508 | case GGML_GLU_OP_GEGLU_QUICK: |
| 10509 | { |
| 10510 | ggml_compute_forward_geglu_quick(params, dst); |
| 10511 | } break; |
| 10512 | case GGML_GLU_OP_SWIGLU_CLAMP: |
| 10513 | { |
| 10514 | ggml_compute_forward_swiglu_clamp(params, dst); |
| 10515 | } break; |
| 10516 | default: |
| 10517 | { |
| 10518 | GGML_ABORT("fatal error"); |
| 10519 | } |
| 10520 | } |
| 10521 | } |
| 10522 | |
| 10523 | // ggml_compute_forward_get_rel_pos |
| 10524 | |
| 10525 | static void ggml_compute_forward_get_rel_pos_f16( |
| 10526 | const ggml_compute_params * params, |
| 10527 | ggml_tensor * dst) { |
| 10528 | GGML_UNUSED(params); |
| 10529 | |
| 10530 | const ggml_tensor * src0 = dst->src[0]; |
| 10531 | |
| 10532 | // ref: https://github.com/facebookresearch/segment-anything/blob/main/segment_anything/modeling/image_encoder.py#L292-L322 |
| 10533 | |
| 10534 | GGML_TENSOR_UNARY_OP_LOCALS |
| 10535 | |
| 10536 | const int64_t w = ne1; |
| 10537 | |
| 10538 | ggml_fp16_t * src0_data = (ggml_fp16_t *) src0->data; |
| 10539 | ggml_fp16_t * dst_data = (ggml_fp16_t *) dst->data; |
| 10540 | |
| 10541 | for (int64_t i2 = 0; i2 < ne2; ++i2) { |
| 10542 | for (int64_t i1 = 0; i1 < ne1; ++i1) { |
| 10543 | const int64_t pos = (w - i1 - 1) + i2; |
| 10544 | for (int64_t i0 = 0; i0 < ne0; ++i0) { |
| 10545 | dst_data[i2*ne1*ne0 + i1*ne0 + i0] = src0_data[pos*ne00 + i0]; |
| 10546 | } |
| 10547 | } |
| 10548 | } |
| 10549 | } |
| 10550 | |
| 10551 | void ggml_compute_forward_get_rel_pos( |
| 10552 | const ggml_compute_params * params, |
| 10553 | ggml_tensor * dst) { |
| 10554 | |
| 10555 | const ggml_tensor * src0 = dst->src[0]; |
| 10556 | |
| 10557 | switch (src0->type) { |
| 10558 | case GGML_TYPE_F16: |
| 10559 | case GGML_TYPE_BF16: |
| 10560 | { |
| 10561 | ggml_compute_forward_get_rel_pos_f16(params, dst); |
| 10562 | } break; |
| 10563 | default: |
| 10564 | { |
| 10565 | GGML_ABORT("fatal error"); |
| 10566 | } |
| 10567 | } |
| 10568 | } |
| 10569 | |
| 10570 | // ggml_compute_forward_add_rel_pos |
| 10571 | |
| 10572 | static void ggml_compute_forward_add_rel_pos_f32( |
| 10573 | const ggml_compute_params * params, |
| 10574 | ggml_tensor * dst) { |
| 10575 | |
| 10576 | const ggml_tensor * src0 = dst->src[0]; |
| 10577 | const ggml_tensor * src1 = dst->src[1]; |
| 10578 | const ggml_tensor * src2 = dst->src[2]; |
| 10579 | |
| 10580 | const bool inplace = (bool) ((int32_t *) dst->op_params)[0]; |
| 10581 | if (!inplace) { |
| 10582 | if (params->ith == 0) { |
| 10583 | memcpy((char *) dst->data, (char *) src0->data, ggml_nbytes(dst)); |
| 10584 | } |
| 10585 | ggml_barrier(params->threadpool); |
| 10586 | } |
| 10587 | // ref: https://github.com/facebookresearch/segment-anything/blob/main/segment_anything/modeling/image_encoder.py#L357-L359 |
| 10588 | |
| 10589 | float * src1_data = (float *) src1->data; |
| 10590 | float * src2_data = (float *) src2->data; |
| 10591 | float * dst_data = (float *) dst->data; |
| 10592 | |
| 10593 | const int64_t ne10 = src1->ne[0]; |
| 10594 | const int64_t ne11 = src1->ne[1]; |
| 10595 | const int64_t ne12 = src1->ne[2]; |
| 10596 | const int64_t ne13 = src1->ne[3]; |
| 10597 | |
| 10598 | const int ith = params->ith; |
| 10599 | const int nth = params->nth; |
| 10600 | |
| 10601 | // total patches in dst |
| 10602 | const int np = ne13; |
| 10603 | |
| 10604 | // patches per thread |
| 10605 | const int dp = (np + nth - 1)/nth; |
| 10606 | |
| 10607 | // patch range for this thread |
| 10608 | const int ip0 = dp*ith; |
| 10609 | const int ip1 = MIN(ip0 + dp, np); |
| 10610 | |
| 10611 | for (int64_t i13 = ip0; i13 < ip1; ++i13) { |
| 10612 | for (int64_t i12 = 0; i12 < ne12; ++i12) { |
| 10613 | for (int64_t i11 = 0; i11 < ne11; ++i11) { |
| 10614 | const int64_t jp1 = i13*ne12*ne11*ne10 + i12*ne11*ne10 + i11*ne10; |
| 10615 | for (int64_t i10 = 0; i10 < ne10; ++i10) { |
| 10616 | const int64_t jp0 = jp1 + i10; |
| 10617 | const float src1_e = src1_data[jp0]; |
| 10618 | const float src2_e = src2_data[jp0]; |
| 10619 | |
| 10620 | const int64_t jdh = jp0 * ne10; |
| 10621 | const int64_t jdw = jdh - (ne10 - 1) * i10; |
| 10622 | |
| 10623 | for (int64_t j = 0; j < ne10; ++j) { |
| 10624 | dst_data[jdh + j ] += src2_e; |
| 10625 | dst_data[jdw + j*ne10] += src1_e; |
| 10626 | } |
| 10627 | } |
| 10628 | } |
| 10629 | } |
| 10630 | } |
| 10631 | } |
| 10632 | |
| 10633 | void ggml_compute_forward_add_rel_pos( |
| 10634 | const ggml_compute_params * params, |
| 10635 | ggml_tensor * dst) { |
| 10636 | |
| 10637 | const ggml_tensor * src0 = dst->src[0]; |
| 10638 | |
| 10639 | switch (src0->type) { |
| 10640 | case GGML_TYPE_F32: |
| 10641 | { |
| 10642 | ggml_compute_forward_add_rel_pos_f32(params, dst); |
| 10643 | } break; |
| 10644 | default: |
| 10645 | { |
| 10646 | GGML_ABORT("fatal error"); |
| 10647 | } |
| 10648 | } |
| 10649 | } |
| 10650 | |
| 10651 | // ggml_compute_forward_rwkv_wkv6 |
| 10652 | |
| 10653 | static void ggml_compute_forward_rwkv_wkv6_f32( |
| 10654 | const ggml_compute_params * params, |
| 10655 | ggml_tensor * dst) { |
| 10656 | const int64_t T = dst->src[1]->ne[2]; |
| 10657 | const int64_t C = dst->ne[0]; |
| 10658 | const int64_t HEADS = dst->src[1]->ne[1]; |
| 10659 | const int64_t n_seqs = dst->src[5]->ne[1]; |
| 10660 | const int64_t head_size = C / HEADS; |
| 10661 | |
| 10662 | float * dst_data = (float *) dst->data; |
| 10663 | float * state = ((float *) dst->data) + C * T; |
| 10664 | |
| 10665 | const int ith = params->ith; |
| 10666 | const int nth = params->nth; |
| 10667 | |
| 10668 | const int h_start = (HEADS * (ith )) / nth; |
| 10669 | const int h_end = ((HEADS * (ith + 1)) / nth < HEADS) ? |
| 10670 | (HEADS * (ith + 1)) / nth : HEADS; |
| 10671 | |
| 10672 | float * k = (float *) dst->src[0]->data; |
| 10673 | float * v = (float *) dst->src[1]->data; |
| 10674 | float * r = (float *) dst->src[2]->data; |
| 10675 | float * time_faaaa = (float *) dst->src[3]->data; |
| 10676 | float * time_decay = (float *) dst->src[4]->data; |
| 10677 | |
| 10678 | size_t t_stride = HEADS * head_size; // Same to C |
| 10679 | |
| 10680 | size_t h_stride = C / HEADS; |
| 10681 | GGML_ASSERT(C % HEADS == 0); // C must be divisible by HEADS |
| 10682 | size_t h_stride_2d = head_size * head_size; |
| 10683 | |
| 10684 | if (ith == 0) { |
| 10685 | memset(dst_data, 0, T * C * sizeof(float)); |
| 10686 | } |
| 10687 | ggml_barrier(params->threadpool); |
| 10688 | |
| 10689 | |
| 10690 | #if defined(__AVX__) && !defined(__AVX512F__) |
| 10691 | #define GGML_F32X GGML_F32x8 |
| 10692 | #define GGML_F32X_SET1 GGML_F32x8_SET1 |
| 10693 | #define GGML_F32X_LOAD GGML_F32x8_LOAD |
| 10694 | #define GGML_F32X_STORE GGML_F32x8_STORE |
| 10695 | #define GGML_F32X_MUL GGML_F32x8_MUL |
| 10696 | #define GGML_F32X_FMA GGML_F32x8_FMA |
| 10697 | #define WKV_VECTOR_SIZE 8 |
| 10698 | #elif defined(__AVX512F__) |
| 10699 | #define GGML_F32X GGML_F32x16 |
| 10700 | #define GGML_F32X_SET1 GGML_F32x16_SET1 |
| 10701 | #define GGML_F32X_LOAD GGML_F32x16_LOAD |
| 10702 | #define GGML_F32X_STORE GGML_F32x16_STORE |
| 10703 | #define GGML_F32X_MUL GGML_F32x16_MUL |
| 10704 | #define GGML_F32X_FMA GGML_F32x16_FMA |
| 10705 | #define WKV_VECTOR_SIZE 16 |
| 10706 | #elif defined(__ARM_FEATURE_SVE) && defined(__aarch64__) |
| 10707 | #define GGML_F32X GGML_F32xt |
| 10708 | #define GGML_F32X_SET1 GGML_F32xt_SET1 |
| 10709 | #define GGML_F32X_LOAD GGML_F32xt_LOAD |
| 10710 | #define GGML_F32X_STORE GGML_F32xt_STORE |
| 10711 | #define GGML_F32X_MUL GGML_F32xt_MUL |
| 10712 | #define GGML_F32X_FMA GGML_F32xt_FMA |
| 10713 | #define WKV_VECTOR_SIZE 8 |
| 10714 | #elif defined(__ARM_NEON) && defined(__aarch64__) |
| 10715 | #define GGML_F32X GGML_F32x4 |
| 10716 | #define GGML_F32X_SET1 GGML_F32x4_SET1 |
| 10717 | #define GGML_F32X_LOAD GGML_F32x4_LOAD |
| 10718 | #define GGML_F32X_STORE GGML_F32x4_STORE |
| 10719 | #define GGML_F32X_MUL GGML_F32x4_MUL |
| 10720 | #define GGML_F32X_FMA GGML_F32x4_FMA |
| 10721 | #define WKV_VECTOR_SIZE 4 |
| 10722 | #endif |
| 10723 | |
| 10724 | #ifdef WKV_VECTOR_SIZE |
| 10725 | int wkv_vector_size; |
| 10726 | #if defined(__ARM_FEATURE_SVE) |
| 10727 | wkv_vector_size = svcntw(); |
| 10728 | #else |
| 10729 | wkv_vector_size = WKV_VECTOR_SIZE; |
| 10730 | #endif |
| 10731 | const int64_t vec_count = head_size / wkv_vector_size; |
| 10732 | |
| 10733 | for (int64_t t = 0; t < T; t++) { |
| 10734 | size_t t_offset = t * t_stride; |
| 10735 | size_t state_offset = head_size * C * (t / (T / n_seqs)); |
| 10736 | float * state_cur = state + state_offset; |
| 10737 | float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[5]->data + state_offset; |
| 10738 | |
| 10739 | for (int64_t h = h_start; h < h_end; h++) { |
| 10740 | size_t h_offset = h * h_stride; |
| 10741 | size_t t_h_offset = t_offset + h_offset; |
| 10742 | size_t h_2d_offset = h * h_stride_2d; |
| 10743 | |
| 10744 | for (int64_t i = 0; i < head_size; i++) { |
| 10745 | size_t t_h_i_offset = t_h_offset + i; |
| 10746 | size_t h_i_offset = h_offset + i; |
| 10747 | size_t h_2d_i_offset = h_2d_offset + i * h_stride; |
| 10748 | |
| 10749 | float k_val = k[t_h_i_offset]; |
| 10750 | float r_val = r[t_h_i_offset]; |
| 10751 | float time_faaaa_val = time_faaaa[h_i_offset]; |
| 10752 | float time_decay_val = time_decay[t_h_i_offset]; |
| 10753 | |
| 10754 | // Broadcast scalar values to vectors |
| 10755 | GGML_F32X k_vec = GGML_F32X_SET1(k_val); |
| 10756 | GGML_F32X r_vec = GGML_F32X_SET1(r_val); |
| 10757 | GGML_F32X time_faaaa_vec = GGML_F32X_SET1(time_faaaa_val); |
| 10758 | GGML_F32X time_decay_vec = GGML_F32X_SET1(time_decay_val); |
| 10759 | |
| 10760 | for (int64_t j = 0; j < vec_count; j++) { |
| 10761 | size_t base_j = j * wkv_vector_size; |
| 10762 | size_t t_h_j_offset = t_h_offset + base_j; |
| 10763 | size_t h_2d_i_j_offset = h_2d_i_offset + base_j; |
| 10764 | |
| 10765 | // Load x elements at once |
| 10766 | GGML_F32X v_vec = GGML_F32X_LOAD(&v[t_h_j_offset]); |
| 10767 | GGML_F32X prev_state_vec = GGML_F32X_LOAD(&state_prev[h_2d_i_j_offset]); |
| 10768 | GGML_F32X dst_vec = GGML_F32X_LOAD(&dst_data[t_h_j_offset]); |
| 10769 | |
| 10770 | // Compute kv = v * k |
| 10771 | GGML_F32X kv_vec = GGML_F32X_MUL(v_vec, k_vec); |
| 10772 | |
| 10773 | // Compute temp = kv * time_faaaa + prev_state |
| 10774 | GGML_F32X temp_vec = GGML_F32X_FMA(prev_state_vec, kv_vec, time_faaaa_vec); |
| 10775 | |
| 10776 | // Update dst: dst += temp * r |
| 10777 | dst_vec = GGML_F32X_FMA(dst_vec, temp_vec, r_vec); |
| 10778 | GGML_F32X_STORE(&dst_data[t_h_j_offset], dst_vec); |
| 10779 | |
| 10780 | // Update state: state = prev_state * time_decay + kv |
| 10781 | GGML_F32X new_state_vec = GGML_F32X_FMA(kv_vec, prev_state_vec, time_decay_vec); |
| 10782 | GGML_F32X_STORE(&state_cur[h_2d_i_j_offset], new_state_vec); |
| 10783 | } |
| 10784 | |
| 10785 | // Handle remaining elements, this will not be used. |
| 10786 | for (int64_t j = vec_count * wkv_vector_size; j < head_size; j++) { |
| 10787 | size_t t_h_j_offset = t_h_offset + j; |
| 10788 | size_t h_2d_i_j_offset = h_2d_i_offset + j; |
| 10789 | float v_val = v[t_h_j_offset]; |
| 10790 | float kv_val = v_val * k_val; |
| 10791 | float prev_state_val = state_prev[h_2d_i_j_offset]; |
| 10792 | float temp_val = kv_val * time_faaaa_val + prev_state_val; |
| 10793 | dst_data[t_h_j_offset] += temp_val * r_val; |
| 10794 | state_cur[h_2d_i_j_offset] = prev_state_val * time_decay_val + kv_val; |
| 10795 | } |
| 10796 | } |
| 10797 | } |
| 10798 | } |
| 10799 | |
| 10800 | #else |
| 10801 | // basically fused operations: |
| 10802 | // dst = r @ (time_faaaa * (k @ v) + state), |
| 10803 | // state = time_decay * state + (k @ v), |
| 10804 | // recursive through each token |
| 10805 | for (int64_t t = 0; t < T; t++) { |
| 10806 | size_t t_offset = t * t_stride; |
| 10807 | size_t state_offset = head_size * C * (t / (T / n_seqs)); |
| 10808 | float * state_cur = state + state_offset; |
| 10809 | float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[5]->data + state_offset; |
| 10810 | |
| 10811 | for (int64_t h = h_start; h < h_end; h++) { |
| 10812 | size_t h_offset = h * h_stride; |
| 10813 | size_t t_h_offset = t_offset + h_offset; |
| 10814 | size_t h_2d_offset = h * h_stride_2d; |
| 10815 | |
| 10816 | for (int64_t i = 0; i < head_size; i++) { |
| 10817 | size_t t_h_i_offset = t_h_offset + i; |
| 10818 | size_t h_i_offset = h_offset + i; |
| 10819 | size_t h_2d_i_offset = h_2d_offset + i * h_stride; |
| 10820 | |
| 10821 | float k_val = k[t_h_i_offset]; |
| 10822 | float r_val = r[t_h_i_offset]; |
| 10823 | float time_faaaa_val = time_faaaa[h_i_offset]; |
| 10824 | // RWKV v6: different time_decay for each token. |
| 10825 | float time_decay_val = time_decay[t_h_i_offset]; |
| 10826 | |
| 10827 | for (int64_t j = 0; j < head_size; j++) { |
| 10828 | size_t t_h_j_offset = t_h_offset + j; |
| 10829 | size_t h_2d_i_j_offset = h_2d_i_offset + j; |
| 10830 | |
| 10831 | float v_val = v[t_h_j_offset]; |
| 10832 | float kv_val = v_val * k_val; |
| 10833 | float prev_state_val = state_prev[h_2d_i_j_offset]; |
| 10834 | float temp_val = kv_val * time_faaaa_val + prev_state_val; |
| 10835 | dst_data[t_h_j_offset] += temp_val * r_val; |
| 10836 | state_cur[h_2d_i_j_offset] = prev_state_val * time_decay_val + kv_val; |
| 10837 | } |
| 10838 | } |
| 10839 | } |
| 10840 | } |
| 10841 | #endif |
| 10842 | } |
| 10843 | |
| 10844 | |
| 10845 | void ggml_compute_forward_rwkv_wkv6( |
| 10846 | const ggml_compute_params * params, |
| 10847 | ggml_tensor * dst) { |
| 10848 | |
| 10849 | const ggml_tensor * src0 = dst->src[0]; |
| 10850 | |
| 10851 | switch (src0->type) { |
| 10852 | case GGML_TYPE_F32: |
| 10853 | { |
| 10854 | ggml_compute_forward_rwkv_wkv6_f32(params, dst); |
| 10855 | } break; |
| 10856 | default: |
| 10857 | { |
| 10858 | GGML_ABORT("fatal error"); |
| 10859 | } |
| 10860 | } |
| 10861 | } |
| 10862 | |
| 10863 | // ggml_compute_forward_gla |
| 10864 | |
| 10865 | static void ggml_compute_forward_gla_f32( |
| 10866 | const ggml_compute_params * params, |
| 10867 | ggml_tensor * dst) { |
| 10868 | const int64_t T = dst->src[1]->ne[2]; |
| 10869 | const int64_t C = dst->ne[0]; |
| 10870 | const int64_t HEADS = dst->src[1]->ne[1]; |
| 10871 | const int64_t n_seqs = dst->src[4]->ne[1]; |
| 10872 | const int64_t head_size = C / HEADS; |
| 10873 | const float scale = ggml_get_op_params_f32(dst, 0); |
| 10874 | |
| 10875 | float * dst_data = (float *) dst->data; |
| 10876 | float * state = ((float *) dst->data) + C * T; |
| 10877 | |
| 10878 | const int ith = params->ith; |
| 10879 | const int nth = params->nth; |
| 10880 | |
| 10881 | const int h_start = (HEADS * (ith )) / nth; |
| 10882 | const int h_end = ((HEADS * (ith + 1)) / nth < HEADS) ? |
| 10883 | (HEADS * (ith + 1)) / nth : HEADS; |
| 10884 | |
| 10885 | float * k = (float *) dst->src[0]->data; |
| 10886 | float * v = (float *) dst->src[1]->data; |
| 10887 | float * q = (float *) dst->src[2]->data; |
| 10888 | float * g = (float *) dst->src[3]->data; |
| 10889 | |
| 10890 | size_t t_stride = HEADS * head_size; // Same to C |
| 10891 | |
| 10892 | size_t h_stride = C / HEADS; |
| 10893 | GGML_ASSERT(C % HEADS == 0); // C must be divisible by HEADS |
| 10894 | size_t h_stride_2d = head_size * head_size; |
| 10895 | |
| 10896 | if (ith == 0) { |
| 10897 | memset(dst_data, 0, T * C * sizeof(float)); |
| 10898 | } |
| 10899 | ggml_barrier(params->threadpool); |
| 10900 | |
| 10901 | |
| 10902 | #if defined(__AVX__) && !defined(__AVX512F__) |
| 10903 | #define GGML_F32X GGML_F32x8 |
| 10904 | #define GGML_F32X_SET1 GGML_F32x8_SET1 |
| 10905 | #define GGML_F32X_LOAD GGML_F32x8_LOAD |
| 10906 | #define GGML_F32X_STORE GGML_F32x8_STORE |
| 10907 | #define GGML_F32X_MUL GGML_F32x8_MUL |
| 10908 | #define GGML_F32X_FMA GGML_F32x8_FMA |
| 10909 | #define GLA_VECTOR_SIZE 8 |
| 10910 | #elif defined(__AVX512F__) |
| 10911 | #define GGML_F32X GGML_F32x16 |
| 10912 | #define GGML_F32X_SET1 GGML_F32x16_SET1 |
| 10913 | #define GGML_F32X_LOAD GGML_F32x16_LOAD |
| 10914 | #define GGML_F32X_STORE GGML_F32x16_STORE |
| 10915 | #define GGML_F32X_MUL GGML_F32x16_MUL |
| 10916 | #define GGML_F32X_FMA GGML_F32x16_FMA |
| 10917 | #define GLA_VECTOR_SIZE 16 |
| 10918 | #elif defined(__ARM_FEATURE_SVE) && defined(__aarch64__) |
| 10919 | #define GGML_F32X GGML_F32xt |
| 10920 | #define GGML_F32X_SET1 GGML_F32xt_SET1 |
| 10921 | #define GGML_F32X_LOAD GGML_F32xt_LOAD |
| 10922 | #define GGML_F32X_STORE GGML_F32xt_STORE |
| 10923 | #define GGML_F32X_MUL GGML_F32xt_MUL |
| 10924 | #define GGML_F32X_FMA GGML_F32xt_FMA |
| 10925 | #define GLA_VECTOR_SIZE 8 |
| 10926 | #elif defined(__ARM_NEON) && defined(__aarch64__) |
| 10927 | #define GGML_F32X GGML_F32x4 |
| 10928 | #define GGML_F32X_SET1 GGML_F32x4_SET1 |
| 10929 | #define GGML_F32X_LOAD GGML_F32x4_LOAD |
| 10930 | #define GGML_F32X_STORE GGML_F32x4_STORE |
| 10931 | #define GGML_F32X_MUL GGML_F32x4_MUL |
| 10932 | #define GGML_F32X_FMA GGML_F32x4_FMA |
| 10933 | #define GLA_VECTOR_SIZE 4 |
| 10934 | #endif |
| 10935 | |
| 10936 | #ifdef GLA_VECTOR_SIZE |
| 10937 | int gla_vector_size; |
| 10938 | #if defined(__ARM_FEATURE_SVE) |
| 10939 | gla_vector_size = svcntw(); |
| 10940 | #else |
| 10941 | gla_vector_size = GLA_VECTOR_SIZE; |
| 10942 | #endif |
| 10943 | const int64_t vec_count = head_size / gla_vector_size; |
| 10944 | |
| 10945 | for (int64_t t = 0; t < T; t++) { |
| 10946 | size_t t_offset = t * t_stride; |
| 10947 | size_t state_offset = head_size * C * (t / (T / n_seqs)); |
| 10948 | float * state_cur = state + state_offset; |
| 10949 | float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[4]->data + state_offset; |
| 10950 | |
| 10951 | for (int64_t h = h_start; h < h_end; h++) { |
| 10952 | size_t h_offset = h * h_stride; |
| 10953 | size_t t_h_offset = t_offset + h_offset; |
| 10954 | size_t h_2d_offset = h * h_stride_2d; |
| 10955 | |
| 10956 | for (int64_t i = 0; i < head_size; i++) { |
| 10957 | size_t t_h_i_offset = t_h_offset + i; |
| 10958 | size_t h_2d_i_offset = h_2d_offset + i * h_stride; |
| 10959 | |
| 10960 | float k_val = k[t_h_i_offset]; |
| 10961 | float q_val = q[t_h_i_offset] * scale; |
| 10962 | float g_val = g[t_h_i_offset]; |
| 10963 | |
| 10964 | // Broadcast scalar values to vectors |
| 10965 | GGML_F32X k_vec = GGML_F32X_SET1(k_val); |
| 10966 | GGML_F32X q_vec = GGML_F32X_SET1(q_val); |
| 10967 | GGML_F32X g_vec = GGML_F32X_SET1(g_val); |
| 10968 | |
| 10969 | for (int64_t j = 0; j < vec_count; j++) { |
| 10970 | size_t base_j = j * gla_vector_size; |
| 10971 | size_t t_h_j_offset = t_h_offset + base_j; |
| 10972 | size_t h_2d_i_j_offset = h_2d_i_offset + base_j; |
| 10973 | |
| 10974 | // Load x elements at once |
| 10975 | GGML_F32X v_vec = GGML_F32X_LOAD(&v[t_h_j_offset]); |
| 10976 | GGML_F32X prev_state_vec = GGML_F32X_LOAD(&state_prev[h_2d_i_j_offset]); |
| 10977 | GGML_F32X dst_vec = GGML_F32X_LOAD(&dst_data[t_h_j_offset]); |
| 10978 | |
| 10979 | // Compute kv = v * k |
| 10980 | GGML_F32X kv_vec = GGML_F32X_MUL(v_vec, k_vec); |
| 10981 | |
| 10982 | // Compute temp = prev_state * g + kv |
| 10983 | GGML_F32X temp_vec = GGML_F32X_FMA(kv_vec, prev_state_vec, g_vec); |
| 10984 | |
| 10985 | // Update dst: dst += temp * q |
| 10986 | dst_vec = GGML_F32X_FMA(dst_vec, temp_vec, q_vec); |
| 10987 | GGML_F32X_STORE(&dst_data[t_h_j_offset], dst_vec); |
| 10988 | |
| 10989 | // Update state |
| 10990 | GGML_F32X_STORE(&state_cur[h_2d_i_j_offset], temp_vec); |
| 10991 | } |
| 10992 | |
| 10993 | // Handle remaining elements, this will not be used. |
| 10994 | for (int64_t j = vec_count * gla_vector_size; j < head_size; j++) { |
| 10995 | size_t t_h_j_offset = t_h_offset + j; |
| 10996 | size_t h_2d_i_j_offset = h_2d_i_offset + j; |
| 10997 | float v_val = v[t_h_j_offset]; |
| 10998 | float kv_val = v_val * k_val; |
| 10999 | float prev_state_val = state_prev[h_2d_i_j_offset]; |
| 11000 | float temp_val = kv_val + prev_state_val * g_val; |
| 11001 | dst_data[t_h_j_offset] += temp_val * q_val; |
| 11002 | state_cur[h_2d_i_j_offset] = temp_val; |
| 11003 | } |
| 11004 | } |
| 11005 | } |
| 11006 | } |
| 11007 | |
| 11008 | #else |
| 11009 | for (int64_t t = 0; t < T; t++) { |
| 11010 | size_t t_offset = t * t_stride; |
| 11011 | size_t state_offset = head_size * C * (t / (T / n_seqs)); |
| 11012 | float * state_cur = state + state_offset; |
| 11013 | float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[4]->data + state_offset; |
| 11014 | |
| 11015 | for (int64_t h = h_start; h < h_end; h++) { |
| 11016 | size_t h_offset = h * h_stride; |
| 11017 | size_t t_h_offset = t_offset + h_offset; |
| 11018 | size_t h_2d_offset = h * h_stride_2d; |
| 11019 | |
| 11020 | for (int64_t i = 0; i < head_size; i++) { |
| 11021 | size_t t_h_i_offset = t_h_offset + i; |
| 11022 | size_t h_2d_i_offset = h_2d_offset + i * h_stride; |
| 11023 | |
| 11024 | float k_val = k[t_h_i_offset]; |
| 11025 | float q_val = q[t_h_i_offset] * scale; |
| 11026 | float g_val = g[t_h_i_offset]; |
| 11027 | |
| 11028 | for (int64_t j = 0; j < head_size; j++) { |
| 11029 | size_t t_h_j_offset = t_h_offset + j; |
| 11030 | size_t h_2d_i_j_offset = h_2d_i_offset + j; |
| 11031 | |
| 11032 | float v_val = v[t_h_j_offset]; |
| 11033 | float kv_val = v_val * k_val; |
| 11034 | float prev_state_val = state_prev[h_2d_i_j_offset]; |
| 11035 | float temp_val = prev_state_val * g_val + kv_val; |
| 11036 | dst_data[t_h_j_offset] += temp_val * q_val; |
| 11037 | state_cur[h_2d_i_j_offset] = temp_val; |
| 11038 | } |
| 11039 | } |
| 11040 | } |
| 11041 | } |
| 11042 | #endif |
| 11043 | } |
| 11044 | |
| 11045 | |
| 11046 | void ggml_compute_forward_gla( |
| 11047 | const ggml_compute_params * params, |
| 11048 | ggml_tensor * dst) { |
| 11049 | |
| 11050 | const ggml_tensor * src0 = dst->src[0]; |
| 11051 | |
| 11052 | switch (src0->type) { |
| 11053 | case GGML_TYPE_F32: |
| 11054 | { |
| 11055 | ggml_compute_forward_gla_f32(params, dst); |
| 11056 | } break; |
| 11057 | default: |
| 11058 | { |
| 11059 | GGML_ABORT("fatal error"); |
| 11060 | } |
| 11061 | } |
| 11062 | } |
| 11063 | |
| 11064 | static void ggml_compute_forward_solve_tri_f32(const struct ggml_compute_params * params, struct ggml_tensor * dst) { |
| 11065 | const struct ggml_tensor * src0 = dst->src[0]; // A (lower triangular) |
| 11066 | const struct ggml_tensor * src1 = dst->src[1]; // B (RHS) |
| 11067 | |
| 11068 | GGML_TENSOR_BINARY_OP_LOCALS; |
| 11069 | |
| 11070 | GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 11071 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 11072 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 11073 | |
| 11074 | GGML_ASSERT(ne00 == ne01); // A must be square |
| 11075 | GGML_ASSERT(ne0 == ne10); // solution cols == B cols |
| 11076 | GGML_ASSERT(ne1 == ne11); // solution rows == B rows |
| 11077 | |
| 11078 | GGML_ASSERT(ne02 == ne12 && ne12 == ne2); |
| 11079 | GGML_ASSERT(ne03 == ne13 && ne13 == ne3); |
| 11080 | |
| 11081 | const int ith = params->ith; |
| 11082 | const int nth = params->nth; |
| 11083 | |
| 11084 | const int64_t k = ne10; // number of RHS columns |
| 11085 | const int64_t n = ne11; // A is n×n |
| 11086 | const int64_t nr = ne02 * ne03 * k; // we're parallelizing on columns here, so seq x token x column will be the unit |
| 11087 | |
| 11088 | // chunks per thread |
| 11089 | const int64_t dr = (nr + nth - 1)/nth; |
| 11090 | |
| 11091 | // chunk range for this thread |
| 11092 | const int64_t ir0 = dr*ith; |
| 11093 | const int64_t ir1 = MIN(ir0 + dr, nr); |
| 11094 | |
| 11095 | const float * A = (const float *) src0->data; // [n, n, B1, B2] |
| 11096 | const float * B = (const float *) src1->data; // [n, k, B1, B2] |
| 11097 | float * X = ( float *) dst->data; // [n, k, B1, B2] |
| 11098 | |
| 11099 | for (int64_t ir = ir0; ir < ir1; ++ir) { |
| 11100 | const int64_t i03 = ir/(ne02*k); |
| 11101 | const int64_t i02 = (ir - i03*ne02*k)/k; |
| 11102 | const int64_t i01 = (ir - i03*ne02*k - i02*k); |
| 11103 | |
| 11104 | const float * A_batch = A + i02 * nb02 / sizeof(float) + i03 * nb03 / sizeof(float); |
| 11105 | const float * B_batch = B + i02 * nb12 / sizeof(float) + i03 * nb13 / sizeof(float); |
| 11106 | |
| 11107 | float * X_batch = X + i02 * nb2 / sizeof(float) + i03 * nb3 / sizeof(float); |
| 11108 | |
| 11109 | for (int64_t i00 = 0; i00 < n; ++i00) { |
| 11110 | float sum = 0.0f; |
| 11111 | for (int64_t t = 0; t < i00; ++t) { |
| 11112 | sum += A_batch[i00 * n + t] * X_batch[t * k + i01]; |
| 11113 | } |
| 11114 | |
| 11115 | const float diag = A_batch[i00 * n + i00]; |
| 11116 | assert(diag != 0.0f && "Zero diagonal in triangular matrix"); |
| 11117 | |
| 11118 | X_batch[i00 * k + i01] = (B_batch[i00 * k + i01] - sum) / diag; |
| 11119 | } |
| 11120 | } |
| 11121 | } |
| 11122 | |
| 11123 | void ggml_compute_forward_solve_tri(const struct ggml_compute_params * params, struct ggml_tensor * dst) { |
| 11124 | const ggml_tensor * src0 = dst->src[0]; |
| 11125 | const ggml_tensor * src1 = dst->src[1]; |
| 11126 | |
| 11127 | if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) { |
| 11128 | ggml_compute_forward_solve_tri_f32(params, dst); |
| 11129 | } else { |
| 11130 | GGML_ABORT("fatal error"); |
| 11131 | } |
| 11132 | } |
| 11133 | |
| 11134 | // ggml_compute_forward_gated_delta_net |
| 11135 | static void ggml_compute_forward_gated_delta_net_one_chunk( |
| 11136 | const ggml_compute_params * params, |
| 11137 | ggml_tensor * dst, |
| 11138 | int64_t ir0, |
| 11139 | int64_t ir1) { |
| 11140 | |
| 11141 | ggml_tensor * src_q = dst->src[0]; |
| 11142 | ggml_tensor * src_k = dst->src[1]; |
| 11143 | ggml_tensor * src_v = dst->src[2]; |
| 11144 | ggml_tensor * src_g = dst->src[3]; |
| 11145 | ggml_tensor * src_beta = dst->src[4]; |
| 11146 | ggml_tensor * src_state = dst->src[5]; |
| 11147 | |
| 11148 | const int64_t S_v = src_v->ne[0]; |
| 11149 | const int64_t H = src_v->ne[1]; |
| 11150 | const int64_t n_tokens = src_v->ne[2]; |
| 11151 | const int64_t n_seqs = src_v->ne[3]; |
| 11152 | |
| 11153 | GGML_ASSERT(ggml_is_contiguous_rows(src_q)); |
| 11154 | GGML_ASSERT(ggml_is_contiguous_rows(src_k)); |
| 11155 | GGML_ASSERT(ggml_is_contiguous_rows(src_v)); |
| 11156 | GGML_ASSERT(ggml_is_contiguous(src_g)); |
| 11157 | GGML_ASSERT(ggml_is_contiguous(src_beta)); |
| 11158 | GGML_ASSERT(ggml_is_contiguous(src_state)); |
| 11159 | |
| 11160 | GGML_ASSERT(src_g->ne[0] == 1 || src_g->ne[0] == S_v); |
| 11161 | GGML_ASSERT(src_beta->ne[0] == 1); |
| 11162 | |
| 11163 | GGML_TENSOR_LOCALS(int64_t, neq, src_q, ne); |
| 11164 | GGML_TENSOR_LOCALS(size_t, nbq, src_q, nb); |
| 11165 | GGML_TENSOR_LOCALS(int64_t, nek, src_k, ne); |
| 11166 | GGML_TENSOR_LOCALS(size_t, nbk, src_k, nb); |
| 11167 | GGML_TENSOR_LOCALS(int64_t, nev, src_v, ne); |
| 11168 | GGML_TENSOR_LOCALS(size_t, nbv, src_v, nb); |
| 11169 | GGML_TENSOR_LOCALS(int64_t, neg, src_g, ne); |
| 11170 | GGML_TENSOR_LOCALS(size_t, nbg, src_g, nb); |
| 11171 | GGML_TENSOR_LOCALS(size_t, nbb, src_beta, nb); |
| 11172 | |
| 11173 | const bool kda = (neg0 == S_v); |
| 11174 | |
| 11175 | // K (snapshot slot count) is an op param; state holds s0 only [S_v, S_v, H, n_seqs]. |
| 11176 | const int64_t K = ggml_get_op_params_i32(dst, 0); |
| 11177 | GGML_ASSERT(K >= 1); |
| 11178 | // per-seq stride in floats (seq s starts at state + s * seq_stride) |
| 11179 | const int64_t state_seq_stride = src_state->nb[3] / sizeof(float); |
| 11180 | |
| 11181 | const int64_t per_thread = S_v + (K > 1 ? S_v * S_v : 0); |
| 11182 | const int ith = params->ith; |
| 11183 | |
| 11184 | float * delta = (float *)params->wdata + ith * per_thread + CACHE_LINE_SIZE_F32; |
| 11185 | float * state_work = K > 1 ? (delta + S_v) : nullptr; |
| 11186 | |
| 11187 | // output layout: [attn_scores | new_states] |
| 11188 | // attn_scores: S_v * H * n_tokens * n_seqs floats |
| 11189 | // new_states: S_v * S_v * H * n_seqs * K floats (K snapshot slots; last min(n_tokens, K)) |
| 11190 | const int64_t attn_score_elems = S_v * H * n_tokens * n_seqs; |
| 11191 | const int64_t state_size_per_snap = S_v * S_v * H * n_seqs; |
| 11192 | float * attn_out_base = (float *)dst->data; |
| 11193 | float * state_out_base = (float *)dst->data + attn_score_elems; |
| 11194 | |
| 11195 | // snapshot slot mapping: slot 0 = most recent state, slot s = s tokens back. |
| 11196 | // When n_tokens < K only slots 0..n_tokens-1 are written; older slots are caller-owned. |
| 11197 | |
| 11198 | const float * state_in_base = (const float *)src_state->data; |
| 11199 | |
| 11200 | //const int64_t rq1 = nev1 / neq1; |
| 11201 | //const int64_t rk1 = nev1 / nek1; |
| 11202 | const int64_t rq3 = nev3 / neq3; |
| 11203 | const int64_t rk3 = nev3 / nek3; |
| 11204 | |
| 11205 | const float scale = 1.0f / sqrtf((float) S_v); |
| 11206 | |
| 11207 | for (int64_t ir = ir0; ir < ir1; ++ir) { |
| 11208 | const int64_t iv1 = ir % H; // head_index |
| 11209 | const int64_t iv3 = ir / H; // sequence |
| 11210 | |
| 11211 | const int64_t iq1 = iv1 % neq1; |
| 11212 | const int64_t ik1 = iv1 % nek1; |
| 11213 | |
| 11214 | const int64_t iq3 = iv3 / rq3; |
| 11215 | const int64_t ik3 = iv3 / rk3; |
| 11216 | |
| 11217 | // For K=1, write directly to the single output slot to avoid an extra memcpy at the end. |
| 11218 | // For K>1, work in scratch and copy out per-token when the slot is in range. |
| 11219 | float * s_out = (K > 1) |
| 11220 | ? state_work |
| 11221 | : state_out_base + (iv3 * H + iv1) * S_v * S_v; |
| 11222 | |
| 11223 | // copy input state into the working buffer and operate in-place |
| 11224 | // state layout [S_v, S_v, H, n_seqs]: seq iv3 starts at iv3 * state_seq_stride. |
| 11225 | const float * s_in = state_in_base + iv3 * state_seq_stride + iv1 * S_v * S_v; |
| 11226 | memcpy(s_out, s_in, S_v * S_v * sizeof(float)); |
| 11227 | |
| 11228 | // attn output pointer for first token of this (head, seq) |
| 11229 | float * attn_data = attn_out_base + (iv3 * n_tokens * H + iv1) * S_v; |
| 11230 | |
| 11231 | for (int64_t t = 0; t < n_tokens; t++) { |
| 11232 | const float * q_d = (const float *)((const char *)src_q->data + iq3 * nbq3 + t * nbq2 + iq1 * nbq1); |
| 11233 | const float * k_d = (const float *)((const char *)src_k->data + ik3 * nbk3 + t * nbk2 + ik1 * nbk1); |
| 11234 | const float * v_d = (const float *)((const char *)src_v->data + iv3 * nbv3 + t * nbv2 + iv1 * nbv1); |
| 11235 | |
| 11236 | const float beta_val = *(const float *)((const char *)src_beta->data + iv3 * nbb3 + t * nbb2 + iv1 * nbb1); |
| 11237 | const float * g_d = (const float *)((const char *)src_g->data + iv3 * nbg3 + t * nbg2 + iv1 * nbg1); |
| 11238 | |
| 11239 | // state is stored transposed: s_out[j*S_v + i] = S[i][j] |
| 11240 | // so row j of s_out = column j of S (contiguous access) |
| 11241 | |
| 11242 | if (kda) { |
| 11243 | // precompute exp(g) into delta scratch (reused below) |
| 11244 | for (int64_t i = 0; i < S_v; ++i) { |
| 11245 | delta[i] = expf(g_d[i]); |
| 11246 | } |
| 11247 | // S[i][:] *= exp(g[i]) => for each row j of M: M[j][i] *= exp(g[i]) |
| 11248 | for (int64_t j = 0; j < S_v; ++j) { |
| 11249 | ggml_vec_mul_f32(S_v, &s_out[j * S_v], &s_out[j * S_v], delta); |
| 11250 | } |
| 11251 | } else { |
| 11252 | ggml_vec_scale_f32(S_v * S_v, s_out, expf(g_d[0])); |
| 11253 | } |
| 11254 | |
| 11255 | // delta[j] = sum_i S[i][j] * k[i] = dot(row j of M, k) |
| 11256 | for (int64_t j = 0; j < S_v; ++j) { |
| 11257 | float sum = 0.0f; |
| 11258 | ggml_vec_dot_f32(S_v, &sum, 0, &s_out[j * S_v], 0, k_d, 0, 1); |
| 11259 | delta[j] = (v_d[j] - sum) * beta_val; |
| 11260 | } |
| 11261 | |
| 11262 | // outer product: S[i][j] += k[i] * delta[j] => M[j][i] += delta[j] * k[i] |
| 11263 | for (int64_t j = 0; j < S_v; ++j) { |
| 11264 | ggml_vec_mad_f32(S_v, &s_out[j * S_v], k_d, delta[j]); |
| 11265 | } |
| 11266 | |
| 11267 | // attn_out[j] = sum_i S[i][j] * q[i] = dot(row j of M, q) |
| 11268 | for (int64_t j = 0; j < S_v; ++j) { |
| 11269 | float sum = 0.0f; |
| 11270 | ggml_vec_dot_f32(S_v, &sum, 0, &s_out[j * S_v], 0, q_d, 0, 1); |
| 11271 | attn_data[j] = sum * scale; |
| 11272 | } |
| 11273 | |
| 11274 | attn_data += S_v * H; // advance to next token |
| 11275 | |
| 11276 | if (K > 1) { |
| 11277 | const int64_t target_slot = n_tokens - 1 - t; |
| 11278 | if (target_slot >= 0 && target_slot < K) { |
| 11279 | float * curr_state_o = state_out_base + target_slot * state_size_per_snap + |
| 11280 | (iv3 * H + iv1) * S_v * S_v; |
| 11281 | memcpy(curr_state_o, s_out, S_v * S_v * sizeof(float)); |
| 11282 | } |
| 11283 | } |
| 11284 | } |
| 11285 | } |
| 11286 | } |
| 11287 | |
| 11288 | |
| 11289 | static void ggml_compute_forward_gated_delta_net_f32( |
| 11290 | const ggml_compute_params * params, |
| 11291 | ggml_tensor * dst) { |
| 11292 | |
| 11293 | ggml_tensor * V = dst->src[2]; |
| 11294 | int64_t nr = V->ne[1] * V->ne[3]; |
| 11295 | |
| 11296 | // disable for NUMA |
| 11297 | const bool disable_chunking = ggml_is_numa(); |
| 11298 | |
| 11299 | int nth = params->nth; |
| 11300 | int ith = params->ith; |
| 11301 | |
| 11302 | // 4x chunks per thread |
| 11303 | int nth_scaled = nth * 4; |
| 11304 | int64_t chunk_size = (nr + nth_scaled - 1) / nth_scaled; |
| 11305 | int64_t nchunk = (nr + chunk_size - 1) / chunk_size; |
| 11306 | |
| 11307 | if (nth == 1 || nchunk < nth || disable_chunking) { |
| 11308 | nchunk = nth; |
| 11309 | } |
| 11310 | |
| 11311 | if (ith == 0) { |
| 11312 | ggml_threadpool_chunk_set(params->threadpool, nth); |
| 11313 | } |
| 11314 | |
| 11315 | ggml_barrier(params->threadpool); |
| 11316 | |
| 11317 | const int64_t dr = (nr + nchunk - 1) / nchunk; |
| 11318 | |
| 11319 | int current_chunk = ith; |
| 11320 | |
| 11321 | while (current_chunk < nchunk) { |
| 11322 | const int64_t ir0 = dr * current_chunk; |
| 11323 | const int64_t ir1 = MIN(ir0 + dr, nr); |
| 11324 | |
| 11325 | ggml_compute_forward_gated_delta_net_one_chunk(params, dst, ir0, ir1); |
| 11326 | current_chunk = ggml_threadpool_chunk_add(params->threadpool, 1); |
| 11327 | } |
| 11328 | } |
| 11329 | |
| 11330 | void ggml_compute_forward_gated_delta_net( |
| 11331 | const ggml_compute_params * params, |
| 11332 | ggml_tensor * dst) { |
| 11333 | const ggml_tensor * src0 = dst->src[0]; |
| 11334 | |
| 11335 | switch (src0->type) { |
| 11336 | case GGML_TYPE_F32: |
| 11337 | { |
| 11338 | ggml_compute_forward_gated_delta_net_f32(params, dst); |
| 11339 | } break; |
| 11340 | default: |
| 11341 | { |
| 11342 | GGML_ABORT("fatal error"); |
| 11343 | } |
| 11344 | } |
| 11345 | } |
| 11346 | |
| 11347 | |
| 11348 | // ggml_compute_forward_dsv4_hc_comb |
| 11349 | |
| 11350 | static void ggml_dsv4_hc_comb_norm_cols(float * comb, float eps) { |
| 11351 | constexpr int64_t hc = 4; |
| 11352 | |
| 11353 | for (int64_t idst = 0; idst < hc; ++idst) { |
| 11354 | float sum = eps; |
| 11355 | for (int64_t isrc = 0; isrc < hc; ++isrc) { |
| 11356 | sum += comb[idst + hc*isrc]; |
| 11357 | } |
| 11358 | |
| 11359 | const float inv_sum = 1.0f / sum; |
| 11360 | for (int64_t isrc = 0; isrc < hc; ++isrc) { |
| 11361 | comb[idst + hc*isrc] *= inv_sum; |
| 11362 | } |
| 11363 | } |
| 11364 | } |
| 11365 | |
| 11366 | static void ggml_dsv4_hc_comb_norm_rows(float * comb, float eps) { |
| 11367 | constexpr int64_t hc = 4; |
| 11368 | |
| 11369 | for (int64_t isrc = 0; isrc < hc; ++isrc) { |
| 11370 | float sum = eps; |
| 11371 | for (int64_t idst = 0; idst < hc; ++idst) { |
| 11372 | sum += comb[idst + hc*isrc]; |
| 11373 | } |
| 11374 | |
| 11375 | const float inv_sum = 1.0f / sum; |
| 11376 | for (int64_t idst = 0; idst < hc; ++idst) { |
| 11377 | comb[idst + hc*isrc] *= inv_sum; |
| 11378 | } |
| 11379 | } |
| 11380 | } |
| 11381 | |
| 11382 | static void ggml_compute_forward_dsv4_hc_comb_f32( |
| 11383 | const ggml_compute_params * params, |
| 11384 | ggml_tensor * dst) { |
| 11385 | const ggml_tensor * mixes = dst->src[0]; |
| 11386 | const ggml_tensor * scale = dst->src[1]; |
| 11387 | const ggml_tensor * base = dst->src[2]; |
| 11388 | |
| 11389 | GGML_ASSERT(mixes->type == GGML_TYPE_F32); |
| 11390 | GGML_ASSERT(scale->type == GGML_TYPE_F32); |
| 11391 | GGML_ASSERT(base->type == GGML_TYPE_F32); |
| 11392 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 11393 | |
| 11394 | constexpr int64_t hc = 4; |
| 11395 | constexpr int64_t comb_offset = 2*hc; |
| 11396 | constexpr int64_t hc_mix_dim = (2 + hc)*hc; |
| 11397 | |
| 11398 | const int64_t n_tokens = mixes->ne[1]; |
| 11399 | |
| 11400 | GGML_ASSERT(mixes->ne[0] == hc_mix_dim); |
| 11401 | GGML_ASSERT(dst->ne[0] == hc); |
| 11402 | GGML_ASSERT(dst->ne[1] == hc); |
| 11403 | GGML_ASSERT(dst->ne[2] == n_tokens); |
| 11404 | GGML_ASSERT(scale->ne[0] >= 3); |
| 11405 | GGML_ASSERT(base->ne[0] == hc_mix_dim); |
| 11406 | |
| 11407 | GGML_TENSOR_LOCALS(size_t, nbm, mixes, nb); |
| 11408 | GGML_TENSOR_LOCALS(size_t, nbs, scale, nb); |
| 11409 | GGML_TENSOR_LOCALS(size_t, nbb, base, nb); |
| 11410 | GGML_TENSOR_LOCALS(size_t, nbd, dst, nb); |
| 11411 | |
| 11412 | const float eps = ggml_get_op_params_f32(dst, 0); |
| 11413 | const int32_t n_iter = ggml_get_op_params_i32(dst, 1); |
| 11414 | GGML_ASSERT(n_iter > 0); |
| 11415 | |
| 11416 | const int ith = params->ith; |
| 11417 | const int nth = params->nth; |
| 11418 | |
| 11419 | const int64_t dr = (n_tokens + nth - 1) / nth; |
| 11420 | const int64_t it0 = dr * ith; |
| 11421 | const int64_t it1 = MIN(it0 + dr, n_tokens); |
| 11422 | |
| 11423 | const float scale_comb = *(const float *) ((const char *) scale->data + 2*nbs0); |
| 11424 | |
| 11425 | for (int64_t it = it0; it < it1; ++it) { |
| 11426 | float comb[hc*hc]; |
| 11427 | |
| 11428 | for (int64_t isrc = 0; isrc < hc; ++isrc) { |
| 11429 | float max = -INFINITY; |
| 11430 | for (int64_t idst = 0; idst < hc; ++idst) { |
| 11431 | const int64_t idx = idst + hc*isrc; |
| 11432 | const float xv = *(const float *) ((const char *) mixes->data + (comb_offset + idx)*nbm0 + it*nbm1); |
| 11433 | const float bv = *(const float *) ((const char *) base->data + (comb_offset + idx)*nbb0); |
| 11434 | const float v = xv * scale_comb + bv; |
| 11435 | comb[idx] = v; |
| 11436 | max = MAX(max, v); |
| 11437 | } |
| 11438 | |
| 11439 | float sum = 0.0f; |
| 11440 | for (int64_t idst = 0; idst < hc; ++idst) { |
| 11441 | const int64_t idx = idst + hc*isrc; |
| 11442 | const float v = expf(comb[idx] - max); |
| 11443 | comb[idx] = v; |
| 11444 | sum += v; |
| 11445 | } |
| 11446 | |
| 11447 | const float inv_sum = 1.0f / sum; |
| 11448 | for (int64_t idst = 0; idst < hc; ++idst) { |
| 11449 | const int64_t idx = idst + hc*isrc; |
| 11450 | comb[idx] = comb[idx] * inv_sum + eps; |
| 11451 | } |
| 11452 | } |
| 11453 | |
| 11454 | ggml_dsv4_hc_comb_norm_cols(comb, eps); |
| 11455 | for (int32_t i = 1; i < n_iter; ++i) { |
| 11456 | ggml_dsv4_hc_comb_norm_rows(comb, eps); |
| 11457 | ggml_dsv4_hc_comb_norm_cols(comb, eps); |
| 11458 | } |
| 11459 | |
| 11460 | for (int64_t isrc = 0; isrc < hc; ++isrc) { |
| 11461 | for (int64_t idst = 0; idst < hc; ++idst) { |
| 11462 | const int64_t idx = idst + hc*isrc; |
| 11463 | *(float *) ((char *) dst->data + idst*nbd0 + isrc*nbd1 + it*nbd2) = comb[idx]; |
| 11464 | } |
| 11465 | } |
| 11466 | } |
| 11467 | } |
| 11468 | |
| 11469 | void ggml_compute_forward_dsv4_hc_comb( |
| 11470 | const ggml_compute_params * params, |
| 11471 | ggml_tensor * dst) { |
| 11472 | const ggml_tensor * src0 = dst->src[0]; |
| 11473 | |
| 11474 | switch (src0->type) { |
| 11475 | case GGML_TYPE_F32: |
| 11476 | { |
| 11477 | ggml_compute_forward_dsv4_hc_comb_f32(params, dst); |
| 11478 | } break; |
| 11479 | default: |
| 11480 | { |
| 11481 | GGML_ABORT("fatal error"); |
| 11482 | } |
| 11483 | } |
| 11484 | } |
| 11485 | |
| 11486 | // ggml_compute_forward_dsv4_hc_pre |
| 11487 | |
| 11488 | static void ggml_compute_forward_dsv4_hc_pre_f32( |
| 11489 | const ggml_compute_params * params, |
| 11490 | ggml_tensor * dst) { |
| 11491 | const ggml_tensor * x = dst->src[0]; |
| 11492 | const ggml_tensor * weights = dst->src[1]; |
| 11493 | |
| 11494 | GGML_ASSERT(x->type == GGML_TYPE_F32); |
| 11495 | GGML_ASSERT(weights->type == GGML_TYPE_F32); |
| 11496 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 11497 | |
| 11498 | const int64_t n_embd = x->ne[0]; |
| 11499 | const int64_t hc = x->ne[1]; |
| 11500 | const int64_t n_tokens = x->ne[2]; |
| 11501 | |
| 11502 | const float scale = ggml_get_op_params_f32(dst, 0); |
| 11503 | const bool gated = ggml_get_op_params_i32(dst, 1) != 0; |
| 11504 | |
| 11505 | GGML_ASSERT(dst->ne[0] == n_embd); |
| 11506 | GGML_ASSERT(dst->ne[1] == n_tokens); |
| 11507 | if (gated) { |
| 11508 | GGML_ASSERT(weights->ne[0] == n_embd); |
| 11509 | GGML_ASSERT(weights->ne[1] == hc); |
| 11510 | GGML_ASSERT(weights->ne[2] == n_tokens); |
| 11511 | } else { |
| 11512 | GGML_ASSERT(weights->ne[0] == hc); |
| 11513 | GGML_ASSERT(weights->ne[1] == n_tokens); |
| 11514 | } |
| 11515 | |
| 11516 | GGML_TENSOR_LOCALS(size_t, nbx, x, nb); |
| 11517 | GGML_TENSOR_LOCALS(size_t, nbw, weights, nb); |
| 11518 | GGML_TENSOR_LOCALS(size_t, nbd, dst, nb); |
| 11519 | |
| 11520 | const int ith = params->ith; |
| 11521 | const int nth = params->nth; |
| 11522 | |
| 11523 | const int64_t nr = n_embd * n_tokens; |
| 11524 | const int64_t dr = (nr + nth - 1) / nth; |
| 11525 | const int64_t ir0 = dr * ith; |
| 11526 | const int64_t ir1 = MIN(ir0 + dr, nr); |
| 11527 | |
| 11528 | for (int64_t ir = ir0; ir < ir1; ++ir) { |
| 11529 | const int64_t i0 = ir % n_embd; |
| 11530 | const int64_t it = ir / n_embd; |
| 11531 | |
| 11532 | float sum = 0.0f; |
| 11533 | for (int64_t ih = 0; ih < hc; ++ih) { |
| 11534 | const float xv = *(const float *) ((const char *) x->data + i0*nbx0 + ih*nbx1 + it*nbx2); |
| 11535 | float wv; |
| 11536 | if (gated) { |
| 11537 | const float gv = *(const float *) ((const char *) weights->data + i0*nbw0 + ih*nbw1 + it*nbw2); |
| 11538 | wv = 1.0f / (1.0f + expf(-gv)); |
| 11539 | } else { |
| 11540 | wv = *(const float *) ((const char *) weights->data + ih*nbw0 + it*nbw1); |
| 11541 | } |
| 11542 | sum += xv * wv; |
| 11543 | } |
| 11544 | |
| 11545 | *(float *) ((char *) dst->data + i0*nbd0 + it*nbd1) = scale * sum; |
| 11546 | } |
| 11547 | } |
| 11548 | |
| 11549 | void ggml_compute_forward_dsv4_hc_pre( |
| 11550 | const ggml_compute_params * params, |
| 11551 | ggml_tensor * dst) { |
| 11552 | const ggml_tensor * src0 = dst->src[0]; |
| 11553 | |
| 11554 | switch (src0->type) { |
| 11555 | case GGML_TYPE_F32: |
| 11556 | { |
| 11557 | ggml_compute_forward_dsv4_hc_pre_f32(params, dst); |
| 11558 | } break; |
| 11559 | default: |
| 11560 | { |
| 11561 | GGML_ABORT("fatal error"); |
| 11562 | } |
| 11563 | } |
| 11564 | } |
| 11565 | |
| 11566 | // ggml_compute_forward_dsv4_hc_post |
| 11567 | |
| 11568 | static void ggml_compute_forward_dsv4_hc_post_f32( |
| 11569 | const ggml_compute_params * params, |
| 11570 | ggml_tensor * dst) { |
| 11571 | const ggml_tensor * x = dst->src[0]; |
| 11572 | const ggml_tensor * residual = dst->src[1]; |
| 11573 | const ggml_tensor * post = dst->src[2]; |
| 11574 | const ggml_tensor * comb = dst->src[3]; |
| 11575 | |
| 11576 | GGML_ASSERT(x->type == GGML_TYPE_F32); |
| 11577 | GGML_ASSERT(residual->type == GGML_TYPE_F32); |
| 11578 | GGML_ASSERT(post->type == GGML_TYPE_F32); |
| 11579 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 11580 | |
| 11581 | const int64_t n_embd = x->ne[0]; |
| 11582 | const int64_t n_tokens = x->ne[1]; |
| 11583 | const int64_t hc = residual->ne[1]; |
| 11584 | |
| 11585 | GGML_ASSERT(dst->ne[0] == n_embd); |
| 11586 | GGML_ASSERT(dst->ne[1] == hc); |
| 11587 | GGML_ASSERT(dst->ne[2] == n_tokens); |
| 11588 | GGML_ASSERT(residual->ne[0] == n_embd); |
| 11589 | GGML_ASSERT(residual->ne[2] == n_tokens); |
| 11590 | GGML_ASSERT(post->ne[0] == hc); |
| 11591 | GGML_ASSERT(post->ne[1] == n_tokens); |
| 11592 | |
| 11593 | // comb == NULL: identity mixing, each stream keeps its own residual |
| 11594 | size_t nbc0 = 0; |
| 11595 | size_t nbc1 = 0; |
| 11596 | size_t nbc2 = 0; |
| 11597 | if (comb) { |
| 11598 | GGML_ASSERT(comb->type == GGML_TYPE_F32); |
| 11599 | GGML_ASSERT(comb->ne[0] == hc); |
| 11600 | GGML_ASSERT(comb->ne[1] == hc); |
| 11601 | GGML_ASSERT(comb->ne[2] == n_tokens); |
| 11602 | nbc0 = comb->nb[0]; |
| 11603 | nbc1 = comb->nb[1]; |
| 11604 | nbc2 = comb->nb[2]; |
| 11605 | } |
| 11606 | |
| 11607 | GGML_TENSOR_LOCALS(size_t, nbx, x, nb); |
| 11608 | GGML_TENSOR_LOCALS(size_t, nbr, residual, nb); |
| 11609 | GGML_TENSOR_LOCALS(size_t, nbp, post, nb); |
| 11610 | GGML_TENSOR_LOCALS(size_t, nbd, dst, nb); |
| 11611 | |
| 11612 | const int ith = params->ith; |
| 11613 | const int nth = params->nth; |
| 11614 | |
| 11615 | const int64_t nr = n_embd * hc * n_tokens; |
| 11616 | const int64_t dr = (nr + nth - 1) / nth; |
| 11617 | const int64_t ir0 = dr * ith; |
| 11618 | const int64_t ir1 = MIN(ir0 + dr, nr); |
| 11619 | |
| 11620 | for (int64_t ir = ir0; ir < ir1; ++ir) { |
| 11621 | const int64_t i0 = ir % n_embd; |
| 11622 | const int64_t idst = (ir / n_embd) % hc; |
| 11623 | const int64_t it = ir / (n_embd * hc); |
| 11624 | |
| 11625 | const float xv = *(const float *) ((const char *) x->data + i0*nbx0 + it*nbx1); |
| 11626 | const float pv = *(const float *) ((const char *) post->data + idst*nbp0 + it*nbp1); |
| 11627 | |
| 11628 | float sum = xv * pv; |
| 11629 | if (comb) { |
| 11630 | for (int64_t isrc = 0; isrc < hc; ++isrc) { |
| 11631 | const float rv = *(const float *) ((const char *) residual->data + i0*nbr0 + isrc*nbr1 + it*nbr2); |
| 11632 | const float cv = *(const float *) ((const char *) comb->data + idst*nbc0 + isrc*nbc1 + it*nbc2); |
| 11633 | sum += rv * cv; |
| 11634 | } |
| 11635 | } else { |
| 11636 | sum += *(const float *) ((const char *) residual->data + i0*nbr0 + idst*nbr1 + it*nbr2); |
| 11637 | } |
| 11638 | |
| 11639 | *(float *) ((char *) dst->data + i0*nbd0 + idst*nbd1 + it*nbd2) = sum; |
| 11640 | } |
| 11641 | } |
| 11642 | |
| 11643 | void ggml_compute_forward_dsv4_hc_post( |
| 11644 | const ggml_compute_params * params, |
| 11645 | ggml_tensor * dst) { |
| 11646 | const ggml_tensor * src0 = dst->src[0]; |
| 11647 | |
| 11648 | switch (src0->type) { |
| 11649 | case GGML_TYPE_F32: |
| 11650 | { |
| 11651 | ggml_compute_forward_dsv4_hc_post_f32(params, dst); |
| 11652 | } break; |
| 11653 | default: |
| 11654 | { |
| 11655 | GGML_ABORT("fatal error"); |
| 11656 | } |
| 11657 | } |
| 11658 | } |
| 11659 | |
| 11660 | // ggml_compute_forward_top_k_mask |
| 11661 | |
| 11662 | void ggml_compute_forward_top_k_mask( |
| 11663 | const ggml_compute_params * params, |
| 11664 | ggml_tensor * dst) { |
| 11665 | const ggml_tensor * mask = dst->src[0]; |
| 11666 | const ggml_tensor * top_k = dst->src[1]; |
| 11667 | const ggml_tensor * score = dst->src[2]; |
| 11668 | const ggml_tensor * blocks = dst->src[3]; |
| 11669 | const ggml_tensor * extra = dst->src[4]; |
| 11670 | |
| 11671 | GGML_ASSERT(dst->type == mask->type); |
| 11672 | GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type)); |
| 11673 | |
| 11674 | const int64_t n_kv = mask->ne[0]; |
| 11675 | const int64_t ne1 = dst->ne[1]; |
| 11676 | const int64_t ne2 = dst->ne[2]; |
| 11677 | const int64_t n_rows = ggml_nrows(dst); |
| 11678 | const int64_t n_top = top_k->ne[0]; |
| 11679 | const int64_t n_blk = blocks ? blocks->ne[0] : 1; |
| 11680 | const int64_t n_pick = blocks ? blocks->ne[1] : score ? score->ne[0] : n_kv; |
| 11681 | const int64_t n_extra = extra ? extra->ne[0] : 0; |
| 11682 | |
| 11683 | const size_t ts = ggml_type_size(dst->type); |
| 11684 | |
| 11685 | const int ith = params->ith; |
| 11686 | const int nth = params->nth; |
| 11687 | |
| 11688 | for (int64_t ir = ith; ir < n_rows; ir += nth) { |
| 11689 | const int64_t i1 = ir % ne1; |
| 11690 | const int64_t i2 = (ir / ne1) % ne2; |
| 11691 | const int64_t i3 = ir / (ne1*ne2); |
| 11692 | |
| 11693 | const char * mask_row = (const char *) mask->data |
| 11694 | + (i1 % mask->ne[1])*mask->nb[1] + (i2 % mask->ne[2])*mask->nb[2] + (i3 % mask->ne[3])*mask->nb[3]; |
| 11695 | char * dst_row = (char *) dst->data + i1*dst->nb[1] + i2*dst->nb[2] + i3*dst->nb[3]; |
| 11696 | |
| 11697 | if (dst->type == GGML_TYPE_F16) { |
| 11698 | const ggml_fp16_t ninf = GGML_CPU_FP32_TO_FP16(-INFINITY); |
| 11699 | for (int64_t c = 0; c < n_kv; ++c) { |
| 11700 | ((ggml_fp16_t *) dst_row)[c] = ninf; |
| 11701 | } |
| 11702 | } else { |
| 11703 | for (int64_t c = 0; c < n_kv; ++c) { |
| 11704 | ((float *) dst_row)[c] = -INFINITY; |
| 11705 | } |
| 11706 | } |
| 11707 | |
| 11708 | auto keep = [&](int32_t c) { |
| 11709 | if (c >= 0 && c < n_kv) { |
| 11710 | memcpy(dst_row + c*ts, mask_row + c*ts, ts); |
| 11711 | } |
| 11712 | }; |
| 11713 | |
| 11714 | const char * top_k_row = (const char *) top_k->data + i1*top_k->nb[1] + i2*top_k->nb[2] + i3*top_k->nb[3]; |
| 11715 | |
| 11716 | for (int64_t j = 0; j < n_top; ++j) { |
| 11717 | const int32_t p = *(const int32_t *) (top_k_row + j*top_k->nb[0]); |
| 11718 | if (p < 0 || p >= n_pick) { |
| 11719 | continue; |
| 11720 | } |
| 11721 | if (score) { |
| 11722 | const float s = *(const float *) ((const char *) score->data |
| 11723 | + p*score->nb[0] + i1*score->nb[1] + i2*score->nb[2] + i3*score->nb[3]); |
| 11724 | if (!(s > -INFINITY)) { |
| 11725 | continue; |
| 11726 | } |
| 11727 | } |
| 11728 | if (!blocks) { |
| 11729 | keep(p); |
| 11730 | continue; |
| 11731 | } |
| 11732 | for (int64_t k = 0; k < n_blk; ++k) { |
| 11733 | keep(*(const int32_t *) ((const char *) blocks->data + k*blocks->nb[0] + p*blocks->nb[1])); |
| 11734 | } |
| 11735 | } |
| 11736 | |
| 11737 | if (extra) { |
| 11738 | const char * extra_row = (const char *) extra->data + i1*extra->nb[1] + i2*extra->nb[2] + i3*extra->nb[3]; |
| 11739 | for (int64_t j = 0; j < n_extra; ++j) { |
| 11740 | keep(*(const int32_t *) (extra_row + j*extra->nb[0])); |
| 11741 | } |
| 11742 | } |
| 11743 | } |
| 11744 | } |
| 11745 | |
| 11746 | // ggml_compute_forward_rwkv_wkv7 |
| 11747 | |
| 11748 | static void ggml_compute_forward_rwkv_wkv7_f32( |
| 11749 | const ggml_compute_params * params, |
| 11750 | ggml_tensor * dst) { |
| 11751 | const int64_t T = dst->src[1]->ne[2]; |
| 11752 | const int64_t C = dst->ne[0]; |
| 11753 | const int64_t HEADS = dst->src[1]->ne[1]; |
| 11754 | const int64_t n_seqs = dst->src[6]->ne[1]; |
| 11755 | const int64_t head_size = C / HEADS; |
| 11756 | |
| 11757 | float * dst_data = (float *) dst->data; |
| 11758 | float * state = ((float *) dst->data) + C * T; |
| 11759 | |
| 11760 | const int ith = params->ith; |
| 11761 | const int nth = params->nth; |
| 11762 | |
| 11763 | const int h_start = (HEADS * (ith )) / nth; |
| 11764 | const int h_end = ((HEADS * (ith + 1)) / nth < HEADS) ? |
| 11765 | (HEADS * (ith + 1)) / nth : HEADS; |
| 11766 | |
| 11767 | float * r = (float *) dst->src[0]->data; |
| 11768 | float * w = (float *) dst->src[1]->data; |
| 11769 | float * k = (float *) dst->src[2]->data; |
| 11770 | float * v = (float *) dst->src[3]->data; |
| 11771 | float * a = (float *) dst->src[4]->data; |
| 11772 | float * b = (float *) dst->src[5]->data; |
| 11773 | |
| 11774 | int64_t t_stride = HEADS * head_size; // Same to C |
| 11775 | |
| 11776 | int64_t h_stride = C / HEADS; |
| 11777 | GGML_ASSERT(C % HEADS == 0); // C must be divisible by HEADS |
| 11778 | int64_t h_stride_2d = head_size * head_size; |
| 11779 | |
| 11780 | #if defined(GGML_SIMD) |
| 11781 | #if defined(__ARM_FEATURE_SVE) || defined(__riscv_v) |
| 11782 | // scalar Route to scalar implementation //TODO: Write SVE code and RVV code |
| 11783 | for (int64_t t = 0; t < T; t++) { |
| 11784 | int64_t t_offset = t * t_stride; |
| 11785 | int64_t state_offset = head_size * C * (t / (T / n_seqs)); |
| 11786 | float * state_cur = state + state_offset; |
| 11787 | float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[6]->data + state_offset; |
| 11788 | |
| 11789 | for (int64_t h = h_start; h < h_end; h++) { |
| 11790 | int64_t h_offset = h * h_stride; |
| 11791 | int64_t t_h_offset = t_offset + h_offset; |
| 11792 | int64_t h_2d_offset = h * h_stride_2d; |
| 11793 | |
| 11794 | for (int64_t i = 0; i < head_size; i++) { |
| 11795 | int64_t t_h_i_offset = t_h_offset + i; |
| 11796 | int64_t h_2d_i_offset = h_2d_offset + i * h_stride; |
| 11797 | |
| 11798 | float v_val = v[t_h_i_offset]; |
| 11799 | |
| 11800 | float sa = 0, result = 0; |
| 11801 | for (int64_t j = 0; j < head_size; j++) { |
| 11802 | sa += a[t_h_offset + j] * state_prev[h_2d_i_offset + j]; |
| 11803 | } |
| 11804 | |
| 11805 | for (int64_t j = 0; j < head_size; j++) { |
| 11806 | int64_t t_h_j_offset = t_h_offset + j; |
| 11807 | int64_t h_2d_i_j_offset = h_2d_i_offset + j; |
| 11808 | |
| 11809 | float r_val = r[t_h_j_offset]; |
| 11810 | float w_val = w[t_h_j_offset]; |
| 11811 | float k_val = k[t_h_j_offset]; |
| 11812 | float b_val = b[t_h_j_offset]; |
| 11813 | float kv_val = v_val * k_val; |
| 11814 | float prev_state_val = state_prev[h_2d_i_j_offset]; |
| 11815 | state_cur[h_2d_i_j_offset] = prev_state_val * w_val + kv_val + sa * b_val; |
| 11816 | result += state_cur[h_2d_i_j_offset] * r_val; |
| 11817 | } |
| 11818 | dst_data[t_h_i_offset] = result; |
| 11819 | } |
| 11820 | } |
| 11821 | } |
| 11822 | #else |
| 11823 | for (int64_t t = 0; t < T; t++) { |
| 11824 | int64_t t_offset = t * t_stride; |
| 11825 | int64_t state_offset = head_size * C * (t / (T / n_seqs)); |
| 11826 | float * state_cur = state + state_offset; |
| 11827 | float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[6]->data + state_offset; |
| 11828 | |
| 11829 | for (int64_t h = h_start; h < h_end; h++) { |
| 11830 | int64_t h_offset = h * h_stride; |
| 11831 | int64_t t_h_offset = t_offset + h_offset; |
| 11832 | int64_t h_2d_offset = h * h_stride_2d; |
| 11833 | |
| 11834 | for (int64_t ii = 0; ii < head_size; ii++) { |
| 11835 | int64_t t_h_i_offset = t_h_offset + ii; |
| 11836 | int64_t h_2d_i_offset = h_2d_offset + ii * h_stride; |
| 11837 | |
| 11838 | GGML_F32_VEC v_vec = GGML_F32_VEC_SET1(v[t_h_i_offset]); |
| 11839 | |
| 11840 | float sa = 0; |
| 11841 | { |
| 11842 | GGML_F32_VEC sum[GGML_F32_ARR] = { GGML_F32_VEC_ZERO }; |
| 11843 | GGML_F32_VEC ax[GGML_F32_ARR]; |
| 11844 | GGML_F32_VEC ay[GGML_F32_ARR]; |
| 11845 | for (int64_t j = 0; j < head_size; j += GGML_F32_STEP) { |
| 11846 | for (int64_t kk = 0; kk < GGML_F32_ARR; kk++) { |
| 11847 | ax[kk] = GGML_F32_VEC_LOAD(&a[t_h_offset + j + kk * GGML_F32_EPR]); |
| 11848 | ay[kk] = GGML_F32_VEC_LOAD(&state_prev[h_2d_i_offset + j + kk * GGML_F32_EPR]); |
| 11849 | sum[kk] = GGML_F32_VEC_FMA(sum[kk], ax[kk], ay[kk]); |
| 11850 | } |
| 11851 | } |
| 11852 | GGML_F32_VEC_REDUCE(sa, sum); |
| 11853 | } |
| 11854 | |
| 11855 | GGML_F32_VEC sa_vec = GGML_F32_VEC_SET1(sa); |
| 11856 | |
| 11857 | int64_t j = 0; |
| 11858 | GGML_F32_VEC result_vec[GGML_F32_ARR] = { GGML_F32_VEC_ZERO }; |
| 11859 | for (; j < head_size; j += GGML_F32_STEP) { |
| 11860 | for (int64_t kk = 0; kk < GGML_F32_ARR; kk++) { |
| 11861 | int64_t t_h_j_offset = t_h_offset + j + kk * GGML_F32_EPR; |
| 11862 | int64_t h_2d_i_j_offset = h_2d_i_offset + j + kk * GGML_F32_EPR; |
| 11863 | |
| 11864 | GGML_F32_VEC r_vec = GGML_F32_VEC_LOAD(&r[t_h_j_offset]); |
| 11865 | GGML_F32_VEC w_vec = GGML_F32_VEC_LOAD(&w[t_h_j_offset]); |
| 11866 | GGML_F32_VEC k_vec = GGML_F32_VEC_LOAD(&k[t_h_j_offset]); |
| 11867 | GGML_F32_VEC b_vec = GGML_F32_VEC_LOAD(&b[t_h_j_offset]); |
| 11868 | |
| 11869 | k_vec = GGML_F32_VEC_MUL(v_vec, k_vec); |
| 11870 | |
| 11871 | GGML_F32_VEC state_vec = GGML_F32_VEC_LOAD(&state_prev[h_2d_i_j_offset]); |
| 11872 | // kv + s * decay + sa * b |
| 11873 | state_vec = GGML_F32_VEC_FMA(k_vec, state_vec, w_vec); |
| 11874 | state_vec = GGML_F32_VEC_FMA(state_vec, sa_vec, b_vec); |
| 11875 | GGML_F32_VEC_STORE(&state_cur[h_2d_i_j_offset], state_vec); |
| 11876 | |
| 11877 | result_vec[kk] = GGML_F32_VEC_FMA(result_vec[kk], state_vec, r_vec); |
| 11878 | } |
| 11879 | } |
| 11880 | GGML_F32_VEC_REDUCE(dst_data[t_h_i_offset], result_vec); |
| 11881 | |
| 11882 | // There shouldn't be left-overs though. |
| 11883 | for (; j < head_size; j++) { |
| 11884 | int64_t t_h_j_offset = t_h_offset + j; |
| 11885 | int64_t h_2d_i_j_offset = h_2d_i_offset + j; |
| 11886 | |
| 11887 | float r_val = r[t_h_j_offset]; |
| 11888 | float w_val = w[t_h_j_offset]; |
| 11889 | float k_val = k[t_h_j_offset]; |
| 11890 | float b_val = b[t_h_j_offset]; |
| 11891 | float kv_val = v[t_h_i_offset] * k_val; |
| 11892 | |
| 11893 | float prev_state_val = state_prev[h_2d_i_j_offset]; |
| 11894 | state_cur[h_2d_i_j_offset] = prev_state_val * w_val + kv_val + sa * b_val; |
| 11895 | dst_data[t_h_i_offset] += state_cur[h_2d_i_j_offset] * r_val; |
| 11896 | } |
| 11897 | } |
| 11898 | } |
| 11899 | } |
| 11900 | #endif |
| 11901 | #else |
| 11902 | for (int64_t t = 0; t < T; t++) { |
| 11903 | int64_t t_offset = t * t_stride; |
| 11904 | int64_t state_offset = head_size * C * (t / (T / n_seqs)); |
| 11905 | float * state_cur = state + state_offset; |
| 11906 | float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[6]->data + state_offset; |
| 11907 | |
| 11908 | for (int64_t h = h_start; h < h_end; h++) { |
| 11909 | int64_t h_offset = h * h_stride; |
| 11910 | int64_t t_h_offset = t_offset + h_offset; |
| 11911 | int64_t h_2d_offset = h * h_stride_2d; |
| 11912 | |
| 11913 | for (int64_t i = 0; i < head_size; i++) { |
| 11914 | int64_t t_h_i_offset = t_h_offset + i; |
| 11915 | int64_t h_2d_i_offset = h_2d_offset + i * h_stride; |
| 11916 | |
| 11917 | float v_val = v[t_h_i_offset]; |
| 11918 | |
| 11919 | float sa = 0, result = 0; |
| 11920 | for (int64_t j = 0; j < head_size; j++) { |
| 11921 | sa += a[t_h_offset + j] * state_prev[h_2d_i_offset + j]; |
| 11922 | } |
| 11923 | |
| 11924 | for (int64_t j = 0; j < head_size; j++) { |
| 11925 | int64_t t_h_j_offset = t_h_offset + j; |
| 11926 | int64_t h_2d_i_j_offset = h_2d_i_offset + j; |
| 11927 | |
| 11928 | float r_val = r[t_h_j_offset]; |
| 11929 | float w_val = w[t_h_j_offset]; |
| 11930 | float k_val = k[t_h_j_offset]; |
| 11931 | float b_val = b[t_h_j_offset]; |
| 11932 | float kv_val = v_val * k_val; |
| 11933 | float prev_state_val = state_prev[h_2d_i_j_offset]; |
| 11934 | state_cur[h_2d_i_j_offset] = prev_state_val * w_val + kv_val + sa * b_val; |
| 11935 | result += state_cur[h_2d_i_j_offset] * r_val; |
| 11936 | } |
| 11937 | dst_data[t_h_i_offset] = result; |
| 11938 | } |
| 11939 | } |
| 11940 | } |
| 11941 | #endif |
| 11942 | } |
| 11943 | |
| 11944 | |
| 11945 | void ggml_compute_forward_rwkv_wkv7( |
| 11946 | const ggml_compute_params * params, |
| 11947 | ggml_tensor * dst) { |
| 11948 | |
| 11949 | const ggml_tensor * src0 = dst->src[0]; |
| 11950 | |
| 11951 | switch (src0->type) { |
| 11952 | case GGML_TYPE_F32: |
| 11953 | { |
| 11954 | ggml_compute_forward_rwkv_wkv7_f32(params, dst); |
| 11955 | } break; |
| 11956 | default: |
| 11957 | { |
| 11958 | GGML_ABORT("fatal error"); |
| 11959 | } |
| 11960 | } |
| 11961 | } |
| 11962 | |
| 11963 | // ggml_compute_forward_map_custom1 |
| 11964 | |
| 11965 | void ggml_compute_forward_map_custom1( |
| 11966 | const ggml_compute_params * params, |
| 11967 | ggml_tensor * dst) { |
| 11968 | |
| 11969 | const ggml_tensor * a = dst->src[0]; |
| 11970 | |
| 11971 | struct ggml_map_custom1_op_params p; |
| 11972 | memcpy(&p, dst->op_params, sizeof(p)); |
| 11973 | |
| 11974 | p.fun(dst, a, params->ith, params->nth, p.userdata); |
| 11975 | } |
| 11976 | |
| 11977 | // ggml_compute_forward_map_custom2 |
| 11978 | |
| 11979 | void ggml_compute_forward_map_custom2( |
| 11980 | const ggml_compute_params * params, |
| 11981 | ggml_tensor * dst) { |
| 11982 | |
| 11983 | const ggml_tensor * a = dst->src[0]; |
| 11984 | const ggml_tensor * b = dst->src[1]; |
| 11985 | |
| 11986 | struct ggml_map_custom2_op_params p; |
| 11987 | memcpy(&p, dst->op_params, sizeof(p)); |
| 11988 | |
| 11989 | p.fun(dst, a, b, params->ith, params->nth, p.userdata); |
| 11990 | } |
| 11991 | |
| 11992 | // ggml_compute_forward_map_custom3 |
| 11993 | |
| 11994 | void ggml_compute_forward_map_custom3( |
| 11995 | const ggml_compute_params * params, |
| 11996 | ggml_tensor * dst) { |
| 11997 | |
| 11998 | const ggml_tensor * a = dst->src[0]; |
| 11999 | const ggml_tensor * b = dst->src[1]; |
| 12000 | const ggml_tensor * c = dst->src[2]; |
| 12001 | |
| 12002 | struct ggml_map_custom3_op_params p; |
| 12003 | memcpy(&p, dst->op_params, sizeof(p)); |
| 12004 | |
| 12005 | p.fun(dst, a, b, c, params->ith, params->nth, p.userdata); |
| 12006 | } |
| 12007 | |
| 12008 | // ggml_compute_forward_custom |
| 12009 | |
| 12010 | void ggml_compute_forward_custom( |
| 12011 | const struct ggml_compute_params * params, |
| 12012 | struct ggml_tensor * dst) { |
| 12013 | |
| 12014 | struct ggml_custom_op_params p; |
| 12015 | memcpy(&p, dst->op_params, sizeof(p)); |
| 12016 | |
| 12017 | p.fun(dst, params->ith, params->nth, p.userdata); |
| 12018 | } |
| 12019 | |
| 12020 | // ggml_compute_forward_cross_entropy_loss |
| 12021 | |
| 12022 | static void ggml_compute_forward_cross_entropy_loss_f32( |
| 12023 | const ggml_compute_params * params, |
| 12024 | ggml_tensor * dst) { |
| 12025 | |
| 12026 | const ggml_tensor * src0 = dst->src[0]; |
| 12027 | const ggml_tensor * src1 = dst->src[1]; |
| 12028 | |
| 12029 | GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 12030 | GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 12031 | GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type)); |
| 12032 | GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type)); |
| 12033 | GGML_ASSERT(ggml_are_same_shape(src0, src1)); |
| 12034 | GGML_ASSERT(ggml_is_scalar(dst)); |
| 12035 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 12036 | |
| 12037 | // TODO: handle transposed/permuted matrices |
| 12038 | const int64_t nc = src0->ne[0]; |
| 12039 | const int64_t nr = ggml_nrows(src0); |
| 12040 | |
| 12041 | const int ith = params->ith; |
| 12042 | const int nth = params->nth; |
| 12043 | |
| 12044 | float * sums = (float *) params->wdata; |
| 12045 | float * st = ((float *) params->wdata) + nth + ith*nc; |
| 12046 | float sum_thread = 0.0f; |
| 12047 | |
| 12048 | GGML_ASSERT(params->wsize >= sizeof(float) * (nth + nth * nc)); |
| 12049 | |
| 12050 | // rows per thread |
| 12051 | const int64_t dr = (nr + nth - 1)/nth; |
| 12052 | |
| 12053 | // row range for this thread |
| 12054 | const int64_t ir0 = dr*ith; |
| 12055 | const int64_t ir1 = MIN(ir0 + dr, nr); |
| 12056 | |
| 12057 | for (int64_t i1 = ir0; i1 < ir1; ++i1) { |
| 12058 | const float * s0 = (const float *)((const char *) src0->data + i1*src0->nb[1]); |
| 12059 | const float * s1 = (const float *)((const char *) src1->data + i1*src1->nb[1]); |
| 12060 | |
| 12061 | #ifndef NDEBUG |
| 12062 | for (int64_t i = 0; i < nc; ++i) { |
| 12063 | //printf("p[%d] = %f\n", i, p[i]); |
| 12064 | assert(!isnan(s0[i])); |
| 12065 | assert(!isnan(s1[i])); |
| 12066 | } |
| 12067 | #endif // NDEBUG |
| 12068 | |
| 12069 | float max = -INFINITY; |
| 12070 | ggml_vec_max_f32(nc, &max, s0); |
| 12071 | const ggml_float sum_softmax = ggml_vec_log_soft_max_f32(nc, st, s0, max); |
| 12072 | assert(sum_softmax >= 0.0); |
| 12073 | |
| 12074 | ggml_vec_add1_f32(nc, st, st, -sum_softmax); |
| 12075 | ggml_vec_mul_f32(nc, st, st, s1); |
| 12076 | |
| 12077 | float sum_st = 0.0f; |
| 12078 | ggml_vec_sum_f32(nc, &sum_st, st); |
| 12079 | sum_thread += sum_st; |
| 12080 | |
| 12081 | #ifndef NDEBUG |
| 12082 | for (int64_t i = 0; i < nc; ++i) { |
| 12083 | assert(!isnan(st[i])); |
| 12084 | assert(!isinf(st[i])); |
| 12085 | } |
| 12086 | #endif // NDEBUG |
| 12087 | } |
| 12088 | sums[ith] = sum_thread; |
| 12089 | ggml_barrier(params->threadpool); |
| 12090 | |
| 12091 | if (ith == 0) { |
| 12092 | float * dp = (float *) dst->data; |
| 12093 | ggml_vec_sum_f32(nth, dp, sums); |
| 12094 | dp[0] *= -1.0f / (float) nr; |
| 12095 | } |
| 12096 | } |
| 12097 | |
| 12098 | void ggml_compute_forward_cross_entropy_loss( |
| 12099 | const ggml_compute_params * params, |
| 12100 | ggml_tensor * dst) { |
| 12101 | |
| 12102 | const ggml_tensor * src0 = dst->src[0]; |
| 12103 | |
| 12104 | switch (src0->type) { |
| 12105 | case GGML_TYPE_F32: |
| 12106 | { |
| 12107 | ggml_compute_forward_cross_entropy_loss_f32(params, dst); |
| 12108 | } break; |
| 12109 | default: |
| 12110 | { |
| 12111 | GGML_ABORT("fatal error"); |
| 12112 | } |
| 12113 | } |
| 12114 | } |
| 12115 | |
| 12116 | // ggml_compute_forward_cross_entropy_loss_back |
| 12117 | |
| 12118 | static void ggml_compute_forward_cross_entropy_loss_back_f32( |
| 12119 | const ggml_compute_params * params, |
| 12120 | ggml_tensor * dst) { |
| 12121 | |
| 12122 | const ggml_tensor * grad = dst->src[0]; // gradient of forward pass output |
| 12123 | const ggml_tensor * src0f = dst->src[1]; // src0 of forward pass |
| 12124 | const ggml_tensor * src1f = dst->src[2]; // src1 of forward pass |
| 12125 | |
| 12126 | GGML_ASSERT(ggml_is_contiguous(dst)); |
| 12127 | GGML_ASSERT(ggml_is_contiguous(src0f)); |
| 12128 | GGML_ASSERT(ggml_is_contiguous(src1f)); |
| 12129 | GGML_ASSERT(ggml_is_contiguous(grad)); |
| 12130 | GGML_ASSERT(ggml_are_same_shape(src0f, src1f) && ggml_are_same_shape(src0f, dst)); |
| 12131 | |
| 12132 | const int64_t ith = params->ith; |
| 12133 | const int64_t nth = params->nth; |
| 12134 | |
| 12135 | // TODO: handle transposed/permuted matrices |
| 12136 | const int64_t nc = src0f->ne[0]; |
| 12137 | const int64_t nr = ggml_nrows(src0f); |
| 12138 | |
| 12139 | // rows per thread |
| 12140 | const int64_t dr = (nr + nth - 1)/nth; |
| 12141 | |
| 12142 | // row range for this thread |
| 12143 | const int64_t ir0 = dr*ith; |
| 12144 | const int64_t ir1 = MIN(ir0 + dr, nr); |
| 12145 | |
| 12146 | const float d_by_nr = ((const float *) grad->data)[0] / (float) nr; |
| 12147 | |
| 12148 | for (int64_t i1 = ir0; i1 < ir1; i1++) { |
| 12149 | float * ds0 = (float *)((char *) dst->data + i1*dst->nb[1]); |
| 12150 | const float * s0 = (const float *)((const char *) src0f->data + i1*src0f->nb[1]); |
| 12151 | const float * s1 = (const float *)((const char *) src1f->data + i1*src1f->nb[1]); |
| 12152 | |
| 12153 | #ifndef NDEBUG |
| 12154 | for (int64_t i = 0; i < nc; ++i) { |
| 12155 | //printf("p[%d] = %f\n", i, p[i]); |
| 12156 | assert(!isnan(s0[i])); |
| 12157 | assert(!isnan(s1[i])); |
| 12158 | } |
| 12159 | #endif // NDEBUG |
| 12160 | |
| 12161 | // soft_max |
| 12162 | float max = -INFINITY; |
| 12163 | ggml_vec_max_f32(nc, &max, s0); |
| 12164 | const ggml_float sum = ggml_vec_soft_max_f32(nc, ds0, s0, max); |
| 12165 | assert(sum > 0.0); |
| 12166 | ggml_vec_scale_f32(nc, ds0, 1.0/sum); |
| 12167 | |
| 12168 | // grad(src0f) = (softmax(src0f) - src1f) * grad(cross_entropy_loss(src0f, src1f)) / nr |
| 12169 | ggml_vec_sub_f32(nc, ds0, ds0, s1); |
| 12170 | ggml_vec_scale_f32(nc, ds0, d_by_nr); |
| 12171 | |
| 12172 | #ifndef NDEBUG |
| 12173 | for (int64_t i = 0; i < nc; ++i) { |
| 12174 | assert(!isnan(ds0[i])); |
| 12175 | assert(!isinf(ds0[i])); |
| 12176 | } |
| 12177 | #endif // NDEBUG |
| 12178 | } |
| 12179 | } |
| 12180 | |
| 12181 | void ggml_compute_forward_cross_entropy_loss_back( |
| 12182 | const ggml_compute_params * params, |
| 12183 | ggml_tensor * dst) { |
| 12184 | |
| 12185 | const ggml_tensor * src0 = dst->src[0]; |
| 12186 | |
| 12187 | switch (src0->type) { |
| 12188 | case GGML_TYPE_F32: |
| 12189 | { |
| 12190 | ggml_compute_forward_cross_entropy_loss_back_f32(params, dst); |
| 12191 | } break; |
| 12192 | default: |
| 12193 | { |
| 12194 | GGML_ABORT("fatal error"); |
| 12195 | } |
| 12196 | } |
| 12197 | } |
| 12198 | |
| 12199 | static void ggml_compute_forward_opt_step_adamw_f32( |
| 12200 | const ggml_compute_params * params, |
| 12201 | ggml_tensor * dst) { |
| 12202 | |
| 12203 | const ggml_tensor * src0 = dst->src[0]; |
| 12204 | const ggml_tensor * src0_grad = dst->src[1]; |
| 12205 | const ggml_tensor * src0_grad_m = dst->src[2]; |
| 12206 | const ggml_tensor * src0_grad_v = dst->src[3]; |
| 12207 | const ggml_tensor * adamw_params = dst->src[4]; |
| 12208 | |
| 12209 | GGML_ASSERT(ggml_are_same_shape(src0, src0_grad)); |
| 12210 | GGML_ASSERT(ggml_are_same_shape(src0, src0_grad_m)); |
| 12211 | GGML_ASSERT(ggml_are_same_shape(src0, src0_grad_v)); |
| 12212 | GGML_ASSERT(ggml_nelements(adamw_params) == 7); |
| 12213 | |
| 12214 | const int ith = params->ith; |
| 12215 | const int nth = params->nth; |
| 12216 | |
| 12217 | const int nr = ggml_nrows(src0); |
| 12218 | |
| 12219 | GGML_TENSOR_UNARY_OP_LOCALS |
| 12220 | GGML_ASSERT(nb00 == sizeof(float)); |
| 12221 | |
| 12222 | // rows per thread |
| 12223 | const int dr = (nr + nth - 1)/nth; |
| 12224 | |
| 12225 | // row range for this thread |
| 12226 | const int ir0 = dr*ith; |
| 12227 | const int ir1 = MIN(ir0 + dr, nr); |
| 12228 | |
| 12229 | const float * adamw_params_ptr = ggml_get_data_f32(adamw_params); |
| 12230 | |
| 12231 | const float alpha = adamw_params_ptr[0]; |
| 12232 | const float beta1 = adamw_params_ptr[1]; |
| 12233 | const float beta2 = adamw_params_ptr[2]; |
| 12234 | const float eps = adamw_params_ptr[3]; |
| 12235 | const float wd = adamw_params_ptr[4]; |
| 12236 | const float beta1h = adamw_params_ptr[5]; |
| 12237 | const float beta2h = adamw_params_ptr[6]; |
| 12238 | const float keep = 1.f - alpha * wd; |
| 12239 | for (int ir = ir0; ir < ir1; ++ir) { |
| 12240 | const int64_t i03 = ir/(ne02*ne01); |
| 12241 | const int64_t i02 = (ir - i03*ne02*ne01)/ne01; |
| 12242 | const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01); |
| 12243 | |
| 12244 | const size_t offset = i03*nb03 + i02*nb02 + i01*nb01; |
| 12245 | |
| 12246 | float * w = (float *) ((char *) src0->data + offset); // weight |
| 12247 | const float * g = (const float *) ((const char *) src0_grad->data + offset); // grad |
| 12248 | float * m = (float *) ((char *) src0_grad_m->data + offset); |
| 12249 | float * v = (float *) ((char *) src0_grad_v->data + offset); |
| 12250 | |
| 12251 | for (int i00 = 0; i00 < ne00; ++i00) { |
| 12252 | m[i00] = m[i00]*beta1 + g[i00]*(1.0f - beta1); |
| 12253 | v[i00] = v[i00]*beta2 + g[i00]*g[i00]*(1.0f - beta2); |
| 12254 | |
| 12255 | const float mh = m[i00]*beta1h; |
| 12256 | const float vh = sqrtf(v[i00]*beta2h) + eps; |
| 12257 | |
| 12258 | // The weight decay is applied independently of the Adam momenta m and v. |
| 12259 | // This is NOT equivalent to l2 regularization that adds w[i00]*w[i00] to the loss. |
| 12260 | // See: https://arxiv.org/pdf/1711.05101v3.pdf |
| 12261 | w[i00] = w[i00] * keep - alpha * mh / vh; |
| 12262 | } |
| 12263 | } |
| 12264 | } |
| 12265 | |
| 12266 | void ggml_compute_forward_opt_step_adamw( |
| 12267 | const ggml_compute_params * params, |
| 12268 | ggml_tensor * dst) { |
| 12269 | |
| 12270 | const ggml_tensor * src0 = dst->src[0]; |
| 12271 | |
| 12272 | switch (src0->type) { |
| 12273 | case GGML_TYPE_F32: |
| 12274 | { |
| 12275 | ggml_compute_forward_opt_step_adamw_f32(params, dst); |
| 12276 | } break; |
| 12277 | default: |
| 12278 | { |
| 12279 | GGML_ABORT("fatal error"); |
| 12280 | } |
| 12281 | } |
| 12282 | } |
| 12283 | |
| 12284 | static void ggml_compute_forward_opt_step_sgd_f32(const ggml_compute_params * params, ggml_tensor * dst) { |
| 12285 | const ggml_tensor * src0 = dst->src[0]; |
| 12286 | const ggml_tensor * src0_grad = dst->src[1]; |
| 12287 | const ggml_tensor * sgd_params = dst->src[2]; |
| 12288 | |
| 12289 | GGML_ASSERT(ggml_are_same_shape(src0, src0_grad)); |
| 12290 | GGML_ASSERT(ggml_nelements(sgd_params) == 2); |
| 12291 | |
| 12292 | const int ith = params->ith; |
| 12293 | const int nth = params->nth; |
| 12294 | |
| 12295 | const int nr = ggml_nrows(src0); |
| 12296 | |
| 12297 | GGML_TENSOR_UNARY_OP_LOCALS |
| 12298 | GGML_ASSERT(nb00 == sizeof(float)); |
| 12299 | |
| 12300 | // rows per thread |
| 12301 | const int dr = (nr + nth - 1) / nth; |
| 12302 | |
| 12303 | // row range for this thread |
| 12304 | const int ir0 = dr * ith; |
| 12305 | const int ir1 = MIN(ir0 + dr, nr); |
| 12306 | |
| 12307 | // using adamw param subset we care about - alpha, wd - could have a separate struct |
| 12308 | const float * sgd_params_ptr = ggml_get_data_f32(sgd_params); |
| 12309 | const float alpha = sgd_params_ptr[0]; |
| 12310 | const float keep = 1.f - alpha * sgd_params_ptr[1]; |
| 12311 | |
| 12312 | for (int ir = ir0; ir < ir1; ++ir) { |
| 12313 | const int64_t i03 = ir / (ne02 * ne01); |
| 12314 | const int64_t i02 = (ir - i03 * ne02 * ne01) / ne01; |
| 12315 | const int64_t i01 = (ir - i03 * ne02 * ne01 - i02 * ne01); |
| 12316 | |
| 12317 | const size_t offset = i03 * nb03 + i02 * nb02 + i01 * nb01; |
| 12318 | |
| 12319 | float * w = (float *) ((char *) src0->data + offset); // weight |
| 12320 | const float * g = (const float *) ((const char *) src0_grad->data + offset); // grad |
| 12321 | |
| 12322 | for (int i00 = 0; i00 < ne00; ++i00) { |
| 12323 | w[i00] = w[i00] * keep - alpha * g[i00]; |
| 12324 | } |
| 12325 | } |
| 12326 | } |
| 12327 | |
| 12328 | void ggml_compute_forward_opt_step_sgd(const ggml_compute_params * params, ggml_tensor * dst) { |
| 12329 | const ggml_tensor * src0 = dst->src[0]; |
| 12330 | |
| 12331 | switch (src0->type) { |
| 12332 | case GGML_TYPE_F32: |
| 12333 | { |
| 12334 | ggml_compute_forward_opt_step_sgd_f32(params, dst); |
| 12335 | } |
| 12336 | break; |
| 12337 | default: |
| 12338 | { |
| 12339 | GGML_ABORT("fatal error - sgd is F32 only"); |
| 12340 | } |
| 12341 | } |
| 12342 | } |
| 12343 | |
| 12344 | static inline float ggml_fwht_load(const float value) { |
| 12345 | return value; |
| 12346 | } |
| 12347 | |
| 12348 | static inline float ggml_fwht_load(const ggml_fp16_t value) { |
| 12349 | return ggml_fp16_to_fp32(value); |
| 12350 | } |
| 12351 | |
| 12352 | template<typename src_t> |
| 12353 | static void ggml_compute_forward_fwht_impl(const ggml_compute_params * params, ggml_tensor * dst) { |
| 12354 | const ggml_tensor * src0 = dst->src[0]; |
| 12355 | const ggml_tensor * src1 = dst->src[1]; |
| 12356 | |
| 12357 | GGML_ASSERT(src1->type == (std::is_same_v<src_t, float> ? GGML_TYPE_F32 : GGML_TYPE_F16)); |
| 12358 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 12359 | |
| 12360 | GGML_TENSOR_BINARY_OP_LOCALS |
| 12361 | |
| 12362 | const int ith = params->ith; |
| 12363 | const int nth = params->nth; |
| 12364 | |
| 12365 | const int64_t n = ne10; |
| 12366 | GGML_ASSERT((n & (n - 1)) == 0); // must be power of 2 |
| 12367 | |
| 12368 | const int64_t nr = ne11 * ne12 * ne13; |
| 12369 | const int64_t rows_per_thread = (nr + nth - 1) / nth; |
| 12370 | const int64_t start_row = ith * rows_per_thread; |
| 12371 | const int64_t end_row = MIN(start_row + rows_per_thread, nr); |
| 12372 | |
| 12373 | const float scale = 1.0f / sqrtf((float)n); |
| 12374 | |
| 12375 | #if defined(GGML_SIMD) |
| 12376 | const GGML_F32_VEC v_minus_one = GGML_F32_VEC_SET1(-1.0f); |
| 12377 | #endif |
| 12378 | |
| 12379 | for (int64_t r = start_row; r < end_row; r++) { |
| 12380 | const int64_t i13 = r / (ne11 * ne12); |
| 12381 | const int64_t i12 = (r - i13 * ne11 * ne12) / ne11; |
| 12382 | const int64_t i11 = r - i13 * ne11 * ne12 - i12 * ne11; |
| 12383 | |
| 12384 | const src_t * src_row = (const src_t *) ((const char *) src1->data + i11 * nb11 + i12 * nb12 + i13 * nb13); |
| 12385 | float * dst_row = (float *) ((char *) dst->data + i11 * nb1 + i12 * nb2 + i13 * nb3); |
| 12386 | |
| 12387 | for (int64_t j = 0; j < n; j++) { |
| 12388 | dst_row[j] = ggml_fwht_load(src_row[j]) * scale; |
| 12389 | } |
| 12390 | |
| 12391 | // Scalar passes |
| 12392 | #if defined(GGML_SIMD) |
| 12393 | #if defined(__ARM_FEATURE_SVE) |
| 12394 | const int step = svcntw(); |
| 12395 | #else |
| 12396 | const int step = GGML_F32_EPR; |
| 12397 | #endif |
| 12398 | #else |
| 12399 | const int step = n; |
| 12400 | #endif |
| 12401 | for (int64_t len = 1; len < step && len < n; len <<= 1) { |
| 12402 | for (int64_t i = 0; i < n; i += 2 * len) { |
| 12403 | for (int64_t j = 0; j < len; j++) { |
| 12404 | float u = dst_row[i + j]; |
| 12405 | float v = dst_row[i + len + j]; |
| 12406 | dst_row[i + j] = u + v; |
| 12407 | dst_row[i + len + j] = u - v; |
| 12408 | } |
| 12409 | } |
| 12410 | } |
| 12411 | |
| 12412 | // SIMD passes using GGML_F32_VEC_* macros for multi-architecture support |
| 12413 | #if defined(GGML_SIMD) |
| 12414 | for (int64_t len = step; len < n; len <<= 1) { |
| 12415 | for (int64_t i = 0; i < n; i += 2 * len) { |
| 12416 | for (int64_t j = 0; j < len; j += step) { |
| 12417 | GGML_F32_VEC u = GGML_F32_VEC_LOAD(dst_row + i + j); |
| 12418 | GGML_F32_VEC v = GGML_F32_VEC_LOAD(dst_row + i + len + j); |
| 12419 | |
| 12420 | GGML_F32_VEC_STORE(dst_row + i + j, GGML_F32_VEC_ADD(u, v)); |
| 12421 | GGML_F32_VEC_STORE(dst_row + i + len + j, GGML_F32_VEC_FMA(u, v, v_minus_one)); |
| 12422 | } |
| 12423 | } |
| 12424 | } |
| 12425 | #endif |
| 12426 | } |
| 12427 | } |
| 12428 | |
| 12429 | void ggml_compute_forward_fwht(const ggml_compute_params * params, ggml_tensor * dst) { |
| 12430 | const ggml_tensor * src1 = dst->src[1]; |
| 12431 | |
| 12432 | switch (src1->type) { |
| 12433 | case GGML_TYPE_F32: |
| 12434 | { |
| 12435 | ggml_compute_forward_fwht_impl<float>(params, dst); |
| 12436 | } |
| 12437 | break; |
| 12438 | case GGML_TYPE_F16: |
| 12439 | { |
| 12440 | ggml_compute_forward_fwht_impl<ggml_fp16_t>(params, dst); |
| 12441 | } |
| 12442 | break; |
| 12443 | default: |
| 12444 | { |
| 12445 | GGML_ABORT("fatal error - fwht supports F32 and F16 input"); |
| 12446 | } |
| 12447 | } |
| 12448 | } |
| 12449 | |
| 12450 | // ggml_compute_forward_lightning_indexer |
| 12451 | |
| 12452 | void ggml_compute_forward_lightning_indexer( |
| 12453 | const ggml_compute_params * params, |
| 12454 | ggml_tensor * dst) { |
| 12455 | |
| 12456 | const ggml_tensor * q = dst->src[0]; |
| 12457 | const ggml_tensor * k = dst->src[1]; |
| 12458 | const ggml_tensor * w = dst->src[2]; // weights |
| 12459 | const ggml_tensor * m = dst->src[3]; // mask |
| 12460 | |
| 12461 | GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 12462 | GGML_ASSERT( q->type == GGML_TYPE_F32); |
| 12463 | GGML_ASSERT( w->type == GGML_TYPE_F32); |
| 12464 | GGML_ASSERT( m->type == GGML_TYPE_F16); |
| 12465 | |
| 12466 | GGML_TENSOR_LOCALS(int64_t, neq, q, ne) |
| 12467 | GGML_TENSOR_LOCALS(size_t, nbq, q, nb) |
| 12468 | GGML_TENSOR_LOCALS(int64_t, nek, k, ne) |
| 12469 | GGML_TENSOR_LOCALS(size_t, nbk, k, nb) |
| 12470 | GGML_TENSOR_LOCALS(int64_t, new, w, ne) |
| 12471 | GGML_TENSOR_LOCALS(size_t, nbw, w, nb) |
| 12472 | GGML_TENSOR_LOCALS(int64_t, nem, m, ne) |
| 12473 | GGML_TENSOR_LOCALS(size_t, nbm, m, nb) |
| 12474 | GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 12475 | GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 12476 | |
| 12477 | GGML_ASSERT( nb0 == ggml_type_size(dst->type)); |
| 12478 | GGML_ASSERT(nbq0 == ggml_type_size( q->type)); |
| 12479 | GGML_ASSERT(nbk0 == ggml_type_size( k->type)); |
| 12480 | GGML_ASSERT(nbw0 == ggml_type_size( w->type)); |
| 12481 | GGML_ASSERT(nbm0 == ggml_type_size( m->type)); |
| 12482 | |
| 12483 | const int n_embd = q->ne[0]; |
| 12484 | const int n_head = q->ne[1]; |
| 12485 | const int n_tokens = q->ne[2]; |
| 12486 | const int n_stream = q->ne[3]; |
| 12487 | const int n_kv = k->ne[2]; |
| 12488 | |
| 12489 | ggml_to_float_t const k_to_float = ggml_get_type_traits(k->type)->to_float; |
| 12490 | GGML_ASSERT((k->type == GGML_TYPE_F32 || k_to_float) && "lightning indexer: unsupported K-type"); |
| 12491 | |
| 12492 | const int nr = n_kv; |
| 12493 | const int ith = params->ith; |
| 12494 | const int nth = params->nth; |
| 12495 | |
| 12496 | // (temporary) buffer for K converted to float |
| 12497 | float * k_row_f32 = (float *) params->wdata + ith*(1*n_embd + CACHE_LINE_SIZE_F32); |
| 12498 | |
| 12499 | // rows per thread |
| 12500 | const int dr = (nr + nth - 1)/nth; |
| 12501 | |
| 12502 | // row range for this thread |
| 12503 | const int ir0 = dr*ith; |
| 12504 | const int ir1 = MIN(ir0 + dr, nr); |
| 12505 | |
| 12506 | for (int s = 0; s < n_stream; ++s) { |
| 12507 | for (int t = 0; t < n_tokens; ++t) { |
| 12508 | const float * w_row = (float *) ((char *) w->data + t*nbw1 + s*nbw3); |
| 12509 | const ggml_fp16_t * m_row = (ggml_fp16_t *) ((char *) m->data + t*nbm1 + (s%nem3)*nbm3); |
| 12510 | float * dst_row = (float *) ((char *) dst->data + t*nb1 + s*nb3 ); |
| 12511 | for (int ik = ir0; ik < ir1; ++ik) { |
| 12512 | char * k_row = (char *) k->data + ik*nbk2 + s*nbk3; |
| 12513 | if (k_to_float) { |
| 12514 | k_to_float(k_row, k_row_f32, n_embd); |
| 12515 | } else { |
| 12516 | k_row_f32 = (float *) k_row; |
| 12517 | } |
| 12518 | float score = 0.0f; |
| 12519 | for (int h = 0; h < n_head; ++h) { |
| 12520 | // dot product of q and k for head h |
| 12521 | float qk = 0.0f; |
| 12522 | const float * q_row = (float *) ((char *) q->data + h*nbq1 + t*nbq2 + s*nbq3); |
| 12523 | ggml_vec_dot_f32(n_embd, &qk, 0, q_row, 0, k_row_f32, 0, 1); |
| 12524 | // ReLU and weights (prescaled) |
| 12525 | score += MAX(qk, 0.0f) * w_row[h]; |
| 12526 | } |
| 12527 | // apply mask |
| 12528 | dst_row[ik] = score + GGML_CPU_FP16_TO_FP32(m_row[ik]); |
| 12529 | } |
| 12530 | } |
| 12531 | } |
| 12532 | } |