blob: 114d38a21a388953eef700ebb4f70ac753820a06 [file] [log] [blame]
Sanghoon Leec8a85ba2017-11-29 11:23:14 +00001/*
Sanghoon Leed7ba5392017-12-13 11:28:50 +00002 * Copyright (c) 2017, 2018 ARM Limited.
Sanghoon Leec8a85ba2017-11-29 11:23:14 +00003 *
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_CONVOLUTION_FIXTURE
25#define ARM_COMPUTE_TEST_CONVOLUTION_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29#include "tests/AssetsLibrary.h"
30#include "tests/Globals.h"
31#include "tests/IAccessor.h"
32#include "tests/framework/Asserts.h"
33#include "tests/framework/Fixture.h"
Georgios Pinitasc7951962017-12-12 17:58:11 +000034#include "tests/validation/reference/Convolution.h"
Sanghoon Leec8a85ba2017-11-29 11:23:14 +000035
36#include <random>
37
38namespace arm_compute
39{
40namespace test
41{
42namespace validation
43{
Sanghoon Leed7ba5392017-12-13 11:28:50 +000044template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
Sanghoon Leec8a85ba2017-11-29 11:23:14 +000045class ConvolutionValidationFixture : public framework::Fixture
46{
Sanghoon Leed7ba5392017-12-13 11:28:50 +000047protected:
Sanghoon Leec8a85ba2017-11-29 11:23:14 +000048 template <typename...>
Sanghoon Leed7ba5392017-12-13 11:28:50 +000049 void setup(TensorShape shape, DataType data_type, BorderMode border_mode, const unsigned int width, const unsigned int height)
Sanghoon Leec8a85ba2017-11-29 11:23:14 +000050 {
51 std::mt19937 gen(library->seed());
52 std::uniform_int_distribution<uint8_t> distribution(0, 255);
53 const uint8_t constant_border_value = distribution(gen);
54
Sanghoon Leed7ba5392017-12-13 11:28:50 +000055 // Generate random scale value between 1 and 255.
56 std::uniform_int_distribution<uint8_t> distribution_scale(1, 255);
57 const uint32_t scale = distribution_scale(gen);
Sanghoon Leec8a85ba2017-11-29 11:23:14 +000058
Sanghoon Leed7ba5392017-12-13 11:28:50 +000059 ARM_COMPUTE_ERROR_ON(3 != width && 5 != width && 7 != width && 9 != width);
60 ARM_COMPUTE_ERROR_ON(3 != height && 5 != height && 7 != height && 9 != height);
Sanghoon Leec8a85ba2017-11-29 11:23:14 +000061
Sanghoon Leed7ba5392017-12-13 11:28:50 +000062 int16_t conv[width * height];
63 create_conv(conv);
64
65 _width = width;
66 _height = height;
67 _target = compute_target(shape, data_type, conv, scale, border_mode, constant_border_value);
68 _reference = compute_reference(shape, data_type, conv, scale, border_mode, constant_border_value);
Sanghoon Leec8a85ba2017-11-29 11:23:14 +000069 }
70
Sanghoon Leed7ba5392017-12-13 11:28:50 +000071 void
72 create_conv(int16_t *conv)
Sanghoon Leec8a85ba2017-11-29 11:23:14 +000073 {
74 std::mt19937 gen(library->seed());
75 std::uniform_int_distribution<int16_t> distribution_int16(-32768, 32767);
76
Sanghoon Leed7ba5392017-12-13 11:28:50 +000077 for(unsigned int i = 0; i < _width * _height; ++i)
Sanghoon Leec8a85ba2017-11-29 11:23:14 +000078 {
79 conv[i] = distribution_int16(gen);
80 }
81 }
82
83 template <typename U>
84 void fill(U &&tensor, int i)
85 {
86 library->fill_tensor_uniform(tensor, i);
87 }
88
Sanghoon Leed7ba5392017-12-13 11:28:50 +000089 SimpleTensor<T> compute_reference(const TensorShape &shape, DataType data_type, const int16_t *conv, uint32_t scale, BorderMode border_mode, uint8_t constant_border_value)
90 {
91 ARM_COMPUTE_ERROR_ON(data_type != DataType::U8);
92
93 // Create reference
94 SimpleTensor<T> src{ shape, data_type };
95
96 // Fill reference
97 fill(src, 0);
98
99 // Compute reference
100 return reference::convolution<T>(src, conv, scale, border_mode, constant_border_value, _width, _height);
101 }
102
103 virtual TensorType compute_target(const TensorShape &shape, DataType data_type, const int16_t *conv, uint32_t scale, BorderMode border_mode, uint8_t constant_border_value) = 0;
104
105 BorderMode _border_mode{};
106 TensorType _target{};
107 SimpleTensor<T> _reference{};
108 unsigned int _width{};
109 unsigned int _height{};
110};
111
112template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
113class ConvolutionSquareValidationFixture : public ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>
114{
115public:
116 template <typename...>
117 void setup(TensorShape shape, DataType data_type, BorderMode border_mode, const unsigned int width)
118 {
119 ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, border_mode, width, width);
120 }
121
122protected:
Sanghoon Leec8a85ba2017-11-29 11:23:14 +0000123 TensorType compute_target(const TensorShape &shape, DataType data_type, const int16_t *conv, uint32_t scale, BorderMode border_mode, uint8_t constant_border_value)
124 {
125 // Create tensors
126 TensorType src = create_tensor<TensorType>(shape, data_type);
127 TensorType dst = create_tensor<TensorType>(shape, data_type);
128
129 // Create and configure function
130 FunctionType convolution;
131 convolution.configure(&src, &dst, conv, scale, border_mode, constant_border_value);
132
133 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
134 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
135
136 // Allocate tensors
137 src.allocator()->allocate();
138 dst.allocator()->allocate();
139
140 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
141 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
142
143 // Fill tensors
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000144 this->fill(AccessorType(src), 0);
145 this->fill(AccessorType(dst), 1);
Sanghoon Leec8a85ba2017-11-29 11:23:14 +0000146
147 // Compute function
148 convolution.run();
149
150 return dst;
151 }
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000152};
Sanghoon Leec8a85ba2017-11-29 11:23:14 +0000153
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000154template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
155class ConvolutionRectangleValidationFixture : public ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>
156{
157public:
158 template <typename...>
159 void setup(TensorShape shape, DataType data_type, BorderMode border_mode, const unsigned int width, const unsigned int height)
Sanghoon Leec8a85ba2017-11-29 11:23:14 +0000160 {
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000161 ConvolutionValidationFixture<TensorType, AccessorType, FunctionType, T>::setup(shape, data_type, border_mode, width, height);
Sanghoon Leec8a85ba2017-11-29 11:23:14 +0000162 }
163
Sanghoon Leed7ba5392017-12-13 11:28:50 +0000164protected:
165 TensorType compute_target(const TensorShape &shape, DataType data_type, const int16_t *conv, uint32_t scale, BorderMode border_mode, uint8_t constant_border_value)
166 {
167 // Create tensors
168 TensorType src = create_tensor<TensorType>(shape, data_type);
169 TensorType dst = create_tensor<TensorType>(shape, data_type);
170
171 // Create and configure function
172 FunctionType convolution;
173 convolution.configure(&src, &dst, conv, this->_width, this->_height, scale, border_mode, constant_border_value);
174
175 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
176 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
177
178 // Allocate tensors
179 src.allocator()->allocate();
180 dst.allocator()->allocate();
181
182 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
183 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
184
185 // Fill tensors
186 this->fill(AccessorType(src), 0);
187 this->fill(AccessorType(dst), 1);
188
189 // Compute function
190 convolution.run();
191
192 return dst;
193 }
Sanghoon Leec8a85ba2017-11-29 11:23:14 +0000194};
195} // namespace validation
196} // namespace test
197} // namespace arm_compute
198#endif /* ARM_COMPUTE_TEST_CONVOLUTION_FIXTURE */