2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2017 The TensorFlow Authors. All Rights Reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "kernels/L2Pool2D.h"
20 #include "kernels/Utils.h"
22 #include <tensorflow/lite/kernels/internal/optimized/optimized_ops.h>
26 namespace luci_interpreter
32 L2Pool2D::L2Pool2D(const Tensor *input, Tensor *output, const Pool2DParams ¶ms)
33 : KernelWithParams<Pool2DParams>({input}, {output}, params)
37 void L2Pool2D::configure()
39 assert(input()->shape().num_dims() == 4);
40 assert(input()->element_type() == output()->element_type());
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);
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);
52 computeOutputSize(padding, height, params().filter_height, params().stride_height, 1);
54 computePadding(params().stride_width, 1, width, params().filter_width, out_width);
56 computePadding(params().stride_height, 1, height, params().filter_height, out_height);
58 assert(input()->element_type() == DataType::FLOAT32);
59 output()->resize({batches, out_height, out_width, channels_out});
62 void L2Pool2D::execute() const
64 switch (input()->element_type())
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()));
83 throw std::runtime_error("Unsupported type.");
87 } // namespace kernels
88 } // namespace luci_interpreter