blob: fcd6226f07496b5a43c361091f8e1ceb27a80f39 [file] [log] [blame]
Georgios Pinitasd9769582017-08-03 10:19:40 +01001/*
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +01002 * Copyright (c) 2017-2018 ARM Limited.
Georgios Pinitasd9769582017-08-03 10:19:40 +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 */
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000024#include "L2NormalizeLayer.h"
Georgios Pinitasd9769582017-08-03 10:19:40 +010025#include "ReductionOperation.h"
26
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010027#include "tests/validation/Helpers.h"
Georgios Pinitasd9769582017-08-03 10:19:40 +010028
29#include <algorithm>
30#include <cmath>
31
32namespace arm_compute
33{
34namespace test
35{
36namespace validation
37{
38namespace reference
39{
40namespace
41{
42TensorShape 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
50template <typename T>
51SimpleTensor<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(src, get_output_shape(src.shape(), axis), axis, ReductionOperation::SUM_SQUARE);
58
59 // Compute reference
Michalis Spyrou5538d342018-11-14 08:10:13 +000060 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 Pinitasd9769582017-08-03 10:19:40 +010063
64 for(int du = 0; du < upper_dims; ++du)
65 {
Michalis Spyrou5538d342018-11-14 08:10:13 +000066 const T *src_row_ptr = src.data() + du * lower_dims;
67 T *dst_row_ptr = dst.data() + du * lower_dims;
68 switch(axis)
Georgios Pinitasd9769582017-08-03 10:19:40 +010069 {
Michalis Spyrou5538d342018-11-14 08:10:13 +000070 case 0:
Georgios Pinitasd9769582017-08-03 10:19:40 +010071 {
Michalis Spyrou5538d342018-11-14 08:10:13 +000072 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 Pinitasd9769582017-08-03 10:19:40 +010092 }
93 }
94
95 return dst;
96}
97
98template SimpleTensor<float> l2_normalize(const SimpleTensor<float> &src, unsigned int axis, float epsilon);
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +010099template SimpleTensor<half> l2_normalize(const SimpleTensor<half> &src, unsigned int axis, float epsilon);
Georgios Pinitasd9769582017-08-03 10:19:40 +0100100} // namespace reference
101} // namespace validation
102} // namespace test
103} // namespace arm_compute