blob: 189e77f0fcc8090d1686cc8e82c80259b80a5522 [file] [log] [blame]
Manuel Bottini053e7512018-12-28 15:05:20 +00001/*
2 * Copyright (c) 2018-2019 ARM Limited.
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 */
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{
44 const unsigned int num_elems_processed_per_iteration = 16 / sizeof(T);
45 /** NEON vector tag type. */
46 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
52 Iterator output_it(output, window);
53 execute_window_loop(window, [&](const Coordinates & id)
54 {
55 for(unsigned int count = 0; count < num_elems_processed_per_iteration; ++count)
56 {
57 id_vec = wrapper::vsetlane(static_cast<T>(id.x() + count), id_vec, count);
58 }
59 // start + step * id
60 const auto res_vec = wrapper::vmla(start_vec, id_vec, step_vec);
61 const auto out_ptr = reinterpret_cast<T *>(output_it.ptr());
62 wrapper::vstore(out_ptr, res_vec);
63 },
64 output_it);
65}
66
67Status validate_arguments(const ITensorInfo &output, const float start, const float end, const float step)
68{
69 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&output,
70 1,
71 DataType::U8, DataType::S8,
72 DataType::U16, DataType::S16,
73 DataType::U32, DataType::S32,
74 DataType::F16, DataType::F32);
75
76 ARM_COMPUTE_RETURN_ERROR_ON_MSG((start == end), "start of the requested sequence must not be equal to the end");
77 ARM_COMPUTE_RETURN_ERROR_ON_MSG(((start < end) && (step <= 0)), "step must be greater than 0 when start < end");
78 ARM_COMPUTE_RETURN_ERROR_ON_MSG(((start > end) && (step >= 0)), "step must be less than 0 when start > end");
79
Michalis Spyrouf63885b2019-01-16 14:18:09 +000080 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");
81 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");
82 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 +000083
84 ARM_COMPUTE_RETURN_ERROR_ON_MSG((start == end), "start of the requested sequence must not be equal to the end");
85
86 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 +000087 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 +000088
89 return Status{};
90}
91
92std::pair<Status, Window> validate_and_configure_window(ITensorInfo &output, const float start, const float end, const float step)
93{
94 const unsigned int num_elems_processed_per_iteration = 16 / output.element_size();
95
96 // Auto initialize output if not initialized
Michalis Spyrouf63885b2019-01-16 14:18:09 +000097 auto_init_if_empty(output, TensorShape(num_of_elements_in_range(start, end, step)), 1, output.data_type(), output.quantization_info());
Manuel Bottini053e7512018-12-28 15:05:20 +000098
99 // Configure kernel window
100 Window win = calculate_max_window(output, Steps(num_elems_processed_per_iteration));
101 AccessWindowHorizontal output_access(&output, 0, num_elems_processed_per_iteration);
102 bool window_changed = update_window_and_padding(win, output_access);
Michalis Spyrouf63885b2019-01-16 14:18:09 +0000103 output_access.set_valid_region(win, ValidRegion(Coordinates(), TensorShape(num_of_elements_in_range(start, end, step))));
Manuel Bottini053e7512018-12-28 15:05:20 +0000104 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
105 return std::make_pair(err, win);
106}
107} // namespace
108
109NERangeKernel::NERangeKernel()
110 : _func(nullptr), _start(0), _end(1), _step(1), _output(nullptr)
111{
112}
113
114void NERangeKernel::configure(ITensor *output, float start, float end, float step)
115{
116 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
117
118 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*(output->info()), start, end, step));
119
120 // Configure kernel window
121 auto win_config = validate_and_configure_window(*(output->info()), start, end, step);
122 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
123
124 _start = start;
125 _end = end;
126 _step = step;
127 _output = output;
128 switch(_output->info()->data_type())
129 {
130 case DataType::U8:
131 _func = &range_function<uint8_t>;
132 break;
133 case DataType::U16:
134 _func = &range_function<uint16_t>;
135 break;
136 case DataType::U32:
137 _func = &range_function<uint32_t>;
138 break;
139 case DataType::S8:
140 _func = &range_function<int8_t>;
141 break;
142 case DataType::S16:
143 _func = &range_function<int16_t>;
144 break;
145 case DataType::S32:
146 _func = &range_function<int32_t>;
147 break;
148 case DataType::F32:
149 _func = &range_function<float>;
150 break;
151#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
152 case DataType::F16:
153 _func = &range_function<float16_t>;
154 break;
155#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
156 default:
157 ARM_COMPUTE_ERROR("Unsupported data type.");
158 break;
159 }
160
161 INEKernel::configure(win_config.second);
162}
163
164Status NERangeKernel::validate(const ITensorInfo *output, float start, float end, float step)
165{
166 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
167
168 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*output, start, end, step));
169 ARM_COMPUTE_RETURN_ON_ERROR((validate_and_configure_window(*(output->clone()), start, end, step)).first);
170
171 return Status{};
172}
173
174void NERangeKernel::run(const Window &window, const ThreadInfo &info)
175{
176 ARM_COMPUTE_UNUSED(info);
177 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
178 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
179 ARM_COMPUTE_ERROR_ON(_func == nullptr);
180
181 (*_func)(_output, _start, _step, window);
182}
183} // namespace arm_compute