blob: fba44008ba19d8b9c94ae43ae8da4a353553cef8 [file] [log] [blame]
Pablo Tello299025a2017-09-29 11:30:12 +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#ifndef ARM_COMPUTE_TEST_GEMMLOWP_FIXTURE
25#define ARM_COMPUTE_TEST_GEMMLOWP_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"
34#include "tests/validation/CPP/GEMMLowp.h"
35#include "tests/validation/Helpers.h"
36
37#include <random>
38
39namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
45template <typename TensorType, typename AccessorType, typename FunctionType>
46class GEMMLowpOffsetValidationFixture : public framework::Fixture
47{
48public:
49 template <typename...>
Gian Marcofa4cacd2017-10-18 17:05:02 +010050 void setup(TensorShape shape_a, TensorShape shape_b, TensorShape shape_c, int32_t a_offset, int32_t b_offset, int32_t c_offset, int32_t c_mult_int, int32_t out_shift, DataType data_type)
Pablo Tello299025a2017-09-29 11:30:12 +010051 {
Gian Marcofa4cacd2017-10-18 17:05:02 +010052 _target = compute_target(shape_a, shape_b, shape_c, a_offset, b_offset, c_offset, c_mult_int, out_shift, data_type);
53 _reference = compute_reference(shape_a, shape_b, shape_c, a_offset, b_offset, c_offset, c_mult_int, out_shift, data_type);
Pablo Tello299025a2017-09-29 11:30:12 +010054 }
55
56protected:
57 template <typename U>
58 void fill(U &&tensor, int i)
59 {
Pablo Tello6ff12a02017-11-02 16:09:35 +000060 ARM_COMPUTE_ERROR_ON(tensor.data_type() != DataType::S8);
Pablo Tello299025a2017-09-29 11:30:12 +010061 std::uniform_int_distribution<> distribution(0, 3);
62 library->fill(tensor, distribution, i);
63 }
64
65 TensorType compute_target(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c,
Gian Marcofa4cacd2017-10-18 17:05:02 +010066 int32_t a_offset, int32_t b_offset, int32_t c_offset, int32_t c_mult_int, int32_t out_shift, DataType data_type)
Pablo Tello299025a2017-09-29 11:30:12 +010067 {
68 // Create tensors
Gian Marcofa4cacd2017-10-18 17:05:02 +010069 TensorType a = create_tensor<TensorType>(shape_a, data_type, 1);
70 TensorType b = create_tensor<TensorType>(shape_b, data_type, 1);
71 TensorType c = create_tensor<TensorType>(shape_c, data_type, 1);
Pablo Tello299025a2017-09-29 11:30:12 +010072
73 // Create and configure function
74 FunctionType gemmlowp;
75 gemmlowp.configure(&a, &b, &c, a_offset, b_offset, c_offset, c_mult_int, out_shift);
76
77 ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS);
78 ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS);
79 ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS);
80
81 // Allocate tensors
82 a.allocator()->allocate();
83 b.allocator()->allocate();
84 c.allocator()->allocate();
85
86 ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS);
87 ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS);
88 ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS);
89
90 // Fill tensors
91 fill(AccessorType(a), 0);
92 fill(AccessorType(b), 1);
93 fill(AccessorType(c), 2);
94
95 // Compute GEMM function
96 gemmlowp.run();
97 return c;
98 }
99
Pablo Tello6ff12a02017-11-02 16:09:35 +0000100 SimpleTensor<int8_t> compute_reference(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c,
101 int32_t a_offset, int32_t b_offset, int32_t c_offset, int32_t c_mult_int, int32_t out_shift, DataType data_type)
Pablo Tello299025a2017-09-29 11:30:12 +0100102 {
103 // Create reference
Pablo Tello6ff12a02017-11-02 16:09:35 +0000104 SimpleTensor<int8_t> a{ shape_a, data_type, 1 };
105 SimpleTensor<int8_t> b{ shape_b, data_type, 1 };
106 SimpleTensor<int8_t> c{ shape_c, data_type, 1 };
Pablo Tello299025a2017-09-29 11:30:12 +0100107
108 // Fill reference
109 fill(a, 0);
110 fill(b, 1);
111 fill(c, 2);
112
Pablo Tello6ff12a02017-11-02 16:09:35 +0000113 return reference::gemmlowp<int8_t>(a, b, c, a_offset, b_offset, c_offset, c_mult_int, out_shift);
Pablo Tello299025a2017-09-29 11:30:12 +0100114 }
115
Pablo Tello6ff12a02017-11-02 16:09:35 +0000116 TensorType _target{};
117 SimpleTensor<int8_t> _reference{};
Pablo Tello299025a2017-09-29 11:30:12 +0100118};
119
Pablo Tellobf2fb952017-09-29 16:43:25 +0100120template <typename TensorType, typename AccessorType, typename FunctionType>
Pablo Tello6ff12a02017-11-02 16:09:35 +0000121class GEMMLowpMatrixMultiplyValidationFixture : public framework::Fixture
Pablo Tellobf2fb952017-09-29 16:43:25 +0100122{
123public:
124 template <typename...>
125 void setup(size_t m, size_t n, size_t k)
126 {
127 const TensorShape shape_a(k, m);
128 const TensorShape shape_b(n, k);
129 const TensorShape shape_c(n, m);
130 _target = compute_target(shape_a, shape_b, shape_c);
131 _reference = compute_reference(shape_a, shape_b, shape_c);
132 }
133
134protected:
135 template <typename U>
136 void fill(U &&tensor, int i, int lo, int hi)
137 {
138 std::uniform_int_distribution<> distribution(lo, hi);
139 library->fill(tensor, distribution, i);
140 }
141
142 TensorType compute_target(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c)
143 {
144 // Create tensors
Pablo Tello6ff12a02017-11-02 16:09:35 +0000145 TensorType a = create_tensor<TensorType>(shape_a, DataType::S8, 1);
146 TensorType b = create_tensor<TensorType>(shape_b, DataType::S8, 1);
147 TensorType c = create_tensor<TensorType>(shape_c, DataType::S32, 1);
Pablo Tellobf2fb952017-09-29 16:43:25 +0100148
149 // Create and configure function
150 FunctionType gemmlowp;
151 gemmlowp.configure(&a, &b, &c);
152
153 ARM_COMPUTE_EXPECT(a.info()->is_resizable(), framework::LogLevel::ERRORS);
154 ARM_COMPUTE_EXPECT(b.info()->is_resizable(), framework::LogLevel::ERRORS);
155 ARM_COMPUTE_EXPECT(c.info()->is_resizable(), framework::LogLevel::ERRORS);
156
157 // Allocate tensors
158 a.allocator()->allocate();
159 b.allocator()->allocate();
160 c.allocator()->allocate();
161
162 ARM_COMPUTE_EXPECT(!a.info()->is_resizable(), framework::LogLevel::ERRORS);
163 ARM_COMPUTE_EXPECT(!b.info()->is_resizable(), framework::LogLevel::ERRORS);
164 ARM_COMPUTE_EXPECT(!c.info()->is_resizable(), framework::LogLevel::ERRORS);
165
166 // Fill tensors
Pablo Tello6ff12a02017-11-02 16:09:35 +0000167 fill(AccessorType(a), 0, -128, 127);
168 fill(AccessorType(b), 1, -128, 127);
Pablo Tellobf2fb952017-09-29 16:43:25 +0100169 fill(AccessorType(c), 2, 0, 0);
170
171 // Compute GEMM function
172 gemmlowp.run();
173 return c;
174 }
175
Pablo Tello6ff12a02017-11-02 16:09:35 +0000176 SimpleTensor<int32_t> compute_reference(const TensorShape &shape_a, const TensorShape &shape_b, const TensorShape &shape_c)
Pablo Tellobf2fb952017-09-29 16:43:25 +0100177 {
178 // Create reference
Pablo Tello6ff12a02017-11-02 16:09:35 +0000179 SimpleTensor<int8_t> a{ shape_a, DataType::S8, 1 };
180 SimpleTensor<int8_t> b{ shape_b, DataType::S8, 1 };
181 SimpleTensor<int32_t> c{ shape_c, DataType::S32, 1 };
Pablo Tellobf2fb952017-09-29 16:43:25 +0100182
183 // Fill reference
Pablo Tello6ff12a02017-11-02 16:09:35 +0000184 fill(a, 0, -128, 127);
185 fill(b, 1, -128, 127);
Pablo Tellobf2fb952017-09-29 16:43:25 +0100186 fill(c, 2, 0, 0);
187
188 return reference::gemmlowp(a, b, c);
189 }
190
Pablo Tello6ff12a02017-11-02 16:09:35 +0000191 TensorType _target{};
192 SimpleTensor<int32_t> _reference{};
Pablo Tellobf2fb952017-09-29 16:43:25 +0100193};
194
Pablo Tello299025a2017-09-29 11:30:12 +0100195} // namespace validation
196} // namespace test
197} // namespace arm_compute
198#endif /* ARM_COMPUTE_TEST_GEMMLOWP_FIXTURE */