1 /*
2 * Copyright (c) 2017-2019 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 "L2NormalizeLayer.h"
25 #include "ReductionOperation.h"
26
27 #include "tests/validation/Helpers.h"
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 {
get_output_shape(TensorShape shape,unsigned int axis)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>
l2_normalize(const SimpleTensor<T> & src,unsigned int axis,float epsilon)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
57 SimpleTensor<T> sum = reduction_operation<T, T>(src, get_output_shape(src.shape(), axis), axis, ReductionOperation::SUM_SQUARE);
58
59 // Compute reference
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);
63
64 for(int du = 0; du < upper_dims; ++du)
65 {
66 const T *src_row_ptr = src.data() + du * lower_dims;
67 T *dst_row_ptr = dst.data() + du * lower_dims;
68 switch(axis)
69 {
70 case 0:
71 {
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");
92 }
93 }
94
95 return dst;
96 }
97
98 template SimpleTensor<float> l2_normalize(const SimpleTensor<float> &src, unsigned int axis, float epsilon);
99 template SimpleTensor<half> l2_normalize(const SimpleTensor<half> &src, unsigned int axis, float epsilon);
100 } // namespace reference
101 } // namespace validation
102 } // namespace test
103 } // namespace arm_compute
104