Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-cann/aclnn_ops.cpp # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-cann / aclnn_ops.cpp` 4479 lines · 217271 bytes Raw Blame History 1 /* 2 * Copyright (c) 2023-2026 The ggml authors 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a copy 5 * of this software and associated documentation files (the "Software"), to 6 * deal in the Software without restriction, including without limitation the 7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 * sell copies of the Software, and to permit persons to whom the Software is 9 * furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 * IN THE SOFTWARE. 21 */ 22 23 #include "aclnn_ops.h" 24 25 #include "ggml-impl.h" 26 #include "ggml.h" 27 28 29 #include 30 #include 31 #include 32 #include 33 #include 34 #include 35 #include 36 #include 37 #include 38 #include 39 #include 40 #include 41 #include 42 #include 43 #include 44 #include 45 #include 46 #include 47 #include 48 #include 49 #include 50 #include 51 #include 52 #include 53 #include 54 #include 55 #include 56 #include 57 #include 58 #include 59 #include 60 #include 61 #include 62 #include 63 #include 64 #include 65 #include 66 #include 67 #include 68 #include 69 #include 70 #include 71 #include 72 #include 73 #include 74 #include 75 #include 76 #include 77 #include 78 #include 79 #include 80 #include 81 #include 82 #include 83 #include 84 #include 85 #include 86 #include 87 #include 88 #include 89 90 #include 91 #include 92 #include 93 #include 94 95 #define GGML_COMMON_DECL_C 96 97 #include "../ggml-common.h" 98 99 void bcast_shape(ggml_tensor * src0, 100 ggml_tensor * src1, 101 ggml_tensor * dst, 102 acl_tensor_ptr & acl_src0, 103 acl_tensor_ptr & acl_src1, 104 acl_tensor_ptr & acl_dst) { 105 GGML_ASSERT(ggml_are_same_shape(src0, dst) && ggml_can_repeat(src1, src0)); 106 // Need bcast 107 if (!ggml_are_same_shape(src0, src1) && ggml_cann_need_bcast(src0, src1)) { 108 BCAST_SHAPE(src0, src1) 109 acl_src0 = ggml_cann_create_tensor(src0, BCAST_PARAM(src0)); 110 acl_src1 = ggml_cann_create_tensor(src1, BCAST_PARAM(src1)); 111 acl_dst = ggml_cann_create_tensor(dst, BCAST_PARAM(src0)); 112 } else { 113 acl_src0 = ggml_cann_create_tensor(src0); 114 acl_src1 = ggml_cann_create_tensor(src1); 115 acl_dst = ggml_cann_create_tensor(dst); 116 } 117 } 118 119 void ggml_cann_op_unary(std::function unary_op, 120 ggml_backend_cann_context & ctx, 121 ggml_tensor * dst) { 122 ggml_tensor * src = dst->src[0]; 123 124 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src); 125 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 126 127 unary_op(ctx, acl_src.get(), acl_dst.get()); 128 } 129 130 void ggml_cann_op_unary_gated(std::function unary_op, 131 ggml_backend_cann_context & ctx, 132 ggml_tensor * dst) { 133 ggml_tensor * src0 = dst->src[0]; 134 ggml_tensor * src1 = dst->src[1]; 135 136 GGML_ASSERT(ggml_is_contiguous_1(src0)); 137 GGML_ASSERT(ggml_is_contiguous_1(dst)); 138 const int32_t swapped = ggml_get_op_params_i32(dst, 1); 139 140 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 141 acl_tensor_ptr acl_src0, acl_src1; 142 if (src1) { 143 GGML_ASSERT(ggml_is_contiguous_1(src1)); 144 GGML_ASSERT(src0->type == src1->type); 145 146 acl_src0 = ggml_cann_create_tensor(src0); 147 acl_src1 = ggml_cann_create_tensor(src1); 148 } else { 149 int64_t ne[] = { src0->ne[0] / 2, src0->ne[1], src0->ne[2], src0->ne[3] }; 150 size_t nb[] = { src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3] }; 151 acl_src0 = ggml_cann_create_tensor(src0, ne, nb, GGML_MAX_DIMS, ACL_FORMAT_ND, 0); 152 acl_src1 = ggml_cann_create_tensor(src0, ne, nb, GGML_MAX_DIMS, ACL_FORMAT_ND, ne[0] * ggml_element_size(src0)); 153 if (swapped) { 154 std::swap(acl_src0, acl_src1); 155 } 156 } 157 158 unary_op(ctx, acl_src0.get(), acl_dst.get()); 159 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceMul, acl_dst.get(), acl_src1.get()); 160 } 161 162 // Fused SwiGLU using aclnnSwiGlu: splits input along innermost dim, applies 163 // SiLU to left half, multiplies by right half. 164 // 165 // Falls back to the generic two-kernel path when src[1] != nullptr (two 166 // independent halves) or swapped != 0 (reversed activation order), as 167 // aclnnSwiGlu only handles the single interleaved tensor in standard order. 168 // 169 // CANN tiling for SwiGlu requires (storageShapeDim + viewDims) to be even. 170 // aclCreateTensor always uses storageShapeDim=1, so viewDims must be odd. 171 // We use a 3D view (1+3=4, even) to satisfy this constraint while preserving 172 // correct split semantics along the innermost (ne[0]) dimension. 173 void ggml_cann_swiglu(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 174 auto silu_fn = [](ggml_backend_cann_context & ctx, aclTensor * acl_src, aclTensor * acl_dst) { 175 GGML_CANN_CALL_ACLNN_OP(ctx, Silu, acl_src, acl_dst); 176 }; 177 178 const int32_t swapped = ggml_get_op_params_i32(dst, 1); 179 if (dst->src[1] != nullptr || swapped != 0) { 180 ggml_cann_op_unary_gated(silu_fn, ctx, dst); 181 return; 182 } 183 184 // aclnnSwiGlu requires the split dim (src->ne[0]) to be even; fall back otherwise. 185 if (dst->src[0]->ne[0] % 2 != 0) { 186 ggml_cann_op_unary_gated(silu_fn, ctx, dst); 187 return; 188 } 189 190 ggml_tensor * src0 = dst->src[0]; 191 size_t elem_size = ggml_element_size(src0); 192 193 // src0 GGML: [2*ne0, ne1, ne2, ne3] → 3D view [2*ne0, ne1, ne2*ne3] 194 // CANN reversed: [ne2*ne3, ne1, 2*ne0], split along CANN dim 2 (last). 195 int64_t ne0_x2 = src0->ne[0]; 196 int64_t ne1 = src0->ne[1]; 197 int64_t ne23 = src0->ne[2] * src0->ne[3]; 198 int64_t src3d_ne[] = { ne0_x2, ne1, ne23 }; 199 size_t src3d_nb[] = { (size_t)src0->nb[0], (size_t)src0->nb[1], (size_t)src0->nb[2] }; 200 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src0->data, ggml_cann_type_mapping(src0->type), 201 elem_size, src3d_ne, src3d_nb, 3); 202 203 // dst GGML: [ne0, ne1, ne2, ne3] → 3D view [ne0, ne1, ne2*ne3] 204 int64_t ne0 = dst->ne[0]; 205 int64_t dst3d_ne[] = { ne0, ne1, ne23 }; 206 size_t dst3d_nb[] = { (size_t)dst->nb[0], (size_t)dst->nb[1], (size_t)dst->nb[2] }; 207 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst->data, ggml_cann_type_mapping(dst->type), 208 elem_size, dst3d_ne, dst3d_nb, 3); 209 210 // CANN tensor [ne23, ne1, 2*ne0]: split along CANN dim 2 (last) = 2*ne0. 211 GGML_CANN_CALL_ACLNN_OP(ctx, SwiGlu, acl_src.get(), (int64_t)2, acl_dst.get()); 212 } 213 214 void ggml_cann_swiglu_clamp(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 215 ggml_tensor * src0 = dst->src[0]; 216 ggml_tensor * src1 = dst->src[1]; 217 218 GGML_ASSERT(ggml_is_contiguous_1(src0)); 219 GGML_ASSERT(ggml_is_contiguous_1(dst)); 220 221 const int32_t swapped = ggml_get_op_params_i32(dst, 1); 222 acl_tensor_ptr acl_gate; 223 acl_tensor_ptr acl_up; 224 if (src1) { 225 GGML_ASSERT(ggml_is_contiguous_1(src1)); 226 GGML_ASSERT(src0->type == src1->type); 227 acl_gate = ggml_cann_create_tensor(src0); 228 acl_up = ggml_cann_create_tensor(src1); 229 } else { 230 int64_t ne[] = { src0->ne[0] / 2, src0->ne[1], src0->ne[2], src0->ne[3] }; 231 size_t nb[] = { src0->nb[0], src0->nb[1], src0->nb[2], src0->nb[3] }; 232 acl_gate = ggml_cann_create_tensor(src0, ne, nb, GGML_MAX_DIMS, ACL_FORMAT_ND, 0); 233 acl_up = ggml_cann_create_tensor(src0, ne, nb, GGML_MAX_DIMS, ACL_FORMAT_ND, ne[0] * ggml_element_size(src0)); 234 if (swapped) { 235 std::swap(acl_gate, acl_up); 236 } 237 } 238 239 ggml_cann_pool_alloc temp_alloc(ctx.pool(), ggml_nbytes(dst)); 240 acl_tensor_ptr acl_temp = ggml_cann_create_tensor(temp_alloc.get(), ggml_cann_type_mapping(dst->type), 241 ggml_element_size(dst), dst->ne, dst->nb, GGML_MAX_DIMS); 242 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 243 244 const float limit = ggml_get_op_params_f32(dst, 3); 245 float min_gate = -INFINITY; 246 float min_up = -limit; 247 float max_value = limit; 248 acl_scalar_ptr acl_min_gate = ggml_cann_create_scalar(&min_gate, ACL_FLOAT); 249 acl_scalar_ptr acl_min_up = ggml_cann_create_scalar(&min_up, ACL_FLOAT); 250 acl_scalar_ptr acl_limit = ggml_cann_create_scalar(&max_value, ACL_FLOAT); 251 252 GGML_CANN_CALL_ACLNN_OP(ctx, Clamp, acl_gate.get(), acl_min_gate.get(), acl_limit.get(), acl_temp.get()); 253 GGML_CANN_CALL_ACLNN_OP(ctx, Silu, acl_temp.get(), acl_dst.get()); 254 GGML_CANN_CALL_ACLNN_OP(ctx, Clamp, acl_up.get(), acl_min_up.get(), acl_limit.get(), acl_temp.get()); 255 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceMul, acl_dst.get(), acl_temp.get()); 256 } 257 258 // Fused GeGLU using aclnnGeGluV3: splits input along ne[0] (CANN last dim), 259 // activates the LEFT half with GELU, multiplies by right half. 260 // approximate: 0=tanh, 1=none(erf). activateLeft=true matches GGML convention. 261 // outGelu is a required-but-discard output buffer. 262 // 263 // Falls back to the generic two-kernel path when src[1] != nullptr (two 264 // independent halves) or swapped != 0 (reversed activation order), as 265 // aclnnGeGluV3 only handles the single interleaved tensor in standard order. 266 void ggml_cann_geglu(ggml_backend_cann_context & ctx, ggml_tensor * dst, int64_t approximate) { 267 auto gelu_fn = [](ggml_backend_cann_context & ctx, aclTensor * acl_src, aclTensor * acl_dst) { 268 GGML_CANN_CALL_ACLNN_OP(ctx, Gelu, acl_src, acl_dst); 269 }; 270 271 const int32_t swapped = ggml_get_op_params_i32(dst, 1); 272 if (dst->src[1] != nullptr || swapped != 0) { 273 ggml_cann_op_unary_gated(gelu_fn, ctx, dst); 274 return; 275 } 276 277 // aclnnGeGluV3 requires the split dim (src->ne[0]) to be even; fall back otherwise. 278 if (dst->src[0]->ne[0] % 2 != 0) { 279 ggml_cann_op_unary_gated(gelu_fn, ctx, dst); 280 return; 281 } 282 283 ggml_tensor * src0 = dst->src[0]; 284 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src0); 285 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 286 287 // Allocate a temporary buffer for the required outGelu output (same shape as dst). 288 // Build contiguous strides since the pool allocation is a fresh buffer. 289 size_t elem_size = ggml_element_size(dst); 290 int64_t ne[GGML_MAX_DIMS] = { dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3] }; 291 size_t nb[GGML_MAX_DIMS]; 292 nb[0] = elem_size; 293 for (int i = 1; i < GGML_MAX_DIMS; i++) { 294 nb[i] = nb[i - 1] * ne[i - 1]; 295 } 296 size_t gelu_out_size = nb[GGML_MAX_DIMS - 1] * ne[GGML_MAX_DIMS - 1]; 297 ggml_cann_pool_alloc gelu_out_alloc(ctx.pool(), gelu_out_size); 298 299 acl_tensor_ptr acl_gelu_out = ggml_cann_create_tensor( 300 gelu_out_alloc.get(), ggml_cann_type_mapping(dst->type), elem_size, ne, nb, GGML_MAX_DIMS); 301 // V3 adds activateLeft param; true → Gelu(left)*right, matching GGML convention. 302 // GGML dim 0 → CANN last dim (index GGML_MAX_DIMS-1 = 3 for 4D tensor). 303 GGML_CANN_CALL_ACLNN_OP(ctx, GeGluV3, acl_src.get(), (int64_t)(GGML_MAX_DIMS - 1), approximate, true, 304 acl_dst.get(), acl_gelu_out.get()); 305 } 306 307 /** 308 * @brief Repeats elements of a tensor along each dimension according to the 309 * specified repeat array. 310 * 311 * @param ctx The context for the CANN backend operations. 312 * @param acl_src The source tensor to be repeated. 313 * @param acl_dst The destination tensor after repeating. 314 * @param repeat_array The array specifying the number of repetitions along each 315 * dimension. 316 */ 317 static void aclnn_repeat(ggml_backend_cann_context & ctx, 318 aclTensor * acl_src, 319 aclTensor * acl_dst, 320 int64_t * repeat_array) { 321 // repeat tensor along each dim with repeat_array 322 acl_int_array_ptr repeats = ggml_cann_create_int_array(repeat_array, GGML_MAX_DIMS); 323 324 GGML_CANN_CALL_ACLNN_OP(ctx, Repeat, acl_src, repeats.get(), acl_dst); 325 } 326 327 /** 328 * @brief Casts the data type of a source tensor to a destination tensor. 329 * 330 * This function casts the data type of the source tensor `acl_src` to the 331 * specified data type `cast_data_type` and stores the result in the destination 332 * tensor `acl_dst`. 333 * 334 * @param ctx The context for the CANN backend operations. 335 * @param acl_src The source tensor whose data type will be casted. 336 * @param acl_dst The destination tensor where the casted result will be stored. 337 * @param cast_data_type The target data type to which the source tensor will be 338 * casted. 339 */ 340 static void aclnn_cast(ggml_backend_cann_context & ctx, 341 aclTensor * acl_src, 342 aclTensor * acl_dst, 343 aclDataType cast_data_type) { 344 GGML_CANN_CALL_ACLNN_OP(ctx, Cast, acl_src, cast_data_type, acl_dst); 345 } 346 347 void ggml_cann_repeat(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 348 ggml_tensor * src = dst->src[0]; 349 GGML_ASSERT(ggml_can_repeat(src, dst)); 350 351 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src); 352 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 353 354 int64_t repeatsArray[] = { dst->ne[3] / src->ne[3], dst->ne[2] / src->ne[2], dst->ne[1] / src->ne[1], 355 dst->ne[0] / src->ne[0] }; 356 357 aclnn_repeat(ctx, acl_src.get(), acl_dst.get(), repeatsArray); 358 } 359 360 void aclnn_add(ggml_backend_cann_context & ctx, aclTensor * acl_src0, aclTensor * acl_src1, aclTensor * acl_dst) { 361 float alphaValue = 1.0f; 362 acl_scalar_ptr alpha = ggml_cann_create_scalar(&alphaValue, aclDataType::ACL_FLOAT); 363 if (acl_dst != nullptr) { 364 GGML_CANN_CALL_ACLNN_OP(ctx, Add, acl_src0, acl_src1, alpha.get(), acl_dst); 365 } else { 366 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceAdd, acl_src0, acl_src1, alpha.get()); 367 } 368 } 369 370 void aclnn_sub(ggml_backend_cann_context & ctx, aclTensor * acl_src0, aclTensor * acl_src1, aclTensor * acl_dst) { 371 float alphaValue = 1.0f; 372 acl_scalar_ptr alpha = ggml_cann_create_scalar(&alphaValue, aclDataType::ACL_FLOAT); 373 if (acl_dst != nullptr) { 374 GGML_CANN_CALL_ACLNN_OP(ctx, Sub, acl_src0, acl_src1, alpha.get(), acl_dst); 375 } else { 376 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceSub, acl_src0, acl_src1, alpha.get()); 377 } 378 } 379 380 void aclnn_mul(ggml_backend_cann_context & ctx, aclTensor * acl_src, aclTensor * acl_other, aclTensor * acl_dst) { 381 if (acl_dst != nullptr) { 382 GGML_CANN_CALL_ACLNN_OP(ctx, Mul, acl_src, acl_other, acl_dst); 383 } else { 384 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceMul, acl_src, acl_other); 385 } 386 } 387 388 void aclnn_div(ggml_backend_cann_context & ctx, aclTensor * acl_src, aclTensor * acl_other, aclTensor * acl_dst) { 389 if (acl_dst != nullptr) { 390 GGML_CANN_CALL_ACLNN_OP(ctx, Div, acl_src, acl_other, acl_dst); 391 } else { 392 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceDiv, acl_src, acl_other); 393 } 394 } 395 396 /** 397 * @brief Multiplies elements of a tensor by a scalar value, optionally 398 * in-place. 399 * 400 * This function multiplies each element of the source tensor `acl_src` by the 401 * scalar `scale` and stores the result in the destination tensor `acl_dst`. If 402 * `inplace` is true, `acl_dst` will not be used and the operation is performed 403 * in-place on `acl_src`. 404 * The operation is defined as: 405 * \f[ 406 * \text {acl_dst }_i=\text {acl_src }_i \times \text {scale} 407 * \f] 408 * 409 * @param ctx The context for the CANN backend operations. 410 * @param acl_src The source tensor whose elements will be multiplied. 411 * @param scale The scalar value by which each element of `acl_src` will be 412 * multiplied. 413 * @param acl_dst The destination tensor where the result will be stored if 414 * `inplace` is false. 415 * @param inplace Flag indicating whether to perform the operation in-place on 416 * `acl_src`. 417 */ 418 static void aclnn_muls(ggml_backend_cann_context & ctx, 419 aclTensor * acl_src, 420 float scale, 421 aclTensor * acl_dst, 422 bool inplace) { 423 acl_scalar_ptr acl_scale = ggml_cann_create_scalar(&scale, aclDataType::ACL_FLOAT); 424 if (inplace) { 425 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceMuls, acl_src, acl_scale.get()); 426 } else { 427 GGML_CANN_CALL_ACLNN_OP(ctx, Muls, acl_src, acl_scale.get(), acl_dst); 428 } 429 } 430 431 void ggml_cann_leaky_relu(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 432 ggml_tensor * src = dst->src[0]; 433 434 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src); 435 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 436 437 float negative_slope; 438 memcpy(&negative_slope, dst->op_params, sizeof(float)); 439 acl_scalar_ptr acl_negative_slope = ggml_cann_create_scalar(&negative_slope, aclDataType::ACL_FLOAT); 440 441 GGML_CANN_CALL_ACLNN_OP(ctx, LeakyRelu, acl_src.get(), acl_negative_slope.get(), acl_dst.get()); 442 } 443 444 /** 445 * @brief Concatenates a list of tensors along a specified dimension and stores 446 * the result in a destination tensor. 447 * 448 * @param ctx The context for the CANN backend operations. 449 * @param tensorList The list of tensors to be concatenated. 450 * @param acl_dst The destination tensor where the concatenated result will be 451 * stored. 452 * @param concat_dim The dimension along which the tensors will be concatenated. 453 */ 454 static void aclnn_concat(ggml_backend_cann_context & ctx, 455 aclTensorList * tensorList, 456 aclTensor * acl_dst, 457 int64_t concat_dim) { 458 GGML_CANN_CALL_ACLNN_OP(ctx, Cat, tensorList, concat_dim, acl_dst); 459 } 460 461 void ggml_cann_concat(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 462 ggml_tensor * src0 = dst->src[0]; 463 ggml_tensor * src1 = dst->src[1]; 464 acl_tensor_ptr acl_src0 = ggml_cann_create_tensor(src0); 465 acl_tensor_ptr acl_src1 = ggml_cann_create_tensor(src1); 466 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 467 468 const int32_t dim = ggml_get_op_params_i32(dst, 0); 469 470 GGML_ASSERT(dim >= 0 && dim < 4); 471 int32_t acl_dim = 3 - dim; 472 473 acl_tensor_list_ptr tensor_list = ggml_cann_create_tensor_list(acl_src0, acl_src1); 474 aclnn_concat(ctx, tensor_list.get(), acl_dst.get(), acl_dim); 475 } 476 477 /** 478 * @brief Creates a tensor with values starting from `start`, incremented by 479 * `step`, and ending before `stop`. 480 * 481 * This function performs the operation: 482 * \f[ 483 * \text {out }_{i+1}=\text {out }_i+\text {step} 484 * \f] 485 * the range is [start, stop). 486 * 487 * @param ctx The context for the CANN backend operations. 488 * @param acl_dst The destination tensor where the values will be stored. 489 * @param start The starting value of the range. 490 * @param stop The ending value of the range (exclusive). 491 * @param step The step size between consecutive values. 492 * @param n_elements The number of elements in the destination tensor. 493 */ 494 static void aclnn_arange(ggml_backend_cann_context & ctx, 495 aclTensor * acl_dst, 496 float start, 497 float stop, 498 float step, 499 int64_t n_elements) { 500 int64_t steps = (int64_t) std::ceil((stop - start) / step); 501 GGML_ASSERT(n_elements == steps); 502 503 acl_scalar_ptr acl_start = ggml_cann_create_scalar(&start, aclDataType::ACL_FLOAT); 504 acl_scalar_ptr acl_end = ggml_cann_create_scalar(&stop, aclDataType::ACL_FLOAT); 505 acl_scalar_ptr acl_step = ggml_cann_create_scalar(&step, aclDataType::ACL_FLOAT); 506 507 GGML_CANN_CALL_ACLNN_OP(ctx, Arange, acl_start.get(), acl_end.get(), acl_step.get(), acl_dst); 508 } 509 510 void ggml_cann_arange(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 511 GGML_ASSERT(dst->type == GGML_TYPE_F32); 512 513 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 514 515 int64_t n_elements = ggml_nelements(dst); 516 float start; 517 float stop; 518 float step; 519 memcpy(&start, (float *) dst->op_params + 0, sizeof(float)); 520 memcpy(&stop, (float *) dst->op_params + 1, sizeof(float)); 521 memcpy(&step, (float *) dst->op_params + 2, sizeof(float)); 522 523 aclnn_arange(ctx, acl_dst.get(), start, stop, step, n_elements); 524 } 525 526 void ggml_cann_clamp(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 527 ggml_tensor * src = dst->src[0]; 528 529 float min; 530 float max; 531 memcpy(&min, dst->op_params, sizeof(float)); 532 memcpy(&max, (float *) dst->op_params + 1, sizeof(float)); 533 534 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src); 535 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 536 537 acl_scalar_ptr acl_min = ggml_cann_create_scalar(&min, aclDataType::ACL_FLOAT); 538 acl_scalar_ptr acl_max = ggml_cann_create_scalar(&max, aclDataType::ACL_FLOAT); 539 540 GGML_CANN_CALL_ACLNN_OP(ctx, Clamp, acl_src.get(), acl_min.get(), acl_max.get(), acl_dst.get()); 541 } 542 543 void ggml_cann_scale(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 544 ggml_tensor * src = dst->src[0]; 545 546 // scale factor 547 float v; 548 memcpy(&v, dst->op_params, sizeof(float)); 549 550 acl_scalar_ptr scale = ggml_cann_create_scalar(&v, aclDataType::ACL_FLOAT); 551 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src); 552 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 553 554 GGML_CANN_CALL_ACLNN_OP(ctx, Muls, acl_src.get(), scale.get(), acl_dst.get()); 555 } 556 557 void ggml_cann_argsort(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 558 ggml_tensor * src = dst->src[0]; 559 enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0]; 560 561 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src); 562 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 563 ggml_cann_pool_alloc temp_buffer_allocator(ctx.pool(), ggml_nelements(dst) * sizeof(int64_t)); 564 void * buffer = temp_buffer_allocator.get(); 565 acl_tensor_ptr tmp_tensor = 566 ggml_cann_create_tensor(buffer, ACL_INT64, ggml_type_size(dst->type), dst->ne, dst->nb, GGML_MAX_DIMS); 567 GGML_CANN_CALL_ACLNN_OP(ctx, Argsort, acl_src.get(), -1, (order == GGML_SORT_ORDER_DESC ? true : false), 568 tmp_tensor.get()); 569 GGML_CANN_CALL_ACLNN_OP(ctx, Cast, tmp_tensor.get(), ggml_cann_type_mapping(dst->type), acl_dst.get()); 570 } 571 572 void ggml_cann_norm(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 573 ggml_tensor * src = dst->src[0]; 574 575 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src); 576 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 577 578 float eps; 579 memcpy(&eps, dst->op_params, sizeof(float)); 580 581 std::vector normData = { dst->ne[0] }; 582 acl_int_array_ptr norm = ggml_cann_create_int_array(normData.data(), normData.size()); 583 GGML_CANN_CALL_ACLNN_OP(ctx, LayerNorm, acl_src.get(), norm.get(), nullptr, nullptr, eps, acl_dst.get(), nullptr, 584 nullptr); 585 } 586 587 void ggml_cann_l2_norm(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 588 ggml_tensor * src = dst->src[0]; 589 590 float eps; 591 memcpy(&eps, dst->op_params, sizeof(float)); 592 593 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src); 594 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 595 596 size_t type_size = ggml_type_size(src->type); 597 int64_t n_bytes = src->ne[3] * src->ne[2] * src->ne[1] * type_size; 598 ggml_cann_pool_alloc temp_buffer_allocator(ctx.pool(), n_bytes); 599 void * buffer = temp_buffer_allocator.get(); 600 601 int64_t norm_ne[] = { 1, src->ne[1], src->ne[2], src->ne[3] }; 602 size_t norm_nb[GGML_MAX_DIMS]; 603 norm_nb[0] = sizeof(float); 604 for (int i = 1; i < GGML_MAX_DIMS; ++i) { 605 norm_nb[i] = norm_nb[i - 1] * norm_ne[i - 1]; 606 } 607 acl_tensor_ptr acl_norm = ggml_cann_create_tensor(buffer, ACL_FLOAT, sizeof(float), norm_ne, norm_nb, GGML_MAX_DIMS); 608 609 std::vector norm_dims = { 3 }; 610 acl_int_array_ptr dims_array = ggml_cann_create_int_array(norm_dims.data(), norm_dims.size()); 611 612 float p_value = 2.0f; 613 acl_scalar_ptr p_scalar = ggml_cann_create_scalar(&p_value, aclDataType::ACL_FLOAT); 614 GGML_CANN_CALL_ACLNN_OP(ctx, Norm, acl_src.get(), p_scalar.get(), dims_array.get(), true, acl_norm.get()); 615 616 ggml_cann_pool_alloc clamp_buffer_allocator(ctx.pool()); 617 acl_tensor_ptr acl_clamped; 618 619 if (eps > 0.0f) { 620 void * clamp_buf = clamp_buffer_allocator.alloc(n_bytes); 621 acl_clamped = ggml_cann_create_tensor(clamp_buf, ACL_FLOAT, sizeof(float), norm_ne, norm_nb, GGML_MAX_DIMS); 622 acl_scalar_ptr eps_scalar = ggml_cann_create_scalar(&eps, aclDataType::ACL_FLOAT); 623 GGML_CANN_CALL_ACLNN_OP(ctx, ClampMin, acl_norm.get(), eps_scalar.get(), acl_clamped.get()); 624 } 625 626 aclTensor * acl_div_input = acl_clamped ? acl_clamped.get() : acl_norm.get(); 627 GGML_CANN_CALL_ACLNN_OP(ctx, Div, acl_src.get(), acl_div_input, acl_dst.get()); 628 } 629 630 void ggml_cann_cross_entropy_loss(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 631 ggml_tensor * src0 = dst->src[0]; 632 ggml_tensor * src1 = dst->src[1]; 633 634 const int64_t nc = src0->ne[0]; 635 const int64_t nr = ggml_nrows(src0); 636 637 int64_t logits_ne[] = { nc, nr }; 638 size_t logits_nb[2]; 639 logits_nb[0] = ggml_type_size(src0->type); 640 logits_nb[1] = logits_nb[0] * logits_ne[0]; 641 acl_tensor_ptr acl_logits = ggml_cann_create_tensor(src0->data, ACL_FLOAT, sizeof(float), logits_ne, logits_nb, 2); 642 643 int64_t labels_ne[] = { nc, nr }; 644 size_t labels_nb[2]; 645 labels_nb[0] = ggml_type_size(src1->type); 646 labels_nb[1] = labels_nb[0] * labels_ne[0]; 647 acl_tensor_ptr acl_labels = ggml_cann_create_tensor(src1->data, ACL_FLOAT, sizeof(float), labels_ne, labels_nb, 2); 648 649 size_t loss_per_sample_type_size = sizeof(float); 650 int64_t loss_per_sample_n_bytes = nr * loss_per_sample_type_size; 651 ggml_cann_pool_alloc loss_per_sample_allocator(ctx.pool(), loss_per_sample_n_bytes); 652 void * loss_per_sample_buffer = loss_per_sample_allocator.get(); 653 654 int64_t loss_per_sample_ne[] = { nr }; 655 size_t loss_per_sample_nb[1]; 656 loss_per_sample_nb[0] = loss_per_sample_type_size; 657 acl_tensor_ptr acl_loss_per_sample = ggml_cann_create_tensor( 658 loss_per_sample_buffer, ACL_FLOAT, loss_per_sample_type_size, loss_per_sample_ne, loss_per_sample_nb, 1); 659 660 size_t backprop_n_bytes = nr * nc * sizeof(float); 661 ggml_cann_pool_alloc backprop_allocator(ctx.pool(), backprop_n_bytes); 662 void * backprop_buffer = backprop_allocator.get(); 663 acl_tensor_ptr acl_backprop = ggml_cann_create_tensor(backprop_buffer, ACL_FLOAT, sizeof(float), logits_ne, logits_nb, 2); 664 665 GGML_CANN_CALL_ACLNN_OP(ctx, SoftmaxCrossEntropyWithLogits, acl_logits.get(), acl_labels.get(), 666 acl_loss_per_sample.get(), acl_backprop.get()); 667 668 size_t total_sum_type_size = sizeof(float); 669 int64_t total_sum_n_bytes = 1 * total_sum_type_size; 670 ggml_cann_pool_alloc total_sum_allocator(ctx.pool(), total_sum_n_bytes); 671 void * total_sum_buffer = total_sum_allocator.get(); 672 673 int64_t total_sum_ne[] = { 1 }; 674 size_t total_sum_nb[1]; 675 total_sum_nb[0] = total_sum_type_size; 676 677 acl_tensor_ptr acl_total_sum = 678 ggml_cann_create_tensor(total_sum_buffer, ACL_FLOAT, total_sum_type_size, total_sum_ne, total_sum_nb, 1); 679 680 std::vector total_sum_dims = { 0 }; 681 acl_int_array_ptr total_sum_dims_array = ggml_cann_create_int_array(total_sum_dims.data(), total_sum_dims.size()); 682 bool keep_dims = false; 683 684 GGML_CANN_CALL_ACLNN_OP(ctx, ReduceSum, acl_loss_per_sample.get(), total_sum_dims_array.get(), keep_dims, ACL_FLOAT, 685 acl_total_sum.get()); 686 687 float value = 1.0f / static_cast(nr); 688 acl_scalar_ptr scale_factor = ggml_cann_create_scalar(&value, aclDataType::ACL_FLOAT); 689 acl_tensor_ptr acl_dst = 690 ggml_cann_create_tensor(dst->data, ACL_FLOAT, sizeof(float), total_sum_ne, total_sum_nb, 1); 691 692 GGML_CANN_CALL_ACLNN_OP(ctx, Muls, acl_total_sum.get(), scale_factor.get(), acl_dst.get()); 693 } 694 695 void ggml_cann_group_norm(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 696 ggml_tensor * src = dst->src[0]; 697 698 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src); 699 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 700 701 int n_groups = dst->op_params[0]; 702 703 float eps; 704 memcpy(&eps, dst->op_params + 1, sizeof(float)); 705 706 int64_t N = src->ne[3]; 707 int64_t C = src->ne[2]; 708 int64_t HxW = src->ne[1] * src->ne[0]; 709 710 size_t type_size = ggml_type_size(src->type); 711 int64_t ne[] = { n_groups, N }; 712 size_t nb[] = { type_size, type_size * n_groups }; 713 size_t n_bytes = N * n_groups; 714 715 ggml_cann_pool_alloc temp_buffer_allocator(ctx.pool(), n_bytes * 2); 716 void * buffer = temp_buffer_allocator.get(); 717 acl_tensor_ptr acl_mean_out = ggml_cann_create_tensor(buffer, ACL_FLOAT, type_size, ne, nb, ACL_FORMAT_ND); 718 acl_tensor_ptr acl_rstd_out = 719 ggml_cann_create_tensor((char *) buffer + n_bytes, ACL_FLOAT, type_size, ne, nb, ACL_FORMAT_ND); 720 721 GGML_CANN_CALL_ACLNN_OP(ctx, GroupNorm, acl_src.get(), nullptr, nullptr, N, C, HxW, n_groups, eps, acl_dst.get(), 722 acl_mean_out.get(), acl_rstd_out.get()); 723 } 724 725 void ggml_cann_set(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 726 ggml_tensor * src0 = dst->src[0]; 727 ggml_tensor * src1 = dst->src[1]; 728 729 size_t nb1 = ((int32_t *) dst->op_params)[0]; 730 size_t nb2 = ((int32_t *) dst->op_params)[1]; 731 size_t nb3 = ((int32_t *) dst->op_params)[2]; 732 size_t offset = ((int32_t *) dst->op_params)[3]; 733 bool inplace = (bool) ((int32_t *) dst->op_params)[4]; 734 735 size_t param_nb[] = { ggml_element_size(src0), nb1, nb2, nb3 }; 736 737 // Create a view of dst at the target offset with src1's dimensions 738 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst, src1->ne, param_nb, GGML_MAX_DIMS, ACL_FORMAT_ND, offset); 739 acl_tensor_ptr acl_src1 = ggml_cann_create_tensor(src1); 740 741 if (!inplace) { 742 // First copy src0 to dst entirely 743 size_t cpy_size = ggml_nbytes(dst); 744 ACL_CHECK( 745 aclrtMemcpyAsync(dst->data, cpy_size, src0->data, cpy_size, ACL_MEMCPY_DEVICE_TO_DEVICE, ctx.stream())); 746 } 747 748 // Copy src1 into the target region of dst 749 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceCopy, acl_dst.get(), acl_src1.get()); 750 } 751 752 void ggml_cann_acc(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 753 ggml_tensor * src0 = dst->src[0]; 754 ggml_tensor * src1 = dst->src[1]; 755 756 size_t nb1 = ((int32_t *) dst->op_params)[0]; 757 size_t nb2 = ((int32_t *) dst->op_params)[1]; 758 size_t nb3 = ((int32_t *) dst->op_params)[2]; 759 size_t offset = ((int32_t *) dst->op_params)[3]; 760 bool inplace = (bool) ((int32_t *) dst->op_params)[4]; 761 762 size_t param_nb[] = { ggml_element_size(src0), nb1, nb2, nb3 }; 763 764 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst, src1->ne, param_nb, GGML_MAX_DIMS, ACL_FORMAT_ND, offset); 765 acl_tensor_ptr acl_src1 = ggml_cann_create_tensor(src1); 766 767 acl_scalar_ptr alpha = nullptr; 768 float alphaValue = 1.0f; 769 alpha = ggml_cann_create_scalar(&alphaValue, aclDataType::ACL_FLOAT); 770 771 if (!inplace) { 772 size_t cpy_size = ggml_nbytes(dst); 773 ACL_CHECK( 774 aclrtMemcpyAsync(dst->data, cpy_size, src0->data, cpy_size, ACL_MEMCPY_DEVICE_TO_DEVICE, ctx.stream())); 775 acl_tensor_ptr acl_src0 = 776 ggml_cann_create_tensor(src0, src1->ne, src0->nb, GGML_MAX_DIMS, ACL_FORMAT_ND, offset); 777 778 GGML_CANN_CALL_ACLNN_OP(ctx, Add, acl_src0.get(), acl_src1.get(), alpha.get(), acl_dst.get()); 779 } else { 780 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceAdd, acl_dst.get(), acl_src1.get(), alpha.get()); 781 } 782 } 783 784 /** 785 * @brief Performs sum reduction on a given tensor along specified dimensions. 786 * 787 * This function reduces the input tensor by summing along the specified dimensions. 788 * 789 * @param ctx The context for the CANN backend operations. 790 * @param dst The destination tensor where the reduced result will be stored. 791 * @param dim An array of dimension indices. 792 * @param dim_size The number of dimensions. 793 */ 794 static void aclnn_reduce_sum(ggml_backend_cann_context & ctx, ggml_tensor * dst, int64_t * dim, size_t dim_size) { 795 GGML_ASSERT(dst->ne[0] == 1); 796 ggml_tensor * src = dst->src[0]; 797 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src); 798 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 799 acl_int_array_ptr reduce_dims = ggml_cann_create_int_array(dim, dim_size); 800 801 GGML_CANN_CALL_ACLNN_OP(ctx, ReduceSum, acl_src.get(), reduce_dims.get(), true, ggml_cann_type_mapping(dst->type), 802 acl_dst.get()); 803 } 804 805 void ggml_cann_sum_rows(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 806 int64_t reduce_dims[] = { 3 }; 807 aclnn_reduce_sum(ctx, dst, reduce_dims, 1); 808 } 809 810 void ggml_cann_sum(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 811 int64_t reduce_dims[] = { 0, 1, 2, 3 }; 812 aclnn_reduce_sum(ctx, dst, reduce_dims, 4); 813 } 814 815 void ggml_cann_cumsum(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 816 ggml_tensor * src = dst->src[0]; 817 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src); 818 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 819 // GGML cumsum operates along dim 0 (innermost / ne[0]). 820 // ggml_cann_create_tensor reverses dimensions to [ne3,ne2,ne1,ne0], 821 // so GGML dim 0 maps to CANN dim 3 (the last dim of the 4-D tensor). 822 GGML_CANN_CALL_ACLNN_OP(ctx, Cumsum, acl_src.get(), (int64_t)3, 823 ggml_cann_type_mapping(dst->type), acl_dst.get()); 824 } 825 826 void ggml_cann_solve_tri(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 827 ggml_tensor * src0 = dst->src[0]; // A: [N, N, B2, B3] lower triangular 828 ggml_tensor * src1 = dst->src[1]; // B: [K, N, B2, B3] 829 830 acl_tensor_ptr acl_a = ggml_cann_create_tensor(src0); 831 acl_tensor_ptr acl_b = ggml_cann_create_tensor(src1); 832 acl_tensor_ptr acl_x = ggml_cann_create_tensor(dst); 833 834 // mOut: triangular copy of A (required output), same shape as A. 835 const size_t a_bytes = ggml_nbytes(src0); 836 ggml_cann_pool_alloc m_alloc(ctx.pool(), a_bytes); 837 acl_tensor_ptr acl_m = ggml_cann_create_tensor( 838 m_alloc.get(), ggml_cann_type_mapping(src0->type), 839 ggml_type_size(src0->type), src0->ne, src0->nb, GGML_MAX_DIMS); 840 841 // Solve AX = B: upper=false (lower tri), transpose=false, unitriangular=false. 842 GGML_CANN_CALL_ACLNN_OP(ctx, TriangularSolve, 843 acl_b.get(), acl_a.get(), false, false, false, 844 acl_x.get(), acl_m.get()); 845 } 846 847 void ggml_cann_diag(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 848 ggml_tensor * src = dst->src[0]; 849 850 GGML_ASSERT(src->ne[1] == 1); 851 852 const int64_t N = src->ne[0]; 853 const int64_t n_batch = src->ne[2] * src->ne[3]; 854 const size_t nb_f32 = sizeof(float); 855 856 // Fill dst with zeros. 857 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 858 { 859 float zero = 0.0f; 860 acl_scalar_ptr acl_zero = ggml_cann_create_scalar(&zero, ACL_FLOAT); 861 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceFillScalar, acl_dst.get(), acl_zero.get()); 862 } 863 864 // Copy src vector onto the diagonal of dst via strided views. 865 // src viewed as [N, n_batch], contiguous strides. 866 int64_t ne_vec[2] = { N, n_batch }; 867 size_t nb_src_vec[2] = { nb_f32, N * nb_f32 }; 868 // dst diagonal view: stride (N+1)*4 steps along the diagonal. 869 size_t nb_dst_diag[2] = { (N + 1) * nb_f32, N * N * nb_f32 }; 870 871 acl_tensor_ptr acl_src_vec = ggml_cann_create_tensor(src->data, ACL_FLOAT, nb_f32, ne_vec, nb_src_vec, 2); 872 acl_tensor_ptr acl_dst_diag = ggml_cann_create_tensor(dst->data, ACL_FLOAT, nb_f32, ne_vec, nb_dst_diag, 2); 873 874 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceCopy, acl_dst_diag.get(), acl_src_vec.get()); 875 } 876 877 void ggml_cann_fill(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 878 float c = ggml_get_op_params_f32(dst, 0); 879 880 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 881 acl_scalar_ptr acl_c = ggml_cann_create_scalar(&c, ACL_FLOAT); 882 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceFillScalar, acl_dst.get(), acl_c.get()); 883 } 884 885 void ggml_cann_tri(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 886 ggml_tensor * src = dst->src[0]; 887 888 const int64_t S = src->ne[0]; 889 const int64_t n_batch = src->ne[2] * src->ne[3]; 890 const size_t nb_f32 = sizeof(float); 891 892 int64_t ne3d[3] = { S, S, n_batch }; 893 size_t nb3d[3] = { nb_f32, S * nb_f32, S * S * nb_f32 }; 894 895 const ggml_tri_type ttype = (ggml_tri_type) ggml_get_op_params_i32(dst, 0); 896 897 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src->data, ACL_FLOAT, nb_f32, ne3d, nb3d, 3); 898 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst->data, ACL_FLOAT, nb_f32, ne3d, nb3d, 3); 899 900 switch (ttype) { 901 case GGML_TRI_TYPE_LOWER: 902 // Tril(-1): preserve row > col (strict lower), zero upper + diagonal. 903 GGML_CANN_CALL_ACLNN_OP(ctx, Tril, acl_src.get(), (int64_t)-1, acl_dst.get()); 904 break; 905 case GGML_TRI_TYPE_UPPER_DIAG: 906 // Triu(0): preserve row <= col (upper + diagonal), zero strict lower. 907 GGML_CANN_CALL_ACLNN_OP(ctx, Triu, acl_src.get(), (int64_t)0, acl_dst.get()); 908 break; 909 case GGML_TRI_TYPE_UPPER: 910 // Triu(1): preserve row < col (strict upper), zero lower + diagonal. 911 GGML_CANN_CALL_ACLNN_OP(ctx, Triu, acl_src.get(), (int64_t)1, acl_dst.get()); 912 break; 913 case GGML_TRI_TYPE_LOWER_DIAG: 914 // Tril(0): preserve row >= col (lower + diagonal), zero strict upper. 915 GGML_CANN_CALL_ACLNN_OP(ctx, Tril, acl_src.get(), (int64_t)0, acl_dst.get()); 916 break; 917 default: 918 GGML_ABORT("unsupported tri type"); 919 } 920 } 921 922 void ggml_cann_upsample_nearest2d(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 923 ggml_tensor * src = dst->src[0]; 924 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src, nullptr, nullptr, 0, ACL_FORMAT_NCHW); 925 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst, nullptr, nullptr, 0, ACL_FORMAT_NCHW); 926 927 std::vector output_size{ dst->ne[1], dst->ne[0] }; 928 acl_int_array_ptr output_size_array = ggml_cann_create_int_array(output_size.data(), 2); 929 930 GGML_CANN_CALL_ACLNN_OP(ctx, UpsampleNearest2d, acl_src.get(), output_size_array.get(), acl_dst.get()); 931 } 932 933 /** 934 * @brief Pads a tensor with a specified value along each dimension. 935 * 936 * This function performs padding of the source tensor `acl_src` and stores the 937 * result in the destination tensor `acl_dst`. The padding values for each 938 * dimension are specified in the `paddings` array. 939 * 940 * @param ctx The context for the CANN backend operations. 941 * @param acl_src The source tensor to be padded. 942 * @param acl_dst The destination tensor where the padded result will be stored. 943 * @param paddings An array specifying the padding values for each dimension. 944 * The size of the array should be twice the number of dimensions of the tensor. 945 * @param value The value to be used for padding. The default value is 0.0. 946 */ 947 static void aclnn_pad(ggml_backend_cann_context & ctx, 948 aclTensor * acl_src, 949 aclTensor * acl_dst, 950 int64_t * paddings, 951 float value = 0.0f) { 952 acl_int_array_ptr acl_pad = ggml_cann_create_int_array(paddings, GGML_MAX_DIMS * 2); 953 acl_scalar_ptr acl_value = ggml_cann_create_scalar(&value, aclDataType::ACL_FLOAT); 954 955 GGML_CANN_CALL_ACLNN_OP(ctx, ConstantPadNd, acl_src, acl_pad.get(), acl_value.get(), acl_dst); 956 } 957 958 void ggml_cann_pad(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 959 ggml_tensor * src = dst->src[0]; 960 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src); 961 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 962 963 // padding: value in the array means how much distance will be padding. 964 // the position of elements in the array means which dirction to padding, 965 // each position means: [dim0.front, dim0.behind, dim1.front, dim1.behind, 966 // dim2.front, dim2.behind, dim3.front, dim3.behind] 967 const int32_t lp0 = ggml_get_op_params_i32(dst, 0); 968 const int32_t rp0 = ggml_get_op_params_i32(dst, 1); 969 const int32_t lp1 = ggml_get_op_params_i32(dst, 2); 970 const int32_t rp1 = ggml_get_op_params_i32(dst, 3); 971 const int32_t lp2 = ggml_get_op_params_i32(dst, 4); 972 const int32_t rp2 = ggml_get_op_params_i32(dst, 5); 973 const int32_t lp3 = ggml_get_op_params_i32(dst, 6); 974 const int32_t rp3 = ggml_get_op_params_i32(dst, 7); 975 976 int64_t paddings[] = { lp0, rp0, lp1, rp1, lp2, rp2, lp3, rp3 }; 977 aclnn_pad(ctx, acl_src.get(), acl_dst.get(), paddings); 978 } 979 980 /** 981 * @brief Performs 2D average pooling on the input tensor and stores the result 982 * in the destination tensor. 983 * 984 * This function performs average pooling on the source tensor and stores the 985 * result in the destination tensor. The pooling parameters (kernel size, 986 * strides, padding) are specified in the `op_params` of the destination tensor. 987 * 988 * @param ctx The context for the CANN backend operations. 989 * @param dst The destination tensor where the result will be stored. The source 990 * tensor is referenced by `dst->src[0]`. 991 */ 992 static void ggml_cann_avg_pool2d(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 993 ggml_tensor * src = dst->src[0]; 994 GGML_ASSERT(src->type == GGML_TYPE_F32); 995 GGML_ASSERT(dst->type == GGML_TYPE_F32); 996 997 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src, nullptr, nullptr, 0, ACL_FORMAT_NCHW); 998 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst, nullptr, nullptr, 0, ACL_FORMAT_NCHW); 999 1000 const int32_t * opts = (const int32_t *) dst->op_params; 1001 const int k0 = opts[1]; 1002 const int k1 = opts[2]; 1003 const int s0 = opts[3]; 1004 const int s1 = opts[4]; 1005 const int p0 = opts[5]; 1006 const int p1 = opts[6]; 1007 1008 std::vector kernel_dims = { k1, k0 }; 1009 std::vector stride_dims = { s1, s0 }; 1010 std::vector padding_avg_dims = { p1, p0 }; // (padH, padW) 1011 1012 acl_int_array_ptr kernel_size = ggml_cann_create_int_array(kernel_dims.data(), 2); 1013 acl_int_array_ptr strides = ggml_cann_create_int_array(stride_dims.data(), 2); 1014 acl_int_array_ptr paddings_avg = ggml_cann_create_int_array(padding_avg_dims.data(), 2); 1015 1016 bool ceil_mode = false; 1017 bool count_include_pad = true; 1018 int64_t divisor_override = 0; 1019 int8_t cube_math_type = 0; 1020 #ifdef ASCEND_310P 1021 cube_math_type = 1; 1022 #endif 1023 1024 GGML_CANN_CALL_ACLNN_OP(ctx, AvgPool2d, acl_src.get(), kernel_size.get(), strides.get(), paddings_avg.get(), 1025 ceil_mode, count_include_pad, divisor_override, cube_math_type, acl_dst.get()); 1026 } 1027 1028 /** 1029 * @brief Performs 2D max pooling on the input tensor and stores the result in 1030 * the destination tensor. 1031 * 1032 * This function performs max pooling on the source tensor and stores the result 1033 * in the destination tensor. The pooling parameters (kernel size, strides, 1034 * padding) are specified in the `op_params` of the destination tensor. 1035 * 1036 * @param ctx The context for the CANN backend operations. 1037 * @param dst The destination tensor where the result will be stored. The source 1038 * tensor is referenced by `dst->src[0]`. 1039 */ 1040 static void ggml_cann_max_pool2d(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 1041 ggml_tensor * src = dst->src[0]; 1042 GGML_ASSERT(src->type == GGML_TYPE_F32); 1043 GGML_ASSERT(dst->type == GGML_TYPE_F32); 1044 1045 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src, nullptr, nullptr, 0, ACL_FORMAT_NCHW); 1046 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst, nullptr, nullptr, 0, ACL_FORMAT_NCHW); 1047 1048 const int32_t * opts = (const int32_t *) dst->op_params; 1049 const int k0 = opts[1]; 1050 const int k1 = opts[2]; 1051 const int s0 = opts[3]; 1052 const int s1 = opts[4]; 1053 const int p0 = opts[5]; 1054 const int p1 = opts[6]; 1055 1056 int64_t temp_ne[] = { src->ne[0] + p0 * 2, src->ne[1] + p1 * 2, src->ne[2], src->ne[3] }; 1057 size_t temp_nb[GGML_MAX_DIMS]; 1058 1059 temp_nb[0] = ggml_element_size(src); 1060 for (int i = 1; i < GGML_MAX_DIMS; i++) { 1061 temp_nb[i] = temp_nb[i - 1] * temp_ne[i - 1]; 1062 } 1063 1064 ggml_cann_pool_alloc temp_buffer_allocator(ctx.pool(), ggml_nbytes(src) + p0 * 2 + p1 * 2 * src->nb[1]); 1065 void * buffer = temp_buffer_allocator.get(); 1066 acl_tensor_ptr tmp_tensor = ggml_cann_create_tensor(buffer, ACL_FLOAT, ggml_element_size(src), temp_ne, temp_nb, 1067 GGML_MAX_DIMS, ACL_FORMAT_NCHW); 1068 1069 // pad: see padding in ggml_cann_pad() 1070 int64_t paddings[] = { p0, p0, p1, p1, 0, 0, 0, 0 }; 1071 float value = -FLT_MAX; 1072 aclnn_pad(ctx, acl_src.get(), tmp_tensor.get(), paddings, value); 1073 1074 // max_pool 1075 std::vector kernel_dims = { k1, k0 }; 1076 std::vector stride_dims = { s1, s0 }; 1077 // padding_max_dims: [dim0_start, dim0_end, dim1_start, dim1_end] 1078 std::vector padding_max_dims = { 0, 0, 0, 0 }; 1079 std::vector dilation_size = { 1, 1 }; 1080 acl_int_array_ptr kernel_size = ggml_cann_create_int_array(kernel_dims.data(), 2); 1081 acl_int_array_ptr strides = ggml_cann_create_int_array(stride_dims.data(), 2); 1082 acl_int_array_ptr paddings_max = ggml_cann_create_int_array(padding_max_dims.data(), 4); 1083 acl_int_array_ptr dilations = ggml_cann_create_int_array(dilation_size.data(), 2); 1084 1085 bool ceil_mode = false; 1086 int64_t auto_pads = 0; 1087 GGML_CANN_CALL_ACLNN_OP(ctx, MaxPool, tmp_tensor.get(), kernel_size.get(), strides.get(), auto_pads, 1088 paddings_max.get(), dilations.get(), ceil_mode, acl_dst.get()); 1089 } 1090 1091 void ggml_cann_pool2d(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 1092 const int32_t * opts = (const int32_t *) dst->op_params; 1093 enum ggml_op_pool op = static_cast(opts[0]); 1094 switch (op) { 1095 case GGML_OP_POOL_AVG: 1096 ggml_cann_avg_pool2d(ctx, dst); 1097 break; 1098 case GGML_OP_POOL_MAX: 1099 ggml_cann_max_pool2d(ctx, dst); 1100 break; 1101 case GGML_OP_POOL_COUNT: 1102 GGML_ABORT("fatal error"); 1103 break; 1104 } 1105 } 1106 1107 /** 1108 * @brief Copies data from the source tensor to the destination tensor. 1109 * 1110 * This function copies data from the source tensor `acl_src` to the destination 1111 * tensor `acl_dst`. 1112 * 1113 * @param ctx The context for the CANN backend operations. 1114 * @param acl_src The source tensor from which data will be copied. 1115 * @param acl_dst The destination tensor where the data will be copied to. 1116 */ 1117 static void cann_copy(ggml_backend_cann_context & ctx, aclTensor * acl_src, aclTensor * acl_dst) { 1118 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceCopy, acl_dst, acl_src); 1119 } 1120 1121 void ggml_cann_dup(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 1122 ggml_tensor * src0 = dst->src[0]; 1123 1124 if (ggml_are_same_shape(src0, dst)) { 1125 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src0); 1126 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 1127 if (dst->type == src0->type) { 1128 cann_copy(ctx, acl_src.get(), acl_dst.get()); 1129 } else { 1130 aclnn_cast(ctx, acl_src.get(), acl_dst.get(), ggml_cann_type_mapping(dst->type)); 1131 } 1132 } else { 1133 void * src_trans_buffer = src0->data; 1134 ggml_cann_pool_alloc src_buffer_allocator; 1135 if (!ggml_is_contiguous(src0)) { 1136 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src0); 1137 src_buffer_allocator.alloc(ctx.pool(), ggml_nelements(src0) * ggml_type_size(src0->type)); 1138 src_trans_buffer = src_buffer_allocator.get(); 1139 size_t src_trans_nb[GGML_MAX_DIMS]; 1140 src_trans_nb[0] = ggml_type_size(src0->type); 1141 for (int i = 1; i < GGML_MAX_DIMS; i++) { 1142 src_trans_nb[i] = src_trans_nb[i - 1] * src0->ne[i - 1]; 1143 } 1144 acl_tensor_ptr src_trans_tensor = 1145 ggml_cann_create_tensor(src_trans_buffer, ggml_cann_type_mapping(src0->type), 1146 ggml_type_size(src0->type), src0->ne, src_trans_nb, GGML_MAX_DIMS); 1147 cann_copy(ctx, acl_src.get(), src_trans_tensor.get()); 1148 } 1149 1150 size_t src_reshape_nb[GGML_MAX_DIMS]; 1151 src_reshape_nb[0] = ggml_type_size(src0->type); 1152 for (int i = 1; i < GGML_MAX_DIMS; i++) { 1153 src_reshape_nb[i] = src_reshape_nb[i - 1] * dst->ne[i - 1]; 1154 } 1155 1156 acl_tensor_ptr trans_acl_src = 1157 ggml_cann_create_tensor(src_trans_buffer, ggml_cann_type_mapping(src0->type), ggml_type_size(src0->type), 1158 dst->ne, src_reshape_nb, GGML_MAX_DIMS, ACL_FORMAT_ND); 1159 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 1160 1161 if (dst->type == src0->type) { 1162 cann_copy(ctx, trans_acl_src.get(), acl_dst.get()); 1163 } else { 1164 aclnn_cast(ctx, trans_acl_src.get(), acl_dst.get(), ggml_cann_type_mapping(dst->type)); 1165 } 1166 } 1167 } 1168 1169 /** 1170 * @brief Creates an ACL tensor initialized with zeros using a provided buffer. 1171 * 1172 * This function initializes a tensor with zeros using the specified buffer and 1173 * tensor parameters. 1174 * 1175 * @param ctx The context for the CANN backend operations. 1176 * @param buffer The buffer to be used for the tensor data. 1177 * @param n_bytes The size of the buffer in bytes. 1178 * @param ne An array specifying the extents (sizes) of each dimension of the 1179 * tensor. 1180 * @param dims The number of dimensions of the tensor. 1181 * @param type The data type of the tensor. 1182 * @param type_size The size of each element in the tensor data type. 1183 * @return A tensor smart pointer initialized with zeros. 1184 */ 1185 static acl_tensor_ptr aclnn_zero(ggml_backend_cann_context & ctx, 1186 void * buffer, 1187 size_t n_bytes, 1188 int64_t * ne, 1189 int64_t dims, 1190 aclDataType type, 1191 size_t type_size) { 1192 size_t nb[GGML_MAX_DIMS]; 1193 nb[0] = type_size; 1194 for (int i = 1; i < dims; i++) { 1195 nb[i] = nb[i - 1] * ne[i - 1]; 1196 } 1197 1198 acl_tensor_ptr zero = ggml_cann_create_tensor(buffer, type, type_size, ne, nb, dims); 1199 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceZero, zero.get()); 1200 return zero; 1201 GGML_UNUSED(n_bytes); 1202 } 1203 1204 /** 1205 * @brief Creates an ACL tensor initialized with value using a provided buffer. 1206 * 1207 * This function initializes a tensor with value using the specified buffer and 1208 * tensor parameters. 1209 * 1210 * @param ctx The context for the CANN backend operations. 1211 * @param buffer The buffer to be used for the tensor data. 1212 * @param n_bytes The size of the buffer in bytes. 1213 * @param ne An array specifying the extents (sizes) of each dimension of the 1214 * tensor. 1215 * @param dims The number of dimensions of the tensor. 1216 * @param type The data type of the tensor. 1217 * @param type_size The size of each element in the tensor data type. 1218 * @param value The value to be used for initializing the tensor (default 1219 * is 1.0). 1220 * @return A tensor smart pointer initialized with value. 1221 */ 1222 static acl_tensor_ptr aclnn_values(ggml_backend_cann_context & ctx, 1223 void * buffer, 1224 size_t n_bytes, 1225 int64_t * ne, 1226 int64_t dims, 1227 aclDataType type, 1228 size_t type_size, 1229 float value = 1.0f) { 1230 acl_tensor_ptr acl_tensor = aclnn_zero(ctx, buffer, n_bytes, ne, dims, type, type_size); 1231 float alpha_host = 1.0f; 1232 acl_scalar_ptr alpha = ggml_cann_create_scalar(&alpha_host, aclDataType::ACL_FLOAT); 1233 acl_scalar_ptr other = ggml_cann_create_scalar(&value, aclDataType::ACL_FLOAT); 1234 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceAdds, acl_tensor.get(), other.get(), alpha.get()); 1235 return acl_tensor; 1236 } 1237 1238 /** 1239 * @brief Fills a tensor with a scalar value. 1240 * 1241 * This function fills the destination tensor `acl_dst` with the scalar value 1242 * `scalar`. 1243 * 1244 * @param ctx The context for the CANN backend operations. 1245 * @param scalar The scalar value used to fill the tensor. 1246 * @param acl_dst The destination tensor to be filled with the scalar value. 1247 */ 1248 static void aclnn_fill_scalar(ggml_backend_cann_context & ctx, float scalar, aclTensor * acl_dst) { 1249 acl_scalar_ptr acl_scalar = ggml_cann_create_scalar(&scalar, aclDataType::ACL_FLOAT); 1250 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceFillScalar, acl_dst, acl_scalar.get()); 1251 } 1252 1253 /** 1254 * @brief Get or expand a cached tensor filled with a scalar value. 1255 * 1256 * This function manages cached device memory for tensors. If the current 1257 * cache size is insufficient for the requested tensor shape, the old memory will 1258 * be released and new memory will be allocated. The allocated buffer is 1259 * initialized with the given scalar value using CANN operations. 1260 * Finally, an aclTensor object is created from the cached memory and returned. 1261 * 1262 * @param ctx The CANN backend context that manages device memory. 1263 * @param buffer A pointer to the cached device buffer (will be allocated 1264 * or reallocated if necessary). 1265 * @param cache_element The current number of cached elements. This will be 1266 * updated when the cache is expanded. 1267 * @param ne The tensor shape array (number of elements in each dimension). 1268 * @param nb The stride size for each dimension. 1269 * @param dtype Data type of cached tensor. 1270 * @param dims The number of tensor dimensions. 1271 * @param value The scalar value used to fill the tensor (supports zero 1272 * initialization via memset or arbitrary values via fill_scalar). 1273 * @return A tensor smart pointer created from the cached buffer. 1274 */ 1275 static acl_tensor_ptr get_cache_acl_tensor(ggml_backend_cann_context & ctx, 1276 void ** buffer, 1277 int64_t & cache_element, 1278 int64_t * ne, 1279 size_t * nb, 1280 ggml_type dtype, 1281 int64_t dims, 1282 float value) { 1283 // Calculate total number of elements 1284 int64_t n_element = 1; 1285 for (int i = 0; i < dims; i++) { 1286 n_element *= ne[i]; 1287 } 1288 size_t size = n_element * ggml_type_size(dtype); 1289 1290 // Allocate or expand cache if needed 1291 if (cache_element < n_element) { 1292 if (*buffer != nullptr) { 1293 aclrtFree(*buffer); 1294 *buffer = nullptr; 1295 } 1296 1297 ACL_CHECK(aclrtMalloc(buffer, size, ACL_MEM_MALLOC_HUGE_FIRST)); 1298 cache_element = n_element; 1299 1300 // Initialize cache 1301 int64_t pool_ne[1] = { n_element }; 1302 size_t pool_nb[1] = { ggml_type_size(dtype) }; 1303 acl_tensor_ptr acl_value = 1304 ggml_cann_create_tensor(*buffer, ggml_cann_type_mapping(dtype), ggml_type_size(dtype), pool_ne, pool_nb, 1); 1305 aclnn_fill_scalar(ctx, value, acl_value.get()); 1306 } 1307 1308 return ggml_cann_create_tensor(*buffer, ggml_cann_type_mapping(dtype), ggml_type_size(dtype), ne, nb, dims); 1309 } 1310 1311 void ggml_cann_rms_norm(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 1312 ggml_tensor * src = dst->src[0]; 1313 1314 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src); 1315 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 1316 1317 float eps; 1318 memcpy(&eps, dst->op_params, sizeof(float)); 1319 1320 // build gamma. 1321 size_t acl_gamma_nb[GGML_MAX_DIMS]; 1322 // gamma's type is the same with dst. 1323 acl_gamma_nb[0] = ggml_type_size(dst->type); 1324 for (int i = 1; i < GGML_MAX_DIMS; i++) { 1325 acl_gamma_nb[i] = acl_gamma_nb[i - 1] * src->ne[i - 1]; 1326 } 1327 acl_tensor_ptr acl_gamma = get_cache_acl_tensor( 1328 ctx, &ctx.rms_norm_one_tensor_cache.cache, ctx.rms_norm_one_tensor_cache.size, src->ne, acl_gamma_nb, dst->type, 1329 1, // dims 1330 1.0f // value 1331 ); 1332 1333 // build rstd. 1334 int64_t acl_rstd_ne[] = { src->ne[1], src->ne[2], src->ne[3] }; 1335 size_t acl_rstd_nb[GGML_MAX_DIMS - 1]; 1336 // rstd will always be F32. 1337 acl_rstd_nb[0] = sizeof(float); 1338 for (int i = 1; i < GGML_MAX_DIMS - 1; i++) { 1339 acl_rstd_nb[i] = acl_rstd_nb[i - 1] * acl_rstd_ne[i - 1]; 1340 } 1341 acl_tensor_ptr acl_rstd = 1342 get_cache_acl_tensor(ctx, &ctx.rms_norm_zero_tensor_cache.cache, ctx.rms_norm_zero_tensor_cache.size, 1343 acl_rstd_ne, acl_rstd_nb, GGML_TYPE_F32, GGML_MAX_DIMS - 1, 1344 0.0f // value 1345 ); 1346 1347 GGML_CANN_CALL_ACLNN_OP(ctx, RmsNorm, acl_src.get(), acl_gamma.get(), eps, acl_dst.get(), acl_rstd.get()); 1348 } 1349 1350 // TODO: performace is low. 1351 void ggml_cann_diag_mask(ggml_backend_cann_context & ctx, ggml_tensor * dst, float value) { 1352 ggml_tensor * src = dst->src[0]; 1353 1354 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src); 1355 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 1356 1357 const int n_past = ((int32_t *) dst->op_params)[0]; 1358 1359 ggml_cann_pool_alloc one_tensor_allocator(ctx.pool(), ggml_nbytes(src)); 1360 void * buffer = one_tensor_allocator.get(); 1361 1362 acl_tensor_ptr mask_tensor = ggml_cann_create_tensor(buffer, ggml_cann_type_mapping(src->type), 1363 ggml_type_size(src->type), src->ne, src->nb, GGML_MAX_DIMS); 1364 1365 aclnn_fill_scalar(ctx, value, mask_tensor.get()); 1366 1367 float alphaValue = 1.0f; 1368 acl_scalar_ptr alpha = ggml_cann_create_scalar(&alphaValue, aclDataType::ACL_FLOAT); 1369 1370 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceTriu, mask_tensor.get(), n_past + 1); 1371 GGML_CANN_CALL_ACLNN_OP(ctx, Tril, acl_src.get(), n_past + 1, acl_dst.get()); 1372 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceAdd, acl_dst.get(), mask_tensor.get(), alpha.get()); 1373 } 1374 1375 /** 1376 * @brief Permutes the dimensions of a tensor according to a specified order. 1377 * 1378 * This function permutes the dimensions of the source tensor `acl_src` 1379 * according to the order specified in the `new_dim` array and stores the result 1380 * in the destination tensor `acl_dst`. 1381 * 1382 * @param ctx The context for the CANN backend operations. 1383 * @param acl_src The source tensor whose dimensions will be permuted. 1384 * @param acl_dst The destination tensor where the permuted result will be 1385 * stored. 1386 * @param new_dim An array specifying the new order of dimensions for the 1387 * tensor. 1388 * @param dims The number of dimensions in the tensor. 1389 */ 1390 static void aclnn_permute(ggml_backend_cann_context & ctx, 1391 aclTensor * acl_src, 1392 aclTensor * acl_dst, 1393 int64_t * new_dim, 1394 uint64_t dims) { 1395 acl_int_array_ptr acl_dims = ggml_cann_create_int_array(new_dim, dims); 1396 GGML_CANN_CALL_ACLNN_OP(ctx, Permute, acl_src, acl_dims.get(), acl_dst); 1397 } 1398 1399 static void ggml_cann_im2col_2d_post_process(ggml_backend_cann_context & ctx, 1400 ggml_tensor * dst, 1401 ggml_tensor * src1, 1402 aclTensor * tmp_cast_tensor, 1403 aclTensor * tmp_im2col_tensor) { 1404 // Permute: [N, IC * KH * KW, OW * OH] -> [N, OW * OH, IC * KH * KW] 1405 int64_t dst_ne[] = { dst->ne[0], dst->ne[1] * dst->ne[2], dst->ne[3] }; 1406 size_t dst_nb[] = { dst->nb[0], dst->nb[1], dst->nb[3] }; 1407 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst, dst_ne, dst_nb, GGML_MAX_DIMS - 1); 1408 1409 int64_t permute_dim[] = { 0, 2, 1 }; 1410 if (src1->type != dst->type) { 1411 aclnn_permute(ctx, tmp_cast_tensor, acl_dst.get(), permute_dim, 3); 1412 } else { 1413 aclnn_permute(ctx, tmp_im2col_tensor, acl_dst.get(), permute_dim, 3); 1414 } 1415 } 1416 1417 static void ggml_cann_im2col_1d_post_process(ggml_backend_cann_context & ctx, 1418 ggml_tensor * dst, 1419 ggml_tensor * src1, 1420 aclTensor * tmp_cast_tensor, 1421 aclTensor * tmp_im2col_tensor, 1422 const std::vector & im2col_op_params) { 1423 // get params 1424 const int64_t KH = im2col_op_params[0]; 1425 const int64_t KW = im2col_op_params[1]; 1426 const int64_t IW = im2col_op_params[2]; 1427 const int64_t IC = im2col_op_params[3]; 1428 const int64_t N = im2col_op_params[4]; 1429 const int64_t OH = im2col_op_params[5]; 1430 const int64_t OW = im2col_op_params[6]; 1431 const int64_t s0 = im2col_op_params[7]; 1432 const int64_t p0 = im2col_op_params[8]; 1433 const int64_t d0 = im2col_op_params[9]; 1434 const int64_t n_bytes_factor = im2col_op_params[10]; 1435 1436 // Permute: [N, IC * KH * KW, OW * OH] -> 1437 // [N, OW * OH * n_bytes_factor, IC * KH * KW] 1438 ggml_cann_pool_alloc tmp_permute_allocator(ctx.pool()); 1439 tmp_permute_allocator.alloc(ggml_nbytes(dst) * n_bytes_factor); 1440 void * tmp_permute_buffer = tmp_permute_allocator.get(); 1441 1442 int64_t tmp_permute_ne[] = { IC * KH * KW, OW * OH * n_bytes_factor, N }; 1443 size_t tmp_permute_nb[GGML_MAX_DIMS - 1]; 1444 tmp_permute_nb[0] = ggml_type_size(dst->type); 1445 for (int i = 1; i < GGML_MAX_DIMS - 1; i++) { 1446 tmp_permute_nb[i] = tmp_permute_nb[i - 1] * tmp_permute_ne[i - 1]; 1447 } 1448 1449 acl_tensor_ptr tmp_permute_tensor = 1450 ggml_cann_create_tensor(tmp_permute_buffer, ggml_cann_type_mapping(dst->type), ggml_type_size(dst->type), 1451 tmp_permute_ne, tmp_permute_nb, GGML_MAX_DIMS - 1, ACL_FORMAT_ND); 1452 1453 int64_t permute_dim[] = { 0, 2, 1 }; 1454 if (src1->type != dst->type) { 1455 aclnn_permute(ctx, tmp_cast_tensor, tmp_permute_tensor.get(), permute_dim, 3); 1456 } else { 1457 aclnn_permute(ctx, tmp_im2col_tensor, tmp_permute_tensor.get(), permute_dim, 3); 1458 } 1459 1460 // number of times the kernel moves in W dimension 1461 const int n_step_w = (IW + 2 * p0 - d0 * (KW - 1) - 1) / s0 + 1; 1462 size_t offset; 1463 void * cur_dst_buffer = dst->data, *cur_permute_buffer = tmp_permute_buffer; 1464 1465 // memory copy with offset to restore 1D im2col from 2d 1466 if (IC > 1) { 1467 offset = IC * KH * KW * n_step_w * ggml_type_size(dst->type); 1468 size_t cpy_size = KH * KW * ggml_type_size(dst->type); 1469 1470 for (int c = 0; c < IC; c++) { 1471 cur_permute_buffer = (char *) tmp_permute_buffer + offset + KH * KW * c * ggml_type_size(dst->type); 1472 cur_dst_buffer = (char *) dst->data + c * KH * KW * n_step_w * ggml_type_size(dst->type); 1473 1474 for (int i = 0; i < n_step_w; i++) { 1475 ACL_CHECK(aclrtMemcpyAsync(cur_dst_buffer, cpy_size, cur_permute_buffer, cpy_size, 1476 ACL_MEMCPY_DEVICE_TO_DEVICE, ctx.stream())); 1477 cur_dst_buffer = (char *) cur_dst_buffer + KH * KW * ggml_type_size(dst->type); 1478 cur_permute_buffer = (char *) cur_permute_buffer + KH * KW * IC * ggml_type_size(dst->type); 1479 } 1480 } 1481 } else { 1482 offset = KH * KW * n_step_w * ggml_type_size(dst->type); // equal to ggml_nbytes(dst) 1483 ACL_CHECK(aclrtMemcpyAsync(dst->data, offset, (char *) tmp_permute_buffer + offset, offset, 1484 ACL_MEMCPY_DEVICE_TO_DEVICE, ctx.stream())); 1485 } 1486 } 1487 1488 void ggml_cann_im2col(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 1489 ggml_tensor * src0 = dst->src[0]; // kernel 1490 ggml_tensor * src1 = dst->src[1]; // input 1491 1492 GGML_TENSOR_BINARY_OP_LOCALS; 1493 1494 // aclnnIm2col only works on 2D. set s1, p1, d1 to 1 to perform 2D 1495 // im2col and do post-processing to restore it to 1D. 1496 const bool is_2D = ((const int32_t *) (dst->op_params))[6] == 1; 1497 const int32_t s0 = ((const int32_t *) (dst->op_params))[0]; 1498 const int32_t s1 = is_2D ? ((const int32_t *) (dst->op_params))[1] : 1; 1499 const int32_t p0 = ((const int32_t *) (dst->op_params))[2]; 1500 const int32_t p1 = is_2D ? ((const int32_t *) (dst->op_params))[3] : 1; 1501 const int32_t d0 = ((const int32_t *) (dst->op_params))[4]; 1502 const int32_t d1 = is_2D ? ((const int32_t *) (dst->op_params))[5] : 1; 1503 1504 const int64_t N = ne13; 1505 const int64_t IC = ne12; 1506 const int64_t KH = ne01; 1507 const int64_t KW = ne00; 1508 const int64_t IW = ne10; 1509 1510 const int64_t OH = is_2D ? ne2 : 1; 1511 const int64_t OW = ne1; 1512 1513 // memory allocated increased to 3x when is_2D == false 1514 const int64_t n_bytes_factor = is_2D ? 1 : 3; 1515 1516 // im2col: [N,C,H,W] -> [N, IC * KH * KW, OW * OH * n_bytes_factor] 1517 acl_tensor_ptr acl_src1 = ggml_cann_create_tensor(src1); 1518 int64_t tmp_im2col_ne[] = { OW * OH * n_bytes_factor, IC * KH * KW, N }; 1519 size_t tmp_im2col_nb[GGML_MAX_DIMS - 1]; 1520 1521 tmp_im2col_nb[0] = ggml_type_size(src1->type); 1522 for (int i = 1; i < GGML_MAX_DIMS - 1; i++) { 1523 tmp_im2col_nb[i] = tmp_im2col_nb[i - 1] * tmp_im2col_ne[i - 1]; 1524 } 1525 1526 // Calculate im2col. 1527 // If dst is f16, tmp_buffer is f32, we need alloc src.typesize * 1528 // dst.elemcount. 1529 ggml_cann_pool_alloc im2col_allocator(ctx.pool(), ggml_nelements(dst) * ggml_element_size(src1) * n_bytes_factor); 1530 void * tmp_im2col_buffer = im2col_allocator.get(); 1531 1532 acl_tensor_ptr tmp_im2col_tensor = 1533 ggml_cann_create_tensor(tmp_im2col_buffer, ggml_cann_type_mapping(src1->type), ggml_type_size(src1->type), 1534 tmp_im2col_ne, tmp_im2col_nb, GGML_MAX_DIMS - 1, ACL_FORMAT_ND); 1535 1536 std::vector kernel_dims = { KH, KW }; 1537 std::vector dilation_size = { d1, d0 }; 1538 std::vector padding_dims = { p1, p0 }; 1539 std::vector stride_dims = { s1, s0 }; 1540 acl_int_array_ptr kernel_size = ggml_cann_create_int_array(kernel_dims.data(), 2); 1541 acl_int_array_ptr dilations = ggml_cann_create_int_array(dilation_size.data(), 2); 1542 acl_int_array_ptr paddings = ggml_cann_create_int_array(padding_dims.data(), 2); 1543 acl_int_array_ptr strides = ggml_cann_create_int_array(stride_dims.data(), 2); 1544 GGML_CANN_CALL_ACLNN_OP(ctx, Im2col, acl_src1.get(), kernel_size.get(), dilations.get(), paddings.get(), 1545 strides.get(), tmp_im2col_tensor.get()); 1546 1547 // Cast if dst is f16. 1548 acl_tensor_ptr tmp_cast_tensor; 1549 ggml_cann_pool_alloc tmp_cast_allocator(ctx.pool()); 1550 void * tmp_cast_buffer = nullptr; 1551 if (src1->type != dst->type) { 1552 tmp_cast_allocator.alloc(ggml_nbytes(dst) * n_bytes_factor); 1553 tmp_cast_buffer = tmp_cast_allocator.get(); 1554 size_t temp_cast_nb[GGML_MAX_DIMS - 1]; 1555 temp_cast_nb[0] = ggml_type_size(dst->type); 1556 for (int i = 1; i < GGML_MAX_DIMS - 1; i++) { 1557 temp_cast_nb[i] = temp_cast_nb[i - 1] * tmp_im2col_ne[i - 1]; 1558 } 1559 1560 tmp_cast_tensor = 1561 ggml_cann_create_tensor(tmp_cast_buffer, ggml_cann_type_mapping(dst->type), ggml_type_size(dst->type), 1562 tmp_im2col_ne, temp_cast_nb, GGML_MAX_DIMS - 1, ACL_FORMAT_ND); 1563 aclnn_cast(ctx, tmp_im2col_tensor.get(), tmp_cast_tensor.get(), ggml_cann_type_mapping(dst->type)); 1564 } 1565 1566 // post-processing 1567 if (is_2D) { 1568 ggml_cann_im2col_2d_post_process(ctx, dst, src1, tmp_cast_tensor.get(), tmp_im2col_tensor.get()); 1569 } else { 1570 std::vector im2col_op_params = { KH, KW, IW, IC, N, OH, OW, s0, p0, d0, n_bytes_factor }; 1571 ggml_cann_im2col_1d_post_process(ctx, dst, src1, tmp_cast_tensor.get(), tmp_im2col_tensor.get(), 1572 im2col_op_params); 1573 } 1574 } 1575 1576 /** 1577 * @brief Applies element-wise exponential function to the elements of a tensor. 1578 * 1579 * This function computes the exponential of each element in the source tensor 1580 * `acl_src` and stores the result back into the same tensor. 1581 * The operation is defined as: 1582 * \f[ 1583 * \text {acl_src }_i=e^{acl\_src_i} 1584 * \f] 1585 * 1586 * @param ctx The context for the CANN backend operations. 1587 * @param acl_src The tensor on which the exponential function will be applied. 1588 */ 1589 static void aclnn_exp(ggml_backend_cann_context & ctx, aclTensor * acl_src) { 1590 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceExp, acl_src); 1591 } 1592 1593 void aclnn_cos(ggml_backend_cann_context & ctx, aclTensor * acl_src, aclTensor * acl_dst) { 1594 if (acl_dst == nullptr) { 1595 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceCos, acl_src); 1596 } else { 1597 GGML_CANN_CALL_ACLNN_OP(ctx, Cos, acl_src, acl_dst); 1598 } 1599 } 1600 1601 void aclnn_sin(ggml_backend_cann_context & ctx, aclTensor * acl_src, aclTensor * acl_dst) { 1602 if (acl_dst == nullptr) { 1603 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceSin, acl_src); 1604 } else { 1605 GGML_CANN_CALL_ACLNN_OP(ctx, Sin, acl_src, acl_dst); 1606 } 1607 } 1608 1609 void ggml_cann_timestep_embedding(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 1610 const ggml_tensor * src = dst->src[0]; 1611 1612 GGML_ASSERT(src->type == GGML_TYPE_F32); 1613 GGML_ASSERT(dst->type == GGML_TYPE_F32); 1614 1615 const int dim = dst->op_params[0]; 1616 const int max_period = dst->op_params[1]; 1617 int half = dim / 2; 1618 1619 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src); 1620 1621 // arange: [0, ..., half) 1622 float start = 0; 1623 float stop = half; 1624 float step = 1; 1625 int64_t n_elements_arange = half; 1626 int64_t tmp_arange_ne[] = { half }; 1627 size_t tmp_arange_nb[] = { sizeof(dst->type) }; 1628 1629 ggml_cann_pool_alloc arange_allocator(ctx.pool(), half * sizeof(dst->type)); 1630 void * tmp_arange_buffer = arange_allocator.get(); 1631 acl_tensor_ptr tmp_arange_tensor = 1632 ggml_cann_create_tensor(tmp_arange_buffer, ggml_cann_type_mapping(dst->type), ggml_type_size(dst->type), 1633 tmp_arange_ne, tmp_arange_nb, GGML_MAX_DIMS - 3, ACL_FORMAT_ND); 1634 1635 aclnn_arange(ctx, tmp_arange_tensor.get(), start, stop, step, n_elements_arange); 1636 1637 // freq 1638 float freq_param = -logf(max_period) / half; 1639 bool inplace = true; 1640 aclnn_muls(ctx, tmp_arange_tensor.get(), freq_param, nullptr, inplace); 1641 aclnn_exp(ctx, tmp_arange_tensor.get()); 1642 1643 // permute: src [0,1,2,3]->[0,1,3,2] 1644 int64_t tmp_permute_ne[] = { src->ne[1], src->ne[0], src->ne[2], src->ne[3] }; 1645 size_t tmp_permute_nb[GGML_MAX_DIMS]; 1646 tmp_permute_nb[0] = ggml_type_size(src->type); 1647 for (int i = 1; i < GGML_MAX_DIMS; i++) { 1648 tmp_permute_nb[i] = tmp_permute_nb[i - 1] * tmp_permute_ne[i - 1]; 1649 } 1650 1651 ggml_cann_pool_alloc permute_allocator(ctx.pool(), ggml_nbytes(src)); 1652 void * tmp_permute_buffer = permute_allocator.get(); 1653 acl_tensor_ptr tmp_permute_tensor = 1654 ggml_cann_create_tensor(tmp_permute_buffer, ggml_cann_type_mapping(src->type), ggml_type_size(src->type), 1655 tmp_permute_ne, tmp_permute_nb, GGML_MAX_DIMS, ACL_FORMAT_ND); 1656 int64_t permute_dim[] = { 0, 1, 3, 2 }; 1657 int64_t num_dims = 4; 1658 aclnn_permute(ctx, acl_src.get(), tmp_permute_tensor.get(), permute_dim, num_dims); 1659 1660 // timestep * freq 1661 int64_t tmp_mul_ne[] = { src->ne[1] * half, src->ne[0], src->ne[2], src->ne[3] }; 1662 size_t tmp_mul_nb[GGML_MAX_DIMS]; 1663 tmp_mul_nb[0] = ggml_type_size(src->type); 1664 for (int i = 1; i < GGML_MAX_DIMS; i++) { 1665 tmp_mul_nb[i] = tmp_mul_nb[i - 1] * tmp_mul_ne[i - 1]; 1666 } 1667 1668 int mul_nelements = src->ne[1] * half * src->ne[0] * src->ne[2] * src->ne[3]; 1669 1670 ggml_cann_pool_alloc mul_allocator(ctx.pool(), mul_nelements * ggml_type_size(src->type)); 1671 void * tmp_mul_buffer = mul_allocator.get(); 1672 acl_tensor_ptr tmp_mul_tensor = 1673 ggml_cann_create_tensor(tmp_mul_buffer, ggml_cann_type_mapping(src->type), ggml_type_size(src->type), 1674 tmp_mul_ne, tmp_mul_nb, GGML_MAX_DIMS, ACL_FORMAT_ND); 1675 aclnn_mul(ctx, tmp_permute_tensor.get(), tmp_arange_tensor.get(), tmp_mul_tensor.get()); 1676 1677 // cos 1678 ggml_cann_pool_alloc cos_allocator(ctx.pool(), mul_nelements * ggml_type_size(src->type)); 1679 void * tmp_cos_buffer = cos_allocator.get(); 1680 acl_tensor_ptr tmp_cos_tensor = 1681 ggml_cann_create_tensor(tmp_cos_buffer, ggml_cann_type_mapping(dst->type), ggml_type_size(dst->type), 1682 tmp_mul_ne, tmp_mul_nb, GGML_MAX_DIMS, ACL_FORMAT_ND); 1683 1684 aclnn_cos(ctx, tmp_mul_tensor.get(), tmp_cos_tensor.get()); 1685 1686 // sin 1687 ggml_cann_pool_alloc sin_allocator(ctx.pool(), mul_nelements * ggml_type_size(src->type)); 1688 void * tmp_sin_buffer = sin_allocator.get(); 1689 acl_tensor_ptr tmp_sin_tensor = 1690 ggml_cann_create_tensor(tmp_sin_buffer, ggml_cann_type_mapping(dst->type), ggml_type_size(dst->type), 1691 tmp_mul_ne, tmp_mul_nb, GGML_MAX_DIMS, ACL_FORMAT_ND); 1692 1693 aclnn_sin(ctx, tmp_mul_tensor.get(), tmp_sin_tensor.get()); 1694 1695 // concat 1696 int64_t concat_dim = 3; 1697 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 1698 acl_tensor_list_ptr tensor_list = ggml_cann_create_tensor_list(tmp_cos_tensor, tmp_sin_tensor); 1699 aclnn_concat(ctx, tensor_list.get(), acl_dst.get(), concat_dim); 1700 } 1701 1702 /** 1703 * @brief Raises each element of a tensor to the power of the corresponding 1704 * element in another tensor. 1705 * 1706 * This function computes the element-wise power of the destination tensor 1707 * `acl_dst` raised to the power of the exponent tensor `acl_exp`. 1708 * The operation is defined as: 1709 * \f[ 1710 * \text {acl_dst }_i=acl\_dst_i^{\text {acl_exp }_i} 1711 * \f] 1712 * 1713 * @param ctx The context for the CANN backend operations. 1714 * @param acl_dst The destination tensor, which also serves as the base tensor. 1715 * @param acl_exp The exponent tensor, each element of which is used to raise 1716 * the corresponding element in the destination tensor. 1717 */ 1718 static void aclnn_pow_tensor_tensor(ggml_backend_cann_context & ctx, aclTensor * acl_dst, aclTensor * acl_exp) { 1719 GGML_CANN_CALL_ACLNN_OP(ctx, InplacePowTensorTensor, acl_dst, acl_exp); 1720 } 1721 1722 /** 1723 * @brief Generate a range of values and apply a scalar base exponentiation. 1724 * 1725 * This function creates an evenly spaced sequence from `start` to `stop` (exclusive), 1726 * with step size `step`, stores it in a temporary buffer, and then computes: 1727 * 1728 * @f[ 1729 * slope[i] = m^{\left( start + i \cdot step \right)}, \quad 0 \le i < size 1730 * @f] 1731 * 1732 * The results are written to the provided @p slope_buffer. 1733 * 1734 * @param ctx CANN backend context for memory allocation and operator execution. 1735 * @param slope_buffer Pointer to the output buffer (float array) for the computed slope values. 1736 * @param m Scalar base for the exponentiation. 1737 * @param size Number of elements in the generated sequence. 1738 * @param start Starting exponent offset. 1739 * @param stop Stopping exponent offset (exclusive). 1740 * @param step Step size for the exponent increment. 1741 * @param dtype Data type for slope tensor. 1742 */ 1743 static void aclnn_get_slope_inner(ggml_backend_cann_context & ctx, 1744 void * slope_buffer, 1745 float m, 1746 int64_t size, 1747 float start, 1748 float stop, 1749 float step, 1750 ggml_type dtype) { 1751 aclDataType acl_type = ggml_cann_type_mapping(dtype); 1752 size_t type_size = ggml_type_size(dtype); 1753 1754 int64_t ne[] = { size }; 1755 size_t nb[] = { type_size }; 1756 1757 ggml_cann_pool_alloc arange_allocator(ctx.pool(), size * type_size); 1758 void * arange_buffer = arange_allocator.get(); 1759 1760 acl_tensor_ptr arange_tensor = ggml_cann_create_tensor(arange_buffer, acl_type, type_size, ne, nb, 1); 1761 aclnn_arange(ctx, arange_tensor.get(), start, stop, step, size); 1762 1763 acl_tensor_ptr slope_tensor = ggml_cann_create_tensor(slope_buffer, acl_type, type_size, ne, nb, 1); 1764 1765 acl_scalar_ptr sc = ggml_cann_create_scalar(&m, aclDataType::ACL_FLOAT); 1766 1767 GGML_CANN_CALL_ACLNN_OP(ctx, PowScalarTensor, sc.get(), arange_tensor.get(), slope_tensor.get()); 1768 } 1769 1770 /** 1771 * @brief Compute slope values for multiple attention heads based on ALiBi bias parameters. 1772 * 1773 * This function generates slope values for each attention head according to the ALiBi 1774 * (Attention with Linear Biases) method. It splits the computation into two ranges depending 1775 * on whether the head index is less than @p n_head_log2 or not, and uses different base values 1776 * (`m0` and `m1`) for the exponentiation. 1777 * 1778 * @f[ 1779 * slope[h] = 1780 * \begin{cases} 1781 * m_0^{(h + 1)}, & h < n\_head\_log2 \\ 1782 * m_1^{\left( 2 \cdot (h - n\_head\_log2) + 1 \right)}, & h \geq n\_head\_log2 1783 * \end{cases} 1784 * \quad , \quad \text{if } max\_bias > 0 1785 * @f] 1786 * 1787 * If @p max_bias <= 0, all slope values are set to 1.0. 1788 * 1789 * @param ctx CANN backend context for memory allocation and operator execution. 1790 * @param n_head Total number of attention heads. 1791 * @param slope_buffer Pointer to the output buffer (float array) for storing slopes. 1792 * @param max_bias Maximum bias value for slope computation. 1793 * @param dtype Data type for slope tensor. 1794 * 1795 */ 1796 static void aclnn_get_slope(ggml_backend_cann_context & ctx, 1797 int64_t n_head, 1798 void * slope_buffer, 1799 float max_bias, 1800 ggml_type dtype) { 1801 const int n_head_log2 = 1u << (uint32_t) floor(log2(n_head)); 1802 1803 float m0 = powf(2.0f, -(max_bias) / n_head_log2); 1804 float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2); 1805 1806 // const float slope = (max_bias > 0.0f) ? 1807 // h < n_head_log2 ? 1808 // powf(m0, h + 1) : 1809 // powf(m1, 2*(h - n_head_log2) + 1) : 1810 // 1.0f; 1811 // arange1 1812 float start = 0 + 1; 1813 float end = (n_head_log2 - 1) + 1; 1814 float step = 1; 1815 float count = n_head_log2; 1816 // end needs to be +1 because aclnn uses a left-closed, right-open interval. 1817 aclnn_get_slope_inner(ctx, slope_buffer, m0, count, start, end + 1, step, dtype); 1818 if (n_head_log2 < n_head) { 1819 // arange2 1820 start = 2 * (n_head_log2 - n_head_log2) + 1; 1821 end = 2 * ((n_head - 1) - n_head_log2) + 1; 1822 step = 2; 1823 count = n_head - n_head_log2; 1824 aclnn_get_slope_inner(ctx, (char *) slope_buffer + n_head_log2 * ggml_type_size(dtype), m1, count, start, end + 1, 1825 step, dtype); 1826 } 1827 } 1828 1829 /** 1830 * @brief Add ALiBi (Attention with Linear Biases) positional biases to the attention mask. 1831 * 1832 * This function computes the ALiBi slopes for each attention head (if max_bias > 0), 1833 * multiplies them with the attention mask to produce bias tensors, and adds these biases 1834 * to the destination tensor (@p dst). 1835 * 1836 * The function performs necessary broadcasting of the mask and slope tensors to match 1837 * the shape of the destination tensor, then applies element-wise multiplication and addition 1838 * using CANN operators. 1839 * 1840 * @param ctx CANN backend context for memory management and operator execution. 1841 * @param mask Input attention mask tensor, assumed to be contiguous. 1842 * @param dst Destination tensor to which ALiBi biases will be added. 1843 * @param dst_ptr Pointer to the memory of the destination tensor. 1844 * @param max_bias Maximum bias value controlling the slope scaling. 1845 * 1846 * @note 1847 * - Write data into dst_ptr using only the shape information of the dst tensor. 1848 * - `GGML_MAX_DIMS + 2` is used to extend tensor dimensions for broadcasting. 1849 */ 1850 static void aclnn_add_alibi(ggml_backend_cann_context & ctx, 1851 ggml_tensor * mask, 1852 ggml_tensor * dst, 1853 void * dst_ptr, 1854 float max_bias) { 1855 void * slope_buffer = nullptr; 1856 void * bias_buffer = nullptr; 1857 1858 if (max_bias > 0.0f) { 1859 int64_t n_heads = dst->ne[2]; 1860 ggml_cann_pool_alloc slope_allocator(ctx.pool(), n_heads * sizeof(float)); 1861 slope_buffer = slope_allocator.get(); 1862 ggml_cann_pool_alloc bias_allocator(ctx.pool(), ggml_nelements(dst) * ggml_element_size(dst)); 1863 bias_buffer = bias_allocator.get(); 1864 aclnn_get_slope(ctx, n_heads, slope_buffer, max_bias, GGML_TYPE_F32); 1865 } 1866 1867 // broadcast for mask, slop and dst; 1868 int64_t nr2 = dst->ne[2] / mask->ne[2]; 1869 int64_t nr3 = dst->ne[3] / mask->ne[3]; 1870 1871 // broadcast the mask across rows 1872 int64_t mask_ne[] = { mask->ne[0], dst->ne[1], mask->ne[2], 1, mask->ne[3], 1 }; 1873 size_t mask_nb[] = { mask_nb[0] = mask->nb[0], mask_nb[1] = mask->nb[1], mask_nb[2] = mask->nb[2], 1874 mask_nb[3] = mask->nb[2], mask_nb[4] = mask->nb[3], mask_nb[5] = mask->nb[3] }; 1875 1876 int64_t dst_ne[] = { dst->ne[0], dst->ne[1], mask->ne[2], nr2, mask->ne[3], nr3 }; 1877 size_t dst_nb[] = { dst_nb[0] = dst->nb[0], dst_nb[1] = dst->nb[1], dst_nb[2] = dst->nb[2], 1878 dst_nb[3] = dst->nb[2], dst_nb[4] = dst->nb[3], dst_nb[5] = dst->nb[3] }; 1879 1880 // slope is a 1 dim tensor, slope.ne2 == dst.ne2 1881 int64_t slope_ne[] = { 1, 1, mask->ne[2], nr2, 1, 1 }; 1882 size_t slope_nb[GGML_MAX_DIMS + 2]; 1883 slope_nb[0] = sizeof(float); 1884 for (int i = 1; i < GGML_MAX_DIMS + 2; i++) { 1885 slope_nb[i] = slope_nb[i - 1] * slope_ne[i - 1]; 1886 } 1887 1888 acl_tensor_ptr acl_slope = 1889 ggml_cann_create_tensor(slope_buffer, ACL_FLOAT, sizeof(float), slope_ne, slope_nb, GGML_MAX_DIMS + 2); 1890 acl_tensor_ptr acl_mask = ggml_cann_create_tensor(mask, mask_ne, mask_nb, GGML_MAX_DIMS + 2); 1891 1892 // write data into dst_ptr using only the shape information of the dst tensor. 1893 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst_ptr, ggml_cann_type_mapping(dst->type), 1894 ggml_type_size(dst->type), dst_ne, dst_nb, GGML_MAX_DIMS + 2); 1895 1896 if (max_bias > 0.0f) { 1897 int64_t bias_ne[] = { mask->ne[0], dst->ne[1], mask->ne[2], nr2, mask->ne[3], 1 }; 1898 size_t bias_nb[GGML_MAX_DIMS + 2]; 1899 bias_nb[0] = sizeof(float); 1900 for (int i = 1; i < GGML_MAX_DIMS + 2; i++) { 1901 bias_nb[i] = bias_nb[i - 1] * bias_ne[i - 1]; 1902 } 1903 acl_tensor_ptr bias_tensor = 1904 ggml_cann_create_tensor(bias_buffer, ACL_FLOAT, sizeof(float), bias_ne, bias_nb, GGML_MAX_DIMS + 2); 1905 1906 aclnn_mul(ctx, acl_slope.get(), acl_mask.get(), bias_tensor.get()); 1907 aclnn_add(ctx, acl_dst.get(), bias_tensor.get()); 1908 } else { 1909 aclnn_add(ctx, acl_dst.get(), acl_mask.get()); 1910 } 1911 } 1912 1913 void ggml_cann_cpy(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 1914 ggml_cann_dup(ctx, dst); 1915 } 1916 1917 /** 1918 * @brief Applies the softmax function to a tensor along a specified dimension. 1919 * 1920 * This function computes the softmax of the source tensor `acl_src` along the 1921 * specified dimension `dim` and stores the result in the destination tensor 1922 * `acl_dst`. 1923 * 1924 * @param ctx The context for the CANN backend operations. 1925 * @param acl_src The source tensor on which the softmax function will be 1926 * applied. 1927 * @param dim The dimension along which the softmax function will be computed. 1928 * @param acl_dst The destination tensor where the softmax results will be 1929 * stored. 1930 */ 1931 static void aclnn_softmax(ggml_backend_cann_context & ctx, aclTensor * acl_src, int64_t dim, aclTensor * acl_dst) { 1932 GGML_CANN_CALL_ACLNN_OP(ctx, Softmax, acl_src, dim, acl_dst); 1933 } 1934 1935 void ggml_cann_softmax(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 1936 ggml_tensor * src0 = dst->src[0]; 1937 ggml_tensor * src1 = dst->src[1]; // mask 1938 1939 acl_tensor_ptr acl_src0 = ggml_cann_create_tensor(src0); 1940 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 1941 1942 float scale = 1.0f; 1943 float max_bias = 0.0f; 1944 1945 memcpy(&scale, (float *) dst->op_params + 0, sizeof(float)); 1946 memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float)); 1947 1948 // input mul scale 1949 acl_scalar_ptr acl_scale = ggml_cann_create_scalar(&scale, aclDataType::ACL_FLOAT); 1950 ggml_cann_pool_alloc src_tensor_allocator(ctx.pool(), ggml_nbytes(src0)); 1951 void * src_tensor_buffer = src_tensor_allocator.get(); 1952 acl_tensor_ptr softmax_tensor = ggml_cann_create_tensor(src_tensor_buffer, ggml_cann_type_mapping(src0->type), 1953 ggml_element_size(src0), src0->ne, src0->nb, GGML_MAX_DIMS); 1954 1955 aclnn_muls(ctx, acl_src0.get(), scale, softmax_tensor.get(), false); 1956 1957 // mask 1958 if (src1) { 1959 aclnn_add_alibi(ctx, src1, src0, src_tensor_buffer, max_bias); 1960 } 1961 // softmax 1962 aclnn_softmax(ctx, softmax_tensor.get(), 3, acl_dst.get()); 1963 } 1964 1965 1966 void ggml_cann_get_rows(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 1967 ggml_tensor * src0 = dst->src[0]; // weight 1968 ggml_tensor * src1 = dst->src[1]; // index 1969 1970 GGML_ASSERT(dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16 1971 || dst->type == GGML_TYPE_BF16); 1972 1973 // n_idx: number of row indices per (i2, i3) batch slice. 1974 // ggml guarantees: src0->ne[2] == src1->ne[1], src0->ne[3] == src1->ne[2], src1->ne[3] == 1. 1975 const int64_t n_idx = src1->ne[0]; 1976 1977 // Gather all (i2, i3) batch slices from src into dst. 1978 // ggml_cann_create_tensor reverses dims, so ACL sees [ne1, ne0]. 1979 // GatherV2 with dim=0 gathers along ACL dim-0 == ggml ne[1] (the vocabulary / row axis). 1980 // nb: the 4 strides of the source buffer (nb[0..1] for the 2D slice shape, 1981 // nb[2..3] for computing per-batch-slice base pointer offsets). 1982 auto gather_batched = [&](void * src_base, aclDataType acl_type, size_t type_size, 1983 const size_t * nb) { 1984 int64_t src_ne[2] = { src0->ne[0], src0->ne[1] }; 1985 size_t src_nb_2d[2] = { nb[0], nb[1] }; 1986 int64_t dst_ne[2] = { src0->ne[0], n_idx }; 1987 size_t dst_nb_2d[2] = { dst->nb[0], dst->nb[1] }; 1988 int64_t idx_ne[1] = { n_idx }; 1989 size_t idx_nb[1] = { (size_t)ggml_element_size(src1) }; 1990 1991 for (int64_t i3 = 0; i3 < src0->ne[3]; i3++) { 1992 for (int64_t i2 = 0; i2 < src0->ne[2]; i2++) { 1993 acl_tensor_ptr acl_src = ggml_cann_create_tensor( 1994 (char *)src_base + i3 * nb[3] + i2 * nb[2], 1995 acl_type, type_size, src_ne, src_nb_2d, 2); 1996 acl_tensor_ptr acl_idx = ggml_cann_create_tensor( 1997 (char *)src1->data + i3 * src1->nb[2] + i2 * src1->nb[1], 1998 ggml_cann_type_mapping(src1->type), (size_t)ggml_element_size(src1), 1999 idx_ne, idx_nb, 1); 2000 acl_tensor_ptr acl_dst = ggml_cann_create_tensor( 2001 (char *)dst->data + i3 * dst->nb[3] + i2 * dst->nb[2], 2002 acl_type, type_size, dst_ne, dst_nb_2d, 2); 2003 GGML_CANN_CALL_ACLNN_OP(ctx, GatherV2, acl_src.get(), 0, acl_idx.get(), acl_dst.get()); 2004 } 2005 } 2006 }; 2007 2008 switch (src0->type) { 2009 case GGML_TYPE_BF16: 2010 case GGML_TYPE_F16: 2011 case GGML_TYPE_F32: 2012 if (src0->type == dst->type) { 2013 gather_batched(src0->data, 2014 ggml_cann_type_mapping(src0->type), ggml_type_size(src0->type), 2015 src0->nb); 2016 } else { 2017 // Cast src0 to dst type, then gather. 2018 ggml_cann_pool_alloc src_cast_allocator(ctx.pool(), 2019 ggml_nelements(src0) * ggml_element_size(dst)); 2020 size_t src_cast_nb[GGML_MAX_DIMS]; 2021 src_cast_nb[0] = ggml_type_size(dst->type); 2022 for (int i = 1; i < GGML_MAX_DIMS; i++) { 2023 src_cast_nb[i] = src_cast_nb[i - 1] * src0->ne[i - 1]; 2024 } 2025 acl_tensor_ptr acl_src0 = ggml_cann_create_tensor(src0); 2026 acl_tensor_ptr acl_src_cast = ggml_cann_create_tensor( 2027 src_cast_allocator.get(), ggml_cann_type_mapping(dst->type), ggml_type_size(dst->type), 2028 src0->ne, src_cast_nb, GGML_MAX_DIMS); 2029 aclnn_cast(ctx, acl_src0.get(), acl_src_cast.get(), ggml_cann_type_mapping(dst->type)); 2030 2031 gather_batched(src_cast_allocator.get(), 2032 ggml_cann_type_mapping(dst->type), ggml_type_size(dst->type), 2033 src_cast_nb); 2034 } 2035 break; 2036 case GGML_TYPE_Q8_0: 2037 { 2038 // Dequantize Q8_0 to dst type, then gather. 2039 size_t weight_nb[GGML_MAX_DIMS + 1], scale_nb[GGML_MAX_DIMS + 1], dequant_nb[GGML_MAX_DIMS + 1]; 2040 int64_t weight_ne[GGML_MAX_DIMS + 1], scale_ne[GGML_MAX_DIMS + 1], *dequant_ne; 2041 weight_ne[0] = QK8_0; 2042 weight_ne[1] = src0->ne[0] / QK8_0; 2043 weight_nb[0] = sizeof(int8_t); 2044 weight_nb[1] = weight_nb[0] * weight_ne[0]; 2045 for (int i = 2; i < GGML_MAX_DIMS + 1; i++) { 2046 weight_ne[i] = src0->ne[i - 1]; 2047 weight_nb[i] = weight_nb[i - 1] * weight_ne[i - 1]; 2048 } 2049 scale_ne[0] = 1; 2050 scale_ne[1] = src0->ne[0] / QK8_0; 2051 scale_nb[0] = sizeof(uint16_t); 2052 scale_nb[1] = scale_nb[0] * scale_ne[0]; 2053 for (int i = 2; i < GGML_MAX_DIMS + 1; i++) { 2054 scale_ne[i] = src0->ne[i - 1]; 2055 scale_nb[i] = scale_nb[i - 1] * scale_ne[i - 1]; 2056 } 2057 dequant_ne = weight_ne; 2058 dequant_nb[0] = ggml_type_size(dst->type); 2059 for (int i = 1; i < GGML_MAX_DIMS + 1; i++) { 2060 dequant_nb[i] = dequant_nb[i - 1] * dequant_ne[i - 1]; 2061 } 2062 const int64_t scale_offset = ggml_nelements(src0) * sizeof(int8_t); 2063 ggml_cann_pool_alloc dequant_allocator(ctx.pool(), 2064 ggml_nelements(src0) * ggml_type_size(dst->type)); 2065 acl_tensor_ptr acl_weight = ggml_cann_create_tensor(src0->data, ACL_INT8, sizeof(int8_t), 2066 weight_ne, weight_nb, GGML_MAX_DIMS + 1); 2067 acl_tensor_ptr acl_scale = ggml_cann_create_tensor( 2068 src0->data, ACL_FLOAT16, sizeof(uint16_t), scale_ne, scale_nb, 2069 GGML_MAX_DIMS + 1, ACL_FORMAT_ND, scale_offset); 2070 acl_tensor_ptr acl_dequant = ggml_cann_create_tensor( 2071 dequant_allocator.get(), ggml_cann_type_mapping(dst->type), 2072 ggml_type_size(dst->type), dequant_ne, dequant_nb, GGML_MAX_DIMS + 1); 2073 aclnn_mul(ctx, acl_weight.get(), acl_scale.get(), acl_dequant.get()); 2074 2075 // Reinterpret dequant buffer as 4D [src0->ne] with contiguous strides. 2076 dequant_ne = src0->ne; 2077 dequant_nb[0] = ggml_type_size(dst->type); 2078 for (int i = 1; i < GGML_MAX_DIMS; i++) { 2079 dequant_nb[i] = dequant_nb[i - 1] * src0->ne[i - 1]; 2080 } 2081 gather_batched(dequant_allocator.get(), 2082 ggml_cann_type_mapping(dst->type), ggml_type_size(dst->type), 2083 dequant_nb); 2084 break; 2085 } 2086 default: 2087 GGML_ABORT("Unsupported tensor type for GGML_OP_GET_ROWS"); 2088 break; 2089 } 2090 } 2091 2092 void ggml_cann_set_rows(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 2093 ggml_tensor * src0 = dst->src[0]; // source values 2094 ggml_tensor * src1 = dst->src[1]; // row indices 2095 2096 // n_idx: number of source rows to scatter per batch slice. 2097 // ggml guarantees: src0->ne[1] == src1->ne[0]. 2098 const int64_t n_idx = src1->ne[0]; 2099 2100 // Copy n_idx rows of src [ne0, n_idx] into dst [ne0, ne1] at positions given by a 1D index. 2101 // ggml_cann_create_tensor reverses dims, so ACL sees [ne1, ne0] for dst. 2102 // InplaceIndexCopy with dim=0 copies along ACL dim-0 == ggml ne[1] (the row axis). 2103 // src_nb: the 4 strides of the source buffer (nb[0..1] for the 2D slice shape, 2104 // nb[2..3] for computing per-batch-slice base pointer offsets). 2105 auto scatter_batched = [&](void * src_base, aclDataType acl_type, size_t type_size, 2106 const size_t * src_nb) { 2107 int64_t d_ne[2] = { dst->ne[0], dst->ne[1] }; 2108 size_t d_nb[2] = { dst->nb[0], dst->nb[1] }; 2109 int64_t s_ne[2] = { dst->ne[0], n_idx }; 2110 size_t s_nb_2d[2] = { src_nb[0], src_nb[1] }; 2111 int64_t i_ne[1] = { n_idx }; 2112 size_t i_nb[1] = { (size_t)ggml_element_size(src1) }; 2113 2114 for (int64_t i3 = 0; i3 < dst->ne[3]; i3++) { 2115 for (int64_t i2 = 0; i2 < dst->ne[2]; i2++) { 2116 acl_tensor_ptr acl_dst = ggml_cann_create_tensor( 2117 (char *)dst->data + i3 * dst->nb[3] + i2 * dst->nb[2], 2118 acl_type, type_size, d_ne, d_nb, 2); 2119 acl_tensor_ptr acl_idx = ggml_cann_create_tensor( 2120 (char *)src1->data + (i3 % src1->ne[2]) * src1->nb[2] + (i2 % src1->ne[1]) * src1->nb[1], 2121 ggml_cann_type_mapping(src1->type), (size_t)ggml_element_size(src1), 2122 i_ne, i_nb, 1); 2123 acl_tensor_ptr acl_src = ggml_cann_create_tensor( 2124 (char *)src_base + i3 * src_nb[3] + i2 * src_nb[2], 2125 acl_type, type_size, s_ne, s_nb_2d, 2); 2126 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceIndexCopy, acl_dst.get(), 0, acl_idx.get(), acl_src.get()); 2127 } 2128 } 2129 }; 2130 2131 switch (dst->type) { 2132 case GGML_TYPE_F32: 2133 scatter_batched(src0->data, 2134 ggml_cann_type_mapping(dst->type), ggml_type_size(dst->type), 2135 src0->nb); 2136 break; 2137 case GGML_TYPE_F16: 2138 case GGML_TYPE_BF16: 2139 { 2140 // Cast src0 (F32) to dst type first. 2141 ggml_cann_pool_alloc src_cast_allocator(ctx.pool(), 2142 ggml_nelements(src0) * ggml_type_size(dst->type)); 2143 size_t src_cast_nb[GGML_MAX_DIMS]; 2144 src_cast_nb[0] = ggml_type_size(dst->type); 2145 for (int i = 1; i < GGML_MAX_DIMS; i++) { 2146 src_cast_nb[i] = src_cast_nb[i - 1] * src0->ne[i - 1]; 2147 } 2148 acl_tensor_ptr acl_src0 = ggml_cann_create_tensor(src0); 2149 acl_tensor_ptr acl_src_cast = ggml_cann_create_tensor( 2150 src_cast_allocator.get(), ggml_cann_type_mapping(dst->type), ggml_type_size(dst->type), 2151 src0->ne, src_cast_nb, GGML_MAX_DIMS); 2152 aclnn_cast(ctx, acl_src0.get(), acl_src_cast.get(), ggml_cann_type_mapping(dst->type)); 2153 2154 scatter_batched(src_cast_allocator.get(), 2155 ggml_cann_type_mapping(dst->type), ggml_type_size(dst->type), 2156 src_cast_nb); 2157 break; 2158 } 2159 default: 2160 GGML_ABORT("Unsupported tensor type for GGML_OP_SET_ROWS"); 2161 break; 2162 } 2163 } 2164 2165 /** 2166 * @brief Repeats elements of a tensor along a specified dimension. 2167 * 2168 * This function repeats each element of the source tensor `acl_src` a specified 2169 * number of times (`repeats`) along the specified dimension `dim` and stores 2170 * the result in the destination tensor `acl_dst`. 2171 * 2172 * @param ctx The context for the CANN backend operations. 2173 * @param acl_src The source tensor whose elements will be repeated. 2174 * @param acl_dst The destination tensor where the repeated elements will be 2175 * stored. 2176 * @param dim The dimension along which the elements will be repeated. 2177 * @param repeats The number of times each element will be repeated. 2178 * @param output_size The size of the output tensor. 2179 */ 2180 static void aclnn_repeat_interleave(ggml_backend_cann_context & ctx, 2181 aclTensor * acl_src, 2182 aclTensor * acl_dst, 2183 int64_t dim, 2184 int64_t repeats, 2185 int64_t output_size) { 2186 GGML_CANN_CALL_ACLNN_OP(ctx, RepeatInterleaveIntWithDim, acl_src, repeats, dim, output_size, acl_dst); 2187 } 2188 2189 /** 2190 * @brief Performs matrix multiplication with floating-point precision on 2191 * tensors using the CANN backend. 2192 * 2193 * This function performs matrix multiplication of the input tensor and the 2194 * weight tensor, handling broadcasting and transposing as needed, and stores 2195 * the result in the destination tensor `dst`. 2196 * 2197 * @param ctx The context for the CANN backend operations. 2198 * @param dst The destination tensor where the result of the matrix 2199 * multiplication will be stored. 2200 */ 2201 static void ggml_cann_mat_mul_fp(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 2202 ggml_tensor * weight = dst->src[0]; // weight 2203 ggml_tensor * input = dst->src[1]; // input 2204 2205 // when weight ne2 or ne3 is 1, aclnnMatmulGetWorkspaceSize will auto 2206 // broadcast, when weight ne2 or ne3 is not 1, weight need repeat. 2207 BCAST_MUL_MAT_SHAPE(input, weight, dst); 2208 2209 int64_t n_dims = bcast_dims; 2210 if (bcast_input_ne[3] == bcast_weight_ne[3] && bcast_input_ne[3] == 1) { 2211 if (bcast_input_ne[2] == 1 && bcast_weight_ne[2] == 1) { 2212 n_dims = 2; 2213 } else if (bcast_input_ne[2] == 1) { 2214 n_dims = 3; 2215 } 2216 } 2217 2218 acl_tensor_ptr acl_input_tensor = ggml_cann_create_tensor(input, bcast_input_ne, bcast_input_nb, n_dims); 2219 int64_t transpose_ne[] = { bcast_weight_ne[1], bcast_weight_ne[0], bcast_weight_ne[2], 2220 bcast_weight_ne[3], bcast_weight_ne[4], bcast_weight_ne[5] }; 2221 size_t transpose_nb[] = { bcast_weight_nb[1], bcast_weight_nb[0], bcast_weight_nb[2], 2222 bcast_weight_nb[3], bcast_weight_nb[4], bcast_weight_nb[5] }; 2223 acl_tensor_ptr acl_weight_tensor; 2224 2225 // Only check env once. 2226 static bool weight_to_nz = parse_bool(get_env_as_lowercase("GGML_CANN_WEIGHT_NZ").value_or("on")); 2227 if (weight_to_nz && weight->type != GGML_TYPE_BF16 && is_matmul_weight(weight)) { 2228 acl_weight_tensor = ggml_cann_create_tensor(weight, transpose_ne, transpose_nb, n_dims, ACL_FORMAT_FRACTAL_NZ); 2229 } else { 2230 acl_weight_tensor = ggml_cann_create_tensor(weight, transpose_ne, transpose_nb, n_dims, ACL_FORMAT_ND); 2231 } 2232 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst, bcast_dst_ne, bcast_dst_nb, n_dims); 2233 2234 switch (n_dims) { 2235 case 2: 2236 GGML_CANN_CALL_ACLNN_OP(ctx, Mm, acl_input_tensor.get(), acl_weight_tensor.get(), acl_dst.get(), 2); 2237 break; 2238 case 3: 2239 GGML_CANN_CALL_ACLNN_OP(ctx, BatchMatMul, acl_input_tensor.get(), acl_weight_tensor.get(), acl_dst.get(), 2240 2); 2241 break; 2242 default: 2243 // ALLOW_FP32_DOWN_PRECISION, when input is 2244 // fp32, atlas a2 will transpose it to HFLOAT32. 2245 GGML_CANN_CALL_ACLNN_OP(ctx, Matmul, acl_input_tensor.get(), acl_weight_tensor.get(), acl_dst.get(), 1); 2246 break; 2247 } 2248 } 2249 2250 /** 2251 * @brief Performs matrix multiplication with quantized weights and 2252 * floating-point inputs using the CANN backend. 2253 * 2254 * This function performs matrix multiplication of the input tensor `src1` and 2255 * the weight tensor `src0`, handling broadcasting, transposing, and 2256 * quantization as needed, and stores the result in the destination tensor 2257 * `dst`. 2258 * 2259 * @param ctx The context for the CANN backend operations. 2260 * @param dst The destination tensor where the result of the matrix 2261 * multiplication will be stored. 2262 */ 2263 static void ggml_cann_mul_mat_quant(ggml_backend_cann_context & ctx, ggml_tensor * dst, const enum ggml_type type) { 2264 ggml_tensor * src0 = dst->src[0]; // weight 2265 ggml_tensor * src1 = dst->src[1]; // input 2266 2267 // The shape of the weight is NCHW. 2268 // Matrix multiplication uses HW dims. 2269 // HC is regarded as batch. 2270 // weight need transpose. 2271 float weight_elem_size; 2272 if (type == GGML_TYPE_Q4_0) { 2273 weight_elem_size = float(sizeof(uint8_t)) / 2; 2274 } else if (type == GGML_TYPE_Q8_0) { 2275 weight_elem_size = float(sizeof(uint8_t)); 2276 } else { 2277 GGML_ABORT("Only support Q4_0 and Q8_0 MUL_MAT"); 2278 } 2279 float weight_nb[] = { src0->ne[0] * weight_elem_size, weight_elem_size }; 2280 size_t weight_stride = src0->ne[1] * src0->ne[0] * weight_elem_size; 2281 size_t weight_size = weight_stride * src0->ne[2] * src0->ne[3]; 2282 2283 // scale stored at the end of weight. Also need transpose. 2284 size_t scale_elem_size = sizeof(uint16_t); 2285 size_t scale_nb[] = { src0->ne[0] / QK8_0 * scale_elem_size, scale_elem_size }; 2286 size_t scale_stride = src0->ne[1] * src0->ne[0] / QK8_0 * scale_elem_size; 2287 char * scale_offset = (char *) src0->data + weight_size; 2288 2289 // input 2290 size_t input_elem_size = sizeof(uint16_t); 2291 int64_t input_ne[] = { src1->ne[0], src1->ne[1] }; 2292 size_t input_nb[] = { input_elem_size, input_ne[0] * input_elem_size }; 2293 size_t input_stride = input_ne[0] * input_ne[1] * input_elem_size; 2294 ggml_cann_pool_alloc input_alloctor(ctx.pool()); 2295 void * input_buffer = src1->data; 2296 2297 // case in 2298 if (src1->type != GGML_TYPE_F16) { 2299 acl_tensor_ptr acl_src1_tensor = ggml_cann_create_tensor(src1); 2300 input_buffer = input_alloctor.alloc(ggml_nelements(src1) * input_elem_size); 2301 2302 int64_t * input_cast_ne = src1->ne; 2303 size_t input_cast_nb[GGML_MAX_DIMS]; 2304 input_cast_nb[0] = sizeof(uint16_t); 2305 for (int i = 1; i < GGML_MAX_DIMS; i++) { 2306 input_cast_nb[i] = input_cast_nb[i - 1] * input_cast_ne[i - 1]; 2307 } 2308 2309 acl_tensor_ptr acl_input_tensor = ggml_cann_create_tensor(input_buffer, ACL_FLOAT16, input_elem_size, 2310 input_cast_ne, input_cast_nb, GGML_MAX_DIMS); 2311 aclnn_cast(ctx, acl_src1_tensor.get(), acl_input_tensor.get(), ACL_FLOAT16); 2312 } 2313 2314 // output 2315 size_t output_elem_size = sizeof(uint16_t); 2316 size_t output_nb[] = { output_elem_size, dst->ne[0] * output_elem_size }; 2317 ggml_cann_pool_alloc output_allocator(ctx.pool()); 2318 void * output_buffer = output_allocator.alloc(ggml_nelements(dst) * output_elem_size); 2319 size_t output_stride = dst->ne[0] * dst->ne[1] * output_elem_size; 2320 2321 // aclnn 2322 int64_t max_elem_size = 65535; 2323 int64_t split_size = (src0->ne[1] / max_elem_size) + 1; 2324 ggml_cann_pool_alloc workspace_allocator(ctx.pool()); 2325 for (int64_t n1 = 0; n1 < src1->ne[3]; n1++) { 2326 for (int64_t c1 = 0; c1 < src1->ne[2]; c1++) { 2327 int64_t n0 = n1 / (src1->ne[3] / src0->ne[3]); 2328 int64_t c0 = c1 / (src1->ne[2] / src0->ne[2]); 2329 2330 int64_t batch1 = (n1 * src1->ne[2]) + c1; 2331 int64_t batch0 = (n0 * src0->ne[2]) + c0; 2332 2333 acl_tensor_ptr acl_input_tensor = ggml_cann_create_tensor( 2334 (char *) input_buffer + batch1 * input_stride, ACL_FLOAT16, input_elem_size, input_ne, input_nb, 2); 2335 2336 // first split 2337 int64_t weight_ne_offset = 0; 2338 int64_t weight_ne[2] = { max_elem_size > src0->ne[1] ? src0->ne[1] : max_elem_size, src0->ne[0] }; 2339 int64_t scale_ne_offset = 0; 2340 int64_t scale_ne[2] = { weight_ne[0], weight_ne[1] / QK8_0 }; 2341 int64_t output_ne_offset = 0; 2342 int64_t output_ne[2] = { weight_ne[0], dst->ne[1] }; 2343 2344 acl_tensor_ptr acl_weight_tensor = 2345 ggml_cann_create_tensor((char *) src0->data + batch0 * weight_stride, ggml_cann_type_mapping(type), 2346 weight_elem_size, weight_ne, weight_nb, 2, ACL_FORMAT_ND, weight_ne_offset); 2347 acl_tensor_ptr acl_scale_tensor = 2348 ggml_cann_create_tensor(scale_offset + batch0 * scale_stride, ACL_FLOAT16, scale_elem_size, scale_ne, 2349 scale_nb, 2, ACL_FORMAT_ND, scale_ne_offset); 2350 acl_tensor_ptr acl_output_tensor = 2351 ggml_cann_create_tensor((char *) output_buffer + batch1 * output_stride, ACL_FLOAT16, output_elem_size, 2352 output_ne, output_nb, 2, ACL_FORMAT_ND, output_ne_offset); 2353 int64_t antiquantGroupSize = 0; 2354 if (src0->ne[0] > QK8_0) { 2355 antiquantGroupSize = QK8_0; 2356 } 2357 GGML_CANN_CALL_ACLNN_OP(ctx, WeightQuantBatchMatmulV2, acl_input_tensor.get(), acl_weight_tensor.get(), 2358 acl_scale_tensor.get(), nullptr, nullptr, nullptr, nullptr, antiquantGroupSize, 2359 acl_output_tensor.get()); 2360 2361 // other splits 2362 for (int64_t split = 1; split < split_size; split++) { 2363 weight_ne_offset += weight_elem_size * weight_ne[0] * weight_ne[1]; 2364 weight_ne[0] = 2365 max_elem_size * (split + 1) > src0->ne[1] ? src0->ne[1] - (max_elem_size * split) : max_elem_size; 2366 scale_ne_offset += scale_elem_size * scale_ne[0] * scale_ne[1]; 2367 scale_ne[0] = weight_ne[0]; 2368 output_ne_offset += output_elem_size * output_ne[0] * output_ne[1]; 2369 output_ne[0] = weight_ne[0]; 2370 2371 acl_weight_tensor = 2372 ggml_cann_create_tensor((char *) src0->data + batch0 * weight_stride, ggml_cann_type_mapping(type), 2373 weight_elem_size, weight_ne, weight_nb, 2, ACL_FORMAT_ND, weight_ne_offset); 2374 acl_scale_tensor = 2375 ggml_cann_create_tensor(scale_offset + batch0 * scale_stride, ACL_FLOAT16, scale_elem_size, 2376 scale_ne, scale_nb, 2, ACL_FORMAT_ND, scale_ne_offset); 2377 acl_output_tensor = 2378 ggml_cann_create_tensor((char *) output_buffer + batch1 * output_stride, ACL_FLOAT16, 2379 output_elem_size, output_ne, output_nb, 2, ACL_FORMAT_ND, output_ne_offset); 2380 GGML_CANN_CALL_ACLNN_OP(ctx, WeightQuantBatchMatmulV2, acl_input_tensor.get(), acl_weight_tensor.get(), 2381 acl_scale_tensor.get(), nullptr, nullptr, nullptr, nullptr, antiquantGroupSize, 2382 acl_output_tensor.get()); 2383 } 2384 } 2385 } 2386 2387 // cast out 2388 if (dst->type != GGML_TYPE_F16) { 2389 int64_t * output_cast_ne = dst->ne; 2390 size_t output_cast_nb[GGML_MAX_DIMS]; 2391 output_cast_nb[0] = sizeof(uint16_t); 2392 for (int i = 1; i < GGML_MAX_DIMS; i++) { 2393 output_cast_nb[i] = output_cast_nb[i - 1] * output_cast_ne[i - 1]; 2394 } 2395 2396 acl_tensor_ptr acl_output_tensor = ggml_cann_create_tensor(output_buffer, ACL_FLOAT16, output_elem_size, 2397 output_cast_ne, output_cast_nb, GGML_MAX_DIMS); 2398 acl_tensor_ptr acl_dst_tensor = ggml_cann_create_tensor(dst); 2399 aclnn_cast(ctx, acl_output_tensor.get(), acl_dst_tensor.get(), ggml_cann_type_mapping(dst->type)); 2400 } 2401 } 2402 2403 void ggml_cann_mul_mat(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 2404 const enum ggml_type type = dst->src[0]->type; 2405 switch (type) { 2406 case GGML_TYPE_F32: 2407 case GGML_TYPE_F16: 2408 #ifndef ASCEND_310P 2409 case GGML_TYPE_BF16: 2410 #endif 2411 ggml_cann_mat_mul_fp(ctx, dst); 2412 break; 2413 case GGML_TYPE_Q4_0: 2414 case GGML_TYPE_Q8_0: 2415 ggml_cann_mul_mat_quant(ctx, dst, type); 2416 break; 2417 default: 2418 GGML_ABORT("Unsupported type for mul_mat"); 2419 break; 2420 } 2421 } 2422 2423 /** 2424 * @brief Rolls the elements of a tensor along a specified dimension. 2425 * 2426 * This function rolls the elements of the source tensor `acl_src` by the 2427 * specified shifts `shifts` along the specified dimensions `dims`, and stores 2428 * the result in the destination tensor `acl_dst`. 2429 * 2430 * @param ctx The context for the CANN backend operations. 2431 * @param acl_src The source tensor whose elements will be rolled. 2432 * @param acl_dst The destination tensor where the rolled elements will be 2433 * stored. 2434 * @param shifts An array specifying the number of positions by which elements 2435 * are shifted. 2436 * @param dims An array specifying the dimensions along which elements are 2437 * shifted. 2438 */ 2439 static void aclnn_roll(ggml_backend_cann_context & ctx, 2440 aclTensor * acl_src, 2441 aclTensor * acl_dst, 2442 int64_t * shifts, 2443 int64_t * dims) { 2444 acl_int_array_ptr acl_shifts = ggml_cann_create_int_array(shifts, 1); 2445 acl_int_array_ptr acl_dims = ggml_cann_create_int_array(dims, 1); 2446 GGML_CANN_CALL_ACLNN_OP(ctx, Roll, acl_src, acl_shifts.get(), acl_dims.get(), acl_dst); 2447 } 2448 2449 /** 2450 * @brief Fills specified positions of a tensor with a scalar value. 2451 * 2452 * This function fills the positions in the source tensor `acl_src` specified by 2453 * `index` along the dimension `dim` with the scalar value `value`. 2454 * 2455 * @param ctx The context for the CANN backend operations. 2456 * @param acl_src The source tensor where the positions will be filled. 2457 * @param dim The dimension along which the positions are specified. 2458 * @param index An array specifying the positions to be filled. 2459 * @param index_num The number of positions specified in the index array. 2460 * @param value The scalar value used to fill the specified positions. 2461 */ 2462 static void aclnn_index_fill_tensor(ggml_backend_cann_context & ctx, 2463 aclTensor * acl_src, 2464 int64_t dim, 2465 int64_t * index, 2466 int64_t index_num, 2467 float value) { 2468 acl_int_array_ptr acl_index = ggml_cann_create_int_array(index, index_num); 2469 acl_scalar_ptr acl_value = ggml_cann_create_scalar(&value, aclDataType::ACL_FLOAT); 2470 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceIndexFillTensor, acl_src, dim, acl_index.get(), acl_value.get()); 2471 } 2472 2473 /** 2474 * @brief Initializes and caches all intermediate tensors required for RoPE 2475 * (Rotary Position Embedding), including support for Yarn, mRoPE, 2476 * i-mRoPE, Neox repeat strategy, independent sectors, frequency factors, 2477 * and multi-section rotary groups. 2478 * 2479 * This function computes and caches the per-dimension θ coefficients used for 2480 * Q/K rotary embedding. The cache is shared across layers, and recomputed only 2481 * when any dependent parameter changes. 2482 * 2483 * The function now supports: 2484 * - Yarn RoPE extrapolation (via @param corr_dims and @param ext_factor) 2485 * - Per-dimension independent sector exponent rules (indep_sects + sections[]) 2486 * - Multi-section RoPE (mRoPE) index mapping (mrope_used + is_imrope) 2487 * - Frequency factor division (src2) 2488 * - Neox / normal repeat expansion modes 2489 * 2490 * @param ctx CANN backend context, containing memory pool, 2491 * cached buffers, and runtime stream. 2492 * @param dst Destination ggml_tensor whose computation 2493 * depends on RoPE (typically Qcur or Kcur). 2494 * @param corr_dims [low, high] Yarn correction range. 2495 * @param ext_factor Yarn extrapolation strength. 0 = disabled. 2496 * @param theta_scale Base multiplier for per-dimension θ exponent. 2497 * @param freq_scale Global frequency scaling factor. 2498 * @param attn_factor Optional scaling applied to sin/cos (if needed). 2499 * @param is_neox Whether to use Neox-style dimension interleave. 2500 * @param sections 4-way sector sizes for independent-section RoPE 2501 * and multi-section mRoPE (t/h/w/e). 2502 * @param mrope_used Whether to enable multi-section rotary embedding. 2503 * @param is_imrope Whether to apply interleaved mRoPE rules. 2504 * @param indep_sects Whether each dimension runs independent exponent 2505 * resets based on @p sections. 2506 */ 2507 static void aclnn_rope_cache_init(ggml_backend_cann_context & ctx, 2508 ggml_tensor * dst, 2509 float * corr_dims, 2510 float ext_factor, 2511 float theta_scale, 2512 float freq_scale, 2513 float attn_factor, 2514 bool is_neox, 2515 int sections[4], 2516 bool mrope_used, 2517 bool is_imrope, 2518 bool indep_sects, 2519 int64_t rope_dims) { 2520 ggml_tensor * src1 = dst->src[1]; // position 2521 ggml_tensor * src2 = dst->src[2]; // freq_factors 2522 2523 int64_t theta_scale_length = rope_dims / 2; 2524 int64_t position_length = dst->ne[2]; 2525 2526 // TODO: check theta_scale_length and position_length. 2527 if (src2 == nullptr && ctx.rope_cache.cached && 2528 ctx.rope_cache.equal(theta_scale_length, position_length, ext_factor, theta_scale, freq_scale, attn_factor, 2529 is_neox, indep_sects, mrope_used, is_imrope, sections)) { 2530 // use cache. 2531 return; 2532 } 2533 2534 // Step0: calculate tensor shape. 2535 int64_t theta_scale_ne[] = { theta_scale_length, 1, 1, 1 }; 2536 size_t theta_scale_nb[] = { sizeof(float), theta_scale_length * sizeof(float), theta_scale_length * sizeof(float), 2537 theta_scale_length * sizeof(float) }; 2538 2539 GGML_ASSERT(src1->type == GGML_TYPE_I32); 2540 int64_t position_ne[] = { 1, 1, position_length, 1 }; 2541 size_t position_nb[] = { sizeof(int32_t), sizeof(int32_t), sizeof(int32_t), sizeof(int32_t) * position_length }; 2542 2543 int64_t cache_ne[] = { theta_scale_length, 1, position_length, 1 }; 2544 size_t cache_nb[GGML_MAX_DIMS]; 2545 cache_nb[0] = sizeof(float); 2546 for (int i = 1; i < GGML_MAX_DIMS; i++) { 2547 cache_nb[i] = cache_nb[i - 1] * cache_ne[i - 1]; 2548 } 2549 2550 // Step1: Compute the coefficient of theta. During the cache_init process, aside from 2551 // (1) multiplying by the position, 2552 // (2) dividing by freq_factors, 2553 // (3) computing the sine and cosine, 2554 // the other parameters used in the computation generally do not change in most scenarios. 2555 // Therefore, we can first compute this part of the result and then cache it. 2556 2557 // Step1.1: prepare theta_scale exponent. if this exponent updated, should update theta_scale_tensor. 2558 acl_tensor_ptr acl_theta_scale_tensor; 2559 bool theta_scale_updated = false; 2560 if (ctx.rope_cache.theta_scale_length != theta_scale_length || ctx.rope_cache.theta_scale != theta_scale || 2561 ctx.rope_cache.indep_sects != indep_sects) { 2562 theta_scale_updated = true; 2563 if (ctx.rope_cache.theta_scale_exp_host != nullptr) { 2564 free(ctx.rope_cache.theta_scale_exp_host); 2565 } 2566 ctx.rope_cache.theta_scale_exp_host = (float *) malloc(theta_scale_length * sizeof(float)); 2567 GGML_ASSERT(ctx.rope_cache.theta_scale_exp_host != nullptr); 2568 if (!indep_sects) { 2569 ctx.rope_cache.theta_scale_exp_host[0] = 1; 2570 for (int i = 1; i < theta_scale_length; i++) { 2571 ctx.rope_cache.theta_scale_exp_host[i] = ctx.rope_cache.theta_scale_exp_host[i - 1] * theta_scale; 2572 } 2573 } else { 2574 int sect_dims = sections[0] + sections[1] + sections[2] + sections[3]; 2575 int sec_w = sections[1] + sections[0]; 2576 int sec_e = sections[2] + sec_w; 2577 2578 ctx.rope_cache.theta_scale_exp_host[0] = 1; 2579 for (int i = 1; i < theta_scale_length; i++) { 2580 int sector = i % sect_dims; 2581 if (sector == 0 || sector == sections[0] || sector == sec_w || sector == sec_e) { 2582 ctx.rope_cache.theta_scale_exp_host[i] = 1; 2583 continue; 2584 } 2585 ctx.rope_cache.theta_scale_exp_host[i] = ctx.rope_cache.theta_scale_exp_host[i - 1] * theta_scale; 2586 } 2587 } 2588 2589 if (ctx.rope_cache.theta_scale_cache != nullptr) { 2590 ACL_CHECK(aclrtFree(ctx.rope_cache.theta_scale_cache)); 2591 } 2592 ACL_CHECK(aclrtMalloc(&ctx.rope_cache.theta_scale_cache, theta_scale_length * sizeof(float), 2593 ACL_MEM_MALLOC_HUGE_FIRST)); 2594 2595 ACL_CHECK(aclrtMemcpyAsync(ctx.rope_cache.theta_scale_cache, theta_scale_length * sizeof(float), 2596 ctx.rope_cache.theta_scale_exp_host, theta_scale_length * sizeof(float), 2597 ACL_MEMCPY_HOST_TO_DEVICE, ctx.stream())); 2598 } 2599 acl_theta_scale_tensor = ggml_cann_create_tensor(ctx.rope_cache.theta_scale_cache, ACL_FLOAT, sizeof(float), 2600 theta_scale_ne, theta_scale_nb, 1); 2601 2602 // Step1.2: prepare rope_yarn_ramp, if this part updated, should update theta_scale_tensor. 2603 // TODO: acl_yarn_ramp_tensor use rope cache. 2604 bool yarn_ramp_tensor_updated = false; 2605 acl_tensor_ptr acl_yarn_ramp_tensor; 2606 if (ext_factor != 0 && (theta_scale_updated || ctx.rope_cache.theta_scale_length != theta_scale_length || 2607 ctx.rope_cache.freq_scale != freq_scale)) { 2608 yarn_ramp_tensor_updated = true; 2609 if (ctx.rope_cache.yarn_ramp_cache != nullptr) { 2610 ACL_CHECK(aclrtFree(ctx.rope_cache.yarn_ramp_cache)); 2611 } 2612 ACL_CHECK(aclrtMalloc(&ctx.rope_cache.yarn_ramp_cache, theta_scale_length * sizeof(float), 2613 ACL_MEM_MALLOC_HUGE_FIRST)); 2614 // -rope_yarn_ramp 2615 // const float y = (i0 / 2 - low) / MAX(0.001f, high - low); 2616 // return MIN(1, MAX(0, y)) - 1; 2617 acl_yarn_ramp_tensor = ggml_cann_create_tensor(ctx.rope_cache.yarn_ramp_cache, ACL_FLOAT, sizeof(float), 2618 theta_scale_ne, theta_scale_nb, 1); 2619 float zero_value = 0, one_value = 1; 2620 float denom_safe_value = MAX(0.001f, corr_dims[1] - corr_dims[0]); 2621 acl_scalar_ptr low = ggml_cann_create_scalar(&corr_dims[0], aclDataType::ACL_FLOAT); 2622 acl_scalar_ptr zero = ggml_cann_create_scalar(&zero_value, aclDataType::ACL_FLOAT); 2623 acl_scalar_ptr one = ggml_cann_create_scalar(&one_value, aclDataType::ACL_FLOAT); 2624 acl_scalar_ptr denom_safe = ggml_cann_create_scalar(&denom_safe_value, aclDataType::ACL_FLOAT); 2625 acl_scalar_ptr ext_factor_sc = ggml_cann_create_scalar(&ext_factor, aclDataType::ACL_FLOAT); 2626 2627 aclnn_arange(ctx, acl_yarn_ramp_tensor.get(), 0, theta_scale_length, 1, theta_scale_length); 2628 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceSubs, acl_yarn_ramp_tensor.get(), low.get(), one.get()); 2629 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceDivs, acl_yarn_ramp_tensor.get(), denom_safe.get()); 2630 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceThreshold, acl_yarn_ramp_tensor.get(), zero.get(), zero.get()); 2631 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceClampMax, acl_yarn_ramp_tensor.get(), one.get()); 2632 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceSubs, acl_yarn_ramp_tensor.get(), one.get(), one.get()); 2633 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceMuls, acl_yarn_ramp_tensor.get(), ext_factor_sc.get()); 2634 2635 // theta_interp = freq_scale * theta_extrap; 2636 // theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix; 2637 // theta = freq_scale * theta_extrap * (1 - ramp_mix) + theta_extrap * ramp_mix; 2638 // theta = freq_scale * theta_extrap - freq_scale * theta_extrap * ramp_mix + theta_extrap * ramp_mix; 2639 // theta = theta_extrap * (freq_scale - freq_scale * ramp_mix + ramp_mix); 2640 // 2641 // we cache (freq_scale - freq_scale * ramp_mix + ramp_mix), Considering that the rope_yarn_ramp here is the inverse 2642 // cache freq_scale + (freq_scale - 1) * ramp_mix 2643 float freq_scale_1 = freq_scale - 1; 2644 acl_scalar_ptr freq_scale_sc = ggml_cann_create_scalar(&freq_scale, aclDataType::ACL_FLOAT); 2645 acl_scalar_ptr freq_scale_1_sc = ggml_cann_create_scalar(&freq_scale_1, aclDataType::ACL_FLOAT); 2646 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceMuls, acl_yarn_ramp_tensor.get(), freq_scale_1_sc.get()); 2647 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceAdds, acl_yarn_ramp_tensor.get(), freq_scale_sc.get(), one.get()); 2648 } else { 2649 acl_yarn_ramp_tensor = ggml_cann_create_tensor(ctx.rope_cache.yarn_ramp_cache, ACL_FLOAT, sizeof(float), 2650 theta_scale_ne, theta_scale_nb, 1); 2651 } 2652 // Step 1.3: update theta_scale_tensor according to ext_factor or freq_scale. 2653 if (ext_factor != 0) { 2654 if (theta_scale_updated || yarn_ramp_tensor_updated) { 2655 theta_scale_updated = true; 2656 aclnn_mul(ctx, acl_theta_scale_tensor.get(), acl_yarn_ramp_tensor.get()); 2657 } 2658 } else { 2659 if (freq_scale != 1 && (ctx.rope_cache.freq_scale != freq_scale || theta_scale_updated)) { 2660 theta_scale_updated = true; 2661 aclnn_muls(ctx, acl_theta_scale_tensor.get(), freq_scale, nullptr, true); 2662 } 2663 } 2664 2665 // Nothing changed, use cache. 2666 if (!theta_scale_updated) { 2667 acl_theta_scale_tensor = ggml_cann_create_tensor(ctx.rope_cache.theta_scale_cache, ACL_FLOAT, sizeof(float), 2668 theta_scale_ne, theta_scale_nb, GGML_MAX_DIMS); 2669 } 2670 2671 // Step 1.4: prepare select index if mrope 2672 acl_tensor_ptr position_select_index_tensor; 2673 if (mrope_used) { 2674 if (ctx.rope_cache.sections[0] != sections[0] || ctx.rope_cache.sections[1] != sections[1] || 2675 ctx.rope_cache.sections[2] != sections[2] || ctx.rope_cache.sections[3] != sections[3] || 2676 ctx.rope_cache.theta_scale_length != theta_scale_length || ctx.rope_cache.is_imrope != is_imrope) { 2677 if (ctx.rope_cache.position_select_index_host != nullptr) { 2678 free(ctx.rope_cache.position_select_index_host); 2679 } 2680 ctx.rope_cache.position_select_index_host = (int *) malloc(theta_scale_length * sizeof(int)); 2681 GGML_ASSERT(ctx.rope_cache.position_select_index_host != nullptr); 2682 int sect_dims = sections[0] + sections[1] + sections[2] + sections[3]; 2683 int sec_w = sections[1] + sections[0]; 2684 int sec_e = sections[2] + sec_w; 2685 // t,h,w,e 2686 for (int i = 0; i < theta_scale_length; i++) { 2687 int sector = i % sect_dims; 2688 2689 if (is_imrope) { // qwen3vl apply interleaved mrope 2690 if (sector % 3 == 1 && sector < 3 * sections[1]) { 2691 ctx.rope_cache.position_select_index_host[i] = 1; 2692 } else if (sector % 3 == 2 && sector < 3 * sections[2]) { 2693 ctx.rope_cache.position_select_index_host[i] = 2; 2694 } else if (sector % 3 == 0 && sector < 3 * sections[0]) { 2695 ctx.rope_cache.position_select_index_host[i] = 0; 2696 } else { 2697 ctx.rope_cache.position_select_index_host[i] = 3; 2698 } 2699 } else { 2700 if (sector >= sections[0] && sector < sec_w) { 2701 ctx.rope_cache.position_select_index_host[i] = 1; 2702 } else if (sector >= sec_w && sector < sec_e) { 2703 ctx.rope_cache.position_select_index_host[i] = 2; 2704 } else if (sector >= sec_e) { 2705 ctx.rope_cache.position_select_index_host[i] = 3; 2706 } else { 2707 ctx.rope_cache.position_select_index_host[i] = 0; 2708 } 2709 } 2710 } 2711 2712 if (ctx.rope_cache.position_select_index != nullptr) { 2713 ACL_CHECK(aclrtFree(ctx.rope_cache.position_select_index)); 2714 } 2715 ACL_CHECK(aclrtMalloc(&ctx.rope_cache.position_select_index, theta_scale_length * sizeof(int), 2716 ACL_MEM_MALLOC_HUGE_FIRST)); 2717 2718 ACL_CHECK(aclrtMemcpyAsync(ctx.rope_cache.position_select_index, theta_scale_length * sizeof(int), 2719 ctx.rope_cache.position_select_index_host, theta_scale_length * sizeof(int), 2720 ACL_MEMCPY_HOST_TO_DEVICE, ctx.stream())); 2721 } 2722 2723 position_select_index_tensor = ggml_cann_create_tensor(ctx.rope_cache.position_select_index, ACL_INT32, 2724 sizeof(int), theta_scale_ne, theta_scale_nb, 1); 2725 } 2726 2727 // Step2: divide by freq_factors 2728 ggml_cann_pool_alloc freq_fac_res_allocator(ctx.pool()); 2729 if (src2) { 2730 freq_fac_res_allocator.alloc(theta_scale_length * sizeof(float)); 2731 void * freq_fac_res_ptr = freq_fac_res_allocator.get(); 2732 acl_tensor_ptr acl_freq_factors_tensor = 2733 ggml_cann_create_tensor(src2->data, ggml_cann_type_mapping(src2->type), ggml_type_size(src2->type), 2734 theta_scale_ne, theta_scale_nb, GGML_MAX_DIMS); 2735 acl_tensor_ptr acl_freq_fac_res_tensor = ggml_cann_create_tensor(freq_fac_res_ptr, ACL_FLOAT, sizeof(float), 2736 theta_scale_ne, theta_scale_nb, GGML_MAX_DIMS); 2737 aclnn_div(ctx, acl_theta_scale_tensor.get(), acl_freq_factors_tensor.get(), acl_freq_fac_res_tensor.get()); 2738 std::swap(acl_theta_scale_tensor, acl_freq_fac_res_tensor); 2739 } 2740 2741 // Step3: prepare position_tensor 2742 acl_tensor_ptr acl_position_tensor; 2743 ggml_cann_pool_alloc mrope_position_acllocator(ctx.pool()); 2744 if (mrope_used) { 2745 // Step3.1: select current position; 2746 // position : 2747 // pos1: [[0, 1 ,2 ,3 ], 2748 // pos2: [4, 5 ,6 ,7 ], 2749 // pos3: [8, 9 ,10,11], 2750 // pos4: [12,13,14,15] ] 2751 // 2752 // select index = [0, 1, 2, 2, 1, 0] 2753 // 2754 // selected_tensor: 2755 // [[0, 1 ,2 ,3 ], 2756 // [4, 5 ,6 ,7 ], 2757 // [8, 9 ,10,11], 2758 // [8, 9 ,10,11], 2759 // [4, 5 ,6 ,7 ], 2760 // [0, 1 ,2 ,3 ]] 2761 // 2762 // transpose, from [seq_len:dims] to [dims:seq_len] 2763 // [0, 4, 8 ,8 ,4, 0], 2764 // [1, 5, 9, 9, 5, 1], 2765 // [2, 6, 10,10,6 ,2], 2766 // [3, 7, 11,11,7 3 ]] 2767 // 2768 // multipy by theta_scale_tensor 2769 // [theta_scale^0, theta_scale^1, ..., theta_scale ^ n] 2770 2771 int64_t mrope_position_ne[] = { position_length, 4 }; 2772 size_t mrope_position_nb[] = { sizeof(int), position_length * sizeof(int) }; 2773 acl_tensor_ptr mrope_position = 2774 ggml_cann_create_tensor(src1->data, ggml_cann_type_mapping(src1->type), ggml_type_size(src1->type), 2775 mrope_position_ne, mrope_position_nb, 2); 2776 2777 // selected position tensor's shape is a transpose of cache tensor. 2778 int64_t selected_position_ne[] = { position_length, theta_scale_length }; 2779 size_t selected_position_nb[] = { sizeof(float), position_length * sizeof(float) }; 2780 mrope_position_acllocator.alloc(theta_scale_length * position_length * sizeof(float)); 2781 void * mrope_position_buffer = mrope_position_acllocator.get(); 2782 acl_position_tensor = 2783 ggml_cann_create_tensor(mrope_position_buffer, ggml_cann_type_mapping(src1->type), 2784 ggml_type_size(src1->type), selected_position_ne, selected_position_nb, 2); 2785 GGML_CANN_CALL_ACLNN_OP(ctx, IndexSelect, mrope_position.get(), 0, position_select_index_tensor.get(), 2786 acl_position_tensor.get()); 2787 2788 // transpose 2789 int64_t transposed_ne[] = { position_length, 1, theta_scale_length, 1 }; 2790 size_t transposed_nb[GGML_MAX_DIMS]; 2791 transposed_nb[0] = sizeof(float); 2792 for (int i = 1; i < GGML_MAX_DIMS; i++) { 2793 transposed_nb[i] = transposed_nb[i - 1] * transposed_ne[i - 1]; 2794 } 2795 2796 std::swap(transposed_ne[0], transposed_ne[2]); 2797 std::swap(transposed_nb[0], transposed_nb[2]); 2798 2799 acl_position_tensor = 2800 ggml_cann_create_tensor(mrope_position_buffer, ggml_cann_type_mapping(src1->type), 2801 ggml_type_size(src1->type), transposed_ne, transposed_nb, GGML_MAX_DIMS); 2802 2803 } else { 2804 // auto bcast. 2805 acl_position_tensor = 2806 ggml_cann_create_tensor(src1->data, ggml_cann_type_mapping(src1->type), ggml_type_size(src1->type), 2807 position_ne, position_nb, GGML_MAX_DIMS); 2808 } 2809 2810 // Step4: multiply by the position 2811 int64_t theta_length = theta_scale_length * position_length; 2812 ggml_cann_pool_alloc theta_allocator(ctx.pool(), theta_length * sizeof(float)); 2813 void * theta_buffer = theta_allocator.get(); 2814 2815 acl_tensor_ptr acl_theta_tensor = 2816 ggml_cann_create_tensor(theta_buffer, ACL_FLOAT, sizeof(float), cache_ne, cache_nb, GGML_MAX_DIMS); 2817 aclnn_mul(ctx, acl_position_tensor.get(), acl_theta_scale_tensor.get(), acl_theta_tensor.get()); 2818 2819 // Step5: calculate sin cos. 2820 // init sin_repeat && cos_repeat, only to accelerate first layer on each device 2821 if (position_length > ctx.rope_cache.position_length) { 2822 ctx.rope_cache.position_length = position_length; 2823 if (ctx.rope_cache.sin_cache != nullptr) { 2824 ACL_CHECK(aclrtFree(ctx.rope_cache.sin_cache)); 2825 } 2826 if (ctx.rope_cache.cos_cache != nullptr) { 2827 ACL_CHECK(aclrtFree(ctx.rope_cache.cos_cache)); 2828 } 2829 int64_t repeat_theta_length = theta_scale_length * position_length * 2; 2830 ACL_CHECK( 2831 aclrtMalloc(&ctx.rope_cache.sin_cache, repeat_theta_length * sizeof(float), ACL_MEM_MALLOC_HUGE_FIRST)); 2832 ACL_CHECK( 2833 aclrtMalloc(&ctx.rope_cache.cos_cache, repeat_theta_length * sizeof(float), ACL_MEM_MALLOC_HUGE_FIRST)); 2834 } 2835 2836 // sin/cos 2837 ggml_cann_pool_alloc sin_allocator(ctx.pool(), theta_length * sizeof(float)); 2838 void * sin_buffer = sin_allocator.get(); 2839 acl_tensor_ptr acl_sin_tensor = 2840 ggml_cann_create_tensor(sin_buffer, ACL_FLOAT, sizeof(float), cache_ne, cache_nb, GGML_MAX_DIMS, ACL_FORMAT_ND); 2841 aclnn_sin(ctx, acl_theta_tensor.get(), acl_sin_tensor.get()); 2842 2843 ggml_cann_pool_alloc cos_allocator(ctx.pool(), theta_length * sizeof(float)); 2844 void * cos_buffer = cos_allocator.get(); 2845 acl_tensor_ptr acl_cos_tensor = 2846 ggml_cann_create_tensor(cos_buffer, ACL_FLOAT, sizeof(float), cache_ne, cache_nb, GGML_MAX_DIMS, ACL_FORMAT_ND); 2847 aclnn_cos(ctx, acl_theta_tensor.get(), acl_cos_tensor.get()); 2848 2849 if (ext_factor != 0) { 2850 attn_factor *= 1.0f + 0.1f * logf(1.0f / freq_scale); 2851 } 2852 2853 // Step 5: multiply by attn_factor 2854 if (attn_factor != 1) { 2855 aclnn_muls(ctx, acl_sin_tensor.get(), attn_factor, nullptr, true); 2856 aclnn_muls(ctx, acl_cos_tensor.get(), attn_factor, nullptr, true); 2857 } 2858 2859 int64_t sin_reshape_ne[4] = { rope_dims, 1, dst->ne[2], 1 }; 2860 size_t sin_reshape_nb[GGML_MAX_DIMS]; 2861 sin_reshape_nb[0] = sizeof(float); 2862 for (int i = 1; i < GGML_MAX_DIMS; i++) { 2863 sin_reshape_nb[i] = sin_reshape_nb[i - 1] * sin_reshape_ne[i - 1]; 2864 } 2865 acl_tensor_ptr acl_sin_repeat_tensor = ggml_cann_create_tensor(ctx.rope_cache.sin_cache, ACL_FLOAT, sizeof(float), 2866 sin_reshape_ne, sin_reshape_nb, GGML_MAX_DIMS); 2867 acl_tensor_ptr acl_cos_repeat_tensor = ggml_cann_create_tensor(ctx.rope_cache.cos_cache, ACL_FLOAT, sizeof(float), 2868 sin_reshape_ne, sin_reshape_nb, GGML_MAX_DIMS); 2869 2870 // Step 6: repeat 2871 if (is_neox) { 2872 // [sinθ1, sinθ1, sinθ2, sinθ2, ..., sinθn, sinθn] 2873 int64_t repeatsArray[] = { 1, 1, 1, 2 }; 2874 aclnn_repeat(ctx, acl_sin_tensor.get(), acl_sin_repeat_tensor.get(), repeatsArray); 2875 aclnn_repeat(ctx, acl_cos_tensor.get(), acl_cos_repeat_tensor.get(), repeatsArray); 2876 } else { 2877 int64_t num_repeats = 2; 2878 int64_t dim = 3; 2879 int64_t output_size = theta_scale_length * num_repeats; 2880 // [sinθ1, sinθ2, ..., sinθn, sinθ1, sinθ2, ..., sinθn] 2881 aclnn_repeat_interleave(ctx, acl_sin_tensor.get(), acl_sin_repeat_tensor.get(), dim, num_repeats, output_size); 2882 aclnn_repeat_interleave(ctx, acl_cos_tensor.get(), acl_cos_repeat_tensor.get(), dim, num_repeats, output_size); 2883 } 2884 2885 // Update cached value. 2886 ctx.rope_cache.cached = true; 2887 ctx.rope_cache.set(theta_scale_length, position_length, ext_factor, theta_scale, freq_scale, attn_factor, is_neox, 2888 indep_sects, mrope_used, is_imrope, sections); 2889 } 2890 2891 #ifdef __cplusplus 2892 extern "C" { 2893 #endif 2894 aclnnStatus aclnnRotaryPositionEmbeddingGetWorkspaceSize(const aclTensor * x, 2895 const aclTensor * cos, 2896 const aclTensor * sin, 2897 int64_t mode, 2898 const aclTensor * yOut, 2899 uint64_t * workspaceSize, 2900 aclOpExecutor ** executor); 2901 aclnnStatus aclnnRotaryPositionEmbedding(void * workspace, 2902 uint64_t workspaceSize, 2903 aclOpExecutor * executor, 2904 aclrtStream stream); 2905 #ifdef __cplusplus 2906 } 2907 #endif 2908 2909 void ggml_cann_rope(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 2910 ggml_tensor * src0 = dst->src[0]; // input 2911 2912 // param 2913 float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow; 2914 int sections[4]; 2915 // const int n_past = ((int32_t *) dst->op_params)[0]; 2916 const int n_dims = ((int32_t *) dst->op_params)[1]; 2917 const int mode = ((int32_t *) dst->op_params)[2]; 2918 // const int n_ctx = ((int32_t *) dst->op_params)[3]; 2919 const int n_ctx_orig = ((int32_t *) dst->op_params)[4]; 2920 2921 GGML_TENSOR_UNARY_OP_LOCALS 2922 2923 memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float)); 2924 memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float)); 2925 memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float)); 2926 memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float)); 2927 memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float)); 2928 memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float)); 2929 memcpy(§ions, (int32_t *) dst->op_params + 11, sizeof(int) * 4); 2930 2931 GGML_ASSERT(n_dims % 2 == 0); 2932 GGML_ASSERT(n_dims <= ne00); 2933 2934 const float theta_scale = powf(freq_base, -2.0f / n_dims); 2935 2936 float corr_dims[2]; 2937 ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims); 2938 2939 bool is_neox = mode & GGML_ROPE_TYPE_NEOX; 2940 const bool is_imrope = mode == GGML_ROPE_TYPE_IMROPE; // qwen3vl apply interleaved mrope 2941 // mrope_used means the GGML_ROPE_TYPE_MROPE bit is set. 2942 // Note: this bit is also set for imrope and some vision modes, 2943 // so mrope_used does NOT exclusively indicate pure mrope. 2944 const bool mrope_used = mode & GGML_ROPE_TYPE_MROPE; 2945 const bool is_vision = mode == GGML_ROPE_TYPE_VISION; 2946 2947 if (mrope_used) { 2948 GGML_ASSERT(sections[0] > 0 || sections[1] > 0 || sections[2] > 0); 2949 } 2950 2951 if (is_vision) { 2952 GGML_ASSERT(n_dims == ne0 / 2); 2953 } 2954 2955 if (is_imrope || mrope_used) { 2956 is_neox = true; 2957 } 2958 2959 int64_t rope_dims = n_dims; 2960 2961 //Our current RotaryPositionEmbedding does not support the VISION mode, 2962 //but essentially it only modifies theta_base in mrope, 2963 //then repeats it at the end in the same way as is_neox. 2964 //In fact, RoPE is still applied across all dimensions. 2965 if (is_vision) { 2966 rope_dims = src0->ne[0]; 2967 } 2968 int64_t tail_dims = ne00 - rope_dims; 2969 bool has_tail = tail_dims > 0; 2970 2971 // init ctx.rope_cos/rope_sin cache 2972 aclnn_rope_cache_init(ctx, dst, corr_dims, ext_factor, theta_scale, freq_scale, attn_factor, is_neox, sections, 2973 mrope_used, is_imrope, is_vision, rope_dims); 2974 2975 // Cache is generated with ne00 dimensions, so we use ne00 for reshape 2976 int64_t sin_reshape_ne[4] = { rope_dims, 1, ne02, 1 }; 2977 size_t sin_reshape_nb[GGML_MAX_DIMS]; 2978 sin_reshape_nb[0] = sizeof(float); 2979 for (int i = 1; i < GGML_MAX_DIMS; i++) { 2980 sin_reshape_nb[i] = sin_reshape_nb[i - 1] * sin_reshape_ne[i - 1]; 2981 } 2982 acl_tensor_ptr acl_sin_reshape_tensor = ggml_cann_create_tensor(ctx.rope_cache.sin_cache, ACL_FLOAT, sizeof(float), 2983 sin_reshape_ne, sin_reshape_nb, GGML_MAX_DIMS); 2984 acl_tensor_ptr acl_cos_reshape_tensor = ggml_cann_create_tensor(ctx.rope_cache.cos_cache, ACL_FLOAT, sizeof(float), 2985 sin_reshape_ne, sin_reshape_nb, GGML_MAX_DIMS); 2986 2987 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src0); 2988 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 2989 #ifdef ASCEND_310P 2990 // Special ROPE operation for 310P 2991 2992 // roll input 2993 void * input_roll_buffer; 2994 acl_tensor_ptr acl_minus_one_tensor; 2995 void * minus_one_scale_buffer = nullptr; 2996 ggml_cann_pool_alloc roll_allocator(ctx.pool(), ggml_nbytes(src0)); 2997 ggml_cann_pool_alloc minus_one_scale_allocator(ctx.pool(), sizeof(float) * src0->ne[0]); 2998 if (!is_neox) { 2999 // roll input: [q0,q1,q2,q3,...] -> [q1,q0,q3,q2,...] 3000 input_roll_buffer = roll_allocator.get(); 3001 int64_t input_roll_ne[4] = { 2, src0->ne[1] * (src0->ne[0] / 2), src0->ne[2], src0->ne[3] }; 3002 size_t input_roll_nb[GGML_MAX_DIMS]; 3003 input_roll_nb[0] = ggml_type_size(src0->type); 3004 for (int i = 1; i < GGML_MAX_DIMS; i++) { 3005 input_roll_nb[i] = input_roll_nb[i - 1] * input_roll_ne[i - 1]; 3006 } 3007 acl_tensor_ptr acl_input_roll_tensor = 3008 ggml_cann_create_tensor(input_roll_buffer, ggml_cann_type_mapping(src0->type), ggml_type_size(src0->type), 3009 input_roll_ne, input_roll_nb, GGML_MAX_DIMS); 3010 acl_tensor_ptr acl_input_tensor = 3011 ggml_cann_create_tensor(src0->data, ggml_cann_type_mapping(src0->type), ggml_type_size(src0->type), 3012 input_roll_ne, input_roll_nb, GGML_MAX_DIMS); 3013 3014 int64_t shifts[] = { 1 }; 3015 int64_t dims[] = { 3 }; 3016 aclnn_roll(ctx, acl_input_tensor.get(), acl_input_roll_tensor.get(), shifts, dims); 3017 3018 // init [-1, 1, -1, 1, ...] 3019 minus_one_scale_buffer = minus_one_scale_allocator.get(); 3020 3021 int64_t minus_one_ne[4] = { src0->ne[0], 1, 1, 1 }; 3022 size_t minus_one_nb[GGML_MAX_DIMS]; 3023 minus_one_nb[0] = sizeof(float); 3024 for (int i = 1; i < GGML_MAX_DIMS; i++) { 3025 minus_one_nb[i] = minus_one_nb[i - 1] * minus_one_ne[i - 1]; 3026 } 3027 acl_minus_one_tensor = aclnn_values(ctx, minus_one_scale_buffer, sizeof(float) * src0->ne[0], minus_one_ne, 3028 GGML_MAX_DIMS, ACL_FLOAT, sizeof(float), 1); 3029 int64_t dim = 3; 3030 int64_t * index = new int64_t[src0->ne[0]]; 3031 for (int i = 0; i < src0->ne[0]; i++) { 3032 index[i] = i / 2 * 2; 3033 } 3034 int64_t index_num = src0->ne[0]; 3035 float value = -1; 3036 aclnn_index_fill_tensor(ctx, acl_minus_one_tensor.get(), dim, index, index_num, value); 3037 } else { 3038 // roll input: [q0,q1,q2,...] -> 3039 // [q_half,q_half+1,...,q_end,q0,q1,...q_half-1] 3040 input_roll_buffer = roll_allocator.get(); 3041 acl_tensor_ptr acl_input_roll_tensor = 3042 ggml_cann_create_tensor(input_roll_buffer, ggml_cann_type_mapping(src0->type), ggml_type_size(src0->type), 3043 src0->ne, src0->nb, GGML_MAX_DIMS); 3044 acl_tensor_ptr acl_input_tensor = ggml_cann_create_tensor(src0); 3045 3046 int64_t shifts[] = { src0->ne[0] / 2 }; 3047 int64_t dims[] = { 3 }; 3048 aclnn_roll(ctx, acl_input_tensor.get(), acl_input_roll_tensor.get(), shifts, dims); 3049 3050 // init [-1, -1, -1, 1, 1,1,...] 3051 minus_one_scale_buffer = minus_one_scale_allocator.get(); 3052 int64_t minus_one_ne[4] = { src0->ne[0], 1, 1, 1 }; 3053 size_t minus_one_nb[GGML_MAX_DIMS]; 3054 minus_one_nb[0] = sizeof(float); 3055 for (int i = 1; i < GGML_MAX_DIMS; i++) { 3056 minus_one_nb[i] = minus_one_nb[i - 1] * minus_one_ne[i - 1]; 3057 } 3058 acl_minus_one_tensor = aclnn_values(ctx, minus_one_scale_buffer, sizeof(float) * src0->ne[0], minus_one_ne, 3059 GGML_MAX_DIMS, ACL_FLOAT, sizeof(float), 1); 3060 // -1 * first half 3061 int64_t first_half_ne[4] = { src0->ne[0] / 2, 1, 1, 1 }; 3062 size_t first_half_nb[GGML_MAX_DIMS]; 3063 first_half_nb[0] = sizeof(float); 3064 for (int i = 1; i < GGML_MAX_DIMS; i++) { 3065 first_half_nb[i] = first_half_nb[i - 1] * first_half_ne[i - 1]; 3066 } 3067 acl_tensor_ptr acl_first_half_tensor = ggml_cann_create_tensor(minus_one_scale_buffer, ACL_FLOAT, sizeof(float), 3068 first_half_ne, first_half_nb, GGML_MAX_DIMS); 3069 bool inplace = true; 3070 float scale = -1; 3071 aclnn_muls(ctx, acl_first_half_tensor.get(), scale, nullptr, inplace); 3072 } 3073 3074 // TODO: n_dims < ne0 3075 GGML_ASSERT(n_dims == src0->ne[0]); 3076 3077 // input * scale 3078 ggml_cann_pool_alloc roll_mul_scale_allocator(ctx.pool(), ggml_nbytes(src0)); 3079 void * input_roll_mul_scale_buffer = roll_mul_scale_allocator.get(); 3080 size_t input_nb[GGML_MAX_DIMS]; 3081 input_nb[0] = ggml_type_size(src0->type); 3082 for (int i = 1; i < GGML_MAX_DIMS; i++) { 3083 input_nb[i] = input_nb[i - 1] * src0->ne[i - 1]; 3084 } 3085 acl_tensor_ptr acl_input_roll_mul_scale_tensor = 3086 ggml_cann_create_tensor(input_roll_mul_scale_buffer, ggml_cann_type_mapping(src0->type), 3087 ggml_type_size(src0->type), src0->ne, input_nb, GGML_MAX_DIMS); 3088 acl_tensor_ptr acl_input_roll_reshape_tensor = 3089 ggml_cann_create_tensor(input_roll_buffer, ggml_cann_type_mapping(src0->type), ggml_type_size(src0->type), 3090 src0->ne, input_nb, GGML_MAX_DIMS); 3091 3092 aclnn_mul(ctx, acl_input_roll_reshape_tensor.get(), acl_minus_one_tensor.get(), 3093 acl_input_roll_mul_scale_tensor.get()); 3094 3095 // output 3096 void * output_fp32_buffer; 3097 if (src0->type == GGML_TYPE_F32) { 3098 aclnn_mul(ctx, acl_src.get(), acl_cos_reshape_tensor.get()); 3099 aclnn_mul(ctx, acl_input_roll_mul_scale_tensor.get(), acl_sin_reshape_tensor.get()); 3100 aclnn_add(ctx, acl_src.get(), acl_input_roll_mul_scale_tensor.get(), acl_dst.get()); 3101 // TODO: ne0 != n_dims in mode2 3102 } else if (src0->type == GGML_TYPE_F16) { 3103 size_t input_fp32_nb[GGML_MAX_DIMS]; 3104 input_fp32_nb[0] = sizeof(float); 3105 for (int i = 1; i < GGML_MAX_DIMS; i++) { 3106 input_fp32_nb[i] = input_fp32_nb[i - 1] * dst->ne[i - 1]; 3107 } 3108 ggml_cann_pool_alloc fp32_allocator1(ctx.pool(), ggml_nelements(dst) * sizeof(float)); 3109 void * input_fp32_buffer1 = fp32_allocator1.get(); 3110 acl_tensor_ptr input_fp32_tensor1 = ggml_cann_create_tensor(input_fp32_buffer1, ACL_FLOAT, sizeof(float), 3111 dst->ne, input_fp32_nb, GGML_MAX_DIMS); 3112 ggml_cann_pool_alloc fp32_allocator2(ctx.pool(), ggml_nelements(dst) * sizeof(float)); 3113 void * input_fp32_buffer2 = fp32_allocator2.get(); 3114 acl_tensor_ptr input_fp32_tensor2 = ggml_cann_create_tensor(input_fp32_buffer2, ACL_FLOAT, sizeof(float), 3115 dst->ne, input_fp32_nb, GGML_MAX_DIMS); 3116 3117 ggml_cann_pool_alloc fp32_allocator(ctx.pool(), ggml_nelements(dst) * sizeof(float)); 3118 output_fp32_buffer = fp32_allocator.get(); 3119 acl_tensor_ptr output_fp32_tensor = ggml_cann_create_tensor(output_fp32_buffer, ACL_FLOAT, sizeof(float), 3120 dst->ne, input_fp32_nb, GGML_MAX_DIMS); 3121 aclnn_mul(ctx, acl_src.get(), acl_cos_reshape_tensor.get(), input_fp32_tensor1.get()); 3122 aclnn_mul(ctx, acl_input_roll_mul_scale_tensor.get(), acl_sin_reshape_tensor.get(), input_fp32_tensor2.get()); 3123 aclnn_add(ctx, input_fp32_tensor1.get(), input_fp32_tensor2.get(), output_fp32_tensor.get()); 3124 aclnn_cast(ctx, output_fp32_tensor.get(), acl_dst.get(), ACL_FLOAT16); 3125 } 3126 return; 3127 #endif 3128 int64_t acl_mode = is_neox ? 0 : 1; 3129 3130 // Pre-define head and tail dimensions for reuse 3131 int64_t head_ne[GGML_MAX_DIMS] = { rope_dims, ne01, ne02, ne03 }; 3132 int64_t tail_ne[GGML_MAX_DIMS] = { tail_dims, ne01, ne02, ne03 }; 3133 3134 // Step 1: Prepare trans tensors for F16 type conversion to F32 if needed 3135 bool src_dst_need_trans = false; 3136 ggml_cann_pool_alloc src_trans_allocator(ctx.pool()); 3137 ggml_cann_pool_alloc dst_trans_allocator(ctx.pool()); 3138 acl_tensor_ptr acl_src_trans_tensor; 3139 acl_tensor_ptr acl_dst_trans_tensor; 3140 void * src_trans_buffer = nullptr; 3141 void * dst_trans_buffer = nullptr; 3142 size_t src_dst_trans_nb[GGML_MAX_DIMS]; 3143 if (src0->type == GGML_TYPE_F16) { 3144 src_dst_need_trans = true; 3145 src_trans_buffer = src_trans_allocator.alloc(ggml_nelements(src0) * sizeof(float)); 3146 dst_trans_buffer = dst_trans_allocator.alloc(ggml_nelements(dst) * sizeof(float)); 3147 3148 src_dst_trans_nb[0] = sizeof(float); 3149 for (int i = 1; i < GGML_MAX_DIMS; i++) { 3150 src_dst_trans_nb[i] = src_dst_trans_nb[i - 1] * src0->ne[i - 1]; 3151 } 3152 acl_src_trans_tensor = ggml_cann_create_tensor(src_trans_buffer, ACL_FLOAT, sizeof(float), src0->ne, 3153 src_dst_trans_nb, GGML_MAX_DIMS); 3154 acl_dst_trans_tensor = ggml_cann_create_tensor(dst_trans_buffer, ACL_FLOAT, sizeof(float), dst->ne, 3155 src_dst_trans_nb, GGML_MAX_DIMS); 3156 aclnn_cast(ctx, acl_src.get(), acl_src_trans_tensor.get(), ACL_FLOAT); 3157 } 3158 3159 // Step 2: Prepare head tensors for tail splitting if needed 3160 acl_tensor_ptr acl_src_head; 3161 acl_tensor_ptr acl_dst_head; 3162 if (has_tail) { 3163 // Create head views for RotaryPositionEmbedding (only first rope_dims dimensions) 3164 // RotaryPositionEmbedding requires contiguous dst tensor, so we use a temporary buffer 3165 if (src_dst_need_trans) { 3166 // Use F32 trans tensor strides 3167 acl_src_head = ggml_cann_create_tensor((char *) src_trans_buffer, ACL_FLOAT, sizeof(float), head_ne, 3168 src_dst_trans_nb, GGML_MAX_DIMS); 3169 } else { 3170 // Use original F32 tensor strides 3171 acl_src_head = ggml_cann_create_tensor((char *) src0->data, ACL_FLOAT, sizeof(float), head_ne, src0->nb, 3172 GGML_MAX_DIMS); 3173 } 3174 3175 int64_t head_elements = rope_dims * ne01 * ne02 * ne03; 3176 ggml_cann_pool_alloc dst_head_contiguous_allocator(ctx.pool(), head_elements * sizeof(float)); 3177 void * dst_head_contiguous_buffer = dst_head_contiguous_allocator.get(); 3178 3179 size_t head_contiguous_nb[GGML_MAX_DIMS]; 3180 head_contiguous_nb[0] = sizeof(float); 3181 for (int i = 1; i < GGML_MAX_DIMS; i++) { 3182 head_contiguous_nb[i] = head_contiguous_nb[i - 1] * head_ne[i - 1]; 3183 } 3184 acl_dst_head = ggml_cann_create_tensor(dst_head_contiguous_buffer, ACL_FLOAT, sizeof(float), head_ne, 3185 head_contiguous_nb, GGML_MAX_DIMS); 3186 } 3187 3188 // Step 3: Execute RotaryPositionEmbedding 3189 if (has_tail) { 3190 // Rotate only the head portion (first rope_dims dimensions) 3191 GGML_CANN_CALL_ACLNN_OP(ctx, RotaryPositionEmbedding, acl_src_head.get(), acl_cos_reshape_tensor.get(), 3192 acl_sin_reshape_tensor.get(), acl_mode, acl_dst_head.get()); 3193 3194 // Copy head result from contiguous buffer back to destination tensor 3195 if (src_dst_need_trans) { 3196 acl_tensor_ptr acl_dst_head_target = ggml_cann_create_tensor( 3197 (char *) dst_trans_buffer, ACL_FLOAT, sizeof(float), head_ne, src_dst_trans_nb, GGML_MAX_DIMS); 3198 cann_copy(ctx, acl_dst_head.get(), acl_dst_head_target.get()); 3199 } else { 3200 acl_tensor_ptr acl_dst_head_target = 3201 ggml_cann_create_tensor((char *) dst->data, ACL_FLOAT, sizeof(float), head_ne, dst->nb, GGML_MAX_DIMS); 3202 cann_copy(ctx, acl_dst_head.get(), acl_dst_head_target.get()); 3203 } 3204 } else if (src_dst_need_trans) { 3205 // Rotate full tensor (no tail), using trans tensors 3206 GGML_CANN_CALL_ACLNN_OP(ctx, RotaryPositionEmbedding, acl_src_trans_tensor.get(), acl_cos_reshape_tensor.get(), 3207 acl_sin_reshape_tensor.get(), acl_mode, acl_dst_trans_tensor.get()); 3208 } else if (src0->data == dst->data && !ggml_is_contiguous(src0)) { 3209 // In-place on non-contiguous tensor: RotaryPositionEmbedding cannot safely 3210 // read and write the same non-contiguous buffer. Use contiguous temporaries. 3211 size_t contiguous_nb[GGML_MAX_DIMS]; 3212 contiguous_nb[0] = sizeof(float); 3213 for (int i = 1; i < GGML_MAX_DIMS; i++) { 3214 contiguous_nb[i] = contiguous_nb[i - 1] * src0->ne[i - 1]; 3215 } 3216 int64_t total_elements = ggml_nelements(src0); 3217 ggml_cann_pool_alloc inplace_src_alloc(ctx.pool(), total_elements * sizeof(float)); 3218 ggml_cann_pool_alloc inplace_dst_alloc(ctx.pool(), total_elements * sizeof(float)); 3219 3220 acl_tensor_ptr acl_src_contig = ggml_cann_create_tensor(inplace_src_alloc.get(), ACL_FLOAT, sizeof(float), 3221 src0->ne, contiguous_nb, GGML_MAX_DIMS); 3222 acl_tensor_ptr acl_dst_contig = ggml_cann_create_tensor(inplace_dst_alloc.get(), ACL_FLOAT, sizeof(float), 3223 dst->ne, contiguous_nb, GGML_MAX_DIMS); 3224 3225 cann_copy(ctx, acl_src.get(), acl_src_contig.get()); 3226 GGML_CANN_CALL_ACLNN_OP(ctx, RotaryPositionEmbedding, acl_src_contig.get(), acl_cos_reshape_tensor.get(), 3227 acl_sin_reshape_tensor.get(), acl_mode, acl_dst_contig.get()); 3228 cann_copy(ctx, acl_dst_contig.get(), acl_dst.get()); 3229 } else { 3230 // Rotate full tensor (no tail), using original tensors 3231 GGML_CANN_CALL_ACLNN_OP(ctx, RotaryPositionEmbedding, acl_src.get(), acl_cos_reshape_tensor.get(), 3232 acl_sin_reshape_tensor.get(), acl_mode, acl_dst.get()); 3233 } 3234 3235 // Step 4: Copy unrotated tail portion from source to destination 3236 if (has_tail) { 3237 size_t src_tail_offset; 3238 size_t dst_tail_offset; 3239 3240 auto copy_tail_device = [&](void * src_ptr, void * dst_ptr, aclDataType dtype, size_t elem_size, 3241 size_t * nb_src_arr, size_t * nb_dst_arr) { 3242 acl_tensor_ptr acl_src_tail = 3243 ggml_cann_create_tensor(src_ptr, dtype, elem_size, tail_ne, nb_src_arr, GGML_MAX_DIMS); 3244 acl_tensor_ptr acl_dst_tail = 3245 ggml_cann_create_tensor(dst_ptr, dtype, elem_size, tail_ne, nb_dst_arr, GGML_MAX_DIMS); 3246 cann_copy(ctx, acl_src_tail.get(), acl_dst_tail.get()); 3247 }; 3248 3249 if (src_dst_need_trans) { 3250 // Use F32 trans tensor strides and offsets 3251 src_tail_offset = rope_dims * src_dst_trans_nb[0]; 3252 dst_tail_offset = rope_dims * src_dst_trans_nb[0]; 3253 copy_tail_device((char *) src_trans_buffer + src_tail_offset, (char *) dst_trans_buffer + dst_tail_offset, 3254 ACL_FLOAT, sizeof(float), src_dst_trans_nb, src_dst_trans_nb); 3255 } else { 3256 // Use original tensor strides and offsets 3257 src_tail_offset = rope_dims * nb00; 3258 dst_tail_offset = rope_dims * nb0; 3259 copy_tail_device((char *) src0->data + src_tail_offset, (char *) dst->data + dst_tail_offset, 3260 ggml_cann_type_mapping(dst->type), ggml_element_size(dst), src0->nb, dst->nb); 3261 } 3262 } 3263 3264 // Step 5: Cast back to F16 if needed 3265 if (src_dst_need_trans) { 3266 aclnn_cast(ctx, acl_dst_trans_tensor.get(), acl_dst.get(), ACL_FLOAT16); 3267 } 3268 } 3269 3270 void ggml_cann_rope_cache_preload(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 3271 ggml_tensor * src0 = dst->src[0]; 3272 3273 float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow; 3274 int sections[4]; 3275 const int n_dims = ((int32_t *) dst->op_params)[1]; 3276 const int mode = ((int32_t *) dst->op_params)[2]; 3277 const int n_ctx_orig = ((int32_t *) dst->op_params)[4]; 3278 3279 GGML_TENSOR_UNARY_OP_LOCALS 3280 3281 memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float)); 3282 memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float)); 3283 memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float)); 3284 memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float)); 3285 memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float)); 3286 memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float)); 3287 memcpy(§ions, (int32_t *) dst->op_params + 11, sizeof(int) * 4); 3288 3289 const float theta_scale = powf(freq_base, -2.0f / n_dims); 3290 3291 float corr_dims[2]; 3292 ggml_rope_yarn_corr_dims(n_dims, n_ctx_orig, freq_base, beta_fast, beta_slow, corr_dims); 3293 3294 bool is_neox = mode & GGML_ROPE_TYPE_NEOX; 3295 const bool is_imrope = mode == GGML_ROPE_TYPE_IMROPE; 3296 const bool mrope_used = mode & GGML_ROPE_TYPE_MROPE; 3297 const bool is_vision = mode == GGML_ROPE_TYPE_VISION; 3298 3299 if (is_imrope || mrope_used) { 3300 is_neox = true; 3301 } 3302 3303 int64_t rope_dims = n_dims; 3304 if (is_vision) { 3305 rope_dims = src0->ne[0]; 3306 } 3307 3308 // Run the full cache init on the non-captured stream. This performs all 3309 // host-to-device memcpy, aclrtMalloc/Free, and on-device computations 3310 // so that the memory pool is warmed up and cache metadata is populated. 3311 aclnn_rope_cache_init(ctx, dst, corr_dims, ext_factor, theta_scale, freq_scale, attn_factor, is_neox, sections, 3312 mrope_used, is_imrope, is_vision, rope_dims); 3313 3314 // Reset `cached` so that during graph capture the on-device computations 3315 // (sin/cos, position multiply, repeat, etc.) still execute and get recorded 3316 // into the captured graph. The cache metadata (theta_scale_length, 3317 // theta_scale, sections, position_length, etc.) remains set, which causes 3318 // all host-to-device copy and malloc/free branches to be skipped. 3319 ctx.rope_cache.cached = false; 3320 } 3321 3322 void ggml_cann_argmax(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 3323 ggml_tensor * src0 = dst->src[0]; 3324 3325 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src0); 3326 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst, dst->ne, dst->nb, 3); 3327 3328 GGML_CANN_CALL_ACLNN_OP(ctx, ArgMax, acl_src.get(), 3, false, acl_dst.get()); 3329 } 3330 3331 void ggml_cann_conv_transpose_1d(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 3332 ggml_tensor * src0 = dst->src[0]; 3333 ggml_tensor * src1 = dst->src[1]; 3334 3335 // stride 3336 int64_t s0 = ((const int32_t *) (dst->op_params))[0]; 3337 3338 acl_tensor_ptr acl_input = ggml_cann_create_tensor(src1, src1->ne, src1->nb, 3, ACL_FORMAT_NCL); 3339 acl_tensor_ptr acl_weight = ggml_cann_create_tensor(src0, src0->ne, src0->nb, 3, ACL_FORMAT_NCL); 3340 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst, dst->ne, dst->nb, 3, ACL_FORMAT_NCL); 3341 3342 // get base information of input and kernel 3343 int64_t input_len = *(src1->ne); 3344 int64_t dst_len = *(dst->ne); 3345 int64_t kernel_size = *(src0->ne); 3346 3347 // set the max kernel size for each conv 3348 int64_t max_kernel_size = 255; 3349 3350 // compute the partition of kernel 3351 int64_t part_num = 1; 3352 part_num = (kernel_size + max_kernel_size - 1) / max_kernel_size; 3353 3354 int64_t strideVal[1]; 3355 strideVal[0] = s0; 3356 acl_int_array_ptr stride = ggml_cann_create_int_array(strideVal, 1); 3357 int64_t paddingVal[] = { 0 }; 3358 acl_int_array_ptr padding = ggml_cann_create_int_array(paddingVal, 1); 3359 int64_t dilationVal[] = { 1 }; 3360 acl_int_array_ptr dilation = ggml_cann_create_int_array(dilationVal, 1); 3361 bool transposed = true; 3362 int64_t groups = 1; 3363 int8_t cubeMathType = 0; 3364 3365 #ifdef ASCEND_310P 3366 cubeMathType = 1; 3367 #endif 3368 3369 auto weight_type = ggml_cann_type_mapping(src0->type); 3370 auto dst_type = ggml_cann_type_mapping(dst->type); 3371 3372 // slice the kernel to make each conv available 3373 int64_t slice_dim = -1; 3374 int64_t slice_start = 0; 3375 int64_t slice_end = max_kernel_size; 3376 int64_t slice_step = 1; 3377 int64_t interval = max_kernel_size; 3378 3379 int64_t left_pad_len = dilationVal[0] * (max_kernel_size - 1) + 1 - 2 * paddingVal[0]; 3380 int64_t right_pad_len = 0; 3381 3382 acl_scalar_ptr alpha = nullptr; 3383 float alphaValue = 1.0; 3384 alpha = ggml_cann_create_scalar(&alphaValue, aclDataType::ACL_FLOAT); 3385 3386 // set zero to destination 3387 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceZero, acl_dst.get()); 3388 3389 for (int k = 0; k < part_num; k++) { 3390 // create part kernel tensor and slice from big kernel 3391 slice_start = max_kernel_size * k; 3392 if (k == part_num - 1) { 3393 slice_end = kernel_size; 3394 interval = kernel_size - max_kernel_size * k; 3395 } else { 3396 slice_end = max_kernel_size * (k + 1); 3397 } 3398 3399 int64_t part_ne[4]; 3400 for (int i = 0; i < 4; i++) { 3401 part_ne[i] = *(src0->ne + i); 3402 } 3403 part_ne[0] = interval; 3404 3405 size_t part_nb[4]; 3406 part_nb[0] = sizeof(weight_type); 3407 for (int i = 1; i < 4; i++) { 3408 part_nb[i] = part_nb[i - 1] * part_ne[i - 1]; 3409 } 3410 3411 ggml_cann_pool_alloc part_kernel_allocator; 3412 part_kernel_allocator.alloc(ctx.pool(), part_nb[3]); 3413 void * part_kernel_buf = part_kernel_allocator.get(); 3414 3415 acl_tensor_ptr part_kernel = ggml_cann_create_tensor(part_kernel_buf, weight_type, ggml_element_size(src0), 3416 part_ne, part_nb, 3, ACL_FORMAT_NCL); 3417 3418 GGML_CANN_CALL_ACLNN_OP(ctx, Slice, acl_weight.get(), slice_dim, slice_start, slice_end, slice_step, 3419 part_kernel.get()); 3420 3421 // create the part conv result tensor 3422 int64_t part_dst_ne[4]; 3423 for (int i = 0; i < 4; i++) { 3424 part_dst_ne[i] = *(dst->ne + i); 3425 } 3426 part_dst_ne[0] = (input_len - 1) * strideVal[0] - 2 * paddingVal[0] + dilationVal[0] * (part_ne[0] - 1) + 1; 3427 3428 size_t part_dst_nb[4]; 3429 part_dst_nb[0] = sizeof(weight_type); 3430 for (int i = 1; i < 4; i++) { 3431 part_dst_nb[i] = part_dst_nb[i - 1] * part_dst_ne[i - 1]; 3432 } 3433 ggml_cann_pool_alloc part_dst_allocator; 3434 part_dst_allocator.alloc(ctx.pool(), part_dst_nb[3]); 3435 void * part_dst_buf = part_dst_allocator.get(); 3436 3437 acl_tensor_ptr acl_part_dst = ggml_cann_create_tensor(part_dst_buf, dst_type, ggml_element_size(dst), 3438 part_dst_ne, part_dst_nb, 3, ACL_FORMAT_NCL); 3439 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceZero, acl_part_dst.get()); 3440 3441 // compute part conv transpose 1d 3442 GGML_CANN_CALL_ACLNN_OP(ctx, Convolution, acl_input.get(), part_kernel.get(), nullptr, stride.get(), 3443 padding.get(), dilation.get(), transposed, padding.get(), groups, acl_part_dst.get(), 3444 cubeMathType); 3445 3446 // compute the position of part result in final result 3447 int64_t global_start = slice_start; 3448 int64_t global_end = std::min((input_len - 1) * strideVal[0] + slice_end, dst_len); 3449 3450 left_pad_len = global_start; 3451 right_pad_len = dst_len - global_end; 3452 3453 std::vector padDataVal = { left_pad_len, right_pad_len }; 3454 acl_int_array_ptr padData = ggml_cann_create_int_array(padDataVal.data(), 2); 3455 3456 acl_scalar_ptr pad_value = nullptr; 3457 float pad_valueVal = 0.0; 3458 pad_value = ggml_cann_create_scalar(&pad_valueVal, aclDataType::ACL_FLOAT); 3459 3460 int64_t conv_result_ne[4]; 3461 for (int i = 0; i < 4; i++) { 3462 conv_result_ne[i] = *(dst->ne + i); 3463 } 3464 3465 size_t conv_result_nb[4]; 3466 conv_result_nb[0] = sizeof(weight_type); 3467 for (int i = 1; i < 4; i++) { 3468 conv_result_nb[i] = conv_result_nb[i - 1] * conv_result_ne[i - 1]; 3469 } 3470 3471 ggml_cann_pool_alloc conv_result_allocator; 3472 conv_result_allocator.alloc(ctx.pool(), conv_result_nb[3]); 3473 void * conv_result_buf = conv_result_allocator.get(); 3474 3475 acl_tensor_ptr conv_result = ggml_cann_create_tensor(conv_result_buf, dst_type, ggml_element_size(dst), 3476 conv_result_ne, conv_result_nb, 3, ACL_FORMAT_NCL); 3477 3478 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceZero, conv_result.get()); 3479 GGML_CANN_CALL_ACLNN_OP(ctx, ConstantPadNd, acl_part_dst.get(), padData.get(), pad_value.get(), 3480 conv_result.get()); 3481 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceAdd, acl_dst.get(), conv_result.get(), alpha.get()); 3482 } 3483 } 3484 3485 void ggml_cann_elu(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 3486 ggml_tensor * src0 = dst->src[0]; 3487 3488 acl_tensor_ptr acl_input = ggml_cann_create_tensor(src0); 3489 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 3490 3491 float alphaValue = 1.0f; 3492 acl_scalar_ptr alpha = nullptr; 3493 alpha = ggml_cann_create_scalar(&alphaValue, aclDataType::ACL_FLOAT); 3494 3495 GGML_CANN_CALL_ACLNN_OP(ctx, Elu, acl_input.get(), alpha.get(), alpha.get(), alpha.get(), acl_dst.get()); 3496 } 3497 3498 void ggml_cann_mean(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 3499 ggml_tensor * src0 = dst->src[0]; 3500 3501 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src0); 3502 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 3503 3504 int64_t reduceDimValue[] = { 3 }; 3505 acl_int_array_ptr reduceDim = ggml_cann_create_int_array(reduceDimValue, 1); 3506 bool keepDim = true; 3507 3508 GGML_CANN_CALL_ACLNN_OP(ctx, Mean, acl_src.get(), reduceDim.get(), keepDim, ACL_FLOAT, acl_dst.get()); 3509 } 3510 3511 void ggml_cann_pad_reflect_1d(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 3512 ggml_tensor * src0 = dst->src[0]; 3513 int32_t * opts = (int32_t *) dst->op_params; 3514 int64_t paddingsArray[2] = { opts[0], opts[1] }; 3515 acl_int_array_ptr paddings = ggml_cann_create_int_array(paddingsArray, 2); 3516 3517 // Collapsing ne[2]*ne[3] into a single batch dimension requires that dim3 3518 // is contiguous with respect to dim2 in both src and dst. 3519 GGML_ASSERT(src0->nb[3] == src0->nb[2] * src0->ne[2]); 3520 GGML_ASSERT(dst->nb[3] == dst->nb[2] * dst->ne[2]); 3521 3522 int64_t src_ne_3d[3] = { src0->ne[0], src0->ne[1], src0->ne[2] * src0->ne[3] }; 3523 int64_t dst_ne_3d[3] = { dst->ne[0], dst->ne[1], dst->ne[2] * dst->ne[3] }; 3524 3525 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src0->data, ggml_cann_type_mapping(src0->type), 3526 ggml_element_size(src0), src_ne_3d, src0->nb, 3); 3527 3528 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst->data, ggml_cann_type_mapping(dst->type), 3529 ggml_element_size(dst), dst_ne_3d, dst->nb, 3); 3530 3531 GGML_CANN_CALL_ACLNN_OP(ctx, ReflectionPad1d, acl_src.get(), paddings.get(), acl_dst.get()); 3532 } 3533 3534 void ggml_cann_count_equal(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 3535 ggml_tensor * src0 = dst->src[0]; 3536 ggml_tensor * src1 = dst->src[1]; 3537 3538 // Write element-wise equality (0 or 1) into a temporary buffer to avoid 3539 // modifying src0 in-place. Use the same type as src0 so ReduceSum can 3540 // consume it directly without a type cast. 3541 ggml_cann_pool_alloc eq_alloc(ctx.pool(), ggml_nelements(src0) * ggml_element_size(src0)); 3542 size_t eq_nb[GGML_MAX_DIMS]; 3543 eq_nb[0] = ggml_element_size(src0); 3544 for (int i = 1; i < GGML_MAX_DIMS; i++) { 3545 eq_nb[i] = eq_nb[i - 1] * src0->ne[i - 1]; 3546 } 3547 acl_tensor_ptr acl_eq = ggml_cann_create_tensor( 3548 eq_alloc.get(), ggml_cann_type_mapping(src0->type), ggml_element_size(src0), 3549 src0->ne, eq_nb, GGML_MAX_DIMS); 3550 3551 acl_tensor_ptr acl_self = ggml_cann_create_tensor(src0); 3552 acl_tensor_ptr acl_other = ggml_cann_create_tensor(src1); 3553 GGML_CANN_CALL_ACLNN_OP(ctx, EqTensor, acl_self.get(), acl_other.get(), acl_eq.get()); 3554 3555 // Sum the 0/1 values into dst. 3556 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 3557 int64_t dims[4] = { 0, 1, 2, 3 }; 3558 acl_int_array_ptr dims_arr = ggml_cann_create_int_array(dims, 4); 3559 GGML_CANN_CALL_ACLNN_OP(ctx, ReduceSum, acl_eq.get(), dims_arr.get(), true, 3560 ggml_cann_type_mapping(dst->type), acl_dst.get()); 3561 } 3562 3563 void ggml_cann_step(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 3564 ggml_tensor * src0 = dst->src[0]; 3565 3566 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src0); 3567 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 3568 3569 float alphaValue = 0.0f; 3570 acl_scalar_ptr alpha = nullptr; 3571 alpha = ggml_cann_create_scalar(&alphaValue, aclDataType::ACL_FLOAT); 3572 3573 GGML_CANN_CALL_ACLNN_OP(ctx, GtScalar, acl_src.get(), alpha.get(), acl_dst.get()); 3574 } 3575 3576 void ggml_cann_softplus(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 3577 ggml_tensor * src0 = dst->src[0]; 3578 3579 acl_tensor_ptr acl_src = ggml_cann_create_tensor(src0); 3580 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst); 3581 3582 float beta_val = 1.0f; 3583 float threshold_val = 20.0f; 3584 acl_scalar_ptr beta = ggml_cann_create_scalar(&beta_val, ACL_FLOAT); 3585 acl_scalar_ptr threshold = ggml_cann_create_scalar(&threshold_val, ACL_FLOAT); 3586 3587 GGML_CANN_CALL_ACLNN_OP(ctx, Softplus, acl_src.get(), beta.get(), threshold.get(), acl_dst.get()); 3588 } 3589 3590 void ggml_cann_geglu_quick(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 3591 auto gelu_quick_fn = [](ggml_backend_cann_context & ctx, aclTensor * acl_src, aclTensor * acl_dst) { 3592 GGML_CANN_CALL_ACLNN_OP(ctx, GeluV2, acl_src, 0, acl_dst); 3593 }; 3594 ggml_cann_op_unary_gated(gelu_quick_fn, ctx, dst); 3595 } 3596 3597 /** 3598 * @brief Performs expert-specific matrix multiplication (MoE) with 3599 * floating-point precision using the CANN backend. 3600 * 3601 * This function executes a matrix multiplication operation tailored for 3602 * Mixture of Experts (MoE) models, where the input tensor is multiplied 3603 * with expert-specific weight matrices. It uses the CANN backend for 3604 * efficient computation and stores the result in the destination tensor `dst`. 3605 * The operation may leverage identity-based optimizations or routing masks 3606 * as part of sparse expert selection. 3607 * 3608 * @param ctx The context for executing CANN backend operations. 3609 * @param dst The destination tensor where the MoE multiplication result 3610 * will be stored. 3611 * 3612 * @note This function assumes floating-point data types and is designed for 3613 * MoE architectures, possibly involving sparse expert routing. 3614 */ 3615 static void ggml_cann_mul_mat_id_fp(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 3616 //dst [M, K, N, 1] 3617 ggml_tensor * src0 = dst->src[0]; //src0 [D, M, A, 1] -> [D, M, K, 1] 3618 ggml_tensor * src1 = dst->src[1]; //src1 [D, B, N, 1], B = K or B = 1 -> [D, 1, K, 1] 3619 ggml_tensor * ids = dst->src[2]; //ids [K, N] 3620 3621 GGML_ASSERT(src0->ne[3] == 1); 3622 GGML_ASSERT(src1->ne[3] == 1); 3623 GGML_ASSERT(dst->ne[3] == 1); 3624 3625 int64_t batch = src1->ne[2]; 3626 GGML_ASSERT(batch == ids->ne[1]); 3627 3628 ggml_cann_pool_alloc export_allocator(ctx.pool(), src0->ne[0] * src0->ne[1] * ids->ne[0] * ggml_element_size(src0)); 3629 void * export_ptr = export_allocator.get(); 3630 for (int64_t i = 0; i < batch; i++) { 3631 acl_tensor_ptr select_index = ggml_cann_create_tensor(ids, ids->ne, ids->nb, 1, ACL_FORMAT_ND, i * ids->nb[1]); 3632 acl_tensor_ptr export_weight = ggml_cann_create_tensor(src0, src0->ne, src0->nb, 3); 3633 3634 int64_t select_export_ne[] = { src0->ne[0], src0->ne[1], ids->ne[0] }; 3635 size_t select_export_nb[3]; 3636 select_export_nb[0] = src0->nb[0]; 3637 for (int k = 1; k < 3; k++) { 3638 select_export_nb[k] = select_export_nb[k - 1] * select_export_ne[k - 1]; 3639 } 3640 3641 acl_tensor_ptr select_export = 3642 ggml_cann_create_tensor(export_ptr, ggml_cann_type_mapping(src0->type), ggml_element_size(src0), 3643 select_export_ne, select_export_nb, 3); 3644 GGML_CANN_CALL_ACLNN_OP(ctx, IndexSelect, export_weight.get(), 0, select_index.get(), select_export.get()); 3645 3646 int64_t select_transpose_ne[] = { select_export_ne[1], select_export_ne[0], select_export_ne[2] }; 3647 size_t select_transpose_nb[] = { select_export_nb[1], select_export_nb[0], select_export_nb[2] }; 3648 acl_tensor_ptr select_export_transpose = 3649 ggml_cann_create_tensor(export_ptr, ggml_cann_type_mapping(src0->type), ggml_element_size(src0), 3650 select_transpose_ne, select_transpose_nb, 3); 3651 3652 int64_t active_tensor_ne[] = { src1->ne[0], 1, src1->ne[1] }; 3653 size_t active_tensor_nb[] = { src1->nb[0], src1->nb[1], src1->nb[1] }; 3654 acl_tensor_ptr active_tensor = 3655 ggml_cann_create_tensor(src1, active_tensor_ne, active_tensor_nb, 3, ACL_FORMAT_ND, i * src1->nb[2]); 3656 3657 int64_t dst_ne[] = { dst->ne[0], 1, dst->ne[1] }; 3658 size_t dst_nb[] = { dst->nb[0], dst->nb[1], dst->nb[1] }; 3659 acl_tensor_ptr acl_dst = ggml_cann_create_tensor(dst, dst_ne, dst_nb, 3, ACL_FORMAT_ND, i * dst->nb[2]); 3660 3661 GGML_CANN_CALL_ACLNN_OP(ctx, BatchMatMul, active_tensor.get(), select_export_transpose.get(), acl_dst.get(), 2); 3662 } 3663 } 3664 3665 /** 3666 * @brief Performs quantized matrix multiplication for Mixture of Experts (MoE) 3667 * models using the CANN backend. 3668 * 3669 * This function implements MUL_MAT_ID operation for quantized weight matrices 3670 * (Q4_0 and Q8_0 formats). It selects expert-specific weight matrices based on 3671 * the provided expert indices, and computes matrix multiplication using CANN's 3672 * WeightQuantBatchMatmulV2 operator. 3673 * 3674 * The function performs the following steps: 3675 * 1. Converts input/output tensors to F16 format if necessary 3676 * 2. Uses IndexSelect to extract expert-specific weights and scales based on indices 3677 * 3. Performs quantized matrix multiplication for each expert using WeightQuantBatchMatmulV2 3678 * 4. Converts output back to the target type if needed 3679 * 3680 * Tensor shapes: 3681 * - dst: [M, K, N, 1] - output tensor 3682 * - src0: [D, M, A, 1] - quantized weight matrices (Q4_0 or Q8_0) 3683 * - src1: [D, B, N, 1] - input activations (B = K for per-expert input, or B = 1 for broadcast) 3684 * - ids: [K, N] - expert indices for routing 3685 * 3686 * @param ctx The CANN backend context for operation execution. 3687 * @param dst The destination tensor where the multiplication result will be stored. 3688 * 3689 * @note Only Q4_0 and Q8_0 quantization formats are supported. 3690 * @note The function handles automatic type conversion to/from F16 as needed by the hardware. 3691 */ 3692 static void ggml_cann_mul_mat_id_quant(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 3693 // dst: [M, K, N, 1] 3694 // src0: [D, M, A, 1] - quantized weights 3695 // src1: [D, B, N, 1] - input activations, B = K or B = 1 3696 // ids: [K, N] - expert indices 3697 ggml_tensor * src0 = dst->src[0]; 3698 ggml_tensor * src1 = dst->src[1]; 3699 ggml_tensor * ids = dst->src[2]; 3700 3701 GGML_ASSERT(src0->ne[3] == 1); 3702 GGML_ASSERT(src1->ne[3] == 1); 3703 GGML_ASSERT(dst->ne[3] == 1); 3704 GGML_ASSERT(src1->ne[2] == ids->ne[1]); 3705 3706 const int64_t n_batches = ids->ne[1]; 3707 const int64_t n_select_experts = ids->ne[0]; 3708 const enum ggml_type type = src0->type; 3709 3710 const int32_t group_size = QK8_0; // Both Q4_0 and Q8_0 use group size of 32 3711 GGML_ASSERT(group_size == QK4_0); 3712 3713 // Calculate element size for quantized weights 3714 const float weight_elem_size = 3715 (type == GGML_TYPE_Q4_0) ? 0.5f : 3716 (type == GGML_TYPE_Q8_0) ? 1.0f : 3717 (GGML_ABORT("MUL_MAT_ID only supports Q4_0 and Q8_0"), 0.0f); 3718 3719 // Calculate scale offset in memory 3720 const size_t weight_size = src0->ne[0] * src0->ne[1] * src0->ne[2] * weight_elem_size; 3721 const size_t scale_elem_size = sizeof(uint16_t); 3722 char * scale_data = (char *) src0->data + weight_size; 3723 3724 // Allocate buffers for selected expert weights and scales 3725 const size_t selected_weight_size = src0->ne[0] * src0->ne[1] * n_select_experts * weight_elem_size; 3726 ggml_cann_pool_alloc selected_weight_alloc(ctx.pool(), selected_weight_size); 3727 void * selected_weight_buffer = selected_weight_alloc.get(); 3728 3729 const size_t selected_scale_size = (src0->ne[0] / group_size) * src0->ne[1] * n_select_experts * scale_elem_size; 3730 ggml_cann_pool_alloc selected_scale_alloc(ctx.pool(), selected_scale_size); 3731 void * selected_scale_buffer = selected_scale_alloc.get(); 3732 3733 // Helper lambda to allocate and cast tensor to F16 if needed 3734 constexpr size_t f16_elem_size = sizeof(uint16_t); 3735 auto prepare_f16_buffer = [&](ggml_tensor * tensor, ggml_cann_pool_alloc & allocator, 3736 bool need_cast = false) -> void * { 3737 if (tensor->type == GGML_TYPE_F16) { 3738 return tensor->data; 3739 } 3740 3741 size_t total_size = f16_elem_size; 3742 for (int i = 0; i < GGML_MAX_DIMS; i++) { 3743 total_size *= tensor->ne[i]; 3744 } 3745 void * buffer = allocator.alloc(total_size); 3746 3747 if (need_cast == false) { 3748 return buffer; 3749 } 3750 3751 int64_t ne[GGML_MAX_DIMS]; 3752 size_t nb[GGML_MAX_DIMS] = { f16_elem_size }; 3753 for (int i = 0; i < GGML_MAX_DIMS; i++) { 3754 ne[i] = tensor->ne[i]; 3755 if (i > 0) { 3756 nb[i] = nb[i - 1] * ne[i - 1]; 3757 } 3758 } 3759 3760 acl_tensor_ptr src_tensor = ggml_cann_create_tensor(tensor); 3761 acl_tensor_ptr f16_tensor = ggml_cann_create_tensor(buffer, ACL_FLOAT16, f16_elem_size, ne, nb, GGML_MAX_DIMS); 3762 aclnn_cast(ctx, src_tensor.get(), f16_tensor.get(), ACL_FLOAT16); 3763 3764 return buffer; 3765 }; 3766 3767 // Prepare input and output buffers 3768 ggml_cann_pool_alloc input_alloc(ctx.pool()); 3769 void * input_buffer = prepare_f16_buffer(src1, input_alloc, true); 3770 3771 ggml_cann_pool_alloc output_alloc(ctx.pool()); 3772 void * output_buffer = prepare_f16_buffer(dst, output_alloc, false); 3773 3774 // Process each batch 3775 for (int64_t batch_idx = 0; batch_idx < n_batches; batch_idx++) { 3776 // Create index tensor for current batch 3777 const size_t index_offset = batch_idx * ids->nb[1]; 3778 acl_tensor_ptr batch_indices = ggml_cann_create_tensor(ids, ids->ne, ids->nb, 1, ACL_FORMAT_ND, index_offset); 3779 3780 // Select quantized weights using expert indices 3781 // Q4_0 stores 2 values per byte, Q8_0 stores 1 value per byte 3782 const int64_t weight_d = (type == GGML_TYPE_Q4_0) ? src0->ne[0] / 2 : src0->ne[0]; 3783 const int64_t weight_m = src0->ne[1]; 3784 const int64_t weight_n_experts = src0->ne[2]; 3785 3786 int64_t weight_ne[3] = { weight_d, weight_m, weight_n_experts }; 3787 size_t weight_nb[3] = { sizeof(int8_t), weight_d * sizeof(int8_t), weight_d * weight_m * sizeof(int8_t) }; 3788 3789 acl_tensor_ptr all_weights = 3790 ggml_cann_create_tensor(src0->data, ACL_INT8, sizeof(int8_t), weight_ne, weight_nb, 3); 3791 3792 int64_t selected_weight_ne[3] = { weight_d, weight_m, n_select_experts }; 3793 size_t selected_weight_nb[3] = { sizeof(int8_t), weight_d * sizeof(int8_t), 3794 weight_d * weight_m * sizeof(int8_t) }; 3795 3796 acl_tensor_ptr selected_weights = ggml_cann_create_tensor(selected_weight_buffer, ACL_INT8, sizeof(int8_t), 3797 selected_weight_ne, selected_weight_nb, 3); 3798 3799 GGML_CANN_CALL_ACLNN_OP(ctx, IndexSelect, all_weights.get(), 0, batch_indices.get(), selected_weights.get()); 3800 3801 // Select scales using the same expert indices 3802 const int64_t scale_d = src0->ne[0] / group_size; 3803 int64_t scale_ne[3] = { scale_d, weight_m, weight_n_experts }; 3804 size_t scale_nb[3] = { scale_elem_size, scale_d * scale_elem_size, scale_d * weight_m * scale_elem_size }; 3805 3806 acl_tensor_ptr all_scales = 3807 ggml_cann_create_tensor(scale_data, ACL_FLOAT16, scale_elem_size, scale_ne, scale_nb, 3); 3808 3809 int64_t selected_scale_ne[3] = { scale_d, weight_m, n_select_experts }; 3810 size_t selected_scale_nb[3] = { scale_elem_size, scale_d * scale_elem_size, 3811 scale_d * weight_m * scale_elem_size }; 3812 3813 acl_tensor_ptr selected_scales = ggml_cann_create_tensor(selected_scale_buffer, ACL_FLOAT16, scale_elem_size, 3814 selected_scale_ne, selected_scale_nb, 3); 3815 3816 GGML_CANN_CALL_ACLNN_OP(ctx, IndexSelect, all_scales.get(), 0, batch_indices.get(), selected_scales.get()); 3817 3818 // Process each expert for current batch 3819 // IndexSelect output layout: [D, M, K] in contiguous format 3820 // WeightQuantBatchMatmulV2 expects: [M, D] with row-major stride 3821 for (int64_t expert_idx = 0; expert_idx < n_select_experts; expert_idx++) { 3822 // Determine input offset: broadcast if src1->ne[1]==1, otherwise use per-expert input 3823 const size_t input_offset = 3824 (batch_idx * src1->ne[1] + (src1->ne[1] == 1 ? 0 : expert_idx)) * src1->ne[0] * f16_elem_size; 3825 const size_t output_offset = (batch_idx * dst->ne[1] + expert_idx) * dst->ne[0] * f16_elem_size; 3826 3827 // Create weight view for current expert: [D, M, K] -> [M, D] 3828 int64_t weight_view_ne[2] = { weight_m, src0->ne[0] }; 3829 float weight_view_nb[2] = { src0->ne[0] * weight_elem_size, weight_elem_size }; 3830 const size_t weight_view_offset = expert_idx * selected_weight_nb[2]; 3831 3832 acl_tensor_ptr weight_view = 3833 ggml_cann_create_tensor(selected_weight_buffer, ggml_cann_type_mapping(type), weight_elem_size, 3834 weight_view_ne, weight_view_nb, 2, ACL_FORMAT_ND, weight_view_offset); 3835 3836 // Create scale view for current expert: [D, M, K] -> [M, D] 3837 int64_t scale_view_ne[2] = { weight_m, scale_d }; 3838 size_t scale_view_nb[2] = { selected_scale_nb[1], selected_scale_nb[0] }; 3839 const size_t scale_view_offset = expert_idx * selected_scale_nb[2]; 3840 3841 acl_tensor_ptr scale_view = 3842 ggml_cann_create_tensor(selected_scale_buffer, ACL_FLOAT16, scale_elem_size, scale_view_ne, 3843 scale_view_nb, 2, ACL_FORMAT_ND, scale_view_offset); 3844 3845 // Create input activation tensor [D, 1] 3846 int64_t input_ne[2] = { src1->ne[0], 1 }; 3847 size_t input_nb[2] = { f16_elem_size, src1->ne[0] * f16_elem_size }; 3848 3849 acl_tensor_ptr input_tensor = ggml_cann_create_tensor(input_buffer, ACL_FLOAT16, f16_elem_size, input_ne, 3850 input_nb, 2, ACL_FORMAT_ND, input_offset); 3851 3852 // Create output tensor [M, 1] 3853 int64_t output_ne[2] = { dst->ne[0], 1 }; 3854 size_t output_nb[2] = { f16_elem_size, dst->ne[0] * f16_elem_size }; 3855 3856 acl_tensor_ptr output_tensor = ggml_cann_create_tensor(output_buffer, ACL_FLOAT16, f16_elem_size, output_ne, 3857 output_nb, 2, ACL_FORMAT_ND, output_offset); 3858 3859 // Perform quantized matrix multiplication 3860 GGML_CANN_CALL_ACLNN_OP(ctx, WeightQuantBatchMatmulV2, input_tensor.get(), weight_view.get(), 3861 scale_view.get(), nullptr, nullptr, nullptr, nullptr, group_size, 3862 output_tensor.get()); 3863 } 3864 } 3865 3866 // Cast output back to original type if we used a temporary F16 buffer 3867 if (dst->type != GGML_TYPE_F16) { 3868 int64_t ne[GGML_MAX_DIMS]; 3869 size_t nb[GGML_MAX_DIMS] = { f16_elem_size }; 3870 for (int i = 0; i < GGML_MAX_DIMS; i++) { 3871 ne[i] = dst->ne[i]; 3872 if (i > 0) { 3873 nb[i] = nb[i - 1] * ne[i - 1]; 3874 } 3875 } 3876 3877 acl_tensor_ptr f16_output = 3878 ggml_cann_create_tensor(output_buffer, ACL_FLOAT16, f16_elem_size, ne, nb, GGML_MAX_DIMS); 3879 acl_tensor_ptr dst_tensor = ggml_cann_create_tensor(dst); 3880 3881 aclnn_cast(ctx, f16_output.get(), dst_tensor.get(), ggml_cann_type_mapping(dst->type)); 3882 } 3883 } 3884 3885 void ggml_cann_mul_mat_id(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 3886 const enum ggml_type type = dst->src[0]->type; 3887 switch (type) { 3888 case GGML_TYPE_F32: 3889 case GGML_TYPE_F16: 3890 ggml_cann_mul_mat_id_fp(ctx, dst); 3891 break; 3892 case GGML_TYPE_Q4_0: 3893 case GGML_TYPE_Q8_0: 3894 ggml_cann_mul_mat_id_quant(ctx, dst); 3895 break; 3896 default: 3897 GGML_ABORT("Unsupported type for mul_mat_id"); 3898 break; 3899 } 3900 } 3901 3902 void ggml_cann_flash_attn_ext(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 3903 ggml_tensor * src0 = dst->src[0]; // q, fp32 | B, N, S, D (uncont) -> B, S, N, D (cont) 3904 ggml_tensor * src1 = dst->src[1]; // k, fp16 | B, N, S, D (uncont) -> B, S, N, D (cont) 3905 ggml_tensor * src2 = dst->src[2]; // v, fp16 | B, N, S, D (uncont) -> B, S, N, D (cont) 3906 ggml_tensor * src3 = dst->src[3]; // mask, fp16 3907 3908 // B, N, S, D (uncont) -> B, S, N, D (cont) 3909 int64_t src0_bsnd_ne[GGML_MAX_DIMS]; 3910 memcpy(src0_bsnd_ne, src0->ne, GGML_MAX_DIMS * sizeof(int64_t)); 3911 size_t src0_bsnd_nb[GGML_MAX_DIMS]; 3912 memcpy(src0_bsnd_nb, src0->nb, GGML_MAX_DIMS * sizeof(size_t)); 3913 int64_t src1_bsnd_ne[GGML_MAX_DIMS]; 3914 memcpy(src1_bsnd_ne, src1->ne, GGML_MAX_DIMS * sizeof(int64_t)); 3915 size_t src1_bsnd_nb[GGML_MAX_DIMS]; 3916 memcpy(src1_bsnd_nb, src1->nb, GGML_MAX_DIMS * sizeof(size_t)); 3917 int64_t src2_bsnd_ne[GGML_MAX_DIMS]; 3918 memcpy(src2_bsnd_ne, src2->ne, GGML_MAX_DIMS * sizeof(int64_t)); 3919 size_t src2_bsnd_nb[GGML_MAX_DIMS]; 3920 memcpy(src2_bsnd_nb, src2->nb, GGML_MAX_DIMS * sizeof(size_t)); 3921 3922 auto transpose12 = [](int64_t * ne, size_t * nb) { 3923 int64_t ne_tmp = ne[1]; 3924 size_t nb_tmp = nb[1]; 3925 ne[1] = ne[2]; 3926 nb[1] = nb[2]; 3927 ne[2] = ne_tmp; 3928 nb[2] = nb_tmp; 3929 }; 3930 3931 transpose12(src0_bsnd_ne, src0_bsnd_nb); 3932 transpose12(src1_bsnd_ne, src1_bsnd_nb); 3933 transpose12(src2_bsnd_ne, src2_bsnd_nb); 3934 3935 float maxBias = 0.0f; 3936 float scaleValue = 1.0f; 3937 float logitSoftcap = 0.0f; 3938 memcpy(&scaleValue, (float *) dst->op_params + 0, sizeof(float)); 3939 memcpy(&maxBias, (float *) dst->op_params + 1, sizeof(float)); 3940 memcpy(&logitSoftcap, (float *) dst->op_params + 2, sizeof(float)); 3941 3942 if (logitSoftcap == 0.0f) { 3943 size_t faElemSize = sizeof(uint16_t); 3944 auto faDataType = ACL_FLOAT16; //ACL_BF16; 3945 3946 acl_tensor_ptr acl_q_tensor = nullptr; 3947 acl_tensor_ptr acl_k_tensor = nullptr; 3948 acl_tensor_ptr acl_v_tensor = nullptr; 3949 3950 // Step 1: cast the src0 (Query) to fp16 if needed 3951 ggml_cann_pool_alloc src0_f16_allocator(ctx.pool()); 3952 void * src0_f16_buffer = nullptr; 3953 3954 if (ggml_cann_type_mapping(src0->type) != faDataType) { 3955 acl_tensor_ptr acl_src0_f32_tensor = 3956 ggml_cann_create_tensor(src0, src0_bsnd_ne, src0_bsnd_nb, GGML_MAX_DIMS); 3957 src0_f16_buffer = src0_f16_allocator.alloc(ggml_nelements(src0) * faElemSize); 3958 3959 int64_t * src0_f16_ne = src0_bsnd_ne; 3960 size_t src0_f16_nb[GGML_MAX_DIMS]; 3961 src0_f16_nb[0] = sizeof(uint16_t); 3962 for (int i = 1; i < GGML_MAX_DIMS; ++i) { 3963 src0_f16_nb[i] = src0_f16_nb[i - 1] * src0_f16_ne[i - 1]; 3964 } 3965 3966 acl_q_tensor = ggml_cann_create_tensor(src0_f16_buffer, faDataType, faElemSize, src0_f16_ne, src0_f16_nb, 3967 GGML_MAX_DIMS); 3968 aclnn_cast(ctx, acl_src0_f32_tensor.get(), acl_q_tensor.get(), faDataType); 3969 } else { 3970 acl_q_tensor = ggml_cann_create_tensor(src0, src0_bsnd_ne, src0_bsnd_nb, GGML_MAX_DIMS); 3971 } 3972 3973 // Step 2: create the acl tensors for src1 (Key), src2 (Value), 3974 // and the direct output from FusedInferAttention 3975 3976 acl_k_tensor = ggml_cann_create_tensor(src1, src1_bsnd_ne, src1_bsnd_nb, GGML_MAX_DIMS); 3977 acl_v_tensor = ggml_cann_create_tensor(src2, src2_bsnd_ne, src2_bsnd_nb, GGML_MAX_DIMS); 3978 3979 // Step 2.5: Pad Q, K, V along head dimension if D is not a multiple of 16 3980 // (required by FusedInferAttentionScoreV2) 3981 const int64_t D = src0->ne[0]; 3982 const int64_t D_padded = GGML_PAD(D, 16); 3983 const bool needs_padding = (D != D_padded); 3984 3985 ggml_cann_pool_alloc q_pad_allocator(ctx.pool()); 3986 ggml_cann_pool_alloc k_pad_allocator(ctx.pool()); 3987 ggml_cann_pool_alloc v_pad_allocator(ctx.pool()); 3988 3989 if (needs_padding) { 3990 int64_t paddings[] = { 0, D_padded - D, 0, 0, 0, 0, 0, 0 }; 3991 3992 auto pad_fa_tensor = [&](acl_tensor_ptr & tensor, const int64_t * bsnd_ne, 3993 ggml_cann_pool_alloc & allocator) { 3994 int64_t pad_ne[GGML_MAX_DIMS] = { D_padded, bsnd_ne[1], bsnd_ne[2], bsnd_ne[3] }; 3995 size_t pad_nb[GGML_MAX_DIMS]; 3996 pad_nb[0] = faElemSize; 3997 for (int i = 1; i < GGML_MAX_DIMS; ++i) { 3998 pad_nb[i] = pad_nb[i - 1] * pad_ne[i - 1]; 3999 } 4000 int64_t nelements = pad_ne[0] * pad_ne[1] * pad_ne[2] * pad_ne[3]; 4001 void * buffer = allocator.alloc(nelements * faElemSize); 4002 acl_tensor_ptr padded = 4003 ggml_cann_create_tensor(buffer, faDataType, faElemSize, pad_ne, pad_nb, GGML_MAX_DIMS); 4004 aclnn_pad(ctx, tensor.get(), padded.get(), paddings); 4005 tensor = std::move(padded); 4006 }; 4007 4008 pad_fa_tensor(acl_q_tensor, src0_bsnd_ne, q_pad_allocator); 4009 pad_fa_tensor(acl_k_tensor, src1_bsnd_ne, k_pad_allocator); 4010 pad_fa_tensor(acl_v_tensor, src2_bsnd_ne, v_pad_allocator); 4011 4012 src0_bsnd_ne[0] = D_padded; 4013 src1_bsnd_ne[0] = D_padded; 4014 src2_bsnd_ne[0] = D_padded; 4015 } 4016 4017 // Step 3: create the PSEShift tensor if needed 4018 // this tensor is considered as mask (f16) in the llama.cpp 4019 acl_tensor_ptr bcast_pse_tensor; 4020 ggml_cann_pool_alloc bcast_pse_allocator(ctx.pool()); 4021 if (src3 != nullptr) { 4022 // Construct the truncated pse tensor (common for prefill/decode) 4023 int64_t trunc_pse_ne[GGML_MAX_DIMS] = { 4024 src3->ne[0], // D 4025 src0->ne[1], // S (number of Q tokens) 4026 src3->ne[2], // mask N 4027 src3->ne[3] // B 4028 }; 4029 size_t * trunc_pse_nb = src3->nb; 4030 4031 acl_tensor_ptr acl_mask_f16_trunc_tensor = ggml_cann_create_tensor( 4032 src3->data, ACL_FLOAT16, sizeof(uint16_t), trunc_pse_ne, trunc_pse_nb, GGML_MAX_DIMS); 4033 4034 int64_t bcast_pse_ne[GGML_MAX_DIMS]; 4035 size_t bcast_pse_nb[GGML_MAX_DIMS]; 4036 bcast_pse_ne[0] = src3->ne[0]; // D 4037 bcast_pse_ne[1] = src0->ne[1]; // S 4038 bcast_pse_ne[2] = src0->ne[2]; // N (num_heads) 4039 bcast_pse_ne[3] = src3->ne[3]; // B 4040 if (maxBias == 0.0f) { 4041 // When maxBias == 0.0f, use nb = 0 reduce once repeat (Qwen2) 4042 // Construct the bcast tensor (simulate repeat on the head dimension using stride=0) 4043 bcast_pse_nb[0] = sizeof(uint16_t); 4044 bcast_pse_nb[1] = bcast_pse_nb[0] * bcast_pse_ne[0]; 4045 bcast_pse_nb[2] = 0; // <---- the head dimension shares the same data 4046 bcast_pse_nb[3] = src3->nb[3]; 4047 4048 bcast_pse_tensor = ggml_cann_create_tensor(src3->data, ACL_FLOAT16, sizeof(uint16_t), bcast_pse_ne, 4049 bcast_pse_nb, GGML_MAX_DIMS); 4050 4051 } else { 4052 bcast_pse_nb[0] = sizeof(uint16_t); 4053 for (int i = 1; i < GGML_MAX_DIMS; i++) { 4054 bcast_pse_nb[i] = bcast_pse_nb[i - 1] * bcast_pse_ne[i - 1]; 4055 } 4056 4057 void * bcast_pse_buffer = 4058 bcast_pse_allocator.alloc(ggml_nelements(src3) * src0->ne[2] * sizeof(uint16_t)); 4059 4060 bcast_pse_tensor = ggml_cann_create_tensor(bcast_pse_buffer, ACL_FLOAT16, sizeof(uint16_t), 4061 bcast_pse_ne, bcast_pse_nb, GGML_MAX_DIMS); 4062 4063 int64_t repeats[] = { 1, src0->ne[2], 1, 1 }; 4064 aclnn_repeat(ctx, acl_mask_f16_trunc_tensor.get(), bcast_pse_tensor.get(), repeats); 4065 4066 // alibi 4067 // Compute the slope if needed. Derived from ggml_cann_softmax(). 4068 const int64_t n_heads = src0->ne[2]; 4069 ggml_cann_pool_alloc slope_allocator(ctx.pool(), n_heads * sizeof(uint16_t)); 4070 void * slope_buffer = slope_allocator.get(); 4071 aclnn_get_slope(ctx, n_heads, slope_buffer, maxBias, GGML_TYPE_F16); 4072 4073 int64_t slope_ne[] = { 1, 1, n_heads, 1 }; 4074 size_t slope_nb[GGML_MAX_DIMS]; 4075 slope_nb[0] = sizeof(uint16_t); 4076 for (int i = 1; i < GGML_MAX_DIMS; i++) { 4077 slope_nb[i] = slope_nb[i - 1] * slope_ne[0]; 4078 } 4079 4080 acl_tensor_ptr slope_tensor = ggml_cann_create_tensor(slope_buffer, ACL_FLOAT16, sizeof(uint16_t), 4081 slope_ne, slope_nb, GGML_MAX_DIMS); 4082 GGML_CANN_CALL_ACLNN_OP(ctx, InplaceMul, bcast_pse_tensor.get(), slope_tensor.get()); 4083 } 4084 } 4085 4086 // Step 4: set the inputs for FusedInferAttention. 4087 acl_tensor_list_ptr acl_k_tensor_list = ggml_cann_create_tensor_list(acl_k_tensor); 4088 acl_tensor_list_ptr acl_v_tensor_list = ggml_cann_create_tensor_list(acl_v_tensor); 4089 4090 int64_t numHeads = src0->ne[2]; // N 4091 int64_t numKeyValueHeads = src1->ne[2]; 4092 // double scaleValue = 1 / sqrt(src0->ne[0]); // 1/sqrt(d) 4093 int64_t preTokens = 65535; 4094 int64_t nextTokens = 65535; 4095 char layout[5] = { 'B', 'S', 'N', 'D', 0 }; 4096 int64_t sparseMode = 0; 4097 int64_t innerPrecise = (src0->ne[1] == 1) ? 0 : 2; 4098 int64_t blockSize = 0; 4099 int64_t antiquantMode = 0; 4100 bool softmaxLseFlag = false; 4101 int64_t keyAntiquantMode = 0; 4102 int64_t valueAntiquantMode = 0; 4103 4104 GGML_ASSERT(dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16); 4105 acl_tensor_ptr fa_dst_tensor; 4106 ggml_cann_pool_alloc out_f16_allocator(ctx.pool()); 4107 if (dst->type == GGML_TYPE_F32 || needs_padding) { 4108 int64_t * out_f16_ne = src0_bsnd_ne; 4109 size_t out_f16_nb[GGML_MAX_DIMS]; 4110 out_f16_nb[0] = faElemSize; 4111 for (int i = 1; i < GGML_MAX_DIMS; ++i) { 4112 out_f16_nb[i] = out_f16_nb[i - 1] * out_f16_ne[i - 1]; 4113 } 4114 int64_t out_nelements = out_f16_ne[0] * out_f16_ne[1] * out_f16_ne[2] * out_f16_ne[3]; 4115 void * out_f16_buffer = out_f16_allocator.alloc(out_nelements * faElemSize); 4116 4117 fa_dst_tensor = 4118 ggml_cann_create_tensor(out_f16_buffer, faDataType, faElemSize, out_f16_ne, out_f16_nb, GGML_MAX_DIMS); 4119 } else { 4120 fa_dst_tensor = ggml_cann_create_tensor(dst); 4121 } 4122 4123 GGML_CANN_CALL_ACLNN_OP(ctx, FusedInferAttentionScoreV2, acl_q_tensor.get(), acl_k_tensor_list.get(), 4124 acl_v_tensor_list.get(), // q, k, v 4125 bcast_pse_tensor.get(), nullptr, // pse, mask 4126 nullptr, nullptr, // actSeqLen, actSeqLenkv 4127 nullptr, nullptr, // deqScale1, quantScale1 4128 nullptr, nullptr, nullptr, // deqScale2, quantScale2, quantOffset2 4129 nullptr, nullptr, // antiquantScale, antiquantOffset 4130 nullptr, // blockTable 4131 nullptr, nullptr, // qPadSize, kvPadSize 4132 nullptr, nullptr, // kAntiquantScale, kAntiQuantOffset 4133 nullptr, nullptr, // vAntiquantScale, vAntiQuantOffset 4134 nullptr, nullptr, nullptr, // kSharedPrefix, vSharedPrefix, actSharedLen 4135 numHeads, scaleValue, // heads, scaleValue 4136 preTokens, nextTokens, // preTokens, nextTokens 4137 layout, // inputLayout 4138 numKeyValueHeads, // numKVHeads 4139 sparseMode, innerPrecise, // sparseMode, innerPrecise 4140 blockSize, antiquantMode, // blockSize, antiquantMode 4141 softmaxLseFlag, // softmaxLseFlag 4142 keyAntiquantMode, valueAntiquantMode, // keyAntiqMode, valueAntiqMode 4143 fa_dst_tensor.get(), // attentionOut 4144 nullptr // softmaxLse 4145 ); 4146 4147 // Step 6: post-processing — slice padded output and/or cast to f32 4148 if (needs_padding) { 4149 ggml_cann_pool_alloc sliced_f16_allocator(ctx.pool()); 4150 4151 if (dst->type == GGML_TYPE_F32) { 4152 int64_t sliced_ne[GGML_MAX_DIMS] = { D, src0_bsnd_ne[1], src0_bsnd_ne[2], src0_bsnd_ne[3] }; 4153 size_t sliced_nb[GGML_MAX_DIMS]; 4154 sliced_nb[0] = faElemSize; 4155 for (int i = 1; i < GGML_MAX_DIMS; ++i) { 4156 sliced_nb[i] = sliced_nb[i - 1] * sliced_ne[i - 1]; 4157 } 4158 int64_t sliced_nelements = sliced_ne[0] * sliced_ne[1] * sliced_ne[2] * sliced_ne[3]; 4159 void * sliced_buffer = sliced_f16_allocator.alloc(sliced_nelements * faElemSize); 4160 acl_tensor_ptr sliced_f16_tensor = ggml_cann_create_tensor(sliced_buffer, faDataType, faElemSize, 4161 sliced_ne, sliced_nb, GGML_MAX_DIMS); 4162 4163 GGML_CANN_CALL_ACLNN_OP(ctx, Slice, fa_dst_tensor.get(), 4164 (int64_t) -1, (int64_t) 0, D, (int64_t) 1, sliced_f16_tensor.get()); 4165 4166 acl_tensor_ptr acl_dst_tensor = ggml_cann_create_tensor(dst); 4167 aclnn_cast(ctx, sliced_f16_tensor.get(), acl_dst_tensor.get(), ggml_cann_type_mapping(dst->type)); 4168 } else { 4169 acl_tensor_ptr acl_dst_tensor = ggml_cann_create_tensor(dst); 4170 GGML_CANN_CALL_ACLNN_OP(ctx, Slice, fa_dst_tensor.get(), 4171 (int64_t) -1, (int64_t) 0, D, (int64_t) 1, acl_dst_tensor.get()); 4172 } 4173 } else if (dst->type == GGML_TYPE_F32) { 4174 acl_tensor_ptr acl_dst_tensor = ggml_cann_create_tensor(dst); 4175 aclnn_cast(ctx, fa_dst_tensor.get(), acl_dst_tensor.get(), ggml_cann_type_mapping(dst->type)); 4176 } 4177 } else { 4178 GGML_ABORT("Function is not implemented."); 4179 } 4180 } 4181 4182 static void ggml_cann_out_prod_fp(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 4183 ggml_tensor * src0 = dst->src[0]; // weight [ne00=m, ne01=K, ne02, ne03] 4184 ggml_tensor * src1 = dst->src[1]; // input [ne10=n, ne11=K, ne12, ne13] 4185 GGML_TENSOR_BINARY_OP_LOCALS 4186 4187 // dst[i,j] = sum_k src0[i,k] * src1[j,k] i.e. dst = src0 @ src1^T. 4188 // 4189 // ggml_cann_create_tensor reverses dimension order, so ACL sees: 4190 // acl_src0 slice: ggml[m,K] -> ACL[K,m] 4191 // acl_src1 slice: ggml[n,K] -> ACL[K,n] 4192 // acl_dst slice: ggml[m,n] -> ACL[n,m] 4193 // 4194 // Build a transposed view of src1 by swapping ne[0]/ne[1]: 4195 // src1_t: ggml[K,n] (swapped strides) -> ACL[n,K] 4196 // 4197 // Matmul(src1_t [n,K], src0 [K,m]) = [n,m] = acl_dst ✓ 4198 // 4199 // The outer batch loop is kept because src0 may have fewer batch slices than 4200 // dst (ne02 <= ne2, ne03 <= ne3): this is a strided-broadcast not supported 4201 // by standard CANN Matmul broadcasting. 4202 4203 const aclDataType src0_acl_type = ggml_cann_type_mapping(src0->type); 4204 const aclDataType src1_acl_type = ggml_cann_type_mapping(src1->type); 4205 const aclDataType dst_acl_type = ggml_cann_type_mapping(dst->type); 4206 const size_t src0_type_sz = ggml_type_size(src0->type); 4207 const size_t src1_type_sz = ggml_type_size(src1->type); 4208 const size_t dst_type_sz = ggml_type_size(dst->type); 4209 4210 const int64_t dps2 = ne2 / ne02; 4211 const int64_t dps3 = ne3 / ne03; 4212 4213 for (int64_t i3 = 0; i3 < ne3; i3++) { 4214 for (int64_t i2 = 0; i2 < ne2; i2++) { 4215 const int64_t i02 = i2 / dps2; 4216 const int64_t i03 = i3 / dps3; 4217 4218 // src0 2D slice at [i02, i03]: ggml [m, K] -> ACL [K, m] 4219 int64_t src0_ne[2] = { ne00, ne01 }; 4220 size_t src0_nb[2] = { nb00, nb01 }; 4221 acl_tensor_ptr acl_src0_s = ggml_cann_create_tensor( 4222 (char *) src0->data + i02 * nb02 + i03 * nb03, 4223 src0_acl_type, src0_type_sz, src0_ne, src0_nb, 2); 4224 4225 // src1 transposed 2D slice at [i2, i3]: swap ne/nb -> ggml[K,n] -> ACL[n,K] 4226 int64_t src1_t_ne[2] = { ne11, ne10 }; 4227 size_t src1_t_nb[2] = { nb11, nb10 }; 4228 acl_tensor_ptr acl_src1_t = ggml_cann_create_tensor( 4229 (char *) src1->data + i2 * nb12 + i3 * nb13, 4230 src1_acl_type, src1_type_sz, src1_t_ne, src1_t_nb, 2); 4231 4232 // dst 2D slice at [i2, i3]: ggml [m, n] -> ACL [n, m] 4233 int64_t dst_ne[2] = { ne0, ne1 }; 4234 size_t dst_nb[2] = { nb0, nb1 }; 4235 acl_tensor_ptr acl_dst_s = ggml_cann_create_tensor( 4236 (char *) dst->data + i2 * nb2 + i3 * nb3, 4237 dst_acl_type, dst_type_sz, dst_ne, dst_nb, 2); 4238 4239 // Matmul(src1_t [n,K], src0 [K,m]) = [n,m] = acl_dst_s ✓ 4240 GGML_CANN_CALL_ACLNN_OP(ctx, Matmul, 4241 acl_src1_t.get(), acl_src0_s.get(), acl_dst_s.get(), (int8_t) 1); 4242 } 4243 } 4244 } 4245 4246 void ggml_cann_out_prod(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 4247 ggml_tensor * src0 = dst->src[0]; 4248 4249 const enum ggml_type type = src0->type; 4250 4251 switch (type) { 4252 case GGML_TYPE_F32: 4253 case GGML_TYPE_F16: 4254 ggml_cann_out_prod_fp(ctx, dst); 4255 break; 4256 default: 4257 GGML_ABORT("Unsupport type for GGML_OP_OUT_PROD"); 4258 break; 4259 } 4260 } 4261 4262 void ggml_cann_ssm_conv(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 4263 ggml_tensor * src0 = dst->src[0]; // conv_x 4264 ggml_tensor * src1 = dst->src[1]; // conv1d.weight 4265 4266 // This op is currently defined only for F32 in ggml_cpu 4267 GGML_ASSERT(src0->type == GGML_TYPE_F32); 4268 GGML_ASSERT(src1->type == GGML_TYPE_F32); 4269 GGML_ASSERT(dst->type == GGML_TYPE_F32); 4270 4271 // Shapes follow ggml_compute_forward_ssm_conv_f32 4272 const int64_t nc = src1->ne[0]; // d_conv 4273 const int64_t ncs = src0->ne[0]; // d_conv - 1 + n_t 4274 const int64_t nr = src0->ne[1]; // d_inner 4275 const int64_t n_s = src0->ne[2]; // n_seqs 4276 4277 const int64_t n_t = dst->ne[1]; // tokens per sequence 4278 4279 GGML_ASSERT(dst->ne[0] == nr); // dst: {d_inner, n_t, n_s} 4280 GGML_ASSERT(src1->ne[1] == nr); // weight: {d_conv, d_inner} 4281 GGML_ASSERT(ncs == nc - 1 + n_t); // conv_x: {d_conv - 1 + n_t, d_inner, n_s} 4282 GGML_ASSERT(src0->nb[0] == sizeof(float)); 4283 GGML_ASSERT(src1->nb[0] == sizeof(float)); 4284 4285 // --- Build CANN tensors --- 4286 4287 // 1) Input: conv_x as NCL 4288 // 4289 // src0->ne = { ncs, nr, n_s, 1 } // {L_in, C, N} 4290 // Passing ACL_FORMAT_NCL here means: 4291 // reversed dims -> [N, C, L_in] = [n_s, nr, ncs] 4292 acl_tensor_ptr acl_x = ggml_cann_create_tensor(src0, src0->ne, src0->nb, 3, ACL_FORMAT_NCL); 4293 4294 // 2) Weights: depthwise conv kernel, view src1 as {K, 1, C} 4295 // 4296 // src1 original: ne = { nc, nr, 1, 1 } // [K, C, 1, 1] 4297 // we want a view: ne_w = { nc, 1, nr } // [K, 1, C] 4298 // so that reversed dims -> [C, 1, K] which matches 4299 // [out_channels, in_channels/groups, kernel_size] 4300 int64_t w_ne[GGML_MAX_DIMS] = { nc, 1, nr, 1 }; // [K, 1 input ch. per group, C groups] 4301 // Layout: src1 data is [K, C] with 4302 // offset(k, c) = k*nb0 + c*nb1 4303 // We want offset_w(k, 0, c) = k*nb0 + c*nb1, 4304 // so we can reuse nb0 and nb1, and set nb2 = nb1. 4305 size_t w_nb[GGML_MAX_DIMS] = { src1->nb[0], src1->nb[1], src1->nb[1], src1->nb[3] }; // same as src1 4306 4307 acl_tensor_ptr acl_w = ggml_cann_create_tensor(src1->data, ggml_cann_type_mapping(src1->type), 4308 ggml_type_size(src1->type), w_ne, w_nb, 3, ACL_FORMAT_NCL); 4309 4310 // 3) Output: dst is { d_inner, n_t, n_s } (CLN) 4311 // 4312 // We need an NCL view of the same buffer: 4313 // desired NCL logical shape: { L_out = n_t, C = nr, N = n_s } 4314 // 4315 // Original CLN layout: 4316 // dst->ne = { nr, n_t, n_s } 4317 // dst->nb[0] = sizeof(float) 4318 // dst->nb[1] = nr * sizeof(float) 4319 // dst->nb[2] = nr * n_t * sizeof(float) 4320 // 4321 // We want offset_new(L, C, N) = offset_orig(C, L, N). 4322 // Choose: 4323 // nb_y[0] = nr * sizeof(float); // step in L 4324 // nb_y[1] = sizeof(float); // step in C 4325 // nb_y[2] = nr * n_t * sizeof(float); // step in N 4326 int64_t y_ne[GGML_MAX_DIMS] = { n_t, nr, n_s, 1 }; // [L_out, C, N] 4327 size_t y_nb[GGML_MAX_DIMS] = { dst->ne[0] * sizeof(float), sizeof(float), dst->ne[0] * dst->ne[1] * sizeof(float), 4328 dst->nb[3] }; // [nr, 1, nr * n_t] 4329 4330 acl_tensor_ptr acl_y = ggml_cann_create_tensor(dst->data, ggml_cann_type_mapping(dst->type), 4331 ggml_type_size(dst->type), y_ne, y_nb, 3, ACL_FORMAT_NCL); 4332 4333 // --- Conv1d parameters: depthwise, stride 1, no padding ("valid") --- 4334 int64_t strideVal[1] = { 1 }; 4335 int64_t paddingVal[1] = { 0 }; 4336 int64_t dilationVal[1] = { 1 }; 4337 4338 acl_int_array_ptr stride = ggml_cann_create_int_array(strideVal, 1); 4339 acl_int_array_ptr padding = ggml_cann_create_int_array(paddingVal, 1); 4340 acl_int_array_ptr dilation = ggml_cann_create_int_array(dilationVal, 1); 4341 4342 const bool transposed = false; 4343 const int64_t groups = nr; // depthwise: one group per inner dim 4344 int8_t cubeMathType = 0; 4345 4346 #ifdef ASCEND_310P 4347 cubeMathType = 1; 4348 #endif 4349 4350 GGML_CANN_CALL_ACLNN_OP(ctx, Convolution, 4351 acl_x.get(), // input: N, C, L_in = ncs 4352 acl_w.get(), // weight: [C, 1, K] with groups=nr 4353 nullptr, // bias 4354 stride.get(), padding.get(), dilation.get(), transposed, 4355 padding.get(), // output padding (unused for non-transposed) 4356 groups, acl_y.get(), cubeMathType); 4357 } 4358 4359 void ggml_cann_op_add_rms_norm_fused(ggml_backend_cann_context & ctx, 4360 ggml_tensor * add_node, 4361 ggml_tensor * rms_norm_node) { 4362 // Get the two input tensors for ADD operation 4363 ggml_tensor * x1 = add_node->src[0]; 4364 ggml_tensor * x2 = add_node->src[1]; 4365 4366 // Create ACL tensors for the two ADD inputs 4367 acl_tensor_ptr acl_x1 = ggml_cann_create_tensor(x1); 4368 acl_tensor_ptr acl_x2 = ggml_cann_create_tensor(x2); 4369 4370 // Get epsilon parameter from rms_norm_tensor 4371 float eps; 4372 memcpy(&eps, rms_norm_node->op_params, sizeof(float)); 4373 4374 // Build gamma tensor (RMS normalization scaling factor) 4375 // Gamma should match the normalized dimensions (last dimension of x1) 4376 size_t acl_gamma_nb[GGML_MAX_DIMS]; 4377 acl_gamma_nb[0] = ggml_type_size(rms_norm_node->type); 4378 for (int i = 1; i < GGML_MAX_DIMS; i++) { 4379 acl_gamma_nb[i] = acl_gamma_nb[i - 1] * x1->ne[i - 1]; 4380 } 4381 acl_tensor_ptr acl_gamma = 4382 get_cache_acl_tensor(ctx, &ctx.rms_norm_one_tensor_cache.cache, ctx.rms_norm_one_tensor_cache.size, x1->ne, 4383 acl_gamma_nb, rms_norm_node->type, 4384 1, // dims - only the last dimension 4385 1.0f // value 4386 ); 4387 4388 // Build rstdOut tensor (output for normalized standard deviation) 4389 // Shape should be the dimensions that are NOT normalized 4390 int64_t acl_rstd_ne[] = { 1, x1->ne[1], x1->ne[2], x1->ne[3] }; 4391 size_t acl_rstd_nb[GGML_MAX_DIMS - 1]; 4392 acl_rstd_nb[0] = sizeof(float); 4393 for (int i = 1; i < GGML_MAX_DIMS - 1; i++) { 4394 acl_rstd_nb[i] = acl_rstd_nb[i - 1] * acl_rstd_ne[i - 1]; 4395 } 4396 acl_tensor_ptr acl_rstd = 4397 get_cache_acl_tensor(ctx, &ctx.rms_norm_zero_tensor_cache.cache, ctx.rms_norm_zero_tensor_cache.size, 4398 acl_rstd_ne, acl_rstd_nb, GGML_TYPE_F32, GGML_MAX_DIMS, 4399 0.0f // value 4400 ); 4401 4402 acl_tensor_ptr acl_xout = ggml_cann_create_tensor(add_node); 4403 4404 // Create yOut tensor (final output after RMS normalization) 4405 acl_tensor_ptr acl_yout = ggml_cann_create_tensor(rms_norm_node); 4406 4407 // Call fused ADD + RMS_NORM operator 4408 GGML_CANN_CALL_ACLNN_OP(ctx, AddRmsNorm, acl_x1.get(), acl_x2.get(), acl_gamma.get(), 4409 eps, // double type 4410 acl_yout.get(), acl_rstd.get(), acl_xout.get()); 4411 } 4412 4413 void ggml_cann_gated_linear_attn(ggml_backend_cann_context & ctx, ggml_tensor * dst) { 4414 ggml_tensor * k = dst->src[0]; 4415 ggml_tensor * v = dst->src[1]; 4416 ggml_tensor * q = dst->src[2]; 4417 ggml_tensor * g = dst->src[3]; 4418 ggml_tensor * s = dst->src[4]; 4419 4420 int64_t B = dst->src[4]->ne[1]; 4421 int64_t T = dst->src[0]->ne[2]; 4422 int64_t H = dst->src[0]->ne[1]; 4423 int64_t C = dst->ne[0]; 4424 int64_t D = C / H; 4425 int64_t L = T / B; 4426 4427 int64_t ne_qkg[2] = { 1, D }; 4428 int64_t ne_s[2] = { D, D }; 4429 int64_t ne_st[2] = { ne_s[1], ne_s[0] }; 4430 int64_t ne_vo[2] = { D, 1 }; 4431 int64_t ne_q[1] = { D }; 4432 size_t nb_base = ggml_type_size(k->type); 4433 size_t nb_qkg[2] = { nb_base, nb_base }; 4434 size_t nb_s[2] = { nb_base, D * nb_base }; 4435 size_t nb_st[2] = { nb_s[1], nb_s[0] }; 4436 size_t nb_vo[2] = { nb_base, D * nb_base }; 4437 size_t nb_q[1] = { nb_base }; 4438 4439 const float scale = ggml_get_op_params_f32(dst, 0); 4440 4441 acl_tensor_ptr acl_s = ggml_cann_create_tensor(s, s->ne, s->nb, 2, ACL_FORMAT_ND); 4442 acl_tensor_ptr new_state = ggml_cann_create_tensor(dst, s->ne, s->nb, 2, ACL_FORMAT_ND, (B * L * H * D) * nb_base); 4443 cann_copy(ctx, acl_s.get(), new_state.get()); 4444 4445 for (int64_t b = 0; b < B; b++) { 4446 for (int64_t h = 0; h < H; h++) { 4447 size_t s_offset = (b * (H * D * D) + h * (D * D)) * nb_base; 4448 // D * D 4449 acl_tensor_ptr acl_s_new = 4450 ggml_cann_create_tensor(dst, ne_s, nb_s, 2, ACL_FORMAT_ND, (B * L * H * D) * nb_base + s_offset); 4451 acl_tensor_ptr acl_s_new_t = 4452 ggml_cann_create_tensor(dst, ne_st, nb_st, 2, ACL_FORMAT_ND, (B * L * H * D) * nb_base + s_offset); 4453 for (int64_t l = 0; l < L; l++) { 4454 size_t qkvgo_offset = (b * (L * H * D) + l * (H * D) + h * (D)) * nb_base; 4455 // D * 1 4456 acl_tensor_ptr acl_k = ggml_cann_create_tensor(k, ne_qkg, nb_qkg, 2, ACL_FORMAT_ND, qkvgo_offset); 4457 acl_tensor_ptr acl_g = ggml_cann_create_tensor(g, ne_qkg, nb_qkg, 2, ACL_FORMAT_ND, qkvgo_offset); 4458 // D 4459 acl_tensor_ptr acl_q = ggml_cann_create_tensor(q, ne_q, nb_q, 1, ACL_FORMAT_ND, qkvgo_offset); 4460 // 1 * D 4461 acl_tensor_ptr acl_v = ggml_cann_create_tensor(v, ne_vo, nb_vo, 2, ACL_FORMAT_ND, qkvgo_offset); 4462 // D 4463 acl_tensor_ptr acl_o = ggml_cann_create_tensor(dst, ne_q, nb_q, 1, ACL_FORMAT_ND, qkvgo_offset); 4464 // k ⊗ v 4465 size_t buf_size = D * D * nb_base; 4466 ggml_cann_pool_alloc buffer_allocator(ctx.pool(), buf_size); 4467 acl_tensor_ptr tmp_tensor = ggml_cann_create_tensor( 4468 buffer_allocator.get(), ggml_cann_type_mapping(k->type), nb_base, ne_s, nb_s, 2); 4469 aclnn_mul(ctx, acl_k.get(), acl_v.get(), tmp_tensor.get()); 4470 //s_new = g ⊗ s_old + k ⊗ v 4471 aclnn_mul(ctx, acl_s_new.get(), acl_g.get(), nullptr); 4472 aclnn_add(ctx, acl_s_new.get(), tmp_tensor.get(), nullptr); 4473 // compute output 4474 GGML_CANN_CALL_ACLNN_OP(ctx, Mv, acl_s_new_t.get(), acl_q.get(), acl_o.get(), 1); 4475 aclnn_muls(ctx, acl_o.get(), scale, nullptr, true); 4476 } 4477 } 4478 } 4479 }