Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 1 | /* |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +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/CLReverseKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 28 | #include "arm_compute/core/CL/CLValidate.h" |
| 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/TensorInfo.h" |
| 33 | #include "arm_compute/core/Window.h" |
| 34 | |
| 35 | namespace arm_compute |
| 36 | { |
| 37 | namespace |
| 38 | { |
| 39 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *axis) |
| 40 | { |
| 41 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, axis); |
| 42 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
| 43 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QASYMM8, |
| 44 | DataType::U16, DataType::S16, |
| 45 | DataType::U32, DataType::S32, |
| 46 | DataType::F16, DataType::F32); |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(axis, 1, DataType::U32); |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis->num_dimensions() > 1, "Axis must be a 1D tensor"); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis->dimension(0) > 4, "Only up to 4 dimensions can be reversed"); |
| 50 | |
| 51 | // Checks performed when output is configured |
| 52 | if(output->total_size() != 0) |
| 53 | { |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 56 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | return Status{}; |
| 60 | } |
| 61 | } // namespace |
| 62 | |
| 63 | CLReverseKernel::CLReverseKernel() |
| 64 | : _input(nullptr), _output(nullptr), _axis(nullptr) |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | void CLReverseKernel::configure(const ICLTensor *input, ICLTensor *output, const ICLTensor *axis) |
| 69 | { |
| 70 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, axis); |
| 71 | |
| 72 | _input = input; |
| 73 | _output = output; |
| 74 | _axis = axis; |
| 75 | |
| 76 | // Output tensor auto initialization if not yet initialized |
| 77 | auto_init_if_empty(*output->info(), *input->info()->clone()); |
| 78 | |
| 79 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), axis->info())); |
| 80 | |
| 81 | // Set kernel build options |
| 82 | CLBuildOptions build_opts; |
| 83 | build_opts.add_option("-DNUM_REVERSE_DIMS=" + support::cpp11::to_string(axis->info()->dimension(0))); |
Gian Marco Iodice | 5ba5e09 | 2018-12-06 17:13:09 +0000 | [diff] [blame] | 84 | switch(input->info()->element_size()) |
| 85 | { |
| 86 | case 1: |
| 87 | build_opts.add_option("-DDATA_TYPE=uchar"); |
| 88 | break; |
| 89 | case 2: |
| 90 | build_opts.add_option("-DDATA_TYPE=ushort"); |
| 91 | break; |
| 92 | case 4: |
| 93 | build_opts.add_option("-DDATA_TYPE=uint"); |
| 94 | break; |
| 95 | default: |
| 96 | ARM_COMPUTE_ERROR("Data type not supported"); |
| 97 | } |
Michele Di Giorgio | 5daeffd | 2018-11-26 10:01:15 +0000 | [diff] [blame] | 98 | |
| 99 | // Create kernel |
| 100 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("reverse", build_opts.options())); |
| 101 | |
| 102 | // Set static kernel arguments |
| 103 | unsigned int idx = 2 * num_arguments_per_4D_tensor() + num_arguments_per_1D_tensor(); |
| 104 | add_argument<cl_uint>(idx, input->info()->dimension(0)); |
| 105 | add_argument<cl_uint>(idx, input->info()->dimension(1)); |
| 106 | add_argument<cl_uint>(idx, input->info()->dimension(2)); |
| 107 | add_argument<cl_uint>(idx, input->info()->dimension(3)); |
| 108 | |
| 109 | // Configure kernel window |
| 110 | Window win = calculate_max_window(*output->info(), Steps()); |
| 111 | ICLKernel::configure_internal(win); |
| 112 | |
| 113 | // Set config_id for enabling LWS tuning |
| 114 | _config_id += "reverse_"; |
| 115 | _config_id += lower_string(string_from_data_type(input->info()->data_type())); |
| 116 | _config_id += "_"; |
| 117 | _config_id += support::cpp11::to_string(input->info()->dimension(0)); |
| 118 | _config_id += "_"; |
| 119 | _config_id += support::cpp11::to_string(input->info()->dimension(1)); |
| 120 | _config_id += "_"; |
| 121 | _config_id += support::cpp11::to_string(input->info()->dimension(2)); |
| 122 | } |
| 123 | |
| 124 | Status CLReverseKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *axis) |
| 125 | { |
| 126 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, axis)); |
| 127 | return Status{}; |
| 128 | } |
| 129 | |
| 130 | void CLReverseKernel::run(const Window &window, cl::CommandQueue &queue) |
| 131 | { |
| 132 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 133 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 134 | |
| 135 | Window collapsed = window.collapse(ICLKernel::window(), Window::DimZ); |
| 136 | Window slice = collapsed.first_slice_window_4D(); |
| 137 | Window axis_slice = collapsed.first_slice_window_1D(); |
| 138 | |
| 139 | do |
| 140 | { |
| 141 | unsigned int idx = 0; |
| 142 | add_4D_tensor_argument(idx, _input, slice); |
| 143 | add_1D_tensor_argument(idx, _axis, axis_slice); |
| 144 | add_4D_tensor_argument(idx, _output, slice); |
| 145 | enqueue(queue, *this, slice, lws_hint()); |
| 146 | } |
| 147 | while(collapsed.slide_window_slice_4D(slice)); |
| 148 | } |
| 149 | } // namespace arm_compute |