4
4
* @copyright (c) 2023, DeepLink.
5
5
*/
6
6
7
- #include " ../common/acloprunner.hpp"
7
+ #include " ../aclnn/acl_scalar.hpp"
8
+ #include " ../aclnn/adaptor.hpp"
8
9
9
10
namespace impl {
10
11
namespace ascend {
@@ -13,8 +14,10 @@ diopiError_t diopiLayerNorm(diopiContextHandle_t ctx, diopiTensorHandle_t out, d
13
14
diopiConstTensorHandle_t input, diopiConstTensorHandle_t weight, diopiConstTensorHandle_t bias, diopiSize_t normalizedShape,
14
15
double eps) {
15
16
AscendTensor inputAt (input);
17
+ AscendTensor outAt (out);
16
18
if (0 == inputAt.numel ()) {
17
- AclOpRunner<1 , 1 >(" Fills" , ctx).addInput (out).setAttr <float >(" value" , 0 ).addOutput (out).run ();
19
+ diopiScalar_t zeroScalar = constructDiopiScalarT (outAt.dtype (), 0.0 );
20
+ DIOPI_ASCEND_CALL_ACLNN (aclnnInplaceFillScalar, ctx, out, &zeroScalar);
18
21
return diopiSuccess;
19
22
}
20
23
@@ -27,18 +30,8 @@ diopiError_t diopiLayerNorm(diopiContextHandle_t ctx, diopiTensorHandle_t out, d
27
30
const int axis = inShape.len - normalizedShape.len ;
28
31
int64_t beginDim = axis;
29
32
30
- // call acl op
31
- AclOpRunner<3 , 3 >(" LayerNorm" , ctx)
32
- .addInput (input)
33
- .addInput (weightTemp)
34
- .addInput (biasTemp)
35
- .addOutput (out)
36
- .addOutput (saveMean)
37
- .addOutput (saveInvstd)
38
- .setAttr (" begin_norm_axis" , beginDim)
39
- .setAttr (" begin_params_axis" , beginDim)
40
- .setAttr <float >(" epsilon" , eps)
41
- .run ();
33
+ // call aclnnLayerNorm
34
+ DIOPI_ASCEND_CALL_ACLNN (aclnnLayerNorm, ctx, input, normalizedShape, weightTemp, biasTemp, eps, out, saveMean, saveInvstd);
42
35
return diopiSuccess;
43
36
}
44
37
@@ -47,6 +40,7 @@ diopiError_t diopiLayerNormBackward(diopiContextHandle_t ctx, diopiTensorHandle_
47
40
diopiConstTensorHandle_t bias, diopiConstTensorHandle_t mean, diopiConstTensorHandle_t rstd, diopiSize_t normalizedShape) {
48
41
AscendTensor inputAt (input);
49
42
diopiTensorHandle_t weightTemp = createTensorIfNullptrOrConstCast (ctx, weight, normalizedShape, inputAt.dtype (), true , 1 );
43
+ diopiTensorHandle_t biasTemp = createTensorIfNullptrOrConstCast (ctx, bias, normalizedShape, inputAt.dtype (), true , 0 );
50
44
diopiTensorHandle_t gradWeightTemp = createTensorIfNullptrOrConstCast (ctx, gradWeight, normalizedShape, inputAt.dtype (), false , 0 );
51
45
diopiTensorHandle_t gradBiasTemp = createTensorIfNullptrOrConstCast (ctx, gradBias, normalizedShape, inputAt.dtype (), false , 0 );
52
46
@@ -57,16 +51,30 @@ diopiError_t diopiLayerNormBackward(diopiContextHandle_t ctx, diopiTensorHandle_
57
51
rstdAt.unsqueeze (rstdAt.dim ());
58
52
}
59
53
60
- AclOpRunner<5 , 3 >(" LayerNormGrad" , ctx)
61
- .addInput (gradOutput)
62
- .addInput (input)
63
- .addInput (rstdAt)
64
- .addInput (meanAt)
65
- .addInput (weightTemp)
66
- .addOutput (gradInput)
67
- .addOutput (gradWeightTemp)
68
- .addOutput (gradBiasTemp)
69
- .run ();
54
+ std::array<bool , 3 > gradMask = {true , true , true };
55
+ if (nullptr == gradInput) {
56
+ gradMask[0 ] = false ;
57
+ }
58
+ if (nullptr == gradWeight) {
59
+ gradMask[1 ] = false ;
60
+ }
61
+ if (nullptr == gradBias) {
62
+ gradMask[2 ] = false ;
63
+ }
64
+
65
+ DIOPI_ASCEND_CALL_ACLNN (aclnnLayerNormBackward,
66
+ ctx,
67
+ gradOutput,
68
+ inputAt,
69
+ normalizedShape,
70
+ meanAt,
71
+ rstdAt,
72
+ weightTemp,
73
+ biasTemp,
74
+ gradMask,
75
+ gradInput,
76
+ gradWeightTemp,
77
+ gradBiasTemp);
70
78
return diopiSuccess;
71
79
}
72
80
0 commit comments