ONE - On-device Neural Engine
Loading...
Searching...
No Matches
L2Pool2D.cpp
Go to the documentation of this file.
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 "PALL2Pool2D.h"
23
24#include <stdexcept>
25
26namespace luci_interpreter
27{
28
29namespace kernels
30{
31
32L2Pool2D::L2Pool2D(const Tensor *input, Tensor *output, const Pool2DParams &params)
33 : KernelWithParams<Pool2DParams>({input}, {output}, params)
34{
35}
36
38{
39 LUCI_INTERPRETER_CHECK(input()->shape().num_dims() == 4);
40 LUCI_INTERPRETER_CHECK(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 LUCI_INTERPRETER_CHECK(input()->element_type() == DataType::FLOAT32);
59 output()->resize({batches, out_height, out_width, channels_out});
60}
61
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 luci_interpreter_pal::L2Pool(op_params, getTensorShape(input()),
79 getTensorData<float>(input()), getTensorShape(output()),
80 getTensorData<float>(output()));
81 break;
82 default:
83 throw std::runtime_error("luci-intp L2Pool2D Unsupported type.");
84 }
85}
86
87} // namespace kernels
88} // namespace luci_interpreter
const Pool2DParams & params() const
Definition Kernel.h:67
int32_t dim(int i) const
Definition Tensor.h:41
void resize(const Shape &new_shape)
Definition Tensor.cpp:56
const Shape & shape() const
Definition Tensor.h:107
const Tensor * input() const
Definition L2Pool2D.h:35
void execute() const override
Definition L2Pool2D.cpp:62
L2Pool2D(const Tensor *input, Tensor *output, const Pool2DParams &params)
Definition L2Pool2D.cpp:32
#define LUCI_INTERPRETER_CHECK(cond)
Definition Utils.h:36
int32_t computePadding(int32_t stride, int32_t dilation_rate, int32_t in_size, int32_t filter_size, int32_t out_size)
Definition Utils.h:41
tflite::RuntimeShape getTensorShape(const Tensor *tensor)
Definition Utils.h:194
void calculateActivationRange(Activation activation, T *activation_min, T *activation_max)
Definition Utils.cpp:52
int32_t computeOutputSize(Padding padding, int32_t image_size, int32_t filter_size, int32_t stride, int32_t dilation_rate=1)
Definition Utils.h:59