blob: e0eb5cf2020a5f8199305f9733a0127d556754d7 [file] [log] [blame]
Michalis Spyrou22f917c2019-05-21 13:30:10 +01001/*
Viet-Hoa Do47370942023-11-13 17:20:45 +00002 * Copyright (c) 2019-2020, 2023 Arm Limited.
Michalis Spyrou22f917c2019-05-21 13:30:10 +01003 *
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 */
Michalis Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEDepthToSpaceLayerKernel.h"
Michalis Spyrou22f917c2019-05-21 13:30:10 +010025
Viet-Hoa Do47370942023-11-13 17:20:45 +000026#include "arm_compute/core/CoreTypes.h"
Michalis Spyrou22f917c2019-05-21 13:30:10 +010027#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
Michalis Spyrou22f917c2019-05-21 13:30:10 +010029#include "arm_compute/core/Types.h"
Michalis Spyrou22f917c2019-05-21 13:30:10 +010030#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031#include "arm_compute/core/Validate.h"
32
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/helpers/AutoConfiguration.h"
34#include "src/core/helpers/WindowHelpers.h"
Viet-Hoa Do47370942023-11-13 17:20:45 +000035#include "src/cpu/kernels/depth_to_space/list.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010036
Michalis Spyrou22f917c2019-05-21 13:30:10 +010037#include <cstdint>
38
Michalis Spyrou22f917c2019-05-21 13:30:10 +010039namespace arm_compute
40{
41namespace
42{
43Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, int32_t block_shape)
44{
45 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas33843562019-12-10 13:33:18 +000046 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Michalis Spyrou22f917c2019-05-21 13:30:10 +010047 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
48 ARM_COMPUTE_RETURN_ERROR_ON(block_shape < 2);
49
50 const DataLayout data_layout = input->data_layout();
51 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
52 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_channel] % (block_shape * block_shape) != 0);
53 // Validate output if initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010054 if (output->total_size() != 0)
Michalis Spyrou22f917c2019-05-21 13:30:10 +010055 {
56 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
57 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010058 ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_width] !=
59 (block_shape * input->tensor_shape()[idx_width]));
60 ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_height] !=
61 (block_shape * input->tensor_shape()[idx_height]));
Michalis Spyrou22f917c2019-05-21 13:30:10 +010062 ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 4);
63 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
64 }
65
66 return Status{};
67}
68} // namespace
69
70NEDepthToSpaceLayerKernel::NEDepthToSpaceLayerKernel()
Viet-Hoa Do47370942023-11-13 17:20:45 +000071 : _input(nullptr),
72 _output(nullptr),
73 _block_shape(),
74 _data_layout(DataLayout::UNKNOWN),
75 _split_dimension(Window::DimY)
Michalis Spyrou22f917c2019-05-21 13:30:10 +010076{
77}
78
79void NEDepthToSpaceLayerKernel::configure(const ITensor *input, ITensor *output, int32_t block_shape)
80{
81 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Viet-Hoa Do47370942023-11-13 17:20:45 +000082 TensorShape output_shape = misc::shape_calculator::compute_depth_to_space_shape(
83 input->info()->tensor_shape(), input->info()->data_layout(), block_shape);
Michalis Spyrou22f917c2019-05-21 13:30:10 +010084 // Output auto inizialitation if not yet initialized
85 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
86
87 // Perform validation step
88 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), block_shape));
89
90 _input = input;
91 _output = output;
92 _block_shape = block_shape;
Georgios Pinitas0e4556c2019-12-20 10:29:12 +000093 _data_layout = input->info()->data_layout();
Michalis Spyrou22f917c2019-05-21 13:30:10 +010094
Viet-Hoa Do47370942023-11-13 17:20:45 +000095 constexpr size_t dim_b = 3;
96 const auto dim_h = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT);
97 const auto dim_w = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH);
98 const auto dim_c = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
99
100 ARM_COMPUTE_ERROR_ON(get_data_layout_dimension_index(_data_layout, DataLayoutDimension::BATCHES) != dim_b);
101
Michalis Spyrou22f917c2019-05-21 13:30:10 +0100102 // Configure kernel window
Viet-Hoa Do47370942023-11-13 17:20:45 +0000103 Steps steps;
104 steps.set(dim_h, block_shape);
105 steps.set(dim_w, block_shape);
106 steps.set(dim_c, output->info()->dimension(dim_c));
107
108 Window win = calculate_max_window(*output->info(), steps);
Michalis Spyrou22f917c2019-05-21 13:30:10 +0100109 ICPPKernel::configure(win);
Viet-Hoa Do47370942023-11-13 17:20:45 +0000110
111 const auto num_batches = input->info()->tensor_shape().total_size_upper(dim_b);
112 if (num_batches > 1)
113 {
114 _split_dimension = dim_b;
115 }
116 else
117 {
118 _split_dimension = dim_h;
119 }
Michalis Spyrou22f917c2019-05-21 13:30:10 +0100120}
121
122Status NEDepthToSpaceLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, int32_t block_shape)
123{
124 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
125 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, block_shape));
126 return Status{};
127}
128
Viet-Hoa Do47370942023-11-13 17:20:45 +0000129size_t NEDepthToSpaceLayerKernel::get_split_dimension() const
130{
131 return _split_dimension;
132}
133
Michalis Spyrou22f917c2019-05-21 13:30:10 +0100134void NEDepthToSpaceLayerKernel::run(const Window &window, const ThreadInfo &info)
135{
136 ARM_COMPUTE_UNUSED(info);
137 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
138 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window);
139
Viet-Hoa Do47370942023-11-13 17:20:45 +0000140 const auto *input_info = _input->info();
141 const auto *output_info = _output->info();
Michalis Spyrou22f917c2019-05-21 13:30:10 +0100142
Viet-Hoa Do47370942023-11-13 17:20:45 +0000143 const auto element_size = input_info->element_size();
144 const auto &input_strides = input_info->strides_in_bytes();
145 const auto &output_strides = output_info->strides_in_bytes();
Michalis Spyrou22f917c2019-05-21 13:30:10 +0100146
Viet-Hoa Do47370942023-11-13 17:20:45 +0000147 const auto &input_shape = input_info->tensor_shape();
Michalis Spyrou22f917c2019-05-21 13:30:10 +0100148
Viet-Hoa Do47370942023-11-13 17:20:45 +0000149 const uintptr_t k_input_strides[] = {input_strides[0], input_strides[1], input_strides[2], input_strides[3]};
150 const uintptr_t k_output_strides[] = {output_strides[0], output_strides[1], output_strides[2], output_strides[3]};
151
152 const uint8_t *k_input_ptr = _input->buffer();
153 uint8_t *k_output_ptr = //
154 _output->buffer() + //
155 window[3].start() * output_strides[3] + //
156 window[2].start() * output_strides[2] + //
157 window[1].start() * output_strides[1] + //
158 window[0].start() * output_strides[0];
159
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100160 if (_data_layout == DataLayout::NCHW)
Michalis Spyrou22f917c2019-05-21 13:30:10 +0100161 {
Viet-Hoa Do47370942023-11-13 17:20:45 +0000162 ARM_COMPUTE_ERROR_ON_MSG(window[2].start() != 0 || window[2].end() != window[2].step(),
163 "The window cannot be splitted in channel dimension");
Michalis Spyrou22f917c2019-05-21 13:30:10 +0100164
Viet-Hoa Do47370942023-11-13 17:20:45 +0000165 const uintptr_t k_input_shape[] = {
166 window.num_iterations(0), //
167 window.num_iterations(1), //
168 input_shape[2], // The window cannot be splitted in channel dimension.
169 window.num_iterations(3) //
170 };
171
172 k_input_ptr += window[3].start() * input_strides[3] + //
173 window[2].start() * _block_shape * _block_shape * input_strides[2] + //
174 (window[1].start() / _block_shape) * input_strides[1] + //
175 (window[0].start() / _block_shape) * input_strides[0];
176
177 cpu::depth_to_space_nchw_any( //
178 k_input_ptr, k_output_ptr, //
179 k_input_shape, k_input_strides, k_output_strides, //
180 element_size, _block_shape);
Michalis Spyrou22f917c2019-05-21 13:30:10 +0100181 }
182 else
183 {
Viet-Hoa Do47370942023-11-13 17:20:45 +0000184 ARM_COMPUTE_ERROR_ON_MSG(window[0].start() != 0 || window[0].end() != window[0].step(),
185 "The window cannot be splitted in channel dimension");
Michalis Spyrou22f917c2019-05-21 13:30:10 +0100186
Viet-Hoa Do47370942023-11-13 17:20:45 +0000187 const uintptr_t k_input_shape[] = {
188 input_shape[0], // The window cannot be splitted in channel dimension.
189 window.num_iterations(1), //
190 window.num_iterations(2), //
191 window.num_iterations(3) //
192 };
193
194 k_input_ptr += window[3].start() * input_strides[3] + //
195 (window[2].start() / _block_shape) * input_strides[2] + //
196 (window[1].start() / _block_shape) * input_strides[1] + //
197 window[0].start() * _block_shape * _block_shape * input_strides[0];
198
199 cpu::depth_to_space_nhwc_any( //
200 k_input_ptr, k_output_ptr, //
201 k_input_shape, k_input_strides, k_output_strides, //
202 element_size, _block_shape);
Michalis Spyrou22f917c2019-05-21 13:30:10 +0100203 }
204}
205} // namespace arm_compute