blob: 5d3af3b03d34c50fdae24bf97429bbf5ceb6b73a [file] [log] [blame]
George Wort5a97b282018-12-21 16:21:04 +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/NEElementwiseUnaryKernel.h"
25
26#include "arm_compute/core/CPP/Validate.h"
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/IAccessWindow.h"
30#include "arm_compute/core/ITensor.h"
31#include "arm_compute/core/NEON/NEAsymm.h"
32#include "arm_compute/core/NEON/NEFixedPoint.h"
Usama Arif0a5a57a2019-05-23 14:20:33 +010033#include "arm_compute/core/NEON/NEMath.h"
George Wort5a97b282018-12-21 16:21:04 +000034#include "arm_compute/core/NEON/wrapper/wrapper.h"
35#include "arm_compute/core/TensorInfo.h"
36#include "arm_compute/core/Validate.h"
Michalis Spyroue6bcb5b2019-06-07 11:47:16 +010037#include "support/ToolchainSupport.h"
George Wort5a97b282018-12-21 16:21:04 +000038
39#include <algorithm>
40#include <arm_neon.h>
41#include <cstdint>
42#include <map>
43#include <string>
44
45namespace arm_compute
46{
47class Coordinates;
48
49namespace
50{
51template <ElementWiseUnary op, typename ScalarType>
52inline ScalarType elementwise_op_scalar(const ScalarType &a)
53{
54 switch(op)
55 {
56 case ElementWiseUnary::RSQRT:
57 return 1 / sqrt(a);
58 case ElementWiseUnary::EXP:
59 return std::exp(a);
Usama Ariff6e475c2019-05-10 12:06:28 +010060 case ElementWiseUnary::NEG:
61 return -a;
Usama Arifc255aa72019-05-13 16:26:29 +010062 case ElementWiseUnary::LOG:
63 return std::log(a);
Manuel Bottini6ac59922019-05-15 14:06:02 +010064 case ElementWiseUnary::ABS:
65 return std::abs(a);
Usama Arif0a5a57a2019-05-23 14:20:33 +010066 case ElementWiseUnary::ROUND:
Michalis Spyroue6bcb5b2019-06-07 11:47:16 +010067 return support::cpp11::nearbyint(a);
Manuel Bottinied753262019-05-15 15:30:47 +010068 case ElementWiseUnary::SIN:
69 return std::sin(a);
George Wort5a97b282018-12-21 16:21:04 +000070 default:
71 ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
72 }
73}
74
Usama Ariff6e475c2019-05-10 12:06:28 +010075/* Elementwise operations that are supported for float */
Manuel Bottinied753262019-05-15 15:30:47 +010076template <ElementWiseUnary op, typename ScalarType, bool is_float, typename VectorType, typename std::enable_if<is_float, int>::type = 0>
George Wort5a97b282018-12-21 16:21:04 +000077inline VectorType elementwise_op(const VectorType &a)
78{
79 switch(op)
80 {
81 case ElementWiseUnary::RSQRT:
82 return wrapper::vinvsqrt(a);
83 case ElementWiseUnary::EXP:
84 return wrapper::vexpq(a);
Usama Ariff6e475c2019-05-10 12:06:28 +010085 case ElementWiseUnary::NEG:
86 return wrapper::vneg(a);
Usama Arifc255aa72019-05-13 16:26:29 +010087 case ElementWiseUnary::LOG:
88 return wrapper::vlog(a);
Manuel Bottini6ac59922019-05-15 14:06:02 +010089 case ElementWiseUnary::ABS:
90 return wrapper::vabs(a);
Usama Arif0a5a57a2019-05-23 14:20:33 +010091 case ElementWiseUnary::ROUND:
92 return wrapper::vround(a);
Manuel Bottinied753262019-05-15 15:30:47 +010093 case ElementWiseUnary::SIN:
94 return wrapper::vsin(a);
George Wort5a97b282018-12-21 16:21:04 +000095 default:
96 ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
97 }
98}
99
Usama Ariff6e475c2019-05-10 12:06:28 +0100100/* Elementwise operations that are supported for non floats */
Manuel Bottinied753262019-05-15 15:30:47 +0100101template < ElementWiseUnary op, typename ScalarType, bool is_float, typename VectorType, typename std::enable_if < !is_float, int >::type = 0 >
Usama Ariff6e475c2019-05-10 12:06:28 +0100102inline VectorType elementwise_op(const VectorType &a)
103{
104 switch(op)
105 {
106 case ElementWiseUnary::NEG:
107 return wrapper::vneg(a);
Manuel Bottini6ac59922019-05-15 14:06:02 +0100108 case ElementWiseUnary::ABS:
109 return wrapper::vabs(a);
Usama Ariff6e475c2019-05-10 12:06:28 +0100110 default:
111 ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
112 }
113}
114
115template <ElementWiseUnary op, typename ScalarType, bool is_float>
George Wort5a97b282018-12-21 16:21:04 +0000116void elementwise_op(const ITensor *in, ITensor *out, const Window &window)
117{
118 const int window_step_x = 16 / sizeof(ScalarType);
119 const auto window_start_x = static_cast<int>(window.x().start());
120 const auto window_end_x = static_cast<int>(window.x().end());
121
122 Window win = window;
123 win.set(Window::DimX, Window::Dimension(0, 1, 1));
124
125 Iterator input(in, win);
126 Iterator output(out, win);
127
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100128 execute_window_loop(win, [&](const Coordinates &)
George Wort5a97b282018-12-21 16:21:04 +0000129 {
130 auto output_ptr = reinterpret_cast<ScalarType *>(output.ptr());
131 const auto input_ptr = reinterpret_cast<const ScalarType *>(input.ptr());
132
133 int x = window_start_x;
134 for(; x <= window_end_x - window_step_x; x += window_step_x)
135 {
Manuel Bottinied753262019-05-15 15:30:47 +0100136 wrapper::vstore(output_ptr + x, elementwise_op<op, ScalarType, is_float>(wrapper::vloadq(input_ptr + x)));
George Wort5a97b282018-12-21 16:21:04 +0000137 }
138 for(; x < window_end_x; ++x)
139 {
140 *(output_ptr + x) = elementwise_op_scalar<op>(*(input_ptr + x));
141 }
142 },
143 input, output);
144}
145
146template <ElementWiseUnary op>
147std::function<void(const ITensor *input, ITensor *output, const Window &window)>
148configure_func(const ITensor *input, ITensor *output)
149{
150 std::string function_to_call("op_");
151 function_to_call += string_from_data_type(input->info()->data_type()) + "_";
152 function_to_call += string_from_data_type(output->info()->data_type());
153
154 static std::map<std::string, NEElementwiseUnaryKernel::ElementwiseUnaryFunction *> map_function =
155 {
Usama Ariff6e475c2019-05-10 12:06:28 +0100156 { "op_F32_F32", &elementwise_op<op, float, true> },
157 { "op_S32_S32", &elementwise_op<op, int32_t, false> },
George Wort5a97b282018-12-21 16:21:04 +0000158 };
159#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Usama Ariff6e475c2019-05-10 12:06:28 +0100160 map_function["op_F16_F16"] = &elementwise_op<op, float16_t, true>;
George Wort5a97b282018-12-21 16:21:04 +0000161#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
162
163 auto it = map_function.find(function_to_call);
164
165 if(it != map_function.end())
166 {
167 auto func = it->second;
168 return [func](const ITensor * input, ITensor * output, const Window & window)
169 {
170 func(input, output, window);
171 };
172 }
173 return nullptr;
174}
175} // namespace
176
177NEElementwiseUnaryKernel::NEElementwiseUnaryKernel()
178 : _function(nullptr), _input(nullptr), _output(nullptr)
179{
180}
181
182void NEElementwiseUnaryKernel::configure(ElementWiseUnary op, const ITensor *input, ITensor *output)
183{
Usama Ariff6e475c2019-05-10 12:06:28 +0100184 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(op, *input->info(), *output->info()));
George Wort5a97b282018-12-21 16:21:04 +0000185 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
186
187 // Configure kernel window
188 const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(*input->info());
189 const TensorShape &out_shape = broadcast_pair.first;
190 const ValidRegion &valid_region = broadcast_pair.second;
191
192 // Auto initialize output if not initialized
193 auto_init_if_empty(*output->info(), out_shape, 1, input->info()->data_type());
194
195 Window win = calculate_max_window(valid_region);
196
197 _input = input;
198 _output = output;
199
200 INEKernel::configure(win);
201
202 switch(op)
203 {
204 case ElementWiseUnary::RSQRT:
205 _function = configure_func<ElementWiseUnary::RSQRT>(input, output);
206 break;
207 case ElementWiseUnary::EXP:
208 _function = configure_func<ElementWiseUnary::EXP>(input, output);
209 break;
Usama Ariff6e475c2019-05-10 12:06:28 +0100210 case ElementWiseUnary::NEG:
211 _function = configure_func<ElementWiseUnary::NEG>(input, output);
212 break;
Usama Arifc255aa72019-05-13 16:26:29 +0100213 case ElementWiseUnary::LOG:
214 _function = configure_func<ElementWiseUnary::LOG>(input, output);
215 break;
Manuel Bottini6ac59922019-05-15 14:06:02 +0100216 case ElementWiseUnary::ABS:
217 _function = configure_func<ElementWiseUnary::ABS>(input, output);
218 break;
Usama Arif0a5a57a2019-05-23 14:20:33 +0100219 case ElementWiseUnary::ROUND:
220 _function = configure_func<ElementWiseUnary::ROUND>(input, output);
221 break;
Manuel Bottinied753262019-05-15 15:30:47 +0100222 case ElementWiseUnary::SIN:
223 _function = configure_func<ElementWiseUnary::SIN>(input, output);
224 break;
George Wort5a97b282018-12-21 16:21:04 +0000225 default:
226 ARM_COMPUTE_ERROR("NOT_SUPPORTED!");
227 }
228}
229
Usama Ariff6e475c2019-05-10 12:06:28 +0100230Status NEElementwiseUnaryKernel::validate_arguments(ElementWiseUnary op, const ITensorInfo &input, const ITensorInfo &output)
George Wort5a97b282018-12-21 16:21:04 +0000231{
232 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(&input);
Usama Ariff6e475c2019-05-10 12:06:28 +0100233 switch(op)
234 {
235 case ElementWiseUnary::EXP:
236 case ElementWiseUnary::RSQRT:
Usama Arifc255aa72019-05-13 16:26:29 +0100237 case ElementWiseUnary::LOG:
Usama Arif0a5a57a2019-05-23 14:20:33 +0100238 case ElementWiseUnary::ROUND:
Manuel Bottinied753262019-05-15 15:30:47 +0100239 case ElementWiseUnary::SIN:
Usama Ariff6e475c2019-05-10 12:06:28 +0100240 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input, 1, DataType::F16, DataType::F32);
241 break;
242 case ElementWiseUnary::NEG:
Manuel Bottini6ac59922019-05-15 14:06:02 +0100243 case ElementWiseUnary::ABS:
Usama Ariff6e475c2019-05-10 12:06:28 +0100244 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input, 1, DataType::F16, DataType::F32, DataType::S32);
245 break;
246 default:
Manuel Bottini6ac59922019-05-15 14:06:02 +0100247 ARM_COMPUTE_ERROR("ElementWiseUnary operation not supported");
Usama Ariff6e475c2019-05-10 12:06:28 +0100248 }
George Wort5a97b282018-12-21 16:21:04 +0000249 // Validate in case of configured output
250 if(output.total_size() > 0)
251 {
252 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input, &output);
253 }
254
255 return Status{};
256}
257
258Status NEElementwiseUnaryKernel::validate(ElementWiseUnary op, const ITensorInfo *input, const ITensorInfo *output)
259{
George Wort5a97b282018-12-21 16:21:04 +0000260 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Usama Ariff6e475c2019-05-10 12:06:28 +0100261 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(op, *input, *output));
George Wort5a97b282018-12-21 16:21:04 +0000262 return Status{};
263}
264
265void NEElementwiseUnaryKernel::run(const Window &window, const ThreadInfo &info)
266{
267 ARM_COMPUTE_UNUSED(info);
268 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
269 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
270 ARM_COMPUTE_ERROR_ON(_function == nullptr);
271 _function(_input, _output, window);
272}
273} // namespace arm_compute