blob: 60c2646b77a7d1f0c6b048935ee864ebe0f56ded [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +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 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010024#include "NEON/NEAccessor.h"
25#include "TypePrinter.h"
Moritz Pflanzer94450f12017-06-30 12:48:43 +010026#include "tests/Globals.h"
27#include "tests/Utils.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "validation/Datasets.h"
29#include "validation/Reference.h"
30#include "validation/Validation.h"
31
32#include "arm_compute/runtime/NEON/functions/NENormalizationLayer.h"
33
34#include <random>
35
36using namespace arm_compute;
37using namespace arm_compute::test;
38using namespace arm_compute::test::neon;
39using namespace arm_compute::test::validation;
40
41namespace
42{
43/** Define tolerance of the normalization layer depending on values data type.
44 *
45 * @param[in] dt Data type of the tensors' values.
46 *
47 * @return Tolerance depending on the data type.
48 */
49float normalization_layer_tolerance(DataType dt)
50{
51 switch(dt)
52 {
53 case DataType::QS8:
54 return 2.0f;
Pablo Tellodf246182017-07-03 16:25:09 +010055 case DataType::F16:
56 return 0.001f;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057 case DataType::F32:
58 return 1e-05;
59 default:
60 return 0.f;
61 }
62}
63
64/** Compute Neon normalization layer function.
65 *
66 * @param[in] shape Shape of the input and output tensors.
67 * @param[in] dt Data type of input and output tensors.
68 * @param[in] norm_info Normalization Layer information.
69 * @param[in] fixed_point_position (Optional) Fixed point position that expresses the number of bits for the fractional part of the number when the tensor's data type is QS8 or QS16 (default = 0).
70 *
71 * @return Computed output tensor.
72 */
73Tensor compute_normalization_layer(const TensorShape &shape, DataType dt, NormalizationLayerInfo norm_info, int fixed_point_position = 0)
74{
75 // Create tensors
Moritz Pflanzer94450f12017-06-30 12:48:43 +010076 Tensor src = create_tensor<Tensor>(shape, dt, 1, fixed_point_position);
77 Tensor dst = create_tensor<Tensor>(shape, dt, 1, fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078
79 // Create and configure function
80 NENormalizationLayer norm;
81 norm.configure(&src, &dst, norm_info);
82
83 // Allocate tensors
84 src.allocator()->allocate();
85 dst.allocator()->allocate();
86
87 BOOST_TEST(!src.info()->is_resizable());
88 BOOST_TEST(!dst.info()->is_resizable());
89
90 // Fill tensors
91 if(dt == DataType::QS8)
92 {
93 const int8_t one_fixed_point = 1 << fixed_point_position;
94 const int8_t minus_one_fixed_point = -one_fixed_point;
95 library->fill_tensor_uniform(NEAccessor(src), 0, minus_one_fixed_point, one_fixed_point);
96 }
97 else
98 {
99 library->fill_tensor_uniform(NEAccessor(src), 0);
100 }
101
102 // Compute function
103 norm.run();
104
105 return dst;
106}
107} // namespace
108
109#ifndef DOXYGEN_SKIP_THIS
110BOOST_AUTO_TEST_SUITE(NEON)
111BOOST_AUTO_TEST_SUITE(NormalizationLayer)
112
Pablo Tellodf246182017-07-03 16:25:09 +0100113#ifdef ARM_COMPUTE_ENABLE_FP16
114BOOST_AUTO_TEST_SUITE(Float16)
115BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
116BOOST_DATA_TEST_CASE(RunSmall,
117 SmallShapes() * DataType::F16 *NormalizationTypes() * boost::unit_test::data::xrange(3, 9, 2) * boost::unit_test::data::make({ 0.5f, 1.0f, 2.0f }),
118 shape, dt, norm_type, norm_size, beta)
119{
120 // Provide normalization layer information
121 NormalizationLayerInfo norm_info(norm_type, norm_size, 5, beta);
122
123 // Compute function
124 Tensor dst = compute_normalization_layer(shape, dt, norm_info);
125
126 // Compute reference
127 RawTensor ref_dst = Reference::compute_reference_normalization_layer(shape, dt, norm_info);
128
129 // Validate output
130 validate(NEAccessor(dst), ref_dst, normalization_layer_tolerance(DataType::F16));
131}
132
133BOOST_AUTO_TEST_SUITE_END()
134#endif /* ARM_COMPUTE_ENABLE_FP16 */
135
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136BOOST_AUTO_TEST_SUITE(Float)
137BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
138BOOST_DATA_TEST_CASE(RunSmall,
139 SmallShapes() * DataType::F32 *NormalizationTypes() * boost::unit_test::data::xrange(3, 9, 2) * boost::unit_test::data::make({ 0.5f, 1.0f, 2.0f }),
140 shape, dt, norm_type, norm_size, beta)
141{
142 // Provide normalization layer information
143 NormalizationLayerInfo norm_info(norm_type, norm_size, 5, beta);
144
145 // Compute function
146 Tensor dst = compute_normalization_layer(shape, dt, norm_info);
147
148 // Compute reference
149 RawTensor ref_dst = Reference::compute_reference_normalization_layer(shape, dt, norm_info);
150
151 // Validate output
152 validate(NEAccessor(dst), ref_dst, normalization_layer_tolerance(DataType::F32));
153}
154BOOST_AUTO_TEST_SUITE_END()
155
156BOOST_AUTO_TEST_SUITE(Quantized)
157BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
158BOOST_DATA_TEST_CASE(RunSmall,
159 SmallShapes() * DataType::QS8 *NormalizationTypes() * boost::unit_test::data::xrange(3, 7, 2) * (boost::unit_test::data::xrange(1, 6) * boost::unit_test::data::make({ 0.5f, 1.0f, 2.0f })),
160 shape, dt, norm_type, norm_size, fixed_point_position, beta)
161{
162 // Provide normalization layer information
163 NormalizationLayerInfo norm_info(norm_type, norm_size, 5, beta, 1.f);
164
165 // Compute function
166 Tensor dst = compute_normalization_layer(shape, dt, norm_info, fixed_point_position);
167
168 // Compute reference
169 RawTensor ref_dst = Reference::compute_reference_normalization_layer(shape, dt, norm_info, fixed_point_position);
170
171 // Validate output
172 validate(NEAccessor(dst), ref_dst, normalization_layer_tolerance(DataType::QS8));
173}
174BOOST_AUTO_TEST_SUITE_END()
175
176BOOST_AUTO_TEST_SUITE_END()
177BOOST_AUTO_TEST_SUITE_END()
Anthony Barbierac69aa12017-07-03 17:39:37 +0100178#endif /* DOXYGEN_SKIP_THIS */