blob: a8ba7dac551756a92caf7e00a63f955d2807a8c6 [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;
55 case DataType::F32:
56 return 1e-05;
57 default:
58 return 0.f;
59 }
60}
61
62/** Compute Neon normalization layer function.
63 *
64 * @param[in] shape Shape of the input and output tensors.
65 * @param[in] dt Data type of input and output tensors.
66 * @param[in] norm_info Normalization Layer information.
67 * @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).
68 *
69 * @return Computed output tensor.
70 */
71Tensor compute_normalization_layer(const TensorShape &shape, DataType dt, NormalizationLayerInfo norm_info, int fixed_point_position = 0)
72{
73 // Create tensors
Moritz Pflanzer94450f12017-06-30 12:48:43 +010074 Tensor src = create_tensor<Tensor>(shape, dt, 1, fixed_point_position);
75 Tensor dst = create_tensor<Tensor>(shape, dt, 1, fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076
77 // Create and configure function
78 NENormalizationLayer norm;
79 norm.configure(&src, &dst, norm_info);
80
81 // Allocate tensors
82 src.allocator()->allocate();
83 dst.allocator()->allocate();
84
85 BOOST_TEST(!src.info()->is_resizable());
86 BOOST_TEST(!dst.info()->is_resizable());
87
88 // Fill tensors
89 if(dt == DataType::QS8)
90 {
91 const int8_t one_fixed_point = 1 << fixed_point_position;
92 const int8_t minus_one_fixed_point = -one_fixed_point;
93 library->fill_tensor_uniform(NEAccessor(src), 0, minus_one_fixed_point, one_fixed_point);
94 }
95 else
96 {
97 library->fill_tensor_uniform(NEAccessor(src), 0);
98 }
99
100 // Compute function
101 norm.run();
102
103 return dst;
104}
105} // namespace
106
107#ifndef DOXYGEN_SKIP_THIS
108BOOST_AUTO_TEST_SUITE(NEON)
109BOOST_AUTO_TEST_SUITE(NormalizationLayer)
110
111BOOST_AUTO_TEST_SUITE(Float)
112BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
113BOOST_DATA_TEST_CASE(RunSmall,
114 SmallShapes() * DataType::F32 *NormalizationTypes() * boost::unit_test::data::xrange(3, 9, 2) * boost::unit_test::data::make({ 0.5f, 1.0f, 2.0f }),
115 shape, dt, norm_type, norm_size, beta)
116{
117 // Provide normalization layer information
118 NormalizationLayerInfo norm_info(norm_type, norm_size, 5, beta);
119
120 // Compute function
121 Tensor dst = compute_normalization_layer(shape, dt, norm_info);
122
123 // Compute reference
124 RawTensor ref_dst = Reference::compute_reference_normalization_layer(shape, dt, norm_info);
125
126 // Validate output
127 validate(NEAccessor(dst), ref_dst, normalization_layer_tolerance(DataType::F32));
128}
129BOOST_AUTO_TEST_SUITE_END()
130
131BOOST_AUTO_TEST_SUITE(Quantized)
132BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
133BOOST_DATA_TEST_CASE(RunSmall,
134 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 })),
135 shape, dt, norm_type, norm_size, fixed_point_position, beta)
136{
137 // Provide normalization layer information
138 NormalizationLayerInfo norm_info(norm_type, norm_size, 5, beta, 1.f);
139
140 // Compute function
141 Tensor dst = compute_normalization_layer(shape, dt, norm_info, fixed_point_position);
142
143 // Compute reference
144 RawTensor ref_dst = Reference::compute_reference_normalization_layer(shape, dt, norm_info, fixed_point_position);
145
146 // Validate output
147 validate(NEAccessor(dst), ref_dst, normalization_layer_tolerance(DataType::QS8));
148}
149BOOST_AUTO_TEST_SUITE_END()
150
151BOOST_AUTO_TEST_SUITE_END()
152BOOST_AUTO_TEST_SUITE_END()
Anthony Barbierac69aa12017-07-03 17:39:37 +0100153#endif /* DOXYGEN_SKIP_THIS */