|
1 | 1 | #ifndef CANN_ACLNN_OPS
|
2 | 2 | #define CANN_ACLNN_OPS
|
3 | 3 |
|
| 4 | +/** |
| 5 | + * @file acl_tensor |
| 6 | + * @brief This file contains related functions of ggml_tensor and acl_tensor. |
| 7 | + * Contains conversion from ggml_tensor to acl_tensor, broadcast and other |
| 8 | + * functions. |
| 9 | + * @author hipudding <[email protected]> |
| 10 | + * @author wangshuai09 <[email protected]> |
| 11 | + * @date July 15, 2024 |
| 12 | + * |
| 13 | + * Copyright (c) 2023-2024 The ggml authors |
| 14 | + * |
| 15 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 16 | + * of this software and associated documentation files (the "Software"), to |
| 17 | + * deal in the Software without restriction, including without limitation the |
| 18 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 19 | + * sell copies of the Software, and to permit persons to whom the Software is |
| 20 | + * furnished to do so, subject to the following conditions: |
| 21 | + * |
| 22 | + * The above copyright notice and this permission notice shall be included in |
| 23 | + * all copies or substantial portions of the Software. |
| 24 | + * |
| 25 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 26 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 27 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 28 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 29 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 30 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 31 | + * IN THE SOFTWARE. |
| 32 | + */ |
| 33 | + |
4 | 34 | #include <aclnnop/aclnn_add.h>
|
5 | 35 | #include <aclnnop/aclnn_arange.h>
|
6 | 36 | #include <aclnnop/aclnn_argsort.h>
|
|
18 | 48 | #include "acl_tensor.h"
|
19 | 49 | #include "common.h"
|
20 | 50 |
|
| 51 | +/** |
| 52 | + * @brief Repeats a ggml tensor along each dimension to match the dimensions |
| 53 | + * of another tensor. |
| 54 | + * |
| 55 | + * @details This function repeats the elements of a source ggml tensor along |
| 56 | + * each dimension to create a destination tensor with the specified |
| 57 | + * dimensions. The operation is performed using the ACL backend and |
| 58 | + * executed asynchronously on the device. |
| 59 | + * |
| 60 | + * @param ctx The CANN context used for operations. |
| 61 | + * @param dst The ggml tensor representing the destination, which op is |
| 62 | + * GGML_OP_REPEAT and specifies the desired dimensions. |
| 63 | + */ |
21 | 64 | void ggml_cann_repeat(ggml_backend_cann_context& ctx, ggml_tensor* dst);
|
22 | 65 |
|
| 66 | +/** |
| 67 | + * @brief Adds two ggml tensors using the CANN backend. |
| 68 | + * |
| 69 | + * @details This function performs an element-wise addition of two tensors. In |
| 70 | + * case the tensors do not have the same shape, one or both tensors |
| 71 | + * will be broadcasted to match the shape of the other before the |
| 72 | + * addition is performed.The formula for the operation is given by: |
| 73 | + * \f[ |
| 74 | + * \text{dst} = \text{acl_src0} + \alpha \cdot \text{acl_src1} |
| 75 | + * \f] |
| 76 | + * |
| 77 | + * @param ctx The CANN context used for operations. |
| 78 | + * @param dst The ggml tensor representing the destination, result of the |
| 79 | + * addition is stored at dst->data, and dst->op is GGML_OP_ADD |
| 80 | + */ |
23 | 81 | void ggml_cann_add(ggml_backend_cann_context& ctx, ggml_tensor* dst);
|
24 | 82 |
|
| 83 | +/** |
| 84 | + * @brief Applies the Leaky ReLU activation function to a tensor using the CANN |
| 85 | + * backend. |
| 86 | + * |
| 87 | + * @details This function computes the Leaky ReLU activation for each element of |
| 88 | + * the input tensor. The Leaky ReLU function allows a small gradient |
| 89 | + * when the unit is not active (i.e., when the input is negative). The |
| 90 | + * Leaky ReLU function is defined as: |
| 91 | + * \f[ |
| 92 | + * \text{dst} = \max(0, src) + \text{negativeSlope} \cdot \min(0, |
| 93 | + * src) |
| 94 | + * \f] |
| 95 | + * |
| 96 | + * @param ctx The CANN context used for operations. |
| 97 | + * @param dst The destination tensor where the result of the Leaky ReLU |
| 98 | + * activation is stored, which op is GGML_OP_LEAKY_RELU |
| 99 | + */ |
25 | 100 | void ggml_cann_leaky_relu(ggml_backend_cann_context& ctx, ggml_tensor* dst);
|
26 | 101 |
|
| 102 | +/** |
| 103 | + * @brief Concatenates multiple tensors along a specified dimension using the |
| 104 | + * CANN backend. |
| 105 | + * |
| 106 | + * @param ctx The CANN context used for operations. |
| 107 | + * @param tensorList A pointer to the list of tensors to be concatenated. |
| 108 | + * @param dst The destination tensor where the result of the |
| 109 | + * concatenation is stored. dst->op is GGML_OP_CONCAT. |
| 110 | + * @param concat_dim The dimension along which the tensors are concatenated. |
| 111 | + * |
| 112 | + * @attention tensorList length should be 2 and the dimension using for concat |
| 113 | + * default to 1. |
| 114 | + */ |
27 | 115 | void ggml_cann_concat(ggml_backend_cann_context& ctx, ggml_tensor* dst);
|
28 | 116 |
|
| 117 | +/** |
| 118 | + * @brief Generates a sequence of evenly spaced values within a specified |
| 119 | + * interval for a ggml tensor using the CANN backend. |
| 120 | + * |
| 121 | + * @details This function creates a sequence of numbers over a specified i |
| 122 | + * nterval, starting from `start`, ending before `stop`, and |
| 123 | + * incrementing by `step`. The sequence is stored in the destination |
| 124 | + * tensor `dst`. |
| 125 | + * |
| 126 | + * @param ctx The CANN context used for operations. |
| 127 | + * @param dst The destination tensor where the generated sequence will be stored. |
| 128 | + * `start`, 'stop' and 'step' are in dst->op_params and dst->op is |
| 129 | + * GGML_OP_ARANGE |
| 130 | + */ |
29 | 131 | void ggml_cann_arange(ggml_backend_cann_context& ctx, ggml_tensor* dst);
|
30 | 132 |
|
| 133 | +/** |
| 134 | + * @brief Computes the square of the elements of a ggml tensor using the CANN |
| 135 | + * backend. |
| 136 | + * @details The function sets the second source tensor of the destination |
| 137 | + * tensor `dst` to be equal to the first source tensor. This is |
| 138 | + * effectively squaring the elements since the multiplication becomes |
| 139 | + * `element * element`. |
| 140 | + * @param ctx The CANN context used for operations. |
| 141 | + * @param dst The destination tensor where the squared values will be stored, |
| 142 | + * which dst->op is GGML_OP_SQR |
| 143 | + */ |
31 | 144 | void ggml_cann_sqr(ggml_backend_cann_context& ctx, ggml_tensor* dst);
|
32 | 145 |
|
| 146 | +/** |
| 147 | + * @brief Applies a clamp operation to the elements of a ggml tensor using the |
| 148 | + * CANN backend. |
| 149 | + * |
| 150 | + * @details This function clamps the elements of the input tensor `src` to a |
| 151 | + * specified range defined by `min` and `max` values. The result is |
| 152 | + * stored in the destination tensor `dst`. The operation is defined as: |
| 153 | + * \f[ |
| 154 | + * y = \max(\min(x, max\_value), min\_value) |
| 155 | + * \f] |
| 156 | + * where `x` is an element of the input tensor, and `y` is the |
| 157 | + * corresponding element in the output tensor. |
| 158 | + * @param ctx The CANN context used for operations. |
| 159 | + * @param dst The destination tensor where the clamped values will be stored. |
| 160 | + * dst->op is GGML_OP_CLAMP, `min` and `max` value is in dst->params. |
| 161 | + */ |
33 | 162 | void ggml_cann_clamp(ggml_backend_cann_context& ctx, ggml_tensor* dst);
|
34 | 163 |
|
| 164 | +/** |
| 165 | + * @brief Scales the elements of a ggml tensor by a constant factor using the |
| 166 | + * CANN backend. |
| 167 | + * |
| 168 | + * @details This function multiplies each element of the input tensor `src` by |
| 169 | + * a scaling factor `scale`, storing the result in the destination |
| 170 | + * tensor `dst`. The operation is defined as: |
| 171 | + * \f[ |
| 172 | + * dst = src \times scale |
| 173 | + * \f] |
| 174 | + * |
| 175 | + * @param ctx The CANN context used for operations. |
| 176 | + * @param dst The destination tensor where the scaled values will be stored. |
| 177 | + * dst->op is GGML_OP_SCALE and scale value is in dst->params. |
| 178 | + */ |
35 | 179 | void ggml_cann_scale(ggml_backend_cann_context& ctx, ggml_tensor* dst);
|
36 | 180 |
|
| 181 | +/** |
| 182 | + * @brief Sorts the elements of a ggml tensor and returns the indices that |
| 183 | + * would sort the tensor using the CANN backend. |
| 184 | + * |
| 185 | + * @details This function performs an argsort operation on the input tensor |
| 186 | + * `src`. It sorts the elements of `src` in either ascending or |
| 187 | + * descending order, depending on the `GGML_SORT_ORDER_DESC`, |
| 188 | + * and returns the indices that would sort the original tensor. |
| 189 | + * |
| 190 | + * @param ctx The CANN context used for operations. |
| 191 | + * @param dst The destination tensor where the sorted indices will be stored. |
| 192 | + * dst->op is ARGSORT |
| 193 | + */ |
37 | 194 | void ggml_cann_argsort(ggml_backend_cann_context& ctx, ggml_tensor* dst);
|
38 | 195 |
|
| 196 | +/** |
| 197 | + * @brief Computes the Layer Normalization for a ggml tensor using the CANN |
| 198 | + * backend. |
| 199 | + * |
| 200 | + * @details This function applies the Layer Normalization operation on the |
| 201 | + * input tensor `src` and stores the result in the destination tensor |
| 202 | + * `dst`. Layer Normalization normalizes the features at each sample in |
| 203 | + * a mini-batch independently. It is commonly used in neural networks |
| 204 | + * to normalize the activations of a layer by adjusting and scaling |
| 205 | + * the outputs. The operation is defined as: |
| 206 | + * \f[ |
| 207 | + * \text { out }=\frac{x-\mathrm{E}[x]}{\sqrt{\operatorname{Var}[x]+eps}} |
| 208 | + * \f], Var is default dst->ne[0]. eps is dst->params. |
| 209 | + * |
| 210 | + * @param ctx The CANN context used for operations. |
| 211 | + * @param dst The destination tensor where the normalized values will be stored. |
| 212 | + */ |
39 | 213 | void ggml_cann_norm(ggml_backend_cann_context& ctx, ggml_tensor* dst);
|
40 | 214 |
|
| 215 | +/** |
| 216 | + * @brief Computes the Group Normalization for a ggml tensor using the CANN |
| 217 | + * backend. |
| 218 | + * |
| 219 | + * @brief This function applies the Group Normalization operation on the input |
| 220 | + * tensor `src` and stores the result in the destination tensor `dst`. |
| 221 | + * Group Normalization divides the channels into groups and normalizes |
| 222 | + * the features within each group across spatial locations. |
| 223 | + * It is commonly used in convolutional neural networks to improve |
| 224 | + * training stability and performance. |
| 225 | + * |
| 226 | + * @details |
| 227 | + * The function first creates ACL tensors from the ggml tensors `src` and `dst`. It then defines the epsilon value (`eps`) |
| 228 | + * and the number of groups (`n_groups`) for normalization. Variables for workspace allocation are initialized. The |
| 229 | + * function calculates the workspace size required for the Group Normalization operation using the |
| 230 | + * `aclnnGroupNormGetWorkspaceSize` function. If a workspace is needed, it allocates memory using the ggml pool allocator. |
| 231 | + * Temporary tensors `acl_mean_out` and `acl_rstd_out` are created for intermediate results. The Group Normalization |
| 232 | + * operation is executed using the `aclnnGroupNorm` function. Finally, the allocated resources are released. |
| 233 | + * |
| 234 | + * @param ctx The CANN context used for operations. |
| 235 | + * @param dst The destination tensor where the normalized values will be stored. |
| 236 | + * |
| 237 | + * @attention eps defaults to 1e-6f |
| 238 | + */ |
41 | 239 | void ggml_cann_group_norm(ggml_backend_cann_context& ctx, ggml_tensor* dst);
|
42 | 240 |
|
43 | 241 | void ggml_cann_softmax(ggml_backend_cann_context& ctx, ggml_tensor* dst);
|
|
0 commit comments