• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020 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/fp32/local_response_norm_fp32.h"
18 #include <math.h>
19 #include "nnacl/errorcode.h"
20 
LocalResponseNorm(const float * input_ptr,int out_size,int channel,float * output_ptr,const LocalResponseNormParameter * param)21 int LocalResponseNorm(const float *input_ptr, int out_size, int channel, float *output_ptr,
22                       const LocalResponseNormParameter *param) {
23   NNACL_CHECK_NULL_RETURN_ERR(input_ptr);
24   NNACL_CHECK_NULL_RETURN_ERR(output_ptr);
25   NNACL_CHECK_NULL_RETURN_ERR(param);
26   int64_t depth_radius = param->depth_radius_;
27   float bias = param->bias_;
28   float alpha = param->alpha_;
29   float beta = param->beta_;
30 
31   for (int i = 0; i < out_size; i++) {
32     const float *in_data = input_ptr + i * channel;
33     float *out_data = output_ptr + i * channel;
34     // border_left
35     for (int j = 0; j < MSMIN(depth_radius, channel); j++) {
36       int left = MSMAX(0, j - depth_radius);
37       int right = MSMIN(channel - 1, j + depth_radius);
38       float sum = 0.0f;
39       for (int k = left; k <= right; k++) {
40         const float in_val = in_data[k];
41         sum += in_val * in_val;
42       }
43       out_data[j] = in_data[j] * (float)(powf(sum * alpha + bias, -beta));
44     }
45     // center
46     if (2 * depth_radius + 1 < channel) {
47       float tmp_sum = 0.0f;
48       for (int j = 0; j < depth_radius * 2 + 1; ++j) {
49         tmp_sum += in_data[j] * in_data[j];
50       }
51       out_data[depth_radius] = in_data[depth_radius] * (powf(tmp_sum * alpha + bias, -beta));
52       for (int j = depth_radius + 1; j < channel - depth_radius; ++j) {
53         tmp_sum -= in_data[j - depth_radius - 1] * in_data[j - depth_radius - 1];
54         tmp_sum += in_data[j + depth_radius] * in_data[j + depth_radius];
55         out_data[j] = in_data[j] * (float)(powf(tmp_sum * alpha + bias, -beta));
56       }
57     }
58     // border_right
59     for (int j = MSMAX(0, channel - depth_radius); j < channel; j++) {
60       int left = MSMAX(0, j - depth_radius);
61       int right = MSMIN(channel - 1, j + depth_radius);
62       float sum = 0.0f;
63       for (int k = left; k <= right; k++) {
64         const float in_val = in_data[k];
65         sum += in_val * in_val;
66       }
67       out_data[j] = in_data[j] * (float)(powf(sum * alpha + bias, -beta));
68     }
69   }
70   return 0;
71 }
72