Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 Arm Limited. |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +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 | */ |
Giorgio Arena | 04a8f8c | 2017-11-23 11:45:24 +0000 | [diff] [blame] | 24 | #include "L2NormalizeLayer.h" |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 25 | #include "ReductionOperation.h" |
| 26 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 27 | #include "tests/validation/Helpers.h" |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 28 | |
| 29 | #include <algorithm> |
| 30 | #include <cmath> |
| 31 | |
| 32 | namespace arm_compute |
| 33 | { |
| 34 | namespace test |
| 35 | { |
| 36 | namespace validation |
| 37 | { |
| 38 | namespace reference |
| 39 | { |
| 40 | namespace |
| 41 | { |
| 42 | TensorShape get_output_shape(TensorShape shape, unsigned int axis) |
| 43 | { |
| 44 | TensorShape output_shape(shape); |
| 45 | output_shape.set(axis, 1); |
| 46 | return output_shape; |
| 47 | } |
| 48 | } // namespace |
| 49 | |
| 50 | template <typename T> |
| 51 | SimpleTensor<T> l2_normalize(const SimpleTensor<T> &src, unsigned int axis, float epsilon) |
| 52 | { |
| 53 | // Create reference |
| 54 | SimpleTensor<T> dst{ src.shape(), src.data_type() }; |
| 55 | |
| 56 | // Reduce across given axis |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 57 | SimpleTensor<T> sum = reduction_operation<T, T>(src, get_output_shape(src.shape(), axis), axis, ReductionOperation::SUM_SQUARE); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 58 | |
| 59 | // Compute reference |
Michalis Spyrou | 5538d34 | 2018-11-14 08:10:13 +0000 | [diff] [blame] | 60 | const int upper_dims = src.shape().total_size_upper(axis + 1); |
| 61 | const int lower_dims = src.shape().total_size_lower(axis + 1); |
| 62 | const int lower_dims_sum = sum.shape().total_size_lower(axis + 1); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 63 | |
| 64 | for(int du = 0; du < upper_dims; ++du) |
| 65 | { |
Michalis Spyrou | 5538d34 | 2018-11-14 08:10:13 +0000 | [diff] [blame] | 66 | const T *src_row_ptr = src.data() + du * lower_dims; |
| 67 | T *dst_row_ptr = dst.data() + du * lower_dims; |
| 68 | switch(axis) |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 69 | { |
Michalis Spyrou | 5538d34 | 2018-11-14 08:10:13 +0000 | [diff] [blame] | 70 | case 0: |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 71 | { |
Michalis Spyrou | 5538d34 | 2018-11-14 08:10:13 +0000 | [diff] [blame] | 72 | const int elems = src.shape()[0]; |
| 73 | const T normalization_value = sqrt(std::max(sum[du], static_cast<T>(epsilon))); |
| 74 | std::transform(src_row_ptr, src_row_ptr + elems, dst_row_ptr, [normalization_value](T val) |
| 75 | { |
| 76 | return val / normalization_value; |
| 77 | }); |
| 78 | } |
| 79 | break; |
| 80 | case 1: |
| 81 | case 2: |
| 82 | { |
| 83 | for(int ld = 0; ld < lower_dims; ++ld) |
| 84 | { |
| 85 | const T normalization_value = sqrt(std::max(sum[ld % lower_dims_sum + du * lower_dims_sum], static_cast<T>(epsilon))); |
| 86 | dst_row_ptr[ld] = src_row_ptr[ld] / normalization_value; |
| 87 | } |
| 88 | } |
| 89 | break; |
| 90 | default: |
| 91 | ARM_COMPUTE_ERROR("Axis not supported"); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | return dst; |
| 96 | } |
| 97 | |
| 98 | template SimpleTensor<float> l2_normalize(const SimpleTensor<float> &src, unsigned int axis, float epsilon); |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 99 | template SimpleTensor<half> l2_normalize(const SimpleTensor<half> &src, unsigned int axis, float epsilon); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 100 | } // namespace reference |
| 101 | } // namespace validation |
| 102 | } // namespace test |
| 103 | } // namespace arm_compute |