Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 1 | /* |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +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/CLRangeKernel.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 "utils/Utils.h" |
| 30 | |
| 31 | using namespace arm_compute; |
| 32 | |
| 33 | namespace |
| 34 | { |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 35 | unsigned int get_num_elems_processed_per_iteration(const DataType dt) |
| 36 | { |
| 37 | unsigned int num_elems_processed_per_iteration = preferred_vector_width(CLKernelLibrary::get().get_device(), dt); |
| 38 | if(num_elems_processed_per_iteration > 8) |
| 39 | { |
| 40 | num_elems_processed_per_iteration = 8; //kernel uses only 8 lanes. |
| 41 | } |
| 42 | return num_elems_processed_per_iteration; |
| 43 | } |
| 44 | |
| 45 | Status validate_arguments(const ITensorInfo &output, const float start, const float end, const float step) |
| 46 | { |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&output, |
| 48 | 1, |
| 49 | DataType::U8, DataType::S8, DataType::QASYMM8, |
| 50 | DataType::U16, DataType::S16, |
| 51 | DataType::U32, DataType::S32, |
| 52 | DataType::F16, DataType::F32); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&output); |
| 54 | |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((start == end), "start of the requested sequence must not be equal to the end"); |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(((start < end) && (step <= 0)), "step must be greater than 0 when start < end"); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(((start > end) && (step >= 0)), "step must be less than 0 when start > end"); |
| 58 | |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!utils::check_value_range(start, output.data_type(), output.quantization_info()), "start value is outside the range of the data type"); |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!utils::check_value_range(end, output.data_type(), output.quantization_info()), "end value is outside the range of the data type"); |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!utils::check_value_range(step, output.data_type(), output.quantization_info()), "step value is outside the range of the data type"); |
| 62 | |
| 63 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((start == end), "start of the requested sequence must not be equal to the end"); |
| 64 | |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output.num_dimensions() != 1, "Output has to be a 1-D tensor"); |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 66 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output.tensor_shape().total_size() < utils::num_of_elements_in_range(start, end, step), "Output tensor size is incorrect"); |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 67 | |
| 68 | return Status{}; |
| 69 | } |
| 70 | |
| 71 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo &output, const float start, const float end, const float step) |
| 72 | { |
| 73 | unsigned int num_elems_processed_per_iteration = get_num_elems_processed_per_iteration(output.data_type()); |
| 74 | // Auto initialize output if not initialized |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 75 | auto_init_if_empty(output, TensorShape(utils::num_of_elements_in_range(start, end, step)), 1, output.data_type(), output.quantization_info()); |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 76 | |
| 77 | // Configure kernel window |
| 78 | Window win = calculate_max_window(output, Steps(num_elems_processed_per_iteration)); |
| 79 | |
| 80 | AccessWindowHorizontal output_access(&output, 0, num_elems_processed_per_iteration); |
| 81 | bool window_changed = update_window_and_padding(win, output_access); |
Manuel Bottini | 053e751 | 2018-12-28 15:05:20 +0000 | [diff] [blame] | 82 | output_access.set_valid_region(win, ValidRegion(Coordinates(), TensorShape(utils::num_of_elements_in_range(start, end, step)))); |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 83 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 84 | return std::make_pair(err, win); |
| 85 | } |
| 86 | } // namespace |
| 87 | |
| 88 | CLRangeKernel::CLRangeKernel() |
| 89 | : _start(0), _end(1), _step(1), _output(nullptr) |
| 90 | { |
| 91 | } |
| 92 | |
| 93 | void CLRangeKernel::configure(ICLTensor *output, const float start, const float end, const float step) |
| 94 | { |
| 95 | ARM_COMPUTE_ERROR_ON_NULLPTR(output); |
| 96 | |
| 97 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*(output->info()), start, end, step)); |
| 98 | |
| 99 | // Configure kernel window |
| 100 | auto win_config = validate_and_configure_window(*(output->info()), start, end, step); |
| 101 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 102 | |
| 103 | _start = start; |
| 104 | _end = end; |
| 105 | _step = step; |
| 106 | _output = output; |
| 107 | |
| 108 | std::string kernel_name = "range"; |
| 109 | |
| 110 | unsigned int num_elems_processed_per_iteration = get_num_elems_processed_per_iteration(output->info()->data_type()); |
| 111 | // Set build options |
| 112 | CLBuildOptions build_opts; |
| 113 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(output->info()->data_type())); |
| 114 | build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)); |
| 115 | build_opts.add_option("-DSTART=" + support::cpp11::to_string(start)); |
| 116 | build_opts.add_option("-DSTEP=" + support::cpp11::to_string(step)); |
| 117 | if(is_data_type_quantized_asymmetric(output->info()->data_type())) |
| 118 | { |
| 119 | build_opts.add_option("-DOFFSET_OUT=" + support::cpp11::to_string(output->info()->quantization_info().offset)); |
| 120 | build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(output->info()->quantization_info().scale)); |
| 121 | kernel_name += "_quantized"; |
| 122 | } |
| 123 | // Create kernel |
| 124 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); |
| 125 | ICLKernel::configure_internal(win_config.second); |
| 126 | |
| 127 | // Set config_id for enabling LWS tuning |
| 128 | _config_id = kernel_name; |
| 129 | _config_id += "_"; |
| 130 | _config_id += lower_string(string_from_data_type(output->info()->data_type())); |
| 131 | _config_id += "_"; |
| 132 | _config_id += support::cpp11::to_string(output->info()->dimension(0)); |
| 133 | } |
| 134 | |
| 135 | Status CLRangeKernel::validate(const ITensorInfo *output, const float start, const float end, const float step) |
| 136 | { |
| 137 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); |
| 138 | |
| 139 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*output, start, end, step)); |
| 140 | ARM_COMPUTE_RETURN_ON_ERROR((validate_and_configure_window(*(output->clone()), start, end, step)).first); |
| 141 | |
| 142 | return Status{}; |
| 143 | } |
| 144 | |
| 145 | void CLRangeKernel::run(const Window &window, cl::CommandQueue &queue) |
| 146 | { |
| 147 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 148 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 149 | unsigned int idx = 0; |
| 150 | add_1D_tensor_argument(idx, _output, window); |
| 151 | |
| 152 | enqueue(queue, *this, window, lws_hint()); |
| 153 | } |