blob: e30265715861daef9c3d47132e5af2bd86198620 [file] [log] [blame]
Moritz Pflanzerb3d25792017-07-26 11:49:37 +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 "arm_compute/core/TensorShape.h"
25#include "arm_compute/core/Types.h"
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010026#include "tests/AssetsLibrary.h"
27#include "tests/Globals.h"
28#include "tests/IAccessor.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010029#include "tests/framework/Asserts.h"
30#include "tests/framework/Fixture.h"
31#include "tests/validation/CPP/ConvolutionLayer.h"
32#include "tests/validation/Helpers.h"
33#include "tests/validation/fixtures/ConvolutionLayerFixture.h"
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010034
35#include <random>
36
37namespace arm_compute
38{
39namespace test
40{
41namespace validation
42{
43template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Chunosovd621bca2017-11-03 17:33:15 +070044class DirectConvolutionValidationGenericFixture : public framework::Fixture
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010045{
46public:
47 template <typename...>
Chunosovd621bca2017-11-03 17:33:15 +070048 void setup(TensorShape input_shape, int stride_x, int stride_y, int pad_x, int pad_y, unsigned int kernel_size, unsigned int num_kernels,
49 DataType data_type, int fractional_bits, QuantizationInfo quantization_info)
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010050 {
Chunosovd621bca2017-11-03 17:33:15 +070051 _fractional_bits = fractional_bits;
52 _quantization_info = quantization_info;
53 _data_type = data_type;
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010054
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010055 const TensorShape weights_shape(kernel_size, kernel_size, input_shape.z(), num_kernels);
56 const TensorShape bias_shape(num_kernels);
57 const PadStrideInfo info(stride_x, stride_y, pad_x, pad_y, DimensionRoundingType::FLOOR);
58 const TensorShape output_shape = get_output_shape(input_shape, weights_shape, info);
59
Chunosovd621bca2017-11-03 17:33:15 +070060 _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, data_type, fractional_bits, quantization_info);
61 _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, data_type, fractional_bits, quantization_info);
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010062 }
63
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010064protected:
65 template <typename U>
66 void fill(U &&tensor, int i)
67 {
68 switch(tensor.data_type())
69 {
Chunosovd621bca2017-11-03 17:33:15 +070070 case DataType::QASYMM8:
71 {
72 std::uniform_int_distribution<uint8_t> distribution(0, 10);
73 library->fill(tensor, distribution, i);
74 break;
75 }
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010076 case DataType::F16:
77 case DataType::F32:
78 {
79 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
80 library->fill(tensor, distribution, i);
81 break;
82 }
83 default:
84 library->fill_tensor_uniform(tensor, i);
85 }
86 }
87
88 TensorType compute_target(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info,
Chunosovd621bca2017-11-03 17:33:15 +070089 DataType data_type, int fixed_point_position, QuantizationInfo quantization_info)
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010090 {
91 // Create tensors
Chunosovd621bca2017-11-03 17:33:15 +070092 TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, fixed_point_position, quantization_info);
93 TensorType weights = create_tensor<TensorType>(weights_shape, data_type, 1, fixed_point_position, quantization_info);
94 TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1, fixed_point_position, quantization_info);
95 TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, fixed_point_position, quantization_info);
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010096
97 // Create and configure function
98 FunctionType conv;
99 conv.configure(&src, &weights, &bias, &dst, info);
100
101 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
102 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
103 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
104 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
105
106 // Allocate tensors
107 src.allocator()->allocate();
108 weights.allocator()->allocate();
109 bias.allocator()->allocate();
110 dst.allocator()->allocate();
111
112 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
113 ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS);
114 ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS);
115 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
116
117 // Fill tensors
118 fill(AccessorType(src), 0);
119 fill(AccessorType(weights), 1);
120 fill(AccessorType(bias), 2);
121
122 // Compute NEConvolutionLayer function
123 conv.run();
124
125 return dst;
126 }
127
128 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info,
Chunosovd621bca2017-11-03 17:33:15 +0700129 DataType data_type, int fixed_point_position, QuantizationInfo quantization_info)
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100130 {
131 // Create reference
Chunosovd621bca2017-11-03 17:33:15 +0700132 SimpleTensor<T> src{ input_shape, data_type, 1, fixed_point_position, quantization_info };
133 SimpleTensor<T> weights{ weights_shape, data_type, 1, fixed_point_position, quantization_info };
134 SimpleTensor<T> bias{ bias_shape, data_type, 1, fixed_point_position, quantization_info };
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100135
136 // Fill reference
137 fill(src, 0);
138 fill(weights, 1);
139 fill(bias, 2);
140
141 return reference::convolution_layer<T>(src, weights, bias, output_shape, info);
142 }
143
Chunosovd621bca2017-11-03 17:33:15 +0700144 TensorType _target{};
145 SimpleTensor<T> _reference{};
146 int _fractional_bits{};
147 QuantizationInfo _quantization_info{};
148 DataType _data_type{};
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100149
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100150private:
151 TensorShape get_output_shape(TensorShape in_shape, TensorShape kernel_shape, const PadStrideInfo &info)
152 {
153 TensorShape out_shape(in_shape);
154 const std::pair<unsigned int, unsigned int> scaled_dims = scaled_dimensions(in_shape.x(),
155 in_shape.y(),
156 kernel_shape.x(),
157 kernel_shape.y(),
158 info);
159 out_shape.set(0, scaled_dims.first);
160 out_shape.set(1, scaled_dims.second);
161 out_shape.set(2, kernel_shape[3]);
162 return out_shape;
163 }
164};
165
166template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Chunosovd621bca2017-11-03 17:33:15 +0700167class DirectConvolutionValidationFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100168{
169public:
170 template <typename...>
171 void setup(TensorShape input_shape, int stride_x, int stride_y, int pad_x, int pad_y, unsigned int kernel_size, unsigned int num_kernels, DataType data_type)
172 {
Chunosovd621bca2017-11-03 17:33:15 +0700173 DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, stride_x, stride_y, pad_x, pad_y, kernel_size, num_kernels, data_type, 0, QuantizationInfo());
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100174 }
175};
Chunosovd621bca2017-11-03 17:33:15 +0700176
177template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
178class DirectConvolutionValidationFixedPointFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
179{
180public:
181 template <typename...>
182 void setup(TensorShape input_shape, int stride_x, int stride_y, int pad_x, int pad_y, unsigned int kernel_size, unsigned int num_kernels, DataType data_type, int fractional_bits)
183 {
184 DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, stride_x, stride_y, pad_x, pad_y, kernel_size, num_kernels, data_type, fractional_bits,
185 QuantizationInfo());
186 }
187};
188
189template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
190class DirectConvolutionValidationQuantizedFixture : public DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>
191{
192public:
193 template <typename...>
194 void setup(TensorShape input_shape, int stride_x, int stride_y, int pad_x, int pad_y, unsigned int kernel_size, unsigned int num_kernels, DataType data_type, QuantizationInfo quantization_info)
195 {
196 DirectConvolutionValidationGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, stride_x, stride_y, pad_x, pad_y, kernel_size, num_kernels, data_type, 0, quantization_info);
197 }
198};
199
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100200} // namespace validation
201} // namespace test
202} // namespace arm_compute