blob: df631c3c039a77e3885fc08c26ccebcfeaea7221 [file] [log] [blame]
Michalis Spyrou22f917c2019-05-21 13:30:10 +01001/*
2 * Copyright (c) 2019 ARM Limited.
3 *
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/NEON/kernels/NEDepthToSpaceLayerKernel.h"
25
26#include "arm_compute/core/Helpers.h"
27#include "arm_compute/core/ITensor.h"
28#include "arm_compute/core/NEON/wrapper/wrapper.h"
29#include "arm_compute/core/Types.h"
30#include "arm_compute/core/Validate.h"
31#include "arm_compute/core/utils/misc/ShapeCalculator.h"
32#include <arm_neon.h>
33#include <cstdint>
34
35using namespace arm_compute::misc::shape_calculator;
36
37namespace arm_compute
38{
39namespace
40{
41Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, int32_t block_shape)
42{
43 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
44 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
45 ARM_COMPUTE_RETURN_ERROR_ON(block_shape < 2);
46
47 const DataLayout data_layout = input->data_layout();
48 const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
49 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_channel] % (block_shape * block_shape) != 0);
50 // Validate output if initialized
51 if(output->total_size() != 0)
52 {
53 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
54 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
55 ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_width] != (block_shape * input->tensor_shape()[idx_width]));
56 ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_height] != (block_shape * input->tensor_shape()[idx_height]));
57 ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 4);
58 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
59 }
60
61 return Status{};
62}
63} // namespace
64
65NEDepthToSpaceLayerKernel::NEDepthToSpaceLayerKernel()
66 : _input(nullptr), _output(nullptr), _block_shape()
67{
68}
69
70void NEDepthToSpaceLayerKernel::configure(const ITensor *input, ITensor *output, int32_t block_shape)
71{
72 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
73 TensorShape output_shape = compute_depth_to_space_shape(input->info(), block_shape);
74 // Output auto inizialitation if not yet initialized
75 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
76
77 // Perform validation step
78 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), block_shape));
79
80 _input = input;
81 _output = output;
82 _block_shape = block_shape;
83
84 // Configure kernel window
85 Window win = calculate_max_window(*input->info(), Steps());
86 ICPPKernel::configure(win);
87}
88
89Status NEDepthToSpaceLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, int32_t block_shape)
90{
91 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
92 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, block_shape));
93 return Status{};
94}
95
96void NEDepthToSpaceLayerKernel::run(const Window &window, const ThreadInfo &info)
97{
98 ARM_COMPUTE_UNUSED(info);
99 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
100 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window);
101
102 const int idx_channel = get_data_layout_dimension_index(_input->info()->data_layout(), DataLayoutDimension::CHANNEL);
103 const int depth_size = _input->info()->dimension(idx_channel);
104 const int r = (depth_size / (_block_shape * _block_shape));
105 const int element_size = _input->info()->element_size();
106
107 Window slice_out = window.first_slice_window_3D();
108
109 // The slice_out slice does not move
110 slice_out.set(Window::DimX, Window::Dimension(0, 0, 0));
111 slice_out.set(Window::DimY, Window::Dimension(0, 0, 0));
112 slice_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
113
114 // Main loop for NCHW and NHWC
115 if(_input->info()->data_layout() == DataLayout::NCHW)
116 {
117 Window slice_in = window.first_slice_window_2D();
118 do
119 {
120 Iterator in(_input, slice_in);
121 execute_window_loop(slice_in, [&](const Coordinates & id)
122 {
123 const int x = id.x();
124 const int y = id.y();
125
126 const int z = id.z() % r;
127 const int out_x = x * _block_shape + (id.z() / r) % _block_shape;
128 const int out_y = y * _block_shape + (id.z() / r) / _block_shape;
129 Coordinates output_coords{ out_x, out_y, z, id[3] };
130 memcpy(_output->ptr_to_element(output_coords), in.ptr(), element_size);
131 },
132 in);
133 }
134 while(window.slide_window_slice_2D(slice_in));
135 }
136 else
137 {
138 Window slice_in = window.first_slice_window_3D();
139 do
140 {
141 Iterator in(_input, slice_in);
142 execute_window_loop(slice_in, [&](const Coordinates & id)
143 {
144 const int x = id.y();
145 const int y = id.z();
146
147 const int z = id.x() % r;
148 const int out_x = x * _block_shape + (id.x() / r) % _block_shape;
149 const int out_y = y * _block_shape + (id.x() / r) / _block_shape;
150 Coordinates output_coords{ z, out_x, out_y, id[3] };
151 memcpy(_output->ptr_to_element(output_coords), in.ptr(), element_size);
152 },
153 in);
154 }
155 while(window.slide_window_slice_3D(slice_in));
156 }
157}
158} // namespace arm_compute