Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 Arm Limited. |
Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +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/CLUpsampleLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
| 27 | #include "arm_compute/core/CL/CLHelpers.h" |
| 28 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/CL/CLValidate.h" |
Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/CL/ICLTensor.h" |
| 31 | #include "arm_compute/core/Error.h" |
| 32 | #include "arm_compute/core/Helpers.h" |
| 33 | #include "arm_compute/core/Validate.h" |
| 34 | #include "arm_compute/core/Window.h" |
| 35 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 36 | #include "support/StringSupport.h" |
Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +0100 | [diff] [blame] | 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | CLUpsampleLayerKernel::CLUpsampleLayerKernel() |
Georgios Pinitas | 7fdcfb1 | 2020-01-09 16:45:46 +0000 | [diff] [blame] | 41 | : _input(nullptr), _output(nullptr), _info(), _data_layout(DataLayout::UNKNOWN), _num_elems_processed_per_iteration_input_x() |
Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +0100 | [diff] [blame] | 42 | { |
| 43 | } |
| 44 | |
| 45 | Status CLUpsampleLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const Size2D &info, const InterpolationPolicy upsampling_policy) |
| 46 | { |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 48 | ARM_COMPUTE_UNUSED(upsampling_policy); |
| 49 | |
| 50 | DataLayout data_layout = input->data_layout(); |
| 51 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 52 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 53 | |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 54 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 55 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
| 56 | |
Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +0100 | [diff] [blame] | 57 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.x() != 2 || info.y() != 2, "Only stride 2 is supported"); |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(upsampling_policy != InterpolationPolicy::NEAREST_NEIGHBOR, "Only nearest neighbor policy supported"); |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 59 | |
| 60 | if(output->total_size() != 0) |
| 61 | { |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 63 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(idx_width) != info.x() * input->dimension(idx_width)); |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(idx_height) != info.y() * input->dimension(idx_height)); |
| 66 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
| 67 | } |
Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +0100 | [diff] [blame] | 68 | |
| 69 | return Status{}; |
| 70 | } |
| 71 | |
| 72 | void CLUpsampleLayerKernel::configure(const ICLTensor *input, ICLTensor *output, const Size2D &info, const InterpolationPolicy upsampling_policy) |
| 73 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 74 | configure(CLKernelLibrary::get().get_compile_context(), input, output, info, upsampling_policy); |
| 75 | } |
| 76 | |
Manuel Bottini | 2803f70 | 2020-04-21 16:20:03 +0100 | [diff] [blame] | 77 | void CLUpsampleLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const Size2D &info, const InterpolationPolicy upsampling_policy) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 78 | { |
Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +0100 | [diff] [blame] | 79 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 80 | ARM_COMPUTE_UNUSED(upsampling_policy); |
| 81 | |
| 82 | _input = input; |
| 83 | _output = output; |
| 84 | _info = info; |
Georgios Pinitas | 7fdcfb1 | 2020-01-09 16:45:46 +0000 | [diff] [blame] | 85 | _data_layout = input->info()->data_layout(); |
Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +0100 | [diff] [blame] | 86 | _num_elems_processed_per_iteration_input_x = 1; |
| 87 | |
Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +0100 | [diff] [blame] | 88 | TensorShape output_shape = misc::shape_calculator::compute_upsample_shape(*input->info(), info); |
| 89 | auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type()); |
Georgios Pinitas | 7fdcfb1 | 2020-01-09 16:45:46 +0000 | [diff] [blame] | 90 | output->info()->set_data_layout(_data_layout); |
Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +0100 | [diff] [blame] | 91 | |
| 92 | unsigned int num_elems_processed_per_iteration_x = 16; |
| 93 | const int output_width_x = output->info()->dimension(0); |
| 94 | const bool multi_access_x = ((output_width_x / num_elems_processed_per_iteration_x) > 0); |
| 95 | |
| 96 | // Perform validation step |
| 97 | ARM_COMPUTE_ERROR_THROW_ON(CLUpsampleLayerKernel::validate(input->info(), output->info(), info, upsampling_policy)); |
| 98 | |
| 99 | Window win{}; |
| 100 | |
Georgios Pinitas | 7fdcfb1 | 2020-01-09 16:45:46 +0000 | [diff] [blame] | 101 | switch(_data_layout) |
Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +0100 | [diff] [blame] | 102 | { |
| 103 | case DataLayout::NCHW: |
| 104 | { |
| 105 | win = calculate_max_window(*output->info()); |
| 106 | win.set(Window::DimY, Window::Dimension(win.y().start(), win.y().end(), info.y())); |
| 107 | if(multi_access_x) |
| 108 | { |
| 109 | _num_elems_processed_per_iteration_input_x = num_elems_processed_per_iteration_x / info.x(); |
| 110 | win.set(Window::DimX, Window::Dimension(win.x().start(), ceil_to_multiple(win.x().end(), num_elems_processed_per_iteration_x), num_elems_processed_per_iteration_x)); |
| 111 | } |
| 112 | break; |
| 113 | } |
| 114 | case DataLayout::NHWC: |
| 115 | { |
| 116 | win = calculate_max_window(*output->info()); |
| 117 | win.set(Window::DimY, Window::Dimension(win.y().start(), win.y().end(), info.x())); |
| 118 | win.set(Window::DimZ, Window::Dimension(win.z().start(), win.z().end(), info.y())); |
| 119 | if(multi_access_x) |
| 120 | { |
| 121 | _num_elems_processed_per_iteration_input_x = num_elems_processed_per_iteration_x; |
| 122 | win.set(Window::DimX, Window::Dimension(win.x().start(), ceil_to_multiple(win.x().end(), |
| 123 | num_elems_processed_per_iteration_x), |
| 124 | num_elems_processed_per_iteration_x)); |
| 125 | } |
| 126 | break; |
| 127 | } |
| 128 | default: |
| 129 | ARM_COMPUTE_ERROR("Not implemented"); |
| 130 | } |
| 131 | |
| 132 | // Create kernel |
| 133 | CLBuildOptions build_opts; |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 134 | build_opts.add_option(("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(input->info()->element_size()))); |
Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +0100 | [diff] [blame] | 135 | build_opts.add_option_if(multi_access_x, "-DVEC_SIZE_IN=" + support::cpp11::to_string(_num_elems_processed_per_iteration_input_x)); |
| 136 | build_opts.add_option_if(multi_access_x, "-DVEC_SIZE_OUT=" + support::cpp11::to_string(num_elems_processed_per_iteration_x)); |
| 137 | build_opts.add_option_if(multi_access_x, "-DLAST_ACCESSED_X_IN=" + support::cpp11::to_string(std::max<int>(_input->info()->dimension(0) - _num_elems_processed_per_iteration_input_x, 0))); |
| 138 | build_opts.add_option_if(multi_access_x, "-DLAST_ACCESSED_X_OUT=" + support::cpp11::to_string(std::max<int>(output_width_x - num_elems_processed_per_iteration_x, 0))); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 139 | _kernel = create_kernel(compile_context, "upsample_layer_" + lower_string(string_from_data_layout(input->info()->data_layout())), build_opts.options()); |
Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +0100 | [diff] [blame] | 140 | |
| 141 | ICLKernel::configure_internal(win); |
| 142 | } |
| 143 | |
| 144 | void CLUpsampleLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 145 | { |
| 146 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 147 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 148 | |
| 149 | Window collapsed_window = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 150 | Window slice_out = collapsed_window.first_slice_window_3D(); |
| 151 | Window slice_in = collapsed_window.first_slice_window_3D(); |
| 152 | |
Georgios Pinitas | 7fdcfb1 | 2020-01-09 16:45:46 +0000 | [diff] [blame] | 153 | switch(_data_layout) |
Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +0100 | [diff] [blame] | 154 | { |
| 155 | case DataLayout::NCHW: |
| 156 | slice_in.set(Window::DimX, Window::Dimension(0, _input->info()->dimension(0), _num_elems_processed_per_iteration_input_x)); |
| 157 | slice_in.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), 1)); |
| 158 | break; |
| 159 | case DataLayout::NHWC: |
| 160 | slice_in.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), 1)); |
| 161 | slice_in.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), 1)); |
| 162 | break; |
| 163 | default: |
| 164 | ARM_COMPUTE_ERROR("Not implemented"); |
| 165 | } |
| 166 | |
| 167 | do |
| 168 | { |
| 169 | unsigned int idx = 0; |
| 170 | add_3D_tensor_argument(idx, _input, slice_in); |
| 171 | add_3D_tensor_argument(idx, _output, slice_out); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 172 | enqueue(queue, *this, slice_out, lws_hint()); |
Michalis Spyrou | ceb889e | 2018-09-17 18:24:41 +0100 | [diff] [blame] | 173 | } |
| 174 | while(collapsed_window.slide_window_slice_3D(slice_out) && collapsed_window.slide_window_slice_3D(slice_in)); |
| 175 | } |
| 176 | } // namespace arm_compute |