Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 1 | /* |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2021 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 | */ |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 24 | #include "src/core/CL/kernels/CLRangeKernel.h" |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/CL/ICLTensor.h" |
Michalis Spyrou | f63885b | 2019-01-16 14:18:09 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/Utils.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 29 | #include "src/core/CL/CLValidate.h" |
| 30 | #include "src/core/helpers/AutoConfiguration.h" |
| 31 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 32 | #include "support/StringSupport.h" |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 33 | |
Manuel Bottini | a243205 | 2020-11-25 18:38:05 +0000 | [diff] [blame] | 34 | namespace arm_compute |
| 35 | { |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 36 | namespace |
| 37 | { |
Manuel Bottini | a243205 | 2020-11-25 18:38:05 +0000 | [diff] [blame] | 38 | constexpr unsigned int vector_size_byte_opencl = 16; |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 39 | |
Manuel Bottini | a243205 | 2020-11-25 18:38:05 +0000 | [diff] [blame] | 40 | Status validate_arguments(const ITensorInfo *output, const float start, const float end, const float step) |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 41 | { |
Manuel Bottini | a243205 | 2020-11-25 18:38:05 +0000 | [diff] [blame] | 42 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); |
| 43 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 44 | 1, |
| 45 | DataType::U8, DataType::S8, DataType::QASYMM8, |
| 46 | DataType::U16, DataType::S16, |
| 47 | DataType::U32, DataType::S32, |
| 48 | DataType::F16, DataType::F32); |
Manuel Bottini | a243205 | 2020-11-25 18:38:05 +0000 | [diff] [blame] | 49 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(output); |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 50 | |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((start == end), "start of the requested sequence must not be equal to the end"); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(((start < end) && (step <= 0)), "step must be greater than 0 when start < end"); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(((start > end) && (step >= 0)), "step must be less than 0 when start > end"); |
| 54 | |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 55 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((start == end), "start of the requested sequence must not be equal to the end"); |
| 56 | |
Manuel Bottini | a243205 | 2020-11-25 18:38:05 +0000 | [diff] [blame] | 57 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!check_value_range(start, output->data_type(), output->quantization_info()), "start value is outside the range of the data type"); |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!check_value_range(end, output->data_type(), output->quantization_info()), "end value is outside the range of the data type"); |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!check_value_range(step, output->data_type(), output->quantization_info()), "step value is outside the range of the data type"); |
| 60 | |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->num_dimensions() != 1, "Output has to be a 1-D tensor"); |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->tensor_shape().total_size() < 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] | 63 | |
| 64 | return Status{}; |
| 65 | } |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 66 | } // namespace |
| 67 | |
| 68 | CLRangeKernel::CLRangeKernel() |
| 69 | : _start(0), _end(1), _step(1), _output(nullptr) |
| 70 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 71 | _type = CLKernelType::ELEMENTWISE; |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void CLRangeKernel::configure(ICLTensor *output, const float start, const float end, const float step) |
| 75 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 76 | configure(CLKernelLibrary::get().get_compile_context(), output, start, end, step); |
| 77 | } |
| 78 | |
Manuel Bottini | 2803f70 | 2020-04-21 16:20:03 +0100 | [diff] [blame] | 79 | void CLRangeKernel::configure(const CLCompileContext &compile_context, ICLTensor *output, const float start, const float end, const float step) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 80 | { |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 81 | ARM_COMPUTE_ERROR_ON_NULLPTR(output); |
Manuel Bottini | a243205 | 2020-11-25 18:38:05 +0000 | [diff] [blame] | 82 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(output->info(), start, end, step)); |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 83 | |
| 84 | // Configure kernel window |
Manuel Bottini | a243205 | 2020-11-25 18:38:05 +0000 | [diff] [blame] | 85 | unsigned int num_elems_processed_per_iteration = adjust_vec_size(vector_size_byte_opencl / output->info()->element_size(), output->info()->dimension(0)); |
| 86 | Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration)); |
| 87 | |
| 88 | auto padding_info = get_padding_info({ output }); |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 89 | |
| 90 | _start = start; |
| 91 | _end = end; |
| 92 | _step = step; |
| 93 | _output = output; |
| 94 | |
| 95 | std::string kernel_name = "range"; |
| 96 | |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 97 | // Set build options |
| 98 | CLBuildOptions build_opts; |
| 99 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(output->info()->data_type())); |
| 100 | build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration)); |
Manuel Bottini | a243205 | 2020-11-25 18:38:05 +0000 | [diff] [blame] | 101 | build_opts.add_option("-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(output->info()->dimension(0) % num_elems_processed_per_iteration)); |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 102 | build_opts.add_option("-DSTART=" + support::cpp11::to_string(start)); |
| 103 | build_opts.add_option("-DSTEP=" + support::cpp11::to_string(step)); |
| 104 | if(is_data_type_quantized_asymmetric(output->info()->data_type())) |
| 105 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 106 | const UniformQuantizationInfo qinfo = output->info()->quantization_info().uniform(); |
| 107 | build_opts.add_option("-DOFFSET_OUT=" + support::cpp11::to_string(qinfo.offset)); |
| 108 | build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(qinfo.scale)); |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 109 | kernel_name += "_quantized"; |
| 110 | } |
| 111 | // Create kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 112 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Manuel Bottini | a243205 | 2020-11-25 18:38:05 +0000 | [diff] [blame] | 113 | ICLKernel::configure_internal(win); |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 114 | |
| 115 | // Set config_id for enabling LWS tuning |
| 116 | _config_id = kernel_name; |
| 117 | _config_id += "_"; |
| 118 | _config_id += lower_string(string_from_data_type(output->info()->data_type())); |
| 119 | _config_id += "_"; |
| 120 | _config_id += support::cpp11::to_string(output->info()->dimension(0)); |
Manuel Bottini | a243205 | 2020-11-25 18:38:05 +0000 | [diff] [blame] | 121 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | Status CLRangeKernel::validate(const ITensorInfo *output, const float start, const float end, const float step) |
| 125 | { |
Manuel Bottini | a243205 | 2020-11-25 18:38:05 +0000 | [diff] [blame] | 126 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(output, start, end, step)); |
Vidhya Sudhan Loganathan | 5e96be7 | 2018-12-18 14:17:00 +0000 | [diff] [blame] | 127 | return Status{}; |
| 128 | } |
| 129 | |
| 130 | void CLRangeKernel::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 | unsigned int idx = 0; |
| 135 | add_1D_tensor_argument(idx, _output, window); |
| 136 | |
| 137 | enqueue(queue, *this, window, lws_hint()); |
| 138 | } |
Manuel Bottini | a243205 | 2020-11-25 18:38:05 +0000 | [diff] [blame] | 139 | } // namespace arm_compute |