• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2018-2020 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 "src/core/NEON/kernels/NEReorgLayerKernel.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 #include "src/core/helpers/AutoConfiguration.h"
34 #include "src/core/helpers/WindowHelpers.h"
35 
36 #include <cstddef>
37 #include <cstdint>
38 
39 namespace arm_compute
40 {
41 namespace
42 {
validate_arguments(const ITensorInfo * input,const ITensorInfo * output,int32_t stride)43 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, int32_t stride)
44 {
45     //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions.
46     ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
47     ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN);
48 
49     const size_t idx_width  = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH);
50     const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT);
51 
52     ARM_COMPUTE_RETURN_ERROR_ON(stride <= 0);
53     ARM_COMPUTE_RETURN_ERROR_ON_MSG((input->tensor_shape()[idx_width] % stride) != 0, "The width of the input tensor must be a multiple of stride");
54     ARM_COMPUTE_RETURN_ERROR_ON_MSG((input->tensor_shape()[idx_height] % stride) != 0, "The height of the input tensor must be a multiple of stride");
55 
56     // Validate output if initialized
57     if(output->total_size() != 0)
58     {
59         const TensorInfo tensor_info_output = output->clone()->set_tensor_shape(misc::shape_calculator::compute_reorg_output_shape(*input, stride));
60         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output);
61         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
62     }
63 
64     return Status{};
65 }
66 } // namespace
67 
NEReorgLayerKernel()68 NEReorgLayerKernel::NEReorgLayerKernel()
69     : _input(nullptr), _output(nullptr), _stride(1)
70 {
71 }
72 
configure(const ITensor * input,ITensor * output,int32_t stride)73 void NEReorgLayerKernel::configure(const ITensor *input, ITensor *output, int32_t stride)
74 {
75     ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
76 
77     // Output auto inizialitation if not yet initialized
78     const TensorShape output_shape = misc::shape_calculator::compute_reorg_output_shape(*input->info(), stride);
79     auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
80 
81     // Perform validation step
82     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), stride));
83 
84     _input  = input;
85     _output = output;
86     _stride = stride;
87 
88     // The NEReorgLayerKernel doesn't need padding so update_window_and_padding() can be skipped
89     output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
90 
91     // Configure kernel window
92     Window win = calculate_max_window(*output->info(), Steps());
93 
94     ICPPKernel::configure(win);
95 }
96 
validate(const ITensorInfo * input,const ITensorInfo * output,int32_t stride)97 Status NEReorgLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, int32_t stride)
98 {
99     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, stride));
100     return Status{};
101 }
102 
run(const Window & window,const ThreadInfo & info)103 void NEReorgLayerKernel::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 
109     const DataLayout data_layout = _input->info()->data_layout();
110     const size_t     idx_w       = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
111     const size_t     idx_h       = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
112     const size_t     idx_c       = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
113 
114     const unsigned int stride = _stride;
115     const unsigned int out_c  = _output->info()->tensor_shape()[idx_c] / (stride * stride);
116     const uint8_t     *in_ptr = _input->buffer();
117 
118     // Collapse
119     Window collapsed_window = window.collapse_if_possible(window, 4);
120 
121     // Create Iterator
122     Iterator out(_output, collapsed_window);
123 
124     // Perform reorg
125     execute_window_loop(collapsed_window, [&](const Coordinates & id)
126     {
127         // Get spatial coords and channels
128         const unsigned int w = id[idx_w];
129         const unsigned int h = id[idx_h];
130         const unsigned int c = id[idx_c];
131 
132         // Calculate mapping
133         const unsigned int offset     = c / out_c;
134         Coordinates        map_coords = id;
135         map_coords.set(idx_w, w * stride + offset % stride);
136         map_coords.set(idx_h, h * stride + offset / stride);
137         map_coords.set(idx_c, c % out_c);
138 
139         // Perform mapping
140         std::memcpy(out.ptr(), in_ptr + _input->info()->offset_element_in_bytes(map_coords), _input->info()->element_size());
141     },
142     out);
143 }
144 } // namespace arm_compute
145