• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2022 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "nnacl/kernel/group_norm.h"
18 #include "nnacl/fp32/group_norm_fp32.h"
19 #include "nnacl/group_norm_parameter.h"
20 #include "nnacl/op_base.h"
21 #include "nnacl/errorcode.h"
22 #include "nnacl/tensor_c.h"
23 #include "nnacl/tensor_c_utils.h"
24 
GroupNormResize(struct KernelBase * self)25 int GroupNormResize(struct KernelBase *self) {
26   GroupNormStru *groupnorm = (GroupNormStru *)self;
27   NNACL_CHECK_NULL_RETURN_ERR(groupnorm);
28   GroupNormParameter *param = (GroupNormParameter *)groupnorm->base.param_;
29   NNACL_CHECK_NULL_RETURN_ERR(param);
30   NNACL_CHECK_FALSE(self->in_size_ < kInputSize2, NNACL_TENSOR_SIZE_INVALID);
31   NNACL_CHECK_FALSE(self->out_size_ < 1, NNACL_TENSOR_SIZE_INVALID);
32 
33   self->Release(self);
34 
35   TensorC *in0 = self->in_[0];
36   NNACL_CHECK_FALSE(in0->shape_size_ < C1NUM, NNACL_GROUP_NORM_SHAPE_SIZE_INVALID);
37   NNACL_CHECK_FALSE(in0->format_ != Format_NCHW, NNACL_GROUP_NORM_FORMAT_INVALID);
38 
39   param->unit_ = GetHeight(in0) * GetWidth(in0);
40   param->batch_ = GetBatch(in0);
41   param->channel_ = GetChannel(in0);
42   return self->Prepare(self);
43 }
44 
GroupNormPrepare(struct KernelBase * self)45 int GroupNormPrepare(struct KernelBase *self) {
46   GroupNormStru *groupnorm = (GroupNormStru *)self;
47   NNACL_CHECK_NULL_RETURN_ERR(groupnorm);
48   GroupNormParameter *param = (GroupNormParameter *)groupnorm->base.param_;
49   NNACL_CHECK_NULL_RETURN_ERR(param);
50   NNACL_CHECK_FALSE(param->num_groups_ < 0, NNACL_GROUP_NORM_NUM_GROUPS_INVALID);
51   NNACL_CHECK_FALSE(param->channel_ % param->num_groups_, NNACL_GROUP_NORM_NUM_GROUPS_INVALID);
52   NNACL_CHECK_FALSE(param->num_groups_ == 0, NNACL_GROUP_NORM_NUM_GROUPS_INVALID);
53 
54   size_t mean_var_elem_num = param->num_groups_;
55   param->mean_ = malloc(mean_var_elem_num * sizeof(float));
56   param->variance_ = malloc(mean_var_elem_num * sizeof(float));
57   if (param->mean_ == NULL || param->variance_ == NULL) {
58     self->Release(self);
59     return NNACL_MALLOC_BUFFER_FAILED;
60   }
61   return NNACL_OK;
62 }
63 
GroupNormRelease(struct KernelBase * self)64 int GroupNormRelease(struct KernelBase *self) {
65   GroupNormStru *groupnorm = (GroupNormStru *)self;
66   NNACL_CHECK_NULL_RETURN_ERR(groupnorm);
67   GroupNormParameter *param = (GroupNormParameter *)groupnorm->base.param_;
68   NNACL_CHECK_NULL_RETURN_ERR(param);
69 
70   if (param->mean_ != NULL) {
71     free(param->mean_);
72     param->mean_ = NULL;
73   }
74   if (param->variance_ != NULL) {
75     free(param->variance_);
76     param->variance_ = NULL;
77   }
78 
79   return NNACL_OK;
80 }
81 
GroupNormImpl(void * param,int task_id,float l,float r)82 int GroupNormImpl(void *param, int task_id, float l, float r) {
83   NNACL_CHECK_NULL_RETURN_ERR(param);
84   GroupNormStru *groupnorm_stru = (GroupNormStru *)param;
85   GroupNormParameter *groupnorm_param = (GroupNormParameter *)groupnorm_stru->base.param_;
86   NNACL_CHECK_NULL_RETURN_ERR(groupnorm_param);
87 
88   const void *input_data = groupnorm_stru->base.in_[0]->data_;
89   NNACL_CHECK_NULL_RETURN_ERR(input_data);
90   const void *scale_data = groupnorm_stru->base.in_[C1NUM]->data_;
91   NNACL_CHECK_NULL_RETURN_ERR(scale_data);
92   const void *offset_data = groupnorm_stru->base.in_[C2NUM]->data_;
93   NNACL_CHECK_NULL_RETURN_ERR(offset_data);
94   void *output_data = groupnorm_stru->base.out_[0]->data_;
95   NNACL_CHECK_NULL_RETURN_ERR(output_data);
96 
97   NNACL_CHECK_NULL_RETURN_ERR(groupnorm_param->mean_);
98   NNACL_CHECK_NULL_RETURN_ERR(groupnorm_param->variance_);
99 
100   int ret = GroupNormFp32(input_data, scale_data, offset_data, groupnorm_param->mean_, groupnorm_param->variance_,
101                           groupnorm_param, task_id, output_data);
102 
103   return ret;
104 }
105 
GroupNormCompute(struct KernelBase * self)106 int GroupNormCompute(struct KernelBase *self) {
107   return self->env_->ParallelLaunch(self->env_->thread_pool_, GroupNormImpl, self, self->param_->thread_num_);
108 }
109 
CreateGroupNorm(OpParameter * param,int data_type)110 KernelBase *CreateGroupNorm(OpParameter *param, int data_type) {
111   GroupNormStru *groupnorm = (GroupNormStru *)malloc(sizeof(GroupNormStru));
112   NNACL_MALLOC_CHECK_NULL_RETURN_NULL(groupnorm);
113 
114   groupnorm->base.Prepare = GroupNormPrepare;
115   groupnorm->base.Resize = GroupNormResize;
116   groupnorm->base.Release = GroupNormRelease;
117   groupnorm->base.Compute = GroupNormCompute;
118 
119   return (void *)groupnorm;
120 }
121 
122 REG_KERNEL_CREATOR(PrimType_GroupNormFusion, kNumberTypeFloat32, CreateGroupNorm);
123