blob: 696d14fbbb5e5c5ab75d796fd2db324643f74828 [file] [log] [blame]
Moritz Pflanzer6db73ce2017-07-19 10:18:42 +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#ifndef ARM_COMPUTE_TEST_NORMALIZATION_LAYER_FIXTURE
25#define ARM_COMPUTE_TEST_NORMALIZATION_LAYER_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "arm_compute/runtime/Tensor.h"
Moritz Pflanzer6db73ce2017-07-19 10:18:42 +010030#include "tests/AssetsLibrary.h"
31#include "tests/Globals.h"
32#include "tests/IAccessor.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010033#include "tests/framework/Asserts.h"
34#include "tests/framework/Fixture.h"
35#include "tests/validation/CPP/NormalizationLayer.h"
Moritz Pflanzer6db73ce2017-07-19 10:18:42 +010036
37#include <random>
38
39namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
45template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
46class NormalizationValidationFixedPointFixture : public framework::Fixture
47{
48public:
49 template <typename...>
50 void setup(TensorShape shape, NormType norm_type, int norm_size, float beta, DataType data_type, int fractional_bits)
51 {
52 _fractional_bits = fractional_bits;
53 NormalizationLayerInfo info(norm_type, norm_size, 5, beta);
54
55 _target = compute_target(shape, info, data_type, fractional_bits);
56 _reference = compute_reference(shape, info, data_type, fractional_bits);
57 }
58
59protected:
60 template <typename U>
61 void fill(U &&tensor)
62 {
63 if(_fractional_bits == 0)
64 {
65 library->fill_tensor_uniform(tensor, 0);
66 }
67 else
68 {
69 const int one_fixed = 1 << _fractional_bits;
70 std::uniform_int_distribution<> distribution(-one_fixed, one_fixed);
71 library->fill(tensor, distribution, 0);
72 }
73 }
74
75 TensorType compute_target(const TensorShape &shape, NormalizationLayerInfo info, DataType data_type, int fixed_point_position = 0)
76 {
77 // Create tensors
78 TensorType src = create_tensor<TensorType>(shape, data_type, 1, fixed_point_position);
79 TensorType dst = create_tensor<TensorType>(shape, data_type, 1, fixed_point_position);
80
81 // Create and configure function
82 FunctionType norm_layer;
83 norm_layer.configure(&src, &dst, info);
84
85 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
86 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
87
88 // Allocate tensors
89 src.allocator()->allocate();
90 dst.allocator()->allocate();
91
92 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
93 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
94
95 // Fill tensors
96 fill(AccessorType(src));
97
98 // Compute function
99 norm_layer.run();
100
101 return dst;
102 }
103
104 SimpleTensor<T> compute_reference(const TensorShape &shape, NormalizationLayerInfo info, DataType data_type, int fixed_point_position = 0)
105 {
106 // Create reference
107 SimpleTensor<T> src{ shape, data_type, 1, fixed_point_position };
108
109 // Fill reference
110 fill(src);
111
112 return reference::normalization_layer<T>(src, info);
113 }
114
115 TensorType _target{};
116 SimpleTensor<T> _reference{};
117 int _fractional_bits{};
118};
119
120template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
121class NormalizationValidationFixture : public NormalizationValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T>
122{
123public:
124 template <typename...>
125 void setup(TensorShape shape, NormType norm_type, int norm_size, float beta, DataType data_type)
126 {
127 NormalizationValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, norm_type, norm_size, beta, data_type, 0);
128 }
129};
130} // namespace validation
131} // namespace test
132} // namespace arm_compute
133#endif /* ARM_COMPUTE_TEST_NORMALIZATION_LAYER_FIXTURE */