Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 1 | /* |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [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/CLDeconvolutionLayerUpsampleKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/CL/CLValidate.h" |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/CL/ICLTensor.h" |
| 30 | #include "arm_compute/core/Error.h" |
| 31 | #include "arm_compute/core/Helpers.h" |
| 32 | #include "arm_compute/core/Validate.h" |
| 33 | #include "arm_compute/core/Window.h" |
| 34 | |
| 35 | using namespace arm_compute; |
| 36 | |
| 37 | CLDeconvolutionLayerUpsampleKernel::CLDeconvolutionLayerUpsampleKernel() |
| 38 | : _input(nullptr), _output(nullptr), _inner_border(), _info() |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | Status CLDeconvolutionLayerUpsampleKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const BorderSize &inner_border, |
| 43 | const PadStrideInfo &info) |
| 44 | { |
| 45 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 46 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Michele Di Giorgio | 9fef38a | 2018-07-06 18:06:58 +0100 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 49 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 50 | |
| 51 | const DataLayout data_layout = input->data_layout(); |
| 52 | |
| 53 | const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 54 | const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 55 | const size_t idx_c = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 56 | |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(idx_w) == 0); |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(idx_h) == 0); |
Anthony Barbier | 21f67d6 | 2018-02-16 15:17:48 +0000 | [diff] [blame] | 59 | ARM_COMPUTE_RETURN_ERROR_ON(!info.padding_is_symmetric()); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 60 | |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 61 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(idx_c) != output->dimension(idx_c)); |
| 62 | for(size_t i = 3; i < Coordinates::num_max_dimensions; ++i) |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 63 | { |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(i) != output->dimension(i)); |
| 65 | } |
| 66 | |
| 67 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(inner_border.right > info.stride().first - 1, "inner_border_right must be smaller that stride_x"); |
| 68 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(inner_border.top > info.stride().second - 1, "inner_border_top must be smaller that stride_y"); |
| 69 | |
| 70 | return Status{}; |
| 71 | } |
| 72 | |
| 73 | void CLDeconvolutionLayerUpsampleKernel::configure(const ICLTensor *input, ICLTensor *output, const BorderSize &inner_border, |
| 74 | const PadStrideInfo &info) |
| 75 | { |
| 76 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 77 | |
| 78 | _input = input; |
| 79 | _output = output; |
| 80 | _inner_border = inner_border; |
| 81 | _info = info; |
| 82 | |
| 83 | // Perform validation step |
| 84 | ARM_COMPUTE_ERROR_THROW_ON(CLDeconvolutionLayerUpsampleKernel::validate(input->info(), output->info(), inner_border, info)); |
| 85 | |
| 86 | // Create kernel |
Georgios Pinitas | 793f87d | 2018-05-18 20:08:58 +0100 | [diff] [blame] | 87 | CLBuildOptions build_opts; |
| 88 | build_opts.add_option(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()))); |
| 89 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("deconvolution_upsample", build_opts.options())); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 90 | |
| 91 | constexpr unsigned int num_elems_processed_per_iteration = 1; |
| 92 | |
| 93 | // Configure kernel window |
Georgios Pinitas | 59716fa | 2018-02-13 19:57:31 +0000 | [diff] [blame] | 94 | Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration)); |
| 95 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 96 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape())); |
| 97 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 98 | ICLKernel::configure_internal(win); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void CLDeconvolutionLayerUpsampleKernel::run(const Window &window, cl::CommandQueue &queue) |
| 102 | { |
| 103 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 104 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 105 | |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 106 | const DataLayout data_layout = _input->info()->data_layout(); |
| 107 | |
| 108 | const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 109 | const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 110 | |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 111 | const int out_start_x = _info.pad().first; |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 112 | const int out_end_x = _output->info()->dimension(idx_w) - _inner_border.right - _info.pad().first + _info.stride().first - 1; |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 113 | const int out_step_x = _info.stride().first; |
| 114 | |
| 115 | const int out_start_y = _inner_border.top + _info.pad().second; |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 116 | const int out_end_y = _output->info()->dimension(idx_h) - _info.pad().second + _info.stride().second - 1; |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 117 | const int out_step_y = _info.stride().second; |
| 118 | |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 119 | switch(data_layout) |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 120 | { |
Michele Di Giorgio | ed5a492 | 2018-09-13 16:22:01 +0100 | [diff] [blame] | 121 | case DataLayout::NCHW: |
| 122 | { |
| 123 | Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 124 | |
| 125 | Window slice_out = collapsed.first_slice_window_3D(); |
| 126 | slice_out.set(Window::DimX, Window::Dimension(out_start_x, out_end_x, out_step_x)); |
| 127 | slice_out.set(Window::DimY, Window::Dimension(out_start_y, out_end_y, out_step_y)); |
| 128 | |
| 129 | Window slice_in = collapsed.first_slice_window_3D(); |
| 130 | |
| 131 | do |
| 132 | { |
| 133 | unsigned int idx = 0; |
| 134 | add_3D_tensor_argument(idx, _input, slice_in); |
| 135 | add_3D_tensor_argument(idx, _output, slice_out); |
| 136 | enqueue(queue, *this, slice_out); |
| 137 | } |
| 138 | while(collapsed.slide_window_slice_3D(slice_in) && collapsed.slide_window_slice_3D(slice_out)); |
| 139 | break; |
| 140 | } |
| 141 | case DataLayout::NHWC: |
| 142 | { |
| 143 | // NOTE: not collapsing in NHWC |
| 144 | Window slice_out = window.first_slice_window_3D(); |
| 145 | slice_out.set(Window::DimY, Window::Dimension(out_start_x, out_end_x, out_step_x)); |
| 146 | slice_out.set(Window::DimZ, Window::Dimension(out_start_y, out_end_y, out_step_y)); |
| 147 | |
| 148 | Window slice_in = window.first_slice_window_3D(); |
| 149 | |
| 150 | do |
| 151 | { |
| 152 | unsigned int idx = 0; |
| 153 | add_3D_tensor_argument(idx, _input, slice_in); |
| 154 | add_3D_tensor_argument(idx, _output, slice_out); |
| 155 | enqueue(queue, *this, slice_out); |
| 156 | } |
| 157 | while(window.slide_window_slice_3D(slice_in) && window.slide_window_slice_3D(slice_out)); |
| 158 | break; |
| 159 | } |
| 160 | default: |
| 161 | ARM_COMPUTE_ERROR("Unsupported data layout"); |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 162 | } |
Michalis Spyrou | 780db4e | 2017-11-23 09:49:51 +0000 | [diff] [blame] | 163 | } |