Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / InstanceNorm.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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 "kernels/InstanceNorm.h"
18
19 #include "kernels/Utils.h"
20
21 #include <tensorflow/lite/kernels/internal/common.h>
22 #include <cmath>
23
24 namespace luci_interpreter
25 {
26 namespace kernels
27 {
28
29 InstanceNorm::InstanceNorm(const Tensor *input, const Tensor *gamma, const Tensor *beta,
30                            Tensor *output, const InstanceNormParams &params)
31   : KernelWithParams<InstanceNormParams>({input, gamma, beta}, {output}, params)
32 {
33 }
34
35 void InstanceNorm::configure()
36 {
37   LUCI_INTERPRETER_CHECK(input()->shape().num_dims() == 4);
38   LUCI_INTERPRETER_CHECK(input()->element_type() == output()->element_type());
39   LUCI_INTERPRETER_CHECK(gamma()->element_type() == input()->element_type());
40   LUCI_INTERPRETER_CHECK(gamma()->shape().num_dims() == 1);
41   LUCI_INTERPRETER_CHECK(gamma()->shape().dim(0) == input()->shape().dim(3) ||
42                          gamma()->shape().dim(0) == 1);
43   LUCI_INTERPRETER_CHECK(beta()->element_type() == input()->element_type());
44   LUCI_INTERPRETER_CHECK(beta()->shape().num_dims() == 1);
45   LUCI_INTERPRETER_CHECK(beta()->shape().dim(0) == input()->shape().dim(3) ||
46                          beta()->shape().dim(0) == 1);
47   // TODO: enable it only if kernel with dynamic shapes
48   output()->resize(input()->shape());
49 }
50
51 void InstanceNorm::execute() const
52 {
53   switch (input()->element_type())
54   {
55     case DataType::FLOAT32:
56       evalFloat();
57       break;
58     default:
59       assert(false && "Unsupported type.");
60   }
61 }
62
63 void InstanceNorm::evalFloat() const
64 {
65   float activation_min, activation_max;
66   calculateActivationRange(params().activation, &activation_min, &activation_max);
67   auto input_shape = getTensorShape(input());
68   auto output_shape = getTensorShape(output());
69   const int32_t batches = tflite::MatchingDim(input_shape, 0, output_shape, 0);
70   const int32_t heights = tflite::MatchingDim(input_shape, 1, output_shape, 1);
71   const int32_t widths = tflite::MatchingDim(input_shape, 2, output_shape, 2);
72   const int32_t channels = tflite::MatchingDim(input_shape, 3, output_shape, 3);
73   const float *input_data = getTensorData<float>(input());
74   const float *gamma_data = getTensorData<float>(gamma());
75   auto gamma_shape = getTensorShape(gamma());
76   bool single_gamma = gamma_shape.DimensionsCount() == 1 && gamma_shape.Dims(0) == 1;
77   const float *beta_data = getTensorData<float>(beta());
78   auto beta_shape = getTensorShape(beta());
79   bool single_beta = beta_shape.DimensionsCount() == 1 && beta_shape.Dims(0) == 1;
80   float *output_data = getTensorData<float>(output());
81   for (int32_t batch = 0; batch < batches; batch++)
82   {
83     for (int32_t channel = 0; channel < channels; channel++)
84     {
85       double sum = 0.0f;
86       double square_sum = 0.0f;
87       int32_t size = heights * widths;
88       for (int32_t height = 0; height < heights; height++)
89       {
90         for (int32_t width = 0; width < widths; width++)
91         {
92           double input_val = input_data[tflite::Offset(input_shape, batch, height, width, channel)];
93           sum += input_val;
94           square_sum += (input_val * input_val);
95         }
96       }
97       double mean = sum / size;
98       double var = square_sum / size - mean * mean;
99
100       double gamma = single_gamma ? gamma_data[0] : gamma_data[channel];
101       double beta = single_beta ? beta_data[0] : beta_data[channel];
102       double a = gamma / (std::sqrt(var + params().epsilon));
103       double b = -mean * a + beta;
104
105       for (int32_t height = 0; height < heights; height++)
106       {
107         for (int32_t width = 0; width < widths; width++)
108         {
109           double input_value =
110             input_data[tflite::Offset(output_shape, batch, height, width, channel)];
111           double output_value = input_value * a + b;
112           output_data[tflite::Offset(output_shape, batch, height, width, channel)] =
113             tflite::ActivationFunctionWithMinMax((float)output_value, activation_min,
114                                                  activation_max);
115         }
116       }
117     }
118   }
119 }
120
121 } // namespace kernels
122 } // namespace luci_interpreter