blob: 622f6210b9b1319c2d534a160ed454b8d6730136 [file] [log] [blame]
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +00001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2018-2021, 2023 Arm Limited.
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +00003 *
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 Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLRangeKernel.h"
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000025
26#include "arm_compute/core/CL/CLHelpers.h"
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000027#include "arm_compute/core/CL/ICLTensor.h"
Michalis Spyrouf63885b2019-01-16 14:18:09 +000028#include "arm_compute/core/Utils.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000029#include "arm_compute/core/utils/helpers/AdjustVecSize.h"
30#include "arm_compute/core/utils/StringUtils.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/core/CL/CLValidate.h"
33#include "src/core/helpers/AutoConfiguration.h"
34#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000035#include "support/StringSupport.h"
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000036
Manuel Bottinia2432052020-11-25 18:38:05 +000037namespace arm_compute
38{
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000039namespace
40{
Manuel Bottinia2432052020-11-25 18:38:05 +000041constexpr unsigned int vector_size_byte_opencl = 16;
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000042
Manuel Bottinia2432052020-11-25 18:38:05 +000043Status validate_arguments(const ITensorInfo *output, const float start, const float end, const float step)
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000044{
Manuel Bottinia2432052020-11-25 18:38:05 +000045 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010046 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::S8, DataType::QASYMM8,
47 DataType::U16, DataType::S16, DataType::U32, DataType::S32,
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000048 DataType::F16, DataType::F32);
Manuel Bottinia2432052020-11-25 18:38:05 +000049 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(output);
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000050
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 Loganathan5e96be72018-12-18 14:17:00 +000055 ARM_COMPUTE_RETURN_ERROR_ON_MSG((start == end), "start of the requested sequence must not be equal to the end");
56
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010057 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!check_value_range(start, output->data_type(), output->quantization_info()),
58 "start value is outside the range of the data type");
59 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!check_value_range(end, output->data_type(), output->quantization_info()),
60 "end value is outside the range of the data type");
61 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!check_value_range(step, output->data_type(), output->quantization_info()),
62 "step value is outside the range of the data type");
Manuel Bottinia2432052020-11-25 18:38:05 +000063
64 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->num_dimensions() != 1, "Output has to be a 1-D tensor");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010065 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->tensor_shape().total_size() < num_of_elements_in_range(start, end, step),
66 "Output tensor size is incorrect");
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000067
68 return Status{};
69}
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000070} // namespace
71
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010072CLRangeKernel::CLRangeKernel() : _start(0), _end(1), _step(1), _output(nullptr)
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000073{
Giorgio Arena4a95bba2021-06-28 11:00:27 +010074 _type = CLKernelType::ELEMENTWISE;
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000075}
76
77void CLRangeKernel::configure(ICLTensor *output, const float start, const float end, const float step)
78{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010079 configure(CLKernelLibrary::get().get_compile_context(), output, start, end, step);
80}
81
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010082void CLRangeKernel::configure(
83 const CLCompileContext &compile_context, ICLTensor *output, const float start, const float end, const float step)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010084{
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000085 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
Manuel Bottinia2432052020-11-25 18:38:05 +000086 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(output->info(), start, end, step));
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000087
88 // Configure kernel window
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010089 unsigned int num_elems_processed_per_iteration =
90 adjust_vec_size(vector_size_byte_opencl / output->info()->element_size(), output->info()->dimension(0));
91 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
Manuel Bottinia2432052020-11-25 18:38:05 +000092
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010093 auto padding_info = get_padding_info({output});
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000094
95 _start = start;
96 _end = end;
97 _step = step;
98 _output = output;
99
100 std::string kernel_name = "range";
101
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000102 // Set build options
103 CLBuildOptions build_opts;
104 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(output->info()->data_type()));
105 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100106 build_opts.add_option("-DVEC_SIZE_LEFTOVER=" +
107 support::cpp11::to_string(output->info()->dimension(0) % num_elems_processed_per_iteration));
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000108 build_opts.add_option("-DSTART=" + support::cpp11::to_string(start));
109 build_opts.add_option("-DSTEP=" + support::cpp11::to_string(step));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100110 if (is_data_type_quantized_asymmetric(output->info()->data_type()))
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000111 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100112 const UniformQuantizationInfo qinfo = output->info()->quantization_info().uniform();
113 build_opts.add_option("-DOFFSET_OUT=" + support::cpp11::to_string(qinfo.offset));
114 build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(qinfo.scale));
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000115 kernel_name += "_quantized";
116 }
117 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100118 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Manuel Bottinia2432052020-11-25 18:38:05 +0000119 ICLKernel::configure_internal(win);
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000120
121 // Set config_id for enabling LWS tuning
122 _config_id = kernel_name;
123 _config_id += "_";
124 _config_id += lower_string(string_from_data_type(output->info()->data_type()));
125 _config_id += "_";
126 _config_id += support::cpp11::to_string(output->info()->dimension(0));
Manuel Bottinia2432052020-11-25 18:38:05 +0000127 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000128}
129
130Status CLRangeKernel::validate(const ITensorInfo *output, const float start, const float end, const float step)
131{
Manuel Bottinia2432052020-11-25 18:38:05 +0000132 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(output, start, end, step));
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000133 return Status{};
134}
135
136void CLRangeKernel::run(const Window &window, cl::CommandQueue &queue)
137{
138 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
139 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
140 unsigned int idx = 0;
141 add_1D_tensor_argument(idx, _output, window);
142
143 enqueue(queue, *this, window, lws_hint());
144}
Matthew Bentham314d3e22023-06-23 10:53:52 +0000145} // namespace arm_compute