dc0fec9c7bce6962070e6654b5bebdb0caadf684
[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  * Copyright 2019 The TensorFlow Authors. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *    http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "kernels/InstanceNorm.h"
19
20 #include "kernels/Utils.h"
21
22 #include <tensorflow/lite/kernels/internal/common.h>
23 #include <cmath>
24
25 namespace luci_interpreter
26 {
27 namespace kernels
28 {
29
30 InstanceNorm::InstanceNorm(const Tensor *input, const Tensor *gamma, const Tensor *beta,
31                            Tensor *output, const InstanceNormParams &params)
32   : KernelWithParams<InstanceNormParams>({input, gamma, beta}, {output}, params)
33 {
34 }
35
36 void InstanceNorm::configure()
37 {
38   LUCI_INTERPRETER_CHECK(input()->shape().num_dims() == 4);
39   LUCI_INTERPRETER_CHECK(input()->element_type() == output()->element_type());
40   LUCI_INTERPRETER_CHECK(gamma()->element_type() == input()->element_type());
41   LUCI_INTERPRETER_CHECK(gamma()->shape().num_dims() == 1);
42   LUCI_INTERPRETER_CHECK(gamma()->shape().dim(0) == input()->shape().dim(3) ||
43                          gamma()->shape().dim(0) == 1);
44   LUCI_INTERPRETER_CHECK(beta()->element_type() == input()->element_type());
45   LUCI_INTERPRETER_CHECK(beta()->shape().num_dims() == 1);
46   LUCI_INTERPRETER_CHECK(beta()->shape().dim(0) == input()->shape().dim(3) ||
47                          beta()->shape().dim(0) == 1);
48   // TODO: enable it only if kernel with dynamic shapes
49   output()->resize(input()->shape());
50 }
51
52 void InstanceNorm::execute() const
53 {
54   switch (input()->element_type())
55   {
56     case DataType::FLOAT32:
57       evalFloat();
58       break;
59     default:
60       assert(false && "Unsupported type.");
61   }
62 }
63
64 void InstanceNorm::evalFloat() const
65 {
66   float activation_min, activation_max;
67   calculateActivationRange(params().activation, &activation_min, &activation_max);
68   auto input_shape = getTensorShape(input());
69   auto output_shape = getTensorShape(output());
70   const int32_t batches = tflite::MatchingDim(input_shape, 0, output_shape, 0);
71   const int32_t heights = tflite::MatchingDim(input_shape, 1, output_shape, 1);
72   const int32_t widths = tflite::MatchingDim(input_shape, 2, output_shape, 2);
73   const int32_t channels = tflite::MatchingDim(input_shape, 3, output_shape, 3);
74   const float *input_data = getTensorData<float>(input());
75   const float *gamma_data = getTensorData<float>(gamma());
76   auto gamma_shape = getTensorShape(gamma());
77   bool single_gamma = gamma_shape.DimensionsCount() == 1 && gamma_shape.Dims(0) == 1;
78   const float *beta_data = getTensorData<float>(beta());
79   auto beta_shape = getTensorShape(beta());
80   bool single_beta = beta_shape.DimensionsCount() == 1 && beta_shape.Dims(0) == 1;
81   float *output_data = getTensorData<float>(output());
82   for (int32_t batch = 0; batch < batches; batch++)
83   {
84     for (int32_t channel = 0; channel < channels; channel++)
85     {
86       double sum = 0.0f;
87       double square_sum = 0.0f;
88       int32_t size = heights * widths;
89       for (int32_t height = 0; height < heights; height++)
90       {
91         for (int32_t width = 0; width < widths; width++)
92         {
93           double input_val = input_data[tflite::Offset(input_shape, batch, height, width, channel)];
94           sum += input_val;
95           square_sum += (input_val * input_val);
96         }
97       }
98       double mean = sum / size;
99       double var = square_sum / size - mean * mean;
100
101       double gamma = single_gamma ? gamma_data[0] : gamma_data[channel];
102       double beta = single_beta ? beta_data[0] : beta_data[channel];
103       double a = gamma / (std::sqrt(var + params().epsilon));
104       double b = -mean * a + beta;
105
106       for (int32_t height = 0; height < heights; height++)
107       {
108         for (int32_t width = 0; width < widths; width++)
109         {
110           double input_value =
111             input_data[tflite::Offset(output_shape, batch, height, width, channel)];
112           double output_value = input_value * a + b;
113           output_data[tflite::Offset(output_shape, batch, height, width, channel)] =
114             tflite::ActivationFunctionWithMinMax((float)output_value, activation_min,
115                                                  activation_max);
116         }
117       }
118     }
119   }
120 }
121
122 } // namespace kernels
123 } // namespace luci_interpreter