blob: 67754f0712c0ccd9ffeb311c0389b9b57efa35a1 [file] [log] [blame]
Ioan-Cristian Szabo432a7d42017-10-12 09:25:19 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Ioan-Cristian Szabo432a7d42017-10-12 09:25:19 +01003 *
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"
Ioan-Cristian Szabo432a7d42017-10-12 09:25:19 +010027#include "tests/validation/Helpers.h"
28
29namespace arm_compute
30{
31namespace test
32{
33namespace validation
34{
35namespace reference
36{
37template <typename T1, typename T2>
38SimpleTensor<T2> accumulate(const SimpleTensor<T1> &src, DataType output_data_type)
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;
Michalis Spyroud1d77222020-04-08 14:10:15 +010045#if defined(_OPENMP)
46 #pragma omp parallel for
47#endif /* _OPENMP */
Ioan-Cristian Szabo432a7d42017-10-12 09:25:19 +010048 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
57template <typename T1, typename T2>
58SimpleTensor<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;
Michalis Spyroud1d77222020-04-08 14:10:15 +010067#if defined(_OPENMP)
68 #pragma omp parallel for
69#endif /* _OPENMP */
Ioan-Cristian Szabo432a7d42017-10-12 09:25:19 +010070 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
79template <typename T1, typename T2>
80SimpleTensor<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;
Michalis Spyroud1d77222020-04-08 14:10:15 +010090#if defined(_OPENMP)
91 #pragma omp parallel for
92#endif /* _OPENMP */
Ioan-Cristian Szabo432a7d42017-10-12 09:25:19 +010093 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
102template SimpleTensor<int16_t> accumulate(const SimpleTensor<uint8_t> &src, DataType output_data_type);
103template SimpleTensor<uint8_t> accumulate_weighted(const SimpleTensor<uint8_t> &src, float alpha, DataType output_data_type);
104template 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