blob: c8a456a4277cf710c02ed3d34c8e848fa4177022 [file] [log] [blame]
Manuel Bottini053e7512018-12-28 15:05:20 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Manuel Bottini053e7512018-12-28 15:05:20 +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/NEON/kernels/NERangeKernel.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/IAccessWindow.h"
29#include "arm_compute/core/ITensor.h"
30#include "arm_compute/core/NEON/NEAsymm.h"
31#include "arm_compute/core/NEON/wrapper/wrapper.h"
32#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Validate.h"
34
Michalis Spyrouf63885b2019-01-16 14:18:09 +000035#include "arm_compute/core/Utils.h"
Manuel Bottini053e7512018-12-28 15:05:20 +000036
37namespace arm_compute
38{
39namespace
40{
41template <typename T>
42void range_function(ITensor *output, float start, float step, const Window &window)
43{
Manuel Bottini053e7512018-12-28 15:05:20 +000044 /** NEON vector tag type. */
45 using ExactTagType = typename wrapper::traits::neon_bitvector<T, wrapper::traits::BitWidth::W128>::tag_type;
46
47 const auto step_vec = wrapper::vdup_n(static_cast<T>(step), ExactTagType{});
48 const auto start_vec = wrapper::vdup_n(static_cast<T>(start), ExactTagType{});
49 auto id_vec = wrapper::vdup_n(static_cast<T>(0.f), ExactTagType{});
50
Michalis Spyroucd5b4a32020-02-27 15:35:53 +000051 const auto window_start_x = static_cast<int>(window.x().start());
52 const auto window_end_x = static_cast<int>(window.x().end());
53 const int window_step_x = 16 / sizeof(T);
54
55 Window win{ window };
56 win.set(Window::DimX, Window::Dimension(0, 1, 1));
57 Iterator output_it(output, win);
58
59 execute_window_loop(win, [&](const Coordinates &)
Manuel Bottini053e7512018-12-28 15:05:20 +000060 {
Michalis Spyroucd5b4a32020-02-27 15:35:53 +000061 int x = window_start_x;
Manuel Bottini053e7512018-12-28 15:05:20 +000062 const auto out_ptr = reinterpret_cast<T *>(output_it.ptr());
Michalis Spyroucd5b4a32020-02-27 15:35:53 +000063 for(; x <= (window_end_x - window_step_x); x += window_step_x)
64 {
Michalis Spyrou10b49c72020-04-02 13:12:32 +010065 for(int count = 0; count < window_step_x; ++count)
Michalis Spyroucd5b4a32020-02-27 15:35:53 +000066 {
67 id_vec = wrapper::vsetlane(static_cast<T>(x + count), id_vec, count);
68 }
69
70 // start + step * id
71 const auto res_vec = wrapper::vmla(start_vec, id_vec, step_vec);
72 wrapper::vstore(out_ptr + x, res_vec);
73 }
74
75 // Compute left-over elements
76 for(; x < window_end_x; ++x)
77 {
78 const auto res = start + x * step;
79 *(out_ptr + x) = res;
80 }
81
Manuel Bottini053e7512018-12-28 15:05:20 +000082 },
83 output_it);
84}
85
86Status validate_arguments(const ITensorInfo &output, const float start, const float end, const float step)
87{
88 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&output,
89 1,
90 DataType::U8, DataType::S8,
91 DataType::U16, DataType::S16,
92 DataType::U32, DataType::S32,
93 DataType::F16, DataType::F32);
94
95 ARM_COMPUTE_RETURN_ERROR_ON_MSG((start == end), "start of the requested sequence must not be equal to the end");
96 ARM_COMPUTE_RETURN_ERROR_ON_MSG(((start < end) && (step <= 0)), "step must be greater than 0 when start < end");
97 ARM_COMPUTE_RETURN_ERROR_ON_MSG(((start > end) && (step >= 0)), "step must be less than 0 when start > end");
98
Michalis Spyrouf63885b2019-01-16 14:18:09 +000099 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");
100 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");
101 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");
Manuel Bottini053e7512018-12-28 15:05:20 +0000102
103 ARM_COMPUTE_RETURN_ERROR_ON_MSG((start == end), "start of the requested sequence must not be equal to the end");
104
105 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 +0000106 ARM_COMPUTE_RETURN_ERROR_ON_MSG(output.tensor_shape().total_size() < num_of_elements_in_range(start, end, step), "Output tensor size is incorrect");
Manuel Bottini053e7512018-12-28 15:05:20 +0000107
108 return Status{};
109}
Manuel Bottini053e7512018-12-28 15:05:20 +0000110} // namespace
111
112NERangeKernel::NERangeKernel()
113 : _func(nullptr), _start(0), _end(1), _step(1), _output(nullptr)
114{
115}
116
117void NERangeKernel::configure(ITensor *output, float start, float end, float step)
118{
119 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
120
121 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*(output->info()), start, end, step));
122
Michalis Spyroucd5b4a32020-02-27 15:35:53 +0000123 // Auto initialize output if not initialized
124 auto_init_if_empty(*output->info(), TensorShape(num_of_elements_in_range(start, end, step)), 1, output->info()->data_type(), output->info()->quantization_info());
125
Manuel Bottini053e7512018-12-28 15:05:20 +0000126 // Configure kernel window
Michalis Spyroucd5b4a32020-02-27 15:35:53 +0000127 Window win = calculate_max_window(*output->info(), Steps());
128 Coordinates coord;
129 coord.set_num_dimensions(output->info()->num_dimensions());
130 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
Manuel Bottini053e7512018-12-28 15:05:20 +0000131
132 _start = start;
133 _end = end;
134 _step = step;
135 _output = output;
136 switch(_output->info()->data_type())
137 {
138 case DataType::U8:
139 _func = &range_function<uint8_t>;
140 break;
141 case DataType::U16:
142 _func = &range_function<uint16_t>;
143 break;
144 case DataType::U32:
145 _func = &range_function<uint32_t>;
146 break;
147 case DataType::S8:
148 _func = &range_function<int8_t>;
149 break;
150 case DataType::S16:
151 _func = &range_function<int16_t>;
152 break;
153 case DataType::S32:
154 _func = &range_function<int32_t>;
155 break;
156 case DataType::F32:
157 _func = &range_function<float>;
158 break;
159#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
160 case DataType::F16:
161 _func = &range_function<float16_t>;
162 break;
163#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
164 default:
165 ARM_COMPUTE_ERROR("Unsupported data type.");
166 break;
167 }
168
Michalis Spyroucd5b4a32020-02-27 15:35:53 +0000169 INEKernel::configure(win);
Manuel Bottini053e7512018-12-28 15:05:20 +0000170}
171
172Status NERangeKernel::validate(const ITensorInfo *output, float start, float end, float step)
173{
174 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
175
176 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*output, start, end, step));
Manuel Bottini053e7512018-12-28 15:05:20 +0000177
178 return Status{};
179}
180
181void NERangeKernel::run(const Window &window, const ThreadInfo &info)
182{
183 ARM_COMPUTE_UNUSED(info);
184 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
185 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
186 ARM_COMPUTE_ERROR_ON(_func == nullptr);
187
188 (*_func)(_output, _start, _step, window);
189}
190} // namespace arm_compute