| 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 contigous 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 ggml_compute_forward_dup_to_q<ggml_fp16_t>(params, dst); |
| 544 |
} break; |
| 545 |
case GGML_TYPE_BF16: |
| 546 |
{ |
| 547 |
/**/ if (dst->type == GGML_TYPE_F16) ggml_compute_forward_dup_flt<ggml_bf16_t, ggml_fp16_t>(params, dst); |
| 548 |
else if (dst->type == GGML_TYPE_BF16) ggml_compute_forward_dup_flt<ggml_bf16_t, ggml_bf16_t>(params, dst); |
| 549 |
else if (dst->type == GGML_TYPE_F32) ggml_compute_forward_dup_flt<ggml_bf16_t, float >(params, dst); |
| 550 |
else ggml_compute_forward_dup_to_q<ggml_bf16_t>(params, dst); |
| 551 |
} break; |
| 552 |
case GGML_TYPE_F32: |
| 553 |
{ |
| 554 |
/**/ if (dst->type == GGML_TYPE_F16) ggml_compute_forward_dup_flt<float, ggml_fp16_t>(params, dst); |
| 555 |
else if (dst->type == GGML_TYPE_BF16) ggml_compute_forward_dup_flt<float, ggml_bf16_t>(params, dst); |
| 556 |
else if (dst->type == GGML_TYPE_F32) ggml_compute_forward_dup_flt<float, float >(params, dst); |
| 557 |
else if (dst->type == GGML_TYPE_I32) ggml_compute_forward_dup_flt<float, int32_t >(params, dst); |
| 558 |
else ggml_compute_forward_dup_to_q<float>(params, dst); |
| 559 |
} break; |
| 560 |
case GGML_TYPE_I32: |
| 561 |
{ |
| 562 |
if (dst->type == GGML_TYPE_F32) ggml_compute_forward_dup_flt<int32_t, float>(params, dst); |
| 563 |
else GGML_ABORT("not implemented"); |
| 564 |
} break; |
| 565 |
default: |
| 566 |
{ |
| 567 |
if (ggml_is_quantized(src0->type) && dst->type == GGML_TYPE_F32) { |
| 568 |
ggml_compute_forward_dup_from_q(params, dst); |
| 569 |
break; |
| 570 |
} |
| 571 |
GGML_ABORT("fatal error"); |
| 572 |
} |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
// ggml_compute_forward_add |
| 577 |
|
| 578 |
static void ggml_compute_forward_add_q_f32( |
| 579 |
const ggml_compute_params * params, |
| 580 |
ggml_tensor * dst) { |
| 581 |
|
| 582 |
const ggml_tensor * src0 = dst->src[0]; |
| 583 |
const ggml_tensor * src1 = dst->src[1]; |
| 584 |
|
| 585 |
GGML_ASSERT(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst)); |
| 586 |
|
| 587 |
const int nr = ggml_nrows(src0); |
| 588 |
|
| 589 |
GGML_TENSOR_BINARY_OP_LOCALS |
| 590 |
|
| 591 |
const int ith = params->ith; |
| 592 |
const int nth = params->nth; |
| 593 |
|
| 594 |
const ggml_type type = src0->type; |
| 595 |
const ggml_type dtype = dst->type; |
| 596 |
ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float; |
| 597 |
ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(dtype)->from_float; |
| 598 |
|
| 599 |
// we don't support permuted src0 or src1 |
| 600 |
GGML_ASSERT(nb00 == ggml_type_size(type)); |
| 601 |
GGML_ASSERT(nb10 == sizeof(float)); |
| 602 |
|
| 603 |
// dst cannot be transposed or permuted |
| 604 |
GGML_ASSERT(nb0 <= nb1); |
| 605 |
GGML_ASSERT(nb1 <= nb2); |
| 606 |
GGML_ASSERT(nb2 <= nb3); |
| 607 |
|
| 608 |
GGML_ASSERT(ggml_is_quantized(src0->type)); |
| 609 |
GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 610 |
|
| 611 |
// rows per thread |
| 612 |
const int dr = (nr + nth - 1)/nth; |
| 613 |
|
| 614 |
// row range for this thread |
| 615 |
const int ir0 = dr*ith; |
| 616 |
const int ir1 = MIN(ir0 + dr, nr); |
| 617 |
|
| 618 |
float * wdata = (float *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith; |
| 619 |
|
| 620 |
for (int ir = ir0; ir < ir1; ++ir) { |
| 621 |
// src0 indices |
| 622 |
const int i03 = ir/(ne02*ne01); |
| 623 |
const int i02 = (ir - i03*ne02*ne01)/ne01; |
| 624 |
const int i01 = (ir - i03*ne02*ne01 - i02*ne01); |
| 625 |
|
| 626 |
// src1 and dst are same shape as src0 => same indices |
| 627 |
const int i13 = i03; |
| 628 |
const int i12 = i02; |
| 629 |
const int i11 = i01; |
| 630 |
|
| 631 |
const int i3 = i03; |
| 632 |
const int i2 = i02; |
| 633 |
const int i1 = i01; |
| 634 |
|
| 635 |
void * src0_row = (void *) ((char *) src0->data + (i01*nb01 + i02*nb02 + i03*nb03)); |
| 636 |
float * src1_row = (float *)((char *) src1->data + (i11*nb11 + i12*nb12 + i13*nb13)); |
| 637 |
void * dst_row = (void *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3)); |
| 638 |
|
| 639 |
assert(ne00 % 32 == 0); |
| 640 |
|
| 641 |
// unquantize row from src0 to temp buffer |
| 642 |
dequantize_row_q(src0_row, wdata, ne00); |
| 643 |
// add src1 |
| 644 |
ggml_vec_acc_f32(ne00, wdata, src1_row); |
| 645 |
// quantize row to dst |
| 646 |
if (quantize_row_q != NULL) { |
| 647 |
quantize_row_q(wdata, dst_row, ne00); |
| 648 |
} else { |
| 649 |
memcpy(dst_row, wdata, ne0*nb0); |
| 650 |
} |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
void ggml_compute_forward_add( |
| 655 |
const ggml_compute_params * params, |
| 656 |
ggml_tensor * dst) { |
| 657 |
|
| 658 |
const ggml_tensor * src0 = dst->src[0]; |
| 659 |
|
| 660 |
switch (src0->type) { |
| 661 |
case GGML_TYPE_F32: |
| 662 |
case GGML_TYPE_F16: |
| 663 |
case GGML_TYPE_BF16: |
| 664 |
{ |
| 665 |
ggml_compute_forward_add_non_quantized(params, dst); |
| 666 |
} break; |
| 667 |
case GGML_TYPE_Q4_0: |
| 668 |
case GGML_TYPE_Q4_1: |
| 669 |
case GGML_TYPE_Q5_0: |
| 670 |
case GGML_TYPE_Q5_1: |
| 671 |
case GGML_TYPE_Q8_0: |
| 672 |
case GGML_TYPE_MXFP4: |
| 673 |
case GGML_TYPE_Q2_K: |
| 674 |
case GGML_TYPE_Q3_K: |
| 675 |
case GGML_TYPE_Q4_K: |
| 676 |
case GGML_TYPE_Q5_K: |
| 677 |
case GGML_TYPE_Q6_K: |
| 678 |
case GGML_TYPE_TQ1_0: |
| 679 |
case GGML_TYPE_TQ2_0: |
| 680 |
case GGML_TYPE_IQ2_XXS: |
| 681 |
case GGML_TYPE_IQ2_XS: |
| 682 |
case GGML_TYPE_IQ3_XXS: |
| 683 |
case GGML_TYPE_IQ1_S: |
| 684 |
case GGML_TYPE_IQ1_M: |
| 685 |
case GGML_TYPE_IQ4_NL: |
| 686 |
case GGML_TYPE_IQ4_XS: |
| 687 |
case GGML_TYPE_IQ3_S: |
| 688 |
case GGML_TYPE_IQ2_S: |
| 689 |
{ |
| 690 |
ggml_compute_forward_add_q_f32(params, dst); |
| 691 |
} break; |
| 692 |
default: |
| 693 |
{ |
| 694 |
GGML_ABORT("fatal error"); |
| 695 |
} |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
// ggml_compute_forward_add_id |
| 700 |
|
| 701 |
static void ggml_compute_forward_add_id_f32( |
| 702 |
const ggml_compute_params * params, |
| 703 |
ggml_tensor * dst) { |
| 704 |
|
| 705 |
const ggml_tensor * src0 = dst->src[0]; |
| 706 |
const ggml_tensor * src1 = dst->src[1]; |
| 707 |
const ggml_tensor * src2 = dst->src[2]; |
| 708 |
|
| 709 |
GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 710 |
GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 711 |
GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 712 |
GGML_ASSERT(src2->type == GGML_TYPE_I32); |
| 713 |
|
| 714 |
GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 715 |
GGML_ASSERT(src1->nb[0] == sizeof(float)); |
| 716 |
|
| 717 |
const int ith = params->ith; |
| 718 |
const int nth = params->nth; |
| 719 |
|
| 720 |
const int nr = ggml_nrows(src0); |
| 721 |
|
| 722 |
GGML_TENSOR_TERNARY_OP_LOCALS |
| 723 |
|
| 724 |
GGML_ASSERT( nb0 == sizeof(float)); |
| 725 |
GGML_ASSERT(nb10 == sizeof(float)); |
| 726 |
|
| 727 |
// rows per thread |
| 728 |
const int dr = (nr + nth - 1)/nth; |
| 729 |
|
| 730 |
// row range for this thread |
| 731 |
const int ir0 = dr*ith; |
| 732 |
const int ir1 = MIN(ir0 + dr, nr); |
| 733 |
|
| 734 |
for (int ir = ir0; ir < ir1; ++ir) { |
| 735 |
// src0 indices |
| 736 |
const int i3 = ir/(ne2*ne1); |
| 737 |
const int i2 = (ir - i3*ne2*ne1)/ne1; |
| 738 |
const int i1 = (ir - i3*ne2*ne1 - i2*ne1); |
| 739 |
|
| 740 |
// src1 indices |
| 741 |
const int i11 = *(int32_t *) ((char *) src2->data + i1*nb20 + i2*nb21); |
| 742 |
|
| 743 |
GGML_ASSERT(i11 >= 0 && i11 < ne11); |
| 744 |
|
| 745 |
ggml_vec_add_f32(ne0, |
| 746 |
(float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ), |
| 747 |
(float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01), |
| 748 |
(float *) ((char *) src1->data + i11*nb11)); |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
void ggml_compute_forward_add_id( |
| 753 |
const ggml_compute_params * params, |
| 754 |
ggml_tensor * dst) { |
| 755 |
|
| 756 |
const ggml_tensor * src0 = dst->src[0]; |
| 757 |
|
| 758 |
switch (src0->type) { |
| 759 |
case GGML_TYPE_F32: |
| 760 |
{ |
| 761 |
ggml_compute_forward_add_id_f32(params, dst); |
| 762 |
} break; |
| 763 |
default: |
| 764 |
{ |
| 765 |
GGML_ABORT("unsupported type for ggml_compute_forward_add_id: %s", ggml_type_name(src0->type)); |
| 766 |
} |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
// ggml_compute_forward_add1 |
| 771 |
|
| 772 |
static void ggml_compute_forward_add1_f32( |
| 773 |
const ggml_compute_params * params, |
| 774 |
ggml_tensor * dst) { |
| 775 |
|
| 776 |
const ggml_tensor * src0 = dst->src[0]; |
| 777 |
const ggml_tensor * src1 = dst->src[1]; |
| 778 |
|
| 779 |
GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 780 |
GGML_ASSERT(ggml_is_scalar(src1)); |
| 781 |
|
| 782 |
const int ith = params->ith; |
| 783 |
const int nth = params->nth; |
| 784 |
|
| 785 |
const int nr = ggml_nrows(src0); |
| 786 |
|
| 787 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 788 |
|
| 789 |
GGML_ASSERT( nb0 == sizeof(float)); |
| 790 |
GGML_ASSERT(nb00 == sizeof(float)); |
| 791 |
|
| 792 |
// rows per thread |
| 793 |
const int dr = (nr + nth - 1)/nth; |
| 794 |
|
| 795 |
// row range for this thread |
| 796 |
const int ir0 = dr*ith; |
| 797 |
const int ir1 = MIN(ir0 + dr, nr); |
| 798 |
|
| 799 |
for (int ir = ir0; ir < ir1; ++ir) { |
| 800 |
// src0 and dst are same shape => same indices |
| 801 |
const int i3 = ir/(ne2*ne1); |
| 802 |
const int i2 = (ir - i3*ne2*ne1)/ne1; |
| 803 |
const int i1 = (ir - i3*ne2*ne1 - i2*ne1); |
| 804 |
|
| 805 |
#ifdef GGML_USE_ACCELERATE |
| 806 |
GGML_UNUSED(ggml_vec_add1_f32); |
| 807 |
|
| 808 |
vDSP_vadd( |
| 809 |
(float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01), 1, |
| 810 |
(float *) ((char *) src1->data), 0, |
| 811 |
(float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ), 1, |
| 812 |
ne0); |
| 813 |
#else |
| 814 |
ggml_vec_add1_f32(ne0, |
| 815 |
(float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ), |
| 816 |
(float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01), |
| 817 |
*(float *) src1->data); |
| 818 |
#endif |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
static void ggml_compute_forward_add1_f16_f32( |
| 823 |
const ggml_compute_params * params, |
| 824 |
ggml_tensor * dst) { |
| 825 |
|
| 826 |
const ggml_tensor * src0 = dst->src[0]; |
| 827 |
const ggml_tensor * src1 = dst->src[1]; |
| 828 |
|
| 829 |
GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 830 |
GGML_ASSERT(ggml_is_scalar(src1)); |
| 831 |
|
| 832 |
// scalar to add |
| 833 |
const float v = *(float *) src1->data; |
| 834 |
|
| 835 |
const int ith = params->ith; |
| 836 |
const int nth = params->nth; |
| 837 |
|
| 838 |
const int nr = ggml_nrows(src0); |
| 839 |
|
| 840 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 841 |
|
| 842 |
GGML_ASSERT(src0->type == GGML_TYPE_F16); |
| 843 |
GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 844 |
GGML_ASSERT(dst->type == GGML_TYPE_F16); |
| 845 |
|
| 846 |
GGML_ASSERT( nb0 == sizeof(ggml_fp16_t)); |
| 847 |
GGML_ASSERT(nb00 == sizeof(ggml_fp16_t)); |
| 848 |
|
| 849 |
// rows per thread |
| 850 |
const int dr = (nr + nth - 1)/nth; |
| 851 |
|
| 852 |
// row range for this thread |
| 853 |
const int ir0 = dr*ith; |
| 854 |
const int ir1 = MIN(ir0 + dr, nr); |
| 855 |
|
| 856 |
for (int ir = ir0; ir < ir1; ++ir) { |
| 857 |
// src0 and dst are same shape => same indices |
| 858 |
const int i3 = ir/(ne2*ne1); |
| 859 |
const int i2 = (ir - i3*ne2*ne1)/ne1; |
| 860 |
const int i1 = (ir - i3*ne2*ne1 - i2*ne1); |
| 861 |
|
| 862 |
ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ); |
| 863 |
ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01); |
| 864 |
for (int i = 0; i < ne0; i++) { |
| 865 |
dst_ptr[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(src0_ptr[i]) + v); |
| 866 |
} |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
static void ggml_compute_forward_add1_f16_f16( |
| 871 |
const ggml_compute_params * params, |
| 872 |
ggml_tensor * dst) { |
| 873 |
|
| 874 |
const ggml_tensor * src0 = dst->src[0]; |
| 875 |
const ggml_tensor * src1 = dst->src[1]; |
| 876 |
|
| 877 |
GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 878 |
GGML_ASSERT(ggml_is_scalar(src1)); |
| 879 |
|
| 880 |
// scalar to add |
| 881 |
const float v = GGML_CPU_FP16_TO_FP32(*(ggml_fp16_t *) src1->data); |
| 882 |
|
| 883 |
const int ith = params->ith; |
| 884 |
const int nth = params->nth; |
| 885 |
|
| 886 |
const int nr = ggml_nrows(src0); |
| 887 |
|
| 888 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 889 |
|
| 890 |
GGML_ASSERT(src0->type == GGML_TYPE_F16); |
| 891 |
GGML_ASSERT(src1->type == GGML_TYPE_F16); |
| 892 |
GGML_ASSERT(dst->type == GGML_TYPE_F16); |
| 893 |
|
| 894 |
GGML_ASSERT( nb0 == sizeof(ggml_fp16_t)); |
| 895 |
GGML_ASSERT(nb00 == sizeof(ggml_fp16_t)); |
| 896 |
|
| 897 |
// rows per thread |
| 898 |
const int dr = (nr + nth - 1)/nth; |
| 899 |
|
| 900 |
// row range for this thread |
| 901 |
const int ir0 = dr*ith; |
| 902 |
const int ir1 = MIN(ir0 + dr, nr); |
| 903 |
|
| 904 |
for (int ir = ir0; ir < ir1; ++ir) { |
| 905 |
// src0 and dst are same shape => same indices |
| 906 |
const int i3 = ir/(ne2*ne1); |
| 907 |
const int i2 = (ir - i3*ne2*ne1)/ne1; |
| 908 |
const int i1 = (ir - i3*ne2*ne1 - i2*ne1); |
| 909 |
|
| 910 |
ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ); |
| 911 |
ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01); |
| 912 |
for (int i = 0; i < ne0; i++) { |
| 913 |
dst_ptr[i] = GGML_CPU_FP32_TO_FP16(GGML_CPU_FP16_TO_FP32(src0_ptr[i]) + v); |
| 914 |
} |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
static void ggml_compute_forward_add1_q_f32( |
| 919 |
const ggml_compute_params * params, |
| 920 |
ggml_tensor * dst) { |
| 921 |
|
| 922 |
const ggml_tensor * src0 = dst->src[0]; |
| 923 |
const ggml_tensor * src1 = dst->src[1]; |
| 924 |
|
| 925 |
GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 926 |
GGML_ASSERT(ggml_is_scalar(src1)); |
| 927 |
|
| 928 |
// scalar to add |
| 929 |
const float v = *(float *) src1->data; |
| 930 |
|
| 931 |
const int ith = params->ith; |
| 932 |
const int nth = params->nth; |
| 933 |
|
| 934 |
const int nr = ggml_nrows(src0); |
| 935 |
|
| 936 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 937 |
|
| 938 |
const ggml_type type = src0->type; |
| 939 |
ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float; |
| 940 |
ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu(type)->from_float; |
| 941 |
|
| 942 |
// we don't support permuted src0 |
| 943 |
GGML_ASSERT(nb00 == ggml_type_size(type)); |
| 944 |
|
| 945 |
// dst cannot be transposed or permuted |
| 946 |
GGML_ASSERT(nb0 <= nb1); |
| 947 |
GGML_ASSERT(nb1 <= nb2); |
| 948 |
GGML_ASSERT(nb2 <= nb3); |
| 949 |
|
| 950 |
GGML_ASSERT(ggml_is_quantized(src0->type)); |
| 951 |
GGML_ASSERT(dst->type == src0->type); |
| 952 |
GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 953 |
|
| 954 |
// rows per thread |
| 955 |
const int dr = (nr + nth - 1)/nth; |
| 956 |
|
| 957 |
// row range for this thread |
| 958 |
const int ir0 = dr*ith; |
| 959 |
const int ir1 = MIN(ir0 + dr, nr); |
| 960 |
|
| 961 |
float * wdata = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32) * ith; |
| 962 |
|
| 963 |
for (int ir = ir0; ir < ir1; ++ir) { |
| 964 |
// src0 and dst are same shape => same indices |
| 965 |
const int i3 = ir/(ne2*ne1); |
| 966 |
const int i2 = (ir - i3*ne2*ne1)/ne1; |
| 967 |
const int i1 = (ir - i3*ne2*ne1 - i2*ne1); |
| 968 |
|
| 969 |
void * src0_row = (void *) ((char *) src0->data + (i1*nb01 + i2*nb02 + i3*nb03)); |
| 970 |
void * dst_row = (void *) ((char *) dst->data + (i1*nb1 + i2*nb2 + i3*nb0 )); |
| 971 |
|
| 972 |
assert(ne0 % 32 == 0); |
| 973 |
|
| 974 |
// unquantize row from src0 to temp buffer |
| 975 |
dequantize_row_q(src0_row, wdata, ne0); |
| 976 |
// add src1 |
| 977 |
ggml_vec_acc1_f32(ne0, wdata, v); |
| 978 |
// quantize row to dst |
| 979 |
quantize_row_q(wdata, dst_row, ne0); |
| 980 |
} |
| 981 |
} |
| 982 |
|
| 983 |
static void ggml_compute_forward_add1_bf16_f32( |
| 984 |
const ggml_compute_params * params, |
| 985 |
ggml_tensor * dst) { |
| 986 |
|
| 987 |
const ggml_tensor * src0 = dst->src[0]; |
| 988 |
const ggml_tensor * src1 = dst->src[1]; |
| 989 |
|
| 990 |
GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 991 |
GGML_ASSERT(ggml_is_scalar(src1)); |
| 992 |
|
| 993 |
// scalar to add |
| 994 |
const float v = *(float *) src1->data; |
| 995 |
|
| 996 |
const int ith = params->ith; |
| 997 |
const int nth = params->nth; |
| 998 |
|
| 999 |
const int nr = ggml_nrows(src0); |
| 1000 |
|
| 1001 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 1002 |
|
| 1003 |
GGML_ASSERT(src0->type == GGML_TYPE_BF16); |
| 1004 |
GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 1005 |
GGML_ASSERT(dst->type == GGML_TYPE_BF16); |
| 1006 |
|
| 1007 |
GGML_ASSERT( nb0 == sizeof(ggml_bf16_t)); |
| 1008 |
GGML_ASSERT(nb00 == sizeof(ggml_bf16_t)); |
| 1009 |
|
| 1010 |
// rows per thread |
| 1011 |
const int dr = (nr + nth - 1)/nth; |
| 1012 |
|
| 1013 |
// row range for this thread |
| 1014 |
const int ir0 = dr*ith; |
| 1015 |
const int ir1 = MIN(ir0 + dr, nr); |
| 1016 |
|
| 1017 |
for (int ir = ir0; ir < ir1; ++ir) { |
| 1018 |
// src0 and dst are same shape => same indices |
| 1019 |
const int i3 = ir/(ne2*ne1); |
| 1020 |
const int i2 = (ir - i3*ne2*ne1)/ne1; |
| 1021 |
const int i1 = (ir - i3*ne2*ne1 - i2*ne1); |
| 1022 |
|
| 1023 |
ggml_bf16_t * dst_ptr = (ggml_bf16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ); |
| 1024 |
ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01); |
| 1025 |
for (int i = 0; i < ne0; i++) { |
| 1026 |
dst_ptr[i] = GGML_FP32_TO_BF16(GGML_BF16_TO_FP32(src0_ptr[i]) + v); |
| 1027 |
} |
| 1028 |
} |
| 1029 |
} |
| 1030 |
|
| 1031 |
static void ggml_compute_forward_add1_bf16_bf16( |
| 1032 |
const ggml_compute_params * params, |
| 1033 |
ggml_tensor * dst) { |
| 1034 |
|
| 1035 |
const ggml_tensor * src0 = dst->src[0]; |
| 1036 |
const ggml_tensor * src1 = dst->src[1]; |
| 1037 |
|
| 1038 |
GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 1039 |
GGML_ASSERT(ggml_is_scalar(src1)); |
| 1040 |
|
| 1041 |
// scalar to add |
| 1042 |
const float v = GGML_BF16_TO_FP32(*(ggml_bf16_t *) src1->data); |
| 1043 |
|
| 1044 |
const int ith = params->ith; |
| 1045 |
const int nth = params->nth; |
| 1046 |
|
| 1047 |
const int nr = ggml_nrows(src0); |
| 1048 |
|
| 1049 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 1050 |
|
| 1051 |
GGML_ASSERT(src0->type == GGML_TYPE_BF16); |
| 1052 |
GGML_ASSERT(src1->type == GGML_TYPE_BF16); |
| 1053 |
GGML_ASSERT(dst->type == GGML_TYPE_BF16); |
| 1054 |
|
| 1055 |
GGML_ASSERT( nb0 == sizeof(ggml_bf16_t)); |
| 1056 |
GGML_ASSERT(nb00 == sizeof(ggml_bf16_t)); |
| 1057 |
|
| 1058 |
// rows per thread |
| 1059 |
const int dr = (nr + nth - 1)/nth; |
| 1060 |
|
| 1061 |
// row range for this thread |
| 1062 |
const int ir0 = dr*ith; |
| 1063 |
const int ir1 = MIN(ir0 + dr, nr); |
| 1064 |
|
| 1065 |
for (int ir = ir0; ir < ir1; ++ir) { |
| 1066 |
// src0 and dst are same shape => same indices |
| 1067 |
const int i3 = ir/(ne2*ne1); |
| 1068 |
const int i2 = (ir - i3*ne2*ne1)/ne1; |
| 1069 |
const int i1 = (ir - i3*ne2*ne1 - i2*ne1); |
| 1070 |
|
| 1071 |
ggml_bf16_t * dst_ptr = (ggml_bf16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 ); |
| 1072 |
ggml_bf16_t * src0_ptr = (ggml_bf16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01); |
| 1073 |
for (int i = 0; i < ne0; i++) { |
| 1074 |
dst_ptr[i] = GGML_FP32_TO_BF16(GGML_BF16_TO_FP32(src0_ptr[i]) + v); |
| 1075 |
} |
| 1076 |
} |
| 1077 |
} |
| 1078 |
|
| 1079 |
void ggml_compute_forward_add1( |
| 1080 |
const ggml_compute_params * params, |
| 1081 |
ggml_tensor * dst) { |
| 1082 |
|
| 1083 |
const ggml_tensor * src0 = dst->src[0]; |
| 1084 |
const ggml_tensor * src1 = dst->src[1]; |
| 1085 |
|
| 1086 |
switch (src0->type) { |
| 1087 |
case GGML_TYPE_F32: |
| 1088 |
{ |
| 1089 |
ggml_compute_forward_add1_f32(params, dst); |
| 1090 |
} break; |
| 1091 |
case GGML_TYPE_F16: |
| 1092 |
{ |
| 1093 |
if (src1->type == GGML_TYPE_F16) { |
| 1094 |
ggml_compute_forward_add1_f16_f16(params, dst); |
| 1095 |
} |
| 1096 |
else if (src1->type == GGML_TYPE_F32) { |
| 1097 |
ggml_compute_forward_add1_f16_f32(params, dst); |
| 1098 |
} |
| 1099 |
else { |
| 1100 |
GGML_ABORT("fatal error"); |
| 1101 |
} |
| 1102 |
} break; |
| 1103 |
case GGML_TYPE_BF16: |
| 1104 |
{ |
| 1105 |
if (src1->type == GGML_TYPE_BF16) { |
| 1106 |
ggml_compute_forward_add1_bf16_bf16(params, dst); |
| 1107 |
} |
| 1108 |
else if (src1->type == GGML_TYPE_F32) { |
| 1109 |
ggml_compute_forward_add1_bf16_f32(params, dst); |
| 1110 |
} |
| 1111 |
else { |
| 1112 |
GGML_ABORT("fatal error"); |
| 1113 |
} |
| 1114 |
} break; |
| 1115 |
case GGML_TYPE_Q4_0: |
| 1116 |
case GGML_TYPE_Q4_1: |
| 1117 |
case GGML_TYPE_Q5_0: |
| 1118 |
case GGML_TYPE_Q5_1: |
| 1119 |
case GGML_TYPE_Q8_0: |
| 1120 |
case GGML_TYPE_Q8_1: |
| 1121 |
case GGML_TYPE_MXFP4: |
| 1122 |
case GGML_TYPE_Q2_K: |
| 1123 |
case GGML_TYPE_Q3_K: |
| 1124 |
case GGML_TYPE_Q4_K: |
| 1125 |
case GGML_TYPE_Q5_K: |
| 1126 |
case GGML_TYPE_Q6_K: |
| 1127 |
case GGML_TYPE_TQ1_0: |
| 1128 |
case GGML_TYPE_TQ2_0: |
| 1129 |
case GGML_TYPE_IQ2_XXS: |
| 1130 |
case GGML_TYPE_IQ2_XS: |
| 1131 |
case GGML_TYPE_IQ3_XXS: |
| 1132 |
case GGML_TYPE_IQ1_S: |
| 1133 |
case GGML_TYPE_IQ1_M: |
| 1134 |
case GGML_TYPE_IQ4_NL: |
| 1135 |
case GGML_TYPE_IQ4_XS: |
| 1136 |
case GGML_TYPE_IQ3_S: |
| 1137 |
case GGML_TYPE_IQ2_S: |
| 1138 |
{ |
| 1139 |
ggml_compute_forward_add1_q_f32(params, dst); |
| 1140 |
} break; |
| 1141 |
default: |
| 1142 |
{ |
| 1143 |
GGML_ABORT("fatal error"); |
| 1144 |
} |
| 1145 |
} |
| 1146 |
} |
| 1147 |
|
| 1148 |
// ggml_compute_forward_acc |
| 1149 |
|
| 1150 |
static void ggml_compute_forward_acc_f32( |
| 1151 |
const ggml_compute_params * params, |
| 1152 |
ggml_tensor * dst) { |
| 1153 |
|
| 1154 |
const ggml_tensor * src0 = dst->src[0]; |
| 1155 |
const ggml_tensor * src1 = dst->src[1]; |
| 1156 |
|
| 1157 |
GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 1158 |
GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0)); |
| 1159 |
|
| 1160 |
// view src0 and dst with these strides and data offset inbytes during acc |
| 1161 |
// nb0 is implicitly element_size because src0 and dst are contiguous |
| 1162 |
size_t nb1 = ((int32_t *) dst->op_params)[0]; |
| 1163 |
size_t nb2 = ((int32_t *) dst->op_params)[1]; |
| 1164 |
size_t nb3 = ((int32_t *) dst->op_params)[2]; |
| 1165 |
size_t offset = ((int32_t *) dst->op_params)[3]; |
| 1166 |
bool inplace = (bool) ((int32_t *) dst->op_params)[4]; |
| 1167 |
|
| 1168 |
if (!inplace) { |
| 1169 |
if (params->ith == 0) { |
| 1170 |
// memcpy needs to be synchronized across threads to avoid race conditions. |
| 1171 |
// => do it in INIT phase |
| 1172 |
memcpy( |
| 1173 |
((char *) dst->data), |
| 1174 |
((char *) src0->data), |
| 1175 |
ggml_nbytes(dst)); |
| 1176 |
} |
| 1177 |
ggml_barrier(params->threadpool); |
| 1178 |
} |
| 1179 |
|
| 1180 |
const int ith = params->ith; |
| 1181 |
const int nth = params->nth; |
| 1182 |
|
| 1183 |
const int nr = ggml_nrows(src1); |
| 1184 |
const int nc = src1->ne[0]; |
| 1185 |
|
| 1186 |
GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) |
| 1187 |
GGML_TENSOR_LOCALS(size_t, nb1, src1, nb) |
| 1188 |
|
| 1189 |
// src0 and dst as viewed during acc |
| 1190 |
const size_t nb0 = ggml_element_size(src0); |
| 1191 |
|
| 1192 |
const size_t nb00 = nb0; |
| 1193 |
const size_t nb01 = nb1; |
| 1194 |
const size_t nb02 = nb2; |
| 1195 |
const size_t nb03 = nb3; |
| 1196 |
|
| 1197 |
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)); |
| 1198 |
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)); |
| 1199 |
|
| 1200 |
GGML_ASSERT(nb10 == sizeof(float)); |
| 1201 |
|
| 1202 |
// rows per thread |
| 1203 |
const int dr = (nr + nth - 1)/nth; |
| 1204 |
|
| 1205 |
// row range for this thread |
| 1206 |
const int ir0 = dr*ith; |
| 1207 |
const int ir1 = MIN(ir0 + dr, nr); |
| 1208 |
|
| 1209 |
for (int ir = ir0; ir < ir1; ++ir) { |
| 1210 |
// src0 and dst are viewed with shape of src1 and offset |
| 1211 |
// => same indices |
| 1212 |
const int i3 = ir/(ne12*ne11); |
| 1213 |
const int i2 = (ir - i3*ne12*ne11)/ne11; |
| 1214 |
const int i1 = (ir - i3*ne12*ne11 - i2*ne11); |
| 1215 |
|
| 1216 |
#ifdef GGML_USE_ACCELERATE |
| 1217 |
vDSP_vadd( |
| 1218 |
(float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + offset), 1, |
| 1219 |
(float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11), 1, |
| 1220 |
(float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset), 1, nc); |
| 1221 |
#else |
| 1222 |
ggml_vec_add_f32(nc, |
| 1223 |
(float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset), |
| 1224 |
(float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + offset), |
| 1225 |
(float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11)); |
| 1226 |
#endif |
| 1227 |
} |
| 1228 |
} |
| 1229 |
|
| 1230 |
void ggml_compute_forward_acc( |
| 1231 |
const ggml_compute_params * params, |
| 1232 |
ggml_tensor * dst) { |
| 1233 |
|
| 1234 |
const ggml_tensor * src0 = dst->src[0]; |
| 1235 |
|
| 1236 |
switch (src0->type) { |
| 1237 |
case GGML_TYPE_F32: |
| 1238 |
{ |
| 1239 |
ggml_compute_forward_acc_f32(params, dst); |
| 1240 |
} break; |
| 1241 |
case GGML_TYPE_F16: |
| 1242 |
case GGML_TYPE_BF16: |
| 1243 |
case GGML_TYPE_Q4_0: |
| 1244 |
case GGML_TYPE_Q4_1: |
| 1245 |
case GGML_TYPE_Q5_0: |
| 1246 |
case GGML_TYPE_Q5_1: |
| 1247 |
case GGML_TYPE_Q8_0: |
| 1248 |
case GGML_TYPE_Q8_1: |
| 1249 |
case GGML_TYPE_MXFP4: |
| 1250 |
case GGML_TYPE_Q2_K: |
| 1251 |
case GGML_TYPE_Q3_K: |
| 1252 |
case GGML_TYPE_Q4_K: |
| 1253 |
case GGML_TYPE_Q5_K: |
| 1254 |
case GGML_TYPE_Q6_K: |
| 1255 |
case GGML_TYPE_TQ1_0: |
| 1256 |
case GGML_TYPE_TQ2_0: |
| 1257 |
case GGML_TYPE_IQ2_XXS: |
| 1258 |
case GGML_TYPE_IQ2_XS: |
| 1259 |
case GGML_TYPE_IQ3_XXS: |
| 1260 |
case GGML_TYPE_IQ1_S: |
| 1261 |
case GGML_TYPE_IQ1_M: |
| 1262 |
case GGML_TYPE_IQ4_NL: |
| 1263 |
case GGML_TYPE_IQ4_XS: |
| 1264 |
case GGML_TYPE_IQ3_S: |
| 1265 |
case GGML_TYPE_IQ2_S: |
| 1266 |
default: |
| 1267 |
{ |
| 1268 |
GGML_ABORT("fatal error"); |
| 1269 |
} |
| 1270 |
} |
| 1271 |
} |
| 1272 |
|
| 1273 |
// ggml_compute_forward_sum |
| 1274 |
|
| 1275 |
static void ggml_compute_forward_sum_f32( |
| 1276 |
const ggml_compute_params * params, |
| 1277 |
ggml_tensor * dst) { |
| 1278 |
|
| 1279 |
const ggml_tensor * src0 = dst->src[0]; |
| 1280 |
|
| 1281 |
if (params->ith != 0) { |
| 1282 |
return; |
| 1283 |
} |
| 1284 |
|
| 1285 |
assert(ggml_is_scalar(dst)); |
| 1286 |
assert(src0->nb[0] == sizeof(float)); |
| 1287 |
|
| 1288 |
GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 1289 |
GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
| 1290 |
|
| 1291 |
ggml_float sum = 0; |
| 1292 |
ggml_float row_sum = 0; |
| 1293 |
|
| 1294 |
for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 1295 |
for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 1296 |
for (int64_t i01 = 0; i01 < ne01; i01++) { |
| 1297 |
ggml_vec_sum_f32_ggf(ne00, |
| 1298 |
&row_sum, |
| 1299 |
(float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03)); |
| 1300 |
sum += row_sum; |
| 1301 |
} |
| 1302 |
} |
| 1303 |
} |
| 1304 |
((float *) dst->data)[0] = sum; |
| 1305 |
} |
| 1306 |
|
| 1307 |
static void ggml_compute_forward_sum_f16( |
| 1308 |
const ggml_compute_params * params, |
| 1309 |
ggml_tensor * dst) { |
| 1310 |
|
| 1311 |
const ggml_tensor * src0 = dst->src[0]; |
| 1312 |
|
| 1313 |
if (params->ith != 0) { |
| 1314 |
return; |
| 1315 |
} |
| 1316 |
|
| 1317 |
assert(ggml_is_scalar(dst)); |
| 1318 |
|
| 1319 |
assert(src0->nb[0] == sizeof(ggml_fp16_t)); |
| 1320 |
|
| 1321 |
GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 1322 |
GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
| 1323 |
|
| 1324 |
float sum = 0; |
| 1325 |
float row_sum = 0; |
| 1326 |
|
| 1327 |
for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 1328 |
for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 1329 |
for (int64_t i01 = 0; i01 < ne01; i01++) { |
| 1330 |
ggml_vec_sum_f16_ggf(ne00, |
| 1331 |
&row_sum, |
| 1332 |
(ggml_fp16_t *) ((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03)); |
| 1333 |
sum += row_sum; |
| 1334 |
} |
| 1335 |
} |
| 1336 |
} |
| 1337 |
((ggml_fp16_t *) dst->data)[0] = GGML_CPU_FP32_TO_FP16(sum); |
| 1338 |
} |
| 1339 |
|
| 1340 |
static void ggml_compute_forward_sum_bf16( |
| 1341 |
const ggml_compute_params * params, |
| 1342 |
ggml_tensor * dst) { |
| 1343 |
|
| 1344 |
const ggml_tensor * src0 = dst->src[0]; |
| 1345 |
|
| 1346 |
if (params->ith != 0) { |
| 1347 |
return; |
| 1348 |
} |
| 1349 |
|
| 1350 |
assert(ggml_is_scalar(dst)); |
| 1351 |
|
| 1352 |
assert(src0->nb[0] == sizeof(ggml_bf16_t)); |
| 1353 |
|
| 1354 |
GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 1355 |
GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
| 1356 |
|
| 1357 |
float sum = 0; |
| 1358 |
float row_sum = 0; |
| 1359 |
|
| 1360 |
for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 1361 |
for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 1362 |
for (int64_t i01 = 0; i01 < ne01; i01++) { |
| 1363 |
ggml_vec_sum_bf16_ggf(ne00, |
| 1364 |
&row_sum, |
| 1365 |
(ggml_bf16_t *) ((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03)); |
| 1366 |
sum += row_sum; |
| 1367 |
} |
| 1368 |
} |
| 1369 |
} |
| 1370 |
((ggml_bf16_t *) dst->data)[0] = GGML_FP32_TO_BF16(sum); |
| 1371 |
} |
| 1372 |
|
| 1373 |
void ggml_compute_forward_sum( |
| 1374 |
const ggml_compute_params * params, |
| 1375 |
ggml_tensor * dst) { |
| 1376 |
|
| 1377 |
const ggml_tensor * src0 = dst->src[0]; |
| 1378 |
|
| 1379 |
switch (src0->type) { |
| 1380 |
case GGML_TYPE_F32: |
| 1381 |
{ |
| 1382 |
ggml_compute_forward_sum_f32(params, dst); |
| 1383 |
} break; |
| 1384 |
case GGML_TYPE_F16: |
| 1385 |
{ |
| 1386 |
ggml_compute_forward_sum_f16(params, dst); |
| 1387 |
} break; |
| 1388 |
case GGML_TYPE_BF16: |
| 1389 |
{ |
| 1390 |
ggml_compute_forward_sum_bf16(params, dst); |
| 1391 |
} break; |
| 1392 |
default: |
| 1393 |
{ |
| 1394 |
GGML_ABORT("fatal error"); |
| 1395 |
} |
| 1396 |
} |
| 1397 |
} |
| 1398 |
|
| 1399 |
// ggml_compute_forward_cumsum |
| 1400 |
|
| 1401 |
static void ggml_compute_forward_cumsum_f32( |
| 1402 |
const ggml_compute_params * params, |
| 1403 |
ggml_tensor * dst) { |
| 1404 |
|
| 1405 |
const ggml_tensor * src0 = dst->src[0]; |
| 1406 |
|
| 1407 |
GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 1408 |
GGML_ASSERT(dst->nb[0] == sizeof(float)); |
| 1409 |
|
| 1410 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 1411 |
|
| 1412 |
GGML_ASSERT(ne0 == ne00); |
| 1413 |
GGML_ASSERT(ne1 == ne01); |
| 1414 |
GGML_ASSERT(ne2 == ne02); |
| 1415 |
GGML_ASSERT(ne3 == ne03); |
| 1416 |
|
| 1417 |
const auto [ir0, ir1] = get_thread_range(params, src0); |
| 1418 |
|
| 1419 |
for (int64_t ir = ir0; ir < ir1; ++ir) { |
| 1420 |
const int64_t i03 = ir/(ne02*ne01); |
| 1421 |
const int64_t i02 = (ir - i03*ne02*ne01)/ne01; |
| 1422 |
const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01); |
| 1423 |
|
| 1424 |
float * src_row = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03); |
| 1425 |
float * dst_row = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3); |
| 1426 |
|
| 1427 |
ggml_vec_cumsum_f32(ne00, dst_row, src_row); |
| 1428 |
} |
| 1429 |
} |
| 1430 |
|
| 1431 |
void ggml_compute_forward_cumsum( |
| 1432 |
const ggml_compute_params * params, |
| 1433 |
ggml_tensor * dst) { |
| 1434 |
|
| 1435 |
const ggml_tensor * src0 = dst->src[0]; |
| 1436 |
|
| 1437 |
switch (src0->type) { |
| 1438 |
case GGML_TYPE_F32: |
| 1439 |
{ |
| 1440 |
ggml_compute_forward_cumsum_f32(params, dst); |
| 1441 |
} break; |
| 1442 |
default: |
| 1443 |
{ |
| 1444 |
GGML_ABORT("fatal error"); |
| 1445 |
} |
| 1446 |
} |
| 1447 |
} |
| 1448 |
|
| 1449 |
// ggml_compute_forward_sum_rows |
| 1450 |
|
| 1451 |
static void ggml_compute_forward_sum_rows_f32( |
| 1452 |
const ggml_compute_params * params, |
| 1453 |
ggml_tensor * dst) { |
| 1454 |
|
| 1455 |
const ggml_tensor * src0 = dst->src[0]; |
| 1456 |
|
| 1457 |
if (params->ith != 0) { |
| 1458 |
return; |
| 1459 |
} |
| 1460 |
|
| 1461 |
GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 1462 |
GGML_ASSERT(dst->nb[0] == sizeof(float)); |
| 1463 |
|
| 1464 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 1465 |
|
| 1466 |
GGML_ASSERT(ne0 == 1); |
| 1467 |
GGML_ASSERT(ne1 == ne01); |
| 1468 |
GGML_ASSERT(ne2 == ne02); |
| 1469 |
GGML_ASSERT(ne3 == ne03); |
| 1470 |
|
| 1471 |
for (int64_t i3 = 0; i3 < ne03; i3++) { |
| 1472 |
for (int64_t i2 = 0; i2 < ne02; i2++) { |
| 1473 |
for (int64_t i1 = 0; i1 < ne01; i1++) { |
| 1474 |
float * src_row = (float *) ((char *) src0->data + i1*nb01 + i2*nb02 + i3*nb03); |
| 1475 |
float * dst_row = (float *) ((char *) dst->data + i1*nb1 + i2*nb2 + i3*nb3); |
| 1476 |
float row_sum = 0; |
| 1477 |
ggml_vec_sum_f32(ne00, &row_sum, src_row); |
| 1478 |
dst_row[0] = row_sum; |
| 1479 |
} |
| 1480 |
} |
| 1481 |
} |
| 1482 |
} |
| 1483 |
|
| 1484 |
void ggml_compute_forward_sum_rows( |
| 1485 |
const ggml_compute_params * params, |
| 1486 |
ggml_tensor * dst) { |
| 1487 |
|
| 1488 |
const ggml_tensor * src0 = dst->src[0]; |
| 1489 |
|
| 1490 |
switch (src0->type) { |
| 1491 |
case GGML_TYPE_F32: |
| 1492 |
{ |
| 1493 |
ggml_compute_forward_sum_rows_f32(params, dst); |
| 1494 |
} break; |
| 1495 |
default: |
| 1496 |
{ |
| 1497 |
GGML_ABORT("fatal error"); |
| 1498 |
} |
| 1499 |
} |
| 1500 |
} |
| 1501 |
|
| 1502 |
// ggml_compute_forward_mean |
| 1503 |
|
| 1504 |
static void ggml_compute_forward_mean_f32( |
| 1505 |
const ggml_compute_params * params, |
| 1506 |
ggml_tensor * dst) { |
| 1507 |
|
| 1508 |
const ggml_tensor * src0 = dst->src[0]; |
| 1509 |
|
| 1510 |
if (params->ith != 0) { |
| 1511 |
return; |
| 1512 |
} |
| 1513 |
|
| 1514 |
assert(src0->nb[0] == sizeof(float)); |
| 1515 |
|
| 1516 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 1517 |
|
| 1518 |
assert(ne0 == 1); |
| 1519 |
assert(ne1 == ne01); |
| 1520 |
assert(ne2 == ne02); |
| 1521 |
assert(ne3 == ne03); |
| 1522 |
|
| 1523 |
GGML_UNUSED(ne0); |
| 1524 |
GGML_UNUSED(ne1); |
| 1525 |
GGML_UNUSED(ne2); |
| 1526 |
GGML_UNUSED(ne3); |
| 1527 |
|
| 1528 |
for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 1529 |
for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 1530 |
for (int64_t i01 = 0; i01 < ne01; i01++) { |
| 1531 |
ggml_vec_sum_f32(ne00, |
| 1532 |
(float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3), |
| 1533 |
(float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03)); |
| 1534 |
|
| 1535 |
*(float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3) /= (float) ne00; |
| 1536 |
} |
| 1537 |
} |
| 1538 |
} |
| 1539 |
} |
| 1540 |
|
| 1541 |
void ggml_compute_forward_mean( |
| 1542 |
const ggml_compute_params * params, |
| 1543 |
ggml_tensor * dst) { |
| 1544 |
|
| 1545 |
const ggml_tensor * src0 = dst->src[0]; |
| 1546 |
|
| 1547 |
switch (src0->type) { |
| 1548 |
case GGML_TYPE_F32: |
| 1549 |
{ |
| 1550 |
ggml_compute_forward_mean_f32(params, dst); |
| 1551 |
} break; |
| 1552 |
default: |
| 1553 |
{ |
| 1554 |
GGML_ABORT("fatal error"); |
| 1555 |
} |
| 1556 |
} |
| 1557 |
} |
| 1558 |
|
| 1559 |
// ggml_compute_forward_argmax |
| 1560 |
|
| 1561 |
static void ggml_compute_forward_argmax_f32( |
| 1562 |
const ggml_compute_params * params, |
| 1563 |
ggml_tensor * dst) { |
| 1564 |
|
| 1565 |
const ggml_tensor * src0 = dst->src[0]; |
| 1566 |
|
| 1567 |
if (params->ith != 0) { |
| 1568 |
return; |
| 1569 |
} |
| 1570 |
|
| 1571 |
assert(src0->nb[0] == sizeof(float)); |
| 1572 |
assert(dst->nb[0] == sizeof(float)); |
| 1573 |
|
| 1574 |
const int64_t ne00 = src0->ne[0]; |
| 1575 |
const int64_t ne01 = src0->ne[1]; |
| 1576 |
|
| 1577 |
const size_t nb01 = src0->nb[1]; |
| 1578 |
const size_t nb0 = dst->nb[0]; |
| 1579 |
|
| 1580 |
for (int64_t i1 = 0; i1 < ne01; i1++) { |
| 1581 |
float * src = (float *) ((char *) src0->data + i1*nb01); |
| 1582 |
int32_t * dst_ = (int32_t *) ((char *) dst->data + i1*nb0); |
| 1583 |
int v = 0; |
| 1584 |
ggml_vec_argmax_f32(ne00, &v, src); |
| 1585 |
dst_[0] = v; |
| 1586 |
} |
| 1587 |
} |
| 1588 |
|
| 1589 |
void ggml_compute_forward_argmax( |
| 1590 |
const ggml_compute_params * params, |
| 1591 |
ggml_tensor * dst) { |
| 1592 |
|
| 1593 |
const ggml_tensor * src0 = dst->src[0]; |
| 1594 |
|
| 1595 |
switch (src0->type) { |
| 1596 |
case GGML_TYPE_F32: |
| 1597 |
{ |
| 1598 |
ggml_compute_forward_argmax_f32(params, dst); |
| 1599 |
} break; |
| 1600 |
default: |
| 1601 |
{ |
| 1602 |
GGML_ABORT("fatal error"); |
| 1603 |
} |
| 1604 |
} |
| 1605 |
} |
| 1606 |
|
| 1607 |
// ggml_compute_forward_count_equal |
| 1608 |
|
| 1609 |
static void ggml_compute_forward_count_equal_i32( |
| 1610 |
const ggml_compute_params * params, |
| 1611 |
ggml_tensor * dst) { |
| 1612 |
|
| 1613 |
const ggml_tensor * src0 = dst->src[0]; |
| 1614 |
const ggml_tensor * src1 = dst->src[1]; |
| 1615 |
|
| 1616 |
GGML_TENSOR_BINARY_OP_LOCALS; |
| 1617 |
|
| 1618 |
GGML_ASSERT(src0->type == GGML_TYPE_I32); |
| 1619 |
GGML_ASSERT(src1->type == GGML_TYPE_I32); |
| 1620 |
GGML_ASSERT(ggml_are_same_shape(src0, src1)); |
| 1621 |
GGML_ASSERT(ggml_is_scalar(dst)); |
| 1622 |
GGML_ASSERT(dst->type == GGML_TYPE_I64); |
| 1623 |
|
| 1624 |
const int64_t nr = ggml_nrows(src0); |
| 1625 |
|
| 1626 |
const int ith = params->ith; |
| 1627 |
const int nth = params->nth; |
| 1628 |
|
| 1629 |
int64_t * sums = (int64_t *) params->wdata; |
| 1630 |
int64_t sum_thread = 0; |
| 1631 |
|
| 1632 |
// rows per thread |
| 1633 |
const int64_t dr = (nr + nth - 1)/nth; |
| 1634 |
|
| 1635 |
// row range for this thread |
| 1636 |
const int64_t ir0 = dr*ith; |
| 1637 |
const int64_t ir1 = MIN(ir0 + dr, nr); |
| 1638 |
|
| 1639 |
for (int64_t ir = ir0; ir < ir1; ++ir) { |
| 1640 |
const int64_t i03 = ir / (ne02*ne01); |
| 1641 |
const int64_t i02 = (ir - i03*ne03) / ne01; |
| 1642 |
const int64_t i01 = ir - i03*ne03 - i02*ne02; |
| 1643 |
|
| 1644 |
const char * data0 = (const char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01; |
| 1645 |
const char * data1 = (const char *) src1->data + i03*nb13 + i02*nb12 + i01*nb11; |
| 1646 |
|
| 1647 |
for (int64_t i00 = 0; i00 < ne00; ++i00) { |
| 1648 |
const int32_t val0 = *((const int32_t *) (data0 + i00*nb00)); |
| 1649 |
const int32_t val1 = *((const int32_t *) (data1 + i00*nb10)); |
| 1650 |
|
| 1651 |
sum_thread += val0 == val1; |
| 1652 |
} |
| 1653 |
} |
| 1654 |
if (ith != 0) { |
| 1655 |
sums[ith] = sum_thread; |
| 1656 |
} |
| 1657 |
ggml_barrier(params->threadpool); |
| 1658 |
|
| 1659 |
if (ith != 0) { |
| 1660 |
return; |
| 1661 |
} |
| 1662 |
|
| 1663 |
for (int ith_other = 1; ith_other < nth; ++ith_other) { |
| 1664 |
sum_thread += sums[ith_other]; |
| 1665 |
} |
| 1666 |
*((int64_t *) dst->data) = sum_thread; |
| 1667 |
} |
| 1668 |
|
| 1669 |
void ggml_compute_forward_count_equal( |
| 1670 |
const ggml_compute_params * params, |
| 1671 |
ggml_tensor * dst) { |
| 1672 |
|
| 1673 |
const ggml_tensor * src0 = dst->src[0]; |
| 1674 |
|
| 1675 |
switch (src0->type) { |
| 1676 |
case GGML_TYPE_I32: |
| 1677 |
{ |
| 1678 |
ggml_compute_forward_count_equal_i32(params, dst); |
| 1679 |
} break; |
| 1680 |
default: |
| 1681 |
{ |
| 1682 |
GGML_ABORT("fatal error"); |
| 1683 |
} |
| 1684 |
} |
| 1685 |
} |
| 1686 |
|
| 1687 |
// ggml_compute_forward_repeat |
| 1688 |
|
| 1689 |
static void ggml_compute_forward_repeat_f32( |
| 1690 |
const ggml_compute_params * params, |
| 1691 |
ggml_tensor * dst) { |
| 1692 |
|
| 1693 |
const ggml_tensor * src0 = dst->src[0]; |
| 1694 |
|
| 1695 |
if (params->ith != 0) { |
| 1696 |
return; |
| 1697 |
} |
| 1698 |
|
| 1699 |
GGML_ASSERT(ggml_can_repeat(src0, dst)); |
| 1700 |
|
| 1701 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 1702 |
|
| 1703 |
// guaranteed to be an integer due to the check in ggml_can_repeat |
| 1704 |
const int nr0 = (int)(ne0/ne00); |
| 1705 |
const int nr1 = (int)(ne1/ne01); |
| 1706 |
const int nr2 = (int)(ne2/ne02); |
| 1707 |
const int nr3 = (int)(ne3/ne03); |
| 1708 |
|
| 1709 |
// TODO: support for transposed / permuted tensors |
| 1710 |
GGML_ASSERT(nb0 == sizeof(float)); |
| 1711 |
GGML_ASSERT(nb00 == sizeof(float)); |
| 1712 |
|
| 1713 |
// TODO: maybe this is not optimal? |
| 1714 |
for (int i3 = 0; i3 < nr3; i3++) { |
| 1715 |
for (int k3 = 0; k3 < ne03; k3++) { |
| 1716 |
for (int i2 = 0; i2 < nr2; i2++) { |
| 1717 |
for (int k2 = 0; k2 < ne02; k2++) { |
| 1718 |
for (int i1 = 0; i1 < nr1; i1++) { |
| 1719 |
for (int k1 = 0; k1 < ne01; k1++) { |
| 1720 |
for (int i0 = 0; i0 < nr0; i0++) { |
| 1721 |
ggml_vec_cpy_f32(ne00, |
| 1722 |
(float *) ((char *) dst->data + (i3*ne03 + k3)*nb3 + (i2*ne02 + k2)*nb2 + (i1*ne01 + k1)*nb1 + (i0*ne00)*nb0), |
| 1723 |
(float *) ((char *) src0->data + ( k3)*nb03 + ( k2)*nb02 + ( k1)*nb01)); |
| 1724 |
} |
| 1725 |
} |
| 1726 |
} |
| 1727 |
} |
| 1728 |
} |
| 1729 |
} |
| 1730 |
} |
| 1731 |
} |
| 1732 |
|
| 1733 |
static void ggml_compute_forward_repeat_f16( |
| 1734 |
const ggml_compute_params * params, |
| 1735 |
ggml_tensor * dst) { |
| 1736 |
|
| 1737 |
const ggml_tensor * src0 = dst->src[0]; |
| 1738 |
|
| 1739 |
if (params->ith != 0) { |
| 1740 |
return; |
| 1741 |
} |
| 1742 |
|
| 1743 |
GGML_ASSERT(ggml_can_repeat(src0, dst)); |
| 1744 |
|
| 1745 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 1746 |
|
| 1747 |
// guaranteed to be an integer due to the check in ggml_can_repeat |
| 1748 |
const int nr0 = (int)(ne0/ne00); |
| 1749 |
const int nr1 = (int)(ne1/ne01); |
| 1750 |
const int nr2 = (int)(ne2/ne02); |
| 1751 |
const int nr3 = (int)(ne3/ne03); |
| 1752 |
|
| 1753 |
// TODO: support for transposed / permuted tensors |
| 1754 |
GGML_ASSERT(nb0 == sizeof(ggml_fp16_t)); |
| 1755 |
GGML_ASSERT(nb00 == sizeof(ggml_fp16_t)); |
| 1756 |
|
| 1757 |
// TODO: maybe this is not optimal? |
| 1758 |
for (int i3 = 0; i3 < nr3; i3++) { |
| 1759 |
for (int k3 = 0; k3 < ne03; k3++) { |
| 1760 |
for (int i2 = 0; i2 < nr2; i2++) { |
| 1761 |
for (int k2 = 0; k2 < ne02; k2++) { |
| 1762 |
for (int i1 = 0; i1 < nr1; i1++) { |
| 1763 |
for (int k1 = 0; k1 < ne01; k1++) { |
| 1764 |
for (int i0 = 0; i0 < nr0; i0++) { |
| 1765 |
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); |
| 1766 |
ggml_fp16_t * x = (ggml_fp16_t *) ((char *) src0->data + ( k3)*nb03 + ( k2)*nb02 + ( k1)*nb01); |
| 1767 |
// ggml_vec_cpy_f16(ne00, y, x) |
| 1768 |
for (int i = 0; i < ne00; ++i) { |
| 1769 |
y[i] = x[i]; |
| 1770 |
} |
| 1771 |
} |
| 1772 |
} |
| 1773 |
} |
| 1774 |
} |
| 1775 |
} |
| 1776 |
} |
| 1777 |
} |
| 1778 |
} |
| 1779 |
|
| 1780 |
void ggml_compute_forward_repeat( |
| 1781 |
const ggml_compute_params * params, |
| 1782 |
ggml_tensor * dst) { |
| 1783 |
|
| 1784 |
const ggml_tensor * src0 = dst->src[0]; |
| 1785 |
|
| 1786 |
switch (src0->type) { |
| 1787 |
case GGML_TYPE_F16: |
| 1788 |
case GGML_TYPE_BF16: |
| 1789 |
case GGML_TYPE_I16: |
| 1790 |
{ |
| 1791 |
ggml_compute_forward_repeat_f16(params, dst); |
| 1792 |
} break; |
| 1793 |
case GGML_TYPE_F32: |
| 1794 |
case GGML_TYPE_I32: |
| 1795 |
{ |
| 1796 |
ggml_compute_forward_repeat_f32(params, dst); |
| 1797 |
} break; |
| 1798 |
// TODO: templateify the implemenation and support for I64 |
| 1799 |
// ref https://github.com/ggml-org/llama.cpp/pull/14274#discussion_r2169492225 |
| 1800 |
//case GGML_TYPE_I64: |
| 1801 |
// { |
| 1802 |
// ggml_compute_forward_repeat_i64(params, dst); |
| 1803 |
// } break; |
| 1804 |
default: |
| 1805 |
{ |
| 1806 |
GGML_ABORT("fatal error"); |
| 1807 |
} |
| 1808 |
} |
| 1809 |
} |
| 1810 |
|
| 1811 |
// ggml_compute_forward_repeat_back |
| 1812 |
|
| 1813 |
static void ggml_compute_forward_repeat_back_f32( |
| 1814 |
const ggml_compute_params * params, |
| 1815 |
ggml_tensor * dst) { |
| 1816 |
|
| 1817 |
const ggml_tensor * src0 = dst->src[0]; |
| 1818 |
|
| 1819 |
if (params->ith != 0) { |
| 1820 |
return; |
| 1821 |
} |
| 1822 |
|
| 1823 |
GGML_ASSERT(ggml_can_repeat(dst, src0)); |
| 1824 |
|
| 1825 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 1826 |
|
| 1827 |
// guaranteed to be an integer due to the check in ggml_can_repeat |
| 1828 |
const int nr0 = (int)(ne00/ne0); |
| 1829 |
const int nr1 = (int)(ne01/ne1); |
| 1830 |
const int nr2 = (int)(ne02/ne2); |
| 1831 |
const int nr3 = (int)(ne03/ne3); |
| 1832 |
|
| 1833 |
// TODO: support for transposed / permuted tensors |
| 1834 |
GGML_ASSERT(nb0 == sizeof(float)); |
| 1835 |
GGML_ASSERT(nb00 == sizeof(float)); |
| 1836 |
|
| 1837 |
if (ggml_is_contiguous(dst)) { |
| 1838 |
ggml_vec_set_f32(ne0*ne1*ne2*ne3, (float *)dst->data, 0); |
| 1839 |
} else { |
| 1840 |
for (int k3 = 0; k3 < ne3; k3++) { |
| 1841 |
for (int k2 = 0; k2 < ne2; k2++) { |
| 1842 |
for (int k1 = 0; k1 < ne1; k1++) { |
| 1843 |
ggml_vec_set_f32(ne0, |
| 1844 |
(float *) ((char *) dst->data + k1*nb1 + k2*nb2 + k3*nb3), |
| 1845 |
0); |
| 1846 |
} |
| 1847 |
} |
| 1848 |
} |
| 1849 |
} |
| 1850 |
|
| 1851 |
// TODO: maybe this is not optimal? |
| 1852 |
for (int i3 = 0; i3 < nr3; i3++) { |
| 1853 |
for (int k3 = 0; k3 < ne3; k3++) { |
| 1854 |
for (int i2 = 0; i2 < nr2; i2++) { |
| 1855 |
for (int k2 = 0; k2 < ne2; k2++) { |
| 1856 |
for (int i1 = 0; i1 < nr1; i1++) { |
| 1857 |
for (int k1 = 0; k1 < ne1; k1++) { |
| 1858 |
for (int i0 = 0; i0 < nr0; i0++) { |
| 1859 |
ggml_vec_acc_f32(ne0, |
| 1860 |
(float *) ((char *) dst->data + ( k3)*nb3 + ( k2)*nb2 + ( k1)*nb1), |
| 1861 |
(float *) ((char *) src0->data + (i3*ne3 + k3)*nb03 + (i2*ne2 + k2)*nb02 + (i1*ne1 + k1)*nb01 + (i0*ne0)*nb00)); |
| 1862 |
} |
| 1863 |
} |
| 1864 |
} |
| 1865 |
} |
| 1866 |
} |
| 1867 |
} |
| 1868 |
} |
| 1869 |
} |
| 1870 |
|
| 1871 |
void ggml_compute_forward_repeat_back( |
| 1872 |
const ggml_compute_params * params, |
| 1873 |
ggml_tensor * dst) { |
| 1874 |
|
| 1875 |
const ggml_tensor * src0 = dst->src[0]; |
| 1876 |
|
| 1877 |
switch (src0->type) { |
| 1878 |
case GGML_TYPE_F32: |
| 1879 |
{ |
| 1880 |
ggml_compute_forward_repeat_back_f32(params, dst); |
| 1881 |
} break; |
| 1882 |
default: |
| 1883 |
{ |
| 1884 |
GGML_ABORT("fatal error"); |
| 1885 |
} |
| 1886 |
} |
| 1887 |
} |
| 1888 |
|
| 1889 |
// ggml_compute_forward_concat |
| 1890 |
|
| 1891 |
static void ggml_compute_forward_concat_any( |
| 1892 |
const ggml_compute_params * params, |
| 1893 |
ggml_tensor * dst) { |
| 1894 |
|
| 1895 |
const ggml_tensor * src0 = dst->src[0]; |
| 1896 |
const ggml_tensor * src1 = dst->src[1]; |
| 1897 |
|
| 1898 |
const size_t len = ggml_type_size(src0->type); |
| 1899 |
|
| 1900 |
const int ith = params->ith; |
| 1901 |
const int nth = params->nth; |
| 1902 |
|
| 1903 |
GGML_TENSOR_BINARY_OP_LOCALS |
| 1904 |
|
| 1905 |
const int32_t dim = ggml_get_op_params_i32(dst, 0); |
| 1906 |
|
| 1907 |
GGML_ASSERT(dim >= 0 && dim < 4); |
| 1908 |
|
| 1909 |
int64_t o[4] = {0, 0, 0, 0}; |
| 1910 |
o[dim] = src0->ne[dim]; |
| 1911 |
|
| 1912 |
const char * x; |
| 1913 |
|
| 1914 |
// TODO: smarter multi-theading |
| 1915 |
for (int i3 = 0; i3 < ne3; i3++) { |
| 1916 |
for (int i2 = ith; i2 < ne2; i2 += nth) { |
| 1917 |
for (int i1 = 0; i1 < ne1; i1++) { |
| 1918 |
for (int i0 = 0; i0 < ne0; i0++) { |
| 1919 |
if (i0 < ne00 && i1 < ne01 && i2 < ne02 && i3 < ne03) { |
| 1920 |
x = (const char *)src0->data + (i0 )*nb00 + (i1 )*nb01 + (i2 )*nb02 + (i3 )*nb03; |
| 1921 |
} else { |
| 1922 |
x = (const char *)src1->data + (i0 - o[0])*nb10 + (i1 - o[1])*nb11 + (i2 - o[2])*nb12 + (i3 - o[3])*nb13; |
| 1923 |
} |
| 1924 |
|
| 1925 |
char * y = (char *)dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3; |
| 1926 |
|
| 1927 |
memcpy(y, x, len); |
| 1928 |
} |
| 1929 |
} |
| 1930 |
} |
| 1931 |
} |
| 1932 |
} |
| 1933 |
|
| 1934 |
static void ggml_compute_forward_concat_i8( |
| 1935 |
const ggml_compute_params * params, |
| 1936 |
ggml_tensor * dst) { |
| 1937 |
|
| 1938 |
const ggml_tensor * src0 = dst->src[0]; |
| 1939 |
const ggml_tensor * src1 = dst->src[1]; |
| 1940 |
|
| 1941 |
GGML_ASSERT(ggml_type_size(src0->type) == sizeof(int8_t)); |
| 1942 |
|
| 1943 |
const int ith = params->ith; |
| 1944 |
const int nth = params->nth; |
| 1945 |
|
| 1946 |
GGML_TENSOR_BINARY_OP_LOCALS |
| 1947 |
|
| 1948 |
const int32_t dim = ggml_get_op_params_i32(dst, 0); |
| 1949 |
|
| 1950 |
GGML_ASSERT(dim >= 0 && dim < 4); |
| 1951 |
|
| 1952 |
int64_t o[4] = {0, 0, 0, 0}; |
| 1953 |
o[dim] = src0->ne[dim]; |
| 1954 |
|
| 1955 |
const int8_t * x; |
| 1956 |
|
| 1957 |
// TODO: smarter multi-theading |
| 1958 |
for (int i3 = 0; i3 < ne3; i3++) { |
| 1959 |
for (int i2 = ith; i2 < ne2; i2 += nth) { |
| 1960 |
for (int i1 = 0; i1 < ne1; i1++) { |
| 1961 |
for (int i0 = 0; i0 < ne0; i0++) { |
| 1962 |
if (i0 < ne00 && i1 < ne01 && i2 < ne02 && i3 < ne03) { |
| 1963 |
x = (const int8_t *) ((const char *)src0->data + (i0 )*nb00 + (i1 )*nb01 + (i2 )*nb02 + (i3 )*nb03); |
| 1964 |
} else { |
| 1965 |
x = (const int8_t *) ((const char *)src1->data + (i0 - o[0])*nb10 + (i1 - o[1])*nb11 + (i2 - o[2])*nb12 + (i3 - o[3])*nb13); |
| 1966 |
} |
| 1967 |
|
| 1968 |
int8_t * y = (int8_t *)((char *)dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3); |
| 1969 |
|
| 1970 |
*y = *x; |
| 1971 |
} |
| 1972 |
} |
| 1973 |
} |
| 1974 |
} |
| 1975 |
} |
| 1976 |
|
| 1977 |
static void ggml_compute_forward_concat_f16( |
| 1978 |
const ggml_compute_params * params, |
| 1979 |
ggml_tensor * dst) { |
| 1980 |
|
| 1981 |
const ggml_tensor * src0 = dst->src[0]; |
| 1982 |
const ggml_tensor * src1 = dst->src[1]; |
| 1983 |
|
| 1984 |
GGML_ASSERT(ggml_type_size(src0->type) == sizeof(ggml_fp16_t)); |
| 1985 |
|
| 1986 |
const int ith = params->ith; |
| 1987 |
const int nth = params->nth; |
| 1988 |
|
| 1989 |
GGML_TENSOR_BINARY_OP_LOCALS |
| 1990 |
|
| 1991 |
const int32_t dim = ggml_get_op_params_i32(dst, 0); |
| 1992 |
|
| 1993 |
GGML_ASSERT(dim >= 0 && dim < 4); |
| 1994 |
|
| 1995 |
int64_t o[4] = {0, 0, 0, 0}; |
| 1996 |
o[dim] = src0->ne[dim]; |
| 1997 |
|
| 1998 |
const ggml_fp16_t * x; |
| 1999 |
|
| 2000 |
// TODO: smarter multi-theading |
| 2001 |
for (int i3 = 0; i3 < ne3; i3++) { |
| 2002 |
for (int i2 = ith; i2 < ne2; i2 += nth) { |
| 2003 |
for (int i1 = 0; i1 < ne1; i1++) { |
| 2004 |
for (int i0 = 0; i0 < ne0; i0++) { |
| 2005 |
if (i0 < ne00 && i1 < ne01 && i2 < ne02 && i3 < ne03) { |
| 2006 |
x = (const ggml_fp16_t *) ((const char *)src0->data + (i0 )*nb00 + (i1 )*nb01 + (i2 )*nb02 + (i3 )*nb03); |
| 2007 |
} else { |
| 2008 |
x = (const ggml_fp16_t *) ((const char *)src1->data + (i0 - o[0])*nb10 + (i1 - o[1])*nb11 + (i2 - o[2])*nb12 + (i3 - o[3])*nb13); |
| 2009 |
} |
| 2010 |
|
| 2011 |
ggml_fp16_t * y = (ggml_fp16_t *)((char *)dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3); |
| 2012 |
|
| 2013 |
*y = *x; |
| 2014 |
} |
| 2015 |
} |
| 2016 |
} |
| 2017 |
} |
| 2018 |
} |
| 2019 |
|
| 2020 |
static void ggml_compute_forward_concat_f32( |
| 2021 |
const ggml_compute_params * params, |
| 2022 |
ggml_tensor * dst) { |
| 2023 |
|
| 2024 |
const ggml_tensor * src0 = dst->src[0]; |
| 2025 |
const ggml_tensor * src1 = dst->src[1]; |
| 2026 |
|
| 2027 |
GGML_ASSERT(ggml_type_size(src0->type) == sizeof(float)); |
| 2028 |
|
| 2029 |
const int ith = params->ith; |
| 2030 |
const int nth = params->nth; |
| 2031 |
|
| 2032 |
GGML_TENSOR_BINARY_OP_LOCALS |
| 2033 |
|
| 2034 |
const int32_t dim = ggml_get_op_params_i32(dst, 0); |
| 2035 |
|
| 2036 |
GGML_ASSERT(dim >= 0 && dim < 4); |
| 2037 |
|
| 2038 |
int64_t o[4] = {0, 0, 0, 0}; |
| 2039 |
o[dim] = src0->ne[dim]; |
| 2040 |
|
| 2041 |
const float * x; |
| 2042 |
|
| 2043 |
// TODO: smarter multi-theading |
| 2044 |
for (int i3 = 0; i3 < ne3; i3++) { |
| 2045 |
for (int i2 = ith; i2 < ne2; i2 += nth) { |
| 2046 |
for (int i1 = 0; i1 < ne1; i1++) { |
| 2047 |
for (int i0 = 0; i0 < ne0; i0++) { |
| 2048 |
if (i0 < ne00 && i1 < ne01 && i2 < ne02 && i3 < ne03) { |
| 2049 |
x = (const float *) ((const char *)src0->data + (i0 )*nb00 + (i1 )*nb01 + (i2 )*nb02 + (i3 )*nb03); |
| 2050 |
} else { |
| 2051 |
x = (const float *) ((const char *)src1->data + (i0 - o[0])*nb10 + (i1 - o[1])*nb11 + (i2 - o[2])*nb12 + (i3 - o[3])*nb13); |
| 2052 |
} |
| 2053 |
|
| 2054 |
float * y = (float *)((char *)dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3); |
| 2055 |
|
| 2056 |
*y = *x; |
| 2057 |
} |
| 2058 |
} |
| 2059 |
} |
| 2060 |
} |
| 2061 |
} |
| 2062 |
|
| 2063 |
void ggml_compute_forward_concat( |
| 2064 |
const ggml_compute_params * params, |
| 2065 |
ggml_tensor * dst) { |
| 2066 |
|
| 2067 |
const ggml_tensor * src0 = dst->src[0]; |
| 2068 |
|
| 2069 |
switch (src0->type) { |
| 2070 |
case GGML_TYPE_F16: |
| 2071 |
case GGML_TYPE_BF16: |
| 2072 |
case GGML_TYPE_I16: |
| 2073 |
{ |
| 2074 |
ggml_compute_forward_concat_f16(params, dst); |
| 2075 |
} break; |
| 2076 |
case GGML_TYPE_I8: |
| 2077 |
{ |
| 2078 |
ggml_compute_forward_concat_i8(params, dst); |
| 2079 |
} break; |
| 2080 |
case GGML_TYPE_F32: |
| 2081 |
case GGML_TYPE_I32: |
| 2082 |
{ |
| 2083 |
ggml_compute_forward_concat_f32(params, dst); |
| 2084 |
} break; |
| 2085 |
default: |
| 2086 |
{ |
| 2087 |
ggml_compute_forward_concat_any(params, dst); |
| 2088 |
} |
| 2089 |
} |
| 2090 |
} |
| 2091 |
|
| 2092 |
// ggml_compute_forward_gelu |
| 2093 |
|
| 2094 |
static void ggml_compute_forward_gelu_f32( |
| 2095 |
const ggml_compute_params * params, |
| 2096 |
ggml_tensor * dst) { |
| 2097 |
|
| 2098 |
const ggml_tensor * src0 = dst->src[0]; |
| 2099 |
|
| 2100 |
assert(ggml_is_contiguous_rows(src0)); |
| 2101 |
assert(ggml_are_same_shape(src0, dst)); |
| 2102 |
|
| 2103 |
GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 2104 |
GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
| 2105 |
GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 2106 |
GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 2107 |
|
| 2108 |
const int ith = params->ith; |
| 2109 |
const int nth = params->nth; |
| 2110 |
|
| 2111 |
const int nc = src0->ne[0]; |
| 2112 |
const int nr = ggml_nrows(src0); |
| 2113 |
|
| 2114 |
// rows per thread |
| 2115 |
const int dr = (nr + nth - 1)/nth; |
| 2116 |
|
| 2117 |
// row range for this thread |
| 2118 |
const int ir0 = dr*ith; |
| 2119 |
const int ir1 = MIN(ir0 + dr, nr); |
| 2120 |
|
| 2121 |
for (int ir = ir0; ir < ir1; ++ir) { |
| 2122 |
const int i3 = ir/(ne02*ne01); |
| 2123 |
const int i2 = (ir - i3*ne02*ne01)/ne01; |
| 2124 |
const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
| 2125 |
|
| 2126 |
ggml_vec_gelu_f32(nc, |
| 2127 |
(float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
| 2128 |
(float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
| 2129 |
|
| 2130 |
#ifndef NDEBUG |
| 2131 |
for (int k = 0; k < nc; k++) { |
| 2132 |
const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 2133 |
GGML_UNUSED(x); |
| 2134 |
assert(!isnan(x)); |
| 2135 |
assert(!isinf(x)); |
| 2136 |
} |
| 2137 |
#endif |
| 2138 |
} |
| 2139 |
} |
| 2140 |
|
| 2141 |
static void ggml_compute_forward_gelu_f16( |
| 2142 |
const ggml_compute_params * params, |
| 2143 |
ggml_tensor * dst) { |
| 2144 |
|
| 2145 |
const ggml_tensor * src0 = dst->src[0]; |
| 2146 |
|
| 2147 |
assert(ggml_is_contiguous_rows(src0)); |
| 2148 |
assert(ggml_are_same_shape(src0, dst)); |
| 2149 |
|
| 2150 |
GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 2151 |
GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
| 2152 |
GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 2153 |
GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 2154 |
|
| 2155 |
const int ith = params->ith; |
| 2156 |
const int nth = params->nth; |
| 2157 |
|
| 2158 |
const int nc = src0->ne[0]; |
| 2159 |
const int nr = ggml_nrows(src0); |
| 2160 |
|
| 2161 |
// rows per thread |
| 2162 |
const int dr = (nr + nth - 1)/nth; |
| 2163 |
|
| 2164 |
// row range for this thread |
| 2165 |
const int ir0 = dr*ith; |
| 2166 |
const int ir1 = MIN(ir0 + dr, nr); |
| 2167 |
|
| 2168 |
for (int ir = ir0; ir < ir1; ++ir) { |
| 2169 |
const int i3 = ir/(ne02*ne01); |
| 2170 |
const int i2 = (ir - i3*ne02*ne01)/ne01; |
| 2171 |
const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
| 2172 |
|
| 2173 |
ggml_vec_gelu_f16(nc, |
| 2174 |
(ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
| 2175 |
(ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
| 2176 |
|
| 2177 |
#ifndef NDEBUG |
| 2178 |
for (int k = 0; k < nc; k++) { |
| 2179 |
const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 2180 |
const float v = GGML_CPU_FP16_TO_FP32(x); |
| 2181 |
GGML_UNUSED(v); |
| 2182 |
assert(!isnan(v)); |
| 2183 |
assert(!isinf(v)); |
| 2184 |
} |
| 2185 |
#endif |
| 2186 |
} |
| 2187 |
} |
| 2188 |
|
| 2189 |
static void ggml_compute_forward_gelu( |
| 2190 |
const ggml_compute_params * params, |
| 2191 |
ggml_tensor * dst) { |
| 2192 |
|
| 2193 |
const ggml_tensor * src0 = dst->src[0]; |
| 2194 |
|
| 2195 |
switch (src0->type) { |
| 2196 |
case GGML_TYPE_F32: |
| 2197 |
{ |
| 2198 |
ggml_compute_forward_gelu_f32(params, dst); |
| 2199 |
} break; |
| 2200 |
case GGML_TYPE_F16: |
| 2201 |
{ |
| 2202 |
ggml_compute_forward_gelu_f16(params, dst); |
| 2203 |
} break; |
| 2204 |
default: |
| 2205 |
{ |
| 2206 |
GGML_ABORT("fatal error"); |
| 2207 |
} |
| 2208 |
} |
| 2209 |
} |
| 2210 |
|
| 2211 |
// ggml_compute_fill |
| 2212 |
|
| 2213 |
static void ggml_compute_forward_fill_f32(const ggml_compute_params * params, ggml_tensor * dst) { |
| 2214 |
const float c = ggml_get_op_params_f32(dst, 0); |
| 2215 |
|
| 2216 |
GGML_TENSOR_LOCALS(int64_t, ne, dst, ne); |
| 2217 |
GGML_TENSOR_LOCALS(size_t, nb, dst, nb); |
| 2218 |
|
| 2219 |
const auto [ir0, ir1] = get_thread_range(params, dst); |
| 2220 |
|
| 2221 |
for (int64_t ir = ir0; ir < ir1; ++ir) { |
| 2222 |
const int64_t i03 = ir/(ne2*ne1); |
| 2223 |
const int64_t i02 = (ir - i03*ne2*ne1)/ne1; |
| 2224 |
const int64_t i01 = (ir - i03*ne2*ne1 - i02*ne1); |
| 2225 |
|
| 2226 |
float * dst_ptr = (float *) ((char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1); |
| 2227 |
|
| 2228 |
ggml_vec_set_f32(ne0, dst_ptr, c); |
| 2229 |
} |
| 2230 |
} |
| 2231 |
|
| 2232 |
void ggml_compute_forward_fill(const ggml_compute_params * params, ggml_tensor * dst) { |
| 2233 |
ggml_compute_forward_fill_f32(params, dst); |
| 2234 |
} |
| 2235 |
|
| 2236 |
// ggml_compute_tri |
| 2237 |
|
| 2238 |
static void ggml_compute_forward_tri_f32(const ggml_compute_params * params, ggml_tensor * dst) { |
| 2239 |
const ggml_tensor * src0 = dst->src[0]; |
| 2240 |
|
| 2241 |
const ggml_tri_type ttype = (ggml_tri_type) ggml_get_op_params_i32(dst, 0); |
| 2242 |
|
| 2243 |
GGML_ASSERT(ggml_is_contiguous(src0)); |
| 2244 |
|
| 2245 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 2246 |
|
| 2247 |
const auto [ir0, ir1] = get_thread_range(params, src0); |
| 2248 |
|
| 2249 |
bool (*bipred)(int, int); |
| 2250 |
|
| 2251 |
switch (ttype) { |
| 2252 |
case GGML_TRI_TYPE_LOWER: bipred = [](int i, int r) { return i < r; }; break; |
| 2253 |
case GGML_TRI_TYPE_LOWER_DIAG: bipred = [](int i, int r) { return i <= r; }; break; |
| 2254 |
case GGML_TRI_TYPE_UPPER: bipred = [](int i, int r) { return i > r; }; break; |
| 2255 |
case GGML_TRI_TYPE_UPPER_DIAG: bipred = [](int i, int r) { return i >= r; }; break; |
| 2256 |
default: GGML_ABORT("invalid tri type"); |
| 2257 |
} |
| 2258 |
|
| 2259 |
for (int64_t ir = ir0; ir < ir1; ++ir) { |
| 2260 |
const int64_t i03 = ir/(ne02*ne01); |
| 2261 |
const int64_t i02 = (ir - i03*ne02*ne01)/ne01; |
| 2262 |
const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01); |
| 2263 |
|
| 2264 |
const float * src_ptr = (const float *) ((const char *) src0->data + i03*nb03 + i02*nb02 + i01*nb01); |
| 2265 |
float * dst_ptr = ( float *) (( char *) dst->data + i03*nb3 + i02*nb2 + i01*nb1); |
| 2266 |
|
| 2267 |
for (int i0 = 0; i0 < ne0; ++i0) { |
| 2268 |
dst_ptr[i0] = bipred(i0, i01) ? src_ptr[i0] : 0.0f; |
| 2269 |
} |
| 2270 |
} |
| 2271 |
} |
| 2272 |
|
| 2273 |
void ggml_compute_forward_tri(const ggml_compute_params * params, ggml_tensor * dst) { |
| 2274 |
const ggml_tensor * src0 = dst->src[0]; |
| 2275 |
|
| 2276 |
switch (src0->type) { |
| 2277 |
case GGML_TYPE_F32: |
| 2278 |
{ |
| 2279 |
ggml_compute_forward_tri_f32(params, dst); |
| 2280 |
} break; |
| 2281 |
default: |
| 2282 |
{ |
| 2283 |
GGML_ABORT("fatal error"); |
| 2284 |
} |
| 2285 |
} |
| 2286 |
} |
| 2287 |
|
| 2288 |
// ggml_compute_forward_gelu_erf |
| 2289 |
|
| 2290 |
static void ggml_compute_forward_gelu_erf_f32( |
| 2291 |
const ggml_compute_params * params, |
| 2292 |
ggml_tensor * dst) { |
| 2293 |
|
| 2294 |
const ggml_tensor * src0 = dst->src[0]; |
| 2295 |
|
| 2296 |
assert(ggml_is_contiguous_rows(src0)); |
| 2297 |
assert(ggml_are_same_shape(src0, dst)); |
| 2298 |
|
| 2299 |
GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 2300 |
GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
| 2301 |
GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 2302 |
GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 2303 |
|
| 2304 |
const int ith = params->ith; |
| 2305 |
const int nth = params->nth; |
| 2306 |
|
| 2307 |
const int nc = src0->ne[0]; |
| 2308 |
const int nr = ggml_nrows(src0); |
| 2309 |
|
| 2310 |
// rows per thread |
| 2311 |
const int dr = (nr + nth - 1)/nth; |
| 2312 |
|
| 2313 |
// row range for this thread |
| 2314 |
const int ir0 = dr*ith; |
| 2315 |
const int ir1 = MIN(ir0 + dr, nr); |
| 2316 |
|
| 2317 |
for (int ir = ir0; ir < ir1; ++ir) { |
| 2318 |
const int i3 = ir/(ne02*ne01); |
| 2319 |
const int i2 = (ir - i3*ne02*ne01)/ne01; |
| 2320 |
const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
| 2321 |
|
| 2322 |
ggml_vec_gelu_erf_f32(nc, |
| 2323 |
(float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
| 2324 |
(float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
| 2325 |
|
| 2326 |
#ifndef NDEBUG |
| 2327 |
for (int k = 0; k < nc; k++) { |
| 2328 |
const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 2329 |
GGML_UNUSED(x); |
| 2330 |
assert(!isnan(x)); |
| 2331 |
assert(!isinf(x)); |
| 2332 |
} |
| 2333 |
#endif |
| 2334 |
} |
| 2335 |
} |
| 2336 |
|
| 2337 |
static void ggml_compute_forward_gelu_erf_f16( |
| 2338 |
const ggml_compute_params * params, |
| 2339 |
ggml_tensor * dst) { |
| 2340 |
|
| 2341 |
const ggml_tensor * src0 = dst->src[0]; |
| 2342 |
|
| 2343 |
assert(ggml_is_contiguous_rows(src0)); |
| 2344 |
assert(ggml_are_same_shape(src0, dst)); |
| 2345 |
|
| 2346 |
GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 2347 |
GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
| 2348 |
GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 2349 |
GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 2350 |
|
| 2351 |
const int ith = params->ith; |
| 2352 |
const int nth = params->nth; |
| 2353 |
|
| 2354 |
const int nc = src0->ne[0]; |
| 2355 |
const int nr = ggml_nrows(src0); |
| 2356 |
|
| 2357 |
// rows per thread |
| 2358 |
const int dr = (nr + nth - 1)/nth; |
| 2359 |
|
| 2360 |
// row range for this thread |
| 2361 |
const int ir0 = dr*ith; |
| 2362 |
const int ir1 = MIN(ir0 + dr, nr); |
| 2363 |
|
| 2364 |
for (int ir = ir0; ir < ir1; ++ir) { |
| 2365 |
const int i3 = ir/(ne02*ne01); |
| 2366 |
const int i2 = (ir - i3*ne02*ne01)/ne01; |
| 2367 |
const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
| 2368 |
|
| 2369 |
ggml_vec_gelu_erf_f16(nc, |
| 2370 |
(ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
| 2371 |
(ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
| 2372 |
|
| 2373 |
#ifndef NDEBUG |
| 2374 |
for (int k = 0; k < nc; k++) { |
| 2375 |
const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 2376 |
const float v = GGML_CPU_FP16_TO_FP32(x); |
| 2377 |
GGML_UNUSED(v); |
| 2378 |
assert(!isnan(v)); |
| 2379 |
assert(!isinf(v)); |
| 2380 |
} |
| 2381 |
#endif |
| 2382 |
} |
| 2383 |
} |
| 2384 |
|
| 2385 |
static void ggml_compute_forward_gelu_erf( |
| 2386 |
const ggml_compute_params * params, |
| 2387 |
ggml_tensor * dst) { |
| 2388 |
|
| 2389 |
const ggml_tensor * src0 = dst->src[0]; |
| 2390 |
|
| 2391 |
switch (src0->type) { |
| 2392 |
case GGML_TYPE_F32: |
| 2393 |
{ |
| 2394 |
ggml_compute_forward_gelu_erf_f32(params, dst); |
| 2395 |
} break; |
| 2396 |
case GGML_TYPE_F16: |
| 2397 |
{ |
| 2398 |
ggml_compute_forward_gelu_erf_f16(params, dst); |
| 2399 |
} break; |
| 2400 |
default: |
| 2401 |
{ |
| 2402 |
GGML_ABORT("fatal error"); |
| 2403 |
} |
| 2404 |
} |
| 2405 |
} |
| 2406 |
|
| 2407 |
// ggml_compute_forward_gelu_quick |
| 2408 |
|
| 2409 |
static void ggml_compute_forward_gelu_quick_f32( |
| 2410 |
const ggml_compute_params * params, |
| 2411 |
ggml_tensor * dst) { |
| 2412 |
|
| 2413 |
const ggml_tensor * src0 = dst->src[0]; |
| 2414 |
|
| 2415 |
assert(ggml_is_contiguous_rows(src0)); |
| 2416 |
assert(ggml_are_same_shape(src0, dst)); |
| 2417 |
|
| 2418 |
GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 2419 |
GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
| 2420 |
GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 2421 |
GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 2422 |
|
| 2423 |
const int ith = params->ith; |
| 2424 |
const int nth = params->nth; |
| 2425 |
|
| 2426 |
const int nc = src0->ne[0]; |
| 2427 |
const int nr = ggml_nrows(src0); |
| 2428 |
|
| 2429 |
// rows per thread |
| 2430 |
const int dr = (nr + nth - 1)/nth; |
| 2431 |
|
| 2432 |
// row range for this thread |
| 2433 |
const int ir0 = dr*ith; |
| 2434 |
const int ir1 = MIN(ir0 + dr, nr); |
| 2435 |
|
| 2436 |
for (int ir = ir0; ir < ir1; ++ir) { |
| 2437 |
const int i3 = ir/(ne02*ne01); |
| 2438 |
const int i2 = (ir - i3*ne02*ne01)/ne01; |
| 2439 |
const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
| 2440 |
|
| 2441 |
ggml_vec_gelu_quick_f32(nc, |
| 2442 |
(float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
| 2443 |
(float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
| 2444 |
|
| 2445 |
#ifndef NDEBUG |
| 2446 |
for (int k = 0; k < nc; k++) { |
| 2447 |
const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 2448 |
GGML_UNUSED(x); |
| 2449 |
assert(!isnan(x)); |
| 2450 |
assert(!isinf(x)); |
| 2451 |
} |
| 2452 |
#endif |
| 2453 |
} |
| 2454 |
} |
| 2455 |
|
| 2456 |
static void ggml_compute_forward_gelu_quick_f16( |
| 2457 |
const ggml_compute_params * params, |
| 2458 |
ggml_tensor * dst) { |
| 2459 |
|
| 2460 |
const ggml_tensor * src0 = dst->src[0]; |
| 2461 |
|
| 2462 |
assert(ggml_is_contiguous_rows(src0)); |
| 2463 |
assert(ggml_are_same_shape(src0, dst)); |
| 2464 |
|
| 2465 |
GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 2466 |
GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
| 2467 |
GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 2468 |
GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 2469 |
|
| 2470 |
const int ith = params->ith; |
| 2471 |
const int nth = params->nth; |
| 2472 |
|
| 2473 |
const int nc = src0->ne[0]; |
| 2474 |
const int nr = ggml_nrows(src0); |
| 2475 |
|
| 2476 |
// rows per thread |
| 2477 |
const int dr = (nr + nth - 1)/nth; |
| 2478 |
|
| 2479 |
// row range for this thread |
| 2480 |
const int ir0 = dr*ith; |
| 2481 |
const int ir1 = MIN(ir0 + dr, nr); |
| 2482 |
|
| 2483 |
for (int ir = ir0; ir < ir1; ++ir) { |
| 2484 |
const int i3 = ir/(ne02*ne01); |
| 2485 |
const int i2 = (ir - i3*ne02*ne01)/ne01; |
| 2486 |
const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
| 2487 |
|
| 2488 |
ggml_vec_gelu_quick_f16(nc, |
| 2489 |
(ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
| 2490 |
(ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
| 2491 |
|
| 2492 |
#ifndef NDEBUG |
| 2493 |
for (int k = 0; k < nc; k++) { |
| 2494 |
const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 2495 |
const float v = GGML_CPU_FP16_TO_FP32(x); |
| 2496 |
GGML_UNUSED(v); |
| 2497 |
assert(!isnan(v)); |
| 2498 |
assert(!isinf(v)); |
| 2499 |
} |
| 2500 |
#endif |
| 2501 |
} |
| 2502 |
} |
| 2503 |
|
| 2504 |
static void ggml_compute_forward_gelu_quick( |
| 2505 |
const ggml_compute_params * params, |
| 2506 |
ggml_tensor * dst) { |
| 2507 |
|
| 2508 |
const ggml_tensor * src0 = dst->src[0]; |
| 2509 |
|
| 2510 |
switch (src0->type) { |
| 2511 |
case GGML_TYPE_F32: |
| 2512 |
{ |
| 2513 |
ggml_compute_forward_gelu_quick_f32(params, dst); |
| 2514 |
} break; |
| 2515 |
case GGML_TYPE_F16: |
| 2516 |
{ |
| 2517 |
ggml_compute_forward_gelu_quick_f16(params, dst); |
| 2518 |
} break; |
| 2519 |
default: |
| 2520 |
{ |
| 2521 |
GGML_ABORT("fatal error"); |
| 2522 |
} |
| 2523 |
} |
| 2524 |
} |
| 2525 |
|
| 2526 |
// ggml_compute_forward_silu |
| 2527 |
|
| 2528 |
static void ggml_compute_forward_silu_f32( |
| 2529 |
const ggml_compute_params * params, |
| 2530 |
ggml_tensor * dst) { |
| 2531 |
|
| 2532 |
const ggml_tensor * src0 = dst->src[0]; |
| 2533 |
|
| 2534 |
assert(ggml_is_contiguous_rows(src0)); |
| 2535 |
assert(ggml_are_same_shape(src0, dst)); |
| 2536 |
|
| 2537 |
GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 2538 |
GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
| 2539 |
GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 2540 |
GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 2541 |
|
| 2542 |
const int ith = params->ith; |
| 2543 |
const int nth = params->nth; |
| 2544 |
|
| 2545 |
const int nc = src0->ne[0]; |
| 2546 |
const int nr = ggml_nrows(src0); |
| 2547 |
|
| 2548 |
// rows per thread |
| 2549 |
const int dr = (nr + nth - 1)/nth; |
| 2550 |
|
| 2551 |
// row range for this thread |
| 2552 |
const int ir0 = dr*ith; |
| 2553 |
const int ir1 = MIN(ir0 + dr, nr); |
| 2554 |
|
| 2555 |
for (int ir = ir0; ir < ir1; ++ir) { |
| 2556 |
const int i3 = ir/(ne02*ne01); |
| 2557 |
const int i2 = (ir - i3*ne02*ne01)/ne01; |
| 2558 |
const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
| 2559 |
|
| 2560 |
ggml_vec_silu_f32(nc, |
| 2561 |
(float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
| 2562 |
(float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
| 2563 |
|
| 2564 |
#ifndef NDEBUG |
| 2565 |
for (int k = 0; k < nc; k++) { |
| 2566 |
const float x = ((float *) ((char *) dst->data + i1*(dst->nb[1])))[k]; |
| 2567 |
GGML_UNUSED(x); |
| 2568 |
assert(!isnan(x)); |
| 2569 |
assert(!isinf(x)); |
| 2570 |
} |
| 2571 |
#endif |
| 2572 |
} |
| 2573 |
} |
| 2574 |
|
| 2575 |
static void ggml_compute_forward_silu_f16( |
| 2576 |
const ggml_compute_params * params, |
| 2577 |
ggml_tensor * dst) { |
| 2578 |
|
| 2579 |
const ggml_tensor * src0 = dst->src[0]; |
| 2580 |
|
| 2581 |
assert(ggml_is_contiguous_rows(src0)); |
| 2582 |
assert(ggml_are_same_shape(src0, dst)); |
| 2583 |
|
| 2584 |
GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 2585 |
GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) |
| 2586 |
GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 2587 |
GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 2588 |
|
| 2589 |
const int ith = params->ith; |
| 2590 |
const int nth = params->nth; |
| 2591 |
|
| 2592 |
const int nc = src0->ne[0]; |
| 2593 |
const int nr = ggml_nrows(src0); |
| 2594 |
|
| 2595 |
// rows per thread |
| 2596 |
const int dr = (nr + nth - 1)/nth; |
| 2597 |
|
| 2598 |
// row range for this thread |
| 2599 |
const int ir0 = dr*ith; |
| 2600 |
const int ir1 = MIN(ir0 + dr, nr); |
| 2601 |
|
| 2602 |
for (int ir = ir0; ir < ir1; ++ir) { |
| 2603 |
const int i3 = ir/(ne02*ne01); |
| 2604 |
const int i2 = (ir - i3*ne02*ne01)/ne01; |
| 2605 |
const int i1 = (ir - i3*ne02*ne01 - i2*ne01); |
| 2606 |
|
| 2607 |
ggml_vec_silu_f16(nc, |
| 2608 |
(ggml_fp16_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1), |
| 2609 |
(ggml_fp16_t *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
| 2610 |
|
| 2611 |
#ifndef NDEBUG |
| 2612 |
for (int k = 0; k < nc; k++) { |
| 2613 |
const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i1*(dst->nb[1])))[k]; |
| 2614 |
const float v = GGML_CPU_FP16_TO_FP32(x); |
| 2615 |
GGML_UNUSED(v); |
| 2616 |
assert(!isnan(v)); |
| 2617 |
assert(!isinf(v)); |
| 2618 |
} |
| 2619 |
#endif |
| 2620 |
} |
| 2621 |
} |
| 2622 |
|
| 2623 |
static void ggml_compute_forward_silu( |
| 2624 |
const ggml_compute_params * params, |
| 2625 |
ggml_tensor * dst) { |
| 2626 |
|
| 2627 |
const ggml_tensor * src0 = dst->src[0]; |
| 2628 |
|
| 2629 |
switch (src0->type) { |
| 2630 |
case GGML_TYPE_F32: |
| 2631 |
{ |
| 2632 |
ggml_compute_forward_silu_f32(params, dst); |
| 2633 |
} break; |
| 2634 |
case GGML_TYPE_F16: |
| 2635 |
{ |
| 2636 |
ggml_compute_forward_silu_f16(params, dst); |
| 2637 |
} break; |
| 2638 |
default: |
| 2639 |
{ |
| 2640 |
GGML_ABORT("fatal error"); |
| 2641 |
} |
| 2642 |
} |
| 2643 |
} |
| 2644 |
// ggml_compute_forward_leaky_relu |
| 2645 |
|
| 2646 |
static void ggml_compute_forward_leaky_relu_f32( |
| 2647 |
const ggml_compute_params * params, |
| 2648 |
ggml_tensor * dst) { |
| 2649 |
|
| 2650 |
const ggml_tensor * src0 = dst->src[0]; |
| 2651 |
|
| 2652 |
if (params->ith != 0) { |
| 2653 |
return; |
| 2654 |
} |
| 2655 |
|
| 2656 |
assert(ggml_is_contiguous_1(src0)); |
| 2657 |
assert(ggml_is_contiguous_1(dst)); |
| 2658 |
assert(ggml_are_same_shape(src0, dst)); |
| 2659 |
|
| 2660 |
const int n = ggml_nrows(src0); |
| 2661 |
const int nc = src0->ne[0]; |
| 2662 |
|
| 2663 |
float negative_slope; |
| 2664 |
memcpy(&negative_slope, dst->op_params, sizeof(float)); |
| 2665 |
|
| 2666 |
assert(dst->nb[0] == sizeof(float)); |
| 2667 |
assert(src0->nb[0] == sizeof(float)); |
| 2668 |
|
| 2669 |
for (int i = 0; i < n; i++) { |
| 2670 |
ggml_vec_leaky_relu_f32(nc, |
| 2671 |
(float *) ((char *) dst->data + i*( dst->nb[1])), |
| 2672 |
(float *) ((char *) src0->data + i*(src0->nb[1])), negative_slope); |
| 2673 |
} |
| 2674 |
} |
| 2675 |
|
| 2676 |
static void ggml_compute_forward_leaky_relu_f16( |
| 2677 |
const ggml_compute_params * params, |
| 2678 |
ggml_tensor * dst) { |
| 2679 |
|
| 2680 |
const ggml_tensor * src0 = dst->src[0]; |
| 2681 |
|
| 2682 |
if (params->ith != 0) { |
| 2683 |
return; |
| 2684 |
} |
| 2685 |
|
| 2686 |
assert(ggml_is_contiguous_1(src0)); |
| 2687 |
assert(ggml_is_contiguous_1(dst)); |
| 2688 |
assert(ggml_are_same_shape(src0, dst)); |
| 2689 |
|
| 2690 |
const int n = ggml_nrows(src0); |
| 2691 |
const int nc = src0->ne[0]; |
| 2692 |
|
| 2693 |
float negative_slope; |
| 2694 |
memcpy(&negative_slope, dst->op_params, sizeof(float)); |
| 2695 |
|
| 2696 |
assert(dst->nb[0] == sizeof(ggml_fp16_t)); |
| 2697 |
assert(src0->nb[0] == sizeof(ggml_fp16_t)); |
| 2698 |
|
| 2699 |
for (int i = 0; i < n; i++) { |
| 2700 |
ggml_vec_leaky_relu_f16(nc, |
| 2701 |
(ggml_fp16_t *) ((char *) dst->data + i*( dst->nb[1])), |
| 2702 |
(ggml_fp16_t *) ((char *) src0->data + i*(src0->nb[1])), negative_slope); |
| 2703 |
} |
| 2704 |
} |
| 2705 |
|
| 2706 |
void ggml_compute_forward_leaky_relu( |
| 2707 |
const ggml_compute_params * params, |
| 2708 |
ggml_tensor * dst) { |
| 2709 |
|
| 2710 |
const ggml_tensor * src0 = dst->src[0]; |
| 2711 |
|
| 2712 |
switch (src0->type) { |
| 2713 |
case GGML_TYPE_F32: |
| 2714 |
{ |
| 2715 |
ggml_compute_forward_leaky_relu_f32(params, dst); |
| 2716 |
} break; |
| 2717 |
case GGML_TYPE_F16: |
| 2718 |
{ |
| 2719 |
ggml_compute_forward_leaky_relu_f16(params, dst); |
| 2720 |
} break; |
| 2721 |
default: |
| 2722 |
{ |
| 2723 |
GGML_ABORT("fatal error"); |
| 2724 |
} |
| 2725 |
} |
| 2726 |
} |
| 2727 |
|
| 2728 |
// ggml_compute_forward_silu_back |
| 2729 |
|
| 2730 |
static void ggml_compute_forward_silu_back_f32( |
| 2731 |
const ggml_compute_params * params, |
| 2732 |
ggml_tensor * dst) { |
| 2733 |
|
| 2734 |
const ggml_tensor * grad = dst->src[0]; |
| 2735 |
const ggml_tensor * src1 = dst->src[1]; |
| 2736 |
|
| 2737 |
assert(ggml_is_contiguous_1(grad)); |
| 2738 |
assert(ggml_is_contiguous_1(src1)); |
| 2739 |
assert(ggml_is_contiguous_1(dst)); |
| 2740 |
assert(ggml_are_same_shape(src1, dst)); |
| 2741 |
assert(ggml_are_same_shape(src1, grad)); |
| 2742 |
|
| 2743 |
const int ith = params->ith; |
| 2744 |
const int nth = params->nth; |
| 2745 |
|
| 2746 |
const int nc = src1->ne[0]; |
| 2747 |
const int nr = ggml_nrows(src1); |
| 2748 |
|
| 2749 |
// rows per thread |
| 2750 |
const int dr = (nr + nth - 1)/nth; |
| 2751 |
|
| 2752 |
// row range for this thread |
| 2753 |
const int ir0 = dr*ith; |
| 2754 |
const int ir1 = MIN(ir0 + dr, nr); |
| 2755 |
|
| 2756 |
for (int i1 = ir0; i1 < ir1; i1++) { |
| 2757 |
ggml_vec_silu_backward_f32(nc, |
| 2758 |
(float *) ((char *) dst->data + i1*( dst->nb[1])), |
| 2759 |
(float *) ((char *) src1->data + i1*(src1->nb[1])), |
| 2760 |
(float *) ((char *) grad->data + i1*(grad->nb[1]))); |
| 2761 |
|
| 2762 |
#ifndef NDEBUG |
| 2763 |
for (int k = 0; k < nc; k++) { |
| 2764 |
const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 2765 |
GGML_UNUSED(x); |
| 2766 |
assert(!isnan(x)); |
| 2767 |
assert(!isinf(x)); |
| 2768 |
} |
| 2769 |
#endif |
| 2770 |
} |
| 2771 |
} |
| 2772 |
|
| 2773 |
static void ggml_compute_forward_silu_back_f16( |
| 2774 |
const ggml_compute_params * params, |
| 2775 |
ggml_tensor * dst) { |
| 2776 |
|
| 2777 |
const ggml_tensor * grad = dst->src[0]; |
| 2778 |
const ggml_tensor * src1 = dst->src[1]; |
| 2779 |
|
| 2780 |
assert(ggml_is_contiguous_1(grad)); |
| 2781 |
assert(ggml_is_contiguous_1(src1)); |
| 2782 |
assert(ggml_is_contiguous_1(dst)); |
| 2783 |
assert(ggml_are_same_shape(src1, dst)); |
| 2784 |
assert(ggml_are_same_shape(src1, grad)); |
| 2785 |
|
| 2786 |
const int ith = params->ith; |
| 2787 |
const int nth = params->nth; |
| 2788 |
|
| 2789 |
const int nc = src1->ne[0]; |
| 2790 |
const int nr = ggml_nrows(src1); |
| 2791 |
|
| 2792 |
// rows per thread |
| 2793 |
const int dr = (nr + nth - 1)/nth; |
| 2794 |
|
| 2795 |
// row range for this thread |
| 2796 |
const int ir0 = dr*ith; |
| 2797 |
const int ir1 = MIN(ir0 + dr, nr); |
| 2798 |
|
| 2799 |
for (int i1 = ir0; i1 < ir1; i1++) { |
| 2800 |
ggml_vec_silu_backward_f16(nc, |
| 2801 |
(ggml_fp16_t *) ((char *) dst->data + i1*( dst->nb[1])), |
| 2802 |
(ggml_fp16_t *) ((char *) src1->data + i1*(src1->nb[1])), |
| 2803 |
(ggml_fp16_t *) ((char *) grad->data + i1*(grad->nb[1]))); |
| 2804 |
|
| 2805 |
#ifndef NDEBUG |
| 2806 |
for (int k = 0; k < nc; k++) { |
| 2807 |
const float x = ((ggml_fp16_t *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 2808 |
const float v = GGML_CPU_FP16_TO_FP32(x); |
| 2809 |
GGML_UNUSED(v); |
| 2810 |
assert(!isnan(v)); |
| 2811 |
assert(!isinf(v)); |
| 2812 |
} |
| 2813 |
#endif |
| 2814 |
} |
| 2815 |
} |
| 2816 |
|
| 2817 |
void ggml_compute_forward_silu_back( |
| 2818 |
const ggml_compute_params * params, |
| 2819 |
ggml_tensor * dst) { |
| 2820 |
|
| 2821 |
const ggml_tensor * src0 = dst->src[0]; |
| 2822 |
|
| 2823 |
switch (src0->type) { |
| 2824 |
case GGML_TYPE_F32: |
| 2825 |
{ |
| 2826 |
ggml_compute_forward_silu_back_f32(params, dst); |
| 2827 |
} break; |
| 2828 |
case GGML_TYPE_F16: |
| 2829 |
{ |
| 2830 |
ggml_compute_forward_silu_back_f16(params, dst); |
| 2831 |
} break; |
| 2832 |
default: |
| 2833 |
{ |
| 2834 |
GGML_ABORT("fatal error"); |
| 2835 |
} |
| 2836 |
} |
| 2837 |
} |
| 2838 |
|
| 2839 |
// ggml_compute_forward_reglu |
| 2840 |
|
| 2841 |
static void ggml_compute_forward_reglu_f32( |
| 2842 |
const ggml_compute_params * params, |
| 2843 |
ggml_tensor * dst) { |
| 2844 |
|
| 2845 |
const ggml_tensor * src0 = dst->src[0]; |
| 2846 |
const ggml_tensor * src1 = dst->src[1]; |
| 2847 |
char * src0_d = (char *) src0->data; |
| 2848 |
char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 2849 |
const size_t src0_o = src0->nb[1]; |
| 2850 |
const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 2851 |
|
| 2852 |
GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 2853 |
GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 2854 |
|
| 2855 |
if (src1) { |
| 2856 |
GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 2857 |
GGML_ASSERT(src0->type == src1->type); |
| 2858 |
} |
| 2859 |
|
| 2860 |
const int ith = params->ith; |
| 2861 |
const int nth = params->nth; |
| 2862 |
|
| 2863 |
const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 2864 |
const int nr = ggml_nrows(src0); |
| 2865 |
|
| 2866 |
GGML_ASSERT(dst->ne[0] == nc); |
| 2867 |
GGML_ASSERT(ggml_nrows(dst) == nr); |
| 2868 |
|
| 2869 |
const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 2870 |
|
| 2871 |
// rows per thread |
| 2872 |
const int dr = (nr + nth - 1)/nth; |
| 2873 |
|
| 2874 |
// row range for this thread |
| 2875 |
const int ir0 = dr*ith; |
| 2876 |
const int ir1 = MIN(ir0 + dr, nr); |
| 2877 |
|
| 2878 |
for (int i1 = ir0; i1 < ir1; i1++) { |
| 2879 |
float * src0_p = (float *) (src0_d + i1*src0_o); |
| 2880 |
float * src1_p = (float *) (src1_d + i1*src1_o); |
| 2881 |
|
| 2882 |
if (!src1) { |
| 2883 |
src0_p += swapped ? nc : 0; |
| 2884 |
src1_p += swapped ? 0 : nc; |
| 2885 |
} |
| 2886 |
|
| 2887 |
ggml_vec_reglu_f32(nc, (float *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
| 2888 |
|
| 2889 |
#ifndef NDEBUG |
| 2890 |
for (int k = 0; k < nc; k++) { |
| 2891 |
const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 2892 |
GGML_UNUSED(x); |
| 2893 |
assert(!isnan(x)); |
| 2894 |
assert(!isinf(x)); |
| 2895 |
} |
| 2896 |
#endif |
| 2897 |
} |
| 2898 |
} |
| 2899 |
|
| 2900 |
static void ggml_compute_forward_reglu_f16( |
| 2901 |
const ggml_compute_params * params, |
| 2902 |
ggml_tensor * dst) { |
| 2903 |
|
| 2904 |
const ggml_tensor * src0 = dst->src[0]; |
| 2905 |
const ggml_tensor * src1 = dst->src[1]; |
| 2906 |
char * src0_d = (char *) src0->data; |
| 2907 |
char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 2908 |
const size_t src0_o = src0->nb[1]; |
| 2909 |
const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 2910 |
|
| 2911 |
GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 2912 |
GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 2913 |
|
| 2914 |
if (src1) { |
| 2915 |
GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 2916 |
GGML_ASSERT(src0->type == src1->type); |
| 2917 |
} |
| 2918 |
|
| 2919 |
const int ith = params->ith; |
| 2920 |
const int nth = params->nth; |
| 2921 |
|
| 2922 |
const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 2923 |
const int nr = ggml_nrows(src0); |
| 2924 |
|
| 2925 |
GGML_ASSERT(dst->ne[0] == nc); |
| 2926 |
GGML_ASSERT(ggml_nrows(dst) == nr); |
| 2927 |
|
| 2928 |
const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 2929 |
|
| 2930 |
// rows per thread |
| 2931 |
const int dr = (nr + nth - 1)/nth; |
| 2932 |
|
| 2933 |
// row range for this thread |
| 2934 |
const int ir0 = dr*ith; |
| 2935 |
const int ir1 = MIN(ir0 + dr, nr); |
| 2936 |
|
| 2937 |
for (int i1 = ir0; i1 < ir1; i1++) { |
| 2938 |
ggml_fp16_t * src0_p = (ggml_fp16_t *) (src0_d + i1*src0_o); |
| 2939 |
ggml_fp16_t * src1_p = (ggml_fp16_t *) (src1_d + i1*src1_o); |
| 2940 |
|
| 2941 |
if (!src1) { |
| 2942 |
src0_p += swapped ? nc : 0; |
| 2943 |
src1_p += swapped ? 0 : nc; |
| 2944 |
} |
| 2945 |
|
| 2946 |
ggml_vec_reglu_f16(nc, (ggml_fp16_t *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
| 2947 |
|
| 2948 |
#ifndef NDEBUG |
| 2949 |
for (int k = 0; k < nc; k++) { |
| 2950 |
const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 2951 |
const float v = GGML_FP16_TO_FP32(x); |
| 2952 |
GGML_UNUSED(v); |
| 2953 |
assert(!isnan(v)); |
| 2954 |
assert(!isinf(v)); |
| 2955 |
} |
| 2956 |
#endif |
| 2957 |
} |
| 2958 |
} |
| 2959 |
|
| 2960 |
static void ggml_compute_forward_reglu( |
| 2961 |
const ggml_compute_params * params, |
| 2962 |
ggml_tensor * dst) { |
| 2963 |
|
| 2964 |
const ggml_tensor * src0 = dst->src[0]; |
| 2965 |
|
| 2966 |
switch (src0->type) { |
| 2967 |
case GGML_TYPE_F32: |
| 2968 |
{ |
| 2969 |
ggml_compute_forward_reglu_f32(params, dst); |
| 2970 |
} break; |
| 2971 |
case GGML_TYPE_F16: |
| 2972 |
{ |
| 2973 |
ggml_compute_forward_reglu_f16(params, dst); |
| 2974 |
} break; |
| 2975 |
default: |
| 2976 |
{ |
| 2977 |
GGML_ABORT("fatal error"); |
| 2978 |
} |
| 2979 |
} |
| 2980 |
} |
| 2981 |
|
| 2982 |
// ggml_compute_forward_geglu |
| 2983 |
|
| 2984 |
static void ggml_compute_forward_geglu_f32( |
| 2985 |
const ggml_compute_params * params, |
| 2986 |
ggml_tensor * dst) { |
| 2987 |
|
| 2988 |
const ggml_tensor * src0 = dst->src[0]; |
| 2989 |
const ggml_tensor * src1 = dst->src[1]; |
| 2990 |
char * src0_d = (char *) src0->data; |
| 2991 |
char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 2992 |
const size_t src0_o = src0->nb[1]; |
| 2993 |
const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 2994 |
|
| 2995 |
GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 2996 |
GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 2997 |
|
| 2998 |
if (src1) { |
| 2999 |
GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 3000 |
GGML_ASSERT(src0->type == src1->type); |
| 3001 |
} |
| 3002 |
|
| 3003 |
const int ith = params->ith; |
| 3004 |
const int nth = params->nth; |
| 3005 |
|
| 3006 |
const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 3007 |
const int nr = ggml_nrows(src0); |
| 3008 |
|
| 3009 |
GGML_ASSERT(dst->ne[0] == nc); |
| 3010 |
GGML_ASSERT(ggml_nrows(dst) == nr); |
| 3011 |
|
| 3012 |
const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 3013 |
|
| 3014 |
// rows per thread |
| 3015 |
const int dr = (nr + nth - 1)/nth; |
| 3016 |
|
| 3017 |
// row range for this thread |
| 3018 |
const int ir0 = dr*ith; |
| 3019 |
const int ir1 = MIN(ir0 + dr, nr); |
| 3020 |
|
| 3021 |
for (int i1 = ir0; i1 < ir1; i1++) { |
| 3022 |
float * src0_p = (float *) (src0_d + i1*src0_o); |
| 3023 |
float * src1_p = (float *) (src1_d + i1*src1_o); |
| 3024 |
|
| 3025 |
if (!src1) { |
| 3026 |
src0_p += swapped ? nc : 0; |
| 3027 |
src1_p += swapped ? 0 : nc; |
| 3028 |
} |
| 3029 |
|
| 3030 |
ggml_vec_geglu_f32(nc, (float *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
| 3031 |
|
| 3032 |
#ifndef NDEBUG |
| 3033 |
for (int k = 0; k < nc; k++) { |
| 3034 |
const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 3035 |
GGML_UNUSED(x); |
| 3036 |
assert(!isnan(x)); |
| 3037 |
assert(!isinf(x)); |
| 3038 |
} |
| 3039 |
#endif |
| 3040 |
} |
| 3041 |
} |
| 3042 |
|
| 3043 |
static void ggml_compute_forward_geglu_f16( |
| 3044 |
const ggml_compute_params * params, |
| 3045 |
ggml_tensor * dst) { |
| 3046 |
|
| 3047 |
const ggml_tensor * src0 = dst->src[0]; |
| 3048 |
const ggml_tensor * src1 = dst->src[1]; |
| 3049 |
char * src0_d = (char *) src0->data; |
| 3050 |
char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 3051 |
const size_t src0_o = src0->nb[1]; |
| 3052 |
const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 3053 |
|
| 3054 |
GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 3055 |
GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 3056 |
|
| 3057 |
if (src1) { |
| 3058 |
GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 3059 |
GGML_ASSERT(src0->type == src1->type); |
| 3060 |
} |
| 3061 |
|
| 3062 |
const int ith = params->ith; |
| 3063 |
const int nth = params->nth; |
| 3064 |
|
| 3065 |
const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 3066 |
const int nr = ggml_nrows(src0); |
| 3067 |
|
| 3068 |
GGML_ASSERT(dst->ne[0] == nc); |
| 3069 |
GGML_ASSERT(ggml_nrows(dst) == nr); |
| 3070 |
|
| 3071 |
const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 3072 |
|
| 3073 |
// rows per thread |
| 3074 |
const int dr = (nr + nth - 1)/nth; |
| 3075 |
|
| 3076 |
// row range for this thread |
| 3077 |
const int ir0 = dr*ith; |
| 3078 |
const int ir1 = MIN(ir0 + dr, nr); |
| 3079 |
|
| 3080 |
for (int i1 = ir0; i1 < ir1; i1++) { |
| 3081 |
ggml_fp16_t * src0_p = (ggml_fp16_t *) (src0_d + i1*src0_o); |
| 3082 |
ggml_fp16_t * src1_p = (ggml_fp16_t *) (src1_d + i1*src1_o); |
| 3083 |
|
| 3084 |
if (!src1) { |
| 3085 |
src0_p += swapped ? nc : 0; |
| 3086 |
src1_p += swapped ? 0 : nc; |
| 3087 |
} |
| 3088 |
|
| 3089 |
ggml_vec_geglu_f16(nc, (ggml_fp16_t *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
| 3090 |
|
| 3091 |
#ifndef NDEBUG |
| 3092 |
for (int k = 0; k < nc; k++) { |
| 3093 |
const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 3094 |
const float v = GGML_FP16_TO_FP32(x); |
| 3095 |
GGML_UNUSED(v); |
| 3096 |
assert(!isnan(v)); |
| 3097 |
assert(!isinf(v)); |
| 3098 |
} |
| 3099 |
#endif |
| 3100 |
} |
| 3101 |
} |
| 3102 |
|
| 3103 |
static void ggml_compute_forward_geglu( |
| 3104 |
const ggml_compute_params * params, |
| 3105 |
ggml_tensor * dst) { |
| 3106 |
|
| 3107 |
const ggml_tensor * src0 = dst->src[0]; |
| 3108 |
|
| 3109 |
switch (src0->type) { |
| 3110 |
case GGML_TYPE_F32: |
| 3111 |
{ |
| 3112 |
ggml_compute_forward_geglu_f32(params, dst); |
| 3113 |
} break; |
| 3114 |
case GGML_TYPE_F16: |
| 3115 |
{ |
| 3116 |
ggml_compute_forward_geglu_f16(params, dst); |
| 3117 |
} break; |
| 3118 |
default: |
| 3119 |
{ |
| 3120 |
GGML_ABORT("fatal error"); |
| 3121 |
} |
| 3122 |
} |
| 3123 |
} |
| 3124 |
|
| 3125 |
// ggml_compute_forward_swiglu |
| 3126 |
|
| 3127 |
static void ggml_compute_forward_swiglu_f32( |
| 3128 |
const ggml_compute_params * params, |
| 3129 |
ggml_tensor * dst) { |
| 3130 |
|
| 3131 |
const ggml_tensor * src0 = dst->src[0]; |
| 3132 |
const ggml_tensor * src1 = dst->src[1]; |
| 3133 |
char * src0_d = (char *) src0->data; |
| 3134 |
char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 3135 |
const size_t src0_o = src0->nb[1]; |
| 3136 |
const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 3137 |
|
| 3138 |
GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 3139 |
GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 3140 |
|
| 3141 |
if (src1) { |
| 3142 |
GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 3143 |
GGML_ASSERT(src0->type == src1->type); |
| 3144 |
} |
| 3145 |
|
| 3146 |
const int ith = params->ith; |
| 3147 |
const int nth = params->nth; |
| 3148 |
|
| 3149 |
const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 3150 |
const int nr = ggml_nrows(src0); |
| 3151 |
|
| 3152 |
GGML_ASSERT(dst->ne[0] == nc); |
| 3153 |
GGML_ASSERT(ggml_nrows(dst) == nr); |
| 3154 |
|
| 3155 |
const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 3156 |
|
| 3157 |
// rows per thread |
| 3158 |
const int dr = (nr + nth - 1)/nth; |
| 3159 |
|
| 3160 |
// row range for this thread |
| 3161 |
const int ir0 = dr*ith; |
| 3162 |
const int ir1 = MIN(ir0 + dr, nr); |
| 3163 |
|
| 3164 |
for (int i1 = ir0; i1 < ir1; i1++) { |
| 3165 |
float * src0_p = (float *) (src0_d + i1*src0_o); |
| 3166 |
float * src1_p = (float *) (src1_d + i1*src1_o); |
| 3167 |
|
| 3168 |
if (!src1) { |
| 3169 |
src0_p += swapped ? nc : 0; |
| 3170 |
src1_p += swapped ? 0 : nc; |
| 3171 |
} |
| 3172 |
|
| 3173 |
ggml_vec_swiglu_f32(nc, (float *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
| 3174 |
|
| 3175 |
#ifndef NDEBUG |
| 3176 |
for (int k = 0; k < nc; k++) { |
| 3177 |
const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 3178 |
GGML_UNUSED(x); |
| 3179 |
assert(!isnan(x)); |
| 3180 |
assert(!isinf(x)); |
| 3181 |
} |
| 3182 |
#endif |
| 3183 |
} |
| 3184 |
} |
| 3185 |
|
| 3186 |
static void ggml_compute_forward_swiglu_f16( |
| 3187 |
const ggml_compute_params * params, |
| 3188 |
ggml_tensor * dst) { |
| 3189 |
|
| 3190 |
const ggml_tensor * src0 = dst->src[0]; |
| 3191 |
const ggml_tensor * src1 = dst->src[1]; |
| 3192 |
char * src0_d = (char *) src0->data; |
| 3193 |
char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 3194 |
const size_t src0_o = src0->nb[1]; |
| 3195 |
const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 3196 |
|
| 3197 |
GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 3198 |
GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 3199 |
|
| 3200 |
if (src1) { |
| 3201 |
GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 3202 |
GGML_ASSERT(src0->type == src1->type); |
| 3203 |
} |
| 3204 |
|
| 3205 |
const int ith = params->ith; |
| 3206 |
const int nth = params->nth; |
| 3207 |
|
| 3208 |
const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 3209 |
const int nr = ggml_nrows(src0); |
| 3210 |
|
| 3211 |
GGML_ASSERT(dst->ne[0] == nc); |
| 3212 |
GGML_ASSERT(ggml_nrows(dst) == nr); |
| 3213 |
|
| 3214 |
const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 3215 |
|
| 3216 |
// rows per thread |
| 3217 |
const int dr = (nr + nth - 1)/nth; |
| 3218 |
|
| 3219 |
// row range for this thread |
| 3220 |
const int ir0 = dr*ith; |
| 3221 |
const int ir1 = MIN(ir0 + dr, nr); |
| 3222 |
|
| 3223 |
for (int i1 = ir0; i1 < ir1; i1++) { |
| 3224 |
ggml_fp16_t * src0_p = (ggml_fp16_t *) (src0_d + i1*src0_o); |
| 3225 |
ggml_fp16_t * src1_p = (ggml_fp16_t *) (src1_d + i1*src1_o); |
| 3226 |
|
| 3227 |
if (!src1) { |
| 3228 |
src0_p += swapped ? nc : 0; |
| 3229 |
src1_p += swapped ? 0 : nc; |
| 3230 |
} |
| 3231 |
|
| 3232 |
ggml_vec_swiglu_f16(nc, (ggml_fp16_t *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
| 3233 |
|
| 3234 |
#ifndef NDEBUG |
| 3235 |
for (int k = 0; k < nc; k++) { |
| 3236 |
const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 3237 |
const float v = GGML_FP16_TO_FP32(x); |
| 3238 |
GGML_UNUSED(v); |
| 3239 |
assert(!isnan(v)); |
| 3240 |
assert(!isinf(v)); |
| 3241 |
} |
| 3242 |
#endif |
| 3243 |
} |
| 3244 |
} |
| 3245 |
|
| 3246 |
static void ggml_compute_forward_swiglu( |
| 3247 |
const ggml_compute_params * params, |
| 3248 |
ggml_tensor * dst) { |
| 3249 |
|
| 3250 |
const ggml_tensor * src0 = dst->src[0]; |
| 3251 |
|
| 3252 |
switch (src0->type) { |
| 3253 |
case GGML_TYPE_F32: |
| 3254 |
{ |
| 3255 |
ggml_compute_forward_swiglu_f32(params, dst); |
| 3256 |
} break; |
| 3257 |
case GGML_TYPE_F16: |
| 3258 |
{ |
| 3259 |
ggml_compute_forward_swiglu_f16(params, dst); |
| 3260 |
} break; |
| 3261 |
default: |
| 3262 |
{ |
| 3263 |
GGML_ABORT("fatal error"); |
| 3264 |
} |
| 3265 |
} |
| 3266 |
} |
| 3267 |
|
| 3268 |
// ggml_compute_forward_swiglu_oai |
| 3269 |
|
| 3270 |
static void ggml_compute_forward_swiglu_oai_f32( |
| 3271 |
const ggml_compute_params * params, |
| 3272 |
ggml_tensor * dst) { |
| 3273 |
|
| 3274 |
const ggml_tensor * src0 = dst->src[0]; |
| 3275 |
const ggml_tensor * src1 = dst->src[1]; |
| 3276 |
char * src0_d = (char *) src0->data; |
| 3277 |
char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 3278 |
const size_t src0_o = src0->nb[1]; |
| 3279 |
const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 3280 |
|
| 3281 |
GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 3282 |
GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 3283 |
|
| 3284 |
if (src1) { |
| 3285 |
GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 3286 |
GGML_ASSERT(src0->type == src1->type); |
| 3287 |
} |
| 3288 |
|
| 3289 |
const int ith = params->ith; |
| 3290 |
const int nth = params->nth; |
| 3291 |
|
| 3292 |
const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 3293 |
const int nr = ggml_nrows(src0); |
| 3294 |
|
| 3295 |
GGML_ASSERT(dst->ne[0] == nc); |
| 3296 |
GGML_ASSERT(ggml_nrows(dst) == nr); |
| 3297 |
|
| 3298 |
const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 3299 |
const float alpha = ggml_get_op_params_f32(dst, 2); |
| 3300 |
const float limit = ggml_get_op_params_f32(dst, 3); |
| 3301 |
|
| 3302 |
// rows per thread |
| 3303 |
const int dr = (nr + nth - 1)/nth; |
| 3304 |
|
| 3305 |
// row range for this thread |
| 3306 |
const int ir0 = dr*ith; |
| 3307 |
const int ir1 = MIN(ir0 + dr, nr); |
| 3308 |
|
| 3309 |
for (int i1 = ir0; i1 < ir1; i1++) { |
| 3310 |
float * src0_p = (float *) (src0_d + i1*src0_o); |
| 3311 |
float * src1_p = (float *) (src1_d + i1*src1_o); |
| 3312 |
float * dst_p = (float *) ((char *) dst->data + i1*(dst->nb[1])); |
| 3313 |
|
| 3314 |
if (!src1) { |
| 3315 |
src0_p += swapped ? nc : 0; |
| 3316 |
src1_p += swapped ? 0 : nc; |
| 3317 |
} |
| 3318 |
|
| 3319 |
for (int k = 0; k < nc; k++) { |
| 3320 |
const float x = std::min(src0_p[k], limit); |
| 3321 |
const float y = std::clamp(src1_p[k], -limit, limit); |
| 3322 |
const float out_glu = x / (1.f + expf(alpha * (-x))); |
| 3323 |
dst_p[k] = out_glu * (y + 1.f); |
| 3324 |
} |
| 3325 |
|
| 3326 |
#ifndef NDEBUG |
| 3327 |
for (int k = 0; k < nc; k++) { |
| 3328 |
const float x = dst_p[k]; |
| 3329 |
GGML_UNUSED(x); |
| 3330 |
assert(!isnan(x)); |
| 3331 |
assert(!isinf(x)); |
| 3332 |
} |
| 3333 |
#endif |
| 3334 |
} |
| 3335 |
} |
| 3336 |
|
| 3337 |
static void ggml_compute_forward_swiglu_oai( |
| 3338 |
const ggml_compute_params * params, |
| 3339 |
ggml_tensor * dst) { |
| 3340 |
|
| 3341 |
const ggml_tensor * src0 = dst->src[0]; |
| 3342 |
|
| 3343 |
switch (src0->type) { |
| 3344 |
case GGML_TYPE_F32: |
| 3345 |
{ |
| 3346 |
ggml_compute_forward_swiglu_oai_f32(params, dst); |
| 3347 |
} break; |
| 3348 |
default: |
| 3349 |
{ |
| 3350 |
GGML_ABORT("fatal error"); |
| 3351 |
} |
| 3352 |
} |
| 3353 |
} |
| 3354 |
|
| 3355 |
// ggml_compute_forward_geglu_erf |
| 3356 |
|
| 3357 |
static void ggml_compute_forward_geglu_erf_f32( |
| 3358 |
const ggml_compute_params * params, |
| 3359 |
ggml_tensor * dst) { |
| 3360 |
|
| 3361 |
const ggml_tensor * src0 = dst->src[0]; |
| 3362 |
const ggml_tensor * src1 = dst->src[1]; |
| 3363 |
char * src0_d = (char *) src0->data; |
| 3364 |
char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 3365 |
const size_t src0_o = src0->nb[1]; |
| 3366 |
const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 3367 |
|
| 3368 |
GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 3369 |
GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 3370 |
|
| 3371 |
if (src1) { |
| 3372 |
GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 3373 |
GGML_ASSERT(src0->type == src1->type); |
| 3374 |
} |
| 3375 |
|
| 3376 |
const int ith = params->ith; |
| 3377 |
const int nth = params->nth; |
| 3378 |
|
| 3379 |
const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 3380 |
const int nr = ggml_nrows(src0); |
| 3381 |
|
| 3382 |
GGML_ASSERT(dst->ne[0] == nc); |
| 3383 |
GGML_ASSERT(ggml_nrows(dst) == nr); |
| 3384 |
|
| 3385 |
const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 3386 |
|
| 3387 |
// rows per thread |
| 3388 |
const int dr = (nr + nth - 1)/nth; |
| 3389 |
|
| 3390 |
// row range for this thread |
| 3391 |
const int ir0 = dr*ith; |
| 3392 |
const int ir1 = MIN(ir0 + dr, nr); |
| 3393 |
|
| 3394 |
for (int i1 = ir0; i1 < ir1; i1++) { |
| 3395 |
float * src0_p = (float *) (src0_d + i1*src0_o); |
| 3396 |
float * src1_p = (float *) (src1_d + i1*src1_o); |
| 3397 |
|
| 3398 |
if (!src1) { |
| 3399 |
src0_p += swapped ? nc : 0; |
| 3400 |
src1_p += swapped ? 0 : nc; |
| 3401 |
} |
| 3402 |
|
| 3403 |
ggml_vec_geglu_erf_f32(nc, (float *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
| 3404 |
|
| 3405 |
#ifndef NDEBUG |
| 3406 |
for (int k = 0; k < nc; k++) { |
| 3407 |
const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 3408 |
GGML_UNUSED(x); |
| 3409 |
assert(!isnan(x)); |
| 3410 |
assert(!isinf(x)); |
| 3411 |
} |
| 3412 |
#endif |
| 3413 |
} |
| 3414 |
} |
| 3415 |
|
| 3416 |
static void ggml_compute_forward_geglu_erf_f16( |
| 3417 |
const ggml_compute_params * params, |
| 3418 |
ggml_tensor * dst) { |
| 3419 |
|
| 3420 |
const ggml_tensor * src0 = dst->src[0]; |
| 3421 |
const ggml_tensor * src1 = dst->src[1]; |
| 3422 |
char * src0_d = (char *) src0->data; |
| 3423 |
char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 3424 |
const size_t src0_o = src0->nb[1]; |
| 3425 |
const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 3426 |
|
| 3427 |
GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 3428 |
GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 3429 |
|
| 3430 |
if (src1) { |
| 3431 |
GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 3432 |
GGML_ASSERT(src0->type == src1->type); |
| 3433 |
} |
| 3434 |
|
| 3435 |
const int ith = params->ith; |
| 3436 |
const int nth = params->nth; |
| 3437 |
|
| 3438 |
const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 3439 |
const int nr = ggml_nrows(src0); |
| 3440 |
|
| 3441 |
GGML_ASSERT(dst->ne[0] == nc); |
| 3442 |
GGML_ASSERT(ggml_nrows(dst) == nr); |
| 3443 |
|
| 3444 |
const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 3445 |
|
| 3446 |
// rows per thread |
| 3447 |
const int dr = (nr + nth - 1)/nth; |
| 3448 |
|
| 3449 |
// row range for this thread |
| 3450 |
const int ir0 = dr*ith; |
| 3451 |
const int ir1 = MIN(ir0 + dr, nr); |
| 3452 |
|
| 3453 |
for (int i1 = ir0; i1 < ir1; i1++) { |
| 3454 |
ggml_fp16_t * src0_p = (ggml_fp16_t *) (src0_d + i1*src0_o); |
| 3455 |
ggml_fp16_t * src1_p = (ggml_fp16_t *) (src1_d + i1*src1_o); |
| 3456 |
|
| 3457 |
if (!src1) { |
| 3458 |
src0_p += swapped ? nc : 0; |
| 3459 |
src1_p += swapped ? 0 : nc; |
| 3460 |
} |
| 3461 |
|
| 3462 |
ggml_vec_geglu_erf_f16(nc, (ggml_fp16_t *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
| 3463 |
|
| 3464 |
#ifndef NDEBUG |
| 3465 |
for (int k = 0; k < nc; k++) { |
| 3466 |
const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 3467 |
const float v = GGML_FP16_TO_FP32(x); |
| 3468 |
GGML_UNUSED(v); |
| 3469 |
assert(!isnan(v)); |
| 3470 |
assert(!isinf(v)); |
| 3471 |
} |
| 3472 |
#endif |
| 3473 |
} |
| 3474 |
} |
| 3475 |
|
| 3476 |
static void ggml_compute_forward_geglu_erf( |
| 3477 |
const ggml_compute_params * params, |
| 3478 |
ggml_tensor * dst) { |
| 3479 |
|
| 3480 |
const ggml_tensor * src0 = dst->src[0]; |
| 3481 |
|
| 3482 |
switch (src0->type) { |
| 3483 |
case GGML_TYPE_F32: |
| 3484 |
{ |
| 3485 |
ggml_compute_forward_geglu_erf_f32(params, dst); |
| 3486 |
} break; |
| 3487 |
case GGML_TYPE_F16: |
| 3488 |
{ |
| 3489 |
ggml_compute_forward_geglu_erf_f16(params, dst); |
| 3490 |
} break; |
| 3491 |
default: |
| 3492 |
{ |
| 3493 |
GGML_ABORT("fatal error"); |
| 3494 |
} |
| 3495 |
} |
| 3496 |
} |
| 3497 |
|
| 3498 |
// ggml_compute_forward_geglu_quick |
| 3499 |
|
| 3500 |
static void ggml_compute_forward_geglu_quick_f32( |
| 3501 |
const ggml_compute_params * params, |
| 3502 |
ggml_tensor * dst) { |
| 3503 |
|
| 3504 |
const ggml_tensor * src0 = dst->src[0]; |
| 3505 |
const ggml_tensor * src1 = dst->src[1]; |
| 3506 |
char * src0_d = (char *) src0->data; |
| 3507 |
char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 3508 |
const size_t src0_o = src0->nb[1]; |
| 3509 |
const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 3510 |
|
| 3511 |
GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 3512 |
GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 3513 |
|
| 3514 |
if (src1) { |
| 3515 |
GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 3516 |
GGML_ASSERT(src0->type == src1->type); |
| 3517 |
} |
| 3518 |
|
| 3519 |
const int ith = params->ith; |
| 3520 |
const int nth = params->nth; |
| 3521 |
|
| 3522 |
const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 3523 |
const int nr = ggml_nrows(src0); |
| 3524 |
|
| 3525 |
GGML_ASSERT(dst->ne[0] == nc); |
| 3526 |
GGML_ASSERT(ggml_nrows(dst) == nr); |
| 3527 |
|
| 3528 |
const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 3529 |
|
| 3530 |
// rows per thread |
| 3531 |
const int dr = (nr + nth - 1)/nth; |
| 3532 |
|
| 3533 |
// row range for this thread |
| 3534 |
const int ir0 = dr*ith; |
| 3535 |
const int ir1 = MIN(ir0 + dr, nr); |
| 3536 |
|
| 3537 |
for (int i1 = ir0; i1 < ir1; i1++) { |
| 3538 |
float * src0_p = (float *) (src0_d + i1*src0_o); |
| 3539 |
float * src1_p = (float *) (src1_d + i1*src1_o); |
| 3540 |
|
| 3541 |
if (!src1) { |
| 3542 |
src0_p += swapped ? nc : 0; |
| 3543 |
src1_p += swapped ? 0 : nc; |
| 3544 |
} |
| 3545 |
|
| 3546 |
ggml_vec_geglu_quick_f32(nc, (float *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
| 3547 |
|
| 3548 |
#ifndef NDEBUG |
| 3549 |
for (int k = 0; k < nc; k++) { |
| 3550 |
const float x = ((float *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 3551 |
GGML_UNUSED(x); |
| 3552 |
assert(!isnan(x)); |
| 3553 |
assert(!isinf(x)); |
| 3554 |
} |
| 3555 |
#endif |
| 3556 |
} |
| 3557 |
} |
| 3558 |
|
| 3559 |
static void ggml_compute_forward_geglu_quick_f16( |
| 3560 |
const ggml_compute_params * params, |
| 3561 |
ggml_tensor * dst) { |
| 3562 |
|
| 3563 |
const ggml_tensor * src0 = dst->src[0]; |
| 3564 |
const ggml_tensor * src1 = dst->src[1]; |
| 3565 |
char * src0_d = (char *) src0->data; |
| 3566 |
char * src1_d = (char *) (src1 ? src1->data : src0->data); |
| 3567 |
const size_t src0_o = src0->nb[1]; |
| 3568 |
const size_t src1_o = src1 ? src1->nb[1] : src0->nb[1]; |
| 3569 |
|
| 3570 |
GGML_ASSERT(ggml_is_contiguous_1(src0)); |
| 3571 |
GGML_ASSERT(ggml_is_contiguous_1(dst)); |
| 3572 |
|
| 3573 |
if (src1) { |
| 3574 |
GGML_ASSERT(ggml_is_contiguous_1(src1)); |
| 3575 |
GGML_ASSERT(src0->type == src1->type); |
| 3576 |
} |
| 3577 |
|
| 3578 |
const int ith = params->ith; |
| 3579 |
const int nth = params->nth; |
| 3580 |
|
| 3581 |
const int nc = src1 ? src0->ne[0] : src0->ne[0] / 2; |
| 3582 |
const int nr = ggml_nrows(src0); |
| 3583 |
|
| 3584 |
GGML_ASSERT(dst->ne[0] == nc); |
| 3585 |
GGML_ASSERT(ggml_nrows(dst) == nr); |
| 3586 |
|
| 3587 |
const int32_t swapped = ggml_get_op_params_i32(dst, 1); |
| 3588 |
|
| 3589 |
// rows per thread |
| 3590 |
const int dr = (nr + nth - 1)/nth; |
| 3591 |
|
| 3592 |
// row range for this thread |
| 3593 |
const int ir0 = dr*ith; |
| 3594 |
const int ir1 = MIN(ir0 + dr, nr); |
| 3595 |
|
| 3596 |
for (int i1 = ir0; i1 < ir1; i1++) { |
| 3597 |
ggml_fp16_t * src0_p = (ggml_fp16_t *) (src0_d + i1*src0_o); |
| 3598 |
ggml_fp16_t * src1_p = (ggml_fp16_t *) (src1_d + i1*src1_o); |
| 3599 |
|
| 3600 |
if (!src1) { |
| 3601 |
src0_p += swapped ? nc : 0; |
| 3602 |
src1_p += swapped ? 0 : nc; |
| 3603 |
} |
| 3604 |
|
| 3605 |
ggml_vec_geglu_quick_f16(nc, (ggml_fp16_t *) ((char *) dst->data + i1*(dst->nb[1])), src0_p, src1_p); |
| 3606 |
|
| 3607 |
#ifndef NDEBUG |
| 3608 |
for (int k = 0; k < nc; k++) { |
| 3609 |
const ggml_fp16_t x = ((ggml_fp16_t *) ((char *) dst->data + i1*( dst->nb[1])))[k]; |
| 3610 |
const float v = GGML_FP16_TO_FP32(x); |
| 3611 |
GGML_UNUSED(v); |
| 3612 |
assert(!isnan(v)); |
| 3613 |
assert(!isinf(v)); |
| 3614 |
} |
| 3615 |
#endif |
| 3616 |
} |
| 3617 |
} |
| 3618 |
|
| 3619 |
static void ggml_compute_forward_geglu_quick( |
| 3620 |
const ggml_compute_params * params, |
| 3621 |
ggml_tensor * dst) { |
| 3622 |
|
| 3623 |
const ggml_tensor * src0 = dst->src[0]; |
| 3624 |
|
| 3625 |
switch (src0->type) { |
| 3626 |
case GGML_TYPE_F32: |
| 3627 |
{ |
| 3628 |
ggml_compute_forward_geglu_quick_f32(params, dst); |
| 3629 |
} break; |
| 3630 |
case GGML_TYPE_F16: |
| 3631 |
{ |
| 3632 |
ggml_compute_forward_geglu_quick_f16(params, dst); |
| 3633 |
} break; |
| 3634 |
default: |
| 3635 |
{ |
| 3636 |
GGML_ABORT("fatal error"); |
| 3637 |
} |
| 3638 |
} |
| 3639 |
} |
| 3640 |
|
| 3641 |
// ggml_compute_forward_norm |
| 3642 |
|
| 3643 |
static void ggml_compute_forward_norm_f32( |
| 3644 |
const ggml_compute_params * params, |
| 3645 |
ggml_tensor * dst) { |
| 3646 |
|
| 3647 |
const ggml_tensor * src0 = dst->src[0]; |
| 3648 |
|
| 3649 |
GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 3650 |
|
| 3651 |
GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 3652 |
|
| 3653 |
const int ith = params->ith; |
| 3654 |
const int nth = params->nth; |
| 3655 |
|
| 3656 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 3657 |
|
| 3658 |
float eps; |
| 3659 |
memcpy(&eps, dst->op_params, sizeof(float)); |
| 3660 |
|
| 3661 |
GGML_ASSERT(eps >= 0.0f); |
| 3662 |
|
| 3663 |
for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 3664 |
for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 3665 |
for (int64_t i01 = ith; i01 < ne01; i01 += nth) { |
| 3666 |
const float * x = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03); |
| 3667 |
|
| 3668 |
float sum = 0.0; |
| 3669 |
ggml_vec_sum_f32(ne00, &sum, x); |
| 3670 |
float mean = sum/ne00; |
| 3671 |
|
| 3672 |
float * y = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3); |
| 3673 |
float variance = 0; |
| 3674 |
|
| 3675 |
#ifdef GGML_USE_ACCELERATE |
| 3676 |
mean = -mean; |
| 3677 |
vDSP_vsadd(x, 1, &mean, y, 1, ne00); |
| 3678 |
vDSP_measqv(y, 1, &variance, ne00); |
| 3679 |
#else |
| 3680 |
variance = ggml_vec_cvar_f32(ne00, y, x, mean); |
| 3681 |
#endif //GGML_USE_ACCELERATE |
| 3682 |
|
| 3683 |
const float scale = 1.0f/sqrtf(variance + eps); |
| 3684 |
ggml_vec_scale_f32(ne00, y, scale); |
| 3685 |
} |
| 3686 |
} |
| 3687 |
} |
| 3688 |
} |
| 3689 |
|
| 3690 |
void ggml_compute_forward_norm( |
| 3691 |
const ggml_compute_params * params, |
| 3692 |
ggml_tensor * dst) { |
| 3693 |
|
| 3694 |
const ggml_tensor * src0 = dst->src[0]; |
| 3695 |
|
| 3696 |
switch (src0->type) { |
| 3697 |
case GGML_TYPE_F32: |
| 3698 |
{ |
| 3699 |
ggml_compute_forward_norm_f32(params, dst); |
| 3700 |
} break; |
| 3701 |
default: |
| 3702 |
{ |
| 3703 |
GGML_ABORT("fatal error"); |
| 3704 |
} |
| 3705 |
} |
| 3706 |
} |
| 3707 |
|
| 3708 |
// ggml_compute_forward_group_rms_norm |
| 3709 |
|
| 3710 |
static void ggml_compute_forward_rms_norm_f32( |
| 3711 |
const ggml_compute_params * params, |
| 3712 |
ggml_tensor * dst) { |
| 3713 |
|
| 3714 |
const ggml_tensor * src0 = dst->src[0]; |
| 3715 |
|
| 3716 |
GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 3717 |
|
| 3718 |
GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 3719 |
|
| 3720 |
const int ith = params->ith; |
| 3721 |
const int nth = params->nth; |
| 3722 |
|
| 3723 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 3724 |
|
| 3725 |
float eps; |
| 3726 |
memcpy(&eps, dst->op_params, sizeof(float)); |
| 3727 |
|
| 3728 |
GGML_ASSERT(eps >= 0.0f); |
| 3729 |
|
| 3730 |
// TODO: optimize |
| 3731 |
for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 3732 |
for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 3733 |
for (int64_t i01 = ith; i01 < ne01; i01 += nth) { |
| 3734 |
const float * x = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03); |
| 3735 |
|
| 3736 |
ggml_float sum = 0.0; |
| 3737 |
for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 3738 |
sum += (ggml_float)(x[i00] * x[i00]); |
| 3739 |
} |
| 3740 |
|
| 3741 |
const float mean = sum/ne00; |
| 3742 |
|
| 3743 |
float * y = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3); |
| 3744 |
|
| 3745 |
memcpy(y, x, ne00 * sizeof(float)); |
| 3746 |
// for (int i00 = 0; i00 < ne00; i00++) { |
| 3747 |
// y[i00] = x[i00]; |
| 3748 |
// } |
| 3749 |
|
| 3750 |
const float scale = 1.0f/sqrtf(mean + eps); |
| 3751 |
|
| 3752 |
// if you hit this, likely you got an inf somewhere earlier |
| 3753 |
assert(scale > 0.0f); |
| 3754 |
|
| 3755 |
ggml_vec_scale_f32(ne00, y, scale); |
| 3756 |
} |
| 3757 |
} |
| 3758 |
} |
| 3759 |
} |
| 3760 |
|
| 3761 |
void ggml_compute_forward_rms_norm( |
| 3762 |
const ggml_compute_params * params, |
| 3763 |
ggml_tensor * dst) { |
| 3764 |
|
| 3765 |
const ggml_tensor * src0 = dst->src[0]; |
| 3766 |
|
| 3767 |
switch (src0->type) { |
| 3768 |
case GGML_TYPE_F32: |
| 3769 |
{ |
| 3770 |
ggml_compute_forward_rms_norm_f32(params, dst); |
| 3771 |
} break; |
| 3772 |
default: |
| 3773 |
{ |
| 3774 |
GGML_ABORT("fatal error"); |
| 3775 |
} |
| 3776 |
} |
| 3777 |
} |
| 3778 |
|
| 3779 |
static void ggml_compute_forward_rms_norm_back_f32( |
| 3780 |
const ggml_compute_params * params, |
| 3781 |
ggml_tensor * dst) { |
| 3782 |
|
| 3783 |
const ggml_tensor * src0 = dst->src[0]; // gradients from forward pass output |
| 3784 |
const ggml_tensor * src1 = dst->src[1]; // src1 from forward pass |
| 3785 |
|
| 3786 |
GGML_ASSERT(ggml_are_same_shape(src0, dst) && ggml_are_same_shape(src0, src1)); |
| 3787 |
|
| 3788 |
GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 3789 |
GGML_ASSERT(src1->nb[0] == sizeof(float)); |
| 3790 |
|
| 3791 |
const int ith = params->ith; |
| 3792 |
const int nth = params->nth; |
| 3793 |
|
| 3794 |
GGML_TENSOR_BINARY_OP_LOCALS |
| 3795 |
|
| 3796 |
float eps; |
| 3797 |
memcpy(&eps, dst->op_params, sizeof(float)); |
| 3798 |
|
| 3799 |
// TODO: optimize |
| 3800 |
for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 3801 |
for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 3802 |
for (int64_t i01 = ith; i01 < ne01; i01 += nth) { |
| 3803 |
// src1 is same shape as src0 => same indices |
| 3804 |
const int64_t i11 = i01; |
| 3805 |
const int64_t i12 = i02; |
| 3806 |
const int64_t i13 = i03; |
| 3807 |
|
| 3808 |
const float * dz = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03); |
| 3809 |
const float * x = (float *) ((char *) src1->data + i11*nb11 + i12*nb12 + i13*nb13); |
| 3810 |
|
| 3811 |
ggml_float sum_xx = 0.0; |
| 3812 |
ggml_float sum_xdz = 0.0; |
| 3813 |
|
| 3814 |
for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 3815 |
sum_xx += (ggml_float)(x[i00] * x[i00]); |
| 3816 |
sum_xdz += (ggml_float)(x[i00] * dz[i00]); |
| 3817 |
} |
| 3818 |
|
| 3819 |
//const float mean = (float)(sum_xx)/ne00; |
| 3820 |
const float mean_eps = (float)(sum_xx)/ne00 + eps; |
| 3821 |
const float sum_eps = (float)(sum_xx) + eps*ne00; |
| 3822 |
//const float mean_xdz = (float)(sum_xdz)/ne00; |
| 3823 |
// we could cache rms from forward pass to improve performance. |
| 3824 |
// to do this implement ggml_rms and compose ggml_rms_norm using ggml_rms. |
| 3825 |
//const float rms = sqrtf(mean_eps); |
| 3826 |
const float rrms = 1.0f / sqrtf(mean_eps); |
| 3827 |
//const float scale = -rrms/(ne00 * mean_eps); // -1/(n*rms**3) |
| 3828 |
|
| 3829 |
{ |
| 3830 |
// z = rms_norm(x) |
| 3831 |
// |
| 3832 |
// rms_norm(src1) = |
| 3833 |
// scale( |
| 3834 |
// src1, |
| 3835 |
// div( |
| 3836 |
// 1, |
| 3837 |
// sqrt( |
| 3838 |
// add( |
| 3839 |
// scale( |
| 3840 |
// sum( |
| 3841 |
// sqr( |
| 3842 |
// src1)), |
| 3843 |
// (1.0/N)), |
| 3844 |
// eps)))); |
| 3845 |
|
| 3846 |
// postorder: |
| 3847 |
// ## op args grad |
| 3848 |
// 00 param src1 grad[#00] |
| 3849 |
// 01 const 1 |
| 3850 |
// 02 sqr (#00) grad[#02] |
| 3851 |
// 03 sum (#02) grad[#03] |
| 3852 |
// 04 const 1/N |
| 3853 |
// 05 scale (#03, #04) grad[#05] |
| 3854 |
// 06 const eps |
| 3855 |
// 07 add (#05, #06) grad[#07] |
| 3856 |
// 08 sqrt (#07) grad[#08] |
| 3857 |
// 09 div (#01,#08) grad[#09] |
| 3858 |
// 10 scale (#00,#09) grad[#10] |
| 3859 |
// |
| 3860 |
// backward pass, given grad[#10] |
| 3861 |
// #10: scale |
| 3862 |
// grad[#00] += scale(grad[#10],#09) |
| 3863 |
// grad[#09] += sum(mul(grad[#10],#00)) |
| 3864 |
// #09: div |
| 3865 |
// grad[#08] += neg(mul(grad[#09], div(#09,#08))) |
| 3866 |
// #08: sqrt |
| 3867 |
// grad[#07] += mul(grad[#08], div(0.5, #08)) |
| 3868 |
// #07: add |
| 3869 |
// grad[#05] += grad[#07] |
| 3870 |
// #05: scale |
| 3871 |
// grad[#03] += scale(grad[#05],#04) |
| 3872 |
// #03: sum |
| 3873 |
// grad[#02] += repeat(grad[#03], #02) |
| 3874 |
// #02: |
| 3875 |
// grad[#00] += scale(mul(#00, grad[#02]), 2.0) |
| 3876 |
// |
| 3877 |
// substitute and simplify: |
| 3878 |
// grad[#00] = scale(grad(#10), #09) + scale(mul(#00, grad[#02]), 2.0) |
| 3879 |
// grad[#02] = repeat(grad[#03], #02) |
| 3880 |
// grad[#02] = repeat(scale(grad[#05],#04), #02) |
| 3881 |
// grad[#02] = repeat(scale(grad[#07],#04), #02) |
| 3882 |
// grad[#02] = repeat(scale(mul(grad[#08], div(0.5, #08)),#04), #02) |
| 3883 |
// grad[#02] = repeat(scale(mul(neg(mul(grad[#09], div(#09,#08))), div(0.5, #08)),#04), #02) |
| 3884 |
// grad[#02] = repeat(scale(mul(neg(mul(sum(mul(grad[#10],#00)), div(#09,#08))), div(0.5, #08)),#04), #02) |
| 3885 |
// grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(#09,#08) * div(0.5, #08) * (1/N)), #02) |
| 3886 |
// grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(div(#01,#08),#08) * div(0.5, #08) * (1/N)), #02) |
| 3887 |
// grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(1,#08*#08) * div(0.5, #08) * (1/N)), #02) |
| 3888 |
// grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(1,#07) * div(0.5, #08) * (1/N)), #02) |
| 3889 |
// grad[#00] = scale(grad(#10), #09) + scale(mul(#00, grad[#02]), 2.0) |
| 3890 |
// 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) |
| 3891 |
// 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) |
| 3892 |
// grad[#00] = scale(grad(#10), #09) + scale(#00, -(sum(mul(grad[#10],#00)) * div(1,#07) * div(1,#08) * (1/N))) |
| 3893 |
// grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(1,#07*#08) * (-1/N)) |
| 3894 |
// grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(1,#07*#08) * (-1/N)) |
| 3895 |
// grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(1,mean_eps*rms) * (-1/N)) |
| 3896 |
// grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(-1,rms*N*mean_eps)) |
| 3897 |
// grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(-1,rms*N*(sum_xx/N+eps))) |
| 3898 |
// grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(-1,rms*N*sum_xx+rms*N*eps)) |
| 3899 |
// grad[#00] = scale(dz, rrms) + scale(x, sum(mul(dz,x)) * div(-1,rms*N*mean_eps)) |
| 3900 |
// grad[#00] = scale(dz, rrms) + scale(x, sum_xdz * div(-1,rms*N*mean_eps)) |
| 3901 |
// a = b*c + d*e |
| 3902 |
// a = b*c*f/f + d*e*f/f |
| 3903 |
// a = (b*c*f + d*e*f)*(1/f) |
| 3904 |
// a = (b*c*(1/c) + d*e*(1/c))*(1/(1/c)) |
| 3905 |
// a = (b + d*e/c)*c |
| 3906 |
// b = dz, c = rrms, d = x, e = sum_xdz * div(-1,rms*N*mean_eps) |
| 3907 |
// a = (dz + x*sum_xdz * div(-1,rms*N*mean_eps)/rrms)*rrms |
| 3908 |
// a = (dz + x*sum_xdz * div(-1,rms*N*mean_eps)*rms)*rrms |
| 3909 |
// a = (dz + x*sum_xdz * div(-rms,rms*N*mean_eps))*rrms |
| 3910 |
// a = (dz + x*sum_xdz * div(-1,N*mean_eps))*rrms |
| 3911 |
// a = (dz + x*div(-sum_xdz,N*mean_eps))*rrms |
| 3912 |
// a = (dz + x*div(-mean_xdz,mean_eps))*rrms |
| 3913 |
// grad[#00] = scale(dz + scale(x, div(-mean_xdz,mean_eps)),rrms) |
| 3914 |
// grad[#00] = scale(dz + scale(x, -mean_xdz/mean_eps),rrms) |
| 3915 |
// dx = scale(dz + scale(x, -mean_xdz/mean_eps),rrms) |
| 3916 |
} |
| 3917 |
// dx = scale(dz + scale(x, -mean_xdz/mean_eps),rrms) |
| 3918 |
// post-order: |
| 3919 |
// dx := x |
| 3920 |
// dx := scale(dx,-mean_xdz/mean_eps) |
| 3921 |
// dx := add(dx, dz) |
| 3922 |
// dx := scale(dx, rrms) |
| 3923 |
float * dx = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3); |
| 3924 |
|
| 3925 |
// dx[i00] = (x*(-sum_xdz/sum_eps) + dz) / sqrtf(mean_eps) |
| 3926 |
ggml_vec_cpy_f32 (ne00, dx, x); |
| 3927 |
// ggml_vec_scale_f32(ne00, dx, -mean_xdz/mean_eps); |
| 3928 |
ggml_vec_scale_f32(ne00, dx, (float)(-sum_xdz)/sum_eps); |
| 3929 |
ggml_vec_acc_f32 (ne00, dx, dz); |
| 3930 |
ggml_vec_scale_f32(ne00, dx, rrms); |
| 3931 |
} |
| 3932 |
} |
| 3933 |
} |
| 3934 |
} |
| 3935 |
|
| 3936 |
void ggml_compute_forward_rms_norm_back( |
| 3937 |
const ggml_compute_params * params, |
| 3938 |
ggml_tensor * dst) { |
| 3939 |
|
| 3940 |
const ggml_tensor * src0 = dst->src[0]; |
| 3941 |
|
| 3942 |
switch (src0->type) { |
| 3943 |
case GGML_TYPE_F32: |
| 3944 |
{ |
| 3945 |
ggml_compute_forward_rms_norm_back_f32(params, dst); |
| 3946 |
} break; |
| 3947 |
default: |
| 3948 |
{ |
| 3949 |
GGML_ABORT("fatal error"); |
| 3950 |
} |
| 3951 |
} |
| 3952 |
} |
| 3953 |
|
| 3954 |
// ggml_compute_forward_group_norm |
| 3955 |
|
| 3956 |
static void ggml_compute_forward_group_norm_f32( |
| 3957 |
const ggml_compute_params * params, |
| 3958 |
ggml_tensor * dst) { |
| 3959 |
|
| 3960 |
const ggml_tensor * src0 = dst->src[0]; |
| 3961 |
|
| 3962 |
GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 3963 |
|
| 3964 |
GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 3965 |
|
| 3966 |
const int ith = params->ith; |
| 3967 |
const int nth = params->nth; |
| 3968 |
|
| 3969 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 3970 |
|
| 3971 |
// TODO: optimize |
| 3972 |
|
| 3973 |
float eps; |
| 3974 |
memcpy(&eps, dst->op_params + 1, sizeof(float)); |
| 3975 |
|
| 3976 |
int n_channels = src0->ne[2]; |
| 3977 |
int n_groups = dst->op_params[0]; |
| 3978 |
int n_channels_per_group = (n_channels + n_groups - 1) / n_groups; |
| 3979 |
for (int i = ith; i < n_groups; i += nth) { |
| 3980 |
int start = i * n_channels_per_group; |
| 3981 |
int end = start + n_channels_per_group; |
| 3982 |
if (end > n_channels) { |
| 3983 |
end = n_channels; |
| 3984 |
} |
| 3985 |
int step = end - start; |
| 3986 |
|
| 3987 |
for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 3988 |
ggml_float sum = 0.0; |
| 3989 |
for (int64_t i02 = start; i02 < end; i02++) { |
| 3990 |
for (int64_t i01 = 0; i01 < ne01; i01++) { |
| 3991 |
const float * x = (float *)((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03); |
| 3992 |
|
| 3993 |
ggml_float sumr = 0.0; |
| 3994 |
for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 3995 |
sumr += (ggml_float)x[i00]; |
| 3996 |
} |
| 3997 |
sum += sumr; |
| 3998 |
} |
| 3999 |
} |
| 4000 |
const float mean = sum / (ne00 * ne01 * step); |
| 4001 |
|
| 4002 |
ggml_float sum2 = 0.0; |
| 4003 |
for (int64_t i02 = start; i02 < end; i02++) { |
| 4004 |
for (int64_t i01 = 0; i01 < ne01; i01++) { |
| 4005 |
const float * x = (float *)((char *) src0->data + i01 * nb01 + i02 * nb02 + i03 * nb03); |
| 4006 |
|
| 4007 |
float * y = (float *)((char *) dst->data + i01 * nb1 + i02 * nb2 + i03 * nb3); |
| 4008 |
|
| 4009 |
ggml_float sumr = 0.0; |
| 4010 |
for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 4011 |
float v = x[i00] - mean; |
| 4012 |
y[i00] = v; |
| 4013 |
sumr += (ggml_float)(v * v); |
| 4014 |
} |
| 4015 |
sum2 += sumr; |
| 4016 |
} |
| 4017 |
} |
| 4018 |
const float variance = sum2 / (ne00 * ne01 * step); |
| 4019 |
const float scale = 1.0f / sqrtf(variance + eps); |
| 4020 |
|
| 4021 |
for (int64_t i02 = start; i02 < end; i02++) { |
| 4022 |
for (int64_t i01 = 0; i01 < ne01; i01++) { |
| 4023 |
float * y = (float *)((char *) dst->data + i01 * nb1 + i02 * nb2 + i03 * nb3); |
| 4024 |
ggml_vec_scale_f32(ne00, y, scale); |
| 4025 |
} |
| 4026 |
} |
| 4027 |
} |
| 4028 |
} |
| 4029 |
} |
| 4030 |
|
| 4031 |
void ggml_compute_forward_group_norm( |
| 4032 |
const ggml_compute_params * params, |
| 4033 |
ggml_tensor * dst) { |
| 4034 |
|
| 4035 |
const ggml_tensor * src0 = dst->src[0]; |
| 4036 |
|
| 4037 |
switch (src0->type) { |
| 4038 |
case GGML_TYPE_F32: |
| 4039 |
{ |
| 4040 |
ggml_compute_forward_group_norm_f32(params, dst); |
| 4041 |
} break; |
| 4042 |
default: |
| 4043 |
{ |
| 4044 |
GGML_ABORT("fatal error"); |
| 4045 |
} |
| 4046 |
} |
| 4047 |
} |
| 4048 |
|
| 4049 |
// ggml_compute_forward_l2_norm |
| 4050 |
|
| 4051 |
static void ggml_compute_forward_l2_norm_f32( |
| 4052 |
const ggml_compute_params * params, |
| 4053 |
ggml_tensor * dst) { |
| 4054 |
|
| 4055 |
const ggml_tensor * src0 = dst->src[0]; |
| 4056 |
|
| 4057 |
GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 4058 |
|
| 4059 |
GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 4060 |
|
| 4061 |
const int ith = params->ith; |
| 4062 |
const int nth = params->nth; |
| 4063 |
|
| 4064 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 4065 |
|
| 4066 |
float eps; |
| 4067 |
memcpy(&eps, dst->op_params, sizeof(float)); |
| 4068 |
|
| 4069 |
GGML_ASSERT(eps >= 0.0f); |
| 4070 |
|
| 4071 |
// TODO: optimize |
| 4072 |
for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 4073 |
for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 4074 |
for (int64_t i01 = ith; i01 < ne01; i01 += nth) { |
| 4075 |
const float * x = (float *) ((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03); |
| 4076 |
|
| 4077 |
ggml_float sum = 0.0; |
| 4078 |
for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 4079 |
sum += (ggml_float)(x[i00] * x[i00]); |
| 4080 |
} |
| 4081 |
|
| 4082 |
float * y = (float *) ((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3); |
| 4083 |
|
| 4084 |
memcpy(y, x, ne00 * sizeof(float)); |
| 4085 |
|
| 4086 |
const float scale = 1.0f/fmaxf(sqrtf(sum), eps); |
| 4087 |
|
| 4088 |
ggml_vec_scale_f32(ne00, y, scale); |
| 4089 |
} |
| 4090 |
} |
| 4091 |
} |
| 4092 |
} |
| 4093 |
|
| 4094 |
void ggml_compute_forward_l2_norm( |
| 4095 |
const ggml_compute_params * params, |
| 4096 |
ggml_tensor * dst) { |
| 4097 |
|
| 4098 |
const ggml_tensor * src0 = dst->src[0]; |
| 4099 |
|
| 4100 |
switch (src0->type) { |
| 4101 |
case GGML_TYPE_F32: |
| 4102 |
{ |
| 4103 |
ggml_compute_forward_l2_norm_f32(params, dst); |
| 4104 |
} break; |
| 4105 |
default: |
| 4106 |
{ |
| 4107 |
GGML_ABORT("fatal error"); |
| 4108 |
} |
| 4109 |
} |
| 4110 |
} |
| 4111 |
|
| 4112 |
// ggml_compute_forward_out_prod |
| 4113 |
|
| 4114 |
static void ggml_compute_forward_out_prod_f32( |
| 4115 |
const ggml_compute_params * params, |
| 4116 |
ggml_tensor * dst) { |
| 4117 |
|
| 4118 |
const ggml_tensor * src0 = dst->src[0]; |
| 4119 |
const ggml_tensor * src1 = dst->src[1]; |
| 4120 |
|
| 4121 |
GGML_TENSOR_BINARY_OP_LOCALS |
| 4122 |
|
| 4123 |
GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 4124 |
GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 4125 |
GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 4126 |
|
| 4127 |
const int ith = params->ith; |
| 4128 |
const int nth = params->nth; |
| 4129 |
|
| 4130 |
GGML_ASSERT(ne0 == ne00); |
| 4131 |
GGML_ASSERT(ne1 == ne10); |
| 4132 |
GGML_ASSERT(ne2 == ne12); |
| 4133 |
GGML_ASSERT(ne3 == ne13); |
| 4134 |
|
| 4135 |
GGML_ASSERT(ne2 % ne02 == 0); |
| 4136 |
GGML_ASSERT(ne3 % ne03 == 0); |
| 4137 |
|
| 4138 |
// we don't support permuted src0 or src1 |
| 4139 |
GGML_ASSERT(nb00 == sizeof(float)); |
| 4140 |
|
| 4141 |
// dst cannot be transposed or permuted |
| 4142 |
GGML_ASSERT(nb0 == sizeof(float)); |
| 4143 |
// GGML_ASSERT(nb0 <= nb1); |
| 4144 |
// GGML_ASSERT(nb1 <= nb2); |
| 4145 |
// GGML_ASSERT(nb2 <= nb3); |
| 4146 |
|
| 4147 |
// nb01 >= nb00 - src0 is not transposed |
| 4148 |
// compute by src0 rows |
| 4149 |
|
| 4150 |
if (ith == 0) { |
| 4151 |
ggml_vec_set_f32(ne0*ne1*ne2*ne3, (float *)dst->data, 0); |
| 4152 |
} |
| 4153 |
ggml_barrier(params->threadpool); |
| 4154 |
|
| 4155 |
// dst[:,:,:,:] = 0 |
| 4156 |
// for i2,i3: |
| 4157 |
// for i1: |
| 4158 |
// for i01: |
| 4159 |
// for i0: |
| 4160 |
// dst[i0,i1,i2,i3] += src0[i0,i01,i2,i3] * src1[i1,i01,i2,i3] |
| 4161 |
|
| 4162 |
// parallelize by last three dimensions |
| 4163 |
|
| 4164 |
// total rows in dst |
| 4165 |
const int64_t nr = ne1*ne2*ne3; |
| 4166 |
|
| 4167 |
// rows per thread |
| 4168 |
const int64_t dr = (nr + nth - 1)/nth; |
| 4169 |
|
| 4170 |
// row range for this thread |
| 4171 |
const int64_t ir0 = dr*ith; |
| 4172 |
const int64_t ir1 = MIN(ir0 + dr, nr); |
| 4173 |
|
| 4174 |
// block-tiling attempt |
| 4175 |
const int64_t blck_0 = MAX(GGML_VEC_MAD_UNROLL, 32); |
| 4176 |
const int64_t blck_1 = 16; |
| 4177 |
|
| 4178 |
// dps == dst per src0, used for group query attention |
| 4179 |
const int64_t dps2 = ne2 / ne02; |
| 4180 |
const int64_t dps3 = ne3 / ne03; |
| 4181 |
|
| 4182 |
for (int64_t bir = ir0; bir < ir1; bir += blck_1) { |
| 4183 |
const int64_t bir1 = MIN(bir + blck_1, ir1); |
| 4184 |
for (int64_t bi01 = 0; bi01 < ne01; bi01 += blck_0) { |
| 4185 |
const int64_t bne01 = MIN(bi01 + blck_0, ne01); |
| 4186 |
for (int64_t ir = bir; ir < bir1; ++ir) { |
| 4187 |
// dst indices |
| 4188 |
const int64_t i3 = ir/(ne2*ne1); |
| 4189 |
const int64_t i2 = (ir - i3*ne2*ne1)/ne1; |
| 4190 |
const int64_t i1 = (ir - i3*ne2*ne1 - i2*ne1); |
| 4191 |
|
| 4192 |
const int64_t i02 = i2 / dps2; |
| 4193 |
const int64_t i03 = i3 / dps3; |
| 4194 |
|
| 4195 |
//const int64_t i10 = i1; |
| 4196 |
const int64_t i12 = i2; |
| 4197 |
const int64_t i13 = i3; |
| 4198 |
|
| 4199 |
#if GGML_VEC_MAD_UNROLL > 2 |
| 4200 |
const int64_t bne01_unroll = bne01 - (bne01 % GGML_VEC_MAD_UNROLL); |
| 4201 |
for (int64_t i01 = bi01; i01 < bne01_unroll; i01 += GGML_VEC_MAD_UNROLL) { |
| 4202 |
const int64_t i11 = i01; |
| 4203 |
|
| 4204 |
float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03)); |
| 4205 |
float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13)); |
| 4206 |
float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3)); |
| 4207 |
|
| 4208 |
ggml_vec_mad_f32_unroll(ne0, nb01, nb11, d, s0, s1); |
| 4209 |
} |
| 4210 |
for (int64_t i01 = bne01_unroll; i01 < bne01; ++i01) { |
| 4211 |
const int64_t i11 = i01; |
| 4212 |
|
| 4213 |
float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03)); |
| 4214 |
float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13)); |
| 4215 |
float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3)); |
| 4216 |
|
| 4217 |
ggml_vec_mad_f32(ne0, d, s0, *s1); |
| 4218 |
} |
| 4219 |
#else |
| 4220 |
for (int64_t i01 = bi01; i01 < bne01; ++i01) { |
| 4221 |
const int64_t i11 = i01; |
| 4222 |
|
| 4223 |
float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03)); |
| 4224 |
float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13)); |
| 4225 |
float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3)); |
| 4226 |
|
| 4227 |
ggml_vec_mad_f32(ne0, d, s0, *s1); |
| 4228 |
} |
| 4229 |
#endif |
| 4230 |
} |
| 4231 |
} |
| 4232 |
} |
| 4233 |
} |
| 4234 |
|
| 4235 |
static void ggml_compute_forward_out_prod_q_f32( |
| 4236 |
const ggml_compute_params * params, |
| 4237 |
ggml_tensor * dst) { |
| 4238 |
|
| 4239 |
const ggml_tensor * src0 = dst->src[0]; |
| 4240 |
const ggml_tensor * src1 = dst->src[1]; |
| 4241 |
|
| 4242 |
GGML_TENSOR_BINARY_OP_LOCALS; |
| 4243 |
|
| 4244 |
const int ith = params->ith; |
| 4245 |
const int nth = params->nth; |
| 4246 |
|
| 4247 |
const ggml_type type = src0->type; |
| 4248 |
ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float; |
| 4249 |
|
| 4250 |
GGML_ASSERT(ne02 == ne12); |
| 4251 |
GGML_ASSERT(ne03 == ne13); |
| 4252 |
GGML_ASSERT(ne2 == ne12); |
| 4253 |
GGML_ASSERT(ne3 == ne13); |
| 4254 |
|
| 4255 |
// we don't support permuted src0 dim0 |
| 4256 |
GGML_ASSERT(nb00 == ggml_type_size(type)); |
| 4257 |
|
| 4258 |
// dst dim0 cannot be transposed or permuted |
| 4259 |
GGML_ASSERT(nb0 == sizeof(float)); |
| 4260 |
// GGML_ASSERT(nb0 <= nb1); |
| 4261 |
// GGML_ASSERT(nb1 <= nb2); |
| 4262 |
// GGML_ASSERT(nb2 <= nb3); |
| 4263 |
|
| 4264 |
GGML_ASSERT(ne0 == ne00); |
| 4265 |
GGML_ASSERT(ne1 == ne10); |
| 4266 |
GGML_ASSERT(ne2 == ne02); |
| 4267 |
GGML_ASSERT(ne3 == ne03); |
| 4268 |
|
| 4269 |
// nb01 >= nb00 - src0 is not transposed |
| 4270 |
// compute by src0 rows |
| 4271 |
|
| 4272 |
if (ith == 0) { |
| 4273 |
ggml_vec_set_f32(ne0*ne1*ne2*ne3, (float *)dst->data, 0); |
| 4274 |
} |
| 4275 |
ggml_barrier(params->threadpool); |
| 4276 |
|
| 4277 |
// parallelize by last three dimensions |
| 4278 |
|
| 4279 |
// total rows in dst |
| 4280 |
const int64_t nr = ne1*ne2*ne3; |
| 4281 |
|
| 4282 |
// rows per thread |
| 4283 |
const int64_t dr = (nr + nth - 1)/nth; |
| 4284 |
|
| 4285 |
// row range for this thread |
| 4286 |
const int64_t ir0 = dr*ith; |
| 4287 |
const int64_t ir1 = MIN(ir0 + dr, nr); |
| 4288 |
|
| 4289 |
// dst[:,:,:,:] = 0 |
| 4290 |
// for i2,i3: |
| 4291 |
// for i1: |
| 4292 |
// for i01: |
| 4293 |
// for i0: |
| 4294 |
// dst[i0,i1,i2,i3] += src0[i0,i01,i2,i3] * src1[i1,i01,i2,i3] |
| 4295 |
|
| 4296 |
float * wdata = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32) * ith; |
| 4297 |
|
| 4298 |
for (int64_t ir = ir0; ir < ir1; ++ir) { |
| 4299 |
// dst indices |
| 4300 |
const int64_t i3 = ir/(ne2*ne1); |
| 4301 |
const int64_t i2 = (ir - i3*ne2*ne1)/ne1; |
| 4302 |
const int64_t i1 = (ir - i3*ne2*ne1 - i2*ne1); |
| 4303 |
|
| 4304 |
const int64_t i02 = i2; |
| 4305 |
const int64_t i03 = i3; |
| 4306 |
|
| 4307 |
//const int64_t i10 = i1; |
| 4308 |
const int64_t i12 = i2; |
| 4309 |
const int64_t i13 = i3; |
| 4310 |
|
| 4311 |
for (int64_t i01 = 0; i01 < ne01; ++i01) { |
| 4312 |
const int64_t i11 = i01; |
| 4313 |
|
| 4314 |
float * s0 = (float *) ((char *) src0->data + ( i01*nb01 + i02*nb02 + i03*nb03)); |
| 4315 |
float * s1 = (float *) ((char *) src1->data + (i1*nb10 + i11*nb11 + i12*nb12 + i13*nb13)); |
| 4316 |
float * d = (float *) ((char *) dst->data + ( i1*nb1 + i2*nb2 + i3*nb3)); |
| 4317 |
|
| 4318 |
dequantize_row_q(s0, wdata, ne0); |
| 4319 |
ggml_vec_mad_f32(ne0, d, wdata, *s1); |
| 4320 |
} |
| 4321 |
} |
| 4322 |
} |
| 4323 |
|
| 4324 |
void ggml_compute_forward_out_prod( |
| 4325 |
const ggml_compute_params * params, |
| 4326 |
ggml_tensor * dst) { |
| 4327 |
|
| 4328 |
const ggml_tensor * src0 = dst->src[0]; |
| 4329 |
|
| 4330 |
switch (src0->type) { |
| 4331 |
case GGML_TYPE_Q4_0: |
| 4332 |
case GGML_TYPE_Q4_1: |
| 4333 |
case GGML_TYPE_Q5_0: |
| 4334 |
case GGML_TYPE_Q5_1: |
| 4335 |
case GGML_TYPE_Q8_0: |
| 4336 |
case GGML_TYPE_MXFP4: |
| 4337 |
case GGML_TYPE_Q2_K: |
| 4338 |
case GGML_TYPE_Q3_K: |
| 4339 |
case GGML_TYPE_Q4_K: |
| 4340 |
case GGML_TYPE_Q5_K: |
| 4341 |
case GGML_TYPE_Q6_K: |
| 4342 |
case GGML_TYPE_TQ1_0: |
| 4343 |
case GGML_TYPE_TQ2_0: |
| 4344 |
case GGML_TYPE_IQ2_XXS: |
| 4345 |
case GGML_TYPE_IQ2_XS: |
| 4346 |
case GGML_TYPE_IQ3_XXS: |
| 4347 |
case GGML_TYPE_IQ1_S: |
| 4348 |
case GGML_TYPE_IQ1_M: |
| 4349 |
case GGML_TYPE_IQ4_NL: |
| 4350 |
case GGML_TYPE_IQ4_XS: |
| 4351 |
case GGML_TYPE_IQ3_S: |
| 4352 |
case GGML_TYPE_IQ2_S: |
| 4353 |
{ |
| 4354 |
ggml_compute_forward_out_prod_q_f32(params, dst); |
| 4355 |
} break; |
| 4356 |
case GGML_TYPE_F16: |
| 4357 |
{ |
| 4358 |
GGML_ABORT("fatal error"); // todo |
| 4359 |
// ggml_compute_forward_out_prod_f16_f32(params, dst); |
| 4360 |
} |
| 4361 |
case GGML_TYPE_F32: |
| 4362 |
{ |
| 4363 |
ggml_compute_forward_out_prod_f32(params, dst); |
| 4364 |
} break; |
| 4365 |
default: |
| 4366 |
{ |
| 4367 |
GGML_ABORT("fatal error"); |
| 4368 |
} |
| 4369 |
} |
| 4370 |
} |
| 4371 |
|
| 4372 |
// ggml_compute_forward_scale |
| 4373 |
|
| 4374 |
static void ggml_compute_forward_scale_f32( |
| 4375 |
const ggml_compute_params * params, |
| 4376 |
ggml_tensor * dst) { |
| 4377 |
|
| 4378 |
const ggml_tensor * src0 = dst->src[0]; |
| 4379 |
|
| 4380 |
GGML_ASSERT(ggml_is_contiguous(src0)); |
| 4381 |
GGML_ASSERT(ggml_is_contiguous(dst)); |
| 4382 |
GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 4383 |
|
| 4384 |
float s; // scale factor |
| 4385 |
float b; // bias |
| 4386 |
|
| 4387 |
memcpy(&s, (float *) dst->op_params + 0, sizeof(float)); |
| 4388 |
memcpy(&b, (float *) dst->op_params + 1, sizeof(float)); |
| 4389 |
|
| 4390 |
const int ith = params->ith; |
| 4391 |
const int nth = params->nth; |
| 4392 |
|
| 4393 |
const int nc = src0->ne[0]; |
| 4394 |
const int nr = ggml_nrows(src0); |
| 4395 |
|
| 4396 |
// rows per thread |
| 4397 |
const int dr = (nr + nth - 1)/nth; |
| 4398 |
|
| 4399 |
// row range for this thread |
| 4400 |
const int ir0 = dr*ith; |
| 4401 |
const int ir1 = MIN(ir0 + dr, nr); |
| 4402 |
|
| 4403 |
const size_t nb01 = src0->nb[1]; |
| 4404 |
|
| 4405 |
const size_t nb1 = dst->nb[1]; |
| 4406 |
|
| 4407 |
if (b == 0.0f) { |
| 4408 |
for (int i1 = ir0; i1 < ir1; i1++) { |
| 4409 |
if (dst->data != src0->data) { |
| 4410 |
// src0 is same shape as dst => same indices |
| 4411 |
// TODO: add x parameter to ggml_vec_scale_f32 and remove this memcpy |
| 4412 |
memcpy((char *)dst->data + i1*nb1, (char *)src0->data + i1*nb01, nc * sizeof(float)); |
| 4413 |
} |
| 4414 |
ggml_vec_scale_f32(nc, (float *) ((char *) dst->data + i1*nb1), s); |
| 4415 |
} |
| 4416 |
} else { |
| 4417 |
for (int i1 = ir0; i1 < ir1; i1++) { |
| 4418 |
ggml_vec_mad1_f32(nc, |
| 4419 |
(float *) ((char *) dst->data + i1*nb1), |
| 4420 |
(float *) ((char *) src0->data + i1*nb1), |
| 4421 |
s, b); |
| 4422 |
} |
| 4423 |
} |
| 4424 |
} |
| 4425 |
|
| 4426 |
void ggml_compute_forward_scale( |
| 4427 |
const ggml_compute_params * params, |
| 4428 |
ggml_tensor * dst) { |
| 4429 |
|
| 4430 |
const ggml_tensor * src0 = dst->src[0]; |
| 4431 |
|
| 4432 |
switch (src0->type) { |
| 4433 |
case GGML_TYPE_F32: |
| 4434 |
{ |
| 4435 |
ggml_compute_forward_scale_f32(params, dst); |
| 4436 |
} break; |
| 4437 |
default: |
| 4438 |
{ |
| 4439 |
GGML_ABORT("fatal error"); |
| 4440 |
} |
| 4441 |
} |
| 4442 |
} |
| 4443 |
|
| 4444 |
// ggml_compute_forward_set |
| 4445 |
|
| 4446 |
static void ggml_compute_forward_set_f32( |
| 4447 |
const ggml_compute_params * params, |
| 4448 |
ggml_tensor * dst) { |
| 4449 |
|
| 4450 |
const ggml_tensor * src0 = dst->src[0]; |
| 4451 |
const ggml_tensor * src1 = dst->src[1]; |
| 4452 |
|
| 4453 |
GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 4454 |
GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0)); |
| 4455 |
|
| 4456 |
// view src0 and dst with these strides and data offset inbytes during set |
| 4457 |
// nb0 is implicitly element_size because src0 and dst are contiguous |
| 4458 |
size_t nb1 = ((int32_t *) dst->op_params)[0]; |
| 4459 |
size_t nb2 = ((int32_t *) dst->op_params)[1]; |
| 4460 |
size_t nb3 = ((int32_t *) dst->op_params)[2]; |
| 4461 |
size_t offset = ((int32_t *) dst->op_params)[3]; |
| 4462 |
bool inplace = (bool) ((int32_t *) dst->op_params)[4]; |
| 4463 |
|
| 4464 |
if (!inplace) { |
| 4465 |
if (params->ith == 0) { |
| 4466 |
// memcpy needs to be synchronized across threads to avoid race conditions. |
| 4467 |
// => do it in INIT phase |
| 4468 |
memcpy( |
| 4469 |
((char *) dst->data), |
| 4470 |
((char *) src0->data), |
| 4471 |
ggml_nbytes(dst)); |
| 4472 |
} |
| 4473 |
ggml_barrier(params->threadpool); |
| 4474 |
} |
| 4475 |
|
| 4476 |
const int ith = params->ith; |
| 4477 |
const int nth = params->nth; |
| 4478 |
|
| 4479 |
const int nr = ggml_nrows(src1); |
| 4480 |
const int nc = src1->ne[0]; |
| 4481 |
|
| 4482 |
GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) |
| 4483 |
GGML_TENSOR_LOCALS(size_t, nb1, src1, nb) |
| 4484 |
|
| 4485 |
// src0 and dst as viewed during set |
| 4486 |
const size_t nb0 = ggml_element_size(src0); |
| 4487 |
|
| 4488 |
const int im0 = (ne10 == 0 ? 0 : ne10-1); |
| 4489 |
const int im1 = (ne11 == 0 ? 0 : ne11-1); |
| 4490 |
const int im2 = (ne12 == 0 ? 0 : ne12-1); |
| 4491 |
const int im3 = (ne13 == 0 ? 0 : ne13-1); |
| 4492 |
|
| 4493 |
GGML_ASSERT(offset + im0*nb0 + im1*nb1 + im2*nb2 + im3*nb3 <= ggml_nbytes(dst)); |
| 4494 |
|
| 4495 |
GGML_ASSERT(nb10 == sizeof(float)); |
| 4496 |
|
| 4497 |
// rows per thread |
| 4498 |
const int dr = (nr + nth - 1)/nth; |
| 4499 |
|
| 4500 |
// row range for this thread |
| 4501 |
const int ir0 = dr*ith; |
| 4502 |
const int ir1 = MIN(ir0 + dr, nr); |
| 4503 |
|
| 4504 |
for (int ir = ir0; ir < ir1; ++ir) { |
| 4505 |
// src0 and dst are viewed with shape of src1 and offset |
| 4506 |
// => same indices |
| 4507 |
const int i3 = ir/(ne12*ne11); |
| 4508 |
const int i2 = (ir - i3*ne12*ne11)/ne11; |
| 4509 |
const int i1 = (ir - i3*ne12*ne11 - i2*ne11); |
| 4510 |
|
| 4511 |
ggml_vec_cpy_f32(nc, |
| 4512 |
(float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset), |
| 4513 |
(float *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11)); |
| 4514 |
} |
| 4515 |
} |
| 4516 |
|
| 4517 |
static void ggml_compute_forward_set_i32( |
| 4518 |
const ggml_compute_params * params, |
| 4519 |
ggml_tensor * dst) { |
| 4520 |
|
| 4521 |
const ggml_tensor * src0 = dst->src[0]; |
| 4522 |
const ggml_tensor * src1 = dst->src[1]; |
| 4523 |
|
| 4524 |
GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 4525 |
GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0)); |
| 4526 |
|
| 4527 |
// view src0 and dst with these strides and data offset inbytes during set |
| 4528 |
// nb0 is implicitly element_size because src0 and dst are contiguous |
| 4529 |
size_t nb1 = ((int32_t *) dst->op_params)[0]; |
| 4530 |
size_t nb2 = ((int32_t *) dst->op_params)[1]; |
| 4531 |
size_t nb3 = ((int32_t *) dst->op_params)[2]; |
| 4532 |
size_t offset = ((int32_t *) dst->op_params)[3]; |
| 4533 |
bool inplace = (bool) ((int32_t *) dst->op_params)[4]; |
| 4534 |
|
| 4535 |
if (!inplace) { |
| 4536 |
if (params->ith == 0) { |
| 4537 |
// memcpy needs to be synchronized across threads to avoid race conditions. |
| 4538 |
// => do it in INIT phase |
| 4539 |
memcpy( |
| 4540 |
((char *) dst->data), |
| 4541 |
((char *) src0->data), |
| 4542 |
ggml_nbytes(dst)); |
| 4543 |
} |
| 4544 |
ggml_barrier(params->threadpool); |
| 4545 |
} |
| 4546 |
|
| 4547 |
const int ith = params->ith; |
| 4548 |
const int nth = params->nth; |
| 4549 |
|
| 4550 |
const int nr = ggml_nrows(src1); |
| 4551 |
const int nc = src1->ne[0]; |
| 4552 |
|
| 4553 |
GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) |
| 4554 |
GGML_TENSOR_LOCALS(size_t, nb1, src1, nb) |
| 4555 |
|
| 4556 |
// src0 and dst as viewed during set |
| 4557 |
const size_t nb0 = ggml_element_size(src0); |
| 4558 |
|
| 4559 |
const int im0 = (ne10 == 0 ? 0 : ne10-1); |
| 4560 |
const int im1 = (ne11 == 0 ? 0 : ne11-1); |
| 4561 |
const int im2 = (ne12 == 0 ? 0 : ne12-1); |
| 4562 |
const int im3 = (ne13 == 0 ? 0 : ne13-1); |
| 4563 |
|
| 4564 |
GGML_ASSERT(offset + im0*nb0 + im1*nb1 + im2*nb2 + im3*nb3 <= ggml_nbytes(dst)); |
| 4565 |
|
| 4566 |
GGML_ASSERT(nb10 == sizeof(int32_t)); |
| 4567 |
|
| 4568 |
// rows per thread |
| 4569 |
const int dr = (nr + nth - 1)/nth; |
| 4570 |
|
| 4571 |
// row range for this thread |
| 4572 |
const int ir0 = dr*ith; |
| 4573 |
const int ir1 = MIN(ir0 + dr, nr); |
| 4574 |
|
| 4575 |
for (int ir = ir0; ir < ir1; ++ir) { |
| 4576 |
// src0 and dst are viewed with shape of src1 and offset |
| 4577 |
// => same indices |
| 4578 |
const int i3 = ir/(ne12*ne11); |
| 4579 |
const int i2 = (ir - i3*ne12*ne11)/ne11; |
| 4580 |
const int i1 = (ir - i3*ne12*ne11 - i2*ne11); |
| 4581 |
|
| 4582 |
ggml_vec_cpy_i32(nc, |
| 4583 |
(int32_t *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + offset), |
| 4584 |
(int32_t *) ((char *) src1->data + i3*nb13 + i2*nb12 + i1*nb11)); |
| 4585 |
} |
| 4586 |
} |
| 4587 |
|
| 4588 |
void ggml_compute_forward_set( |
| 4589 |
const ggml_compute_params * params, |
| 4590 |
ggml_tensor * dst) { |
| 4591 |
|
| 4592 |
const ggml_tensor * src0 = dst->src[0]; |
| 4593 |
|
| 4594 |
switch (src0->type) { |
| 4595 |
case GGML_TYPE_F32: |
| 4596 |
{ |
| 4597 |
ggml_compute_forward_set_f32(params, dst); |
| 4598 |
} break; |
| 4599 |
case GGML_TYPE_I32: |
| 4600 |
{ |
| 4601 |
ggml_compute_forward_set_i32(params, dst); |
| 4602 |
} break; |
| 4603 |
case GGML_TYPE_F16: |
| 4604 |
case GGML_TYPE_BF16: |
| 4605 |
case GGML_TYPE_Q4_0: |
| 4606 |
case GGML_TYPE_Q4_1: |
| 4607 |
case GGML_TYPE_Q5_0: |
| 4608 |
case GGML_TYPE_Q5_1: |
| 4609 |
case GGML_TYPE_Q8_0: |
| 4610 |
case GGML_TYPE_Q8_1: |
| 4611 |
case GGML_TYPE_MXFP4: |
| 4612 |
case GGML_TYPE_Q2_K: |
| 4613 |
case GGML_TYPE_Q3_K: |
| 4614 |
case GGML_TYPE_Q4_K: |
| 4615 |
case GGML_TYPE_Q5_K: |
| 4616 |
case GGML_TYPE_Q6_K: |
| 4617 |
case GGML_TYPE_TQ1_0: |
| 4618 |
case GGML_TYPE_TQ2_0: |
| 4619 |
case GGML_TYPE_IQ2_XXS: |
| 4620 |
case GGML_TYPE_IQ2_XS: |
| 4621 |
case GGML_TYPE_IQ3_XXS: |
| 4622 |
case GGML_TYPE_IQ1_S: |
| 4623 |
case GGML_TYPE_IQ1_M: |
| 4624 |
case GGML_TYPE_IQ4_NL: |
| 4625 |
case GGML_TYPE_IQ4_XS: |
| 4626 |
case GGML_TYPE_IQ3_S: |
| 4627 |
case GGML_TYPE_IQ2_S: |
| 4628 |
default: |
| 4629 |
{ |
| 4630 |
GGML_ABORT("fatal error"); |
| 4631 |
} |
| 4632 |
} |
| 4633 |
} |
| 4634 |
|
| 4635 |
// ggml_compute_forward_cpy |
| 4636 |
|
| 4637 |
void ggml_compute_forward_cpy( |
| 4638 |
const ggml_compute_params * params, |
| 4639 |
ggml_tensor * dst) { |
| 4640 |
ggml_compute_forward_dup(params, dst); |
| 4641 |
} |
| 4642 |
|
| 4643 |
// ggml_compute_forward_cont |
| 4644 |
|
| 4645 |
void ggml_compute_forward_cont( |
| 4646 |
const ggml_compute_params * params, |
| 4647 |
ggml_tensor * dst) { |
| 4648 |
ggml_compute_forward_dup(params, dst); |
| 4649 |
} |
| 4650 |
|
| 4651 |
// ggml_compute_forward_get_rows |
| 4652 |
|
| 4653 |
static void ggml_compute_forward_get_rows_q( |
| 4654 |
const ggml_compute_params * params, |
| 4655 |
ggml_tensor * dst) { |
| 4656 |
|
| 4657 |
const ggml_tensor * src0 = dst->src[0]; |
| 4658 |
const ggml_tensor * src1 = dst->src[1]; |
| 4659 |
|
| 4660 |
GGML_TENSOR_BINARY_OP_LOCALS |
| 4661 |
|
| 4662 |
const int64_t nc = ne00; |
| 4663 |
const int64_t nr = ggml_nelements(src1); |
| 4664 |
|
| 4665 |
const ggml_type type = src0->type; |
| 4666 |
ggml_to_float_t const dequantize_row_q = ggml_get_type_traits(type)->to_float; |
| 4667 |
|
| 4668 |
assert(ne0 == nc); |
| 4669 |
assert(ne02 == ne11); |
| 4670 |
assert(nb00 == ggml_type_size(type)); |
| 4671 |
assert(ggml_nrows(dst) == nr); |
| 4672 |
|
| 4673 |
const int ith = params->ith; |
| 4674 |
const int nth = params->nth; |
| 4675 |
|
| 4676 |
// rows per thread |
| 4677 |
const int dr = (nr + nth - 1)/nth; |
| 4678 |
|
| 4679 |
// row range for this thread |
| 4680 |
const int ir0 = dr*ith; |
| 4681 |
const int ir1 = MIN(ir0 + dr, nr); |
| 4682 |
|
| 4683 |
for (int64_t i = ir0; i < ir1; ++i) { |
| 4684 |
const int64_t i12 = i/(ne11*ne10); |
| 4685 |
const int64_t i11 = (i - i12*ne11*ne10)/ne10; |
| 4686 |
const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10); |
| 4687 |
const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12); |
| 4688 |
|
| 4689 |
GGML_ASSERT(i01 >= 0 && i01 < ne01); |
| 4690 |
|
| 4691 |
dequantize_row_q( |
| 4692 |
(const void *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03), |
| 4693 |
(float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3), nc); |
| 4694 |
} |
| 4695 |
} |
| 4696 |
|
| 4697 |
static void ggml_compute_forward_get_rows_f16( |
| 4698 |
const ggml_compute_params * params, |
| 4699 |
ggml_tensor * dst) { |
| 4700 |
|
| 4701 |
const ggml_tensor * src0 = dst->src[0]; |
| 4702 |
const ggml_tensor * src1 = dst->src[1]; |
| 4703 |
|
| 4704 |
GGML_TENSOR_BINARY_OP_LOCALS |
| 4705 |
|
| 4706 |
const int64_t nc = ne00; |
| 4707 |
const int64_t nr = ggml_nelements(src1); |
| 4708 |
|
| 4709 |
assert(ne0 == nc); |
| 4710 |
assert(ne02 == ne11); |
| 4711 |
assert(nb00 == sizeof(ggml_fp16_t)); |
| 4712 |
assert(ggml_nrows(dst) == nr); |
| 4713 |
|
| 4714 |
const int ith = params->ith; |
| 4715 |
const int nth = params->nth; |
| 4716 |
|
| 4717 |
// rows per thread |
| 4718 |
const int dr = (nr + nth - 1)/nth; |
| 4719 |
|
| 4720 |
// row range for this thread |
| 4721 |
const int ir0 = dr*ith; |
| 4722 |
const int ir1 = MIN(ir0 + dr, nr); |
| 4723 |
|
| 4724 |
for (int64_t i = ir0; i < ir1; ++i) { |
| 4725 |
const int64_t i12 = i/(ne11*ne10); |
| 4726 |
const int64_t i11 = (i - i12*ne11*ne10)/ne10; |
| 4727 |
const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10); |
| 4728 |
const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12); |
| 4729 |
|
| 4730 |
GGML_ASSERT(i01 >= 0 && i01 < ne01); |
| 4731 |
|
| 4732 |
ggml_cpu_fp16_to_fp32( |
| 4733 |
(const ggml_fp16_t*) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03), |
| 4734 |
(float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3), nc); |
| 4735 |
} |
| 4736 |
} |
| 4737 |
|
| 4738 |
static void ggml_compute_forward_get_rows_bf16( |
| 4739 |
const ggml_compute_params * params, |
| 4740 |
ggml_tensor * dst) { |
| 4741 |
|
| 4742 |
const ggml_tensor * src0 = dst->src[0]; |
| 4743 |
const ggml_tensor * src1 = dst->src[1]; |
| 4744 |
|
| 4745 |
GGML_TENSOR_BINARY_OP_LOCALS |
| 4746 |
|
| 4747 |
const int64_t nc = ne00; |
| 4748 |
const int64_t nr = ggml_nelements(src1); |
| 4749 |
|
| 4750 |
assert(ne0 == nc); |
| 4751 |
assert(ne02 == ne11); |
| 4752 |
assert(nb00 == sizeof(ggml_bf16_t)); |
| 4753 |
assert(ggml_nrows(dst) == nr); |
| 4754 |
|
| 4755 |
const int ith = params->ith; |
| 4756 |
const int nth = params->nth; |
| 4757 |
|
| 4758 |
// rows per thread |
| 4759 |
const int dr = (nr + nth - 1)/nth; |
| 4760 |
|
| 4761 |
// row range for this thread |
| 4762 |
const int ir0 = dr*ith; |
| 4763 |
const int ir1 = MIN(ir0 + dr, nr); |
| 4764 |
|
| 4765 |
for (int64_t i = ir0; i < ir1; ++i) { |
| 4766 |
const int64_t i12 = i/(ne11*ne10); |
| 4767 |
const int64_t i11 = (i - i12*ne11*ne10)/ne10; |
| 4768 |
const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10); |
| 4769 |
const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12); |
| 4770 |
|
| 4771 |
GGML_ASSERT(i01 >= 0 && i01 < ne01); |
| 4772 |
|
| 4773 |
ggml_cpu_bf16_to_fp32( |
| 4774 |
(const ggml_bf16_t *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03), |
| 4775 |
(float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3), nc); |
| 4776 |
} |
| 4777 |
} |
| 4778 |
|
| 4779 |
static void ggml_compute_forward_get_rows_f32( |
| 4780 |
const ggml_compute_params * params, |
| 4781 |
ggml_tensor * dst) { |
| 4782 |
|
| 4783 |
const ggml_tensor * src0 = dst->src[0]; |
| 4784 |
const ggml_tensor * src1 = dst->src[1]; |
| 4785 |
|
| 4786 |
GGML_TENSOR_BINARY_OP_LOCALS |
| 4787 |
|
| 4788 |
const int64_t nc = ne00; |
| 4789 |
const int64_t nr = ggml_nelements(src1); |
| 4790 |
|
| 4791 |
assert(ne0 == nc); |
| 4792 |
assert(ne02 == ne11); |
| 4793 |
assert(nb00 == sizeof(float)); |
| 4794 |
assert(ggml_nrows(dst) == nr); |
| 4795 |
|
| 4796 |
const int ith = params->ith; |
| 4797 |
const int nth = params->nth; |
| 4798 |
|
| 4799 |
// rows per thread |
| 4800 |
const int dr = (nr + nth - 1)/nth; |
| 4801 |
|
| 4802 |
// row range for this thread |
| 4803 |
const int ir0 = dr*ith; |
| 4804 |
const int ir1 = MIN(ir0 + dr, nr); |
| 4805 |
|
| 4806 |
for (int64_t i = ir0; i < ir1; ++i) { |
| 4807 |
const int64_t i12 = i/(ne11*ne10); |
| 4808 |
const int64_t i11 = (i - i12*ne11*ne10)/ne10; |
| 4809 |
const int64_t i10 = (i - i12*ne11*ne10 - i11*ne10); |
| 4810 |
const int64_t i01 = *(int32_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12); |
| 4811 |
|
| 4812 |
GGML_ASSERT(i01 >= 0 && i01 < ne01); |
| 4813 |
|
| 4814 |
ggml_vec_cpy_f32(nc, |
| 4815 |
(float *) ((char *) dst->data + i10*nb1 + i11*nb2 + i12*nb3), |
| 4816 |
(float *) ((char *) src0->data + i01*nb01 + i11*nb02 + i12*nb03)); |
| 4817 |
} |
| 4818 |
} |
| 4819 |
|
| 4820 |
void ggml_compute_forward_get_rows( |
| 4821 |
const ggml_compute_params * params, |
| 4822 |
ggml_tensor * dst) { |
| 4823 |
|
| 4824 |
const ggml_tensor * src0 = dst->src[0]; |
| 4825 |
|
| 4826 |
switch (src0->type) { |
| 4827 |
case GGML_TYPE_Q4_0: |
| 4828 |
case GGML_TYPE_Q4_1: |
| 4829 |
case GGML_TYPE_Q5_0: |
| 4830 |
case GGML_TYPE_Q5_1: |
| 4831 |
case GGML_TYPE_Q8_0: |
| 4832 |
case GGML_TYPE_Q8_1: |
| 4833 |
case GGML_TYPE_MXFP4: |
| 4834 |
case GGML_TYPE_Q2_K: |
| 4835 |
case GGML_TYPE_Q3_K: |
| 4836 |
case GGML_TYPE_Q4_K: |
| 4837 |
case GGML_TYPE_Q5_K: |
| 4838 |
case GGML_TYPE_Q6_K: |
| 4839 |
case GGML_TYPE_TQ1_0: |
| 4840 |
case GGML_TYPE_TQ2_0: |
| 4841 |
case GGML_TYPE_IQ2_XXS: |
| 4842 |
case GGML_TYPE_IQ2_XS: |
| 4843 |
case GGML_TYPE_IQ3_XXS: |
| 4844 |
case GGML_TYPE_IQ1_S: |
| 4845 |
case GGML_TYPE_IQ1_M: |
| 4846 |
case GGML_TYPE_IQ4_NL: |
| 4847 |
case GGML_TYPE_IQ4_XS: |
| 4848 |
case GGML_TYPE_IQ3_S: |
| 4849 |
case GGML_TYPE_IQ2_S: |
| 4850 |
{ |
| 4851 |
ggml_compute_forward_get_rows_q(params, dst); |
| 4852 |
} break; |
| 4853 |
case GGML_TYPE_F16: |
| 4854 |
{ |
| 4855 |
ggml_compute_forward_get_rows_f16(params, dst); |
| 4856 |
} break; |
| 4857 |
case GGML_TYPE_BF16: |
| 4858 |
{ |
| 4859 |
ggml_compute_forward_get_rows_bf16(params, dst); |
| 4860 |
} break; |
| 4861 |
case GGML_TYPE_F32: |
| 4862 |
case GGML_TYPE_I32: |
| 4863 |
{ |
| 4864 |
ggml_compute_forward_get_rows_f32(params, dst); |
| 4865 |
} break; |
| 4866 |
default: |
| 4867 |
{ |
| 4868 |
GGML_ABORT("fatal error"); |
| 4869 |
} |
| 4870 |
} |
| 4871 |
|
| 4872 |
//static bool first = true; |
| 4873 |
//printf("ne0 = %d, ne1 = %d, ne2 = %d\n", dst->ne[0], dst->ne[1], dst->ne[2]); |
| 4874 |
//if (first) { |
| 4875 |
// first = false; |
| 4876 |
//} else { |
| 4877 |
// for (int k = 0; k < dst->ne[1]; ++k) { |
| 4878 |
// for (int j = 0; j < dst->ne[0]/16; ++j) { |
| 4879 |
// for (int i = 0; i < 16; ++i) { |
| 4880 |
// printf("%8.4f ", ((float *) dst->data)[k*dst->ne[0] + j*16 + i]); |
| 4881 |
// } |
| 4882 |
// printf("\n"); |
| 4883 |
// } |
| 4884 |
// printf("\n"); |
| 4885 |
// } |
| 4886 |
// printf("\n"); |
| 4887 |
// exit(0); |
| 4888 |
//} |
| 4889 |
} |
| 4890 |
|
| 4891 |
template<typename idx_t> |
| 4892 |
static void ggml_compute_forward_set_rows_f32( |
| 4893 |
const ggml_compute_params * params, |
| 4894 |
ggml_tensor * dst) { |
| 4895 |
|
| 4896 |
const ggml_tensor * src0 = dst->src[0]; |
| 4897 |
const ggml_tensor * src1 = dst->src[1]; |
| 4898 |
|
| 4899 |
GGML_TENSOR_BINARY_OP_LOCALS |
| 4900 |
|
| 4901 |
const int64_t nc = ne00; |
| 4902 |
const int64_t nr = ne01; |
| 4903 |
|
| 4904 |
assert(ne0 == nc); |
| 4905 |
assert(ne2 == ne02); |
| 4906 |
assert(ne3 == ne03); |
| 4907 |
assert(src0->type == GGML_TYPE_F32); |
| 4908 |
assert(ne02 % ne11 == 0); |
| 4909 |
assert(ne03 % ne12 == 0); |
| 4910 |
|
| 4911 |
const int ith = params->ith; |
| 4912 |
const int nth = params->nth; |
| 4913 |
|
| 4914 |
// rows per thread |
| 4915 |
const int64_t dr = (nr + nth - 1)/nth; |
| 4916 |
|
| 4917 |
// row range for this thread |
| 4918 |
const int64_t ir0 = dr*ith; |
| 4919 |
const int64_t ir1 = std::min(ir0 + dr, nr); |
| 4920 |
|
| 4921 |
ggml_from_float_t const from_float = ggml_get_type_traits_cpu(dst->type)->from_float; |
| 4922 |
|
| 4923 |
for (int64_t i03 = 0; i03 < ne03; ++i03) { |
| 4924 |
for (int64_t i02 = 0; i02 < ne02; ++i02) { |
| 4925 |
for (int64_t i = ir0; i < ir1; ++i) { |
| 4926 |
const int64_t i12 = i03%ne12; |
| 4927 |
const int64_t i11 = i02%ne11; |
| 4928 |
const int64_t i10 = i; |
| 4929 |
|
| 4930 |
const int64_t i1 = *(idx_t *) ((char *) src1->data + i10*nb10 + i11*nb11 + i12*nb12); |
| 4931 |
|
| 4932 |
GGML_ASSERT(i1 >= 0 && i1 < ne1); |
| 4933 |
|
| 4934 |
from_float( |
| 4935 |
(const float *) ((char *) src0->data + i*nb01 + i02*nb02 + i03*nb03), |
| 4936 |
((char *) dst->data + i1*nb1 + i02*nb2 + i03*nb3), nc); |
| 4937 |
} |
| 4938 |
} |
| 4939 |
} |
| 4940 |
} |
| 4941 |
|
| 4942 |
void ggml_compute_forward_set_rows( |
| 4943 |
const ggml_compute_params * params, |
| 4944 |
ggml_tensor * dst) { |
| 4945 |
|
| 4946 |
const ggml_tensor * src0 = dst->src[0]; |
| 4947 |
const ggml_tensor * src1 = dst->src[1]; |
| 4948 |
|
| 4949 |
switch (src0->type) { |
| 4950 |
case GGML_TYPE_F32: |
| 4951 |
{ |
| 4952 |
if (src1->type == GGML_TYPE_I64) { |
| 4953 |
ggml_compute_forward_set_rows_f32<int64_t>(params, dst); |
| 4954 |
} else if (src1->type == GGML_TYPE_I32) { |
| 4955 |
ggml_compute_forward_set_rows_f32<int32_t>(params, dst); |
| 4956 |
} else { |
| 4957 |
GGML_ABORT("src1->type = %d (%s) not supported", src1->type, ggml_type_name(src1->type)); |
| 4958 |
} |
| 4959 |
} break; |
| 4960 |
default: |
| 4961 |
{ |
| 4962 |
GGML_ABORT("src0->type = %d (%s) not supported", src0->type, ggml_type_name(src0->type)); |
| 4963 |
} |
| 4964 |
} |
| 4965 |
} |
| 4966 |
|
| 4967 |
// ggml_compute_forward_get_rows_back |
| 4968 |
|
| 4969 |
static void ggml_compute_forward_get_rows_back_f32_f16( |
| 4970 |
const ggml_compute_params * params, |
| 4971 |
ggml_tensor * dst) { |
| 4972 |
|
| 4973 |
const ggml_tensor * src0 = dst->src[0]; |
| 4974 |
const ggml_tensor * src1 = dst->src[1]; |
| 4975 |
|
| 4976 |
if (params->ith != 0) { |
| 4977 |
return; |
| 4978 |
} |
| 4979 |
|
| 4980 |
GGML_ASSERT(ggml_is_contiguous(dst)); |
| 4981 |
|
| 4982 |
// ggml_compute_forward_dup_same_cont(params, opt0, dst); |
| 4983 |
|
| 4984 |
memset(dst->data, 0, ggml_nbytes(dst)); |
| 4985 |
|
| 4986 |
const int nc = src0->ne[0]; |
| 4987 |
const int nr = ggml_nelements(src1); |
| 4988 |
|
| 4989 |
GGML_ASSERT( dst->ne[0] == nc); |
| 4990 |
GGML_ASSERT(src0->nb[0] == sizeof(ggml_fp16_t)); |
| 4991 |
|
| 4992 |
for (int i = 0; i < nr; ++i) { |
| 4993 |
const int r = ((int32_t *) src1->data)[i]; |
| 4994 |
|
| 4995 |
for (int j = 0; j < nc; ++j) { |
| 4996 |
ggml_fp16_t v = ((ggml_fp16_t *) ((char *) src0->data + i*src0->nb[1]))[j]; |
| 4997 |
((float *) ((char *) dst->data + r*dst->nb[1]))[j] += GGML_CPU_FP16_TO_FP32(v); |
| 4998 |
} |
| 4999 |
} |
| 5000 |
} |
| 5001 |
|
| 5002 |
static void ggml_compute_forward_get_rows_back_f32( |
| 5003 |
const ggml_compute_params * params, |
| 5004 |
ggml_tensor * dst) { |
| 5005 |
|
| 5006 |
const ggml_tensor * src0 = dst->src[0]; |
| 5007 |
const ggml_tensor * src1 = dst->src[1]; |
| 5008 |
|
| 5009 |
if (params->ith != 0) { |
| 5010 |
return; |
| 5011 |
} |
| 5012 |
|
| 5013 |
GGML_ASSERT(ggml_is_contiguous(dst)); |
| 5014 |
|
| 5015 |
// ggml_compute_forward_dup_same_cont(params, opt0, dst); |
| 5016 |
|
| 5017 |
memset(dst->data, 0, ggml_nbytes(dst)); |
| 5018 |
|
| 5019 |
const int nc = src0->ne[0]; |
| 5020 |
const int nr = ggml_nelements(src1); |
| 5021 |
|
| 5022 |
GGML_ASSERT( dst->ne[0] == nc); |
| 5023 |
GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 5024 |
|
| 5025 |
for (int i = 0; i < nr; ++i) { |
| 5026 |
const int r = ((int32_t *) src1->data)[i]; |
| 5027 |
|
| 5028 |
ggml_vec_add_f32(nc, |
| 5029 |
(float *) ((char *) dst->data + r*dst->nb[1]), |
| 5030 |
(float *) ((char *) dst->data + r*dst->nb[1]), |
| 5031 |
(float *) ((char *) src0->data + i*src0->nb[1])); |
| 5032 |
} |
| 5033 |
} |
| 5034 |
|
| 5035 |
void ggml_compute_forward_get_rows_back( |
| 5036 |
const ggml_compute_params * params, |
| 5037 |
ggml_tensor * dst) { |
| 5038 |
|
| 5039 |
const ggml_tensor * src0 = dst->src[0]; |
| 5040 |
|
| 5041 |
switch (src0->type) { |
| 5042 |
case GGML_TYPE_F16: |
| 5043 |
{ |
| 5044 |
ggml_compute_forward_get_rows_back_f32_f16(params, dst); |
| 5045 |
} break; |
| 5046 |
case GGML_TYPE_F32: |
| 5047 |
{ |
| 5048 |
ggml_compute_forward_get_rows_back_f32(params, dst); |
| 5049 |
} break; |
| 5050 |
default: |
| 5051 |
{ |
| 5052 |
GGML_ABORT("fatal error"); |
| 5053 |
} |
| 5054 |
} |
| 5055 |
|
| 5056 |
//static bool first = true; |
| 5057 |
//printf("ne0 = %d, ne1 = %d, ne2 = %d\n", dst->ne[0], dst->ne[1], dst->ne[2]); |
| 5058 |
//if (first) { |
| 5059 |
// first = false; |
| 5060 |
//} else { |
| 5061 |
// for (int k = 0; k < dst->ne[1]; ++k) { |
| 5062 |
// for (int j = 0; j < dst->ne[0]/16; ++j) { |
| 5063 |
// for (int i = 0; i < 16; ++i) { |
| 5064 |
// printf("%8.4f ", ((float *) dst->data)[k*dst->ne[0] + j*16 + i]); |
| 5065 |
// } |
| 5066 |
// printf("\n"); |
| 5067 |
// } |
| 5068 |
// printf("\n"); |
| 5069 |
// } |
| 5070 |
// printf("\n"); |
| 5071 |
// exit(0); |
| 5072 |
//} |
| 5073 |
} |
| 5074 |
|
| 5075 |
// ggml_compute_forward_diag |
| 5076 |
|
| 5077 |
static void ggml_compute_forward_diag_f32( |
| 5078 |
const ggml_compute_params * params, |
| 5079 |
ggml_tensor * dst) { |
| 5080 |
|
| 5081 |
const ggml_tensor * src0 = dst->src[0]; |
| 5082 |
|
| 5083 |
if (params->ith != 0) { |
| 5084 |
return; |
| 5085 |
} |
| 5086 |
|
| 5087 |
// TODO: handle transposed/permuted matrices |
| 5088 |
|
| 5089 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 5090 |
|
| 5091 |
GGML_ASSERT(ne00 == ne0); |
| 5092 |
GGML_ASSERT(ne00 == ne1); |
| 5093 |
GGML_ASSERT(ne01 == 1); |
| 5094 |
GGML_ASSERT(ne02 == ne2); |
| 5095 |
GGML_ASSERT(ne03 == ne3); |
| 5096 |
|
| 5097 |
GGML_ASSERT(nb00 == sizeof(float)); |
| 5098 |
GGML_ASSERT(nb0 == sizeof(float)); |
| 5099 |
|
| 5100 |
for (int i3 = 0; i3 < ne3; i3++) { |
| 5101 |
for (int i2 = 0; i2 < ne2; i2++) { |
| 5102 |
for (int i1 = 0; i1 < ne1; i1++) { |
| 5103 |
float * d = (float *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1); |
| 5104 |
float * s = (float *)((char *) src0->data + i3*nb03 + i2*nb02); |
| 5105 |
for (int i0 = 0; i0 < i1; i0++) { |
| 5106 |
d[i0] = 0; |
| 5107 |
} |
| 5108 |
d[i1] = s[i1]; |
| 5109 |
for (int i0 = i1+1; i0 < ne0; i0++) { |
| 5110 |
d[i0] = 0; |
| 5111 |
} |
| 5112 |
} |
| 5113 |
} |
| 5114 |
} |
| 5115 |
} |
| 5116 |
|
| 5117 |
void ggml_compute_forward_diag( |
| 5118 |
const ggml_compute_params * params, |
| 5119 |
ggml_tensor * dst) { |
| 5120 |
|
| 5121 |
const ggml_tensor * src0 = dst->src[0]; |
| 5122 |
|
| 5123 |
switch (src0->type) { |
| 5124 |
case GGML_TYPE_F32: |
| 5125 |
{ |
| 5126 |
ggml_compute_forward_diag_f32(params, dst); |
| 5127 |
} break; |
| 5128 |
default: |
| 5129 |
{ |
| 5130 |
GGML_ABORT("fatal error"); |
| 5131 |
} |
| 5132 |
} |
| 5133 |
} |
| 5134 |
|
| 5135 |
// ggml_compute_forward_diag_mask_inf |
| 5136 |
|
| 5137 |
static void ggml_compute_forward_diag_mask_f32( |
| 5138 |
const ggml_compute_params * params, |
| 5139 |
ggml_tensor * dst, |
| 5140 |
const float value) { |
| 5141 |
|
| 5142 |
const ggml_tensor * src0 = dst->src[0]; |
| 5143 |
|
| 5144 |
const int ith = params->ith; |
| 5145 |
const int nth = params->nth; |
| 5146 |
|
| 5147 |
const int n_past = ((int32_t *) dst->op_params)[0]; |
| 5148 |
const bool inplace = src0->data == dst->data; |
| 5149 |
|
| 5150 |
GGML_ASSERT(n_past >= 0); |
| 5151 |
|
| 5152 |
if (!inplace) { |
| 5153 |
if (ith == 0) { |
| 5154 |
// memcpy needs to be synchronized across threads to avoid race conditions. |
| 5155 |
// => do it in INIT phase |
| 5156 |
GGML_ASSERT(ggml_nelements(dst) == ggml_nelements(src0)); |
| 5157 |
GGML_ASSERT(ggml_is_contiguous(dst) && ggml_is_contiguous(src0)); |
| 5158 |
memcpy( |
| 5159 |
((char *) dst->data), |
| 5160 |
((char *) src0->data), |
| 5161 |
ggml_nbytes(dst)); |
| 5162 |
} |
| 5163 |
ggml_barrier(params->threadpool); |
| 5164 |
} |
| 5165 |
|
| 5166 |
// TODO: handle transposed/permuted matrices |
| 5167 |
|
| 5168 |
const int n = ggml_nrows(src0); |
| 5169 |
const int nc = src0->ne[0]; |
| 5170 |
const int nr = src0->ne[1]; |
| 5171 |
const int nz = n/nr; |
| 5172 |
|
| 5173 |
GGML_ASSERT( dst->nb[0] == sizeof(float)); |
| 5174 |
GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 5175 |
|
| 5176 |
for (int k = 0; k < nz; k++) { |
| 5177 |
for (int j = ith; j < nr; j += nth) { |
| 5178 |
for (int i = n_past; i < nc; i++) { |
| 5179 |
if (i > n_past + j) { |
| 5180 |
*(float *)((char *) dst->data + k*dst->nb[2] + j*dst->nb[1] + i*dst->nb[0]) = value; |
| 5181 |
} |
| 5182 |
} |
| 5183 |
} |
| 5184 |
} |
| 5185 |
} |
| 5186 |
|
| 5187 |
void ggml_compute_forward_diag_mask_inf( |
| 5188 |
const ggml_compute_params * params, |
| 5189 |
ggml_tensor * dst) { |
| 5190 |
|
| 5191 |
const ggml_tensor * src0 = dst->src[0]; |
| 5192 |
|
| 5193 |
switch (src0->type) { |
| 5194 |
case GGML_TYPE_F32: |
| 5195 |
{ |
| 5196 |
ggml_compute_forward_diag_mask_f32(params, dst, -INFINITY); |
| 5197 |
} break; |
| 5198 |
default: |
| 5199 |
{ |
| 5200 |
GGML_ABORT("fatal error"); |
| 5201 |
} |
| 5202 |
} |
| 5203 |
} |
| 5204 |
|
| 5205 |
void ggml_compute_forward_diag_mask_zero( |
| 5206 |
const ggml_compute_params * params, |
| 5207 |
ggml_tensor * dst) { |
| 5208 |
|
| 5209 |
const ggml_tensor * src0 = dst->src[0]; |
| 5210 |
|
| 5211 |
switch (src0->type) { |
| 5212 |
case GGML_TYPE_F32: |
| 5213 |
{ |
| 5214 |
ggml_compute_forward_diag_mask_f32(params, dst, 0); |
| 5215 |
} break; |
| 5216 |
default: |
| 5217 |
{ |
| 5218 |
GGML_ABORT("fatal error"); |
| 5219 |
} |
| 5220 |
} |
| 5221 |
} |
| 5222 |
|
| 5223 |
// ggml_compute_forward_soft_max |
| 5224 |
|
| 5225 |
static void ggml_compute_forward_soft_max_f32( |
| 5226 |
const ggml_compute_params * params, |
| 5227 |
ggml_tensor * dst) { |
| 5228 |
|
| 5229 |
const ggml_tensor * src0 = dst->src[0]; |
| 5230 |
const ggml_tensor * src1 = dst->src[1]; |
| 5231 |
const ggml_tensor * src2 = dst->src[2]; |
| 5232 |
|
| 5233 |
assert(ggml_is_contiguous(dst)); |
| 5234 |
assert(ggml_are_same_shape(src0, dst)); |
| 5235 |
|
| 5236 |
float scale = 1.0f; |
| 5237 |
float max_bias = 0.0f; |
| 5238 |
|
| 5239 |
memcpy(&scale, (float *) dst->op_params + 0, sizeof(float)); |
| 5240 |
memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float)); |
| 5241 |
|
| 5242 |
const int ith = params->ith; |
| 5243 |
const int nth = params->nth; |
| 5244 |
|
| 5245 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 5246 |
|
| 5247 |
const int64_t nb11 = src1 ? src1->nb[1] : 1; |
| 5248 |
const int64_t nb12 = src1 ? src1->nb[2] : 1; |
| 5249 |
const int64_t nb13 = src1 ? src1->nb[3] : 1; |
| 5250 |
|
| 5251 |
const int64_t ne12 = src1 ? src1->ne[2] : 1; |
| 5252 |
const int64_t ne13 = src1 ? src1->ne[3] : 1; |
| 5253 |
|
| 5254 |
// TODO: is this supposed to be ceil instead of floor? |
| 5255 |
// https://huggingface.co/mosaicml/mpt-7b/blob/main/attention.py#L370 |
| 5256 |
const uint32_t n_head = ne02; |
| 5257 |
const uint32_t n_head_log2 = 1u << (uint32_t) floor(log2(n_head)); |
| 5258 |
|
| 5259 |
const float m0 = powf(2.0f, -(max_bias ) / n_head_log2); |
| 5260 |
const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2); |
| 5261 |
|
| 5262 |
float * wp = (float *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith; |
| 5263 |
|
| 5264 |
const bool use_f16 = (src1 && src1->type == GGML_TYPE_F16); |
| 5265 |
|
| 5266 |
// sinks |
| 5267 |
const float * sk = src2 ? (float *)((char *) src2->data) : nullptr; |
| 5268 |
|
| 5269 |
for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 5270 |
for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 5271 |
for (int64_t i01 = ith; i01 < ne01; i01 += nth) { |
| 5272 |
const int64_t i11 = i01; |
| 5273 |
const int64_t i12 = i02%ne12; |
| 5274 |
const int64_t i13 = i03%ne13; |
| 5275 |
|
| 5276 |
// ALiBi |
| 5277 |
const uint32_t h = i02; // head |
| 5278 |
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; |
| 5279 |
|
| 5280 |
float * sp = (float *)((char *) src0->data + i01*nb01 + i02*nb02 + i03*nb03); |
| 5281 |
float * dp = (float *)((char *) dst->data + i01*nb1 + i02*nb2 + i03*nb3); |
| 5282 |
|
| 5283 |
// broadcast the mask across rows |
| 5284 |
ggml_fp16_t * mp_f16 = src1 ? (ggml_fp16_t *)((char *) src1->data + i11*nb11 + i12*nb12 + i13*nb13) : NULL; |
| 5285 |
float * mp_f32 = src1 ? (float *)((char *) src1->data + i11*nb11 + i12*nb12 + i13*nb13) : NULL; |
| 5286 |
|
| 5287 |
ggml_vec_cpy_f32 (ne00, wp, sp); |
| 5288 |
ggml_vec_scale_f32(ne00, wp, scale); |
| 5289 |
if (mp_f32) { |
| 5290 |
if (use_f16) { |
| 5291 |
for (int i = 0; i < ne00; ++i) { |
| 5292 |
wp[i] += slope*GGML_CPU_FP16_TO_FP32(mp_f16[i]); |
| 5293 |
} |
| 5294 |
} else { |
| 5295 |
for (int i = 0; i < ne00; ++i) { |
| 5296 |
wp[i] += slope*mp_f32[i]; |
| 5297 |
} |
| 5298 |
} |
| 5299 |
} |
| 5300 |
|
| 5301 |
#ifndef NDEBUG |
| 5302 |
for (int i = 0; i < ne00; ++i) { |
| 5303 |
//printf("p[%d] = %f\n", i, p[i]); |
| 5304 |
assert(!isnan(wp[i])); |
| 5305 |
} |
| 5306 |
#endif |
| 5307 |
|
| 5308 |
float max = -INFINITY; |
| 5309 |
ggml_vec_max_f32(ne00, &max, wp); |
| 5310 |
|
| 5311 |
// if we have sinks, make a correction as if they were included in the softmax |
| 5312 |
if (sk) { |
| 5313 |
max = MAX(max, sk[i02]); |
| 5314 |
} |
| 5315 |
|
| 5316 |
ggml_float sum = ggml_vec_soft_max_f32(ne00, dp, wp, max); |
| 5317 |
assert(sum > 0.0); |
| 5318 |
|
| 5319 |
if (sk) { |
| 5320 |
sum += (ggml_float) expf(sk[i02] - max); |
| 5321 |
} |
| 5322 |
|
| 5323 |
sum = 1.0/sum; |
| 5324 |
ggml_vec_scale_f32(ne00, dp, sum); |
| 5325 |
|
| 5326 |
#ifndef NDEBUG |
| 5327 |
for (int i = 0; i < ne00; ++i) { |
| 5328 |
assert(!isnan(dp[i])); |
| 5329 |
assert(!isinf(dp[i])); |
| 5330 |
} |
| 5331 |
#endif |
| 5332 |
} |
| 5333 |
} |
| 5334 |
} |
| 5335 |
} |
| 5336 |
|
| 5337 |
void ggml_compute_forward_soft_max( |
| 5338 |
const ggml_compute_params * params, |
| 5339 |
ggml_tensor * dst) { |
| 5340 |
|
| 5341 |
const ggml_tensor * src0 = dst->src[0]; |
| 5342 |
|
| 5343 |
switch (src0->type) { |
| 5344 |
case GGML_TYPE_F32: |
| 5345 |
{ |
| 5346 |
ggml_compute_forward_soft_max_f32(params, dst); |
| 5347 |
} break; |
| 5348 |
default: |
| 5349 |
{ |
| 5350 |
GGML_ABORT("fatal error"); |
| 5351 |
} |
| 5352 |
} |
| 5353 |
} |
| 5354 |
|
| 5355 |
|
| 5356 |
// ggml_compute_forward_soft_max_ext_back |
| 5357 |
|
| 5358 |
static void ggml_compute_forward_soft_max_ext_back_f32( |
| 5359 |
const ggml_compute_params * params, |
| 5360 |
ggml_tensor * dst) { |
| 5361 |
|
| 5362 |
const ggml_tensor * src0 = dst->src[0]; |
| 5363 |
const ggml_tensor * src1 = dst->src[1]; |
| 5364 |
|
| 5365 |
GGML_ASSERT(ggml_is_contiguous(src0)); |
| 5366 |
GGML_ASSERT(ggml_is_contiguous(src1)); |
| 5367 |
GGML_ASSERT(ggml_is_contiguous(dst)); |
| 5368 |
GGML_ASSERT(ggml_are_same_shape(src0, dst)); |
| 5369 |
GGML_ASSERT(ggml_are_same_shape(src1, dst)); |
| 5370 |
|
| 5371 |
float scale = 1.0f; |
| 5372 |
float max_bias = 0.0f; |
| 5373 |
|
| 5374 |
memcpy(&scale, (const float *) dst->op_params + 0, sizeof(float)); |
| 5375 |
memcpy(&max_bias, (const float *) dst->op_params + 1, sizeof(float)); |
| 5376 |
|
| 5377 |
GGML_ASSERT(max_bias == 0.0f); |
| 5378 |
|
| 5379 |
// TODO: handle transposed/permuted matrices |
| 5380 |
|
| 5381 |
const int ith = params->ith; |
| 5382 |
const int nth = params->nth; |
| 5383 |
|
| 5384 |
const int nc = src0->ne[0]; |
| 5385 |
const int nr = ggml_nrows(src0); |
| 5386 |
|
| 5387 |
// rows per thread |
| 5388 |
const int dr = (nr + nth - 1)/nth; |
| 5389 |
|
| 5390 |
// row range for this thread |
| 5391 |
const int ir0 = dr*ith; |
| 5392 |
const int ir1 = MIN(ir0 + dr, nr); |
| 5393 |
|
| 5394 |
for (int i1 = ir0; i1 < ir1; i1++) { |
| 5395 |
float *dy = (float *)((char *) src0->data + i1*src0->nb[1]); |
| 5396 |
float *y = (float *)((char *) src1->data + i1*src1->nb[1]); |
| 5397 |
float *dx = (float *)((char *) dst->data + i1*dst->nb[1]); |
| 5398 |
|
| 5399 |
#ifndef NDEBUG |
| 5400 |
for (int i = 0; i < nc; ++i) { |
| 5401 |
//printf("p[%d] = %f\n", i, p[i]); |
| 5402 |
assert(!isnan(dy[i])); |
| 5403 |
assert(!isnan(y[i])); |
| 5404 |
} |
| 5405 |
#endif |
| 5406 |
// Jii = yi - yi*yi |
| 5407 |
// Jij = -yi*yj |
| 5408 |
// J = diag(y)-y.T*y |
| 5409 |
// dx = J * dy |
| 5410 |
// dxk = sum_i(Jki * dyi) |
| 5411 |
// dxk = sum_i(-yk*yi * dyi) - (-yk*yk)*dyk + (yk - yk*yk)*dyk |
| 5412 |
// dxk = sum_i(-yk*yi * dyi) + yk*yk*dyk + yk*dyk - yk*yk*dyk |
| 5413 |
// dxk = sum_i(-yk*yi * dyi) + yk*dyk |
| 5414 |
// dxk = -yk * sum_i(yi * dyi) + yk*dyk |
| 5415 |
// dxk = -yk * dot(y, dy) + yk*dyk |
| 5416 |
// dxk = yk * (- dot(y, dy) + dyk) |
| 5417 |
// dxk = yk * (dyk - dot(y, dy)) |
| 5418 |
// |
| 5419 |
// post-order: |
| 5420 |
// dot_y_dy := dot(y, dy) |
| 5421 |
// dx := dy |
| 5422 |
// dx := dx - dot_y_dy |
| 5423 |
// dx := dx * y |
| 5424 |
|
| 5425 |
// linear runtime, no additional memory |
| 5426 |
float dot_y_dy = 0; |
| 5427 |
ggml_vec_dot_f32 (nc, &dot_y_dy, 0, y, 0, dy, 0, 1); |
| 5428 |
ggml_vec_cpy_f32 (nc, dx, dy); |
| 5429 |
ggml_vec_acc1_f32 (nc, dx, -dot_y_dy); |
| 5430 |
ggml_vec_mul_f32 (nc, dx, dx, y); |
| 5431 |
ggml_vec_scale_f32(nc, dx, scale); |
| 5432 |
|
| 5433 |
#ifndef NDEBUG |
| 5434 |
for (int i = 0; i < nc; ++i) { |
| 5435 |
assert(!isnan(dx[i])); |
| 5436 |
assert(!isinf(dx[i])); |
| 5437 |
} |
| 5438 |
#endif |
| 5439 |
} |
| 5440 |
} |
| 5441 |
|
| 5442 |
void ggml_compute_forward_soft_max_ext_back( |
| 5443 |
const ggml_compute_params * params, |
| 5444 |
ggml_tensor * dst) { |
| 5445 |
|
| 5446 |
const ggml_tensor * src0 = dst->src[0]; |
| 5447 |
|
| 5448 |
switch (src0->type) { |
| 5449 |
case GGML_TYPE_F32: |
| 5450 |
{ |
| 5451 |
ggml_compute_forward_soft_max_ext_back_f32(params, dst); |
| 5452 |
} break; |
| 5453 |
default: |
| 5454 |
{ |
| 5455 |
GGML_ABORT("fatal error"); |
| 5456 |
} |
| 5457 |
} |
| 5458 |
} |
| 5459 |
|
| 5460 |
// ggml_compute_forward_clamp |
| 5461 |
|
| 5462 |
static void ggml_compute_forward_clamp_f32( |
| 5463 |
const ggml_compute_params * params, |
| 5464 |
ggml_tensor * dst) { |
| 5465 |
|
| 5466 |
const ggml_tensor * src0 = dst->src[0]; |
| 5467 |
|
| 5468 |
float min; |
| 5469 |
float max; |
| 5470 |
memcpy(&min, (float *) dst->op_params + 0, sizeof(float)); |
| 5471 |
memcpy(&max, (float *) dst->op_params + 1, sizeof(float)); |
| 5472 |
|
| 5473 |
const int ith = params->ith; |
| 5474 |
const int nth = params->nth; |
| 5475 |
|
| 5476 |
const int n = ggml_nrows(src0); |
| 5477 |
const int nc = src0->ne[0]; |
| 5478 |
|
| 5479 |
const size_t nb00 = src0->nb[0]; |
| 5480 |
const size_t nb01 = src0->nb[1]; |
| 5481 |
|
| 5482 |
const size_t nb0 = dst->nb[0]; |
| 5483 |
const size_t nb1 = dst->nb[1]; |
| 5484 |
|
| 5485 |
GGML_ASSERT( nb0 == sizeof(float)); |
| 5486 |
GGML_ASSERT(nb00 == sizeof(float)); |
| 5487 |
|
| 5488 |
for (int j = ith; j < n; j += nth) { |
| 5489 |
float * dst_ptr = (float *) ((char *) dst->data + j*nb1); |
| 5490 |
float * src0_ptr = (float *) ((char *) src0->data + j*nb01); |
| 5491 |
|
| 5492 |
for (int i = 0; i < nc; i++) { |
| 5493 |
dst_ptr[i] = MAX(MIN(src0_ptr[i], max), min); |
| 5494 |
} |
| 5495 |
} |
| 5496 |
} |
| 5497 |
|
| 5498 |
static void ggml_compute_forward_clamp_f16( |
| 5499 |
const ggml_compute_params * params, |
| 5500 |
ggml_tensor * dst) { |
| 5501 |
|
| 5502 |
const ggml_tensor * src0 = dst->src[0]; |
| 5503 |
|
| 5504 |
float min; |
| 5505 |
float max; |
| 5506 |
memcpy(&min, (float *) dst->op_params + 0, sizeof(float)); |
| 5507 |
memcpy(&max, (float *) dst->op_params + 1, sizeof(float)); |
| 5508 |
|
| 5509 |
const int ith = params->ith; |
| 5510 |
const int nth = params->nth; |
| 5511 |
|
| 5512 |
const int n = ggml_nrows(src0); |
| 5513 |
const int nc = src0->ne[0]; |
| 5514 |
|
| 5515 |
const size_t nb00 = src0->nb[0]; |
| 5516 |
const size_t nb01 = src0->nb[1]; |
| 5517 |
|
| 5518 |
const size_t nb0 = dst->nb[0]; |
| 5519 |
const size_t nb1 = dst->nb[1]; |
| 5520 |
|
| 5521 |
GGML_ASSERT( nb0 == sizeof(ggml_fp16_t)); |
| 5522 |
GGML_ASSERT(nb00 == sizeof(ggml_fp16_t)); |
| 5523 |
|
| 5524 |
for (int j = ith; j < n; j += nth) { |
| 5525 |
ggml_fp16_t * dst_ptr = (ggml_fp16_t *) ((char *) dst->data + j*nb1); |
| 5526 |
ggml_fp16_t * src0_ptr = (ggml_fp16_t *) ((char *) src0->data + j*nb01); |
| 5527 |
|
| 5528 |
for (int i = 0; i < nc; i++) { |
| 5529 |
float v = GGML_CPU_FP16_TO_FP32(src0_ptr[i]); |
| 5530 |
dst_ptr[i] = GGML_CPU_FP32_TO_FP16(MAX(MIN(v, max), min)); |
| 5531 |
} |
| 5532 |
} |
| 5533 |
} |
| 5534 |
|
| 5535 |
void ggml_compute_forward_clamp( |
| 5536 |
const ggml_compute_params * params, |
| 5537 |
ggml_tensor * dst) { |
| 5538 |
|
| 5539 |
const ggml_tensor * src0 = dst->src[0]; |
| 5540 |
|
| 5541 |
switch (src0->type) { |
| 5542 |
case GGML_TYPE_F32: |
| 5543 |
{ |
| 5544 |
ggml_compute_forward_clamp_f32(params, dst); |
| 5545 |
} break; |
| 5546 |
case GGML_TYPE_F16: |
| 5547 |
{ |
| 5548 |
ggml_compute_forward_clamp_f16(params, dst); |
| 5549 |
} break; |
| 5550 |
case GGML_TYPE_BF16: |
| 5551 |
case GGML_TYPE_Q4_0: |
| 5552 |
case GGML_TYPE_Q4_1: |
| 5553 |
case GGML_TYPE_Q5_0: |
| 5554 |
case GGML_TYPE_Q5_1: |
| 5555 |
case GGML_TYPE_Q8_0: |
| 5556 |
case GGML_TYPE_Q8_1: |
| 5557 |
case GGML_TYPE_MXFP4: |
| 5558 |
case GGML_TYPE_Q2_K: |
| 5559 |
case GGML_TYPE_Q3_K: |
| 5560 |
case GGML_TYPE_Q4_K: |
| 5561 |
case GGML_TYPE_Q5_K: |
| 5562 |
case GGML_TYPE_Q6_K: |
| 5563 |
case GGML_TYPE_TQ1_0: |
| 5564 |
case GGML_TYPE_TQ2_0: |
| 5565 |
case GGML_TYPE_IQ2_XXS: |
| 5566 |
case GGML_TYPE_IQ2_XS: |
| 5567 |
case GGML_TYPE_IQ3_XXS: |
| 5568 |
case GGML_TYPE_IQ1_S: |
| 5569 |
case GGML_TYPE_IQ1_M: |
| 5570 |
case GGML_TYPE_IQ4_NL: |
| 5571 |
case GGML_TYPE_IQ4_XS: |
| 5572 |
case GGML_TYPE_IQ3_S: |
| 5573 |
case GGML_TYPE_IQ2_S: |
| 5574 |
case GGML_TYPE_Q8_K: |
| 5575 |
case GGML_TYPE_I8: |
| 5576 |
case GGML_TYPE_I16: |
| 5577 |
case GGML_TYPE_I32: |
| 5578 |
case GGML_TYPE_I64: |
| 5579 |
case GGML_TYPE_F64: |
| 5580 |
case GGML_TYPE_COUNT: |
| 5581 |
{ |
| 5582 |
GGML_ABORT("fatal error"); |
| 5583 |
} |
| 5584 |
} |
| 5585 |
} |
| 5586 |
|
| 5587 |
// ggml_compute_forward_rope |
| 5588 |
|
| 5589 |
static float rope_yarn_ramp(const float low, const float high, const int i0) { |
| 5590 |
const float y = (i0 / 2 - low) / MAX(0.001f, high - low); |
| 5591 |
return 1 - MIN(1, MAX(0, y)); |
| 5592 |
} |
| 5593 |
|
| 5594 |
// YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn |
| 5595 |
// MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng. |
| 5596 |
static void rope_yarn( |
| 5597 |
float theta_extrap, float freq_scale, float corr_dims[2], int64_t i0, float ext_factor, float mscale, |
| 5598 |
float * cos_theta, float * sin_theta) { |
| 5599 |
// Get n-d rotational scaling corrected for extrapolation |
| 5600 |
float theta_interp = freq_scale * theta_extrap; |
| 5601 |
float theta = theta_interp; |
| 5602 |
if (ext_factor != 0.0f) { |
| 5603 |
float ramp_mix = rope_yarn_ramp(corr_dims[0], corr_dims[1], i0) * ext_factor; |
| 5604 |
theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix; |
| 5605 |
|
| 5606 |
// Get n-d magnitude scaling corrected for interpolation |
| 5607 |
mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale); |
| 5608 |
} |
| 5609 |
*cos_theta = cosf(theta) * mscale; |
| 5610 |
*sin_theta = sinf(theta) * mscale; |
| 5611 |
} |
| 5612 |
|
| 5613 |
static void ggml_rope_cache_init( |
| 5614 |
float theta_base, float freq_scale, const float * freq_factors, float corr_dims[2], int64_t ne0, float ext_factor, float mscale, |
| 5615 |
float * cache, float sin_sign, float theta_scale) { |
| 5616 |
// ref: https://github.com/jquesnelle/yarn/blob/master/scaled_rope/LlamaYaRNScaledRotaryEmbedding.py |
| 5617 |
float theta = theta_base; |
| 5618 |
for (int64_t i0 = 0; i0 < ne0; i0 += 2) { |
| 5619 |
const float ff = freq_factors ? freq_factors[i0/2] : 1.0f; |
| 5620 |
rope_yarn( |
| 5621 |
theta/ff, freq_scale, corr_dims, i0, ext_factor, mscale, &cache[i0 + 0], &cache[i0 + 1] |
| 5622 |
); |
| 5623 |
cache[i0 + 1] *= sin_sign; |
| 5624 |
|
| 5625 |
theta *= theta_scale; |
| 5626 |
} |
| 5627 |
} |
| 5628 |
|
| 5629 |
static void ggml_mrope_cache_init( |
| 5630 |
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, |
| 5631 |
float freq_scale, const float * freq_factors, float corr_dims[2], int64_t ne0, float ext_factor, float mscale, |
| 5632 |
float * cache, float sin_sign, float theta_scale) { |
| 5633 |
// ref: https://github.com/jquesnelle/yarn/blob/master/scaled_rope/LlamaYaRNScaledRotaryEmbedding.py |
| 5634 |
float theta_t = theta_base_t; |
| 5635 |
float theta_h = theta_base_h; |
| 5636 |
float theta_w = theta_base_w; |
| 5637 |
float theta_e = theta_base_e; // extra position id for vision encoder |
| 5638 |
int sect_dims = sections[0] + sections[1] + sections[2] + sections[3]; |
| 5639 |
int sec_w = sections[1] + sections[0]; |
| 5640 |
int sec_e = sections[2] + sec_w; |
| 5641 |
GGML_ASSERT(sect_dims <= ne0); |
| 5642 |
|
| 5643 |
for (int64_t i0 = 0; i0 < ne0; i0 += 2) { |
| 5644 |
const float ff = freq_factors ? freq_factors[i0/2] : 1.0f; |
| 5645 |
|
| 5646 |
int sector = (i0 / 2) % sect_dims; |
| 5647 |
if (indep_sects) { |
| 5648 |
// compute theta independently for each dim sections |
| 5649 |
// (i.e. reset corresponding theta when `i0` go from one section to another) |
| 5650 |
if (sector == 0) { |
| 5651 |
theta_t = theta_base_t; |
| 5652 |
} |
| 5653 |
else if (sector == sections[0]) { |
| 5654 |
theta_h = theta_base_h;; |
| 5655 |
} |
| 5656 |
else if (sector == sec_w) { |
| 5657 |
theta_w = theta_base_w; |
| 5658 |
} |
| 5659 |
else if (sector == sec_e) { |
| 5660 |
theta_e = theta_base_e; |
| 5661 |
} |
| 5662 |
} |
| 5663 |
|
| 5664 |
float theta = theta_t; |
| 5665 |
if (is_imrope) { // qwen3vl apply interleaved mrope |
| 5666 |
if (sector % 3 == 1 && sector < 3 * sections[1]) { |
| 5667 |
theta = theta_h; |
| 5668 |
} else if (sector % 3 == 2 && sector < 3 * sections[2]) { |
| 5669 |
theta = theta_w; |
| 5670 |
} else if (sector % 3 == 0 && sector < 3 * sections[0]) { |
| 5671 |
theta = theta_t; |
| 5672 |
} else { |
| 5673 |
theta = theta_e; |
| 5674 |
} |
| 5675 |
} else { |
| 5676 |
if (sector >= sections[0] && sector < sec_w) { |
| 5677 |
theta = theta_h; |
| 5678 |
} |
| 5679 |
else if (sector >= sec_w && sector < sec_w + sections[2]) { |
| 5680 |
theta = theta_w; |
| 5681 |
} |
| 5682 |
else if (sector >= sec_w + sections[2]) { |
| 5683 |
theta = theta_e; |
| 5684 |
} |
| 5685 |
} |
| 5686 |
|
| 5687 |
rope_yarn( |
| 5688 |
theta/ff, freq_scale, corr_dims, i0, ext_factor, mscale, &cache[i0 + 0], &cache[i0 + 1] |
| 5689 |
); |
| 5690 |
cache[i0 + 1] *= sin_sign; |
| 5691 |
|
| 5692 |
theta_t *= theta_scale; |
| 5693 |
theta_w *= theta_scale; |
| 5694 |
theta_h *= theta_scale; |
| 5695 |
theta_e *= theta_scale; |
| 5696 |
} |
| 5697 |
} |
| 5698 |
|
| 5699 |
|
| 5700 |
template<typename T> |
| 5701 |
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) { |
| 5702 |
for (int64_t i0 = 0; i0 < n; i0 += 2) { |
| 5703 |
const int64_t ic = i0/scale; // hack for GGML_ROPE_TYPE_NORMAL, where we need ic = i0; for all other cases, ic = i0/2 |
| 5704 |
|
| 5705 |
const float cos_theta = cache[i0 + 0]; |
| 5706 |
const float sin_theta = cache[i0 + 1]; |
| 5707 |
|
| 5708 |
const T * const src = src_data + ic; |
| 5709 |
T * dst = dst_data + ic; |
| 5710 |
|
| 5711 |
const float x0 = type_conversion_table<T>::to_f32(src[0]); |
| 5712 |
const float x1 = type_conversion_table<T>::to_f32(src[n_offset]); |
| 5713 |
|
| 5714 |
dst[0] = type_conversion_table<T>::from_f32(x0*cos_theta - x1*sin_theta); |
| 5715 |
dst[n_offset] = type_conversion_table<T>::from_f32(x0*sin_theta + x1*cos_theta); |
| 5716 |
} |
| 5717 |
} |
| 5718 |
|
| 5719 |
template<typename T> //float or ggml_fp16_t |
| 5720 |
static void ggml_compute_forward_rope_flt( |
| 5721 |
const ggml_compute_params * params, |
| 5722 |
ggml_tensor * dst, |
| 5723 |
const bool forward) { |
| 5724 |
|
| 5725 |
const ggml_tensor * src0 = dst->src[0]; |
| 5726 |
const ggml_tensor * src1 = dst->src[1]; |
| 5727 |
const ggml_tensor * src2 = dst->src[2]; |
| 5728 |
|
| 5729 |
GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16); |
| 5730 |
GGML_ASSERT(src1->type == GGML_TYPE_I32); |
| 5731 |
|
| 5732 |
float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow; |
| 5733 |
int sections[4]; |
| 5734 |
|
| 5735 |
//const int n_past = ((int32_t *) dst->op_params)[0]; |
| 5736 |
const int n_dims = ((int32_t *) dst->op_params)[1]; |
| 5737 |
const int mode = ((int32_t *) dst->op_params)[2]; |
| 5738 |
//const int n_ctx = ((int32_t *) dst->op_params)[3]; |
| 5739 |
const int n_ctx_orig = ((int32_t *) dst->op_params)[4]; |
| 5740 |
|
| 5741 |
memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float)); |
| 5742 |
memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float)); |
| 5743 |
memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float)); |
| 5744 |
memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float)); |
| 5745 |
memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float)); |
| 5746 |
memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float)); |
| 5747 |
memcpy(§ions, (int32_t *) dst->op_params + 11, sizeof(int)*4); |
| 5748 |
|
| 5749 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 5750 |
|
| 5751 |
//printf("ne0: %d, ne1: %d, ne2: %d, ne3: %d\n", ne0, ne1, ne2, ne3); |
| 5752 |
//printf("n_past = %d, ne2 = %d\n", n_past, ne2); |
| 5753 |
|
| 5754 |
GGML_ASSERT(nb0 == nb00); |
| 5755 |
GGML_ASSERT(nb0 == sizeof(T)); |
| 5756 |
|
| 5757 |
const int ith = params->ith; |
| 5758 |
const int nth = params->nth; |
| 5759 |
|
| 5760 |
const int nr = ggml_nrows(dst); |
| 5761 |
|
| 5762 |
GGML_ASSERT(n_dims <= ne0); |
| 5763 |
GGML_ASSERT(n_dims % 2 == 0); |
| 5764 |
|
| 5765 |
// rows per thread |
| 5766 |
const int dr = (nr + nth - 1)/nth; |
| 5767 |
|
| 5768 |
// row range for this thread |
| 5769 |
const int ir0 = dr*ith; |
| 5770 |
const int ir1 = MIN(ir0 + dr, nr); |
| 5771 |
|
| 5772 |
// row index used to determine which thread to use |
| 5773 |
int ir = 0; |
| 5774 |
|
| 5775 |
const float theta_scale = powf(freq_base, -2.0f/n_dims); |
| 5776 |
|
| 5777 |
float corr_dims[2]; |
| 5778 |
ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims); |
| 5779 |
|
| 5780 |
const bool is_imrope = mode == GGML_ROPE_TYPE_IMROPE; // qwen3vl apply interleaved mrope |
| 5781 |
const bool mrope_used = mode & GGML_ROPE_TYPE_MROPE; // ggml_rope_multi, note: also true for vision (24 & 8 == true) and for imrope |
| 5782 |
const bool is_vision = mode == GGML_ROPE_TYPE_VISION; |
| 5783 |
|
| 5784 |
if (mrope_used) { |
| 5785 |
GGML_ASSERT(sections[0] > 0 || sections[1] > 0 || sections[2] > 0); |
| 5786 |
} |
| 5787 |
|
| 5788 |
if (is_vision) { |
| 5789 |
GGML_ASSERT(n_dims == ne0/2); |
| 5790 |
} |
| 5791 |
|
| 5792 |
const float * freq_factors = NULL; |
| 5793 |
if (src2 != NULL) { |
| 5794 |
GGML_ASSERT(src2->type == GGML_TYPE_F32); |
| 5795 |
GGML_ASSERT(src2->ne[0] >= n_dims / 2); |
| 5796 |
freq_factors = (const float *) src2->data; |
| 5797 |
} |
| 5798 |
|
| 5799 |
// backward process uses inverse rotation by cos and sin. |
| 5800 |
// cos and sin build a rotation matrix, where the inverse is the transpose. |
| 5801 |
// this essentially just switches the sign of sin. |
| 5802 |
const float sin_sign = forward ? 1.0f : -1.0f; |
| 5803 |
|
| 5804 |
const int32_t * pos = (const int32_t *) src1->data; |
| 5805 |
|
| 5806 |
for (int64_t i3 = 0; i3 < ne3; i3++) { // batch |
| 5807 |
for (int64_t i2 = 0; i2 < ne2; i2++) { // seq-len |
| 5808 |
|
| 5809 |
float * cache = (float *) params->wdata + (ne0 + CACHE_LINE_SIZE_F32)*ith; |
| 5810 |
if (!mrope_used) { |
| 5811 |
const int64_t p = pos[i2]; |
| 5812 |
ggml_rope_cache_init(p, freq_scale, freq_factors, corr_dims, ne0, ext_factor, attn_factor, cache, sin_sign, theta_scale); |
| 5813 |
} |
| 5814 |
else { |
| 5815 |
const int64_t p_t = pos[i2]; |
| 5816 |
const int64_t p_h = pos[i2 + ne2]; |
| 5817 |
const int64_t p_w = pos[i2 + ne2 * 2]; |
| 5818 |
const int64_t p_e = pos[i2 + ne2 * 3]; |
| 5819 |
ggml_mrope_cache_init( |
| 5820 |
p_t, p_h, p_w, p_e, sections, is_imrope, is_vision, |
| 5821 |
freq_scale, freq_factors, corr_dims, ne0, ext_factor, attn_factor, cache, sin_sign, theta_scale); |
| 5822 |
} |
| 5823 |
|
| 5824 |
for (int64_t i1 = 0; i1 < ne1; i1++) { // attn-heads |
| 5825 |
if (ir++ < ir0) continue; |
| 5826 |
if (ir > ir1) break; |
| 5827 |
|
| 5828 |
T * src = (T *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01); |
| 5829 |
T * dst_data = (T *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1); |
| 5830 |
|
| 5831 |
switch (mode) { |
| 5832 |
case GGML_ROPE_TYPE_NORMAL: |
| 5833 |
rotate_pairs<T>(n_dims, 1, cache, src, dst_data, 1); |
| 5834 |
break; |
| 5835 |
case GGML_ROPE_TYPE_NEOX: |
| 5836 |
case GGML_ROPE_TYPE_MROPE: |
| 5837 |
case GGML_ROPE_TYPE_IMROPE: |
| 5838 |
rotate_pairs<T>(n_dims, n_dims/2, cache, src, dst_data); |
| 5839 |
break; |
| 5840 |
case GGML_ROPE_TYPE_VISION: |
| 5841 |
rotate_pairs<T>(ne0, n_dims, cache, src, dst_data); |
| 5842 |
break; |
| 5843 |
default: |
| 5844 |
GGML_ABORT("rope type not supported"); |
| 5845 |
} |
| 5846 |
|
| 5847 |
if (!is_vision) { |
| 5848 |
// fill the remain channels with data from src tensor |
| 5849 |
for (int64_t i0 = n_dims; i0 < ne0; i0 += 2) { |
| 5850 |
const T * const src = (T *)((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01 + i0*nb00); |
| 5851 |
T * dst_data = (T *)((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + i0*nb0); |
| 5852 |
|
| 5853 |
dst_data[0] = src[0]; |
| 5854 |
dst_data[1] = src[1]; |
| 5855 |
} |
| 5856 |
} |
| 5857 |
} //attn-heads |
| 5858 |
} |
| 5859 |
} |
| 5860 |
} |
| 5861 |
|
| 5862 |
void ggml_compute_forward_rope( |
| 5863 |
const ggml_compute_params * params, |
| 5864 |
ggml_tensor * dst) { |
| 5865 |
|
| 5866 |
const ggml_tensor * src0 = dst->src[0]; |
| 5867 |
|
| 5868 |
switch (src0->type) { |
| 5869 |
case GGML_TYPE_F16: |
| 5870 |
{ |
| 5871 |
ggml_compute_forward_rope_flt<ggml_fp16_t>(params, dst, true); |
| 5872 |
} break; |
| 5873 |
case GGML_TYPE_F32: |
| 5874 |
{ |
| 5875 |
ggml_compute_forward_rope_flt<float>(params, dst, true); |
| 5876 |
} break; |
| 5877 |
default: |
| 5878 |
{ |
| 5879 |
GGML_ABORT("fatal error"); |
| 5880 |
} |
| 5881 |
} |
| 5882 |
} |
| 5883 |
|
| 5884 |
// ggml_compute_forward_rope_back |
| 5885 |
|
| 5886 |
void ggml_compute_forward_rope_back( |
| 5887 |
const ggml_compute_params * params, |
| 5888 |
ggml_tensor * dst) { |
| 5889 |
|
| 5890 |
const ggml_tensor * src0 = dst->src[0]; |
| 5891 |
|
| 5892 |
switch (src0->type) { |
| 5893 |
case GGML_TYPE_F16: |
| 5894 |
{ |
| 5895 |
ggml_compute_forward_rope_flt<ggml_fp16_t>(params, dst, false); |
| 5896 |
} break; |
| 5897 |
case GGML_TYPE_F32: |
| 5898 |
{ |
| 5899 |
ggml_compute_forward_rope_flt<float>(params, dst, false); |
| 5900 |
} break; |
| 5901 |
default: |
| 5902 |
{ |
| 5903 |
GGML_ABORT("fatal error"); |
| 5904 |
} |
| 5905 |
} |
| 5906 |
} |
| 5907 |
|
| 5908 |
// ggml_compute_forward_conv_transpose_1d |
| 5909 |
|
| 5910 |
static void ggml_compute_forward_conv_transpose_1d_f16_f32( |
| 5911 |
const ggml_compute_params * params, |
| 5912 |
ggml_tensor * dst) { |
| 5913 |
|
| 5914 |
const ggml_tensor * src0 = dst->src[0]; |
| 5915 |
const ggml_tensor * src1 = dst->src[1]; |
| 5916 |
|
| 5917 |
GGML_ASSERT(src0->type == GGML_TYPE_F16); |
| 5918 |
GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 5919 |
GGML_ASSERT( dst->type == GGML_TYPE_F32); |
| 5920 |
|
| 5921 |
GGML_TENSOR_BINARY_OP_LOCALS |
| 5922 |
|
| 5923 |
const int ith = params->ith; |
| 5924 |
const int nth = params->nth; |
| 5925 |
|
| 5926 |
const int nk = ne00*ne01*ne02; |
| 5927 |
|
| 5928 |
GGML_ASSERT(nb00 == sizeof(ggml_fp16_t)); |
| 5929 |
GGML_ASSERT(nb10 == sizeof(float)); |
| 5930 |
|
| 5931 |
if (ith == 0) { |
| 5932 |
memset(params->wdata, 0, params->wsize); |
| 5933 |
|
| 5934 |
// permute kernel data (src0) from (K x Cout x Cin) to (Cin x K x Cout) |
| 5935 |
{ |
| 5936 |
ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + 0; |
| 5937 |
|
| 5938 |
for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 5939 |
for (int64_t i01 = 0; i01 < ne01; i01++) { |
| 5940 |
const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i02*nb02 + i01*nb01); |
| 5941 |
ggml_fp16_t * dst_data = wdata + i01*ne00*ne02; |
| 5942 |
for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 5943 |
dst_data[i00*ne02 + i02] = src[i00]; |
| 5944 |
} |
| 5945 |
} |
| 5946 |
} |
| 5947 |
} |
| 5948 |
|
| 5949 |
// permute source data (src1) from (L x Cin) to (Cin x L) |
| 5950 |
{ |
| 5951 |
ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + nk; |
| 5952 |
ggml_fp16_t * dst_data = wdata; |
| 5953 |
|
| 5954 |
for (int64_t i11 = 0; i11 < ne11; i11++) { |
| 5955 |
const float * const src = (float *)((char *) src1->data + i11*nb11); |
| 5956 |
for (int64_t i10 = 0; i10 < ne10; i10++) { |
| 5957 |
dst_data[i10*ne11 + i11] = GGML_CPU_FP32_TO_FP16(src[i10]); |
| 5958 |
} |
| 5959 |
} |
| 5960 |
} |
| 5961 |
|
| 5962 |
// need to zero dst since we are accumulating into it |
| 5963 |
memset(dst->data, 0, ggml_nbytes(dst)); |
| 5964 |
} |
| 5965 |
ggml_barrier(params->threadpool); |
| 5966 |
|
| 5967 |
const int32_t s0 = ((const int32_t*)(dst->op_params))[0]; |
| 5968 |
|
| 5969 |
// total rows in dst |
| 5970 |
const int nr = ne1; |
| 5971 |
|
| 5972 |
// rows per thread |
| 5973 |
const int dr = (nr + nth - 1)/nth; |
| 5974 |
|
| 5975 |
// row range for this thread |
| 5976 |
const int ir0 = dr*ith; |
| 5977 |
const int ir1 = MIN(ir0 + dr, nr); |
| 5978 |
|
| 5979 |
ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + 0; |
| 5980 |
ggml_fp16_t * const wdata_src = wdata + nk; |
| 5981 |
|
| 5982 |
for (int i1 = ir0; i1 < ir1; i1++) { |
| 5983 |
float * dst_data = (float *)((char *) dst->data + i1*nb1); |
| 5984 |
ggml_fp16_t * wdata_kernel = wdata + i1*ne02*ne00; |
| 5985 |
for (int i10 = 0; i10 < ne10; i10++) { |
| 5986 |
const int i1n = i10*ne11; |
| 5987 |
for (int i00 = 0; i00 < ne00; i00++) { |
| 5988 |
float v = 0; |
| 5989 |
ggml_vec_dot_f16(ne02, &v, 0, |
| 5990 |
(ggml_fp16_t *) wdata_src + i1n, 0, |
| 5991 |
(ggml_fp16_t *) wdata_kernel + i00*ne02, 0, 1); |
| 5992 |
dst_data[i10*s0 + i00] += v; |
| 5993 |
} |
| 5994 |
} |
| 5995 |
} |
| 5996 |
} |
| 5997 |
|
| 5998 |
static void ggml_compute_forward_conv_transpose_1d_f32( |
| 5999 |
const ggml_compute_params * params, |
| 6000 |
ggml_tensor * dst) { |
| 6001 |
|
| 6002 |
const ggml_tensor * src0 = dst->src[0]; |
| 6003 |
const ggml_tensor * src1 = dst->src[1]; |
| 6004 |
|
| 6005 |
GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 6006 |
GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 6007 |
GGML_ASSERT( dst->type == GGML_TYPE_F32); |
| 6008 |
|
| 6009 |
GGML_TENSOR_BINARY_OP_LOCALS |
| 6010 |
|
| 6011 |
const int ith = params->ith; |
| 6012 |
const int nth = params->nth; |
| 6013 |
|
| 6014 |
const int nk = ne00*ne01*ne02; |
| 6015 |
|
| 6016 |
GGML_ASSERT(nb00 == sizeof(float)); |
| 6017 |
GGML_ASSERT(nb10 == sizeof(float)); |
| 6018 |
|
| 6019 |
if (ith == 0) { |
| 6020 |
memset(params->wdata, 0, params->wsize); |
| 6021 |
|
| 6022 |
// prepare kernel data (src0) from (K x Cout x Cin) to (Cin x K x Cout) |
| 6023 |
{ |
| 6024 |
float * const wdata = (float *) params->wdata + 0; |
| 6025 |
|
| 6026 |
for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 6027 |
for (int64_t i01 = 0; i01 < ne01; i01++) { |
| 6028 |
const float * const src = (float *)((char *) src0->data + i02*nb02 + i01*nb01); |
| 6029 |
float * dst_data = wdata + i01*ne00*ne02; |
| 6030 |
for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 6031 |
dst_data[i00*ne02 + i02] = src[i00]; |
| 6032 |
} |
| 6033 |
} |
| 6034 |
} |
| 6035 |
} |
| 6036 |
|
| 6037 |
// prepare source data (src1) |
| 6038 |
{ |
| 6039 |
float * const wdata = (float *) params->wdata + nk; |
| 6040 |
float * dst_data = wdata; |
| 6041 |
|
| 6042 |
for (int64_t i11 = 0; i11 < ne11; i11++) { |
| 6043 |
const float * const src = (float *)((char *) src1->data + i11*nb11); |
| 6044 |
for (int64_t i10 = 0; i10 < ne10; i10++) { |
| 6045 |
dst_data[i10*ne11 + i11] = src[i10]; |
| 6046 |
} |
| 6047 |
} |
| 6048 |
} |
| 6049 |
|
| 6050 |
// need to zero dst since we are accumulating into it |
| 6051 |
memset(dst->data, 0, ggml_nbytes(dst)); |
| 6052 |
} |
| 6053 |
ggml_barrier(params->threadpool); |
| 6054 |
|
| 6055 |
const int32_t s0 = ((const int32_t*)(dst->op_params))[0]; |
| 6056 |
|
| 6057 |
// total rows in dst |
| 6058 |
const int nr = ne1; |
| 6059 |
|
| 6060 |
// rows per thread |
| 6061 |
const int dr = (nr + nth - 1)/nth; |
| 6062 |
|
| 6063 |
// row range for this thread |
| 6064 |
const int ir0 = dr*ith; |
| 6065 |
const int ir1 = MIN(ir0 + dr, nr); |
| 6066 |
|
| 6067 |
float * const wdata = (float *) params->wdata + 0; |
| 6068 |
float * const wdata_src = wdata + nk; |
| 6069 |
|
| 6070 |
for (int i1 = ir0; i1 < ir1; i1++) { |
| 6071 |
float * dst_data = (float *)((char *) dst->data + i1*nb1); |
| 6072 |
float * wdata_kernel = wdata + i1*ne02*ne00; |
| 6073 |
for (int i10 = 0; i10 < ne10; i10++) { |
| 6074 |
const int i1n = i10*ne11; |
| 6075 |
for (int i00 = 0; i00 < ne00; i00++) { |
| 6076 |
float v = 0; |
| 6077 |
ggml_vec_dot_f32(ne02, &v, 0, |
| 6078 |
wdata_src + i1n, 0, |
| 6079 |
wdata_kernel + i00*ne02, 0, 1); |
| 6080 |
dst_data[i10*s0 + i00] += v; |
| 6081 |
} |
| 6082 |
} |
| 6083 |
} |
| 6084 |
} |
| 6085 |
|
| 6086 |
void ggml_compute_forward_conv_transpose_1d( |
| 6087 |
const ggml_compute_params * params, |
| 6088 |
ggml_tensor * dst) { |
| 6089 |
|
| 6090 |
const ggml_tensor * src0 = dst->src[0]; |
| 6091 |
|
| 6092 |
switch (src0->type) { |
| 6093 |
case GGML_TYPE_F16: |
| 6094 |
{ |
| 6095 |
ggml_compute_forward_conv_transpose_1d_f16_f32(params, dst); |
| 6096 |
} break; |
| 6097 |
case GGML_TYPE_F32: |
| 6098 |
{ |
| 6099 |
ggml_compute_forward_conv_transpose_1d_f32(params, dst); |
| 6100 |
} break; |
| 6101 |
default: |
| 6102 |
{ |
| 6103 |
GGML_ABORT("fatal error"); |
| 6104 |
} |
| 6105 |
} |
| 6106 |
} |
| 6107 |
|
| 6108 |
// ggml_compute_forward_im2col_f32 |
| 6109 |
// src0: kernel [OC, IC, KH, KW] |
| 6110 |
// src1: image [N, IC, IH, IW] |
| 6111 |
// dst: result [N, OH, OW, IC*KH*KW] |
| 6112 |
static void ggml_compute_forward_im2col_f32( |
| 6113 |
const ggml_compute_params * params, |
| 6114 |
ggml_tensor * dst) { |
| 6115 |
|
| 6116 |
const ggml_tensor * src0 = dst->src[0]; |
| 6117 |
const ggml_tensor * src1 = dst->src[1]; |
| 6118 |
|
| 6119 |
GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 6120 |
GGML_ASSERT( dst->type == GGML_TYPE_F32); |
| 6121 |
|
| 6122 |
GGML_TENSOR_BINARY_OP_LOCALS; |
| 6123 |
|
| 6124 |
const int32_t s0 = ((const int32_t *)(dst->op_params))[0]; |
| 6125 |
const int32_t s1 = ((const int32_t *)(dst->op_params))[1]; |
| 6126 |
const int32_t p0 = ((const int32_t *)(dst->op_params))[2]; |
| 6127 |
const int32_t p1 = ((const int32_t *)(dst->op_params))[3]; |
| 6128 |
const int32_t d0 = ((const int32_t *)(dst->op_params))[4]; |
| 6129 |
const int32_t d1 = ((const int32_t *)(dst->op_params))[5]; |
| 6130 |
const bool is_2D = ((const int32_t *)(dst->op_params))[6] == 1; |
| 6131 |
|
| 6132 |
const int ith = params->ith; |
| 6133 |
const int nth = params->nth; |
| 6134 |
|
| 6135 |
const int64_t N = is_2D ? ne13 : ne12; |
| 6136 |
const int64_t IC = is_2D ? ne12 : ne11; |
| 6137 |
const int64_t IH = is_2D ? ne11 : 1; |
| 6138 |
const int64_t IW = ne10; |
| 6139 |
|
| 6140 |
const int64_t KH = is_2D ? ne01 : 1; |
| 6141 |
const int64_t KW = ne00; |
| 6142 |
|
| 6143 |
const int64_t OH = is_2D ? ne2 : 1; |
| 6144 |
const int64_t OW = ne1; |
| 6145 |
|
| 6146 |
int ofs0 = is_2D ? nb13 : nb12; |
| 6147 |
int ofs1 = is_2D ? nb12 : nb11; |
| 6148 |
|
| 6149 |
GGML_ASSERT(nb10 == sizeof(float)); |
| 6150 |
|
| 6151 |
// im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW] |
| 6152 |
{ |
| 6153 |
float * const wdata = (float *) dst->data; |
| 6154 |
|
| 6155 |
for (int64_t in = 0; in < N; in++) { |
| 6156 |
for (int64_t ioh = 0; ioh < OH; ioh++) { // 1 |
| 6157 |
for (int64_t iow = 0; iow < OW; iow++) { |
| 6158 |
for (int64_t iic = ith; iic < IC; iic += nth) { |
| 6159 |
|
| 6160 |
// micro kernel |
| 6161 |
float * dst_data = wdata + (in*OH*OW + ioh*OW + iow)*(IC*KH*KW); // [IC, KH, KW] |
| 6162 |
const float * const src_data = (float *)((char *) src1->data + in*ofs0 + iic*ofs1); // [IH, IW] |
| 6163 |
|
| 6164 |
for (int64_t ikh = 0; ikh < KH; ikh++) { // 1 |
| 6165 |
for (int64_t ikw = 0; ikw < KW; ikw++) { |
| 6166 |
const int64_t iiw = iow*s0 + ikw*d0 - p0; |
| 6167 |
const int64_t iih = ioh*s1 + ikh*d1 - p1; |
| 6168 |
|
| 6169 |
if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) { |
| 6170 |
dst_data[iic*(KH*KW) + ikh*KW + ikw] = 0; |
| 6171 |
} else { |
| 6172 |
dst_data[iic*(KH*KW) + ikh*KW + ikw] = (src_data[iih*IW + iiw]); |
| 6173 |
} |
| 6174 |
} |
| 6175 |
} |
| 6176 |
} |
| 6177 |
} |
| 6178 |
} |
| 6179 |
} |
| 6180 |
} |
| 6181 |
} |
| 6182 |
|
| 6183 |
|
| 6184 |
// ggml_compute_forward_im2col_f16 |
| 6185 |
// src0: kernel [OC, IC, KH, KW] |
| 6186 |
// src1: image [N, IC, IH, IW] |
| 6187 |
// dst: result [N, OH, OW, IC*KH*KW] |
| 6188 |
static void ggml_compute_forward_im2col_f16( |
| 6189 |
const ggml_compute_params * params, |
| 6190 |
ggml_tensor * dst) { |
| 6191 |
|
| 6192 |
const ggml_tensor * src0 = dst->src[0]; |
| 6193 |
const ggml_tensor * src1 = dst->src[1]; |
| 6194 |
|
| 6195 |
GGML_ASSERT(src0->type == GGML_TYPE_F16); |
| 6196 |
GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 6197 |
GGML_ASSERT( dst->type == GGML_TYPE_F16); |
| 6198 |
|
| 6199 |
GGML_TENSOR_BINARY_OP_LOCALS; |
| 6200 |
|
| 6201 |
const int32_t s0 = ((const int32_t *)(dst->op_params))[0]; |
| 6202 |
const int32_t s1 = ((const int32_t *)(dst->op_params))[1]; |
| 6203 |
const int32_t p0 = ((const int32_t *)(dst->op_params))[2]; |
| 6204 |
const int32_t p1 = ((const int32_t *)(dst->op_params))[3]; |
| 6205 |
const int32_t d0 = ((const int32_t *)(dst->op_params))[4]; |
| 6206 |
const int32_t d1 = ((const int32_t *)(dst->op_params))[5]; |
| 6207 |
const bool is_2D = ((const int32_t *)(dst->op_params))[6] == 1; |
| 6208 |
|
| 6209 |
const int ith = params->ith; |
| 6210 |
const int nth = params->nth; |
| 6211 |
|
| 6212 |
const int64_t N = is_2D ? ne13 : ne12; |
| 6213 |
const int64_t IC = is_2D ? ne12 : ne11; |
| 6214 |
const int64_t IH = is_2D ? ne11 : 1; |
| 6215 |
const int64_t IW = ne10; |
| 6216 |
|
| 6217 |
const int64_t KH = is_2D ? ne01 : 1; |
| 6218 |
const int64_t KW = ne00; |
| 6219 |
|
| 6220 |
const int64_t OH = is_2D ? ne2 : 1; |
| 6221 |
const int64_t OW = ne1; |
| 6222 |
|
| 6223 |
int ofs0 = is_2D ? nb13 : nb12; |
| 6224 |
int ofs1 = is_2D ? nb12 : nb11; |
| 6225 |
|
| 6226 |
GGML_ASSERT(nb00 == sizeof(ggml_fp16_t)); |
| 6227 |
GGML_ASSERT(nb10 == sizeof(float)); |
| 6228 |
|
| 6229 |
// im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW] |
| 6230 |
{ |
| 6231 |
ggml_fp16_t * const wdata = (ggml_fp16_t *) dst->data; |
| 6232 |
|
| 6233 |
for (int64_t in = 0; in < N; in++) { |
| 6234 |
for (int64_t ioh = 0; ioh < OH; ioh++) { // 1 |
| 6235 |
for (int64_t iow = 0; iow < OW; iow++) { |
| 6236 |
for (int64_t iic = ith; iic < IC; iic += nth) { |
| 6237 |
|
| 6238 |
// micro kernel |
| 6239 |
ggml_fp16_t * dst_data = wdata + (in*OH*OW + ioh*OW + iow)*(IC*KH*KW); // [IC, KH, KW] |
| 6240 |
const float * const src_data = (float *)((char *) src1->data + in*ofs0 + iic*ofs1); // [IH, IW] |
| 6241 |
|
| 6242 |
for (int64_t ikh = 0; ikh < KH; ikh++) { // 1 |
| 6243 |
for (int64_t ikw = 0; ikw < KW; ikw++) { |
| 6244 |
const int64_t iiw = iow*s0 + ikw*d0 - p0; |
| 6245 |
const int64_t iih = ioh*s1 + ikh*d1 - p1; |
| 6246 |
|
| 6247 |
if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) { |
| 6248 |
dst_data[iic*(KH*KW) + ikh*KW + ikw] = 0; |
| 6249 |
} else { |
| 6250 |
dst_data[iic*(KH*KW) + ikh*KW + ikw] = GGML_CPU_FP32_TO_FP16(src_data[iih*IW + iiw]); |
| 6251 |
} |
| 6252 |
} |
| 6253 |
} |
| 6254 |
} |
| 6255 |
} |
| 6256 |
} |
| 6257 |
} |
| 6258 |
} |
| 6259 |
} |
| 6260 |
|
| 6261 |
void ggml_compute_forward_im2col( |
| 6262 |
const ggml_compute_params * params, |
| 6263 |
ggml_tensor * dst) { |
| 6264 |
switch (dst->type) { |
| 6265 |
case GGML_TYPE_F16: |
| 6266 |
{ |
| 6267 |
ggml_compute_forward_im2col_f16(params, dst); |
| 6268 |
} break; |
| 6269 |
case GGML_TYPE_F32: |
| 6270 |
{ |
| 6271 |
ggml_compute_forward_im2col_f32(params, dst); |
| 6272 |
} break; |
| 6273 |
default: |
| 6274 |
{ |
| 6275 |
GGML_ABORT("fatal error"); |
| 6276 |
} |
| 6277 |
} |
| 6278 |
} |
| 6279 |
|
| 6280 |
// ggml_compute_forward_im2col_back_f32 |
| 6281 |
|
| 6282 |
void ggml_compute_forward_im2col_back_f32( |
| 6283 |
const ggml_compute_params * params, |
| 6284 |
ggml_tensor * dst) { |
| 6285 |
|
| 6286 |
const ggml_tensor * src0 = dst->src[0]; // gradients of forward pass output |
| 6287 |
const ggml_tensor * src1 = dst->src[1]; // convolution kernel |
| 6288 |
|
| 6289 |
GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 6290 |
GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 6291 |
GGML_ASSERT( dst->type == GGML_TYPE_F32); |
| 6292 |
|
| 6293 |
GGML_TENSOR_BINARY_OP_LOCALS; |
| 6294 |
|
| 6295 |
const int32_t s0 = ((const int32_t *)(dst->op_params))[0]; |
| 6296 |
const int32_t s1 = ((const int32_t *)(dst->op_params))[1]; |
| 6297 |
const int32_t p0 = ((const int32_t *)(dst->op_params))[2]; |
| 6298 |
const int32_t p1 = ((const int32_t *)(dst->op_params))[3]; |
| 6299 |
const int32_t d0 = ((const int32_t *)(dst->op_params))[4]; |
| 6300 |
const int32_t d1 = ((const int32_t *)(dst->op_params))[5]; |
| 6301 |
const bool is_2D = ((const int32_t *)(dst->op_params))[6] == 1; |
| 6302 |
|
| 6303 |
const int ith = params->ith; |
| 6304 |
const int nth = params->nth; |
| 6305 |
|
| 6306 |
const int64_t N = is_2D ? ne3 : ne2; |
| 6307 |
const int64_t IC = is_2D ? ne2 : ne1; |
| 6308 |
const int64_t IH = is_2D ? ne1 : 1; |
| 6309 |
const int64_t IW = ne0; |
| 6310 |
|
| 6311 |
const int64_t KH = is_2D ? ne11 : 1; |
| 6312 |
const int64_t KW = ne10; |
| 6313 |
|
| 6314 |
const int64_t OH = is_2D ? ne02 : 1; |
| 6315 |
const int64_t OW = ne01; |
| 6316 |
|
| 6317 |
int ofs0 = is_2D ? nb3 : nb2; |
| 6318 |
int ofs1 = is_2D ? nb2 : nb1; |
| 6319 |
|
| 6320 |
GGML_ASSERT(nb0 == sizeof(float)); |
| 6321 |
|
| 6322 |
// im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW] |
| 6323 |
{ |
| 6324 |
float * const wdata = (float *) dst->data; |
| 6325 |
|
| 6326 |
for (int64_t in = 0; in < N; in++) { |
| 6327 |
for (int64_t iic = ith; iic < IC; iic += nth) { |
| 6328 |
for (int64_t iih = 0; iih < IH; iih++) { |
| 6329 |
for (int64_t iiw = 0; iiw < IW; iiw++) { |
| 6330 |
|
| 6331 |
// micro kernel |
| 6332 |
float grad = 0.0f; |
| 6333 |
for (int64_t ikh = 0; ikh < KH; ikh++) { |
| 6334 |
for (int64_t ikw = 0; ikw < KW; ikw++) { |
| 6335 |
// For s0 > 1 some values were skipped over in the forward pass. |
| 6336 |
// These values have tmpw % s0 != 0 and need to be skipped in the backwards pass as well. |
| 6337 |
const int64_t tmpw = (iiw + p0 - ikw*d0); |
| 6338 |
if (tmpw % s0 != 0) { |
| 6339 |
continue; |
| 6340 |
} |
| 6341 |
const int64_t iow = tmpw / s0; |
| 6342 |
|
| 6343 |
// Equivalent logic as above except for s1. |
| 6344 |
int64_t ioh; |
| 6345 |
if (is_2D) { |
| 6346 |
const int64_t tmph = iih + p1 - ikh*d1; |
| 6347 |
|
| 6348 |
if (tmph % s1 != 0) { |
| 6349 |
continue; |
| 6350 |
} |
| 6351 |
|
| 6352 |
ioh = tmph / s1; |
| 6353 |
} else { |
| 6354 |
ioh = 0; |
| 6355 |
} |
| 6356 |
|
| 6357 |
if (iow < 0 || iow >= OW || ioh < 0 || ioh >= OH) { |
| 6358 |
continue; |
| 6359 |
} |
| 6360 |
|
| 6361 |
const float * const grad_in = (const float *) src0->data |
| 6362 |
+ (in*OH*OW + ioh*OW + iow)*(IC*KH*KW); // [IC, KH, KW] |
| 6363 |
grad += grad_in[iic*(KH*KW) + ikh*KW + ikw]; |
| 6364 |
} |
| 6365 |
} |
| 6366 |
float * dst_data = (float *)((char *) wdata + (in*ofs0 + iic*ofs1)); // [IH, IW] |
| 6367 |
dst_data[iih*IW + iiw] = grad; |
| 6368 |
} |
| 6369 |
} |
| 6370 |
} |
| 6371 |
} |
| 6372 |
} |
| 6373 |
} |
| 6374 |
|
| 6375 |
|
| 6376 |
// ggml_compute_forward_im2col_3d_f16 |
| 6377 |
// src0: kernel [OC*IC, KD, KH, KW] |
| 6378 |
// src1: image [N*IC, ID, IH, IW] |
| 6379 |
// dst: result [N*OD, OH, OW, IC * KD * KH * KW] |
| 6380 |
static void ggml_compute_forward_im2col_3d_f16( |
| 6381 |
const ggml_compute_params * params, |
| 6382 |
ggml_tensor * dst) { |
| 6383 |
|
| 6384 |
const ggml_tensor * src0 = dst->src[0]; |
| 6385 |
const ggml_tensor * src1 = dst->src[1]; |
| 6386 |
|
| 6387 |
GGML_ASSERT(src0->type == GGML_TYPE_F16); |
| 6388 |
GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 6389 |
GGML_ASSERT( dst->type == GGML_TYPE_F16); |
| 6390 |
|
| 6391 |
GGML_TENSOR_BINARY_OP_LOCALS; |
| 6392 |
|
| 6393 |
const int32_t s0 = ((const int32_t *)(dst->op_params))[0]; |
| 6394 |
const int32_t s1 = ((const int32_t *)(dst->op_params))[1]; |
| 6395 |
const int32_t s2 = ((const int32_t *)(dst->op_params))[2]; |
| 6396 |
const int32_t p0 = ((const int32_t *)(dst->op_params))[3]; |
| 6397 |
const int32_t p1 = ((const int32_t *)(dst->op_params))[4]; |
| 6398 |
const int32_t p2 = ((const int32_t *)(dst->op_params))[5]; |
| 6399 |
const int32_t d0 = ((const int32_t *)(dst->op_params))[6]; |
| 6400 |
const int32_t d1 = ((const int32_t *)(dst->op_params))[7]; |
| 6401 |
const int32_t d2 = ((const int32_t *)(dst->op_params))[8]; |
| 6402 |
const int32_t IC = ((const int32_t *)(dst->op_params))[9]; |
| 6403 |
|
| 6404 |
|
| 6405 |
const int ith = params->ith; |
| 6406 |
const int nth = params->nth; |
| 6407 |
|
| 6408 |
const int64_t N = ne13 / IC; |
| 6409 |
const int64_t ID = ne12; |
| 6410 |
const int64_t IH = ne11; |
| 6411 |
const int64_t IW = ne10; |
| 6412 |
|
| 6413 |
const int64_t OC = ne03 / IC; |
| 6414 |
GGML_UNUSED(OC); |
| 6415 |
const int64_t KD = ne02; |
| 6416 |
const int64_t KH = ne01; |
| 6417 |
const int64_t KW = ne00; |
| 6418 |
|
| 6419 |
const int64_t OD = ne3 / N; |
| 6420 |
const int64_t OH = ne2; |
| 6421 |
const int64_t OW = ne1; |
| 6422 |
const int64_t OH_OW = OH*OW; |
| 6423 |
const int64_t KD_KH_KW = KD*KH*KW; |
| 6424 |
const int64_t KH_KW = KH*KW; |
| 6425 |
const int64_t IC_KD_KH_KW = IC*KD*KH*KW; |
| 6426 |
|
| 6427 |
GGML_ASSERT(nb10 == sizeof(float)); |
| 6428 |
|
| 6429 |
// im2col: [N*IC, ID, IH, IW] => [N*OD, OH, OW, IC * KD * KH * KW] |
| 6430 |
{ |
| 6431 |
ggml_fp16_t * const wdata = (ggml_fp16_t *) dst->data; |
| 6432 |
|
| 6433 |
for (int64_t in = 0; in < N; in++) { |
| 6434 |
for (int64_t iod = 0; iod < OD; iod++) { |
| 6435 |
for (int64_t ioh = 0; ioh < OH; ioh++) { |
| 6436 |
for (int64_t iow = 0; iow < OW; iow++) { |
| 6437 |
for (int64_t iic = ith; iic < IC; iic += nth) { |
| 6438 |
|
| 6439 |
// micro kernel |
| 6440 |
ggml_fp16_t * dst_data = wdata + (in*OD*OH_OW + iod*OH_OW + ioh*OW + iow)*IC_KD_KH_KW; // [IC, KD, KH, KW] |
| 6441 |
const float * const src_data = (const float *) ((const char *)src1->data + (in*IC + iic)*nb13); // [ID, IH, IW] |
| 6442 |
|
| 6443 |
for (int64_t ikd = 0; ikd < KD; ikd++) { |
| 6444 |
for (int64_t ikh = 0; ikh < KH; ikh++) { |
| 6445 |
for (int64_t ikw = 0; ikw < KW; ikw++) { |
| 6446 |
const int64_t iiw = iow*s0 + ikw*d0 - p0; |
| 6447 |
const int64_t iih = ioh*s1 + ikh*d1 - p1; |
| 6448 |
const int64_t iid = iod*s2 + ikd*d2 - p2; |
| 6449 |
|
| 6450 |
if (iid < 0 || iid >= ID || iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) { |
| 6451 |
dst_data[iic*KD_KH_KW + ikd * KH_KW + ikh*KW + ikw] = 0; |
| 6452 |
} else { |
| 6453 |
const float * const s = (const float *) ((const char *)src_data + iid*nb12 + iih*nb11 + iiw*nb10); // [ID, IH, IW] |
| 6454 |
dst_data[iic*KD_KH_KW + ikd * KH_KW + ikh*KW + ikw] = GGML_CPU_FP32_TO_FP16(*s); |
| 6455 |
} |
| 6456 |
} |
| 6457 |
} |
| 6458 |
} |
| 6459 |
} |
| 6460 |
} |
| 6461 |
} |
| 6462 |
} |
| 6463 |
} |
| 6464 |
} |
| 6465 |
} |
| 6466 |
|
| 6467 |
// ggml_compute_forward_im2col_3d_f32 |
| 6468 |
// src0: kernel [OC*IC, KD, KH, KW] |
| 6469 |
// src1: image [N*IC, ID, IH, IW] |
| 6470 |
// dst: result [N*OD, OH, OW, IC * KD * KH * KW] |
| 6471 |
static void ggml_compute_forward_im2col_3d_f32( |
| 6472 |
const ggml_compute_params * params, |
| 6473 |
ggml_tensor * dst) { |
| 6474 |
|
| 6475 |
const ggml_tensor * src0 = dst->src[0]; |
| 6476 |
const ggml_tensor * src1 = dst->src[1]; |
| 6477 |
|
| 6478 |
GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 6479 |
GGML_ASSERT( dst->type == GGML_TYPE_F32); |
| 6480 |
|
| 6481 |
GGML_TENSOR_BINARY_OP_LOCALS; |
| 6482 |
|
| 6483 |
const int32_t s0 = ((const int32_t *)(dst->op_params))[0]; |
| 6484 |
const int32_t s1 = ((const int32_t *)(dst->op_params))[1]; |
| 6485 |
const int32_t s2 = ((const int32_t *)(dst->op_params))[2]; |
| 6486 |
const int32_t p0 = ((const int32_t *)(dst->op_params))[3]; |
| 6487 |
const int32_t p1 = ((const int32_t *)(dst->op_params))[4]; |
| 6488 |
const int32_t p2 = ((const int32_t *)(dst->op_params))[5]; |
| 6489 |
const int32_t d0 = ((const int32_t *)(dst->op_params))[6]; |
| 6490 |
const int32_t d1 = ((const int32_t *)(dst->op_params))[7]; |
| 6491 |
const int32_t d2 = ((const int32_t *)(dst->op_params))[8]; |
| 6492 |
const int32_t IC = ((const int32_t *)(dst->op_params))[9]; |
| 6493 |
|
| 6494 |
|
| 6495 |
const int ith = params->ith; |
| 6496 |
const int nth = params->nth; |
| 6497 |
|
| 6498 |
const int64_t N = ne13 / IC; |
| 6499 |
const int64_t ID = ne12; |
| 6500 |
const int64_t IH = ne11; |
| 6501 |
const int64_t IW = ne10; |
| 6502 |
|
| 6503 |
const int64_t OC = ne03 / IC; |
| 6504 |
GGML_UNUSED(OC); |
| 6505 |
const int64_t KD = ne02; |
| 6506 |
const int64_t KH = ne01; |
| 6507 |
const int64_t KW = ne00; |
| 6508 |
|
| 6509 |
const int64_t OD = ne3 / N; |
| 6510 |
const int64_t OH = ne2; |
| 6511 |
const int64_t OW = ne1; |
| 6512 |
|
| 6513 |
const int64_t OH_OW = OH*OW; |
| 6514 |
const int64_t KD_KH_KW = KD*KH*KW; |
| 6515 |
const int64_t KH_KW = KH*KW; |
| 6516 |
const int64_t IC_KD_KH_KW = IC*KD*KH*KW; |
| 6517 |
|
| 6518 |
GGML_ASSERT(nb10 == sizeof(float)); |
| 6519 |
|
| 6520 |
// im2col: [N*IC, ID, IH, IW] => [N*OD, OH, OW, IC * KD * KH * KW] |
| 6521 |
{ |
| 6522 |
float * const wdata = (float *) dst->data; |
| 6523 |
|
| 6524 |
for (int64_t in = 0; in < N; in++) { |
| 6525 |
for (int64_t iod = 0; iod < OD; iod++) { |
| 6526 |
for (int64_t ioh = 0; ioh < OH; ioh++) { |
| 6527 |
for (int64_t iow = 0; iow < OW; iow++) { |
| 6528 |
for (int64_t iic = ith; iic < IC; iic += nth) { |
| 6529 |
|
| 6530 |
// micro kernel |
| 6531 |
float * dst_data = wdata + (in*OD*OH_OW + iod*OH_OW + ioh*OW + iow)*IC_KD_KH_KW; // [IC, KD, KH, KW] |
| 6532 |
const float * const src_data = (const float *) ((const char *)src1->data + (in*IC + iic)*nb13); // [ID, IH, IW] |
| 6533 |
|
| 6534 |
for (int64_t ikd = 0; ikd < KD; ikd++) { |
| 6535 |
for (int64_t ikh = 0; ikh < KH; ikh++) { |
| 6536 |
for (int64_t ikw = 0; ikw < KW; ikw++) { |
| 6537 |
const int64_t iiw = iow*s0 + ikw*d0 - p0; |
| 6538 |
const int64_t iih = ioh*s1 + ikh*d1 - p1; |
| 6539 |
const int64_t iid = iod*s2 + ikd*d2 - p2; |
| 6540 |
|
| 6541 |
if (iid < 0 || iid >= ID || iih < 0 || iih >= IH || iiw < 0 || iiw >= IW || iid < 0 || iid >= ID) { |
| 6542 |
dst_data[iic*KD_KH_KW + ikd * KH_KW + ikh*KW + ikw] = 0; |
| 6543 |
} else { |
| 6544 |
const float * const s = (const float *) ((const char *)src_data + iid*nb12 + iih*nb11 + iiw*nb10); // [ID, IH, IW] |
| 6545 |
dst_data[iic*KD_KH_KW + ikd * KH_KW + ikh*KW + ikw] = *s; |
| 6546 |
} |
| 6547 |
} |
| 6548 |
} |
| 6549 |
} |
| 6550 |
} |
| 6551 |
} |
| 6552 |
} |
| 6553 |
} |
| 6554 |
} |
| 6555 |
} |
| 6556 |
} |
| 6557 |
|
| 6558 |
|
| 6559 |
void ggml_compute_forward_im2col_3d( |
| 6560 |
const ggml_compute_params * params, |
| 6561 |
ggml_tensor * dst) { |
| 6562 |
switch (dst->type) { |
| 6563 |
case GGML_TYPE_F16: |
| 6564 |
{ |
| 6565 |
ggml_compute_forward_im2col_3d_f16(params, dst); |
| 6566 |
} break; |
| 6567 |
case GGML_TYPE_F32: |
| 6568 |
{ |
| 6569 |
ggml_compute_forward_im2col_3d_f32(params, dst); |
| 6570 |
} break; |
| 6571 |
default: |
| 6572 |
{ |
| 6573 |
GGML_ABORT("fatal error"); |
| 6574 |
} |
| 6575 |
} |
| 6576 |
} |
| 6577 |
|
| 6578 |
static void ggml_call_mul_mat(ggml_type type, const ggml_compute_params * params, int64_t m, int64_t n, int64_t k, |
| 6579 |
void * a, void * b, float * c) { |
| 6580 |
const ggml_type_traits * traits = ggml_get_type_traits(type); |
| 6581 |
struct ggml_tensor src1 = {}; |
| 6582 |
src1.type = type; |
| 6583 |
src1.ne[0] = k; |
| 6584 |
src1.ne[1] = m; |
| 6585 |
src1.ne[2] = 1; |
| 6586 |
src1.ne[3] = 1; |
| 6587 |
src1.nb[0] = traits->type_size; |
| 6588 |
src1.nb[1] = k * traits->type_size; |
| 6589 |
src1.nb[2] = src1.nb[1]; |
| 6590 |
src1.nb[3] = src1.nb[2]; |
| 6591 |
src1.data = a; |
| 6592 |
|
| 6593 |
struct ggml_tensor src0 = {}; |
| 6594 |
src0.type = type; |
| 6595 |
src0.ne[0] = k; |
| 6596 |
src0.ne[1] = n; |
| 6597 |
src0.ne[2] = 1; |
| 6598 |
src0.ne[3] = 1; |
| 6599 |
src0.nb[0] = traits->type_size; |
| 6600 |
src0.nb[1] = k * traits->type_size; |
| 6601 |
src0.nb[2] = src0.nb[1]; |
| 6602 |
src0.nb[3] = src0.nb[2]; |
| 6603 |
src0.data = b; |
| 6604 |
|
| 6605 |
struct ggml_tensor dst = {}; |
| 6606 |
dst.ne[0] = n; |
| 6607 |
dst.ne[1] = m; |
| 6608 |
dst.ne[2] = 1; |
| 6609 |
dst.ne[3] = 1; |
| 6610 |
dst.nb[0] = sizeof(float); |
| 6611 |
dst.nb[1] = n * sizeof(float); |
| 6612 |
dst.nb[2] = dst.nb[1]; |
| 6613 |
dst.nb[3] = dst.nb[2]; |
| 6614 |
dst.data = c; |
| 6615 |
dst.src[0] = &src0; |
| 6616 |
dst.src[1] = &src1; |
| 6617 |
|
| 6618 |
ggml_compute_forward_mul_mat(params, &dst); |
| 6619 |
} |
| 6620 |
|
| 6621 |
static inline int64_t ggml_wrap_around(int64_t coord, int64_t size) { |
| 6622 |
return (coord + size) % size; // adding size avoids negative number weirdness |
| 6623 |
} |
| 6624 |
|
| 6625 |
// ggml_compute_forward_conv_2d |
| 6626 |
|
| 6627 |
|
| 6628 |
static void ggml_compute_forward_conv_2d_impl(const ggml_compute_params * params, |
| 6629 |
const ggml_tensor * kernel, // [KW, KH, IC, OC] |
| 6630 |
const ggml_tensor * src, // [W, H, C, N] |
| 6631 |
ggml_tensor * dst, // [OW, OH, OC, N] |
| 6632 |
ggml_type kernel_type) { |
| 6633 |
|
| 6634 |
GGML_ASSERT(ggml_is_contiguous(kernel)); |
| 6635 |
GGML_ASSERT(kernel_type == GGML_TYPE_F16 || kernel_type == GGML_TYPE_F32); |
| 6636 |
GGML_ASSERT(kernel->type == kernel_type); |
| 6637 |
|
| 6638 |
const ggml_type_traits * traits = ggml_get_type_traits(kernel_type); |
| 6639 |
|
| 6640 |
const int32_t stride_x = dst->op_params[0]; |
| 6641 |
const int32_t stride_y = dst->op_params[1]; |
| 6642 |
const int32_t pad_x = dst->op_params[2]; |
| 6643 |
const int32_t pad_y = dst->op_params[3]; |
| 6644 |
const int32_t dilation_x = dst->op_params[4]; |
| 6645 |
const int32_t dilation_y = dst->op_params[5]; |
| 6646 |
|
| 6647 |
const int64_t c_in = src->ne[2]; |
| 6648 |
const int64_t c_out = kernel->ne[3]; |
| 6649 |
GGML_ASSERT(c_in == kernel->ne[2]); |
| 6650 |
|
| 6651 |
const int64_t src_w = src->ne[0]; |
| 6652 |
const int64_t src_h = src->ne[1]; |
| 6653 |
const int64_t knl_w = kernel->ne[0]; |
| 6654 |
const int64_t knl_h = kernel->ne[1]; |
| 6655 |
const int64_t dst_w = dst->ne[0]; |
| 6656 |
const int64_t dst_h = dst->ne[1]; |
| 6657 |
|
| 6658 |
const float * src_data = (float *) src->data; |
| 6659 |
void * knl_data = kernel->data; |
| 6660 |
float * dst_data = (float *) dst->data; |
| 6661 |
|
| 6662 |
const int64_t knl_n = knl_w * knl_h * c_in; |
| 6663 |
const int64_t patch_total = dst->ne[3] * dst_w * dst_h; |
| 6664 |
|
| 6665 |
const int64_t space_per_patch = knl_n * traits->type_size + c_out * sizeof(float); |
| 6666 |
const int64_t batch_size = params->wsize / space_per_patch; |
| 6667 |
const int64_t patches_per_batch = batch_size > 8 ? (batch_size / 8) * 8 : batch_size; |
| 6668 |
const int64_t batch_n = (patch_total + patches_per_batch - 1) / patches_per_batch; |
| 6669 |
|
| 6670 |
GGML_ASSERT(patches_per_batch > 0 && batch_size >= 1); |
| 6671 |
|
| 6672 |
void * tmp = params->wdata; |
| 6673 |
|
| 6674 |
for (int64_t batch_i = 0; batch_i < batch_n; ++batch_i) { |
| 6675 |
|
| 6676 |
const int64_t patch_start_batch = batch_i * patches_per_batch; |
| 6677 |
const int64_t patch_end_batch = std::min(patch_start_batch + patches_per_batch, |
| 6678 |
patch_total); |
| 6679 |
const int64_t patch_n = patch_end_batch - patch_start_batch; |
| 6680 |
|
| 6681 |
const int64_t patch_per_thread = (patch_n + params->nth - 1) / params->nth; |
| 6682 |
const int64_t patch_start = patch_start_batch + params->ith * patch_per_thread; |
| 6683 |
const int64_t patch_end = std::min(patch_start + patch_per_thread, patch_end_batch); |
| 6684 |
|
| 6685 |
//im2col for a patch |
| 6686 |
for (int64_t p = patch_start; p < patch_end; ++p) { |
| 6687 |
const int64_t batch_n = p / (dst_w * dst_h); |
| 6688 |
const int64_t src_x = (p / dst_w) % dst_h; |
| 6689 |
const int64_t src_y = p % dst_w; |
| 6690 |
|
| 6691 |
const float * src_base = (const float *)((const char *)src_data + batch_n * src->nb[3]); |
| 6692 |
char * dst_row = (char *) tmp + (p % patches_per_batch) * knl_n * traits->type_size; |
| 6693 |
|
| 6694 |
for (int64_t ic = 0; ic < c_in; ++ic) { |
| 6695 |
for (int64_t ky = 0; ky < knl_h; ++ky) { |
| 6696 |
for (int64_t kx = 0; kx < knl_w; ++kx) { |
| 6697 |
const int64_t sy = src_x * stride_y + ky * dilation_y - pad_y; |
| 6698 |
const int64_t sx = src_y * stride_x + kx * dilation_x - pad_x; |
| 6699 |
|
| 6700 |
int64_t dst_idx = ic * (knl_h * knl_w) + ky * knl_w + kx; |
| 6701 |
|
| 6702 |
float src_val; |
| 6703 |
if (sy < 0 || sy >= src_h || sx < 0 || sx >= src_w) { |
| 6704 |
src_val = 0.0f; |
| 6705 |
} else { |
| 6706 |
const float * src_ptr = (const float *)((const char *)src_base + sx * src->nb[0] + sy * src->nb[1] + ic * src->nb[2]); |
| 6707 |
src_val = *src_ptr; |
| 6708 |
} |
| 6709 |
|
| 6710 |
char * element_ptr = dst_row + dst_idx * traits->type_size; |
| 6711 |
if (kernel_type == GGML_TYPE_F32) { |
| 6712 |
*(float *) element_ptr = src_val; |
| 6713 |
} else if (kernel_type == GGML_TYPE_F16) { |
| 6714 |
*(ggml_fp16_t *) element_ptr = GGML_CPU_FP32_TO_FP16(src_val); |
| 6715 |
} |
| 6716 |
} |
| 6717 |
} |
| 6718 |
} |
| 6719 |
} // patches handled by this thread |
| 6720 |
|
| 6721 |
ggml_barrier(params->threadpool); |
| 6722 |
|
| 6723 |
float * gemm_output = (float *) ((char *) tmp + patches_per_batch * knl_n * traits->type_size); |
| 6724 |
|
| 6725 |
GGML_ASSERT(gemm_output + patch_n * c_out <= (float*)tmp + params->wsize); |
| 6726 |
|
| 6727 |
// GEMM: patches[patch_n, knl_n] × kernel[knl_n, c_out] = output[patch_n, c_out] |
| 6728 |
ggml_call_mul_mat(kernel_type, params, patch_n, c_out, knl_n, tmp, knl_data, gemm_output); |
| 6729 |
|
| 6730 |
ggml_barrier(params->threadpool); |
| 6731 |
|
| 6732 |
|
| 6733 |
//permute back [OC, N, OH, OW] to [N, OC, OH, OW] |
| 6734 |
const int64_t permute_per_thread = (patch_n + params->nth - 1) / params->nth; |
| 6735 |
const int64_t permute_start = params->ith * permute_per_thread; |
| 6736 |
const int64_t permute_end = std::min(permute_start + permute_per_thread, patch_n); |
| 6737 |
|
| 6738 |
for (int64_t i = permute_start; i < permute_end; ++i) { |
| 6739 |
const int64_t p = patch_start_batch + i; |
| 6740 |
const int64_t batch_n = p / (dst_w * dst_h); |
| 6741 |
const int64_t dst_y = (p / dst_w) % dst_h; |
| 6742 |
const int64_t dst_x = p % dst_w; |
| 6743 |
|
| 6744 |
for (int64_t oc = 0; oc < c_out; ++oc) { |
| 6745 |
const float value = gemm_output[i * c_out + oc]; |
| 6746 |
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]); |
| 6747 |
*dst_ptr = value; |
| 6748 |
} |
| 6749 |
} |
| 6750 |
} |
| 6751 |
} |
| 6752 |
|
| 6753 |
void ggml_compute_forward_conv_2d( |
| 6754 |
const ggml_compute_params * params, |
| 6755 |
ggml_tensor * dst) { |
| 6756 |
|
| 6757 |
const ggml_tensor * src0 = dst->src[0]; |
| 6758 |
const ggml_tensor * src1 = dst->src[1]; |
| 6759 |
|
| 6760 |
ggml_compute_forward_conv_2d_impl(params, src0, src1, dst, src0->type); |
| 6761 |
} |
| 6762 |
|
| 6763 |
// ggml_compute_forward_conv_3d |
| 6764 |
|
| 6765 |
static void ggml_compute_forward_conv_3d_impl(const ggml_compute_params * params, |
| 6766 |
const ggml_tensor * kernel, |
| 6767 |
const ggml_tensor * src, |
| 6768 |
ggml_tensor * dst, |
| 6769 |
ggml_type kernel_type) { |
| 6770 |
|
| 6771 |
GGML_ASSERT(ggml_is_contiguous(kernel)); |
| 6772 |
GGML_ASSERT(kernel_type == GGML_TYPE_F16 || kernel_type == GGML_TYPE_F32); |
| 6773 |
GGML_ASSERT(kernel->type == kernel_type); |
| 6774 |
|
| 6775 |
const ggml_type_traits * traits = ggml_get_type_traits(kernel_type); |
| 6776 |
|
| 6777 |
const int32_t s0 = dst->op_params[0]; |
| 6778 |
const int32_t s1 = dst->op_params[1]; |
| 6779 |
const int32_t s2 = dst->op_params[2]; |
| 6780 |
const int32_t p0 = dst->op_params[3]; |
| 6781 |
const int32_t p1 = dst->op_params[4]; |
| 6782 |
const int32_t p2 = dst->op_params[5]; |
| 6783 |
const int32_t d0 = dst->op_params[6]; |
| 6784 |
const int32_t d1 = dst->op_params[7]; |
| 6785 |
const int32_t d2 = dst->op_params[8]; |
| 6786 |
const int32_t c = dst->op_params[9]; |
| 6787 |
const int32_t n = dst->op_params[10]; |
| 6788 |
const int32_t oc = dst->op_params[11]; |
| 6789 |
|
| 6790 |
const int64_t src_w = src->ne[0]; |
| 6791 |
const int64_t src_h = src->ne[1]; |
| 6792 |
const int64_t src_d = src->ne[2]; |
| 6793 |
const int64_t knl_w = kernel->ne[0]; |
| 6794 |
const int64_t knl_h = kernel->ne[1]; |
| 6795 |
const int64_t knl_d = kernel->ne[2]; |
| 6796 |
const int64_t dst_w = dst->ne[0]; |
| 6797 |
const int64_t dst_h = dst->ne[1]; |
| 6798 |
const int64_t dst_d = dst->ne[2]; |
| 6799 |
|
| 6800 |
const float * src_data = (float *) src->data; |
| 6801 |
void * knl_data = kernel->data; |
| 6802 |
float * dst_data = (float *) dst->data; |
| 6803 |
|
| 6804 |
const int64_t knl_n_per_channel = knl_w * knl_h * knl_d; |
| 6805 |
const int64_t knl_n_total = knl_n_per_channel * c; |
| 6806 |
const int64_t patch_total = n * dst_w * dst_h * dst_d; |
| 6807 |
|
| 6808 |
const int64_t space_per_patch = knl_n_total * traits->type_size + oc * sizeof(float); |
| 6809 |
const int64_t batch_size = params->wsize / space_per_patch; |
| 6810 |
const int64_t patches_per_batch = batch_size > 8 ? (batch_size / 8) * 8 : batch_size; |
| 6811 |
const int64_t batch_n = (patch_total + patches_per_batch - 1) / patches_per_batch; |
| 6812 |
|
| 6813 |
GGML_ASSERT(patches_per_batch > 0 && batch_size >= 1); |
| 6814 |
|
| 6815 |
void * tmp = params->wdata; |
| 6816 |
|
| 6817 |
for (int64_t batch_i = 0; batch_i < batch_n; ++batch_i) { |
| 6818 |
const int64_t patch_start_batch = batch_i * patches_per_batch; |
| 6819 |
const int64_t patch_end_batch = std::min(patch_start_batch + patches_per_batch, patch_total); |
| 6820 |
const int64_t patch_n_in_batch = patch_end_batch - patch_start_batch; |
| 6821 |
|
| 6822 |
const int64_t patch_per_thread = (patch_n_in_batch + params->nth - 1) / params->nth; |
| 6823 |
const int64_t patch_start = patch_start_batch + params->ith * patch_per_thread; |
| 6824 |
const int64_t patch_end = std::min(patch_start + patch_per_thread, patch_end_batch); |
| 6825 |
|
| 6826 |
for (int64_t p = patch_start; p < patch_end; ++p) { |
| 6827 |
const int64_t p_in_batch = p % (dst_w * dst_h * dst_d); |
| 6828 |
const int64_t p_in_depth = p_in_batch % (dst_w * dst_h); |
| 6829 |
const int64_t batch_idx = p / (dst_w * dst_h * dst_d); |
| 6830 |
const int64_t dst_z = p_in_batch / (dst_w * dst_h); |
| 6831 |
const int64_t dst_y = p_in_depth / dst_w; |
| 6832 |
const int64_t dst_x = p_in_depth % dst_w; |
| 6833 |
|
| 6834 |
char * dst_row = (char *) tmp + (p % patches_per_batch) * knl_n_total * traits->type_size; |
| 6835 |
|
| 6836 |
for (int64_t ic = 0; ic < c; ++ic) { |
| 6837 |
for (int64_t kz = 0; kz < knl_d; ++kz) { |
| 6838 |
for (int64_t ky = 0; ky < knl_h; ++ky) { |
| 6839 |
for (int64_t kx = 0; kx < knl_w; ++kx) { |
| 6840 |
const int64_t sz = dst_z * s2 + kz * d2 - p2; |
| 6841 |
const int64_t sy = dst_y * s1 + ky * d1 - p1; |
| 6842 |
const int64_t sx = dst_x * s0 + kx * d0 - p0; |
| 6843 |
|
| 6844 |
int64_t dst_idx = ic * knl_n_per_channel + kz * (knl_h * knl_w) + ky * knl_w + kx; |
| 6845 |
|
| 6846 |
float src_val; |
| 6847 |
if (sz < 0 || sz >= src_d || sy < 0 || sy >= src_h || sx < 0 || sx >= src_w) { |
| 6848 |
src_val = 0.0f; |
| 6849 |
} else { |
| 6850 |
const int64_t cn_idx = batch_idx * c + ic; |
| 6851 |
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]); |
| 6852 |
src_val = *src_ptr; |
| 6853 |
} |
| 6854 |
|
| 6855 |
char * element_ptr = dst_row + dst_idx * traits->type_size; |
| 6856 |
if (kernel_type == GGML_TYPE_F32) { |
| 6857 |
*(float *)element_ptr = src_val; |
| 6858 |
} else if (kernel_type == GGML_TYPE_F16) { |
| 6859 |
*(ggml_fp16_t *)element_ptr = GGML_CPU_FP32_TO_FP16(src_val); |
| 6860 |
} |
| 6861 |
} |
| 6862 |
} |
| 6863 |
} |
| 6864 |
} |
| 6865 |
} |
| 6866 |
|
| 6867 |
ggml_barrier(params->threadpool); |
| 6868 |
|
| 6869 |
float * gemm_output = (float *) ((char *) tmp + patches_per_batch * knl_n_total * traits->type_size); |
| 6870 |
ggml_call_mul_mat(kernel_type, params, patch_n_in_batch, oc, knl_n_total, tmp, knl_data, gemm_output); |
| 6871 |
|
| 6872 |
ggml_barrier(params->threadpool); |
| 6873 |
|
| 6874 |
const int64_t permute_per_thread = (patch_n_in_batch + params->nth - 1) / params->nth; |
| 6875 |
const int64_t permute_start = params->ith * permute_per_thread; |
| 6876 |
const int64_t permute_end = std::min(permute_start + permute_per_thread, patch_n_in_batch); |
| 6877 |
|
| 6878 |
for (int64_t i = permute_start; i < permute_end; ++i) { |
| 6879 |
const int64_t p = patch_start_batch + i; |
| 6880 |
const int64_t p_in_batch = p % (dst_w * dst_h * dst_d); |
| 6881 |
const int64_t p_in_depth = p_in_batch % (dst_w * dst_h); |
| 6882 |
const int64_t batch_idx = p / (dst_w * dst_h * dst_d); |
| 6883 |
const int64_t dst_z = p_in_batch / (dst_w * dst_h); |
| 6884 |
const int64_t dst_y = p_in_depth / dst_w; |
| 6885 |
const int64_t dst_x = p_in_depth % dst_w; |
| 6886 |
|
| 6887 |
for (int64_t ioc = 0; ioc < oc; ++ioc) { |
| 6888 |
const float value = gemm_output[i * oc + ioc]; |
| 6889 |
const int64_t ocn_idx = batch_idx * oc + ioc; |
| 6890 |
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]); |
| 6891 |
*dst_ptr = value; |
| 6892 |
} |
| 6893 |
} |
| 6894 |
} |
| 6895 |
} |
| 6896 |
|
| 6897 |
void ggml_compute_forward_conv_3d( |
| 6898 |
const ggml_compute_params * params, |
| 6899 |
ggml_tensor * dst) { |
| 6900 |
const ggml_tensor * src0 = dst->src[0]; |
| 6901 |
const ggml_tensor * src1 = dst->src[1]; |
| 6902 |
ggml_compute_forward_conv_3d_impl(params, src0, src1, dst, src0->type); |
| 6903 |
} |
| 6904 |
|
| 6905 |
// ggml_compute_forward_conv_transpose_2d |
| 6906 |
|
| 6907 |
void ggml_compute_forward_conv_transpose_2d( |
| 6908 |
const ggml_compute_params * params, |
| 6909 |
ggml_tensor * dst) { |
| 6910 |
|
| 6911 |
const ggml_tensor * src0 = dst->src[0]; |
| 6912 |
const ggml_tensor * src1 = dst->src[1]; |
| 6913 |
|
| 6914 |
GGML_ASSERT(src0->type == GGML_TYPE_F16); |
| 6915 |
GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 6916 |
GGML_ASSERT( dst->type == GGML_TYPE_F32); |
| 6917 |
|
| 6918 |
GGML_TENSOR_BINARY_OP_LOCALS |
| 6919 |
|
| 6920 |
const int ith = params->ith; |
| 6921 |
const int nth = params->nth; |
| 6922 |
|
| 6923 |
const int nk = ne00*ne01*ne02*ne03; |
| 6924 |
|
| 6925 |
GGML_ASSERT(nb00 == sizeof(ggml_fp16_t)); |
| 6926 |
GGML_ASSERT(nb10 == sizeof(float)); |
| 6927 |
|
| 6928 |
if (ith == 0) { |
| 6929 |
memset(params->wdata, 0, params->wsize); |
| 6930 |
|
| 6931 |
// permute kernel data (src0) from (Kw x Kh x Cout x Cin) to (Cin x Kw x Kh x Cout) |
| 6932 |
{ |
| 6933 |
ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + 0; |
| 6934 |
|
| 6935 |
for (int64_t i03 = 0; i03 < ne03; i03++) { |
| 6936 |
for (int64_t i02 = 0; i02 < ne02; i02++) { |
| 6937 |
const ggml_fp16_t * const src = (ggml_fp16_t *)((char *) src0->data + i03*nb03 + i02*nb02); |
| 6938 |
ggml_fp16_t * dst_data = wdata + i02*ne01*ne00*ne03; |
| 6939 |
for (int64_t i01 = 0; i01 < ne01; i01++) { |
| 6940 |
for (int64_t i00 = 0; i00 < ne00; i00++) { |
| 6941 |
dst_data[i01*ne00*ne03 + i00*ne03 + i03] = src[i01 * ne00 + i00]; |
| 6942 |
} |
| 6943 |
} |
| 6944 |
} |
| 6945 |
} |
| 6946 |
} |
| 6947 |
|
| 6948 |
// permute source data (src1) from (Sw x Sh x Cin) to (Cin x Sw x Sh) |
| 6949 |
{ |
| 6950 |
ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + nk; |
| 6951 |
for (int i12 = 0; i12 < ne12; i12++) { |
| 6952 |
for (int i11 = 0; i11 < ne11; i11++) { |
| 6953 |
const float * const src = (float *)((char *) src1->data + i12*nb12 + i11*nb11); |
| 6954 |
ggml_fp16_t * dst_data = wdata + i11*ne10*ne12; |
| 6955 |
for (int i10 = 0; i10 < ne10; i10++) { |
| 6956 |
dst_data[i10*ne12 + i12] = GGML_CPU_FP32_TO_FP16(src[i10]); |
| 6957 |
} |
| 6958 |
} |
| 6959 |
} |
| 6960 |
} |
| 6961 |
|
| 6962 |
memset(dst->data, 0, ggml_nbytes(dst)); |
| 6963 |
} |
| 6964 |
ggml_barrier(params->threadpool); |
| 6965 |
|
| 6966 |
const int32_t stride = ggml_get_op_params_i32(dst, 0); |
| 6967 |
|
| 6968 |
// total patches in dst |
| 6969 |
const int np = ne2; |
| 6970 |
|
| 6971 |
// patches per thread |
| 6972 |
const int dp = (np + nth - 1)/nth; |
| 6973 |
|
| 6974 |
// patch range for this thread |
| 6975 |
const int ip0 = dp*ith; |
| 6976 |
const int ip1 = MIN(ip0 + dp, np); |
| 6977 |
|
| 6978 |
ggml_fp16_t * const wdata = (ggml_fp16_t *) params->wdata + 0; |
| 6979 |
ggml_fp16_t * const wdata_src = wdata + nk; |
| 6980 |
|
| 6981 |
for (int i2 = ip0; i2 < ip1; i2++) { // Cout |
| 6982 |
float * dst_data = (float *)((char *) dst->data + i2*nb2); |
| 6983 |
ggml_fp16_t * wdata_kernel = wdata + i2*ne01*ne00*ne03; |
| 6984 |
for (int i11 = 0; i11 < ne11; i11++) { |
| 6985 |
for (int i10 = 0; i10 < ne10; i10++) { |
| 6986 |
const int i1n = i11*ne10*ne12 + i10*ne12; |
| 6987 |
for (int i01 = 0; i01 < ne01; i01++) { |
| 6988 |
for (int i00 = 0; i00 < ne00; i00++) { |
| 6989 |
float v = 0; |
| 6990 |
ggml_vec_dot_f16(ne03, &v, 0, |
| 6991 |
wdata_src + i1n, 0, |
| 6992 |
wdata_kernel + i01*ne00*ne03 + i00*ne03, 0, 1); |
| 6993 |
dst_data[(i11*stride + i01)*ne0 + i10*stride + i00] += v; |
| 6994 |
} |
| 6995 |
} |
| 6996 |
} |
| 6997 |
} |
| 6998 |
} |
| 6999 |
} |
| 7000 |
|
| 7001 |
// ggml_compute_forward_conv_2d_dw |
| 7002 |
|
| 7003 |
struct ggml_conv_2d_dw_params { |
| 7004 |
int64_t channels; |
| 7005 |
int64_t batch; |
| 7006 |
int64_t src_w; |
| 7007 |
int64_t src_h; |
| 7008 |
int64_t dst_w; |
| 7009 |
int64_t dst_h; |
| 7010 |
int64_t knl_w; |
| 7011 |
int64_t knl_h; |
| 7012 |
int stride_x; |
| 7013 |
int stride_y; |
| 7014 |
int pad_x; |
| 7015 |
int pad_y; |
| 7016 |
int dilation_x; |
| 7017 |
int dilation_y; |
| 7018 |
}; |
| 7019 |
|
| 7020 |
static void ggml_compute_forward_conv_2d_dw_cwhn( |
| 7021 |
const ggml_compute_params * params, |
| 7022 |
const ggml_tensor * src, |
| 7023 |
const ggml_tensor * kernel, |
| 7024 |
ggml_tensor * dst, |
| 7025 |
const ggml_conv_2d_dw_params & p) { |
| 7026 |
|
| 7027 |
const int64_t c = p.channels; |
| 7028 |
const float * knl_data = (const float *)kernel->data; |
| 7029 |
|
| 7030 |
const int64_t rows_total = p.dst_h * p.batch; |
| 7031 |
const int64_t rows_per_thread = (rows_total + params->nth - 1) / params->nth; |
| 7032 |
const int64_t row_start = params->ith * rows_per_thread; |
| 7033 |
const int64_t row_end = MIN(row_start + rows_per_thread, rows_total); |
| 7034 |
|
| 7035 |
#ifdef GGML_SIMD |
| 7036 |
#if defined(__ARM_FEATURE_SVE) |
| 7037 |
const int64_t pkg_size = svcntw(); |
| 7038 |
#else |
| 7039 |
const int64_t pkg_size = GGML_F32_EPR; |
| 7040 |
#endif |
| 7041 |
const int64_t pkg_count = c / pkg_size; |
| 7042 |
const int64_t c_pkg_end = pkg_count * pkg_size; |
| 7043 |
#else |
| 7044 |
const int64_t c_pkg_end = 0; |
| 7045 |
#endif |
| 7046 |
|
| 7047 |
for (int64_t row = row_start; row < row_end; ++row) { |
| 7048 |
const int64_t dst_y = row % p.dst_h; |
| 7049 |
const float * src_data = (const float *)src->data + (row / p.dst_h) * p.src_w * p.src_h * c; |
| 7050 |
for (int64_t dst_x = 0; dst_x < p.dst_w; ++dst_x) { |
| 7051 |
float * dst_data = (float *)dst->data + (row * p.dst_w + dst_x) * c; |
| 7052 |
const int64_t src_y_base = dst_y * p.stride_y - p.pad_y; |
| 7053 |
const int64_t src_x_base = dst_x * p.stride_x - p.pad_x; |
| 7054 |
|
| 7055 |
#ifdef GGML_SIMD |
| 7056 |
// Vectorized loop |
| 7057 |
for (int64_t c_i = 0; c_i < c_pkg_end; c_i += pkg_size) { |
| 7058 |
GGML_F32_VEC sum = GGML_F32_VEC_ZERO; |
| 7059 |
for (int64_t knl_y = 0; knl_y < p.knl_h; ++knl_y) { |
| 7060 |
const int64_t src_y = src_y_base + knl_y * p.dilation_y; |
| 7061 |
if (src_y < 0 || src_y >= p.src_h) { |
| 7062 |
continue; |
| 7063 |
} |
| 7064 |
for (int64_t knl_x = 0; knl_x < p.knl_w; ++knl_x) { |
| 7065 |
const int64_t src_x = src_x_base + knl_x * p.dilation_x; |
| 7066 |
if (src_x < 0 || src_x >= p.src_w) { |
| 7067 |
continue; |
| 7068 |
} |
| 7069 |
GGML_F32_VEC k = GGML_F32_VEC_LOAD(knl_data + (knl_y * p.knl_w + knl_x) * c + c_i); |
| 7070 |
GGML_F32_VEC s = GGML_F32_VEC_LOAD(src_data + (src_y * p.src_w + src_x) * c + c_i); |
| 7071 |
sum = GGML_F32_VEC_FMA(sum, k, s); |
| 7072 |
} |
| 7073 |
} |
| 7074 |
GGML_F32_VEC_STORE(dst_data + c_i, sum); |
| 7075 |
} |
| 7076 |
#endif |
| 7077 |
// Scalar loop |
| 7078 |
for (int64_t c_i = c_pkg_end; c_i < c; ++c_i) { |
| 7079 |
float sum = 0.0f; |
| 7080 |
for (int64_t knl_y = 0; knl_y < p.knl_h; ++knl_y) { |
| 7081 |
const int64_t src_y = src_y_base + knl_y * p.dilation_y; |
| 7082 |
if (src_y < 0 || src_y >= p.src_h) { |
| 7083 |
continue; |
| 7084 |
} |
| 7085 |
for (int64_t knl_x = 0; knl_x < p.knl_w; ++knl_x) { |
| 7086 |
const int64_t src_x = src_x_base + knl_x * p.dilation_x; |
| 7087 |
if (src_x < 0 || src_x >= p.src_w) { |
| 7088 |
continue; |
| 7089 |
} |
| 7090 |
sum += knl_data[(knl_y * p.knl_w + knl_x) * c + c_i] |
| 7091 |
* src_data[(src_y * p.src_w + src_x) * c + c_i]; |
| 7092 |
} |
| 7093 |
} |
| 7094 |
dst_data[c_i] = sum; |
| 7095 |
} |
| 7096 |
} |
| 7097 |
} |
| 7098 |
} |
| 7099 |
|
| 7100 |
static void ggml_compute_forward_conv_2d_dw_whcn( |
| 7101 |
const ggml_compute_params * params, |
| 7102 |
const ggml_tensor * src, |
| 7103 |
const ggml_tensor * kernel, |
| 7104 |
ggml_tensor * dst, |
| 7105 |
const ggml_conv_2d_dw_params & p) { |
| 7106 |
|
| 7107 |
const int64_t n = p.channels * p.batch; |
| 7108 |
const int64_t per_thread = (n + params->nth - 1) / params->nth; |
| 7109 |
const int64_t start = params->ith * per_thread; |
| 7110 |
const int64_t end = MIN(start + per_thread, n); |
| 7111 |
|
| 7112 |
for (int64_t i = start; i < end; ++i) { |
| 7113 |
const float * knl_data = (const float *)kernel->data + (i % p.channels) * p.knl_w * p.knl_h; |
| 7114 |
const float * src_data = (const float *)src->data + i * p.src_w * p.src_h; |
| 7115 |
float * dst_data = (float *)dst->data + i * p.dst_w * p.dst_h; |
| 7116 |
|
| 7117 |
for (int64_t dst_y = 0; dst_y < p.dst_h; ++dst_y) { |
| 7118 |
for (int64_t dst_x = 0; dst_x < p.dst_w; ++dst_x) { |
| 7119 |
|
| 7120 |
float sum = 0.0f; |
| 7121 |
for (int64_t knl_y = 0; knl_y < p.knl_h; ++knl_y) { |
| 7122 |
const int64_t src_y = dst_y * p.stride_y + knl_y * p.dilation_y - p.pad_y; |
| 7123 |
if (src_y < 0 || src_y >= p.src_h) { |
| 7124 |
continue; |
| 7125 |
} |
| 7126 |
for (int64_t knl_x = 0; knl_x < p.knl_w; ++knl_x) { |
| 7127 |
const int64_t src_x = dst_x * p.stride_x + knl_x * p.dilation_x - p.pad_x; |
| 7128 |
if (src_x < 0 || src_x >= p.src_w) { |
| 7129 |
continue; |
| 7130 |
} |
| 7131 |
sum += knl_data[knl_y * p.knl_w + knl_x] |
| 7132 |
* src_data[src_y * p.src_w + src_x]; |
| 7133 |
} |
| 7134 |
} |
| 7135 |
dst_data[dst_y * p.dst_w + dst_x] = sum; |
| 7136 |
} |
| 7137 |
} |
| 7138 |
} |
| 7139 |
} |
| 7140 |
|
| 7141 |
void ggml_compute_forward_conv_2d_dw( |
| 7142 |
const ggml_compute_params * params, |
| 7143 |
ggml_tensor * dst) { |
| 7144 |
|
| 7145 |
const ggml_tensor * kernel = dst->src[0]; |
| 7146 |
const ggml_tensor * src = dst->src[1]; |
| 7147 |
ggml_conv_2d_dw_params p; |
| 7148 |
p.channels = src->ne[2]; |
| 7149 |
p.batch = src->ne[3]; |
| 7150 |
p.src_w = src->ne[0]; |
| 7151 |
p.src_h = src->ne[1]; |
| 7152 |
p.dst_w = dst->ne[0]; |
| 7153 |
p.dst_h = dst->ne[1]; |
| 7154 |
p.knl_w = kernel->ne[0]; |
| 7155 |
p.knl_h = kernel->ne[1]; |
| 7156 |
p.stride_x = dst->op_params[0]; |
| 7157 |
p.stride_y = dst->op_params[1]; |
| 7158 |
p.pad_x = dst->op_params[2]; |
| 7159 |
p.pad_y = dst->op_params[3]; |
| 7160 |
p.dilation_x = dst->op_params[4]; |
| 7161 |
p.dilation_y = dst->op_params[5]; |
| 7162 |
|
| 7163 |
GGML_ASSERT(kernel->ne[3] == p.channels); |
| 7164 |
GGML_ASSERT(dst->ne[3] == p.batch); |
| 7165 |
|
| 7166 |
if (ggml_is_contiguous(src)) { |
| 7167 |
ggml_compute_forward_conv_2d_dw_whcn(params, src, kernel, dst, p); |
| 7168 |
} else if (ggml_is_contiguous_channels(src)) { |
| 7169 |
// kernel should also have channels most contiguous in memory |
| 7170 |
GGML_ASSERT(kernel->nb[0] >= kernel->nb[2] && kernel->nb[1] >= kernel->nb[0]); |
| 7171 |
ggml_compute_forward_conv_2d_dw_cwhn(params, src, kernel, dst, p); |
| 7172 |
} else { |
| 7173 |
GGML_ABORT("non-contiguous memory layout not supported"); |
| 7174 |
} |
| 7175 |
} |
| 7176 |
|
| 7177 |
// ggml_compute_forward_pool_1d_ksp |
| 7178 |
static void ggml_compute_forward_pool_1d_ksp( |
| 7179 |
const ggml_compute_params * params, |
| 7180 |
const ggml_op_pool op, |
| 7181 |
const int k, |
| 7182 |
const int s, |
| 7183 |
const int p, |
| 7184 |
ggml_tensor * dst) { |
| 7185 |
|
| 7186 |
const ggml_tensor * src = dst->src[0]; |
| 7187 |
|
| 7188 |
assert(src->type == GGML_TYPE_F32 || src->type == GGML_TYPE_F16); |
| 7189 |
|
| 7190 |
if (params->ith != 0) { |
| 7191 |
return; |
| 7192 |
} |
| 7193 |
|
| 7194 |
const int64_t IW = src->ne[0]; |
| 7195 |
const int64_t OW = dst->ne[0]; |
| 7196 |
|
| 7197 |
const int64_t nr = ggml_nrows(src); |
| 7198 |
|
| 7199 |
for (int64_t ir = 0; ir < nr; ++ir) { |
| 7200 |
const char * srow_bytes = (const char *) src->data + ir * src->nb[1]; |
| 7201 |
float * drow = (float *) (( char *) dst->data + ir * dst->nb[1]); |
| 7202 |
|
| 7203 |
for (int64_t ow = 0; ow < OW; ++ow) { |
| 7204 |
float res = 0; |
| 7205 |
switch (op) { |
| 7206 |
case GGML_OP_POOL_AVG: res = 0.0f; break; |
| 7207 |
case GGML_OP_POOL_MAX: res = -FLT_MAX; break; |
| 7208 |
case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error"); |
| 7209 |
} |
| 7210 |
|
| 7211 |
int count = 0; |
| 7212 |
const int base = (int) ow * s - p; |
| 7213 |
|
| 7214 |
for (int ki = 0; ki < k; ++ki) { |
| 7215 |
const int j = base + ki; |
| 7216 |
if (j < 0 || j >= (int) IW) { |
| 7217 |
continue; |
| 7218 |
} |
| 7219 |
|
| 7220 |
float v; |
| 7221 |
if (src->type == GGML_TYPE_F32) { |
| 7222 |
v = ((const float *) srow_bytes)[j]; |
| 7223 |
} else { |
| 7224 |
v = GGML_CPU_FP16_TO_FP32(((const ggml_fp16_t *) srow_bytes)[j]); |
| 7225 |
} |
| 7226 |
|
| 7227 |
switch (op) { |
| 7228 |
case GGML_OP_POOL_AVG: res += v; break; |
| 7229 |
case GGML_OP_POOL_MAX: res = std::max(v, res); break; |
| 7230 |
case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error"); |
| 7231 |
} |
| 7232 |
|
| 7233 |
++count; |
| 7234 |
} |
| 7235 |
|
| 7236 |
switch (op) { |
| 7237 |
case GGML_OP_POOL_AVG: res = (count > 0) ? (res / count) : 0.0f; break; |
| 7238 |
case GGML_OP_POOL_MAX: break; |
| 7239 |
case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error"); |
| 7240 |
} |
| 7241 |
|
| 7242 |
drow[ow] = res; |
| 7243 |
} |
| 7244 |
} |
| 7245 |
} |
| 7246 |
|
| 7247 |
// ggml_compute_forward_pool_1d |
| 7248 |
|
| 7249 |
void ggml_compute_forward_pool_1d( |
| 7250 |
const ggml_compute_params * params, |
| 7251 |
ggml_tensor * dst) { |
| 7252 |
|
| 7253 |
const int32_t * opts = (const int32_t *)dst->op_params; |
| 7254 |
ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]); |
| 7255 |
const int k0 = opts[1]; |
| 7256 |
const int s0 = opts[2]; |
| 7257 |
const int p0 = opts[3]; |
| 7258 |
|
| 7259 |
ggml_compute_forward_pool_1d_ksp(params, op, k0, s0, p0, dst); |
| 7260 |
} |
| 7261 |
|
| 7262 |
// ggml_compute_forward_pool_2d |
| 7263 |
|
| 7264 |
void ggml_compute_forward_pool_2d( |
| 7265 |
const ggml_compute_params * params, |
| 7266 |
ggml_tensor * dst) { |
| 7267 |
|
| 7268 |
const ggml_tensor * src = dst->src[0]; |
| 7269 |
|
| 7270 |
assert(src->type == GGML_TYPE_F32 || src->type == GGML_TYPE_F16); |
| 7271 |
|
| 7272 |
if (params->ith != 0) { |
| 7273 |
return; |
| 7274 |
} |
| 7275 |
|
| 7276 |
const int32_t * opts = (const int32_t *)dst->op_params; |
| 7277 |
|
| 7278 |
ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]); |
| 7279 |
const int k0 = opts[1]; |
| 7280 |
const int k1 = opts[2]; |
| 7281 |
const int s0 = opts[3]; |
| 7282 |
const int s1 = opts[4]; |
| 7283 |
const int p0 = opts[5]; |
| 7284 |
const int p1 = opts[6]; |
| 7285 |
const char * cdata = (const char*)src->data; |
| 7286 |
const char * const data_end = cdata + ggml_nbytes(src); |
| 7287 |
|
| 7288 |
const int64_t px = dst->ne[0]; |
| 7289 |
const int64_t py = dst->ne[1]; |
| 7290 |
const int64_t pa = px * py; |
| 7291 |
|
| 7292 |
float * dplane = (float *)dst->data; |
| 7293 |
|
| 7294 |
const int ka = k0 * k1; |
| 7295 |
const int offset0 = -p0; |
| 7296 |
const int offset1 = -p1; |
| 7297 |
|
| 7298 |
while (cdata < data_end) { |
| 7299 |
for (int oy = 0; oy < py; ++oy) { |
| 7300 |
float * const drow = dplane + oy * px; |
| 7301 |
float * const out = drow; |
| 7302 |
|
| 7303 |
for (int ox = 0; ox < px; ++ox) { |
| 7304 |
float res = 0; |
| 7305 |
switch (op) { |
| 7306 |
case GGML_OP_POOL_AVG: res = 0; break; |
| 7307 |
case GGML_OP_POOL_MAX: res = -FLT_MAX; break; |
| 7308 |
case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error"); |
| 7309 |
} |
| 7310 |
|
| 7311 |
const int ix = offset0 + ox * s0; |
| 7312 |
const int iy = offset1 + oy * s1; |
| 7313 |
|
| 7314 |
for (int ky = 0; ky < k1; ++ky) { |
| 7315 |
if (iy + ky < 0 || iy + ky >= src->ne[1]) { |
| 7316 |
continue; |
| 7317 |
} |
| 7318 |
|
| 7319 |
const void * srow = (const void *)(cdata + src->nb[1] * (iy + ky)); |
| 7320 |
for (int kx = 0; kx < k0; ++kx) { |
| 7321 |
int j = ix + kx; |
| 7322 |
if (j < 0 || j >= src->ne[0]) { |
| 7323 |
continue; |
| 7324 |
} |
| 7325 |
|
| 7326 |
const float srow_j = (src->type == GGML_TYPE_F32) ? ((const float*)srow)[j] : GGML_CPU_FP16_TO_FP32(((const ggml_fp16_t*)srow)[j]); |
| 7327 |
switch (op) { |
| 7328 |
case GGML_OP_POOL_AVG: res += srow_j; break; |
| 7329 |
case GGML_OP_POOL_MAX: res = std::max(srow_j, res); break; |
| 7330 |
case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error"); |
| 7331 |
} |
| 7332 |
} |
| 7333 |
} |
| 7334 |
switch (op) { |
| 7335 |
case GGML_OP_POOL_AVG: res /= ka; break; |
| 7336 |
case GGML_OP_POOL_MAX: break; |
| 7337 |
case GGML_OP_POOL_COUNT: GGML_ABORT("fatal error"); |
| 7338 |
} |
| 7339 |
|
| 7340 |
out[ox] = res; |
| 7341 |
} |
| 7342 |
} |
| 7343 |
|
| 7344 |
cdata += src->nb[2]; |
| 7345 |
dplane += pa; |
| 7346 |
} |
| 7347 |
} |
| 7348 |
|
| 7349 |
// ggml_compute_forward_pool_2d_back |
| 7350 |
|
| 7351 |
void ggml_compute_forward_pool_2d_back( |
| 7352 |
const ggml_compute_params * params, |
| 7353 |
ggml_tensor * dst) { |
| 7354 |
|
| 7355 |
const ggml_tensor * src = dst->src[0]; |
| 7356 |
const ggml_tensor * dstf = dst->src[1]; // forward tensor of dst |
| 7357 |
|
| 7358 |
assert(dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16); |
| 7359 |
|
| 7360 |
if (params->ith != 0) { |
| 7361 |
return; |
| 7362 |
} |
| 7363 |
|
| 7364 |
const int32_t * opts = (const int32_t *)dst->op_params; |
| 7365 |
ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]); |
| 7366 |
const int k0 = opts[1]; |
| 7367 |
const int k1 = opts[2]; |
| 7368 |
const int s0 = opts[3]; |
| 7369 |
const int s1 = opts[4]; |
| 7370 |
const int p0 = opts[5]; |
| 7371 |
const int p1 = opts[6]; |
| 7372 |
|
| 7373 |
char * cdata = (char *) dst->data; |
| 7374 |
const char * cdataf = (const char *) dstf->data; |
| 7375 |
const char * const data_end = cdata + ggml_nbytes(dst); |
| 7376 |
|
| 7377 |
GGML_ASSERT(params->ith == 0); |
| 7378 |
memset(cdata, 0, ggml_nbytes(dst)); |
| 7379 |
|
| 7380 |
const int64_t px = src->ne[0]; |
| 7381 |
const int64_t py = src->ne[1]; |
| 7382 |
const int64_t pa = px * py; |
| 7383 |
|
| 7384 |
const float * splane = (const float *) src->data; |
| 7385 |
|
| 7386 |
const int ka = k0 * k1; |
| 7387 |
const int offset0 = -p0; |
| 7388 |
const int offset1 = -p1; |
| 7389 |
|
| 7390 |
while (cdata < data_end) { |
| 7391 |
for (int oy = 0; oy < py; ++oy) { |
| 7392 |
const float * const srow = splane + oy * px; |
| 7393 |
for (int ox = 0; ox < px; ++ox) { |
| 7394 |
const float grad0 = srow[ox]; |
| 7395 |
|
| 7396 |
const int ix = offset0 + ox * s0; |
| 7397 |
const int iy = offset1 + oy * s1; |
| 7398 |
|
| 7399 |
if (op == GGML_OP_POOL_MAX) { |
| 7400 |
float maxval = -FLT_MAX; |
| 7401 |
int kxmax = -1; |
| 7402 |
int kymax = -1; |
| 7403 |
|
| 7404 |
for (int ky = 0; ky < k1; ++ky) { |
| 7405 |
if (iy + ky < 0 || iy + ky >= dst->ne[1]) { |
| 7406 |
continue; |
| 7407 |
} |
| 7408 |
const void * drowf = (const void *)(cdataf + dst->nb[1] * (iy + ky)); |
| 7409 |
for (int kx = 0; kx < k0; ++kx) { |
| 7410 |
int j = ix + kx; |
| 7411 |
if (j < 0 || j >= dst->ne[0]) { |
| 7412 |
continue; |
| 7413 |
} |
| 7414 |
|
| 7415 |
const float val = dst->type == GGML_TYPE_F32 ? |
| 7416 |
((const float *) drowf)[j] : GGML_CPU_FP16_TO_FP32(((const ggml_fp16_t *) drowf)[j]); |
| 7417 |
if (val <= maxval) { |
| 7418 |
continue; |
| 7419 |
} |
| 7420 |
|
| 7421 |
maxval = val; |
| 7422 |
kxmax = kx; |
| 7423 |
kymax = ky; |
| 7424 |
} |
| 7425 |
} |
| 7426 |
|
| 7427 |
if (kxmax == -1 || kymax == -1) { |
| 7428 |
continue; |
| 7429 |
} |
| 7430 |
|
| 7431 |
void * drow = (void *)(cdata + dst->nb[1] * (iy + kymax)); |
| 7432 |
const int j = ix + kxmax; |
| 7433 |
if (dst->type == GGML_TYPE_F32) { |
| 7434 |
((float *) drow)[j] += grad0; |
| 7435 |
} else { |
| 7436 |
((ggml_fp16_t *) drow)[j] = GGML_CPU_FP32_TO_FP16(grad0 + GGML_CPU_FP16_TO_FP32(((const ggml_fp16_t *) drow)[j])); |
| 7437 |
} |
| 7438 |
} else if (op == GGML_OP_POOL_AVG) { |
| 7439 |
const float grad = grad0 / ka; |
| 7440 |
|
| 7441 |
for (int ky = 0; ky < k1; ++ky) { |
| 7442 |
if (iy + ky < 0 || iy + ky >= dst->ne[1]) { |
| 7443 |
continue; |
| 7444 |
} |
| 7445 |
void * drow = (void *)(cdata + dst->nb[1] * (iy + ky)); |
| 7446 |
for (int kx = 0; kx < k0; ++kx) { |
| 7447 |
int j = ix + kx; |
| 7448 |
if (j < 0 || j >= dst->ne[0]) { |
| 7449 |
continue; |
| 7450 |
} |
| 7451 |
|
| 7452 |
if (dst->type == GGML_TYPE_F32) { |
| 7453 |
((float *) drow)[j] += grad; |
| 7454 |
} else { |
| 7455 |
((ggml_fp16_t *) drow)[j] += GGML_CPU_FP32_TO_FP16(grad); |
| 7456 |
} |
| 7457 |
} |
| 7458 |
} |
| 7459 |
} else { |
| 7460 |
GGML_ASSERT(false); |
| 7461 |
} |
| 7462 |
} |
| 7463 |
} |
| 7464 |
|
| 7465 |
cdata += dst->nb[2]; |
| 7466 |
cdataf += dst->nb[2]; |
| 7467 |
splane += pa; |
| 7468 |
} |
| 7469 |
} |
| 7470 |
|
| 7471 |
// ggml_compute_forward_upscale |
| 7472 |
|
| 7473 |
static void ggml_compute_forward_upscale_f32( |
| 7474 |
const ggml_compute_params * params, |
| 7475 |
ggml_tensor * dst) { |
| 7476 |
|
| 7477 |
const ggml_tensor * src0 = dst->src[0]; |
| 7478 |
|
| 7479 |
GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 7480 |
|
| 7481 |
const int ith = params->ith; |
| 7482 |
const int nth = params->nth; |
| 7483 |
|
| 7484 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 7485 |
|
| 7486 |
float sf0 = (float)ne0/src0->ne[0]; |
| 7487 |
float sf1 = (float)ne1/src0->ne[1]; |
| 7488 |
float sf2 = (float)ne2/src0->ne[2]; |
| 7489 |
float sf3 = (float)ne3/src0->ne[3]; |
| 7490 |
float pixel_offset = 0.5f; |
| 7491 |
|
| 7492 |
const int32_t mode_flags = ggml_get_op_params_i32(dst, 0); |
| 7493 |
const ggml_scale_mode mode = (ggml_scale_mode) (mode_flags & 0xFF); |
| 7494 |
|
| 7495 |
if (mode_flags & GGML_SCALE_FLAG_ALIGN_CORNERS) { |
| 7496 |
pixel_offset = 0.0f; |
| 7497 |
sf0 = ne0 > 1 && ne00 > 1 ? (float)(ne0 - 1) / (ne00 - 1) : sf0; |
| 7498 |
sf1 = ne1 > 1 && ne01 > 1 ? (float)(ne1 - 1) / (ne01 - 1) : sf1; |
| 7499 |
} |
| 7500 |
|
| 7501 |
if (mode == GGML_SCALE_MODE_NEAREST) { |
| 7502 |
for (int64_t i3 = 0; i3 < ne3; i3++) { |
| 7503 |
const int64_t i03 = i3 / sf3; |
| 7504 |
for (int64_t i2 = ith; i2 < ne2; i2 += nth) { |
| 7505 |
const int64_t i02 = i2 / sf2; |
| 7506 |
for (int64_t i1 = 0; i1 < ne1; i1++) { |
| 7507 |
const int64_t i01 = i1 / sf1; |
| 7508 |
for (int64_t i0 = 0; i0 < ne0; i0++) { |
| 7509 |
const int64_t i00 = i0 / sf0; |
| 7510 |
|
| 7511 |
const float * x = (float *)((char *) src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03); |
| 7512 |
float * y = (float *)((char *) dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3); |
| 7513 |
|
| 7514 |
*y = *x; |
| 7515 |
} |
| 7516 |
} |
| 7517 |
} |
| 7518 |
} |
| 7519 |
} else if (mode == GGML_SCALE_MODE_BILINEAR && (mode_flags & GGML_SCALE_FLAG_ANTIALIAS)) { |
| 7520 |
// Similar to F.interpolate(..., mode="bilinear", align_corners=False, antialias=True) |
| 7521 |
// https://github.com/pytorch/pytorch/blob/8871ff29b743948d1225389d5b7068f37b22750b/aten/src/ATen/native/cpu/UpSampleKernel.cpp |
| 7522 |
auto triangle_filter = [](float x) -> float { |
| 7523 |
return std::max(1.0f - fabsf(x), 0.0f); |
| 7524 |
}; |
| 7525 |
|
| 7526 |
// support and invscale, minimum 1 pixel for bilinear |
| 7527 |
const float support1 = std::max(1.0f, 1.0f / sf1); |
| 7528 |
const float invscale1 = 1.0f / support1; |
| 7529 |
const float support0 = std::max(1.0f, 1.0f / sf0); |
| 7530 |
const float invscale0 = 1.0f / support0; |
| 7531 |
|
| 7532 |
for (int64_t i3 = 0; i3 < ne3; i3++) { |
| 7533 |
const int64_t i03 = i3 / sf3; |
| 7534 |
for (int64_t i2 = ith; i2 < ne2; i2 += nth) { |
| 7535 |
const int64_t i02 = i2 / sf2; |
| 7536 |
for (int64_t i1 = 0; i1 < ne1; i1++) { |
| 7537 |
const float y = ((float) i1 + pixel_offset) / sf1; |
| 7538 |
for (int64_t i0 = 0; i0 < ne0; i0++) { |
| 7539 |
const float x = ((float) i0 + pixel_offset) / sf0; |
| 7540 |
|
| 7541 |
// the range of source pixels that contribute |
| 7542 |
const int64_t x_min = std::max<int64_t>(x - support0 + pixel_offset, 0); |
| 7543 |
const int64_t x_max = std::min<int64_t>(x + support0 + pixel_offset, ne00); |
| 7544 |
const int64_t y_min = std::max<int64_t>(y - support1 + pixel_offset, 0); |
| 7545 |
const int64_t y_max = std::min<int64_t>(y + support1 + pixel_offset, ne01); |
| 7546 |
|
| 7547 |
// bilinear filter with antialiasing |
| 7548 |
float val = 0.0f; |
| 7549 |
float total_weight = 0.0f; |
| 7550 |
|
| 7551 |
for (int64_t sy = y_min; sy < y_max; sy++) { |
| 7552 |
const float weight_y = triangle_filter((sy - y + pixel_offset) * invscale1); |
| 7553 |
|
| 7554 |
for (int64_t sx = x_min; sx < x_max; sx++) { |
| 7555 |
const float weight_x = triangle_filter((sx - x + pixel_offset) * invscale0); |
| 7556 |
const float weight = weight_x * weight_y; |
| 7557 |
|
| 7558 |
if (weight <= 0.0f) { |
| 7559 |
continue; |
| 7560 |
} |
| 7561 |
|
| 7562 |
const float pixel = *(const float *)((const char *)src0->data + sx*nb00 + sy*nb01 + i02*nb02 + i03*nb03); |
| 7563 |
val += pixel * weight; |
| 7564 |
total_weight += weight; |
| 7565 |
} |
| 7566 |
} |
| 7567 |
|
| 7568 |
if (total_weight > 0.0f) { |
| 7569 |
val /= total_weight; |
| 7570 |
} |
| 7571 |
|
| 7572 |
float * dst_ptr = (float *)((char *)dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3); |
| 7573 |
*dst_ptr = val; |
| 7574 |
} |
| 7575 |
} |
| 7576 |
} |
| 7577 |
} |
| 7578 |
} else if (mode == GGML_SCALE_MODE_BILINEAR) { |
| 7579 |
for (int64_t i3 = 0; i3 < ne3; i3++) { |
| 7580 |
const int64_t i03 = i3 / sf3; |
| 7581 |
for (int64_t i2 = ith; i2 < ne2; i2 += nth) { |
| 7582 |
const int64_t i02 = i2 / sf2; |
| 7583 |
for (int64_t i1 = 0; i1 < ne1; i1++) { |
| 7584 |
const float y = ((float)i1 + pixel_offset) / sf1 - pixel_offset; |
| 7585 |
int64_t y0 = (int64_t)floorf(y); |
| 7586 |
int64_t y1 = y0 + 1; |
| 7587 |
|
| 7588 |
y0 = std::max(int64_t(0), std::min(y0, ne01 - 1)); |
| 7589 |
y1 = std::max(int64_t(0), std::min(y1, ne01 - 1)); |
| 7590 |
|
| 7591 |
float dy = y - (float)y0; |
| 7592 |
dy = std::max(0.0f, std::min(dy, 1.0f)); |
| 7593 |
|
| 7594 |
for (int64_t i0 = 0; i0 < ne0; i0++) { |
| 7595 |
const float x = ((float)i0 + pixel_offset) / sf0 - pixel_offset; |
| 7596 |
int64_t x0 = (int64_t)floorf(x); |
| 7597 |
int64_t x1 = x0 + 1; |
| 7598 |
|
| 7599 |
x0 = std::max(int64_t(0), std::min(x0, ne00 - 1)); |
| 7600 |
x1 = std::max(int64_t(0), std::min(x1, ne00 - 1)); |
| 7601 |
|
| 7602 |
float dx = x - (float)x0; |
| 7603 |
dx = std::max(0.0f, std::min(dx, 1.0f)); |
| 7604 |
|
| 7605 |
// fetch the four surrounding pixel values and interpolate |
| 7606 |
const float a = *(const float *)((const char *)src0->data + x0*nb00 + y0*nb01 + i02*nb02 + i03*nb03); |
| 7607 |
const float b = *(const float *)((const char *)src0->data + x1*nb00 + y0*nb01 + i02*nb02 + i03*nb03); |
| 7608 |
const float c = *(const float *)((const char *)src0->data + x0*nb00 + y1*nb01 + i02*nb02 + i03*nb03); |
| 7609 |
const float d = *(const float *)((const char *)src0->data + x1*nb00 + y1*nb01 + i02*nb02 + i03*nb03); |
| 7610 |
|
| 7611 |
const float val = a*(1 - dx)*(1 - dy) + b*dx*(1 - dy) + c*(1 - dx)*dy + d*dx*dy; |
| 7612 |
|
| 7613 |
float * y_dst = (float *)((char *)dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3); |
| 7614 |
*y_dst = val; |
| 7615 |
} |
| 7616 |
} |
| 7617 |
} |
| 7618 |
} |
| 7619 |
} else if (mode == GGML_SCALE_MODE_BICUBIC) { |
| 7620 |
// https://en.wikipedia.org/wiki/Bicubic_interpolation#Bicubic_convolution_algorithm |
| 7621 |
const float a = -0.75f; // use alpha = -0.75 (same as PyTorch) |
| 7622 |
auto weight1 = [a](float x) { return ((a + 2) * x - (a + 3)) * x * x + 1; }; |
| 7623 |
auto weight2 = [a](float x) { return ((a * x - 5 * a) * x + 8 * a) * x - 4 * a; }; |
| 7624 |
auto bicubic = [=](float p0, float p1, float p2, float p3, float x) { |
| 7625 |
const float w0 = weight2(x + 1); |
| 7626 |
const float w1 = weight1(x + 0); |
| 7627 |
const float w2 = weight1(1 - x); |
| 7628 |
const float w3 = weight2(2 - x); |
| 7629 |
return p0*w0 + p1*w1 + p2*w2 + p3*w3; |
| 7630 |
}; |
| 7631 |
|
| 7632 |
for (int64_t i3 = 0; i3 < ne3; i3++) { |
| 7633 |
const int64_t i03 = i3 / sf3; |
| 7634 |
for (int64_t i2 = ith; i2 < ne2; i2 += nth) { |
| 7635 |
const int64_t i02 = i2 / sf2; |
| 7636 |
for (int64_t i1 = 0; i1 < ne1; i1++) { |
| 7637 |
const float y = ((float)i1 + pixel_offset) / sf1 - pixel_offset; |
| 7638 |
const int64_t y0 = (int64_t)floorf(y); |
| 7639 |
const float dy = y - (float)y0; |
| 7640 |
|
| 7641 |
for (int64_t i0 = 0; i0 < ne0; i0++) { |
| 7642 |
const float x = ((float)i0 + pixel_offset) / sf0 - pixel_offset; |
| 7643 |
const int64_t x0 = (int64_t)floorf(x); |
| 7644 |
const float dx = x - (float)x0; |
| 7645 |
|
| 7646 |
auto p = [=](int64_t x_off, int64_t y_off) -> float { |
| 7647 |
int64_t i00 = std::max(int64_t(0), std::min(x0 + x_off, ne00 - 1)); |
| 7648 |
int64_t i01 = std::max(int64_t(0), std::min(y0 + y_off, ne01 - 1)); |
| 7649 |
return *(const float *)((const char *)src0->data + i00*nb00 + i01*nb01 + i02*nb02 + i03*nb03); |
| 7650 |
}; |
| 7651 |
|
| 7652 |
const float val = bicubic( |
| 7653 |
bicubic(p(-1,-1), p(0,-1), p(1,-1), p(2,-1), dx), |
| 7654 |
bicubic(p(-1, 0), p(0, 0), p(1, 0), p(2, 0), dx), |
| 7655 |
bicubic(p(-1, 1), p(0, 1), p(1, 1), p(2, 1), dx), |
| 7656 |
bicubic(p(-1, 2), p(0, 2), p(1, 2), p(2, 2), dx), dy); |
| 7657 |
|
| 7658 |
float * y_dst = (float *)((char *)dst->data + i0*nb0 + i1*nb1 + i2*nb2 + i3*nb3); |
| 7659 |
*y_dst = val; |
| 7660 |
} |
| 7661 |
} |
| 7662 |
} |
| 7663 |
} |
| 7664 |
} else { |
| 7665 |
GGML_ABORT("unsupported upscale mode"); |
| 7666 |
} |
| 7667 |
} |
| 7668 |
|
| 7669 |
void ggml_compute_forward_upscale( |
| 7670 |
const ggml_compute_params * params, |
| 7671 |
ggml_tensor * dst) { |
| 7672 |
|
| 7673 |
const ggml_tensor * src0 = dst->src[0]; |
| 7674 |
|
| 7675 |
switch (src0->type) { |
| 7676 |
case GGML_TYPE_F32: |
| 7677 |
{ |
| 7678 |
ggml_compute_forward_upscale_f32(params, dst); |
| 7679 |
} break; |
| 7680 |
default: |
| 7681 |
{ |
| 7682 |
GGML_ABORT("fatal error"); |
| 7683 |
} |
| 7684 |
} |
| 7685 |
} |
| 7686 |
|
| 7687 |
|
| 7688 |
// ggml_compute_forward_pad |
| 7689 |
|
| 7690 |
template<bool circular_t> |
| 7691 |
static void ggml_compute_forward_pad_f32( |
| 7692 |
const ggml_compute_params * params, |
| 7693 |
ggml_tensor * dst) { |
| 7694 |
|
| 7695 |
const ggml_tensor * src0 = dst->src[0]; |
| 7696 |
|
| 7697 |
assert(dst->nb[0] == sizeof(float)); |
| 7698 |
|
| 7699 |
const int ith = params->ith; |
| 7700 |
const int nth = params->nth; |
| 7701 |
|
| 7702 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 7703 |
|
| 7704 |
float * dst_ptr = (float *) dst->data; |
| 7705 |
const int32_t lp0 = ggml_get_op_params_i32(dst, 0); |
| 7706 |
const int32_t rp0 = ggml_get_op_params_i32(dst, 1); |
| 7707 |
const int32_t lp1 = ggml_get_op_params_i32(dst, 2); |
| 7708 |
const int32_t rp1 = ggml_get_op_params_i32(dst, 3); |
| 7709 |
const int32_t lp2 = ggml_get_op_params_i32(dst, 4); |
| 7710 |
const int32_t rp2 = ggml_get_op_params_i32(dst, 5); |
| 7711 |
const int32_t lp3 = ggml_get_op_params_i32(dst, 6); |
| 7712 |
const int32_t rp3 = ggml_get_op_params_i32(dst, 7); |
| 7713 |
|
| 7714 |
// TODO: optimize |
| 7715 |
|
| 7716 |
for (int64_t i2 = 0; i2 < ne2; ++i2) { |
| 7717 |
for (int64_t i1 = ith; i1 < ne1; i1 += nth) { |
| 7718 |
for (int64_t i0 = 0; i0 < ne0; ++i0) { |
| 7719 |
for (int64_t i3 = 0; i3 < ne3; ++i3) { |
| 7720 |
// circular means wrap around on a torus, so x and y loop around |
| 7721 |
if constexpr (circular_t) { |
| 7722 |
const int64_t dst_idx = i3*(ne0*ne1*ne2) + i2*(ne0*ne1) + i1*ne0 + i0; |
| 7723 |
const int64_t src_i0 = ggml_wrap_around(i0 - lp0, ne00); |
| 7724 |
const int64_t src_i1 = ggml_wrap_around(i1 - lp1, ne01); |
| 7725 |
const int64_t src_i2 = ggml_wrap_around(i2 - lp2, ne02); |
| 7726 |
const int64_t src_i3 = ggml_wrap_around(i3 - lp3, ne03); |
| 7727 |
|
| 7728 |
const int64_t src_idx = |
| 7729 |
src_i3*nb03 + |
| 7730 |
src_i2*nb02 + |
| 7731 |
src_i1*nb01 + |
| 7732 |
src_i0*nb00; |
| 7733 |
|
| 7734 |
const float * src_ptr = (const float *)((char *) src0->data + src_idx); |
| 7735 |
dst_ptr[dst_idx] = *src_ptr; |
| 7736 |
} else { |
| 7737 |
const int64_t dst_idx = i3*(ne0*ne1*ne2) + i2*(ne0*ne1) + i1*ne0 + i0; |
| 7738 |
if ((i0 >= lp0 && i0 < ne0 - rp0) \ |
| 7739 |
&& (i1 >= lp1 && i1 < ne1 - rp1) \ |
| 7740 |
&& (i2 >= lp2 && i2 < ne2 - rp2) \ |
| 7741 |
&& (i3 >= lp3 && i3 < ne3 - rp3)) { |
| 7742 |
const int64_t src_idx = (i3 - lp3)*nb03 + (i2 - lp2)*nb02 + (i1 - lp1)*nb01 + (i0 - lp0)*nb00; |
| 7743 |
const float * src_ptr = (const float *)((char *) src0->data + src_idx); |
| 7744 |
dst_ptr[dst_idx] = *src_ptr; |
| 7745 |
} else { |
| 7746 |
dst_ptr[dst_idx] = 0; |
| 7747 |
} |
| 7748 |
} |
| 7749 |
} |
| 7750 |
} |
| 7751 |
} |
| 7752 |
} |
| 7753 |
} |
| 7754 |
|
| 7755 |
|
| 7756 |
void ggml_compute_forward_pad( |
| 7757 |
const ggml_compute_params * params, |
| 7758 |
ggml_tensor * dst) { |
| 7759 |
const ggml_tensor * src0 = dst->src[0]; |
| 7760 |
const bool circular = (bool) ggml_get_op_params_i32(dst, 8); |
| 7761 |
switch (src0->type) { |
| 7762 |
case GGML_TYPE_F32: |
| 7763 |
{ |
| 7764 |
if (circular) { |
| 7765 |
ggml_compute_forward_pad_f32<true>(params, dst); |
| 7766 |
} else { |
| 7767 |
ggml_compute_forward_pad_f32<false>(params, dst); |
| 7768 |
} |
| 7769 |
} break; |
| 7770 |
default: |
| 7771 |
{ |
| 7772 |
GGML_ABORT("fatal error"); |
| 7773 |
} |
| 7774 |
} |
| 7775 |
} |
| 7776 |
|
| 7777 |
// ggml_compute_forward_pad_reflect_1d |
| 7778 |
|
| 7779 |
void ggml_compute_forward_pad_reflect_1d( |
| 7780 |
const ggml_compute_params * params, |
| 7781 |
ggml_tensor * dst) { |
| 7782 |
|
| 7783 |
const ggml_tensor * src0 = dst->src[0]; |
| 7784 |
|
| 7785 |
GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 7786 |
GGML_ASSERT( dst->type == GGML_TYPE_F32); |
| 7787 |
|
| 7788 |
const int ith = params->ith; |
| 7789 |
const int nth = params->nth; |
| 7790 |
|
| 7791 |
const int32_t * opts = (const int32_t *) dst->op_params; |
| 7792 |
const int p0 = opts[0]; |
| 7793 |
const int p1 = opts[1]; |
| 7794 |
|
| 7795 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 7796 |
|
| 7797 |
for (int64_t i3 = 0; i3 < ne3; i3++) { |
| 7798 |
for (int64_t i2 = 0; i2 < ne2; i2++) { |
| 7799 |
for (int64_t i1 = ith; i1 < ne1; i1 += nth) { |
| 7800 |
float * left = (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + p0*nb0); |
| 7801 |
float * right = (float *) ((char *) dst->data + i3*nb3 + i2*nb2 + i1*nb1 + (ne0-p1-1)*nb0); |
| 7802 |
|
| 7803 |
ggml_vec_cpy_f32(ne00, left, (float *) ((char *) src0->data + i3*nb03 + i2*nb02 + i1*nb01)); |
| 7804 |
|
| 7805 |
for (int i0 = 1; i0 <= p0; i0++) { left[-i0] = left[i0]; } |
| 7806 |
for (int i0 = 1; i0 <= p1; i0++) { right[i0] = right[-i0]; } |
| 7807 |
} |
| 7808 |
} |
| 7809 |
} |
| 7810 |
} |
| 7811 |
|
| 7812 |
// ggml_compute_forward_roll |
| 7813 |
|
| 7814 |
static int64_t ggml_wrap_index(int64_t i, int64_t ne) { |
| 7815 |
if (i < 0) { |
| 7816 |
return i + ne; |
| 7817 |
} else if (i >= ne) { |
| 7818 |
return i - ne; |
| 7819 |
} |
| 7820 |
return i; |
| 7821 |
} |
| 7822 |
|
| 7823 |
static void ggml_compute_forward_roll_f32( |
| 7824 |
const ggml_compute_params * params, |
| 7825 |
ggml_tensor * dst) { |
| 7826 |
|
| 7827 |
const ggml_tensor * src0 = dst->src[0]; |
| 7828 |
const float * src_data = (const float *) src0->data; |
| 7829 |
float * dst_data = (float *) dst->data; |
| 7830 |
|
| 7831 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 7832 |
|
| 7833 |
const int s0 = ggml_get_op_params_i32(dst, 0); |
| 7834 |
const int s1 = ggml_get_op_params_i32(dst, 1); |
| 7835 |
const int s2 = ggml_get_op_params_i32(dst, 2); |
| 7836 |
const int s3 = ggml_get_op_params_i32(dst, 3); |
| 7837 |
|
| 7838 |
const int64_t total = ne1 * ne2 * ne3; |
| 7839 |
const int64_t per_thread = (total + params->nth) / params->nth; |
| 7840 |
const int64_t start = params->ith * per_thread; |
| 7841 |
const int64_t end = std::min(start + per_thread, total); |
| 7842 |
|
| 7843 |
for (int64_t i = start; i < end; ++i) { |
| 7844 |
const int64_t i1 = i % ne1; |
| 7845 |
const int64_t i2 = (i / ne1) % ne2; |
| 7846 |
const int64_t i3 = i / (ne2 * ne1); |
| 7847 |
float * dst_row = dst_data + (i3*nb3 + i2*nb2 + i1*nb1) / sizeof(float); |
| 7848 |
|
| 7849 |
const int64_t i01 = ggml_wrap_index(i1 - s1, ne01); |
| 7850 |
const int64_t i02 = ggml_wrap_index(i2 - s2, ne02); |
| 7851 |
const int64_t i03 = ggml_wrap_index(i3 - s3, ne03); |
| 7852 |
const float * src_row = src_data + (i03*nb03 + i02*nb02 + i01*nb01) / sizeof(float); |
| 7853 |
|
| 7854 |
const int64_t s = ggml_wrap_index(-s0, ne00); |
| 7855 |
const int64_t n = ne00 - s; |
| 7856 |
ggml_vec_cpy_f32(n, dst_row, src_row + s); |
| 7857 |
ggml_vec_cpy_f32(s, dst_row + n, src_row); |
| 7858 |
} |
| 7859 |
} |
| 7860 |
|
| 7861 |
void ggml_compute_forward_roll( |
| 7862 |
const ggml_compute_params * params, |
| 7863 |
ggml_tensor * dst) { |
| 7864 |
|
| 7865 |
const ggml_tensor * src0 = dst->src[0]; |
| 7866 |
|
| 7867 |
switch (src0->type) { |
| 7868 |
case GGML_TYPE_F32: |
| 7869 |
{ |
| 7870 |
ggml_compute_forward_roll_f32(params, dst); |
| 7871 |
} break; |
| 7872 |
default: |
| 7873 |
{ |
| 7874 |
GGML_ABORT("fatal error"); |
| 7875 |
} |
| 7876 |
} |
| 7877 |
} |
| 7878 |
|
| 7879 |
// ggml_compute_forward_arange |
| 7880 |
|
| 7881 |
static void ggml_compute_forward_arange_f32( |
| 7882 |
const ggml_compute_params * params, |
| 7883 |
ggml_tensor * dst) { |
| 7884 |
|
| 7885 |
GGML_ASSERT(dst->nb[0] == sizeof(float)); |
| 7886 |
|
| 7887 |
const int ith = params->ith; |
| 7888 |
const int nth = params->nth; |
| 7889 |
|
| 7890 |
const float start = ggml_get_op_params_f32(dst, 0); |
| 7891 |
const float stop = ggml_get_op_params_f32(dst, 1); |
| 7892 |
const float step = ggml_get_op_params_f32(dst, 2); |
| 7893 |
|
| 7894 |
const int64_t steps = (int64_t) ceilf((stop - start) / step); |
| 7895 |
|
| 7896 |
GGML_ASSERT(ggml_nelements(dst) == steps); |
| 7897 |
|
| 7898 |
for (int64_t i = ith; i < steps; i+= nth) { |
| 7899 |
float value = start + step * i; |
| 7900 |
((float *)dst->data)[i] = value; |
| 7901 |
} |
| 7902 |
} |
| 7903 |
|
| 7904 |
void ggml_compute_forward_arange( |
| 7905 |
const ggml_compute_params * params, |
| 7906 |
ggml_tensor * dst) { |
| 7907 |
switch (dst->type) { |
| 7908 |
case GGML_TYPE_F32: |
| 7909 |
{ |
| 7910 |
ggml_compute_forward_arange_f32(params, dst); |
| 7911 |
} break; |
| 7912 |
default: |
| 7913 |
{ |
| 7914 |
GGML_ABORT("fatal error"); |
| 7915 |
} |
| 7916 |
} |
| 7917 |
} |
| 7918 |
|
| 7919 |
static void ggml_compute_forward_timestep_embedding_f32( |
| 7920 |
const ggml_compute_params * params, |
| 7921 |
ggml_tensor * dst) { |
| 7922 |
|
| 7923 |
const ggml_tensor * src0 = dst->src[0]; |
| 7924 |
|
| 7925 |
GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 7926 |
|
| 7927 |
const int ith = params->ith; |
| 7928 |
const int nth = params->nth; |
| 7929 |
|
| 7930 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 7931 |
|
| 7932 |
const int dim = ggml_get_op_params_i32(dst, 0); |
| 7933 |
const int max_period = ggml_get_op_params_i32(dst, 1); |
| 7934 |
|
| 7935 |
int half = dim / 2; |
| 7936 |
|
| 7937 |
for (int64_t i = 0; i < ne00; i++) { |
| 7938 |
float * embed_data = (float *)((char *) dst->data + i*nb1); |
| 7939 |
for (int64_t j = ith; j < half; j += nth) { |
| 7940 |
float timestep = ((float *)src0->data)[i]; |
| 7941 |
float freq = (float)expf(-logf(max_period) * j / half); |
| 7942 |
float arg = timestep * freq; |
| 7943 |
embed_data[j] = cosf(arg); |
| 7944 |
embed_data[j + half] = sinf(arg); |
| 7945 |
} |
| 7946 |
if (dim % 2 != 0 && ith == 0) { |
| 7947 |
embed_data[2 * half] = 0.f; |
| 7948 |
} |
| 7949 |
} |
| 7950 |
} |
| 7951 |
|
| 7952 |
void ggml_compute_forward_timestep_embedding( |
| 7953 |
const ggml_compute_params * params, |
| 7954 |
ggml_tensor * dst) { |
| 7955 |
|
| 7956 |
const ggml_tensor * src0 = dst->src[0]; |
| 7957 |
|
| 7958 |
switch (src0->type) { |
| 7959 |
case GGML_TYPE_F32: |
| 7960 |
{ |
| 7961 |
ggml_compute_forward_timestep_embedding_f32(params, dst); |
| 7962 |
} break; |
| 7963 |
default: |
| 7964 |
{ |
| 7965 |
GGML_ABORT("fatal error"); |
| 7966 |
} |
| 7967 |
} |
| 7968 |
} |
| 7969 |
|
| 7970 |
// ggml_compute_forward_argsort |
| 7971 |
|
| 7972 |
template<enum ggml_sort_order order> |
| 7973 |
struct cmp_argsort { |
| 7974 |
const float * data; |
| 7975 |
bool operator()(int32_t a, int32_t b) const { |
| 7976 |
if constexpr (order == GGML_SORT_ORDER_ASC) { |
| 7977 |
return data[a] < data[b]; |
| 7978 |
} else { |
| 7979 |
return data[a] > data[b]; |
| 7980 |
} |
| 7981 |
} |
| 7982 |
}; |
| 7983 |
|
| 7984 |
static void ggml_compute_forward_argsort_f32( |
| 7985 |
const ggml_compute_params * params, |
| 7986 |
ggml_tensor * dst) { |
| 7987 |
|
| 7988 |
const ggml_tensor * src0 = dst->src[0]; |
| 7989 |
|
| 7990 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 7991 |
|
| 7992 |
GGML_ASSERT(nb0 == sizeof(float)); |
| 7993 |
|
| 7994 |
const int ith = params->ith; |
| 7995 |
const int nth = params->nth; |
| 7996 |
|
| 7997 |
const int64_t nr = ggml_nrows(src0); |
| 7998 |
|
| 7999 |
ggml_sort_order order = (ggml_sort_order) ggml_get_op_params_i32(dst, 0); |
| 8000 |
|
| 8001 |
for (int64_t i = ith; i < nr; i += nth) { |
| 8002 |
const float * src_data = (float *)((char *) src0->data + i*nb01); |
| 8003 |
|
| 8004 |
int32_t * dst_data = (int32_t *)((char *) dst->data + i*nb1); |
| 8005 |
|
| 8006 |
for (int64_t j = 0; j < ne0; j++) { |
| 8007 |
dst_data[j] = j; |
| 8008 |
} |
| 8009 |
|
| 8010 |
switch (order) { |
| 8011 |
case GGML_SORT_ORDER_ASC: |
| 8012 |
std::sort(dst_data, dst_data + ne0, cmp_argsort<GGML_SORT_ORDER_ASC>{src_data}); |
| 8013 |
break; |
| 8014 |
|
| 8015 |
case GGML_SORT_ORDER_DESC: |
| 8016 |
std::sort(dst_data, dst_data + ne0, cmp_argsort<GGML_SORT_ORDER_DESC>{src_data}); |
| 8017 |
break; |
| 8018 |
|
| 8019 |
default: |
| 8020 |
GGML_ABORT("invalid sort order"); |
| 8021 |
} |
| 8022 |
} |
| 8023 |
} |
| 8024 |
|
| 8025 |
void ggml_compute_forward_argsort( |
| 8026 |
const ggml_compute_params * params, |
| 8027 |
ggml_tensor * dst) { |
| 8028 |
|
| 8029 |
const ggml_tensor * src0 = dst->src[0]; |
| 8030 |
|
| 8031 |
switch (src0->type) { |
| 8032 |
case GGML_TYPE_F32: |
| 8033 |
{ |
| 8034 |
ggml_compute_forward_argsort_f32(params, dst); |
| 8035 |
} break; |
| 8036 |
default: |
| 8037 |
{ |
| 8038 |
GGML_ABORT("fatal error"); |
| 8039 |
} |
| 8040 |
} |
| 8041 |
} |
| 8042 |
|
| 8043 |
// ggml_compute_forward_top_k |
| 8044 |
|
| 8045 |
struct cmp_top_k { |
| 8046 |
const float * data; |
| 8047 |
bool operator()(int32_t a, int32_t b) const { |
| 8048 |
return data[a] > data[b]; |
| 8049 |
} |
| 8050 |
}; |
| 8051 |
|
| 8052 |
static void ggml_compute_forward_top_k_f32( |
| 8053 |
const ggml_compute_params * params, |
| 8054 |
ggml_tensor * dst) { |
| 8055 |
|
| 8056 |
const ggml_tensor * src0 = dst->src[0]; |
| 8057 |
|
| 8058 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 8059 |
|
| 8060 |
GGML_ASSERT(nb0 == sizeof(float)); |
| 8061 |
|
| 8062 |
const int ith = params->ith; |
| 8063 |
const int nth = params->nth; |
| 8064 |
|
| 8065 |
const int64_t nr = ggml_nrows(src0); |
| 8066 |
|
| 8067 |
const int top_k = ne0; |
| 8068 |
|
| 8069 |
int32_t * tmp = (int32_t *) params->wdata + (ne00 + CACHE_LINE_SIZE_F32) * ith; |
| 8070 |
|
| 8071 |
for (int64_t i = ith; i < nr; i += nth) { |
| 8072 |
const float * src_data = (float *)((char *) src0->data + i*nb01); |
| 8073 |
|
| 8074 |
for (int64_t j = 0; j < ne00; j++) { |
| 8075 |
tmp[j] = j; |
| 8076 |
} |
| 8077 |
|
| 8078 |
std::partial_sort(tmp, tmp + top_k, tmp + ne00, cmp_top_k{src_data}); |
| 8079 |
|
| 8080 |
int32_t * dst_data = (int32_t *)((char *) dst->data + i*nb1); |
| 8081 |
|
| 8082 |
std::copy(tmp, tmp + top_k, dst_data); |
| 8083 |
|
| 8084 |
// emphasize that the order is not important |
| 8085 |
if (top_k > 1) { |
| 8086 |
std::swap(dst_data[0], dst_data[1]); |
| 8087 |
} |
| 8088 |
} |
| 8089 |
} |
| 8090 |
|
| 8091 |
void ggml_compute_forward_top_k( |
| 8092 |
const ggml_compute_params * params, |
| 8093 |
ggml_tensor * dst) { |
| 8094 |
|
| 8095 |
const ggml_tensor * src0 = dst->src[0]; |
| 8096 |
|
| 8097 |
switch (src0->type) { |
| 8098 |
case GGML_TYPE_F32: |
| 8099 |
{ |
| 8100 |
ggml_compute_forward_top_k_f32(params, dst); |
| 8101 |
} break; |
| 8102 |
default: |
| 8103 |
{ |
| 8104 |
GGML_ABORT("fatal error"); |
| 8105 |
} |
| 8106 |
} |
| 8107 |
} |
| 8108 |
|
| 8109 |
static void ggml_compute_forward_flash_attn_ext_f16_one_chunk( |
| 8110 |
const ggml_compute_params * params, |
| 8111 |
ggml_tensor * dst, |
| 8112 |
int ir0, int ir1, |
| 8113 |
int64_t ic_start, int64_t ic_end, |
| 8114 |
float * partials, int64_t partial_stride) { |
| 8115 |
|
| 8116 |
const bool write_partials = (partials != nullptr); |
| 8117 |
const ggml_tensor * q = dst->src[0]; |
| 8118 |
const ggml_tensor * k = dst->src[1]; |
| 8119 |
const ggml_tensor * v = dst->src[2]; |
| 8120 |
const ggml_tensor * mask = dst->src[3]; |
| 8121 |
const ggml_tensor * sinks = dst->src[4]; |
| 8122 |
|
| 8123 |
GGML_TENSOR_LOCALS(int64_t, neq, q, ne) |
| 8124 |
GGML_TENSOR_LOCALS(size_t, nbq, q, nb) |
| 8125 |
GGML_TENSOR_LOCALS(int64_t, nek, k, ne) |
| 8126 |
GGML_TENSOR_LOCALS(size_t, nbk, k, nb) |
| 8127 |
GGML_TENSOR_LOCALS(int64_t, nev, v, ne) |
| 8128 |
GGML_TENSOR_LOCALS(size_t, nbv, v, nb) |
| 8129 |
GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 8130 |
GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 8131 |
|
| 8132 |
const int64_t DK = nek0; |
| 8133 |
const int64_t DV = nev0; |
| 8134 |
const int64_t N = neq1; |
| 8135 |
|
| 8136 |
GGML_ASSERT(ne0 == DV); |
| 8137 |
GGML_ASSERT(ne2 == N); |
| 8138 |
|
| 8139 |
// input tensor rows must be contiguous |
| 8140 |
GGML_ASSERT(nbq0 == ggml_type_size(q->type)); |
| 8141 |
GGML_ASSERT(nbk0 == ggml_type_size(k->type)); |
| 8142 |
GGML_ASSERT(nbv0 == ggml_type_size(v->type)); |
| 8143 |
|
| 8144 |
GGML_ASSERT(neq0 == DK); |
| 8145 |
GGML_ASSERT(nek0 == DK); |
| 8146 |
GGML_ASSERT(nev0 == DV); |
| 8147 |
|
| 8148 |
GGML_ASSERT(neq1 == N); |
| 8149 |
|
| 8150 |
// dst cannot be transposed or permuted |
| 8151 |
GGML_ASSERT(nb0 == sizeof(float)); |
| 8152 |
GGML_ASSERT(nb0 <= nb1); |
| 8153 |
GGML_ASSERT(nb1 <= nb2); |
| 8154 |
GGML_ASSERT(nb2 <= nb3); |
| 8155 |
|
| 8156 |
// broadcast factors |
| 8157 |
const int64_t rk2 = neq2/nek2; |
| 8158 |
const int64_t rk3 = neq3/nek3; |
| 8159 |
|
| 8160 |
const int64_t rv2 = neq2/nev2; |
| 8161 |
const int64_t rv3 = neq3/nev3; |
| 8162 |
|
| 8163 |
// parallelize by q rows using ggml_vec_dot_f32 |
| 8164 |
|
| 8165 |
float scale = 1.0f; |
| 8166 |
float max_bias = 0.0f; |
| 8167 |
float logit_softcap = 0.0f; |
| 8168 |
|
| 8169 |
memcpy(&scale, (float *) dst->op_params + 0, sizeof(float)); |
| 8170 |
memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float)); |
| 8171 |
memcpy(&logit_softcap, (float *) dst->op_params + 2, sizeof(float)); |
| 8172 |
|
| 8173 |
if (logit_softcap != 0) { |
| 8174 |
scale /= logit_softcap; |
| 8175 |
} |
| 8176 |
|
| 8177 |
const uint32_t n_head = neq2; |
| 8178 |
const uint32_t n_head_log2 = 1u << (uint32_t) floor(log2(n_head)); |
| 8179 |
|
| 8180 |
const float m0 = powf(2.0f, -(max_bias ) / n_head_log2); |
| 8181 |
const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2); |
| 8182 |
|
| 8183 |
ggml_type const k_vec_dot_type = ggml_get_type_traits_cpu(k->type)->vec_dot_type; |
| 8184 |
ggml_from_float_t const q_to_vec_dot = ggml_get_type_traits_cpu(k_vec_dot_type)->from_float; |
| 8185 |
ggml_vec_dot_t const kq_vec_dot = ggml_get_type_traits_cpu(k->type)->vec_dot; |
| 8186 |
ggml_to_float_t const v_to_float = ggml_get_type_traits(v->type)->to_float; |
| 8187 |
|
| 8188 |
GGML_ASSERT(( q_to_vec_dot) && "fattn: unsupported K-type"); |
| 8189 |
GGML_ASSERT((v->type == GGML_TYPE_F32 || v_to_float ) && "fattn: unsupported V-type"); |
| 8190 |
|
| 8191 |
int ith = params->ith; |
| 8192 |
|
| 8193 |
for (int ir = ir0; ir < ir1; ++ir) { |
| 8194 |
// q indices |
| 8195 |
const int iq3 = ir/(neq2*neq1); |
| 8196 |
const int iq2 = (ir - iq3*neq2*neq1)/neq1; |
| 8197 |
const int iq1 = (ir - iq3*neq2*neq1 - iq2*neq1); |
| 8198 |
|
| 8199 |
const uint32_t h = iq2; // head index |
| 8200 |
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; |
| 8201 |
|
| 8202 |
float S = 0.0f; // sum |
| 8203 |
float M = -INFINITY; // maximum KQ value |
| 8204 |
|
| 8205 |
float * VKQ32 = (float *) params->wdata + ith*(1*DK + 2*DV + CACHE_LINE_SIZE_F32); // FP32 VKQ accumulator |
| 8206 |
float * V32 = (VKQ32 + 1*DV); // (temporary) FP32 V buffer |
| 8207 |
ggml_fp16_t * VKQ16 = (ggml_fp16_t *) (VKQ32 + 1*DV); // (temporary) FP16 VKQ accumulator |
| 8208 |
ggml_fp16_t * Q_q = (ggml_fp16_t *) (VKQ32 + 2*DV); // (temporary) buffer for Q converted to quantized/FP16 |
| 8209 |
|
| 8210 |
if (v->type == GGML_TYPE_F16) { |
| 8211 |
memset(VKQ16, 0, DV*sizeof(ggml_fp16_t)); |
| 8212 |
} else { |
| 8213 |
memset(VKQ32, 0, DV*sizeof(float)); |
| 8214 |
} |
| 8215 |
|
| 8216 |
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; |
| 8217 |
|
| 8218 |
// k indices |
| 8219 |
const int ik3 = iq3 / rk3; |
| 8220 |
const int ik2 = iq2 / rk2; |
| 8221 |
|
| 8222 |
// v indices |
| 8223 |
const int iv3 = iq3 / rv3; |
| 8224 |
const int iv2 = iq2 / rv2; |
| 8225 |
|
| 8226 |
const float * pq = (const float *) ((char *) q->data + (iq1*nbq1 + iq2*nbq2 + iq3*nbq3)); |
| 8227 |
q_to_vec_dot(pq, Q_q, DK); |
| 8228 |
|
| 8229 |
// online softmax / attention |
| 8230 |
// loop over n_kv and n_head_kv |
| 8231 |
// ref: https://arxiv.org/pdf/2112.05682.pdf |
| 8232 |
|
| 8233 |
for (int64_t ic = ic_start; ic < ic_end; ++ic) { |
| 8234 |
const float mv = mp ? slope*GGML_CPU_FP16_TO_FP32(mp[ic]) : 0.0f; |
| 8235 |
if (mv == -INFINITY) { |
| 8236 |
continue; |
| 8237 |
} |
| 8238 |
|
| 8239 |
float s; // KQ value |
| 8240 |
|
| 8241 |
const char * k_data = (const char *) k->data + ( ic*nbk1 + ik2*nbk2 + ik3*nbk3); |
| 8242 |
kq_vec_dot(DK, &s, 0, k_data, 0, Q_q, 0, 1); |
| 8243 |
|
| 8244 |
s = s*scale; // scale KQ value |
| 8245 |
|
| 8246 |
if (logit_softcap != 0.0f) { |
| 8247 |
s = logit_softcap*tanhf(s); |
| 8248 |
} |
| 8249 |
|
| 8250 |
s += mv; // apply mask |
| 8251 |
|
| 8252 |
const float Mold = M; |
| 8253 |
|
| 8254 |
float ms = 1.0f; // upon new higher max val, scale VKQ and KQ sum with this value |
| 8255 |
float vs = 1.0f; // post-softmax KQ value, expf(s - M) |
| 8256 |
|
| 8257 |
const char * v_data = ((const char *) v->data + (ic*nbv1 + iv2*nbv2 + iv3*nbv3)); |
| 8258 |
|
| 8259 |
if (v->type == GGML_TYPE_F16) { |
| 8260 |
if (s > M) { |
| 8261 |
// s is new maximum, ms < 1.0f, vs == expf(s - s) == 1.0f |
| 8262 |
M = s; |
| 8263 |
ms = expf(Mold - M); |
| 8264 |
|
| 8265 |
// V = V*expf(Mold - M) |
| 8266 |
ggml_vec_scale_f16(DV, VKQ16, ms); |
| 8267 |
} else { |
| 8268 |
// no new maximum, ms == 1.0f, vs != 1.0f |
| 8269 |
vs = expf(s - M); |
| 8270 |
} |
| 8271 |
|
| 8272 |
// V += v*expf(s - M) |
| 8273 |
ggml_vec_mad_f16(DV, VKQ16, (const ggml_fp16_t *) v_data, vs); |
| 8274 |
} else { |
| 8275 |
if (s > M) { |
| 8276 |
// s is new maximum, ms < 1.0f, vs == expf(s - s) == 1.0f |
| 8277 |
M = s; |
| 8278 |
ms = expf(Mold - M); |
| 8279 |
|
| 8280 |
// V = V*expf(Mold - M) |
| 8281 |
ggml_vec_scale_f32(DV, VKQ32, ms); |
| 8282 |
} else { |
| 8283 |
// no new maximum, ms == 1.0f, vs != 1.0f |
| 8284 |
vs = expf(s - M); |
| 8285 |
} |
| 8286 |
|
| 8287 |
// V += v*expf(s - M) |
| 8288 |
if (v_to_float) { |
| 8289 |
v_to_float(v_data, V32, DV); |
| 8290 |
ggml_vec_mad_f32(DV, VKQ32, V32, vs); |
| 8291 |
} else { |
| 8292 |
// V is F32 |
| 8293 |
ggml_vec_mad_f32(DV, VKQ32, (const float *) v_data, vs); |
| 8294 |
} |
| 8295 |
} |
| 8296 |
|
| 8297 |
S = S*ms + vs; // scale and increment sum with partial sum |
| 8298 |
} |
| 8299 |
|
| 8300 |
if (v->type == GGML_TYPE_F16) { |
| 8301 |
for (int64_t d = 0; d < DV; ++d) { |
| 8302 |
VKQ32[d] = GGML_CPU_FP16_TO_FP32(VKQ16[d]); |
| 8303 |
} |
| 8304 |
} |
| 8305 |
|
| 8306 |
// sinks - apply only on the first kv-chunk |
| 8307 |
if (sinks && ic_start == 0) { |
| 8308 |
const float s = ((float *)((char *) sinks->data))[h]; |
| 8309 |
|
| 8310 |
float ms = 1.0f; |
| 8311 |
float vs = 1.0f; |
| 8312 |
|
| 8313 |
if (s > M) { |
| 8314 |
ms = expf(M - s); |
| 8315 |
M = s; |
| 8316 |
ggml_vec_scale_f32(DV, VKQ32, ms); |
| 8317 |
} else { |
| 8318 |
vs = expf(s - M); |
| 8319 |
} |
| 8320 |
|
| 8321 |
S = S*ms + vs; |
| 8322 |
} |
| 8323 |
|
| 8324 |
if (write_partials) { |
| 8325 |
// Write M, S, VKQ to partials for later reduction |
| 8326 |
// partials layout: [M, S, VKQ[DV]] per query head |
| 8327 |
float * partial = partials + ir * partial_stride; |
| 8328 |
partial[0] = M; |
| 8329 |
partial[1] = S; |
| 8330 |
memcpy(partial + 2, VKQ32, DV * sizeof(float)); |
| 8331 |
} else { |
| 8332 |
// V /= S |
| 8333 |
const float S_inv = S == 0.0f ? 0.0f : 1.0f/S; |
| 8334 |
ggml_vec_scale_f32(DV, VKQ32, S_inv); |
| 8335 |
|
| 8336 |
// dst indices |
| 8337 |
const int i1 = iq1; |
| 8338 |
const int i2 = iq2; |
| 8339 |
const int i3 = iq3; |
| 8340 |
|
| 8341 |
// permute(0, 2, 1, 3) |
| 8342 |
memcpy((char *) dst->data + (i3*ne2*ne1 + i2 + i1*ne1)*nb1, VKQ32, nb1); |
| 8343 |
} |
| 8344 |
} |
| 8345 |
} |
| 8346 |
|
| 8347 |
static void ggml_compute_forward_flash_attn_ext_tiled( |
| 8348 |
const ggml_compute_params * params, |
| 8349 |
ggml_tensor * dst, |
| 8350 |
int ir0, int ir1) { |
| 8351 |
const ggml_tensor * q = dst->src[0]; |
| 8352 |
const ggml_tensor * k = dst->src[1]; |
| 8353 |
const ggml_tensor * v = dst->src[2]; |
| 8354 |
const ggml_tensor * mask = dst->src[3]; |
| 8355 |
const ggml_tensor * sinks = dst->src[4]; |
| 8356 |
|
| 8357 |
GGML_TENSOR_LOCALS(int64_t, neq, q, ne) |
| 8358 |
GGML_TENSOR_LOCALS(size_t, nbq, q, nb) |
| 8359 |
GGML_TENSOR_LOCALS(int64_t, nek, k, ne) |
| 8360 |
GGML_TENSOR_LOCALS(size_t, nbk, k, nb) |
| 8361 |
GGML_TENSOR_LOCALS(int64_t, nev, v, ne) |
| 8362 |
GGML_TENSOR_LOCALS(size_t, nbv, v, nb) |
| 8363 |
GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 8364 |
GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 8365 |
|
| 8366 |
const int64_t DK = nek0; |
| 8367 |
const int64_t DV = nev0; |
| 8368 |
const int64_t N = neq1; |
| 8369 |
|
| 8370 |
GGML_ASSERT(ne0 == DV); |
| 8371 |
GGML_ASSERT(ne2 == N); |
| 8372 |
|
| 8373 |
// input tensor rows must be contiguous |
| 8374 |
GGML_ASSERT(nbq0 == ggml_type_size(q->type)); |
| 8375 |
GGML_ASSERT(nbk0 == ggml_type_size(k->type)); |
| 8376 |
GGML_ASSERT(nbv0 == ggml_type_size(v->type)); |
| 8377 |
|
| 8378 |
GGML_ASSERT(neq0 == DK); |
| 8379 |
GGML_ASSERT(nek0 == DK); |
| 8380 |
GGML_ASSERT(nev0 == DV); |
| 8381 |
|
| 8382 |
GGML_ASSERT(neq1 == N); |
| 8383 |
|
| 8384 |
// dst cannot be transposed or permuted |
| 8385 |
GGML_ASSERT(nb0 == sizeof(float)); |
| 8386 |
GGML_ASSERT(nb0 <= nb1); |
| 8387 |
GGML_ASSERT(nb1 <= nb2); |
| 8388 |
GGML_ASSERT(nb2 <= nb3); |
| 8389 |
|
| 8390 |
GGML_ASSERT(k->type == v->type); |
| 8391 |
const ggml_type kv_type = k->type; |
| 8392 |
|
| 8393 |
|
| 8394 |
// broadcast factors |
| 8395 |
const int64_t rk2 = neq2/nek2; |
| 8396 |
const int64_t rk3 = neq3/nek3; |
| 8397 |
|
| 8398 |
const int64_t rv2 = neq2/nev2; |
| 8399 |
const int64_t rv3 = neq3/nev3; |
| 8400 |
|
| 8401 |
float scale = 1.0f; |
| 8402 |
float max_bias = 0.0f; |
| 8403 |
float logit_softcap = 0.0f; |
| 8404 |
|
| 8405 |
memcpy(&scale, (float *) dst->op_params + 0, sizeof(float)); |
| 8406 |
memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float)); |
| 8407 |
memcpy(&logit_softcap, (float *) dst->op_params + 2, sizeof(float)); |
| 8408 |
|
| 8409 |
if (logit_softcap != 0) { |
| 8410 |
scale /= logit_softcap; |
| 8411 |
} |
| 8412 |
|
| 8413 |
const uint32_t n_head = neq2; |
| 8414 |
const uint32_t n_head_log2 = 1u << (uint32_t) floor(log2(n_head)); |
| 8415 |
|
| 8416 |
const float m0 = powf(2.0f, -(max_bias ) / n_head_log2); |
| 8417 |
const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2); |
| 8418 |
|
| 8419 |
int ith = params->ith; |
| 8420 |
|
| 8421 |
static constexpr int Q_TILE_SZ = ggml_fa_tile_config::Q; |
| 8422 |
static constexpr int KV_TILE_SZ = ggml_fa_tile_config::KV; |
| 8423 |
|
| 8424 |
int ir = ir0; |
| 8425 |
while (ir < ir1) { |
| 8426 |
// q indices for the start of this tile |
| 8427 |
const int iq3 = ir/(neq2*neq1); |
| 8428 |
const int iq2 = (ir - iq3*neq2*neq1)/neq1; |
| 8429 |
const int iq1 = (ir - iq3*neq2*neq1 - iq2*neq1); |
| 8430 |
|
| 8431 |
// Number of valid rows in this tile: |
| 8432 |
// - limited by tile size (Q_TILE_SZ) |
| 8433 |
// - limited by chunk boundary (ir1 - ir) |
| 8434 |
// - limited by head boundary (neq1 - iq1) to avoid crossing into next head |
| 8435 |
const int tile_rows = MIN(Q_TILE_SZ, MIN((int)(ir1 - ir), (int)(neq1 - iq1))); |
| 8436 |
GGML_ASSERT(tile_rows > 0); |
| 8437 |
|
| 8438 |
const uint32_t h = iq2; // head index |
| 8439 |
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; |
| 8440 |
|
| 8441 |
float S[Q_TILE_SZ]; |
| 8442 |
float M[Q_TILE_SZ]; |
| 8443 |
|
| 8444 |
for (int i = 0 ; i < Q_TILE_SZ; ++i) { |
| 8445 |
S[i] = 0.; |
| 8446 |
M[i] = -INFINITY; |
| 8447 |
} |
| 8448 |
|
| 8449 |
// Per-thread scratch layout: |
| 8450 |
// Q_q: Q_TILE_SZ * DK (converted Q tile — F32 for GEMM, KV type for scalar) |
| 8451 |
// KQ: Q_TILE_SZ * KV_TILE_SZ (attention scores in float) |
| 8452 |
// mask: Q_TILE_SZ * KV_TILE_SZ (mask in float) |
| 8453 |
// VKQ32: Q_TILE_SZ * DV (FP32 output accumulator) |
| 8454 |
// V32: KV_TILE_SZ * DV (F32 buffer for V tile) |
| 8455 |
// K_f32: KV_TILE_SZ * DK (F32 buffer for K tile — GEMM path) |
| 8456 |
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); |
| 8457 |
|
| 8458 |
void * Q_q = base; |
| 8459 |
float * KQ = (float *)((char *)base + Q_TILE_SZ * DK * sizeof(float)); |
| 8460 |
float * mask32 = KQ + Q_TILE_SZ * KV_TILE_SZ; |
| 8461 |
float * VKQ32 = mask32 + Q_TILE_SZ * KV_TILE_SZ; |
| 8462 |
float * V32 = VKQ32 + Q_TILE_SZ * DV; |
| 8463 |
float * K_f32 = V32 + KV_TILE_SZ * DV; |
| 8464 |
|
| 8465 |
memset(VKQ32, 0, Q_TILE_SZ * DV * sizeof(float)); |
| 8466 |
memset(mask32, 0, Q_TILE_SZ * KV_TILE_SZ * sizeof(float)); |
| 8467 |
|
| 8468 |
// k indices |
| 8469 |
const int ik3 = iq3 / rk3; |
| 8470 |
const int ik2 = iq2 / rk2; |
| 8471 |
|
| 8472 |
// v indices |
| 8473 |
const int iv3 = iq3 / rv3; |
| 8474 |
const int iv2 = iq2 / rv2; |
| 8475 |
|
| 8476 |
{ |
| 8477 |
float * Q_f32 = (float *)Q_q; |
| 8478 |
for (int tq = 0; tq < tile_rows; tq++) { |
| 8479 |
const float * pq = (const float *) ((char *) q->data + ((iq1 + tq)*nbq1 + iq2*nbq2 + iq3*nbq3)); |
| 8480 |
memcpy(Q_f32 + tq * DK, pq, DK * sizeof(float)); |
| 8481 |
} |
| 8482 |
for (int tq = tile_rows; tq < Q_TILE_SZ; tq++) { |
| 8483 |
memset(Q_f32 + tq * DK, 0, DK * sizeof(float)); |
| 8484 |
} |
| 8485 |
} |
| 8486 |
|
| 8487 |
memset(K_f32, 0, DK * KV_TILE_SZ * sizeof(float)); |
| 8488 |
memset(V32, 0, KV_TILE_SZ * DV * sizeof(float)); |
| 8489 |
|
| 8490 |
for (int64_t ic = 0; ic < nek1; ic += KV_TILE_SZ) { |
| 8491 |
const int kv_tile = (int)std::min((int64_t)KV_TILE_SZ, nek1 - ic); |
| 8492 |
|
| 8493 |
// skip the tile entirely if all the masks are -inf |
| 8494 |
if (mask) { |
| 8495 |
bool can_skip = true; |
| 8496 |
for (int tq = 0; tq < tile_rows; tq++) { |
| 8497 |
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]); |
| 8498 |
for (int tk = 0; tk < kv_tile; tk++) { |
| 8499 |
mask32[tq * KV_TILE_SZ + tk] = slope * GGML_CPU_FP16_TO_FP32(mp_row[ic + tk]); |
| 8500 |
if (mask32[tq * KV_TILE_SZ + tk] != -INFINITY) { |
| 8501 |
can_skip = false; |
| 8502 |
} |
| 8503 |
} |
| 8504 |
// Pad remaining mask entries with -inf |
| 8505 |
for (int tk = kv_tile; tk < KV_TILE_SZ; tk++) { |
| 8506 |
mask32[tq * KV_TILE_SZ + tk] = -INFINITY; |
| 8507 |
} |
| 8508 |
} |
| 8509 |
|
| 8510 |
if (can_skip) { |
| 8511 |
continue; |
| 8512 |
} |
| 8513 |
} |
| 8514 |
|
| 8515 |
// Pack K tile transposed: K_f32[dk][kv] so KV_TILE is contiguous (SIMD dim) |
| 8516 |
// Zero-pad the last tile so the GEMM always operates on KV_TILE_SZ columns |
| 8517 |
for (int tk = 0; tk < kv_tile; tk++) { |
| 8518 |
const char * k_data = (const char *)k->data + (ic + tk)*nbk1 + ik2*nbk2 + ik3*nbk3; |
| 8519 |
if (kv_type == GGML_TYPE_F16) { |
| 8520 |
const ggml_fp16_t * k_f16 = (const ggml_fp16_t *)k_data; |
| 8521 |
for (int64_t dk = 0; dk < DK; dk++) { |
| 8522 |
K_f32[dk * KV_TILE_SZ + tk] = GGML_CPU_FP16_TO_FP32(k_f16[dk]); |
| 8523 |
} |
| 8524 |
} else { |
| 8525 |
const float * k_f32_src = (const float *)k_data; |
| 8526 |
for (int64_t dk = 0; dk < DK; dk++) { |
| 8527 |
K_f32[dk * KV_TILE_SZ + tk] = k_f32_src[dk]; |
| 8528 |
} |
| 8529 |
} |
| 8530 |
} |
| 8531 |
memset(KQ, 0, Q_TILE_SZ * KV_TILE_SZ * sizeof(float)); |
| 8532 |
simd_gemm(KQ, (const float *)Q_q, K_f32, Q_TILE_SZ, DK, KV_TILE_SZ); |
| 8533 |
ggml_vec_scale_f32(Q_TILE_SZ * KV_TILE_SZ, KQ, scale); |
| 8534 |
|
| 8535 |
// Set padded KQ entries to -inf so softmax gives them zero weight |
| 8536 |
if (kv_tile < KV_TILE_SZ) { |
| 8537 |
for (int tq = 0; tq < Q_TILE_SZ; tq++) { |
| 8538 |
for (int tk = kv_tile; tk < KV_TILE_SZ; tk++) { |
| 8539 |
KQ[tq * KV_TILE_SZ + tk] = -INFINITY; |
| 8540 |
} |
| 8541 |
} |
| 8542 |
} |
| 8543 |
|
| 8544 |
if (logit_softcap != 0.0f) { |
| 8545 |
ggml_vec_tanh_f32(Q_TILE_SZ * KV_TILE_SZ, KQ, KQ); |
| 8546 |
ggml_vec_scale_f32(Q_TILE_SZ * KV_TILE_SZ, KQ, logit_softcap); |
| 8547 |
} |
| 8548 |
|
| 8549 |
if (mask) { |
| 8550 |
ggml_vec_add_f32(tile_rows * KV_TILE_SZ, KQ, KQ, mask32); |
| 8551 |
} |
| 8552 |
|
| 8553 |
bool skip[Q_TILE_SZ] = {}; |
| 8554 |
|
| 8555 |
for (int tq = 0; tq < Q_TILE_SZ; tq++) { |
| 8556 |
float * kq_row = KQ + tq * KV_TILE_SZ; |
| 8557 |
|
| 8558 |
float tile_max; |
| 8559 |
ggml_vec_max_f32(KV_TILE_SZ, &tile_max, kq_row); |
| 8560 |
|
| 8561 |
if (tile_max == -INFINITY) { |
| 8562 |
skip[tq] = true; |
| 8563 |
continue; |
| 8564 |
} |
| 8565 |
|
| 8566 |
const float Mold = M[tq]; |
| 8567 |
const float Mnew = fmaxf(Mold, tile_max); |
| 8568 |
|
| 8569 |
if (Mnew > Mold) { |
| 8570 |
const float ms = expf(Mold - Mnew); |
| 8571 |
ggml_vec_scale_f32(DV, VKQ32 + tq * DV, ms); |
| 8572 |
S[tq] *= ms; |
| 8573 |
} |
| 8574 |
M[tq] = Mnew; |
| 8575 |
|
| 8576 |
|
| 8577 |
S[tq] += ggml_vec_soft_max_f32(KV_TILE_SZ, kq_row, kq_row, Mnew); |
| 8578 |
} |
| 8579 |
|
| 8580 |
// V accumulation: VKQ32 += softmax(KQ) * V |
| 8581 |
// Pack V tile to contiguous F32, zero-padded |
| 8582 |
for (int tk = 0; tk < kv_tile; tk++) { |
| 8583 |
const char * v_data = (const char *)v->data + (ic + tk)*nbv1 + iv2*nbv2 + iv3*nbv3; |
| 8584 |
if (kv_type == GGML_TYPE_F16) { |
| 8585 |
ggml_fp16_to_fp32_row((const ggml_fp16_t *)v_data, V32 + tk * DV, DV); |
| 8586 |
} else { |
| 8587 |
memcpy(V32 + tk * DV, v_data, DV * sizeof(float)); |
| 8588 |
} |
| 8589 |
} |
| 8590 |
for (int tq = 0; tq < Q_TILE_SZ; tq++) { |
| 8591 |
if (skip[tq]) { |
| 8592 |
memset(KQ + tq * KV_TILE_SZ, 0, KV_TILE_SZ * sizeof(float)); |
| 8593 |
} |
| 8594 |
} |
| 8595 |
simd_gemm(VKQ32, KQ, V32, Q_TILE_SZ, KV_TILE_SZ, DV); |
| 8596 |
} |
| 8597 |
|
| 8598 |
// sinks (apply only to valid rows in the tile) |
| 8599 |
if (sinks) { |
| 8600 |
const float s = ((float *)((char *) sinks->data))[h]; |
| 8601 |
|
| 8602 |
for (int tq = 0; tq < tile_rows; tq++) { |
| 8603 |
float ms = 1.0f; |
| 8604 |
float vs = 1.0f; |
| 8605 |
|
| 8606 |
if (s > M[tq]) { |
| 8607 |
ms = expf(M[tq] - s); |
| 8608 |
ggml_vec_scale_f32(DV, VKQ32 + tq * DV, ms); |
| 8609 |
} else { |
| 8610 |
vs = expf(s - M[tq]); |
| 8611 |
} |
| 8612 |
|
| 8613 |
S[tq] = S[tq] * ms + vs; |
| 8614 |
} |
| 8615 |
} |
| 8616 |
|
| 8617 |
for (int tq = 0; tq < tile_rows; tq++) { |
| 8618 |
// V /= S |
| 8619 |
const float S_inv = S[tq] == 0.0f ? 0.0f : 1.0f / S[tq]; |
| 8620 |
ggml_vec_scale_f32(DV, VKQ32 + tq * DV, S_inv); |
| 8621 |
|
| 8622 |
// dst indices |
| 8623 |
const int i1 = iq1 + tq; |
| 8624 |
const int i2 = iq2; |
| 8625 |
const int i3 = iq3; |
| 8626 |
|
| 8627 |
// permute(0, 2, 1, 3) |
| 8628 |
memcpy((char *) dst->data + (i3*ne2*ne1 + i2 + i1*ne1)*nb1, VKQ32 + tq * DV, nb1); |
| 8629 |
} |
| 8630 |
|
| 8631 |
ir += tile_rows; |
| 8632 |
} |
| 8633 |
} |
| 8634 |
|
| 8635 |
// Reduction function: combines partial results across KV chunks |
| 8636 |
// Partials layout in wdata: [n_q_heads][n_chunks][2 + DV] |
| 8637 |
static void ggml_flash_attn_ext_reduce_partials( |
| 8638 |
const ggml_compute_params * params, |
| 8639 |
ggml_tensor * dst, |
| 8640 |
const int64_t n_chunks, |
| 8641 |
const int64_t chunk_size) { |
| 8642 |
|
| 8643 |
const ggml_tensor * q = dst->src[0]; |
| 8644 |
const ggml_tensor * k = dst->src[1]; |
| 8645 |
const ggml_tensor * v = dst->src[2]; |
| 8646 |
|
| 8647 |
const int64_t DK = k->ne[0]; |
| 8648 |
const int64_t DV = v->ne[0]; |
| 8649 |
const int64_t nek1 = k->ne[1]; |
| 8650 |
const int64_t n_q_heads = q->ne[2]; |
| 8651 |
|
| 8652 |
const int ith = params->ith; |
| 8653 |
const int nth = params->nth; |
| 8654 |
|
| 8655 |
const int64_t wdata_per_thread = DK + 2*DV + CACHE_LINE_SIZE_F32; |
| 8656 |
float * thread_wdata = (float *) params->wdata + ith * wdata_per_thread; |
| 8657 |
|
| 8658 |
const int64_t partials_offset = nth * (DK + 2*DV + CACHE_LINE_SIZE_F32); |
| 8659 |
const int64_t partial_size = 2 + DV; |
| 8660 |
const float * partials_base = (const float *) params->wdata + partials_offset; |
| 8661 |
|
| 8662 |
// Output layout |
| 8663 |
const int64_t ne1 = dst->ne[1]; |
| 8664 |
const int64_t ne2 = dst->ne[2]; |
| 8665 |
const size_t nb1 = dst->nb[1]; |
| 8666 |
|
| 8667 |
// Each thread reduces a subset of query heads |
| 8668 |
for (int64_t q_head = ith; q_head < n_q_heads; q_head += nth) { |
| 8669 |
float M_final = -INFINITY; |
| 8670 |
float S_final = 0.0f; |
| 8671 |
float * VKQ_final = thread_wdata; |
| 8672 |
memset(VKQ_final, 0, DV * sizeof(float)); |
| 8673 |
|
| 8674 |
// Combine partials from all chunks |
| 8675 |
for (int64_t chunk_idx = 0; chunk_idx < n_chunks; ++chunk_idx) { |
| 8676 |
const int64_t ic_start = chunk_idx * chunk_size; |
| 8677 |
if (ic_start >= nek1) continue; |
| 8678 |
|
| 8679 |
const float * partial = partials_base + (q_head * n_chunks + chunk_idx) * partial_size; |
| 8680 |
const float M_chunk = partial[0]; |
| 8681 |
const float S_chunk = partial[1]; |
| 8682 |
const float * VKQ_chunk = partial + 2; |
| 8683 |
|
| 8684 |
if (S_chunk == 0.0f) continue; |
| 8685 |
|
| 8686 |
const float M_new = fmaxf(M_final, M_chunk); |
| 8687 |
const float scale_old = expf(M_final - M_new); |
| 8688 |
const float scale_new = expf(M_chunk - M_new); |
| 8689 |
|
| 8690 |
for (int64_t d = 0; d < DV; ++d) { |
| 8691 |
VKQ_final[d] = VKQ_final[d] * scale_old + VKQ_chunk[d] * scale_new; |
| 8692 |
} |
| 8693 |
S_final = S_final * scale_old + S_chunk * scale_new; |
| 8694 |
M_final = M_new; |
| 8695 |
} |
| 8696 |
|
| 8697 |
// Normalize and write to output |
| 8698 |
if (S_final != 0.0f) { |
| 8699 |
const float S_inv = 1.0f / S_final; |
| 8700 |
ggml_vec_scale_f32(DV, VKQ_final, S_inv); |
| 8701 |
} |
| 8702 |
// iq1=0, iq3=0 for decode |
| 8703 |
memcpy((char *) dst->data + (0*ne2*ne1 + q_head + 0*ne1)*nb1, VKQ_final, nb1); |
| 8704 |
} |
| 8705 |
} |
| 8706 |
|
| 8707 |
static void ggml_compute_forward_flash_attn_ext_f16( |
| 8708 |
const ggml_compute_params * params, |
| 8709 |
ggml_tensor * dst) { |
| 8710 |
|
| 8711 |
const ggml_tensor * q = dst->src[0]; |
| 8712 |
const ggml_tensor * k = dst->src[1]; |
| 8713 |
const ggml_tensor * v = dst->src[2]; |
| 8714 |
|
| 8715 |
GGML_TENSOR_LOCALS(int64_t, neq, q, ne) |
| 8716 |
GGML_TENSOR_LOCALS(size_t, nbq, q, nb) |
| 8717 |
GGML_TENSOR_LOCALS(int64_t, nek, k, ne) |
| 8718 |
GGML_TENSOR_LOCALS(size_t, nbk, k, nb) |
| 8719 |
GGML_TENSOR_LOCALS(int64_t, nev, v, ne) |
| 8720 |
GGML_TENSOR_LOCALS(size_t, nbv, v, nb) |
| 8721 |
GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 8722 |
GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 8723 |
|
| 8724 |
const int64_t DK = nek0; |
| 8725 |
const int64_t DV = nev0; |
| 8726 |
const int64_t N = neq1; |
| 8727 |
|
| 8728 |
|
| 8729 |
GGML_ASSERT(ne0 == DV); |
| 8730 |
GGML_ASSERT(ne2 == N); |
| 8731 |
|
| 8732 |
// input tensor rows must be contiguous |
| 8733 |
GGML_ASSERT(nbq0 == ggml_type_size(q->type)); |
| 8734 |
GGML_ASSERT(nbk0 == ggml_type_size(k->type)); |
| 8735 |
GGML_ASSERT(nbv0 == ggml_type_size(v->type)); |
| 8736 |
|
| 8737 |
GGML_ASSERT(neq0 == DK); |
| 8738 |
GGML_ASSERT(nek0 == DK); |
| 8739 |
GGML_ASSERT(nev0 == DV); |
| 8740 |
|
| 8741 |
GGML_ASSERT(neq1 == N); |
| 8742 |
|
| 8743 |
// dst cannot be transposed or permuted |
| 8744 |
GGML_ASSERT(nb0 == sizeof(float)); |
| 8745 |
GGML_ASSERT(nb0 <= nb1); |
| 8746 |
GGML_ASSERT(nb1 <= nb2); |
| 8747 |
GGML_ASSERT(nb2 <= nb3); |
| 8748 |
|
| 8749 |
const int ith = params->ith; |
| 8750 |
const int nth = params->nth; |
| 8751 |
|
| 8752 |
// When use_ref is set, force the vec-only reference implementation (no tiling, no KV-chunking) |
| 8753 |
const bool use_ref = params->use_ref; |
| 8754 |
|
| 8755 |
const bool kv_is_f32_or_f16 = (k->type == GGML_TYPE_F32 || k->type == GGML_TYPE_F16); |
| 8756 |
const bool use_split_kv_path = !use_ref && (neq1 == 1 && neq3 == 1) && kv_is_f32_or_f16 && (k->type == v->type) && q->type == GGML_TYPE_F32 && nek1 >= 512; |
| 8757 |
|
| 8758 |
if (use_split_kv_path) { |
| 8759 |
const int64_t chunk_size = (nek1 + nth - 1) / nth; |
| 8760 |
|
| 8761 |
// Partials buffer layout: [q_head][kv_chunk][M, S, VKQ] |
| 8762 |
const int64_t partial_size = 2 + DV; |
| 8763 |
float * partials_base = (float *) params->wdata + nth * (DK + 2*DV + CACHE_LINE_SIZE_F32); |
| 8764 |
|
| 8765 |
const int64_t ic_start = ith * chunk_size; |
| 8766 |
const int64_t ic_end = std::min(ic_start + chunk_size, nek1); |
| 8767 |
|
| 8768 |
const int64_t partial_stride = nth * partial_size; |
| 8769 |
float * chunk_partials = partials_base + ith * partial_size; |
| 8770 |
|
| 8771 |
if (ic_start < nek1) { |
| 8772 |
for (int64_t q_head = 0; q_head < neq2; q_head++) { |
| 8773 |
ggml_compute_forward_flash_attn_ext_f16_one_chunk( |
| 8774 |
params, dst, q_head, q_head + 1, ic_start, ic_end, |
| 8775 |
chunk_partials, partial_stride); |
| 8776 |
} |
| 8777 |
} else { |
| 8778 |
for (int64_t q_head = 0; q_head < neq2; q_head++) { |
| 8779 |
float * q_partials = chunk_partials + q_head * partial_stride; |
| 8780 |
q_partials[0] = -INFINITY; // M |
| 8781 |
q_partials[1] = 0.0f; // S |
| 8782 |
} |
| 8783 |
} |
| 8784 |
|
| 8785 |
ggml_barrier(params->threadpool); |
| 8786 |
ggml_flash_attn_ext_reduce_partials(params, dst, nth, chunk_size); |
| 8787 |
} else { |
| 8788 |
|
| 8789 |
// total rows in q |
| 8790 |
const int64_t nr = neq1*neq2*neq3; |
| 8791 |
|
| 8792 |
// disable for NUMA |
| 8793 |
const bool disable_chunking = ggml_is_numa(); |
| 8794 |
|
| 8795 |
// 4x chunks per thread |
| 8796 |
int nth_scaled = nth * 4; |
| 8797 |
int64_t chunk_size = (nr + nth_scaled - 1) / nth_scaled; |
| 8798 |
int64_t nchunk = (nr + chunk_size - 1) / chunk_size; |
| 8799 |
|
| 8800 |
if (nth == 1 || nchunk < nth || disable_chunking) { |
| 8801 |
nchunk = nth; |
| 8802 |
} |
| 8803 |
|
| 8804 |
if (ith == 0) { |
| 8805 |
ggml_threadpool_chunk_set(params->threadpool, nth); |
| 8806 |
} |
| 8807 |
|
| 8808 |
ggml_barrier(params->threadpool); |
| 8809 |
|
| 8810 |
const int64_t dr = (nr + nchunk - 1) / nchunk; |
| 8811 |
|
| 8812 |
static constexpr int64_t Q_TILE_SZ = ggml_fa_tile_config::Q; |
| 8813 |
bool use_tiled = !use_ref && |
| 8814 |
(q->type == GGML_TYPE_F32 && |
| 8815 |
kv_is_f32_or_f16 && |
| 8816 |
k->type == v->type && |
| 8817 |
neq1 >= Q_TILE_SZ); |
| 8818 |
#ifdef GGML_SIMD |
| 8819 |
use_tiled &= (DV % GGML_F32_EPR == 0); |
| 8820 |
#endif |
| 8821 |
int current_chunk = ith; |
| 8822 |
|
| 8823 |
while (current_chunk < nchunk) { |
| 8824 |
const int64_t ir0 = dr * current_chunk; |
| 8825 |
const int64_t ir1 = MIN(ir0 + dr, nr); |
| 8826 |
|
| 8827 |
if (use_tiled) { |
| 8828 |
ggml_compute_forward_flash_attn_ext_tiled(params, dst, ir0, ir1); |
| 8829 |
} else { |
| 8830 |
ggml_compute_forward_flash_attn_ext_f16_one_chunk(params, dst, ir0, ir1, 0, nek1, nullptr, 0); |
| 8831 |
} |
| 8832 |
|
| 8833 |
current_chunk = ggml_threadpool_chunk_add(params->threadpool, 1); |
| 8834 |
} |
| 8835 |
} |
| 8836 |
} |
| 8837 |
|
| 8838 |
void ggml_compute_forward_flash_attn_ext( |
| 8839 |
const ggml_compute_params * params, |
| 8840 |
ggml_tensor * dst) { |
| 8841 |
switch (dst->op_params[3]) { |
| 8842 |
case GGML_PREC_DEFAULT: |
| 8843 |
case GGML_PREC_F32: |
| 8844 |
{ |
| 8845 |
// uses F32 accumulators |
| 8846 |
ggml_compute_forward_flash_attn_ext_f16(params, dst); |
| 8847 |
} break; |
| 8848 |
default: |
| 8849 |
{ |
| 8850 |
GGML_ABORT("fatal error"); |
| 8851 |
} |
| 8852 |
} |
| 8853 |
} |
| 8854 |
|
| 8855 |
// ggml_compute_forward_flash_attn_back |
| 8856 |
|
| 8857 |
static void ggml_compute_forward_flash_attn_back_f32( |
| 8858 |
const ggml_compute_params * params, |
| 8859 |
const bool masked, |
| 8860 |
ggml_tensor * dst) { |
| 8861 |
|
| 8862 |
const ggml_tensor * q = dst->src[0]; |
| 8863 |
const ggml_tensor * k = dst->src[1]; |
| 8864 |
const ggml_tensor * v = dst->src[2]; |
| 8865 |
const ggml_tensor * d = dst->src[3]; |
| 8866 |
|
| 8867 |
GGML_TENSOR_LOCALS(int64_t, neq, q, ne) |
| 8868 |
GGML_TENSOR_LOCALS(size_t, nbq, q, nb) |
| 8869 |
GGML_TENSOR_LOCALS(int64_t, nek, k, ne) |
| 8870 |
GGML_TENSOR_LOCALS(size_t, nbk, k, nb) |
| 8871 |
GGML_TENSOR_LOCALS(int64_t, nev, v, ne) |
| 8872 |
GGML_TENSOR_LOCALS(size_t, nbv, v, nb) |
| 8873 |
GGML_TENSOR_LOCALS(int64_t, ned, d, ne) |
| 8874 |
GGML_TENSOR_LOCALS(size_t, nbd, d, nb) |
| 8875 |
GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 8876 |
GGML_TENSOR_LOCALS(size_t, nb, dst, nb) |
| 8877 |
|
| 8878 |
const int ith = params->ith; |
| 8879 |
const int nth = params->nth; |
| 8880 |
|
| 8881 |
const int64_t D = neq0; |
| 8882 |
const int64_t N = neq1; |
| 8883 |
const int64_t P = nek1 - N; |
| 8884 |
const int64_t M = P + N; |
| 8885 |
|
| 8886 |
const int Mup = ggml_up(M, GGML_SOFT_MAX_UNROLL); |
| 8887 |
const int mxDM = MAX(D, Mup); |
| 8888 |
|
| 8889 |
// GGML_ASSERT(ne0 == D); |
| 8890 |
// GGML_ASSERT(ne1 == N); |
| 8891 |
GGML_ASSERT(P >= 0); |
| 8892 |
|
| 8893 |
GGML_ASSERT(nbq0 == sizeof(float)); |
| 8894 |
GGML_ASSERT(nbk0 == sizeof(float)); |
| 8895 |
GGML_ASSERT(nbv0 == sizeof(float)); |
| 8896 |
|
| 8897 |
GGML_ASSERT(neq0 == D); |
| 8898 |
GGML_ASSERT(nek0 == D); |
| 8899 |
GGML_ASSERT(nev1 == D); |
| 8900 |
GGML_ASSERT(ned0 == D); |
| 8901 |
|
| 8902 |
GGML_ASSERT(neq1 == N); |
| 8903 |
GGML_ASSERT(nek1 == N + P); |
| 8904 |
GGML_ASSERT(nev1 == D); |
| 8905 |
GGML_ASSERT(ned1 == N); |
| 8906 |
|
| 8907 |
// dst cannot be transposed or permuted |
| 8908 |
GGML_ASSERT(nb0 == sizeof(float)); |
| 8909 |
GGML_ASSERT(nb0 <= nb1); |
| 8910 |
GGML_ASSERT(nb1 <= nb2); |
| 8911 |
GGML_ASSERT(nb2 <= nb3); |
| 8912 |
|
| 8913 |
if (ith == 0) { |
| 8914 |
memset(dst->data, 0, nb0*ne0*ne1*ne2*ne3); |
| 8915 |
} |
| 8916 |
ggml_barrier(params->threadpool); |
| 8917 |
|
| 8918 |
const int64_t elem_q = ggml_nelements(q); |
| 8919 |
const int64_t elem_k = ggml_nelements(k); |
| 8920 |
|
| 8921 |
ggml_type result_type = dst->type; |
| 8922 |
GGML_ASSERT(ggml_blck_size(result_type) == 1); |
| 8923 |
const size_t tsize = ggml_type_size(result_type); |
| 8924 |
|
| 8925 |
const size_t offs_q = 0; |
| 8926 |
const size_t offs_k = offs_q + GGML_PAD(elem_q * tsize, GGML_MEM_ALIGN); |
| 8927 |
const size_t offs_v = offs_k + GGML_PAD(elem_k * tsize, GGML_MEM_ALIGN); |
| 8928 |
|
| 8929 |
void * grad_q = (char *) dst->data; |
| 8930 |
void * grad_k = (char *) dst->data + offs_k; |
| 8931 |
void * grad_v = (char *) dst->data + offs_v; |
| 8932 |
|
| 8933 |
const size_t nbgq1 = nb0*neq0; |
| 8934 |
const size_t nbgq2 = nb0*neq0*neq1; |
| 8935 |
const size_t nbgq3 = nb0*neq0*neq1*neq2; |
| 8936 |
|
| 8937 |
const size_t nbgk1 = nb0*nek0; |
| 8938 |
const size_t nbgk2 = nb0*nek0*nek1; |
| 8939 |
const size_t nbgk3 = nb0*nek0*nek1*neq2; |
| 8940 |
|
| 8941 |
const size_t nbgv1 = nb0*nev0; |
| 8942 |
const size_t nbgv2 = nb0*nev0*nev1; |
| 8943 |
const size_t nbgv3 = nb0*nev0*nev1*neq2; |
| 8944 |
|
| 8945 |
// parallelize by k rows using ggml_vec_dot_f32 |
| 8946 |
|
| 8947 |
// total rows in k |
| 8948 |
const int nr = nek2*nek3; |
| 8949 |
|
| 8950 |
// rows per thread |
| 8951 |
const int dr = (nr + nth - 1)/nth; |
| 8952 |
|
| 8953 |
// row range for this thread |
| 8954 |
const int ir0 = dr*ith; |
| 8955 |
const int ir1 = MIN(ir0 + dr, nr); |
| 8956 |
|
| 8957 |
const float scale = 1.0f/sqrtf(D); |
| 8958 |
|
| 8959 |
//printf("P=%d N=%d D=%d ir0=%d ir1=%d scale = %f\n", P, N, D, ir0, ir1, scale); |
| 8960 |
|
| 8961 |
// how often k2 (and v2) is repeated in q2 |
| 8962 |
int nrep = neq2/nek2; |
| 8963 |
|
| 8964 |
for (int ir = ir0; ir < ir1; ++ir) { |
| 8965 |
// q indices |
| 8966 |
const int ik3 = ir/(nek2); |
| 8967 |
const int ik2 = ir - ik3*nek2; |
| 8968 |
|
| 8969 |
const int iq3 = ik3; |
| 8970 |
const int id3 = ik3; |
| 8971 |
const int iv3 = ik3; |
| 8972 |
const int iv2 = ik2; |
| 8973 |
|
| 8974 |
for (int irep = 0; irep < nrep; ++irep) { |
| 8975 |
const int iq2 = ik2 + irep*nek2; |
| 8976 |
const int id2 = iq2; |
| 8977 |
|
| 8978 |
// (ik2 + irep*nek2) % nek2 == ik2 |
| 8979 |
for (int iq1 = 0; iq1 < neq1; ++iq1) { |
| 8980 |
const int id1 = iq1; |
| 8981 |
|
| 8982 |
// not sure about CACHE_LINE_SIZE_F32.. |
| 8983 |
// - maybe it must not be multiplied by 2 and excluded from .. in SM 1*(..) offset? |
| 8984 |
float * S = (float *) params->wdata + ith*2*(mxDM + CACHE_LINE_SIZE_F32) + 0*(mxDM+CACHE_LINE_SIZE_F32); |
| 8985 |
float * SM = (float *) params->wdata + ith*2*(mxDM + CACHE_LINE_SIZE_F32) + 1*(mxDM+CACHE_LINE_SIZE_F32); |
| 8986 |
|
| 8987 |
for (int i = M; i < Mup; ++i) { |
| 8988 |
S[i] = -INFINITY; |
| 8989 |
} |
| 8990 |
|
| 8991 |
const int64_t masked_begin = masked ? (P + iq1 + 1) : M; |
| 8992 |
for (int64_t ic = 0; ic < masked_begin; ++ic) { |
| 8993 |
// k indices |
| 8994 |
const int ik1 = ic; |
| 8995 |
|
| 8996 |
// S indices |
| 8997 |
const int i1 = ik1; |
| 8998 |
|
| 8999 |
ggml_vec_dot_f32(neq0, |
| 9000 |
S + i1, 0, |
| 9001 |
(float *) ((char *) k->data + (ik1*nbk1 + ik2*nbk2 + ik3*nbk3)), 0, |
| 9002 |
(float *) ((char *) q->data + (iq1*nbq1 + iq2*nbq2 + iq3*nbq3)), 0, 1); |
| 9003 |
} |
| 9004 |
|
| 9005 |
// scale |
| 9006 |
ggml_vec_scale_f32(masked_begin, S, scale); |
| 9007 |
|
| 9008 |
for (int64_t i = masked_begin; i < M; i++) { |
| 9009 |
S[i] = -INFINITY; |
| 9010 |
} |
| 9011 |
|
| 9012 |
// softmax |
| 9013 |
// exclude known -INF S[..] values from max and loop |
| 9014 |
// dont forget to set their SM values to zero |
| 9015 |
{ |
| 9016 |
float max = -INFINITY; |
| 9017 |
ggml_vec_max_f32(masked_begin, &max, S); |
| 9018 |
|
| 9019 |
ggml_float sum = 0.0; |
| 9020 |
{ |
| 9021 |
#ifdef GGML_SOFT_MAX_ACCELERATE |
| 9022 |
max = -max; |
| 9023 |
vDSP_vsadd(SM, 1, &max, SM, 1, Mup); |
| 9024 |
vvexpf(SM, SM, &Mup); |
| 9025 |
ggml_vec_sum_f32(Mup, &sum, SM); |
| 9026 |
#else |
| 9027 |
sum = ggml_vec_soft_max_f32(Mup, SM, S, max); |
| 9028 |
#endif |
| 9029 |
} |
| 9030 |
|
| 9031 |
assert(sum > 0.0); |
| 9032 |
|
| 9033 |
sum = 1.0/sum; |
| 9034 |
ggml_vec_scale_f32(masked_begin, SM, sum); |
| 9035 |
|
| 9036 |
} |
| 9037 |
|
| 9038 |
// step-by-step explanation |
| 9039 |
{ |
| 9040 |
// forward-process shape grads from backward process |
| 9041 |
// parallel_for ik2,ik3: |
| 9042 |
// for irep: |
| 9043 |
// iq2 = ik2 + irep*nek2 |
| 9044 |
// k[:D,:M,:,:] [D,M,:,:] grad[k][:D,:M,ik2,ik3] += grad[kcur] |
| 9045 |
// q[:D,:N,:,:] [D,N,:,:] grad[q][:D,iq1,iq2,iq3] += grad[qcur] |
| 9046 |
// v[:M,:D,:,:] [M,D,:,:] grad[v][:M,:D,iv2,iv3] += grad[vcur] |
| 9047 |
// for iq1: |
| 9048 |
// kcur = k[:D,:M,ik2,ik3] [D,M,1,1] grad[kcur] = grad[S1].T @ qcur |
| 9049 |
// qcur = q[:D,iq1,iq2,iq3] [D,1,1,1] grad[qcur] = grad[S1] @ kcur |
| 9050 |
// vcur = v[:M,:D,iv2,iv3] [M,D,1,1] grad[vcur] = grad[S5].T @ S4 |
| 9051 |
// S0 = -Inf [D,1,1,1] |
| 9052 |
// ~S1[i] = dot(kcur[:D,i], qcur) |
| 9053 |
// S1 = qcur @ kcur.T [M,1,1,1] grad[S1] = grad[S2] * scale |
| 9054 |
// S2 = S1 * scale [M,1,1,1] grad[S2] = diag_mask_zero(grad[S3], P) |
| 9055 |
// S3 = diag_mask_inf(S2, P) [M,1,1,1] grad[S3] = S4 * (grad[S4] - dot(S4, grad[S4])) |
| 9056 |
// S4 = softmax(S3) [M,1,1,1] grad[S4] = grad[S5] @ vcur |
| 9057 |
// ~S5[i] = dot(vcur[:,i], S4) |
| 9058 |
// S5 = S4 @ vcur.T [D,1,1,1] grad[S5] = d[:D,id1,id2,id3] |
| 9059 |
// ~dst[i,iq1,iq2,iq3] = S5[i] ^ |
| 9060 |
// dst[:D,iq1,iq2,iq3] = S5 | grad[dst[:D,iq1,iq2,iq3]] = d[:D,id1,id2,id3] |
| 9061 |
// dst backward-/ grad[dst] = d |
| 9062 |
// |
| 9063 |
// output gradients with their dependencies: |
| 9064 |
// |
| 9065 |
// grad[kcur] = grad[S1].T @ qcur |
| 9066 |
// grad[S1] = diag_mask_zero(grad[S3], P) * scale |
| 9067 |
// grad[S3] = S4 * (grad[S4] - dot(S4, grad[S4])) |
| 9068 |
// grad[S4] = grad[S5] @ vcur |
| 9069 |
// grad[S4] = d[:D,id1,id2,id3] @ vcur |
| 9070 |
// grad[qcur] = grad[S1] @ kcur |
| 9071 |
// grad[vcur] = grad[S5].T @ S4 |
| 9072 |
// grad[vcur] = d[:D,id1,id2,id3].T @ S4 |
| 9073 |
// |
| 9074 |
// in post-order: |
| 9075 |
// |
| 9076 |
// S1 = qcur @ kcur.T |
| 9077 |
// S2 = S1 * scale |
| 9078 |
// S3 = diag_mask_inf(S2, P) |
| 9079 |
// S4 = softmax(S3) |
| 9080 |
// grad[S4] = d[:D,id1,id2,id3] @ vcur |
| 9081 |
// grad[S3] = S4 * (grad[S4] - dot(S4, grad[S4])) |
| 9082 |
// grad[S1] = diag_mask_zero(grad[S3], P) * scale |
| 9083 |
// grad[qcur] = grad[S1] @ kcur |
| 9084 |
// grad[kcur] = grad[S1].T @ qcur |
| 9085 |
// grad[vcur] = d[:D,id1,id2,id3].T @ S4 |
| 9086 |
// |
| 9087 |
// using less variables (SM=S4): |
| 9088 |
// |
| 9089 |
// S = diag_mask_inf(qcur @ kcur.T * scale, P) |
| 9090 |
// SM = softmax(S) |
| 9091 |
// S = d[:D,iq1,iq2,iq3] @ vcur |
| 9092 |
// dot_SM_gradSM = dot(SM, S) |
| 9093 |
// S = SM * (S - dot(SM, S)) |
| 9094 |
// S = diag_mask_zero(S, P) * scale |
| 9095 |
// |
| 9096 |
// grad[q][:D,iq1,iq2,iq3] += S @ kcur |
| 9097 |
// grad[k][:D,:M,ik2,ik3] += S.T @ qcur |
| 9098 |
// grad[v][:M,:D,iv2,iv3] += d[:D,id1,id2,id3].T @ SM |
| 9099 |
} |
| 9100 |
|
| 9101 |
// S = gradSM = d[:D,id1,id2,id3] @ vcur[:,:,iv2,iv3] |
| 9102 |
// S = d[:D,id1,id2,id3] @ vcur[:,:,iv2,iv3] |
| 9103 |
// for ic: |
| 9104 |
// S[:M] += vcur[:M,ic,iv2,iv3] * d[ic,id1,id2,id3] |
| 9105 |
// exclude known future zero S[..] values from operation |
| 9106 |
ggml_vec_set_f32(masked_begin, S, 0); |
| 9107 |
for (int64_t ic = 0; ic < D; ++ic) { |
| 9108 |
ggml_vec_mad_f32(masked_begin, |
| 9109 |
S, |
| 9110 |
(float *) ((char *) v->data + ( ic*nbv1 + iv2*nbv2 + iv3*nbv3)), |
| 9111 |
*(float *) ((char *) d->data + (ic*nbd0 + id1*nbd1 + id2*nbd2 + id3*nbd3))); |
| 9112 |
} |
| 9113 |
|
| 9114 |
// S = SM * (S - dot(SM, S)) |
| 9115 |
float dot_SM_gradSM = 0; |
| 9116 |
ggml_vec_dot_f32 (masked_begin, &dot_SM_gradSM, 0, SM, 0, S, 0, 1); |
| 9117 |
ggml_vec_acc1_f32(M, S, -dot_SM_gradSM); |
| 9118 |
ggml_vec_mul_f32 (masked_begin, S, S, SM); |
| 9119 |
|
| 9120 |
// S = diag_mask_zero(S, P) * scale |
| 9121 |
// already done by above ggml_vec_set_f32 |
| 9122 |
|
| 9123 |
// exclude known zero S[..] values from operation |
| 9124 |
ggml_vec_scale_f32(masked_begin, S, scale); |
| 9125 |
|
| 9126 |
// S shape [M,1] |
| 9127 |
// SM shape [M,1] |
| 9128 |
// kcur shape [D,M] |
| 9129 |
// qcur shape [D,1] |
| 9130 |
// vcur shape [M,D] |
| 9131 |
|
| 9132 |
// grad[q][:D,iq1,iq2,iq3] += S @ kcur |
| 9133 |
// grad[q][:D,iq1,iq2,iq3] += shape[M,1] @ shape[D,M] |
| 9134 |
// for ic: |
| 9135 |
// grad[q][:D,iq1,iq2,iq3] += S[ic] * kcur[:D,ic,ik2,ik3] |
| 9136 |
// exclude known zero S[..] values from loop |
| 9137 |
for (int64_t ic = 0; ic < masked_begin; ++ic) { |
| 9138 |
ggml_vec_mad_f32(D, |
| 9139 |
(float *) ((char *) grad_q + (iq1*nbgq1 + iq2*nbgq2 + iq3*nbgq3)), |
| 9140 |
(float *) ((char *) k->data + (ic*nbk1 + ik2*nbk2 + ik3*nbk3)), |
| 9141 |
S[ic]); |
| 9142 |
} |
| 9143 |
|
| 9144 |
// grad[k][:D,:M,iq2,iq3] += S.T @ qcur |
| 9145 |
// for ic: |
| 9146 |
// grad[k][:D,ic,iq2,iq3] += S.T[0,ic] * qcur[:D,0] |
| 9147 |
// grad[k][:D,ic,iq2,iq3] += S[ic] * qcur[:D,0] |
| 9148 |
// exclude known zero S[..] values from loop |
| 9149 |
for (int64_t ic = 0; ic < masked_begin; ++ic) { |
| 9150 |
ggml_vec_mad_f32(D, |
| 9151 |
(float *) ((char *) grad_k + (ic*nbgk1 + ik2*nbgk2 + ik3*nbgk3)), |
| 9152 |
(float *) ((char *) q->data + (iq1*nbq1 + iq2*nbq2 + iq3*nbq3)), |
| 9153 |
S[ic]); |
| 9154 |
} |
| 9155 |
|
| 9156 |
// grad[v][:M,:D,iv2,iv3] += d[:D,id1,id2,id3].T @ SM |
| 9157 |
// for ic: |
| 9158 |
// grad[v][:M,ic,iv2,iv3] += d[:D,id1,id2,id3].T[0,ic] * SM[:M] |
| 9159 |
// grad[v][:M,ic,iv2,iv3] += d[ic,id1,id2,id3] * SM[:M] |
| 9160 |
// exclude known zero SM[..] values from mad |
| 9161 |
for (int64_t ic = 0; ic < D; ++ic) { |
| 9162 |
ggml_vec_mad_f32(masked_begin, |
| 9163 |
(float *) ((char *) grad_v + ( ic*nbgv1 + iv2*nbgv2 + iv3*nbgv3)), |
| 9164 |
SM, |
| 9165 |
*(float *) ((char *) d->data + (ic*nbd0 + id1*nbd1 + id2*nbd2 + id3*nbd3))); |
| 9166 |
} |
| 9167 |
} |
| 9168 |
} |
| 9169 |
} |
| 9170 |
} |
| 9171 |
|
| 9172 |
void ggml_compute_forward_flash_attn_back( |
| 9173 |
const ggml_compute_params * params, |
| 9174 |
const bool masked, |
| 9175 |
ggml_tensor * dst) { |
| 9176 |
|
| 9177 |
const ggml_tensor * q = dst->src[0]; |
| 9178 |
|
| 9179 |
switch (q->type) { |
| 9180 |
case GGML_TYPE_F32: |
| 9181 |
{ |
| 9182 |
ggml_compute_forward_flash_attn_back_f32(params, masked, dst); |
| 9183 |
} break; |
| 9184 |
default: |
| 9185 |
{ |
| 9186 |
GGML_ABORT("fatal error"); |
| 9187 |
} |
| 9188 |
} |
| 9189 |
} |
| 9190 |
|
| 9191 |
// ggml_compute_forward_ssm_conv |
| 9192 |
|
| 9193 |
static void ggml_compute_forward_ssm_conv_f32( |
| 9194 |
const ggml_compute_params * params, |
| 9195 |
ggml_tensor * dst) { |
| 9196 |
const ggml_tensor * src0 = dst->src[0]; // conv_x |
| 9197 |
const ggml_tensor * src1 = dst->src[1]; // conv1d.weight |
| 9198 |
|
| 9199 |
const int ith = params->ith; |
| 9200 |
const int nth = params->nth; |
| 9201 |
|
| 9202 |
const int nc = src1->ne[0]; // d_conv |
| 9203 |
const int ncs = src0->ne[0]; // d_conv - 1 + n_t |
| 9204 |
const int nr = src0->ne[1]; // d_inner |
| 9205 |
const int n_t = dst->ne[1]; // tokens per sequence |
| 9206 |
const int n_s = dst->ne[2]; // number of sequences in the batch |
| 9207 |
|
| 9208 |
GGML_ASSERT( dst->ne[0] == nr); |
| 9209 |
GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 9210 |
GGML_ASSERT(src1->nb[0] == sizeof(float)); |
| 9211 |
GGML_ASSERT(src0->nb[1] == src0->ne[0]*sizeof(float)); |
| 9212 |
|
| 9213 |
// rows per thread |
| 9214 |
const int dr = (nr + nth - 1)/nth; |
| 9215 |
|
| 9216 |
// row range for this thread |
| 9217 |
const int ir0 = dr*ith; |
| 9218 |
const int ir1 = MIN(ir0 + dr, nr); |
| 9219 |
const int ir = ir1 - ir0; |
| 9220 |
|
| 9221 |
for (int i3 = 0; i3 < n_s; ++i3) { |
| 9222 |
for (int i2 = 0; i2 < n_t; ++i2) { |
| 9223 |
// {d_conv - 1 + n_t, d_inner, n_seqs} |
| 9224 |
// sliding window |
| 9225 |
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} |
| 9226 |
const float * c = (const float *) ((const char *) src1->data + ir0*(src1->nb[1])); // {d_conv, d_inner} |
| 9227 |
float * x = (float *) ((char *) dst->data + ir0*(dst->nb[0]) + i2*(dst->nb[1]) + i3*(dst->nb[2])); // {d_inner, n_t, n_s} |
| 9228 |
|
| 9229 |
// TODO: transpose the output for smaller strides for big batches? |
| 9230 |
// d_inner |
| 9231 |
for (int i1 = 0; i1 < ir; ++i1) { |
| 9232 |
// rowwise dot product |
| 9233 |
// NOTE: not using ggml_vec_dot_f32, because its sum is in double precision |
| 9234 |
float sumf = 0.0f; |
| 9235 |
|
| 9236 |
// d_conv |
| 9237 |
for (int i0 = 0; i0 < nc; ++i0) { |
| 9238 |
sumf += s[i0 + i1*ncs] * c[i0 + i1*nc]; |
| 9239 |
} |
| 9240 |
x[i1] = sumf; |
| 9241 |
} |
| 9242 |
} |
| 9243 |
} |
| 9244 |
} |
| 9245 |
|
| 9246 |
void ggml_compute_forward_ssm_conv( |
| 9247 |
const ggml_compute_params * params, |
| 9248 |
ggml_tensor * dst) { |
| 9249 |
switch (dst->src[0]->type) { |
| 9250 |
case GGML_TYPE_F32: |
| 9251 |
{ |
| 9252 |
ggml_compute_forward_ssm_conv_f32(params, dst); |
| 9253 |
} break; |
| 9254 |
default: |
| 9255 |
{ |
| 9256 |
GGML_ABORT("fatal error"); |
| 9257 |
} |
| 9258 |
} |
| 9259 |
} |
| 9260 |
|
| 9261 |
// ggml_compute_forward_ssm_scan |
| 9262 |
|
| 9263 |
static void ggml_compute_forward_ssm_scan_f32( |
| 9264 |
const ggml_compute_params * params, |
| 9265 |
ggml_tensor * dst) { |
| 9266 |
const ggml_tensor * src0 = dst->src[0]; // s {d_state, dim, n_head, n_seqs+} |
| 9267 |
const ggml_tensor * src1 = dst->src[1]; // x {dim, n_head, n_seq_tokens, n_seqs} |
| 9268 |
const ggml_tensor * src2 = dst->src[2]; // dt {n_head, n_seq_tokens, n_seqs} |
| 9269 |
const ggml_tensor * src3 = dst->src[3]; // A {d_state, n_head} or {1, n_head} |
| 9270 |
const ggml_tensor * src4 = dst->src[4]; // B {d_state, n_group, n_seq_tokens, n_seqs} |
| 9271 |
const ggml_tensor * src5 = dst->src[5]; // C {d_state, n_group, n_seq_tokens, n_seqs} |
| 9272 |
const ggml_tensor * src6 = dst->src[6]; // ids {n_seqs} |
| 9273 |
|
| 9274 |
const int ith = params->ith; |
| 9275 |
const int nth = params->nth; |
| 9276 |
|
| 9277 |
const int64_t nc = src0->ne[0]; // d_state |
| 9278 |
const int64_t nr = src0->ne[1]; // dim |
| 9279 |
const int64_t nh = src1->ne[1]; // n_head |
| 9280 |
const int64_t ng = src4->ne[1]; |
| 9281 |
const int64_t nt = src1->ne[2]; // number of tokens per sequence |
| 9282 |
const int64_t ns = src1->ne[3]; // number of sequences in the batch |
| 9283 |
|
| 9284 |
// can't use ggml_nbytes because src1 is not necessarily contiguous |
| 9285 |
const int64_t s_off = ggml_nelements(src1) * ggml_element_size(src1); |
| 9286 |
|
| 9287 |
GGML_ASSERT(ggml_nelements(src1) + nc*nr*nh*ns == ggml_nelements(dst)); |
| 9288 |
GGML_ASSERT(src0->nb[0] == sizeof(float)); |
| 9289 |
GGML_ASSERT(src1->nb[0] == sizeof(float)); |
| 9290 |
GGML_ASSERT(src2->nb[0] == sizeof(float)); |
| 9291 |
GGML_ASSERT(src3->nb[0] == sizeof(float)); |
| 9292 |
GGML_ASSERT(src4->nb[0] == sizeof(float)); |
| 9293 |
GGML_ASSERT(src5->nb[0] == sizeof(float)); |
| 9294 |
GGML_ASSERT(src6->nb[0] == sizeof(int32_t)); |
| 9295 |
GGML_ASSERT(nh % ng == 0); |
| 9296 |
|
| 9297 |
// heads per thread |
| 9298 |
const int dh = (nh + nth - 1)/nth; |
| 9299 |
|
| 9300 |
// head range for this thread |
| 9301 |
const int ih0 = dh*ith; |
| 9302 |
const int ih1 = MIN(ih0 + dh, nh); |
| 9303 |
|
| 9304 |
const int32_t * ids = (const int32_t *) src6->data; |
| 9305 |
|
| 9306 |
for (int i3 = 0; i3 < ns; ++i3) { |
| 9307 |
const float * s0 = (const float *) ((const char *) src0->data + ids[i3]*(src0->nb[3])); // {d_state, dim, nh, ns} |
| 9308 |
float * s = ( float *) (( char *) dst->data + i3*(src0->nb[3]) + s_off); // {d_state, dim, nh, ns} |
| 9309 |
|
| 9310 |
for (int i2 = 0; i2 < nt; ++i2) { |
| 9311 |
const float * x = (const float *) ((const char *) src1->data + i2*(src1->nb[2]) + i3*(src1->nb[3])); // {dim, nh, nt, ns} |
| 9312 |
const float * dt = (const float *) ((const char *) src2->data + i2*(src2->nb[1]) + i3*(src2->nb[2])); // {nh, nt, ns} |
| 9313 |
const float * A = (const float *) ((const char *) src3->data); // {d_state, nh} or {1, nh} |
| 9314 |
const float * B = (const float *) ((const char *) src4->data + i2*(src4->nb[2]) + i3*(src4->nb[3])); // {d_state, ng, nt, ns} |
| 9315 |
const float * C = (const float *) ((const char *) src5->data + i2*(src5->nb[2]) + i3*(src5->nb[3])); // {d_state, ng, nt, ns} |
| 9316 |
float * y = ( float *) (( char *) dst->data + i2*(nh*nr*sizeof(float)) + i3*(nt*nh*nr*sizeof(float))); // {dim, nh, nt, ns} |
| 9317 |
|
| 9318 |
if (src3->ne[0] == 1) { |
| 9319 |
// Mamba-2 has a scalar decay factor per head; dA can be outside the state-wise loop |
| 9320 |
|
| 9321 |
// n_head |
| 9322 |
for (int h = ih0; h < ih1; ++h) { |
| 9323 |
// ref: https://github.com/state-spaces/mamba/blob/62db608da60f6fc790b8ed9f4b3225e95ca15fde/mamba_ssm/ops/triton/softplus.py#L16 |
| 9324 |
const float dt_soft_plus = ggml_compute_softplus_f32(dt[h]); |
| 9325 |
const float dA = expf(dt_soft_plus * A[h]); |
| 9326 |
const int g = h / (nh / ng); // repeat_interleave |
| 9327 |
|
| 9328 |
// dim |
| 9329 |
for (int i1 = 0; i1 < nr; ++i1) { |
| 9330 |
const int ii = i1 + h*nr; |
| 9331 |
const float x_dt = x[ii] * dt_soft_plus; |
| 9332 |
float sumf = 0.0f; |
| 9333 |
#if defined(GGML_SIMD) |
| 9334 |
#if defined(__ARM_FEATURE_SVE) |
| 9335 |
const int ggml_f32_epr = svcntw(); |
| 9336 |
const int ggml_f32_step = 1 * ggml_f32_epr; |
| 9337 |
|
| 9338 |
const int np = (nc & ~(ggml_f32_step - 1)); |
| 9339 |
|
| 9340 |
GGML_F32_VEC sum = GGML_F32_VEC_ZERO; |
| 9341 |
|
| 9342 |
GGML_F32_VEC adA = GGML_F32_VEC_SET1(dA); |
| 9343 |
GGML_F32_VEC axdt = GGML_F32_VEC_SET1(x_dt); |
| 9344 |
|
| 9345 |
for (int i = 0; i < np; i += ggml_f32_step) { |
| 9346 |
// TODO: maybe unroll more? |
| 9347 |
for (int j = 0; j < 1; j++) { |
| 9348 |
GGML_F32_VEC t0 = GGML_F32_VEC_LOAD(s0 + i + j*ggml_f32_epr + ii*nc); |
| 9349 |
GGML_F32_VEC t1 = GGML_F32_VEC_LOAD(B + i + j*ggml_f32_epr + g*nc); |
| 9350 |
GGML_F32_VEC t2 = GGML_F32_VEC_LOAD(C + i + j*ggml_f32_epr + g*nc); |
| 9351 |
|
| 9352 |
t0 = GGML_F32_VEC_MUL(t0, adA); |
| 9353 |
t1 = GGML_F32_VEC_MUL(t1, axdt); |
| 9354 |
|
| 9355 |
t0 = GGML_F32_VEC_ADD(t0, t1); |
| 9356 |
|
| 9357 |
sum = GGML_F32_VEC_FMA(sum, t0, t2); |
| 9358 |
|
| 9359 |
GGML_F32_VEC_STORE(s + i + j*ggml_f32_epr + ii*nc, t0); |
| 9360 |
} |
| 9361 |
} |
| 9362 |
|
| 9363 |
sumf = GGML_F32xt_REDUCE_ONE(sum); |
| 9364 |
#elif defined(__riscv_v_intrinsic) |
| 9365 |
// todo: RVV implementation |
| 9366 |
const int np = 0; |
| 9367 |
#else |
| 9368 |
const int np = (nc & ~(GGML_F32_STEP - 1)); |
| 9369 |
|
| 9370 |
GGML_F32_VEC sum[GGML_F32_ARR] = { GGML_F32_VEC_ZERO }; |
| 9371 |
|
| 9372 |
GGML_F32_VEC adA = GGML_F32_VEC_SET1(dA); |
| 9373 |
GGML_F32_VEC axdt = GGML_F32_VEC_SET1(x_dt); |
| 9374 |
|
| 9375 |
GGML_F32_VEC ax[GGML_F32_ARR]; |
| 9376 |
GGML_F32_VEC ay[GGML_F32_ARR]; |
| 9377 |
GGML_F32_VEC az[GGML_F32_ARR]; |
| 9378 |
|
| 9379 |
for (int i = 0; i < np; i += GGML_F32_STEP) { |
| 9380 |
for (int j = 0; j < GGML_F32_ARR; j++) { |
| 9381 |
ax[j] = GGML_F32_VEC_LOAD(s0 + i + j*GGML_F32_EPR + ii*nc); |
| 9382 |
ay[j] = GGML_F32_VEC_LOAD(B + i + j*GGML_F32_EPR + g*nc); |
| 9383 |
az[j] = GGML_F32_VEC_LOAD(C + i + j*GGML_F32_EPR + g*nc); |
| 9384 |
|
| 9385 |
ax[j] = GGML_F32_VEC_MUL(ax[j], adA); |
| 9386 |
ay[j] = GGML_F32_VEC_MUL(ay[j], axdt); |
| 9387 |
|
| 9388 |
ax[j] = GGML_F32_VEC_ADD(ax[j], ay[j]); |
| 9389 |
|
| 9390 |
sum[j] = GGML_F32_VEC_FMA(sum[j], ax[j], az[j]); |
| 9391 |
|
| 9392 |
GGML_F32_VEC_STORE(s + i + j*GGML_F32_EPR + ii*nc, ax[j]); |
| 9393 |
} |
| 9394 |
} |
| 9395 |
|
| 9396 |
// reduce sum0..sum3 to sum0 |
| 9397 |
GGML_F32_VEC_REDUCE(sumf, sum); |
| 9398 |
#endif |
| 9399 |
#else |
| 9400 |
const int np = 0; |
| 9401 |
#endif |
| 9402 |
// d_state |
| 9403 |
for (int i0 = np; i0 < nc; ++i0) { |
| 9404 |
const int i = i0 + ii*nc; |
| 9405 |
const int ig = i0 + g*nc; |
| 9406 |
// state = prev_state * dA + dB * x |
| 9407 |
const float state = (s0[i] * dA) + (B[ig] * x_dt); |
| 9408 |
// y = rowwise_dotprod(state, C) |
| 9409 |
sumf += state * C[ig]; |
| 9410 |
s[i] = state; |
| 9411 |
} |
| 9412 |
y[ii] = sumf; |
| 9413 |
} |
| 9414 |
} |
| 9415 |
} else { |
| 9416 |
// Mamba-1 has an element-wise decay factor for the states |
| 9417 |
|
| 9418 |
// n_head |
| 9419 |
for (int h = ih0; h < ih1; ++h) { |
| 9420 |
// ref: https://github.com/state-spaces/mamba/blob/62db608da60f6fc790b8ed9f4b3225e95ca15fde/mamba_ssm/ops/triton/softplus.py#L16 |
| 9421 |
const float dt_soft_plus = ggml_compute_softplus_f32(dt[h]); |
| 9422 |
const int g = h / (nh / ng); // repeat_interleave |
| 9423 |
|
| 9424 |
// dim |
| 9425 |
for (int i1 = 0; i1 < nr; ++i1) { |
| 9426 |
const int ii = i1 + h*nr; |
| 9427 |
const float x_dt = x[ii] * dt_soft_plus; |
| 9428 |
#if defined(__ARM_FEATURE_SVE) |
| 9429 |
svfloat32_t vx_dt = GGML_F32_VEC_SET1(x_dt); |
| 9430 |
svfloat32_t vdt_soft_plus = GGML_F32_VEC_SET1(dt_soft_plus); |
| 9431 |
svfloat32_t r1_vector = GGML_F32_VEC_ZERO; |
| 9432 |
|
| 9433 |
// d_state |
| 9434 |
// TODO: what happens when (d_state % svcntw()) != 0? |
| 9435 |
for (int64_t k = 0; k < nc; k += svcntw()) { |
| 9436 |
svfloat32_t vA = GGML_F32_VEC_LOAD(&A[h*nc + k]); |
| 9437 |
svfloat32_t vB = GGML_F32_VEC_LOAD(&B[k + g*nc]); |
| 9438 |
svfloat32_t vC = GGML_F32_VEC_LOAD(&C[k + g*nc]); |
| 9439 |
svfloat32_t vs0 = GGML_F32_VEC_LOAD(&s0[ii*nc + k]); |
| 9440 |
|
| 9441 |
svfloat32_t t1 = GGML_F32_VEC_MUL(vdt_soft_plus, vA); |
| 9442 |
t1 = exp_ps_sve(svptrue_b32(), t1); |
| 9443 |
svfloat32_t t2 = GGML_F32_VEC_MUL(vx_dt, vB); |
| 9444 |
|
| 9445 |
vs0 = GGML_F32_VEC_FMA(t2, vs0, t1); |
| 9446 |
r1_vector = GGML_F32_VEC_ADD(GGML_F32_VEC_MUL(vs0, vC), r1_vector); |
| 9447 |
|
| 9448 |
GGML_F32_VEC_STORE(&s[ii*nc + k], vs0); |
| 9449 |
} |
| 9450 |
y[ii] = GGML_F32xt_REDUCE_ONE(r1_vector); |
| 9451 |
#else |
| 9452 |
float sumf = 0.0f; |
| 9453 |
// NOTE: can't really use GGML_SIMD here because d_state is usually 16 |
| 9454 |
// and also because expf is used within the loop. |
| 9455 |
// d_state |
| 9456 |
for (int i0 = 0; i0 < nc; ++i0) { |
| 9457 |
const int i = i0 + ii*nc; |
| 9458 |
const int ig = i0 + g*nc; |
| 9459 |
// state = prev_state * dA + dB * x |
| 9460 |
const float state = (s0[i] * expf(dt_soft_plus * A[i0 + h*nc])) + (B[ig] * x_dt); |
| 9461 |
// y = rowwise_dotprod(state, C) |
| 9462 |
sumf += state * C[ig]; |
| 9463 |
s[i] = state; |
| 9464 |
} |
| 9465 |
y[ii] = sumf; |
| 9466 |
#endif |
| 9467 |
} |
| 9468 |
} |
| 9469 |
} |
| 9470 |
// use the output as the source when it's not the first token-wise iteration |
| 9471 |
s0 = s; |
| 9472 |
} |
| 9473 |
} |
| 9474 |
} |
| 9475 |
|
| 9476 |
void ggml_compute_forward_ssm_scan( |
| 9477 |
const ggml_compute_params * params, |
| 9478 |
ggml_tensor * dst) { |
| 9479 |
switch (dst->src[0]->type) { |
| 9480 |
case GGML_TYPE_F32: |
| 9481 |
{ |
| 9482 |
ggml_compute_forward_ssm_scan_f32(params, dst); |
| 9483 |
} break; |
| 9484 |
default: |
| 9485 |
{ |
| 9486 |
GGML_ABORT("fatal error"); |
| 9487 |
} |
| 9488 |
} |
| 9489 |
} |
| 9490 |
|
| 9491 |
// ggml_compute_forward_win_part |
| 9492 |
|
| 9493 |
static void ggml_compute_forward_win_part_f32( |
| 9494 |
const ggml_compute_params * params, |
| 9495 |
ggml_tensor * dst) { |
| 9496 |
GGML_UNUSED(params); |
| 9497 |
|
| 9498 |
const ggml_tensor * src0 = dst->src[0]; |
| 9499 |
|
| 9500 |
GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 9501 |
GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 9502 |
|
| 9503 |
const int32_t nep0 = ((const int32_t *)(dst->op_params))[0]; |
| 9504 |
const int32_t nep1 = ((const int32_t *)(dst->op_params))[1]; |
| 9505 |
const int32_t w = ((const int32_t *)(dst->op_params))[2]; |
| 9506 |
|
| 9507 |
assert(ne00 == ne0); |
| 9508 |
assert(ne3 == nep0*nep1); |
| 9509 |
|
| 9510 |
// TODO: optimize / multi-thread |
| 9511 |
for (int py = 0; py < nep1; ++py) { |
| 9512 |
for (int px = 0; px < nep0; ++px) { |
| 9513 |
const int64_t i3 = py*nep0 + px; |
| 9514 |
for (int64_t i2 = 0; i2 < ne2; ++i2) { |
| 9515 |
for (int64_t i1 = 0; i1 < ne1; ++i1) { |
| 9516 |
for (int64_t i0 = 0; i0 < ne0; ++i0) { |
| 9517 |
const int64_t i02 = py*w + i2; |
| 9518 |
const int64_t i01 = px*w + i1; |
| 9519 |
const int64_t i00 = i0; |
| 9520 |
|
| 9521 |
const int64_t i = i3*ne2*ne1*ne0 + i2*ne1*ne0 + i1*ne0 + i0; |
| 9522 |
const int64_t j = i02*ne01*ne00 + i01*ne00 + i00; |
| 9523 |
|
| 9524 |
if (py*w + i2 >= ne02 || px*w + i1 >= ne01) { |
| 9525 |
((float *) dst->data)[i] = 0.0f; |
| 9526 |
} else { |
| 9527 |
((float *) dst->data)[i] = ((float *) src0->data)[j]; |
| 9528 |
} |
| 9529 |
} |
| 9530 |
} |
| 9531 |
} |
| 9532 |
} |
| 9533 |
} |
| 9534 |
} |
| 9535 |
|
| 9536 |
void ggml_compute_forward_win_part( |
| 9537 |
const ggml_compute_params * params, |
| 9538 |
ggml_tensor * dst) { |
| 9539 |
|
| 9540 |
const ggml_tensor * src0 = dst->src[0]; |
| 9541 |
|
| 9542 |
switch (src0->type) { |
| 9543 |
case GGML_TYPE_F32: |
| 9544 |
{ |
| 9545 |
ggml_compute_forward_win_part_f32(params, dst); |
| 9546 |
} break; |
| 9547 |
default: |
| 9548 |
{ |
| 9549 |
GGML_ABORT("fatal error"); |
| 9550 |
} |
| 9551 |
} |
| 9552 |
} |
| 9553 |
|
| 9554 |
// ggml_compute_forward_win_unpart |
| 9555 |
|
| 9556 |
static void ggml_compute_forward_win_unpart_f32( |
| 9557 |
const ggml_compute_params * params, |
| 9558 |
ggml_tensor * dst) { |
| 9559 |
GGML_UNUSED(params); |
| 9560 |
|
| 9561 |
const ggml_tensor * src0 = dst->src[0]; |
| 9562 |
|
| 9563 |
GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) |
| 9564 |
GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) |
| 9565 |
|
| 9566 |
const int32_t w = ((const int32_t *)(dst->op_params))[0]; |
| 9567 |
|
| 9568 |
// padding |
| 9569 |
const int px = (w - ne1%w)%w; |
| 9570 |
//const int py = (w - ne2%w)%w; |
| 9571 |
|
| 9572 |
const int npx = (px + ne1)/w; |
| 9573 |
//const int npy = (py + ne2)/w; |
| 9574 |
|
| 9575 |
assert(ne0 == ne00); |
| 9576 |
|
| 9577 |
// TODO: optimize / multi-thread |
| 9578 |
for (int64_t i2 = 0; i2 < ne2; ++i2) { |
| 9579 |
for (int64_t i1 = 0; i1 < ne1; ++i1) { |
| 9580 |
for (int64_t i0 = 0; i0 < ne0; ++i0) { |
| 9581 |
const int ip2 = i2/w; |
| 9582 |
const int ip1 = i1/w; |
| 9583 |
|
| 9584 |
const int64_t i02 = i2%w; |
| 9585 |
const int64_t i01 = i1%w; |
| 9586 |
const int64_t i00 = i0; |
| 9587 |
|
| 9588 |
const int64_t i = (ip2*npx + ip1)*ne02*ne01*ne00 + i02*ne01*ne00 + i01*ne00 + i00; |
| 9589 |
const int64_t j = i2*ne1*ne0 + i1*ne0 + i0; |
| 9590 |
|
| 9591 |
((float *) dst->data)[j] = ((float *) src0->data)[i]; |
| 9592 |
} |
| 9593 |
} |
| 9594 |
} |
| 9595 |
} |
| 9596 |
|
| 9597 |
void ggml_compute_forward_win_unpart( |
| 9598 |
const ggml_compute_params * params, |
| 9599 |
ggml_tensor * dst) { |
| 9600 |
|
| 9601 |
const ggml_tensor * src0 = dst->src[0]; |
| 9602 |
|
| 9603 |
switch (src0->type) { |
| 9604 |
case GGML_TYPE_F32: |
| 9605 |
{ |
| 9606 |
ggml_compute_forward_win_unpart_f32(params, dst); |
| 9607 |
} break; |
| 9608 |
default: |
| 9609 |
{ |
| 9610 |
GGML_ABORT("fatal error"); |
| 9611 |
} |
| 9612 |
} |
| 9613 |
} |
| 9614 |
|
| 9615 |
//gmml_compute_forward_unary |
| 9616 |
|
| 9617 |
void ggml_compute_forward_unary( |
| 9618 |
const ggml_compute_params * params, |
| 9619 |
ggml_tensor * dst) { |
| 9620 |
|
| 9621 |
const ggml_unary_op op = ggml_get_unary_op(dst); |
| 9622 |
|
| 9623 |
switch (op) { |
| 9624 |
case GGML_UNARY_OP_ABS: |
| 9625 |
{ |
| 9626 |
ggml_compute_forward_abs(params, dst); |
| 9627 |
} break; |
| 9628 |
case GGML_UNARY_OP_SGN: |
| 9629 |
{ |
| 9630 |
ggml_compute_forward_sgn(params, dst); |
| 9631 |
} break; |
| 9632 |
case GGML_UNARY_OP_NEG: |
| 9633 |
{ |
| 9634 |
ggml_compute_forward_neg(params, dst); |
| 9635 |
} break; |
| 9636 |
case GGML_UNARY_OP_STEP: |
| 9637 |
{ |
| 9638 |
ggml_compute_forward_step(params, dst); |
| 9639 |
} break; |
| 9640 |
case GGML_UNARY_OP_TANH: |
| 9641 |
{ |
| 9642 |
ggml_compute_forward_tanh(params, dst); |
| 9643 |
} break; |
| 9644 |
case GGML_UNARY_OP_ELU: |
| 9645 |
{ |
| 9646 |
ggml_compute_forward_elu(params, dst); |
| 9647 |
} break; |
| 9648 |
case GGML_UNARY_OP_RELU: |
| 9649 |
{ |
| 9650 |
ggml_compute_forward_relu(params, dst); |
| 9651 |
} break; |
| 9652 |
case GGML_UNARY_OP_SIGMOID: |
| 9653 |
{ |
| 9654 |
ggml_compute_forward_sigmoid(params, dst); |
| 9655 |
} break; |
| 9656 |
case GGML_UNARY_OP_GELU: |
| 9657 |
{ |
| 9658 |
ggml_compute_forward_gelu(params, dst); |
| 9659 |
} break; |
| 9660 |
case GGML_UNARY_OP_GELU_ERF: |
| 9661 |
{ |
| 9662 |
ggml_compute_forward_gelu_erf(params, dst); |
| 9663 |
} break; |
| 9664 |
case GGML_UNARY_OP_GELU_QUICK: |
| 9665 |
{ |
| 9666 |
ggml_compute_forward_gelu_quick(params, dst); |
| 9667 |
} break; |
| 9668 |
case GGML_UNARY_OP_SILU: |
| 9669 |
{ |
| 9670 |
ggml_compute_forward_silu(params, dst); |
| 9671 |
} break; |
| 9672 |
case GGML_UNARY_OP_HARDSWISH: |
| 9673 |
{ |
| 9674 |
ggml_compute_forward_hardswish(params, dst); |
| 9675 |
} break; |
| 9676 |
case GGML_UNARY_OP_HARDSIGMOID: |
| 9677 |
{ |
| 9678 |
ggml_compute_forward_hardsigmoid(params, dst); |
| 9679 |
} break; |
| 9680 |
case GGML_UNARY_OP_EXP: |
| 9681 |
{ |
| 9682 |
ggml_compute_forward_exp(params, dst); |
| 9683 |
} break; |
| 9684 |
case GGML_UNARY_OP_FLOOR: |
| 9685 |
{ |
| 9686 |
ggml_compute_forward_floor(params, dst); |
| 9687 |
} break; |
| 9688 |
case GGML_UNARY_OP_CEIL: |
| 9689 |
{ |
| 9690 |
ggml_compute_forward_ceil(params, dst); |
| 9691 |
} break; |
| 9692 |
case GGML_UNARY_OP_ROUND: |
| 9693 |
{ |
| 9694 |
ggml_compute_forward_round(params, dst); |
| 9695 |
} break; |
| 9696 |
case GGML_UNARY_OP_TRUNC: |
| 9697 |
{ |
| 9698 |
ggml_compute_forward_trunc(params, dst); |
| 9699 |
} break; |
| 9700 |
case GGML_UNARY_OP_XIELU: |
| 9701 |
{ |
| 9702 |
ggml_compute_forward_xielu(params, dst); |
| 9703 |
} break; |
| 9704 |
case GGML_UNARY_OP_EXPM1: |
| 9705 |
{ |
| 9706 |
ggml_compute_forward_expm1(params, dst); |
| 9707 |
} break; |
| 9708 |
case GGML_UNARY_OP_SOFTPLUS: |
| 9709 |
{ |
| 9710 |
ggml_compute_forward_softplus(params, dst); |
| 9711 |
} break; |
| 9712 |
default: |
| 9713 |
{ |
| 9714 |
GGML_ABORT("fatal error"); |
| 9715 |
} |
| 9716 |
} |
| 9717 |
} |
| 9718 |
|
| 9719 |
//ggml_compute_forward_glu |
| 9720 |
|
| 9721 |
void ggml_compute_forward_glu( |
| 9722 |
const ggml_compute_params * params, |
| 9723 |
ggml_tensor * dst) { |
| 9724 |
|
| 9725 |
const ggml_glu_op op = ggml_get_glu_op(dst); |
| 9726 |
|
| 9727 |
switch (op) { |
| 9728 |
case GGML_GLU_OP_REGLU: |
| 9729 |
{ |
| 9730 |
ggml_compute_forward_reglu(params, dst); |
| 9731 |
} break; |
| 9732 |
case GGML_GLU_OP_GEGLU: |
| 9733 |
{ |
| 9734 |
ggml_compute_forward_geglu(params, dst); |
| 9735 |
} break; |
| 9736 |
case GGML_GLU_OP_SWIGLU: |
| 9737 |
{ |
| 9738 |
ggml_compute_forward_swiglu(params, dst); |
| 9739 |
} break; |
| 9740 |
case GGML_GLU_OP_SWIGLU_OAI: |
| 9741 |
{ |
| 9742 |
ggml_compute_forward_swiglu_oai(params, dst); |
| 9743 |
} break; |
| 9744 |
case GGML_GLU_OP_GEGLU_ERF: |
| 9745 |
{ |
| 9746 |
ggml_compute_forward_geglu_erf(params, dst); |
| 9747 |
} break; |
| 9748 |
case GGML_GLU_OP_GEGLU_QUICK: |
| 9749 |
{ |
| 9750 |
ggml_compute_forward_geglu_quick(params, dst); |
| 9751 |
} break; |
| 9752 |
default: |
| 9753 |
{ |
| 9754 |
GGML_ABORT("fatal error"); |
| 9755 |
} |
| 9756 |
} |
| 9757 |
} |
| 9758 |
|
| 9759 |
// ggml_compute_forward_get_rel_pos |
| 9760 |
|
| 9761 |
static void ggml_compute_forward_get_rel_pos_f16( |
| 9762 |
const ggml_compute_params * params, |
| 9763 |
ggml_tensor * dst) { |
| 9764 |
GGML_UNUSED(params); |
| 9765 |
|
| 9766 |
const ggml_tensor * src0 = dst->src[0]; |
| 9767 |
|
| 9768 |
// ref: https://github.com/facebookresearch/segment-anything/blob/main/segment_anything/modeling/image_encoder.py#L292-L322 |
| 9769 |
|
| 9770 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 9771 |
|
| 9772 |
const int64_t w = ne1; |
| 9773 |
|
| 9774 |
ggml_fp16_t * src0_data = (ggml_fp16_t *) src0->data; |
| 9775 |
ggml_fp16_t * dst_data = (ggml_fp16_t *) dst->data; |
| 9776 |
|
| 9777 |
for (int64_t i2 = 0; i2 < ne2; ++i2) { |
| 9778 |
for (int64_t i1 = 0; i1 < ne1; ++i1) { |
| 9779 |
const int64_t pos = (w - i1 - 1) + i2; |
| 9780 |
for (int64_t i0 = 0; i0 < ne0; ++i0) { |
| 9781 |
dst_data[i2*ne1*ne0 + i1*ne0 + i0] = src0_data[pos*ne00 + i0]; |
| 9782 |
} |
| 9783 |
} |
| 9784 |
} |
| 9785 |
} |
| 9786 |
|
| 9787 |
void ggml_compute_forward_get_rel_pos( |
| 9788 |
const ggml_compute_params * params, |
| 9789 |
ggml_tensor * dst) { |
| 9790 |
|
| 9791 |
const ggml_tensor * src0 = dst->src[0]; |
| 9792 |
|
| 9793 |
switch (src0->type) { |
| 9794 |
case GGML_TYPE_F16: |
| 9795 |
case GGML_TYPE_BF16: |
| 9796 |
{ |
| 9797 |
ggml_compute_forward_get_rel_pos_f16(params, dst); |
| 9798 |
} break; |
| 9799 |
default: |
| 9800 |
{ |
| 9801 |
GGML_ABORT("fatal error"); |
| 9802 |
} |
| 9803 |
} |
| 9804 |
} |
| 9805 |
|
| 9806 |
// ggml_compute_forward_add_rel_pos |
| 9807 |
|
| 9808 |
static void ggml_compute_forward_add_rel_pos_f32( |
| 9809 |
const ggml_compute_params * params, |
| 9810 |
ggml_tensor * dst) { |
| 9811 |
|
| 9812 |
const ggml_tensor * src0 = dst->src[0]; |
| 9813 |
const ggml_tensor * src1 = dst->src[1]; |
| 9814 |
const ggml_tensor * src2 = dst->src[2]; |
| 9815 |
|
| 9816 |
const bool inplace = (bool) ((int32_t *) dst->op_params)[0]; |
| 9817 |
if (!inplace) { |
| 9818 |
if (params->ith == 0) { |
| 9819 |
memcpy((char *) dst->data, (char *) src0->data, ggml_nbytes(dst)); |
| 9820 |
} |
| 9821 |
ggml_barrier(params->threadpool); |
| 9822 |
} |
| 9823 |
// ref: https://github.com/facebookresearch/segment-anything/blob/main/segment_anything/modeling/image_encoder.py#L357-L359 |
| 9824 |
|
| 9825 |
float * src1_data = (float *) src1->data; |
| 9826 |
float * src2_data = (float *) src2->data; |
| 9827 |
float * dst_data = (float *) dst->data; |
| 9828 |
|
| 9829 |
const int64_t ne10 = src1->ne[0]; |
| 9830 |
const int64_t ne11 = src1->ne[1]; |
| 9831 |
const int64_t ne12 = src1->ne[2]; |
| 9832 |
const int64_t ne13 = src1->ne[3]; |
| 9833 |
|
| 9834 |
const int ith = params->ith; |
| 9835 |
const int nth = params->nth; |
| 9836 |
|
| 9837 |
// total patches in dst |
| 9838 |
const int np = ne13; |
| 9839 |
|
| 9840 |
// patches per thread |
| 9841 |
const int dp = (np + nth - 1)/nth; |
| 9842 |
|
| 9843 |
// patch range for this thread |
| 9844 |
const int ip0 = dp*ith; |
| 9845 |
const int ip1 = MIN(ip0 + dp, np); |
| 9846 |
|
| 9847 |
for (int64_t i13 = ip0; i13 < ip1; ++i13) { |
| 9848 |
for (int64_t i12 = 0; i12 < ne12; ++i12) { |
| 9849 |
for (int64_t i11 = 0; i11 < ne11; ++i11) { |
| 9850 |
const int64_t jp1 = i13*ne12*ne11*ne10 + i12*ne11*ne10 + i11*ne10; |
| 9851 |
for (int64_t i10 = 0; i10 < ne10; ++i10) { |
| 9852 |
const int64_t jp0 = jp1 + i10; |
| 9853 |
const float src1_e = src1_data[jp0]; |
| 9854 |
const float src2_e = src2_data[jp0]; |
| 9855 |
|
| 9856 |
const int64_t jdh = jp0 * ne10; |
| 9857 |
const int64_t jdw = jdh - (ne10 - 1) * i10; |
| 9858 |
|
| 9859 |
for (int64_t j = 0; j < ne10; ++j) { |
| 9860 |
dst_data[jdh + j ] += src2_e; |
| 9861 |
dst_data[jdw + j*ne10] += src1_e; |
| 9862 |
} |
| 9863 |
} |
| 9864 |
} |
| 9865 |
} |
| 9866 |
} |
| 9867 |
} |
| 9868 |
|
| 9869 |
void ggml_compute_forward_add_rel_pos( |
| 9870 |
const ggml_compute_params * params, |
| 9871 |
ggml_tensor * dst) { |
| 9872 |
|
| 9873 |
const ggml_tensor * src0 = dst->src[0]; |
| 9874 |
|
| 9875 |
switch (src0->type) { |
| 9876 |
case GGML_TYPE_F32: |
| 9877 |
{ |
| 9878 |
ggml_compute_forward_add_rel_pos_f32(params, dst); |
| 9879 |
} break; |
| 9880 |
default: |
| 9881 |
{ |
| 9882 |
GGML_ABORT("fatal error"); |
| 9883 |
} |
| 9884 |
} |
| 9885 |
} |
| 9886 |
|
| 9887 |
// ggml_compute_forward_rwkv_wkv6 |
| 9888 |
|
| 9889 |
static void ggml_compute_forward_rwkv_wkv6_f32( |
| 9890 |
const ggml_compute_params * params, |
| 9891 |
ggml_tensor * dst) { |
| 9892 |
const int64_t T = dst->src[1]->ne[2]; |
| 9893 |
const int64_t C = dst->ne[0]; |
| 9894 |
const int64_t HEADS = dst->src[1]->ne[1]; |
| 9895 |
const int64_t n_seqs = dst->src[5]->ne[1]; |
| 9896 |
const int64_t head_size = C / HEADS; |
| 9897 |
|
| 9898 |
float * dst_data = (float *) dst->data; |
| 9899 |
float * state = ((float *) dst->data) + C * T; |
| 9900 |
|
| 9901 |
const int ith = params->ith; |
| 9902 |
const int nth = params->nth; |
| 9903 |
|
| 9904 |
if (ith >= HEADS) { |
| 9905 |
return; |
| 9906 |
} |
| 9907 |
|
| 9908 |
const int h_start = (HEADS * ith) / nth; |
| 9909 |
const int h_end = ((HEADS * (ith + 1)) / nth < HEADS) ? |
| 9910 |
(HEADS * (ith + 1)) / nth : HEADS; |
| 9911 |
|
| 9912 |
float * k = (float *) dst->src[0]->data; |
| 9913 |
float * v = (float *) dst->src[1]->data; |
| 9914 |
float * r = (float *) dst->src[2]->data; |
| 9915 |
float * time_faaaa = (float *) dst->src[3]->data; |
| 9916 |
float * time_decay = (float *) dst->src[4]->data; |
| 9917 |
|
| 9918 |
size_t t_stride = HEADS * head_size; // Same to C |
| 9919 |
|
| 9920 |
size_t h_stride = C / HEADS; |
| 9921 |
GGML_ASSERT(C % HEADS == 0); // C must be divisible by HEADS |
| 9922 |
size_t h_stride_2d = head_size * head_size; |
| 9923 |
|
| 9924 |
if (ith == 0) { |
| 9925 |
memset(dst_data, 0, T * C * sizeof(float)); |
| 9926 |
} |
| 9927 |
ggml_barrier(params->threadpool); |
| 9928 |
|
| 9929 |
|
| 9930 |
#if defined(__AVX__) && !defined(__AVX512F__) |
| 9931 |
#define GGML_F32X GGML_F32x8 |
| 9932 |
#define GGML_F32X_SET1 GGML_F32x8_SET1 |
| 9933 |
#define GGML_F32X_LOAD GGML_F32x8_LOAD |
| 9934 |
#define GGML_F32X_STORE GGML_F32x8_STORE |
| 9935 |
#define GGML_F32X_MUL GGML_F32x8_MUL |
| 9936 |
#define GGML_F32X_FMA GGML_F32x8_FMA |
| 9937 |
#define WKV_VECTOR_SIZE 8 |
| 9938 |
#elif defined(__AVX512F__) |
| 9939 |
#define GGML_F32X GGML_F32x16 |
| 9940 |
#define GGML_F32X_SET1 GGML_F32x16_SET1 |
| 9941 |
#define GGML_F32X_LOAD GGML_F32x16_LOAD |
| 9942 |
#define GGML_F32X_STORE GGML_F32x16_STORE |
| 9943 |
#define GGML_F32X_MUL GGML_F32x16_MUL |
| 9944 |
#define GGML_F32X_FMA GGML_F32x16_FMA |
| 9945 |
#define WKV_VECTOR_SIZE 16 |
| 9946 |
#elif defined(__ARM_FEATURE_SVE) && defined(__aarch64__) |
| 9947 |
#define GGML_F32X GGML_F32xt |
| 9948 |
#define GGML_F32X_SET1 GGML_F32xt_SET1 |
| 9949 |
#define GGML_F32X_LOAD GGML_F32xt_LOAD |
| 9950 |
#define GGML_F32X_STORE GGML_F32xt_STORE |
| 9951 |
#define GGML_F32X_MUL GGML_F32xt_MUL |
| 9952 |
#define GGML_F32X_FMA GGML_F32xt_FMA |
| 9953 |
#define WKV_VECTOR_SIZE 8 |
| 9954 |
#elif defined(__ARM_NEON) && defined(__aarch64__) |
| 9955 |
#define GGML_F32X GGML_F32x4 |
| 9956 |
#define GGML_F32X_SET1 GGML_F32x4_SET1 |
| 9957 |
#define GGML_F32X_LOAD GGML_F32x4_LOAD |
| 9958 |
#define GGML_F32X_STORE GGML_F32x4_STORE |
| 9959 |
#define GGML_F32X_MUL GGML_F32x4_MUL |
| 9960 |
#define GGML_F32X_FMA GGML_F32x4_FMA |
| 9961 |
#define WKV_VECTOR_SIZE 4 |
| 9962 |
#endif |
| 9963 |
|
| 9964 |
#ifdef WKV_VECTOR_SIZE |
| 9965 |
int wkv_vector_size; |
| 9966 |
#if defined(__ARM_FEATURE_SVE) |
| 9967 |
wkv_vector_size = svcntw(); |
| 9968 |
#else |
| 9969 |
wkv_vector_size = WKV_VECTOR_SIZE; |
| 9970 |
#endif |
| 9971 |
const int64_t vec_count = head_size / wkv_vector_size; |
| 9972 |
|
| 9973 |
for (int64_t t = 0; t < T; t++) { |
| 9974 |
size_t t_offset = t * t_stride; |
| 9975 |
size_t state_offset = head_size * C * (t / (T / n_seqs)); |
| 9976 |
float * state_cur = state + state_offset; |
| 9977 |
float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[5]->data + state_offset; |
| 9978 |
|
| 9979 |
for (int64_t h = h_start; h < h_end; h++) { |
| 9980 |
size_t h_offset = h * h_stride; |
| 9981 |
size_t t_h_offset = t_offset + h_offset; |
| 9982 |
size_t h_2d_offset = h * h_stride_2d; |
| 9983 |
|
| 9984 |
for (int64_t i = 0; i < head_size; i++) { |
| 9985 |
size_t t_h_i_offset = t_h_offset + i; |
| 9986 |
size_t h_i_offset = h_offset + i; |
| 9987 |
size_t h_2d_i_offset = h_2d_offset + i * h_stride; |
| 9988 |
|
| 9989 |
float k_val = k[t_h_i_offset]; |
| 9990 |
float r_val = r[t_h_i_offset]; |
| 9991 |
float time_faaaa_val = time_faaaa[h_i_offset]; |
| 9992 |
float time_decay_val = time_decay[t_h_i_offset]; |
| 9993 |
|
| 9994 |
// Broadcast scalar values to vectors |
| 9995 |
GGML_F32X k_vec = GGML_F32X_SET1(k_val); |
| 9996 |
GGML_F32X r_vec = GGML_F32X_SET1(r_val); |
| 9997 |
GGML_F32X time_faaaa_vec = GGML_F32X_SET1(time_faaaa_val); |
| 9998 |
GGML_F32X time_decay_vec = GGML_F32X_SET1(time_decay_val); |
| 9999 |
|
| 10000 |
for (int64_t j = 0; j < vec_count; j++) { |
| 10001 |
size_t base_j = j * wkv_vector_size; |
| 10002 |
size_t t_h_j_offset = t_h_offset + base_j; |
| 10003 |
size_t h_2d_i_j_offset = h_2d_i_offset + base_j; |
| 10004 |
|
| 10005 |
// Load x elements at once |
| 10006 |
GGML_F32X v_vec = GGML_F32X_LOAD(&v[t_h_j_offset]); |
| 10007 |
GGML_F32X prev_state_vec = GGML_F32X_LOAD(&state_prev[h_2d_i_j_offset]); |
| 10008 |
GGML_F32X dst_vec = GGML_F32X_LOAD(&dst_data[t_h_j_offset]); |
| 10009 |
|
| 10010 |
// Compute kv = v * k |
| 10011 |
GGML_F32X kv_vec = GGML_F32X_MUL(v_vec, k_vec); |
| 10012 |
|
| 10013 |
// Compute temp = kv * time_faaaa + prev_state |
| 10014 |
GGML_F32X temp_vec = GGML_F32X_FMA(prev_state_vec, kv_vec, time_faaaa_vec); |
| 10015 |
|
| 10016 |
// Update dst: dst += temp * r |
| 10017 |
dst_vec = GGML_F32X_FMA(dst_vec, temp_vec, r_vec); |
| 10018 |
GGML_F32X_STORE(&dst_data[t_h_j_offset], dst_vec); |
| 10019 |
|
| 10020 |
// Update state: state = prev_state * time_decay + kv |
| 10021 |
GGML_F32X new_state_vec = GGML_F32X_FMA(kv_vec, prev_state_vec, time_decay_vec); |
| 10022 |
GGML_F32X_STORE(&state_cur[h_2d_i_j_offset], new_state_vec); |
| 10023 |
} |
| 10024 |
|
| 10025 |
// Handle remaining elements, this will not be used. |
| 10026 |
for (int64_t j = vec_count * wkv_vector_size; j < head_size; j++) { |
| 10027 |
size_t t_h_j_offset = t_h_offset + j; |
| 10028 |
size_t h_2d_i_j_offset = h_2d_i_offset + j; |
| 10029 |
float v_val = v[t_h_j_offset]; |
| 10030 |
float kv_val = v_val * k_val; |
| 10031 |
float prev_state_val = state_prev[h_2d_i_j_offset]; |
| 10032 |
float temp_val = kv_val * time_faaaa_val + prev_state_val; |
| 10033 |
dst_data[t_h_j_offset] += temp_val * r_val; |
| 10034 |
state_cur[h_2d_i_j_offset] = prev_state_val * time_decay_val + kv_val; |
| 10035 |
} |
| 10036 |
} |
| 10037 |
} |
| 10038 |
} |
| 10039 |
|
| 10040 |
#else |
| 10041 |
// basically fused operations: |
| 10042 |
// dst = r @ (time_faaaa * (k @ v) + state), |
| 10043 |
// state = time_decay * state + (k @ v), |
| 10044 |
// recursive through each token |
| 10045 |
for (int64_t t = 0; t < T; t++) { |
| 10046 |
size_t t_offset = t * t_stride; |
| 10047 |
size_t state_offset = head_size * C * (t / (T / n_seqs)); |
| 10048 |
float * state_cur = state + state_offset; |
| 10049 |
float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[5]->data + state_offset; |
| 10050 |
|
| 10051 |
for (int64_t h = h_start; h < h_end; h++) { |
| 10052 |
size_t h_offset = h * h_stride; |
| 10053 |
size_t t_h_offset = t_offset + h_offset; |
| 10054 |
size_t h_2d_offset = h * h_stride_2d; |
| 10055 |
|
| 10056 |
for (int64_t i = 0; i < head_size; i++) { |
| 10057 |
size_t t_h_i_offset = t_h_offset + i; |
| 10058 |
size_t h_i_offset = h_offset + i; |
| 10059 |
size_t h_2d_i_offset = h_2d_offset + i * h_stride; |
| 10060 |
|
| 10061 |
float k_val = k[t_h_i_offset]; |
| 10062 |
float r_val = r[t_h_i_offset]; |
| 10063 |
float time_faaaa_val = time_faaaa[h_i_offset]; |
| 10064 |
// RWKV v6: different time_decay for each token. |
| 10065 |
float time_decay_val = time_decay[t_h_i_offset]; |
| 10066 |
|
| 10067 |
for (int64_t j = 0; j < head_size; j++) { |
| 10068 |
size_t t_h_j_offset = t_h_offset + j; |
| 10069 |
size_t h_2d_i_j_offset = h_2d_i_offset + j; |
| 10070 |
|
| 10071 |
float v_val = v[t_h_j_offset]; |
| 10072 |
float kv_val = v_val * k_val; |
| 10073 |
float prev_state_val = state_prev[h_2d_i_j_offset]; |
| 10074 |
float temp_val = kv_val * time_faaaa_val + prev_state_val; |
| 10075 |
dst_data[t_h_j_offset] += temp_val * r_val; |
| 10076 |
state_cur[h_2d_i_j_offset] = prev_state_val * time_decay_val + kv_val; |
| 10077 |
} |
| 10078 |
} |
| 10079 |
} |
| 10080 |
} |
| 10081 |
#endif |
| 10082 |
} |
| 10083 |
|
| 10084 |
|
| 10085 |
void ggml_compute_forward_rwkv_wkv6( |
| 10086 |
const ggml_compute_params * params, |
| 10087 |
ggml_tensor * dst) { |
| 10088 |
|
| 10089 |
const ggml_tensor * src0 = dst->src[0]; |
| 10090 |
|
| 10091 |
switch (src0->type) { |
| 10092 |
case GGML_TYPE_F32: |
| 10093 |
{ |
| 10094 |
ggml_compute_forward_rwkv_wkv6_f32(params, dst); |
| 10095 |
} break; |
| 10096 |
default: |
| 10097 |
{ |
| 10098 |
GGML_ABORT("fatal error"); |
| 10099 |
} |
| 10100 |
} |
| 10101 |
} |
| 10102 |
|
| 10103 |
// ggml_compute_forward_gla |
| 10104 |
|
| 10105 |
static void ggml_compute_forward_gla_f32( |
| 10106 |
const ggml_compute_params * params, |
| 10107 |
ggml_tensor * dst) { |
| 10108 |
const int64_t T = dst->src[1]->ne[2]; |
| 10109 |
const int64_t C = dst->ne[0]; |
| 10110 |
const int64_t HEADS = dst->src[1]->ne[1]; |
| 10111 |
const int64_t n_seqs = dst->src[4]->ne[1]; |
| 10112 |
const int64_t head_size = C / HEADS; |
| 10113 |
const float scale = ggml_get_op_params_f32(dst, 0); |
| 10114 |
|
| 10115 |
float * dst_data = (float *) dst->data; |
| 10116 |
float * state = ((float *) dst->data) + C * T; |
| 10117 |
|
| 10118 |
const int ith = params->ith; |
| 10119 |
const int nth = params->nth; |
| 10120 |
|
| 10121 |
if (ith >= HEADS) { |
| 10122 |
return; |
| 10123 |
} |
| 10124 |
|
| 10125 |
const int h_start = (HEADS * ith) / nth; |
| 10126 |
const int h_end = ((HEADS * (ith + 1)) / nth < HEADS) ? |
| 10127 |
(HEADS * (ith + 1)) / nth : HEADS; |
| 10128 |
|
| 10129 |
float * k = (float *) dst->src[0]->data; |
| 10130 |
float * v = (float *) dst->src[1]->data; |
| 10131 |
float * q = (float *) dst->src[2]->data; |
| 10132 |
float * g = (float *) dst->src[3]->data; |
| 10133 |
|
| 10134 |
size_t t_stride = HEADS * head_size; // Same to C |
| 10135 |
|
| 10136 |
size_t h_stride = C / HEADS; |
| 10137 |
GGML_ASSERT(C % HEADS == 0); // C must be divisible by HEADS |
| 10138 |
size_t h_stride_2d = head_size * head_size; |
| 10139 |
|
| 10140 |
if (ith == 0) { |
| 10141 |
memset(dst_data, 0, T * C * sizeof(float)); |
| 10142 |
} |
| 10143 |
ggml_barrier(params->threadpool); |
| 10144 |
|
| 10145 |
|
| 10146 |
#if defined(__AVX__) && !defined(__AVX512F__) |
| 10147 |
#define GGML_F32X GGML_F32x8 |
| 10148 |
#define GGML_F32X_SET1 GGML_F32x8_SET1 |
| 10149 |
#define GGML_F32X_LOAD GGML_F32x8_LOAD |
| 10150 |
#define GGML_F32X_STORE GGML_F32x8_STORE |
| 10151 |
#define GGML_F32X_MUL GGML_F32x8_MUL |
| 10152 |
#define GGML_F32X_FMA GGML_F32x8_FMA |
| 10153 |
#define GLA_VECTOR_SIZE 8 |
| 10154 |
#elif defined(__AVX512F__) |
| 10155 |
#define GGML_F32X GGML_F32x16 |
| 10156 |
#define GGML_F32X_SET1 GGML_F32x16_SET1 |
| 10157 |
#define GGML_F32X_LOAD GGML_F32x16_LOAD |
| 10158 |
#define GGML_F32X_STORE GGML_F32x16_STORE |
| 10159 |
#define GGML_F32X_MUL GGML_F32x16_MUL |
| 10160 |
#define GGML_F32X_FMA GGML_F32x16_FMA |
| 10161 |
#define GLA_VECTOR_SIZE 16 |
| 10162 |
#elif defined(__ARM_FEATURE_SVE) && defined(__aarch64__) |
| 10163 |
#define GGML_F32X GGML_F32xt |
| 10164 |
#define GGML_F32X_SET1 GGML_F32xt_SET1 |
| 10165 |
#define GGML_F32X_LOAD GGML_F32xt_LOAD |
| 10166 |
#define GGML_F32X_STORE GGML_F32xt_STORE |
| 10167 |
#define GGML_F32X_MUL GGML_F32xt_MUL |
| 10168 |
#define GGML_F32X_FMA GGML_F32xt_FMA |
| 10169 |
#define GLA_VECTOR_SIZE 8 |
| 10170 |
#elif defined(__ARM_NEON) && defined(__aarch64__) |
| 10171 |
#define GGML_F32X GGML_F32x4 |
| 10172 |
#define GGML_F32X_SET1 GGML_F32x4_SET1 |
| 10173 |
#define GGML_F32X_LOAD GGML_F32x4_LOAD |
| 10174 |
#define GGML_F32X_STORE GGML_F32x4_STORE |
| 10175 |
#define GGML_F32X_MUL GGML_F32x4_MUL |
| 10176 |
#define GGML_F32X_FMA GGML_F32x4_FMA |
| 10177 |
#define GLA_VECTOR_SIZE 4 |
| 10178 |
#endif |
| 10179 |
|
| 10180 |
#ifdef GLA_VECTOR_SIZE |
| 10181 |
int gla_vector_size; |
| 10182 |
#if defined(__ARM_FEATURE_SVE) |
| 10183 |
gla_vector_size = svcntw(); |
| 10184 |
#else |
| 10185 |
gla_vector_size = GLA_VECTOR_SIZE; |
| 10186 |
#endif |
| 10187 |
const int64_t vec_count = head_size / gla_vector_size; |
| 10188 |
|
| 10189 |
for (int64_t t = 0; t < T; t++) { |
| 10190 |
size_t t_offset = t * t_stride; |
| 10191 |
size_t state_offset = head_size * C * (t / (T / n_seqs)); |
| 10192 |
float * state_cur = state + state_offset; |
| 10193 |
float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[4]->data + state_offset; |
| 10194 |
|
| 10195 |
for (int64_t h = h_start; h < h_end; h++) { |
| 10196 |
size_t h_offset = h * h_stride; |
| 10197 |
size_t t_h_offset = t_offset + h_offset; |
| 10198 |
size_t h_2d_offset = h * h_stride_2d; |
| 10199 |
|
| 10200 |
for (int64_t i = 0; i < head_size; i++) { |
| 10201 |
size_t t_h_i_offset = t_h_offset + i; |
| 10202 |
size_t h_2d_i_offset = h_2d_offset + i * h_stride; |
| 10203 |
|
| 10204 |
float k_val = k[t_h_i_offset]; |
| 10205 |
float q_val = q[t_h_i_offset] * scale; |
| 10206 |
float g_val = g[t_h_i_offset]; |
| 10207 |
|
| 10208 |
// Broadcast scalar values to vectors |
| 10209 |
GGML_F32X k_vec = GGML_F32X_SET1(k_val); |
| 10210 |
GGML_F32X q_vec = GGML_F32X_SET1(q_val); |
| 10211 |
GGML_F32X g_vec = GGML_F32X_SET1(g_val); |
| 10212 |
|
| 10213 |
for (int64_t j = 0; j < vec_count; j++) { |
| 10214 |
size_t base_j = j * gla_vector_size; |
| 10215 |
size_t t_h_j_offset = t_h_offset + base_j; |
| 10216 |
size_t h_2d_i_j_offset = h_2d_i_offset + base_j; |
| 10217 |
|
| 10218 |
// Load x elements at once |
| 10219 |
GGML_F32X v_vec = GGML_F32X_LOAD(&v[t_h_j_offset]); |
| 10220 |
GGML_F32X prev_state_vec = GGML_F32X_LOAD(&state_prev[h_2d_i_j_offset]); |
| 10221 |
GGML_F32X dst_vec = GGML_F32X_LOAD(&dst_data[t_h_j_offset]); |
| 10222 |
|
| 10223 |
// Compute kv = v * k |
| 10224 |
GGML_F32X kv_vec = GGML_F32X_MUL(v_vec, k_vec); |
| 10225 |
|
| 10226 |
// Compute temp = prev_state * g + kv |
| 10227 |
GGML_F32X temp_vec = GGML_F32X_FMA(kv_vec, prev_state_vec, g_vec); |
| 10228 |
|
| 10229 |
// Update dst: dst += temp * q |
| 10230 |
dst_vec = GGML_F32X_FMA(dst_vec, temp_vec, q_vec); |
| 10231 |
GGML_F32X_STORE(&dst_data[t_h_j_offset], dst_vec); |
| 10232 |
|
| 10233 |
// Update state |
| 10234 |
GGML_F32X_STORE(&state_cur[h_2d_i_j_offset], temp_vec); |
| 10235 |
} |
| 10236 |
|
| 10237 |
// Handle remaining elements, this will not be used. |
| 10238 |
for (int64_t j = vec_count * gla_vector_size; j < head_size; j++) { |
| 10239 |
size_t t_h_j_offset = t_h_offset + j; |
| 10240 |
size_t h_2d_i_j_offset = h_2d_i_offset + j; |
| 10241 |
float v_val = v[t_h_j_offset]; |
| 10242 |
float kv_val = v_val * k_val; |
| 10243 |
float prev_state_val = state_prev[h_2d_i_j_offset]; |
| 10244 |
float temp_val = kv_val + prev_state_val * g_val; |
| 10245 |
dst_data[t_h_j_offset] += temp_val * q_val; |
| 10246 |
state_cur[h_2d_i_j_offset] = temp_val; |
| 10247 |
} |
| 10248 |
} |
| 10249 |
} |
| 10250 |
} |
| 10251 |
|
| 10252 |
#else |
| 10253 |
for (int64_t t = 0; t < T; t++) { |
| 10254 |
size_t t_offset = t * t_stride; |
| 10255 |
size_t state_offset = head_size * C * (t / (T / n_seqs)); |
| 10256 |
float * state_cur = state + state_offset; |
| 10257 |
float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[4]->data + state_offset; |
| 10258 |
|
| 10259 |
for (int64_t h = h_start; h < h_end; h++) { |
| 10260 |
size_t h_offset = h * h_stride; |
| 10261 |
size_t t_h_offset = t_offset + h_offset; |
| 10262 |
size_t h_2d_offset = h * h_stride_2d; |
| 10263 |
|
| 10264 |
for (int64_t i = 0; i < head_size; i++) { |
| 10265 |
size_t t_h_i_offset = t_h_offset + i; |
| 10266 |
size_t h_2d_i_offset = h_2d_offset + i * h_stride; |
| 10267 |
|
| 10268 |
float k_val = k[t_h_i_offset]; |
| 10269 |
float q_val = q[t_h_i_offset] * scale; |
| 10270 |
float g_val = g[t_h_i_offset]; |
| 10271 |
|
| 10272 |
for (int64_t j = 0; j < head_size; j++) { |
| 10273 |
size_t t_h_j_offset = t_h_offset + j; |
| 10274 |
size_t h_2d_i_j_offset = h_2d_i_offset + j; |
| 10275 |
|
| 10276 |
float v_val = v[t_h_j_offset]; |
| 10277 |
float kv_val = v_val * k_val; |
| 10278 |
float prev_state_val = state_prev[h_2d_i_j_offset]; |
| 10279 |
float temp_val = prev_state_val * g_val + kv_val; |
| 10280 |
dst_data[t_h_j_offset] += temp_val * q_val; |
| 10281 |
state_cur[h_2d_i_j_offset] = temp_val; |
| 10282 |
} |
| 10283 |
} |
| 10284 |
} |
| 10285 |
} |
| 10286 |
#endif |
| 10287 |
} |
| 10288 |
|
| 10289 |
|
| 10290 |
void ggml_compute_forward_gla( |
| 10291 |
const ggml_compute_params * params, |
| 10292 |
ggml_tensor * dst) { |
| 10293 |
|
| 10294 |
const ggml_tensor * src0 = dst->src[0]; |
| 10295 |
|
| 10296 |
switch (src0->type) { |
| 10297 |
case GGML_TYPE_F32: |
| 10298 |
{ |
| 10299 |
ggml_compute_forward_gla_f32(params, dst); |
| 10300 |
} break; |
| 10301 |
default: |
| 10302 |
{ |
| 10303 |
GGML_ABORT("fatal error"); |
| 10304 |
} |
| 10305 |
} |
| 10306 |
} |
| 10307 |
|
| 10308 |
static void ggml_compute_forward_solve_tri_f32(const struct ggml_compute_params * params, struct ggml_tensor * dst) { |
| 10309 |
const struct ggml_tensor * src0 = dst->src[0]; // A (lower triangular) |
| 10310 |
const struct ggml_tensor * src1 = dst->src[1]; // B (RHS) |
| 10311 |
|
| 10312 |
GGML_TENSOR_BINARY_OP_LOCALS; |
| 10313 |
|
| 10314 |
GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 10315 |
GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 10316 |
GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 10317 |
|
| 10318 |
GGML_ASSERT(ne00 == ne01); // A must be square |
| 10319 |
GGML_ASSERT(ne0 == ne10); // solution cols == B cols |
| 10320 |
GGML_ASSERT(ne1 == ne11); // solution rows == B rows |
| 10321 |
|
| 10322 |
GGML_ASSERT(ne02 == ne12 && ne12 == ne2); |
| 10323 |
GGML_ASSERT(ne03 == ne13 && ne13 == ne3); |
| 10324 |
|
| 10325 |
const int ith = params->ith; |
| 10326 |
const int nth = params->nth; |
| 10327 |
|
| 10328 |
const int64_t k = ne10; // number of RHS columns |
| 10329 |
const int64_t n = ne11; // A is n×n |
| 10330 |
const int64_t nr = ne02 * ne03 * k; // we're parallelizing on columns here, so seq x token x column will be the unit |
| 10331 |
|
| 10332 |
// chunks per thread |
| 10333 |
const int64_t dr = (nr + nth - 1)/nth; |
| 10334 |
|
| 10335 |
// chunk range for this thread |
| 10336 |
const int64_t ir0 = dr*ith; |
| 10337 |
const int64_t ir1 = MIN(ir0 + dr, nr); |
| 10338 |
|
| 10339 |
const float * A = (const float *) src0->data; // [n, n, B1, B2] |
| 10340 |
const float * B = (const float *) src1->data; // [n, k, B1, B2] |
| 10341 |
float * X = ( float *) dst->data; // [n, k, B1, B2] |
| 10342 |
|
| 10343 |
for (int64_t ir = ir0; ir < ir1; ++ir) { |
| 10344 |
const int64_t i03 = ir/(ne02*k); |
| 10345 |
const int64_t i02 = (ir - i03*ne02*k)/k; |
| 10346 |
const int64_t i01 = (ir - i03*ne02*k - i02*k); |
| 10347 |
|
| 10348 |
const float * A_batch = A + i02 * nb02 / sizeof(float) + i03 * nb03 / sizeof(float); |
| 10349 |
const float * B_batch = B + i02 * nb12 / sizeof(float) + i03 * nb13 / sizeof(float); |
| 10350 |
|
| 10351 |
float * X_batch = X + i02 * nb2 / sizeof(float) + i03 * nb3 / sizeof(float); |
| 10352 |
|
| 10353 |
for (int64_t i00 = 0; i00 < n; ++i00) { |
| 10354 |
float sum = 0.0f; |
| 10355 |
for (int64_t t = 0; t < i00; ++t) { |
| 10356 |
sum += A_batch[i00 * n + t] * X_batch[t * k + i01]; |
| 10357 |
} |
| 10358 |
|
| 10359 |
const float diag = A_batch[i00 * n + i00]; |
| 10360 |
assert(diag != 0.0f && "Zero diagonal in triangular matrix"); |
| 10361 |
|
| 10362 |
X_batch[i00 * k + i01] = (B_batch[i00 * k + i01] - sum) / diag; |
| 10363 |
} |
| 10364 |
} |
| 10365 |
} |
| 10366 |
|
| 10367 |
void ggml_compute_forward_solve_tri(const struct ggml_compute_params * params, struct ggml_tensor * dst) { |
| 10368 |
const ggml_tensor * src0 = dst->src[0]; |
| 10369 |
const ggml_tensor * src1 = dst->src[1]; |
| 10370 |
|
| 10371 |
if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) { |
| 10372 |
ggml_compute_forward_solve_tri_f32(params, dst); |
| 10373 |
} else { |
| 10374 |
GGML_ABORT("fatal error"); |
| 10375 |
} |
| 10376 |
} |
| 10377 |
|
| 10378 |
// ggml_compute_forward_rwkv_wkv7 |
| 10379 |
|
| 10380 |
static void ggml_compute_forward_rwkv_wkv7_f32( |
| 10381 |
const ggml_compute_params * params, |
| 10382 |
ggml_tensor * dst) { |
| 10383 |
const int64_t T = dst->src[1]->ne[2]; |
| 10384 |
const int64_t C = dst->ne[0]; |
| 10385 |
const int64_t HEADS = dst->src[1]->ne[1]; |
| 10386 |
const int64_t n_seqs = dst->src[6]->ne[1]; |
| 10387 |
const int64_t head_size = C / HEADS; |
| 10388 |
|
| 10389 |
float * dst_data = (float *) dst->data; |
| 10390 |
float * state = ((float *) dst->data) + C * T; |
| 10391 |
|
| 10392 |
const int ith = params->ith; |
| 10393 |
const int nth = params->nth; |
| 10394 |
|
| 10395 |
if (ith >= HEADS) { |
| 10396 |
return; |
| 10397 |
} |
| 10398 |
|
| 10399 |
const int h_start = (HEADS * ith) / nth; |
| 10400 |
const int h_end = ((HEADS * (ith + 1)) / nth < HEADS) ? |
| 10401 |
(HEADS * (ith + 1)) / nth : HEADS; |
| 10402 |
|
| 10403 |
float * r = (float *) dst->src[0]->data; |
| 10404 |
float * w = (float *) dst->src[1]->data; |
| 10405 |
float * k = (float *) dst->src[2]->data; |
| 10406 |
float * v = (float *) dst->src[3]->data; |
| 10407 |
float * a = (float *) dst->src[4]->data; |
| 10408 |
float * b = (float *) dst->src[5]->data; |
| 10409 |
|
| 10410 |
int64_t t_stride = HEADS * head_size; // Same to C |
| 10411 |
|
| 10412 |
int64_t h_stride = C / HEADS; |
| 10413 |
GGML_ASSERT(C % HEADS == 0); // C must be divisible by HEADS |
| 10414 |
int64_t h_stride_2d = head_size * head_size; |
| 10415 |
|
| 10416 |
#if defined(GGML_SIMD) |
| 10417 |
#if defined(__ARM_FEATURE_SVE) || defined(__riscv_v_intrinsic) |
| 10418 |
// scalar Route to scalar implementation //TODO: Write SVE code and RVV code |
| 10419 |
for (int64_t t = 0; t < T; t++) { |
| 10420 |
int64_t t_offset = t * t_stride; |
| 10421 |
int64_t state_offset = head_size * C * (t / (T / n_seqs)); |
| 10422 |
float * state_cur = state + state_offset; |
| 10423 |
float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[6]->data + state_offset; |
| 10424 |
|
| 10425 |
for (int64_t h = h_start; h < h_end; h++) { |
| 10426 |
int64_t h_offset = h * h_stride; |
| 10427 |
int64_t t_h_offset = t_offset + h_offset; |
| 10428 |
int64_t h_2d_offset = h * h_stride_2d; |
| 10429 |
|
| 10430 |
for (int64_t i = 0; i < head_size; i++) { |
| 10431 |
int64_t t_h_i_offset = t_h_offset + i; |
| 10432 |
int64_t h_2d_i_offset = h_2d_offset + i * h_stride; |
| 10433 |
|
| 10434 |
float v_val = v[t_h_i_offset]; |
| 10435 |
|
| 10436 |
float sa = 0, result = 0; |
| 10437 |
for (int64_t j = 0; j < head_size; j++) { |
| 10438 |
sa += a[t_h_offset + j] * state_prev[h_2d_i_offset + j]; |
| 10439 |
} |
| 10440 |
|
| 10441 |
for (int64_t j = 0; j < head_size; j++) { |
| 10442 |
int64_t t_h_j_offset = t_h_offset + j; |
| 10443 |
int64_t h_2d_i_j_offset = h_2d_i_offset + j; |
| 10444 |
|
| 10445 |
float r_val = r[t_h_j_offset]; |
| 10446 |
float w_val = w[t_h_j_offset]; |
| 10447 |
float k_val = k[t_h_j_offset]; |
| 10448 |
float b_val = b[t_h_j_offset]; |
| 10449 |
float kv_val = v_val * k_val; |
| 10450 |
float prev_state_val = state_prev[h_2d_i_j_offset]; |
| 10451 |
state_cur[h_2d_i_j_offset] = prev_state_val * w_val + kv_val + sa * b_val; |
| 10452 |
result += state_cur[h_2d_i_j_offset] * r_val; |
| 10453 |
} |
| 10454 |
dst_data[t_h_i_offset] = result; |
| 10455 |
} |
| 10456 |
} |
| 10457 |
} |
| 10458 |
#else |
| 10459 |
for (int64_t t = 0; t < T; t++) { |
| 10460 |
int64_t t_offset = t * t_stride; |
| 10461 |
int64_t state_offset = head_size * C * (t / (T / n_seqs)); |
| 10462 |
float * state_cur = state + state_offset; |
| 10463 |
float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[6]->data + state_offset; |
| 10464 |
|
| 10465 |
for (int64_t h = h_start; h < h_end; h++) { |
| 10466 |
int64_t h_offset = h * h_stride; |
| 10467 |
int64_t t_h_offset = t_offset + h_offset; |
| 10468 |
int64_t h_2d_offset = h * h_stride_2d; |
| 10469 |
|
| 10470 |
for (int64_t ii = 0; ii < head_size; ii++) { |
| 10471 |
int64_t t_h_i_offset = t_h_offset + ii; |
| 10472 |
int64_t h_2d_i_offset = h_2d_offset + ii * h_stride; |
| 10473 |
|
| 10474 |
GGML_F32_VEC v_vec = GGML_F32_VEC_SET1(v[t_h_i_offset]); |
| 10475 |
|
| 10476 |
float sa = 0; |
| 10477 |
{ |
| 10478 |
GGML_F32_VEC sum[GGML_F32_ARR] = { GGML_F32_VEC_ZERO }; |
| 10479 |
GGML_F32_VEC ax[GGML_F32_ARR]; |
| 10480 |
GGML_F32_VEC ay[GGML_F32_ARR]; |
| 10481 |
for (int64_t j = 0; j < head_size; j += GGML_F32_STEP) { |
| 10482 |
for (int64_t kk = 0; kk < GGML_F32_ARR; kk++) { |
| 10483 |
ax[kk] = GGML_F32_VEC_LOAD(&a[t_h_offset + j + kk * GGML_F32_EPR]); |
| 10484 |
ay[kk] = GGML_F32_VEC_LOAD(&state_prev[h_2d_i_offset + j + kk * GGML_F32_EPR]); |
| 10485 |
sum[kk] = GGML_F32_VEC_FMA(sum[kk], ax[kk], ay[kk]); |
| 10486 |
} |
| 10487 |
} |
| 10488 |
GGML_F32_VEC_REDUCE(sa, sum); |
| 10489 |
} |
| 10490 |
|
| 10491 |
GGML_F32_VEC sa_vec = GGML_F32_VEC_SET1(sa); |
| 10492 |
|
| 10493 |
int64_t j = 0; |
| 10494 |
GGML_F32_VEC result_vec[GGML_F32_ARR] = { GGML_F32_VEC_ZERO }; |
| 10495 |
for (; j < head_size; j += GGML_F32_STEP) { |
| 10496 |
for (int64_t kk = 0; kk < GGML_F32_ARR; kk++) { |
| 10497 |
int64_t t_h_j_offset = t_h_offset + j + kk * GGML_F32_EPR; |
| 10498 |
int64_t h_2d_i_j_offset = h_2d_i_offset + j + kk * GGML_F32_EPR; |
| 10499 |
|
| 10500 |
GGML_F32_VEC r_vec = GGML_F32_VEC_LOAD(&r[t_h_j_offset]); |
| 10501 |
GGML_F32_VEC w_vec = GGML_F32_VEC_LOAD(&w[t_h_j_offset]); |
| 10502 |
GGML_F32_VEC k_vec = GGML_F32_VEC_LOAD(&k[t_h_j_offset]); |
| 10503 |
GGML_F32_VEC b_vec = GGML_F32_VEC_LOAD(&b[t_h_j_offset]); |
| 10504 |
|
| 10505 |
k_vec = GGML_F32_VEC_MUL(v_vec, k_vec); |
| 10506 |
|
| 10507 |
GGML_F32_VEC state_vec = GGML_F32_VEC_LOAD(&state_prev[h_2d_i_j_offset]); |
| 10508 |
// kv + s * decay + sa * b |
| 10509 |
state_vec = GGML_F32_VEC_FMA(k_vec, state_vec, w_vec); |
| 10510 |
state_vec = GGML_F32_VEC_FMA(state_vec, sa_vec, b_vec); |
| 10511 |
GGML_F32_VEC_STORE(&state_cur[h_2d_i_j_offset], state_vec); |
| 10512 |
|
| 10513 |
result_vec[kk] = GGML_F32_VEC_FMA(result_vec[kk], state_vec, r_vec); |
| 10514 |
} |
| 10515 |
} |
| 10516 |
GGML_F32_VEC_REDUCE(dst_data[t_h_i_offset], result_vec); |
| 10517 |
|
| 10518 |
// There shouldn't be left-overs though. |
| 10519 |
for (; j < head_size; j++) { |
| 10520 |
int64_t t_h_j_offset = t_h_offset + j; |
| 10521 |
int64_t h_2d_i_j_offset = h_2d_i_offset + j; |
| 10522 |
|
| 10523 |
float r_val = r[t_h_j_offset]; |
| 10524 |
float w_val = w[t_h_j_offset]; |
| 10525 |
float k_val = k[t_h_j_offset]; |
| 10526 |
float b_val = b[t_h_j_offset]; |
| 10527 |
float kv_val = v[t_h_i_offset] * k_val; |
| 10528 |
|
| 10529 |
float prev_state_val = state_prev[h_2d_i_j_offset]; |
| 10530 |
state_cur[h_2d_i_j_offset] = prev_state_val * w_val + kv_val + sa * b_val; |
| 10531 |
dst_data[t_h_i_offset] += state_cur[h_2d_i_j_offset] * r_val; |
| 10532 |
} |
| 10533 |
} |
| 10534 |
} |
| 10535 |
} |
| 10536 |
#endif |
| 10537 |
#else |
| 10538 |
for (int64_t t = 0; t < T; t++) { |
| 10539 |
int64_t t_offset = t * t_stride; |
| 10540 |
int64_t state_offset = head_size * C * (t / (T / n_seqs)); |
| 10541 |
float * state_cur = state + state_offset; |
| 10542 |
float * state_prev = t % (T / n_seqs) ? state_cur : (float*)dst->src[6]->data + state_offset; |
| 10543 |
|
| 10544 |
for (int64_t h = h_start; h < h_end; h++) { |
| 10545 |
int64_t h_offset = h * h_stride; |
| 10546 |
int64_t t_h_offset = t_offset + h_offset; |
| 10547 |
int64_t h_2d_offset = h * h_stride_2d; |
| 10548 |
|
| 10549 |
for (int64_t i = 0; i < head_size; i++) { |
| 10550 |
int64_t t_h_i_offset = t_h_offset + i; |
| 10551 |
int64_t h_2d_i_offset = h_2d_offset + i * h_stride; |
| 10552 |
|
| 10553 |
float v_val = v[t_h_i_offset]; |
| 10554 |
|
| 10555 |
float sa = 0, result = 0; |
| 10556 |
for (int64_t j = 0; j < head_size; j++) { |
| 10557 |
sa += a[t_h_offset + j] * state_prev[h_2d_i_offset + j]; |
| 10558 |
} |
| 10559 |
|
| 10560 |
for (int64_t j = 0; j < head_size; j++) { |
| 10561 |
int64_t t_h_j_offset = t_h_offset + j; |
| 10562 |
int64_t h_2d_i_j_offset = h_2d_i_offset + j; |
| 10563 |
|
| 10564 |
float r_val = r[t_h_j_offset]; |
| 10565 |
float w_val = w[t_h_j_offset]; |
| 10566 |
float k_val = k[t_h_j_offset]; |
| 10567 |
float b_val = b[t_h_j_offset]; |
| 10568 |
float kv_val = v_val * k_val; |
| 10569 |
float prev_state_val = state_prev[h_2d_i_j_offset]; |
| 10570 |
state_cur[h_2d_i_j_offset] = prev_state_val * w_val + kv_val + sa * b_val; |
| 10571 |
result += state_cur[h_2d_i_j_offset] * r_val; |
| 10572 |
} |
| 10573 |
dst_data[t_h_i_offset] = result; |
| 10574 |
} |
| 10575 |
} |
| 10576 |
} |
| 10577 |
#endif |
| 10578 |
} |
| 10579 |
|
| 10580 |
|
| 10581 |
void ggml_compute_forward_rwkv_wkv7( |
| 10582 |
const ggml_compute_params * params, |
| 10583 |
ggml_tensor * dst) { |
| 10584 |
|
| 10585 |
const ggml_tensor * src0 = dst->src[0]; |
| 10586 |
|
| 10587 |
switch (src0->type) { |
| 10588 |
case GGML_TYPE_F32: |
| 10589 |
{ |
| 10590 |
ggml_compute_forward_rwkv_wkv7_f32(params, dst); |
| 10591 |
} break; |
| 10592 |
default: |
| 10593 |
{ |
| 10594 |
GGML_ABORT("fatal error"); |
| 10595 |
} |
| 10596 |
} |
| 10597 |
} |
| 10598 |
|
| 10599 |
// ggml_compute_forward_map_custom1 |
| 10600 |
|
| 10601 |
void ggml_compute_forward_map_custom1( |
| 10602 |
const ggml_compute_params * params, |
| 10603 |
ggml_tensor * dst) { |
| 10604 |
|
| 10605 |
const ggml_tensor * a = dst->src[0]; |
| 10606 |
|
| 10607 |
struct ggml_map_custom1_op_params p; |
| 10608 |
memcpy(&p, dst->op_params, sizeof(p)); |
| 10609 |
|
| 10610 |
p.fun(dst, a, params->ith, params->nth, p.userdata); |
| 10611 |
} |
| 10612 |
|
| 10613 |
// ggml_compute_forward_map_custom2 |
| 10614 |
|
| 10615 |
void ggml_compute_forward_map_custom2( |
| 10616 |
const ggml_compute_params * params, |
| 10617 |
ggml_tensor * dst) { |
| 10618 |
|
| 10619 |
const ggml_tensor * a = dst->src[0]; |
| 10620 |
const ggml_tensor * b = dst->src[1]; |
| 10621 |
|
| 10622 |
struct ggml_map_custom2_op_params p; |
| 10623 |
memcpy(&p, dst->op_params, sizeof(p)); |
| 10624 |
|
| 10625 |
p.fun(dst, a, b, params->ith, params->nth, p.userdata); |
| 10626 |
} |
| 10627 |
|
| 10628 |
// ggml_compute_forward_map_custom3 |
| 10629 |
|
| 10630 |
void ggml_compute_forward_map_custom3( |
| 10631 |
const ggml_compute_params * params, |
| 10632 |
ggml_tensor * dst) { |
| 10633 |
|
| 10634 |
const ggml_tensor * a = dst->src[0]; |
| 10635 |
const ggml_tensor * b = dst->src[1]; |
| 10636 |
const ggml_tensor * c = dst->src[2]; |
| 10637 |
|
| 10638 |
struct ggml_map_custom3_op_params p; |
| 10639 |
memcpy(&p, dst->op_params, sizeof(p)); |
| 10640 |
|
| 10641 |
p.fun(dst, a, b, c, params->ith, params->nth, p.userdata); |
| 10642 |
} |
| 10643 |
|
| 10644 |
// ggml_compute_forward_custom |
| 10645 |
|
| 10646 |
void ggml_compute_forward_custom( |
| 10647 |
const struct ggml_compute_params * params, |
| 10648 |
struct ggml_tensor * dst) { |
| 10649 |
|
| 10650 |
struct ggml_custom_op_params p; |
| 10651 |
memcpy(&p, dst->op_params, sizeof(p)); |
| 10652 |
|
| 10653 |
p.fun(dst, params->ith, params->nth, p.userdata); |
| 10654 |
} |
| 10655 |
|
| 10656 |
// ggml_compute_forward_cross_entropy_loss |
| 10657 |
|
| 10658 |
static void ggml_compute_forward_cross_entropy_loss_f32( |
| 10659 |
const ggml_compute_params * params, |
| 10660 |
ggml_tensor * dst) { |
| 10661 |
|
| 10662 |
const ggml_tensor * src0 = dst->src[0]; |
| 10663 |
const ggml_tensor * src1 = dst->src[1]; |
| 10664 |
|
| 10665 |
GGML_ASSERT(src0->type == GGML_TYPE_F32); |
| 10666 |
GGML_ASSERT(src1->type == GGML_TYPE_F32); |
| 10667 |
GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type)); |
| 10668 |
GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type)); |
| 10669 |
GGML_ASSERT(ggml_are_same_shape(src0, src1)); |
| 10670 |
GGML_ASSERT(ggml_is_scalar(dst)); |
| 10671 |
GGML_ASSERT(dst->type == GGML_TYPE_F32); |
| 10672 |
|
| 10673 |
// TODO: handle transposed/permuted matrices |
| 10674 |
const int64_t nc = src0->ne[0]; |
| 10675 |
const int64_t nr = ggml_nrows(src0); |
| 10676 |
|
| 10677 |
const int ith = params->ith; |
| 10678 |
const int nth = params->nth; |
| 10679 |
|
| 10680 |
float * sums = (float *) params->wdata; |
| 10681 |
float * st = ((float *) params->wdata) + nth + ith*nc; |
| 10682 |
float sum_thread = 0.0f; |
| 10683 |
|
| 10684 |
GGML_ASSERT(params->wsize >= sizeof(float) * (nth + nth * nc)); |
| 10685 |
|
| 10686 |
// rows per thread |
| 10687 |
const int64_t dr = (nr + nth - 1)/nth; |
| 10688 |
|
| 10689 |
// row range for this thread |
| 10690 |
const int64_t ir0 = dr*ith; |
| 10691 |
const int64_t ir1 = MIN(ir0 + dr, nr); |
| 10692 |
|
| 10693 |
for (int64_t i1 = ir0; i1 < ir1; ++i1) { |
| 10694 |
const float * s0 = (const float *)((const char *) src0->data + i1*src0->nb[1]); |
| 10695 |
const float * s1 = (const float *)((const char *) src1->data + i1*src1->nb[1]); |
| 10696 |
|
| 10697 |
#ifndef NDEBUG |
| 10698 |
for (int64_t i = 0; i < nc; ++i) { |
| 10699 |
//printf("p[%d] = %f\n", i, p[i]); |
| 10700 |
assert(!isnan(s0[i])); |
| 10701 |
assert(!isnan(s1[i])); |
| 10702 |
} |
| 10703 |
#endif |
| 10704 |
|
| 10705 |
float max = -INFINITY; |
| 10706 |
ggml_vec_max_f32(nc, &max, s0); |
| 10707 |
const ggml_float sum_softmax = ggml_vec_log_soft_max_f32(nc, st, s0, max); |
| 10708 |
assert(sum_softmax >= 0.0); |
| 10709 |
|
| 10710 |
ggml_vec_add1_f32(nc, st, st, -sum_softmax); |
| 10711 |
ggml_vec_mul_f32(nc, st, st, s1); |
| 10712 |
|
| 10713 |
float sum_st = 0.0f; |
| 10714 |
ggml_vec_sum_f32(nc, &sum_st, st); |
| 10715 |
sum_thread += sum_st; |
| 10716 |
|
| 10717 |
#ifndef NDEBUG |
| 10718 |
for (int64_t i = 0; i < nc; ++i) { |
| 10719 |
assert(!isnan(st[i])); |
| 10720 |
assert(!isinf(st[i])); |
| 10721 |
} |
| 10722 |
#endif |
| 10723 |
} |
| 10724 |
sums[ith] = sum_thread; |
| 10725 |
ggml_barrier(params->threadpool); |
| 10726 |
|
| 10727 |
if (ith == 0) { |
| 10728 |
float * dp = (float *) dst->data; |
| 10729 |
ggml_vec_sum_f32(nth, dp, sums); |
| 10730 |
dp[0] *= -1.0f / (float) nr; |
| 10731 |
} |
| 10732 |
} |
| 10733 |
|
| 10734 |
void ggml_compute_forward_cross_entropy_loss( |
| 10735 |
const ggml_compute_params * params, |
| 10736 |
ggml_tensor * dst) { |
| 10737 |
|
| 10738 |
const ggml_tensor * src0 = dst->src[0]; |
| 10739 |
|
| 10740 |
switch (src0->type) { |
| 10741 |
case GGML_TYPE_F32: |
| 10742 |
{ |
| 10743 |
ggml_compute_forward_cross_entropy_loss_f32(params, dst); |
| 10744 |
} break; |
| 10745 |
default: |
| 10746 |
{ |
| 10747 |
GGML_ABORT("fatal error"); |
| 10748 |
} |
| 10749 |
} |
| 10750 |
} |
| 10751 |
|
| 10752 |
// ggml_compute_forward_cross_entropy_loss_back |
| 10753 |
|
| 10754 |
static void ggml_compute_forward_cross_entropy_loss_back_f32( |
| 10755 |
const ggml_compute_params * params, |
| 10756 |
ggml_tensor * dst) { |
| 10757 |
|
| 10758 |
const ggml_tensor * grad = dst->src[0]; // gradient of forward pass output |
| 10759 |
const ggml_tensor * src0f = dst->src[1]; // src0 of forward pass |
| 10760 |
const ggml_tensor * src1f = dst->src[2]; // src1 of forward pass |
| 10761 |
|
| 10762 |
GGML_ASSERT(ggml_is_contiguous(dst)); |
| 10763 |
GGML_ASSERT(ggml_is_contiguous(src0f)); |
| 10764 |
GGML_ASSERT(ggml_is_contiguous(src1f)); |
| 10765 |
GGML_ASSERT(ggml_is_contiguous(grad)); |
| 10766 |
GGML_ASSERT(ggml_are_same_shape(src0f, src1f) && ggml_are_same_shape(src0f, dst)); |
| 10767 |
|
| 10768 |
const int64_t ith = params->ith; |
| 10769 |
const int64_t nth = params->nth; |
| 10770 |
|
| 10771 |
// TODO: handle transposed/permuted matrices |
| 10772 |
const int64_t nc = src0f->ne[0]; |
| 10773 |
const int64_t nr = ggml_nrows(src0f); |
| 10774 |
|
| 10775 |
// rows per thread |
| 10776 |
const int64_t dr = (nr + nth - 1)/nth; |
| 10777 |
|
| 10778 |
// row range for this thread |
| 10779 |
const int64_t ir0 = dr*ith; |
| 10780 |
const int64_t ir1 = MIN(ir0 + dr, nr); |
| 10781 |
|
| 10782 |
const float d_by_nr = ((const float *) grad->data)[0] / (float) nr; |
| 10783 |
|
| 10784 |
for (int64_t i1 = ir0; i1 < ir1; i1++) { |
| 10785 |
float * ds0 = (float *)((char *) dst->data + i1*dst->nb[1]); |
| 10786 |
const float * s0 = (const float *)((const char *) src0f->data + i1*src0f->nb[1]); |
| 10787 |
const float * s1 = (const float *)((const char *) src1f->data + i1*src1f->nb[1]); |
| 10788 |
|
| 10789 |
#ifndef NDEBUG |
| 10790 |
for (int64_t i = 0; i < nc; ++i) { |
| 10791 |
//printf("p[%d] = %f\n", i, p[i]); |
| 10792 |
assert(!isnan(s0[i])); |
| 10793 |
assert(!isnan(s1[i])); |
| 10794 |
} |
| 10795 |
#endif |
| 10796 |
|
| 10797 |
// soft_max |
| 10798 |
float max = -INFINITY; |
| 10799 |
ggml_vec_max_f32(nc, &max, s0); |
| 10800 |
const ggml_float sum = ggml_vec_soft_max_f32(nc, ds0, s0, max); |
| 10801 |
assert(sum > 0.0); |
| 10802 |
ggml_vec_scale_f32(nc, ds0, 1.0/sum); |
| 10803 |
|
| 10804 |
// grad(src0f) = (softmax(src0f) - src1f) * grad(cross_entropy_loss(src0f, src1f)) / nr |
| 10805 |
ggml_vec_sub_f32(nc, ds0, ds0, s1); |
| 10806 |
ggml_vec_scale_f32(nc, ds0, d_by_nr); |
| 10807 |
|
| 10808 |
#ifndef NDEBUG |
| 10809 |
for (int64_t i = 0; i < nc; ++i) { |
| 10810 |
assert(!isnan(ds0[i])); |
| 10811 |
assert(!isinf(ds0[i])); |
| 10812 |
} |
| 10813 |
#endif |
| 10814 |
} |
| 10815 |
} |
| 10816 |
|
| 10817 |
void ggml_compute_forward_cross_entropy_loss_back( |
| 10818 |
const ggml_compute_params * params, |
| 10819 |
ggml_tensor * dst) { |
| 10820 |
|
| 10821 |
const ggml_tensor * src0 = dst->src[0]; |
| 10822 |
|
| 10823 |
switch (src0->type) { |
| 10824 |
case GGML_TYPE_F32: |
| 10825 |
{ |
| 10826 |
ggml_compute_forward_cross_entropy_loss_back_f32(params, dst); |
| 10827 |
} break; |
| 10828 |
default: |
| 10829 |
{ |
| 10830 |
GGML_ABORT("fatal error"); |
| 10831 |
} |
| 10832 |
} |
| 10833 |
} |
| 10834 |
|
| 10835 |
static void ggml_compute_forward_opt_step_adamw_f32( |
| 10836 |
const ggml_compute_params * params, |
| 10837 |
ggml_tensor * dst) { |
| 10838 |
|
| 10839 |
const ggml_tensor * src0 = dst->src[0]; |
| 10840 |
const ggml_tensor * src0_grad = dst->src[1]; |
| 10841 |
const ggml_tensor * src0_grad_m = dst->src[2]; |
| 10842 |
const ggml_tensor * src0_grad_v = dst->src[3]; |
| 10843 |
const ggml_tensor * adamw_params = dst->src[4]; |
| 10844 |
|
| 10845 |
GGML_ASSERT(ggml_are_same_shape(src0, src0_grad)); |
| 10846 |
GGML_ASSERT(ggml_are_same_shape(src0, src0_grad_m)); |
| 10847 |
GGML_ASSERT(ggml_are_same_shape(src0, src0_grad_v)); |
| 10848 |
GGML_ASSERT(ggml_nelements(adamw_params) == 7); |
| 10849 |
|
| 10850 |
const int ith = params->ith; |
| 10851 |
const int nth = params->nth; |
| 10852 |
|
| 10853 |
const int nr = ggml_nrows(src0); |
| 10854 |
|
| 10855 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 10856 |
GGML_ASSERT(nb00 == sizeof(float)); |
| 10857 |
|
| 10858 |
// rows per thread |
| 10859 |
const int dr = (nr + nth - 1)/nth; |
| 10860 |
|
| 10861 |
// row range for this thread |
| 10862 |
const int ir0 = dr*ith; |
| 10863 |
const int ir1 = MIN(ir0 + dr, nr); |
| 10864 |
|
| 10865 |
const float * adamw_params_ptr = ggml_get_data_f32(adamw_params); |
| 10866 |
|
| 10867 |
const float alpha = adamw_params_ptr[0]; |
| 10868 |
const float beta1 = adamw_params_ptr[1]; |
| 10869 |
const float beta2 = adamw_params_ptr[2]; |
| 10870 |
const float eps = adamw_params_ptr[3]; |
| 10871 |
const float wd = adamw_params_ptr[4]; |
| 10872 |
const float beta1h = adamw_params_ptr[5]; |
| 10873 |
const float beta2h = adamw_params_ptr[6]; |
| 10874 |
const float keep = 1.f - alpha * wd; |
| 10875 |
for (int ir = ir0; ir < ir1; ++ir) { |
| 10876 |
const int64_t i03 = ir/(ne02*ne01); |
| 10877 |
const int64_t i02 = (ir - i03*ne02*ne01)/ne01; |
| 10878 |
const int64_t i01 = (ir - i03*ne02*ne01 - i02*ne01); |
| 10879 |
|
| 10880 |
const size_t offset = i03*nb03 + i02*nb02 + i01*nb01; |
| 10881 |
|
| 10882 |
float * w = (float *) ((char *) src0->data + offset); // weight |
| 10883 |
const float * g = (const float *) ((const char *) src0_grad->data + offset); // grad |
| 10884 |
float * m = (float *) ((char *) src0_grad_m->data + offset); |
| 10885 |
float * v = (float *) ((char *) src0_grad_v->data + offset); |
| 10886 |
|
| 10887 |
for (int i00 = 0; i00 < ne00; ++i00) { |
| 10888 |
m[i00] = m[i00]*beta1 + g[i00]*(1.0f - beta1); |
| 10889 |
v[i00] = v[i00]*beta2 + g[i00]*g[i00]*(1.0f - beta2); |
| 10890 |
|
| 10891 |
const float mh = m[i00]*beta1h; |
| 10892 |
const float vh = sqrtf(v[i00]*beta2h) + eps; |
| 10893 |
|
| 10894 |
// The weight decay is applied independently of the Adam momenta m and v. |
| 10895 |
// This is NOT equivalent to l2 regularization that adds w[i00]*w[i00] to the loss. |
| 10896 |
// See: https://arxiv.org/pdf/1711.05101v3.pdf |
| 10897 |
w[i00] = w[i00] * keep - alpha * mh / vh; |
| 10898 |
} |
| 10899 |
} |
| 10900 |
} |
| 10901 |
|
| 10902 |
void ggml_compute_forward_opt_step_adamw( |
| 10903 |
const ggml_compute_params * params, |
| 10904 |
ggml_tensor * dst) { |
| 10905 |
|
| 10906 |
const ggml_tensor * src0 = dst->src[0]; |
| 10907 |
|
| 10908 |
switch (src0->type) { |
| 10909 |
case GGML_TYPE_F32: |
| 10910 |
{ |
| 10911 |
ggml_compute_forward_opt_step_adamw_f32(params, dst); |
| 10912 |
} break; |
| 10913 |
default: |
| 10914 |
{ |
| 10915 |
GGML_ABORT("fatal error"); |
| 10916 |
} |
| 10917 |
} |
| 10918 |
} |
| 10919 |
|
| 10920 |
static void ggml_compute_forward_opt_step_sgd_f32(const ggml_compute_params * params, ggml_tensor * dst) { |
| 10921 |
const ggml_tensor * src0 = dst->src[0]; |
| 10922 |
const ggml_tensor * src0_grad = dst->src[1]; |
| 10923 |
const ggml_tensor * sgd_params = dst->src[2]; |
| 10924 |
|
| 10925 |
GGML_ASSERT(ggml_are_same_shape(src0, src0_grad)); |
| 10926 |
GGML_ASSERT(ggml_nelements(sgd_params) == 2); |
| 10927 |
|
| 10928 |
const int ith = params->ith; |
| 10929 |
const int nth = params->nth; |
| 10930 |
|
| 10931 |
const int nr = ggml_nrows(src0); |
| 10932 |
|
| 10933 |
GGML_TENSOR_UNARY_OP_LOCALS |
| 10934 |
GGML_ASSERT(nb00 == sizeof(float)); |
| 10935 |
|
| 10936 |
// rows per thread |
| 10937 |
const int dr = (nr + nth - 1) / nth; |
| 10938 |
|
| 10939 |
// row range for this thread |
| 10940 |
const int ir0 = dr * ith; |
| 10941 |
const int ir1 = MIN(ir0 + dr, nr); |
| 10942 |
|
| 10943 |
// using adamw param subset we care about - alpha, wd - could have a separate struct |
| 10944 |
const float * sgd_params_ptr = ggml_get_data_f32(sgd_params); |
| 10945 |
const float alpha = sgd_params_ptr[0]; |
| 10946 |
const float keep = 1.f - alpha * sgd_params_ptr[1]; |
| 10947 |
|
| 10948 |
for (int ir = ir0; ir < ir1; ++ir) { |
| 10949 |
const int64_t i03 = ir / (ne02 * ne01); |
| 10950 |
const int64_t i02 = (ir - i03 * ne02 * ne01) / ne01; |
| 10951 |
const int64_t i01 = (ir - i03 * ne02 * ne01 - i02 * ne01); |
| 10952 |
|
| 10953 |
const size_t offset = i03 * nb03 + i02 * nb02 + i01 * nb01; |
| 10954 |
|
| 10955 |
float * w = (float *) ((char *) src0->data + offset); // weight |
| 10956 |
const float * g = (const float *) ((const char *) src0_grad->data + offset); // grad |
| 10957 |
|
| 10958 |
for (int i00 = 0; i00 < ne00; ++i00) { |
| 10959 |
w[i00] = w[i00] * keep - alpha * g[i00]; |
| 10960 |
} |
| 10961 |
} |
| 10962 |
} |
| 10963 |
|
| 10964 |
void ggml_compute_forward_opt_step_sgd(const ggml_compute_params * params, ggml_tensor * dst) { |
| 10965 |
const ggml_tensor * src0 = dst->src[0]; |
| 10966 |
|
| 10967 |
switch (src0->type) { |
| 10968 |
case GGML_TYPE_F32: |
| 10969 |
{ |
| 10970 |
ggml_compute_forward_opt_step_sgd_f32(params, dst); |
| 10971 |
} |
| 10972 |
break; |
| 10973 |
default: |
| 10974 |
{ |
| 10975 |
GGML_ABORT("fatal error - sgd is F32 only"); |
| 10976 |
} |
| 10977 |
} |
| 10978 |
} |