blob: 8348b4335e0e88722ad9901ef7c5f2ca62206574 [file] [log] [blame]
Michalis Spyrou33a69902018-02-23 15:01:52 +00001/*
Michele Di Giorgio6a1db9b2020-04-28 15:38:10 +01002 * Copyright (c) 2017-2020 ARM Limited.
Michalis Spyrou33a69902018-02-23 15:01:52 +00003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "arm_compute/core/CPP/kernels/CPPUpsampleKernel.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
29#include "arm_compute/core/TensorInfo.h"
30#include "arm_compute/core/Types.h"
31#include "arm_compute/core/Validate.h"
32#include "arm_compute/core/utils/misc/ShapeCalculator.h"
33
34#include <cstddef>
35#include <cstdint>
36
Manuel Bottinid25af672019-07-10 17:06:12 +010037namespace arm_compute
38{
Michalis Spyrou33a69902018-02-23 15:01:52 +000039CPPUpsampleKernel::CPPUpsampleKernel()
Manuel Bottinic1b76fa2019-06-17 12:04:40 +010040 : _input(nullptr), _output(nullptr), _info()
Michalis Spyrou33a69902018-02-23 15:01:52 +000041{
42}
43
44bool CPPUpsampleKernel::is_parallelisable() const
45{
46 return false;
47}
48
Manuel Bottinic1b76fa2019-06-17 12:04:40 +010049void CPPUpsampleKernel::configure(const ITensor *input, ITensor *output, const PadStrideInfo &info)
Michalis Spyrou33a69902018-02-23 15:01:52 +000050{
51 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
52
Manuel Bottinic1b76fa2019-06-17 12:04:40 +010053 _input = input;
54 _output = output;
55 _info = info;
Michalis Spyrou33a69902018-02-23 15:01:52 +000056
57 // Configure kernel window
58 Window win = calculate_max_window(*input->info(), Steps());
59
60 // The CPPUpsampleKernel doesn't need padding so update_window_and_padding() can be skipped
61 Coordinates coord;
62 coord.set_num_dimensions(output->info()->num_dimensions());
63 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
64
65 ICPPKernel::configure(win);
66}
67
68void CPPUpsampleKernel::run(const Window &window, const ThreadInfo &info)
69{
70 ARM_COMPUTE_UNUSED(info);
71 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
72 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window);
73
Manuel Bottini6e10aa32020-04-30 13:28:23 +010074 const DataLayout data_layout = _input->info()->data_layout();
75 const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
76 const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
77
Michalis Spyrou33a69902018-02-23 15:01:52 +000078 // Initialize _scaled_output buffer
Manuel Bottini6e10aa32020-04-30 13:28:23 +010079 const int width_scaled = _output->info()->dimension(idx_w);
80 const int height_scaled = _output->info()->dimension(idx_h);
81 const int stride_width = _info.stride().first;
82 const int stride_height = _info.stride().second;
83 const int start_width = _info.pad_left();
84 const int start_height = _info.pad_top();
85 const int end_width = width_scaled - _info.pad_right();
86 const int end_height = height_scaled - _info.pad_bottom();
Usama Arif2899e002019-04-16 14:32:25 +010087 const size_t element_size = _input->info()->element_size();
Michalis Spyrou33a69902018-02-23 15:01:52 +000088
Michele Di Giorgio6a1db9b2020-04-28 15:38:10 +010089 // The fill value is normally 0, but for quantized types '0' corresponds to the offset
90 switch(_output->info()->data_type())
91 {
92 case DataType::QASYMM8:
93 {
94 const uint8_t fill_value = _output->info()->quantization_info().uniform().offset;
95 std::fill_n(_output->buffer(), _output->info()->total_size(), fill_value);
96 }
97 break;
98 case DataType::QASYMM8_SIGNED:
99 {
100 const int8_t fill_value = _output->info()->quantization_info().uniform().offset;
101 std::fill_n(_output->buffer(), _output->info()->total_size(), fill_value);
102 }
103 break;
104 default:
105 std::fill_n(_output->buffer(), _output->info()->total_size(), 0);
106 }
Michalis Spyrou33a69902018-02-23 15:01:52 +0000107
108 // Create window
109 Window window_out(window);
Manuel Bottini6e10aa32020-04-30 13:28:23 +0100110 if(data_layout == DataLayout::NCHW)
111 {
112 window_out.set(Window::DimX, Window::Dimension(start_width, end_width, stride_width));
113 window_out.set(Window::DimY, Window::Dimension(start_height, end_height, stride_height));
114 }
115 else
116 {
117 window_out.set(Window::DimY, Window::Dimension(start_width, end_width, stride_width));
118 window_out.set(Window::DimZ, Window::Dimension(start_height, end_height, stride_height));
119 }
Michalis Spyrou33a69902018-02-23 15:01:52 +0000120
121 // Create iterators
122 Iterator in(_input, window);
123 Iterator out(_output, window_out);
124
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100125 execute_window_loop(window, [&](const Coordinates &)
Michalis Spyrou33a69902018-02-23 15:01:52 +0000126 {
Usama Arif2899e002019-04-16 14:32:25 +0100127 memcpy(out.ptr(), in.ptr(), element_size);
Michalis Spyrou33a69902018-02-23 15:01:52 +0000128 },
129 in, out);
130}
Manuel Bottinid25af672019-07-10 17:06:12 +0100131} // namespace arm_compute