Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 "ReductionOperation.h" |
| 25 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 26 | #include "tests/validation/Helpers.h" |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 27 | |
| 28 | #include <algorithm> |
| 29 | #include <cmath> |
| 30 | |
| 31 | namespace arm_compute |
| 32 | { |
| 33 | namespace test |
| 34 | { |
| 35 | namespace validation |
| 36 | { |
| 37 | namespace reference |
| 38 | { |
| 39 | namespace |
| 40 | { |
| 41 | template <typename T> |
| 42 | struct square |
| 43 | { |
| 44 | T operator()(const T &lhs, const T &rhs) const |
| 45 | { |
| 46 | return (lhs + rhs * rhs); |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | template <typename T> |
| 51 | T reduce_operation(T *ptr, int reduce_elements, ReductionOperation op) |
| 52 | { |
| 53 | switch(op) |
| 54 | { |
| 55 | case ReductionOperation::SUM_SQUARE: |
| 56 | return std::accumulate(ptr, ptr + reduce_elements, 0.f, square<T>()); |
| 57 | default: |
| 58 | ARM_COMPUTE_ERROR("Unsupported reduction operation"); |
| 59 | } |
| 60 | } |
| 61 | } // namespace |
| 62 | |
| 63 | template <typename T> |
| 64 | SimpleTensor<T> reduction_operation(const SimpleTensor<T> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op) |
| 65 | { |
| 66 | // Create reference |
| 67 | SimpleTensor<T> dst{ dst_shape, src.data_type() }; |
| 68 | |
| 69 | // Compute reference |
| 70 | const int reduce_elems = src.shape()[axis]; |
| 71 | const int upper_dims = src.shape().total_size_upper(axis + 1); |
| 72 | |
| 73 | for(int du = 0; du < upper_dims; ++du) |
| 74 | { |
| 75 | if(axis == 0) |
| 76 | { |
| 77 | const T *src_row_ptr = src.data() + du * reduce_elems; |
| 78 | dst[du] = reduce_operation(src_row_ptr, reduce_elems, op); |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | ARM_COMPUTE_ERROR("Unsupported reduction axis"); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return dst; |
| 87 | } |
| 88 | |
| 89 | template SimpleTensor<float> reduction_operation(const SimpleTensor<float> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op); |
| 90 | } // namespace reference |
| 91 | } // namespace validation |
| 92 | } // namespace test |
| 93 | } // namespace arm_compute |