Michalis Spyrou | 16934a5 | 2018-08-21 18:03:58 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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/CL/kernels/CLSpaceToBatchLayerKernel.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" |
| 30 | |
| 31 | using namespace arm_compute::misc::shape_calculator; |
| 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | namespace |
| 36 | { |
| 37 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *block_info, const ITensorInfo *padddings, const ITensorInfo *output) |
| 38 | { |
| 39 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, block_info, padddings, 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); |
| 42 | |
| 43 | // Validate output if initialized |
| 44 | if(output->total_size() != 0) |
| 45 | { |
| 46 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 47 | } |
| 48 | |
| 49 | return Status{}; |
| 50 | } |
| 51 | Status validate_arguments_static(const ITensorInfo *input, const int block_shape_x, const int block_shape_y, const Size2D &padding_left, const Size2D &padding_right, |
| 52 | const ITensorInfo *output) |
| 53 | { |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON(block_shape_x < 1 || block_shape_y < 1); |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4); |
| 57 | |
| 58 | // Validate output if initialized |
| 59 | if(output->total_size() != 0) |
| 60 | { |
Michalis Spyrou | 13a51e1 | 2018-09-18 13:09:30 +0100 | [diff] [blame] | 61 | const DataLayout data_layout = input->data_layout(); |
| 62 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 63 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 64 | const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 65 | const int idx_batch = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES); |
| 66 | ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_width] < padding_left.x() + padding_right.y()); |
| 67 | ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_width] / block_shape_x != (output->tensor_shape()[idx_width] - padding_left.x() - padding_right.y())); |
| 68 | ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_height] / block_shape_y != (output->tensor_shape()[idx_height] - padding_left.x() - padding_right.y())); |
| 69 | ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_channel] != output->tensor_shape()[idx_channel]); |
| 70 | ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_batch] % (block_shape_x * block_shape_y) != 0); |
Michalis Spyrou | 16934a5 | 2018-08-21 18:03:58 +0100 | [diff] [blame] | 71 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 72 | } |
| 73 | |
| 74 | return Status{}; |
| 75 | } |
| 76 | } // namespace |
| 77 | |
| 78 | CLSpaceToBatchLayerKernel::CLSpaceToBatchLayerKernel() |
| 79 | : _input(nullptr), _block_shape(nullptr), _paddings(nullptr), _output(nullptr) |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | void CLSpaceToBatchLayerKernel::configure(const ICLTensor *input, const ICLTensor *block_shape, const ICLTensor *paddings, ICLTensor *output) |
| 84 | { |
| 85 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 86 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), block_shape->info(), paddings->info(), output->info())); |
| 87 | |
| 88 | _input = input; |
| 89 | _block_shape = block_shape; |
| 90 | _paddings = paddings; |
| 91 | _output = output; |
| 92 | |
Michalis Spyrou | 13a51e1 | 2018-09-18 13:09:30 +0100 | [diff] [blame] | 93 | const DataLayout data_layout = input->info()->data_layout(); |
| 94 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 95 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 96 | const int idx_batch = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES); |
| 97 | |
Michalis Spyrou | 16934a5 | 2018-08-21 18:03:58 +0100 | [diff] [blame] | 98 | // Create kernel |
| 99 | CLBuildOptions build_opts; |
| 100 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
Michalis Spyrou | 13a51e1 | 2018-09-18 13:09:30 +0100 | [diff] [blame] | 101 | build_opts.add_option("-DWIDTH_OUT=" + support::cpp11::to_string(output->info()->dimension(idx_width))); |
| 102 | build_opts.add_option("-DHEIGHT_OUT=" + support::cpp11::to_string(output->info()->dimension(idx_height))); |
| 103 | build_opts.add_option("-DBATCH_SIZE=" + support::cpp11::to_string(output->info()->dimension(idx_batch))); |
| 104 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("space_to_batch_" + lower_string(string_from_data_layout(input->info()->data_layout())), build_opts.options())); |
Michalis Spyrou | 16934a5 | 2018-08-21 18:03:58 +0100 | [diff] [blame] | 105 | |
| 106 | // Configure kernel window |
| 107 | Window win = calculate_max_window(*output->info(), Steps()); |
| 108 | ICLKernel::configure_internal(win); |
| 109 | } |
| 110 | |
| 111 | void CLSpaceToBatchLayerKernel::configure(const ICLTensor *input, const int block_shape_x, const int block_shape_y, const Size2D &padding_left, const Size2D &padding_right, |
| 112 | ICLTensor *output) |
| 113 | { |
| 114 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 115 | |
| 116 | TensorShape output_shape = misc::shape_calculator::compute_space_to_batch_shape(input->info(), block_shape_x, block_shape_y, padding_left, padding_right); |
| 117 | auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type()); |
| 118 | |
| 119 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_static(input->info(), block_shape_x, block_shape_y, padding_left, padding_right, output->info())); |
| 120 | |
| 121 | _input = input; |
| 122 | _output = output; |
| 123 | |
Michalis Spyrou | 13a51e1 | 2018-09-18 13:09:30 +0100 | [diff] [blame] | 124 | const DataLayout data_layout = input->info()->data_layout(); |
| 125 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 126 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 127 | const int idx_batch = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES); |
| 128 | |
Michalis Spyrou | 16934a5 | 2018-08-21 18:03:58 +0100 | [diff] [blame] | 129 | // Create kernel |
| 130 | CLBuildOptions build_opts; |
| 131 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
Michalis Spyrou | 13a51e1 | 2018-09-18 13:09:30 +0100 | [diff] [blame] | 132 | build_opts.add_option("-DWIDTH_OUT=" + support::cpp11::to_string(output->info()->dimension(idx_width))); |
| 133 | build_opts.add_option("-DHEIGHT_OUT=" + support::cpp11::to_string(output->info()->dimension(idx_height))); |
| 134 | build_opts.add_option("-DBATCH_SIZE=" + support::cpp11::to_string(output->info()->dimension(idx_batch))); |
Michalis Spyrou | 16934a5 | 2018-08-21 18:03:58 +0100 | [diff] [blame] | 135 | build_opts.add_option("-DBLOCK_SHAPE_X=" + support::cpp11::to_string(block_shape_x)); |
| 136 | build_opts.add_option("-DBLOCK_SHAPE_Y=" + support::cpp11::to_string(block_shape_y)); |
Michalis Spyrou | edf26ea | 2018-11-21 14:17:42 +0000 | [diff] [blame^] | 137 | build_opts.add_option("-DPAD_LEFT_X=" + support::cpp11::to_string(padding_left.x())); |
| 138 | build_opts.add_option("-DPAD_RIGHT_X=" + support::cpp11::to_string(padding_right.x())); |
| 139 | build_opts.add_option("-DPAD_LEFT_Y=" + support::cpp11::to_string(padding_left.y())); |
| 140 | build_opts.add_option("-DPAD_RIGHT_Y=" + support::cpp11::to_string(padding_right.y())); |
Michalis Spyrou | 13a51e1 | 2018-09-18 13:09:30 +0100 | [diff] [blame] | 141 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("space_to_batch_static_" + lower_string(string_from_data_layout(input->info()->data_layout())), build_opts.options())); |
Michalis Spyrou | 16934a5 | 2018-08-21 18:03:58 +0100 | [diff] [blame] | 142 | |
| 143 | // Configure kernel window |
| 144 | Window win = calculate_max_window(*output->info(), Steps()); |
| 145 | ICLKernel::configure_internal(win); |
| 146 | } |
| 147 | |
| 148 | Status CLSpaceToBatchLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *block_shape, const ITensorInfo *paddings, const ITensorInfo *output) |
| 149 | { |
| 150 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, block_shape, paddings, output)); |
| 151 | return Status{}; |
| 152 | } |
| 153 | Status CLSpaceToBatchLayerKernel::validate(const ITensorInfo *input, const int block_shape_x, const int block_shape_y, const Size2D &padding_left, const Size2D &padding_right, |
| 154 | const ITensorInfo *output) |
| 155 | { |
| 156 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_static(input, block_shape_x, block_shape_y, padding_left, padding_right, output)); |
| 157 | return Status{}; |
| 158 | } |
| 159 | |
| 160 | void CLSpaceToBatchLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 161 | { |
| 162 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 163 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 164 | |
| 165 | Window slice_out = window.first_slice_window_3D(); |
| 166 | |
| 167 | Window slice_in = window.first_slice_window_4D(); |
| 168 | slice_in.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 169 | slice_in.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 170 | slice_in.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 171 | slice_in.set(3, Window::Dimension(0, 0, 0)); |
| 172 | |
| 173 | Window vector_slice = window.first_slice_window_1D(); |
| 174 | vector_slice.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 175 | |
| 176 | Window padding_slice = window.first_slice_window_2D(); |
| 177 | padding_slice.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 178 | padding_slice.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 179 | |
| 180 | int batch_id = 0; |
| 181 | do |
| 182 | { |
| 183 | unsigned int idx = 0; |
| 184 | add_4D_tensor_argument(idx, _input, slice_in); |
| 185 | if(_paddings != nullptr && _block_shape != nullptr) |
| 186 | { |
| 187 | add_2D_tensor_argument(idx, _paddings, padding_slice); |
| 188 | add_1D_tensor_argument(idx, _block_shape, vector_slice); |
| 189 | } |
| 190 | add_argument(idx, batch_id); |
| 191 | add_3D_tensor_argument(idx, _output, slice_out); |
| 192 | enqueue(queue, *this, slice_out); |
| 193 | ++batch_id; |
| 194 | } |
| 195 | while(window.slide_window_slice_3D(slice_out)); |
| 196 | } |
| 197 | } // namespace arm_compute |