blob: b49c5ee344b2d3ffeff520f25fb9cf61ac3f673a [file] [log] [blame]
Manuel Bottini5b7d5372019-05-17 14:04:22 +01001/*
Georgios Pinitasddb93bb2020-10-02 16:38:59 +01002 * Copyright (c) 2019-2020 Arm Limited.
Manuel Bottini5b7d5372019-05-17 14:04:22 +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/NESpaceToDepthLayerKernel.h"
Manuel Bottini5b7d5372019-05-17 14:04:22 +010025
26#include "arm_compute/core/Helpers.h"
27#include "arm_compute/core/ITensor.h"
Manuel Bottini5b7d5372019-05-17 14:04:22 +010028#include "arm_compute/core/Types.h"
Manuel Bottini5b7d5372019-05-17 14:04:22 +010029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010030#include "arm_compute/core/Validate.h"
31
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/core/helpers/AutoConfiguration.h"
33#include "src/core/helpers/WindowHelpers.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010034#include "src/core/NEON/wrapper/wrapper.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035
Manuel Bottini5b7d5372019-05-17 14:04:22 +010036#include <arm_neon.h>
37#include <cstdint>
38
39using namespace arm_compute::misc::shape_calculator;
40
41namespace arm_compute
42{
43namespace
44{
45Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, int32_t block_shape)
46{
47 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas33843562019-12-10 13:33:18 +000048 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Manuel Bottini5b7d5372019-05-17 14:04:22 +010049 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
50
51 ARM_COMPUTE_RETURN_ERROR_ON(block_shape < 1);
52
53 // Validate output if initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010054 if (output->total_size() != 0)
Manuel Bottini5b7d5372019-05-17 14:04:22 +010055 {
56 const DataLayout data_layout = input->data_layout();
57 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
58 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
59 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
60 const int idx_batch = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES);
61 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_width] % block_shape != 0);
62 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_height] % block_shape != 0);
63 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_batch] != output->tensor_shape()[idx_batch]);
64 ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_channel] % (block_shape * block_shape) != 0);
65 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().total_size() != output->tensor_shape().total_size());
66 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
67 }
68
69 return Status{};
70}
71} // namespace
72
73NESpaceToDepthLayerKernel::NESpaceToDepthLayerKernel()
Georgios Pinitas0e4556c2019-12-20 10:29:12 +000074 : _input(nullptr), _output(nullptr), _block_shape(), _data_layout(DataLayout::UNKNOWN)
Manuel Bottini5b7d5372019-05-17 14:04:22 +010075{
76}
77
78void NESpaceToDepthLayerKernel::configure(const ITensor *input, ITensor *output, int32_t block_shape)
79{
80 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
81
82 TensorShape output_shape = misc::shape_calculator::compute_space_to_depth_shape(input->info(), block_shape);
83 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type());
84
85 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), block_shape));
86
87 _input = input;
88 _block_shape = block_shape;
89 _output = output;
Pablo Tello62bdd8c2019-12-19 15:27:34 +000090 _data_layout = input->info()->data_layout();
Manuel Bottini5b7d5372019-05-17 14:04:22 +010091
92 // Configure kernel window
93 Window win = calculate_max_window(*output->info(), Steps());
94 INEKernel::configure(win);
95}
96
97Status NESpaceToDepthLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, int32_t block_shape)
98{
99 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, block_shape));
100 return Status{};
101}
102
103void NESpaceToDepthLayerKernel::run(const Window &window, const ThreadInfo &info)
104{
105 ARM_COMPUTE_UNUSED(info);
106 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
107 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window);
108
Georgios Pinitas0e4556c2019-12-20 10:29:12 +0000109 const int channel_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL);
110 const int element_size = _input->info()->element_size();
Manuel Bottini5b7d5372019-05-17 14:04:22 +0100111
112 const size_t channel_size = _input->info()->dimension(channel_idx);
113
114 Window slice_out = window.first_slice_window_3D();
115
116 int batch_id = 0;
117
118 // Main loop for NCHW and NHWC
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100119 if (_data_layout == DataLayout::NCHW)
Manuel Bottini5b7d5372019-05-17 14:04:22 +0100120 {
121 do
122 {
123 Iterator out(_output, slice_out);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100124 execute_window_loop(
125 slice_out,
126 [&](const Coordinates &id)
127 {
128 const size_t channel_id = id.z();
129 const size_t in_x = id.x() * _block_shape + (channel_id / channel_size) % _block_shape;
130 const size_t in_y = id.y() * _block_shape + (channel_id / channel_size) / _block_shape;
131 const int z = channel_id % channel_size;
132 Coordinates input_coords{in_x, in_y, z, batch_id};
133 memcpy(out.ptr(), _input->ptr_to_element(input_coords), element_size);
134 },
135 out);
Manuel Bottini5b7d5372019-05-17 14:04:22 +0100136 ++batch_id;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100137 } while (window.slide_window_slice_3D(slice_out));
Manuel Bottini5b7d5372019-05-17 14:04:22 +0100138 }
139 else
140 {
141 do
142 {
143 Iterator out(_output, slice_out);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100144 execute_window_loop(
145 slice_out,
146 [&](const Coordinates &id)
147 {
148 const size_t channel_id = id.x();
149 const size_t in_x = id.y() * _block_shape + (channel_id / channel_size) % _block_shape;
150 const size_t in_y = id.z() * _block_shape + (channel_id / channel_size) / _block_shape;
151 const int z = channel_id % channel_size;
152 Coordinates input_coords{z, in_x, in_y, batch_id};
153 memcpy(out.ptr(), _input->ptr_to_element(input_coords), element_size);
154 },
155 out);
Manuel Bottini5b7d5372019-05-17 14:04:22 +0100156 ++batch_id;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100157 } while (window.slide_window_slice_3D(slice_out));
Manuel Bottini5b7d5372019-05-17 14:04:22 +0100158 }
159}
160} // namespace arm_compute