Compute Library
 20.02.1
PixelWiseMultiplication.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-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  */
25 
27 
28 namespace arm_compute
29 {
30 namespace test
31 {
32 namespace validation
33 {
34 namespace reference
35 {
36 template <class T>
37 struct is_floating_point
38  : std::integral_constant < bool,
39  std::is_same<float, typename std::remove_cv<T>::type>::value || std::is_same<half_float::half, typename std::remove_cv<T>::type>::value
40  || std::is_same<double, typename std::remove_cv<T>::type>::value || std::is_same<long double, typename std::remove_cv<T>::type>::value >
41 {
42 };
43 
44 namespace
45 {
46 /** Compute the result of `src1 * src2 * scale`. The result type always matches the type of @p src2.
47  *
48  * @param[in] src1 An input value. Data types supported: U8/S16/F16/F32.
49  * @param[in] src2 An input value. Data types supported: same as @p src1.
50  * @param[in] scale Scale to apply after multiplication.
51  * Scale must be positive and its value must be either 1/255 or 1/2^n where n is between 0 and 15.
52  * @param[in] convert_policy Overflow policy. Supported overflow policies: Wrap, Saturate
53  * @param[in] rounding_policy Rounding policy. Supported rounding modes: to zero, to nearest even.
54  */
55 template <typename T1, typename T2>
56 T2 mul(const T1 src1, const T2 src2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy)
57 {
58  using intermediate_type = typename common_promoted_signed_type<T1, T2, T2>::intermediate_type;
59 
60  const double val = static_cast<intermediate_type>(src1) * static_cast<intermediate_type>(src2) * static_cast<double>(scale);
61 
62  if(is_floating_point<T2>::value)
63  {
64  const auto result = static_cast<T2>(val);
65 
66  return result;
67  }
68  else
69  {
70  double rounded_val = 0;
71  switch(rounding_policy)
72  {
74  rounded_val = support::cpp11::trunc(val);
75  break;
77  rounded_val = round_half_up(val);
78  break;
80  rounded_val = round_half_even(val);
81  break;
82  default:
83  ARM_COMPUTE_ERROR("Unsupported rounding policy");
84  }
85 
86  const auto result = static_cast<T2>((convert_policy == ConvertPolicy::SATURATE) ? saturate_cast<T2>(rounded_val) : rounded_val);
87 
88  return result;
89  }
90 }
91 
92 template <size_t dim>
93 struct BroadcastUnroll
94 {
95  template <typename T1, typename T2>
96  static void unroll(const SimpleTensor<T1> &src1, const SimpleTensor<T2> &src2, SimpleTensor<T2> &dst,
98  Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst)
99  {
100  const bool src1_is_broadcast = (src1.shape()[dim - 1] != dst.shape()[dim - 1]);
101  const bool src2_is_broadcast = (src2.shape()[dim - 1] != dst.shape()[dim - 1]);
102 
103  id_src1.set(dim - 1, 0);
104  id_src2.set(dim - 1, 0);
105  id_dst.set(dim - 1, 0);
106 
107  for(size_t i = 0; i < dst.shape()[dim - 1]; ++i, ++id_dst[dim - 1])
108  {
109  BroadcastUnroll < dim - 1 >::unroll(src1, src2, dst, scale, convert_policy, rounding_policy, id_src1, id_src2, id_dst);
110 
111  id_src1[dim - 1] += !src1_is_broadcast;
112  id_src2[dim - 1] += !src2_is_broadcast;
113  }
114  }
115 };
116 
117 template <>
118 struct BroadcastUnroll<0>
119 {
120  template <typename T1, typename T2>
121  static void unroll(const SimpleTensor<T1> &src1, const SimpleTensor<T2> &src2, SimpleTensor<T2> &dst,
123  Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst)
124  {
125  dst[coord2index(dst.shape(), id_dst)] = mul(src1[coord2index(src1.shape(), id_src1)], src2[coord2index(src2.shape(), id_src2)], scale, convert_policy, rounding_policy);
126  }
127 };
128 } // namespace
129 
130 template <typename T1, typename T2>
132  const QuantizationInfo &qout)
133 {
134  ARM_COMPUTE_UNUSED(qout);
135 
137 
138  if(scale < 0)
139  {
140  ARM_COMPUTE_ERROR("Scale of pixel-wise multiplication must be non-negative");
141  }
142 
143  Coordinates id_src1{};
144  Coordinates id_src2{};
145  Coordinates id_dst{};
146 
147  BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(src1, src2, dst, scale, convert_policy, rounding_policy, id_src1, id_src2, id_dst);
148 
149  return dst;
150 }
151 
152 template <>
154  const QuantizationInfo &qout)
155 {
157 
158  if(src1.data_type() == DataType::QASYMM8 && src2.data_type() == DataType::QASYMM8)
159  {
162  SimpleTensor<float> dst_tmp = pixel_wise_multiplication<float>(src1_tmp, src2_tmp, scale, convert_policy, rounding_policy, qout);
163  dst = convert_to_asymmetric<uint8_t>(dst_tmp, qout);
164  }
165  else
166  {
167  if(scale < 0)
168  {
169  ARM_COMPUTE_ERROR("Scale of pixel-wise multiplication must be non-negative");
170  }
171 
172  Coordinates id_src1{};
173  Coordinates id_src2{};
174  Coordinates id_dst{};
175  BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(src1, src2, dst, scale, convert_policy, rounding_policy, id_src1, id_src2, id_dst);
176  }
177  return dst;
178 }
179 
180 template <>
182  const QuantizationInfo &qout)
183 {
184  SimpleTensor<int8_t> dst(TensorShape::broadcast_shape(src1.shape(), src2.shape()), src2.data_type(), 1, qout);
185 
187  {
190  SimpleTensor<float> dst_tmp = pixel_wise_multiplication<float>(src1_tmp, src2_tmp, scale, convert_policy, rounding_policy, qout);
191  dst = convert_to_asymmetric<int8_t>(dst_tmp, qout);
192  }
193  else
194  {
195  if(scale < 0)
196  {
197  ARM_COMPUTE_ERROR("Scale of pixel-wise multiplication must be non-negative");
198  }
199 
200  Coordinates id_src1{};
201  Coordinates id_src2{};
202  Coordinates id_dst{};
203  BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(src1, src2, dst, scale, convert_policy, rounding_policy, id_src1, id_src2, id_dst);
204  }
205  return dst;
206 }
207 
208 template <>
210  const QuantizationInfo &qout)
211 {
213 
214  if(src1.data_type() == DataType::QSYMM16 && src2.data_type() == DataType::QSYMM16)
215  {
216  SimpleTensor<float> src1_tmp = convert_from_symmetric<int16_t>(src1);
217  SimpleTensor<float> src2_tmp = convert_from_symmetric<int16_t>(src2);
218  SimpleTensor<float> dst_tmp = pixel_wise_multiplication<float>(src1_tmp, src2_tmp, scale, convert_policy, rounding_policy, qout);
219  dst = convert_to_symmetric<int16_t>(dst_tmp, qout);
220  }
221  else
222  {
223  if(scale < 0)
224  {
225  ARM_COMPUTE_ERROR("Scale of pixel-wise multiplication must be non-negative");
226  }
227 
228  Coordinates id_src1{};
229  Coordinates id_src2{};
230  Coordinates id_dst{};
231  BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(src1, src2, dst, scale, convert_policy, rounding_policy, id_src1, id_src2, id_dst);
232  }
233  return dst;
234 }
235 // *INDENT-OFF*
236 // clang-format off
240 // clang-format on
241 // *INDENT-ON*
242 } // namespace reference
243 } // namespace validation
244 } // namespace test
245 } // namespace arm_compute
SimpleTensor< T2 > pixel_wise_multiplication(const SimpleTensor< T1 > &src1, const SimpleTensor< T2 > &src2, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy, const QuantizationInfo &qout)
T trunc(T value)
Truncate floating-point value.
quantized, symmetric fixed-point 16-bit number
Rounds to nearest value; half rounds away from zero.
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
DataType data_type() const override
Data type of the tensor.
Definition: SimpleTensor.h:358
static TensorShape broadcast_shape(const Shapes &... shapes)
If shapes are broadcast compatible, return the broadcasted shape.
Definition: TensorShape.h:210
SimpleTensor< float > convert_from_asymmetric(const SimpleTensor< uint8_t > &src)
Definition: Helpers.cpp:112
TensorShape shape() const override
Shape of the tensor.
Definition: SimpleTensor.h:321
Copyright (c) 2017-2020 ARM Limited.
int coord2index(const TensorShape &shape, const Coordinates &coord)
Linearise the given coordinate.
Definition: Utils.h:485
T round_half_even(T value, T epsilon=std::numeric_limits< T >::epsilon())
Round floating-point value with half value rounding to nearest even.
Definition: Utils.h:87
Quantization information.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
quantized, asymmetric fixed-point 8-bit number unsigned
Coordinates of an item.
Definition: Coordinates.h:37
RoundingPolicy
Rounding method.
Definition: Rounding.h:30
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:59
Rounds to nearest value; half rounds to nearest even.
typename traits::make_signed_conditional_t< promoted_type >::type intermediate_type
Intermediate type.
Definition: Utils.h:433
quantized, asymmetric fixed-point 8-bit number signed
T round_half_up(T value)
Round floating-point value with half value rounding to positive infinity.
Definition: Utils.h:74
SimpleTensor< T > scale(const SimpleTensor< T > &src, float scale_x, float scale_y, InterpolationPolicy policy, BorderMode border_mode, T constant_border_value, SamplingPolicy sampling_policy, bool ceil_policy_scale, bool align_corners)
Definition: Scale.cpp:188
Truncates the least significant values that are lost in operations.
ConvertPolicy
Policy to handle overflow.
Definition: Types.h:359