char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
0190529ec
char/aliceai-t5-moe
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 17static void ggml_compute_forward_dup_same_cont ( 18const ggml_compute_params * params , 19ggml_tensor * dst ) { 20 21const ggml_tensor * src0 = dst -> src [0 ]; 22 23GGML_ASSERT (ggml_nelements (dst )== ggml_nelements (src0 )); 24GGML_ASSERT (ggml_is_contiguous (dst )&& ggml_is_contiguous (src0 )); 25GGML_ASSERT (src0 -> type == dst -> type ); 26 27const size_t nb0 = ggml_type_size (src0 -> type ); 28 29const int ith = params -> ith ;// thread index 30const int nth = params -> nth ;// number of threads 31 32// parallelize by blocks 33const int nk = ggml_nelements (src0 )/ggml_blck_size (src0 -> type ); 34const int dr = (nk + nth - 1 ) /nth ; 35const int k0 = dr * ith ; 36const int k1 = MIN (k0 + dr ,nk ); 37 38if (k0 < k1 ) { 39memcpy ( 40 ((char * )dst -> data + k0 * nb0 ), 41 ((char * )src0 -> data + k0 * nb0 ), 42 (k1 - k0 )* nb0 ); 43 } 44} 45 46template < typename src_t ,typename dst_t > 47static void ggml_compute_forward_dup_flt ( 48const ggml_compute_params * params , 49ggml_tensor * dst ) { 50 51const ggml_tensor * src0 = dst -> src [0 ]; 52 53GGML_ASSERT (ggml_nelements (dst )== ggml_nelements (src0 )); 54GGML_ASSERT (!ggml_is_quantized (src0 -> type )&& !ggml_is_quantized (dst -> type )); 55 56GGML_TENSOR_UNARY_OP_LOCALS 57 58const int ith = params -> ith ;// thread index 59const int nth = params -> nth ;// number of threads 60 61// parallelize by rows 62const int nr = ne01 ; 63// number of rows per thread 64const int dr = (nr + nth - 1 ) /nth ; 65// row range for this thread 66const int ir0 = dr * ith ; 67const int ir1 = MIN (ir0 + dr ,nr ); 68 69// case: type & row size equal 70if (src0 -> type == dst -> type && 71ne00 == ne0 && 72nb00 == ggml_type_size (src0 -> type )&& nb0 == ggml_type_size (dst -> type )) { 73// copy by rows 74const size_t rs = ne00 * nb00 ; 75for (int64_t i03 = 0 ;i03 < ne03 ;i03 ++ ) { 76for (int64_t i02 = 0 ;i02 < ne02 ;i02 ++ ) { 77for (int64_t i01 = ir0 ;i01 < ir1 ;i01 ++ ) { 78memcpy ( 79 ((char * )dst -> data + i01 * nb1 + i02 * nb2 + i03 * nb3 ), 80 ((char * )src0 -> data + i01 * nb01 + i02 * nb02 + i03 * nb03 ), 81rs ); 82 } 83 } 84 } 85return ; 86 } 87 88// case: dst tensor is contiguous 89if (ggml_is_contiguous (dst )) { 90if (nb00 == sizeof (src_t )) { 91if constexpr (std::is_same_v < dst_t ,src_t > ) { 92// same type 93size_t id = 0 ; 94const size_t rs = ne00 * nb00 ; 95char * dst_ptr = (char * )dst -> data ; 96 97for (int i03 = 0 ;i03 < ne03 ;i03 ++ ) { 98for (int i02 = 0 ;i02 < ne02 ;i02 ++ ) { 99id += rs * ir0 ; 100for (int i01 = ir0 ;i01 < ir1 ;i01 ++ ) { 101const char * src0_ptr = (char * )src0 -> data + i01 * nb01 + i02 * nb02 + i03 * nb03 ; 102memcpy (dst_ptr + id ,src0_ptr ,rs ); 103id += rs ; 104 } 105id += rs * (ne01 - ir1 ); 106 } 107 } 108 }else { 109// casting between non-quantized types 110size_t id = 0 ; 111dst_t * dst_ptr = (dst_t * )dst -> data ; 112 113for (int i03 = 0 ;i03 < ne03 ;i03 ++ ) { 114for (int i02 = 0 ;i02 < ne02 ;i02 ++ ) { 115id += ne00 * ir0 ; 116for (int i01 = ir0 ;i01 < ir1 ;i01 ++ ) { 117const src_t * src0_ptr = (src_t * ) ((char * )src0 -> data + i01 * nb01 + i02 * nb02 + i03 * nb03 ); 118for (int i00 = 0 ;i00 < ne00 ;i00 ++ ) { 119float tmp = type_conversion_table < src_t > ::to_f32 (src0_ptr [i00 ]); 120dst_ptr [id ]= type_conversion_table < dst_t > ::from_f32 (tmp ); 121id ++ ; 122 } 123 } 124id += ne00 * (ne01 - ir1 ); 125 } 126 } 127 } 128 }else { 129//printf("%s: this is not optimal - fix me\n", __func__); 130 131size_t id = 0 ; 132dst_t * dst_ptr = (dst_t * )dst -> data ; 133 134for (int i03 = 0 ;i03 < ne03 ;i03 ++ ) { 135for (int i02 = 0 ;i02 < ne02 ;i02 ++ ) { 136id += ne00 * ir0 ; 137for (int i01 = ir0 ;i01 < ir1 ;i01 ++ ) { 138for (int i00 = 0 ;i00 < ne00 ;i00 ++ ) { 139const src_t * src0_ptr = (src_t * ) ((char * )src0 -> data + i00 * nb00 + i01 * nb01 + i02 * nb02 + i03 * nb03 ); 140 141float tmp = type_conversion_table < src_t > ::to_f32 (* src0_ptr ); 142dst_ptr [id ]= type_conversion_table < dst_t > ::from_f32 (tmp ); 143id ++ ; 144 } 145 } 146id += ne00 * (ne01 - ir1 ); 147 } 148 } 149 } 150return ; 151 } 152 153// dst counters 154int64_t i10 = 0 ; 155int64_t i11 = 0 ; 156int64_t i12 = 0 ; 157int64_t i13 = 0 ; 158 159if constexpr (std::is_same_v < dst_t ,src_t > ) { 160for (int64_t i03 = 0 ;i03 < ne03 ;i03 ++ ) { 161for (int64_t i02 = 0 ;i02 < ne02 ;i02 ++ ) { 162i10 += ne00 * ir0 ; 163while (i10 >=ne0 ) { 164i10 -= ne0 ; 165if (++ i11 == ne1 ) { 166i11 = 0 ; 167if (++ i12 == ne2 ) { 168i12 = 0 ; 169if (++ i13 == ne3 ) { 170i13 = 0 ; 171 } 172 } 173 } 174 } 175for (int64_t i01 = ir0 ;i01 < ir1 ;i01 ++ ) { 176for (int64_t i00 = 0 ;i00 < ne00 ;i00 ++ ) { 177const char * src0_ptr = ((char * )src0 -> data + i00 * nb00 + i01 * nb01 + i02 * nb02 + i03 * nb03 ); 178char * dst_ptr = ((char * )dst -> data + i10 * nb0 + i11 * nb1 + i12 * nb2 + i13 * nb3 ); 179 180memcpy (dst_ptr ,src0_ptr ,sizeof (dst_t )); 181 182if (++ i10 == ne00 ) { 183i10 = 0 ; 184if (++ i11 == ne01 ) { 185i11 = 0 ; 186if (++ i12 == ne02 ) { 187i12 = 0 ; 188if (++ i13 == ne03 ) { 189i13 = 0 ; 190 } 191 } 192 } 193 } 194 } 195 } 196i10 += ne00 * (ne01 - ir1 ); 197while (i10 >=ne0 ) { 198i10 -= ne0 ; 199if (++ i11 == ne1 ) { 200i11 = 0 ; 201if (++ i12 == ne2 ) { 202i12 = 0 ; 203if (++ i13 == ne3 ) { 204i13 = 0 ; 205 } 206 } 207 } 208 } 209 } 210 } 211 212 }else { 213for (int64_t i03 = 0 ;i03 < ne03 ;i03 ++ ) { 214for (int64_t i02 = 0 ;i02 < ne02 ;i02 ++ ) { 215i10 += ne00 * ir0 ; 216while (i10 >=ne0 ) { 217i10 -= ne0 ; 218if (++ i11 == ne1 ) { 219i11 = 0 ; 220if (++ i12 == ne2 ) { 221i12 = 0 ; 222if (++ i13 == ne3 ) { 223i13 = 0 ; 224 } 225 } 226 } 227 } 228for (int64_t i01 = ir0 ;i01 < ir1 ;i01 ++ ) { 229for (int64_t i00 = 0 ;i00 < ne00 ;i00 ++ ) { 230const char * src0_ptr = ((char * )src0 -> data + i00 * nb00 + i01 * nb01 + i02 * nb02 + i03 * nb03 ); 231char * dst_ptr = ((char * )dst -> data + i10 * nb0 + i11 * nb1 + i12 * nb2 + i13 * nb3 ); 232 233float 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 236if (++ i10 == ne0 ) { 237i10 = 0 ; 238if (++ i11 == ne1 ) { 239i11 = 0 ; 240if (++ i12 == ne2 ) { 241i12 = 0 ; 242if (++ i13 == ne3 ) { 243i13 = 0 ; 244 } 245 } 246 } 247 } 248 } 249 } 250i10 += ne00 * (ne01 - ir1 ); 251while (i10 >=ne0 ) { 252i10 -= ne0 ; 253if (++ i11 == ne1 ) { 254i11 = 0 ; 255if (++ i12 == ne2 ) { 256i12 = 0 ; 257if (++ i13 == ne3 ) { 258i13 = 0 ; 259 } 260 } 261 } 262 } 263 } 264 } 265 } 266} 267 268 269template < typename src_t > 270static void ggml_compute_forward_dup_to_q ( 271const ggml_compute_params * params , 272ggml_tensor * dst ) { 273 274const ggml_tensor * src0 = dst -> src [0 ]; 275 276GGML_ASSERT (ggml_nelements (dst )== ggml_nelements (src0 )); 277GGML_ASSERT (!ggml_is_quantized (src0 -> type )); 278 279GGML_TENSOR_UNARY_OP_LOCALS 280 281const int ith = params -> ith ;// thread index 282const int nth = params -> nth ;// number of threads 283 284// parallelize by rows 285const int nr = ne01 ; 286// number of rows per thread 287const int dr = (nr + nth - 1 ) /nth ; 288// row range for this thread 289const int ir0 = dr * ith ; 290const int ir1 = MIN (ir0 + dr ,nr ); 291 292if (ggml_is_contiguous (dst )&& 293nb00 == sizeof (src_t )&& 294ggml_get_type_traits_cpu (dst -> type )-> from_float ) { 295// casting non-quantized types --> intermediate f32 --> quantized 296ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu (dst -> type )-> from_float ; 297float * src0_f32 = (float * )params -> wdata + (ne00 + CACHE_LINE_SIZE_F32 )* ith ; 298 299size_t id = 0 ; 300size_t rs = nb0 * (ne00 /ggml_blck_size (dst -> type )); 301char * dst_ptr = (char * )dst -> data ; 302 303for (int i03 = 0 ;i03 < ne03 ;i03 ++ ) { 304for (int i02 = 0 ;i02 < ne02 ;i02 ++ ) { 305id += rs * ir0 ; 306for (int i01 = ir0 ;i01 < ir1 ;i01 ++ ) { 307const src_t * src0_ptr = (src_t * ) ((char * )src0 -> data + i01 * nb01 + i02 * nb02 + i03 * nb03 ); 308 309for (int i00 = 0 ;i00 < ne00 ;i00 ++ ) { 310src0_f32 [i00 ]= type_conversion_table < src_t > ::to_f32 (src0_ptr [i00 ]); 311 } 312 313quantize_row_q (src0_f32 ,dst_ptr + id ,ne00 ); 314id += rs ; 315 } 316id += rs * (ne01 - ir1 ); 317 } 318 } 319 }else { 320// printf("%s %s\n", ggml_type_name(src0->type), ggml_type_name(dst->type)); 321GGML_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. 326static void ggml_compute_forward_dup_bytes ( 327const ggml_compute_params * params , 328ggml_tensor * dst ) { 329const ggml_tensor * src0 = dst -> src [0 ]; 330 331GGML_ASSERT (ggml_nelements (dst )== ggml_nelements (src0 )); 332GGML_ASSERT (src0 -> type == dst -> type ); 333 334GGML_TENSOR_UNARY_OP_LOCALS ; 335 336if (ggml_is_contiguous (src0 )&& ggml_is_contiguous (dst )) { 337ggml_compute_forward_dup_same_cont (params ,dst ); 338return ; 339 } 340 341const size_t type_size = ggml_type_size (src0 -> type ); 342 343const int ith = params -> ith ;// thread index 344const int nth = params -> nth ;// number of threads 345 346// parallelize by rows 347const int nr = ne01 ; 348// number of rows per thread 349const int dr = (nr + nth - 1 ) /nth ; 350// row range for this thread 351const int ir0 = dr * ith ; 352const int ir1 = MIN (ir0 + dr ,nr ); 353 354if (src0 -> type == dst -> type && 355ggml_are_same_shape (src0 ,dst )&& 356nb00 == type_size && nb0 == type_size ) { 357// copy by rows 358const size_t rs = ggml_row_size (src0 -> type ,ne00 ); 359for (int64_t i03 = 0 ;i03 < ne03 ;i03 ++ ) { 360for (int64_t i02 = 0 ;i02 < ne02 ;i02 ++ ) { 361for (int64_t i01 = ir0 ;i01 < ir1 ;i01 ++ ) { 362memcpy ( 363 ((char * )dst -> data + i01 * nb1 + i02 * nb2 + i03 * nb3 ), 364 ((char * )src0 -> data + i01 * nb01 + i02 * nb02 + i03 * nb03 ), 365rs ); 366 } 367 } 368 } 369return ; 370 } 371 372if (ggml_is_contiguous (dst )) { 373size_t id = 0 ; 374char * dst_ptr = (char * )dst -> data ; 375const size_t rs = ne00 * type_size ; 376 377if (nb00 == type_size ) { 378// src0 is contiguous on first dimension, copy by rows 379for (int64_t i03 = 0 ;i03 < ne03 ;i03 ++ ) { 380for (int64_t i02 = 0 ;i02 < ne02 ;i02 ++ ) { 381id += rs * ir0 ; 382for (int64_t i01 = ir0 ;i01 < ir1 ;i01 ++ ) { 383const char * src0_ptr = (char * )src0 -> data + i01 * nb01 + i02 * nb02 + i03 * nb03 ; 384memcpy (dst_ptr + id ,src0_ptr ,rs ); 385id += rs ; 386 } 387id += rs * (ne01 - ir1 ); 388 } 389 } 390 }else { 391//printf("%s: this is not optimal - fix me\n", __func__); 392 393for (int64_t i03 = 0 ;i03 < ne03 ;i03 ++ ) { 394for (int64_t i02 = 0 ;i02 < ne02 ;i02 ++ ) { 395id += rs * ir0 ; 396for (int64_t i01 = ir0 ;i01 < ir1 ;i01 ++ ) { 397for (int64_t i00 = 0 ;i00 < ne00 ;i00 ++ ) { 398const char * src0_ptr = (char * )src0 -> data + i00 * nb00 + i01 * nb01 + i02 * nb02 + i03 * nb03 ; 399memcpy (dst_ptr + id ,src0_ptr ,type_size ); 400 401id += type_size ; 402 } 403 } 404id += rs * (ne01 - ir1 ); 405 } 406 } 407 } 408 409return ; 410 } 411 412// dst counters 413int64_t k10 = 0 ; 414int64_t i11 = 0 ; 415int64_t i12 = 0 ; 416int64_t i13 = 0 ; 417 418// number of blocks in a row 419const int64_t nk00 = ne00 /ggml_blck_size (src0 -> type ); 420const int64_t nk0 = ne0 /ggml_blck_size (dst -> type ); 421 422for (int64_t i03 = 0 ;i03 < ne03 ;i03 ++ ) { 423for (int64_t i02 = 0 ;i02 < ne02 ;i02 ++ ) { 424k10 += nk00 * ir0 ; 425while (k10 >=nk0 ) { 426k10 -= nk0 ; 427if (++ i11 == ne1 ) { 428i11 = 0 ; 429if (++ i12 == ne2 ) { 430i12 = 0 ; 431if (++ i13 == ne3 ) { 432i13 = 0 ; 433 } 434 } 435 } 436 } 437for (int64_t i01 = ir0 ;i01 < ir1 ;i01 ++ ) { 438for (int64_t k00 = 0 ;k00 < nk00 ;k00 ++ ) { 439const char * src0_ptr = ((char * )src0 -> data + k00 * nb00 + i01 * nb01 + i02 * nb02 + i03 * nb03 ); 440char * dst_ptr = ((char * )dst -> data + k10 * nb0 + i11 * nb1 + i12 * nb2 + i13 * nb3 ); 441 442memcpy (dst_ptr ,src0_ptr ,type_size ); 443 444if (++ k10 == nk0 ) { 445k10 = 0 ; 446if (++ i11 == ne1 ) { 447i11 = 0 ; 448if (++ i12 == ne2 ) { 449i12 = 0 ; 450if (++ i13 == ne3 ) { 451i13 = 0 ; 452 } 453 } 454 } 455 } 456 } 457 } 458k10 += nk00 * (ne01 - ir1 ); 459while (k10 >=nk0 ) { 460k10 -= nk0 ; 461if (++ i11 == ne1 ) { 462i11 = 0 ; 463if (++ i12 == ne2 ) { 464i12 = 0 ; 465if (++ i13 == ne3 ) { 466i13 = 0 ; 467 } 468 } 469 } 470 } 471 } 472 } 473} 474 475static void ggml_compute_forward_dup_from_q ( 476const ggml_compute_params * params , 477ggml_tensor * dst ) { 478 479const ggml_tensor * src0 = dst -> src [0 ]; 480const ggml_tensor * src1 = dst -> src [1 ]; 481 482GGML_TENSOR_BINARY_OP_LOCALS 483 484const ggml_type type = src0 -> type ; 485ggml_to_float_t const dequantize_row_q = ggml_get_type_traits (type )-> to_float ; 486 487size_t qk = ggml_blck_size (type ); 488const int64_t nr = ggml_nelements (src1 ) /qk ; 489 490// destination must be contiguous in the first dimension 491GGML_ASSERT (nb10 == ggml_type_size (dst -> type )); 492// must either have first dimension large enough to hold a row, or fully contiguous 493GGML_ASSERT ((ne10 %qk )== 0 || ggml_is_contiguous (dst )); 494 495const int ith = params -> ith ; 496const int nth = params -> nth ; 497 498const int dr = (nr + nth - 1 )/nth ; 499 500// row range for this thread 501const int ir0 = dr * ith ; 502const int ir1 = MIN (ir0 + dr ,nr ); 503 504for (int64_t ir = ir0 ;ir < ir1 ;++ ir ) { 505 506uint32_t i = ir * qk ; 507 508const int64_t i03 = i /(ne00 * ne01 * ne02 ); 509const int64_t i02 = (i - i03 * ne00 * ne01 * ne02 )/ (ne00 * ne01 ); 510const int64_t i01 = (i - i03 * ne00 * ne01 * ne02 - i02 * ne01 * ne00 ) /ne00 ; 511const int64_t i00 = i - i03 * ne00 * ne01 * ne02 - i02 * ne01 * ne00 - i01 * ne00 ; 512const int64_t x_offset = (i00 /qk )* nb00 + i01 * nb01 + i02 * nb02 + i03 * nb03 ; 513 514const int64_t i13 = i /(ne10 * ne11 * ne12 ); 515const int64_t i12 = (i - i13 * ne10 * ne11 * ne12 ) / (ne10 * ne11 ); 516const int64_t i11 = (i - i13 * ne10 * ne11 * ne12 - i12 * ne10 * ne11 ) /ne10 ; 517const int64_t i10 = i - i13 * ne10 * ne11 * ne12 - i12 * ne10 * ne11 - i11 * ne10 ; 518const int64_t dst_offset = i10 * nb10 + i11 * nb11 + i12 * nb12 + i13 * nb13 ; 519 520dequantize_row_q ( 521 (const void * ) ((char * )src0 -> data + x_offset ), 522 (float * ) ((char * )dst -> data + dst_offset ),qk ); 523 } 524} 525 526void ggml_compute_forward_dup ( 527const ggml_compute_params * params , 528ggml_tensor * dst ) { 529 530const ggml_tensor * src0 = dst -> src [0 ]; 531 532if (src0 -> type == dst -> type ) { 533ggml_compute_forward_dup_bytes (params ,dst ); 534return ; 535 } 536 537switch (src0 -> type ) { 538case GGML_TYPE_F16 : 539 { 540/**/ if (dst -> type == GGML_TYPE_F16 )ggml_compute_forward_dup_flt < ggml_fp16_t ,ggml_fp16_t > (params ,dst ); 541else if (dst -> type == GGML_TYPE_BF16 )ggml_compute_forward_dup_flt < ggml_fp16_t ,ggml_bf16_t > (params ,dst ); 542else if (dst -> type == GGML_TYPE_F32 )ggml_compute_forward_dup_flt < ggml_fp16_t ,float > (params ,dst ); 543else ggml_compute_forward_dup_to_q < ggml_fp16_t > (params ,dst ); 544 }break ; 545case GGML_TYPE_BF16 : 546 { 547/**/ if (dst -> type == GGML_TYPE_F16 )ggml_compute_forward_dup_flt < ggml_bf16_t ,ggml_fp16_t > (params ,dst ); 548else if (dst -> type == GGML_TYPE_BF16 )ggml_compute_forward_dup_flt < ggml_bf16_t ,ggml_bf16_t > (params ,dst ); 549else if (dst -> type == GGML_TYPE_F32 )ggml_compute_forward_dup_flt < ggml_bf16_t ,float > (params ,dst ); 550else ggml_compute_forward_dup_to_q < ggml_bf16_t > (params ,dst ); 551 }break ; 552case GGML_TYPE_F32 : 553 { 554/**/ if (dst -> type == GGML_TYPE_F16 )ggml_compute_forward_dup_flt < float ,ggml_fp16_t > (params ,dst ); 555else if (dst -> type == GGML_TYPE_BF16 )ggml_compute_forward_dup_flt < float ,ggml_bf16_t > (params ,dst ); 556else if (dst -> type == GGML_TYPE_F32 )ggml_compute_forward_dup_flt < float ,float > (params ,dst ); 557else if (dst -> type == GGML_TYPE_I32 )ggml_compute_forward_dup_flt < float ,int32_t > (params ,dst ); 558else ggml_compute_forward_dup_to_q < float > (params ,dst ); 559 }break ; 560case GGML_TYPE_I32 : 561 { 562if (dst -> type == GGML_TYPE_F32 )ggml_compute_forward_dup_flt < int32_t ,float > (params ,dst ); 563else GGML_ABORT ("not implemented" ); 564 }break ; 565default : 566 { 567if (ggml_is_quantized (src0 -> type )&& dst -> type == GGML_TYPE_F32 ) { 568ggml_compute_forward_dup_from_q (params ,dst ); 569break ; 570 } 571GGML_ABORT ("fatal error" ); 572 } 573 } 574} 575 576// ggml_compute_forward_add 577 578static void ggml_compute_forward_add_q_f32 ( 579const ggml_compute_params * params , 580ggml_tensor * dst ) { 581 582const ggml_tensor * src0 = dst -> src [0 ]; 583const ggml_tensor * src1 = dst -> src [1 ]; 584 585GGML_ASSERT (ggml_are_same_shape (src0 ,src1 )&& ggml_are_same_shape (src0 ,dst )); 586 587const int nr = ggml_nrows (src0 ); 588 589GGML_TENSOR_BINARY_OP_LOCALS 590 591const int ith = params -> ith ; 592const int nth = params -> nth ; 593 594const ggml_type type = src0 -> type ; 595const ggml_type dtype = dst -> type ; 596ggml_to_float_t const dequantize_row_q = ggml_get_type_traits (type )-> to_float ; 597ggml_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 600GGML_ASSERT (nb00 == ggml_type_size (type )); 601GGML_ASSERT (nb10 == sizeof (float )); 602 603// dst cannot be transposed or permuted 604GGML_ASSERT (nb0 <=nb1 ); 605GGML_ASSERT (nb1 <=nb2 ); 606GGML_ASSERT (nb2 <=nb3 ); 607 608GGML_ASSERT (ggml_is_quantized (src0 -> type )); 609GGML_ASSERT (src1 -> type == GGML_TYPE_F32 ); 610 611// rows per thread 612const int dr = (nr + nth - 1 )/nth ; 613 614// row range for this thread 615const int ir0 = dr * ith ; 616const int ir1 = MIN (ir0 + dr ,nr ); 617 618float * wdata = (float * )params -> wdata + (ne00 + CACHE_LINE_SIZE_F32 )* ith ; 619 620for (int ir = ir0 ;ir < ir1 ;++ ir ) { 621// src0 indices 622const int i03 = ir /(ne02 * ne01 ); 623const int i02 = (ir - i03 * ne02 * ne01 )/ne01 ; 624const int i01 = (ir - i03 * ne02 * ne01 - i02 * ne01 ); 625 626// src1 and dst are same shape as src0 => same indices 627const int i13 = i03 ; 628const int i12 = i02 ; 629const int i11 = i01 ; 630 631const int i3 = i03 ; 632const int i2 = i02 ; 633const int i1 = i01 ; 634 635void * src0_row = (void * ) ((char * )src0 -> data + (i01 * nb01 + i02 * nb02 + i03 * nb03 )); 636float * src1_row = (float * )((char * )src1 -> data + (i11 * nb11 + i12 * nb12 + i13 * nb13 )); 637void * dst_row = (void * ) ((char * )dst -> data + (i1 * nb1 + i2 * nb2 + i3 * nb3 )); 638 639assert (ne00 %32 == 0 ); 640 641// unquantize row from src0 to temp buffer 642dequantize_row_q (src0_row ,wdata ,ne00 ); 643// add src1 644ggml_vec_acc_f32 (ne00 ,wdata ,src1_row ); 645// quantize row to dst 646if (quantize_row_q != NULL ) { 647quantize_row_q (wdata ,dst_row ,ne00 ); 648 }else { 649memcpy (dst_row ,wdata ,ne0 * nb0 ); 650 } 651 } 652} 653 654void ggml_compute_forward_add ( 655const ggml_compute_params * params , 656ggml_tensor * dst ) { 657 658const ggml_tensor * src0 = dst -> src [0 ]; 659 660switch (src0 -> type ) { 661case GGML_TYPE_F32 : 662case GGML_TYPE_F16 : 663case GGML_TYPE_BF16 : 664 { 665ggml_compute_forward_add_non_quantized (params ,dst ); 666 }break ; 667case GGML_TYPE_Q1_0 : 668case GGML_TYPE_Q2_0 : 669case GGML_TYPE_Q4_0 : 670case GGML_TYPE_Q4_1 : 671case GGML_TYPE_Q5_0 : 672case GGML_TYPE_Q5_1 : 673case GGML_TYPE_Q8_0 : 674case GGML_TYPE_MXFP4 : 675case GGML_TYPE_NVFP4 : 676case GGML_TYPE_Q2_K : 677case GGML_TYPE_Q3_K : 678case GGML_TYPE_Q4_K : 679case GGML_TYPE_Q5_K : 680case GGML_TYPE_Q6_K : 681case GGML_TYPE_TQ1_0 : 682case GGML_TYPE_TQ2_0 : 683case GGML_TYPE_IQ2_XXS : 684case GGML_TYPE_IQ2_XS : 685case GGML_TYPE_IQ3_XXS : 686case GGML_TYPE_IQ1_S : 687case GGML_TYPE_IQ1_M : 688case GGML_TYPE_IQ4_NL : 689case GGML_TYPE_IQ4_XS : 690case GGML_TYPE_IQ3_S : 691case GGML_TYPE_IQ2_S : 692 { 693ggml_compute_forward_add_q_f32 (params ,dst ); 694 }break ; 695default : 696 { 697GGML_ABORT ("fatal error" ); 698 } 699 } 700} 701 702// ggml_compute_forward_add_id 703 704static void ggml_compute_forward_add_id_f32 ( 705const ggml_compute_params * params , 706ggml_tensor * dst ) { 707 708const ggml_tensor * src0 = dst -> src [0 ]; 709const ggml_tensor * src1 = dst -> src [1 ]; 710const ggml_tensor * src2 = dst -> src [2 ]; 711 712GGML_ASSERT (dst -> type == GGML_TYPE_F32 ); 713GGML_ASSERT (src0 -> type == GGML_TYPE_F32 ); 714GGML_ASSERT (src1 -> type == GGML_TYPE_F32 ); 715GGML_ASSERT (src2 -> type == GGML_TYPE_I32 ); 716 717GGML_ASSERT (src0 -> nb [0 ]== sizeof (float )); 718GGML_ASSERT (src1 -> nb [0 ]== sizeof (float )); 719 720const int ith = params -> ith ; 721const int nth = params -> nth ; 722 723const int nr = ggml_nrows (src0 ); 724 725GGML_TENSOR_TERNARY_OP_LOCALS 726 727GGML_ASSERT (nb0 == sizeof (float )); 728GGML_ASSERT (nb10 == sizeof (float )); 729 730// rows per thread 731const int dr = (nr + nth - 1 )/nth ; 732 733// row range for this thread 734const int ir0 = dr * ith ; 735const int ir1 = MIN (ir0 + dr ,nr ); 736 737for (int ir = ir0 ;ir < ir1 ;++ ir ) { 738// src0 indices 739const int i3 = ir /(ne2 * ne1 ); 740const int i2 = (ir - i3 * ne2 * ne1 )/ne1 ; 741const int i1 = (ir - i3 * ne2 * ne1 - i2 * ne1 ); 742 743// src1 indices 744const int i11 = * (int32_t * ) ((char * )src2 -> data + i1 * nb20 + i2 * nb21 ); 745 746GGML_ASSERT (i11 >=0 && i11 < ne11 ); 747 748ggml_vec_add_f32 (ne0 , 749 (float * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * nb1 ), 750 (float * ) ((char * )src0 -> data + i3 * nb03 + i2 * nb02 + i1 * nb01 ), 751 (float * ) ((char * )src1 -> data + i11 * nb11 )); 752 } 753} 754 755void ggml_compute_forward_add_id ( 756const ggml_compute_params * params , 757ggml_tensor * dst ) { 758 759const ggml_tensor * src0 = dst -> src [0 ]; 760 761switch (src0 -> type ) { 762case GGML_TYPE_F32 : 763 { 764ggml_compute_forward_add_id_f32 (params ,dst ); 765 }break ; 766default : 767 { 768GGML_ABORT ("unsupported type for ggml_compute_forward_add_id: %s" ,ggml_type_name (src0 -> type )); 769 } 770 } 771} 772 773// ggml_compute_forward_add1 774 775static void ggml_compute_forward_add1_f32 ( 776const ggml_compute_params * params , 777ggml_tensor * dst ) { 778 779const ggml_tensor * src0 = dst -> src [0 ]; 780const ggml_tensor * src1 = dst -> src [1 ]; 781 782GGML_ASSERT (ggml_are_same_shape (src0 ,dst )); 783GGML_ASSERT (ggml_is_scalar (src1 )); 784 785const int ith = params -> ith ; 786const int nth = params -> nth ; 787 788const int nr = ggml_nrows (src0 ); 789 790GGML_TENSOR_UNARY_OP_LOCALS 791 792GGML_ASSERT (nb0 == sizeof (float )); 793GGML_ASSERT (nb00 == sizeof (float )); 794 795// rows per thread 796const int dr = (nr + nth - 1 )/nth ; 797 798// row range for this thread 799const int ir0 = dr * ith ; 800const int ir1 = MIN (ir0 + dr ,nr ); 801 802for (int ir = ir0 ;ir < ir1 ;++ ir ) { 803// src0 and dst are same shape => same indices 804const int i3 = ir /(ne2 * ne1 ); 805const int i2 = (ir - i3 * ne2 * ne1 )/ne1 ; 806const int i1 = (ir - i3 * ne2 * ne1 - i2 * ne1 ); 807 808#ifdef GGML_USE_ACCELERATE 809GGML_UNUSED (ggml_vec_add1_f32 ); 810 811vDSP_vadd ( 812 (float * ) ((char * )src0 -> data + i3 * nb03 + i2 * nb02 + i1 * nb01 ),1 , 813 (float * ) ((char * )src1 -> data ),0 , 814 (float * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * nb1 ),1 , 815ne0 ); 816#else 817ggml_vec_add1_f32 (ne0 , 818 (float * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * nb1 ), 819 (float * ) ((char * )src0 -> data + i3 * nb03 + i2 * nb02 + i1 * nb01 ), 820* (float * )src1 -> data ); 821#endif 822 } 823} 824 825static void ggml_compute_forward_add1_f16_f32 ( 826const ggml_compute_params * params , 827ggml_tensor * dst ) { 828 829const ggml_tensor * src0 = dst -> src [0 ]; 830const ggml_tensor * src1 = dst -> src [1 ]; 831 832GGML_ASSERT (ggml_are_same_shape (src0 ,dst )); 833GGML_ASSERT (ggml_is_scalar (src1 )); 834 835// scalar to add 836const float v = * (float * )src1 -> data ; 837 838const int ith = params -> ith ; 839const int nth = params -> nth ; 840 841const int nr = ggml_nrows (src0 ); 842 843GGML_TENSOR_UNARY_OP_LOCALS 844 845GGML_ASSERT (src0 -> type == GGML_TYPE_F16 ); 846GGML_ASSERT (src1 -> type == GGML_TYPE_F32 ); 847GGML_ASSERT (dst -> type == GGML_TYPE_F16 ); 848 849GGML_ASSERT (nb0 == sizeof (ggml_fp16_t )); 850GGML_ASSERT (nb00 == sizeof (ggml_fp16_t )); 851 852// rows per thread 853const int dr = (nr + nth - 1 )/nth ; 854 855// row range for this thread 856const int ir0 = dr * ith ; 857const int ir1 = MIN (ir0 + dr ,nr ); 858 859for (int ir = ir0 ;ir < ir1 ;++ ir ) { 860// src0 and dst are same shape => same indices 861const int i3 = ir /(ne2 * ne1 ); 862const int i2 = (ir - i3 * ne2 * ne1 )/ne1 ; 863const int i1 = (ir - i3 * ne2 * ne1 - i2 * ne1 ); 864 865ggml_fp16_t * dst_ptr = (ggml_fp16_t * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * nb1 ); 866ggml_fp16_t * src0_ptr = (ggml_fp16_t * ) ((char * )src0 -> data + i3 * nb03 + i2 * nb02 + i1 * nb01 ); 867for (int i = 0 ;i < ne0 ;i ++ ) { 868dst_ptr [i ]= GGML_CPU_FP32_TO_FP16 (GGML_CPU_FP16_TO_FP32 (src0_ptr [i ])+ v ); 869 } 870 } 871} 872 873static void ggml_compute_forward_add1_f16_f16 ( 874const ggml_compute_params * params , 875ggml_tensor * dst ) { 876 877const ggml_tensor * src0 = dst -> src [0 ]; 878const ggml_tensor * src1 = dst -> src [1 ]; 879 880GGML_ASSERT (ggml_are_same_shape (src0 ,dst )); 881GGML_ASSERT (ggml_is_scalar (src1 )); 882 883// scalar to add 884const float v = GGML_CPU_FP16_TO_FP32 (* (ggml_fp16_t * )src1 -> data ); 885 886const int ith = params -> ith ; 887const int nth = params -> nth ; 888 889const int nr = ggml_nrows (src0 ); 890 891GGML_TENSOR_UNARY_OP_LOCALS 892 893GGML_ASSERT (src0 -> type == GGML_TYPE_F16 ); 894GGML_ASSERT (src1 -> type == GGML_TYPE_F16 ); 895GGML_ASSERT (dst -> type == GGML_TYPE_F16 ); 896 897GGML_ASSERT (nb0 == sizeof (ggml_fp16_t )); 898GGML_ASSERT (nb00 == sizeof (ggml_fp16_t )); 899 900// rows per thread 901const int dr = (nr + nth - 1 )/nth ; 902 903// row range for this thread 904const int ir0 = dr * ith ; 905const int ir1 = MIN (ir0 + dr ,nr ); 906 907for (int ir = ir0 ;ir < ir1 ;++ ir ) { 908// src0 and dst are same shape => same indices 909const int i3 = ir /(ne2 * ne1 ); 910const int i2 = (ir - i3 * ne2 * ne1 )/ne1 ; 911const int i1 = (ir - i3 * ne2 * ne1 - i2 * ne1 ); 912 913ggml_fp16_t * dst_ptr = (ggml_fp16_t * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * nb1 ); 914ggml_fp16_t * src0_ptr = (ggml_fp16_t * ) ((char * )src0 -> data + i3 * nb03 + i2 * nb02 + i1 * nb01 ); 915for (int i = 0 ;i < ne0 ;i ++ ) { 916dst_ptr [i ]= GGML_CPU_FP32_TO_FP16 (GGML_CPU_FP16_TO_FP32 (src0_ptr [i ])+ v ); 917 } 918 } 919} 920 921static void ggml_compute_forward_add1_q_f32 ( 922const ggml_compute_params * params , 923ggml_tensor * dst ) { 924 925const ggml_tensor * src0 = dst -> src [0 ]; 926const ggml_tensor * src1 = dst -> src [1 ]; 927 928GGML_ASSERT (ggml_are_same_shape (src0 ,dst )); 929GGML_ASSERT (ggml_is_scalar (src1 )); 930 931// scalar to add 932const float v = * (float * )src1 -> data ; 933 934const int ith = params -> ith ; 935const int nth = params -> nth ; 936 937const int nr = ggml_nrows (src0 ); 938 939GGML_TENSOR_UNARY_OP_LOCALS 940 941const ggml_type type = src0 -> type ; 942ggml_to_float_t const dequantize_row_q = ggml_get_type_traits (type )-> to_float ; 943ggml_from_float_t const quantize_row_q = ggml_get_type_traits_cpu (type )-> from_float ; 944 945// we don't support permuted src0 946GGML_ASSERT (nb00 == ggml_type_size (type )); 947 948// dst cannot be transposed or permuted 949GGML_ASSERT (nb0 <=nb1 ); 950GGML_ASSERT (nb1 <=nb2 ); 951GGML_ASSERT (nb2 <=nb3 ); 952 953GGML_ASSERT (ggml_is_quantized (src0 -> type )); 954GGML_ASSERT (dst -> type == src0 -> type ); 955GGML_ASSERT (src1 -> type == GGML_TYPE_F32 ); 956 957// rows per thread 958const int dr = (nr + nth - 1 )/nth ; 959 960// row range for this thread 961const int ir0 = dr * ith ; 962const int ir1 = MIN (ir0 + dr ,nr ); 963 964float * wdata = (float * )params -> wdata + (ne0 + CACHE_LINE_SIZE_F32 )* ith ; 965 966for (int ir = ir0 ;ir < ir1 ;++ ir ) { 967// src0 and dst are same shape => same indices 968const int i3 = ir /(ne2 * ne1 ); 969const int i2 = (ir - i3 * ne2 * ne1 )/ne1 ; 970const int i1 = (ir - i3 * ne2 * ne1 - i2 * ne1 ); 971 972void * src0_row = (void * ) ((char * )src0 -> data + (i1 * nb01 + i2 * nb02 + i3 * nb03 )); 973void * dst_row = (void * ) ((char * )dst -> data + (i1 * nb1 + i2 * nb2 + i3 * nb0 )); 974 975assert (ne0 %32 == 0 ); 976 977// unquantize row from src0 to temp buffer 978dequantize_row_q (src0_row ,wdata ,ne0 ); 979// add src1 980ggml_vec_acc1_f32 (ne0 ,wdata ,v ); 981// quantize row to dst 982quantize_row_q (wdata ,dst_row ,ne0 ); 983 } 984} 985 986static void ggml_compute_forward_add1_bf16_f32 ( 987const ggml_compute_params * params , 988ggml_tensor * dst ) { 989 990const ggml_tensor * src0 = dst -> src [0 ]; 991const ggml_tensor * src1 = dst -> src [1 ]; 992 993GGML_ASSERT (ggml_are_same_shape (src0 ,dst )); 994GGML_ASSERT (ggml_is_scalar (src1 )); 995 996// scalar to add 997const float v = * (float * )src1 -> data ; 998 999const int ith = params -> ith ; 1000const int nth = params -> nth ; 1001 1002const int nr = ggml_nrows (src0 ); 1003 1004GGML_TENSOR_UNARY_OP_LOCALS 1005 1006GGML_ASSERT (src0 -> type == GGML_TYPE_BF16 ); 1007GGML_ASSERT (src1 -> type == GGML_TYPE_F32 ); 1008GGML_ASSERT (dst -> type == GGML_TYPE_BF16 ); 1009 1010GGML_ASSERT (nb0 == sizeof (ggml_bf16_t )); 1011GGML_ASSERT (nb00 == sizeof (ggml_bf16_t )); 1012 1013// rows per thread 1014const int dr = (nr + nth - 1 )/nth ; 1015 1016// row range for this thread 1017const int ir0 = dr * ith ; 1018const int ir1 = MIN (ir0 + dr ,nr ); 1019 1020for (int ir = ir0 ;ir < ir1 ;++ ir ) { 1021// src0 and dst are same shape => same indices 1022const int i3 = ir /(ne2 * ne1 ); 1023const int i2 = (ir - i3 * ne2 * ne1 )/ne1 ; 1024const int i1 = (ir - i3 * ne2 * ne1 - i2 * ne1 ); 1025 1026ggml_bf16_t * dst_ptr = (ggml_bf16_t * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * nb1 ); 1027ggml_bf16_t * src0_ptr = (ggml_bf16_t * ) ((char * )src0 -> data + i3 * nb03 + i2 * nb02 + i1 * nb01 ); 1028for (int i = 0 ;i < ne0 ;i ++ ) { 1029dst_ptr [i ]= GGML_FP32_TO_BF16 (GGML_BF16_TO_FP32 (src0_ptr [i ])+ v ); 1030 } 1031 } 1032} 1033 1034static void ggml_compute_forward_add1_bf16_bf16 ( 1035const ggml_compute_params * params , 1036ggml_tensor * dst ) { 1037 1038const ggml_tensor * src0 = dst -> src [0 ]; 1039const ggml_tensor * src1 = dst -> src [1 ]; 1040 1041GGML_ASSERT (ggml_are_same_shape (src0 ,dst )); 1042GGML_ASSERT (ggml_is_scalar (src1 )); 1043 1044// scalar to add 1045const float v = GGML_BF16_TO_FP32 (* (ggml_bf16_t * )src1 -> data ); 1046 1047const int ith = params -> ith ; 1048const int nth = params -> nth ; 1049 1050const int nr = ggml_nrows (src0 ); 1051 1052GGML_TENSOR_UNARY_OP_LOCALS 1053 1054GGML_ASSERT (src0 -> type == GGML_TYPE_BF16 ); 1055GGML_ASSERT (src1 -> type == GGML_TYPE_BF16 ); 1056GGML_ASSERT (dst -> type == GGML_TYPE_BF16 ); 1057 1058GGML_ASSERT (nb0 == sizeof (ggml_bf16_t )); 1059GGML_ASSERT (nb00 == sizeof (ggml_bf16_t )); 1060 1061// rows per thread 1062const int dr = (nr + nth - 1 )/nth ; 1063 1064// row range for this thread 1065const int ir0 = dr * ith ; 1066const int ir1 = MIN (ir0 + dr ,nr ); 1067 1068for (int ir = ir0 ;ir < ir1 ;++ ir ) { 1069// src0 and dst are same shape => same indices 1070const int i3 = ir /(ne2 * ne1 ); 1071const int i2 = (ir - i3 * ne2 * ne1 )/ne1 ; 1072const int i1 = (ir - i3 * ne2 * ne1 - i2 * ne1 ); 1073 1074ggml_bf16_t * dst_ptr = (ggml_bf16_t * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * nb1 ); 1075ggml_bf16_t * src0_ptr = (ggml_bf16_t * ) ((char * )src0 -> data + i3 * nb03 + i2 * nb02 + i1 * nb01 ); 1076for (int i = 0 ;i < ne0 ;i ++ ) { 1077dst_ptr [i ]= GGML_FP32_TO_BF16 (GGML_BF16_TO_FP32 (src0_ptr [i ])+ v ); 1078 } 1079 } 1080} 1081 1082void ggml_compute_forward_add1 ( 1083const ggml_compute_params * params , 1084ggml_tensor * dst ) { 1085 1086const ggml_tensor * src0 = dst -> src [0 ]; 1087const ggml_tensor * src1 = dst -> src [1 ]; 1088 1089switch (src0 -> type ) { 1090case GGML_TYPE_F32 : 1091 { 1092ggml_compute_forward_add1_f32 (params ,dst ); 1093 }break ; 1094case GGML_TYPE_F16 : 1095 { 1096if (src1 -> type == GGML_TYPE_F16 ) { 1097ggml_compute_forward_add1_f16_f16 (params ,dst ); 1098 } 1099else if (src1 -> type == GGML_TYPE_F32 ) { 1100ggml_compute_forward_add1_f16_f32 (params ,dst ); 1101 } 1102else { 1103GGML_ABORT ("fatal error" ); 1104 } 1105 }break ; 1106case GGML_TYPE_BF16 : 1107 { 1108if (src1 -> type == GGML_TYPE_BF16 ) { 1109ggml_compute_forward_add1_bf16_bf16 (params ,dst ); 1110 } 1111else if (src1 -> type == GGML_TYPE_F32 ) { 1112ggml_compute_forward_add1_bf16_f32 (params ,dst ); 1113 } 1114else { 1115GGML_ABORT ("fatal error" ); 1116 } 1117 }break ; 1118case GGML_TYPE_Q1_0 : 1119case GGML_TYPE_Q2_0 : 1120case GGML_TYPE_Q4_0 : 1121case GGML_TYPE_Q4_1 : 1122case GGML_TYPE_Q5_0 : 1123case GGML_TYPE_Q5_1 : 1124case GGML_TYPE_Q8_0 : 1125case GGML_TYPE_Q8_1 : 1126case GGML_TYPE_MXFP4 : 1127case GGML_TYPE_NVFP4 : 1128case GGML_TYPE_Q2_K : 1129case GGML_TYPE_Q3_K : 1130case GGML_TYPE_Q4_K : 1131case GGML_TYPE_Q5_K : 1132case GGML_TYPE_Q6_K : 1133case GGML_TYPE_TQ1_0 : 1134case GGML_TYPE_TQ2_0 : 1135case GGML_TYPE_IQ2_XXS : 1136case GGML_TYPE_IQ2_XS : 1137case GGML_TYPE_IQ3_XXS : 1138case GGML_TYPE_IQ1_S : 1139case GGML_TYPE_IQ1_M : 1140case GGML_TYPE_IQ4_NL : 1141case GGML_TYPE_IQ4_XS : 1142case GGML_TYPE_IQ3_S : 1143case GGML_TYPE_IQ2_S : 1144 { 1145ggml_compute_forward_add1_q_f32 (params ,dst ); 1146 }break ; 1147default : 1148 { 1149GGML_ABORT ("fatal error" ); 1150 } 1151 } 1152} 1153 1154// ggml_compute_forward_acc 1155 1156static void ggml_compute_forward_acc_f32 ( 1157const ggml_compute_params * params , 1158ggml_tensor * dst ) { 1159 1160const ggml_tensor * src0 = dst -> src [0 ]; 1161const ggml_tensor * src1 = dst -> src [1 ]; 1162 1163GGML_ASSERT (ggml_are_same_shape (src0 ,dst )); 1164GGML_ASSERT (ggml_is_contiguous (dst )&& ggml_is_contiguous (src0 )); 1165 1166// view src0 and dst with these strides and data offset inbytes during acc 1167// nb0 is implicitly element_size because src0 and dst are contiguous 1168size_t nb1 = ((int32_t * )dst -> op_params )[0 ]; 1169size_t nb2 = ((int32_t * )dst -> op_params )[1 ]; 1170size_t nb3 = ((int32_t * )dst -> op_params )[2 ]; 1171size_t offset = ((int32_t * )dst -> op_params )[3 ]; 1172bool inplace = (bool ) ((int32_t * )dst -> op_params )[4 ]; 1173 1174if (!inplace ) { 1175if (params -> ith == 0 ) { 1176// memcpy needs to be synchronized across threads to avoid race conditions. 1177// => do it in INIT phase 1178memcpy ( 1179 ((char * )dst -> data ), 1180 ((char * )src0 -> data ), 1181ggml_nbytes (dst )); 1182 } 1183ggml_barrier (params -> threadpool ); 1184 } 1185 1186const int ith = params -> ith ; 1187const int nth = params -> nth ; 1188 1189const int nr = ggml_nrows (src1 ); 1190const int nc = src1 -> ne [0 ]; 1191 1192GGML_TENSOR_LOCALS (int64_t ,ne1 ,src1 ,ne ) 1193GGML_TENSOR_LOCALS (size_t ,nb1 ,src1 ,nb ) 1194 1195// src0 and dst as viewed during acc 1196const size_t nb0 = ggml_element_size (src0 ); 1197 1198const size_t nb00 = nb0 ; 1199const size_t nb01 = nb1 ; 1200const size_t nb02 = nb2 ; 1201const size_t nb03 = nb3 ; 1202 1203GGML_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 )); 1204GGML_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 )); 1205 1206GGML_ASSERT (nb10 == sizeof (float )); 1207 1208// rows per thread 1209const int dr = (nr + nth - 1 )/nth ; 1210 1211// row range for this thread 1212const int ir0 = dr * ith ; 1213const int ir1 = MIN (ir0 + dr ,nr ); 1214 1215for (int ir = ir0 ;ir < ir1 ;++ ir ) { 1216// src0 and dst are viewed with shape of src1 and offset 1217// => same indices 1218const int i3 = ir /(ne12 * ne11 ); 1219const int i2 = (ir - i3 * ne12 * ne11 )/ne11 ; 1220const int i1 = (ir - i3 * ne12 * ne11 - i2 * ne11 ); 1221 1222#ifdef GGML_USE_ACCELERATE 1223vDSP_vadd ( 1224 (float * ) ((char * )src0 -> data + i3 * nb03 + i2 * nb02 + i1 * nb01 + offset ),1 , 1225 (float * ) ((char * )src1 -> data + i3 * nb13 + i2 * nb12 + i1 * nb11 ),1 , 1226 (float * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * nb1 + offset ),1 ,nc ); 1227#else 1228ggml_vec_add_f32 (nc , 1229 (float * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * nb1 + offset ), 1230 (float * ) ((char * )src0 -> data + i3 * nb03 + i2 * nb02 + i1 * nb01 + offset ), 1231 (float * ) ((char * )src1 -> data + i3 * nb13 + i2 * nb12 + i1 * nb11 )); 1232#endif 1233 } 1234} 1235 1236void ggml_compute_forward_acc ( 1237const ggml_compute_params * params , 1238ggml_tensor * dst ) { 1239 1240const ggml_tensor * src0 = dst -> src [0 ]; 1241 1242switch (src0 -> type ) { 1243case GGML_TYPE_F32 : 1244 { 1245ggml_compute_forward_acc_f32 (params ,dst ); 1246 }break ; 1247case GGML_TYPE_F16 : 1248case GGML_TYPE_BF16 : 1249case GGML_TYPE_Q1_0 : 1250case GGML_TYPE_Q2_0 : 1251case GGML_TYPE_Q4_0 : 1252case GGML_TYPE_Q4_1 : 1253case GGML_TYPE_Q5_0 : 1254case GGML_TYPE_Q5_1 : 1255case GGML_TYPE_Q8_0 : 1256case GGML_TYPE_Q8_1 : 1257case GGML_TYPE_MXFP4 : 1258case GGML_TYPE_NVFP4 : 1259case GGML_TYPE_Q2_K : 1260case GGML_TYPE_Q3_K : 1261case GGML_TYPE_Q4_K : 1262case GGML_TYPE_Q5_K : 1263case GGML_TYPE_Q6_K : 1264case GGML_TYPE_TQ1_0 : 1265case GGML_TYPE_TQ2_0 : 1266case GGML_TYPE_IQ2_XXS : 1267case GGML_TYPE_IQ2_XS : 1268case GGML_TYPE_IQ3_XXS : 1269case GGML_TYPE_IQ1_S : 1270case GGML_TYPE_IQ1_M : 1271case GGML_TYPE_IQ4_NL : 1272case GGML_TYPE_IQ4_XS : 1273case GGML_TYPE_IQ3_S : 1274case GGML_TYPE_IQ2_S : 1275default : 1276 { 1277GGML_ABORT ("fatal error" ); 1278 } 1279 } 1280} 1281 1282// ggml_compute_forward_sum 1283 1284static void ggml_compute_forward_sum_f32 ( 1285const ggml_compute_params * params , 1286ggml_tensor * dst ) { 1287 1288const ggml_tensor * src0 = dst -> src [0 ]; 1289 1290if (params -> ith != 0 ) { 1291return ; 1292 } 1293 1294assert (ggml_is_scalar (dst )); 1295assert (src0 -> nb [0 ]== sizeof (float )); 1296 1297GGML_TENSOR_LOCALS (int64_t ,ne0 ,src0 ,ne ) 1298GGML_TENSOR_LOCALS (size_t ,nb0 ,src0 ,nb ) 1299 1300ggml_float sum = 0 ; 1301ggml_float row_sum = 0 ; 1302 1303for (int64_t i03 = 0 ;i03 < ne03 ;i03 ++ ) { 1304for (int64_t i02 = 0 ;i02 < ne02 ;i02 ++ ) { 1305for (int64_t i01 = 0 ;i01 < ne01 ;i01 ++ ) { 1306ggml_vec_sum_f32_ggf (ne00 , 1307& row_sum , 1308 (float * ) ((char * )src0 -> data + i01 * nb01 + i02 * nb02 + i03 * nb03 )); 1309sum += row_sum ; 1310 } 1311 } 1312 } 1313 ((float * )dst -> data )[0 ]= sum ; 1314} 1315 1316static void ggml_compute_forward_sum_f16 ( 1317const ggml_compute_params * params , 1318ggml_tensor * dst ) { 1319 1320const ggml_tensor * src0 = dst -> src [0 ]; 1321 1322if (params -> ith != 0 ) { 1323return ; 1324 } 1325 1326assert (ggml_is_scalar (dst )); 1327 1328assert (src0 -> nb [0 ]== sizeof (ggml_fp16_t )); 1329 1330GGML_TENSOR_LOCALS (int64_t ,ne0 ,src0 ,ne ) 1331GGML_TENSOR_LOCALS (size_t ,nb0 ,src0 ,nb ) 1332 1333float sum = 0 ; 1334float row_sum = 0 ; 1335 1336for (int64_t i03 = 0 ;i03 < ne03 ;i03 ++ ) { 1337for (int64_t i02 = 0 ;i02 < ne02 ;i02 ++ ) { 1338for (int64_t i01 = 0 ;i01 < ne01 ;i01 ++ ) { 1339ggml_vec_sum_f16_ggf (ne00 , 1340& row_sum , 1341 (ggml_fp16_t * ) ((char * )src0 -> data + i01 * nb01 + i02 * nb02 + i03 * nb03 )); 1342sum += row_sum ; 1343 } 1344 } 1345 } 1346 ((ggml_fp16_t * )dst -> data )[0 ]= GGML_CPU_FP32_TO_FP16 (sum ); 1347} 1348 1349static void ggml_compute_forward_sum_bf16 ( 1350const ggml_compute_params * params , 1351ggml_tensor * dst ) { 1352 1353const ggml_tensor * src0 = dst -> src [0 ]; 1354 1355if (params -> ith != 0 ) { 1356return ; 1357 } 1358 1359assert (ggml_is_scalar (dst )); 1360 1361assert (src0 -> nb [0 ]== sizeof (ggml_bf16_t )); 1362 1363GGML_TENSOR_LOCALS (int64_t ,ne0 ,src0 ,ne ) 1364GGML_TENSOR_LOCALS (size_t ,nb0 ,src0 ,nb ) 1365 1366float sum = 0 ; 1367float row_sum = 0 ; 1368 1369for (int64_t i03 = 0 ;i03 < ne03 ;i03 ++ ) { 1370for (int64_t i02 = 0 ;i02 < ne02 ;i02 ++ ) { 1371for (int64_t i01 = 0 ;i01 < ne01 ;i01 ++ ) { 1372ggml_vec_sum_bf16_ggf (ne00 , 1373& row_sum , 1374 (ggml_bf16_t * ) ((char * )src0 -> data + i01 * nb01 + i02 * nb02 + i03 * nb03 )); 1375sum += row_sum ; 1376 } 1377 } 1378 } 1379 ((ggml_bf16_t * )dst -> data )[0 ]= GGML_FP32_TO_BF16 (sum ); 1380} 1381 1382void ggml_compute_forward_sum ( 1383const ggml_compute_params * params , 1384ggml_tensor * dst ) { 1385 1386const ggml_tensor * src0 = dst -> src [0 ]; 1387 1388switch (src0 -> type ) { 1389case GGML_TYPE_F32 : 1390 { 1391ggml_compute_forward_sum_f32 (params ,dst ); 1392 }break ; 1393case GGML_TYPE_F16 : 1394 { 1395ggml_compute_forward_sum_f16 (params ,dst ); 1396 }break ; 1397case GGML_TYPE_BF16 : 1398 { 1399ggml_compute_forward_sum_bf16 (params ,dst ); 1400 }break ; 1401default : 1402 { 1403GGML_ABORT ("fatal error" ); 1404 } 1405 } 1406} 1407 1408// ggml_compute_forward_cumsum 1409 1410static void ggml_compute_forward_cumsum_f32 ( 1411const ggml_compute_params * params , 1412ggml_tensor * dst ) { 1413 1414const ggml_tensor * src0 = dst -> src [0 ]; 1415 1416GGML_ASSERT (src0 -> nb [0 ]== sizeof (float )); 1417GGML_ASSERT (dst -> nb [0 ]== sizeof (float )); 1418 1419GGML_TENSOR_UNARY_OP_LOCALS 1420 1421GGML_ASSERT (ne0 == ne00 ); 1422GGML_ASSERT (ne1 == ne01 ); 1423GGML_ASSERT (ne2 == ne02 ); 1424GGML_ASSERT (ne3 == ne03 ); 1425 1426const auto [ir0 ,ir1 ]= get_thread_range (params ,src0 ); 1427 1428for (int64_t ir = ir0 ;ir < ir1 ;++ ir ) { 1429const int64_t i03 = ir /(ne02 * ne01 ); 1430const int64_t i02 = (ir - i03 * ne02 * ne01 )/ne01 ; 1431const int64_t i01 = (ir - i03 * ne02 * ne01 - i02 * ne01 ); 1432 1433float * src_row = (float * ) ((char * )src0 -> data + i01 * nb01 + i02 * nb02 + i03 * nb03 ); 1434float * dst_row = (float * ) ((char * )dst -> data + i01 * nb1 + i02 * nb2 + i03 * nb3 ); 1435 1436ggml_vec_cumsum_f32 (ne00 ,dst_row ,src_row ); 1437 } 1438} 1439 1440void ggml_compute_forward_cumsum ( 1441const ggml_compute_params * params , 1442ggml_tensor * dst ) { 1443 1444const ggml_tensor * src0 = dst -> src [0 ]; 1445 1446switch (src0 -> type ) { 1447case GGML_TYPE_F32 : 1448 { 1449ggml_compute_forward_cumsum_f32 (params ,dst ); 1450 }break ; 1451default : 1452 { 1453GGML_ABORT ("fatal error" ); 1454 } 1455 } 1456} 1457 1458// ggml_compute_forward_sum_rows 1459 1460static void ggml_compute_forward_sum_rows_f32 ( 1461const ggml_compute_params * params , 1462ggml_tensor * dst ) { 1463 1464const ggml_tensor * src0 = dst -> src [0 ]; 1465 1466if (params -> ith != 0 ) { 1467return ; 1468 } 1469 1470GGML_ASSERT (src0 -> nb [0 ]== sizeof (float )); 1471GGML_ASSERT (dst -> nb [0 ]== sizeof (float )); 1472 1473GGML_TENSOR_UNARY_OP_LOCALS 1474 1475GGML_ASSERT (ne0 == 1 ); 1476GGML_ASSERT (ne1 == ne01 ); 1477GGML_ASSERT (ne2 == ne02 ); 1478GGML_ASSERT (ne3 == ne03 ); 1479 1480for (int64_t i3 = 0 ;i3 < ne03 ;i3 ++ ) { 1481for (int64_t i2 = 0 ;i2 < ne02 ;i2 ++ ) { 1482for (int64_t i1 = 0 ;i1 < ne01 ;i1 ++ ) { 1483float * src_row = (float * ) ((char * )src0 -> data + i1 * nb01 + i2 * nb02 + i3 * nb03 ); 1484float * dst_row = (float * ) ((char * )dst -> data + i1 * nb1 + i2 * nb2 + i3 * nb3 ); 1485float row_sum = 0 ; 1486ggml_vec_sum_f32 (ne00 ,& row_sum ,src_row ); 1487dst_row [0 ]= row_sum ; 1488 } 1489 } 1490 } 1491} 1492 1493void ggml_compute_forward_sum_rows ( 1494const ggml_compute_params * params , 1495ggml_tensor * dst ) { 1496 1497const ggml_tensor * src0 = dst -> src [0 ]; 1498 1499switch (src0 -> type ) { 1500case GGML_TYPE_F32 : 1501 { 1502ggml_compute_forward_sum_rows_f32 (params ,dst ); 1503 }break ; 1504default : 1505 { 1506GGML_ABORT ("fatal error" ); 1507 } 1508 } 1509} 1510 1511// ggml_compute_forward_mean 1512 1513static void ggml_compute_forward_mean_f32 ( 1514const ggml_compute_params * params , 1515ggml_tensor * dst ) { 1516 1517const ggml_tensor * src0 = dst -> src [0 ]; 1518 1519if (params -> ith != 0 ) { 1520return ; 1521 } 1522 1523assert (src0 -> nb [0 ]== sizeof (float )); 1524 1525GGML_TENSOR_UNARY_OP_LOCALS 1526 1527assert (ne0 == 1 ); 1528assert (ne1 == ne01 ); 1529assert (ne2 == ne02 ); 1530assert (ne3 == ne03 ); 1531 1532GGML_UNUSED (ne0 ); 1533GGML_UNUSED (ne1 ); 1534GGML_UNUSED (ne2 ); 1535GGML_UNUSED (ne3 ); 1536 1537for (int64_t i03 = 0 ;i03 < ne03 ;i03 ++ ) { 1538for (int64_t i02 = 0 ;i02 < ne02 ;i02 ++ ) { 1539for (int64_t i01 = 0 ;i01 < ne01 ;i01 ++ ) { 1540ggml_vec_sum_f32 (ne00 , 1541 (float * ) ((char * )dst -> data + i01 * nb1 + i02 * nb2 + i03 * nb3 ), 1542 (float * ) ((char * )src0 -> data + i01 * nb01 + i02 * nb02 + i03 * nb03 )); 1543 1544* (float * ) ((char * )dst -> data + i01 * nb1 + i02 * nb2 + i03 * nb3 ) /= (float )ne00 ; 1545 } 1546 } 1547 } 1548} 1549 1550void ggml_compute_forward_mean ( 1551const ggml_compute_params * params , 1552ggml_tensor * dst ) { 1553 1554const ggml_tensor * src0 = dst -> src [0 ]; 1555 1556switch (src0 -> type ) { 1557case GGML_TYPE_F32 : 1558 { 1559ggml_compute_forward_mean_f32 (params ,dst ); 1560 }break ; 1561default : 1562 { 1563GGML_ABORT ("fatal error" ); 1564 } 1565 } 1566} 1567 1568// ggml_compute_forward_argmax 1569 1570static void ggml_compute_forward_argmax_f32 ( 1571const ggml_compute_params * params , 1572ggml_tensor * dst ) { 1573 1574const ggml_tensor * src0 = dst -> src [0 ]; 1575 1576if (params -> ith != 0 ) { 1577return ; 1578 } 1579 1580assert (src0 -> nb [0 ]== sizeof (float )); 1581assert (dst -> nb [0 ]== sizeof (float )); 1582 1583const int64_t ne00 = src0 -> ne [0 ]; 1584const int64_t ne01 = src0 -> ne [1 ]; 1585 1586const size_t nb01 = src0 -> nb [1 ]; 1587const size_t nb0 = dst -> nb [0 ]; 1588 1589for (int64_t i1 = 0 ;i1 < ne01 ;i1 ++ ) { 1590float * src = (float * ) ((char * )src0 -> data + i1 * nb01 ); 1591int32_t * dst_ = (int32_t * ) ((char * )dst -> data + i1 * nb0 ); 1592int v = 0 ; 1593ggml_vec_argmax_f32 (ne00 ,& v ,src ); 1594dst_ [0 ]= v ; 1595 } 1596} 1597 1598void ggml_compute_forward_argmax ( 1599const ggml_compute_params * params , 1600ggml_tensor * dst ) { 1601 1602const ggml_tensor * src0 = dst -> src [0 ]; 1603 1604switch (src0 -> type ) { 1605case GGML_TYPE_F32 : 1606 { 1607ggml_compute_forward_argmax_f32 (params ,dst ); 1608 }break ; 1609default : 1610 { 1611GGML_ABORT ("fatal error" ); 1612 } 1613 } 1614} 1615 1616// ggml_compute_forward_count_equal 1617 1618static void ggml_compute_forward_count_equal_i32 ( 1619const ggml_compute_params * params , 1620ggml_tensor * dst ) { 1621 1622const ggml_tensor * src0 = dst -> src [0 ]; 1623const ggml_tensor * src1 = dst -> src [1 ]; 1624 1625GGML_TENSOR_BINARY_OP_LOCALS ; 1626 1627GGML_ASSERT (src0 -> type == GGML_TYPE_I32 ); 1628GGML_ASSERT (src1 -> type == GGML_TYPE_I32 ); 1629GGML_ASSERT (ggml_are_same_shape (src0 ,src1 )); 1630GGML_ASSERT (ggml_is_scalar (dst )); 1631GGML_ASSERT (dst -> type == GGML_TYPE_I64 ); 1632 1633const int64_t nr = ggml_nrows (src0 ); 1634 1635const int ith = params -> ith ; 1636const int nth = params -> nth ; 1637 1638int64_t * sums = (int64_t * )params -> wdata ; 1639int64_t sum_thread = 0 ; 1640 1641// rows per thread 1642const int64_t dr = (nr + nth - 1 )/nth ; 1643 1644// row range for this thread 1645const int64_t ir0 = dr * ith ; 1646const int64_t ir1 = MIN (ir0 + dr ,nr ); 1647 1648for (int64_t ir = ir0 ;ir < ir1 ;++ ir ) { 1649const int64_t i03 = ir / (ne02 * ne01 ); 1650const int64_t i02 = (ir - i03 * ne03 ) /ne01 ; 1651const int64_t i01 = ir - i03 * ne03 - i02 * ne02 ; 1652 1653const char * data0 = (const char * )src0 -> data + i03 * nb03 + i02 * nb02 + i01 * nb01 ; 1654const char * data1 = (const char * )src1 -> data + i03 * nb13 + i02 * nb12 + i01 * nb11 ; 1655 1656for (int64_t i00 = 0 ;i00 < ne00 ;++ i00 ) { 1657const int32_t val0 = * ((const int32_t * ) (data0 + i00 * nb00 )); 1658const int32_t val1 = * ((const int32_t * ) (data1 + i00 * nb10 )); 1659 1660sum_thread += val0 == val1 ; 1661 } 1662 } 1663if (ith != 0 ) { 1664sums [ith ]= sum_thread ; 1665 } 1666ggml_barrier (params -> threadpool ); 1667 1668if (ith != 0 ) { 1669return ; 1670 } 1671 1672for (int ith_other = 1 ;ith_other < nth ;++ ith_other ) { 1673sum_thread += sums [ith_other ]; 1674 } 1675* ((int64_t * )dst -> data )= sum_thread ; 1676} 1677 1678void ggml_compute_forward_count_equal ( 1679const ggml_compute_params * params , 1680ggml_tensor * dst ) { 1681 1682const ggml_tensor * src0 = dst -> src [0 ]; 1683 1684switch (src0 -> type ) { 1685case GGML_TYPE_I32 : 1686 { 1687ggml_compute_forward_count_equal_i32 (params ,dst ); 1688 }break ; 1689default : 1690 { 1691GGML_ABORT ("fatal error" ); 1692 } 1693 } 1694} 1695 1696// ggml_compute_forward_repeat 1697 1698static void ggml_compute_forward_repeat_f32 ( 1699const ggml_compute_params * params , 1700ggml_tensor * dst ) { 1701 1702const ggml_tensor * src0 = dst -> src [0 ]; 1703 1704if (params -> ith != 0 ) { 1705return ; 1706 } 1707 1708GGML_ASSERT (ggml_can_repeat (src0 ,dst )); 1709 1710GGML_TENSOR_UNARY_OP_LOCALS 1711 1712// guaranteed to be an integer due to the check in ggml_can_repeat 1713const int nr0 = (int )(ne0 /ne00 ); 1714const int nr1 = (int )(ne1 /ne01 ); 1715const int nr2 = (int )(ne2 /ne02 ); 1716const int nr3 = (int )(ne3 /ne03 ); 1717 1718// TODO: support for transposed / permuted tensors 1719GGML_ASSERT (nb0 == sizeof (float )); 1720GGML_ASSERT (nb00 == sizeof (float )); 1721 1722// TODO: maybe this is not optimal? 1723for (int i3 = 0 ;i3 < nr3 ;i3 ++ ) { 1724for (int k3 = 0 ;k3 < ne03 ;k3 ++ ) { 1725for (int i2 = 0 ;i2 < nr2 ;i2 ++ ) { 1726for (int k2 = 0 ;k2 < ne02 ;k2 ++ ) { 1727for (int i1 = 0 ;i1 < nr1 ;i1 ++ ) { 1728for (int k1 = 0 ;k1 < ne01 ;k1 ++ ) { 1729for (int i0 = 0 ;i0 < nr0 ;i0 ++ ) { 1730ggml_vec_cpy_f32 (ne00 , 1731 (float * ) ((char * )dst -> data + (i3 * ne03 + k3 )* nb3 + (i2 * ne02 + k2 )* nb2 + (i1 * ne01 + k1 )* nb1 + (i0 * ne00 )* nb0 ), 1732 (float * ) ((char * )src0 -> data + (k3 )* nb03 + (k2 )* nb02 + (k1 )* nb01 )); 1733 } 1734 } 1735 } 1736 } 1737 } 1738 } 1739 } 1740} 1741 1742static void ggml_compute_forward_repeat_f16 ( 1743const ggml_compute_params * params , 1744ggml_tensor * dst ) { 1745 1746const ggml_tensor * src0 = dst -> src [0 ]; 1747 1748if (params -> ith != 0 ) { 1749return ; 1750 } 1751 1752GGML_ASSERT (ggml_can_repeat (src0 ,dst )); 1753 1754GGML_TENSOR_UNARY_OP_LOCALS 1755 1756// guaranteed to be an integer due to the check in ggml_can_repeat 1757const int nr0 = (int )(ne0 /ne00 ); 1758const int nr1 = (int )(ne1 /ne01 ); 1759const int nr2 = (int )(ne2 /ne02 ); 1760const int nr3 = (int )(ne3 /ne03 ); 1761 1762// TODO: support for transposed / permuted tensors 1763GGML_ASSERT (nb0 == sizeof (ggml_fp16_t )); 1764GGML_ASSERT (nb00 == sizeof (ggml_fp16_t )); 1765 1766// TODO: maybe this is not optimal? 1767for (int i3 = 0 ;i3 < nr3 ;i3 ++ ) { 1768for (int k3 = 0 ;k3 < ne03 ;k3 ++ ) { 1769for (int i2 = 0 ;i2 < nr2 ;i2 ++ ) { 1770for (int k2 = 0 ;k2 < ne02 ;k2 ++ ) { 1771for (int i1 = 0 ;i1 < nr1 ;i1 ++ ) { 1772for (int k1 = 0 ;k1 < ne01 ;k1 ++ ) { 1773for (int i0 = 0 ;i0 < nr0 ;i0 ++ ) { 1774ggml_fp16_t * y = (ggml_fp16_t * ) ((char * )dst -> data + (i3 * ne03 + k3 )* nb3 + (i2 * ne02 + k2 )* nb2 + (i1 * ne01 + k1 )* nb1 + (i0 * ne00 )* nb0 ); 1775ggml_fp16_t * x = (ggml_fp16_t * ) ((char * )src0 -> data + (k3 )* nb03 + (k2 )* nb02 + (k1 )* nb01 ); 1776// ggml_vec_cpy_f16(ne00, y, x) 1777for (int i = 0 ;i < ne00 ;++ i ) { 1778y [i ]= x [i ]; 1779 } 1780 } 1781 } 1782 } 1783 } 1784 } 1785 } 1786 } 1787} 1788 1789void ggml_compute_forward_repeat ( 1790const ggml_compute_params * params , 1791ggml_tensor * dst ) { 1792 1793const ggml_tensor * src0 = dst -> src [0 ]; 1794 1795switch (src0 -> type ) { 1796case GGML_TYPE_F16 : 1797case GGML_TYPE_BF16 : 1798case GGML_TYPE_I16 : 1799 { 1800ggml_compute_forward_repeat_f16 (params ,dst ); 1801 }break ; 1802case GGML_TYPE_F32 : 1803case GGML_TYPE_I32 : 1804 { 1805ggml_compute_forward_repeat_f32 (params ,dst ); 1806 }break ; 1807// TODO: templateify the implementation and support for I64 1808// ref https://github.com/ggml-org/llama.cpp/pull/14274#discussion_r2169492225 1809//case GGML_TYPE_I64: 1810// { 1811// ggml_compute_forward_repeat_i64(params, dst); 1812// } break; 1813default : 1814 { 1815GGML_ABORT ("fatal error" ); 1816 } 1817 } 1818} 1819 1820// ggml_compute_forward_repeat_back 1821 1822static void ggml_compute_forward_repeat_back_f32 ( 1823const ggml_compute_params * params , 1824ggml_tensor * dst ) { 1825 1826const ggml_tensor * src0 = dst -> src [0 ]; 1827 1828if (params -> ith != 0 ) { 1829return ; 1830 } 1831 1832GGML_ASSERT (ggml_can_repeat (dst ,src0 )); 1833 1834GGML_TENSOR_UNARY_OP_LOCALS 1835 1836// guaranteed to be an integer due to the check in ggml_can_repeat 1837const int nr0 = (int )(ne00 /ne0 ); 1838const int nr1 = (int )(ne01 /ne1 ); 1839const int nr2 = (int )(ne02 /ne2 ); 1840const int nr3 = (int )(ne03 /ne3 ); 1841 1842// TODO: support for transposed / permuted tensors 1843GGML_ASSERT (nb0 == sizeof (float )); 1844GGML_ASSERT (nb00 == sizeof (float )); 1845 1846if (ggml_is_contiguous (dst )) { 1847ggml_vec_set_f32 (ne0 * ne1 * ne2 * ne3 , (float * )dst -> data ,0 ); 1848 }else { 1849for (int k3 = 0 ;k3 < ne3 ;k3 ++ ) { 1850for (int k2 = 0 ;k2 < ne2 ;k2 ++ ) { 1851for (int k1 = 0 ;k1 < ne1 ;k1 ++ ) { 1852ggml_vec_set_f32 (ne0 , 1853 (float * ) ((char * )dst -> data + k1 * nb1 + k2 * nb2 + k3 * nb3 ), 18540 ); 1855 } 1856 } 1857 } 1858 } 1859 1860// TODO: maybe this is not optimal? 1861for (int i3 = 0 ;i3 < nr3 ;i3 ++ ) { 1862for (int k3 = 0 ;k3 < ne3 ;k3 ++ ) { 1863for (int i2 = 0 ;i2 < nr2 ;i2 ++ ) { 1864for (int k2 = 0 ;k2 < ne2 ;k2 ++ ) { 1865for (int i1 = 0 ;i1 < nr1 ;i1 ++ ) { 1866for (int k1 = 0 ;k1 < ne1 ;k1 ++ ) { 1867for (int i0 = 0 ;i0 < nr0 ;i0 ++ ) { 1868ggml_vec_acc_f32 (ne0 , 1869 (float * ) ((char * )dst -> data + (k3 )* nb3 + (k2 )* nb2 + (k1 )* nb1 ), 1870 (float * ) ((char * )src0 -> data + (i3 * ne3 + k3 )* nb03 + (i2 * ne2 + k2 )* nb02 + (i1 * ne1 + k1 )* nb01 + (i0 * ne0 )* nb00 )); 1871 } 1872 } 1873 } 1874 } 1875 } 1876 } 1877 } 1878} 1879 1880void ggml_compute_forward_repeat_back ( 1881const ggml_compute_params * params , 1882ggml_tensor * dst ) { 1883 1884const ggml_tensor * src0 = dst -> src [0 ]; 1885 1886switch (src0 -> type ) { 1887case GGML_TYPE_F32 : 1888 { 1889ggml_compute_forward_repeat_back_f32 (params ,dst ); 1890 }break ; 1891default : 1892 { 1893GGML_ABORT ("fatal error" ); 1894 } 1895 } 1896} 1897 1898// ggml_compute_forward_concat 1899static void ggml_compute_forward_concat_any ( 1900const ggml_compute_params * params , 1901ggml_tensor * dst ) { 1902 1903const ggml_tensor * src0 = dst -> src [0 ]; 1904const ggml_tensor * src1 = dst -> src [1 ]; 1905 1906const int ith = params -> ith ; 1907const int nth = params -> nth ; 1908 1909GGML_TENSOR_BINARY_OP_LOCALS 1910 1911const int32_t dim = ggml_get_op_params_i32 (dst ,0 ); 1912 1913GGML_ASSERT (dim >=0 && dim < 4 ); 1914GGML_ASSERT (ggml_is_contiguous_rows (src0 )); 1915GGML_ASSERT (ggml_is_contiguous_rows (src1 )); 1916 1917int64_t o [4 ]= {0 ,0 ,0 ,0 }; 1918 1919if (dim == 0 ) { 1920GGML_ASSERT (src0 -> ne [0 ] %ggml_blck_size (src0 -> type )== 0 ); 1921GGML_ASSERT (src1 -> ne [0 ] %ggml_blck_size (src1 -> type )== 0 ); 1922 1923o [dim ]= src0 -> ne [dim ]/ggml_blck_size (src0 -> type ); 1924 }else { 1925o [dim ]= src0 -> ne [dim ]; 1926 } 1927 1928// Region 1: copy rows from src0 1929for (int i3 = 0 ;i3 < ne03 ;i3 ++ ) { 1930for (int i2 = ith ;i2 < ne02 ;i2 += nth ) { 1931for (int i1 = 0 ;i1 < ne01 ;i1 ++ ) { 1932const char * x = (const char * )src0 -> data + i1 * nb01 + i2 * nb02 + i3 * nb03 ; 1933char * y = (char * )dst -> data + i1 * nb1 + i2 * nb2 + i3 * nb3 ; 1934memcpy (y ,x ,ggml_row_size (src0 -> type ,ne00 )); 1935 } 1936 } 1937 } 1938 1939// Region 2: copy rows from src1, offset into dst by o[] 1940for (int i3 = 0 ;i3 < ne13 ;i3 ++ ) { 1941for (int i2 = ith ;i2 < ne12 ;i2 += nth ) { 1942for (int i1 = 0 ;i1 < ne11 ;i1 ++ ) { 1943const char * x = (const char * )src1 -> data + i1 * nb11 + i2 * nb12 + i3 * nb13 ; 1944char * y = (char * )dst -> data + (i1 + o [1 ])* nb1 + (i2 + o [2 ])* nb2 + (i3 + o [3 ])* nb3 + o [0 ]* nb0 ; 1945memcpy (y ,x ,ggml_row_size (src1 -> type ,ne10 )); 1946 } 1947 } 1948 } 1949} 1950 1951static void ggml_compute_forward_concat_i8 ( 1952const ggml_compute_params * params , 1953ggml_tensor * dst ) { 1954 1955const ggml_tensor * src0 = dst -> src [0 ]; 1956const ggml_tensor * src1 = dst -> src [1 ]; 1957 1958GGML_ASSERT (ggml_type_size (src0 -> type )== sizeof (int8_t )); 1959 1960const int ith = params -> ith ; 1961const int nth = params -> nth ; 1962 1963GGML_TENSOR_BINARY_OP_LOCALS 1964 1965const int32_t dim = ggml_get_op_params_i32 (dst ,0 ); 1966 1967GGML_ASSERT (dim >=0 && dim < 4 ); 1968 1969int64_t o [4 ]= {0 ,0 ,0 ,0 }; 1970o [dim ]= src0 -> ne [dim ]; 1971 1972const int8_t * x ; 1973 1974// TODO: smarter multi-theading 1975for (int i3 = 0 ;i3 < ne3 ;i3 ++ ) { 1976for (int i2 = ith ;i2 < ne2 ;i2 += nth ) { 1977for (int i1 = 0 ;i1 < ne1 ;i1 ++ ) { 1978for (int i0 = 0 ;i0 < ne0 ;i0 ++ ) { 1979if (i0 < ne00 && i1 < ne01 && i2 < ne02 && i3 < ne03 ) { 1980x = (const int8_t * ) ((const char * )src0 -> data + (i0 )* nb00 + (i1 )* nb01 + (i2 )* nb02 + (i3 )* nb03 ); 1981 }else { 1982x = (const int8_t * ) ((const char * )src1 -> data + (i0 - o [0 ])* nb10 + (i1 - o [1 ])* nb11 + (i2 - o [2 ])* nb12 + (i3 - o [3 ])* nb13 ); 1983 } 1984 1985int8_t * y = (int8_t * )((char * )dst -> data + i0 * nb0 + i1 * nb1 + i2 * nb2 + i3 * nb3 ); 1986 1987* y = * x ; 1988 } 1989 } 1990 } 1991 } 1992} 1993 1994static void ggml_compute_forward_concat_f16 ( 1995const ggml_compute_params * params , 1996ggml_tensor * dst ) { 1997 1998const ggml_tensor * src0 = dst -> src [0 ]; 1999const ggml_tensor * src1 = dst -> src [1 ]; 2000 2001GGML_ASSERT (ggml_type_size (src0 -> type )== sizeof (ggml_fp16_t )); 2002 2003const int ith = params -> ith ; 2004const int nth = params -> nth ; 2005 2006GGML_TENSOR_BINARY_OP_LOCALS 2007 2008const int32_t dim = ggml_get_op_params_i32 (dst ,0 ); 2009 2010GGML_ASSERT (dim >=0 && dim < 4 ); 2011 2012int64_t o [4 ]= {0 ,0 ,0 ,0 }; 2013o [dim ]= src0 -> ne [dim ]; 2014 2015const ggml_fp16_t * x ; 2016 2017// TODO: smarter multi-theading 2018for (int i3 = 0 ;i3 < ne3 ;i3 ++ ) { 2019for (int i2 = ith ;i2 < ne2 ;i2 += nth ) { 2020for (int i1 = 0 ;i1 < ne1 ;i1 ++ ) { 2021for (int i0 = 0 ;i0 < ne0 ;i0 ++ ) { 2022if (i0 < ne00 && i1 < ne01 && i2 < ne02 && i3 < ne03 ) { 2023x = (const ggml_fp16_t * ) ((const char * )src0 -> data + (i0 )* nb00 + (i1 )* nb01 + (i2 )* nb02 + (i3 )* nb03 ); 2024 }else { 2025x = (const ggml_fp16_t * ) ((const char * )src1 -> data + (i0 - o [0 ])* nb10 + (i1 - o [1 ])* nb11 + (i2 - o [2 ])* nb12 + (i3 - o [3 ])* nb13 ); 2026 } 2027 2028ggml_fp16_t * y = (ggml_fp16_t * )((char * )dst -> data + i0 * nb0 + i1 * nb1 + i2 * nb2 + i3 * nb3 ); 2029 2030* y = * x ; 2031 } 2032 } 2033 } 2034 } 2035} 2036 2037static void ggml_compute_forward_concat_f32 ( 2038const ggml_compute_params * params , 2039ggml_tensor * dst ) { 2040 2041const ggml_tensor * src0 = dst -> src [0 ]; 2042const ggml_tensor * src1 = dst -> src [1 ]; 2043 2044GGML_ASSERT (ggml_type_size (src0 -> type )== sizeof (float )); 2045 2046const int ith = params -> ith ; 2047const int nth = params -> nth ; 2048 2049GGML_TENSOR_BINARY_OP_LOCALS 2050 2051const int32_t dim = ggml_get_op_params_i32 (dst ,0 ); 2052 2053GGML_ASSERT (dim >=0 && dim < 4 ); 2054 2055int64_t o [4 ]= {0 ,0 ,0 ,0 }; 2056o [dim ]= src0 -> ne [dim ]; 2057 2058const float * x ; 2059 2060// TODO: smarter multi-theading 2061for (int i3 = 0 ;i3 < ne3 ;i3 ++ ) { 2062for (int i2 = ith ;i2 < ne2 ;i2 += nth ) { 2063for (int i1 = 0 ;i1 < ne1 ;i1 ++ ) { 2064for (int i0 = 0 ;i0 < ne0 ;i0 ++ ) { 2065if (i0 < ne00 && i1 < ne01 && i2 < ne02 && i3 < ne03 ) { 2066x = (const float * ) ((const char * )src0 -> data + (i0 )* nb00 + (i1 )* nb01 + (i2 )* nb02 + (i3 )* nb03 ); 2067 }else { 2068x = (const float * ) ((const char * )src1 -> data + (i0 - o [0 ])* nb10 + (i1 - o [1 ])* nb11 + (i2 - o [2 ])* nb12 + (i3 - o [3 ])* nb13 ); 2069 } 2070 2071float * y = (float * )((char * )dst -> data + i0 * nb0 + i1 * nb1 + i2 * nb2 + i3 * nb3 ); 2072 2073* y = * x ; 2074 } 2075 } 2076 } 2077 } 2078} 2079 2080void ggml_compute_forward_concat ( 2081const ggml_compute_params * params , 2082ggml_tensor * dst ) { 2083 2084const ggml_tensor * src0 = dst -> src [0 ]; 2085 2086switch (src0 -> type ) { 2087case GGML_TYPE_F16 : 2088case GGML_TYPE_BF16 : 2089case GGML_TYPE_I16 : 2090 { 2091ggml_compute_forward_concat_f16 (params ,dst ); 2092 }break ; 2093case GGML_TYPE_I8 : 2094 { 2095ggml_compute_forward_concat_i8 (params ,dst ); 2096 }break ; 2097case GGML_TYPE_F32 : 2098case GGML_TYPE_I32 : 2099 { 2100ggml_compute_forward_concat_f32 (params ,dst ); 2101 }break ; 2102default : 2103 { 2104ggml_compute_forward_concat_any (params ,dst ); 2105 } 2106 } 2107} 2108 2109// ggml_compute_forward_gelu 2110 2111static void ggml_compute_forward_gelu_f32 ( 2112const ggml_compute_params * params , 2113ggml_tensor * dst ) { 2114 2115const ggml_tensor * src0 = dst -> src [0 ]; 2116 2117assert (ggml_is_contiguous_rows (src0 )); 2118assert (ggml_are_same_shape (src0 ,dst )); 2119 2120GGML_TENSOR_LOCALS (int64_t ,ne0 ,src0 ,ne ) 2121GGML_TENSOR_LOCALS (size_t ,nb0 ,src0 ,nb ) 2122GGML_TENSOR_LOCALS (int64_t ,ne ,dst ,ne ) 2123GGML_TENSOR_LOCALS (size_t ,nb ,dst ,nb ) 2124 2125const int ith = params -> ith ; 2126const int nth = params -> nth ; 2127 2128const int nc = src0 -> ne [0 ]; 2129const int nr = ggml_nrows (src0 ); 2130 2131// rows per thread 2132const int dr = (nr + nth - 1 )/nth ; 2133 2134// row range for this thread 2135const int ir0 = dr * ith ; 2136const int ir1 = MIN (ir0 + dr ,nr ); 2137 2138for (int ir = ir0 ;ir < ir1 ;++ ir ) { 2139const int i3 = ir /(ne02 * ne01 ); 2140const int i2 = (ir - i3 * ne02 * ne01 )/ne01 ; 2141const int i1 = (ir - i3 * ne02 * ne01 - i2 * ne01 ); 2142 2143ggml_vec_gelu_f32 (nc , 2144 (float * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * nb1 ), 2145 (float * ) ((char * )src0 -> data + i3 * nb03 + i2 * nb02 + i1 * nb01 )); 2146 2147#ifndef NDEBUG 2148for (int k = 0 ;k < nc ;k ++ ) { 2149const float x = ((float * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * (dst -> nb [1 ])))[k ]; 2150GGML_UNUSED (x ); 2151assert (!isnan (x )); 2152assert (!isinf (x )); 2153 } 2154#endif // NDEBUG 2155 } 2156} 2157 2158static void ggml_compute_forward_gelu_f16 ( 2159const ggml_compute_params * params , 2160ggml_tensor * dst ) { 2161 2162const ggml_tensor * src0 = dst -> src [0 ]; 2163 2164assert (ggml_is_contiguous_rows (src0 )); 2165assert (ggml_are_same_shape (src0 ,dst )); 2166 2167GGML_TENSOR_LOCALS (int64_t ,ne0 ,src0 ,ne ) 2168GGML_TENSOR_LOCALS (size_t ,nb0 ,src0 ,nb ) 2169GGML_TENSOR_LOCALS (int64_t ,ne ,dst ,ne ) 2170GGML_TENSOR_LOCALS (size_t ,nb ,dst ,nb ) 2171 2172const int ith = params -> ith ; 2173const int nth = params -> nth ; 2174 2175const int nc = src0 -> ne [0 ]; 2176const int nr = ggml_nrows (src0 ); 2177 2178// rows per thread 2179const int dr = (nr + nth - 1 )/nth ; 2180 2181// row range for this thread 2182const int ir0 = dr * ith ; 2183const int ir1 = MIN (ir0 + dr ,nr ); 2184 2185for (int ir = ir0 ;ir < ir1 ;++ ir ) { 2186const int i3 = ir /(ne02 * ne01 ); 2187const int i2 = (ir - i3 * ne02 * ne01 )/ne01 ; 2188const int i1 = (ir - i3 * ne02 * ne01 - i2 * ne01 ); 2189 2190ggml_vec_gelu_f16 (nc , 2191 (ggml_fp16_t * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * nb1 ), 2192 (ggml_fp16_t * ) ((char * )src0 -> data + i3 * nb03 + i2 * nb02 + i1 * nb01 )); 2193 2194#ifndef NDEBUG 2195for (int k = 0 ;k < nc ;k ++ ) { 2196const ggml_fp16_t x = ((ggml_fp16_t * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * (dst -> nb [1 ])))[k ]; 2197const float v = GGML_CPU_FP16_TO_FP32 (x ); 2198GGML_UNUSED (v ); 2199assert (!isnan (v )); 2200assert (!isinf (v )); 2201 } 2202#endif // NDEBUG 2203 } 2204} 2205 2206static void ggml_compute_forward_gelu ( 2207const ggml_compute_params * params , 2208ggml_tensor * dst ) { 2209 2210const ggml_tensor * src0 = dst -> src [0 ]; 2211 2212switch (src0 -> type ) { 2213case GGML_TYPE_F32 : 2214 { 2215ggml_compute_forward_gelu_f32 (params ,dst ); 2216 }break ; 2217case GGML_TYPE_F16 : 2218 { 2219ggml_compute_forward_gelu_f16 (params ,dst ); 2220 }break ; 2221default : 2222 { 2223GGML_ABORT ("fatal error" ); 2224 } 2225 } 2226} 2227 2228// ggml_compute_fill 2229 2230static void ggml_compute_forward_fill_f32 (const ggml_compute_params * params ,ggml_tensor * dst ) { 2231const float c = ggml_get_op_params_f32 (dst ,0 ); 2232 2233GGML_TENSOR_LOCALS (int64_t ,ne ,dst ,ne ); 2234GGML_TENSOR_LOCALS (size_t ,nb ,dst ,nb ); 2235 2236const auto [ir0 ,ir1 ]= get_thread_range (params ,dst ); 2237 2238for (int64_t ir = ir0 ;ir < ir1 ;++ ir ) { 2239const int64_t i03 = ir /(ne2 * ne1 ); 2240const int64_t i02 = (ir - i03 * ne2 * ne1 )/ne1 ; 2241const int64_t i01 = (ir - i03 * ne2 * ne1 - i02 * ne1 ); 2242 2243float * dst_ptr = (float * ) ((char * )dst -> data + i03 * nb3 + i02 * nb2 + i01 * nb1 ); 2244 2245ggml_vec_set_f32 (ne0 ,dst_ptr ,c ); 2246 } 2247} 2248 2249static void ggml_compute_forward_fill_f16 (const ggml_compute_params * params ,ggml_tensor * dst ) { 2250const ggml_fp16_t c = GGML_CPU_FP32_TO_FP16 (ggml_get_op_params_f32 (dst ,0 )); 2251 2252GGML_TENSOR_LOCALS (int64_t ,ne ,dst ,ne ); 2253GGML_TENSOR_LOCALS (size_t ,nb ,dst ,nb ); 2254 2255const auto [ir0 ,ir1 ]= get_thread_range (params ,dst ); 2256 2257for (int64_t ir = ir0 ;ir < ir1 ;++ ir ) { 2258const int64_t i03 = ir /(ne2 * ne1 ); 2259const int64_t i02 = (ir - i03 * ne2 * ne1 )/ne1 ; 2260const int64_t i01 = (ir - i03 * ne2 * ne1 - i02 * ne1 ); 2261 2262ggml_fp16_t * dst_ptr = (ggml_fp16_t * ) ((char * )dst -> data + i03 * nb3 + i02 * nb2 + i01 * nb1 ); 2263 2264ggml_vec_set_f16 (ne0 ,dst_ptr ,c ); 2265 } 2266} 2267 2268void ggml_compute_forward_fill (const ggml_compute_params * params ,ggml_tensor * dst ) { 2269const ggml_tensor * src0 = dst -> src [0 ]; 2270 2271switch (src0 -> type ) { 2272case GGML_TYPE_F32 : 2273 { 2274ggml_compute_forward_fill_f32 (params ,dst ); 2275 }break ; 2276case GGML_TYPE_F16 : 2277 { 2278ggml_compute_forward_fill_f16 (params ,dst ); 2279 }break ; 2280default : 2281 { 2282GGML_ABORT ("unsupported type for ggml_compute_forward_fill: %s" ,ggml_type_name (src0 -> type )); 2283 } 2284 } 2285} 2286 2287// ggml_compute_tri 2288 2289static void ggml_compute_forward_tri_f32 (const ggml_compute_params * params ,ggml_tensor * dst ) { 2290const ggml_tensor * src0 = dst -> src [0 ]; 2291 2292const ggml_tri_type ttype = (ggml_tri_type )ggml_get_op_params_i32 (dst ,0 ); 2293 2294GGML_ASSERT (ggml_is_contiguous (src0 )); 2295 2296GGML_TENSOR_UNARY_OP_LOCALS 2297 2298const auto [ir0 ,ir1 ]= get_thread_range (params ,src0 ); 2299 2300bool (* bipred )(int ,int ); 2301 2302switch (ttype ) { 2303case GGML_TRI_TYPE_LOWER :bipred = [](int i ,int r ) {return i < r ; };break ; 2304case GGML_TRI_TYPE_LOWER_DIAG :bipred = [](int i ,int r ) {return i <=r ; };break ; 2305case GGML_TRI_TYPE_UPPER :bipred = [](int i ,int r ) {return i > r ; };break ; 2306case GGML_TRI_TYPE_UPPER_DIAG :bipred = [](int i ,int r ) {return i >=r ; };break ; 2307default :GGML_ABORT ("invalid tri type" ); 2308 } 2309 2310for (int64_t ir = ir0 ;ir < ir1 ;++ ir ) { 2311const int64_t i03 = ir /(ne02 * ne01 ); 2312const int64_t i02 = (ir - i03 * ne02 * ne01 )/ne01 ; 2313const int64_t i01 = (ir - i03 * ne02 * ne01 - i02 * ne01 ); 2314 2315const float * src_ptr = (const float * ) ((const char * )src0 -> data + i03 * nb03 + i02 * nb02 + i01 * nb01 ); 2316float * dst_ptr = (float * ) ((char * )dst -> data + i03 * nb3 + i02 * nb2 + i01 * nb1 ); 2317 2318for (int i0 = 0 ;i0 < ne0 ;++ i0 ) { 2319dst_ptr [i0 ]= bipred (i0 ,i01 ) ?src_ptr [i0 ] :0.0f ; 2320 } 2321 } 2322} 2323 2324void ggml_compute_forward_tri (const ggml_compute_params * params ,ggml_tensor * dst ) { 2325const ggml_tensor * src0 = dst -> src [0 ]; 2326 2327switch (src0 -> type ) { 2328case GGML_TYPE_F32 : 2329 { 2330ggml_compute_forward_tri_f32 (params ,dst ); 2331 }break ; 2332default : 2333 { 2334GGML_ABORT ("fatal error" ); 2335 } 2336 } 2337} 2338 2339// ggml_compute_forward_gelu_erf 2340 2341static void ggml_compute_forward_gelu_erf_f32 ( 2342const ggml_compute_params * params , 2343ggml_tensor * dst ) { 2344 2345const ggml_tensor * src0 = dst -> src [0 ]; 2346 2347assert (ggml_is_contiguous_rows (src0 )); 2348assert (ggml_are_same_shape (src0 ,dst )); 2349 2350GGML_TENSOR_LOCALS (int64_t ,ne0 ,src0 ,ne ) 2351GGML_TENSOR_LOCALS (size_t ,nb0 ,src0 ,nb ) 2352GGML_TENSOR_LOCALS (int64_t ,ne ,dst ,ne ) 2353GGML_TENSOR_LOCALS (size_t ,nb ,dst ,nb ) 2354 2355const int ith = params -> ith ; 2356const int nth = params -> nth ; 2357 2358const int nc = src0 -> ne [0 ]; 2359const int nr = ggml_nrows (src0 ); 2360 2361// rows per thread 2362const int dr = (nr + nth - 1 )/nth ; 2363 2364// row range for this thread 2365const int ir0 = dr * ith ; 2366const int ir1 = MIN (ir0 + dr ,nr ); 2367 2368for (int ir = ir0 ;ir < ir1 ;++ ir ) { 2369const int i3 = ir /(ne02 * ne01 ); 2370const int i2 = (ir - i3 * ne02 * ne01 )/ne01 ; 2371const int i1 = (ir - i3 * ne02 * ne01 - i2 * ne01 ); 2372 2373ggml_vec_gelu_erf_f32 (nc , 2374 (float * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * nb1 ), 2375 (float * ) ((char * )src0 -> data + i3 * nb03 + i2 * nb02 + i1 * nb01 )); 2376 2377#ifndef NDEBUG 2378for (int k = 0 ;k < nc ;k ++ ) { 2379const float x = ((float * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * (dst -> nb [1 ])))[k ]; 2380GGML_UNUSED (x ); 2381assert (!isnan (x )); 2382assert (!isinf (x )); 2383 } 2384#endif // NDEBUG 2385 } 2386} 2387 2388static void ggml_compute_forward_gelu_erf_f16 ( 2389const ggml_compute_params * params , 2390ggml_tensor * dst ) { 2391 2392const ggml_tensor * src0 = dst -> src [0 ]; 2393 2394assert (ggml_is_contiguous_rows (src0 )); 2395assert (ggml_are_same_shape (src0 ,dst )); 2396 2397GGML_TENSOR_LOCALS (int64_t ,ne0 ,src0 ,ne ) 2398GGML_TENSOR_LOCALS (size_t ,nb0 ,src0 ,nb ) 2399GGML_TENSOR_LOCALS (int64_t ,ne ,dst ,ne ) 2400GGML_TENSOR_LOCALS (size_t ,nb ,dst ,nb ) 2401 2402const int ith = params -> ith ; 2403const int nth = params -> nth ; 2404 2405const int nc = src0 -> ne [0 ]; 2406const int nr = ggml_nrows (src0 ); 2407 2408// rows per thread 2409const int dr = (nr + nth - 1 )/nth ; 2410 2411// row range for this thread 2412const int ir0 = dr * ith ; 2413const int ir1 = MIN (ir0 + dr ,nr ); 2414 2415for (int ir = ir0 ;ir < ir1 ;++ ir ) { 2416const int i3 = ir /(ne02 * ne01 ); 2417const int i2 = (ir - i3 * ne02 * ne01 )/ne01 ; 2418const int i1 = (ir - i3 * ne02 * ne01 - i2 * ne01 ); 2419 2420ggml_vec_gelu_erf_f16 (nc , 2421 (ggml_fp16_t * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * nb1 ), 2422 (ggml_fp16_t * ) ((char * )src0 -> data + i3 * nb03 + i2 * nb02 + i1 * nb01 )); 2423 2424#ifndef NDEBUG 2425for (int k = 0 ;k < nc ;k ++ ) { 2426const ggml_fp16_t x = ((ggml_fp16_t * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * (dst -> nb [1 ])))[k ]; 2427const float v = GGML_CPU_FP16_TO_FP32 (x ); 2428GGML_UNUSED (v ); 2429assert (!isnan (v )); 2430assert (!isinf (v )); 2431 } 2432#endif // NDEBUG 2433 } 2434} 2435 2436static void ggml_compute_forward_gelu_erf ( 2437const ggml_compute_params * params , 2438ggml_tensor * dst ) { 2439 2440const ggml_tensor * src0 = dst -> src [0 ]; 2441 2442switch (src0 -> type ) { 2443case GGML_TYPE_F32 : 2444 { 2445ggml_compute_forward_gelu_erf_f32 (params ,dst ); 2446 }break ; 2447case GGML_TYPE_F16 : 2448 { 2449ggml_compute_forward_gelu_erf_f16 (params ,dst ); 2450 }break ; 2451default : 2452 { 2453GGML_ABORT ("fatal error" ); 2454 } 2455 } 2456} 2457 2458// ggml_compute_forward_gelu_quick 2459 2460static void ggml_compute_forward_gelu_quick_f32 ( 2461const ggml_compute_params * params , 2462ggml_tensor * dst ) { 2463 2464const ggml_tensor * src0 = dst -> src [0 ]; 2465 2466assert (ggml_is_contiguous_rows (src0 )); 2467assert (ggml_are_same_shape (src0 ,dst )); 2468 2469GGML_TENSOR_LOCALS (int64_t ,ne0 ,src0 ,ne ) 2470GGML_TENSOR_LOCALS (size_t ,nb0 ,src0 ,nb ) 2471GGML_TENSOR_LOCALS (int64_t ,ne ,dst ,ne ) 2472GGML_TENSOR_LOCALS (size_t ,nb ,dst ,nb ) 2473 2474const int ith = params -> ith ; 2475const int nth = params -> nth ; 2476 2477const int nc = src0 -> ne [0 ]; 2478const int nr = ggml_nrows (src0 ); 2479 2480// rows per thread 2481const int dr = (nr + nth - 1 )/nth ; 2482 2483// row range for this thread 2484const int ir0 = dr * ith ; 2485const int ir1 = MIN (ir0 + dr ,nr ); 2486 2487for (int ir = ir0 ;ir < ir1 ;++ ir ) { 2488const int i3 = ir /(ne02 * ne01 ); 2489const int i2 = (ir - i3 * ne02 * ne01 )/ne01 ; 2490const int i1 = (ir - i3 * ne02 * ne01 - i2 * ne01 ); 2491 2492ggml_vec_gelu_quick_f32 (nc , 2493 (float * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * nb1 ), 2494 (float * ) ((char * )src0 -> data + i3 * nb03 + i2 * nb02 + i1 * nb01 )); 2495 2496#ifndef NDEBUG 2497for (int k = 0 ;k < nc ;k ++ ) { 2498const float x = ((float * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * (dst -> nb [1 ])))[k ]; 2499GGML_UNUSED (x ); 2500assert (!isnan (x )); 2501assert (!isinf (x )); 2502 } 2503#endif // NDEBUG 2504 } 2505} 2506 2507static void ggml_compute_forward_gelu_quick_f16 ( 2508const ggml_compute_params * params , 2509ggml_tensor * dst ) { 2510 2511const ggml_tensor * src0 = dst -> src [0 ]; 2512 2513assert (ggml_is_contiguous_rows (src0 )); 2514assert (ggml_are_same_shape (src0 ,dst )); 2515 2516GGML_TENSOR_LOCALS (int64_t ,ne0 ,src0 ,ne ) 2517GGML_TENSOR_LOCALS (size_t ,nb0 ,src0 ,nb ) 2518GGML_TENSOR_LOCALS (int64_t ,ne ,dst ,ne ) 2519GGML_TENSOR_LOCALS (size_t ,nb ,dst ,nb ) 2520 2521const int ith = params -> ith ; 2522const int nth = params -> nth ; 2523 2524const int nc = src0 -> ne [0 ]; 2525const int nr = ggml_nrows (src0 ); 2526 2527// rows per thread 2528const int dr = (nr + nth - 1 )/nth ; 2529 2530// row range for this thread 2531const int ir0 = dr * ith ; 2532const int ir1 = MIN (ir0 + dr ,nr ); 2533 2534for (int ir = ir0 ;ir < ir1 ;++ ir ) { 2535const int i3 = ir /(ne02 * ne01 ); 2536const int i2 = (ir - i3 * ne02 * ne01 )/ne01 ; 2537const int i1 = (ir - i3 * ne02 * ne01 - i2 * ne01 ); 2538 2539ggml_vec_gelu_quick_f16 (nc , 2540 (ggml_fp16_t * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * nb1 ), 2541 (ggml_fp16_t * ) ((char * )src0 -> data + i3 * nb03 + i2 * nb02 + i1 * nb01 )); 2542 2543#ifndef NDEBUG 2544for (int k = 0 ;k < nc ;k ++ ) { 2545const ggml_fp16_t x = ((ggml_fp16_t * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * (dst -> nb [1 ])))[k ]; 2546const float v = GGML_CPU_FP16_TO_FP32 (x ); 2547GGML_UNUSED (v ); 2548assert (!isnan (v )); 2549assert (!isinf (v )); 2550 } 2551#endif // NDEBUG 2552 } 2553} 2554 2555static void ggml_compute_forward_gelu_quick ( 2556const ggml_compute_params * params , 2557ggml_tensor * dst ) { 2558 2559const ggml_tensor * src0 = dst -> src [0 ]; 2560 2561switch (src0 -> type ) { 2562case GGML_TYPE_F32 : 2563 { 2564ggml_compute_forward_gelu_quick_f32 (params ,dst ); 2565 }break ; 2566case GGML_TYPE_F16 : 2567 { 2568ggml_compute_forward_gelu_quick_f16 (params ,dst ); 2569 }break ; 2570default : 2571 { 2572GGML_ABORT ("fatal error" ); 2573 } 2574 } 2575} 2576 2577// ggml_compute_forward_silu 2578 2579static void ggml_compute_forward_silu_f32 ( 2580const ggml_compute_params * params , 2581ggml_tensor * dst ) { 2582 2583const ggml_tensor * src0 = dst -> src [0 ]; 2584 2585assert (ggml_is_contiguous_rows (src0 )); 2586assert (ggml_are_same_shape (src0 ,dst )); 2587 2588GGML_TENSOR_LOCALS (int64_t ,ne0 ,src0 ,ne ) 2589GGML_TENSOR_LOCALS (size_t ,nb0 ,src0 ,nb ) 2590GGML_TENSOR_LOCALS (int64_t ,ne ,dst ,ne ) 2591GGML_TENSOR_LOCALS (size_t ,nb ,dst ,nb ) 2592 2593const int ith = params -> ith ; 2594const int nth = params -> nth ; 2595 2596const int nc = src0 -> ne [0 ]; 2597const int nr = ggml_nrows (src0 ); 2598 2599// rows per thread 2600const int dr = (nr + nth - 1 )/nth ; 2601 2602// row range for this thread 2603const int ir0 = dr * ith ; 2604const int ir1 = MIN (ir0 + dr ,nr ); 2605 2606for (int ir = ir0 ;ir < ir1 ;++ ir ) { 2607const int i3 = ir /(ne02 * ne01 ); 2608const int i2 = (ir - i3 * ne02 * ne01 )/ne01 ; 2609const int i1 = (ir - i3 * ne02 * ne01 - i2 * ne01 ); 2610 2611ggml_vec_silu_f32 (nc , 2612 (float * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * nb1 ), 2613 (float * ) ((char * )src0 -> data + i3 * nb03 + i2 * nb02 + i1 * nb01 )); 2614 2615#ifndef NDEBUG 2616for (int k = 0 ;k < nc ;k ++ ) { 2617const float x = ((float * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * (dst -> nb [1 ])))[k ]; 2618GGML_UNUSED (x ); 2619assert (!isnan (x )); 2620assert (!isinf (x )); 2621 } 2622#endif // NDEBUG 2623 } 2624} 2625 2626static void ggml_compute_forward_silu_f16 ( 2627const ggml_compute_params * params , 2628ggml_tensor * dst ) { 2629 2630const ggml_tensor * src0 = dst -> src [0 ]; 2631 2632assert (ggml_is_contiguous_rows (src0 )); 2633assert (ggml_are_same_shape (src0 ,dst )); 2634 2635GGML_TENSOR_LOCALS (int64_t ,ne0 ,src0 ,ne ) 2636GGML_TENSOR_LOCALS (size_t ,nb0 ,src0 ,nb ) 2637GGML_TENSOR_LOCALS (int64_t ,ne ,dst ,ne ) 2638GGML_TENSOR_LOCALS (size_t ,nb ,dst ,nb ) 2639 2640const int ith = params -> ith ; 2641const int nth = params -> nth ; 2642 2643const int nc = src0 -> ne [0 ]; 2644const int nr = ggml_nrows (src0 ); 2645 2646// rows per thread 2647const int dr = (nr + nth - 1 )/nth ; 2648 2649// row range for this thread 2650const int ir0 = dr * ith ; 2651const int ir1 = MIN (ir0 + dr ,nr ); 2652 2653for (int ir = ir0 ;ir < ir1 ;++ ir ) { 2654const int i3 = ir /(ne02 * ne01 ); 2655const int i2 = (ir - i3 * ne02 * ne01 )/ne01 ; 2656const int i1 = (ir - i3 * ne02 * ne01 - i2 * ne01 ); 2657 2658ggml_vec_silu_f16 (nc , 2659 (ggml_fp16_t * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * nb1 ), 2660 (ggml_fp16_t * ) ((char * )src0 -> data + i3 * nb03 + i2 * nb02 + i1 * nb01 )); 2661 2662#ifndef NDEBUG 2663for (int k = 0 ;k < nc ;k ++ ) { 2664const ggml_fp16_t x = ((ggml_fp16_t * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * (dst -> nb [1 ])))[k ]; 2665const float v = GGML_CPU_FP16_TO_FP32 (x ); 2666GGML_UNUSED (v ); 2667assert (!isnan (v )); 2668assert (!isinf (v )); 2669 } 2670#endif // NDEBUG 2671 } 2672} 2673 2674static void ggml_compute_forward_silu ( 2675const ggml_compute_params * params , 2676ggml_tensor * dst ) { 2677 2678const ggml_tensor * src0 = dst -> src [0 ]; 2679 2680switch (src0 -> type ) { 2681case GGML_TYPE_F32 : 2682 { 2683ggml_compute_forward_silu_f32 (params ,dst ); 2684 }break ; 2685case GGML_TYPE_F16 : 2686 { 2687ggml_compute_forward_silu_f16 (params ,dst ); 2688 }break ; 2689default : 2690 { 2691GGML_ABORT ("fatal error" ); 2692 } 2693 } 2694} 2695// ggml_compute_forward_leaky_relu 2696 2697static void ggml_compute_forward_leaky_relu_f32 ( 2698const ggml_compute_params * params , 2699ggml_tensor * dst ) { 2700 2701const ggml_tensor * src0 = dst -> src [0 ]; 2702 2703if (params -> ith != 0 ) { 2704return ; 2705 } 2706 2707assert (ggml_is_contiguous_1 (src0 )); 2708assert (ggml_is_contiguous_1 (dst )); 2709assert (ggml_are_same_shape (src0 ,dst )); 2710 2711const int n = ggml_nrows (src0 ); 2712const int nc = src0 -> ne [0 ]; 2713 2714float negative_slope ; 2715memcpy (& negative_slope ,dst -> op_params ,sizeof (float )); 2716 2717assert (dst -> nb [0 ]== sizeof (float )); 2718assert (src0 -> nb [0 ]== sizeof (float )); 2719 2720for (int i = 0 ;i < n ;i ++ ) { 2721ggml_vec_leaky_relu_f32 (nc , 2722 (float * ) ((char * )dst -> data + i * (dst -> nb [1 ])), 2723 (float * ) ((char * )src0 -> data + i * (src0 -> nb [1 ])),negative_slope ); 2724 } 2725} 2726 2727static void ggml_compute_forward_leaky_relu_f16 ( 2728const ggml_compute_params * params , 2729ggml_tensor * dst ) { 2730 2731const ggml_tensor * src0 = dst -> src [0 ]; 2732 2733if (params -> ith != 0 ) { 2734return ; 2735 } 2736 2737assert (ggml_is_contiguous_1 (src0 )); 2738assert (ggml_is_contiguous_1 (dst )); 2739assert (ggml_are_same_shape (src0 ,dst )); 2740 2741const int n = ggml_nrows (src0 ); 2742const int nc = src0 -> ne [0 ]; 2743 2744float negative_slope ; 2745memcpy (& negative_slope ,dst -> op_params ,sizeof (float )); 2746 2747assert (dst -> nb [0 ]== sizeof (ggml_fp16_t )); 2748assert (src0 -> nb [0 ]== sizeof (ggml_fp16_t )); 2749 2750for (int i = 0 ;i < n ;i ++ ) { 2751ggml_vec_leaky_relu_f16 (nc , 2752 (ggml_fp16_t * ) ((char * )dst -> data + i * (dst -> nb [1 ])), 2753 (ggml_fp16_t * ) ((char * )src0 -> data + i * (src0 -> nb [1 ])),negative_slope ); 2754 } 2755} 2756 2757void ggml_compute_forward_leaky_relu ( 2758const ggml_compute_params * params , 2759ggml_tensor * dst ) { 2760 2761const ggml_tensor * src0 = dst -> src [0 ]; 2762 2763switch (src0 -> type ) { 2764case GGML_TYPE_F32 : 2765 { 2766ggml_compute_forward_leaky_relu_f32 (params ,dst ); 2767 }break ; 2768case GGML_TYPE_F16 : 2769 { 2770ggml_compute_forward_leaky_relu_f16 (params ,dst ); 2771 }break ; 2772default : 2773 { 2774GGML_ABORT ("fatal error" ); 2775 } 2776 } 2777} 2778 2779// ggml_compute_forward_silu_back 2780 2781static void ggml_compute_forward_silu_back_f32 ( 2782const ggml_compute_params * params , 2783ggml_tensor * dst ) { 2784 2785const ggml_tensor * grad = dst -> src [0 ]; 2786const ggml_tensor * src1 = dst -> src [1 ]; 2787 2788assert (ggml_is_contiguous_1 (grad )); 2789assert (ggml_is_contiguous_1 (src1 )); 2790assert (ggml_is_contiguous_1 (dst )); 2791assert (ggml_are_same_shape (src1 ,dst )); 2792assert (ggml_are_same_shape (src1 ,grad )); 2793 2794const int ith = params -> ith ; 2795const int nth = params -> nth ; 2796 2797const int nc = src1 -> ne [0 ]; 2798const int nr = ggml_nrows (src1 ); 2799 2800// rows per thread 2801const int dr = (nr + nth - 1 )/nth ; 2802 2803// row range for this thread 2804const int ir0 = dr * ith ; 2805const int ir1 = MIN (ir0 + dr ,nr ); 2806 2807for (int i1 = ir0 ;i1 < ir1 ;i1 ++ ) { 2808ggml_vec_silu_backward_f32 (nc , 2809 (float * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])), 2810 (float * ) ((char * )src1 -> data + i1 * (src1 -> nb [1 ])), 2811 (float * ) ((char * )grad -> data + i1 * (grad -> nb [1 ]))); 2812 2813#ifndef NDEBUG 2814for (int k = 0 ;k < nc ;k ++ ) { 2815const float x = ((float * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])))[k ]; 2816GGML_UNUSED (x ); 2817assert (!isnan (x )); 2818assert (!isinf (x )); 2819 } 2820#endif // NDEBUG 2821 } 2822} 2823 2824static void ggml_compute_forward_silu_back_f16 ( 2825const ggml_compute_params * params , 2826ggml_tensor * dst ) { 2827 2828const ggml_tensor * grad = dst -> src [0 ]; 2829const ggml_tensor * src1 = dst -> src [1 ]; 2830 2831assert (ggml_is_contiguous_1 (grad )); 2832assert (ggml_is_contiguous_1 (src1 )); 2833assert (ggml_is_contiguous_1 (dst )); 2834assert (ggml_are_same_shape (src1 ,dst )); 2835assert (ggml_are_same_shape (src1 ,grad )); 2836 2837const int ith = params -> ith ; 2838const int nth = params -> nth ; 2839 2840const int nc = src1 -> ne [0 ]; 2841const int nr = ggml_nrows (src1 ); 2842 2843// rows per thread 2844const int dr = (nr + nth - 1 )/nth ; 2845 2846// row range for this thread 2847const int ir0 = dr * ith ; 2848const int ir1 = MIN (ir0 + dr ,nr ); 2849 2850for (int i1 = ir0 ;i1 < ir1 ;i1 ++ ) { 2851ggml_vec_silu_backward_f16 (nc , 2852 (ggml_fp16_t * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])), 2853 (ggml_fp16_t * ) ((char * )src1 -> data + i1 * (src1 -> nb [1 ])), 2854 (ggml_fp16_t * ) ((char * )grad -> data + i1 * (grad -> nb [1 ]))); 2855 2856#ifndef NDEBUG 2857for (int k = 0 ;k < nc ;k ++ ) { 2858const float x = ((ggml_fp16_t * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])))[k ]; 2859const float v = GGML_CPU_FP16_TO_FP32 (x ); 2860GGML_UNUSED (v ); 2861assert (!isnan (v )); 2862assert (!isinf (v )); 2863 } 2864#endif // NDEBUG 2865 } 2866} 2867 2868void ggml_compute_forward_silu_back ( 2869const ggml_compute_params * params , 2870ggml_tensor * dst ) { 2871 2872const ggml_tensor * src0 = dst -> src [0 ]; 2873 2874switch (src0 -> type ) { 2875case GGML_TYPE_F32 : 2876 { 2877ggml_compute_forward_silu_back_f32 (params ,dst ); 2878 }break ; 2879case GGML_TYPE_F16 : 2880 { 2881ggml_compute_forward_silu_back_f16 (params ,dst ); 2882 }break ; 2883default : 2884 { 2885GGML_ABORT ("fatal error" ); 2886 } 2887 } 2888} 2889 2890// ggml_compute_forward_reglu 2891 2892static void ggml_compute_forward_reglu_f32 ( 2893const ggml_compute_params * params , 2894ggml_tensor * dst ) { 2895 2896const ggml_tensor * src0 = dst -> src [0 ]; 2897const ggml_tensor * src1 = dst -> src [1 ]; 2898char * src0_d = (char * )src0 -> data ; 2899char * src1_d = (char * ) (src1 ?src1 -> data :src0 -> data ); 2900const size_t src0_o = src0 -> nb [1 ]; 2901const size_t src1_o = src1 ?src1 -> nb [1 ] :src0 -> nb [1 ]; 2902 2903GGML_ASSERT (ggml_is_contiguous_1 (src0 )); 2904GGML_ASSERT (ggml_is_contiguous_1 (dst )); 2905 2906if (src1 ) { 2907GGML_ASSERT (ggml_is_contiguous_1 (src1 )); 2908GGML_ASSERT (src0 -> type == src1 -> type ); 2909 } 2910 2911const int ith = params -> ith ; 2912const int nth = params -> nth ; 2913 2914const int nc = src1 ?src0 -> ne [0 ] :src0 -> ne [0 ] /2 ; 2915const int nr = ggml_nrows (src0 ); 2916 2917GGML_ASSERT (dst -> ne [0 ]== nc ); 2918GGML_ASSERT (ggml_nrows (dst )== nr ); 2919 2920const int32_t swapped = ggml_get_op_params_i32 (dst ,1 ); 2921 2922// rows per thread 2923const int dr = (nr + nth - 1 )/nth ; 2924 2925// row range for this thread 2926const int ir0 = dr * ith ; 2927const int ir1 = MIN (ir0 + dr ,nr ); 2928 2929for (int i1 = ir0 ;i1 < ir1 ;i1 ++ ) { 2930float * src0_p = (float * ) (src0_d + i1 * src0_o ); 2931float * src1_p = (float * ) (src1_d + i1 * src1_o ); 2932 2933if (!src1 ) { 2934src0_p += swapped ?nc :0 ; 2935src1_p += swapped ?0 :nc ; 2936 } 2937 2938ggml_vec_reglu_f32 (nc , (float * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])),src0_p ,src1_p ); 2939 2940#ifndef NDEBUG 2941for (int k = 0 ;k < nc ;k ++ ) { 2942const float x = ((float * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])))[k ]; 2943GGML_UNUSED (x ); 2944assert (!isnan (x )); 2945assert (!isinf (x )); 2946 } 2947#endif // NDEBUG 2948 } 2949} 2950 2951static void ggml_compute_forward_reglu_f16 ( 2952const ggml_compute_params * params , 2953ggml_tensor * dst ) { 2954 2955const ggml_tensor * src0 = dst -> src [0 ]; 2956const ggml_tensor * src1 = dst -> src [1 ]; 2957char * src0_d = (char * )src0 -> data ; 2958char * src1_d = (char * ) (src1 ?src1 -> data :src0 -> data ); 2959const size_t src0_o = src0 -> nb [1 ]; 2960const size_t src1_o = src1 ?src1 -> nb [1 ] :src0 -> nb [1 ]; 2961 2962GGML_ASSERT (ggml_is_contiguous_1 (src0 )); 2963GGML_ASSERT (ggml_is_contiguous_1 (dst )); 2964 2965if (src1 ) { 2966GGML_ASSERT (ggml_is_contiguous_1 (src1 )); 2967GGML_ASSERT (src0 -> type == src1 -> type ); 2968 } 2969 2970const int ith = params -> ith ; 2971const int nth = params -> nth ; 2972 2973const int nc = src1 ?src0 -> ne [0 ] :src0 -> ne [0 ] /2 ; 2974const int nr = ggml_nrows (src0 ); 2975 2976GGML_ASSERT (dst -> ne [0 ]== nc ); 2977GGML_ASSERT (ggml_nrows (dst )== nr ); 2978 2979const int32_t swapped = ggml_get_op_params_i32 (dst ,1 ); 2980 2981// rows per thread 2982const int dr = (nr + nth - 1 )/nth ; 2983 2984// row range for this thread 2985const int ir0 = dr * ith ; 2986const int ir1 = MIN (ir0 + dr ,nr ); 2987 2988for (int i1 = ir0 ;i1 < ir1 ;i1 ++ ) { 2989ggml_fp16_t * src0_p = (ggml_fp16_t * ) (src0_d + i1 * src0_o ); 2990ggml_fp16_t * src1_p = (ggml_fp16_t * ) (src1_d + i1 * src1_o ); 2991 2992if (!src1 ) { 2993src0_p += swapped ?nc :0 ; 2994src1_p += swapped ?0 :nc ; 2995 } 2996 2997ggml_vec_reglu_f16 (nc , (ggml_fp16_t * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])),src0_p ,src1_p ); 2998 2999#ifndef NDEBUG 3000for (int k = 0 ;k < nc ;k ++ ) { 3001const ggml_fp16_t x = ((ggml_fp16_t * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])))[k ]; 3002const float v = GGML_FP16_TO_FP32 (x ); 3003GGML_UNUSED (v ); 3004assert (!isnan (v )); 3005assert (!isinf (v )); 3006 } 3007#endif // NDEBUG 3008 } 3009} 3010 3011static void ggml_compute_forward_reglu ( 3012const ggml_compute_params * params , 3013ggml_tensor * dst ) { 3014 3015const ggml_tensor * src0 = dst -> src [0 ]; 3016 3017switch (src0 -> type ) { 3018case GGML_TYPE_F32 : 3019 { 3020ggml_compute_forward_reglu_f32 (params ,dst ); 3021 }break ; 3022case GGML_TYPE_F16 : 3023 { 3024ggml_compute_forward_reglu_f16 (params ,dst ); 3025 }break ; 3026default : 3027 { 3028GGML_ABORT ("fatal error" ); 3029 } 3030 } 3031} 3032 3033// ggml_compute_forward_geglu 3034 3035static void ggml_compute_forward_geglu_f32 ( 3036const ggml_compute_params * params , 3037ggml_tensor * dst ) { 3038 3039const ggml_tensor * src0 = dst -> src [0 ]; 3040const ggml_tensor * src1 = dst -> src [1 ]; 3041char * src0_d = (char * )src0 -> data ; 3042char * src1_d = (char * ) (src1 ?src1 -> data :src0 -> data ); 3043const size_t src0_o = src0 -> nb [1 ]; 3044const size_t src1_o = src1 ?src1 -> nb [1 ] :src0 -> nb [1 ]; 3045 3046GGML_ASSERT (ggml_is_contiguous_1 (src0 )); 3047GGML_ASSERT (ggml_is_contiguous_1 (dst )); 3048 3049if (src1 ) { 3050GGML_ASSERT (ggml_is_contiguous_1 (src1 )); 3051GGML_ASSERT (src0 -> type == src1 -> type ); 3052 } 3053 3054const int ith = params -> ith ; 3055const int nth = params -> nth ; 3056 3057const int nc = src1 ?src0 -> ne [0 ] :src0 -> ne [0 ] /2 ; 3058const int nr = ggml_nrows (src0 ); 3059 3060GGML_ASSERT (dst -> ne [0 ]== nc ); 3061GGML_ASSERT (ggml_nrows (dst )== nr ); 3062 3063const int32_t swapped = ggml_get_op_params_i32 (dst ,1 ); 3064 3065// rows per thread 3066const int dr = (nr + nth - 1 )/nth ; 3067 3068// row range for this thread 3069const int ir0 = dr * ith ; 3070const int ir1 = MIN (ir0 + dr ,nr ); 3071 3072for (int i1 = ir0 ;i1 < ir1 ;i1 ++ ) { 3073float * src0_p = (float * ) (src0_d + i1 * src0_o ); 3074float * src1_p = (float * ) (src1_d + i1 * src1_o ); 3075 3076if (!src1 ) { 3077src0_p += swapped ?nc :0 ; 3078src1_p += swapped ?0 :nc ; 3079 } 3080 3081ggml_vec_geglu_f32 (nc , (float * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])),src0_p ,src1_p ); 3082 3083#ifndef NDEBUG 3084for (int k = 0 ;k < nc ;k ++ ) { 3085const float x = ((float * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])))[k ]; 3086GGML_UNUSED (x ); 3087assert (!isnan (x )); 3088assert (!isinf (x )); 3089 } 3090#endif // NDEBUG 3091 } 3092} 3093 3094static void ggml_compute_forward_geglu_f16 ( 3095const ggml_compute_params * params , 3096ggml_tensor * dst ) { 3097 3098const ggml_tensor * src0 = dst -> src [0 ]; 3099const ggml_tensor * src1 = dst -> src [1 ]; 3100char * src0_d = (char * )src0 -> data ; 3101char * src1_d = (char * ) (src1 ?src1 -> data :src0 -> data ); 3102const size_t src0_o = src0 -> nb [1 ]; 3103const size_t src1_o = src1 ?src1 -> nb [1 ] :src0 -> nb [1 ]; 3104 3105GGML_ASSERT (ggml_is_contiguous_1 (src0 )); 3106GGML_ASSERT (ggml_is_contiguous_1 (dst )); 3107 3108if (src1 ) { 3109GGML_ASSERT (ggml_is_contiguous_1 (src1 )); 3110GGML_ASSERT (src0 -> type == src1 -> type ); 3111 } 3112 3113const int ith = params -> ith ; 3114const int nth = params -> nth ; 3115 3116const int nc = src1 ?src0 -> ne [0 ] :src0 -> ne [0 ] /2 ; 3117const int nr = ggml_nrows (src0 ); 3118 3119GGML_ASSERT (dst -> ne [0 ]== nc ); 3120GGML_ASSERT (ggml_nrows (dst )== nr ); 3121 3122const int32_t swapped = ggml_get_op_params_i32 (dst ,1 ); 3123 3124// rows per thread 3125const int dr = (nr + nth - 1 )/nth ; 3126 3127// row range for this thread 3128const int ir0 = dr * ith ; 3129const int ir1 = MIN (ir0 + dr ,nr ); 3130 3131for (int i1 = ir0 ;i1 < ir1 ;i1 ++ ) { 3132ggml_fp16_t * src0_p = (ggml_fp16_t * ) (src0_d + i1 * src0_o ); 3133ggml_fp16_t * src1_p = (ggml_fp16_t * ) (src1_d + i1 * src1_o ); 3134 3135if (!src1 ) { 3136src0_p += swapped ?nc :0 ; 3137src1_p += swapped ?0 :nc ; 3138 } 3139 3140ggml_vec_geglu_f16 (nc , (ggml_fp16_t * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])),src0_p ,src1_p ); 3141 3142#ifndef NDEBUG 3143for (int k = 0 ;k < nc ;k ++ ) { 3144const ggml_fp16_t x = ((ggml_fp16_t * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])))[k ]; 3145const float v = GGML_FP16_TO_FP32 (x ); 3146GGML_UNUSED (v ); 3147assert (!isnan (v )); 3148assert (!isinf (v )); 3149 } 3150#endif // NDEBUG 3151 } 3152} 3153 3154static void ggml_compute_forward_geglu ( 3155const ggml_compute_params * params , 3156ggml_tensor * dst ) { 3157 3158const ggml_tensor * src0 = dst -> src [0 ]; 3159 3160switch (src0 -> type ) { 3161case GGML_TYPE_F32 : 3162 { 3163ggml_compute_forward_geglu_f32 (params ,dst ); 3164 }break ; 3165case GGML_TYPE_F16 : 3166 { 3167ggml_compute_forward_geglu_f16 (params ,dst ); 3168 }break ; 3169default : 3170 { 3171GGML_ABORT ("fatal error" ); 3172 } 3173 } 3174} 3175 3176// ggml_compute_forward_swiglu 3177 3178static void ggml_compute_forward_swiglu_f32 ( 3179const ggml_compute_params * params , 3180ggml_tensor * dst ) { 3181 3182const ggml_tensor * src0 = dst -> src [0 ]; 3183const ggml_tensor * src1 = dst -> src [1 ]; 3184char * src0_d = (char * )src0 -> data ; 3185char * src1_d = (char * ) (src1 ?src1 -> data :src0 -> data ); 3186const size_t src0_o = src0 -> nb [1 ]; 3187const size_t src1_o = src1 ?src1 -> nb [1 ] :src0 -> nb [1 ]; 3188 3189GGML_ASSERT (ggml_is_contiguous_1 (src0 )); 3190GGML_ASSERT (ggml_is_contiguous_1 (dst )); 3191 3192if (src1 ) { 3193GGML_ASSERT (ggml_is_contiguous_1 (src1 )); 3194GGML_ASSERT (src0 -> type == src1 -> type ); 3195 } 3196 3197const int ith = params -> ith ; 3198const int nth = params -> nth ; 3199 3200const int nc = src1 ?src0 -> ne [0 ] :src0 -> ne [0 ] /2 ; 3201const int nr = ggml_nrows (src0 ); 3202 3203GGML_ASSERT (dst -> ne [0 ]== nc ); 3204GGML_ASSERT (ggml_nrows (dst )== nr ); 3205 3206const int32_t swapped = ggml_get_op_params_i32 (dst ,1 ); 3207 3208// rows per thread 3209const int dr = (nr + nth - 1 )/nth ; 3210 3211// row range for this thread 3212const int ir0 = dr * ith ; 3213const int ir1 = MIN (ir0 + dr ,nr ); 3214 3215for (int i1 = ir0 ;i1 < ir1 ;i1 ++ ) { 3216float * src0_p = (float * ) (src0_d + i1 * src0_o ); 3217float * src1_p = (float * ) (src1_d + i1 * src1_o ); 3218 3219if (!src1 ) { 3220src0_p += swapped ?nc :0 ; 3221src1_p += swapped ?0 :nc ; 3222 } 3223 3224ggml_vec_swiglu_f32 (nc , (float * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])),src0_p ,src1_p ); 3225 3226#ifndef NDEBUG 3227for (int k = 0 ;k < nc ;k ++ ) { 3228const float x = ((float * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])))[k ]; 3229GGML_UNUSED (x ); 3230assert (!isnan (x )); 3231assert (!isinf (x )); 3232 } 3233#endif // NDEBUG 3234 } 3235} 3236 3237static void ggml_compute_forward_swiglu_f16 ( 3238const ggml_compute_params * params , 3239ggml_tensor * dst ) { 3240 3241const ggml_tensor * src0 = dst -> src [0 ]; 3242const ggml_tensor * src1 = dst -> src [1 ]; 3243char * src0_d = (char * )src0 -> data ; 3244char * src1_d = (char * ) (src1 ?src1 -> data :src0 -> data ); 3245const size_t src0_o = src0 -> nb [1 ]; 3246const size_t src1_o = src1 ?src1 -> nb [1 ] :src0 -> nb [1 ]; 3247 3248GGML_ASSERT (ggml_is_contiguous_1 (src0 )); 3249GGML_ASSERT (ggml_is_contiguous_1 (dst )); 3250 3251if (src1 ) { 3252GGML_ASSERT (ggml_is_contiguous_1 (src1 )); 3253GGML_ASSERT (src0 -> type == src1 -> type ); 3254 } 3255 3256const int ith = params -> ith ; 3257const int nth = params -> nth ; 3258 3259const int nc = src1 ?src0 -> ne [0 ] :src0 -> ne [0 ] /2 ; 3260const int nr = ggml_nrows (src0 ); 3261 3262GGML_ASSERT (dst -> ne [0 ]== nc ); 3263GGML_ASSERT (ggml_nrows (dst )== nr ); 3264 3265const int32_t swapped = ggml_get_op_params_i32 (dst ,1 ); 3266 3267// rows per thread 3268const int dr = (nr + nth - 1 )/nth ; 3269 3270// row range for this thread 3271const int ir0 = dr * ith ; 3272const int ir1 = MIN (ir0 + dr ,nr ); 3273 3274for (int i1 = ir0 ;i1 < ir1 ;i1 ++ ) { 3275ggml_fp16_t * src0_p = (ggml_fp16_t * ) (src0_d + i1 * src0_o ); 3276ggml_fp16_t * src1_p = (ggml_fp16_t * ) (src1_d + i1 * src1_o ); 3277 3278if (!src1 ) { 3279src0_p += swapped ?nc :0 ; 3280src1_p += swapped ?0 :nc ; 3281 } 3282 3283ggml_vec_swiglu_f16 (nc , (ggml_fp16_t * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])),src0_p ,src1_p ); 3284 3285#ifndef NDEBUG 3286for (int k = 0 ;k < nc ;k ++ ) { 3287const ggml_fp16_t x = ((ggml_fp16_t * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])))[k ]; 3288const float v = GGML_FP16_TO_FP32 (x ); 3289GGML_UNUSED (v ); 3290assert (!isnan (v )); 3291assert (!isinf (v )); 3292 } 3293#endif // NDEBUG 3294 } 3295} 3296 3297static void ggml_compute_forward_swiglu ( 3298const ggml_compute_params * params , 3299ggml_tensor * dst ) { 3300 3301const ggml_tensor * src0 = dst -> src [0 ]; 3302 3303switch (src0 -> type ) { 3304case GGML_TYPE_F32 : 3305 { 3306ggml_compute_forward_swiglu_f32 (params ,dst ); 3307 }break ; 3308case GGML_TYPE_F16 : 3309 { 3310ggml_compute_forward_swiglu_f16 (params ,dst ); 3311 }break ; 3312default : 3313 { 3314GGML_ABORT ("fatal error" ); 3315 } 3316 } 3317} 3318 3319// ggml_compute_forward_swiglu_oai 3320 3321static void ggml_compute_forward_swiglu_oai_f32 ( 3322const ggml_compute_params * params , 3323ggml_tensor * dst ) { 3324 3325const ggml_tensor * src0 = dst -> src [0 ]; 3326const ggml_tensor * src1 = dst -> src [1 ]; 3327char * src0_d = (char * )src0 -> data ; 3328char * src1_d = (char * ) (src1 ?src1 -> data :src0 -> data ); 3329const size_t src0_o = src0 -> nb [1 ]; 3330const size_t src1_o = src1 ?src1 -> nb [1 ] :src0 -> nb [1 ]; 3331 3332GGML_ASSERT (ggml_is_contiguous_1 (src0 )); 3333GGML_ASSERT (ggml_is_contiguous_1 (dst )); 3334 3335if (src1 ) { 3336GGML_ASSERT (ggml_is_contiguous_1 (src1 )); 3337GGML_ASSERT (src0 -> type == src1 -> type ); 3338 } 3339 3340const int ith = params -> ith ; 3341const int nth = params -> nth ; 3342 3343const int nc = src1 ?src0 -> ne [0 ] :src0 -> ne [0 ] /2 ; 3344const int nr = ggml_nrows (src0 ); 3345 3346GGML_ASSERT (dst -> ne [0 ]== nc ); 3347GGML_ASSERT (ggml_nrows (dst )== nr ); 3348 3349const int32_t swapped = ggml_get_op_params_i32 (dst ,1 ); 3350const float alpha = ggml_get_op_params_f32 (dst ,2 ); 3351const float limit = ggml_get_op_params_f32 (dst ,3 ); 3352 3353// rows per thread 3354const int dr = (nr + nth - 1 )/nth ; 3355 3356// row range for this thread 3357const int ir0 = dr * ith ; 3358const int ir1 = MIN (ir0 + dr ,nr ); 3359 3360for (int i1 = ir0 ;i1 < ir1 ;i1 ++ ) { 3361float * src0_p = (float * ) (src0_d + i1 * src0_o ); 3362float * src1_p = (float * ) (src1_d + i1 * src1_o ); 3363float * dst_p = (float * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])); 3364 3365if (!src1 ) { 3366src0_p += swapped ?nc :0 ; 3367src1_p += swapped ?0 :nc ; 3368 } 3369 3370for (int k = 0 ;k < nc ;k ++ ) { 3371const float x = std::min (src0_p [k ],limit ); 3372const float y = std::clamp (src1_p [k ],- limit ,limit ); 3373const float out_glu = x / (1.f + expf (alpha * (- x ))); 3374dst_p [k ]= out_glu * (y + 1.f ); 3375 } 3376 3377#ifndef NDEBUG 3378for (int k = 0 ;k < nc ;k ++ ) { 3379const float x = dst_p [k ]; 3380GGML_UNUSED (x ); 3381assert (!isnan (x )); 3382assert (!isinf (x )); 3383 } 3384#endif // NDEBUG 3385 } 3386} 3387 3388static void ggml_compute_forward_swiglu_oai ( 3389const ggml_compute_params * params , 3390ggml_tensor * dst ) { 3391 3392const ggml_tensor * src0 = dst -> src [0 ]; 3393 3394switch (src0 -> type ) { 3395case GGML_TYPE_F32 : 3396 { 3397ggml_compute_forward_swiglu_oai_f32 (params ,dst ); 3398 }break ; 3399default : 3400 { 3401GGML_ABORT ("fatal error" ); 3402 } 3403 } 3404} 3405 3406// ggml_compute_forward_swiglu_clamp 3407 3408static void ggml_compute_forward_swiglu_clamp_f32 (const ggml_compute_params * params ,ggml_tensor * dst ) { 3409const ggml_tensor * src0 = dst -> src [0 ]; 3410const ggml_tensor * src1 = dst -> src [1 ]; 3411char * src0_d = (char * )src0 -> data ; 3412char * src1_d = (char * ) (src1 ?src1 -> data :src0 -> data ); 3413const size_t src0_o = src0 -> nb [1 ]; 3414const size_t src1_o = src1 ?src1 -> nb [1 ] :src0 -> nb [1 ]; 3415 3416GGML_ASSERT (ggml_is_contiguous_1 (src0 )); 3417GGML_ASSERT (ggml_is_contiguous_1 (dst )); 3418 3419if (src1 ) { 3420GGML_ASSERT (ggml_is_contiguous_1 (src1 )); 3421GGML_ASSERT (src0 -> type == src1 -> type ); 3422 } 3423 3424const int ith = params -> ith ; 3425const int nth = params -> nth ; 3426 3427const int nc = src1 ?src0 -> ne [0 ] :src0 -> ne [0 ] /2 ; 3428const int nr = ggml_nrows (src0 ); 3429 3430GGML_ASSERT (dst -> ne [0 ]== nc ); 3431GGML_ASSERT (ggml_nrows (dst )== nr ); 3432 3433const int32_t swapped = ggml_get_op_params_i32 (dst ,1 ); 3434const float limit = ggml_get_op_params_f32 (dst ,3 ); 3435 3436const int dr = (nr + nth - 1 ) /nth ; 3437const int ir0 = dr * ith ; 3438const int ir1 = MIN (ir0 + dr ,nr ); 3439 3440for (int i1 = ir0 ;i1 < ir1 ;i1 ++ ) { 3441float * src0_p = (float * ) (src0_d + i1 * src0_o ); 3442float * src1_p = (float * ) (src1_d + i1 * src1_o ); 3443float * dst_p = (float * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])); 3444 3445if (!src1 ) { 3446src0_p += swapped ?nc :0 ; 3447src1_p += swapped ?0 :nc ; 3448 } 3449 3450for (int k = 0 ;k < nc ;k ++ ) { 3451const float gate = std::min (src0_p [k ],limit ); 3452const float up = std::clamp (src1_p [k ],- limit ,limit ); 3453dst_p [k ]= gate / (1.f + expf (- gate ))* up ; 3454 } 3455 3456#ifndef NDEBUG 3457for (int k = 0 ;k < nc ;k ++ ) { 3458const float x = dst_p [k ]; 3459GGML_UNUSED (x ); 3460assert (!isnan (x )); 3461assert (!isinf (x )); 3462 } 3463#endif // NDEBUG 3464 } 3465} 3466 3467static void ggml_compute_forward_swiglu_clamp_f16 (const ggml_compute_params * params ,ggml_tensor * dst ) { 3468const ggml_tensor * src0 = dst -> src [0 ]; 3469const ggml_tensor * src1 = dst -> src [1 ]; 3470char * src0_d = (char * )src0 -> data ; 3471char * src1_d = (char * ) (src1 ?src1 -> data :src0 -> data ); 3472const size_t src0_o = src0 -> nb [1 ]; 3473const size_t src1_o = src1 ?src1 -> nb [1 ] :src0 -> nb [1 ]; 3474 3475GGML_ASSERT (ggml_is_contiguous_1 (src0 )); 3476GGML_ASSERT (ggml_is_contiguous_1 (dst )); 3477 3478if (src1 ) { 3479GGML_ASSERT (ggml_is_contiguous_1 (src1 )); 3480GGML_ASSERT (src0 -> type == src1 -> type ); 3481 } 3482 3483const int ith = params -> ith ; 3484const int nth = params -> nth ; 3485 3486const int nc = src1 ?src0 -> ne [0 ] :src0 -> ne [0 ] /2 ; 3487const int nr = ggml_nrows (src0 ); 3488 3489GGML_ASSERT (dst -> ne [0 ]== nc ); 3490GGML_ASSERT (ggml_nrows (dst )== nr ); 3491 3492const int32_t swapped = ggml_get_op_params_i32 (dst ,1 ); 3493const float limit = ggml_get_op_params_f32 (dst ,3 ); 3494 3495const int dr = (nr + nth - 1 ) /nth ; 3496const int ir0 = dr * ith ; 3497const int ir1 = MIN (ir0 + dr ,nr ); 3498 3499for (int i1 = ir0 ;i1 < ir1 ;i1 ++ ) { 3500ggml_fp16_t * src0_p = (ggml_fp16_t * ) (src0_d + i1 * src0_o ); 3501ggml_fp16_t * src1_p = (ggml_fp16_t * ) (src1_d + i1 * src1_o ); 3502ggml_fp16_t * dst_p = (ggml_fp16_t * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])); 3503 3504if (!src1 ) { 3505src0_p += swapped ?nc :0 ; 3506src1_p += swapped ?0 :nc ; 3507 } 3508 3509for (int k = 0 ;k < nc ;k ++ ) { 3510const float gate = std::min (GGML_FP16_TO_FP32 (src0_p [k ]),limit ); 3511const float up = std::clamp (GGML_FP16_TO_FP32 (src1_p [k ]),- limit ,limit ); 3512dst_p [k ]= GGML_FP32_TO_FP16 (gate / (1.f + expf (- gate ))* up ); 3513 } 3514 3515#ifndef NDEBUG 3516for (int k = 0 ;k < nc ;k ++ ) { 3517const float x = GGML_FP16_TO_FP32 (dst_p [k ]); 3518GGML_UNUSED (x ); 3519assert (!isnan (x )); 3520assert (!isinf (x )); 3521 } 3522#endif // NDEBUG 3523 } 3524} 3525 3526static void ggml_compute_forward_swiglu_clamp (const ggml_compute_params * params ,ggml_tensor * dst ) { 3527switch (dst -> src [0 ]-> type ) { 3528case GGML_TYPE_F32 : 3529ggml_compute_forward_swiglu_clamp_f32 (params ,dst ); 3530break ; 3531case GGML_TYPE_F16 : 3532ggml_compute_forward_swiglu_clamp_f16 (params ,dst ); 3533break ; 3534default : 3535GGML_ABORT ("fatal error" ); 3536 } 3537} 3538 3539// ggml_compute_forward_geglu_erf 3540 3541static void ggml_compute_forward_geglu_erf_f32 ( 3542const ggml_compute_params * params , 3543ggml_tensor * dst ) { 3544 3545const ggml_tensor * src0 = dst -> src [0 ]; 3546const ggml_tensor * src1 = dst -> src [1 ]; 3547char * src0_d = (char * )src0 -> data ; 3548char * src1_d = (char * ) (src1 ?src1 -> data :src0 -> data ); 3549const size_t src0_o = src0 -> nb [1 ]; 3550const size_t src1_o = src1 ?src1 -> nb [1 ] :src0 -> nb [1 ]; 3551 3552GGML_ASSERT (ggml_is_contiguous_1 (src0 )); 3553GGML_ASSERT (ggml_is_contiguous_1 (dst )); 3554 3555if (src1 ) { 3556GGML_ASSERT (ggml_is_contiguous_1 (src1 )); 3557GGML_ASSERT (src0 -> type == src1 -> type ); 3558 } 3559 3560const int ith = params -> ith ; 3561const int nth = params -> nth ; 3562 3563const int nc = src1 ?src0 -> ne [0 ] :src0 -> ne [0 ] /2 ; 3564const int nr = ggml_nrows (src0 ); 3565 3566GGML_ASSERT (dst -> ne [0 ]== nc ); 3567GGML_ASSERT (ggml_nrows (dst )== nr ); 3568 3569const int32_t swapped = ggml_get_op_params_i32 (dst ,1 ); 3570 3571// rows per thread 3572const int dr = (nr + nth - 1 )/nth ; 3573 3574// row range for this thread 3575const int ir0 = dr * ith ; 3576const int ir1 = MIN (ir0 + dr ,nr ); 3577 3578for (int i1 = ir0 ;i1 < ir1 ;i1 ++ ) { 3579float * src0_p = (float * ) (src0_d + i1 * src0_o ); 3580float * src1_p = (float * ) (src1_d + i1 * src1_o ); 3581 3582if (!src1 ) { 3583src0_p += swapped ?nc :0 ; 3584src1_p += swapped ?0 :nc ; 3585 } 3586 3587ggml_vec_geglu_erf_f32 (nc , (float * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])),src0_p ,src1_p ); 3588 3589#ifndef NDEBUG 3590for (int k = 0 ;k < nc ;k ++ ) { 3591const float x = ((float * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])))[k ]; 3592GGML_UNUSED (x ); 3593assert (!isnan (x )); 3594assert (!isinf (x )); 3595 } 3596#endif // NDEBUG 3597 } 3598} 3599 3600static void ggml_compute_forward_geglu_erf_f16 ( 3601const ggml_compute_params * params , 3602ggml_tensor * dst ) { 3603 3604const ggml_tensor * src0 = dst -> src [0 ]; 3605const ggml_tensor * src1 = dst -> src [1 ]; 3606char * src0_d = (char * )src0 -> data ; 3607char * src1_d = (char * ) (src1 ?src1 -> data :src0 -> data ); 3608const size_t src0_o = src0 -> nb [1 ]; 3609const size_t src1_o = src1 ?src1 -> nb [1 ] :src0 -> nb [1 ]; 3610 3611GGML_ASSERT (ggml_is_contiguous_1 (src0 )); 3612GGML_ASSERT (ggml_is_contiguous_1 (dst )); 3613 3614if (src1 ) { 3615GGML_ASSERT (ggml_is_contiguous_1 (src1 )); 3616GGML_ASSERT (src0 -> type == src1 -> type ); 3617 } 3618 3619const int ith = params -> ith ; 3620const int nth = params -> nth ; 3621 3622const int nc = src1 ?src0 -> ne [0 ] :src0 -> ne [0 ] /2 ; 3623const int nr = ggml_nrows (src0 ); 3624 3625GGML_ASSERT (dst -> ne [0 ]== nc ); 3626GGML_ASSERT (ggml_nrows (dst )== nr ); 3627 3628const int32_t swapped = ggml_get_op_params_i32 (dst ,1 ); 3629 3630// rows per thread 3631const int dr = (nr + nth - 1 )/nth ; 3632 3633// row range for this thread 3634const int ir0 = dr * ith ; 3635const int ir1 = MIN (ir0 + dr ,nr ); 3636 3637for (int i1 = ir0 ;i1 < ir1 ;i1 ++ ) { 3638ggml_fp16_t * src0_p = (ggml_fp16_t * ) (src0_d + i1 * src0_o ); 3639ggml_fp16_t * src1_p = (ggml_fp16_t * ) (src1_d + i1 * src1_o ); 3640 3641if (!src1 ) { 3642src0_p += swapped ?nc :0 ; 3643src1_p += swapped ?0 :nc ; 3644 } 3645 3646ggml_vec_geglu_erf_f16 (nc , (ggml_fp16_t * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])),src0_p ,src1_p ); 3647 3648#ifndef NDEBUG 3649for (int k = 0 ;k < nc ;k ++ ) { 3650const ggml_fp16_t x = ((ggml_fp16_t * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])))[k ]; 3651const float v = GGML_FP16_TO_FP32 (x ); 3652GGML_UNUSED (v ); 3653assert (!isnan (v )); 3654assert (!isinf (v )); 3655 } 3656#endif // NDEBUG 3657 } 3658} 3659 3660static void ggml_compute_forward_geglu_erf ( 3661const ggml_compute_params * params , 3662ggml_tensor * dst ) { 3663 3664const ggml_tensor * src0 = dst -> src [0 ]; 3665 3666switch (src0 -> type ) { 3667case GGML_TYPE_F32 : 3668 { 3669ggml_compute_forward_geglu_erf_f32 (params ,dst ); 3670 }break ; 3671case GGML_TYPE_F16 : 3672 { 3673ggml_compute_forward_geglu_erf_f16 (params ,dst ); 3674 }break ; 3675default : 3676 { 3677GGML_ABORT ("fatal error" ); 3678 } 3679 } 3680} 3681 3682// ggml_compute_forward_geglu_quick 3683 3684static void ggml_compute_forward_geglu_quick_f32 ( 3685const ggml_compute_params * params , 3686ggml_tensor * dst ) { 3687 3688const ggml_tensor * src0 = dst -> src [0 ]; 3689const ggml_tensor * src1 = dst -> src [1 ]; 3690char * src0_d = (char * )src0 -> data ; 3691char * src1_d = (char * ) (src1 ?src1 -> data :src0 -> data ); 3692const size_t src0_o = src0 -> nb [1 ]; 3693const size_t src1_o = src1 ?src1 -> nb [1 ] :src0 -> nb [1 ]; 3694 3695GGML_ASSERT (ggml_is_contiguous_1 (src0 )); 3696GGML_ASSERT (ggml_is_contiguous_1 (dst )); 3697 3698if (src1 ) { 3699GGML_ASSERT (ggml_is_contiguous_1 (src1 )); 3700GGML_ASSERT (src0 -> type == src1 -> type ); 3701 } 3702 3703const int ith = params -> ith ; 3704const int nth = params -> nth ; 3705 3706const int nc = src1 ?src0 -> ne [0 ] :src0 -> ne [0 ] /2 ; 3707const int nr = ggml_nrows (src0 ); 3708 3709GGML_ASSERT (dst -> ne [0 ]== nc ); 3710GGML_ASSERT (ggml_nrows (dst )== nr ); 3711 3712const int32_t swapped = ggml_get_op_params_i32 (dst ,1 ); 3713 3714// rows per thread 3715const int dr = (nr + nth - 1 )/nth ; 3716 3717// row range for this thread 3718const int ir0 = dr * ith ; 3719const int ir1 = MIN (ir0 + dr ,nr ); 3720 3721for (int i1 = ir0 ;i1 < ir1 ;i1 ++ ) { 3722float * src0_p = (float * ) (src0_d + i1 * src0_o ); 3723float * src1_p = (float * ) (src1_d + i1 * src1_o ); 3724 3725if (!src1 ) { 3726src0_p += swapped ?nc :0 ; 3727src1_p += swapped ?0 :nc ; 3728 } 3729 3730ggml_vec_geglu_quick_f32 (nc , (float * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])),src0_p ,src1_p ); 3731 3732#ifndef NDEBUG 3733for (int k = 0 ;k < nc ;k ++ ) { 3734const float x = ((float * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])))[k ]; 3735GGML_UNUSED (x ); 3736assert (!isnan (x )); 3737assert (!isinf (x )); 3738 } 3739#endif // NDEBUG 3740 } 3741} 3742 3743static void ggml_compute_forward_geglu_quick_f16 ( 3744const ggml_compute_params * params , 3745ggml_tensor * dst ) { 3746 3747const ggml_tensor * src0 = dst -> src [0 ]; 3748const ggml_tensor * src1 = dst -> src [1 ]; 3749char * src0_d = (char * )src0 -> data ; 3750char * src1_d = (char * ) (src1 ?src1 -> data :src0 -> data ); 3751const size_t src0_o = src0 -> nb [1 ]; 3752const size_t src1_o = src1 ?src1 -> nb [1 ] :src0 -> nb [1 ]; 3753 3754GGML_ASSERT (ggml_is_contiguous_1 (src0 )); 3755GGML_ASSERT (ggml_is_contiguous_1 (dst )); 3756 3757if (src1 ) { 3758GGML_ASSERT (ggml_is_contiguous_1 (src1 )); 3759GGML_ASSERT (src0 -> type == src1 -> type ); 3760 } 3761 3762const int ith = params -> ith ; 3763const int nth = params -> nth ; 3764 3765const int nc = src1 ?src0 -> ne [0 ] :src0 -> ne [0 ] /2 ; 3766const int nr = ggml_nrows (src0 ); 3767 3768GGML_ASSERT (dst -> ne [0 ]== nc ); 3769GGML_ASSERT (ggml_nrows (dst )== nr ); 3770 3771const int32_t swapped = ggml_get_op_params_i32 (dst ,1 ); 3772 3773// rows per thread 3774const int dr = (nr + nth - 1 )/nth ; 3775 3776// row range for this thread 3777const int ir0 = dr * ith ; 3778const int ir1 = MIN (ir0 + dr ,nr ); 3779 3780for (int i1 = ir0 ;i1 < ir1 ;i1 ++ ) { 3781ggml_fp16_t * src0_p = (ggml_fp16_t * ) (src0_d + i1 * src0_o ); 3782ggml_fp16_t * src1_p = (ggml_fp16_t * ) (src1_d + i1 * src1_o ); 3783 3784if (!src1 ) { 3785src0_p += swapped ?nc :0 ; 3786src1_p += swapped ?0 :nc ; 3787 } 3788 3789ggml_vec_geglu_quick_f16 (nc , (ggml_fp16_t * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])),src0_p ,src1_p ); 3790 3791#ifndef NDEBUG 3792for (int k = 0 ;k < nc ;k ++ ) { 3793const ggml_fp16_t x = ((ggml_fp16_t * ) ((char * )dst -> data + i1 * (dst -> nb [1 ])))[k ]; 3794const float v = GGML_FP16_TO_FP32 (x ); 3795GGML_UNUSED (v ); 3796assert (!isnan (v )); 3797assert (!isinf (v )); 3798 } 3799#endif // NDEBUG 3800 } 3801} 3802 3803static void ggml_compute_forward_geglu_quick ( 3804const ggml_compute_params * params , 3805ggml_tensor * dst ) { 3806 3807const ggml_tensor * src0 = dst -> src [0 ]; 3808 3809switch (src0 -> type ) { 3810case GGML_TYPE_F32 : 3811 { 3812ggml_compute_forward_geglu_quick_f32 (params ,dst ); 3813 }break ; 3814case GGML_TYPE_F16 : 3815 { 3816ggml_compute_forward_geglu_quick_f16 (params ,dst ); 3817 }break ; 3818default : 3819 { 3820GGML_ABORT ("fatal error" ); 3821 } 3822 } 3823} 3824 3825// ggml_compute_forward_norm 3826 3827static void ggml_compute_forward_norm_f32 ( 3828const ggml_compute_params * params , 3829ggml_tensor * dst ) { 3830 3831const ggml_tensor * src0 = dst -> src [0 ]; 3832 3833GGML_ASSERT (ggml_are_same_shape (src0 ,dst )); 3834 3835const int ith = params -> ith ; 3836const int nth = params -> nth ; 3837 3838GGML_TENSOR_UNARY_OP_LOCALS 3839 3840float eps ; 3841memcpy (& eps ,dst -> op_params ,sizeof (float )); 3842 3843GGML_ASSERT (eps >=0.0f ); 3844 3845for (int64_t i03 = 0 ;i03 < ne03 ;i03 ++ ) { 3846for (int64_t i02 = 0 ;i02 < ne02 ;i02 ++ ) { 3847for (int64_t i01 = ith ;i01 < ne01 ;i01 += nth ) { 3848const char * x = (const char * )src0 -> data + i01 * nb01 + i02 * nb02 + i03 * nb03 ; 3849char * y = (char * )dst -> data + i01 * nb1 + i02 * nb2 + i03 * nb3 ; 3850 3851if (nb00 == sizeof (float )&& nb0 == sizeof (float )) { 3852const float * xf = (const float * )x ; 3853 3854float sum = 0.0 ; 3855ggml_vec_sum_f32 (ne00 ,& sum ,xf ); 3856float mean = sum /ne00 ; 3857 3858float * yf = (float * )y ; 3859float variance = 0 ; 3860 3861#ifdef GGML_USE_ACCELERATE 3862mean = - mean ; 3863vDSP_vsadd (xf ,1 ,& mean ,yf ,1 ,ne00 ); 3864vDSP_measqv (yf ,1 ,& variance ,ne00 ); 3865#else 3866variance = ggml_vec_cvar_f32 (ne00 ,yf ,xf ,mean ); 3867#endif //GGML_USE_ACCELERATE 3868 3869const float scale = 1.0f /sqrtf (variance + eps ); 3870ggml_vec_scale_f32 (ne00 ,yf ,scale ); 3871 }else { 3872float sum = 0.0 ; 3873for (int64_t i00 = 0 ;i00 < ne00 ;i00 ++ ) { 3874sum += * (const float * ) (x + i00 * nb00 ); 3875 } 3876const float mean = sum /ne00 ; 3877 3878float variance = 0.0f ; 3879for (int64_t i00 = 0 ;i00 < ne00 ;i00 ++ ) { 3880const float v = * (const float * ) (x + i00 * nb00 )- mean ; 3881* (float * ) (y + i00 * nb0 )= v ; 3882variance += v * v ; 3883 } 3884variance /=ne00 ; 3885 3886const float scale = 1.0f /sqrtf (variance + eps ); 3887for (int64_t i00 = 0 ;i00 < ne00 ;i00 ++ ) { 3888* (float * ) (y + i00 * nb0 ) *=scale ; 3889 } 3890 } 3891 } 3892 } 3893 } 3894} 3895 3896void ggml_compute_forward_norm ( 3897const ggml_compute_params * params , 3898ggml_tensor * dst ) { 3899 3900const ggml_tensor * src0 = dst -> src [0 ]; 3901 3902switch (src0 -> type ) { 3903case GGML_TYPE_F32 : 3904 { 3905ggml_compute_forward_norm_f32 (params ,dst ); 3906 }break ; 3907default : 3908 { 3909GGML_ABORT ("fatal error" ); 3910 } 3911 } 3912} 3913 3914// ggml_compute_forward_group_rms_norm 3915 3916// fusion kinds that can be combined with the rms_norm computation in a single pass. 3917// extend this enum when adding new fused variants (e.g. FUSE_ADD, FUSE_MUL_ADD, ...). 3918enum ggml_rms_norm_fuse_op { 3919GGML_RMS_NORM_FUSE_OP_NONE , 3920GGML_RMS_NORM_FUSE_OP_MUL , 3921}; 3922 3923template < ggml_rms_norm_fuse_op FUSE_OP > 3924static void ggml_compute_forward_rms_norm_f32 ( 3925const ggml_compute_params * params , 3926ggml_tensor * dst_rms_norm , 3927ggml_tensor * dst_fused = nullptr ) { 3928 3929const ggml_tensor * src0 = dst_rms_norm -> src [0 ]; 3930const ggml_tensor * src1 = nullptr ; 3931ggml_tensor * dst = dst_rms_norm ; 3932 3933if constexpr (FUSE_OP == GGML_RMS_NORM_FUSE_OP_MUL ) { 3934src1 = (dst_fused -> src [0 ]== dst_rms_norm ) ?dst_fused -> src [1 ] :dst_fused -> src [0 ]; 3935dst = dst_fused ; 3936 } 3937 3938GGML_ASSERT (ggml_are_same_shape (src0 ,dst )); 3939 3940GGML_ASSERT (src0 -> nb [0 ]== sizeof (float )); 3941 3942const int ith = params -> ith ; 3943const int nth = params -> nth ; 3944 3945GGML_TENSOR_BINARY_OP_LOCALS 3946 3947float eps ; 3948memcpy (& eps ,dst_rms_norm -> op_params ,sizeof (float )); 3949GGML_ASSERT (eps >=0.0f ); 3950 3951// TODO: optimize 3952for (int64_t i03 = 0 ;i03 < ne03 ;i03 ++ ) { 3953for (int64_t i02 = 0 ;i02 < ne02 ;i02 ++ ) { 3954for (int64_t i01 = ith ;i01 < ne01 ;i01 += nth ) { 3955const float * x = (float * ) ((char * )src0 -> data + i01 * nb01 + i02 * nb02 + i03 * nb03 ); 3956 3957ggml_float sum = 0.0 ; 3958// worth switching to explicit SIMD? 3959for (int64_t i00 = 0 ;i00 < ne00 ;i00 ++ ) { 3960sum += (ggml_float )(x [i00 ]* x [i00 ]); 3961 } 3962 3963const float mean = sum /ne00 ; 3964const float scale = 1.0f /sqrtf (mean + eps ); 3965 3966// if you hit this, likely you got an inf somewhere earlier 3967assert (scale > 0.0f ); 3968 3969float * y = (float * ) ((char * )dst -> data + i01 * nb1 + i02 * nb2 + i03 * nb3 ); 3970 3971if constexpr (FUSE_OP == GGML_RMS_NORM_FUSE_OP_MUL ) { 3972const int64_t i11 = i01 %ne11 ; 3973const int64_t i12 = i02 %ne12 ; 3974const int64_t i13 = i03 %ne13 ; 3975const float * w = (float * ) ((char * )src1 -> data + i11 * nb11 + i12 * nb12 + i13 * nb13 ); 3976 3977for (int64_t i00 = 0 ;i00 < ne00 ;i00 ++ ) { 3978y [i00 ]= x [i00 ]* scale * w [i00 ]; 3979 } 3980 }else { 3981memcpy (y ,x ,ne00 * sizeof (float )); 3982ggml_vec_scale_f32 (ne00 ,y ,scale ); 3983 } 3984 } 3985 } 3986 } 3987} 3988 3989void ggml_compute_forward_rms_norm ( 3990const ggml_compute_params * params , 3991ggml_tensor * dst ) { 3992 3993const ggml_tensor * src0 = dst -> src [0 ]; 3994 3995switch (src0 -> type ) { 3996case GGML_TYPE_F32 : 3997 { 3998ggml_compute_forward_rms_norm_f32 < GGML_RMS_NORM_FUSE_OP_NONE > (params ,dst ); 3999 }break ; 4000default : 4001 { 4002GGML_ABORT ("fatal error" ); 4003 } 4004 } 4005} 4006 4007// Fused RMS_NORM + MUL: computes dst = rms_norm(src0) * src1 in a single pass. 4008// This avoids materializing the intermediate rms_norm result in memory. 4009void ggml_compute_forward_rms_norm_mul_fused ( 4010const ggml_compute_params * params , 4011ggml_tensor * dst_rms_norm , 4012ggml_tensor * dst_mul ) { 4013 4014GGML_ASSERT (dst_mul != nullptr ); 4015GGML_ASSERT (dst_mul -> src [0 ]== dst_rms_norm || dst_mul -> src [1 ]== dst_rms_norm ); 4016 4017const ggml_tensor * src0 = dst_rms_norm -> src [0 ]; 4018 4019switch (src0 -> type ) { 4020case GGML_TYPE_F32 : 4021 { 4022ggml_compute_forward_rms_norm_f32 < GGML_RMS_NORM_FUSE_OP_MUL > (params ,dst_rms_norm ,dst_mul ); 4023 }break ; 4024default : 4025 { 4026GGML_ABORT ("fatal error" ); 4027 } 4028 } 4029} 4030 4031static void ggml_compute_forward_rms_norm_back_f32 ( 4032const ggml_compute_params * params , 4033ggml_tensor * dst ) { 4034 4035const ggml_tensor * src0 = dst -> src [0 ];// gradients from forward pass output 4036const ggml_tensor * src1 = dst -> src [1 ];// src1 from forward pass 4037 4038GGML_ASSERT (ggml_are_same_shape (src0 ,dst )&& ggml_are_same_shape (src0 ,src1 )); 4039 4040GGML_ASSERT (src0 -> nb [0 ]== sizeof (float )); 4041GGML_ASSERT (src1 -> nb [0 ]== sizeof (float )); 4042 4043const int ith = params -> ith ; 4044const int nth = params -> nth ; 4045 4046GGML_TENSOR_BINARY_OP_LOCALS 4047 4048float eps ; 4049memcpy (& eps ,dst -> op_params ,sizeof (float )); 4050 4051// TODO: optimize 4052for (int64_t i03 = 0 ;i03 < ne03 ;i03 ++ ) { 4053for (int64_t i02 = 0 ;i02 < ne02 ;i02 ++ ) { 4054for (int64_t i01 = ith ;i01 < ne01 ;i01 += nth ) { 4055// src1 is same shape as src0 => same indices 4056const int64_t i11 = i01 ; 4057const int64_t i12 = i02 ; 4058const int64_t i13 = i03 ; 4059 4060const float * dz = (float * ) ((char * )src0 -> data + i01 * nb01 + i02 * nb02 + i03 * nb03 ); 4061const float * x = (float * ) ((char * )src1 -> data + i11 * nb11 + i12 * nb12 + i13 * nb13 ); 4062 4063ggml_float sum_xx = 0.0 ; 4064ggml_float sum_xdz = 0.0 ; 4065 4066for (int64_t i00 = 0 ;i00 < ne00 ;i00 ++ ) { 4067sum_xx += (ggml_float )(x [i00 ]* x [i00 ]); 4068sum_xdz += (ggml_float )(x [i00 ]* dz [i00 ]); 4069 } 4070 4071//const float mean = (float)(sum_xx)/ne00; 4072const float mean_eps = (float )(sum_xx )/ne00 + eps ; 4073const float sum_eps = (float )(sum_xx )+ eps * ne00 ; 4074//const float mean_xdz = (float)(sum_xdz)/ne00; 4075// we could cache rms from forward pass to improve performance. 4076// to do this implement ggml_rms and compose ggml_rms_norm using ggml_rms. 4077//const float rms = sqrtf(mean_eps); 4078const float rrms = 1.0f /sqrtf (mean_eps ); 4079//const float scale = -rrms/(ne00 * mean_eps); // -1/(n*rms**3) 4080 4081 { 4082// z = rms_norm(x) 4083// 4084// rms_norm(src1) = 4085// scale( 4086// src1, 4087// div( 4088// 1, 4089// sqrt( 4090// add( 4091// scale( 4092// sum( 4093// sqr( 4094// src1)), 4095// (1.0/N)), 4096// eps)))); 4097 4098// postorder: 4099// ## op args grad 4100// 00 param src1 grad[#00] 4101// 01 const 1 4102// 02 sqr (#00) grad[#02] 4103// 03 sum (#02) grad[#03] 4104// 04 const 1/N 4105// 05 scale (#03, #04) grad[#05] 4106// 06 const eps 4107// 07 add (#05, #06) grad[#07] 4108// 08 sqrt (#07) grad[#08] 4109// 09 div (#01,#08) grad[#09] 4110// 10 scale (#00,#09) grad[#10] 4111// 4112// backward pass, given grad[#10] 4113// #10: scale 4114// grad[#00] += scale(grad[#10],#09) 4115// grad[#09] += sum(mul(grad[#10],#00)) 4116// #09: div 4117// grad[#08] += neg(mul(grad[#09], div(#09,#08))) 4118// #08: sqrt 4119// grad[#07] += mul(grad[#08], div(0.5, #08)) 4120// #07: add 4121// grad[#05] += grad[#07] 4122// #05: scale 4123// grad[#03] += scale(grad[#05],#04) 4124// #03: sum 4125// grad[#02] += repeat(grad[#03], #02) 4126// #02: 4127// grad[#00] += scale(mul(#00, grad[#02]), 2.0) 4128// 4129// substitute and simplify: 4130// grad[#00] = scale(grad(#10), #09) + scale(mul(#00, grad[#02]), 2.0) 4131// grad[#02] = repeat(grad[#03], #02) 4132// grad[#02] = repeat(scale(grad[#05],#04), #02) 4133// grad[#02] = repeat(scale(grad[#07],#04), #02) 4134// grad[#02] = repeat(scale(mul(grad[#08], div(0.5, #08)),#04), #02) 4135// grad[#02] = repeat(scale(mul(neg(mul(grad[#09], div(#09,#08))), div(0.5, #08)),#04), #02) 4136// grad[#02] = repeat(scale(mul(neg(mul(sum(mul(grad[#10],#00)), div(#09,#08))), div(0.5, #08)),#04), #02) 4137// grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(#09,#08) * div(0.5, #08) * (1/N)), #02) 4138// grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(div(#01,#08),#08) * div(0.5, #08) * (1/N)), #02) 4139// grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(1,#08*#08) * div(0.5, #08) * (1/N)), #02) 4140// grad[#02] = repeat(-(sum(mul(grad[#10],#00)) * div(1,#07) * div(0.5, #08) * (1/N)), #02) 4141// grad[#00] = scale(grad(#10), #09) + scale(mul(#00, grad[#02]), 2.0) 4142// 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) 4143// 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) 4144// grad[#00] = scale(grad(#10), #09) + scale(#00, -(sum(mul(grad[#10],#00)) * div(1,#07) * div(1,#08) * (1/N))) 4145// grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(1,#07*#08) * (-1/N)) 4146// grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(1,#07*#08) * (-1/N)) 4147// grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(1,mean_eps*rms) * (-1/N)) 4148// grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(-1,rms*N*mean_eps)) 4149// grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(-1,rms*N*(sum_xx/N+eps))) 4150// grad[#00] = scale(grad(#10), #09) + scale(#00, sum(mul(grad[#10],#00)) * div(-1,rms*N*sum_xx+rms*N*eps)) 4151// grad[#00] = scale(dz, rrms) + scale(x, sum(mul(dz,x)) * div(-1,rms*N*mean_eps)) 4152// grad[#00] = scale(dz, rrms) + scale(x, sum_xdz * div(-1,rms*N*mean_eps)) 4153// a = b*c + d*e 4154// a = b*c*f/f + d*e*f/f 4155// a = (b*c*f + d*e*f)*(1/f) 4156// a = (b*c*(1/c) + d*e*(1/c))*(1/(1/c)) 4157// a = (b + d*e/c)*c 4158// b = dz, c = rrms, d = x, e = sum_xdz * div(-1,rms*N*mean_eps) 4159// a = (dz + x*sum_xdz * div(-1,rms*N*mean_eps)/rrms)*rrms 4160// a = (dz + x*sum_xdz * div(-1,rms*N*mean_eps)*rms)*rrms 4161// a = (dz + x*sum_xdz * div(-rms,rms*N*mean_eps))*rrms 4162// a = (dz + x*sum_xdz * div(-1,N*mean_eps))*rrms 4163// a = (dz + x*div(-sum_xdz,N*mean_eps))*rrms 4164// a = (dz + x*div(-mean_xdz,mean_eps))*rrms 4165// grad[#00] = scale(dz + scale(x, div(-mean_xdz,mean_eps)),rrms) 4166// grad[#00] = scale(dz + scale(x, -mean_xdz/mean_eps),rrms) 4167// dx = scale(dz + scale(x, -mean_xdz/mean_eps),rrms) 4168 } 4169// dx = scale(dz + scale(x, -mean_xdz/mean_eps),rrms) 4170// post-order: 4171// dx := x 4172// dx := scale(dx,-mean_xdz/mean_eps) 4173// dx := add(dx, dz) 4174// dx := scale(dx, rrms) 4175float * dx = (float * ) ((char * )dst -> data + i01 * nb1 + i02 * nb2 + i03 * nb3 ); 4176 4177// dx[i00] = (dz + x*(-sum_xdz/sum_eps)) * rrms 4178// note: https://github.com/ggml-org/ggml/issues/1491 4179const float scale_x = (float ) (- sum_xdz ) /sum_eps ; 4180for (int64_t i00 = 0 ;i00 < ne00 ;i00 ++ ) { 4181dx [i00 ]= (dz [i00 ]+ x [i00 ]* scale_x )* rrms ; 4182 } 4183 } 4184 } 4185 } 4186} 4187 4188void ggml_compute_forward_rms_norm_back ( 4189const ggml_compute_params * params , 4190ggml_tensor * dst ) { 4191 4192const ggml_tensor * src0 = dst -> src [0 ]; 4193 4194switch (src0 -> type ) { 4195case GGML_TYPE_F32 : 4196 { 4197ggml_compute_forward_rms_norm_back_f32 (params ,dst ); 4198 }break ; 4199default : 4200 { 4201GGML_ABORT ("fatal error" ); 4202 } 4203 } 4204} 4205 4206// ggml_compute_forward_group_norm 4207 4208static void ggml_compute_forward_group_norm_f32 ( 4209const ggml_compute_params * params , 4210ggml_tensor * dst ) { 4211 4212const ggml_tensor * src0 = dst -> src [0 ]; 4213 4214GGML_ASSERT (ggml_are_same_shape (src0 ,dst )); 4215 4216GGML_ASSERT (src0 -> nb [0 ]== sizeof (float )); 4217 4218const int ith = params -> ith ; 4219const int nth = params -> nth ; 4220 4221GGML_TENSOR_UNARY_OP_LOCALS 4222 4223// TODO: optimize 4224 4225float eps ; 4226memcpy (& eps ,dst -> op_params + 1 ,sizeof (float )); 4227 4228int n_channels = src0 -> ne [2 ]; 4229int n_groups = dst -> op_params [0 ]; 4230int n_channels_per_group = (n_channels + n_groups - 1 ) /n_groups ; 4231for (int i = ith ;i < n_groups ;i += nth ) { 4232int start = i * n_channels_per_group ; 4233int end = start + n_channels_per_group ; 4234if (end > n_channels ) { 4235end = n_channels ; 4236 } 4237int step = end - start ; 4238 4239for (int64_t i03 = 0 ;i03 < ne03 ;i03 ++ ) { 4240ggml_float sum = 0.0 ; 4241for (int64_t i02 = start ;i02 < end ;i02 ++ ) { 4242for (int64_t i01 = 0 ;i01 < ne01 ;i01 ++ ) { 4243const float * x = (float * )((char * )src0 -> data + i01 * nb01 + i02 * nb02 + i03 * nb03 ); 4244 4245ggml_float sumr = 0.0 ; 4246for (int64_t i00 = 0 ;i00 < ne00 ;i00 ++ ) { 4247sumr += (ggml_float )x [i00 ]; 4248 } 4249sum += sumr ; 4250 } 4251 } 4252const float mean = sum / (ne00 * ne01 * step ); 4253 4254ggml_float sum2 = 0.0 ; 4255for (int64_t i02 = start ;i02 < end ;i02 ++ ) { 4256for (int64_t i01 = 0 ;i01 < ne01 ;i01 ++ ) { 4257const float * x = (float * )((char * )src0 -> data + i01 * nb01 + i02 * nb02 + i03 * nb03 ); 4258 4259float * y = (float * )((char * )dst -> data + i01 * nb1 + i02 * nb2 + i03 * nb3 ); 4260 4261ggml_float sumr = 0.0 ; 4262for (int64_t i00 = 0 ;i00 < ne00 ;i00 ++ ) { 4263float v = x [i00 ]- mean ; 4264y [i00 ]= v ; 4265sumr += (ggml_float )(v * v ); 4266 } 4267sum2 += sumr ; 4268 } 4269 } 4270const float variance = sum2 / (ne00 * ne01 * step ); 4271const float scale = 1.0f /sqrtf (variance + eps ); 4272 4273for (int64_t i02 = start ;i02 < end ;i02 ++ ) { 4274for (int64_t i01 = 0 ;i01 < ne01 ;i01 ++ ) { 4275float * y = (float * )((char * )dst -> data + i01 * nb1 + i02 * nb2 + i03 * nb3 ); 4276ggml_vec_scale_f32 (ne00 ,y ,scale ); 4277 } 4278 } 4279 } 4280 } 4281} 4282 4283void ggml_compute_forward_group_norm ( 4284const ggml_compute_params * params , 4285ggml_tensor * dst ) { 4286 4287const ggml_tensor * src0 = dst -> src [0 ]; 4288 4289switch (src0 -> type ) { 4290case GGML_TYPE_F32 : 4291 { 4292ggml_compute_forward_group_norm_f32 (params ,dst ); 4293 }break ; 4294default : 4295 { 4296GGML_ABORT ("fatal error" ); 4297 } 4298 } 4299} 4300 4301// ggml_compute_forward_l2_norm 4302 4303static void ggml_compute_forward_l2_norm_f32 ( 4304const ggml_compute_params * params , 4305ggml_tensor * dst ) { 4306 4307const ggml_tensor * src0 = dst -> src [0 ]; 4308 4309GGML_ASSERT (ggml_are_same_shape (src0 ,dst )); 4310 4311const int ith = params -> ith ; 4312const int nth = params -> nth ; 4313 4314GGML_TENSOR_UNARY_OP_LOCALS 4315 4316float eps ; 4317memcpy (& eps ,dst -> op_params ,sizeof (float )); 4318 4319GGML_ASSERT (eps >=0.0f ); 4320 4321// TODO: optimize 4322for (int64_t i03 = 0 ;i03 < ne03 ;i03 ++ ) { 4323for (int64_t i02 = 0 ;i02 < ne02 ;i02 ++ ) { 4324for (int64_t i01 = ith ;i01 < ne01 ;i01 += nth ) { 4325const char * x = (const char * )src0 -> data + i01 * nb01 + i02 * nb02 + i03 * nb03 ; 4326 4327ggml_float sum = 0.0 ; 4328for (int64_t i00 = 0 ;i00 < ne00 ;i00 ++ ) { 4329const float xi = * (const float * ) (x + i00 * nb00 ); 4330sum += (ggml_float )(xi * xi ); 4331 } 4332 4333const float scale = 1.0f /fmaxf (sqrtf (sum ),eps ); 4334 4335char * y = (char * )dst -> data + i01 * nb1 + i02 * nb2 + i03 * nb3 ; 4336 4337if (nb00 == sizeof (float )&& nb0 == sizeof (float )) { 4338memcpy (y ,x ,ne00 * sizeof (float )); 4339ggml_vec_scale_f32 (ne00 , (float * )y ,scale ); 4340 }else { 4341for (int64_t i00 = 0 ;i00 < ne00 ;i00 ++ ) { 4342const float xi = * (const float * ) (x + i00 * nb00 ); 4343* (float * ) (y + i00 * nb0 )= xi * scale ; 4344 } 4345 } 4346 } 4347 } 4348 } 4349} 4350 4351void ggml_compute_forward_l2_norm ( 4352const ggml_compute_params * params , 4353ggml_tensor * dst ) { 4354 4355const ggml_tensor * src0 = dst -> src [0 ]; 4356 4357switch (src0 -> type ) { 4358case GGML_TYPE_F32 : 4359 { 4360ggml_compute_forward_l2_norm_f32 (params ,dst ); 4361 }break ; 4362default : 4363 { 4364GGML_ABORT ("fatal error" ); 4365 } 4366 } 4367} 4368 4369// ggml_compute_forward_out_prod 4370 4371static void ggml_compute_forward_out_prod_f32 ( 4372const ggml_compute_params * params , 4373ggml_tensor * dst ) { 4374 4375const ggml_tensor * src0 = dst -> src [0 ]; 4376const ggml_tensor * src1 = dst -> src [1 ]; 4377 4378GGML_TENSOR_BINARY_OP_LOCALS 4379 4380GGML_ASSERT (dst -> type == GGML_TYPE_F32 ); 4381GGML_ASSERT (src0 -> type == GGML_TYPE_F32 ); 4382GGML_ASSERT (src1 -> type == GGML_TYPE_F32 ); 4383 4384const int ith = params -> ith ; 4385const int nth = params -> nth ; 4386 4387GGML_ASSERT (ne0 == ne00 ); 4388GGML_ASSERT (ne1 == ne10 ); 4389GGML_ASSERT (ne2 == ne12 ); 4390GGML_ASSERT (ne3 == ne13 ); 4391 4392GGML_ASSERT (ne2 %ne02 == 0 ); 4393GGML_ASSERT (ne3 %ne03 == 0 ); 4394 4395// we don't support permuted src0 or src1 4396GGML_ASSERT (nb00 == sizeof (float )); 4397 4398// dst cannot be transposed or permuted 4399GGML_ASSERT (nb0 == sizeof (float )); 4400// GGML_ASSERT(nb0 <= nb1); 4401// GGML_ASSERT(nb1 <= nb2); 4402// GGML_ASSERT(nb2 <= nb3); 4403 4404// nb01 >= nb00 - src0 is not transposed 4405// compute by src0 rows 4406 4407if (ith == 0 ) { 4408ggml_vec_set_f32 (ne0 * ne1 * ne2 * ne3 , (float * )dst -> data ,0 ); 4409 } 4410ggml_barrier (params -> threadpool ); 4411 4412// dst[:,:,:,:] = 0 4413// for i2,i3: 4414// for i1: 4415// for i01: 4416// for i0: 4417// dst[i0,i1,i2,i3] += src0[i0,i01,i2,i3] * src1[i1,i01,i2,i3] 4418 4419// parallelize by last three dimensions 4420 4421// total rows in dst 4422const int64_t nr = ne1 * ne2 * ne3 ; 4423 4424// rows per thread 4425const int64_t dr = (nr + nth - 1 )/nth ; 4426 4427// row range for this thread 4428const int64_t ir0 = dr * ith ; 4429const int64_t ir1 = MIN (ir0 + dr ,nr ); 4430 4431// block-tiling attempt 4432const int64_t blck_0 = MAX (GGML_VEC_MAD_UNROLL ,32 ); 4433const int64_t blck_1 = 16 ; 4434 4435// dps == dst per src0, used for group query attention 4436const int64_t dps2 = ne2 /ne02 ; 4437const int64_t dps3 = ne3 /ne03 ; 4438 4439for (int64_t bir = ir0 ;bir < ir1 ;bir += blck_1 ) { 4440const int64_t bir1 = MIN (bir + blck_1 ,ir1 ); 4441for (int64_t bi01 = 0 ;bi01 < ne01 ;bi01 += blck_0 ) { 4442const int64_t bne01 = MIN (bi01 + blck_0 ,ne01 ); 4443for (int64_t ir = bir ;ir < bir1 ;++ ir ) { 4444// dst indices 4445const int64_t i3 = ir /(ne2 * ne1 ); 4446const int64_t i2 = (ir - i3 * ne2 * ne1 )/ne1 ; 4447const int64_t i1 = (ir - i3 * ne2 * ne1 - i2 * ne1 ); 4448 4449const int64_t i02 = i2 /dps2 ; 4450const int64_t i03 = i3 /dps3 ; 4451 4452//const int64_t i10 = i1; 4453const int64_t i12 = i2 ; 4454const int64_t i13 = i3 ; 4455 4456#if GGML_VEC_MAD_UNROLL > 2 4457const int64_t bne01_unroll = bne01 - (bne01 %GGML_VEC_MAD_UNROLL ); 4458for (int64_t i01 = bi01 ;i01 < bne01_unroll ;i01 += GGML_VEC_MAD_UNROLL ) { 4459const int64_t i11 = i01 ; 4460 4461float * s0 = (float * ) ((char * )src0 -> data + (i01 * nb01 + i02 * nb02 + i03 * nb03 )); 4462float * s1 = (float * ) ((char * )src1 -> data + (i1 * nb10 + i11 * nb11 + i12 * nb12 + i13 * nb13 )); 4463float * d = (float * ) ((char * )dst -> data + (i1 * nb1 + i2 * nb2 + i3 * nb3 )); 4464 4465ggml_vec_mad_f32_unroll (ne0 ,nb01 ,nb11 ,d ,s0 ,s1 ); 4466 } 4467for (int64_t i01 = bne01_unroll ;i01 < bne01 ;++ i01 ) { 4468const int64_t i11 = i01 ; 4469 4470float * s0 = (float * ) ((char * )src0 -> data + (i01 * nb01 + i02 * nb02 + i03 * nb03 )); 4471float * s1 = (float * ) ((char * )src1 -> data + (i1 * nb10 + i11 * nb11 + i12 * nb12 + i13 * nb13 )); 4472float * d = (float * ) ((char * )dst -> data + (i1 * nb1 + i2 * nb2 + i3 * nb3 )); 4473 4474ggml_vec_mad_f32 (ne0 ,d ,s0 ,* s1 ); 4475 } 4476#else 4477for (int64_t i01 = bi01 ;i01 < bne01 ;++ i01 ) { 4478const int64_t i11 = i01 ; 4479 4480float * s0 = (float * ) ((char * )src0 -> data + (i01 * nb01 + i02 * nb02 + i03 * nb03 )); 4481float * s1 = (float * ) ((char * )src1 -> data + (i1 * nb10 + i11 * nb11 + i12 * nb12 + i13 * nb13 )); 4482float * d = (float * ) ((char * )dst -> data + (i1 * nb1 + i2 * nb2 + i3 * nb3 )); 4483 4484ggml_vec_mad_f32 (ne0 ,d ,s0 ,* s1 ); 4485 } 4486#endif 4487 } 4488 } 4489 } 4490} 4491 4492static void ggml_compute_forward_out_prod_q_f32 ( 4493const ggml_compute_params * params , 4494ggml_tensor * dst ) { 4495 4496const ggml_tensor * src0 = dst -> src [0 ]; 4497const ggml_tensor * src1 = dst -> src [1 ]; 4498 4499GGML_TENSOR_BINARY_OP_LOCALS ; 4500 4501const int ith = params -> ith ; 4502const int nth = params -> nth ; 4503 4504const ggml_type type = src0 -> type ; 4505ggml_to_float_t const dequantize_row_q = ggml_get_type_traits (type )-> to_float ; 4506 4507GGML_ASSERT (ne02 == ne12 ); 4508GGML_ASSERT (ne03 == ne13 ); 4509GGML_ASSERT (ne2 == ne12 ); 4510GGML_ASSERT (ne3 == ne13 ); 4511 4512// we don't support permuted src0 dim0 4513GGML_ASSERT (nb00 == ggml_type_size (type )); 4514 4515// dst dim0 cannot be transposed or permuted 4516GGML_ASSERT (nb0 == sizeof (float )); 4517// GGML_ASSERT(nb0 <= nb1); 4518// GGML_ASSERT(nb1 <= nb2); 4519// GGML_ASSERT(nb2 <= nb3); 4520 4521GGML_ASSERT (ne0 == ne00 ); 4522GGML_ASSERT (ne1 == ne10 ); 4523GGML_ASSERT (ne2 == ne02 ); 4524GGML_ASSERT (ne3 == ne03 ); 4525 4526// nb01 >= nb00 - src0 is not transposed 4527// compute by src0 rows 4528 4529if (ith == 0 ) { 4530ggml_vec_set_f32 (ne0 * ne1 * ne2 * ne3 , (float * )dst -> data ,0 ); 4531 } 4532ggml_barrier (params -> threadpool ); 4533 4534// parallelize by last three dimensions 4535 4536// total rows in dst 4537const int64_t nr = ne1 * ne2 * ne3 ; 4538 4539// rows per thread 4540const int64_t dr = (nr + nth - 1 )/nth ; 4541 4542// row range for this thread 4543const int64_t ir0 = dr * ith ; 4544const int64_t ir1 = MIN (ir0 + dr ,nr ); 4545 4546// dst[:,:,:,:] = 0 4547// for i2,i3: 4548// for i1: 4549// for i01: 4550// for i0: 4551// dst[i0,i1,i2,i3] += src0[i0,i01,i2,i3] * src1[i1,i01,i2,i3] 4552 4553float * wdata = (float * )params -> wdata + (ne0 + CACHE_LINE_SIZE_F32 )* ith ; 4554 4555for (int64_t ir = ir0 ;ir < ir1 ;++ ir ) { 4556// dst indices 4557const int64_t i3 = ir /(ne2 * ne1 ); 4558const int64_t i2 = (ir - i3 * ne2 * ne1 )/ne1 ; 4559const int64_t i1 = (ir - i3 * ne2 * ne1 - i2 * ne1 ); 4560 4561const int64_t i02 = i2 ; 4562const int64_t i03 = i3 ; 4563 4564//const int64_t i10 = i1; 4565const int64_t i12 = i2 ; 4566const int64_t i13 = i3 ; 4567 4568for (int64_t i01 = 0 ;i01 < ne01 ;++ i01 ) { 4569const int64_t i11 = i01 ; 4570 4571float * s0 = (float * ) ((char * )src0 -> data + (i01 * nb01 + i02 * nb02 + i03 * nb03 )); 4572float * s1 = (float * ) ((char * )src1 -> data + (i1 * nb10 + i11 * nb11 + i12 * nb12 + i13 * nb13 )); 4573float * d = (float * ) ((char * )dst -> data + (i1 * nb1 + i2 * nb2 + i3 * nb3 )); 4574 4575dequantize_row_q (s0 ,wdata ,ne0 ); 4576ggml_vec_mad_f32 (ne0 ,d ,wdata ,* s1 ); 4577 } 4578 } 4579} 4580 4581static void ggml_compute_forward_out_prod_f16_f32 ( 4582const ggml_compute_params * params , 4583ggml_tensor * dst ) { 4584 4585const ggml_tensor * src0 = dst -> src [0 ]; 4586const ggml_tensor * src1 = dst -> src [1 ]; 4587 4588GGML_TENSOR_BINARY_OP_LOCALS ; 4589 4590const int ith = params -> ith ; 4591const int nth = params -> nth ; 4592 4593GGML_ASSERT (src0 -> type == GGML_TYPE_F16 ); 4594GGML_ASSERT (src1 -> type == GGML_TYPE_F32 ); 4595GGML_ASSERT (dst -> type == GGML_TYPE_F32 ); 4596 4597GGML_ASSERT (ne02 == ne12 ); 4598GGML_ASSERT (ne03 == ne13 ); 4599GGML_ASSERT (ne2 == ne12 ); 4600GGML_ASSERT (ne3 == ne13 ); 4601 4602GGML_ASSERT (nb00 == sizeof (ggml_fp16_t )); 4603GGML_ASSERT (nb0 == sizeof (float )); 4604 4605GGML_ASSERT (ne0 == ne00 ); 4606GGML_ASSERT (ne1 == ne10 ); 4607GGML_ASSERT (ne2 == ne02 ); 4608GGML_ASSERT (ne3 == ne03 ); 4609 4610if (ith == 0 ) { 4611ggml_vec_set_f32 (ne0 * ne1 * ne2 * ne3 , (float * )dst -> data ,0 ); 4612 } 4613ggml_barrier (params -> threadpool ); 4614 4615const int64_t nr = ne1 * ne2 * ne3 ; 4616const int64_t dr = (nr + nth - 1 )/nth ; 4617const int64_t ir0 = dr * ith ; 4618const int64_t ir1 = MIN (ir0 + dr ,nr ); 4619 4620float * wdata = (float * )params -> wdata + (ne0 + CACHE_LINE_SIZE_F32 )* ith ; 4621 4622for (int64_t ir = ir0 ;ir < ir1 ;++ ir ) { 4623const int64_t i3 = ir /(ne2 * ne1 ); 4624const int64_t i2 = (ir - i3 * ne2 * ne1 )/ne1 ; 4625const int64_t i1 = (ir - i3 * ne2 * ne1 - i2 * ne1 ); 4626 4627const int64_t i02 = i2 ; 4628const int64_t i03 = i3 ; 4629 4630const int64_t i12 = i2 ; 4631const int64_t i13 = i3 ; 4632 4633float * d = (float * ) ((char * )dst -> data + (i1 * nb1 + i2 * nb2 + i3 * nb3 )); 4634 4635for (int64_t i01 = 0 ;i01 < ne01 ;++ i01 ) { 4636const int64_t i11 = i01 ; 4637ggml_fp16_t * s0 = (ggml_fp16_t * ) ((char * )src0 -> data + (i01 * nb01 + i02 * nb02 + i03 * nb03 )); 4638float * s1 = (float * ) ((char * )src1 -> data + (i1 * nb10 + i11 * nb11 + i12 * nb12 + i13 * nb13 )); 4639ggml_fp16_to_fp32_row (s0 ,wdata ,ne0 ); 4640ggml_vec_mad_f32 (ne0 ,d ,wdata ,* s1 ); 4641 } 4642 } 4643} 4644 4645void ggml_compute_forward_out_prod ( 4646const ggml_compute_params * params , 4647ggml_tensor * dst ) { 4648 4649const ggml_tensor * src0 = dst -> src [0 ]; 4650 4651switch (src0 -> type ) { 4652case GGML_TYPE_Q1_0 : 4653case GGML_TYPE_Q2_0 : 4654case GGML_TYPE_Q4_0 : 4655case GGML_TYPE_Q4_1 : 4656case GGML_TYPE_Q5_0 : 4657case GGML_TYPE_Q5_1 : 4658case GGML_TYPE_Q8_0 : 4659case GGML_TYPE_MXFP4 : 4660case GGML_TYPE_NVFP4 : 4661case GGML_TYPE_Q2_K : 4662case GGML_TYPE_Q3_K : 4663case GGML_TYPE_Q4_K : 4664case GGML_TYPE_Q5_K : 4665case GGML_TYPE_Q6_K : 4666case GGML_TYPE_TQ1_0 : 4667case GGML_TYPE_TQ2_0 : 4668case GGML_TYPE_IQ2_XXS : 4669case GGML_TYPE_IQ2_XS : 4670case GGML_TYPE_IQ3_XXS : 4671case GGML_TYPE_IQ1_S : 4672case GGML_TYPE_IQ1_M : 4673case GGML_TYPE_IQ4_NL : 4674case GGML_TYPE_IQ4_XS : 4675case GGML_TYPE_IQ3_S : 4676case GGML_TYPE_IQ2_S : 4677 { 4678ggml_compute_forward_out_prod_q_f32 (params ,dst ); 4679 }break ; 4680case GGML_TYPE_F16 : 4681 { 4682ggml_compute_forward_out_prod_f16_f32 (params ,dst ); 4683 }break ; 4684case GGML_TYPE_F32 : 4685 { 4686ggml_compute_forward_out_prod_f32 (params ,dst ); 4687 }break ; 4688default : 4689 { 4690GGML_ABORT ("fatal error" ); 4691 } 4692 } 4693} 4694 4695// ggml_compute_forward_scale 4696 4697static void ggml_compute_forward_scale_f32 ( 4698const ggml_compute_params * params , 4699ggml_tensor * dst ) { 4700 4701const ggml_tensor * src0 = dst -> src [0 ]; 4702 4703GGML_ASSERT (ggml_is_contiguous (src0 )); 4704GGML_ASSERT (ggml_is_contiguous (dst )); 4705GGML_ASSERT (ggml_are_same_shape (src0 ,dst )); 4706 4707float s ;// scale factor 4708float b ;// bias 4709 4710memcpy (& s , (float * )dst -> op_params + 0 ,sizeof (float )); 4711memcpy (& b , (float * )dst -> op_params + 1 ,sizeof (float )); 4712 4713const int ith = params -> ith ; 4714const int nth = params -> nth ; 4715 4716const int nc = src0 -> ne [0 ]; 4717const int nr = ggml_nrows (src0 ); 4718 4719// rows per thread 4720const int dr = (nr + nth - 1 )/nth ; 4721 4722// row range for this thread 4723const int ir0 = dr * ith ; 4724const int ir1 = MIN (ir0 + dr ,nr ); 4725 4726const size_t nb01 = src0 -> nb [1 ]; 4727 4728const size_t nb1 = dst -> nb [1 ]; 4729 4730if (b == 0.0f ) { 4731for (int i1 = ir0 ;i1 < ir1 ;i1 ++ ) { 4732if (dst -> data != src0 -> data ) { 4733// src0 is same shape as dst => same indices 4734// TODO: add x parameter to ggml_vec_scale_f32 and remove this memcpy 4735memcpy ((char * )dst -> data + i1 * nb1 , (char * )src0 -> data + i1 * nb01 ,nc * sizeof (float )); 4736 } 4737ggml_vec_scale_f32 (nc , (float * ) ((char * )dst -> data + i1 * nb1 ),s ); 4738 } 4739 }else { 4740for (int i1 = ir0 ;i1 < ir1 ;i1 ++ ) { 4741ggml_vec_mad1_f32 (nc , 4742 (float * ) ((char * )dst -> data + i1 * nb1 ), 4743 (float * ) ((char * )src0 -> data + i1 * nb1 ), 4744s ,b ); 4745 } 4746 } 4747} 4748 4749void ggml_compute_forward_scale ( 4750const ggml_compute_params * params , 4751ggml_tensor * dst ) { 4752 4753const ggml_tensor * src0 = dst -> src [0 ]; 4754 4755switch (src0 -> type ) { 4756case GGML_TYPE_F32 : 4757 { 4758ggml_compute_forward_scale_f32 (params ,dst ); 4759 }break ; 4760default : 4761 { 4762GGML_ABORT ("fatal error" ); 4763 } 4764 } 4765} 4766 4767// ggml_compute_forward_set 4768 4769static void ggml_compute_forward_set_f32 ( 4770const ggml_compute_params * params , 4771ggml_tensor * dst ) { 4772 4773const ggml_tensor * src0 = dst -> src [0 ]; 4774const ggml_tensor * src1 = dst -> src [1 ]; 4775 4776GGML_ASSERT (ggml_are_same_shape (src0 ,dst )); 4777GGML_ASSERT (ggml_is_contiguous (dst )&& ggml_is_contiguous (src0 )); 4778 4779// view src0 and dst with these strides and data offset inbytes during set 4780// nb0 is implicitly element_size because src0 and dst are contiguous 4781size_t nb1 = ((int32_t * )dst -> op_params )[0 ]; 4782size_t nb2 = ((int32_t * )dst -> op_params )[1 ]; 4783size_t nb3 = ((int32_t * )dst -> op_params )[2 ]; 4784size_t offset = ((int32_t * )dst -> op_params )[3 ]; 4785bool inplace = (bool ) ((int32_t * )dst -> op_params )[4 ]; 4786 4787if (!inplace ) { 4788if (params -> ith == 0 ) { 4789// memcpy needs to be synchronized across threads to avoid race conditions. 4790// => do it in INIT phase 4791memcpy ( 4792 ((char * )dst -> data ), 4793 ((char * )src0 -> data ), 4794ggml_nbytes (dst )); 4795 } 4796ggml_barrier (params -> threadpool ); 4797 } 4798 4799const int ith = params -> ith ; 4800const int nth = params -> nth ; 4801 4802const int nr = ggml_nrows (src1 ); 4803const int nc = src1 -> ne [0 ]; 4804 4805GGML_TENSOR_LOCALS (int64_t ,ne1 ,src1 ,ne ) 4806GGML_TENSOR_LOCALS (size_t ,nb1 ,src1 ,nb ) 4807 4808// src0 and dst as viewed during set 4809const size_t nb0 = ggml_element_size (src0 ); 4810 4811const int im0 = (ne10 == 0 ?0 :ne10 - 1 ); 4812const int im1 = (ne11 == 0 ?0 :ne11 - 1 ); 4813const int im2 = (ne12 == 0 ?0 :ne12 - 1 ); 4814const int im3 = (ne13 == 0 ?0 :ne13 - 1 ); 4815 4816GGML_ASSERT (offset + im0 * nb0 + im1 * nb1 + im2 * nb2 + im3 * nb3 <=ggml_nbytes (dst )); 4817 4818GGML_ASSERT (nb10 == sizeof (float )); 4819 4820// rows per thread 4821const int dr = (nr + nth - 1 )/nth ; 4822 4823// row range for this thread 4824const int ir0 = dr * ith ; 4825const int ir1 = MIN (ir0 + dr ,nr ); 4826 4827for (int ir = ir0 ;ir < ir1 ;++ ir ) { 4828// src0 and dst are viewed with shape of src1 and offset 4829// => same indices 4830const int i3 = ir /(ne12 * ne11 ); 4831const int i2 = (ir - i3 * ne12 * ne11 )/ne11 ; 4832const int i1 = (ir - i3 * ne12 * ne11 - i2 * ne11 ); 4833 4834ggml_vec_cpy_f32 (nc , 4835 (float * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * nb1 + offset ), 4836 (float * ) ((char * )src1 -> data + i3 * nb13 + i2 * nb12 + i1 * nb11 )); 4837 } 4838} 4839 4840static void ggml_compute_forward_set_i32 ( 4841const ggml_compute_params * params , 4842ggml_tensor * dst ) { 4843 4844const ggml_tensor * src0 = dst -> src [0 ]; 4845const ggml_tensor * src1 = dst -> src [1 ]; 4846 4847GGML_ASSERT (ggml_are_same_shape (src0 ,dst )); 4848GGML_ASSERT (ggml_is_contiguous (dst )&& ggml_is_contiguous (src0 )); 4849 4850// view src0 and dst with these strides and data offset inbytes during set 4851// nb0 is implicitly element_size because src0 and dst are contiguous 4852size_t nb1 = ((int32_t * )dst -> op_params )[0 ]; 4853size_t nb2 = ((int32_t * )dst -> op_params )[1 ]; 4854size_t nb3 = ((int32_t * )dst -> op_params )[2 ]; 4855size_t offset = ((int32_t * )dst -> op_params )[3 ]; 4856bool inplace = (bool ) ((int32_t * )dst -> op_params )[4 ]; 4857 4858if (!inplace ) { 4859if (params -> ith == 0 ) { 4860// memcpy needs to be synchronized across threads to avoid race conditions. 4861// => do it in INIT phase 4862memcpy ( 4863 ((char * )dst -> data ), 4864 ((char * )src0 -> data ), 4865ggml_nbytes (dst )); 4866 } 4867ggml_barrier (params -> threadpool ); 4868 } 4869 4870const int ith = params -> ith ; 4871const int nth = params -> nth ; 4872 4873const int nr = ggml_nrows (src1 ); 4874const int nc = src1 -> ne [0 ]; 4875 4876GGML_TENSOR_LOCALS (int64_t ,ne1 ,src1 ,ne ) 4877GGML_TENSOR_LOCALS (size_t ,nb1 ,src1 ,nb ) 4878 4879// src0 and dst as viewed during set 4880const size_t nb0 = ggml_element_size (src0 ); 4881 4882const int im0 = (ne10 == 0 ?0 :ne10 - 1 ); 4883const int im1 = (ne11 == 0 ?0 :ne11 - 1 ); 4884const int im2 = (ne12 == 0 ?0 :ne12 - 1 ); 4885const int im3 = (ne13 == 0 ?0 :ne13 - 1 ); 4886 4887GGML_ASSERT (offset + im0 * nb0 + im1 * nb1 + im2 * nb2 + im3 * nb3 <=ggml_nbytes (dst )); 4888 4889GGML_ASSERT (nb10 == sizeof (int32_t )); 4890 4891// rows per thread 4892const int dr = (nr + nth - 1 )/nth ; 4893 4894// row range for this thread 4895const int ir0 = dr * ith ; 4896const int ir1 = MIN (ir0 + dr ,nr ); 4897 4898for (int ir = ir0 ;ir < ir1 ;++ ir ) { 4899// src0 and dst are viewed with shape of src1 and offset 4900// => same indices 4901const int i3 = ir /(ne12 * ne11 ); 4902const int i2 = (ir - i3 * ne12 * ne11 )/ne11 ; 4903const int i1 = (ir - i3 * ne12 * ne11 - i2 * ne11 ); 4904 4905ggml_vec_cpy_i32 (nc , 4906 (int32_t * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * nb1 + offset ), 4907 (int32_t * ) ((char * )src1 -> data + i3 * nb13 + i2 * nb12 + i1 * nb11 )); 4908 } 4909} 4910 4911void ggml_compute_forward_set ( 4912const ggml_compute_params * params , 4913ggml_tensor * dst ) { 4914 4915const ggml_tensor * src0 = dst -> src [0 ]; 4916 4917switch (src0 -> type ) { 4918case GGML_TYPE_F32 : 4919 { 4920ggml_compute_forward_set_f32 (params ,dst ); 4921 }break ; 4922case GGML_TYPE_I32 : 4923 { 4924ggml_compute_forward_set_i32 (params ,dst ); 4925 }break ; 4926case GGML_TYPE_F16 : 4927case GGML_TYPE_BF16 : 4928case GGML_TYPE_Q1_0 : 4929case GGML_TYPE_Q2_0 : 4930case GGML_TYPE_Q4_0 : 4931case GGML_TYPE_Q4_1 : 4932case GGML_TYPE_Q5_0 : 4933case GGML_TYPE_Q5_1 : 4934case GGML_TYPE_Q8_0 : 4935case GGML_TYPE_Q8_1 : 4936case GGML_TYPE_MXFP4 : 4937case GGML_TYPE_NVFP4 : 4938case GGML_TYPE_Q2_K : 4939case GGML_TYPE_Q3_K : 4940case GGML_TYPE_Q4_K : 4941case GGML_TYPE_Q5_K : 4942case GGML_TYPE_Q6_K : 4943case GGML_TYPE_TQ1_0 : 4944case GGML_TYPE_TQ2_0 : 4945case GGML_TYPE_IQ2_XXS : 4946case GGML_TYPE_IQ2_XS : 4947case GGML_TYPE_IQ3_XXS : 4948case GGML_TYPE_IQ1_S : 4949case GGML_TYPE_IQ1_M : 4950case GGML_TYPE_IQ4_NL : 4951case GGML_TYPE_IQ4_XS : 4952case GGML_TYPE_IQ3_S : 4953case GGML_TYPE_IQ2_S : 4954default : 4955 { 4956GGML_ABORT ("fatal error" ); 4957 } 4958 } 4959} 4960 4961// ggml_compute_forward_cpy 4962 4963void ggml_compute_forward_cpy ( 4964const ggml_compute_params * params , 4965ggml_tensor * dst ) { 4966ggml_compute_forward_dup (params ,dst ); 4967} 4968 4969// ggml_compute_forward_cont 4970 4971void ggml_compute_forward_cont ( 4972const ggml_compute_params * params , 4973ggml_tensor * dst ) { 4974ggml_compute_forward_dup (params ,dst ); 4975} 4976 4977// ggml_compute_forward_get_rows 4978 4979static void ggml_compute_forward_get_rows_q ( 4980const ggml_compute_params * params , 4981ggml_tensor * dst ) { 4982 4983const ggml_tensor * src0 = dst -> src [0 ]; 4984const ggml_tensor * src1 = dst -> src [1 ]; 4985 4986GGML_TENSOR_BINARY_OP_LOCALS 4987 4988const int64_t nc = ne00 ; 4989const int64_t nr = ggml_nelements (src1 ); 4990 4991const ggml_type type = src0 -> type ; 4992ggml_to_float_t const dequantize_row_q = ggml_get_type_traits (type )-> to_float ; 4993 4994assert (ne0 == nc ); 4995assert (ne02 == ne11 ); 4996assert (nb00 == ggml_type_size (type )); 4997assert (ggml_nrows (dst )== nr ); 4998 4999const int ith = params -> ith ; 5000const int nth = params -> nth ; 5001 5002// rows per thread 5003const int dr = (nr + nth - 1 )/nth ; 5004 5005// row range for this thread 5006const int ir0 = dr * ith ; 5007const int ir1 = MIN (ir0 + dr ,nr ); 5008 5009for (int64_t i = ir0 ;i < ir1 ;++ i ) { 5010const int64_t i12 = i /(ne11 * ne10 ); 5011const int64_t i11 = (i - i12 * ne11 * ne10 )/ne10 ; 5012const int64_t i10 = (i - i12 * ne11 * ne10 - i11 * ne10 ); 5013const int64_t i01 = * (int32_t * ) ((char * )src1 -> data + i10 * nb10 + i11 * nb11 + i12 * nb12 ); 5014 5015GGML_ASSERT (i01 >=0 && i01 < ne01 ); 5016 5017dequantize_row_q ( 5018 (const void * ) ((char * )src0 -> data + i01 * nb01 + i11 * nb02 + i12 * nb03 ), 5019 (float * ) ((char * )dst -> data + i10 * nb1 + i11 * nb2 + i12 * nb3 ),nc ); 5020 } 5021} 5022 5023static void ggml_compute_forward_get_rows_f16 ( 5024const ggml_compute_params * params , 5025ggml_tensor * dst ) { 5026 5027const ggml_tensor * src0 = dst -> src [0 ]; 5028const ggml_tensor * src1 = dst -> src [1 ]; 5029 5030GGML_TENSOR_BINARY_OP_LOCALS 5031 5032const int64_t nc = ne00 ; 5033const int64_t nr = ggml_nelements (src1 ); 5034 5035assert (ne0 == nc ); 5036assert (ne02 == ne11 ); 5037assert (nb00 == sizeof (ggml_fp16_t )); 5038assert (ggml_nrows (dst )== nr ); 5039 5040const int ith = params -> ith ; 5041const int nth = params -> nth ; 5042 5043// rows per thread 5044const int dr = (nr + nth - 1 )/nth ; 5045 5046// row range for this thread 5047const int ir0 = dr * ith ; 5048const int ir1 = MIN (ir0 + dr ,nr ); 5049 5050for (int64_t i = ir0 ;i < ir1 ;++ i ) { 5051const int64_t i12 = i /(ne11 * ne10 ); 5052const int64_t i11 = (i - i12 * ne11 * ne10 )/ne10 ; 5053const int64_t i10 = (i - i12 * ne11 * ne10 - i11 * ne10 ); 5054const int64_t i01 = * (int32_t * ) ((char * )src1 -> data + i10 * nb10 + i11 * nb11 + i12 * nb12 ); 5055 5056GGML_ASSERT (i01 >=0 && i01 < ne01 ); 5057 5058ggml_cpu_fp16_to_fp32 ( 5059 (const ggml_fp16_t * ) ((char * )src0 -> data + i01 * nb01 + i11 * nb02 + i12 * nb03 ), 5060 (float * ) ((char * )dst -> data + i10 * nb1 + i11 * nb2 + i12 * nb3 ),nc ); 5061 } 5062} 5063 5064static void ggml_compute_forward_get_rows_bf16 ( 5065const ggml_compute_params * params , 5066ggml_tensor * dst ) { 5067 5068const ggml_tensor * src0 = dst -> src [0 ]; 5069const ggml_tensor * src1 = dst -> src [1 ]; 5070 5071GGML_TENSOR_BINARY_OP_LOCALS 5072 5073const int64_t nc = ne00 ; 5074const int64_t nr = ggml_nelements (src1 ); 5075 5076assert (ne0 == nc ); 5077assert (ne02 == ne11 ); 5078assert (nb00 == sizeof (ggml_bf16_t )); 5079assert (ggml_nrows (dst )== nr ); 5080 5081const int ith = params -> ith ; 5082const int nth = params -> nth ; 5083 5084// rows per thread 5085const int dr = (nr + nth - 1 )/nth ; 5086 5087// row range for this thread 5088const int ir0 = dr * ith ; 5089const int ir1 = MIN (ir0 + dr ,nr ); 5090 5091for (int64_t i = ir0 ;i < ir1 ;++ i ) { 5092const int64_t i12 = i /(ne11 * ne10 ); 5093const int64_t i11 = (i - i12 * ne11 * ne10 )/ne10 ; 5094const int64_t i10 = (i - i12 * ne11 * ne10 - i11 * ne10 ); 5095const int64_t i01 = * (int32_t * ) ((char * )src1 -> data + i10 * nb10 + i11 * nb11 + i12 * nb12 ); 5096 5097GGML_ASSERT (i01 >=0 && i01 < ne01 ); 5098 5099ggml_cpu_bf16_to_fp32 ( 5100 (const ggml_bf16_t * ) ((char * )src0 -> data + i01 * nb01 + i11 * nb02 + i12 * nb03 ), 5101 (float * ) ((char * )dst -> data + i10 * nb1 + i11 * nb2 + i12 * nb3 ),nc ); 5102 } 5103} 5104 5105static void ggml_compute_forward_get_rows_f32 ( 5106const ggml_compute_params * params , 5107ggml_tensor * dst ) { 5108 5109const ggml_tensor * src0 = dst -> src [0 ]; 5110const ggml_tensor * src1 = dst -> src [1 ]; 5111 5112GGML_TENSOR_BINARY_OP_LOCALS 5113 5114const int64_t nc = ne00 ; 5115const int64_t nr = ggml_nelements (src1 ); 5116 5117assert (ne0 == nc ); 5118assert (ne02 == ne11 ); 5119assert (nb00 == sizeof (float )); 5120assert (ggml_nrows (dst )== nr ); 5121 5122const int ith = params -> ith ; 5123const int nth = params -> nth ; 5124 5125// rows per thread 5126const int dr = (nr + nth - 1 )/nth ; 5127 5128// row range for this thread 5129const int ir0 = dr * ith ; 5130const int ir1 = MIN (ir0 + dr ,nr ); 5131 5132for (int64_t i = ir0 ;i < ir1 ;++ i ) { 5133const int64_t i12 = i /(ne11 * ne10 ); 5134const int64_t i11 = (i - i12 * ne11 * ne10 )/ne10 ; 5135const int64_t i10 = (i - i12 * ne11 * ne10 - i11 * ne10 ); 5136const int64_t i01 = * (int32_t * ) ((char * )src1 -> data + i10 * nb10 + i11 * nb11 + i12 * nb12 ); 5137 5138GGML_ASSERT (i01 >=0 && i01 < ne01 ); 5139 5140ggml_vec_cpy_f32 (nc , 5141 (float * ) ((char * )dst -> data + i10 * nb1 + i11 * nb2 + i12 * nb3 ), 5142 (float * ) ((char * )src0 -> data + i01 * nb01 + i11 * nb02 + i12 * nb03 )); 5143 } 5144} 5145 5146void ggml_compute_forward_get_rows ( 5147const ggml_compute_params * params , 5148ggml_tensor * dst ) { 5149 5150const ggml_tensor * src0 = dst -> src [0 ]; 5151 5152switch (src0 -> type ) { 5153case GGML_TYPE_Q1_0 : 5154case GGML_TYPE_Q2_0 : 5155case GGML_TYPE_Q4_0 : 5156case GGML_TYPE_Q4_1 : 5157case GGML_TYPE_Q5_0 : 5158case GGML_TYPE_Q5_1 : 5159case GGML_TYPE_Q8_0 : 5160case GGML_TYPE_Q8_1 : 5161case GGML_TYPE_MXFP4 : 5162case GGML_TYPE_NVFP4 : 5163case GGML_TYPE_Q2_K : 5164case GGML_TYPE_Q3_K : 5165case GGML_TYPE_Q4_K : 5166case GGML_TYPE_Q5_K : 5167case GGML_TYPE_Q6_K : 5168case GGML_TYPE_TQ1_0 : 5169case GGML_TYPE_TQ2_0 : 5170case GGML_TYPE_IQ2_XXS : 5171case GGML_TYPE_IQ2_XS : 5172case GGML_TYPE_IQ3_XXS : 5173case GGML_TYPE_IQ1_S : 5174case GGML_TYPE_IQ1_M : 5175case GGML_TYPE_IQ4_NL : 5176case GGML_TYPE_IQ4_XS : 5177case GGML_TYPE_IQ3_S : 5178case GGML_TYPE_IQ2_S : 5179 { 5180ggml_compute_forward_get_rows_q (params ,dst ); 5181 }break ; 5182case GGML_TYPE_F16 : 5183 { 5184ggml_compute_forward_get_rows_f16 (params ,dst ); 5185 }break ; 5186case GGML_TYPE_BF16 : 5187 { 5188ggml_compute_forward_get_rows_bf16 (params ,dst ); 5189 }break ; 5190case GGML_TYPE_F32 : 5191case GGML_TYPE_I32 : 5192 { 5193ggml_compute_forward_get_rows_f32 (params ,dst ); 5194 }break ; 5195default : 5196 { 5197GGML_ABORT ("fatal error" ); 5198 } 5199 } 5200 5201//static bool first = true; 5202//printf("ne0 = %d, ne1 = %d, ne2 = %d\n", dst->ne[0], dst->ne[1], dst->ne[2]); 5203//if (first) { 5204// first = false; 5205//} else { 5206// for (int k = 0; k < dst->ne[1]; ++k) { 5207// for (int j = 0; j < dst->ne[0]/16; ++j) { 5208// for (int i = 0; i < 16; ++i) { 5209// printf("%8.4f ", ((float *) dst->data)[k*dst->ne[0] + j*16 + i]); 5210// } 5211// printf("\n"); 5212// } 5213// printf("\n"); 5214// } 5215// printf("\n"); 5216// exit(0); 5217//} 5218} 5219 5220template < typename src_t ,typename idx_t > 5221static void ggml_compute_forward_set_rows_impl ( 5222const ggml_compute_params * params , 5223ggml_tensor * dst ) { 5224 5225const ggml_tensor * src0 = dst -> src [0 ]; 5226const ggml_tensor * src1 = dst -> src [1 ]; 5227 5228GGML_TENSOR_BINARY_OP_LOCALS 5229 5230const int64_t nc = ne00 ; 5231const int64_t nr = ne01 ; 5232 5233assert (ne0 == nc ); 5234assert (ne2 == ne02 ); 5235assert (ne3 == ne03 ); 5236GGML_ASSERT (src0 -> type == GGML_TYPE_F32 || src0 -> type == GGML_TYPE_F16 ); 5237assert (ne02 %ne11 == 0 ); 5238assert (ne03 %ne12 == 0 ); 5239 5240const int ith = params -> ith ; 5241const int nth = params -> nth ; 5242 5243// rows per thread 5244const int64_t dr = (nr + nth - 1 )/nth ; 5245 5246// row range for this thread 5247const int64_t ir0 = dr * ith ; 5248const int64_t ir1 = std::min (ir0 + dr ,nr ); 5249 5250const size_t rs = ggml_row_size (src0 -> type ,nc ); 5251 5252ggml_from_float_t const from_float = ggml_get_type_traits_cpu (dst -> type )-> from_float ; 5253 5254for (int64_t i03 = 0 ;i03 < ne03 ;++ i03 ) { 5255for (int64_t i02 = 0 ;i02 < ne02 ;++ i02 ) { 5256for (int64_t i = ir0 ;i < ir1 ;++ i ) { 5257const int64_t i12 = i03 %ne12 ; 5258const int64_t i11 = i02 %ne11 ; 5259const int64_t i10 = i ; 5260 5261const int64_t i1 = * (idx_t * ) ((char * )src1 -> data + i10 * nb10 + i11 * nb11 + i12 * nb12 ); 5262 5263GGML_ASSERT (i1 >=0 && i1 < ne1 ); 5264 5265if constexpr (std::is_same_v < src_t ,float > ) { 5266from_float ( 5267 (const float * ) ((char * )src0 -> data + i * nb01 + i02 * nb02 + i03 * nb03 ), 5268 ((char * )dst -> data + i1 * nb1 + i02 * nb2 + i03 * nb3 ),nc ); 5269 }else if constexpr (std::is_same_v < src_t ,ggml_fp16_t > ) { 5270if (dst -> type == GGML_TYPE_F16 ) { 5271memcpy ( 5272 ((char * )dst -> data + i1 * nb1 + i02 * nb2 + i03 * nb3 ), 5273 ((char * )src0 -> data + i * nb01 + i02 * nb02 + i03 * nb03 ), 5274rs ); 5275 }else { 5276float * wdata = (float * )params -> wdata + (nc + CACHE_LINE_SIZE_F32 )* ith ; 5277ggml_fp16_to_fp32_row ( 5278 (const ggml_fp16_t * ) ((char * )src0 -> data + i * nb01 + i02 * nb02 + i03 * nb03 ), 5279wdata ,nc ); 5280from_float (wdata , 5281 ((char * )dst -> data + i1 * nb1 + i02 * nb2 + i03 * nb3 ),nc ); 5282 } 5283 }else { 5284GGML_ABORT ("src0->type = %d (%s) not supported" ,src0 -> type ,ggml_type_name (src0 -> type )); 5285 } 5286 } 5287 } 5288 } 5289} 5290 5291void ggml_compute_forward_set_rows ( 5292const ggml_compute_params * params , 5293ggml_tensor * dst ) { 5294 5295const ggml_tensor * src0 = dst -> src [0 ]; 5296const ggml_tensor * src1 = dst -> src [1 ]; 5297 5298switch (src0 -> type ) { 5299case GGML_TYPE_F32 : 5300 { 5301if (src1 -> type == GGML_TYPE_I64 ) { 5302ggml_compute_forward_set_rows_impl < float ,int64_t > (params ,dst ); 5303 }else if (src1 -> type == GGML_TYPE_I32 ) { 5304ggml_compute_forward_set_rows_impl < float ,int32_t > (params ,dst ); 5305 }else { 5306GGML_ABORT ("src1->type = %d (%s) not supported" ,src1 -> type ,ggml_type_name (src1 -> type )); 5307 } 5308 }break ; 5309case GGML_TYPE_F16 : 5310 { 5311if (src1 -> type == GGML_TYPE_I64 ) { 5312ggml_compute_forward_set_rows_impl < ggml_fp16_t ,int64_t > (params ,dst ); 5313 }else if (src1 -> type == GGML_TYPE_I32 ) { 5314ggml_compute_forward_set_rows_impl < ggml_fp16_t ,int32_t > (params ,dst ); 5315 }else { 5316GGML_ABORT ("src1->type = %d (%s) not supported" ,src1 -> type ,ggml_type_name (src1 -> type )); 5317 } 5318 }break ; 5319default : 5320 { 5321GGML_ABORT ("src0->type = %d (%s) not supported" ,src0 -> type ,ggml_type_name (src0 -> type )); 5322 } 5323 } 5324} 5325 5326// ggml_compute_forward_get_rows_back 5327 5328static void ggml_compute_forward_get_rows_back_f32_f16 ( 5329const ggml_compute_params * params , 5330ggml_tensor * dst ) { 5331 5332const ggml_tensor * src0 = dst -> src [0 ]; 5333const ggml_tensor * src1 = dst -> src [1 ]; 5334 5335if (params -> ith != 0 ) { 5336return ; 5337 } 5338 5339GGML_ASSERT (ggml_is_contiguous (dst )); 5340 5341// ggml_compute_forward_dup_same_cont(params, opt0, dst); 5342 5343memset (dst -> data ,0 ,ggml_nbytes (dst )); 5344 5345const int nc = src0 -> ne [0 ]; 5346const int nr = ggml_nelements (src1 ); 5347 5348GGML_ASSERT (dst -> ne [0 ]== nc ); 5349GGML_ASSERT (src0 -> nb [0 ]== sizeof (ggml_fp16_t )); 5350 5351for (int i = 0 ;i < nr ;++ i ) { 5352const int r = ((int32_t * )src1 -> data )[i ]; 5353 5354for (int j = 0 ;j < nc ;++ j ) { 5355ggml_fp16_t v = ((ggml_fp16_t * ) ((char * )src0 -> data + i * src0 -> nb [1 ]))[j ]; 5356 ((float * ) ((char * )dst -> data + r * dst -> nb [1 ]))[j ]+= GGML_CPU_FP16_TO_FP32 (v ); 5357 } 5358 } 5359} 5360 5361static void ggml_compute_forward_get_rows_back_f32 ( 5362const ggml_compute_params * params , 5363ggml_tensor * dst ) { 5364 5365const ggml_tensor * src0 = dst -> src [0 ]; 5366const ggml_tensor * src1 = dst -> src [1 ]; 5367 5368if (params -> ith != 0 ) { 5369return ; 5370 } 5371 5372GGML_ASSERT (ggml_is_contiguous (dst )); 5373 5374// ggml_compute_forward_dup_same_cont(params, opt0, dst); 5375 5376memset (dst -> data ,0 ,ggml_nbytes (dst )); 5377 5378const int nc = src0 -> ne [0 ]; 5379const int nr = ggml_nelements (src1 ); 5380 5381GGML_ASSERT (dst -> ne [0 ]== nc ); 5382GGML_ASSERT (src0 -> nb [0 ]== sizeof (float )); 5383 5384for (int i = 0 ;i < nr ;++ i ) { 5385const int r = ((int32_t * )src1 -> data )[i ]; 5386 5387ggml_vec_add_f32 (nc , 5388 (float * ) ((char * )dst -> data + r * dst -> nb [1 ]), 5389 (float * ) ((char * )dst -> data + r * dst -> nb [1 ]), 5390 (float * ) ((char * )src0 -> data + i * src0 -> nb [1 ])); 5391 } 5392} 5393 5394void ggml_compute_forward_get_rows_back ( 5395const ggml_compute_params * params , 5396ggml_tensor * dst ) { 5397 5398const ggml_tensor * src0 = dst -> src [0 ]; 5399 5400switch (src0 -> type ) { 5401case GGML_TYPE_F16 : 5402 { 5403ggml_compute_forward_get_rows_back_f32_f16 (params ,dst ); 5404 }break ; 5405case GGML_TYPE_F32 : 5406 { 5407ggml_compute_forward_get_rows_back_f32 (params ,dst ); 5408 }break ; 5409default : 5410 { 5411GGML_ABORT ("fatal error" ); 5412 } 5413 } 5414 5415//static bool first = true; 5416//printf("ne0 = %d, ne1 = %d, ne2 = %d\n", dst->ne[0], dst->ne[1], dst->ne[2]); 5417//if (first) { 5418// first = false; 5419//} else { 5420// for (int k = 0; k < dst->ne[1]; ++k) { 5421// for (int j = 0; j < dst->ne[0]/16; ++j) { 5422// for (int i = 0; i < 16; ++i) { 5423// printf("%8.4f ", ((float *) dst->data)[k*dst->ne[0] + j*16 + i]); 5424// } 5425// printf("\n"); 5426// } 5427// printf("\n"); 5428// } 5429// printf("\n"); 5430// exit(0); 5431//} 5432} 5433 5434// ggml_compute_forward_diag 5435 5436static void ggml_compute_forward_diag_f32 ( 5437const ggml_compute_params * params , 5438ggml_tensor * dst ) { 5439 5440const ggml_tensor * src0 = dst -> src [0 ]; 5441 5442if (params -> ith != 0 ) { 5443return ; 5444 } 5445 5446// TODO: handle transposed/permuted matrices 5447 5448GGML_TENSOR_UNARY_OP_LOCALS 5449 5450GGML_ASSERT (ne00 == ne0 ); 5451GGML_ASSERT (ne00 == ne1 ); 5452GGML_ASSERT (ne01 == 1 ); 5453GGML_ASSERT (ne02 == ne2 ); 5454GGML_ASSERT (ne03 == ne3 ); 5455 5456GGML_ASSERT (nb00 == sizeof (float )); 5457GGML_ASSERT (nb0 == sizeof (float )); 5458 5459for (int i3 = 0 ;i3 < ne3 ;i3 ++ ) { 5460for (int i2 = 0 ;i2 < ne2 ;i2 ++ ) { 5461for (int i1 = 0 ;i1 < ne1 ;i1 ++ ) { 5462float * d = (float * )((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * nb1 ); 5463float * s = (float * )((char * )src0 -> data + i3 * nb03 + i2 * nb02 ); 5464for (int i0 = 0 ;i0 < i1 ;i0 ++ ) { 5465d [i0 ]= 0 ; 5466 } 5467d [i1 ]= s [i1 ]; 5468for (int i0 = i1 + 1 ;i0 < ne0 ;i0 ++ ) { 5469d [i0 ]= 0 ; 5470 } 5471 } 5472 } 5473 } 5474} 5475 5476void ggml_compute_forward_diag ( 5477const ggml_compute_params * params , 5478ggml_tensor * dst ) { 5479 5480const ggml_tensor * src0 = dst -> src [0 ]; 5481 5482switch (src0 -> type ) { 5483case GGML_TYPE_F32 : 5484 { 5485ggml_compute_forward_diag_f32 (params ,dst ); 5486 }break ; 5487default : 5488 { 5489GGML_ABORT ("fatal error" ); 5490 } 5491 } 5492} 5493 5494// ggml_compute_forward_diag_mask_inf 5495 5496static void ggml_compute_forward_diag_mask_f32 ( 5497const ggml_compute_params * params , 5498ggml_tensor * dst , 5499const float value ) { 5500 5501const ggml_tensor * src0 = dst -> src [0 ]; 5502 5503const int ith = params -> ith ; 5504const int nth = params -> nth ; 5505 5506const int n_past = ((int32_t * )dst -> op_params )[0 ]; 5507const bool inplace = src0 -> data == dst -> data ; 5508 5509GGML_ASSERT (n_past >=0 ); 5510 5511if (!inplace ) { 5512if (ith == 0 ) { 5513// memcpy needs to be synchronized across threads to avoid race conditions. 5514// => do it in INIT phase 5515GGML_ASSERT (ggml_nelements (dst )== ggml_nelements (src0 )); 5516GGML_ASSERT (ggml_is_contiguous (dst )&& ggml_is_contiguous (src0 )); 5517memcpy ( 5518 ((char * )dst -> data ), 5519 ((char * )src0 -> data ), 5520ggml_nbytes (dst )); 5521 } 5522ggml_barrier (params -> threadpool ); 5523 } 5524 5525// TODO: handle transposed/permuted matrices 5526 5527const int n = ggml_nrows (src0 ); 5528const int nc = src0 -> ne [0 ]; 5529const int nr = src0 -> ne [1 ]; 5530const int nz = n /nr ; 5531 5532GGML_ASSERT (dst -> nb [0 ]== sizeof (float )); 5533GGML_ASSERT (src0 -> nb [0 ]== sizeof (float )); 5534 5535for (int k = 0 ;k < nz ;k ++ ) { 5536for (int j = ith ;j < nr ;j += nth ) { 5537for (int i = n_past ;i < nc ;i ++ ) { 5538if (i > n_past + j ) { 5539* (float * )((char * )dst -> data + k * dst -> nb [2 ]+ j * dst -> nb [1 ]+ i * dst -> nb [0 ])= value ; 5540 } 5541 } 5542 } 5543 } 5544} 5545 5546void ggml_compute_forward_diag_mask_inf ( 5547const ggml_compute_params * params , 5548ggml_tensor * dst ) { 5549 5550const ggml_tensor * src0 = dst -> src [0 ]; 5551 5552switch (src0 -> type ) { 5553case GGML_TYPE_F32 : 5554 { 5555ggml_compute_forward_diag_mask_f32 (params ,dst ,- INFINITY ); 5556 }break ; 5557default : 5558 { 5559GGML_ABORT ("fatal error" ); 5560 } 5561 } 5562} 5563 5564void ggml_compute_forward_diag_mask_zero ( 5565const ggml_compute_params * params , 5566ggml_tensor * dst ) { 5567 5568const ggml_tensor * src0 = dst -> src [0 ]; 5569 5570switch (src0 -> type ) { 5571case GGML_TYPE_F32 : 5572 { 5573ggml_compute_forward_diag_mask_f32 (params ,dst ,0 ); 5574 }break ; 5575default : 5576 { 5577GGML_ABORT ("fatal error" ); 5578 } 5579 } 5580} 5581 5582// ggml_compute_forward_soft_max 5583 5584static void ggml_compute_forward_soft_max_f32 ( 5585const ggml_compute_params * params , 5586ggml_tensor * dst ) { 5587 5588const ggml_tensor * src0 = dst -> src [0 ]; 5589const ggml_tensor * src1 = dst -> src [1 ]; 5590const ggml_tensor * src2 = dst -> src [2 ]; 5591 5592assert (ggml_is_contiguous (dst )); 5593assert (ggml_are_same_shape (src0 ,dst )); 5594 5595float scale = 1.0f ; 5596float max_bias = 0.0f ; 5597 5598memcpy (& scale , (float * )dst -> op_params + 0 ,sizeof (float )); 5599memcpy (& max_bias , (float * )dst -> op_params + 1 ,sizeof (float )); 5600 5601const int ith = params -> ith ; 5602const int nth = params -> nth ; 5603 5604GGML_TENSOR_UNARY_OP_LOCALS 5605 5606const int64_t nb11 = src1 ?src1 -> nb [1 ] :1 ; 5607const int64_t nb12 = src1 ?src1 -> nb [2 ] :1 ; 5608const int64_t nb13 = src1 ?src1 -> nb [3 ] :1 ; 5609 5610const int64_t ne12 = src1 ?src1 -> ne [2 ] :1 ; 5611const int64_t ne13 = src1 ?src1 -> ne [3 ] :1 ; 5612 5613// TODO: is this supposed to be ceil instead of floor? 5614// https://huggingface.co/mosaicml/mpt-7b/blob/main/attention.py#L370 5615const uint32_t n_head = ne02 ; 5616const uint32_t n_head_log2 = 1u << (uint32_t )floor (log2 (n_head )); 5617 5618const float m0 = powf (2.0f ,- (max_bias ) /n_head_log2 ); 5619const float m1 = powf (2.0f ,- (max_bias /2.0f ) /n_head_log2 ); 5620 5621float * wp = (float * )params -> wdata + (ne00 + CACHE_LINE_SIZE_F32 )* ith ; 5622 5623const bool use_f16 = (src1 && src1 -> type == GGML_TYPE_F16 ); 5624 5625// sinks 5626const float * sk = src2 ? (float * )((char * )src2 -> data ) :nullptr ; 5627 5628for (int64_t i03 = 0 ;i03 < ne03 ;i03 ++ ) { 5629for (int64_t i02 = 0 ;i02 < ne02 ;i02 ++ ) { 5630for (int64_t i01 = ith ;i01 < ne01 ;i01 += nth ) { 5631const int64_t i11 = i01 ; 5632const int64_t i12 = i02 %ne12 ; 5633const int64_t i13 = i03 %ne13 ; 5634 5635// ALiBi 5636const uint32_t h = i02 ;// head 5637const float slope = (max_bias > 0.0f ) ?h < n_head_log2 ?powf (m0 ,h + 1 ) :powf (m1 ,2 * (h - n_head_log2 )+ 1 ) :1.0f ; 5638 5639float * sp = (float * )((char * )src0 -> data + i01 * nb01 + i02 * nb02 + i03 * nb03 ); 5640float * dp = (float * )((char * )dst -> data + i01 * nb1 + i02 * nb2 + i03 * nb3 ); 5641 5642// broadcast the mask across rows 5643ggml_fp16_t * mp_f16 = src1 ? (ggml_fp16_t * )((char * )src1 -> data + i11 * nb11 + i12 * nb12 + i13 * nb13 ) :NULL ; 5644float * mp_f32 = src1 ? (float * )((char * )src1 -> data + i11 * nb11 + i12 * nb12 + i13 * nb13 ) :NULL ; 5645 5646ggml_vec_cpy_f32 (ne00 ,wp ,sp ); 5647ggml_vec_scale_f32 (ne00 ,wp ,scale ); 5648if (mp_f32 ) { 5649if (use_f16 ) { 5650for (int i = 0 ;i < ne00 ;++ i ) { 5651wp [i ]+= slope * GGML_CPU_FP16_TO_FP32 (mp_f16 [i ]); 5652 } 5653 }else { 5654for (int i = 0 ;i < ne00 ;++ i ) { 5655wp [i ]+= slope * mp_f32 [i ]; 5656 } 5657 } 5658 } 5659 5660#ifndef NDEBUG 5661for (int i = 0 ;i < ne00 ;++ i ) { 5662//printf("p[%d] = %f\n", i, p[i]); 5663assert (!isnan (wp [i ])); 5664 } 5665#endif // NDEBUG 5666 5667float max = - INFINITY ; 5668ggml_vec_max_f32 (ne00 ,& max ,wp ); 5669 5670// if we have sinks, make a correction as if they were included in the softmax 5671if (sk ) { 5672max = MAX (max ,sk [i02 ]); 5673 } 5674 5675ggml_float sum = ggml_vec_soft_max_f32 (ne00 ,dp ,wp ,max ); 5676assert (sum > 0.0 ); 5677 5678if (sk ) { 5679sum += (ggml_float )expf (sk [i02 ]- max ); 5680 } 5681 5682sum = 1.0 /sum ; 5683ggml_vec_scale_f32 (ne00 ,dp ,sum ); 5684 5685#ifndef NDEBUG 5686for (int i = 0 ;i < ne00 ;++ i ) { 5687assert (!isnan (dp [i ])); 5688assert (!isinf (dp [i ])); 5689 } 5690#endif // NDEBUG 5691 } 5692 } 5693 } 5694} 5695 5696void ggml_compute_forward_soft_max ( 5697const ggml_compute_params * params , 5698ggml_tensor * dst ) { 5699 5700const ggml_tensor * src0 = dst -> src [0 ]; 5701 5702switch (src0 -> type ) { 5703case GGML_TYPE_F32 : 5704 { 5705ggml_compute_forward_soft_max_f32 (params ,dst ); 5706 }break ; 5707default : 5708 { 5709GGML_ABORT ("fatal error" ); 5710 } 5711 } 5712} 5713 5714 5715// ggml_compute_forward_soft_max_ext_back 5716 5717static void ggml_compute_forward_soft_max_ext_back_f32 ( 5718const ggml_compute_params * params , 5719ggml_tensor * dst ) { 5720 5721const ggml_tensor * src0 = dst -> src [0 ]; 5722const ggml_tensor * src1 = dst -> src [1 ]; 5723 5724GGML_ASSERT (ggml_is_contiguous (src0 )); 5725GGML_ASSERT (ggml_is_contiguous (src1 )); 5726GGML_ASSERT (ggml_is_contiguous (dst )); 5727GGML_ASSERT (ggml_are_same_shape (src0 ,dst )); 5728GGML_ASSERT (ggml_are_same_shape (src1 ,dst )); 5729 5730float scale = 1.0f ; 5731float max_bias = 0.0f ; 5732 5733memcpy (& scale , (const float * )dst -> op_params + 0 ,sizeof (float )); 5734memcpy (& max_bias , (const float * )dst -> op_params + 1 ,sizeof (float )); 5735 5736GGML_ASSERT (max_bias == 0.0f ); 5737 5738// TODO: handle transposed/permuted matrices 5739 5740const int ith = params -> ith ; 5741const int nth = params -> nth ; 5742 5743const int nc = src0 -> ne [0 ]; 5744const int nr = ggml_nrows (src0 ); 5745 5746// rows per thread 5747const int dr = (nr + nth - 1 )/nth ; 5748 5749// row range for this thread 5750const int ir0 = dr * ith ; 5751const int ir1 = MIN (ir0 + dr ,nr ); 5752 5753for (int i1 = ir0 ;i1 < ir1 ;i1 ++ ) { 5754float * dy = (float * )((char * )src0 -> data + i1 * src0 -> nb [1 ]); 5755float * y = (float * )((char * )src1 -> data + i1 * src1 -> nb [1 ]); 5756float * dx = (float * )((char * )dst -> data + i1 * dst -> nb [1 ]); 5757 5758#ifndef NDEBUG 5759for (int i = 0 ;i < nc ;++ i ) { 5760//printf("p[%d] = %f\n", i, p[i]); 5761assert (!isnan (dy [i ])); 5762assert (!isnan (y [i ])); 5763 } 5764#endif // NDEBUG 5765// Jii = yi - yi*yi 5766// Jij = -yi*yj 5767// J = diag(y)-y.T*y 5768// dx = J * dy 5769// dxk = sum_i(Jki * dyi) 5770// dxk = sum_i(-yk*yi * dyi) - (-yk*yk)*dyk + (yk - yk*yk)*dyk 5771// dxk = sum_i(-yk*yi * dyi) + yk*yk*dyk + yk*dyk - yk*yk*dyk 5772// dxk = sum_i(-yk*yi * dyi) + yk*dyk 5773// dxk = -yk * sum_i(yi * dyi) + yk*dyk 5774// dxk = -yk * dot(y, dy) + yk*dyk 5775// dxk = yk * (- dot(y, dy) + dyk) 5776// dxk = yk * (dyk - dot(y, dy)) 5777// 5778// post-order: 5779// dot_y_dy := dot(y, dy) 5780// dx := dy 5781// dx := dx - dot_y_dy 5782// dx := dx * y 5783 5784// linear runtime, no additional memory 5785float dot_y_dy = 0 ; 5786ggml_vec_dot_f32 (nc ,& dot_y_dy ,0 ,y ,0 ,dy ,0 ,1 ); 5787ggml_vec_cpy_f32 (nc ,dx ,dy ); 5788ggml_vec_acc1_f32 (nc ,dx ,- dot_y_dy ); 5789ggml_vec_mul_f32 (nc ,dx ,dx ,y ); 5790ggml_vec_scale_f32 (nc ,dx ,scale ); 5791 5792#ifndef NDEBUG 5793for (int i = 0 ;i < nc ;++ i ) { 5794assert (!isnan (dx [i ])); 5795assert (!isinf (dx [i ])); 5796 } 5797#endif // NDEBUG 5798 } 5799} 5800 5801void ggml_compute_forward_soft_max_ext_back ( 5802const ggml_compute_params * params , 5803ggml_tensor * dst ) { 5804 5805const ggml_tensor * src0 = dst -> src [0 ]; 5806 5807switch (src0 -> type ) { 5808case GGML_TYPE_F32 : 5809 { 5810ggml_compute_forward_soft_max_ext_back_f32 (params ,dst ); 5811 }break ; 5812default : 5813 { 5814GGML_ABORT ("fatal error" ); 5815 } 5816 } 5817} 5818 5819// ggml_compute_forward_clamp 5820 5821static void ggml_compute_forward_clamp_f32 ( 5822const ggml_compute_params * params , 5823ggml_tensor * dst ) { 5824 5825const ggml_tensor * src0 = dst -> src [0 ]; 5826 5827float min ; 5828float max ; 5829memcpy (& min , (float * )dst -> op_params + 0 ,sizeof (float )); 5830memcpy (& max , (float * )dst -> op_params + 1 ,sizeof (float )); 5831 5832const int ith = params -> ith ; 5833const int nth = params -> nth ; 5834 5835const int n = ggml_nrows (src0 ); 5836const int nc = src0 -> ne [0 ]; 5837 5838const size_t nb00 = src0 -> nb [0 ]; 5839const size_t nb01 = src0 -> nb [1 ]; 5840 5841const size_t nb0 = dst -> nb [0 ]; 5842const size_t nb1 = dst -> nb [1 ]; 5843 5844GGML_ASSERT (nb0 == sizeof (float )); 5845GGML_ASSERT (nb00 == sizeof (float )); 5846 5847for (int j = ith ;j < n ;j += nth ) { 5848float * dst_ptr = (float * ) ((char * )dst -> data + j * nb1 ); 5849float * src0_ptr = (float * ) ((char * )src0 -> data + j * nb01 ); 5850 5851for (int i = 0 ;i < nc ;i ++ ) { 5852dst_ptr [i ]= MAX (MIN (src0_ptr [i ],max ),min ); 5853 } 5854 } 5855} 5856 5857static void ggml_compute_forward_clamp_f16 ( 5858const ggml_compute_params * params , 5859ggml_tensor * dst ) { 5860 5861const ggml_tensor * src0 = dst -> src [0 ]; 5862 5863float min ; 5864float max ; 5865memcpy (& min , (float * )dst -> op_params + 0 ,sizeof (float )); 5866memcpy (& max , (float * )dst -> op_params + 1 ,sizeof (float )); 5867 5868const int ith = params -> ith ; 5869const int nth = params -> nth ; 5870 5871const int n = ggml_nrows (src0 ); 5872const int nc = src0 -> ne [0 ]; 5873 5874const size_t nb00 = src0 -> nb [0 ]; 5875const size_t nb01 = src0 -> nb [1 ]; 5876 5877const size_t nb0 = dst -> nb [0 ]; 5878const size_t nb1 = dst -> nb [1 ]; 5879 5880GGML_ASSERT (nb0 == sizeof (ggml_fp16_t )); 5881GGML_ASSERT (nb00 == sizeof (ggml_fp16_t )); 5882 5883for (int j = ith ;j < n ;j += nth ) { 5884ggml_fp16_t * dst_ptr = (ggml_fp16_t * ) ((char * )dst -> data + j * nb1 ); 5885ggml_fp16_t * src0_ptr = (ggml_fp16_t * ) ((char * )src0 -> data + j * nb01 ); 5886 5887for (int i = 0 ;i < nc ;i ++ ) { 5888float v = GGML_CPU_FP16_TO_FP32 (src0_ptr [i ]); 5889dst_ptr [i ]= GGML_CPU_FP32_TO_FP16 (MAX (MIN (v ,max ),min )); 5890 } 5891 } 5892} 5893 5894void ggml_compute_forward_clamp ( 5895const ggml_compute_params * params , 5896ggml_tensor * dst ) { 5897 5898const ggml_tensor * src0 = dst -> src [0 ]; 5899 5900switch (src0 -> type ) { 5901case GGML_TYPE_F32 : 5902 { 5903ggml_compute_forward_clamp_f32 (params ,dst ); 5904 }break ; 5905case GGML_TYPE_F16 : 5906 { 5907ggml_compute_forward_clamp_f16 (params ,dst ); 5908 }break ; 5909case GGML_TYPE_BF16 : 5910case GGML_TYPE_Q1_0 : 5911case GGML_TYPE_Q2_0 : 5912case GGML_TYPE_Q4_0 : 5913case GGML_TYPE_Q4_1 : 5914case GGML_TYPE_Q5_0 : 5915case GGML_TYPE_Q5_1 : 5916case GGML_TYPE_Q8_0 : 5917case GGML_TYPE_Q8_1 : 5918case GGML_TYPE_MXFP4 : 5919case GGML_TYPE_NVFP4 : 5920case GGML_TYPE_Q2_K : 5921case GGML_TYPE_Q3_K : 5922case GGML_TYPE_Q4_K : 5923case GGML_TYPE_Q5_K : 5924case GGML_TYPE_Q6_K : 5925case GGML_TYPE_TQ1_0 : 5926case GGML_TYPE_TQ2_0 : 5927case GGML_TYPE_IQ2_XXS : 5928case GGML_TYPE_IQ2_XS : 5929case GGML_TYPE_IQ3_XXS : 5930case GGML_TYPE_IQ1_S : 5931case GGML_TYPE_IQ1_M : 5932case GGML_TYPE_IQ4_NL : 5933case GGML_TYPE_IQ4_XS : 5934case GGML_TYPE_IQ3_S : 5935case GGML_TYPE_IQ2_S : 5936case GGML_TYPE_Q8_K : 5937case GGML_TYPE_I8 : 5938case GGML_TYPE_I16 : 5939case GGML_TYPE_I32 : 5940case GGML_TYPE_I64 : 5941case GGML_TYPE_F64 : 5942case GGML_TYPE_COUNT : 5943 { 5944GGML_ABORT ("fatal error" ); 5945 } 5946 } 5947} 5948 5949// ggml_compute_forward_rope 5950 5951static float rope_yarn_ramp (const float low ,const float high ,const int i0 ) { 5952const float y = (i0 /2 - low ) /MAX (0.001f ,high - low ); 5953return 1 - MIN (1 ,MAX (0 ,y )); 5954} 5955 5956// YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn 5957// MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng. 5958static void rope_yarn ( 5959float theta_extrap ,float freq_scale ,float corr_dims [2 ],int64_t i0 ,float ext_factor ,float mscale , 5960float * cos_theta ,float * sin_theta ) { 5961// Get n-d rotational scaling corrected for extrapolation 5962float theta_interp = freq_scale * theta_extrap ; 5963float theta = theta_interp ; 5964if (ext_factor != 0.0f ) { 5965float ramp_mix = rope_yarn_ramp (corr_dims [0 ],corr_dims [1 ],i0 )* ext_factor ; 5966theta = theta_interp * (1 - ramp_mix )+ theta_extrap * ramp_mix ; 5967 5968// Get n-d magnitude scaling corrected for interpolation 5969mscale *=1.0f + 0.1f * logf (1.0f /freq_scale ); 5970 } 5971* cos_theta = cosf (theta )* mscale ; 5972* sin_theta = sinf (theta )* mscale ; 5973} 5974 5975static void ggml_rope_cache_init ( 5976float theta_base ,float freq_scale ,const float * freq_factors ,float corr_dims [2 ],int64_t ne0 ,float ext_factor ,float mscale , 5977float * cache ,float sin_sign ,float theta_scale ) { 5978// ref: https://github.com/jquesnelle/yarn/blob/master/scaled_rope/LlamaYaRNScaledRotaryEmbedding.py 5979float theta = theta_base ; 5980for (int64_t i0 = 0 ;i0 < ne0 ;i0 += 2 ) { 5981const float ff = freq_factors ?freq_factors [i0 /2 ] :1.0f ; 5982rope_yarn ( 5983theta /ff ,freq_scale ,corr_dims ,i0 ,ext_factor ,mscale ,& cache [i0 + 0 ],& cache [i0 + 1 ] 5984 ); 5985cache [i0 + 1 ] *=sin_sign ; 5986 5987theta *=theta_scale ; 5988 } 5989} 5990 5991static void ggml_mrope_cache_init ( 5992float theta_base_t ,float theta_base_h ,float theta_base_w ,float theta_base_e ,int sections [4 ],bool is_imrope ,bool indep_sects , 5993float freq_scale ,const float * freq_factors ,float corr_dims [2 ],int64_t ne0 ,float ext_factor ,float mscale , 5994float * cache ,float sin_sign ,float theta_scale ) { 5995// ref: https://github.com/jquesnelle/yarn/blob/master/scaled_rope/LlamaYaRNScaledRotaryEmbedding.py 5996float theta_t = theta_base_t ; 5997float theta_h = theta_base_h ; 5998float theta_w = theta_base_w ; 5999float theta_e = theta_base_e ;// extra position id for vision encoder 6000int sect_dims = sections [0 ]+ sections [1 ]+ sections [2 ]+ sections [3 ]; 6001int sec_w = sections [1 ]+ sections [0 ]; 6002int sec_e = sections [2 ]+ sec_w ; 6003GGML_ASSERT (sect_dims <=ne0 ); 6004 6005for (int64_t i0 = 0 ;i0 < ne0 ;i0 += 2 ) { 6006const float ff = freq_factors ?freq_factors [i0 /2 ] :1.0f ; 6007 6008int sector = (i0 /2 ) %sect_dims ; 6009if (indep_sects ) { 6010// compute theta independently for each dim sections 6011// (i.e. reset corresponding theta when `i0` go from one section to another) 6012if (sector == 0 ) { 6013theta_t = theta_base_t ; 6014 } 6015else if (sector == sections [0 ]) { 6016theta_h = theta_base_h ;; 6017 } 6018else if (sector == sec_w ) { 6019theta_w = theta_base_w ; 6020 } 6021else if (sector == sec_e ) { 6022theta_e = theta_base_e ; 6023 } 6024 } 6025 6026float theta = theta_t ; 6027if (is_imrope ) {// qwen3vl apply interleaved mrope 6028if (sector %3 == 1 && sector < 3 * sections [1 ]) { 6029theta = theta_h ; 6030 }else if (sector %3 == 2 && sector < 3 * sections [2 ]) { 6031theta = theta_w ; 6032 }else if (sector %3 == 0 && sector < 3 * sections [0 ]) { 6033theta = theta_t ; 6034 }else { 6035theta = theta_e ; 6036 } 6037 }else { 6038if (sector >=sections [0 ]&& sector < sec_w ) { 6039theta = theta_h ; 6040 } 6041else if (sector >=sec_w && sector < sec_w + sections [2 ]) { 6042theta = theta_w ; 6043 } 6044else if (sector >=sec_w + sections [2 ]) { 6045theta = theta_e ; 6046 } 6047 } 6048 6049rope_yarn ( 6050theta /ff ,freq_scale ,corr_dims ,i0 ,ext_factor ,mscale ,& cache [i0 + 0 ],& cache [i0 + 1 ] 6051 ); 6052cache [i0 + 1 ] *=sin_sign ; 6053 6054theta_t *=theta_scale ; 6055theta_w *=theta_scale ; 6056theta_h *=theta_scale ; 6057theta_e *=theta_scale ; 6058 } 6059} 6060 6061 6062template < typename T > 6063static 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 ) { 6064for (int64_t i0 = 0 ;i0 < n ;i0 += 2 ) { 6065const int64_t ic = i0 /scale ;// hack for GGML_ROPE_TYPE_NORMAL, where we need ic = i0; for all other cases, ic = i0/2 6066 6067const float cos_theta = cache [i0 + 0 ]; 6068const float sin_theta = cache [i0 + 1 ]; 6069 6070const T * const src = src_data + ic ; 6071T * dst = dst_data + ic ; 6072 6073const float x0 = type_conversion_table < T > ::to_f32 (src [0 ]); 6074const float x1 = type_conversion_table < T > ::to_f32 (src [n_offset ]); 6075 6076dst [0 ]= type_conversion_table < T > ::from_f32 (x0 * cos_theta - x1 * sin_theta ); 6077dst [n_offset ]= type_conversion_table < T > ::from_f32 (x0 * sin_theta + x1 * cos_theta ); 6078 } 6079} 6080 6081template < typename T > //float or ggml_fp16_t 6082static void ggml_compute_forward_rope_flt ( 6083const ggml_compute_params * params , 6084ggml_tensor * dst , 6085const bool forward ) { 6086 6087const ggml_tensor * src0 = dst -> src [0 ]; 6088const ggml_tensor * src1 = dst -> src [1 ]; 6089const ggml_tensor * src2 = dst -> src [2 ]; 6090 6091GGML_ASSERT (src0 -> type == GGML_TYPE_F32 || src0 -> type == GGML_TYPE_F16 ); 6092GGML_ASSERT (src1 -> type == GGML_TYPE_I32 ); 6093 6094float freq_base ,freq_scale ,ext_factor ,attn_factor ,beta_fast ,beta_slow ; 6095int sections [4 ]; 6096 6097//const int n_past = ((int32_t *) dst->op_params)[0]; 6098const int n_dims = ((int32_t * )dst -> op_params )[1 ]; 6099const int mode = ((int32_t * )dst -> op_params )[2 ]; 6100//const int n_ctx = ((int32_t *) dst->op_params)[3]; 6101const int n_ctx_orig = ((int32_t * )dst -> op_params )[4 ]; 6102 6103memcpy (& freq_base , (int32_t * )dst -> op_params + 5 ,sizeof (float )); 6104memcpy (& freq_scale , (int32_t * )dst -> op_params + 6 ,sizeof (float )); 6105memcpy (& ext_factor , (int32_t * )dst -> op_params + 7 ,sizeof (float )); 6106memcpy (& attn_factor , (int32_t * )dst -> op_params + 8 ,sizeof (float )); 6107memcpy (& beta_fast , (int32_t * )dst -> op_params + 9 ,sizeof (float )); 6108memcpy (& beta_slow , (int32_t * )dst -> op_params + 10 ,sizeof (float )); 6109memcpy (& sections , (int32_t * )dst -> op_params + 11 ,sizeof (int )* 4 ); 6110 6111const int n_offs = ((int32_t * )dst -> op_params )[15 ]; 6112 6113GGML_TENSOR_UNARY_OP_LOCALS 6114 6115//printf("ne0: %d, ne1: %d, ne2: %d, ne3: %d\n", ne0, ne1, ne2, ne3); 6116//printf("n_past = %d, ne2 = %d\n", n_past, ne2); 6117 6118GGML_ASSERT (nb0 == nb00 ); 6119GGML_ASSERT (nb0 == sizeof (T )); 6120 6121const int ith = params -> ith ; 6122const int nth = params -> nth ; 6123 6124const int nr = ggml_nrows (dst ); 6125 6126GGML_ASSERT (n_dims <=ne0 ); 6127GGML_ASSERT (n_dims %2 == 0 ); 6128 6129GGML_ASSERT (n_offs >=0 ); 6130GGML_ASSERT (n_offs %2 == 0 ); 6131GGML_ASSERT (n_offs + n_dims <=ne0 ); 6132 6133// rows per thread 6134const int dr = (nr + nth - 1 )/nth ; 6135 6136// row range for this thread 6137const int ir0 = dr * ith ; 6138const int ir1 = MIN (ir0 + dr ,nr ); 6139 6140// row index used to determine which thread to use 6141int ir = 0 ; 6142 6143const float theta_scale = powf (freq_base ,-2.0f /n_dims ); 6144 6145float corr_dims [2 ]; 6146ggml_rope_yarn_corr_dims (n_dims ,n_ctx_orig ,freq_base ,beta_fast ,beta_slow ,corr_dims ); 6147 6148const bool is_imrope = mode == GGML_ROPE_TYPE_IMROPE ;// qwen3vl apply interleaved mrope 6149const bool mrope_used = mode & GGML_ROPE_TYPE_MROPE ;// ggml_rope_multi, note: also true for vision (24 & 8 == true) and for imrope 6150const bool is_vision = mode == GGML_ROPE_TYPE_VISION ; 6151 6152if (mrope_used ) { 6153GGML_ASSERT (sections [0 ]> 0 || sections [1 ]> 0 || sections [2 ]> 0 ); 6154 } 6155 6156if (is_vision ) { 6157GGML_ASSERT (n_dims == ne0 /2 ); 6158GGML_ASSERT (n_offs == 0 ); 6159 } 6160 6161const float * freq_factors = NULL ; 6162if (src2 != NULL ) { 6163GGML_ASSERT (src2 -> type == GGML_TYPE_F32 ); 6164GGML_ASSERT (src2 -> ne [0 ] >=n_dims /2 ); 6165freq_factors = (const float * )src2 -> data ; 6166 } 6167 6168// backward process uses inverse rotation by cos and sin. 6169// cos and sin build a rotation matrix, where the inverse is the transpose. 6170// this essentially just switches the sign of sin. 6171const float sin_sign = forward ?1.0f :-1.0f ; 6172 6173const int32_t * pos = (const int32_t * )src1 -> data ; 6174 6175int64_t last_i2 = -1 ; 6176 6177for (int64_t i3 = 0 ;i3 < ne3 ;i3 ++ ) {// batch 6178for (int64_t i2 = 0 ;i2 < ne2 ;i2 ++ ) {// seq-len 6179for (int64_t i1 = 0 ;i1 < ne1 ;i1 ++ ) {// attn-heads 6180if (ir ++ < ir0 )continue ;// skip rows mapped to other threads 6181if (ir > ir1 )break ; 6182 6183float * cache = (float * )params -> wdata + (ne0 + CACHE_LINE_SIZE_F32 )* ith ; 6184if (last_i2 != i2 ) { 6185if (!mrope_used ) { 6186const int64_t p = pos [i2 ]; 6187ggml_rope_cache_init (p ,freq_scale ,freq_factors ,corr_dims ,ne0 ,ext_factor ,attn_factor ,cache ,sin_sign ,theta_scale ); 6188 } 6189else { 6190const int64_t p_t = pos [i2 ]; 6191const int64_t p_h = pos [i2 + ne2 ]; 6192const int64_t p_w = pos [i2 + ne2 * 2 ]; 6193const int64_t p_e = pos [i2 + ne2 * 3 ]; 6194ggml_mrope_cache_init ( 6195p_t ,p_h ,p_w ,p_e ,sections ,is_imrope ,is_vision , 6196freq_scale ,freq_factors ,corr_dims ,ne0 ,ext_factor ,attn_factor ,cache ,sin_sign ,theta_scale ); 6197 } 6198 6199last_i2 = i2 ; 6200 } 6201 6202T * src = (T * )((char * )src0 -> data + i3 * nb03 + i2 * nb02 + i1 * nb01 ); 6203T * dst_data = (T * )((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * nb1 ); 6204 6205switch (mode ) { 6206case GGML_ROPE_TYPE_NORMAL : 6207rotate_pairs < T > (n_dims ,1 ,cache ,src + n_offs ,dst_data + n_offs ,1 ); 6208break ; 6209case GGML_ROPE_TYPE_NEOX : 6210case GGML_ROPE_TYPE_MROPE : 6211case GGML_ROPE_TYPE_IMROPE : 6212rotate_pairs < T > (n_dims ,n_dims /2 ,cache ,src + n_offs ,dst_data + n_offs ); 6213break ; 6214case GGML_ROPE_TYPE_VISION : 6215rotate_pairs < T > (ne0 ,n_dims ,cache ,src ,dst_data ); 6216break ; 6217default : 6218GGML_ABORT ("rope type not supported" ); 6219 } 6220 6221if (!is_vision ) { 6222// fill the remain channels with data from src tensor 6223for (int64_t i0 = 0 ;i0 < ne0 ;i0 += 2 ) { 6224if (i0 == n_offs ) { 6225i0 += n_dims - 2 ;// skip the rotated channels 6226continue ; 6227 } 6228const T * const src = (T * )((char * )src0 -> data + i3 * nb03 + i2 * nb02 + i1 * nb01 + i0 * nb00 ); 6229T * dst_data = (T * )((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * nb1 + i0 * nb0 ); 6230 6231dst_data [0 ]= src [0 ]; 6232dst_data [1 ]= src [1 ]; 6233 } 6234 } 6235 }//attn-heads 6236 } 6237 } 6238} 6239 6240void ggml_compute_forward_rope ( 6241const ggml_compute_params * params , 6242ggml_tensor * dst ) { 6243 6244const ggml_tensor * src0 = dst -> src [0 ]; 6245 6246switch (src0 -> type ) { 6247case GGML_TYPE_F16 : 6248 { 6249ggml_compute_forward_rope_flt < ggml_fp16_t > (params ,dst , true); 6250 }break ; 6251case GGML_TYPE_F32 : 6252 { 6253ggml_compute_forward_rope_flt < float > (params ,dst , true); 6254 }break ; 6255default : 6256 { 6257GGML_ABORT ("fatal error" ); 6258 } 6259 } 6260} 6261 6262// ggml_compute_forward_rope_back 6263 6264void ggml_compute_forward_rope_back ( 6265const ggml_compute_params * params , 6266ggml_tensor * dst ) { 6267 6268const ggml_tensor * src0 = dst -> src [0 ]; 6269 6270switch (src0 -> type ) { 6271case GGML_TYPE_F16 : 6272 { 6273ggml_compute_forward_rope_flt < ggml_fp16_t > (params ,dst , false); 6274 }break ; 6275case GGML_TYPE_F32 : 6276 { 6277ggml_compute_forward_rope_flt < float > (params ,dst , false); 6278 }break ; 6279default : 6280 { 6281GGML_ABORT ("fatal error" ); 6282 } 6283 } 6284} 6285 6286// ggml_compute_forward_conv_transpose_1d 6287 6288static void ggml_compute_forward_conv_transpose_1d_f16_f32 ( 6289const ggml_compute_params * params , 6290ggml_tensor * dst ) { 6291 6292const ggml_tensor * src0 = dst -> src [0 ]; 6293const ggml_tensor * src1 = dst -> src [1 ]; 6294 6295GGML_ASSERT (src0 -> type == GGML_TYPE_F16 ); 6296GGML_ASSERT (src1 -> type == GGML_TYPE_F32 ); 6297GGML_ASSERT (dst -> type == GGML_TYPE_F32 ); 6298 6299GGML_TENSOR_BINARY_OP_LOCALS 6300 6301const int ith = params -> ith ; 6302const int nth = params -> nth ; 6303 6304const int nk = ne00 * ne01 * ne02 ; 6305 6306GGML_ASSERT (nb00 == sizeof (ggml_fp16_t )); 6307GGML_ASSERT (nb10 == sizeof (float )); 6308 6309if (ith == 0 ) { 6310memset (params -> wdata ,0 ,params -> wsize ); 6311 6312// permute kernel data (src0) from (K x Cout x Cin) to (Cin x K x Cout) 6313 { 6314ggml_fp16_t * const wdata = (ggml_fp16_t * )params -> wdata + 0 ; 6315 6316for (int64_t i02 = 0 ;i02 < ne02 ;i02 ++ ) { 6317for (int64_t i01 = 0 ;i01 < ne01 ;i01 ++ ) { 6318const ggml_fp16_t * const src = (ggml_fp16_t * )((char * )src0 -> data + i02 * nb02 + i01 * nb01 ); 6319ggml_fp16_t * dst_data = wdata + i01 * ne00 * ne02 ; 6320for (int64_t i00 = 0 ;i00 < ne00 ;i00 ++ ) { 6321dst_data [i00 * ne02 + i02 ]= src [i00 ]; 6322 } 6323 } 6324 } 6325 } 6326 6327// permute source data (src1) from (L x Cin) to (Cin x L) 6328 { 6329ggml_fp16_t * const wdata = (ggml_fp16_t * )params -> wdata + nk ; 6330ggml_fp16_t * dst_data = wdata ; 6331 6332for (int64_t i11 = 0 ;i11 < ne11 ;i11 ++ ) { 6333const float * const src = (float * )((char * )src1 -> data + i11 * nb11 ); 6334for (int64_t i10 = 0 ;i10 < ne10 ;i10 ++ ) { 6335dst_data [i10 * ne11 + i11 ]= GGML_CPU_FP32_TO_FP16 (src [i10 ]); 6336 } 6337 } 6338 } 6339 6340// need to zero dst since we are accumulating into it 6341memset (dst -> data ,0 ,ggml_nbytes (dst )); 6342 } 6343ggml_barrier (params -> threadpool ); 6344 6345const int32_t s0 = ((const int32_t * )(dst -> op_params ))[0 ]; 6346 6347// total rows in dst 6348const int nr = ne1 ; 6349 6350// rows per thread 6351const int dr = (nr + nth - 1 )/nth ; 6352 6353// row range for this thread 6354const int ir0 = dr * ith ; 6355const int ir1 = MIN (ir0 + dr ,nr ); 6356 6357ggml_fp16_t * const wdata = (ggml_fp16_t * )params -> wdata + 0 ; 6358ggml_fp16_t * const wdata_src = wdata + nk ; 6359 6360for (int i1 = ir0 ;i1 < ir1 ;i1 ++ ) { 6361float * dst_data = (float * )((char * )dst -> data + i1 * nb1 ); 6362ggml_fp16_t * wdata_kernel = wdata + i1 * ne02 * ne00 ; 6363for (int i10 = 0 ;i10 < ne10 ;i10 ++ ) { 6364const int i1n = i10 * ne11 ; 6365for (int i00 = 0 ;i00 < ne00 ;i00 ++ ) { 6366float v = 0 ; 6367ggml_vec_dot_f16 (ne02 ,& v ,0 , 6368 (ggml_fp16_t * )wdata_src + i1n ,0 , 6369 (ggml_fp16_t * )wdata_kernel + i00 * ne02 ,0 ,1 ); 6370dst_data [i10 * s0 + i00 ]+= v ; 6371 } 6372 } 6373 } 6374} 6375 6376static void ggml_compute_forward_conv_transpose_1d_f32 ( 6377const ggml_compute_params * params , 6378ggml_tensor * dst ) { 6379 6380const ggml_tensor * src0 = dst -> src [0 ]; 6381const ggml_tensor * src1 = dst -> src [1 ]; 6382 6383GGML_ASSERT (src0 -> type == GGML_TYPE_F32 ); 6384GGML_ASSERT (src1 -> type == GGML_TYPE_F32 ); 6385GGML_ASSERT (dst -> type == GGML_TYPE_F32 ); 6386 6387GGML_TENSOR_BINARY_OP_LOCALS 6388 6389const int ith = params -> ith ; 6390const int nth = params -> nth ; 6391 6392const int nk = ne00 * ne01 * ne02 ; 6393 6394GGML_ASSERT (nb00 == sizeof (float )); 6395GGML_ASSERT (nb10 == sizeof (float )); 6396 6397if (ith == 0 ) { 6398memset (params -> wdata ,0 ,params -> wsize ); 6399 6400// prepare kernel data (src0) from (K x Cout x Cin) to (Cin x K x Cout) 6401 { 6402float * const wdata = (float * )params -> wdata + 0 ; 6403 6404for (int64_t i02 = 0 ;i02 < ne02 ;i02 ++ ) { 6405for (int64_t i01 = 0 ;i01 < ne01 ;i01 ++ ) { 6406const float * const src = (float * )((char * )src0 -> data + i02 * nb02 + i01 * nb01 ); 6407float * dst_data = wdata + i01 * ne00 * ne02 ; 6408for (int64_t i00 = 0 ;i00 < ne00 ;i00 ++ ) { 6409dst_data [i00 * ne02 + i02 ]= src [i00 ]; 6410 } 6411 } 6412 } 6413 } 6414 6415// prepare source data (src1) 6416 { 6417float * const wdata = (float * )params -> wdata + nk ; 6418float * dst_data = wdata ; 6419 6420for (int64_t i11 = 0 ;i11 < ne11 ;i11 ++ ) { 6421const float * const src = (float * )((char * )src1 -> data + i11 * nb11 ); 6422for (int64_t i10 = 0 ;i10 < ne10 ;i10 ++ ) { 6423dst_data [i10 * ne11 + i11 ]= src [i10 ]; 6424 } 6425 } 6426 } 6427 6428// need to zero dst since we are accumulating into it 6429memset (dst -> data ,0 ,ggml_nbytes (dst )); 6430 } 6431ggml_barrier (params -> threadpool ); 6432 6433const int32_t s0 = ((const int32_t * )(dst -> op_params ))[0 ]; 6434 6435// total rows in dst 6436const int nr = ne1 ; 6437 6438// rows per thread 6439const int dr = (nr + nth - 1 )/nth ; 6440 6441// row range for this thread 6442const int ir0 = dr * ith ; 6443const int ir1 = MIN (ir0 + dr ,nr ); 6444 6445float * const wdata = (float * )params -> wdata + 0 ; 6446float * const wdata_src = wdata + nk ; 6447 6448for (int i1 = ir0 ;i1 < ir1 ;i1 ++ ) { 6449float * dst_data = (float * )((char * )dst -> data + i1 * nb1 ); 6450float * wdata_kernel = wdata + i1 * ne02 * ne00 ; 6451for (int i10 = 0 ;i10 < ne10 ;i10 ++ ) { 6452const int i1n = i10 * ne11 ; 6453for (int i00 = 0 ;i00 < ne00 ;i00 ++ ) { 6454float v = 0 ; 6455ggml_vec_dot_f32 (ne02 ,& v ,0 , 6456wdata_src + i1n ,0 , 6457wdata_kernel + i00 * ne02 ,0 ,1 ); 6458dst_data [i10 * s0 + i00 ]+= v ; 6459 } 6460 } 6461 } 6462} 6463 6464void ggml_compute_forward_conv_transpose_1d ( 6465const ggml_compute_params * params , 6466ggml_tensor * dst ) { 6467 6468const ggml_tensor * src0 = dst -> src [0 ]; 6469 6470switch (src0 -> type ) { 6471case GGML_TYPE_F16 : 6472 { 6473ggml_compute_forward_conv_transpose_1d_f16_f32 (params ,dst ); 6474 }break ; 6475case GGML_TYPE_F32 : 6476 { 6477ggml_compute_forward_conv_transpose_1d_f32 (params ,dst ); 6478 }break ; 6479default : 6480 { 6481GGML_ABORT ("fatal error" ); 6482 } 6483 } 6484} 6485 6486// ggml_compute_forward_im2col_f32 6487// src0: kernel [OC, IC, KH, KW] 6488// src1: image [N, IC, IH, IW] 6489// dst: result [N, OH, OW, IC*KH*KW] 6490static void ggml_compute_forward_im2col_f32 ( 6491const ggml_compute_params * params , 6492ggml_tensor * dst ) { 6493 6494const ggml_tensor * src0 = dst -> src [0 ]; 6495const ggml_tensor * src1 = dst -> src [1 ]; 6496 6497GGML_ASSERT (src1 -> type == GGML_TYPE_F32 ); 6498GGML_ASSERT (dst -> type == GGML_TYPE_F32 ); 6499 6500GGML_TENSOR_BINARY_OP_LOCALS ; 6501 6502const int32_t s0 = ((const int32_t * )(dst -> op_params ))[0 ]; 6503const int32_t s1 = ((const int32_t * )(dst -> op_params ))[1 ]; 6504const int32_t p0 = ((const int32_t * )(dst -> op_params ))[2 ]; 6505const int32_t p1 = ((const int32_t * )(dst -> op_params ))[3 ]; 6506const int32_t d0 = ((const int32_t * )(dst -> op_params ))[4 ]; 6507const int32_t d1 = ((const int32_t * )(dst -> op_params ))[5 ]; 6508const bool is_2D = ((const int32_t * )(dst -> op_params ))[6 ]== 1 ; 6509 6510const int ith = params -> ith ; 6511const int nth = params -> nth ; 6512 6513const int64_t N = is_2D ?ne13 :ne12 ; 6514const int64_t IC = is_2D ?ne12 :ne11 ; 6515const int64_t IH = is_2D ?ne11 :1 ; 6516const int64_t IW = ne10 ; 6517 6518const int64_t KH = is_2D ?ne01 :1 ; 6519const int64_t KW = ne00 ; 6520 6521const int64_t OH = is_2D ?ne2 :1 ; 6522const int64_t OW = ne1 ; 6523 6524int ofs0 = is_2D ?nb13 :nb12 ; 6525int ofs1 = is_2D ?nb12 :nb11 ; 6526 6527GGML_ASSERT (nb10 == sizeof (float )); 6528 6529// im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW] 6530 { 6531float * const wdata = (float * )dst -> data ; 6532 6533for (int64_t in = 0 ;in < N ;in ++ ) { 6534for (int64_t ioh = 0 ;ioh < OH ;ioh ++ ) {// 1 6535for (int64_t iow = 0 ;iow < OW ;iow ++ ) { 6536for (int64_t iic = ith ;iic < IC ;iic += nth ) { 6537 6538// micro kernel 6539float * dst_data = wdata + (in * OH * OW + ioh * OW + iow )* (IC * KH * KW );// [IC, KH, KW] 6540const float * const src_data = (float * )((char * )src1 -> data + in * ofs0 + iic * ofs1 );// [IH, IW] 6541 6542for (int64_t ikh = 0 ;ikh < KH ;ikh ++ ) {// 1 6543for (int64_t ikw = 0 ;ikw < KW ;ikw ++ ) { 6544const int64_t iiw = iow * s0 + ikw * d0 - p0 ; 6545const int64_t iih = ioh * s1 + ikh * d1 - p1 ; 6546 6547if (iih < 0 || iih >=IH || iiw < 0 || iiw >=IW ) { 6548dst_data [iic * (KH * KW )+ ikh * KW + ikw ]= 0 ; 6549 }else { 6550dst_data [iic * (KH * KW )+ ikh * KW + ikw ]= (src_data [iih * IW + iiw ]); 6551 } 6552 } 6553 } 6554 } 6555 } 6556 } 6557 } 6558 } 6559} 6560 6561 6562// ggml_compute_forward_im2col_f16 6563// src0: kernel [OC, IC, KH, KW] 6564// src1: image [N, IC, IH, IW] 6565// dst: result [N, OH, OW, IC*KH*KW] 6566static void ggml_compute_forward_im2col_f16 ( 6567const ggml_compute_params * params , 6568ggml_tensor * dst ) { 6569 6570const ggml_tensor * src0 = dst -> src [0 ]; 6571const ggml_tensor * src1 = dst -> src [1 ]; 6572 6573GGML_ASSERT (src1 -> type == GGML_TYPE_F16 || src1 -> type == GGML_TYPE_F32 ); 6574GGML_ASSERT (dst -> type == GGML_TYPE_F16 ); 6575 6576GGML_TENSOR_BINARY_OP_LOCALS ; 6577 6578const int32_t s0 = ((const int32_t * )(dst -> op_params ))[0 ]; 6579const int32_t s1 = ((const int32_t * )(dst -> op_params ))[1 ]; 6580const int32_t p0 = ((const int32_t * )(dst -> op_params ))[2 ]; 6581const int32_t p1 = ((const int32_t * )(dst -> op_params ))[3 ]; 6582const int32_t d0 = ((const int32_t * )(dst -> op_params ))[4 ]; 6583const int32_t d1 = ((const int32_t * )(dst -> op_params ))[5 ]; 6584const bool is_2D = ((const int32_t * )(dst -> op_params ))[6 ]== 1 ; 6585 6586const int ith = params -> ith ; 6587const int nth = params -> nth ; 6588 6589const int64_t N = is_2D ?ne13 :ne12 ; 6590const int64_t IC = is_2D ?ne12 :ne11 ; 6591const int64_t IH = is_2D ?ne11 :1 ; 6592const int64_t IW = ne10 ; 6593 6594const int64_t KH = is_2D ?ne01 :1 ; 6595const int64_t KW = ne00 ; 6596 6597const int64_t OH = is_2D ?ne2 :1 ; 6598const int64_t OW = ne1 ; 6599 6600int ofs0 = is_2D ?nb13 :nb12 ; 6601int ofs1 = is_2D ?nb12 :nb11 ; 6602 6603GGML_ASSERT (nb10 == ggml_type_size (src1 -> type )); 6604 6605// im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW] 6606 { 6607ggml_fp16_t * const wdata = (ggml_fp16_t * )dst -> data ; 6608 6609for (int64_t in = 0 ;in < N ;in ++ ) { 6610for (int64_t ioh = 0 ;ioh < OH ;ioh ++ ) {// 1 6611for (int64_t iow = 0 ;iow < OW ;iow ++ ) { 6612for (int64_t iic = ith ;iic < IC ;iic += nth ) { 6613 6614// micro kernel 6615ggml_fp16_t * dst_data = wdata + (in * OH * OW + ioh * OW + iow )* (IC * KH * KW );// [IC, KH, KW] 6616const float * const src_data_f32 = src1 -> type == GGML_TYPE_F32 6617 ? (const float * )((const char * )src1 -> data + in * ofs0 + iic * ofs1 ) 6618 :nullptr ;// [IH, IW] 6619const ggml_fp16_t * const src_data_f16 = src1 -> type == GGML_TYPE_F16 6620 ? (const ggml_fp16_t * )((const char * )src1 -> data + in * ofs0 + iic * ofs1 ) 6621 :nullptr ;// [IH, IW] 6622 6623for (int64_t ikh = 0 ;ikh < KH ;ikh ++ ) {// 1 6624for (int64_t ikw = 0 ;ikw < KW ;ikw ++ ) { 6625const int64_t iiw = iow * s0 + ikw * d0 - p0 ; 6626const int64_t iih = ioh * s1 + ikh * d1 - p1 ; 6627 6628if (iih < 0 || iih >=IH || iiw < 0 || iiw >=IW ) { 6629dst_data [iic * (KH * KW )+ ikh * KW + ikw ]= 0 ; 6630 }else { 6631if (src_data_f32 != nullptr ) { 6632dst_data [iic * (KH * KW )+ ikh * KW + ikw ]= GGML_CPU_FP32_TO_FP16 (src_data_f32 [iih * IW + iiw ]); 6633 }else { 6634dst_data [iic * (KH * KW )+ ikh * KW + ikw ]= src_data_f16 [iih * IW + iiw ]; 6635 } 6636 } 6637 } 6638 } 6639 } 6640 } 6641 } 6642 } 6643 } 6644} 6645 6646void ggml_compute_forward_im2col ( 6647const ggml_compute_params * params , 6648ggml_tensor * dst ) { 6649switch (dst -> type ) { 6650case GGML_TYPE_F16 : 6651 { 6652ggml_compute_forward_im2col_f16 (params ,dst ); 6653 }break ; 6654case GGML_TYPE_F32 : 6655 { 6656ggml_compute_forward_im2col_f32 (params ,dst ); 6657 }break ; 6658default : 6659 { 6660GGML_ABORT ("fatal error" ); 6661 } 6662 } 6663} 6664 6665// ggml_compute_forward_im2col_back_f32 6666 6667void ggml_compute_forward_im2col_back_f32 ( 6668const ggml_compute_params * params , 6669ggml_tensor * dst ) { 6670 6671const ggml_tensor * src0 = dst -> src [0 ];// gradients of forward pass output 6672const ggml_tensor * src1 = dst -> src [1 ];// convolution kernel 6673 6674GGML_ASSERT (src0 -> type == GGML_TYPE_F32 ); 6675GGML_ASSERT (src1 -> type == GGML_TYPE_F32 || src1 -> type == GGML_TYPE_F16 ); 6676GGML_ASSERT (dst -> type == GGML_TYPE_F32 ); 6677 6678GGML_TENSOR_BINARY_OP_LOCALS ; 6679 6680const int32_t s0 = ((const int32_t * )(dst -> op_params ))[0 ]; 6681const int32_t s1 = ((const int32_t * )(dst -> op_params ))[1 ]; 6682const int32_t p0 = ((const int32_t * )(dst -> op_params ))[2 ]; 6683const int32_t p1 = ((const int32_t * )(dst -> op_params ))[3 ]; 6684const int32_t d0 = ((const int32_t * )(dst -> op_params ))[4 ]; 6685const int32_t d1 = ((const int32_t * )(dst -> op_params ))[5 ]; 6686const bool is_2D = ((const int32_t * )(dst -> op_params ))[6 ]== 1 ; 6687 6688const int ith = params -> ith ; 6689const int nth = params -> nth ; 6690 6691const int64_t N = is_2D ?ne3 :ne2 ; 6692const int64_t IC = is_2D ?ne2 :ne1 ; 6693const int64_t IH = is_2D ?ne1 :1 ; 6694const int64_t IW = ne0 ; 6695 6696const int64_t KH = is_2D ?ne11 :1 ; 6697const int64_t KW = ne10 ; 6698 6699const int64_t OH = is_2D ?ne02 :1 ; 6700const int64_t OW = ne01 ; 6701 6702int ofs0 = is_2D ?nb3 :nb2 ; 6703int ofs1 = is_2D ?nb2 :nb1 ; 6704 6705GGML_ASSERT (nb0 == sizeof (float )); 6706 6707// im2col: [N, IC, IH, IW] => [N, OH, OW, IC*KH*KW] 6708 { 6709float * const wdata = (float * )dst -> data ; 6710 6711for (int64_t in = 0 ;in < N ;in ++ ) { 6712for (int64_t iic = ith ;iic < IC ;iic += nth ) { 6713for (int64_t iih = 0 ;iih < IH ;iih ++ ) { 6714for (int64_t iiw = 0 ;iiw < IW ;iiw ++ ) { 6715 6716// micro kernel 6717float grad = 0.0f ; 6718for (int64_t ikh = 0 ;ikh < KH ;ikh ++ ) { 6719for (int64_t ikw = 0 ;ikw < KW ;ikw ++ ) { 6720// For s0 > 1 some values were skipped over in the forward pass. 6721// These values have tmpw % s0 != 0 and need to be skipped in the backwards pass as well. 6722const int64_t tmpw = (iiw + p0 - ikw * d0 ); 6723if (tmpw %s0 != 0 ) { 6724continue ; 6725 } 6726const int64_t iow = tmpw /s0 ; 6727 6728// Equivalent logic as above except for s1. 6729int64_t ioh ; 6730if (is_2D ) { 6731const int64_t tmph = iih + p1 - ikh * d1 ; 6732 6733if (tmph %s1 != 0 ) { 6734continue ; 6735 } 6736 6737ioh = tmph /s1 ; 6738 }else { 6739ioh = 0 ; 6740 } 6741 6742if (iow < 0 || iow >=OW || ioh < 0 || ioh >=OH ) { 6743continue ; 6744 } 6745 6746const float * const grad_in = (const float * )src0 -> data 6747+ (in * OH * OW + ioh * OW + iow )* (IC * KH * KW );// [IC, KH, KW] 6748grad += grad_in [iic * (KH * KW )+ ikh * KW + ikw ]; 6749 } 6750 } 6751float * dst_data = (float * )((char * )wdata + (in * ofs0 + iic * ofs1 ));// [IH, IW] 6752dst_data [iih * IW + iiw ]= grad ; 6753 } 6754 } 6755 } 6756 } 6757 } 6758} 6759 6760 6761// ggml_compute_forward_im2col_3d_f16 6762// src0: kernel [OC*IC, KD, KH, KW] 6763// src1: image [N*IC, ID, IH, IW] 6764// dst: result [N*OD, OH, OW, IC * KD * KH * KW] 6765static void ggml_compute_forward_im2col_3d_f16 ( 6766const ggml_compute_params * params , 6767ggml_tensor * dst ) { 6768 6769const ggml_tensor * src0 = dst -> src [0 ]; 6770const ggml_tensor * src1 = dst -> src [1 ]; 6771 6772GGML_ASSERT (src1 -> type == GGML_TYPE_F32 ); 6773GGML_ASSERT (dst -> type == GGML_TYPE_F16 ); 6774 6775GGML_TENSOR_BINARY_OP_LOCALS ; 6776 6777const int32_t s0 = ((const int32_t * )(dst -> op_params ))[0 ]; 6778const int32_t s1 = ((const int32_t * )(dst -> op_params ))[1 ]; 6779const int32_t s2 = ((const int32_t * )(dst -> op_params ))[2 ]; 6780const int32_t p0 = ((const int32_t * )(dst -> op_params ))[3 ]; 6781const int32_t p1 = ((const int32_t * )(dst -> op_params ))[4 ]; 6782const int32_t p2 = ((const int32_t * )(dst -> op_params ))[5 ]; 6783const int32_t d0 = ((const int32_t * )(dst -> op_params ))[6 ]; 6784const int32_t d1 = ((const int32_t * )(dst -> op_params ))[7 ]; 6785const int32_t d2 = ((const int32_t * )(dst -> op_params ))[8 ]; 6786const int32_t IC = ((const int32_t * )(dst -> op_params ))[9 ]; 6787 6788 6789const int ith = params -> ith ; 6790const int nth = params -> nth ; 6791 6792const int64_t N = ne13 /IC ; 6793const int64_t ID = ne12 ; 6794const int64_t IH = ne11 ; 6795const int64_t IW = ne10 ; 6796 6797const int64_t OC = ne03 /IC ; 6798GGML_UNUSED (OC ); 6799const int64_t KD = ne02 ; 6800const int64_t KH = ne01 ; 6801const int64_t KW = ne00 ; 6802 6803const int64_t OD = ne3 /N ; 6804const int64_t OH = ne2 ; 6805const int64_t OW = ne1 ; 6806const int64_t OH_OW = OH * OW ; 6807const int64_t KD_KH_KW = KD * KH * KW ; 6808const int64_t KH_KW = KH * KW ; 6809const int64_t IC_KD_KH_KW = IC * KD * KH * KW ; 6810 6811GGML_ASSERT (nb10 == sizeof (float )); 6812 6813// im2col: [N*IC, ID, IH, IW] => [N*OD, OH, OW, IC * KD * KH * KW] 6814 { 6815ggml_fp16_t * const wdata = (ggml_fp16_t * )dst -> data ; 6816 6817for (int64_t in = 0 ;in < N ;in ++ ) { 6818for (int64_t iod = 0 ;iod < OD ;iod ++ ) { 6819for (int64_t ioh = 0 ;ioh < OH ;ioh ++ ) { 6820for (int64_t iow = 0 ;iow < OW ;iow ++ ) { 6821for (int64_t iic = ith ;iic < IC ;iic += nth ) { 6822 6823// micro kernel 6824ggml_fp16_t * dst_data = wdata + (in * OD * OH_OW + iod * OH_OW + ioh * OW + iow )* IC_KD_KH_KW ;// [IC, KD, KH, KW] 6825const float * const src_data = (const float * ) ((const char * )src1 -> data + (in * IC + iic )* nb13 );// [ID, IH, IW] 6826 6827for (int64_t ikd = 0 ;ikd < KD ;ikd ++ ) { 6828for (int64_t ikh = 0 ;ikh < KH ;ikh ++ ) { 6829for (int64_t ikw = 0 ;ikw < KW ;ikw ++ ) { 6830const int64_t iiw = iow * s0 + ikw * d0 - p0 ; 6831const int64_t iih = ioh * s1 + ikh * d1 - p1 ; 6832const int64_t iid = iod * s2 + ikd * d2 - p2 ; 6833 6834if (iid < 0 || iid >=ID || iih < 0 || iih >=IH || iiw < 0 || iiw >=IW ) { 6835dst_data [iic * KD_KH_KW + ikd * KH_KW + ikh * KW + ikw ]= 0 ; 6836 }else { 6837const float * const s = (const float * ) ((const char * )src_data + iid * nb12 + iih * nb11 + iiw * nb10 );// [ID, IH, IW] 6838dst_data [iic * KD_KH_KW + ikd * KH_KW + ikh * KW + ikw ]= GGML_CPU_FP32_TO_FP16 (* s ); 6839 } 6840 } 6841 } 6842 } 6843 } 6844 } 6845 } 6846 } 6847 } 6848 } 6849} 6850 6851// ggml_compute_forward_im2col_3d_f32 6852// src0: kernel [OC*IC, KD, KH, KW] 6853// src1: image [N*IC, ID, IH, IW] 6854// dst: result [N*OD, OH, OW, IC * KD * KH * KW] 6855static void ggml_compute_forward_im2col_3d_f32 ( 6856const ggml_compute_params * params , 6857ggml_tensor * dst ) { 6858 6859const ggml_tensor * src0 = dst -> src [0 ]; 6860const ggml_tensor * src1 = dst -> src [1 ]; 6861 6862GGML_ASSERT (src1 -> type == GGML_TYPE_F32 ); 6863GGML_ASSERT (dst -> type == GGML_TYPE_F32 ); 6864 6865GGML_TENSOR_BINARY_OP_LOCALS ; 6866 6867const int32_t s0 = ((const int32_t * )(dst -> op_params ))[0 ]; 6868const int32_t s1 = ((const int32_t * )(dst -> op_params ))[1 ]; 6869const int32_t s2 = ((const int32_t * )(dst -> op_params ))[2 ]; 6870const int32_t p0 = ((const int32_t * )(dst -> op_params ))[3 ]; 6871const int32_t p1 = ((const int32_t * )(dst -> op_params ))[4 ]; 6872const int32_t p2 = ((const int32_t * )(dst -> op_params ))[5 ]; 6873const int32_t d0 = ((const int32_t * )(dst -> op_params ))[6 ]; 6874const int32_t d1 = ((const int32_t * )(dst -> op_params ))[7 ]; 6875const int32_t d2 = ((const int32_t * )(dst -> op_params ))[8 ]; 6876const int32_t IC = ((const int32_t * )(dst -> op_params ))[9 ]; 6877 6878 6879const int ith = params -> ith ; 6880const int nth = params -> nth ; 6881 6882const int64_t N = ne13 /IC ; 6883const int64_t ID = ne12 ; 6884const int64_t IH = ne11 ; 6885const int64_t IW = ne10 ; 6886 6887const int64_t OC = ne03 /IC ; 6888GGML_UNUSED (OC ); 6889const int64_t KD = ne02 ; 6890const int64_t KH = ne01 ; 6891const int64_t KW = ne00 ; 6892 6893const int64_t OD = ne3 /N ; 6894const int64_t OH = ne2 ; 6895const int64_t OW = ne1 ; 6896 6897const int64_t OH_OW = OH * OW ; 6898const int64_t KD_KH_KW = KD * KH * KW ; 6899const int64_t KH_KW = KH * KW ; 6900const int64_t IC_KD_KH_KW = IC * KD * KH * KW ; 6901 6902GGML_ASSERT (nb10 == sizeof (float )); 6903 6904// im2col: [N*IC, ID, IH, IW] => [N*OD, OH, OW, IC * KD * KH * KW] 6905 { 6906float * const wdata = (float * )dst -> data ; 6907 6908for (int64_t in = 0 ;in < N ;in ++ ) { 6909for (int64_t iod = 0 ;iod < OD ;iod ++ ) { 6910for (int64_t ioh = 0 ;ioh < OH ;ioh ++ ) { 6911for (int64_t iow = 0 ;iow < OW ;iow ++ ) { 6912for (int64_t iic = ith ;iic < IC ;iic += nth ) { 6913 6914// micro kernel 6915float * dst_data = wdata + (in * OD * OH_OW + iod * OH_OW + ioh * OW + iow )* IC_KD_KH_KW ;// [IC, KD, KH, KW] 6916const float * const src_data = (const float * ) ((const char * )src1 -> data + (in * IC + iic )* nb13 );// [ID, IH, IW] 6917 6918for (int64_t ikd = 0 ;ikd < KD ;ikd ++ ) { 6919for (int64_t ikh = 0 ;ikh < KH ;ikh ++ ) { 6920for (int64_t ikw = 0 ;ikw < KW ;ikw ++ ) { 6921const int64_t iiw = iow * s0 + ikw * d0 - p0 ; 6922const int64_t iih = ioh * s1 + ikh * d1 - p1 ; 6923const int64_t iid = iod * s2 + ikd * d2 - p2 ; 6924 6925if (iid < 0 || iid >=ID || iih < 0 || iih >=IH || iiw < 0 || iiw >=IW || iid < 0 || iid >=ID ) { 6926dst_data [iic * KD_KH_KW + ikd * KH_KW + ikh * KW + ikw ]= 0 ; 6927 }else { 6928const float * const s = (const float * ) ((const char * )src_data + iid * nb12 + iih * nb11 + iiw * nb10 );// [ID, IH, IW] 6929dst_data [iic * KD_KH_KW + ikd * KH_KW + ikh * KW + ikw ]= * s ; 6930 } 6931 } 6932 } 6933 } 6934 } 6935 } 6936 } 6937 } 6938 } 6939 } 6940} 6941 6942 6943void ggml_compute_forward_im2col_3d ( 6944const ggml_compute_params * params , 6945ggml_tensor * dst ) { 6946switch (dst -> type ) { 6947case GGML_TYPE_F16 : 6948 { 6949ggml_compute_forward_im2col_3d_f16 (params ,dst ); 6950 }break ; 6951case GGML_TYPE_F32 : 6952 { 6953ggml_compute_forward_im2col_3d_f32 (params ,dst ); 6954 }break ; 6955default : 6956 { 6957GGML_ABORT ("fatal error" ); 6958 } 6959 } 6960} 6961 6962static void ggml_call_mul_mat (ggml_type type ,const ggml_compute_params * params ,int64_t m ,int64_t n ,int64_t k , 6963void * a ,void * b ,float * c ) { 6964const ggml_type_traits * traits = ggml_get_type_traits (type ); 6965struct ggml_tensor src1 = {}; 6966src1 .type = type ; 6967src1 .ne [0 ]= k ; 6968src1 .ne [1 ]= m ; 6969src1 .ne [2 ]= 1 ; 6970src1 .ne [3 ]= 1 ; 6971src1 .nb [0 ]= traits -> type_size ; 6972src1 .nb [1 ]= k * traits -> type_size ; 6973src1 .nb [2 ]= src1 .nb [1 ]; 6974src1 .nb [3 ]= src1 .nb [2 ]; 6975src1 .data = a ; 6976 6977struct ggml_tensor src0 = {}; 6978src0 .type = type ; 6979src0 .ne [0 ]= k ; 6980src0 .ne [1 ]= n ; 6981src0 .ne [2 ]= 1 ; 6982src0 .ne [3 ]= 1 ; 6983src0 .nb [0 ]= traits -> type_size ; 6984src0 .nb [1 ]= k * traits -> type_size ; 6985src0 .nb [2 ]= src0 .nb [1 ]; 6986src0 .nb [3 ]= src0 .nb [2 ]; 6987src0 .data = b ; 6988 6989struct ggml_tensor dst = {}; 6990dst .ne [0 ]= n ; 6991dst .ne [1 ]= m ; 6992dst .ne [2 ]= 1 ; 6993dst .ne [3 ]= 1 ; 6994dst .nb [0 ]= sizeof (float ); 6995dst .nb [1 ]= n * sizeof (float ); 6996dst .nb [2 ]= dst .nb [1 ]; 6997dst .nb [3 ]= dst .nb [2 ]; 6998dst .data = c ; 6999dst .src [0 ]= & src0 ; 7000dst .src [1 ]= & src1 ; 7001 7002ggml_compute_forward_mul_mat (params ,& dst ); 7003} 7004 7005static inline int64_t ggml_wrap_around (int64_t coord ,int64_t size ) { 7006return (coord + size ) %size ;// adding size avoids negative number weirdness 7007} 7008 7009// ggml_compute_forward_col2im_1d 7010// 7011// Scatter-add columns [K*OC, T_in] -> signal [T_out, OC] 7012// where T_out = (T_in - 1)*s + K - 2*p. Gather approach: each output reads ceil(K/s) inputs. 7013// Parallelized over the time axis so the split stays balanced whatever OC is. 7014// Supports F32, F16, BF16 input/output (same type), F32 accumulator. 7015 7016template < typename elem_t > 7017static void ggml_compute_forward_col2im_1d_impl ( 7018const ggml_compute_params * params , 7019ggml_tensor * dst ) { 7020 7021const ggml_tensor * src = dst -> src [0 ];// [K*OC, T_in] 7022 7023GGML_ASSERT (ggml_is_contiguous (src )); 7024GGML_ASSERT (ggml_is_contiguous (dst )); 7025 7026const int32_t s0 = ((const int32_t * )(dst -> op_params ))[0 ]; 7027const int32_t OC = ((const int32_t * )(dst -> op_params ))[1 ]; 7028const int32_t p0 = ((const int32_t * )(dst -> op_params ))[2 ]; 7029 7030const int64_t K_OC = src -> ne [0 ]; 7031const int64_t T_in = src -> ne [1 ]; 7032const int64_t K = K_OC /OC ; 7033const int64_t T_out = dst -> ne [0 ]; 7034 7035const elem_t * col_data = (const elem_t * )src -> data ; 7036elem_t * dst_data = (elem_t * )dst -> data ; 7037 7038const int ith = params -> ith ; 7039const int nth = params -> nth ; 7040 7041// Parallelize over the time axis: the split stays balanced whatever OC is, 7042// down to OC = 1 for mono audio, and threads read disjoint column bands 7043const int64_t dr = (T_out + nth - 1 ) /nth ; 7044const int64_t it0 = dr * ith ; 7045const int64_t it1 = it0 + dr < T_out ?it0 + dr :T_out ; 7046 7047for (int64_t oc = 0 ;oc < OC ;oc ++ ) { 7048for (int64_t t_out = it0 ;t_out < it1 ;t_out ++ ) { 7049const int64_t t_abs = t_out + p0 ;// absolute position in uncropped signal 7050// Gather: find all (t_in, k) where t_in * s + k == t_abs, 0 <= k < K 7051int64_t t_in_min = (t_abs - K + 1 + s0 - 1 ) /s0 ;// ceil((t_abs-K+1)/s) 7052if (t_in_min < 0 )t_in_min = 0 ; 7053int64_t t_in_max = t_abs /s0 ; 7054if (t_in_max >=T_in )t_in_max = T_in - 1 ; 7055 7056float sum = 0.0f ; 7057for (int64_t t_in = t_in_min ;t_in <=t_in_max ;t_in ++ ) { 7058int64_t k = t_abs - t_in * s0 ; 7059if (k >=0 && k < K ) { 7060// col layout: [K*OC, T_in], element (oc*K+k, t_in) 7061sum += type_conversion_table < elem_t > ::to_f32 (col_data [(oc * K + k )+ t_in * K_OC ]); 7062 } 7063 } 7064// dst layout: [T_out, OC], element (t_out, oc) 7065dst_data [t_out + oc * T_out ]= type_conversion_table < elem_t > ::from_f32 (sum ); 7066 } 7067 } 7068} 7069 7070void ggml_compute_forward_col2im_1d ( 7071const ggml_compute_params * params , 7072ggml_tensor * dst ) { 7073switch (dst -> src [0 ]-> type ) { 7074case GGML_TYPE_F32 :ggml_compute_forward_col2im_1d_impl < float > (params ,dst );break ; 7075case GGML_TYPE_F16 :ggml_compute_forward_col2im_1d_impl < ggml_fp16_t > (params ,dst );break ; 7076case GGML_TYPE_BF16 :ggml_compute_forward_col2im_1d_impl < ggml_bf16_t > (params ,dst );break ; 7077default :GGML_ABORT ("col2im_1d: unsupported type %d" ,dst -> src [0 ]-> type ); 7078 } 7079} 7080 7081// ggml_compute_forward_conv_2d 7082 7083 7084static void ggml_compute_forward_conv_2d_impl (const ggml_compute_params * params , 7085const ggml_tensor * kernel ,// [KW, KH, IC, OC] 7086const ggml_tensor * src ,// [W, H, C, N] 7087ggml_tensor * dst ,// [OW, OH, OC, N] 7088ggml_type kernel_type ) { 7089 7090GGML_ASSERT (ggml_is_contiguous (kernel )); 7091GGML_ASSERT (kernel_type == GGML_TYPE_F16 || kernel_type == GGML_TYPE_F32 ); 7092GGML_ASSERT (kernel -> type == kernel_type ); 7093 7094const ggml_type_traits * traits = ggml_get_type_traits (kernel_type ); 7095 7096const int32_t stride_x = dst -> op_params [0 ]; 7097const int32_t stride_y = dst -> op_params [1 ]; 7098const int32_t pad_x = dst -> op_params [2 ]; 7099const int32_t pad_y = dst -> op_params [3 ]; 7100const int32_t dilation_x = dst -> op_params [4 ]; 7101const int32_t dilation_y = dst -> op_params [5 ]; 7102 7103const int64_t c_in = src -> ne [2 ]; 7104const int64_t c_out = kernel -> ne [3 ]; 7105GGML_ASSERT (c_in == kernel -> ne [2 ]); 7106 7107const int64_t src_w = src -> ne [0 ]; 7108const int64_t src_h = src -> ne [1 ]; 7109const int64_t knl_w = kernel -> ne [0 ]; 7110const int64_t knl_h = kernel -> ne [1 ]; 7111const int64_t dst_w = dst -> ne [0 ]; 7112const int64_t dst_h = dst -> ne [1 ]; 7113 7114const float * src_data = (float * )src -> data ; 7115void * knl_data = kernel -> data ; 7116float * dst_data = (float * )dst -> data ; 7117 7118const int64_t knl_n = knl_w * knl_h * c_in ; 7119const int64_t patch_total = dst -> ne [3 ]* dst_w * dst_h ; 7120 7121const int64_t space_per_patch = knl_n * traits -> type_size + c_out * sizeof (float ); 7122const int64_t batch_size = params -> wsize /space_per_patch ; 7123const int64_t patches_per_batch = batch_size > 8 ? (batch_size /8 )* 8 :batch_size ; 7124const int64_t batch_n = (patch_total + patches_per_batch - 1 ) /patches_per_batch ; 7125 7126GGML_ASSERT (patches_per_batch > 0 && batch_size >=1 ); 7127 7128void * tmp = params -> wdata ; 7129 7130for (int64_t batch_i = 0 ;batch_i < batch_n ;++ batch_i ) { 7131 7132const int64_t patch_start_batch = batch_i * patches_per_batch ; 7133const int64_t patch_end_batch = std::min (patch_start_batch + patches_per_batch , 7134patch_total ); 7135const int64_t patch_n = patch_end_batch - patch_start_batch ; 7136 7137const int64_t patch_per_thread = (patch_n + params -> nth - 1 ) /params -> nth ; 7138const int64_t patch_start = patch_start_batch + params -> ith * patch_per_thread ; 7139const int64_t patch_end = std::min (patch_start + patch_per_thread ,patch_end_batch ); 7140 7141//im2col for a patch 7142for (int64_t p = patch_start ;p < patch_end ;++ p ) { 7143const int64_t batch_n = p / (dst_w * dst_h ); 7144const int64_t src_x = (p /dst_w ) %dst_h ; 7145const int64_t src_y = p %dst_w ; 7146 7147const float * src_base = (const float * )((const char * )src_data + batch_n * src -> nb [3 ]); 7148char * dst_row = (char * )tmp + (p %patches_per_batch )* knl_n * traits -> type_size ; 7149 7150for (int64_t ic = 0 ;ic < c_in ;++ ic ) { 7151for (int64_t ky = 0 ;ky < knl_h ;++ ky ) { 7152for (int64_t kx = 0 ;kx < knl_w ;++ kx ) { 7153const int64_t sy = src_x * stride_y + ky * dilation_y - pad_y ; 7154const int64_t sx = src_y * stride_x + kx * dilation_x - pad_x ; 7155 7156int64_t dst_idx = ic * (knl_h * knl_w )+ ky * knl_w + kx ; 7157 7158float src_val ; 7159if (sy < 0 || sy >=src_h || sx < 0 || sx >=src_w ) { 7160src_val = 0.0f ; 7161 }else { 7162const float * src_ptr = (const float * )((const char * )src_base + sx * src -> nb [0 ]+ sy * src -> nb [1 ]+ ic * src -> nb [2 ]); 7163src_val = * src_ptr ; 7164 } 7165 7166char * element_ptr = dst_row + dst_idx * traits -> type_size ; 7167if (kernel_type == GGML_TYPE_F32 ) { 7168* (float * )element_ptr = src_val ; 7169 }else if (kernel_type == GGML_TYPE_F16 ) { 7170* (ggml_fp16_t * )element_ptr = GGML_CPU_FP32_TO_FP16 (src_val ); 7171 } 7172 } 7173 } 7174 } 7175 }// patches handled by this thread 7176 7177ggml_barrier (params -> threadpool ); 7178 7179float * gemm_output = (float * ) ((char * )tmp + patches_per_batch * knl_n * traits -> type_size ); 7180 7181GGML_ASSERT (gemm_output + patch_n * c_out <= (float * )tmp + params -> wsize ); 7182 7183// GEMM: patches[patch_n, knl_n] × kernel[knl_n, c_out] = output[patch_n, c_out] 7184ggml_call_mul_mat (kernel_type ,params ,patch_n ,c_out ,knl_n ,tmp ,knl_data ,gemm_output ); 7185 7186ggml_barrier (params -> threadpool ); 7187 7188 7189//permute back [OC, N, OH, OW] to [N, OC, OH, OW] 7190const int64_t permute_per_thread = (patch_n + params -> nth - 1 ) /params -> nth ; 7191const int64_t permute_start = params -> ith * permute_per_thread ; 7192const int64_t permute_end = std::min (permute_start + permute_per_thread ,patch_n ); 7193 7194for (int64_t i = permute_start ;i < permute_end ;++ i ) { 7195const int64_t p = patch_start_batch + i ; 7196const int64_t batch_n = p / (dst_w * dst_h ); 7197const int64_t dst_y = (p /dst_w ) %dst_h ; 7198const int64_t dst_x = p %dst_w ; 7199 7200for (int64_t oc = 0 ;oc < c_out ;++ oc ) { 7201const float value = gemm_output [i * c_out + oc ]; 7202float * 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 ]); 7203* dst_ptr = value ; 7204 } 7205 } 7206 } 7207} 7208 7209void ggml_compute_forward_conv_2d ( 7210const ggml_compute_params * params , 7211ggml_tensor * dst ) { 7212 7213const ggml_tensor * src0 = dst -> src [0 ]; 7214const ggml_tensor * src1 = dst -> src [1 ]; 7215 7216ggml_compute_forward_conv_2d_impl (params ,src0 ,src1 ,dst ,src0 -> type ); 7217} 7218 7219// ggml_compute_forward_conv_3d 7220 7221static void ggml_compute_forward_conv_3d_impl (const ggml_compute_params * params , 7222const ggml_tensor * kernel , 7223const ggml_tensor * src , 7224ggml_tensor * dst , 7225ggml_type kernel_type ) { 7226 7227GGML_ASSERT (ggml_is_contiguous (kernel )); 7228GGML_ASSERT (kernel_type == GGML_TYPE_F16 || kernel_type == GGML_TYPE_F32 ); 7229GGML_ASSERT (kernel -> type == kernel_type ); 7230 7231const ggml_type_traits * traits = ggml_get_type_traits (kernel_type ); 7232 7233const int32_t s0 = dst -> op_params [0 ]; 7234const int32_t s1 = dst -> op_params [1 ]; 7235const int32_t s2 = dst -> op_params [2 ]; 7236const int32_t p0 = dst -> op_params [3 ]; 7237const int32_t p1 = dst -> op_params [4 ]; 7238const int32_t p2 = dst -> op_params [5 ]; 7239const int32_t d0 = dst -> op_params [6 ]; 7240const int32_t d1 = dst -> op_params [7 ]; 7241const int32_t d2 = dst -> op_params [8 ]; 7242const int32_t c = dst -> op_params [9 ]; 7243const int32_t n = dst -> op_params [10 ]; 7244const int32_t oc = dst -> op_params [11 ]; 7245 7246const int64_t src_w = src -> ne [0 ]; 7247const int64_t src_h = src -> ne [1 ]; 7248const int64_t src_d = src -> ne [2 ]; 7249const int64_t knl_w = kernel -> ne [0 ]; 7250const int64_t knl_h = kernel -> ne [1 ]; 7251const int64_t knl_d = kernel -> ne [2 ]; 7252const int64_t dst_w = dst -> ne [0 ]; 7253const int64_t dst_h = dst -> ne [1 ]; 7254const int64_t dst_d = dst -> ne [2 ]; 7255 7256const float * src_data = (float * )src -> data ; 7257void * knl_data = kernel -> data ; 7258float * dst_data = (float * )dst -> data ; 7259 7260const int64_t knl_n_per_channel = knl_w * knl_h * knl_d ; 7261const int64_t knl_n_total = knl_n_per_channel * c ; 7262const int64_t patch_total = n * dst_w * dst_h * dst_d ; 7263 7264const int64_t space_per_patch = knl_n_total * traits -> type_size + oc * sizeof (float ); 7265const int64_t batch_size = params -> wsize /space_per_patch ; 7266const int64_t patches_per_batch = batch_size > 8 ? (batch_size /8 )* 8 :batch_size ; 7267const int64_t batch_n = (patch_total + patches_per_batch - 1 ) /patches_per_batch ; 7268 7269GGML_ASSERT (patches_per_batch > 0 && batch_size >=1 ); 7270 7271void * tmp = params -> wdata ; 7272 7273for (int64_t batch_i = 0 ;batch_i < batch_n ;++ batch_i ) { 7274const int64_t patch_start_batch = batch_i * patches_per_batch ; 7275const int64_t patch_end_batch = std::min (patch_start_batch + patches_per_batch ,patch_total ); 7276const int64_t patch_n_in_batch = patch_end_batch - patch_start_batch ; 7277 7278const int64_t patch_per_thread = (patch_n_in_batch + params -> nth - 1 ) /params -> nth ; 7279const int64_t patch_start = patch_start_batch + params -> ith * patch_per_thread ; 7280const int64_t patch_end = std::min (patch_start + patch_per_thread ,patch_end_batch ); 7281 7282for (int64_t p = patch_start ;p < patch_end ;++ p ) { 7283const int64_t p_in_batch = p % (dst_w * dst_h * dst_d ); 7284const int64_t p_in_depth = p_in_batch % (dst_w * dst_h ); 7285const int64_t batch_idx = p / (dst_w * dst_h * dst_d ); 7286const int64_t dst_z = p_in_batch / (dst_w * dst_h ); 7287const int64_t dst_y = p_in_depth /dst_w ; 7288const int64_t dst_x = p_in_depth %dst_w ; 7289 7290char * dst_row = (char * )tmp + (p %patches_per_batch )* knl_n_total * traits -> type_size ; 7291 7292for (int64_t ic = 0 ;ic < c ;++ ic ) { 7293for (int64_t kz = 0 ;kz < knl_d ;++ kz ) { 7294for (int64_t ky = 0 ;ky < knl_h ;++ ky ) { 7295for (int64_t kx = 0 ;kx < knl_w ;++ kx ) { 7296const int64_t sz = dst_z * s2 + kz * d2 - p2 ; 7297const int64_t sy = dst_y * s1 + ky * d1 - p1 ; 7298const int64_t sx = dst_x * s0 + kx * d0 - p0 ; 7299 7300int64_t dst_idx = ic * knl_n_per_channel + kz * (knl_h * knl_w )+ ky * knl_w + kx ; 7301 7302float src_val ; 7303if (sz < 0 || sz >=src_d || sy < 0 || sy >=src_h || sx < 0 || sx >=src_w ) { 7304src_val = 0.0f ; 7305 }else { 7306const int64_t cn_idx = batch_idx * c + ic ; 7307const 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 ]); 7308src_val = * src_ptr ; 7309 } 7310 7311char * element_ptr = dst_row + dst_idx * traits -> type_size ; 7312if (kernel_type == GGML_TYPE_F32 ) { 7313* (float * )element_ptr = src_val ; 7314 }else if (kernel_type == GGML_TYPE_F16 ) { 7315* (ggml_fp16_t * )element_ptr = GGML_CPU_FP32_TO_FP16 (src_val ); 7316 } 7317 } 7318 } 7319 } 7320 } 7321 } 7322 7323ggml_barrier (params -> threadpool ); 7324 7325float * gemm_output = (float * ) ((char * )tmp + patches_per_batch * knl_n_total * traits -> type_size ); 7326ggml_call_mul_mat (kernel_type ,params ,patch_n_in_batch ,oc ,knl_n_total ,tmp ,knl_data ,gemm_output ); 7327 7328ggml_barrier (params -> threadpool ); 7329 7330const int64_t permute_per_thread = (patch_n_in_batch + params -> nth - 1 ) /params -> nth ; 7331const int64_t permute_start = params -> ith * permute_per_thread ; 7332const int64_t permute_end = std::min (permute_start + permute_per_thread ,patch_n_in_batch ); 7333 7334for (int64_t i = permute_start ;i < permute_end ;++ i ) { 7335const int64_t p = patch_start_batch + i ; 7336const int64_t p_in_batch = p % (dst_w * dst_h * dst_d ); 7337const int64_t p_in_depth = p_in_batch % (dst_w * dst_h ); 7338const int64_t batch_idx = p / (dst_w * dst_h * dst_d ); 7339const int64_t dst_z = p_in_batch / (dst_w * dst_h ); 7340const int64_t dst_y = p_in_depth /dst_w ; 7341const int64_t dst_x = p_in_depth %dst_w ; 7342 7343for (int64_t ioc = 0 ;ioc < oc ;++ ioc ) { 7344const float value = gemm_output [i * oc + ioc ]; 7345const int64_t ocn_idx = batch_idx * oc + ioc ; 7346float * 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 ]); 7347* dst_ptr = value ; 7348 } 7349 } 7350 } 7351} 7352 7353void ggml_compute_forward_conv_3d ( 7354const ggml_compute_params * params , 7355ggml_tensor * dst ) { 7356const ggml_tensor * src0 = dst -> src [0 ]; 7357const ggml_tensor * src1 = dst -> src [1 ]; 7358ggml_compute_forward_conv_3d_impl (params ,src0 ,src1 ,dst ,src0 -> type ); 7359} 7360 7361template < typename kernel_t > 7362static void ggml_compute_forward_conv_transpose_2d_impl ( 7363const ggml_compute_params * params , 7364ggml_tensor * dst ) { 7365 7366const ggml_tensor * src0 = dst -> src [0 ]; 7367const ggml_tensor * src1 = dst -> src [1 ]; 7368 7369GGML_ASSERT (src0 -> type == GGML_TYPE_F16 || src0 -> type == GGML_TYPE_F32 ); 7370GGML_ASSERT (src1 -> type == GGML_TYPE_F32 ); 7371GGML_ASSERT (dst -> type == GGML_TYPE_F32 ); 7372 7373GGML_TENSOR_BINARY_OP_LOCALS 7374 7375const int ith = params -> ith ; 7376const int nth = params -> nth ; 7377 7378const int nk = ne00 * ne01 * ne02 * ne03 ; 7379 7380GGML_ASSERT (nb00 == ggml_type_size (src0 -> type )); 7381GGML_ASSERT (nb10 == sizeof (float )); 7382 7383if (ith == 0 ) { 7384memset (params -> wdata ,0 ,params -> wsize ); 7385 7386// permute kernel data (src0) from (Kw x Kh x Cout x Cin) to (Cin x Kw x Kh x Cout) 7387 { 7388kernel_t * const wdata = (kernel_t * )params -> wdata + 0 ; 7389 7390for (int64_t i03 = 0 ;i03 < ne03 ;i03 ++ ) { 7391for (int64_t i02 = 0 ;i02 < ne02 ;i02 ++ ) { 7392const kernel_t * const src = (kernel_t * )((char * )src0 -> data + i03 * nb03 + i02 * nb02 ); 7393kernel_t * dst_data = wdata + i02 * ne01 * ne00 * ne03 ; 7394for (int64_t i01 = 0 ;i01 < ne01 ;i01 ++ ) { 7395for (int64_t i00 = 0 ;i00 < ne00 ;i00 ++ ) { 7396dst_data [i01 * ne00 * ne03 + i00 * ne03 + i03 ]= src [i01 * ne00 + i00 ]; 7397 } 7398 } 7399 } 7400 } 7401 } 7402 7403// permute source data (src1) from (Sw x Sh x Cin) to (Cin x Sw x Sh), for all batches 7404 { 7405kernel_t * const wdata = (kernel_t * )params -> wdata + nk ; 7406for (int i13 = 0 ;i13 < ne13 ;i13 ++ ) { 7407kernel_t * const wdata_b = wdata + i13 * ne10 * ne11 * ne12 ; 7408for (int i12 = 0 ;i12 < ne12 ;i12 ++ ) { 7409for (int i11 = 0 ;i11 < ne11 ;i11 ++ ) { 7410const float * const src = (float * )((char * )src1 -> data + i13 * nb13 + i12 * nb12 + i11 * nb11 ); 7411kernel_t * dst_data = wdata_b + i11 * ne10 * ne12 ; 7412for (int i10 = 0 ;i10 < ne10 ;i10 ++ ) { 7413if constexpr (std::is_same_v < kernel_t ,ggml_fp16_t > ) { 7414dst_data [i10 * ne12 + i12 ]= GGML_CPU_FP32_TO_FP16 (src [i10 ]); 7415 }else { 7416dst_data [i10 * ne12 + i12 ]= src [i10 ]; 7417 } 7418 } 7419 } 7420 } 7421 } 7422 } 7423 7424memset (dst -> data ,0 ,ggml_nbytes (dst )); 7425 } 7426ggml_barrier (params -> threadpool ); 7427 7428const int32_t stride = ggml_get_op_params_i32 (dst ,0 ); 7429 7430// total patches in dst 7431const int np = ne2 ; 7432 7433// patches per thread 7434const int dp = (np + nth - 1 )/nth ; 7435 7436// patch range for this thread 7437const int ip0 = dp * ith ; 7438const int ip1 = MIN (ip0 + dp ,np ); 7439 7440kernel_t * const wdata = (kernel_t * )params -> wdata + 0 ; 7441kernel_t * const wdata_src = wdata + nk ; 7442 7443for (int i2 = ip0 ;i2 < ip1 ;i2 ++ ) {// Cout 7444kernel_t * wdata_kernel = wdata + i2 * ne01 * ne00 * ne03 ; 7445for (int i3 = 0 ;i3 < ne3 ;i3 ++ ) {// batch 7446float * dst_data = (float * )((char * )dst -> data + i3 * nb3 + i2 * nb2 ); 7447kernel_t * wdata_src_b = wdata_src + i3 * ne10 * ne11 * ne12 ; 7448for (int i11 = 0 ;i11 < ne11 ;i11 ++ ) { 7449for (int i10 = 0 ;i10 < ne10 ;i10 ++ ) { 7450const int i1n = i11 * ne10 * ne12 + i10 * ne12 ; 7451for (int i01 = 0 ;i01 < ne01 ;i01 ++ ) { 7452for (int i00 = 0 ;i00 < ne00 ;i00 ++ ) { 7453float v = 0 ; 7454if constexpr (std::is_same_v < kernel_t ,ggml_fp16_t > ) { 7455ggml_vec_dot_f16 (ne03 ,& v ,0 , 7456wdata_src_b + i1n ,0 , 7457wdata_kernel + i01 * ne00 * ne03 + i00 * ne03 ,0 ,1 ); 7458 }else { 7459ggml_vec_dot_f32 (ne03 ,& v ,0 , 7460wdata_src_b + i1n ,0 , 7461wdata_kernel + i01 * ne00 * ne03 + i00 * ne03 ,0 ,1 ); 7462 } 7463dst_data [(i11 * stride + i01 )* ne0 + i10 * stride + i00 ]+= v ; 7464 } 7465 } 7466 } 7467 } 7468 } 7469 } 7470} 7471 7472void ggml_compute_forward_conv_transpose_2d ( 7473const ggml_compute_params * params , 7474ggml_tensor * dst ) { 7475 7476const ggml_tensor * src0 = dst -> src [0 ]; 7477 7478switch (src0 -> type ) { 7479case GGML_TYPE_F16 : 7480 { 7481ggml_compute_forward_conv_transpose_2d_impl < ggml_fp16_t > (params ,dst ); 7482 }break ; 7483case GGML_TYPE_F32 : 7484 { 7485ggml_compute_forward_conv_transpose_2d_impl < float > (params ,dst ); 7486 }break ; 7487default : 7488 { 7489GGML_ABORT ("fatal error" ); 7490 } 7491 } 7492} 7493 7494// ggml_compute_forward_conv_2d_dw 7495 7496struct ggml_conv_2d_dw_params { 7497int64_t channels ; 7498int64_t batch ; 7499int64_t src_w ; 7500int64_t src_h ; 7501int64_t dst_w ; 7502int64_t dst_h ; 7503int64_t knl_w ; 7504int64_t knl_h ; 7505int stride_x ; 7506int stride_y ; 7507int pad_x ; 7508int pad_y ; 7509int dilation_x ; 7510int dilation_y ; 7511}; 7512 7513static inline float ggml_conv_2d_dw_knl_f32 (const char * data ,int64_t i ,ggml_type type ) { 7514if (type == GGML_TYPE_F16 ) { 7515return GGML_FP16_TO_FP32 (((const ggml_fp16_t * )data )[i ]); 7516 } 7517return ((const float * )data )[i ]; 7518} 7519 7520static void ggml_compute_forward_conv_2d_dw_cwhn ( 7521const ggml_compute_params * params , 7522const ggml_tensor * src , 7523const ggml_tensor * kernel , 7524ggml_tensor * dst , 7525const ggml_conv_2d_dw_params & p ) { 7526 7527const int64_t c = p .channels ; 7528const char * knl_data = (const char * )kernel -> data ; 7529const ggml_type knl_type = kernel -> type ; 7530 7531const int64_t rows_total = p .dst_h * p .batch ; 7532const int64_t rows_per_thread = (rows_total + params -> nth - 1 ) /params -> nth ; 7533const int64_t row_start = params -> ith * rows_per_thread ; 7534const int64_t row_end = MIN (row_start + rows_per_thread ,rows_total ); 7535 7536#ifdef GGML_SIMD 7537int64_t c_pkg_end = 0 ; 7538int64_t pkg_size = GGML_F32_EPR ; 7539if (knl_type == GGML_TYPE_F32 ) { 7540#if defined(__ARM_FEATURE_SVE ) 7541pkg_size = svcntw (); 7542#else 7543pkg_size = GGML_F32_EPR ; 7544#endif 7545c_pkg_end = (c /pkg_size )* pkg_size ; 7546 } 7547#else 7548const int64_t c_pkg_end = 0 ; 7549#endif 7550 7551for (int64_t row = row_start ;row < row_end ;++ row ) { 7552const int64_t dst_y = row %p .dst_h ; 7553const float * src_data = (const float * )src -> data + (row /p .dst_h )* p .src_w * p .src_h * c ; 7554for (int64_t dst_x = 0 ;dst_x < p .dst_w ;++ dst_x ) { 7555float * dst_data = (float * )dst -> data + (row * p .dst_w + dst_x )* c ; 7556const int64_t src_y_base = dst_y * p .stride_y - p .pad_y ; 7557const int64_t src_x_base = dst_x * p .stride_x - p .pad_x ; 7558 7559#ifdef GGML_SIMD 7560for (int64_t c_i = 0 ;c_i < c_pkg_end ;c_i += pkg_size ) { 7561GGML_F32_VEC sum = GGML_F32_VEC_ZERO ; 7562for (int64_t knl_y = 0 ;knl_y < p .knl_h ;++ knl_y ) { 7563const int64_t src_y = src_y_base + knl_y * p .dilation_y ; 7564if (src_y < 0 || src_y >=p .src_h ) { 7565continue ; 7566 } 7567for (int64_t knl_x = 0 ;knl_x < p .knl_w ;++ knl_x ) { 7568const int64_t src_x = src_x_base + knl_x * p .dilation_x ; 7569if (src_x < 0 || src_x >=p .src_w ) { 7570continue ; 7571 } 7572const float * kp = (const float * )knl_data + (knl_y * p .knl_w + knl_x )* c + c_i ; 7573GGML_F32_VEC k = GGML_F32_VEC_LOAD (kp ); 7574GGML_F32_VEC s = GGML_F32_VEC_LOAD (src_data + (src_y * p .src_w + src_x )* c + c_i ); 7575sum = GGML_F32_VEC_FMA (sum ,k ,s ); 7576 } 7577 } 7578GGML_F32_VEC_STORE (dst_data + c_i ,sum ); 7579 } 7580#endif 7581for (int64_t c_i = c_pkg_end ;c_i < c ;++ c_i ) { 7582float sum = 0.0f ; 7583for (int64_t knl_y = 0 ;knl_y < p .knl_h ;++ knl_y ) { 7584const int64_t src_y = src_y_base + knl_y * p .dilation_y ; 7585if (src_y < 0 || src_y >=p .src_h ) { 7586continue ; 7587 } 7588for (int64_t knl_x = 0 ;knl_x < p .knl_w ;++ knl_x ) { 7589const int64_t src_x = src_x_base + knl_x * p .dilation_x ; 7590if (src_x < 0 || src_x >=p .src_w ) { 7591continue ; 7592 } 7593sum += ggml_conv_2d_dw_knl_f32 (knl_data , (knl_y * p .knl_w + knl_x )* c + c_i ,knl_type ) 7594* src_data [(src_y * p .src_w + src_x )* c + c_i ]; 7595 } 7596 } 7597dst_data [c_i ]= sum ; 7598 } 7599 } 7600 } 7601} 7602 7603static void ggml_compute_forward_conv_2d_dw_whcn ( 7604const ggml_compute_params * params , 7605const ggml_tensor * src , 7606const ggml_tensor * kernel , 7607ggml_tensor * dst , 7608const ggml_conv_2d_dw_params & p ) { 7609 7610const int64_t n = p .channels * p .batch ; 7611const int64_t per_thread = (n + params -> nth - 1 ) /params -> nth ; 7612const int64_t start = params -> ith * per_thread ; 7613const int64_t end = MIN (start + per_thread ,n ); 7614const char * knl_base = (const char * )kernel -> data ; 7615const ggml_type knl_type = kernel -> type ; 7616 7617for (int64_t i = start ;i < end ;++ i ) { 7618const int64_t knl_offset = (i %p .channels )* p .knl_w * p .knl_h ; 7619const float * src_data = (const float * )src -> data + i * p .src_w * p .src_h ; 7620float * dst_data = (float * )dst -> data + i * p .dst_w * p .dst_h ; 7621 7622for (int64_t dst_y = 0 ;dst_y < p .dst_h ;++ dst_y ) { 7623for (int64_t dst_x = 0 ;dst_x < p .dst_w ;++ dst_x ) { 7624 7625float sum = 0.0f ; 7626for (int64_t knl_y = 0 ;knl_y < p .knl_h ;++ knl_y ) { 7627const int64_t src_y = dst_y * p .stride_y + knl_y * p .dilation_y - p .pad_y ; 7628if (src_y < 0 || src_y >=p .src_h ) { 7629continue ; 7630 } 7631for (int64_t knl_x = 0 ;knl_x < p .knl_w ;++ knl_x ) { 7632const int64_t src_x = dst_x * p .stride_x + knl_x * p .dilation_x - p .pad_x ; 7633if (src_x < 0 || src_x >=p .src_w ) { 7634continue ; 7635 } 7636sum += ggml_conv_2d_dw_knl_f32 (knl_base ,knl_offset + knl_y * p .knl_w + knl_x ,knl_type ) 7637* src_data [src_y * p .src_w + src_x ]; 7638 } 7639 } 7640dst_data [dst_y * p .dst_w + dst_x ]= sum ; 7641 } 7642 } 7643 } 7644} 7645 7646void ggml_compute_forward_conv_2d_dw ( 7647const ggml_compute_params * params , 7648ggml_tensor * dst ) { 7649 7650const ggml_tensor * kernel = dst -> src [0 ]; 7651const ggml_tensor * src = dst -> src [1 ]; 7652ggml_conv_2d_dw_params p ; 7653p .channels = src -> ne [2 ]; 7654p .batch = src -> ne [3 ]; 7655p .src_w = src -> ne [0 ]; 7656p .src_h = src -> ne [1 ]; 7657p .dst_w = dst -> ne [0 ]; 7658p .dst_h = dst -> ne [1 ]; 7659p .knl_w = kernel -> ne [0 ]; 7660p .knl_h = kernel -> ne [1 ]; 7661p .stride_x = dst -> op_params [0 ]; 7662p .stride_y = dst -> op_params [1 ]; 7663p .pad_x = dst -> op_params [2 ]; 7664p .pad_y = dst -> op_params [3 ]; 7665p .dilation_x = dst -> op_params [4 ]; 7666p .dilation_y = dst -> op_params [5 ]; 7667 7668GGML_ASSERT (kernel -> type == GGML_TYPE_F32 || kernel -> type == GGML_TYPE_F16 ); 7669GGML_ASSERT (kernel -> ne [3 ]== p .channels ); 7670GGML_ASSERT (dst -> ne [3 ]== p .batch ); 7671 7672if (ggml_is_contiguous (src )) { 7673ggml_compute_forward_conv_2d_dw_whcn (params ,src ,kernel ,dst ,p ); 7674 }else if (ggml_is_contiguous_channels (src )) { 7675GGML_ASSERT (kernel -> nb [0 ] >=kernel -> nb [2 ]&& kernel -> nb [1 ] >=kernel -> nb [0 ]); 7676ggml_compute_forward_conv_2d_dw_cwhn (params ,src ,kernel ,dst ,p ); 7677 }else { 7678GGML_ABORT ("non-contiguous memory layout not supported" ); 7679 } 7680} 7681 7682// ggml_compute_forward_pool_1d_ksp 7683static void ggml_compute_forward_pool_1d_ksp ( 7684const ggml_compute_params * params , 7685const ggml_op_pool op , 7686const int k , 7687const int s , 7688const int p , 7689ggml_tensor * dst ) { 7690 7691const ggml_tensor * src = dst -> src [0 ]; 7692 7693assert (src -> type == GGML_TYPE_F32 || src -> type == GGML_TYPE_F16 ); 7694 7695if (params -> ith != 0 ) { 7696return ; 7697 } 7698 7699const int64_t IW = src -> ne [0 ]; 7700const int64_t OW = dst -> ne [0 ]; 7701 7702const int64_t nr = ggml_nrows (src ); 7703 7704for (int64_t ir = 0 ;ir < nr ;++ ir ) { 7705const char * srow_bytes = (const char * )src -> data + ir * src -> nb [1 ]; 7706float * drow = (float * ) ((char * )dst -> data + ir * dst -> nb [1 ]); 7707 7708for (int64_t ow = 0 ;ow < OW ;++ ow ) { 7709float res = 0 ; 7710switch (op ) { 7711case GGML_OP_POOL_AVG :res = 0.0f ;break ; 7712case GGML_OP_POOL_MAX :res = - FLT_MAX ;break ; 7713case GGML_OP_POOL_COUNT :GGML_ABORT ("fatal error" ); 7714 } 7715 7716int count = 0 ; 7717const int base = (int )ow * s - p ; 7718 7719for (int ki = 0 ;ki < k ;++ ki ) { 7720const int j = base + ki ; 7721if (j < 0 || j >= (int )IW ) { 7722continue ; 7723 } 7724 7725float v ; 7726if (src -> type == GGML_TYPE_F32 ) { 7727v = ((const float * )srow_bytes )[j ]; 7728 }else { 7729v = GGML_CPU_FP16_TO_FP32 (((const ggml_fp16_t * )srow_bytes )[j ]); 7730 } 7731 7732switch (op ) { 7733case GGML_OP_POOL_AVG :res += v ;break ; 7734case GGML_OP_POOL_MAX :res = std::max (v ,res );break ; 7735case GGML_OP_POOL_COUNT :GGML_ABORT ("fatal error" ); 7736 } 7737 7738++ count ; 7739 } 7740 7741switch (op ) { 7742case GGML_OP_POOL_AVG :res = (count > 0 ) ? (res /count ) :0.0f ;break ; 7743case GGML_OP_POOL_MAX :break ; 7744case GGML_OP_POOL_COUNT :GGML_ABORT ("fatal error" ); 7745 } 7746 7747drow [ow ]= res ; 7748 } 7749 } 7750} 7751 7752// ggml_compute_forward_pool_1d 7753 7754void ggml_compute_forward_pool_1d ( 7755const ggml_compute_params * params , 7756ggml_tensor * dst ) { 7757 7758const int32_t * opts = (const int32_t * )dst -> op_params ; 7759ggml_op_pool op = static_cast < ggml_op_pool > (opts [0 ]); 7760const int k0 = opts [1 ]; 7761const int s0 = opts [2 ]; 7762const int p0 = opts [3 ]; 7763 7764ggml_compute_forward_pool_1d_ksp (params ,op ,k0 ,s0 ,p0 ,dst ); 7765} 7766 7767// ggml_compute_forward_pool_2d 7768 7769void ggml_compute_forward_pool_2d ( 7770const ggml_compute_params * params , 7771ggml_tensor * dst ) { 7772 7773const ggml_tensor * src = dst -> src [0 ]; 7774 7775assert (src -> type == GGML_TYPE_F32 || src -> type == GGML_TYPE_F16 ); 7776 7777if (params -> ith != 0 ) { 7778return ; 7779 } 7780 7781const int32_t * opts = (const int32_t * )dst -> op_params ; 7782 7783ggml_op_pool op = static_cast < ggml_op_pool > (opts [0 ]); 7784const int k0 = opts [1 ]; 7785const int k1 = opts [2 ]; 7786const int s0 = opts [3 ]; 7787const int s1 = opts [4 ]; 7788const int p0 = opts [5 ]; 7789const int p1 = opts [6 ]; 7790const char * cdata = (const char * )src -> data ; 7791const char * const data_end = cdata + ggml_nbytes (src ); 7792 7793const int64_t px = dst -> ne [0 ]; 7794const int64_t py = dst -> ne [1 ]; 7795const int64_t pa = px * py ; 7796 7797float * dplane = (float * )dst -> data ; 7798 7799const int ka = k0 * k1 ; 7800const int offset0 = - p0 ; 7801const int offset1 = - p1 ; 7802 7803while (cdata < data_end ) { 7804for (int oy = 0 ;oy < py ;++ oy ) { 7805float * const drow = dplane + oy * px ; 7806float * const out = drow ; 7807 7808for (int ox = 0 ;ox < px ;++ ox ) { 7809float res = 0 ; 7810switch (op ) { 7811case GGML_OP_POOL_AVG :res = 0 ;break ; 7812case GGML_OP_POOL_MAX :res = - FLT_MAX ;break ; 7813case GGML_OP_POOL_COUNT :GGML_ABORT ("fatal error" ); 7814 } 7815 7816const int ix = offset0 + ox * s0 ; 7817const int iy = offset1 + oy * s1 ; 7818 7819for (int ky = 0 ;ky < k1 ;++ ky ) { 7820if (iy + ky < 0 || iy + ky >=src -> ne [1 ]) { 7821continue ; 7822 } 7823 7824const void * srow = (const void * )(cdata + src -> nb [1 ]* (iy + ky )); 7825for (int kx = 0 ;kx < k0 ;++ kx ) { 7826int j = ix + kx ; 7827if (j < 0 || j >=src -> ne [0 ]) { 7828continue ; 7829 } 7830 7831const float srow_j = (src -> type == GGML_TYPE_F32 ) ? ((const float * )srow )[j ] :GGML_CPU_FP16_TO_FP32 (((const ggml_fp16_t * )srow )[j ]); 7832switch (op ) { 7833case GGML_OP_POOL_AVG :res += srow_j ;break ; 7834case GGML_OP_POOL_MAX :res = std::max (srow_j ,res );break ; 7835case GGML_OP_POOL_COUNT :GGML_ABORT ("fatal error" ); 7836 } 7837 } 7838 } 7839switch (op ) { 7840case GGML_OP_POOL_AVG :res /=ka ;break ; 7841case GGML_OP_POOL_MAX :break ; 7842case GGML_OP_POOL_COUNT :GGML_ABORT ("fatal error" ); 7843 } 7844 7845out [ox ]= res ; 7846 } 7847 } 7848 7849cdata += src -> nb [2 ]; 7850dplane += pa ; 7851 } 7852} 7853 7854// ggml_compute_forward_pool_2d_back 7855 7856void ggml_compute_forward_pool_2d_back ( 7857const ggml_compute_params * params , 7858ggml_tensor * dst ) { 7859 7860const ggml_tensor * src = dst -> src [0 ]; 7861const ggml_tensor * dstf = dst -> src [1 ];// forward tensor of dst 7862 7863assert (dst -> type == GGML_TYPE_F32 || dst -> type == GGML_TYPE_F16 ); 7864 7865if (params -> ith != 0 ) { 7866return ; 7867 } 7868 7869const int32_t * opts = (const int32_t * )dst -> op_params ; 7870ggml_op_pool op = static_cast < ggml_op_pool > (opts [0 ]); 7871const int k0 = opts [1 ]; 7872const int k1 = opts [2 ]; 7873const int s0 = opts [3 ]; 7874const int s1 = opts [4 ]; 7875const int p0 = opts [5 ]; 7876const int p1 = opts [6 ]; 7877 7878char * cdata = (char * )dst -> data ; 7879const char * cdataf = (const char * )dstf -> data ; 7880const char * const data_end = cdata + ggml_nbytes (dst ); 7881 7882GGML_ASSERT (params -> ith == 0 ); 7883memset (cdata ,0 ,ggml_nbytes (dst )); 7884 7885const int64_t px = src -> ne [0 ]; 7886const int64_t py = src -> ne [1 ]; 7887const int64_t pa = px * py ; 7888 7889const float * splane = (const float * )src -> data ; 7890 7891const int ka = k0 * k1 ; 7892const int offset0 = - p0 ; 7893const int offset1 = - p1 ; 7894 7895while (cdata < data_end ) { 7896for (int oy = 0 ;oy < py ;++ oy ) { 7897const float * const srow = splane + oy * px ; 7898for (int ox = 0 ;ox < px ;++ ox ) { 7899const float grad0 = srow [ox ]; 7900 7901const int ix = offset0 + ox * s0 ; 7902const int iy = offset1 + oy * s1 ; 7903 7904if (op == GGML_OP_POOL_MAX ) { 7905float maxval = - FLT_MAX ; 7906int kxmax = -1 ; 7907int kymax = -1 ; 7908 7909for (int ky = 0 ;ky < k1 ;++ ky ) { 7910if (iy + ky < 0 || iy + ky >=dst -> ne [1 ]) { 7911continue ; 7912 } 7913const void * drowf = (const void * )(cdataf + dst -> nb [1 ]* (iy + ky )); 7914for (int kx = 0 ;kx < k0 ;++ kx ) { 7915int j = ix + kx ; 7916if (j < 0 || j >=dst -> ne [0 ]) { 7917continue ; 7918 } 7919 7920const float val = dst -> type == GGML_TYPE_F32 ? 7921 ((const float * )drowf )[j ] :GGML_CPU_FP16_TO_FP32 (((const ggml_fp16_t * )drowf )[j ]); 7922if (val <=maxval ) { 7923continue ; 7924 } 7925 7926maxval = val ; 7927kxmax = kx ; 7928kymax = ky ; 7929 } 7930 } 7931 7932if (kxmax == -1 || kymax == -1 ) { 7933continue ; 7934 } 7935 7936void * drow = (void * )(cdata + dst -> nb [1 ]* (iy + kymax )); 7937const int j = ix + kxmax ; 7938if (dst -> type == GGML_TYPE_F32 ) { 7939 ((float * )drow )[j ]+= grad0 ; 7940 }else { 7941 ((ggml_fp16_t * )drow )[j ]= GGML_CPU_FP32_TO_FP16 (grad0 + GGML_CPU_FP16_TO_FP32 (((const ggml_fp16_t * )drow )[j ])); 7942 } 7943 }else if (op == GGML_OP_POOL_AVG ) { 7944const float grad = grad0 /ka ; 7945 7946for (int ky = 0 ;ky < k1 ;++ ky ) { 7947if (iy + ky < 0 || iy + ky >=dst -> ne [1 ]) { 7948continue ; 7949 } 7950void * drow = (void * )(cdata + dst -> nb [1 ]* (iy + ky )); 7951for (int kx = 0 ;kx < k0 ;++ kx ) { 7952int j = ix + kx ; 7953if (j < 0 || j >=dst -> ne [0 ]) { 7954continue ; 7955 } 7956 7957if (dst -> type == GGML_TYPE_F32 ) { 7958 ((float * )drow )[j ]+= grad ; 7959 }else { 7960 ((ggml_fp16_t * )drow )[j ]+= GGML_CPU_FP32_TO_FP16 (grad ); 7961 } 7962 } 7963 } 7964 }else { 7965GGML_ASSERT (false); 7966 } 7967 } 7968 } 7969 7970cdata += dst -> nb [2 ]; 7971cdataf += dst -> nb [2 ]; 7972splane += pa ; 7973 } 7974} 7975 7976// ggml_compute_forward_upscale 7977 7978static void ggml_compute_forward_upscale_f32 ( 7979const ggml_compute_params * params , 7980ggml_tensor * dst ) { 7981 7982const ggml_tensor * src0 = dst -> src [0 ]; 7983 7984GGML_ASSERT (src0 -> type == GGML_TYPE_F32 ); 7985 7986const int ith = params -> ith ; 7987const int nth = params -> nth ; 7988 7989GGML_TENSOR_UNARY_OP_LOCALS 7990 7991float sf0 = (float )ne0 /src0 -> ne [0 ]; 7992float sf1 = (float )ne1 /src0 -> ne [1 ]; 7993float sf2 = (float )ne2 /src0 -> ne [2 ]; 7994float sf3 = (float )ne3 /src0 -> ne [3 ]; 7995float pixel_offset = 0.5f ; 7996 7997const int32_t mode_flags = ggml_get_op_params_i32 (dst ,0 ); 7998const ggml_scale_mode mode = (ggml_scale_mode ) (mode_flags & 0xFF ); 7999 8000if (mode_flags & GGML_SCALE_FLAG_ALIGN_CORNERS ) { 8001pixel_offset = 0.0f ; 8002sf0 = ne0 > 1 && ne00 > 1 ? (float )(ne0 - 1 ) / (ne00 - 1 ) :sf0 ; 8003sf1 = ne1 > 1 && ne01 > 1 ? (float )(ne1 - 1 ) / (ne01 - 1 ) :sf1 ; 8004 } 8005 8006if (mode == GGML_SCALE_MODE_NEAREST ) { 8007for (int64_t i3 = 0 ;i3 < ne3 ;i3 ++ ) { 8008const int64_t i03 = i3 /sf3 ; 8009for (int64_t i2 = ith ;i2 < ne2 ;i2 += nth ) { 8010const int64_t i02 = i2 /sf2 ; 8011for (int64_t i1 = 0 ;i1 < ne1 ;i1 ++ ) { 8012const int64_t i01 = i1 /sf1 ; 8013for (int64_t i0 = 0 ;i0 < ne0 ;i0 ++ ) { 8014const int64_t i00 = i0 /sf0 ; 8015 8016const float * x = (float * )((char * )src0 -> data + i00 * nb00 + i01 * nb01 + i02 * nb02 + i03 * nb03 ); 8017float * y = (float * )((char * )dst -> data + i0 * nb0 + i1 * nb1 + i2 * nb2 + i3 * nb3 ); 8018 8019* y = * x ; 8020 } 8021 } 8022 } 8023 } 8024 }else if (mode == GGML_SCALE_MODE_BILINEAR && (mode_flags & GGML_SCALE_FLAG_ANTIALIAS )) { 8025// Similar to F.interpolate(..., mode="bilinear", align_corners=False, antialias=True) 8026// https://github.com/pytorch/pytorch/blob/8871ff29b743948d1225389d5b7068f37b22750b/aten/src/ATen/native/cpu/UpSampleKernel.cpp 8027auto triangle_filter = [](float x )-> float { 8028return std::max (1.0f - fabsf (x ),0.0f ); 8029 }; 8030 8031// support and invscale, minimum 1 pixel for bilinear 8032const float support1 = std::max (1.0f ,1.0f /sf1 ); 8033const float invscale1 = 1.0f /support1 ; 8034const float support0 = std::max (1.0f ,1.0f /sf0 ); 8035const float invscale0 = 1.0f /support0 ; 8036 8037for (int64_t i3 = 0 ;i3 < ne3 ;i3 ++ ) { 8038const int64_t i03 = i3 /sf3 ; 8039for (int64_t i2 = ith ;i2 < ne2 ;i2 += nth ) { 8040const int64_t i02 = i2 /sf2 ; 8041for (int64_t i1 = 0 ;i1 < ne1 ;i1 ++ ) { 8042const float y = ((float )i1 + pixel_offset ) /sf1 ; 8043for (int64_t i0 = 0 ;i0 < ne0 ;i0 ++ ) { 8044const float x = ((float )i0 + pixel_offset ) /sf0 ; 8045 8046// the range of source pixels that contribute 8047const int64_t x_min = std::max < int64_t > (x - support0 + pixel_offset ,0 ); 8048const int64_t x_max = std::min < int64_t > (x + support0 + pixel_offset ,ne00 ); 8049const int64_t y_min = std::max < int64_t > (y - support1 + pixel_offset ,0 ); 8050const int64_t y_max = std::min < int64_t > (y + support1 + pixel_offset ,ne01 ); 8051 8052// bilinear filter with antialiasing 8053float val = 0.0f ; 8054float total_weight = 0.0f ; 8055 8056for (int64_t sy = y_min ;sy < y_max ;sy ++ ) { 8057const float weight_y = triangle_filter ((sy - y + pixel_offset )* invscale1 ); 8058 8059for (int64_t sx = x_min ;sx < x_max ;sx ++ ) { 8060const float weight_x = triangle_filter ((sx - x + pixel_offset )* invscale0 ); 8061const float weight = weight_x * weight_y ; 8062 8063if (weight <=0.0f ) { 8064continue ; 8065 } 8066 8067const float pixel = * (const float * )((const char * )src0 -> data + sx * nb00 + sy * nb01 + i02 * nb02 + i03 * nb03 ); 8068val += pixel * weight ; 8069total_weight += weight ; 8070 } 8071 } 8072 8073if (total_weight > 0.0f ) { 8074val /=total_weight ; 8075 } 8076 8077float * dst_ptr = (float * )((char * )dst -> data + i0 * nb0 + i1 * nb1 + i2 * nb2 + i3 * nb3 ); 8078* dst_ptr = val ; 8079 } 8080 } 8081 } 8082 } 8083 }else if (mode == GGML_SCALE_MODE_BILINEAR ) { 8084for (int64_t i3 = 0 ;i3 < ne3 ;i3 ++ ) { 8085const int64_t i03 = i3 /sf3 ; 8086for (int64_t i2 = ith ;i2 < ne2 ;i2 += nth ) { 8087const int64_t i02 = i2 /sf2 ; 8088for (int64_t i1 = 0 ;i1 < ne1 ;i1 ++ ) { 8089const float y = ((float )i1 + pixel_offset ) /sf1 - pixel_offset ; 8090int64_t y0 = (int64_t )floorf (y ); 8091int64_t y1 = y0 + 1 ; 8092 8093y0 = std::max (int64_t (0 ), std::min (y0 ,ne01 - 1 )); 8094y1 = std::max (int64_t (0 ), std::min (y1 ,ne01 - 1 )); 8095 8096float dy = y - (float )y0 ; 8097dy = std::max (0.0f , std::min (dy ,1.0f )); 8098 8099for (int64_t i0 = 0 ;i0 < ne0 ;i0 ++ ) { 8100const float x = ((float )i0 + pixel_offset ) /sf0 - pixel_offset ; 8101int64_t x0 = (int64_t )floorf (x ); 8102int64_t x1 = x0 + 1 ; 8103 8104x0 = std::max (int64_t (0 ), std::min (x0 ,ne00 - 1 )); 8105x1 = std::max (int64_t (0 ), std::min (x1 ,ne00 - 1 )); 8106 8107float dx = x - (float )x0 ; 8108dx = std::max (0.0f , std::min (dx ,1.0f )); 8109 8110// fetch the four surrounding pixel values and interpolate 8111const float a = * (const float * )((const char * )src0 -> data + x0 * nb00 + y0 * nb01 + i02 * nb02 + i03 * nb03 ); 8112const float b = * (const float * )((const char * )src0 -> data + x1 * nb00 + y0 * nb01 + i02 * nb02 + i03 * nb03 ); 8113const float c = * (const float * )((const char * )src0 -> data + x0 * nb00 + y1 * nb01 + i02 * nb02 + i03 * nb03 ); 8114const float d = * (const float * )((const char * )src0 -> data + x1 * nb00 + y1 * nb01 + i02 * nb02 + i03 * nb03 ); 8115 8116const float val = a * (1 - dx )* (1 - dy )+ b * dx * (1 - dy )+ c * (1 - dx )* dy + d * dx * dy ; 8117 8118float * y_dst = (float * )((char * )dst -> data + i0 * nb0 + i1 * nb1 + i2 * nb2 + i3 * nb3 ); 8119* y_dst = val ; 8120 } 8121 } 8122 } 8123 } 8124 }else if (mode == GGML_SCALE_MODE_BICUBIC ) { 8125// https://en.wikipedia.org/wiki/Bicubic_interpolation#Bicubic_convolution_algorithm 8126const float a = -0.75f ;// use alpha = -0.75 (same as PyTorch) 8127auto weight1 = [a ](float x ) {return ((a + 2 )* x - (a + 3 ))* x * x + 1 ; }; 8128auto weight2 = [a ](float x ) {return ((a * x - 5 * a )* x + 8 * a )* x - 4 * a ; }; 8129auto bicubic = [= ](float p0 ,float p1 ,float p2 ,float p3 ,float x ) { 8130const float w0 = weight2 (x + 1 ); 8131const float w1 = weight1 (x + 0 ); 8132const float w2 = weight1 (1 - x ); 8133const float w3 = weight2 (2 - x ); 8134return p0 * w0 + p1 * w1 + p2 * w2 + p3 * w3 ; 8135 }; 8136 8137for (int64_t i3 = 0 ;i3 < ne3 ;i3 ++ ) { 8138const int64_t i03 = i3 /sf3 ; 8139for (int64_t i2 = ith ;i2 < ne2 ;i2 += nth ) { 8140const int64_t i02 = i2 /sf2 ; 8141for (int64_t i1 = 0 ;i1 < ne1 ;i1 ++ ) { 8142const float y = ((float )i1 + pixel_offset ) /sf1 - pixel_offset ; 8143const int64_t y0 = (int64_t )floorf (y ); 8144const float dy = y - (float )y0 ; 8145 8146for (int64_t i0 = 0 ;i0 < ne0 ;i0 ++ ) { 8147const float x = ((float )i0 + pixel_offset ) /sf0 - pixel_offset ; 8148const int64_t x0 = (int64_t )floorf (x ); 8149const float dx = x - (float )x0 ; 8150 8151auto p = [= ](int64_t x_off ,int64_t y_off )-> float { 8152int64_t i00 = std::max (int64_t (0 ), std::min (x0 + x_off ,ne00 - 1 )); 8153int64_t i01 = std::max (int64_t (0 ), std::min (y0 + y_off ,ne01 - 1 )); 8154return * (const float * )((const char * )src0 -> data + i00 * nb00 + i01 * nb01 + i02 * nb02 + i03 * nb03 ); 8155 }; 8156 8157const float val = bicubic ( 8158bicubic (p (-1 ,-1 ),p (0 ,-1 ),p (1 ,-1 ),p (2 ,-1 ),dx ), 8159bicubic (p (-1 ,0 ),p (0 ,0 ),p (1 ,0 ),p (2 ,0 ),dx ), 8160bicubic (p (-1 ,1 ),p (0 ,1 ),p (1 ,1 ),p (2 ,1 ),dx ), 8161bicubic (p (-1 ,2 ),p (0 ,2 ),p (1 ,2 ),p (2 ,2 ),dx ),dy ); 8162 8163float * y_dst = (float * )((char * )dst -> data + i0 * nb0 + i1 * nb1 + i2 * nb2 + i3 * nb3 ); 8164* y_dst = val ; 8165 } 8166 } 8167 } 8168 } 8169 }else { 8170GGML_ABORT ("unsupported upscale mode" ); 8171 } 8172} 8173 8174void ggml_compute_forward_upscale ( 8175const ggml_compute_params * params , 8176ggml_tensor * dst ) { 8177 8178const ggml_tensor * src0 = dst -> src [0 ]; 8179 8180switch (src0 -> type ) { 8181case GGML_TYPE_F32 : 8182 { 8183ggml_compute_forward_upscale_f32 (params ,dst ); 8184 }break ; 8185default : 8186 { 8187GGML_ABORT ("fatal error" ); 8188 } 8189 } 8190} 8191 8192 8193// ggml_compute_forward_pad 8194 8195template < bool circular_t > 8196static void ggml_compute_forward_pad_f32 ( 8197const ggml_compute_params * params , 8198ggml_tensor * dst ) { 8199 8200const ggml_tensor * src0 = dst -> src [0 ]; 8201 8202assert (dst -> nb [0 ]== sizeof (float )); 8203 8204const int ith = params -> ith ; 8205const int nth = params -> nth ; 8206 8207GGML_TENSOR_UNARY_OP_LOCALS 8208 8209float * dst_ptr = (float * )dst -> data ; 8210const int32_t lp0 = ggml_get_op_params_i32 (dst ,0 ); 8211const int32_t rp0 = ggml_get_op_params_i32 (dst ,1 ); 8212const int32_t lp1 = ggml_get_op_params_i32 (dst ,2 ); 8213const int32_t rp1 = ggml_get_op_params_i32 (dst ,3 ); 8214const int32_t lp2 = ggml_get_op_params_i32 (dst ,4 ); 8215const int32_t rp2 = ggml_get_op_params_i32 (dst ,5 ); 8216const int32_t lp3 = ggml_get_op_params_i32 (dst ,6 ); 8217const int32_t rp3 = ggml_get_op_params_i32 (dst ,7 ); 8218 8219// TODO: optimize 8220 8221for (int64_t i2 = 0 ;i2 < ne2 ;++ i2 ) { 8222for (int64_t i1 = ith ;i1 < ne1 ;i1 += nth ) { 8223for (int64_t i0 = 0 ;i0 < ne0 ;++ i0 ) { 8224for (int64_t i3 = 0 ;i3 < ne3 ;++ i3 ) { 8225// circular means wrap around on a torus, so x and y loop around 8226if constexpr (circular_t ) { 8227const int64_t dst_idx = i3 * (ne0 * ne1 * ne2 )+ i2 * (ne0 * ne1 )+ i1 * ne0 + i0 ; 8228const int64_t src_i0 = ggml_wrap_around (i0 - lp0 ,ne00 ); 8229const int64_t src_i1 = ggml_wrap_around (i1 - lp1 ,ne01 ); 8230const int64_t src_i2 = ggml_wrap_around (i2 - lp2 ,ne02 ); 8231const int64_t src_i3 = ggml_wrap_around (i3 - lp3 ,ne03 ); 8232 8233const int64_t src_idx = 8234src_i3 * nb03 + 8235src_i2 * nb02 + 8236src_i1 * nb01 + 8237src_i0 * nb00 ; 8238 8239const float * src_ptr = (const float * )((char * )src0 -> data + src_idx ); 8240dst_ptr [dst_idx ]= * src_ptr ; 8241 }else { 8242const int64_t dst_idx = i3 * (ne0 * ne1 * ne2 )+ i2 * (ne0 * ne1 )+ i1 * ne0 + i0 ; 8243if ((i0 >=lp0 && i0 < ne0 - rp0 ) \ 8244&& (i1 >=lp1 && i1 < ne1 - rp1 ) \ 8245&& (i2 >=lp2 && i2 < ne2 - rp2 ) \ 8246&& (i3 >=lp3 && i3 < ne3 - rp3 )) { 8247const int64_t src_idx = (i3 - lp3 )* nb03 + (i2 - lp2 )* nb02 + (i1 - lp1 )* nb01 + (i0 - lp0 )* nb00 ; 8248const float * src_ptr = (const float * )((char * )src0 -> data + src_idx ); 8249dst_ptr [dst_idx ]= * src_ptr ; 8250 }else { 8251dst_ptr [dst_idx ]= 0 ; 8252 } 8253 } 8254 } 8255 } 8256 } 8257 } 8258} 8259 8260 8261void ggml_compute_forward_pad ( 8262const ggml_compute_params * params , 8263ggml_tensor * dst ) { 8264const ggml_tensor * src0 = dst -> src [0 ]; 8265const bool circular = (bool )ggml_get_op_params_i32 (dst ,8 ); 8266switch (src0 -> type ) { 8267case GGML_TYPE_F32 : 8268 { 8269if (circular ) { 8270ggml_compute_forward_pad_f32 < true> (params ,dst ); 8271 }else { 8272ggml_compute_forward_pad_f32 < false> (params ,dst ); 8273 } 8274 }break ; 8275default : 8276 { 8277GGML_ABORT ("fatal error" ); 8278 } 8279 } 8280} 8281 8282// ggml_compute_forward_pad_reflect_1d 8283 8284void ggml_compute_forward_pad_reflect_1d ( 8285const ggml_compute_params * params , 8286ggml_tensor * dst ) { 8287 8288const ggml_tensor * src0 = dst -> src [0 ]; 8289 8290GGML_ASSERT (src0 -> type == GGML_TYPE_F32 ); 8291GGML_ASSERT (dst -> type == GGML_TYPE_F32 ); 8292 8293const int ith = params -> ith ; 8294const int nth = params -> nth ; 8295 8296const int32_t * opts = (const int32_t * )dst -> op_params ; 8297const int p0 = opts [0 ]; 8298const int p1 = opts [1 ]; 8299 8300GGML_TENSOR_UNARY_OP_LOCALS 8301 8302for (int64_t i3 = 0 ;i3 < ne3 ;i3 ++ ) { 8303for (int64_t i2 = 0 ;i2 < ne2 ;i2 ++ ) { 8304for (int64_t i1 = ith ;i1 < ne1 ;i1 += nth ) { 8305float * left = (float * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * nb1 + p0 * nb0 ); 8306float * right = (float * ) ((char * )dst -> data + i3 * nb3 + i2 * nb2 + i1 * nb1 + (ne0 - p1 - 1 )* nb0 ); 8307 8308ggml_vec_cpy_f32 (ne00 ,left , (float * ) ((char * )src0 -> data + i3 * nb03 + i2 * nb02 + i1 * nb01 )); 8309 8310for (int i0 = 1 ;i0 <=p0 ;i0 ++ ) {left [- i0 ]= left [i0 ]; } 8311for (int i0 = 1 ;i0 <=p1 ;i0 ++ ) {right [i0 ]= right [- i0 ]; } 8312 } 8313 } 8314 } 8315} 8316 8317// ggml_compute_forward_roll 8318 8319static int64_t ggml_wrap_index (int64_t i ,int64_t ne ) { 8320if (i < 0 ) { 8321return i + ne ; 8322 }else if (i >=ne ) { 8323return i - ne ; 8324 } 8325return i ; 8326} 8327 8328static void ggml_compute_forward_roll_f32 ( 8329const ggml_compute_params * params , 8330ggml_tensor * dst ) { 8331 8332const ggml_tensor * src0 = dst -> src [0 ]; 8333const float * src_data = (const float * )src0 -> data ; 8334float * dst_data = (float * )dst -> data ; 8335 8336GGML_TENSOR_UNARY_OP_LOCALS 8337 8338const int s0 = ggml_get_op_params_i32 (dst ,0 ); 8339const int s1 = ggml_get_op_params_i32 (dst ,1 ); 8340const int s2 = ggml_get_op_params_i32 (dst ,2 ); 8341const int s3 = ggml_get_op_params_i32 (dst ,3 ); 8342 8343const int64_t total = ne1 * ne2 * ne3 ; 8344const int64_t per_thread = (total + params -> nth ) /params -> nth ; 8345const int64_t start = params -> ith * per_thread ; 8346const int64_t end = std::min (start + per_thread ,total ); 8347 8348for (int64_t i = start ;i < end ;++ i ) { 8349const int64_t i1 = i %ne1 ; 8350const int64_t i2 = (i /ne1 ) %ne2 ; 8351const int64_t i3 = i / (ne2 * ne1 ); 8352float * dst_row = dst_data + (i3 * nb3 + i2 * nb2 + i1 * nb1 ) /sizeof (float ); 8353 8354const int64_t i01 = ggml_wrap_index (i1 - s1 ,ne01 ); 8355const int64_t i02 = ggml_wrap_index (i2 - s2 ,ne02 ); 8356const int64_t i03 = ggml_wrap_index (i3 - s3 ,ne03 ); 8357const float * src_row = src_data + (i03 * nb03 + i02 * nb02 + i01 * nb01 ) /sizeof (float ); 8358 8359const int64_t s = ggml_wrap_index (- s0 ,ne00 ); 8360const int64_t n = ne00 - s ; 8361ggml_vec_cpy_f32 (n ,dst_row ,src_row + s ); 8362ggml_vec_cpy_f32 (s ,dst_row + n ,src_row ); 8363 } 8364} 8365 8366void ggml_compute_forward_roll ( 8367const ggml_compute_params * params , 8368ggml_tensor * dst ) { 8369 8370const ggml_tensor * src0 = dst -> src [0 ]; 8371 8372switch (src0 -> type ) { 8373case GGML_TYPE_F32 : 8374 { 8375ggml_compute_forward_roll_f32 (params ,dst ); 8376 }break ; 8377default : 8378 { 8379GGML_ABORT ("fatal error" ); 8380 } 8381 } 8382} 8383 8384// ggml_compute_forward_arange 8385 8386static void ggml_compute_forward_arange_f32 ( 8387const ggml_compute_params * params , 8388ggml_tensor * dst ) { 8389 8390GGML_ASSERT (dst -> nb [0 ]== sizeof (float )); 8391 8392const int ith = params -> ith ; 8393const int nth = params -> nth ; 8394 8395const float start = ggml_get_op_params_f32 (dst ,0 ); 8396const float stop = ggml_get_op_params_f32 (dst ,1 ); 8397const float step = ggml_get_op_params_f32 (dst ,2 ); 8398 8399const int64_t steps = (int64_t )ceilf ((stop - start ) /step ); 8400 8401GGML_ASSERT (ggml_nelements (dst )== steps ); 8402 8403for (int64_t i = ith ;i < steps ;i += nth ) { 8404float value = start + step * i ; 8405 ((float * )dst -> data )[i ]= value ; 8406 } 8407} 8408 8409void ggml_compute_forward_arange ( 8410const ggml_compute_params * params , 8411ggml_tensor * dst ) { 8412switch (dst -> type ) { 8413case GGML_TYPE_F32 : 8414 { 8415ggml_compute_forward_arange_f32 (params ,dst ); 8416 }break ; 8417default : 8418 { 8419GGML_ABORT ("fatal error" ); 8420 } 8421 } 8422} 8423 8424static void ggml_compute_forward_timestep_embedding_f32 ( 8425const ggml_compute_params * params , 8426ggml_tensor * dst ) { 8427 8428const ggml_tensor * src0 = dst -> src [0 ]; 8429 8430GGML_ASSERT (src0 -> nb [0 ]== sizeof (float )); 8431 8432const int ith = params -> ith ; 8433const int nth = params -> nth ; 8434 8435GGML_TENSOR_UNARY_OP_LOCALS 8436 8437const int dim = ggml_get_op_params_i32 (dst ,0 ); 8438const int max_period = ggml_get_op_params_i32 (dst ,1 ); 8439 8440int half = dim /2 ; 8441 8442for (int64_t i = 0 ;i < ne00 ;i ++ ) { 8443float * embed_data = (float * )((char * )dst -> data + i * nb1 ); 8444for (int64_t j = ith ;j < half ;j += nth ) { 8445float timestep = ((float * )src0 -> data )[i ]; 8446float freq = (float )expf (- logf (max_period )* j /half ); 8447float arg = timestep * freq ; 8448embed_data [j ]= cosf (arg ); 8449embed_data [j + half ]= sinf (arg ); 8450 } 8451if (dim %2 != 0 && ith == 0 ) { 8452embed_data [2 * half ]= 0.f ; 8453 } 8454 } 8455} 8456 8457void ggml_compute_forward_timestep_embedding ( 8458const ggml_compute_params * params , 8459ggml_tensor * dst ) { 8460 8461const ggml_tensor * src0 = dst -> src [0 ]; 8462 8463switch (src0 -> type ) { 8464case GGML_TYPE_F32 : 8465 { 8466ggml_compute_forward_timestep_embedding_f32 (params ,dst ); 8467 }break ; 8468default : 8469 { 8470GGML_ABORT ("fatal error" ); 8471 } 8472 } 8473} 8474 8475// ggml_compute_forward_argsort 8476 8477template < enum ggml_sort_order order > 8478struct cmp_argsort { 8479const float * data ; 8480bool operator()(int32_t a ,int32_t b )const { 8481if constexpr (order == GGML_SORT_ORDER_ASC ) { 8482return data [a ]< data [b ]; 8483 }else { 8484return data [a ]> data [b ]; 8485 } 8486 } 8487}; 8488 8489static void ggml_compute_forward_argsort_f32 ( 8490const ggml_compute_params * params , 8491ggml_tensor * dst ) { 8492 8493const ggml_tensor * src0 = dst -> src [0 ]; 8494 8495GGML_TENSOR_UNARY_OP_LOCALS 8496 8497GGML_ASSERT (nb0 == sizeof (float )); 8498 8499const int ith = params -> ith ; 8500const int nth = params -> nth ; 8501 8502const int64_t nr = ggml_nrows (src0 ); 8503 8504ggml_sort_order order = (ggml_sort_order )ggml_get_op_params_i32 (dst ,0 ); 8505 8506for (int64_t i = ith ;i < nr ;i += nth ) { 8507const float * src_data = (float * )((char * )src0 -> data + i * nb01 ); 8508 8509int32_t * dst_data = (int32_t * )((char * )dst -> data + i * nb1 ); 8510 8511for (int64_t j = 0 ;j < ne0 ;j ++ ) { 8512dst_data [j ]= j ; 8513 } 8514 8515switch (order ) { 8516case GGML_SORT_ORDER_ASC : 8517 std::sort (dst_data ,dst_data + ne0 ,cmp_argsort < GGML_SORT_ORDER_ASC > {src_data }); 8518break ; 8519 8520case GGML_SORT_ORDER_DESC : 8521 std::sort (dst_data ,dst_data + ne0 ,cmp_argsort < GGML_SORT_ORDER_DESC > {src_data }); 8522break ; 8523 8524default : 8525GGML_ABORT ("invalid sort order" ); 8526 } 8527 } 8528} 8529 8530void ggml_compute_forward_argsort ( 8531const ggml_compute_params * params , 8532ggml_tensor * dst ) { 8533 8534const ggml_tensor * src0 = dst -> src [0 ]; 8535 8536switch (src0 -> type ) { 8537case GGML_TYPE_F32 : 8538 { 8539ggml_compute_forward_argsort_f32 (params ,dst ); 8540 }break ; 8541default : 8542 { 8543GGML_ABORT ("fatal error" ); 8544 } 8545 } 8546} 8547 8548// ggml_compute_forward_top_k 8549 8550struct cmp_top_k { 8551const float * data ; 8552bool operator()(int32_t a ,int32_t b )const { 8553return data [a ]> data [b ]; 8554 } 8555}; 8556 8557static void ggml_compute_forward_top_k_f32 ( 8558const ggml_compute_params * params , 8559ggml_tensor * dst ) { 8560 8561const ggml_tensor * src0 = dst -> src [0 ]; 8562 8563GGML_TENSOR_UNARY_OP_LOCALS 8564 8565GGML_ASSERT (nb0 == sizeof (float )); 8566 8567const int ith = params -> ith ; 8568const int nth = params -> nth ; 8569 8570const int64_t nr = ggml_nrows (src0 ); 8571 8572const int top_k = ne0 ; 8573 8574int32_t * tmp = (int32_t * )params -> wdata + (ne00 + CACHE_LINE_SIZE_F32 )* ith ; 8575 8576for (int64_t i = ith ;i < nr ;i += nth ) { 8577const float * src_data = (float * )((char * )src0 -> data + i * nb01 ); 8578 8579for (int64_t j = 0 ;j < ne00 ;j ++ ) { 8580tmp [j ]= j ; 8581 } 8582 8583 std::partial_sort (tmp ,tmp + top_k ,tmp + ne00 ,cmp_top_k {src_data }); 8584 8585int32_t * dst_data = (int32_t * )((char * )dst -> data + i * nb1 ); 8586 8587 std::copy (tmp ,tmp + top_k ,dst_data ); 8588 8589// emphasize that the order is not important 8590if (top_k > 1 ) { 8591 std::swap (dst_data [0 ],dst_data [1 ]); 8592 } 8593 } 8594} 8595 8596void ggml_compute_forward_top_k ( 8597const ggml_compute_params * params , 8598ggml_tensor * dst ) { 8599 8600const ggml_tensor * src0 = dst -> src [0 ]; 8601 8602switch (src0 -> type ) { 8603case GGML_TYPE_F32 : 8604 { 8605ggml_compute_forward_top_k_f32 (params ,dst ); 8606 }break ; 8607default : 8608 { 8609GGML_ABORT ("fatal error" ); 8610 } 8611 } 8612} 8613 8614static void ggml_compute_forward_flash_attn_ext_f16_one_chunk ( 8615const ggml_compute_params * params , 8616ggml_tensor * dst , 8617int ir0 ,int ir1 , 8618int64_t ic_start ,int64_t ic_end , 8619float * partials ,int64_t partial_stride ) { 8620 8621const bool write_partials = (partials != nullptr ); 8622const ggml_tensor * q = dst -> src [0 ]; 8623const ggml_tensor * k = dst -> src [1 ]; 8624const ggml_tensor * v = dst -> src [2 ]; 8625const ggml_tensor * mask = dst -> src [3 ]; 8626const ggml_tensor * sinks = dst -> src [4 ]; 8627 8628GGML_TENSOR_LOCALS (int64_t ,neq ,q ,ne ) 8629GGML_TENSOR_LOCALS (size_t ,nbq ,q ,nb ) 8630GGML_TENSOR_LOCALS (int64_t ,nek ,k ,ne ) 8631GGML_TENSOR_LOCALS (size_t ,nbk ,k ,nb ) 8632GGML_TENSOR_LOCALS (int64_t ,nev ,v ,ne ) 8633GGML_TENSOR_LOCALS (size_t ,nbv ,v ,nb ) 8634GGML_TENSOR_LOCALS (int64_t ,ne ,dst ,ne ) 8635GGML_TENSOR_LOCALS (size_t ,nb ,dst ,nb ) 8636 8637const int64_t DK = nek0 ; 8638const int64_t DV = nev0 ; 8639const int64_t N = neq1 ; 8640 8641GGML_ASSERT (ne0 == DV ); 8642GGML_ASSERT (ne2 == N ); 8643 8644// input tensor rows must be contiguous 8645GGML_ASSERT (nbq0 == ggml_type_size (q -> type )); 8646GGML_ASSERT (nbk0 == ggml_type_size (k -> type )); 8647GGML_ASSERT (nbv0 == ggml_type_size (v -> type )); 8648 8649GGML_ASSERT (neq0 == DK ); 8650GGML_ASSERT (nek0 == DK ); 8651GGML_ASSERT (nev0 == DV ); 8652 8653GGML_ASSERT (neq1 == N ); 8654 8655// dst cannot be transposed or permuted 8656GGML_ASSERT (nb0 == sizeof (float )); 8657GGML_ASSERT (nb0 <=nb1 ); 8658GGML_ASSERT (nb1 <=nb2 ); 8659GGML_ASSERT (nb2 <=nb3 ); 8660 8661// broadcast factors 8662const int64_t rk2 = neq2 /nek2 ; 8663const int64_t rk3 = neq3 /nek3 ; 8664 8665const int64_t rv2 = neq2 /nev2 ; 8666const int64_t rv3 = neq3 /nev3 ; 8667 8668// parallelize by q rows using ggml_vec_dot_f32 8669 8670float scale = 1.0f ; 8671float max_bias = 0.0f ; 8672float logit_softcap = 0.0f ; 8673 8674memcpy (& scale , (float * )dst -> op_params + 0 ,sizeof (float )); 8675memcpy (& max_bias , (float * )dst -> op_params + 1 ,sizeof (float )); 8676memcpy (& logit_softcap , (float * )dst -> op_params + 2 ,sizeof (float )); 8677 8678if (logit_softcap != 0 ) { 8679scale /=logit_softcap ; 8680 } 8681 8682const uint32_t n_head = neq2 ; 8683const uint32_t n_head_log2 = 1u << (uint32_t )floor (log2 (n_head )); 8684 8685const float m0 = powf (2.0f ,- (max_bias ) /n_head_log2 ); 8686const float m1 = powf (2.0f ,- (max_bias /2.0f ) /n_head_log2 ); 8687 8688ggml_type const k_vec_dot_type = ggml_get_type_traits_cpu (k -> type )-> vec_dot_type ; 8689ggml_from_float_t const q_to_vec_dot = ggml_get_type_traits_cpu (k_vec_dot_type )-> from_float ; 8690ggml_vec_dot_t const kq_vec_dot = ggml_get_type_traits_cpu (k -> type )-> vec_dot ; 8691ggml_to_float_t const v_to_float = ggml_get_type_traits (v -> type )-> to_float ; 8692 8693GGML_ASSERT ((q_to_vec_dot )&& "fattn: unsupported K-type" ); 8694GGML_ASSERT ((v -> type == GGML_TYPE_F32 || v_to_float )&& "fattn: unsupported V-type" ); 8695 8696int ith = params -> ith ; 8697 8698for (int ir = ir0 ;ir < ir1 ;++ ir ) { 8699// q indices 8700const int iq3 = ir /(neq2 * neq1 ); 8701const int iq2 = (ir - iq3 * neq2 * neq1 )/neq1 ; 8702const int iq1 = (ir - iq3 * neq2 * neq1 - iq2 * neq1 ); 8703 8704const uint32_t h = iq2 ;// head index 8705const float slope = (max_bias > 0.0f ) ?h < n_head_log2 ?powf (m0 ,h + 1 ) :powf (m1 ,2 * (h - n_head_log2 )+ 1 ) :1.0f ; 8706 8707float S = 0.0f ;// sum 8708float M = - INFINITY ;// maximum KQ value 8709 8710float * VKQ32 = (float * )params -> wdata + ith * (1 * DK + 2 * DV + CACHE_LINE_SIZE_F32 );// FP32 VKQ accumulator 8711float * V32 = (VKQ32 + 1 * DV );// (temporary) FP32 V buffer 8712ggml_fp16_t * VKQ16 = (ggml_fp16_t * ) (VKQ32 + 1 * DV );// (temporary) FP16 VKQ accumulator 8713ggml_fp16_t * Q_q = (ggml_fp16_t * ) (VKQ32 + 2 * DV );// (temporary) buffer for Q converted to quantized/FP16 8714 8715if (v -> type == GGML_TYPE_F16 ) { 8716memset (VKQ16 ,0 ,DV * sizeof (ggml_fp16_t )); 8717 }else { 8718memset (VKQ32 ,0 ,DV * sizeof (float )); 8719 } 8720 8721const 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 ; 8722 8723// k indices 8724const int ik3 = iq3 /rk3 ; 8725const int ik2 = iq2 /rk2 ; 8726 8727// v indices 8728const int iv3 = iq3 /rv3 ; 8729const int iv2 = iq2 /rv2 ; 8730 8731const float * pq = (const float * ) ((char * )q -> data + (iq1 * nbq1 + iq2 * nbq2 + iq3 * nbq3 )); 8732q_to_vec_dot (pq ,Q_q ,DK ); 8733 8734// online softmax / attention 8735// loop over n_kv and n_head_kv 8736// ref: https://arxiv.org/pdf/2112.05682.pdf 8737 8738for (int64_t ic = ic_start ;ic < ic_end ;++ ic ) { 8739const float mv = mp ?slope * GGML_CPU_FP16_TO_FP32 (mp [ic ]) :0.0f ; 8740if (mv == - INFINITY ) { 8741continue ; 8742 } 8743 8744float s ;// KQ value 8745 8746const char * k_data = (const char * )k -> data + (ic * nbk1 + ik2 * nbk2 + ik3 * nbk3 ); 8747kq_vec_dot (DK ,& s ,0 ,k_data ,0 ,Q_q ,0 ,1 ); 8748 8749s = s * scale ;// scale KQ value 8750 8751if (logit_softcap != 0.0f ) { 8752s = logit_softcap * tanhf (s ); 8753 } 8754 8755s += mv ;// apply mask 8756 8757const float Mold = M ; 8758 8759float ms = 1.0f ;// upon new higher max val, scale VKQ and KQ sum with this value 8760float vs = 1.0f ;// post-softmax KQ value, expf(s - M) 8761 8762const char * v_data = ((const char * )v -> data + (ic * nbv1 + iv2 * nbv2 + iv3 * nbv3 )); 8763 8764if (v -> type == GGML_TYPE_F16 ) { 8765if (s > M ) { 8766// s is new maximum, ms < 1.0f, vs == expf(s - s) == 1.0f 8767M = s ; 8768ms = expf (Mold - M ); 8769 8770// V = V*expf(Mold - M) 8771ggml_vec_scale_f16 (DV ,VKQ16 ,ms ); 8772 }else { 8773// no new maximum, ms == 1.0f, vs != 1.0f 8774vs = expf (s - M ); 8775 } 8776 8777// V += v*expf(s - M) 8778ggml_vec_mad_f16 (DV ,VKQ16 , (const ggml_fp16_t * )v_data ,vs ); 8779 }else { 8780if (s > M ) { 8781// s is new maximum, ms < 1.0f, vs == expf(s - s) == 1.0f 8782M = s ; 8783ms = expf (Mold - M ); 8784 8785// V = V*expf(Mold - M) 8786ggml_vec_scale_f32 (DV ,VKQ32 ,ms ); 8787 }else { 8788// no new maximum, ms == 1.0f, vs != 1.0f 8789vs = expf (s - M ); 8790 } 8791 8792// V += v*expf(s - M) 8793if (v_to_float ) { 8794v_to_float (v_data ,V32 ,DV ); 8795ggml_vec_mad_f32 (DV ,VKQ32 ,V32 ,vs ); 8796 }else { 8797// V is F32 8798ggml_vec_mad_f32 (DV ,VKQ32 , (const float * )v_data ,vs ); 8799 } 8800 } 8801 8802S = S * ms + vs ;// scale and increment sum with partial sum 8803 } 8804 8805if (v -> type == GGML_TYPE_F16 ) { 8806for (int64_t d = 0 ;d < DV ;++ d ) { 8807VKQ32 [d ]= GGML_CPU_FP16_TO_FP32 (VKQ16 [d ]); 8808 } 8809 } 8810 8811// sinks - apply only on the first kv-chunk 8812if (sinks && ic_start == 0 ) { 8813const float s = ((float * )((char * )sinks -> data ))[h ]; 8814 8815float ms = 1.0f ; 8816float vs = 1.0f ; 8817 8818if (s > M ) { 8819ms = expf (M - s ); 8820M = s ; 8821ggml_vec_scale_f32 (DV ,VKQ32 ,ms ); 8822 }else { 8823vs = expf (s - M ); 8824 } 8825 8826S = S * ms + vs ; 8827 } 8828 8829if (write_partials ) { 8830// Write M, S, VKQ to partials for later reduction 8831// partials layout: [M, S, VKQ[DV]] per query head 8832float * partial = partials + ir * partial_stride ; 8833partial [0 ]= M ; 8834partial [1 ]= S ; 8835memcpy (partial + 2 ,VKQ32 ,DV * sizeof (float )); 8836 }else { 8837// V /= S 8838const float S_inv = S == 0.0f ?0.0f :1.0f /S ; 8839ggml_vec_scale_f32 (DV ,VKQ32 ,S_inv ); 8840 8841// dst indices 8842const int i1 = iq1 ; 8843const int i2 = iq2 ; 8844const int i3 = iq3 ; 8845 8846// permute(0, 2, 1, 3) 8847memcpy ((char * )dst -> data + (i3 * ne2 * ne1 + i2 + i1 * ne1 )* nb1 ,VKQ32 ,nb1 ); 8848 } 8849 } 8850} 8851 8852static void ggml_compute_forward_flash_attn_ext_tiled ( 8853const ggml_compute_params * params , 8854ggml_tensor * dst , 8855int ir0 ,int ir1 ) { 8856const ggml_tensor * q = dst -> src [0 ]; 8857const ggml_tensor * k = dst -> src [1 ]; 8858const ggml_tensor * v = dst -> src [2 ]; 8859const ggml_tensor * mask = dst -> src [3 ]; 8860const ggml_tensor * sinks = dst -> src [4 ]; 8861 8862GGML_TENSOR_LOCALS (int64_t ,neq ,q ,ne ) 8863GGML_TENSOR_LOCALS (size_t ,nbq ,q ,nb ) 8864GGML_TENSOR_LOCALS (int64_t ,nek ,k ,ne ) 8865GGML_TENSOR_LOCALS (size_t ,nbk ,k ,nb ) 8866GGML_TENSOR_LOCALS (int64_t ,nev ,v ,ne ) 8867GGML_TENSOR_LOCALS (size_t ,nbv ,v ,nb ) 8868GGML_TENSOR_LOCALS (int64_t ,ne ,dst ,ne ) 8869GGML_TENSOR_LOCALS (size_t ,nb ,dst ,nb ) 8870 8871const int64_t DK = nek0 ; 8872const int64_t DV = nev0 ; 8873const int64_t N = neq1 ; 8874 8875GGML_ASSERT (ne0 == DV ); 8876GGML_ASSERT (ne2 == N ); 8877 8878// input tensor rows must be contiguous 8879GGML_ASSERT (nbq0 == ggml_type_size (q -> type )); 8880GGML_ASSERT (nbk0 == ggml_type_size (k -> type )); 8881GGML_ASSERT (nbv0 == ggml_type_size (v -> type )); 8882 8883GGML_ASSERT (neq0 == DK ); 8884GGML_ASSERT (nek0 == DK ); 8885GGML_ASSERT (nev0 == DV ); 8886 8887GGML_ASSERT (neq1 == N ); 8888 8889// dst cannot be transposed or permuted 8890GGML_ASSERT (nb0 == sizeof (float )); 8891GGML_ASSERT (nb0 <=nb1 ); 8892GGML_ASSERT (nb1 <=nb2 ); 8893GGML_ASSERT (nb2 <=nb3 ); 8894 8895GGML_ASSERT (k -> type == v -> type ); 8896const ggml_type kv_type = k -> type ; 8897 8898 8899// broadcast factors 8900const int64_t rk2 = neq2 /nek2 ; 8901const int64_t rk3 = neq3 /nek3 ; 8902 8903const int64_t rv2 = neq2 /nev2 ; 8904const int64_t rv3 = neq3 /nev3 ; 8905 8906float scale = 1.0f ; 8907float max_bias = 0.0f ; 8908float logit_softcap = 0.0f ; 8909 8910memcpy (& scale , (float * )dst -> op_params + 0 ,sizeof (float )); 8911memcpy (& max_bias , (float * )dst -> op_params + 1 ,sizeof (float )); 8912memcpy (& logit_softcap , (float * )dst -> op_params + 2 ,sizeof (float )); 8913 8914if (logit_softcap != 0 ) { 8915scale /=logit_softcap ; 8916 } 8917 8918const uint32_t n_head = neq2 ; 8919const uint32_t n_head_log2 = 1u << (uint32_t )floor (log2 (n_head )); 8920 8921const float m0 = powf (2.0f ,- (max_bias ) /n_head_log2 ); 8922const float m1 = powf (2.0f ,- (max_bias /2.0f ) /n_head_log2 ); 8923 8924int ith = params -> ith ; 8925 8926static constexpr int Q_TILE_SZ = ggml_fa_tile_config::Q ; 8927static constexpr int KV_TILE_SZ = ggml_fa_tile_config::KV ; 8928 8929int ir = ir0 ; 8930while (ir < ir1 ) { 8931// q indices for the start of this tile 8932const int iq3 = ir /(neq2 * neq1 ); 8933const int iq2 = (ir - iq3 * neq2 * neq1 )/neq1 ; 8934const int iq1 = (ir - iq3 * neq2 * neq1 - iq2 * neq1 ); 8935 8936// Number of valid rows in this tile: 8937// - limited by tile size (Q_TILE_SZ) 8938// - limited by chunk boundary (ir1 - ir) 8939// - limited by head boundary (neq1 - iq1) to avoid crossing into next head 8940const int tile_rows = MIN (Q_TILE_SZ ,MIN ((int )(ir1 - ir ), (int )(neq1 - iq1 ))); 8941GGML_ASSERT (tile_rows > 0 ); 8942 8943const uint32_t h = iq2 ;// head index 8944const float slope = (max_bias > 0.0f ) ?h < n_head_log2 ?powf (m0 ,h + 1 ) :powf (m1 ,2 * (h - n_head_log2 )+ 1 ) :1.0f ; 8945 8946float S [Q_TILE_SZ ]; 8947float M [Q_TILE_SZ ]; 8948 8949for (int i = 0 ;i < Q_TILE_SZ ;++ i ) { 8950S [i ]= 0. ; 8951M [i ]= - INFINITY ; 8952 } 8953 8954// Per-thread scratch layout: 8955// Q_q: Q_TILE_SZ * DK (converted Q tile — F32 for GEMM, KV type for scalar) 8956// KQ: Q_TILE_SZ * KV_TILE_SZ (attention scores in float) 8957// mask: Q_TILE_SZ * KV_TILE_SZ (mask in float) 8958// VKQ32: Q_TILE_SZ * DV (FP32 output accumulator) 8959// V32: KV_TILE_SZ * DV (F32 buffer for V tile) 8960// K_f32: KV_TILE_SZ * DK (F32 buffer for K tile — GEMM path) 8961float * 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 ); 8962 8963void * Q_q = base ; 8964float * KQ = (float * )((char * )base + Q_TILE_SZ * DK * sizeof (float )); 8965float * mask32 = KQ + Q_TILE_SZ * KV_TILE_SZ ; 8966float * VKQ32 = mask32 + Q_TILE_SZ * KV_TILE_SZ ; 8967float * V32 = VKQ32 + Q_TILE_SZ * DV ; 8968float * K_f32 = V32 + KV_TILE_SZ * DV ; 8969 8970memset (VKQ32 ,0 ,Q_TILE_SZ * DV * sizeof (float )); 8971memset (mask32 ,0 ,Q_TILE_SZ * KV_TILE_SZ * sizeof (float )); 8972 8973// k indices 8974const int ik3 = iq3 /rk3 ; 8975const int ik2 = iq2 /rk2 ; 8976 8977// v indices 8978const int iv3 = iq3 /rv3 ; 8979const int iv2 = iq2 /rv2 ; 8980 8981 { 8982float * Q_f32 = (float * )Q_q ; 8983for (int tq = 0 ;tq < tile_rows ;tq ++ ) { 8984const float * pq = (const float * ) ((char * )q -> data + ((iq1 + tq )* nbq1 + iq2 * nbq2 + iq3 * nbq3 )); 8985memcpy (Q_f32 + tq * DK ,pq ,DK * sizeof (float )); 8986 } 8987for (int tq = tile_rows ;tq < Q_TILE_SZ ;tq ++ ) { 8988memset (Q_f32 + tq * DK ,0 ,DK * sizeof (float )); 8989 } 8990 } 8991 8992memset (K_f32 ,0 ,DK * KV_TILE_SZ * sizeof (float )); 8993memset (V32 ,0 ,KV_TILE_SZ * DV * sizeof (float )); 8994 8995for (int64_t ic = 0 ;ic < nek1 ;ic += KV_TILE_SZ ) { 8996const int kv_tile = (int )std::min ((int64_t )KV_TILE_SZ ,nek1 - ic ); 8997 8998// skip the tile entirely if all the masks are -inf 8999if (mask ) { 9000bool can_skip = true; 9001for (int tq = 0 ;tq < tile_rows ;tq ++ ) { 9002const 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 ]); 9003for (int tk = 0 ;tk < kv_tile ;tk ++ ) { 9004mask32 [tq * KV_TILE_SZ + tk ]= slope * GGML_CPU_FP16_TO_FP32 (mp_row [ic + tk ]); 9005if (mask32 [tq * KV_TILE_SZ + tk ]!= - INFINITY ) { 9006can_skip = false; 9007 } 9008 } 9009// Pad remaining mask entries with -inf 9010for (int tk = kv_tile ;tk < KV_TILE_SZ ;tk ++ ) { 9011mask32 [tq * KV_TILE_SZ + tk ]= - INFINITY ; 9012 } 9013 } 9014 9015if (can_skip ) { 9016continue ; 9017 } 9018 } 9019 9020// Pack K tile transposed: K_f32[dk][kv] so KV_TILE is contiguous (SIMD dim) 9021// Zero-pad the last tile so the GEMM always operates on KV_TILE_SZ columns 9022for (int tk = 0 ;tk < kv_tile ;tk ++ ) { 9023const char * k_data = (const char * )k -> data + (ic + tk )* nbk1 + ik2 * nbk2 + ik3 * nbk3 ; 9024if (kv_type == GGML_TYPE_F16 ) { 9025const ggml_fp16_t * k_f16 = (const ggml_fp16_t * )k_data ; 9026for (int64_t dk = 0 ;dk < DK ;dk ++ ) { 9027K_f32 [dk * KV_TILE_SZ + tk ]= GGML_CPU_FP16_TO_FP32 (k_f16 [dk ]); 9028 } 9029 }else { 9030const float * k_f32_src = (const float * )k_data ; 9031for (int64_t dk = 0 ;dk < DK ;dk ++ ) { 9032K_f32 [dk * KV_TILE_SZ + tk ]= k_f32_src [dk ]; 9033 } 9034 } 9035 } 9036memset (KQ ,0 ,Q_TILE_SZ * KV_TILE_SZ * sizeof (float )); 9037simd_gemm (KQ , (const float * )Q_q ,K_f32 ,Q_TILE_SZ ,DK ,KV_TILE_SZ ); 9038ggml_vec_scale_f32 (Q_TILE_SZ * KV_TILE_SZ ,KQ ,scale ); 9039 9040// Set padded KQ entries to -inf so softmax gives them zero weight 9041if (kv_tile < KV_TILE_SZ ) { 9042for (int tq = 0 ;tq < Q_TILE_SZ ;tq ++ ) { 9043for (int tk = kv_tile ;tk < KV_TILE_SZ ;tk ++ ) { 9044KQ [tq * KV_TILE_SZ + tk ]= - INFINITY ; 9045 } 9046 } 9047 } 9048 9049if (logit_softcap != 0.0f ) { 9050ggml_vec_tanh_f32 (Q_TILE_SZ * KV_TILE_SZ ,KQ ,KQ ); 9051ggml_vec_scale_f32 (Q_TILE_SZ * KV_TILE_SZ ,KQ ,logit_softcap ); 9052 } 9053 9054if (mask ) { 9055ggml_vec_add_f32 (tile_rows * KV_TILE_SZ ,KQ ,KQ ,mask32 ); 9056 } 9057 9058bool skip [Q_TILE_SZ ]= {}; 9059 9060for (int tq = 0 ;tq < Q_TILE_SZ ;tq ++ ) { 9061float * kq_row = KQ + tq * KV_TILE_SZ ; 9062 9063float tile_max ; 9064ggml_vec_max_f32 (KV_TILE_SZ ,& tile_max ,kq_row ); 9065 9066if (tile_max == - INFINITY ) { 9067skip [tq ]= true; 9068continue ; 9069 } 9070 9071const float Mold = M [tq ]; 9072const float Mnew = fmaxf (Mold ,tile_max ); 9073 9074if (Mnew > Mold ) { 9075const float ms = expf (Mold - Mnew ); 9076ggml_vec_scale_f32 (DV ,VKQ32 + tq * DV ,ms ); 9077S [tq ] *=ms ; 9078 } 9079M [tq ]= Mnew ; 9080 9081 9082S [tq ]+= ggml_vec_soft_max_f32 (KV_TILE_SZ ,kq_row ,kq_row ,Mnew ); 9083 } 9084 9085// V accumulation: VKQ32 += softmax(KQ) * V 9086// Pack V tile to contiguous F32, zero-padded 9087for (int tk = 0 ;tk < kv_tile ;tk ++ ) { 9088const char * v_data = (const char * )v -> data + (ic + tk )* nbv1 + iv2 * nbv2 + iv3 * nbv3 ; 9089if (kv_type == GGML_TYPE_F16 ) { 9090ggml_cpu_fp16_to_fp32 ((const ggml_fp16_t * )v_data ,V32 + tk * DV ,DV ); 9091 }else { 9092memcpy (V32 + tk * DV ,v_data ,DV * sizeof (float )); 9093 } 9094 } 9095for (int tq = 0 ;tq < Q_TILE_SZ ;tq ++ ) { 9096if (skip [tq ]) { 9097memset (KQ + tq * KV_TILE_SZ ,0 ,KV_TILE_SZ * sizeof (float )); 9098 } 9099 } 9100simd_gemm (VKQ32 ,KQ ,V32 ,Q_TILE_SZ ,KV_TILE_SZ ,DV ); 9101 } 9102 9103// sinks (apply only to valid rows in the tile) 9104if (sinks ) { 9105const float s = ((float * )((char * )sinks -> data ))[h ]; 9106 9107for (int tq = 0 ;tq < tile_rows ;tq ++ ) { 9108float ms = 1.0f ; 9109float vs = 1.0f ; 9110 9111if (s > M [tq ]) { 9112ms = expf (M [tq ]- s ); 9113ggml_vec_scale_f32 (DV ,VKQ32 + tq * DV ,ms ); 9114 }else { 9115vs = expf (s - M [tq ]); 9116 } 9117 9118S [tq ]= S [tq ]* ms + vs ; 9119 } 9120 } 9121 9122for (int tq = 0 ;tq < tile_rows ;tq ++ ) { 9123// V /= S 9124const float S_inv = S [tq ]== 0.0f ?0.0f :1.0f /S [tq ]; 9125ggml_vec_scale_f32 (DV ,VKQ32 + tq * DV ,S_inv ); 9126 9127// dst indices 9128const int i1 = iq1 + tq ; 9129const int i2 = iq2 ; 9130const int i3 = iq3 ; 9131 9132// permute(0, 2, 1, 3) 9133memcpy ((char * )dst -> data + (i3 * ne2 * ne1 + i2 + i1 * ne1 )* nb1 ,VKQ32 + tq * DV ,nb1 ); 9134 } 9135 9136ir += tile_rows ; 9137 } 9138} 9139 9140// Reduction function: combines partial results across KV chunks 9141// Partials layout in wdata: [n_q_heads][n_chunks][2 + DV] 9142static void ggml_flash_attn_ext_reduce_partials ( 9143const ggml_compute_params * params , 9144ggml_tensor * dst , 9145const int64_t n_chunks , 9146const int64_t chunk_size ) { 9147 9148const ggml_tensor * q = dst -> src [0 ]; 9149const ggml_tensor * k = dst -> src [1 ]; 9150const ggml_tensor * v = dst -> src [2 ]; 9151 9152const int64_t DK = k -> ne [0 ]; 9153const int64_t DV = v -> ne [0 ]; 9154const int64_t nek1 = k -> ne [1 ]; 9155const int64_t n_q_heads = q -> ne [2 ]; 9156 9157const int ith = params -> ith ; 9158const int nth = params -> nth ; 9159 9160const int64_t wdata_per_thread = DK + 2 * DV + CACHE_LINE_SIZE_F32 ; 9161float * thread_wdata = (float * )params -> wdata + ith * wdata_per_thread ; 9162 9163const int64_t partials_offset = nth * (DK + 2 * DV + CACHE_LINE_SIZE_F32 ); 9164const int64_t partial_size = 2 + DV ; 9165const float * partials_base = (const float * )params -> wdata + partials_offset ; 9166 9167// Output layout 9168const int64_t ne1 = dst -> ne [1 ]; 9169const int64_t ne2 = dst -> ne [2 ]; 9170const size_t nb1 = dst -> nb [1 ]; 9171 9172// Each thread reduces a subset of query heads 9173for (int64_t q_head = ith ;q_head < n_q_heads ;q_head += nth ) { 9174float M_final = - INFINITY ; 9175float S_final = 0.0f ; 9176float * VKQ_final = thread_wdata ; 9177memset (VKQ_final ,0 ,DV * sizeof (float )); 9178 9179// Combine partials from all chunks 9180for (int64_t chunk_idx = 0 ;chunk_idx < n_chunks ;++ chunk_idx ) { 9181const int64_t ic_start = chunk_idx * chunk_size ; 9182if (ic_start >=nek1 )continue ; 9183 9184const float * partial = partials_base + (q_head * n_chunks + chunk_idx )* partial_size ; 9185const float M_chunk = partial [0 ]; 9186const float S_chunk = partial [1 ]; 9187const float * VKQ_chunk = partial + 2 ; 9188 9189if (S_chunk == 0.0f )continue ; 9190 9191const float M_new = fmaxf (M_final ,M_chunk ); 9192const float scale_old = expf (M_final - M_new ); 9193const float scale_new = expf (M_chunk - M_new ); 9194 9195for (int64_t d = 0 ;d < DV ;++ d ) { 9196VKQ_final [d ]= VKQ_final [d ]* scale_old + VKQ_chunk [d ]* scale_new ; 9197 } 9198S_final = S_final * scale_old + S_chunk * scale_new ; 9199M_final = M_new ; 9200 } 9201 9202// Normalize and write to output 9203if (S_final != 0.0f ) { 9204const float S_inv = 1.0f /S_final ; 9205ggml_vec_scale_f32 (DV ,VKQ_final ,S_inv ); 9206 } 9207// iq1=0, iq3=0 for decode 9208memcpy ((char * )dst -> data + (0 * ne2 * ne1 + q_head + 0 * ne1 )* nb1 ,VKQ_final ,nb1 ); 9209 } 9210} 9211 9212static void ggml_compute_forward_flash_attn_ext_f16 ( 9213const ggml_compute_params * params , 9214ggml_tensor * dst ) { 9215 9216const ggml_tensor * q = dst -> src [0 ]; 9217const ggml_tensor * k = dst -> src [1 ]; 9218const ggml_tensor * v = dst -> src [2 ]; 9219 9220GGML_TENSOR_LOCALS (int64_t ,neq ,q ,ne ) 9221GGML_TENSOR_LOCALS (size_t ,nbq ,q ,nb ) 9222GGML_TENSOR_LOCALS (int64_t ,nek ,k ,ne ) 9223GGML_TENSOR_LOCALS (size_t ,nbk ,k ,nb ) 9224GGML_TENSOR_LOCALS (int64_t ,nev ,v ,ne ) 9225GGML_TENSOR_LOCALS (size_t ,nbv ,v ,nb ) 9226GGML_TENSOR_LOCALS (int64_t ,ne ,dst ,ne ) 9227GGML_TENSOR_LOCALS (size_t ,nb ,dst ,nb ) 9228 9229const int64_t DK = nek0 ; 9230const int64_t DV = nev0 ; 9231const int64_t N = neq1 ; 9232 9233 9234GGML_ASSERT (ne0 == DV ); 9235GGML_ASSERT (ne2 == N ); 9236 9237// input tensor rows must be contiguous 9238GGML_ASSERT (nbq0 == ggml_type_size (q -> type )); 9239GGML_ASSERT (nbk0 == ggml_type_size (k -> type )); 9240GGML_ASSERT (nbv0 == ggml_type_size (v -> type )); 9241 9242GGML_ASSERT (neq0 == DK ); 9243GGML_ASSERT (nek0 == DK ); 9244GGML_ASSERT (nev0 == DV ); 9245 9246GGML_ASSERT (neq1 == N ); 9247 9248// dst cannot be transposed or permuted 9249GGML_ASSERT (nb0 == sizeof (float )); 9250GGML_ASSERT (nb0 <=nb1 ); 9251GGML_ASSERT (nb1 <=nb2 ); 9252GGML_ASSERT (nb2 <=nb3 ); 9253 9254const int ith = params -> ith ; 9255const int nth = params -> nth ; 9256 9257// When use_ref is set, force the vec-only reference implementation (no tiling, no KV-chunking) 9258const bool use_ref = params -> use_ref ; 9259 9260const bool kv_is_f32_or_f16 = (k -> type == GGML_TYPE_F32 || k -> type == GGML_TYPE_F16 ); 9261const 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 ; 9262 9263if (use_split_kv_path ) { 9264const int64_t chunk_size = (nek1 + nth - 1 ) /nth ; 9265 9266// Partials buffer layout: [q_head][kv_chunk][M, S, VKQ] 9267const int64_t partial_size = 2 + DV ; 9268float * partials_base = (float * )params -> wdata + nth * (DK + 2 * DV + CACHE_LINE_SIZE_F32 ); 9269 9270const int64_t ic_start = ith * chunk_size ; 9271const int64_t ic_end = std::min (ic_start + chunk_size ,nek1 ); 9272 9273const int64_t partial_stride = nth * partial_size ; 9274float * chunk_partials = partials_base + ith * partial_size ; 9275 9276if (ic_start < nek1 ) { 9277for (int64_t q_head = 0 ;q_head < neq2 ;q_head ++ ) { 9278ggml_compute_forward_flash_attn_ext_f16_one_chunk ( 9279params ,dst ,q_head ,q_head + 1 ,ic_start ,ic_end , 9280chunk_partials ,partial_stride ); 9281 } 9282 }else { 9283for (int64_t q_head = 0 ;q_head < neq2 ;q_head ++ ) { 9284float * q_partials = chunk_partials + q_head * partial_stride ; 9285q_partials [0 ]= - INFINITY ;// M 9286q_partials [1 ]= 0.0f ;// S 9287 } 9288 } 9289 9290ggml_barrier (params -> threadpool ); 9291ggml_flash_attn_ext_reduce_partials (params ,dst ,nth ,chunk_size ); 9292 }else { 9293 9294// total rows in q 9295const int64_t nr = neq1 * neq2 * neq3 ; 9296 9297// disable for NUMA 9298const bool disable_chunking = ggml_is_numa (); 9299 9300// 4x chunks per thread 9301int nth_scaled = nth * 4 ; 9302int64_t chunk_size = (nr + nth_scaled - 1 ) /nth_scaled ; 9303int64_t nchunk = (nr + chunk_size - 1 ) /chunk_size ; 9304 9305if (nth == 1 || nchunk < nth || disable_chunking ) { 9306nchunk = nth ; 9307 } 9308 9309if (ith == 0 ) { 9310ggml_threadpool_chunk_set (params -> threadpool ,nth ); 9311 } 9312 9313ggml_barrier (params -> threadpool ); 9314 9315const int64_t dr = (nr + nchunk - 1 ) /nchunk ; 9316 9317static constexpr int64_t Q_TILE_SZ = ggml_fa_tile_config::Q ; 9318bool use_tiled = !use_ref && 9319 (q -> type == GGML_TYPE_F32 && 9320kv_is_f32_or_f16 && 9321k -> type == v -> type && 9322neq1 >=Q_TILE_SZ ); 9323#ifdef GGML_SIMD 9324#if defined(__ARM_FEATURE_SVE ) 9325const int64_t f32_epr = svcntw (); 9326#else 9327const int64_t f32_epr = GGML_F32_EPR ; 9328#endif 9329use_tiled &= (DV %f32_epr == 0 ); 9330#endif 9331int current_chunk = ith ; 9332 9333while (current_chunk < nchunk ) { 9334const int64_t ir0 = dr * current_chunk ; 9335const int64_t ir1 = MIN (ir0 + dr ,nr ); 9336 9337if (use_tiled ) { 9338ggml_compute_forward_flash_attn_ext_tiled (params ,dst ,ir0 ,ir1 ); 9339 }else { 9340ggml_compute_forward_flash_attn_ext_f16_one_chunk (params ,dst ,ir0 ,ir1 ,0 ,nek1 ,nullptr ,0 ); 9341 } 9342 9343current_chunk = ggml_threadpool_chunk_add (params -> threadpool ,1 ); 9344 } 9345 } 9346} 9347 9348void ggml_compute_forward_flash_attn_ext ( 9349const ggml_compute_params * params , 9350ggml_tensor * dst ) { 9351switch (dst -> op_params [3 ]) { 9352case GGML_PREC_DEFAULT : 9353case GGML_PREC_F32 : 9354 { 9355// uses F32 accumulators 9356ggml_compute_forward_flash_attn_ext_f16 (params ,dst ); 9357 }break ; 9358default : 9359 { 9360GGML_ABORT ("fatal error" ); 9361 } 9362 } 9363} 9364 9365// ggml_compute_forward_flash_attn_back 9366 9367static void ggml_compute_forward_flash_attn_back_f32 ( 9368const ggml_compute_params * params , 9369const bool masked , 9370ggml_tensor * dst ) { 9371 9372const ggml_tensor * q = dst -> src [0 ]; 9373const ggml_tensor * k = dst -> src [1 ]; 9374const ggml_tensor * v = dst -> src [2 ]; 9375const ggml_tensor * d = dst -> src [3 ]; 9376 9377GGML_TENSOR_LOCALS (int64_t ,neq ,q ,ne ) 9378GGML_TENSOR_LOCALS (size_t ,nbq ,q ,nb ) 9379GGML_TENSOR_LOCALS (int64_t ,nek ,k ,ne ) 9380GGML_TENSOR_LOCALS (size_t ,nbk ,k ,nb ) 9381GGML_TENSOR_LOCALS (int64_t ,nev ,v ,ne ) 9382GGML_TENSOR_LOCALS (size_t ,nbv ,v ,nb ) 9383GGML_TENSOR_LOCALS (int64_t ,ned ,d ,ne ) 9384GGML_TENSOR_LOCALS (size_t ,nbd ,d ,nb ) 9385GGML_TENSOR_LOCALS (int64_t ,ne ,dst ,ne ) 9386GGML_TENSOR_LOCALS (size_t ,nb ,dst ,nb ) 9387 9388const int ith = params -> ith ; 9389const int nth = params -> nth ; 9390 9391const int64_t D = neq0 ; 9392const int64_t N = neq1 ; 9393const int64_t P = nek1 - N ; 9394const int64_t M = P + N ; 9395 9396const int Mup = ggml_up (M ,GGML_SOFT_MAX_UNROLL ); 9397const int mxDM = MAX (D ,Mup ); 9398 9399// GGML_ASSERT(ne0 == D); 9400// GGML_ASSERT(ne1 == N); 9401GGML_ASSERT (P >=0 ); 9402 9403GGML_ASSERT (nbq0 == sizeof (float )); 9404GGML_ASSERT (nbk0 == sizeof (float )); 9405GGML_ASSERT (nbv0 == sizeof (float )); 9406 9407GGML_ASSERT (neq0 == D ); 9408GGML_ASSERT (nek0 == D ); 9409GGML_ASSERT (nev1 == D ); 9410GGML_ASSERT (ned0 == D ); 9411 9412GGML_ASSERT (neq1 == N ); 9413GGML_ASSERT (nek1 == N + P ); 9414GGML_ASSERT (nev1 == D ); 9415GGML_ASSERT (ned1 == N ); 9416 9417// dst cannot be transposed or permuted 9418GGML_ASSERT (nb0 == sizeof (float )); 9419GGML_ASSERT (nb0 <=nb1 ); 9420GGML_ASSERT (nb1 <=nb2 ); 9421GGML_ASSERT (nb2 <=nb3 ); 9422 9423if (ith == 0 ) { 9424memset (dst -> data ,0 ,nb0 * ne0 * ne1 * ne2 * ne3 ); 9425 } 9426ggml_barrier (params -> threadpool ); 9427 9428const int64_t elem_q = ggml_nelements (q ); 9429const int64_t elem_k = ggml_nelements (k ); 9430 9431ggml_type result_type = dst -> type ; 9432GGML_ASSERT (ggml_blck_size (result_type )== 1 ); 9433const size_t tsize = ggml_type_size (result_type ); 9434 9435const size_t offs_q = 0 ; 9436const size_t offs_k = offs_q + GGML_PAD (elem_q * tsize ,GGML_MEM_ALIGN ); 9437const size_t offs_v = offs_k + GGML_PAD (elem_k * tsize ,GGML_MEM_ALIGN ); 9438 9439void * grad_q = (char * )dst -> data ; 9440void * grad_k = (char * )dst -> data + offs_k ; 9441void * grad_v = (char * )dst -> data + offs_v ; 9442 9443const size_t nbgq1 = nb0 * neq0 ; 9444const size_t nbgq2 = nb0 * neq0 * neq1 ; 9445const size_t nbgq3 = nb0 * neq0 * neq1 * neq2 ; 9446 9447const size_t nbgk1 = nb0 * nek0 ; 9448const size_t nbgk2 = nb0 * nek0 * nek1 ; 9449const size_t nbgk3 = nb0 * nek0 * nek1 * neq2 ; 9450 9451const size_t nbgv1 = nb0 * nev0 ; 9452const size_t nbgv2 = nb0 * nev0 * nev1 ; 9453const size_t nbgv3 = nb0 * nev0 * nev1 * neq2 ; 9454 9455// parallelize by k rows using ggml_vec_dot_f32 9456 9457// total rows in k 9458const int nr = nek2 * nek3 ; 9459 9460// rows per thread 9461const int dr = (nr + nth - 1 )/nth ; 9462 9463// row range for this thread 9464const int ir0 = dr * ith ; 9465const int ir1 = MIN (ir0 + dr ,nr ); 9466 9467const float scale = 1.0f /sqrtf (D ); 9468 9469//printf("P=%d N=%d D=%d ir0=%d ir1=%d scale = %f\n", P, N, D, ir0, ir1, scale); 9470 9471// how often k2 (and v2) is repeated in q2 9472int nrep = neq2 /nek2 ; 9473 9474for (int ir = ir0 ;ir < ir1 ;++ ir ) { 9475// q indices 9476const int ik3 = ir /(nek2 ); 9477const int ik2 = ir - ik3 * nek2 ; 9478 9479const int iq3 = ik3 ; 9480const int id3 = ik3 ; 9481const int iv3 = ik3 ; 9482const int iv2 = ik2 ; 9483 9484for (int irep = 0 ;irep < nrep ;++ irep ) { 9485const int iq2 = ik2 + irep * nek2 ; 9486const int id2 = iq2 ; 9487 9488// (ik2 + irep*nek2) % nek2 == ik2 9489for (int iq1 = 0 ;iq1 < neq1 ;++ iq1 ) { 9490const int id1 = iq1 ; 9491 9492// not sure about CACHE_LINE_SIZE_F32.. 9493// - maybe it must not be multiplied by 2 and excluded from .. in SM 1*(..) offset? 9494float * S = (float * )params -> wdata + ith * 2 * (mxDM + CACHE_LINE_SIZE_F32 )+ 0 * (mxDM + CACHE_LINE_SIZE_F32 ); 9495float * SM = (float * )params -> wdata + ith * 2 * (mxDM + CACHE_LINE_SIZE_F32 )+ 1 * (mxDM + CACHE_LINE_SIZE_F32 ); 9496 9497for (int i = M ;i < Mup ;++ i ) { 9498S [i ]= - INFINITY ; 9499 } 9500 9501const int64_t masked_begin = masked ? (P + iq1 + 1 ) :M ; 9502for (int64_t ic = 0 ;ic < masked_begin ;++ ic ) { 9503// k indices 9504const int ik1 = ic ; 9505 9506// S indices 9507const int i1 = ik1 ; 9508 9509ggml_vec_dot_f32 (neq0 , 9510S + i1 ,0 , 9511 (float * ) ((char * )k -> data + (ik1 * nbk1 + ik2 * nbk2 + ik3 * nbk3 )),0 , 9512 (float * ) ((char * )q -> data + (iq1 * nbq1 + iq2 * nbq2 + iq3 * nbq3 )),0 ,1 ); 9513 } 9514 9515// scale 9516ggml_vec_scale_f32 (masked_begin ,S ,scale ); 9517 9518for (int64_t i = masked_begin ;i < M ;i ++ ) { 9519S [i ]= - INFINITY ; 9520 } 9521 9522// softmax 9523// exclude known -INF S[..] values from max and loop 9524// dont forget to set their SM values to zero 9525 { 9526float max = - INFINITY ; 9527ggml_vec_max_f32 (masked_begin ,& max ,S ); 9528 9529ggml_float sum = 0.0 ; 9530 { 9531#ifdef GGML_SOFT_MAX_ACCELERATE 9532max = - max ; 9533vDSP_vsadd (SM ,1 ,& max ,SM ,1 ,Mup ); 9534vvexpf (SM ,SM ,& Mup ); 9535ggml_vec_sum_f32 (Mup ,& sum ,SM ); 9536#else 9537sum = ggml_vec_soft_max_f32 (Mup ,SM ,S ,max ); 9538#endif 9539 } 9540 9541assert (sum > 0.0 ); 9542 9543sum = 1.0 /sum ; 9544ggml_vec_scale_f32 (masked_begin ,SM ,sum ); 9545 9546 } 9547 9548// step-by-step explanation 9549 { 9550// forward-process shape grads from backward process 9551// parallel_for ik2,ik3: 9552// for irep: 9553// iq2 = ik2 + irep*nek2 9554// k[:D,:M,:,:] [D,M,:,:] grad[k][:D,:M,ik2,ik3] += grad[kcur] 9555// q[:D,:N,:,:] [D,N,:,:] grad[q][:D,iq1,iq2,iq3] += grad[qcur] 9556// v[:M,:D,:,:] [M,D,:,:] grad[v][:M,:D,iv2,iv3] += grad[vcur] 9557// for iq1: 9558// kcur = k[:D,:M,ik2,ik3] [D,M,1,1] grad[kcur] = grad[S1].T @ qcur 9559// qcur = q[:D,iq1,iq2,iq3] [D,1,1,1] grad[qcur] = grad[S1] @ kcur 9560// vcur = v[:M,:D,iv2,iv3] [M,D,1,1] grad[vcur] = grad[S5].T @ S4 9561// S0 = -Inf [D,1,1,1] 9562// ~S1[i] = dot(kcur[:D,i], qcur) 9563// S1 = qcur @ kcur.T [M,1,1,1] grad[S1] = grad[S2] * scale 9564// S2 = S1 * scale [M,1,1,1] grad[S2] = diag_mask_zero(grad[S3], P) 9565// S3 = diag_mask_inf(S2, P) [M,1,1,1] grad[S3] = S4 * (grad[S4] - dot(S4, grad[S4])) 9566// S4 = softmax(S3) [M,1,1,1] grad[S4] = grad[S5] @ vcur 9567// ~S5[i] = dot(vcur[:,i], S4) 9568// S5 = S4 @ vcur.T [D,1,1,1] grad[S5] = d[:D,id1,id2,id3] 9569// ~dst[i,iq1,iq2,iq3] = S5[i] ^ 9570// dst[:D,iq1,iq2,iq3] = S5 | grad[dst[:D,iq1,iq2,iq3]] = d[:D,id1,id2,id3] 9571// dst backward-/ grad[dst] = d 9572// 9573// output gradients with their dependencies: 9574// 9575// grad[kcur] = grad[S1].T @ qcur 9576// grad[S1] = diag_mask_zero(grad[S3], P) * scale 9577// grad[S3] = S4 * (grad[S4] - dot(S4, grad[S4])) 9578// grad[S4] = grad[S5] @ vcur 9579// grad[S4] = d[:D,id1,id2,id3] @ vcur 9580// grad[qcur] = grad[S1] @ kcur 9581// grad[vcur] = grad[S5].T @ S4 9582// grad[vcur] = d[:D,id1,id2,id3].T @ S4 9583// 9584// in post-order: 9585// 9586// S1 = qcur @ kcur.T 9587// S2 = S1 * scale 9588// S3 = diag_mask_inf(S2, P) 9589// S4 = softmax(S3) 9590// grad[S4] = d[:D,id1,id2,id3] @ vcur 9591// grad[S3] = S4 * (grad[S4] - dot(S4, grad[S4])) 9592// grad[S1] = diag_mask_zero(grad[S3], P) * scale 9593// grad[qcur] = grad[S1] @ kcur 9594// grad[kcur] = grad[S1].T @ qcur 9595// grad[vcur] = d[:D,id1,id2,id3].T @ S4 9596// 9597// using less variables (SM=S4): 9598// 9599// S = diag_mask_inf(qcur @ kcur.T * scale, P) 9600// SM = softmax(S) 9601// S = d[:D,iq1,iq2,iq3] @ vcur 9602// dot_SM_gradSM = dot(SM, S) 9603// S = SM * (S - dot(SM, S)) 9604// S = diag_mask_zero(S, P) * scale 9605// 9606// grad[q][:D,iq1,iq2,iq3] += S @ kcur 9607// grad[k][:D,:M,ik2,ik3] += S.T @ qcur 9608// grad[v][:M,:D,iv2,iv3] += d[:D,id1,id2,id3].T @ SM 9609 } 9610 9611// S = gradSM = d[:D,id1,id2,id3] @ vcur[:,:,iv2,iv3] 9612// S = d[:D,id1,id2,id3] @ vcur[:,:,iv2,iv3] 9613// for ic: 9614// S[:M] += vcur[:M,ic,iv2,iv3] * d[ic,id1,id2,id3] 9615// exclude known future zero S[..] values from operation 9616ggml_vec_set_f32 (masked_begin ,S ,0 ); 9617for (int64_t ic = 0 ;ic < D ;++ ic ) { 9618ggml_vec_mad_f32 (masked_begin , 9619S , 9620 (float * ) ((char * )v -> data + (ic * nbv1 + iv2 * nbv2 + iv3 * nbv3 )), 9621* (float * ) ((char * )d -> data + (ic * nbd0 + id1 * nbd1 + id2 * nbd2 + id3 * nbd3 ))); 9622 } 9623 9624// S = SM * (S - dot(SM, S)) 9625float dot_SM_gradSM = 0 ; 9626ggml_vec_dot_f32 (masked_begin ,& dot_SM_gradSM ,0 ,SM ,0 ,S ,0 ,1 ); 9627ggml_vec_acc1_f32 (M ,S ,- dot_SM_gradSM ); 9628ggml_vec_mul_f32 (masked_begin ,S ,S ,SM ); 9629 9630// S = diag_mask_zero(S, P) * scale 9631// already done by above ggml_vec_set_f32 9632 9633// exclude known zero S[..] values from operation 9634ggml_vec_scale_f32 (masked_begin ,S ,scale ); 9635 9636// S shape [M,1] 9637// SM shape [M,1] 9638// kcur shape [D,M] 9639// qcur shape [D,1] 9640// vcur shape [M,D] 9641 9642// grad[q][:D,iq1,iq2,iq3] += S @ kcur 9643// grad[q][:D,iq1,iq2,iq3] += shape[M,1] @ shape[D,M] 9644// for ic: 9645// grad[q][:D,iq1,iq2,iq3] += S[ic] * kcur[:D,ic,ik2,ik3] 9646// exclude known zero S[..] values from loop 9647for (int64_t ic = 0 ;ic < masked_begin ;++ ic ) { 9648ggml_vec_mad_f32 (D , 9649 (float * ) ((char * )grad_q + (iq1 * nbgq1 + iq2 * nbgq2 + iq3 * nbgq3 )), 9650 (float * ) ((char * )k -> data + (ic * nbk1 + ik2 * nbk2 + ik3 * nbk3 )), 9651S [ic ]); 9652 } 9653 9654// grad[k][:D,:M,iq2,iq3] += S.T @ qcur 9655// for ic: 9656// grad[k][:D,ic,iq2,iq3] += S.T[0,ic] * qcur[:D,0] 9657// grad[k][:D,ic,iq2,iq3] += S[ic] * qcur[:D,0] 9658// exclude known zero S[..] values from loop 9659for (int64_t ic = 0 ;ic < masked_begin ;++ ic ) { 9660ggml_vec_mad_f32 (D , 9661 (float * ) ((char * )grad_k + (ic * nbgk1 + ik2 * nbgk2 + ik3 * nbgk3 )), 9662 (float * ) ((char * )q -> data + (iq1 * nbq1 + iq2 * nbq2 + iq3 * nbq3 )), 9663S [ic ]); 9664 } 9665 9666// grad[v][:M,:D,iv2,iv3] += d[:D,id1,id2,id3].T @ SM 9667// for ic: 9668// grad[v][:M,ic,iv2,iv3] += d[:D,id1,id2,id3].T[0,ic] * SM[:M] 9669// grad[v][:M,ic,iv2,iv3] += d[ic,id1,id2,id3] * SM[:M] 9670// exclude known zero SM[..] values from mad 9671for (int64_t ic = 0 ;ic < D ;++ ic ) { 9672ggml_vec_mad_f32 (masked_begin , 9673 (float * ) ((char * )grad_v + (ic * nbgv1 + iv2 * nbgv2 + iv3 * nbgv3 )), 9674SM , 9675* (float * ) ((char * )d -> data + (ic * nbd0 + id1 * nbd1 + id2 * nbd2 + id3 * nbd3 ))); 9676 } 9677 } 9678 } 9679 } 9680} 9681 9682void ggml_compute_forward_flash_attn_back ( 9683const ggml_compute_params * params , 9684const bool masked , 9685ggml_tensor * dst ) { 9686 9687const ggml_tensor * q = dst -> src [0 ]; 9688 9689switch (q -> type ) { 9690case GGML_TYPE_F32 : 9691 { 9692ggml_compute_forward_flash_attn_back_f32 (params ,masked ,dst ); 9693 }break ; 9694default : 9695 { 9696GGML_ABORT ("fatal error" ); 9697 } 9698 } 9699} 9700 9701// ggml_compute_forward_ssm_conv 9702 9703static void ggml_compute_forward_ssm_conv_f32 ( 9704const ggml_compute_params * params , 9705ggml_tensor * dst ) { 9706const ggml_tensor * src0 = dst -> src [0 ];// conv_x 9707const ggml_tensor * src1 = dst -> src [1 ];// conv1d.weight 9708 9709const int ith = params -> ith ; 9710const int nth = params -> nth ; 9711 9712const int nc = src1 -> ne [0 ];// d_conv 9713const int ncs = src0 -> ne [0 ];// d_conv - 1 + n_t 9714const int nr = src0 -> ne [1 ];// d_inner 9715const int n_t = dst -> ne [1 ];// tokens per sequence 9716const int n_s = dst -> ne [2 ];// number of sequences in the batch 9717 9718GGML_ASSERT (dst -> ne [0 ]== nr ); 9719GGML_ASSERT (src0 -> nb [0 ]== sizeof (float )); 9720GGML_ASSERT (src1 -> nb [0 ]== sizeof (float )); 9721GGML_ASSERT (src0 -> nb [1 ]== src0 -> ne [0 ]* sizeof (float )); 9722 9723// rows per thread 9724const int dr = (nr + nth - 1 )/nth ; 9725 9726// row range for this thread 9727const int ir0 = dr * ith ; 9728const int ir1 = MIN (ir0 + dr ,nr ); 9729const int ir = ir1 - ir0 ; 9730 9731for (int i3 = 0 ;i3 < n_s ;++ i3 ) { 9732for (int i2 = 0 ;i2 < n_t ;++ i2 ) { 9733// {d_conv - 1 + n_t, d_inner, n_seqs} 9734// sliding window 9735const 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} 9736const float * c = (const float * ) ((const char * )src1 -> data + ir0 * (src1 -> nb [1 ]));// {d_conv, d_inner} 9737float * x = (float * ) ((char * )dst -> data + ir0 * (dst -> nb [0 ])+ i2 * (dst -> nb [1 ])+ i3 * (dst -> nb [2 ]));// {d_inner, n_t, n_s} 9738 9739// TODO: transpose the output for smaller strides for big batches? 9740// d_inner 9741for (int i1 = 0 ;i1 < ir ;++ i1 ) { 9742// rowwise dot product 9743// NOTE: not using ggml_vec_dot_f32, because its sum is in double precision 9744float sumf = 0.0f ; 9745 9746// d_conv 9747for (int i0 = 0 ;i0 < nc ;++ i0 ) { 9748sumf += s [i0 + i1 * ncs ]* c [i0 + i1 * nc ]; 9749 } 9750x [i1 ]= sumf ; 9751 } 9752 } 9753 } 9754} 9755 9756void ggml_compute_forward_ssm_conv ( 9757const ggml_compute_params * params , 9758ggml_tensor * dst ) { 9759switch (dst -> src [0 ]-> type ) { 9760case GGML_TYPE_F32 : 9761 { 9762ggml_compute_forward_ssm_conv_f32 (params ,dst ); 9763 }break ; 9764default : 9765 { 9766GGML_ABORT ("fatal error" ); 9767 } 9768 } 9769} 9770 9771// ggml_compute_forward_ssm_scan 9772 9773static void ggml_compute_forward_ssm_scan_f32 ( 9774const ggml_compute_params * params , 9775ggml_tensor * dst ) { 9776const ggml_tensor * src0 = dst -> src [0 ];// s {d_state, dim, n_head, n_seqs+} 9777const ggml_tensor * src1 = dst -> src [1 ];// x {dim, n_head, n_seq_tokens, n_seqs} 9778const ggml_tensor * src2 = dst -> src [2 ];// dt {n_head, n_seq_tokens, n_seqs} 9779const ggml_tensor * src3 = dst -> src [3 ];// A {d_state, n_head} or {1, n_head} 9780const ggml_tensor * src4 = dst -> src [4 ];// B {d_state, n_group, n_seq_tokens, n_seqs} 9781const ggml_tensor * src5 = dst -> src [5 ];// C {d_state, n_group, n_seq_tokens, n_seqs} 9782const ggml_tensor * src6 = dst -> src [6 ];// ids {n_seqs} 9783 9784const int ith = params -> ith ; 9785const int nth = params -> nth ; 9786 9787const int64_t nc = src0 -> ne [0 ];// d_state 9788const int64_t nr = src0 -> ne [1 ];// dim 9789const int64_t nh = src1 -> ne [1 ];// n_head 9790const int64_t ng = src4 -> ne [1 ]; 9791const int64_t nt = src1 -> ne [2 ];// number of tokens per sequence 9792const int64_t ns = src1 -> ne [3 ];// number of sequences in the batch 9793const int64_t K = ggml_get_op_params_i32 (dst ,0 ); 9794 9795// can't use ggml_nbytes because src1 is not necessarily contiguous 9796const int64_t s_off = ggml_nelements (src1 )* ggml_element_size (src1 ); 9797 9798GGML_ASSERT (K >=1 ); 9799GGML_ASSERT (ggml_nelements (src1 )+ K * nc * nr * nh * ns == ggml_nelements (dst )); 9800GGML_ASSERT (src0 -> nb [0 ]== sizeof (float )); 9801GGML_ASSERT (src1 -> nb [0 ]== sizeof (float )); 9802GGML_ASSERT (src2 -> nb [0 ]== sizeof (float )); 9803GGML_ASSERT (src3 -> nb [0 ]== sizeof (float )); 9804GGML_ASSERT (src4 -> nb [0 ]== sizeof (float )); 9805GGML_ASSERT (src5 -> nb [0 ]== sizeof (float )); 9806GGML_ASSERT (src6 -> nb [0 ]== sizeof (int32_t )); 9807GGML_ASSERT (nh %ng == 0 ); 9808GGML_ASSERT (src3 -> ne [0 ]== 1 || K == 1 ); 9809 9810// heads per thread 9811const int dh = (nh + nth - 1 )/nth ; 9812 9813// head range for this thread 9814const int ih0 = dh * ith ; 9815const int ih1 = MIN (ih0 + dh ,nh ); 9816 9817const int32_t * ids = (const int32_t * )src6 -> data ; 9818 9819for (int i3 = 0 ;i3 < ns ;++ i3 ) { 9820const float * s0 = (const float * ) ((const char * )src0 -> data + ids [i3 ]* (src0 -> nb [3 ]));// {d_state, dim, nh, ns} 9821float * s = (float * ) ((char * )dst -> data + i3 * (src0 -> nb [3 ])+ s_off );// {d_state, dim, nh, ns} 9822 9823for (int i2 = 0 ;i2 < nt ;++ i2 ) { 9824const float * x = (const float * ) ((const char * )src1 -> data + i2 * (src1 -> nb [2 ])+ i3 * (src1 -> nb [3 ]));// {dim, nh, nt, ns} 9825const float * dt = (const float * ) ((const char * )src2 -> data + i2 * (src2 -> nb [1 ])+ i3 * (src2 -> nb [2 ]));// {nh, nt, ns} 9826const float * A = (const float * ) ((const char * )src3 -> data );// {d_state, nh} or {1, nh} 9827const float * B = (const float * ) ((const char * )src4 -> data + i2 * (src4 -> nb [2 ])+ i3 * (src4 -> nb [3 ]));// {d_state, ng, nt, ns} 9828const float * C = (const float * ) ((const char * )src5 -> data + i2 * (src5 -> nb [2 ])+ i3 * (src5 -> nb [3 ]));// {d_state, ng, nt, ns} 9829float * y = (float * ) ((char * )dst -> data + i2 * (nh * nr * sizeof (float ))+ i3 * (nt * nh * nr * sizeof (float )));// {dim, nh, nt, ns} 9830 9831if (src3 -> ne [0 ]== 1 ) { 9832// Mamba-2 has a scalar decay factor per head; dA can be outside the state-wise loop 9833 9834// n_head 9835for (int h = ih0 ;h < ih1 ;++ h ) { 9836// ref: https://github.com/state-spaces/mamba/blob/62db608da60f6fc790b8ed9f4b3225e95ca15fde/mamba_ssm/ops/triton/softplus.py#L16 9837const float dt_soft_plus = ggml_compute_softplus_f32 (dt [h ]); 9838const float dA = expf (dt_soft_plus * A [h ]); 9839const int g = h / (nh /ng );// repeat_interleave 9840 9841// dim 9842for (int i1 = 0 ;i1 < nr ;++ i1 ) { 9843const int ii = i1 + h * nr ; 9844const float x_dt = x [ii ]* dt_soft_plus ; 9845float sumf = 0.0f ; 9846#if defined(GGML_SIMD ) 9847#if defined(__ARM_FEATURE_SVE ) 9848const int ggml_f32_epr = svcntw (); 9849const int ggml_f32_step = 1 * ggml_f32_epr ; 9850 9851const int np = (nc & ~(ggml_f32_step - 1 )); 9852 9853GGML_F32_VEC sum = GGML_F32_VEC_ZERO ; 9854 9855GGML_F32_VEC adA = GGML_F32_VEC_SET1 (dA ); 9856GGML_F32_VEC axdt = GGML_F32_VEC_SET1 (x_dt ); 9857 9858for (int i = 0 ;i < np ;i += ggml_f32_step ) { 9859// TODO: maybe unroll more? 9860for (int j = 0 ;j < 1 ;j ++ ) { 9861GGML_F32_VEC t0 = GGML_F32_VEC_LOAD (s0 + i + j * ggml_f32_epr + ii * nc ); 9862GGML_F32_VEC t1 = GGML_F32_VEC_LOAD (B + i + j * ggml_f32_epr + g * nc ); 9863GGML_F32_VEC t2 = GGML_F32_VEC_LOAD (C + i + j * ggml_f32_epr + g * nc ); 9864 9865t0 = GGML_F32_VEC_MUL (t0 ,adA ); 9866t1 = GGML_F32_VEC_MUL (t1 ,axdt ); 9867 9868t0 = GGML_F32_VEC_ADD (t0 ,t1 ); 9869 9870sum = GGML_F32_VEC_FMA (sum ,t0 ,t2 ); 9871 9872GGML_F32_VEC_STORE (s + i + j * ggml_f32_epr + ii * nc ,t0 ); 9873 } 9874 } 9875 9876sumf = GGML_F32xt_REDUCE_ONE (sum ); 9877#elif defined(__riscv_v_intrinsic ) 9878// todo: RVV implementation 9879const int np = 0 ; 9880#else 9881const int np = (nc & ~(GGML_F32_STEP - 1 )); 9882 9883GGML_F32_VEC sum [GGML_F32_ARR ]= {GGML_F32_VEC_ZERO }; 9884 9885GGML_F32_VEC adA = GGML_F32_VEC_SET1 (dA ); 9886GGML_F32_VEC axdt = GGML_F32_VEC_SET1 (x_dt ); 9887 9888GGML_F32_VEC ax [GGML_F32_ARR ]; 9889GGML_F32_VEC ay [GGML_F32_ARR ]; 9890GGML_F32_VEC az [GGML_F32_ARR ]; 9891 9892for (int i = 0 ;i < np ;i += GGML_F32_STEP ) { 9893for (int j = 0 ;j < GGML_F32_ARR ;j ++ ) { 9894ax [j ]= GGML_F32_VEC_LOAD (s0 + i + j * GGML_F32_EPR + ii * nc ); 9895ay [j ]= GGML_F32_VEC_LOAD (B + i + j * GGML_F32_EPR + g * nc ); 9896az [j ]= GGML_F32_VEC_LOAD (C + i + j * GGML_F32_EPR + g * nc ); 9897 9898ax [j ]= GGML_F32_VEC_MUL (ax [j ],adA ); 9899ay [j ]= GGML_F32_VEC_MUL (ay [j ],axdt ); 9900 9901ax [j ]= GGML_F32_VEC_ADD (ax [j ],ay [j ]); 9902 9903sum [j ]= GGML_F32_VEC_FMA (sum [j ],ax [j ],az [j ]); 9904 9905GGML_F32_VEC_STORE (s + i + j * GGML_F32_EPR + ii * nc ,ax [j ]); 9906 } 9907 } 9908 9909// reduce sum0..sum3 to sum0 9910GGML_F32_VEC_REDUCE (sumf ,sum ); 9911#endif 9912#else 9913const int np = 0 ; 9914#endif 9915// d_state 9916for (int i0 = np ;i0 < nc ;++ i0 ) { 9917const int i = i0 + ii * nc ; 9918const int ig = i0 + g * nc ; 9919// state = prev_state * dA + dB * x 9920const float state = (s0 [i ]* dA )+ (B [ig ]* x_dt ); 9921// y = rowwise_dotprod(state, C) 9922sumf += state * C [ig ]; 9923s [i ]= state ; 9924 } 9925y [ii ]= sumf ; 9926 } 9927 } 9928 }else { 9929// Mamba-1 has an element-wise decay factor for the states 9930 9931// n_head 9932for (int h = ih0 ;h < ih1 ;++ h ) { 9933// ref: https://github.com/state-spaces/mamba/blob/62db608da60f6fc790b8ed9f4b3225e95ca15fde/mamba_ssm/ops/triton/softplus.py#L16 9934const float dt_soft_plus = ggml_compute_softplus_f32 (dt [h ]); 9935const int g = h / (nh /ng );// repeat_interleave 9936 9937// dim 9938for (int i1 = 0 ;i1 < nr ;++ i1 ) { 9939const int ii = i1 + h * nr ; 9940const float x_dt = x [ii ]* dt_soft_plus ; 9941#if defined(__ARM_FEATURE_SVE ) 9942svfloat32_t vx_dt = GGML_F32_VEC_SET1 (x_dt ); 9943svfloat32_t vdt_soft_plus = GGML_F32_VEC_SET1 (dt_soft_plus ); 9944svfloat32_t r1_vector = GGML_F32_VEC_ZERO ; 9945 9946// d_state 9947// TODO: what happens when (d_state % svcntw()) != 0? 9948for (int64_t k = 0 ;k < nc ;k += svcntw ()) { 9949svfloat32_t vA = GGML_F32_VEC_LOAD (& A [h * nc + k ]); 9950svfloat32_t vB = GGML_F32_VEC_LOAD (& B [k + g * nc ]); 9951svfloat32_t vC = GGML_F32_VEC_LOAD (& C [k + g * nc ]); 9952svfloat32_t vs0 = GGML_F32_VEC_LOAD (& s0 [ii * nc + k ]); 9953 9954svfloat32_t t1 = GGML_F32_VEC_MUL (vdt_soft_plus ,vA ); 9955t1 = exp_ps_sve (svptrue_b32 (),t1 ); 9956svfloat32_t t2 = GGML_F32_VEC_MUL (vx_dt ,vB ); 9957 9958vs0 = GGML_F32_VEC_FMA (t2 ,vs0 ,t1 ); 9959r1_vector = GGML_F32_VEC_ADD (GGML_F32_VEC_MUL (vs0 ,vC ),r1_vector ); 9960 9961GGML_F32_VEC_STORE (& s [ii * nc + k ],vs0 ); 9962 } 9963y [ii ]= GGML_F32xt_REDUCE_ONE (r1_vector ); 9964#else 9965float sumf = 0.0f ; 9966// NOTE: can't really use GGML_SIMD here because d_state is usually 16 9967// and also because expf is used within the loop. 9968// d_state 9969for (int i0 = 0 ;i0 < nc ;++ i0 ) { 9970const int i = i0 + ii * nc ; 9971const int ig = i0 + g * nc ; 9972// state = prev_state * dA + dB * x 9973const float state = (s0 [i ]* expf (dt_soft_plus * A [i0 + h * nc ]))+ (B [ig ]* x_dt ); 9974// y = rowwise_dotprod(state, C) 9975sumf += state * C [ig ]; 9976s [i ]= state ; 9977 } 9978y [ii ]= sumf ; 9979#endif 9980 } 9981 } 9982 } 9983const int64_t slot = nt - 1 - i2 ; 9984if (K > 1 && slot > 0 && slot < K ) { 9985float * s_snapshot = (float * ) ((char * )dst -> data + s_off + (slot * ns + i3 )* (src0 -> nb [3 ])); 9986for (int h = ih0 ;h < ih1 ;++ h ) { 9987memcpy ((char * )s_snapshot + h * src0 -> nb [2 ], (char * )s + h * src0 -> nb [2 ],src0 -> nb [2 ]); 9988 } 9989 } 9990// use the output as the source when it's not the first token-wise iteration 9991s0 = s ; 9992 } 9993 } 9994} 9995 9996void ggml_compute_forward_ssm_scan ( 9997const ggml_compute_params * params , 9998ggml_tensor * dst ) { 9999switch (dst -> src [0 ]-> type ) { 10000case GGML_TYPE_F32 : 10001 { 10002ggml_compute_forward_ssm_scan_f32 (params ,dst ); 10003 }break ; 10004default : 10005 { 10006GGML_ABORT ("fatal error" ); 10007 } 10008 } 10009} 10010 10011// ggml_compute_forward_win_part 10012 10013static void ggml_compute_forward_win_part_f32 ( 10014const ggml_compute_params * params , 10015ggml_tensor * dst ) { 10016GGML_UNUSED (params ); 10017 10018const ggml_tensor * src0 = dst -> src [0 ]; 10019 10020GGML_TENSOR_LOCALS (int64_t ,ne0 ,src0 ,ne ) 10021GGML_TENSOR_LOCALS (int64_t ,ne ,dst ,ne ) 10022 10023const int32_t nep0 = ((const int32_t * )(dst -> op_params ))[0 ]; 10024const int32_t nep1 = ((const int32_t * )(dst -> op_params ))[1 ]; 10025const int32_t w = ((const int32_t * )(dst -> op_params ))[2 ]; 10026 10027assert (ne00 == ne0 ); 10028assert (ne3 == nep0 * nep1 ); 10029 10030// TODO: optimize / multi-thread 10031for (int py = 0 ;py < nep1 ;++ py ) { 10032for (int px = 0 ;px < nep0 ;++ px ) { 10033const int64_t i3 = py * nep0 + px ; 10034for (int64_t i2 = 0 ;i2 < ne2 ;++ i2 ) { 10035for (int64_t i1 = 0 ;i1 < ne1 ;++ i1 ) { 10036for (int64_t i0 = 0 ;i0 < ne0 ;++ i0 ) { 10037const int64_t i02 = py * w + i2 ; 10038const int64_t i01 = px * w + i1 ; 10039const int64_t i00 = i0 ; 10040 10041const int64_t i = i3 * ne2 * ne1 * ne0 + i2 * ne1 * ne0 + i1 * ne0 + i0 ; 10042const int64_t j = i02 * ne01 * ne00 + i01 * ne00 + i00 ; 10043 10044if (py * w + i2 >=ne02 || px * w + i1 >=ne01 ) { 10045 ((float * )dst -> data )[i ]= 0.0f ; 10046 }else { 10047 ((float * )dst -> data )[i ]= ((float * )src0 -> data )[j ]; 10048 } 10049 } 10050 } 10051 } 10052 } 10053 } 10054} 10055 10056void ggml_compute_forward_win_part ( 10057const ggml_compute_params * params , 10058ggml_tensor * dst ) { 10059 10060const ggml_tensor * src0 = dst -> src [0 ]; 10061 10062switch (src0 -> type ) { 10063case GGML_TYPE_F32 : 10064 { 10065ggml_compute_forward_win_part_f32 (params ,dst ); 10066 }break ; 10067default : 10068 { 10069GGML_ABORT ("fatal error" ); 10070 } 10071 } 10072} 10073 10074// ggml_compute_forward_win_unpart 10075 10076static void ggml_compute_forward_win_unpart_f32 ( 10077const ggml_compute_params * params , 10078ggml_tensor * dst ) { 10079GGML_UNUSED (params ); 10080 10081const ggml_tensor * src0 = dst -> src [0 ]; 10082 10083GGML_TENSOR_LOCALS (int64_t ,ne0 ,src0 ,ne ) 10084GGML_TENSOR_LOCALS (int64_t ,ne ,dst ,ne ) 10085 10086const int32_t w = ((const int32_t * )(dst -> op_params ))[0 ]; 10087 10088// padding 10089const int px = (w - ne1 %w )%w ; 10090//const int py = (w - ne2%w)%w; 10091 10092const int npx = (px + ne1 )/w ; 10093//const int npy = (py + ne2)/w; 10094 10095assert (ne0 == ne00 ); 10096 10097// TODO: optimize / multi-thread 10098for (int64_t i2 = 0 ;i2 < ne2 ;++ i2 ) { 10099for (int64_t i1 = 0 ;i1 < ne1 ;++ i1 ) { 10100for (int64_t i0 = 0 ;i0 < ne0 ;++ i0 ) { 10101const int ip2 = i2 /w ; 10102const int ip1 = i1 /w ; 10103 10104const int64_t i02 = i2 %w ; 10105const int64_t i01 = i1 %w ; 10106const int64_t i00 = i0 ; 10107 10108const int64_t i = (ip2 * npx + ip1 )* ne02 * ne01 * ne00 + i02 * ne01 * ne00 + i01 * ne00 + i00 ; 10109const int64_t j = i2 * ne1 * ne0 + i1 * ne0 + i0 ; 10110 10111 ((float * )dst -> data )[j ]= ((float * )src0 -> data )[i ]; 10112 } 10113 } 10114 } 10115} 10116 10117void ggml_compute_forward_win_unpart ( 10118const ggml_compute_params * params , 10119ggml_tensor * dst ) { 10120 10121const ggml_tensor * src0 = dst -> src [0 ]; 10122 10123switch (src0 -> type ) { 10124case GGML_TYPE_F32 : 10125 { 10126ggml_compute_forward_win_unpart_f32 (params ,dst ); 10127 }break ; 10128default : 10129 { 10130GGML_ABORT ("fatal error" ); 10131 } 10132 } 10133} 10134 10135//ggml_compute_forward_unary 10136 10137void ggml_compute_forward_unary ( 10138const ggml_compute_params * params , 10139ggml_tensor * dst ) { 10140 10141const ggml_unary_op op = ggml_get_unary_op (dst ); 10142 10143switch (op ) { 10144case GGML_UNARY_OP_ABS : 10145 { 10146ggml_compute_forward_abs (params ,dst ); 10147 }break ; 10148case GGML_UNARY_OP_SGN : 10149 { 10150ggml_compute_forward_sgn (params ,dst ); 10151 }break ; 10152case GGML_UNARY_OP_NEG : 10153 { 10154ggml_compute_forward_neg (params ,dst ); 10155 }break ; 10156case GGML_UNARY_OP_STEP : 10157 { 10158ggml_compute_forward_step (params ,dst ); 10159 }break ; 10160case GGML_UNARY_OP_TANH : 10161 { 10162ggml_compute_forward_tanh (params ,dst ); 10163 }break ; 10164case GGML_UNARY_OP_ELU : 10165 { 10166ggml_compute_forward_elu (params ,dst ); 10167 }break ; 10168case GGML_UNARY_OP_RELU : 10169 { 10170ggml_compute_forward_relu (params ,dst ); 10171 }break ; 10172case GGML_UNARY_OP_SIGMOID : 10173 { 10174ggml_compute_forward_sigmoid (params ,dst ); 10175 }break ; 10176case GGML_UNARY_OP_GELU : 10177 { 10178ggml_compute_forward_gelu (params ,dst ); 10179 }break ; 10180case GGML_UNARY_OP_GELU_ERF : 10181 { 10182ggml_compute_forward_gelu_erf (params ,dst ); 10183 }break ; 10184case GGML_UNARY_OP_GELU_QUICK : 10185 { 10186ggml_compute_forward_gelu_quick (params ,dst ); 10187 }break ; 10188case GGML_UNARY_OP_SILU : 10189 { 10190ggml_compute_forward_silu (params ,dst ); 10191 }break ; 10192case GGML_UNARY_OP_HARDSWISH : 10193 { 10194ggml_compute_forward_hardswish (params ,dst ); 10195 }break ; 10196case GGML_UNARY_OP_HARDSIGMOID : 10197 { 10198ggml_compute_forward_hardsigmoid (params ,dst ); 10199 }break ; 10200case GGML_UNARY_OP_EXP : 10201 { 10202ggml_compute_forward_exp (params ,dst ); 10203 }break ; 10204case GGML_UNARY_OP_FLOOR : 10205 { 10206ggml_compute_forward_floor (params ,dst ); 10207 }break ; 10208case GGML_UNARY_OP_CEIL : 10209 { 10210ggml_compute_forward_ceil (params ,dst ); 10211 }break ; 10212case GGML_UNARY_OP_ROUND : 10213 { 10214ggml_compute_forward_round (params ,dst ); 10215 }break ; 10216case GGML_UNARY_OP_TRUNC : 10217 { 10218ggml_compute_forward_trunc (params ,dst ); 10219 }break ; 10220case GGML_UNARY_OP_XIELU : 10221 { 10222ggml_compute_forward_xielu (params ,dst ); 10223 }break ; 10224case GGML_UNARY_OP_EXPM1 : 10225 { 10226ggml_compute_forward_expm1 (params ,dst ); 10227 }break ; 10228case GGML_UNARY_OP_SOFTPLUS : 10229 { 10230ggml_compute_forward_softplus (params ,dst ); 10231 }break ; 10232default : 10233 { 10234GGML_ABORT ("fatal error" ); 10235 } 10236 } 10237} 10238 10239//ggml_compute_forward_glu 10240 10241void ggml_compute_forward_glu ( 10242const ggml_compute_params * params , 10243ggml_tensor * dst ) { 10244 10245const ggml_glu_op op = ggml_get_glu_op (dst ); 10246 10247switch (op ) { 10248case GGML_GLU_OP_REGLU : 10249 { 10250ggml_compute_forward_reglu (params ,dst ); 10251 }break ; 10252case GGML_GLU_OP_GEGLU : 10253 { 10254ggml_compute_forward_geglu (params ,dst ); 10255 }break ; 10256case GGML_GLU_OP_SWIGLU : 10257 { 10258ggml_compute_forward_swiglu (params ,dst ); 10259 }break ; 10260case GGML_GLU_OP_SWIGLU_OAI : 10261 { 10262ggml_compute_forward_swiglu_oai (params ,dst ); 10263 }break ; 10264case GGML_GLU_OP_GEGLU_ERF : 10265 { 10266ggml_compute_forward_geglu_erf (params ,dst ); 10267 }break ; 10268case GGML_GLU_OP_GEGLU_QUICK : 10269 { 10270ggml_compute_forward_geglu_quick (params ,dst ); 10271 }break ; 10272case GGML_GLU_OP_SWIGLU_CLAMP : 10273 { 10274ggml_compute_forward_swiglu_clamp (params ,dst ); 10275 }break ; 10276default : 10277 { 10278GGML_ABORT ("fatal error" ); 10279 } 10280 } 10281} 10282 10283// ggml_compute_forward_get_rel_pos 10284 10285static void ggml_compute_forward_get_rel_pos_f16 ( 10286const ggml_compute_params * params , 10287ggml_tensor * dst ) { 10288GGML_UNUSED (params ); 10289 10290const ggml_tensor * src0 = dst -> src [0 ]; 10291 10292// ref: https://github.com/facebookresearch/segment-anything/blob/main/segment_anything/modeling/image_encoder.py#L292-L322 10293 10294GGML_TENSOR_UNARY_OP_LOCALS 10295 10296const int64_t w = ne1 ; 10297 10298ggml_fp16_t * src0_data = (ggml_fp16_t * )src0 -> data ; 10299ggml_fp16_t * dst_data = (ggml_fp16_t * )dst -> data ; 10300 10301for (int64_t i2 = 0 ;i2 < ne2 ;++ i2 ) { 10302for (int64_t i1 = 0 ;i1 < ne1 ;++ i1 ) { 10303const int64_t pos = (w - i1 - 1 )+ i2 ; 10304for (int64_t i0 = 0 ;i0 < ne0 ;++ i0 ) { 10305dst_data [i2 * ne1 * ne0 + i1 * ne0 + i0 ]= src0_data [pos * ne00 + i0 ]; 10306 } 10307 } 10308 } 10309} 10310 10311void ggml_compute_forward_get_rel_pos ( 10312const ggml_compute_params * params , 10313ggml_tensor * dst ) { 10314 10315const ggml_tensor * src0 = dst -> src [0 ]; 10316 10317switch (src0 -> type ) { 10318case GGML_TYPE_F16 : 10319case GGML_TYPE_BF16 : 10320 { 10321ggml_compute_forward_get_rel_pos_f16 (params ,dst ); 10322 }break ; 10323default : 10324 { 10325GGML_ABORT ("fatal error" ); 10326 } 10327 } 10328} 10329 10330// ggml_compute_forward_add_rel_pos 10331 10332static void ggml_compute_forward_add_rel_pos_f32 ( 10333const ggml_compute_params * params , 10334ggml_tensor * dst ) { 10335 10336const ggml_tensor * src0 = dst -> src [0 ]; 10337const ggml_tensor * src1 = dst -> src [1 ]; 10338const ggml_tensor * src2 = dst -> src [2 ]; 10339 10340const bool inplace = (bool ) ((int32_t * )dst -> op_params )[0 ]; 10341if (!inplace ) { 10342if (params -> ith == 0 ) { 10343memcpy ((char * )dst -> data , (char * )src0 -> data ,ggml_nbytes (dst )); 10344 } 10345ggml_barrier (params -> threadpool ); 10346 } 10347// ref: https://github.com/facebookresearch/segment-anything/blob/main/segment_anything/modeling/image_encoder.py#L357-L359 10348 10349float * src1_data = (float * )src1 -> data ; 10350float * src2_data = (float * )src2 -> data ; 10351float * dst_data = (float * )dst -> data ; 10352 10353const int64_t ne10 = src1 -> ne [0 ]; 10354const int64_t ne11 = src1 -> ne [1 ]; 10355const int64_t ne12 = src1 -> ne [2 ]; 10356const int64_t ne13 = src1 -> ne [3 ]; 10357 10358const int ith = params -> ith ; 10359const int nth = params -> nth ; 10360 10361// total patches in dst 10362const int np = ne13 ; 10363 10364// patches per thread 10365const int dp = (np + nth - 1 )/nth ; 10366 10367// patch range for this thread 10368const int ip0 = dp * ith ; 10369const int ip1 = MIN (ip0 + dp ,np ); 10370 10371for (int64_t i13 = ip0 ;i13 < ip1 ;++ i13 ) { 10372for (int64_t i12 = 0 ;i12 < ne12 ;++ i12 ) { 10373for (int64_t i11 = 0 ;i11 < ne11 ;++ i11 ) { 10374const int64_t jp1 = i13 * ne12 * ne11 * ne10 + i12 * ne11 * ne10 + i11 * ne10 ; 10375for (int64_t i10 = 0 ;i10 < ne10 ;++ i10 ) { 10376const int64_t jp0 = jp1 + i10 ; 10377const float src1_e = src1_data [jp0 ]; 10378const float src2_e = src2_data [jp0 ]; 10379 10380const int64_t jdh = jp0 * ne10 ; 10381const int64_t jdw = jdh - (ne10 - 1 )* i10 ; 10382 10383for (int64_t j = 0 ;j < ne10 ;++ j ) { 10384dst_data [jdh + j ]+= src2_e ; 10385dst_data [jdw + j * ne10 ]+= src1_e ; 10386 } 10387 } 10388 } 10389 } 10390 } 10391} 10392 10393void ggml_compute_forward_add_rel_pos ( 10394const ggml_compute_params * params , 10395ggml_tensor * dst ) { 10396 10397const ggml_tensor * src0 = dst -> src [0 ]; 10398 10399switch (src0 -> type ) { 10400case GGML_TYPE_F32 : 10401 { 10402ggml_compute_forward_add_rel_pos_f32 (params ,dst ); 10403 }break ; 10404default : 10405 { 10406GGML_ABORT ("fatal error" ); 10407 } 10408 } 10409} 10410 10411// ggml_compute_forward_rwkv_wkv6 10412 10413static void ggml_compute_forward_rwkv_wkv6_f32 ( 10414const ggml_compute_params * params , 10415ggml_tensor * dst ) { 10416const int64_t T = dst -> src [1 ]-> ne [2 ]; 10417const int64_t C = dst -> ne [0 ]; 10418const int64_t HEADS = dst -> src [1 ]-> ne [1 ]; 10419const int64_t n_seqs = dst -> src [5 ]-> ne [1 ]; 10420const int64_t head_size = C /HEADS ; 10421 10422float * dst_data = (float * )dst -> data ; 10423float * state = ((float * )dst -> data )+ C * T ; 10424 10425const int ith = params -> ith ; 10426const int nth = params -> nth ; 10427 10428const int h_start = (HEADS * (ith )) /nth ; 10429const int h_end = ((HEADS * (ith + 1 )) /nth < HEADS ) ? 10430 (HEADS * (ith + 1 )) /nth :HEADS ; 10431 10432float * k = (float * )dst -> src [0 ]-> data ; 10433float * v = (float * )dst -> src [1 ]-> data ; 10434float * r = (float * )dst -> src [2 ]-> data ; 10435float * time_faaaa = (float * )dst -> src [3 ]-> data ; 10436float * time_decay = (float * )dst -> src [4 ]-> data ; 10437 10438size_t t_stride = HEADS * head_size ;// Same to C 10439 10440size_t h_stride = C /HEADS ; 10441GGML_ASSERT (C %HEADS == 0 );// C must be divisible by HEADS 10442size_t h_stride_2d = head_size * head_size ; 10443 10444if (ith == 0 ) { 10445memset (dst_data ,0 ,T * C * sizeof (float )); 10446 } 10447ggml_barrier (params -> threadpool ); 10448 10449 10450#if defined(__AVX__ )&& !defined(__AVX512F__ ) 10451#define GGML_F32X GGML_F32x8 10452#define GGML_F32X_SET1 GGML_F32x8_SET1 10453#define GGML_F32X_LOAD GGML_F32x8_LOAD 10454#define GGML_F32X_STORE GGML_F32x8_STORE 10455#define GGML_F32X_MUL GGML_F32x8_MUL 10456#define GGML_F32X_FMA GGML_F32x8_FMA 10457#define WKV_VECTOR_SIZE 8 10458#elif defined(__AVX512F__ ) 10459#define GGML_F32X GGML_F32x16 10460#define GGML_F32X_SET1 GGML_F32x16_SET1 10461#define GGML_F32X_LOAD GGML_F32x16_LOAD 10462#define GGML_F32X_STORE GGML_F32x16_STORE 10463#define GGML_F32X_MUL GGML_F32x16_MUL 10464#define GGML_F32X_FMA GGML_F32x16_FMA 10465#define WKV_VECTOR_SIZE 16 10466#elif defined(__ARM_FEATURE_SVE )&& defined(__aarch64__ ) 10467#define GGML_F32X GGML_F32xt 10468#define GGML_F32X_SET1 GGML_F32xt_SET1 10469#define GGML_F32X_LOAD GGML_F32xt_LOAD 10470#define GGML_F32X_STORE GGML_F32xt_STORE 10471#define GGML_F32X_MUL GGML_F32xt_MUL 10472#define GGML_F32X_FMA GGML_F32xt_FMA 10473#define WKV_VECTOR_SIZE 8 10474#elif defined(__ARM_NEON )&& defined(__aarch64__ ) 10475#define GGML_F32X GGML_F32x4 10476#define GGML_F32X_SET1 GGML_F32x4_SET1 10477#define GGML_F32X_LOAD GGML_F32x4_LOAD 10478#define GGML_F32X_STORE GGML_F32x4_STORE 10479#define GGML_F32X_MUL GGML_F32x4_MUL 10480#define GGML_F32X_FMA GGML_F32x4_FMA 10481#define WKV_VECTOR_SIZE 4 10482#endif 10483 10484#ifdef WKV_VECTOR_SIZE 10485int wkv_vector_size ; 10486#if defined(__ARM_FEATURE_SVE ) 10487wkv_vector_size = svcntw (); 10488#else 10489wkv_vector_size = WKV_VECTOR_SIZE ; 10490#endif 10491const int64_t vec_count = head_size /wkv_vector_size ; 10492 10493for (int64_t t = 0 ;t < T ;t ++ ) { 10494size_t t_offset = t * t_stride ; 10495size_t state_offset = head_size * C * (t / (T /n_seqs )); 10496float * state_cur = state + state_offset ; 10497float * state_prev = t % (T /n_seqs ) ?state_cur : (float * )dst -> src [5 ]-> data + state_offset ; 10498 10499for (int64_t h = h_start ;h < h_end ;h ++ ) { 10500size_t h_offset = h * h_stride ; 10501size_t t_h_offset = t_offset + h_offset ; 10502size_t h_2d_offset = h * h_stride_2d ; 10503 10504for (int64_t i = 0 ;i < head_size ;i ++ ) { 10505size_t t_h_i_offset = t_h_offset + i ; 10506size_t h_i_offset = h_offset + i ; 10507size_t h_2d_i_offset = h_2d_offset + i * h_stride ; 10508 10509float k_val = k [t_h_i_offset ]; 10510float r_val = r [t_h_i_offset ]; 10511float time_faaaa_val = time_faaaa [h_i_offset ]; 10512float time_decay_val = time_decay [t_h_i_offset ]; 10513 10514// Broadcast scalar values to vectors 10515GGML_F32X k_vec = GGML_F32X_SET1 (k_val ); 10516GGML_F32X r_vec = GGML_F32X_SET1 (r_val ); 10517GGML_F32X time_faaaa_vec = GGML_F32X_SET1 (time_faaaa_val ); 10518GGML_F32X time_decay_vec = GGML_F32X_SET1 (time_decay_val ); 10519 10520for (int64_t j = 0 ;j < vec_count ;j ++ ) { 10521size_t base_j = j * wkv_vector_size ; 10522size_t t_h_j_offset = t_h_offset + base_j ; 10523size_t h_2d_i_j_offset = h_2d_i_offset + base_j ; 10524 10525// Load x elements at once 10526GGML_F32X v_vec = GGML_F32X_LOAD (& v [t_h_j_offset ]); 10527GGML_F32X prev_state_vec = GGML_F32X_LOAD (& state_prev [h_2d_i_j_offset ]); 10528GGML_F32X dst_vec = GGML_F32X_LOAD (& dst_data [t_h_j_offset ]); 10529 10530// Compute kv = v * k 10531GGML_F32X kv_vec = GGML_F32X_MUL (v_vec ,k_vec ); 10532 10533// Compute temp = kv * time_faaaa + prev_state 10534GGML_F32X temp_vec = GGML_F32X_FMA (prev_state_vec ,kv_vec ,time_faaaa_vec ); 10535 10536// Update dst: dst += temp * r 10537dst_vec = GGML_F32X_FMA (dst_vec ,temp_vec ,r_vec ); 10538GGML_F32X_STORE (& dst_data [t_h_j_offset ],dst_vec ); 10539 10540// Update state: state = prev_state * time_decay + kv 10541GGML_F32X new_state_vec = GGML_F32X_FMA (kv_vec ,prev_state_vec ,time_decay_vec ); 10542GGML_F32X_STORE (& state_cur [h_2d_i_j_offset ],new_state_vec ); 10543 } 10544 10545// Handle remaining elements, this will not be used. 10546for (int64_t j = vec_count * wkv_vector_size ;j < head_size ;j ++ ) { 10547size_t t_h_j_offset = t_h_offset + j ; 10548size_t h_2d_i_j_offset = h_2d_i_offset + j ; 10549float v_val = v [t_h_j_offset ]; 10550float kv_val = v_val * k_val ; 10551float prev_state_val = state_prev [h_2d_i_j_offset ]; 10552float temp_val = kv_val * time_faaaa_val + prev_state_val ; 10553dst_data [t_h_j_offset ]+= temp_val * r_val ; 10554state_cur [h_2d_i_j_offset ]= prev_state_val * time_decay_val + kv_val ; 10555 } 10556 } 10557 } 10558 } 10559 10560#else 10561// basically fused operations: 10562// dst = r @ (time_faaaa * (k @ v) + state), 10563// state = time_decay * state + (k @ v), 10564// recursive through each token 10565for (int64_t t = 0 ;t < T ;t ++ ) { 10566size_t t_offset = t * t_stride ; 10567size_t state_offset = head_size * C * (t / (T /n_seqs )); 10568float * state_cur = state + state_offset ; 10569float * state_prev = t % (T /n_seqs ) ?state_cur : (float * )dst -> src [5 ]-> data + state_offset ; 10570 10571for (int64_t h = h_start ;h < h_end ;h ++ ) { 10572size_t h_offset = h * h_stride ; 10573size_t t_h_offset = t_offset + h_offset ; 10574size_t h_2d_offset = h * h_stride_2d ; 10575 10576for (int64_t i = 0 ;i < head_size ;i ++ ) { 10577size_t t_h_i_offset = t_h_offset + i ; 10578size_t h_i_offset = h_offset + i ; 10579size_t h_2d_i_offset = h_2d_offset + i * h_stride ; 10580 10581float k_val = k [t_h_i_offset ]; 10582float r_val = r [t_h_i_offset ]; 10583float time_faaaa_val = time_faaaa [h_i_offset ]; 10584// RWKV v6: different time_decay for each token. 10585float time_decay_val = time_decay [t_h_i_offset ]; 10586 10587for (int64_t j = 0 ;j < head_size ;j ++ ) { 10588size_t t_h_j_offset = t_h_offset + j ; 10589size_t h_2d_i_j_offset = h_2d_i_offset + j ; 10590 10591float v_val = v [t_h_j_offset ]; 10592float kv_val = v_val * k_val ; 10593float prev_state_val = state_prev [h_2d_i_j_offset ]; 10594float temp_val = kv_val * time_faaaa_val + prev_state_val ; 10595dst_data [t_h_j_offset ]+= temp_val * r_val ; 10596state_cur [h_2d_i_j_offset ]= prev_state_val * time_decay_val + kv_val ; 10597 } 10598 } 10599 } 10600 } 10601#endif 10602} 10603 10604 10605void ggml_compute_forward_rwkv_wkv6 ( 10606const ggml_compute_params * params , 10607ggml_tensor * dst ) { 10608 10609const ggml_tensor * src0 = dst -> src [0 ]; 10610 10611switch (src0 -> type ) { 10612case GGML_TYPE_F32 : 10613 { 10614ggml_compute_forward_rwkv_wkv6_f32 (params ,dst ); 10615 }break ; 10616default : 10617 { 10618GGML_ABORT ("fatal error" ); 10619 } 10620 } 10621} 10622 10623// ggml_compute_forward_gla 10624 10625static void ggml_compute_forward_gla_f32 ( 10626const ggml_compute_params * params , 10627ggml_tensor * dst ) { 10628const int64_t T = dst -> src [1 ]-> ne [2 ]; 10629const int64_t C = dst -> ne [0 ]; 10630const int64_t HEADS = dst -> src [1 ]-> ne [1 ]; 10631const int64_t n_seqs = dst -> src [4 ]-> ne [1 ]; 10632const int64_t head_size = C /HEADS ; 10633const float scale = ggml_get_op_params_f32 (dst ,0 ); 10634 10635float * dst_data = (float * )dst -> data ; 10636float * state = ((float * )dst -> data )+ C * T ; 10637 10638const int ith = params -> ith ; 10639const int nth = params -> nth ; 10640 10641const int h_start = (HEADS * (ith )) /nth ; 10642const int h_end = ((HEADS * (ith + 1 )) /nth < HEADS ) ? 10643 (HEADS * (ith + 1 )) /nth :HEADS ; 10644 10645float * k = (float * )dst -> src [0 ]-> data ; 10646float * v = (float * )dst -> src [1 ]-> data ; 10647float * q = (float * )dst -> src [2 ]-> data ; 10648float * g = (float * )dst -> src [3 ]-> data ; 10649 10650size_t t_stride = HEADS * head_size ;// Same to C 10651 10652size_t h_stride = C /HEADS ; 10653GGML_ASSERT (C %HEADS == 0 );// C must be divisible by HEADS 10654size_t h_stride_2d = head_size * head_size ; 10655 10656if (ith == 0 ) { 10657memset (dst_data ,0 ,T * C * sizeof (float )); 10658 } 10659ggml_barrier (params -> threadpool ); 10660 10661 10662#if defined(__AVX__ )&& !defined(__AVX512F__ ) 10663#define GGML_F32X GGML_F32x8 10664#define GGML_F32X_SET1 GGML_F32x8_SET1 10665#define GGML_F32X_LOAD GGML_F32x8_LOAD 10666#define GGML_F32X_STORE GGML_F32x8_STORE 10667#define GGML_F32X_MUL GGML_F32x8_MUL 10668#define GGML_F32X_FMA GGML_F32x8_FMA 10669#define GLA_VECTOR_SIZE 8 10670#elif defined(__AVX512F__ ) 10671#define GGML_F32X GGML_F32x16 10672#define GGML_F32X_SET1 GGML_F32x16_SET1 10673#define GGML_F32X_LOAD GGML_F32x16_LOAD 10674#define GGML_F32X_STORE GGML_F32x16_STORE 10675#define GGML_F32X_MUL GGML_F32x16_MUL 10676#define GGML_F32X_FMA GGML_F32x16_FMA 10677#define GLA_VECTOR_SIZE 16 10678#elif defined(__ARM_FEATURE_SVE )&& defined(__aarch64__ ) 10679#define GGML_F32X GGML_F32xt 10680#define GGML_F32X_SET1 GGML_F32xt_SET1 10681#define GGML_F32X_LOAD GGML_F32xt_LOAD 10682#define GGML_F32X_STORE GGML_F32xt_STORE 10683#define GGML_F32X_MUL GGML_F32xt_MUL 10684#define GGML_F32X_FMA GGML_F32xt_FMA 10685#define GLA_VECTOR_SIZE 8 10686#elif defined(__ARM_NEON )&& defined(__aarch64__ ) 10687#define GGML_F32X GGML_F32x4 10688#define GGML_F32X_SET1 GGML_F32x4_SET1 10689#define GGML_F32X_LOAD GGML_F32x4_LOAD 10690#define GGML_F32X_STORE GGML_F32x4_STORE 10691#define GGML_F32X_MUL GGML_F32x4_MUL 10692#define GGML_F32X_FMA GGML_F32x4_FMA 10693#define GLA_VECTOR_SIZE 4 10694#endif 10695 10696#ifdef GLA_VECTOR_SIZE 10697int gla_vector_size ; 10698#if defined(__ARM_FEATURE_SVE ) 10699gla_vector_size = svcntw (); 10700#else 10701gla_vector_size = GLA_VECTOR_SIZE ; 10702#endif 10703const int64_t vec_count = head_size /gla_vector_size ; 10704 10705for (int64_t t = 0 ;t < T ;t ++ ) { 10706size_t t_offset = t * t_stride ; 10707size_t state_offset = head_size * C * (t / (T /n_seqs )); 10708float * state_cur = state + state_offset ; 10709float * state_prev = t % (T /n_seqs ) ?state_cur : (float * )dst -> src [4 ]-> data + state_offset ; 10710 10711for (int64_t h = h_start ;h < h_end ;h ++ ) { 10712size_t h_offset = h * h_stride ; 10713size_t t_h_offset = t_offset + h_offset ; 10714size_t h_2d_offset = h * h_stride_2d ; 10715 10716for (int64_t i = 0 ;i < head_size ;i ++ ) { 10717size_t t_h_i_offset = t_h_offset + i ; 10718size_t h_2d_i_offset = h_2d_offset + i * h_stride ; 10719 10720float k_val = k [t_h_i_offset ]; 10721float q_val = q [t_h_i_offset ]* scale ; 10722float g_val = g [t_h_i_offset ]; 10723 10724// Broadcast scalar values to vectors 10725GGML_F32X k_vec = GGML_F32X_SET1 (k_val ); 10726GGML_F32X q_vec = GGML_F32X_SET1 (q_val ); 10727GGML_F32X g_vec = GGML_F32X_SET1 (g_val ); 10728 10729for (int64_t j = 0 ;j < vec_count ;j ++ ) { 10730size_t base_j = j * gla_vector_size ; 10731size_t t_h_j_offset = t_h_offset + base_j ; 10732size_t h_2d_i_j_offset = h_2d_i_offset + base_j ; 10733 10734// Load x elements at once 10735GGML_F32X v_vec = GGML_F32X_LOAD (& v [t_h_j_offset ]); 10736GGML_F32X prev_state_vec = GGML_F32X_LOAD (& state_prev [h_2d_i_j_offset ]); 10737GGML_F32X dst_vec = GGML_F32X_LOAD (& dst_data [t_h_j_offset ]); 10738 10739// Compute kv = v * k 10740GGML_F32X kv_vec = GGML_F32X_MUL (v_vec ,k_vec ); 10741 10742// Compute temp = prev_state * g + kv 10743GGML_F32X temp_vec = GGML_F32X_FMA (kv_vec ,prev_state_vec ,g_vec ); 10744 10745// Update dst: dst += temp * q 10746dst_vec = GGML_F32X_FMA (dst_vec ,temp_vec ,q_vec ); 10747GGML_F32X_STORE (& dst_data [t_h_j_offset ],dst_vec ); 10748 10749// Update state 10750GGML_F32X_STORE (& state_cur [h_2d_i_j_offset ],temp_vec ); 10751 } 10752 10753// Handle remaining elements, this will not be used. 10754for (int64_t j = vec_count * gla_vector_size ;j < head_size ;j ++ ) { 10755size_t t_h_j_offset = t_h_offset + j ; 10756size_t h_2d_i_j_offset = h_2d_i_offset + j ; 10757float v_val = v [t_h_j_offset ]; 10758float kv_val = v_val * k_val ; 10759float prev_state_val = state_prev [h_2d_i_j_offset ]; 10760float temp_val = kv_val + prev_state_val * g_val ; 10761dst_data [t_h_j_offset ]+= temp_val * q_val ; 10762state_cur [h_2d_i_j_offset ]= temp_val ; 10763 } 10764 } 10765 } 10766 } 10767 10768#else 10769for (int64_t t = 0 ;t < T ;t ++ ) { 10770size_t t_offset = t * t_stride ; 10771size_t state_offset = head_size * C * (t / (T /n_seqs )); 10772float * state_cur = state + state_offset ; 10773float * state_prev = t % (T /n_seqs ) ?state_cur : (float * )dst -> src [4 ]-> data + state_offset ; 10774 10775for (int64_t h = h_start ;h < h_end ;h ++ ) { 10776size_t h_offset = h * h_stride ; 10777size_t t_h_offset = t_offset + h_offset ; 10778size_t h_2d_offset = h * h_stride_2d ; 10779 10780for (int64_t i = 0 ;i < head_size ;i ++ ) { 10781size_t t_h_i_offset = t_h_offset + i ; 10782size_t h_2d_i_offset = h_2d_offset + i * h_stride ; 10783 10784float k_val = k [t_h_i_offset ]; 10785float q_val = q [t_h_i_offset ]* scale ; 10786float g_val = g [t_h_i_offset ]; 10787 10788for (int64_t j = 0 ;j < head_size ;j ++ ) { 10789size_t t_h_j_offset = t_h_offset + j ; 10790size_t h_2d_i_j_offset = h_2d_i_offset + j ; 10791 10792float v_val = v [t_h_j_offset ]; 10793float kv_val = v_val * k_val ; 10794float prev_state_val = state_prev [h_2d_i_j_offset ]; 10795float temp_val = prev_state_val * g_val + kv_val ; 10796dst_data [t_h_j_offset ]+= temp_val * q_val ; 10797state_cur [h_2d_i_j_offset ]= temp_val ; 10798 } 10799 } 10800 } 10801 } 10802#endif 10803} 10804 10805 10806void ggml_compute_forward_gla ( 10807const ggml_compute_params * params , 10808ggml_tensor * dst ) { 10809 10810const ggml_tensor * src0 = dst -> src [0 ]; 10811 10812switch (src0 -> type ) { 10813case GGML_TYPE_F32 : 10814 { 10815ggml_compute_forward_gla_f32 (params ,dst ); 10816 }break ; 10817default : 10818 { 10819GGML_ABORT ("fatal error" ); 10820 } 10821 } 10822} 10823 10824static void ggml_compute_forward_solve_tri_f32 (const struct ggml_compute_params * params ,struct ggml_tensor * dst ) { 10825const struct ggml_tensor * src0 = dst -> src [0 ];// A (lower triangular) 10826const struct ggml_tensor * src1 = dst -> src [1 ];// B (RHS) 10827 10828GGML_TENSOR_BINARY_OP_LOCALS ; 10829 10830GGML_ASSERT (src0 -> type == GGML_TYPE_F32 ); 10831GGML_ASSERT (src1 -> type == GGML_TYPE_F32 ); 10832GGML_ASSERT (dst -> type == GGML_TYPE_F32 ); 10833 10834GGML_ASSERT (ne00 == ne01 );// A must be square 10835GGML_ASSERT (ne0 == ne10 );// solution cols == B cols 10836GGML_ASSERT (ne1 == ne11 );// solution rows == B rows 10837 10838GGML_ASSERT (ne02 == ne12 && ne12 == ne2 ); 10839GGML_ASSERT (ne03 == ne13 && ne13 == ne3 ); 10840 10841const int ith = params -> ith ; 10842const int nth = params -> nth ; 10843 10844const int64_t k = ne10 ;// number of RHS columns 10845const int64_t n = ne11 ;// A is n×n 10846const int64_t nr = ne02 * ne03 * k ;// we're parallelizing on columns here, so seq x token x column will be the unit 10847 10848// chunks per thread 10849const int64_t dr = (nr + nth - 1 )/nth ; 10850 10851// chunk range for this thread 10852const int64_t ir0 = dr * ith ; 10853const int64_t ir1 = MIN (ir0 + dr ,nr ); 10854 10855const float * A = (const float * )src0 -> data ;// [n, n, B1, B2] 10856const float * B = (const float * )src1 -> data ;// [n, k, B1, B2] 10857float * X = (float * )dst -> data ;// [n, k, B1, B2] 10858 10859for (int64_t ir = ir0 ;ir < ir1 ;++ ir ) { 10860const int64_t i03 = ir /(ne02 * k ); 10861const int64_t i02 = (ir - i03 * ne02 * k )/k ; 10862const int64_t i01 = (ir - i03 * ne02 * k - i02 * k ); 10863 10864const float * A_batch = A + i02 * nb02 /sizeof (float )+ i03 * nb03 /sizeof (float ); 10865const float * B_batch = B + i02 * nb12 /sizeof (float )+ i03 * nb13 /sizeof (float ); 10866 10867float * X_batch = X + i02 * nb2 /sizeof (float )+ i03 * nb3 /sizeof (float ); 10868 10869for (int64_t i00 = 0 ;i00 < n ;++ i00 ) { 10870float sum = 0.0f ; 10871for (int64_t t = 0 ;t < i00 ;++ t ) { 10872sum += A_batch [i00 * n + t ]* X_batch [t * k + i01 ]; 10873 } 10874 10875const float diag = A_batch [i00 * n + i00 ]; 10876assert (diag != 0.0f && "Zero diagonal in triangular matrix" ); 10877 10878X_batch [i00 * k + i01 ]= (B_batch [i00 * k + i01 ]- sum ) /diag ; 10879 } 10880 } 10881} 10882 10883void ggml_compute_forward_solve_tri (const struct ggml_compute_params * params ,struct ggml_tensor * dst ) { 10884const ggml_tensor * src0 = dst -> src [0 ]; 10885const ggml_tensor * src1 = dst -> src [1 ]; 10886 10887if (src0 -> type == GGML_TYPE_F32 && src1 -> type == GGML_TYPE_F32 ) { 10888ggml_compute_forward_solve_tri_f32 (params ,dst ); 10889 }else { 10890GGML_ABORT ("fatal error" ); 10891 } 10892} 10893 10894// ggml_compute_forward_gated_delta_net 10895static void ggml_compute_forward_gated_delta_net_one_chunk ( 10896const ggml_compute_params * params , 10897ggml_tensor * dst , 10898int64_t ir0 , 10899int64_t ir1 ) { 10900 10901ggml_tensor * src_q = dst -> src [0 ]; 10902ggml_tensor * src_k = dst -> src [1 ]; 10903ggml_tensor * src_v = dst -> src [2 ]; 10904ggml_tensor * src_g = dst -> src [3 ]; 10905ggml_tensor * src_beta = dst -> src [4 ]; 10906ggml_tensor * src_state = dst -> src [5 ]; 10907 10908const int64_t S_v = src_v -> ne [0 ]; 10909const int64_t H = src_v -> ne [1 ]; 10910const int64_t n_tokens = src_v -> ne [2 ]; 10911const int64_t n_seqs = src_v -> ne [3 ]; 10912 10913GGML_ASSERT (ggml_is_contiguous_rows (src_q )); 10914GGML_ASSERT (ggml_is_contiguous_rows (src_k )); 10915GGML_ASSERT (ggml_is_contiguous_rows (src_v )); 10916GGML_ASSERT (ggml_is_contiguous (src_g )); 10917GGML_ASSERT (ggml_is_contiguous (src_beta )); 10918GGML_ASSERT (ggml_is_contiguous (src_state )); 10919 10920GGML_ASSERT (src_g -> ne [0 ]== 1 || src_g -> ne [0 ]== S_v ); 10921GGML_ASSERT (src_beta -> ne [0 ]== 1 ); 10922 10923GGML_TENSOR_LOCALS (int64_t ,neq ,src_q ,ne ); 10924GGML_TENSOR_LOCALS (size_t ,nbq ,src_q ,nb ); 10925GGML_TENSOR_LOCALS (int64_t ,nek ,src_k ,ne ); 10926GGML_TENSOR_LOCALS (size_t ,nbk ,src_k ,nb ); 10927GGML_TENSOR_LOCALS (int64_t ,nev ,src_v ,ne ); 10928GGML_TENSOR_LOCALS (size_t ,nbv ,src_v ,nb ); 10929GGML_TENSOR_LOCALS (int64_t ,neg ,src_g ,ne ); 10930GGML_TENSOR_LOCALS (size_t ,nbg ,src_g ,nb ); 10931GGML_TENSOR_LOCALS (size_t ,nbb ,src_beta ,nb ); 10932 10933const bool kda = (neg0 == S_v ); 10934 10935// K (snapshot slot count) is an op param; state holds s0 only [S_v, S_v, H, n_seqs]. 10936const int64_t K = ggml_get_op_params_i32 (dst ,0 ); 10937GGML_ASSERT (K >=1 ); 10938// per-seq stride in floats (seq s starts at state + s * seq_stride) 10939const int64_t state_seq_stride = src_state -> nb [3 ] /sizeof (float ); 10940 10941const int64_t per_thread = S_v + (K > 1 ?S_v * S_v :0 ); 10942const int ith = params -> ith ; 10943 10944float * delta = (float * )params -> wdata + ith * per_thread + CACHE_LINE_SIZE_F32 ; 10945float * state_work = K > 1 ? (delta + S_v ) :nullptr ; 10946 10947// output layout: [attn_scores | new_states] 10948// attn_scores: S_v * H * n_tokens * n_seqs floats 10949// new_states: S_v * S_v * H * n_seqs * K floats (K snapshot slots; last min(n_tokens, K)) 10950const int64_t attn_score_elems = S_v * H * n_tokens * n_seqs ; 10951const int64_t state_size_per_snap = S_v * S_v * H * n_seqs ; 10952float * attn_out_base = (float * )dst -> data ; 10953float * state_out_base = (float * )dst -> data + attn_score_elems ; 10954 10955// snapshot slot mapping: slot 0 = most recent state, slot s = s tokens back. 10956// When n_tokens < K only slots 0..n_tokens-1 are written; older slots are caller-owned. 10957 10958const float * state_in_base = (const float * )src_state -> data ; 10959 10960//const int64_t rq1 = nev1 / neq1; 10961//const int64_t rk1 = nev1 / nek1; 10962const int64_t rq3 = nev3 /neq3 ; 10963const int64_t rk3 = nev3 /nek3 ; 10964 10965const float scale = 1.0f /sqrtf ((float )S_v ); 10966 10967for (int64_t ir = ir0 ;ir < ir1 ;++ ir ) { 10968const int64_t iv1 = ir %H ;// head_index 10969const int64_t iv3 = ir /H ;// sequence 10970 10971const int64_t iq1 = iv1 %neq1 ; 10972const int64_t ik1 = iv1 %nek1 ; 10973 10974const int64_t iq3 = iv3 /rq3 ; 10975const int64_t ik3 = iv3 /rk3 ; 10976 10977// For K=1, write directly to the single output slot to avoid an extra memcpy at the end. 10978// For K>1, work in scratch and copy out per-token when the slot is in range. 10979float * s_out = (K > 1 ) 10980 ?state_work 10981 :state_out_base + (iv3 * H + iv1 )* S_v * S_v ; 10982 10983// copy input state into the working buffer and operate in-place 10984// state layout [S_v, S_v, H, n_seqs]: seq iv3 starts at iv3 * state_seq_stride. 10985const float * s_in = state_in_base + iv3 * state_seq_stride + iv1 * S_v * S_v ; 10986memcpy (s_out ,s_in ,S_v * S_v * sizeof (float )); 10987 10988// attn output pointer for first token of this (head, seq) 10989float * attn_data = attn_out_base + (iv3 * n_tokens * H + iv1 )* S_v ; 10990 10991for (int64_t t = 0 ;t < n_tokens ;t ++ ) { 10992const float * q_d = (const float * )((const char * )src_q -> data + iq3 * nbq3 + t * nbq2 + iq1 * nbq1 ); 10993const float * k_d = (const float * )((const char * )src_k -> data + ik3 * nbk3 + t * nbk2 + ik1 * nbk1 ); 10994const float * v_d = (const float * )((const char * )src_v -> data + iv3 * nbv3 + t * nbv2 + iv1 * nbv1 ); 10995 10996const float beta_val = * (const float * )((const char * )src_beta -> data + iv3 * nbb3 + t * nbb2 + iv1 * nbb1 ); 10997const float * g_d = (const float * )((const char * )src_g -> data + iv3 * nbg3 + t * nbg2 + iv1 * nbg1 ); 10998 10999// state is stored transposed: s_out[j*S_v + i] = S[i][j] 11000// so row j of s_out = column j of S (contiguous access) 11001 11002if (kda ) { 11003// precompute exp(g) into delta scratch (reused below) 11004for (int64_t i = 0 ;i < S_v ;++ i ) { 11005delta [i ]= expf (g_d [i ]); 11006 } 11007// S[i][:] *= exp(g[i]) => for each row j of M: M[j][i] *= exp(g[i]) 11008for (int64_t j = 0 ;j < S_v ;++ j ) { 11009ggml_vec_mul_f32 (S_v ,& s_out [j * S_v ],& s_out [j * S_v ],delta ); 11010 } 11011 }else { 11012ggml_vec_scale_f32 (S_v * S_v ,s_out ,expf (g_d [0 ])); 11013 } 11014 11015// delta[j] = sum_i S[i][j] * k[i] = dot(row j of M, k) 11016for (int64_t j = 0 ;j < S_v ;++ j ) { 11017float sum = 0.0f ; 11018ggml_vec_dot_f32 (S_v ,& sum ,0 ,& s_out [j * S_v ],0 ,k_d ,0 ,1 ); 11019delta [j ]= (v_d [j ]- sum )* beta_val ; 11020 } 11021 11022// outer product: S[i][j] += k[i] * delta[j] => M[j][i] += delta[j] * k[i] 11023for (int64_t j = 0 ;j < S_v ;++ j ) { 11024ggml_vec_mad_f32 (S_v ,& s_out [j * S_v ],k_d ,delta [j ]); 11025 } 11026 11027// attn_out[j] = sum_i S[i][j] * q[i] = dot(row j of M, q) 11028for (int64_t j = 0 ;j < S_v ;++ j ) { 11029float sum = 0.0f ; 11030ggml_vec_dot_f32 (S_v ,& sum ,0 ,& s_out [j * S_v ],0 ,q_d ,0 ,1 ); 11031attn_data [j ]= sum * scale ; 11032 } 11033 11034attn_data += S_v * H ;// advance to next token 11035 11036if (K > 1 ) { 11037const int64_t target_slot = n_tokens - 1 - t ; 11038if (target_slot >=0 && target_slot < K ) { 11039float * curr_state_o = state_out_base + target_slot * state_size_per_snap + 11040 (iv3 * H + iv1 )* S_v * S_v ; 11041memcpy (curr_state_o ,s_out ,S_v * S_v * sizeof (float )); 11042 } 11043 } 11044 } 11045 } 11046} 11047 11048 11049static void ggml_compute_forward_gated_delta_net_f32 ( 11050const ggml_compute_params * params , 11051ggml_tensor * dst ) { 11052 11053ggml_tensor * V = dst -> src [2 ]; 11054int64_t nr = V -> ne [1 ]* V -> ne [3 ]; 11055 11056// disable for NUMA 11057const bool disable_chunking = ggml_is_numa (); 11058 11059int nth = params -> nth ; 11060int ith = params -> ith ; 11061 11062// 4x chunks per thread 11063int nth_scaled = nth * 4 ; 11064int64_t chunk_size = (nr + nth_scaled - 1 ) /nth_scaled ; 11065int64_t nchunk = (nr + chunk_size - 1 ) /chunk_size ; 11066 11067if (nth == 1 || nchunk < nth || disable_chunking ) { 11068nchunk = nth ; 11069 } 11070 11071if (ith == 0 ) { 11072ggml_threadpool_chunk_set (params -> threadpool ,nth ); 11073 } 11074 11075ggml_barrier (params -> threadpool ); 11076 11077const int64_t dr = (nr + nchunk - 1 ) /nchunk ; 11078 11079int current_chunk = ith ; 11080 11081while (current_chunk < nchunk ) { 11082const int64_t ir0 = dr * current_chunk ; 11083const int64_t ir1 = MIN (ir0 + dr ,nr ); 11084 11085ggml_compute_forward_gated_delta_net_one_chunk (params ,dst ,ir0 ,ir1 ); 11086current_chunk = ggml_threadpool_chunk_add (params -> threadpool ,1 ); 11087 } 11088} 11089 11090void ggml_compute_forward_gated_delta_net ( 11091const ggml_compute_params * params , 11092ggml_tensor * dst ) { 11093const ggml_tensor * src0 = dst -> src [0 ]; 11094 11095switch (src0 -> type ) { 11096case GGML_TYPE_F32 : 11097 { 11098ggml_compute_forward_gated_delta_net_f32 (params ,dst ); 11099 }break ; 11100default : 11101 { 11102GGML_ABORT ("fatal error" ); 11103 } 11104 } 11105} 11106 11107 11108// ggml_compute_forward_dsv4_hc_comb 11109 11110static void ggml_dsv4_hc_comb_norm_cols (float * comb ,float eps ) { 11111constexpr int64_t hc = 4 ; 11112 11113for (int64_t idst = 0 ;idst < hc ;++ idst ) { 11114float sum = eps ; 11115for (int64_t isrc = 0 ;isrc < hc ;++ isrc ) { 11116sum += comb [idst + hc * isrc ]; 11117 } 11118 11119const float inv_sum = 1.0f /sum ; 11120for (int64_t isrc = 0 ;isrc < hc ;++ isrc ) { 11121comb [idst + hc * isrc ] *=inv_sum ; 11122 } 11123 } 11124} 11125 11126static void ggml_dsv4_hc_comb_norm_rows (float * comb ,float eps ) { 11127constexpr int64_t hc = 4 ; 11128 11129for (int64_t isrc = 0 ;isrc < hc ;++ isrc ) { 11130float sum = eps ; 11131for (int64_t idst = 0 ;idst < hc ;++ idst ) { 11132sum += comb [idst + hc * isrc ]; 11133 } 11134 11135const float inv_sum = 1.0f /sum ; 11136for (int64_t idst = 0 ;idst < hc ;++ idst ) { 11137comb [idst + hc * isrc ] *=inv_sum ; 11138 } 11139 } 11140} 11141 11142static void ggml_compute_forward_dsv4_hc_comb_f32 ( 11143const ggml_compute_params * params , 11144ggml_tensor * dst ) { 11145const ggml_tensor * mixes = dst -> src [0 ]; 11146const ggml_tensor * scale = dst -> src [1 ]; 11147const ggml_tensor * base = dst -> src [2 ]; 11148 11149GGML_ASSERT (mixes -> type == GGML_TYPE_F32 ); 11150GGML_ASSERT (scale -> type == GGML_TYPE_F32 ); 11151GGML_ASSERT (base -> type == GGML_TYPE_F32 ); 11152GGML_ASSERT (dst -> type == GGML_TYPE_F32 ); 11153 11154constexpr int64_t hc = 4 ; 11155constexpr int64_t comb_offset = 2 * hc ; 11156constexpr int64_t hc_mix_dim = (2 + hc )* hc ; 11157 11158const int64_t n_tokens = mixes -> ne [1 ]; 11159 11160GGML_ASSERT (mixes -> ne [0 ]== hc_mix_dim ); 11161GGML_ASSERT (dst -> ne [0 ]== hc ); 11162GGML_ASSERT (dst -> ne [1 ]== hc ); 11163GGML_ASSERT (dst -> ne [2 ]== n_tokens ); 11164GGML_ASSERT (scale -> ne [0 ] >=3 ); 11165GGML_ASSERT (base -> ne [0 ]== hc_mix_dim ); 11166 11167GGML_TENSOR_LOCALS (size_t ,nbm ,mixes ,nb ); 11168GGML_TENSOR_LOCALS (size_t ,nbs ,scale ,nb ); 11169GGML_TENSOR_LOCALS (size_t ,nbb ,base ,nb ); 11170GGML_TENSOR_LOCALS (size_t ,nbd ,dst ,nb ); 11171 11172const float eps = ggml_get_op_params_f32 (dst ,0 ); 11173const int32_t n_iter = ggml_get_op_params_i32 (dst ,1 ); 11174GGML_ASSERT (n_iter > 0 ); 11175 11176const int ith = params -> ith ; 11177const int nth = params -> nth ; 11178 11179const int64_t dr = (n_tokens + nth - 1 ) /nth ; 11180const int64_t it0 = dr * ith ; 11181const int64_t it1 = MIN (it0 + dr ,n_tokens ); 11182 11183const float scale_comb = * (const float * ) ((const char * )scale -> data + 2 * nbs0 ); 11184 11185for (int64_t it = it0 ;it < it1 ;++ it ) { 11186float comb [hc * hc ]; 11187 11188for (int64_t isrc = 0 ;isrc < hc ;++ isrc ) { 11189float max = - INFINITY ; 11190for (int64_t idst = 0 ;idst < hc ;++ idst ) { 11191const int64_t idx = idst + hc * isrc ; 11192const float xv = * (const float * ) ((const char * )mixes -> data + (comb_offset + idx )* nbm0 + it * nbm1 ); 11193const float bv = * (const float * ) ((const char * )base -> data + (comb_offset + idx )* nbb0 ); 11194const float v = xv * scale_comb + bv ; 11195comb [idx ]= v ; 11196max = MAX (max ,v ); 11197 } 11198 11199float sum = 0.0f ; 11200for (int64_t idst = 0 ;idst < hc ;++ idst ) { 11201const int64_t idx = idst + hc * isrc ; 11202const float v = expf (comb [idx ]- max ); 11203comb [idx ]= v ; 11204sum += v ; 11205 } 11206 11207const float inv_sum = 1.0f /sum ; 11208for (int64_t idst = 0 ;idst < hc ;++ idst ) { 11209const int64_t idx = idst + hc * isrc ; 11210comb [idx ]= comb [idx ]* inv_sum + eps ; 11211 } 11212 } 11213 11214ggml_dsv4_hc_comb_norm_cols (comb ,eps ); 11215for (int32_t i = 1 ;i < n_iter ;++ i ) { 11216ggml_dsv4_hc_comb_norm_rows (comb ,eps ); 11217ggml_dsv4_hc_comb_norm_cols (comb ,eps ); 11218 } 11219 11220for (int64_t isrc = 0 ;isrc < hc ;++ isrc ) { 11221for (int64_t idst = 0 ;idst < hc ;++ idst ) { 11222const int64_t idx = idst + hc * isrc ; 11223* (float * ) ((char * )dst -> data + idst * nbd0 + isrc * nbd1 + it * nbd2 )= comb [idx ]; 11224 } 11225 } 11226 } 11227} 11228 11229void ggml_compute_forward_dsv4_hc_comb ( 11230const ggml_compute_params * params , 11231ggml_tensor * dst ) { 11232const ggml_tensor * src0 = dst -> src [0 ]; 11233 11234switch (src0 -> type ) { 11235case GGML_TYPE_F32 : 11236 { 11237ggml_compute_forward_dsv4_hc_comb_f32 (params ,dst ); 11238 }break ; 11239default : 11240 { 11241GGML_ABORT ("fatal error" ); 11242 } 11243 } 11244} 11245 11246// ggml_compute_forward_dsv4_hc_pre 11247 11248static void ggml_compute_forward_dsv4_hc_pre_f32 ( 11249const ggml_compute_params * params , 11250ggml_tensor * dst ) { 11251const ggml_tensor * x = dst -> src [0 ]; 11252const ggml_tensor * weights = dst -> src [1 ]; 11253 11254GGML_ASSERT (x -> type == GGML_TYPE_F32 ); 11255GGML_ASSERT (weights -> type == GGML_TYPE_F32 ); 11256GGML_ASSERT (dst -> type == GGML_TYPE_F32 ); 11257 11258const int64_t n_embd = x -> ne [0 ]; 11259const int64_t hc = x -> ne [1 ]; 11260const int64_t n_tokens = x -> ne [2 ]; 11261 11262GGML_ASSERT (dst -> ne [0 ]== n_embd ); 11263GGML_ASSERT (dst -> ne [1 ]== n_tokens ); 11264GGML_ASSERT (weights -> ne [0 ]== hc ); 11265GGML_ASSERT (weights -> ne [1 ]== n_tokens ); 11266 11267GGML_TENSOR_LOCALS (size_t ,nbx ,x ,nb ); 11268GGML_TENSOR_LOCALS (size_t ,nbw ,weights ,nb ); 11269GGML_TENSOR_LOCALS (size_t ,nbd ,dst ,nb ); 11270 11271const int ith = params -> ith ; 11272const int nth = params -> nth ; 11273 11274const int64_t nr = n_embd * n_tokens ; 11275const int64_t dr = (nr + nth - 1 ) /nth ; 11276const int64_t ir0 = dr * ith ; 11277const int64_t ir1 = MIN (ir0 + dr ,nr ); 11278 11279for (int64_t ir = ir0 ;ir < ir1 ;++ ir ) { 11280const int64_t i0 = ir %n_embd ; 11281const int64_t it = ir /n_embd ; 11282 11283float sum = 0.0f ; 11284for (int64_t ih = 0 ;ih < hc ;++ ih ) { 11285const float xv = * (const float * ) ((const char * )x -> data + i0 * nbx0 + ih * nbx1 + it * nbx2 ); 11286const float wv = * (const float * ) ((const char * )weights -> data + ih * nbw0 + it * nbw1 ); 11287sum += xv * wv ; 11288 } 11289 11290* (float * ) ((char * )dst -> data + i0 * nbd0 + it * nbd1 )= sum ; 11291 } 11292} 11293 11294void ggml_compute_forward_dsv4_hc_pre ( 11295const ggml_compute_params * params , 11296ggml_tensor * dst ) { 11297const ggml_tensor * src0 = dst -> src [0 ]; 11298 11299switch (src0 -> type ) { 11300case GGML_TYPE_F32 : 11301 { 11302ggml_compute_forward_dsv4_hc_pre_f32 (params ,dst ); 11303 }break ; 11304default : 11305 { 11306GGML_ABORT ("fatal error" ); 11307 } 11308 } 11309} 11310 11311// ggml_compute_forward_dsv4_hc_post 11312 11313static void ggml_compute_forward_dsv4_hc_post_f32 ( 11314const ggml_compute_params * params , 11315ggml_tensor * dst ) { 11316const ggml_tensor * x = dst -> src [0 ]; 11317const ggml_tensor * residual = dst -> src [1 ]; 11318const ggml_tensor * post = dst -> src [2 ]; 11319const ggml_tensor * comb = dst -> src [3 ]; 11320 11321GGML_ASSERT (x -> type == GGML_TYPE_F32 ); 11322GGML_ASSERT (residual -> type == GGML_TYPE_F32 ); 11323GGML_ASSERT (post -> type == GGML_TYPE_F32 ); 11324GGML_ASSERT (comb -> type == GGML_TYPE_F32 ); 11325GGML_ASSERT (dst -> type == GGML_TYPE_F32 ); 11326 11327const int64_t n_embd = x -> ne [0 ]; 11328const int64_t n_tokens = x -> ne [1 ]; 11329const int64_t hc = residual -> ne [1 ]; 11330 11331GGML_ASSERT (dst -> ne [0 ]== n_embd ); 11332GGML_ASSERT (dst -> ne [1 ]== hc ); 11333GGML_ASSERT (dst -> ne [2 ]== n_tokens ); 11334GGML_ASSERT (residual -> ne [0 ]== n_embd ); 11335GGML_ASSERT (residual -> ne [2 ]== n_tokens ); 11336GGML_ASSERT (post -> ne [0 ]== hc ); 11337GGML_ASSERT (post -> ne [1 ]== n_tokens ); 11338GGML_ASSERT (comb -> ne [0 ]== hc ); 11339GGML_ASSERT (comb -> ne [1 ]== hc ); 11340GGML_ASSERT (comb -> ne [2 ]== n_tokens ); 11341 11342GGML_TENSOR_LOCALS (size_t ,nbx ,x ,nb ); 11343GGML_TENSOR_LOCALS (size_t ,nbr ,residual ,nb ); 11344GGML_TENSOR_LOCALS (size_t ,nbp ,post ,nb ); 11345GGML_TENSOR_LOCALS (size_t ,nbc ,comb ,nb ); 11346GGML_TENSOR_LOCALS (size_t ,nbd ,dst ,nb ); 11347 11348const int ith = params -> ith ; 11349const int nth = params -> nth ; 11350 11351const int64_t nr = n_embd * hc * n_tokens ; 11352const int64_t dr = (nr + nth - 1 ) /nth ; 11353const int64_t ir0 = dr * ith ; 11354const int64_t ir1 = MIN (ir0 + dr ,nr ); 11355 11356for (int64_t ir = ir0 ;ir < ir1 ;++ ir ) { 11357const int64_t i0 = ir %n_embd ; 11358const int64_t idst = (ir /n_embd ) %hc ; 11359const int64_t it = ir / (n_embd * hc ); 11360 11361const float xv = * (const float * ) ((const char * )x -> data + i0 * nbx0 + it * nbx1 ); 11362const float pv = * (const float * ) ((const char * )post -> data + idst * nbp0 + it * nbp1 ); 11363 11364float sum = xv * pv ; 11365for (int64_t isrc = 0 ;isrc < hc ;++ isrc ) { 11366const float rv = * (const float * ) ((const char * )residual -> data + i0 * nbr0 + isrc * nbr1 + it * nbr2 ); 11367const float cv = * (const float * ) ((const char * )comb -> data + idst * nbc0 + isrc * nbc1 + it * nbc2 ); 11368sum += rv * cv ; 11369 } 11370 11371* (float * ) ((char * )dst -> data + i0 * nbd0 + idst * nbd1 + it * nbd2 )= sum ; 11372 } 11373} 11374 11375void ggml_compute_forward_dsv4_hc_post ( 11376const ggml_compute_params * params , 11377ggml_tensor * dst ) { 11378const ggml_tensor * src0 = dst -> src [0 ]; 11379 11380switch (src0 -> type ) { 11381case GGML_TYPE_F32 : 11382 { 11383ggml_compute_forward_dsv4_hc_post_f32 (params ,dst ); 11384 }break ; 11385default : 11386 { 11387GGML_ABORT ("fatal error" ); 11388 } 11389 } 11390} 11391 11392// ggml_compute_forward_rwkv_wkv7 11393 11394static void ggml_compute_forward_rwkv_wkv7_f32 ( 11395const ggml_compute_params * params , 11396ggml_tensor * dst ) { 11397const int64_t T = dst -> src [1 ]-> ne [2 ]; 11398const int64_t C = dst -> ne [0 ]; 11399const int64_t HEADS = dst -> src [1 ]-> ne [1 ]; 11400const int64_t n_seqs = dst -> src [6 ]-> ne [1 ]; 11401const int64_t head_size = C /HEADS ; 11402 11403float * dst_data = (float * )dst -> data ; 11404float * state = ((float * )dst -> data )+ C * T ; 11405 11406const int ith = params -> ith ; 11407const int nth = params -> nth ; 11408 11409const int h_start = (HEADS * (ith )) /nth ; 11410const int h_end = ((HEADS * (ith + 1 )) /nth < HEADS ) ? 11411 (HEADS * (ith + 1 )) /nth :HEADS ; 11412 11413float * r = (float * )dst -> src [0 ]-> data ; 11414float * w = (float * )dst -> src [1 ]-> data ; 11415float * k = (float * )dst -> src [2 ]-> data ; 11416float * v = (float * )dst -> src [3 ]-> data ; 11417float * a = (float * )dst -> src [4 ]-> data ; 11418float * b = (float * )dst -> src [5 ]-> data ; 11419 11420int64_t t_stride = HEADS * head_size ;// Same to C 11421 11422int64_t h_stride = C /HEADS ; 11423GGML_ASSERT (C %HEADS == 0 );// C must be divisible by HEADS 11424int64_t h_stride_2d = head_size * head_size ; 11425 11426#if defined(GGML_SIMD ) 11427#if defined(__ARM_FEATURE_SVE )|| defined(__riscv_v_intrinsic ) 11428// scalar Route to scalar implementation //TODO: Write SVE code and RVV code 11429for (int64_t t = 0 ;t < T ;t ++ ) { 11430int64_t t_offset = t * t_stride ; 11431int64_t state_offset = head_size * C * (t / (T /n_seqs )); 11432float * state_cur = state + state_offset ; 11433float * state_prev = t % (T /n_seqs ) ?state_cur : (float * )dst -> src [6 ]-> data + state_offset ; 11434 11435for (int64_t h = h_start ;h < h_end ;h ++ ) { 11436int64_t h_offset = h * h_stride ; 11437int64_t t_h_offset = t_offset + h_offset ; 11438int64_t h_2d_offset = h * h_stride_2d ; 11439 11440for (int64_t i = 0 ;i < head_size ;i ++ ) { 11441int64_t t_h_i_offset = t_h_offset + i ; 11442int64_t h_2d_i_offset = h_2d_offset + i * h_stride ; 11443 11444float v_val = v [t_h_i_offset ]; 11445 11446float sa = 0 ,result = 0 ; 11447for (int64_t j = 0 ;j < head_size ;j ++ ) { 11448sa += a [t_h_offset + j ]* state_prev [h_2d_i_offset + j ]; 11449 } 11450 11451for (int64_t j = 0 ;j < head_size ;j ++ ) { 11452int64_t t_h_j_offset = t_h_offset + j ; 11453int64_t h_2d_i_j_offset = h_2d_i_offset + j ; 11454 11455float r_val = r [t_h_j_offset ]; 11456float w_val = w [t_h_j_offset ]; 11457float k_val = k [t_h_j_offset ]; 11458float b_val = b [t_h_j_offset ]; 11459float kv_val = v_val * k_val ; 11460float prev_state_val = state_prev [h_2d_i_j_offset ]; 11461state_cur [h_2d_i_j_offset ]= prev_state_val * w_val + kv_val + sa * b_val ; 11462result += state_cur [h_2d_i_j_offset ]* r_val ; 11463 } 11464dst_data [t_h_i_offset ]= result ; 11465 } 11466 } 11467 } 11468#else 11469for (int64_t t = 0 ;t < T ;t ++ ) { 11470int64_t t_offset = t * t_stride ; 11471int64_t state_offset = head_size * C * (t / (T /n_seqs )); 11472float * state_cur = state + state_offset ; 11473float * state_prev = t % (T /n_seqs ) ?state_cur : (float * )dst -> src [6 ]-> data + state_offset ; 11474 11475for (int64_t h = h_start ;h < h_end ;h ++ ) { 11476int64_t h_offset = h * h_stride ; 11477int64_t t_h_offset = t_offset + h_offset ; 11478int64_t h_2d_offset = h * h_stride_2d ; 11479 11480for (int64_t ii = 0 ;ii < head_size ;ii ++ ) { 11481int64_t t_h_i_offset = t_h_offset + ii ; 11482int64_t h_2d_i_offset = h_2d_offset + ii * h_stride ; 11483 11484GGML_F32_VEC v_vec = GGML_F32_VEC_SET1 (v [t_h_i_offset ]); 11485 11486float sa = 0 ; 11487 { 11488GGML_F32_VEC sum [GGML_F32_ARR ]= {GGML_F32_VEC_ZERO }; 11489GGML_F32_VEC ax [GGML_F32_ARR ]; 11490GGML_F32_VEC ay [GGML_F32_ARR ]; 11491for (int64_t j = 0 ;j < head_size ;j += GGML_F32_STEP ) { 11492for (int64_t kk = 0 ;kk < GGML_F32_ARR ;kk ++ ) { 11493ax [kk ]= GGML_F32_VEC_LOAD (& a [t_h_offset + j + kk * GGML_F32_EPR ]); 11494ay [kk ]= GGML_F32_VEC_LOAD (& state_prev [h_2d_i_offset + j + kk * GGML_F32_EPR ]); 11495sum [kk ]= GGML_F32_VEC_FMA (sum [kk ],ax [kk ],ay [kk ]); 11496 } 11497 } 11498GGML_F32_VEC_REDUCE (sa ,sum ); 11499 } 11500 11501GGML_F32_VEC sa_vec = GGML_F32_VEC_SET1 (sa ); 11502 11503int64_t j = 0 ; 11504GGML_F32_VEC result_vec [GGML_F32_ARR ]= {GGML_F32_VEC_ZERO }; 11505for (;j < head_size ;j += GGML_F32_STEP ) { 11506for (int64_t kk = 0 ;kk < GGML_F32_ARR ;kk ++ ) { 11507int64_t t_h_j_offset = t_h_offset + j + kk * GGML_F32_EPR ; 11508int64_t h_2d_i_j_offset = h_2d_i_offset + j + kk * GGML_F32_EPR ; 11509 11510GGML_F32_VEC r_vec = GGML_F32_VEC_LOAD (& r [t_h_j_offset ]); 11511GGML_F32_VEC w_vec = GGML_F32_VEC_LOAD (& w [t_h_j_offset ]); 11512GGML_F32_VEC k_vec = GGML_F32_VEC_LOAD (& k [t_h_j_offset ]); 11513GGML_F32_VEC b_vec = GGML_F32_VEC_LOAD (& b [t_h_j_offset ]); 11514 11515k_vec = GGML_F32_VEC_MUL (v_vec ,k_vec ); 11516 11517GGML_F32_VEC state_vec = GGML_F32_VEC_LOAD (& state_prev [h_2d_i_j_offset ]); 11518// kv + s * decay + sa * b 11519state_vec = GGML_F32_VEC_FMA (k_vec ,state_vec ,w_vec ); 11520state_vec = GGML_F32_VEC_FMA (state_vec ,sa_vec ,b_vec ); 11521GGML_F32_VEC_STORE (& state_cur [h_2d_i_j_offset ],state_vec ); 11522 11523result_vec [kk ]= GGML_F32_VEC_FMA (result_vec [kk ],state_vec ,r_vec ); 11524 } 11525 } 11526GGML_F32_VEC_REDUCE (dst_data [t_h_i_offset ],result_vec ); 11527 11528// There shouldn't be left-overs though. 11529for (;j < head_size ;j ++ ) { 11530int64_t t_h_j_offset = t_h_offset + j ; 11531int64_t h_2d_i_j_offset = h_2d_i_offset + j ; 11532 11533float r_val = r [t_h_j_offset ]; 11534float w_val = w [t_h_j_offset ]; 11535float k_val = k [t_h_j_offset ]; 11536float b_val = b [t_h_j_offset ]; 11537float kv_val = v [t_h_i_offset ]* k_val ; 11538 11539float prev_state_val = state_prev [h_2d_i_j_offset ]; 11540state_cur [h_2d_i_j_offset ]= prev_state_val * w_val + kv_val + sa * b_val ; 11541dst_data [t_h_i_offset ]+= state_cur [h_2d_i_j_offset ]* r_val ; 11542 } 11543 } 11544 } 11545 } 11546#endif 11547#else 11548for (int64_t t = 0 ;t < T ;t ++ ) { 11549int64_t t_offset = t * t_stride ; 11550int64_t state_offset = head_size * C * (t / (T /n_seqs )); 11551float * state_cur = state + state_offset ; 11552float * state_prev = t % (T /n_seqs ) ?state_cur : (float * )dst -> src [6 ]-> data + state_offset ; 11553 11554for (int64_t h = h_start ;h < h_end ;h ++ ) { 11555int64_t h_offset = h * h_stride ; 11556int64_t t_h_offset = t_offset + h_offset ; 11557int64_t h_2d_offset = h * h_stride_2d ; 11558 11559for (int64_t i = 0 ;i < head_size ;i ++ ) { 11560int64_t t_h_i_offset = t_h_offset + i ; 11561int64_t h_2d_i_offset = h_2d_offset + i * h_stride ; 11562 11563float v_val = v [t_h_i_offset ]; 11564 11565float sa = 0 ,result = 0 ; 11566for (int64_t j = 0 ;j < head_size ;j ++ ) { 11567sa += a [t_h_offset + j ]* state_prev [h_2d_i_offset + j ]; 11568 } 11569 11570for (int64_t j = 0 ;j < head_size ;j ++ ) { 11571int64_t t_h_j_offset = t_h_offset + j ; 11572int64_t h_2d_i_j_offset = h_2d_i_offset + j ; 11573 11574float r_val = r [t_h_j_offset ]; 11575float w_val = w [t_h_j_offset ]; 11576float k_val = k [t_h_j_offset ]; 11577float b_val = b [t_h_j_offset ]; 11578float kv_val = v_val * k_val ; 11579float prev_state_val = state_prev [h_2d_i_j_offset ]; 11580state_cur [h_2d_i_j_offset ]= prev_state_val * w_val + kv_val + sa * b_val ; 11581result += state_cur [h_2d_i_j_offset ]* r_val ; 11582 } 11583dst_data [t_h_i_offset ]= result ; 11584 } 11585 } 11586 } 11587#endif 11588} 11589 11590 11591void ggml_compute_forward_rwkv_wkv7 ( 11592const ggml_compute_params * params , 11593ggml_tensor * dst ) { 11594 11595const ggml_tensor * src0 = dst -> src [0 ]; 11596 11597switch (src0 -> type ) { 11598case GGML_TYPE_F32 : 11599 { 11600ggml_compute_forward_rwkv_wkv7_f32 (params ,dst ); 11601 }break ; 11602default : 11603 { 11604GGML_ABORT ("fatal error" ); 11605 } 11606 } 11607} 11608 11609// ggml_compute_forward_map_custom1 11610 11611void ggml_compute_forward_map_custom1 ( 11612const ggml_compute_params * params , 11613ggml_tensor * dst ) { 11614 11615const ggml_tensor * a = dst -> src [0 ]; 11616 11617struct ggml_map_custom1_op_params p ; 11618memcpy (& p ,dst -> op_params ,sizeof (p )); 11619 11620p .fun (dst ,a ,params -> ith ,params -> nth ,p .userdata ); 11621} 11622 11623// ggml_compute_forward_map_custom2 11624 11625void ggml_compute_forward_map_custom2 ( 11626const ggml_compute_params * params , 11627ggml_tensor * dst ) { 11628 11629const ggml_tensor * a = dst -> src [0 ]; 11630const ggml_tensor * b = dst -> src [1 ]; 11631 11632struct ggml_map_custom2_op_params p ; 11633memcpy (& p ,dst -> op_params ,sizeof (p )); 11634 11635p .fun (dst ,a ,b ,params -> ith ,params -> nth ,p .userdata ); 11636} 11637 11638// ggml_compute_forward_map_custom3 11639 11640void ggml_compute_forward_map_custom3 ( 11641const ggml_compute_params * params , 11642ggml_tensor * dst ) { 11643 11644const ggml_tensor * a = dst -> src [0 ]; 11645const ggml_tensor * b = dst -> src [1 ]; 11646const ggml_tensor * c = dst -> src [2 ]; 11647 11648struct ggml_map_custom3_op_params p ; 11649memcpy (& p ,dst -> op_params ,sizeof (p )); 11650 11651p .fun (dst ,a ,b ,c ,params -> ith ,params -> nth ,p .userdata ); 11652} 11653 11654// ggml_compute_forward_custom 11655 11656void ggml_compute_forward_custom ( 11657const struct ggml_compute_params * params , 11658struct ggml_tensor * dst ) { 11659 11660struct ggml_custom_op_params p ; 11661memcpy (& p ,dst -> op_params ,sizeof (p )); 11662 11663p .fun (dst ,params -> ith ,params -> nth ,p .userdata ); 11664} 11665 11666// ggml_compute_forward_cross_entropy_loss 11667 11668static void ggml_compute_forward_cross_entropy_loss_f32 ( 11669const ggml_compute_params * params , 11670ggml_tensor * dst ) { 11671 11672const ggml_tensor * src0 = dst -> src [0 ]; 11673const ggml_tensor * src1 = dst -> src [1 ]; 11674 11675GGML_ASSERT (src0 -> type == GGML_TYPE_F32 ); 11676GGML_ASSERT (src1 -> type == GGML_TYPE_F32 ); 11677GGML_ASSERT (src0 -> nb [0 ]== ggml_type_size (src0 -> type )); 11678GGML_ASSERT (src1 -> nb [0 ]== ggml_type_size (src1 -> type )); 11679GGML_ASSERT (ggml_are_same_shape (src0 ,src1 )); 11680GGML_ASSERT (ggml_is_scalar (dst )); 11681GGML_ASSERT (dst -> type == GGML_TYPE_F32 ); 11682 11683// TODO: handle transposed/permuted matrices 11684const int64_t nc = src0 -> ne [0 ]; 11685const int64_t nr = ggml_nrows (src0 ); 11686 11687const int ith = params -> ith ; 11688const int nth = params -> nth ; 11689 11690float * sums = (float * )params -> wdata ; 11691float * st = ((float * )params -> wdata )+ nth + ith * nc ; 11692float sum_thread = 0.0f ; 11693 11694GGML_ASSERT (params -> wsize >=sizeof (float )* (nth + nth * nc )); 11695 11696// rows per thread 11697const int64_t dr = (nr + nth - 1 )/nth ; 11698 11699// row range for this thread 11700const int64_t ir0 = dr * ith ; 11701const int64_t ir1 = MIN (ir0 + dr ,nr ); 11702 11703for (int64_t i1 = ir0 ;i1 < ir1 ;++ i1 ) { 11704const float * s0 = (const float * )((const char * )src0 -> data + i1 * src0 -> nb [1 ]); 11705const float * s1 = (const float * )((const char * )src1 -> data + i1 * src1 -> nb [1 ]); 11706 11707#ifndef NDEBUG 11708for (int64_t i = 0 ;i < nc ;++ i ) { 11709//printf("p[%d] = %f\n", i, p[i]); 11710assert (!isnan (s0 [i ])); 11711assert (!isnan (s1 [i ])); 11712 } 11713#endif // NDEBUG 11714 11715float max = - INFINITY ; 11716ggml_vec_max_f32 (nc ,& max ,s0 ); 11717const ggml_float sum_softmax = ggml_vec_log_soft_max_f32 (nc ,st ,s0 ,max ); 11718assert (sum_softmax >=0.0 ); 11719 11720ggml_vec_add1_f32 (nc ,st ,st ,- sum_softmax ); 11721ggml_vec_mul_f32 (nc ,st ,st ,s1 ); 11722 11723float sum_st = 0.0f ; 11724ggml_vec_sum_f32 (nc ,& sum_st ,st ); 11725sum_thread += sum_st ; 11726 11727#ifndef NDEBUG 11728for (int64_t i = 0 ;i < nc ;++ i ) { 11729assert (!isnan (st [i ])); 11730assert (!isinf (st [i ])); 11731 } 11732#endif // NDEBUG 11733 } 11734sums [ith ]= sum_thread ; 11735ggml_barrier (params -> threadpool ); 11736 11737if (ith == 0 ) { 11738float * dp = (float * )dst -> data ; 11739ggml_vec_sum_f32 (nth ,dp ,sums ); 11740dp [0 ] *=-1.0f / (float )nr ; 11741 } 11742} 11743 11744void ggml_compute_forward_cross_entropy_loss ( 11745const ggml_compute_params * params , 11746ggml_tensor * dst ) { 11747 11748const ggml_tensor * src0 = dst -> src [0 ]; 11749 11750switch (src0 -> type ) { 11751case GGML_TYPE_F32 : 11752 { 11753ggml_compute_forward_cross_entropy_loss_f32 (params ,dst ); 11754 }break ; 11755default : 11756 { 11757GGML_ABORT ("fatal error" ); 11758 } 11759 } 11760} 11761 11762// ggml_compute_forward_cross_entropy_loss_back 11763 11764static void ggml_compute_forward_cross_entropy_loss_back_f32 ( 11765const ggml_compute_params * params , 11766ggml_tensor * dst ) { 11767 11768const ggml_tensor * grad = dst -> src [0 ];// gradient of forward pass output 11769const ggml_tensor * src0f = dst -> src [1 ];// src0 of forward pass 11770const ggml_tensor * src1f = dst -> src [2 ];// src1 of forward pass 11771 11772GGML_ASSERT (ggml_is_contiguous (dst )); 11773GGML_ASSERT (ggml_is_contiguous (src0f )); 11774GGML_ASSERT (ggml_is_contiguous (src1f )); 11775GGML_ASSERT (ggml_is_contiguous (grad )); 11776GGML_ASSERT (ggml_are_same_shape (src0f ,src1f )&& ggml_are_same_shape (src0f ,dst )); 11777 11778const int64_t ith = params -> ith ; 11779const int64_t nth = params -> nth ; 11780 11781// TODO: handle transposed/permuted matrices 11782const int64_t nc = src0f -> ne [0 ]; 11783const int64_t nr = ggml_nrows (src0f ); 11784 11785// rows per thread 11786const int64_t dr = (nr + nth - 1 )/nth ; 11787 11788// row range for this thread 11789const int64_t ir0 = dr * ith ; 11790const int64_t ir1 = MIN (ir0 + dr ,nr ); 11791 11792const float d_by_nr = ((const float * )grad -> data )[0 ] / (float )nr ; 11793 11794for (int64_t i1 = ir0 ;i1 < ir1 ;i1 ++ ) { 11795float * ds0 = (float * )((char * )dst -> data + i1 * dst -> nb [1 ]); 11796const float * s0 = (const float * )((const char * )src0f -> data + i1 * src0f -> nb [1 ]); 11797const float * s1 = (const float * )((const char * )src1f -> data + i1 * src1f -> nb [1 ]); 11798 11799#ifndef NDEBUG 11800for (int64_t i = 0 ;i < nc ;++ i ) { 11801//printf("p[%d] = %f\n", i, p[i]); 11802assert (!isnan (s0 [i ])); 11803assert (!isnan (s1 [i ])); 11804 } 11805#endif // NDEBUG 11806 11807// soft_max 11808float max = - INFINITY ; 11809ggml_vec_max_f32 (nc ,& max ,s0 ); 11810const ggml_float sum = ggml_vec_soft_max_f32 (nc ,ds0 ,s0 ,max ); 11811assert (sum > 0.0 ); 11812ggml_vec_scale_f32 (nc ,ds0 ,1.0 /sum ); 11813 11814// grad(src0f) = (softmax(src0f) - src1f) * grad(cross_entropy_loss(src0f, src1f)) / nr 11815ggml_vec_sub_f32 (nc ,ds0 ,ds0 ,s1 ); 11816ggml_vec_scale_f32 (nc ,ds0 ,d_by_nr ); 11817 11818#ifndef NDEBUG 11819for (int64_t i = 0 ;i < nc ;++ i ) { 11820assert (!isnan (ds0 [i ])); 11821assert (!isinf (ds0 [i ])); 11822 } 11823#endif // NDEBUG 11824 } 11825} 11826 11827void ggml_compute_forward_cross_entropy_loss_back ( 11828const ggml_compute_params * params , 11829ggml_tensor * dst ) { 11830 11831const ggml_tensor * src0 = dst -> src [0 ]; 11832 11833switch (src0 -> type ) { 11834case GGML_TYPE_F32 : 11835 { 11836ggml_compute_forward_cross_entropy_loss_back_f32 (params ,dst ); 11837 }break ; 11838default : 11839 { 11840GGML_ABORT ("fatal error" ); 11841 } 11842 } 11843} 11844 11845static void ggml_compute_forward_opt_step_adamw_f32 ( 11846const ggml_compute_params * params , 11847ggml_tensor * dst ) { 11848 11849const ggml_tensor * src0 = dst -> src [0 ]; 11850const ggml_tensor * src0_grad = dst -> src [1 ]; 11851const ggml_tensor * src0_grad_m = dst -> src [2 ]; 11852const ggml_tensor * src0_grad_v = dst -> src [3 ]; 11853const ggml_tensor * adamw_params = dst -> src [4 ]; 11854 11855GGML_ASSERT (ggml_are_same_shape (src0 ,src0_grad )); 11856GGML_ASSERT (ggml_are_same_shape (src0 ,src0_grad_m )); 11857GGML_ASSERT (ggml_are_same_shape (src0 ,src0_grad_v )); 11858GGML_ASSERT (ggml_nelements (adamw_params )== 7 ); 11859 11860const int ith = params -> ith ; 11861const int nth = params -> nth ; 11862 11863const int nr = ggml_nrows (src0 ); 11864 11865GGML_TENSOR_UNARY_OP_LOCALS 11866GGML_ASSERT (nb00 == sizeof (float )); 11867 11868// rows per thread 11869const int dr = (nr + nth - 1 )/nth ; 11870 11871// row range for this thread 11872const int ir0 = dr * ith ; 11873const int ir1 = MIN (ir0 + dr ,nr ); 11874 11875const float * adamw_params_ptr = ggml_get_data_f32 (adamw_params ); 11876 11877const float alpha = adamw_params_ptr [0 ]; 11878const float beta1 = adamw_params_ptr [1 ]; 11879const float beta2 = adamw_params_ptr [2 ]; 11880const float eps = adamw_params_ptr [3 ]; 11881const float wd = adamw_params_ptr [4 ]; 11882const float beta1h = adamw_params_ptr [5 ]; 11883const float beta2h = adamw_params_ptr [6 ]; 11884const float keep = 1.f - alpha * wd ; 11885for (int ir = ir0 ;ir < ir1 ;++ ir ) { 11886const int64_t i03 = ir /(ne02 * ne01 ); 11887const int64_t i02 = (ir - i03 * ne02 * ne01 )/ne01 ; 11888const int64_t i01 = (ir - i03 * ne02 * ne01 - i02 * ne01 ); 11889 11890const size_t offset = i03 * nb03 + i02 * nb02 + i01 * nb01 ; 11891 11892float * w = (float * ) ((char * )src0 -> data + offset );// weight 11893const float * g = (const float * ) ((const char * )src0_grad -> data + offset );// grad 11894float * m = (float * ) ((char * )src0_grad_m -> data + offset ); 11895float * v = (float * ) ((char * )src0_grad_v -> data + offset ); 11896 11897for (int i00 = 0 ;i00 < ne00 ;++ i00 ) { 11898m [i00 ]= m [i00 ]* beta1 + g [i00 ]* (1.0f - beta1 ); 11899v [i00 ]= v [i00 ]* beta2 + g [i00 ]* g [i00 ]* (1.0f - beta2 ); 11900 11901const float mh = m [i00 ]* beta1h ; 11902const float vh = sqrtf (v [i00 ]* beta2h )+ eps ; 11903 11904// The weight decay is applied independently of the Adam momenta m and v. 11905// This is NOT equivalent to l2 regularization that adds w[i00]*w[i00] to the loss. 11906// See: https://arxiv.org/pdf/1711.05101v3.pdf 11907w [i00 ]= w [i00 ]* keep - alpha * mh /vh ; 11908 } 11909 } 11910} 11911 11912void ggml_compute_forward_opt_step_adamw ( 11913const ggml_compute_params * params , 11914ggml_tensor * dst ) { 11915 11916const ggml_tensor * src0 = dst -> src [0 ]; 11917 11918switch (src0 -> type ) { 11919case GGML_TYPE_F32 : 11920 { 11921ggml_compute_forward_opt_step_adamw_f32 (params ,dst ); 11922 }break ; 11923default : 11924 { 11925GGML_ABORT ("fatal error" ); 11926 } 11927 } 11928} 11929 11930static void ggml_compute_forward_opt_step_sgd_f32 (const ggml_compute_params * params ,ggml_tensor * dst ) { 11931const ggml_tensor * src0 = dst -> src [0 ]; 11932const ggml_tensor * src0_grad = dst -> src [1 ]; 11933const ggml_tensor * sgd_params = dst -> src [2 ]; 11934 11935GGML_ASSERT (ggml_are_same_shape (src0 ,src0_grad )); 11936GGML_ASSERT (ggml_nelements (sgd_params )== 2 ); 11937 11938const int ith = params -> ith ; 11939const int nth = params -> nth ; 11940 11941const int nr = ggml_nrows (src0 ); 11942 11943GGML_TENSOR_UNARY_OP_LOCALS 11944GGML_ASSERT (nb00 == sizeof (float )); 11945 11946// rows per thread 11947const int dr = (nr + nth - 1 ) /nth ; 11948 11949// row range for this thread 11950const int ir0 = dr * ith ; 11951const int ir1 = MIN (ir0 + dr ,nr ); 11952 11953// using adamw param subset we care about - alpha, wd - could have a separate struct 11954const float * sgd_params_ptr = ggml_get_data_f32 (sgd_params ); 11955const float alpha = sgd_params_ptr [0 ]; 11956const float keep = 1.f - alpha * sgd_params_ptr [1 ]; 11957 11958for (int ir = ir0 ;ir < ir1 ;++ ir ) { 11959const int64_t i03 = ir / (ne02 * ne01 ); 11960const int64_t i02 = (ir - i03 * ne02 * ne01 ) /ne01 ; 11961const int64_t i01 = (ir - i03 * ne02 * ne01 - i02 * ne01 ); 11962 11963const size_t offset = i03 * nb03 + i02 * nb02 + i01 * nb01 ; 11964 11965float * w = (float * ) ((char * )src0 -> data + offset );// weight 11966const float * g = (const float * ) ((const char * )src0_grad -> data + offset );// grad 11967 11968for (int i00 = 0 ;i00 < ne00 ;++ i00 ) { 11969w [i00 ]= w [i00 ]* keep - alpha * g [i00 ]; 11970 } 11971 } 11972} 11973 11974void ggml_compute_forward_opt_step_sgd (const ggml_compute_params * params ,ggml_tensor * dst ) { 11975const ggml_tensor * src0 = dst -> src [0 ]; 11976 11977switch (src0 -> type ) { 11978case GGML_TYPE_F32 : 11979 { 11980ggml_compute_forward_opt_step_sgd_f32 (params ,dst ); 11981 } 11982break ; 11983default : 11984 { 11985GGML_ABORT ("fatal error - sgd is F32 only" ); 11986 } 11987 } 11988} 11989 11990static void ggml_compute_forward_fwht_f32 (const ggml_compute_params * params ,ggml_tensor * dst ) { 11991const ggml_tensor * src0 = dst -> src [0 ]; 11992const ggml_tensor * src1 = dst -> src [1 ]; 11993 11994GGML_ASSERT (src1 -> type == GGML_TYPE_F32 ); 11995GGML_ASSERT (dst -> type == GGML_TYPE_F32 ); 11996 11997GGML_TENSOR_BINARY_OP_LOCALS 11998 11999const int ith = params -> ith ; 12000const int nth = params -> nth ; 12001 12002const int64_t n = ne10 ; 12003GGML_ASSERT ((n & (n - 1 ))== 0 );// must be power of 2 12004 12005const int64_t nr = ne11 * ne12 * ne13 ; 12006const int64_t rows_per_thread = (nr + nth - 1 ) /nth ; 12007const int64_t start_row = ith * rows_per_thread ; 12008const int64_t end_row = MIN (start_row + rows_per_thread ,nr ); 12009 12010const float scale = 1.0f /sqrtf ((float )n ); 12011 12012#if defined(GGML_SIMD ) 12013const GGML_F32_VEC v_minus_one = GGML_F32_VEC_SET1 (-1.0f ); 12014#endif 12015 12016for (int64_t r = start_row ;r < end_row ;r ++ ) { 12017const int64_t i13 = r / (ne11 * ne12 ); 12018const int64_t i12 = (r - i13 * ne11 * ne12 ) /ne11 ; 12019const int64_t i11 = r - i13 * ne11 * ne12 - i12 * ne11 ; 12020 12021const float * src_row = (const float * ) ((const char * )src1 -> data + i11 * nb11 + i12 * nb12 + i13 * nb13 ); 12022float * dst_row = (float * ) ((char * )dst -> data + i11 * nb1 + i12 * nb2 + i13 * nb3 ); 12023 12024for (int64_t j = 0 ;j < n ;j ++ ) { 12025dst_row [j ]= src_row [j ]* scale ; 12026 } 12027 12028// Scalar passes 12029#if defined(GGML_SIMD ) 12030#if defined(__ARM_FEATURE_SVE ) 12031const int step = svcntw (); 12032#else 12033const int step = GGML_F32_EPR ; 12034#endif 12035#else 12036const int step = n ; 12037#endif 12038for (int64_t len = 1 ;len < step && len < n ;len <<=1 ) { 12039for (int64_t i = 0 ;i < n ;i += 2 * len ) { 12040for (int64_t j = 0 ;j < len ;j ++ ) { 12041float u = dst_row [i + j ]; 12042float v = dst_row [i + len + j ]; 12043dst_row [i + j ]= u + v ; 12044dst_row [i + len + j ]= u - v ; 12045 } 12046 } 12047 } 12048 12049// SIMD passes using GGML_F32_VEC_* macros for multi-architecture support 12050#if defined(GGML_SIMD ) 12051for (int64_t len = step ;len < n ;len <<=1 ) { 12052for (int64_t i = 0 ;i < n ;i += 2 * len ) { 12053for (int64_t j = 0 ;j < len ;j += step ) { 12054GGML_F32_VEC u = GGML_F32_VEC_LOAD (dst_row + i + j ); 12055GGML_F32_VEC v = GGML_F32_VEC_LOAD (dst_row + i + len + j ); 12056 12057GGML_F32_VEC_STORE (dst_row + i + j ,GGML_F32_VEC_ADD (u ,v )); 12058GGML_F32_VEC_STORE (dst_row + i + len + j ,GGML_F32_VEC_FMA (u ,v ,v_minus_one )); 12059 } 12060 } 12061 } 12062#endif 12063 } 12064} 12065 12066void ggml_compute_forward_fwht (const ggml_compute_params * params ,ggml_tensor * dst ) { 12067const ggml_tensor * src1 = dst -> src [1 ]; 12068 12069switch (src1 -> type ) { 12070case GGML_TYPE_F32 : 12071 { 12072ggml_compute_forward_fwht_f32 (params ,dst ); 12073 } 12074break ; 12075default : 12076 { 12077GGML_ABORT ("fatal error - fwht is F32 only" ); 12078 } 12079 } 12080} 12081 12082// ggml_compute_forward_lightning_indexer 12083 12084void ggml_compute_forward_lightning_indexer ( 12085const ggml_compute_params * params , 12086ggml_tensor * dst ) { 12087 12088const ggml_tensor * q = dst -> src [0 ]; 12089const ggml_tensor * k = dst -> src [1 ]; 12090const ggml_tensor * w = dst -> src [2 ];// weights 12091const ggml_tensor * m = dst -> src [3 ];// mask 12092 12093GGML_ASSERT (dst -> type == GGML_TYPE_F32 ); 12094GGML_ASSERT (q -> type == GGML_TYPE_F32 ); 12095GGML_ASSERT (w -> type == GGML_TYPE_F32 ); 12096GGML_ASSERT (m -> type == GGML_TYPE_F16 ); 12097 12098GGML_TENSOR_LOCALS (int64_t ,neq ,q ,ne ) 12099GGML_TENSOR_LOCALS (size_t ,nbq ,q ,nb ) 12100GGML_TENSOR_LOCALS (int64_t ,nek ,k ,ne ) 12101GGML_TENSOR_LOCALS (size_t ,nbk ,k ,nb ) 12102GGML_TENSOR_LOCALS (int64_t ,new ,w ,ne ) 12103GGML_TENSOR_LOCALS (size_t ,nbw ,w ,nb ) 12104GGML_TENSOR_LOCALS (int64_t ,nem ,m ,ne ) 12105GGML_TENSOR_LOCALS (size_t ,nbm ,m ,nb ) 12106GGML_TENSOR_LOCALS (int64_t ,ne ,dst ,ne ) 12107GGML_TENSOR_LOCALS (size_t ,nb ,dst ,nb ) 12108 12109GGML_ASSERT (nb0 == ggml_type_size (dst -> type )); 12110GGML_ASSERT (nbq0 == ggml_type_size (q -> type )); 12111GGML_ASSERT (nbk0 == ggml_type_size (k -> type )); 12112GGML_ASSERT (nbw0 == ggml_type_size (w -> type )); 12113GGML_ASSERT (nbm0 == ggml_type_size (m -> type )); 12114 12115const int n_embd = q -> ne [0 ]; 12116const int n_head = q -> ne [1 ]; 12117const int n_tokens = q -> ne [2 ]; 12118const int n_stream = q -> ne [3 ]; 12119const int n_kv = k -> ne [2 ]; 12120 12121ggml_to_float_t const k_to_float = ggml_get_type_traits (k -> type )-> to_float ; 12122GGML_ASSERT ((k -> type == GGML_TYPE_F32 || k_to_float )&& "lightning indexer: unsupported K-type" ); 12123 12124const int nr = n_kv ; 12125const int ith = params -> ith ; 12126const int nth = params -> nth ; 12127 12128// (temporary) buffer for K converted to float 12129float * k_row_f32 = (float * )params -> wdata + ith * (1 * n_embd + CACHE_LINE_SIZE_F32 ); 12130 12131// rows per thread 12132const int dr = (nr + nth - 1 )/nth ; 12133 12134// row range for this thread 12135const int ir0 = dr * ith ; 12136const int ir1 = MIN (ir0 + dr ,nr ); 12137 12138for (int s = 0 ;s < n_stream ;++ s ) { 12139for (int t = 0 ;t < n_tokens ;++ t ) { 12140const float * w_row = (float * ) ((char * )w -> data + t * nbw1 + s * nbw3 ); 12141const ggml_fp16_t * m_row = (ggml_fp16_t * ) ((char * )m -> data + t * nbm1 + (s %nem3 )* nbm3 ); 12142float * dst_row = (float * ) ((char * )dst -> data + t * nb1 + s * nb3 ); 12143for (int ik = ir0 ;ik < ir1 ;++ ik ) { 12144char * k_row = (char * )k -> data + ik * nbk2 + s * nbk3 ; 12145if (k_to_float ) { 12146k_to_float (k_row ,k_row_f32 ,n_embd ); 12147 }else { 12148k_row_f32 = (float * )k_row ; 12149 } 12150float score = 0.0f ; 12151for (int h = 0 ;h < n_head ;++ h ) { 12152// dot product of q and k for head h 12153float qk = 0.0f ; 12154const float * q_row = (float * ) ((char * )q -> data + h * nbq1 + t * nbq2 + s * nbq3 ); 12155ggml_vec_dot_f32 (n_embd ,& qk ,0 ,q_row ,0 ,k_row_f32 ,0 ,1 ); 12156// ReLU and weights (prescaled) 12157score += MAX (qk ,0.0f )* w_row [h ]; 12158 } 12159// apply mask 12160dst_row [ik ]= score + GGML_CPU_FP16_TO_FP32 (m_row [ik ]); 12161 } 12162 } 12163 } 12164}