Ioan-Cristian Szabo | 432a7d4 | 2017-10-12 09:25:19 +0100 | [diff] [blame] | 1 | /* |
Vidhya Sudhan Loganathan | 014333d | 2018-07-02 09:13:49 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Ioan-Cristian Szabo | 432a7d4 | 2017-10-12 09:25:19 +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 "Accumulate.h" |
| 25 | |
| 26 | #include "arm_compute/core/Types.h" |
Ioan-Cristian Szabo | 432a7d4 | 2017-10-12 09:25:19 +0100 | [diff] [blame] | 27 | #include "tests/validation/Helpers.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> |
| 38 | SimpleTensor<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; |
| 45 | |
| 46 | for(int i = 0; i < src.num_elements(); ++i) |
| 47 | { |
| 48 | intermediate_type val = static_cast<intermediate_type>(src[i]) + static_cast<intermediate_type>(dst[i]); |
| 49 | dst[i] = saturate_cast<T2>(val); |
| 50 | } |
| 51 | |
| 52 | return dst; |
| 53 | } |
| 54 | |
| 55 | template <typename T1, typename T2> |
| 56 | SimpleTensor<T2> accumulate_weighted(const SimpleTensor<T1> &src, float alpha, DataType output_data_type) |
| 57 | { |
| 58 | ARM_COMPUTE_ERROR_ON_MSG(alpha < 0.f || alpha > 1.f, "Weight (alpha) specified in accumulate_weighted must be within the range [0, 1]"); |
| 59 | |
| 60 | SimpleTensor<T2> dst{ src.shape(), output_data_type }; |
| 61 | |
| 62 | library->fill_tensor_uniform(dst, 1, static_cast<T2>(0), static_cast<T2>(std::numeric_limits<T1>::max())); |
| 63 | |
| 64 | using intermediate_type = typename common_promoted_signed_type<T1, T2>::intermediate_type; |
| 65 | |
| 66 | for(int i = 0; i < src.num_elements(); ++i) |
| 67 | { |
| 68 | double val = (1. - static_cast<double>(alpha)) * static_cast<intermediate_type>(dst[i]) + static_cast<double>(alpha) * static_cast<intermediate_type>(src[i]); |
| 69 | dst[i] = static_cast<T2>(val); |
| 70 | } |
| 71 | |
| 72 | return dst; |
| 73 | } |
| 74 | |
| 75 | template <typename T1, typename T2> |
| 76 | SimpleTensor<T2> accumulate_squared(const SimpleTensor<T1> &src, uint32_t shift, DataType output_data_type) |
| 77 | { |
| 78 | ARM_COMPUTE_ERROR_ON_MSG(shift > 15, "Shift in accumulate_squared must be within the range [0, 15]"); |
| 79 | |
| 80 | SimpleTensor<T2> dst{ src.shape(), output_data_type }; |
| 81 | |
| 82 | library->fill_tensor_uniform(dst, 1, static_cast<T2>(0), static_cast<T2>(std::numeric_limits<T1>::max())); |
| 83 | |
| 84 | using intermediate_type = typename common_promoted_signed_type<T1, T2>::intermediate_type; |
| 85 | intermediate_type denom = 1 << shift; |
| 86 | |
| 87 | for(int i = 0; i < src.num_elements(); ++i) |
| 88 | { |
| 89 | intermediate_type val = static_cast<intermediate_type>(dst[i]) + (static_cast<intermediate_type>(src[i]) * static_cast<intermediate_type>(src[i]) / denom); |
| 90 | dst[i] = saturate_cast<T2>(val); |
| 91 | } |
| 92 | |
| 93 | return dst; |
| 94 | } |
| 95 | |
| 96 | template SimpleTensor<int16_t> accumulate(const SimpleTensor<uint8_t> &src, DataType output_data_type); |
| 97 | template SimpleTensor<uint8_t> accumulate_weighted(const SimpleTensor<uint8_t> &src, float alpha, DataType output_data_type); |
| 98 | template SimpleTensor<int16_t> accumulate_squared(const SimpleTensor<uint8_t> &src, uint32_t shift, DataType output_data_type); |
| 99 | } // namespace reference |
| 100 | } // namespace validation |
| 101 | } // namespace test |
| 102 | } // namespace arm_compute |