blob: a4c30b63c2e0dade9f83013f1712c81de77fd22e [file] [log] [blame]
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 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 */
24#include "arm_compute/core/CL/kernels/CLRangeKernel.h"
25
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"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010029#include "src/core/CL/CLValidate.h"
30#include "src/core/helpers/AutoConfiguration.h"
31#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000032#include "support/StringSupport.h"
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000033
34using namespace arm_compute;
35
36namespace
37{
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000038unsigned int get_num_elems_processed_per_iteration(const DataType dt)
39{
40 unsigned int num_elems_processed_per_iteration = preferred_vector_width(CLKernelLibrary::get().get_device(), dt);
41 if(num_elems_processed_per_iteration > 8)
42 {
43 num_elems_processed_per_iteration = 8; //kernel uses only 8 lanes.
44 }
45 return num_elems_processed_per_iteration;
46}
47
48Status validate_arguments(const ITensorInfo &output, const float start, const float end, const float step)
49{
50 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&output,
51 1,
52 DataType::U8, DataType::S8, DataType::QASYMM8,
53 DataType::U16, DataType::S16,
54 DataType::U32, DataType::S32,
55 DataType::F16, DataType::F32);
56 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&output);
57
58 ARM_COMPUTE_RETURN_ERROR_ON_MSG((start == end), "start of the requested sequence must not be equal to the end");
59 ARM_COMPUTE_RETURN_ERROR_ON_MSG(((start < end) && (step <= 0)), "step must be greater than 0 when start < end");
60 ARM_COMPUTE_RETURN_ERROR_ON_MSG(((start > end) && (step >= 0)), "step must be less than 0 when start > end");
61
Michalis Spyrouf63885b2019-01-16 14:18:09 +000062 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");
63 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");
64 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");
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000065
66 ARM_COMPUTE_RETURN_ERROR_ON_MSG((start == end), "start of the requested sequence must not be equal to the end");
67
68 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output.num_dimensions() != 1, "Output has to be a 1-D tensor");
Michalis Spyrouf63885b2019-01-16 14:18:09 +000069 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 Loganathan5e96be72018-12-18 14:17:00 +000070
71 return Status{};
72}
73
74std::pair<Status, Window> validate_and_configure_window(ITensorInfo &output, const float start, const float end, const float step)
75{
76 unsigned int num_elems_processed_per_iteration = get_num_elems_processed_per_iteration(output.data_type());
77 // Auto initialize output if not initialized
Michalis Spyrouf63885b2019-01-16 14:18:09 +000078 auto_init_if_empty(output, TensorShape(num_of_elements_in_range(start, end, step)), 1, output.data_type(), output.quantization_info());
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000079
80 // Configure kernel window
81 Window win = calculate_max_window(output, Steps(num_elems_processed_per_iteration));
82
83 AccessWindowHorizontal output_access(&output, 0, num_elems_processed_per_iteration);
84 bool window_changed = update_window_and_padding(win, output_access);
Michalis Spyrouf63885b2019-01-16 14:18:09 +000085 output_access.set_valid_region(win, ValidRegion(Coordinates(), TensorShape(num_of_elements_in_range(start, end, step))));
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +000086 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
87 return std::make_pair(err, win);
88}
89} // namespace
90
91CLRangeKernel::CLRangeKernel()
92 : _start(0), _end(1), _step(1), _output(nullptr)
93{
94}
95
96void CLRangeKernel::configure(ICLTensor *output, const float start, const float end, const float step)
97{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010098 configure(CLKernelLibrary::get().get_compile_context(), output, start, end, step);
99}
100
Manuel Bottini2803f702020-04-21 16:20:03 +0100101void CLRangeKernel::configure(const CLCompileContext &compile_context, ICLTensor *output, const float start, const float end, const float step)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100102{
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000103 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
104
105 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*(output->info()), start, end, step));
106
107 // Configure kernel window
108 auto win_config = validate_and_configure_window(*(output->info()), start, end, step);
109 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
110
111 _start = start;
112 _end = end;
113 _step = step;
114 _output = output;
115
116 std::string kernel_name = "range";
117
118 unsigned int num_elems_processed_per_iteration = get_num_elems_processed_per_iteration(output->info()->data_type());
119 // Set build options
120 CLBuildOptions build_opts;
121 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(output->info()->data_type()));
122 build_opts.add_option("-DVECTOR_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration));
123 build_opts.add_option("-DSTART=" + support::cpp11::to_string(start));
124 build_opts.add_option("-DSTEP=" + support::cpp11::to_string(step));
125 if(is_data_type_quantized_asymmetric(output->info()->data_type()))
126 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100127 const UniformQuantizationInfo qinfo = output->info()->quantization_info().uniform();
128 build_opts.add_option("-DOFFSET_OUT=" + support::cpp11::to_string(qinfo.offset));
129 build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(qinfo.scale));
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000130 kernel_name += "_quantized";
131 }
132 // Create kernel
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100133 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Vidhya Sudhan Loganathan5e96be72018-12-18 14:17:00 +0000134 ICLKernel::configure_internal(win_config.second);
135
136 // Set config_id for enabling LWS tuning
137 _config_id = kernel_name;
138 _config_id += "_";
139 _config_id += lower_string(string_from_data_type(output->info()->data_type()));
140 _config_id += "_";
141 _config_id += support::cpp11::to_string(output->info()->dimension(0));
142}
143
144Status CLRangeKernel::validate(const ITensorInfo *output, const float start, const float end, const float step)
145{
146 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
147
148 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*output, start, end, step));
149 ARM_COMPUTE_RETURN_ON_ERROR((validate_and_configure_window(*(output->clone()), start, end, step)).first);
150
151 return Status{};
152}
153
154void CLRangeKernel::run(const Window &window, cl::CommandQueue &queue)
155{
156 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
157 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
158 unsigned int idx = 0;
159 add_1D_tensor_argument(idx, _output, window);
160
161 enqueue(queue, *this, window, lws_hint());
162}