Georgios Pinitas | aaa2718 | 2018-11-21 16:32:15 +0000 | [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/CLSelectKernel.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/Helpers.h" |
| 31 | #include "arm_compute/core/TensorInfo.h" |
| 32 | #include "arm_compute/core/Utils.h" |
| 33 | #include "arm_compute/core/Window.h" |
| 34 | |
| 35 | #include "support/ToolchainSupport.h" |
| 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace |
| 40 | { |
| 41 | Status validate_arguments(const ITensorInfo *c, const ITensorInfo *x, const ITensorInfo *y, const ITensorInfo *output) |
| 42 | { |
| 43 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(x); |
| 44 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(x, |
| 45 | 1, |
| 46 | DataType::U8, DataType::S8, DataType::QASYMM8, |
| 47 | DataType::U16, DataType::S16, |
| 48 | DataType::U32, DataType::S32, |
| 49 | DataType::F16, DataType::F32); |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(x, y); |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(x, y); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(c, 1, DataType::U8); |
| 53 | |
| 54 | const bool is_same_rank = (c->tensor_shape().num_dimensions() == x->tensor_shape().num_dimensions()); |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON(is_same_rank && (x->tensor_shape() != c->tensor_shape())); |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON(!is_same_rank && ((c->tensor_shape().num_dimensions() > 1) || (c->tensor_shape().x() != x->tensor_shape()[x->tensor_shape().num_dimensions() - 1]))); |
| 57 | |
| 58 | if(output != nullptr && output->total_size() != 0) |
| 59 | { |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(x, output); |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(x, output); |
| 62 | } |
| 63 | |
| 64 | return Status{}; |
| 65 | } |
| 66 | |
| 67 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *c, ITensorInfo *x, ITensorInfo *y, ITensorInfo *output) |
| 68 | { |
| 69 | if(output != nullptr) |
| 70 | { |
| 71 | // Output tensor auto initialization if not yet initialized |
| 72 | auto_init_if_empty(*output, *x->clone()); |
| 73 | } |
| 74 | |
| 75 | const bool is_same_rank = (c->tensor_shape().num_dimensions() == x->tensor_shape().num_dimensions()); |
| 76 | |
| 77 | const unsigned int num_elems_processed_per_iteration = 16 / x->element_size(); |
| 78 | |
| 79 | // Configure kernel window |
| 80 | Window win = calculate_max_window(*x, Steps(num_elems_processed_per_iteration)); |
| 81 | AccessWindowHorizontal x_access(x, 0, num_elems_processed_per_iteration); |
| 82 | AccessWindowHorizontal y_access(y, 0, num_elems_processed_per_iteration); |
| 83 | bool window_changed = update_window_and_padding(win, x_access, y_access); |
| 84 | |
| 85 | // Update window for condition |
| 86 | if(is_same_rank) |
| 87 | { |
| 88 | AccessWindowHorizontal c_access(c, 0, num_elems_processed_per_iteration); |
| 89 | window_changed = window_changed || update_window_and_padding(win, c_access); |
| 90 | } |
| 91 | |
| 92 | // Update window for output |
| 93 | if(output != nullptr) |
| 94 | { |
| 95 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 96 | window_changed = window_changed || update_window_and_padding(win, output_access); |
| 97 | output_access.set_valid_region(win, x->valid_region()); |
| 98 | } |
| 99 | |
| 100 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 101 | return std::make_pair(err, win); |
| 102 | } |
| 103 | } // namespace |
| 104 | |
| 105 | CLSelectKernel::CLSelectKernel() |
| 106 | : _c(nullptr), _x(nullptr), _y(nullptr), _output(nullptr), _has_same_rank(false) |
| 107 | { |
| 108 | } |
| 109 | void CLSelectKernel::configure(const ICLTensor *c, const ICLTensor *x, const ICLTensor *y, ICLTensor *output) |
| 110 | { |
| 111 | ARM_COMPUTE_ERROR_ON_NULLPTR(c, x, y, output); |
| 112 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(c->info(), x->info(), y->info(), output->info())); |
| 113 | |
| 114 | _c = c; |
| 115 | _x = x; |
| 116 | _y = y; |
| 117 | _output = output; |
| 118 | _has_same_rank = (c->info()->tensor_shape().num_dimensions() == x->info()->tensor_shape().num_dimensions()); |
| 119 | |
| 120 | const unsigned int num_elems_processed_per_iteration = 16 / x->info()->element_size(); |
| 121 | |
| 122 | // Set build options |
| 123 | CLBuildOptions build_opts; |
| 124 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(x->info()->data_type())); |
| 125 | build_opts.add_option("-DSELECT_DATA_TYPE=" + get_cl_select_type_from_data_type(x->info()->data_type())); |
| 126 | build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)); |
| 127 | |
| 128 | // Create kernel |
| 129 | std::string kernel_name = "select"; |
| 130 | if(_has_same_rank) |
| 131 | { |
| 132 | kernel_name += "_same_rank"; |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | const bool is_input_rank_greater_than_two = x->info()->tensor_shape().num_dimensions() > 2; |
| 137 | if(is_input_rank_greater_than_two) |
| 138 | { |
| 139 | const size_t width = x->info()->tensor_shape().x(); |
| 140 | const size_t height = x->info()->tensor_shape().y(); |
| 141 | const size_t outer_size = x->info()->tensor_shape()[x->info()->tensor_shape().num_dimensions() - 1]; |
| 142 | const size_t depth_size = x->info()->tensor_shape().total_size() / (width * height * outer_size); |
| 143 | build_opts.add_option("-DDEPTH_SIZE=" + support::cpp11::to_string(depth_size)); |
| 144 | } |
| 145 | kernel_name += "_different_rank"; |
| 146 | kernel_name += is_input_rank_greater_than_two ? "_n" : "_2"; |
| 147 | } |
| 148 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); |
| 149 | |
| 150 | // Configure kernel window |
| 151 | auto win_config = validate_and_configure_window(c->info(), x->info(), y->info(), output->info()); |
| 152 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 153 | ICLKernel::configure_internal(win_config.second); |
| 154 | |
| 155 | _config_id = "select_"; |
| 156 | _config_id += string_from_data_type(x->info()->data_type()); |
| 157 | _config_id += "_"; |
| 158 | _config_id += support::cpp11::to_string(x->info()->dimension(0)); |
| 159 | _config_id += "_"; |
| 160 | _config_id += support::cpp11::to_string(x->info()->dimension(1)); |
| 161 | _config_id += "_"; |
| 162 | _config_id += support::cpp11::to_string(x->info()->dimension(2)); |
| 163 | } |
| 164 | |
| 165 | Status CLSelectKernel::validate(const ITensorInfo *c, const ITensorInfo *x, const ITensorInfo *y, const ITensorInfo *output) |
| 166 | { |
| 167 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(c, x, y, output)); |
| 168 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(c->clone().get(), x->clone().get(), y->clone().get(), output->clone().get()).first); |
| 169 | return Status{}; |
| 170 | } |
| 171 | |
| 172 | void CLSelectKernel::run(const arm_compute::Window &window, cl::CommandQueue &queue) |
| 173 | { |
| 174 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 175 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 176 | |
| 177 | Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ); |
| 178 | Window slice = collapsed.first_slice_window_3D(); |
| 179 | |
| 180 | if(!_has_same_rank) |
| 181 | { |
| 182 | Window vector_slice = window.first_slice_window_1D(); |
| 183 | vector_slice.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 184 | unsigned int idx = 0; |
| 185 | add_1D_tensor_argument(idx, _c, vector_slice); |
| 186 | } |
| 187 | |
| 188 | do |
| 189 | { |
| 190 | unsigned int idx = _has_same_rank ? 0 : num_arguments_per_1D_tensor(); |
| 191 | if(_has_same_rank) |
| 192 | { |
| 193 | add_3D_tensor_argument(idx, _c, slice); |
| 194 | } |
| 195 | add_3D_tensor_argument(idx, _x, slice); |
| 196 | add_3D_tensor_argument(idx, _y, slice); |
| 197 | add_3D_tensor_argument(idx, _output, slice); |
| 198 | |
| 199 | enqueue(queue, *this, slice, lws_hint()); |
| 200 | } |
| 201 | while(collapsed.slide_window_slice_3D(slice)); |
| 202 | } |
| 203 | } // namespace arm_compute |