Skip to content

Commit 655cddd

Browse files
authored
CANN: Add L2_NORM op support (#16856)
* update L2_NORM op support * update L2_NORM op support * remove extra whitespace
1 parent 5da7664 commit 655cddd

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

ggml/src/ggml-cann/aclnn_ops.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,35 @@ void ggml_cann_norm(ggml_backend_cann_context & ctx, ggml_tensor * dst) {
448448
ggml_cann_release_resources(ctx, norm, acl_src, acl_dst);
449449
}
450450

451+
void ggml_cann_l2_norm(ggml_backend_cann_context & ctx, ggml_tensor * dst) {
452+
ggml_tensor * src = dst->src[0];
453+
454+
aclTensor * acl_src = ggml_cann_create_tensor(src);
455+
aclTensor * acl_dst = ggml_cann_create_tensor(dst);
456+
457+
size_t type_size = ggml_type_size(src->type);
458+
int64_t n_bytes = src->ne[3]* src->ne[2]* src->ne[1]* type_size;
459+
ggml_cann_pool_alloc temp_buffer_allocator(ctx.pool(), n_bytes);
460+
void * buffer = temp_buffer_allocator.get();
461+
462+
int64_t div_ne[] = {1, src->ne[1], src->ne[2], src->ne[3]};
463+
size_t div_nb[GGML_MAX_DIMS];
464+
div_nb[0] = sizeof(float);
465+
for (int i = 1; i < GGML_MAX_DIMS; ++i) {
466+
div_nb[i] = div_nb[i - 1] * div_ne[i - 1];
467+
}
468+
aclTensor * acl_div = ggml_cann_create_tensor(buffer, ACL_FLOAT, type_size, div_ne, div_nb, GGML_MAX_DIMS);
469+
470+
std::vector<int64_t> norm_dims = { 3 };
471+
aclIntArray * dims_array = aclCreateIntArray(norm_dims.data(), norm_dims.size());
472+
473+
float p_value = 2.0f;
474+
aclScalar * p_scalar = aclCreateScalar(&p_value, aclDataType::ACL_FLOAT);
475+
GGML_CANN_CALL_ACLNN_OP(ctx, Norm, acl_src, p_scalar, dims_array, true, acl_div);
476+
GGML_CANN_CALL_ACLNN_OP(ctx, Div, acl_src, acl_div, acl_dst);
477+
ggml_cann_release_resources(ctx, dims_array, p_scalar, acl_src, acl_dst, acl_div);
478+
}
479+
451480
void ggml_cann_group_norm(ggml_backend_cann_context & ctx, ggml_tensor * dst) {
452481
ggml_tensor * src = dst->src[0];
453482

ggml/src/ggml-cann/aclnn_ops.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include <aclnnop/aclnn_cos.h>
4747
#include <aclnnop/aclnn_log.h>
4848
#include <aclnnop/aclnn_sign.h>
49+
#include <aclnnop/aclnn_norm.h>
4950
#include "acl_tensor.h"
5051
#include "common.h"
5152

@@ -187,6 +188,29 @@ void ggml_cann_argsort(ggml_backend_cann_context & ctx, ggml_tensor * dst);
187188
*/
188189
void ggml_cann_norm(ggml_backend_cann_context & ctx, ggml_tensor * dst);
189190

191+
/**
192+
* @brief Computes the L2 Normalization for a ggml tensor using the CANN
193+
* backend.
194+
*
195+
* @details This function applies the L2 Normalization operation on the
196+
* input tensor `src` and stores the result in the destination tensor
197+
* `dst`. L2 Normalization scales the input tensor such that the
198+
* L2 norm along the specified dimension equals 1. This operation
199+
* is commonly used in neural networks for feature normalization
200+
* and vector scaling.
201+
* The operation is defined as:
202+
* \f[
203+
* \text{out} = \frac{x}{\sqrt{\sum{x^2}}}
204+
* \f]
205+
* The normalization is performed along the last dimension by default.
206+
*
207+
* @param ctx The CANN context used for operations.
208+
* @param dst The destination tensor where the normalized values will be stored.
209+
* @attention The normalization is performed along the last dimension of the
210+
* input tensor by default.
211+
*/
212+
void ggml_cann_l2_norm(ggml_backend_cann_context & ctx, ggml_tensor * dst);
213+
190214
/**
191215
* @brief Computes the Group Normalization for a ggml tensor using the CANN
192216
* backend.

ggml/src/ggml-cann/ggml-cann.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,6 +1777,9 @@ static bool ggml_cann_compute_forward(ggml_backend_cann_context & ctx, struct gg
17771777
case GGML_OP_GROUP_NORM:
17781778
ggml_cann_group_norm(ctx, dst);
17791779
break;
1780+
case GGML_OP_L2_NORM:
1781+
ggml_cann_l2_norm(ctx, dst);
1782+
break;
17801783
case GGML_OP_CONCAT:
17811784
ggml_cann_concat(ctx, dst);
17821785
break;
@@ -2515,6 +2518,7 @@ static bool ggml_backend_cann_supports_op(ggml_backend_dev_t dev, const ggml_ten
25152518
// value of paddingW should be at most half of kernelW
25162519
return (p0 <= (k0 / 2)) && (p1 <= (k1 / 2));
25172520
}
2521+
case GGML_OP_L2_NORM:
25182522
case GGML_OP_DUP:
25192523
case GGML_OP_SUM:
25202524
case GGML_OP_IM2COL:

0 commit comments

Comments
 (0)