1 /**
2 * Copyright 2023 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 log_softmaxress or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "nnacl/kernel/local_response_norm.h"
18 #include "nnacl/kernel/default_kernel_base.h"
19 #include "nnacl/fp32/local_response_norm_fp32.h"
20 #include "nnacl/tensor_c_utils.h"
21
LocalResponseNormRun(void * cdata,int task_id,float l,float r)22 int LocalResponseNormRun(void *cdata, int task_id, float l, float r) {
23 LocalResponseNormStruct *lrn = (LocalResponseNormStruct *)cdata;
24 NNACL_CHECK_NULL_RETURN_ERR(lrn);
25 LocalResponseNormParameter *param = (LocalResponseNormParameter *)lrn->base_.param_;
26 NNACL_CHECK_NULL_RETURN_ERR(param);
27
28 TensorC *input = lrn->base_.in_[FIRST_INPUT];
29 NNACL_CHECK_NULL_RETURN_ERR(input);
30 TensorC *output = lrn->base_.out_[OUTPUT_INDEX];
31 NNACL_CHECK_NULL_RETURN_ERR(output);
32 NNACL_CHECK_FALSE(input->shape_size_ != DIMENSION_4D, NNACL_LOCAL_RESPONSE_NORM_SHAPE_INVALID);
33 NNACL_CHECK_FALSE(param->depth_radius_ <= 0, NNACL_LOCAL_RESPONSE_NORM_DEPTH_RADIUS_INVALID);
34
35 float *input_ptr = (float *)input->data_;
36 NNACL_CHECK_NULL_RETURN_ERR(input_ptr);
37 float *output_ptr = (float *)output->data_;
38 NNACL_CHECK_NULL_RETURN_ERR(output_ptr);
39
40 int batch = GetBatch(input);
41 int height = GetHeight(input);
42 int width = GetWidth(input);
43 int channel = GetChannel(input);
44
45 NNACL_CHECK_INT_MUL_NOT_OVERFLOW(batch, width, NNACL_ERR);
46 int size_bw = batch * width;
47 NNACL_CHECK_INT_MUL_NOT_OVERFLOW(size_bw, height, NNACL_ERR);
48 int outer_size = size_bw * height;
49 int stride = UP_DIV(outer_size, lrn->base_.thread_nr_);
50 NNACL_CHECK_INT_MUL_NOT_OVERFLOW(stride, task_id, NNACL_ERR);
51 int start = stride * task_id;
52 int count = MSMIN(stride, outer_size - start);
53
54 NNACL_CHECK_INT_MUL_NOT_OVERFLOW(start, channel, NNACL_ERR);
55 input_ptr += start * channel;
56 output_ptr += start * channel;
57
58 return LocalResponseNorm(input_ptr, count, channel, output_ptr, param);
59 }
60
LrnCompute(KernelBase * self)61 int LrnCompute(KernelBase *self) {
62 return self->env_->ParallelLaunch(self->env_->thread_pool_, LocalResponseNormRun, self, self->thread_nr_);
63 }
64
CreateLocalResponseNorm(OpParameter * param,int data_type)65 KernelBase *CreateLocalResponseNorm(OpParameter *param, int data_type) {
66 LocalResponseNormStruct *lrn = (LocalResponseNormStruct *)malloc(sizeof(LocalResponseNormStruct));
67 NNACL_MALLOC_CHECK_NULL_RETURN_NULL(lrn);
68 memset(lrn, 0, sizeof(LocalResponseNormStruct));
69
70 lrn->base_.Prepare = DefaultPrepare1In1Out;
71 lrn->base_.Release = DefaultRelease;
72 lrn->base_.Resize = DefaultResize;
73 lrn->base_.Compute = LrnCompute;
74 return (KernelBase *)lrn;
75 }
76
77 REG_KERNEL_CREATOR(PrimType_LRN, kNumberTypeFloat32, CreateLocalResponseNorm)
78