blob: 8bf6ea26a8f1071d6a6a25ced09a1ec22d33b1d2 [file] [log] [blame]
Sanghoon Leec8a85ba2017-11-29 11:23:14 +00001/*
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#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{
44template <typename TensorType, typename AccessorType, typename FunctionType, typename T, const unsigned int filter_size>
45class ConvolutionValidationFixture : public framework::Fixture
46{
47public:
48 template <typename...>
49 void setup(TensorShape shape, DataType data_type, BorderMode border_mode)
50 {
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
55 // Generate random scale value between 0 and 255.
56 const uint32_t scale = distribution(gen);
57
58 switch(filter_size)
59 {
60 case 3:
61 case 5:
62 case 7:
63 case 9:
64 int16_t conv[filter_size * filter_size];
65 create_conv(conv);
66
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);
69 break;
70 default:
71 ARM_COMPUTE_ERROR("Filter Size Not Supported");
72 }
73 }
74
75protected:
76 void create_conv(int16_t *conv)
77 {
78 std::mt19937 gen(library->seed());
79 std::uniform_int_distribution<int16_t> distribution_int16(-32768, 32767);
80
81 for(unsigned int i = 0; i < filter_size * filter_size; ++i)
82 {
83 conv[i] = distribution_int16(gen);
84 }
85 }
86
87 template <typename U>
88 void fill(U &&tensor, int i)
89 {
90 library->fill_tensor_uniform(tensor, i);
91 }
92
93 TensorType compute_target(const TensorShape &shape, DataType data_type, const int16_t *conv, uint32_t scale, BorderMode border_mode, uint8_t constant_border_value)
94 {
95 // Create tensors
96 TensorType src = create_tensor<TensorType>(shape, data_type);
97 TensorType dst = create_tensor<TensorType>(shape, data_type);
98
99 // Create and configure function
100 FunctionType convolution;
101 convolution.configure(&src, &dst, conv, scale, border_mode, constant_border_value);
102
103 ARM_COMPUTE_EXPECT(src.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 dst.allocator()->allocate();
109
110 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
111 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
112
113 // Fill tensors
114 fill(AccessorType(src), 0);
115 fill(AccessorType(dst), 1);
116
117 // Compute function
118 convolution.run();
119
120 return dst;
121 }
122
123 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)
124 {
125 ARM_COMPUTE_ERROR_ON(data_type != DataType::U8);
126
127 // Create reference
128 SimpleTensor<T> src{ shape, data_type };
129
130 // Fill reference
131 fill(src, 0);
132
133 // Compute reference
134 return reference::convolution<T>(src, conv, scale, border_mode, constant_border_value, filter_size);
135 }
136
137 BorderMode _border_mode{};
138 TensorType _target{};
139 SimpleTensor<T> _reference{};
140};
141} // namespace validation
142} // namespace test
143} // namespace arm_compute
144#endif /* ARM_COMPUTE_TEST_CONVOLUTION_FIXTURE */