Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 ARM Limited. |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 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/CL/kernels/CLBatchToSpaceLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLValidate.h" |
| 28 | #include "arm_compute/core/CL/ICLTensor.h" |
| 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 30 | #include "support/StringSupport.h" |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 31 | |
| 32 | using namespace arm_compute::misc::shape_calculator; |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | namespace |
| 36 | { |
| 37 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *block_info, const ITensorInfo *output) |
| 38 | { |
| 39 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, block_info, output); |
| 40 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(block_info, 1, DataType::S32); |
| 41 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4); |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 42 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 43 | |
| 44 | // Validate output if initialized |
| 45 | if(output->total_size() != 0) |
| 46 | { |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 4); |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 49 | } |
| 50 | |
| 51 | return Status{}; |
| 52 | } |
| 53 | Status validate_arguments_static(const ITensorInfo *input, const int block_shape_x, const int block_shape_y, const ITensorInfo *output) |
| 54 | { |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON(block_shape_x <= 0); |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON(block_shape_y <= 0); |
| 59 | |
Michalis Spyrou | 8f2cbfa | 2018-11-20 12:39:47 +0000 | [diff] [blame] | 60 | const DataLayout data_layout = input->data_layout(); |
| 61 | const int idx_batch = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES); |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_batch] % (block_shape_x * block_shape_y) != 0); |
| 63 | |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 64 | // Validate output if initialized |
| 65 | if(output->total_size() != 0) |
| 66 | { |
Michalis Spyrou | 8f2cbfa | 2018-11-20 12:39:47 +0000 | [diff] [blame] | 67 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 68 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 69 | const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 70 | ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_width] != (block_shape_x * input->tensor_shape()[idx_width])); |
Pablo Tello | 0a33a21 | 2019-04-23 12:06:19 +0100 | [diff] [blame] | 71 | ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_height] != (block_shape_y * input->tensor_shape()[idx_height])); |
Michalis Spyrou | 8f2cbfa | 2018-11-20 12:39:47 +0000 | [diff] [blame] | 72 | ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_channel] != input->tensor_shape()[idx_channel]); |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 73 | ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 4); |
| 74 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 75 | } |
| 76 | |
| 77 | return Status{}; |
| 78 | } |
| 79 | } // namespace |
| 80 | |
| 81 | CLBatchToSpaceLayerKernel::CLBatchToSpaceLayerKernel() |
| 82 | : _input(nullptr), _block_shape(nullptr), _output(nullptr) |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | void CLBatchToSpaceLayerKernel::configure(const ICLTensor *input, const ICLTensor *block_shape, ICLTensor *output) |
| 87 | { |
| 88 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 89 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), block_shape->info(), output->info())); |
| 90 | |
| 91 | _input = input; |
| 92 | _block_shape = block_shape; |
| 93 | _output = output; |
| 94 | |
Michalis Spyrou | f1addb6 | 2018-09-11 11:16:47 +0100 | [diff] [blame] | 95 | const int idx_width = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH); |
| 96 | |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 97 | // Create kernel |
| 98 | CLBuildOptions build_opts; |
| 99 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 100 | build_opts.add_option("-DBATCH_SIZE=" + support::cpp11::to_string(input->info()->dimension(3))); |
Michalis Spyrou | f1addb6 | 2018-09-11 11:16:47 +0100 | [diff] [blame] | 101 | build_opts.add_option("-DWIDTH_IN=" + support::cpp11::to_string(input->info()->dimension(idx_width))); |
| 102 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("batch_to_space_" + lower_string(string_from_data_layout(input->info()->data_layout())), build_opts.options())); |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 103 | |
| 104 | // Configure kernel window |
| 105 | Window win = calculate_max_window(*input->info(), Steps()); |
| 106 | ICLKernel::configure_internal(win); |
| 107 | } |
| 108 | |
| 109 | void CLBatchToSpaceLayerKernel::configure(const ICLTensor *input, const int32_t block_shape_x, const int32_t block_shape_y, ICLTensor *output) |
| 110 | { |
| 111 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 112 | |
| 113 | TensorShape output_shape = compute_batch_to_space_shape(input->info(), block_shape_x, block_shape_y); |
| 114 | auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type()); |
| 115 | |
| 116 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_static(input->info(), block_shape_x, block_shape_y, output->info())); |
| 117 | |
| 118 | _input = input; |
| 119 | _output = output; |
| 120 | |
Michalis Spyrou | f1addb6 | 2018-09-11 11:16:47 +0100 | [diff] [blame] | 121 | const int idx_width = get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH); |
| 122 | |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 123 | // Create kernel |
| 124 | CLBuildOptions build_opts; |
| 125 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 126 | build_opts.add_option("-DBATCH_SIZE=" + support::cpp11::to_string(input->info()->dimension(3))); |
| 127 | build_opts.add_option("-DBLOCK_SHAPE_X=" + support::cpp11::to_string(block_shape_x)); |
| 128 | build_opts.add_option("-DBLOCK_SHAPE_Y=" + support::cpp11::to_string(block_shape_y)); |
Michalis Spyrou | f1addb6 | 2018-09-11 11:16:47 +0100 | [diff] [blame] | 129 | build_opts.add_option("-DWIDTH_IN=" + support::cpp11::to_string(input->info()->dimension(idx_width))); |
| 130 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("batch_to_space_static_" + lower_string(string_from_data_layout(input->info()->data_layout())), build_opts.options())); |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 131 | |
| 132 | // Configure kernel window |
| 133 | Window win = calculate_max_window(*input->info(), Steps()); |
| 134 | ICLKernel::configure_internal(win); |
| 135 | } |
| 136 | |
| 137 | Status CLBatchToSpaceLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *block_shape, const ITensorInfo *output) |
| 138 | { |
| 139 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, block_shape, output); |
| 140 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, block_shape, output)); |
| 141 | return Status{}; |
| 142 | } |
| 143 | |
| 144 | Status CLBatchToSpaceLayerKernel::validate(const ITensorInfo *input, const int32_t block_shape_x, const int32_t block_shape_y, const ITensorInfo *output) |
| 145 | { |
| 146 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 147 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_static(input, block_shape_x, block_shape_y, output)); |
| 148 | return Status{}; |
| 149 | } |
| 150 | |
| 151 | void CLBatchToSpaceLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 152 | { |
| 153 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 154 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 155 | |
| 156 | Window slice_in = window.first_slice_window_3D(); |
| 157 | Window slice_out = window.first_slice_window_4D(); |
| 158 | |
| 159 | Window vector_slice = window.first_slice_window_1D(); |
| 160 | vector_slice.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 161 | |
| 162 | slice_out.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 163 | slice_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 164 | slice_out.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 165 | slice_out.set(3, Window::Dimension(0, 0, 0)); |
| 166 | |
| 167 | int batch_id = 0; |
| 168 | do |
| 169 | { |
| 170 | unsigned int idx = 0; |
| 171 | add_3D_tensor_argument(idx, _input, slice_in); |
| 172 | add_argument(idx, batch_id); |
| 173 | if(_block_shape != nullptr) |
| 174 | { |
| 175 | add_1D_tensor_argument(idx, _block_shape, vector_slice); |
| 176 | } |
| 177 | add_4D_tensor_argument(idx, _output, slice_out); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 178 | enqueue(queue, *this, slice_in, lws_hint()); |
Michalis Spyrou | 6a8d3b6 | 2018-08-31 10:07:09 +0100 | [diff] [blame] | 179 | |
| 180 | ++batch_id; |
| 181 | } |
| 182 | while(window.slide_window_slice_3D(slice_in)); |
| 183 | } |
| 184 | } // namespace arm_compute |