Compute Library
 21.08
ElementwiseOperations.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020 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 "ElementwiseOperations.h"
25 
26 #include "arm_compute/core/Types.h"
28 
29 namespace arm_compute
30 {
31 namespace test
32 {
33 namespace validation
34 {
35 namespace reference
36 {
37 namespace
38 {
39 template <typename T>
40 T arithm_op(ArithmeticOperation op, T src1, T src2, ConvertPolicy convert_policy)
41 {
42  using intermediate_type = typename common_promoted_signed_type<T>::intermediate_type;
43 
44  intermediate_type val;
45 
46  switch(op)
47  {
49  {
50  val = static_cast<intermediate_type>(src1) + static_cast<intermediate_type>(src2);
51  break;
52  }
54  {
55  val = static_cast<intermediate_type>(src1) - static_cast<intermediate_type>(src2);
56  break;
57  }
59  {
60  val = std::min(static_cast<intermediate_type>(src1), static_cast<intermediate_type>(src2));
61  break;
62  }
64  {
65  val = std::max(static_cast<intermediate_type>(src1), static_cast<intermediate_type>(src2));
66  break;
67  }
69  {
70  intermediate_type tmp = (static_cast<intermediate_type>(src1) - static_cast<intermediate_type>(src2));
71  val = tmp * tmp;
72  break;
73  }
75  {
76  val = (static_cast<intermediate_type>(src1) / static_cast<intermediate_type>(src2));
77  if(std::is_integral<T>::value)
78  {
79  // Implement flooring division
80  val = (src2 == 0) ? 0 : val;
81  if(static_cast<int32_t>(src1) % static_cast<int32_t>(src2) != 0 && ((src1 < 0) != (src2 < 0)))
82  {
83  --val;
84  }
85  }
86  break;
87  }
89  {
90  val = std::pow(static_cast<intermediate_type>(src1), static_cast<intermediate_type>(src2));
91  break;
92  }
94  {
95  const T x = static_cast<intermediate_type>(src1);
96  const T alpha = static_cast<intermediate_type>(src2);
97  val = (x > 0 ? x : alpha * x);
98  break;
99  }
100  default:
101  {
102  ARM_COMPUTE_ERROR("Not handled");
103  }
104  }
105  T result;
107  {
108  result = (convert_policy == ConvertPolicy::SATURATE) ? saturate_cast<T>(val) : static_cast<T>(val);
109  }
110  else
111  {
112  result = static_cast<T>(val);
113  }
114  return result;
115 }
116 
117 template <size_t dim>
118 struct BroadcastUnroll
119 {
120  template <typename T>
121  static void unroll(ArithmeticOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst,
122  ConvertPolicy convert_policy, Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst)
123  {
124  const bool src1_is_broadcast = (src1.shape()[dim - 1] != dst.shape()[dim - 1]);
125  const bool src2_is_broadcast = (src2.shape()[dim - 1] != dst.shape()[dim - 1]);
126 
127  id_src1.set(dim - 1, 0);
128  id_src2.set(dim - 1, 0);
129  id_dst.set(dim - 1, 0);
130 
131  for(size_t i = 0; i < dst.shape()[dim - 1]; ++i, ++id_dst[dim - 1])
132  {
133  BroadcastUnroll < dim - 1 >::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst);
134 
135  id_src1[dim - 1] += !src1_is_broadcast;
136  id_src2[dim - 1] += !src2_is_broadcast;
137  }
138  }
139 };
140 
141 template <>
142 struct BroadcastUnroll<0>
143 {
144  template <typename T>
145  static void unroll(ArithmeticOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst,
146  ConvertPolicy convert_policy, Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst)
147  {
148  dst[coord2index(dst.shape(), id_dst)] = arithm_op(op, src1[coord2index(src1.shape(), id_src1)], src2[coord2index(src2.shape(), id_src2)], convert_policy);
149  }
150 };
151 } // namespace
152 
153 template <typename T>
154 SimpleTensor<T> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<T> &dst, ConvertPolicy convert_policy)
155 {
156  Coordinates id_src1{};
157  Coordinates id_src2{};
158  Coordinates id_dst{};
159 
160  BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst);
161 
162  return dst;
163 }
164 
165 template <>
166 SimpleTensor<uint8_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<uint8_t> &src1, const SimpleTensor<uint8_t> &src2, SimpleTensor<uint8_t> &dst, ConvertPolicy convert_policy)
167 {
168  if(dst.data_type() == DataType::QASYMM8)
169  {
170  SimpleTensor<float> src1_tmp = convert_from_asymmetric(src1);
171  SimpleTensor<float> src2_tmp = convert_from_asymmetric(src2);
172  SimpleTensor<float> dst_tmp(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst.data_type());
173 
174  Coordinates id_src1{};
175  Coordinates id_src2{};
176  Coordinates id_dst{};
177 
178  BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1_tmp, src2_tmp, dst_tmp, convert_policy, id_src1, id_src2, id_dst);
179 
180  dst = convert_to_asymmetric<uint8_t>(dst_tmp, dst.quantization_info());
181  return dst;
182  }
183  else
184  {
185  // DataType::U8
186  Coordinates id_src1{};
187  Coordinates id_src2{};
188  Coordinates id_dst{};
189 
190  BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst);
191 
192  return dst;
193  }
194 }
195 template <>
196 SimpleTensor<int8_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int8_t> &src1, const SimpleTensor<int8_t> &src2, SimpleTensor<int8_t> &dst, ConvertPolicy convert_policy)
197 {
198  if(dst.data_type() == DataType::QASYMM8_SIGNED)
199  {
200  SimpleTensor<float> src1_tmp = convert_from_asymmetric(src1);
201  SimpleTensor<float> src2_tmp = convert_from_asymmetric(src2);
202  SimpleTensor<float> dst_tmp(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst.data_type());
203 
204  Coordinates id_src1{};
205  Coordinates id_src2{};
206  Coordinates id_dst{};
207 
208  BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1_tmp, src2_tmp, dst_tmp, convert_policy, id_src1, id_src2, id_dst);
209 
210  dst = convert_to_asymmetric<int8_t>(dst_tmp, dst.quantization_info());
211  return dst;
212  }
213  else
214  {
215  // DataType::S8
216  Coordinates id_src1{};
217  Coordinates id_src2{};
218  Coordinates id_dst{};
219 
220  BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst);
221 
222  return dst;
223  }
224 }
225 
226 template <>
227 SimpleTensor<int16_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int16_t> &src1, const SimpleTensor<int16_t> &src2, SimpleTensor<int16_t> &dst, ConvertPolicy convert_policy)
228 {
229  if(dst.data_type() == DataType::QSYMM16)
230  {
231  SimpleTensor<float> src1_tmp = convert_from_symmetric<int16_t>(src1);
232  SimpleTensor<float> src2_tmp = convert_from_symmetric<int16_t>(src2);
233  SimpleTensor<float> dst_tmp(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst.data_type());
234 
235  Coordinates id_src1{};
236  Coordinates id_src2{};
237  Coordinates id_dst{};
238 
239  BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1_tmp, src2_tmp, dst_tmp, convert_policy, id_src1, id_src2, id_dst);
240 
241  dst = convert_to_symmetric<int16_t>(dst_tmp, dst.quantization_info());
242  return dst;
243  }
244  else
245  {
246  // DataType::S16
247  Coordinates id_src1{};
248  Coordinates id_src2{};
249  Coordinates id_dst{};
250 
251  BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, convert_policy, id_src1, id_src2, id_dst);
252 
253  return dst;
254  }
255 }
256 
257 template SimpleTensor<int32_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int32_t> &src1, const SimpleTensor<int32_t> &src2, SimpleTensor<int32_t> &dst,
258  ConvertPolicy convert_policy);
259 template SimpleTensor<half> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<half> &src1, const SimpleTensor<half> &src2, SimpleTensor<half> &dst, ConvertPolicy convert_policy);
260 template SimpleTensor<float> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<float> &src1, const SimpleTensor<float> &src2, SimpleTensor<float> &dst, ConvertPolicy convert_policy);
261 
262 template <typename T>
263 SimpleTensor<T> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, DataType dst_data_type, ConvertPolicy convert_policy)
264 {
265  ARM_COMPUTE_ERROR_ON_MSG(is_data_type_quantized(dst_data_type), "For quantized data types, the quantized output tensor should be passed directly.");
266 
267  SimpleTensor<T> dst(TensorShape::broadcast_shape(src1.shape(), src2.shape()), dst_data_type);
268  arithmetic_operation<T>(op, src1, src2, dst, convert_policy);
269  return dst;
270 }
271 
272 template SimpleTensor<int32_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int32_t> &src1, const SimpleTensor<int32_t> &src2, DataType dst_data_type,
273  ConvertPolicy convert_policy);
274 template SimpleTensor<int16_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int16_t> &src1, const SimpleTensor<int16_t> &src2, DataType dst_data_type,
275  ConvertPolicy convert_policy);
276 template SimpleTensor<int8_t> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<int8_t> &src1, const SimpleTensor<int8_t> &src2, DataType dst_data_type, ConvertPolicy convert_policy);
277 template SimpleTensor<half> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<half> &src1, const SimpleTensor<half> &src2, DataType dst_data_type, ConvertPolicy convert_policy);
278 template SimpleTensor<float> arithmetic_operation(ArithmeticOperation op, const SimpleTensor<float> &src1, const SimpleTensor<float> &src2, DataType dst_data_type, ConvertPolicy convert_policy);
279 
280 } // namespace reference
281 } // namespace validation
282 } // namespace test
283 } // namespace arm_compute
bool is_data_type_quantized(DataType dt)
Check if a given data type is of quantized type.
Definition: Utils.h:981
quantized, symmetric fixed-point 16-bit number
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
static TensorShape broadcast_shape(const Shapes &... shapes)
If shapes are broadcast compatible, return the broadcasted shape.
Definition: TensorShape.h:211
SimpleTensor< float > convert_from_asymmetric(const SimpleTensor< uint8_t > &src)
Definition: Helpers.cpp:36
SimpleTensor< T > arithmetic_operation(ArithmeticOperation op, const SimpleTensor< T > &src1, const SimpleTensor< T > &src2, SimpleTensor< T > &dst, ConvertPolicy convert_policy)
Copyright (c) 2017-2021 Arm Limited.
int coord2index(const TensorShape &shape, const Coordinates &coord)
Linearise the given coordinate.
Definition: Utils.h:387
quantized, asymmetric fixed-point 8-bit number unsigned
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
y*x if x < 0, x otherwise
typename traits::make_signed_conditional_t< promoted_type >::type intermediate_type
Intermediate type.
Definition: Utils.h:335
ArithmeticOperation
Arithmetic operation types.
quantized, asymmetric fixed-point 8-bit number signed
DataType
Available data types.
Definition: Types.h:77
ConvertPolicy
Policy to handle overflow.
Definition: Types.h:382