blob: 4fb4d57eb4638bf9bb7300f2d0a1f24502528b5a [file] [log] [blame]
Georgios Pinitasd9769582017-08-03 10:19:40 +01001/*
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 "L2Normalize.h"
25#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
60 const int elems = src.shape()[axis];
61 const int upper_dims = src.shape().total_size_upper(axis + 1);
62
63 for(int du = 0; du < upper_dims; ++du)
64 {
65 if(axis == 0)
66 {
67 const T *src_row_ptr = src.data() + du * elems;
68 T *dst_row_ptr = dst.data() + du * elems;
69 const T normalization_value = std::sqrt(std::max(sum[du], epsilon));
70 std::transform(src_row_ptr, src_row_ptr + elems, dst_row_ptr, [normalization_value](T val)
71 {
72 return val / normalization_value;
73 });
74 }
75 else
76 {
77 ARM_COMPUTE_ERROR("Unsupported normalization axis");
78 }
79 }
80
81 return dst;
82}
83
84template SimpleTensor<float> l2_normalize(const SimpleTensor<float> &src, unsigned int axis, float epsilon);
85} // namespace reference
86} // namespace validation
87} // namespace test
88} // namespace arm_compute