Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / L2Pool2D.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2017 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/L2Pool2D.h"
19
20 #include "kernels/Utils.h"
21
22 #include <tensorflow/lite/kernels/internal/optimized/optimized_ops.h>
23
24 #include <stdexcept>
25
26 namespace luci_interpreter
27 {
28
29 namespace kernels
30 {
31
32 L2Pool2D::L2Pool2D(const Tensor *input, Tensor *output, const Pool2DParams &params)
33     : KernelWithParams<Pool2DParams>({input}, {output}, params)
34 {
35 }
36
37 void L2Pool2D::configure()
38 {
39   assert(input()->shape().num_dims() == 4);
40   assert(input()->element_type() == output()->element_type());
41
42   int batches = input()->shape().dim(0);
43   int height = input()->shape().dim(1);
44   int width = input()->shape().dim(2);
45   int channels_out = input()->shape().dim(3);
46
47   // Matching GetWindowedOutputSize in TensorFlow.
48   auto padding = params().padding;
49   int out_width, out_height;
50   out_width = computeOutputSize(padding, width, params().filter_width, params().stride_width, 1);
51   out_height =
52       computeOutputSize(padding, height, params().filter_height, params().stride_height, 1);
53   _padding_width =
54       computePadding(params().stride_width, 1, width, params().filter_width, out_width);
55   _padding_height =
56       computePadding(params().stride_height, 1, height, params().filter_height, out_height);
57
58   assert(input()->element_type() == DataType::FLOAT32);
59   output()->resize({batches, out_height, out_width, channels_out});
60 }
61
62 void L2Pool2D::execute() const
63 {
64   switch (input()->element_type())
65   {
66     case DataType::FLOAT32:
67       float activation_min, activation_max;
68       calculateActivationRange(params().activation, &activation_min, &activation_max);
69       tflite::PoolParams op_params;
70       op_params.stride_height = params().stride_height;
71       op_params.stride_width = params().stride_width;
72       op_params.filter_height = params().filter_height;
73       op_params.filter_width = params().filter_width;
74       op_params.padding_values.height = _padding_height;
75       op_params.padding_values.width = _padding_width;
76       op_params.float_activation_min = activation_min;
77       op_params.float_activation_max = activation_max;
78       tflite::optimized_ops::L2Pool(op_params, getTensorShape(input()),
79                                     getTensorData<float>(input()), getTensorShape(output()),
80                                     getTensorData<float>(output()));
81       break;
82     default:
83       throw std::runtime_error("Unsupported type.");
84   }
85 }
86
87 } // namespace kernels
88 } // namespace luci_interpreter