blob: a709157c7b2711dafb96a79506e9491e4d527aaa [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>
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010044class DirectConvolutionValidationFixedPointFixture : public framework::Fixture
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010045{
46public:
47 template <typename...>
48 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)
49 {
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010050 _fractional_bits = fractional_bits;
51 _data_type = data_type;
52
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010053 const TensorShape weights_shape(kernel_size, kernel_size, input_shape.z(), num_kernels);
54 const TensorShape bias_shape(num_kernels);
55 const PadStrideInfo info(stride_x, stride_y, pad_x, pad_y, DimensionRoundingType::FLOOR);
56 const TensorShape output_shape = get_output_shape(input_shape, weights_shape, info);
57
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010058 _target = compute_target(input_shape, weights_shape, bias_shape, output_shape, info, data_type, fractional_bits);
59 _reference = compute_reference(input_shape, weights_shape, bias_shape, output_shape, info, data_type, fractional_bits);
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010060 }
61
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010062protected:
63 template <typename U>
64 void fill(U &&tensor, int i)
65 {
66 switch(tensor.data_type())
67 {
68 case DataType::F16:
69 case DataType::F32:
70 {
71 std::uniform_real_distribution<> distribution(-1.0f, 1.0f);
72 library->fill(tensor, distribution, i);
73 break;
74 }
75 default:
76 library->fill_tensor_uniform(tensor, i);
77 }
78 }
79
80 TensorType compute_target(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info,
81 DataType data_type, int fixed_point_position)
82 {
83 // Create tensors
84 TensorType src = create_tensor<TensorType>(input_shape, data_type, 1, fixed_point_position);
85 TensorType weights = create_tensor<TensorType>(weights_shape, data_type, 1, fixed_point_position);
86 TensorType bias = create_tensor<TensorType>(bias_shape, data_type, 1, fixed_point_position);
87 TensorType dst = create_tensor<TensorType>(output_shape, data_type, 1, fixed_point_position);
88
89 // Create and configure function
90 FunctionType conv;
91 conv.configure(&src, &weights, &bias, &dst, info);
92
93 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
94 ARM_COMPUTE_EXPECT(weights.info()->is_resizable(), framework::LogLevel::ERRORS);
95 ARM_COMPUTE_EXPECT(bias.info()->is_resizable(), framework::LogLevel::ERRORS);
96 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
97
98 // Allocate tensors
99 src.allocator()->allocate();
100 weights.allocator()->allocate();
101 bias.allocator()->allocate();
102 dst.allocator()->allocate();
103
104 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
105 ARM_COMPUTE_EXPECT(!weights.info()->is_resizable(), framework::LogLevel::ERRORS);
106 ARM_COMPUTE_EXPECT(!bias.info()->is_resizable(), framework::LogLevel::ERRORS);
107 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
108
109 // Fill tensors
110 fill(AccessorType(src), 0);
111 fill(AccessorType(weights), 1);
112 fill(AccessorType(bias), 2);
113
114 // Compute NEConvolutionLayer function
115 conv.run();
116
117 return dst;
118 }
119
120 SimpleTensor<T> compute_reference(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, const PadStrideInfo &info,
121 DataType data_type, int fixed_point_position)
122 {
123 // Create reference
124 SimpleTensor<T> src{ input_shape, data_type, 1, fixed_point_position };
125 SimpleTensor<T> weights{ weights_shape, data_type, 1, fixed_point_position };
126 SimpleTensor<T> bias{ bias_shape, data_type, 1, fixed_point_position };
127
128 // Fill reference
129 fill(src, 0);
130 fill(weights, 1);
131 fill(bias, 2);
132
133 return reference::convolution_layer<T>(src, weights, bias, output_shape, info);
134 }
135
136 TensorType _target{};
137 SimpleTensor<T> _reference{};
138 int _fractional_bits{};
139 DataType _data_type{};
140
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100141private:
142 TensorShape get_output_shape(TensorShape in_shape, TensorShape kernel_shape, const PadStrideInfo &info)
143 {
144 TensorShape out_shape(in_shape);
145 const std::pair<unsigned int, unsigned int> scaled_dims = scaled_dimensions(in_shape.x(),
146 in_shape.y(),
147 kernel_shape.x(),
148 kernel_shape.y(),
149 info);
150 out_shape.set(0, scaled_dims.first);
151 out_shape.set(1, scaled_dims.second);
152 out_shape.set(2, kernel_shape[3]);
153 return out_shape;
154 }
155};
156
157template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
158class DirectConvolutionValidationFixture : public DirectConvolutionValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T>
159{
160public:
161 template <typename...>
162 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)
163 {
164 DirectConvolutionValidationFixedPointFixture<TensorType, AccessorType, FunctionType, T>::setup(input_shape, stride_x, stride_y, pad_x, pad_y, kernel_size, num_kernels, data_type, 0);
165 }
166};
167} // namespace validation
168} // namespace test
169} // namespace arm_compute