Compute Library
 21.02
Accumulate.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-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 "Accumulate.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 template <typename T1, typename T2>
39 {
40  SimpleTensor<T2> dst{ src.shape(), output_data_type };
41 
42  library->fill_tensor_uniform(dst, 1, static_cast<T2>(0), static_cast<T2>(std::numeric_limits<T1>::max()));
43 
44  using intermediate_type = typename common_promoted_signed_type<T1, T2>::intermediate_type;
45 #if defined(_OPENMP)
46  #pragma omp parallel for
47 #endif /* _OPENMP */
48  for(int i = 0; i < src.num_elements(); ++i)
49  {
50  intermediate_type val = static_cast<intermediate_type>(src[i]) + static_cast<intermediate_type>(dst[i]);
51  dst[i] = saturate_cast<T2>(val);
52  }
53 
54  return dst;
55 }
56 
57 template <typename T1, typename T2>
58 SimpleTensor<T2> accumulate_weighted(const SimpleTensor<T1> &src, float alpha, DataType output_data_type)
59 {
60  ARM_COMPUTE_ERROR_ON_MSG(alpha < 0.f || alpha > 1.f, "Weight (alpha) specified in accumulate_weighted must be within the range [0, 1]");
61 
62  SimpleTensor<T2> dst{ src.shape(), output_data_type };
63 
64  library->fill_tensor_uniform(dst, 1, static_cast<T2>(0), static_cast<T2>(std::numeric_limits<T1>::max()));
65 
66  using intermediate_type = typename common_promoted_signed_type<T1, T2>::intermediate_type;
67 #if defined(_OPENMP)
68  #pragma omp parallel for
69 #endif /* _OPENMP */
70  for(int i = 0; i < src.num_elements(); ++i)
71  {
72  double val = (1. - static_cast<double>(alpha)) * static_cast<intermediate_type>(dst[i]) + static_cast<double>(alpha) * static_cast<intermediate_type>(src[i]);
73  dst[i] = static_cast<T2>(val);
74  }
75 
76  return dst;
77 }
78 
79 template <typename T1, typename T2>
80 SimpleTensor<T2> accumulate_squared(const SimpleTensor<T1> &src, uint32_t shift, DataType output_data_type)
81 {
82  ARM_COMPUTE_ERROR_ON_MSG(shift > 15, "Shift in accumulate_squared must be within the range [0, 15]");
83 
84  SimpleTensor<T2> dst{ src.shape(), output_data_type };
85 
86  library->fill_tensor_uniform(dst, 1, static_cast<T2>(0), static_cast<T2>(std::numeric_limits<T1>::max()));
87 
88  using intermediate_type = typename common_promoted_signed_type<T1, T2>::intermediate_type;
89  intermediate_type denom = 1 << shift;
90 #if defined(_OPENMP)
91  #pragma omp parallel for
92 #endif /* _OPENMP */
93  for(int i = 0; i < src.num_elements(); ++i)
94  {
95  intermediate_type val = static_cast<intermediate_type>(dst[i]) + (static_cast<intermediate_type>(src[i]) * static_cast<intermediate_type>(src[i]) / denom);
96  dst[i] = saturate_cast<T2>(val);
97  }
98 
99  return dst;
100 }
101 
102 template SimpleTensor<int16_t> accumulate(const SimpleTensor<uint8_t> &src, DataType output_data_type);
103 template SimpleTensor<uint8_t> accumulate_weighted(const SimpleTensor<uint8_t> &src, float alpha, DataType output_data_type);
104 template SimpleTensor<int16_t> accumulate_squared(const SimpleTensor<uint8_t> &src, uint32_t shift, DataType output_data_type);
105 } // namespace reference
106 } // namespace validation
107 } // namespace test
108 } // namespace arm_compute
SimpleTensor< T2 > accumulate_squared(const SimpleTensor< T1 > &src, uint32_t shift, DataType output_data_type)
Definition: Accumulate.cpp:80
TensorShape shape() const override
Shape of the tensor.
Definition: SimpleTensor.h:320
SimpleTensor< T2 > accumulate(const SimpleTensor< T1 > &src, DataType output_data_type)
Definition: Accumulate.cpp:38
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2021 Arm Limited.
std::unique_ptr< AssetsLibrary > library
Definition: main.cpp:78
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:58
typename traits::make_signed_conditional_t< promoted_type >::type intermediate_type
Intermediate type.
Definition: Utils.h:437
T saturate_cast(T val)
Saturate a value of type T against the numeric limits of type U.
Definition: Utils.h:414
int num_elements() const override
Number of elements of the tensor.
Definition: SimpleTensor.h:406
SimpleTensor< T2 > accumulate_weighted(const SimpleTensor< T1 > &src, float alpha, DataType output_data_type)
Definition: Accumulate.cpp:58
DataType
Available data types.
Definition: Types.h:77