blob: 0395e0bd3423806c219c76bfedecbe0ded607dc6 [file] [log] [blame]
Manuel Bottini053e7512018-12-28 15:05:20 +00001/*
Sheri Zhangac6499a2021-02-10 15:32:38 +00002 * Copyright (c) 2018-2021 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 */
Michalis Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NERangeKernel.h"
Manuel Bottini053e7512018-12-28 15:05:20 +000025
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
Manuel Bottini053e7512018-12-28 15:05:20 +000028#include "arm_compute/core/ITensor.h"
Manuel Bottini053e7512018-12-28 15:05:20 +000029#include "arm_compute/core/TensorInfo.h"
30#include "arm_compute/core/Validate.h"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010031#include "src/core/NEON/NEAsymm.h"
32#include "src/core/NEON/wrapper/wrapper.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/helpers/AutoConfiguration.h"
34#include "src/core/helpers/WindowHelpers.h"
Manuel Bottini053e7512018-12-28 15:05:20 +000035
Michalis Spyrouf63885b2019-01-16 14:18:09 +000036#include "arm_compute/core/Utils.h"
Manuel Bottini053e7512018-12-28 15:05:20 +000037
38namespace arm_compute
39{
40namespace
41{
42template <typename T>
43void range_function(ITensor *output, float start, float step, const Window &window)
44{
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +000045 /** SIMD vector tag type. */
Manuel Bottini053e7512018-12-28 15:05:20 +000046 using ExactTagType = typename wrapper::traits::neon_bitvector<T, wrapper::traits::BitWidth::W128>::tag_type;
47
48 const auto step_vec = wrapper::vdup_n(static_cast<T>(step), ExactTagType{});
49 const auto start_vec = wrapper::vdup_n(static_cast<T>(start), ExactTagType{});
50 auto id_vec = wrapper::vdup_n(static_cast<T>(0.f), ExactTagType{});
51
Michalis Spyroucd5b4a32020-02-27 15:35:53 +000052 const auto window_start_x = static_cast<int>(window.x().start());
53 const auto window_end_x = static_cast<int>(window.x().end());
54 const int window_step_x = 16 / sizeof(T);
55
56 Window win{ window };
57 win.set(Window::DimX, Window::Dimension(0, 1, 1));
58 Iterator output_it(output, win);
59
60 execute_window_loop(win, [&](const Coordinates &)
Manuel Bottini053e7512018-12-28 15:05:20 +000061 {
Michalis Spyroucd5b4a32020-02-27 15:35:53 +000062 int x = window_start_x;
Manuel Bottini053e7512018-12-28 15:05:20 +000063 const auto out_ptr = reinterpret_cast<T *>(output_it.ptr());
Michalis Spyroucd5b4a32020-02-27 15:35:53 +000064 for(; x <= (window_end_x - window_step_x); x += window_step_x)
65 {
Michalis Spyrou10b49c72020-04-02 13:12:32 +010066 for(int count = 0; count < window_step_x; ++count)
Michalis Spyroucd5b4a32020-02-27 15:35:53 +000067 {
68 id_vec = wrapper::vsetlane(static_cast<T>(x + count), id_vec, count);
69 }
70
71 // start + step * id
72 const auto res_vec = wrapper::vmla(start_vec, id_vec, step_vec);
73 wrapper::vstore(out_ptr + x, res_vec);
74 }
75
76 // Compute left-over elements
77 for(; x < window_end_x; ++x)
78 {
79 const auto res = start + x * step;
80 *(out_ptr + x) = res;
81 }
82
Manuel Bottini053e7512018-12-28 15:05:20 +000083 },
84 output_it);
85}
86
87Status validate_arguments(const ITensorInfo &output, const float start, const float end, const float step)
88{
89 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&output,
90 1,
91 DataType::U8, DataType::S8,
92 DataType::U16, DataType::S16,
93 DataType::U32, DataType::S32,
94 DataType::F16, DataType::F32);
95
96 ARM_COMPUTE_RETURN_ERROR_ON_MSG((start == end), "start of the requested sequence must not be equal to the end");
97 ARM_COMPUTE_RETURN_ERROR_ON_MSG(((start < end) && (step <= 0)), "step must be greater than 0 when start < end");
98 ARM_COMPUTE_RETURN_ERROR_ON_MSG(((start > end) && (step >= 0)), "step must be less than 0 when start > end");
99
Michalis Spyrouf63885b2019-01-16 14:18:09 +0000100 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");
101 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");
102 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 +0000103
104 ARM_COMPUTE_RETURN_ERROR_ON_MSG((start == end), "start of the requested sequence must not be equal to the end");
105
106 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 +0000107 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 +0000108
109 return Status{};
110}
Manuel Bottini053e7512018-12-28 15:05:20 +0000111} // namespace
112
113NERangeKernel::NERangeKernel()
114 : _func(nullptr), _start(0), _end(1), _step(1), _output(nullptr)
115{
116}
117
118void NERangeKernel::configure(ITensor *output, float start, float end, float step)
119{
120 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
121
122 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*(output->info()), start, end, step));
123
Michalis Spyroucd5b4a32020-02-27 15:35:53 +0000124 // Auto initialize output if not initialized
125 auto_init_if_empty(*output->info(), TensorShape(num_of_elements_in_range(start, end, step)), 1, output->info()->data_type(), output->info()->quantization_info());
126
Manuel Bottini053e7512018-12-28 15:05:20 +0000127 // Configure kernel window
SiCongLib88272e2021-02-24 15:40:57 +0000128 Window win = calculate_max_window(*output->info(), Steps());
Manuel Bottini053e7512018-12-28 15:05:20 +0000129
130 _start = start;
131 _end = end;
132 _step = step;
133 _output = output;
134 switch(_output->info()->data_type())
135 {
136 case DataType::U8:
137 _func = &range_function<uint8_t>;
138 break;
139 case DataType::U16:
140 _func = &range_function<uint16_t>;
141 break;
142 case DataType::U32:
143 _func = &range_function<uint32_t>;
144 break;
145 case DataType::S8:
146 _func = &range_function<int8_t>;
147 break;
148 case DataType::S16:
149 _func = &range_function<int16_t>;
150 break;
151 case DataType::S32:
152 _func = &range_function<int32_t>;
153 break;
154 case DataType::F32:
155 _func = &range_function<float>;
156 break;
157#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
158 case DataType::F16:
159 _func = &range_function<float16_t>;
160 break;
161#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
162 default:
163 ARM_COMPUTE_ERROR("Unsupported data type.");
164 break;
165 }
166
Michalis Spyroucd5b4a32020-02-27 15:35:53 +0000167 INEKernel::configure(win);
Manuel Bottini053e7512018-12-28 15:05:20 +0000168}
169
170Status NERangeKernel::validate(const ITensorInfo *output, float start, float end, float step)
171{
172 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
173
174 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*output, start, end, step));
Manuel Bottini053e7512018-12-28 15:05:20 +0000175
176 return Status{};
177}
178
179void NERangeKernel::run(const Window &window, const ThreadInfo &info)
180{
181 ARM_COMPUTE_UNUSED(info);
182 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
183 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
184 ARM_COMPUTE_ERROR_ON(_func == nullptr);
185
186 (*_func)(_output, _start, _step, window);
187}
Sheri Zhangac6499a2021-02-10 15:32:38 +0000188} // namespace arm_compute