Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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/NEArithmeticAdditionKernel.h" |
| 25 | |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CPP/Validate.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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" |
Michele Di Giorgio | 81f0d15 | 2017-07-11 15:00:52 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/NEON/NEFixedPoint.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/TensorInfo.h" |
| 33 | #include "arm_compute/core/Validate.h" |
| 34 | |
| 35 | #include <algorithm> |
| 36 | #include <arm_neon.h> |
| 37 | #include <cstdint> |
| 38 | #include <map> |
| 39 | #include <string> |
| 40 | |
| 41 | using namespace arm_compute; |
| 42 | |
| 43 | namespace arm_compute |
| 44 | { |
| 45 | class Coordinates; |
| 46 | } // namespace arm_compute |
| 47 | |
| 48 | namespace |
| 49 | { |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 50 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 51 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 52 | void add_wrap_U8_U8_U8(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 53 | { |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 54 | Iterator input1(in1, window.broadcast_if_dimension_le_one(in1->info()->tensor_shape())); |
| 55 | Iterator input2(in2, window.broadcast_if_dimension_le_one(in2->info()->tensor_shape())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 56 | Iterator output(out, window); |
| 57 | |
| 58 | execute_window_loop(window, [&](const Coordinates & id) |
| 59 | { |
| 60 | vst1q_u8(output.ptr(), vaddq_u8(vld1q_u8(input1.ptr()), vld1q_u8(input2.ptr()))); |
| 61 | }, |
| 62 | input1, input2, output); |
| 63 | } |
| 64 | |
| 65 | void add_saturate_U8_U8_U8(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 66 | { |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 67 | Iterator input1(in1, window.broadcast_if_dimension_le_one(in1->info()->tensor_shape())); |
| 68 | Iterator input2(in2, window.broadcast_if_dimension_le_one(in2->info()->tensor_shape())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 69 | Iterator output(out, window); |
| 70 | |
| 71 | execute_window_loop(window, [&](const Coordinates & id) |
| 72 | { |
| 73 | vst1q_u8(output.ptr(), vqaddq_u8(vld1q_u8(input1.ptr()), vld1q_u8(input2.ptr()))); |
| 74 | }, |
| 75 | input1, input2, output); |
| 76 | } |
| 77 | |
| 78 | inline int16x8x2_t vadd2q_s16(const int16x8x2_t &a, const int16x8x2_t &b) |
| 79 | { |
| 80 | const int16x8x2_t res = |
| 81 | { |
| 82 | { |
| 83 | vaddq_s16(a.val[0], b.val[0]), |
| 84 | vaddq_s16(a.val[1], b.val[1]) |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | return res; |
| 89 | } |
| 90 | |
| 91 | inline float32x4x4_t vadd4q_f32(const float32x4x4_t &a, const float32x4x4_t &b) |
| 92 | { |
| 93 | const float32x4x4_t res = |
| 94 | { |
| 95 | { |
| 96 | vaddq_f32(a.val[0], b.val[0]), |
| 97 | vaddq_f32(a.val[1], b.val[1]), |
| 98 | vaddq_f32(a.val[2], b.val[2]), |
| 99 | vaddq_f32(a.val[3], b.val[3]) |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | return res; |
| 104 | } |
| 105 | |
| 106 | inline int16x8x2_t vqadd2q_s16(const int16x8x2_t &a, const int16x8x2_t &b) |
| 107 | { |
| 108 | const int16x8x2_t res = |
| 109 | { |
| 110 | { |
| 111 | vqaddq_s16(a.val[0], b.val[0]), |
| 112 | vqaddq_s16(a.val[1], b.val[1]) |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | return res; |
| 117 | } |
| 118 | |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 119 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Pablo Tello | d1b0ecc | 2017-07-11 11:27:04 +0100 | [diff] [blame] | 120 | inline float16x8x2_t vadd2q_f16(const float16x8x2_t &a, const float16x8x2_t &b) |
| 121 | { |
| 122 | const float16x8x2_t res = |
| 123 | { |
| 124 | { |
| 125 | vaddq_f16(a.val[0], b.val[0]), |
| 126 | vaddq_f16(a.val[1], b.val[1]) |
| 127 | } |
| 128 | }; |
| 129 | |
| 130 | return res; |
| 131 | } |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 132 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Pablo Tello | d1b0ecc | 2017-07-11 11:27:04 +0100 | [diff] [blame] | 133 | |
| 134 | void add_F16_F16_F16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 135 | { |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 136 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 137 | Iterator input1(in1, window.broadcast_if_dimension_le_one(in1->info()->tensor_shape())); |
| 138 | Iterator input2(in2, window.broadcast_if_dimension_le_one(in2->info()->tensor_shape())); |
Pablo Tello | d1b0ecc | 2017-07-11 11:27:04 +0100 | [diff] [blame] | 139 | Iterator output(out, window); |
| 140 | |
| 141 | execute_window_loop(window, [&](const Coordinates & id) |
| 142 | { |
| 143 | const float16x8x2_t a = vld2q_f16(reinterpret_cast<const float16_t *>(input1.ptr())); |
| 144 | const float16x8x2_t b = vld2q_f16(reinterpret_cast<const float16_t *>(input2.ptr())); |
| 145 | |
| 146 | vst2q_f16(reinterpret_cast<float16_t *>(output.ptr()), vadd2q_f16(a, b)); |
| 147 | }, |
| 148 | input1, input2, output); |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 149 | #else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Pablo Tello | d1b0ecc | 2017-07-11 11:27:04 +0100 | [diff] [blame] | 150 | ARM_COMPUTE_UNUSED(in1); |
| 151 | ARM_COMPUTE_UNUSED(in2); |
| 152 | ARM_COMPUTE_UNUSED(out); |
| 153 | ARM_COMPUTE_UNUSED(window); |
| 154 | ARM_COMPUTE_ERROR("Not supported, recompile the library with arch=arm64-v8.2-a"); |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 155 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Pablo Tello | d1b0ecc | 2017-07-11 11:27:04 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 158 | void add_F32_F32_F32(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 159 | { |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 160 | Iterator input1(in1, window.broadcast_if_dimension_le_one(in1->info()->tensor_shape())); |
| 161 | Iterator input2(in2, window.broadcast_if_dimension_le_one(in2->info()->tensor_shape())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 162 | Iterator output(out, window); |
| 163 | |
| 164 | execute_window_loop(window, [&](const Coordinates & id) |
| 165 | { |
| 166 | const float32x4x4_t a = vld4q_f32(reinterpret_cast<const float *>(input1.ptr())); |
| 167 | const float32x4x4_t b = vld4q_f32(reinterpret_cast<const float *>(input2.ptr())); |
| 168 | |
| 169 | vst4q_f32(reinterpret_cast<float *>(output.ptr()), vadd4q_f32(a, b)); |
| 170 | }, |
| 171 | input1, input2, output); |
| 172 | } |
| 173 | |
| 174 | void add_wrap_S16_S16_S16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 175 | { |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 176 | Iterator input1(in1, window.broadcast_if_dimension_le_one(in1->info()->tensor_shape())); |
| 177 | Iterator input2(in2, window.broadcast_if_dimension_le_one(in2->info()->tensor_shape())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 178 | Iterator output(out, window); |
| 179 | |
| 180 | execute_window_loop(window, [&](const Coordinates & id) |
| 181 | { |
| 182 | const int16x8x2_t a = vld2q_s16(reinterpret_cast<const int16_t *>(input1.ptr())); |
| 183 | const int16x8x2_t b = vld2q_s16(reinterpret_cast<const int16_t *>(input2.ptr())); |
| 184 | |
| 185 | vst2q_s16(reinterpret_cast<int16_t *>(output.ptr()), vadd2q_s16(a, b)); |
| 186 | }, |
| 187 | input1, input2, output); |
| 188 | } |
| 189 | |
| 190 | void add_saturate_S16_S16_S16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 191 | { |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 192 | Iterator input1(in1, window.broadcast_if_dimension_le_one(in1->info()->tensor_shape())); |
| 193 | Iterator input2(in2, window.broadcast_if_dimension_le_one(in2->info()->tensor_shape())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 194 | Iterator output(out, window); |
| 195 | |
| 196 | execute_window_loop(window, [&](const Coordinates & id) |
| 197 | { |
| 198 | const int16x8x2_t a = vld2q_s16(reinterpret_cast<const int16_t *>(input1.ptr())); |
| 199 | const int16x8x2_t b = vld2q_s16(reinterpret_cast<const int16_t *>(input2.ptr())); |
| 200 | |
| 201 | vst2q_s16(reinterpret_cast<int16_t *>(output.ptr()), vqadd2q_s16(a, b)); |
| 202 | }, |
| 203 | input1, input2, output); |
| 204 | } |
| 205 | |
| 206 | void add_wrap_S16_U8_S16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 207 | { |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 208 | Iterator input1(in1, window.broadcast_if_dimension_le_one(in1->info()->tensor_shape())); |
| 209 | Iterator input2(in2, window.broadcast_if_dimension_le_one(in2->info()->tensor_shape())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 210 | Iterator output(out, window); |
| 211 | |
| 212 | execute_window_loop(window, [&](const Coordinates & id) |
| 213 | { |
| 214 | const int16x8x2_t a = |
| 215 | { |
| 216 | { |
| 217 | vld1q_s16(reinterpret_cast<const int16_t *>(input1.ptr())), |
| 218 | vld1q_s16(reinterpret_cast<const int16_t *>(input1.ptr()) + 8) |
| 219 | } |
| 220 | }; |
| 221 | const uint8x16_t b = vld1q_u8(input2.ptr()); |
| 222 | |
| 223 | vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()), vaddq_s16(a.val[0], vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(b))))); |
| 224 | vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()) + 8, vaddq_s16(a.val[1], vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(b))))); |
| 225 | }, |
| 226 | input1, input2, output); |
| 227 | } |
| 228 | |
| 229 | void add_saturate_S16_U8_S16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 230 | { |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 231 | Iterator input1(in1, window.broadcast_if_dimension_le_one(in1->info()->tensor_shape())); |
| 232 | Iterator input2(in2, window.broadcast_if_dimension_le_one(in2->info()->tensor_shape())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 233 | Iterator output(out, window); |
| 234 | |
| 235 | execute_window_loop(window, [&](const Coordinates & id) |
| 236 | { |
| 237 | const int16x8x2_t a = |
| 238 | { |
| 239 | { |
| 240 | vld1q_s16(reinterpret_cast<const int16_t *>(input1.ptr())), |
| 241 | vld1q_s16(reinterpret_cast<const int16_t *>(input1.ptr()) + 8) |
| 242 | } |
| 243 | }; |
| 244 | const uint8x16_t b = vld1q_u8(input2.ptr()); |
| 245 | |
| 246 | vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()), vqaddq_s16(a.val[0], vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(b))))); |
| 247 | vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()) + 8, vqaddq_s16(a.val[1], vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(b))))); |
| 248 | }, |
| 249 | input1, input2, output); |
| 250 | } |
| 251 | |
| 252 | inline void add_wrap_U8_S16_S16(const ITensor *input1, const ITensor *input2, ITensor *output, const Window &window) |
| 253 | { |
| 254 | //Simply swap the two input buffers: |
| 255 | add_wrap_S16_U8_S16(input2, input1, output, window); |
| 256 | } |
| 257 | |
| 258 | inline void add_saturate_U8_S16_S16(const ITensor *input1, const ITensor *input2, ITensor *output, const Window &window) |
| 259 | { |
| 260 | //Simply swap the two input buffers: |
| 261 | add_saturate_S16_U8_S16(input2, input1, output, window); |
| 262 | } |
| 263 | |
| 264 | void add_wrap_U8_U8_S16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 265 | { |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 266 | Iterator input1(in1, window.broadcast_if_dimension_le_one(in1->info()->tensor_shape())); |
| 267 | Iterator input2(in2, window.broadcast_if_dimension_le_one(in2->info()->tensor_shape())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 268 | Iterator output(out, window); |
| 269 | |
| 270 | execute_window_loop(window, [&](const Coordinates & id) |
| 271 | { |
| 272 | const uint8x16_t a = vld1q_u8(input1.ptr()); |
| 273 | const uint8x16_t b = vld1q_u8(input2.ptr()); |
| 274 | |
| 275 | const int16x8x2_t a_s16 = |
| 276 | { |
| 277 | { |
| 278 | vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(a))), |
| 279 | vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(a))) |
| 280 | } |
| 281 | }; |
| 282 | |
| 283 | const int16x8x2_t b_s16 = |
| 284 | { |
| 285 | { |
| 286 | vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(b))), |
| 287 | vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(b))) |
| 288 | } |
| 289 | }; |
| 290 | |
| 291 | vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()), vaddq_s16(a_s16.val[0], b_s16.val[0])); |
| 292 | vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()) + 8, vaddq_s16(a_s16.val[1], b_s16.val[1])); |
| 293 | }, |
| 294 | input1, input2, output); |
| 295 | } |
| 296 | |
| 297 | void add_saturate_U8_U8_S16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 298 | { |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 299 | Iterator input1(in1, window.broadcast_if_dimension_le_one(in1->info()->tensor_shape())); |
| 300 | Iterator input2(in2, window.broadcast_if_dimension_le_one(in2->info()->tensor_shape())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 301 | Iterator output(out, window); |
| 302 | |
| 303 | execute_window_loop(window, [&](const Coordinates & id) |
| 304 | { |
| 305 | const uint8x16_t a = vld1q_u8(input1.ptr()); |
| 306 | const uint8x16_t b = vld1q_u8(input2.ptr()); |
| 307 | |
| 308 | const int16x8x2_t a_s16 = |
| 309 | { |
| 310 | { |
| 311 | vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(a))), |
| 312 | vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(a))) |
| 313 | } |
| 314 | }; |
| 315 | |
| 316 | const int16x8x2_t b_s16 = |
| 317 | { |
| 318 | { |
| 319 | vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(b))), |
| 320 | vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(b))) |
| 321 | } |
| 322 | }; |
| 323 | |
| 324 | vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()), vqaddq_s16(a_s16.val[0], b_s16.val[0])); |
| 325 | vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()) + 8, vqaddq_s16(a_s16.val[1], b_s16.val[1])); |
| 326 | }, |
| 327 | input1, input2, output); |
| 328 | } |
Ioan-Cristian Szabo | 397d58a | 2017-11-30 15:19:11 +0000 | [diff] [blame] | 329 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 330 | Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output, ConvertPolicy policy) |
Ioan-Cristian Szabo | 397d58a | 2017-11-30 15:19:11 +0000 | [diff] [blame] | 331 | { |
| 332 | ARM_COMPUTE_UNUSED(policy); |
Ioan-Cristian Szabo | 397d58a | 2017-11-30 15:19:11 +0000 | [diff] [blame] | 333 | |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 334 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(&input1); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 335 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input1, 1, DataType::U8, DataType::S16, DataType::F16, DataType::F32); |
| 336 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input2, 1, DataType::U8, DataType::S16, DataType::F16, DataType::F32); |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 337 | |
| 338 | const TensorShape out_shape = TensorShape::broadcast_shape(input1.tensor_shape(), input2.tensor_shape()); |
| 339 | |
| 340 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible"); |
| 341 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 342 | // Validate in case of configured output |
| 343 | if(output.total_size() > 0) |
| 344 | { |
| 345 | ARM_COMPUTE_RETURN_ERROR_ON_MSG( |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 346 | !(input1.data_type() == DataType::U8 && input2.data_type() == DataType::U8 && output.data_type() == DataType::U8) |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 347 | && !(input1.data_type() == DataType::U8 && input2.data_type() == DataType::U8 && output.data_type() == DataType::S16) |
| 348 | && !(input1.data_type() == DataType::U8 && input2.data_type() == DataType::S16 && output.data_type() == DataType::S16) |
| 349 | && !(input1.data_type() == DataType::S16 && input2.data_type() == DataType::U8 && output.data_type() == DataType::S16) |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 350 | && !(input1.data_type() == DataType::S16 && input2.data_type() == DataType::S16 && output.data_type() == DataType::S16) |
| 351 | && !(input1.data_type() == DataType::F32 && input2.data_type() == DataType::F32 && output.data_type() == DataType::F32) |
| 352 | && !(input1.data_type() == DataType::F16 && input2.data_type() == DataType::F16 && output.data_type() == DataType::F16), |
| 353 | "You called addition with the wrong image formats"); |
| 354 | |
| 355 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, output.tensor_shape(), 0), |
| 356 | "Wrong shape for output"); |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 357 | } |
Ioan-Cristian Szabo | 397d58a | 2017-11-30 15:19:11 +0000 | [diff] [blame] | 358 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 359 | return Status{}; |
Ioan-Cristian Szabo | 397d58a | 2017-11-30 15:19:11 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 362 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo &input1, ITensorInfo &input2, ITensorInfo &output) |
Ioan-Cristian Szabo | 397d58a | 2017-11-30 15:19:11 +0000 | [diff] [blame] | 363 | { |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 364 | const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(input1, input2); |
| 365 | const TensorShape &out_shape = broadcast_pair.first; |
| 366 | const ValidRegion &valid_region = broadcast_pair.second; |
Ioan-Cristian Szabo | 397d58a | 2017-11-30 15:19:11 +0000 | [diff] [blame] | 367 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 368 | // Auto initialize output if not initialized |
| 369 | { |
| 370 | set_shape_if_empty(output, out_shape); |
Ioan-Cristian Szabo | 397d58a | 2017-11-30 15:19:11 +0000 | [diff] [blame] | 371 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 372 | if(input1.data_type() == DataType::S16 || input2.data_type() == DataType::S16) |
| 373 | { |
| 374 | set_format_if_unknown(output, Format::S16); |
| 375 | } |
| 376 | else if(input1.data_type() == DataType::F16 && input2.data_type() == DataType::F16) |
| 377 | { |
| 378 | set_format_if_unknown(output, Format::F16); |
| 379 | } |
| 380 | else if(input1.data_type() == DataType::F32 || input2.data_type() == DataType::F32) |
| 381 | { |
| 382 | set_format_if_unknown(output, Format::F32); |
| 383 | } |
| 384 | } |
Ioan-Cristian Szabo | 397d58a | 2017-11-30 15:19:11 +0000 | [diff] [blame] | 385 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 386 | Window win = calculate_max_window(valid_region, Steps(num_elems_processed_per_iteration)); |
| 387 | Window win_input1 = win.broadcast_if_dimension_le_one(input1); |
| 388 | Window win_input2 = win.broadcast_if_dimension_le_one(input2); |
| 389 | |
| 390 | AccessWindowHorizontal input1_access(&input1, 0, num_elems_processed_per_iteration); |
| 391 | AccessWindowHorizontal input2_access(&input2, 0, num_elems_processed_per_iteration); |
| 392 | AccessWindowHorizontal output_access(&output, 0, num_elems_processed_per_iteration); |
| 393 | |
| 394 | bool window_changed = update_window_and_padding(win_input1, input1_access) |
| 395 | || update_window_and_padding(win_input2, input2_access) |
| 396 | || update_window_and_padding(win, output_access); |
Ioan-Cristian Szabo | 397d58a | 2017-11-30 15:19:11 +0000 | [diff] [blame] | 397 | |
| 398 | output_access.set_valid_region(win, valid_region); |
| 399 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 400 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Ioan-Cristian Szabo | 397d58a | 2017-11-30 15:19:11 +0000 | [diff] [blame] | 401 | return std::make_pair(err, win); |
| 402 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 403 | } // namespace |
| 404 | |
| 405 | NEArithmeticAdditionKernel::NEArithmeticAdditionKernel() |
| 406 | : _func(nullptr), _input1(nullptr), _input2(nullptr), _output(nullptr) |
| 407 | { |
| 408 | } |
| 409 | |
| 410 | void NEArithmeticAdditionKernel::configure(const ITensor *input1, const ITensor *input2, ITensor *output, ConvertPolicy policy) |
| 411 | { |
| 412 | ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 413 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*input1->info(), *input2->info(), *output->info(), policy)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 414 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 415 | // Configure kernel window |
| 416 | auto win_config = validate_and_configure_window(*input1->info(), *input2->info(), *output->info()); |
| 417 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 418 | |
| 419 | static std::map<std::string, AddFunction *> map_function = |
| 420 | { |
| 421 | { "add_wrap_U8_U8_U8", &add_wrap_U8_U8_U8 }, |
| 422 | { "add_saturate_U8_U8_U8", &add_saturate_U8_U8_U8 }, |
| 423 | { "add_wrap_S16_U8_S16", &add_wrap_S16_U8_S16 }, |
| 424 | { "add_saturate_S16_U8_S16", &add_saturate_S16_U8_S16 }, |
| 425 | { "add_wrap_U8_S16_S16", &add_wrap_U8_S16_S16 }, |
| 426 | { "add_saturate_U8_S16_S16", &add_saturate_U8_S16_S16 }, |
| 427 | { "add_wrap_U8_U8_S16", &add_wrap_U8_U8_S16 }, |
| 428 | { "add_saturate_U8_U8_S16", &add_saturate_U8_U8_S16 }, |
| 429 | { "add_wrap_S16_S16_S16", &add_wrap_S16_S16_S16 }, |
| 430 | { "add_saturate_S16_S16_S16", &add_saturate_S16_S16_S16 }, |
| 431 | { "add_wrap_F32_F32_F32", &add_F32_F32_F32 }, |
| 432 | { "add_saturate_F32_F32_F32", &add_F32_F32_F32 }, |
Pablo Tello | d1b0ecc | 2017-07-11 11:27:04 +0100 | [diff] [blame] | 433 | { "add_wrap_F16_F16_F16", &add_F16_F16_F16 }, |
| 434 | { "add_saturate_F16_F16_F16", &add_F16_F16_F16 }, |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 435 | }; |
| 436 | |
| 437 | _input1 = input1; |
| 438 | _input2 = input2; |
| 439 | _output = output; |
| 440 | |
| 441 | std::string function_to_call("add_"); |
| 442 | function_to_call += policy == ConvertPolicy::WRAP ? "wrap_" : "saturate_"; |
| 443 | function_to_call += string_from_data_type(input1->info()->data_type()) + "_"; |
| 444 | function_to_call += string_from_data_type(input2->info()->data_type()) + "_"; |
| 445 | function_to_call += string_from_data_type(output->info()->data_type()); |
| 446 | |
| 447 | auto it = map_function.find(function_to_call); |
| 448 | |
| 449 | if(it != map_function.end()) |
| 450 | { |
| 451 | _func = it->second; |
| 452 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 453 | |
Ioan-Cristian Szabo | 397d58a | 2017-11-30 15:19:11 +0000 | [diff] [blame] | 454 | INEKernel::configure(win_config.second); |
| 455 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 456 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 457 | Status NEArithmeticAdditionKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, ConvertPolicy policy) |
Ioan-Cristian Szabo | 397d58a | 2017-11-30 15:19:11 +0000 | [diff] [blame] | 458 | { |
Georgios Pinitas | cbf39c6 | 2018-09-10 15:07:45 +0100 | [diff] [blame] | 459 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output); |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 460 | |
| 461 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*input1, *input2, *output, policy)); |
| 462 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(*input1->clone(), *input2->clone(), *output->clone()).first); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 463 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 464 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 465 | } |
| 466 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 467 | void NEArithmeticAdditionKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 468 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 469 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 470 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 471 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 472 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 473 | |
| 474 | (*_func)(_input1, _input2, _output, window); |
| 475 | } |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 476 | |
| 477 | BorderSize NEArithmeticAdditionKernel::border_size() const |
| 478 | { |
| 479 | const unsigned int replicateSize = _output->info()->dimension(0) - std::min(_input1->info()->dimension(0), _input2->info()->dimension(0)); |
| 480 | const unsigned int border = std::min<unsigned int>(num_elems_processed_per_iteration - 1U, replicateSize); |
| 481 | return BorderSize(0, border, 0, 0); |
| 482 | } |